title: How do I add child class objects to a vector of their parent class (C++)?
body: <p>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.  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.  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.  I just need to know how to name that method (the getTotalArea method) and what reference or pointer to use.  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:</p>
  
  <pre><code>vector&lt;Shape*&gt; shapes;
  shapes.push_back(&amp;circle);
  </code></pre>
  
  <p>Any ideas?  I greatly appreciate your help.</p>
  
  <p></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "shape.h"
  #include "point.h"
  #include &lt;vector&gt;
  using namespace std;
  double getTotalArea(vector&lt;Shape&gt; sh)
  {
    return 0;
  }
  int main(int argc, char** argv)
  {
    vector&lt;Point&gt; my_points;
    my_points.push_back(Point(2, 3));
    my_points.push_back(Point(1, 1));
    my_points.push_back(Point(3, 1));
    cout &lt;&lt; "Points for a triangle:\n\n";
    Triangle triangle(my_points);
    cout &lt;&lt; triangle &lt;&lt; "area = " &lt;&lt; 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 &lt;&lt; "\n\nPoints for a rectangle:\n\n";
    Rectangle rectangle(my_points);
    cout &lt;&lt; rectangle &lt;&lt; "area = " &lt;&lt; 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 &lt;&lt; "\n\nPoints for a square:\n\n";
    Square square(my_points);
    cout &lt;&lt; square &lt;&lt; "area = " &lt;&lt; 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 &lt;&lt; "\n\nPoints for a pentagon:\n\n";
    Pentagon pentagon(my_points);
    cout &lt;&lt; pentagon &lt;&lt; "area = " &lt;&lt; pentagon.getArea();
    my_points.clear();
  
    my_points.push_back(Point(3, 5));
    cout &lt;&lt; "\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 &lt;&lt; oval &lt;&lt; "area = " &lt;&lt; oval.getArea();
    my_points.clear();
  
    cout &lt;&lt; "\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 &lt;&lt; circle &lt;&lt; "area = " &lt;&lt; circle.getArea();
    my_points.clear();
  
    vector&lt;Shape*&gt; shapes;
    shapes.push_back(&amp;circle);
    shapes.push_back(&amp;rectangle);
    shapes.push_back(&amp;square);
    shapes.push_back(&amp;pentagon);
    shapes.push_back(&amp;oval);
    shapes.push_back(&amp;circle);
    double totalArea = getTotalArea(shapes);
  
    return 0;
  }
  </code></pre>
  
  <p></p>
  
  <p>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:</p>
  
  <pre><code>  //a function outside the main method
    void displayTotalArea(vector&lt;Shape*&gt;&amp; sh)
    {
       double total = 0;
  
       for (int x = 0; x &lt; sh.size(); x++)
       {
         total += sh[x]-&gt;getArea();
       }
       cout &lt;&lt; "\n\nTotal area of all shapes = " &lt;&lt; total;
    }
  
    //in the main method
    vector&lt;Shape*&gt; shapes;
    shapes.push_back(&amp;pentagon);
    shapes.push_back(&amp;triangle);
    shapes.push_back(&amp;rectangle);
    shapes.push_back(&amp;square);
    shapes.push_back(&amp;pentagon);
    shapes.push_back(&amp;oval);
    shapes.push_back(&amp;circle);
    displayTotalArea(shapes);
  </code></pre>