[英]How do I add child class objects to a vector of their parent class (C++)?
我有各種形狀類,包括矩形,圓形,正方形,五邊形等,都是Shape類的所有子類,並且所有這些類都有一個getArea()方法來返回其面積。 我正在嘗試制作一個Shape的矢量,可以將所有這些形狀的面積相加,以便可以使用一個函數來計算總面積。 這是我遇到麻煩的代碼的一部分。 我還沒有完成getTotalArea方法的編寫,但這很容易。 我只需要知道如何命名該方法(getTotalArea方法)以及要使用的引用或指針。 同樣,我可以獲得main方法底部的唯一方法是在矢量中使用Shape的指針,並在將子對象添加到矢量中時引用它們:
vector<Shape*> shapes;
shapes.push_back(&circle);
有任何想法嗎? 非常感謝您的幫助。
#include <iostream>
#include "shape.h"
#include "point.h"
#include <vector>
using namespace std;
double getTotalArea(vector<Shape> sh)
{
return 0;
}
int main(int argc, char** argv)
{
vector<Point> my_points;
my_points.push_back(Point(2, 3));
my_points.push_back(Point(1, 1));
my_points.push_back(Point(3, 1));
cout << "Points for a triangle:\n\n";
Triangle triangle(my_points);
cout << triangle << "area = " << triangle.getArea();
my_points.clear();
my_points.push_back(Point(2, 7));
my_points.push_back(Point(9, 7));
my_points.push_back(Point(2, 3));
my_points.push_back(Point(9, 3));
cout << "\n\nPoints for a rectangle:\n\n";
Rectangle rectangle(my_points);
cout << rectangle << "area = " << rectangle.getArea();
my_points.clear();
my_points.push_back(Point(0, 5));
my_points.push_back(Point(5, 5));
my_points.push_back(Point(0, 0));
my_points.push_back(Point(5, 0));
cout << "\n\nPoints for a square:\n\n";
Square square(my_points);
cout << square << "area = " << square.getArea();
my_points.clear();
my_points.push_back(Point(0, 5));
my_points.push_back(Point(6, 6));
my_points.push_back(Point(10, 5));
my_points.push_back(Point(8, 3));
my_points.push_back(Point(1, 2));
cout << "\n\nPoints for a pentagon:\n\n";
Pentagon pentagon(my_points);
cout << pentagon << "area = " << pentagon.getArea();
my_points.clear();
my_points.push_back(Point(3, 5));
cout << "\n\nLength of semi-major axis and length of semi-minor axis for an oval (put in a Point object)):\n\n";
Oval oval(my_points);
cout << oval << "area = " << oval.getArea();
my_points.clear();
cout << "\n\nRadius of a circle (first parameter of a Point object - second is ignored):\n\n";
my_points.push_back(Point(12, 0)); //the second argument here can also be NULL
Circle circle(my_points);
cout << circle << "area = " << circle.getArea();
my_points.clear();
vector<Shape*> shapes;
shapes.push_back(&circle);
shapes.push_back(&rectangle);
shapes.push_back(&square);
shapes.push_back(&pentagon);
shapes.push_back(&oval);
shapes.push_back(&circle);
double totalArea = getTotalArea(shapes);
return 0;
}
感謝你們中的一些人為我指出了正確的方向,我解決了我遇到的問題。 這就是我得到的,並且可以正常使用:
//a function outside the main method
void displayTotalArea(vector<Shape*>& sh)
{
double total = 0;
for (int x = 0; x < sh.size(); x++)
{
total += sh[x]->getArea();
}
cout << "\n\nTotal area of all shapes = " << total;
}
//in the main method
vector<Shape*> shapes;
shapes.push_back(&pentagon);
shapes.push_back(&triangle);
shapes.push_back(&rectangle);
shapes.push_back(&square);
shapes.push_back(&pentagon);
shapes.push_back(&oval);
shapes.push_back(&circle);
displayTotalArea(shapes);
如果我了解您的工作,那么可以這樣做:
double getTotalArea(const vector<Shape*>& sh)
{
double tot = 0;
for (Shape* s : sh)
tot += s->getArea();
return tot;
}
將子級添加到容器中(然后使用vector而不是vector,這樣做會更好,因為當向量超出范圍時,使用非指針版本會釋放形狀):
Triangle t;
Oval v;
std::vector<Shape> container;
container.push_back(t);
container.push_back(v);
這樣編譯就可以了:
#include <iostream>
#include <vector>
class A {};
class B : public A {};
class C : public A {};
int main()
{
std::vector<A> vec;
vec.push_back(B());
vec.push_back(C());
B instance;
vec.push_back(instance);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.