如何将子类对象添加到其父类(C ++)的向量中?

时间:2014-07-18 07:55:13

标签: c++ pointers inheritance reference parent-child

我有各种形状类,从矩形,圆形,正方形,五边形等,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);

1 个答案:

答案 0 :(得分:0)

如果我明白你要做什么,这样做会很好:

double getTotalArea(const vector<Shape*>& sh)
{
    double tot = 0;

    for (Shape* s : sh)
        tot += s->getArea();

    return tot;
}

将child添加到容器中(然后使用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;
}