简体   繁体   English

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

[英]How do I add child class objects to a vector of their parent class (C++)?

I have various shape classes, from rectangle, circle, square, pentagon, etc., all child classes of the Shape class, and all of which have a getArea() method that returns their area. 我有各种形状类,包括矩形,圆形,正方形,五边形等,都是Shape类的所有子类,并且所有这些类都有一个getArea()方法来返回其面积。 I'm trying to make a vector of Shape that can add the areas of all those shapes so I can use a function to calculate the total area. 我正在尝试制作一个Shape的矢量,可以将所有这些形状的面积相加,以便可以使用一个函数来计算总面积。 This is the part of the code I'm having trouble with. 这是我遇到麻烦的代码的一部分。 I haven't finished writing the getTotalArea method but that will be easy. 我还没有完成getTotalArea方法的编写,但这很容易。 I just need to know how to name that method (the getTotalArea method) and what reference or pointer to use. 我只需要知道如何命名该方法(getTotalArea方法)以及要使用的引用或指针。 Also the only way I could get the bottom part of the main method to compile was to use a pointer for the Shape in vector and reference to the child objects when adding them to the vector: 同样,我可以获得main方法底部的唯一方法是在矢量中使用Shape的指针,并在将子对象添加到矢量中时引用它们:

vector<Shape*> shapes;
shapes.push_back(&circle);

Any ideas? 有任何想法吗? I greatly appreciate your help. 非常感谢您的帮助。

#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;
}

I solved the problem I was having, thanks to some of you who pointed me in the right direction. 感谢你们中的一些人为我指出了正确的方向,我解决了我遇到的问题。 This is what I got, and it works without flaw: 这就是我得到的,并且可以正常使用:

  //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);

If I understood what you are up to, this would do fine: 如果我了解您的工作,那么可以这样做:

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

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

    return tot;
}

to add child to the container (and then using vector instead of vector, that is better since when the vector goes out of scope, with the non-pointer version it would release the shapes): 将子级添加到容器中(然后使用vector而不是vector,这样做会更好,因为当向量超出范围时,使用非指针版本会释放形状):

Triangle t;
Oval v;
std::vector<Shape> container;
container.push_back(t);
container.push_back(v);

this compiles ok: 这样编译就可以了:

#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM