title: How to make virtual functions of a super class overrideable for grandchildren in C++?
body: <p>Hey guys here is some code I am going to run, issue is it doesn't work the way I intend to. I am unable to figure out what's wrong with it. I am c++ noob please help.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;stdexcept&gt;
  using namespace std;
  
  /**
   * Super class
   */
  class Shape
  {
  protected:
      int _dimensions;
  public:
      Shape() : _dimensions{0}
      {};
      Shape(int dims) : _dimensions{dims}
      {};
  
      virtual double getArea() {};
      // Because some shapes have no volume.  
      virtual double getVolume() {};  
  
      void setDimensions(int dim)
      {
          this-&gt;_dimensions = dim;
      };
      int getDimensions()
      {
          return this-&gt;_dimensions;
      };
  };
  
  
  /** 
   * Extended classes
   */
  class TwoDimensionalShape : public Shape
  {
  public: 
      TwoDimensionalShape() : Shape{2}
      {};
      // This should throw an error
      double getVolume() {
          throw logic_error("This shape ain't got area!");
      };  
  };
  
  class ThreeDimensionalShape : public Shape
  {
  public: 
      ThreeDimensionalShape() : Shape{3} {};
  };
  
  
  /**
   * Final Concrete classes extending extended classes
   */
  class Circle : public TwoDimensionalShape
  {
  protected:
      double _radius;
  public:
      Circle(double r) : _radius{r}
      {};
      double getArea()
      {
          // pi*r^2
          return M_PI * pow(_radius, 2);
      }
  };
  
  class Square : public TwoDimensionalShape
  {
  protected:
      double _side;
  public:
      Square(double s) : _side{s}
      {}
      double getArea()
      {
          // s^2
          return pow(_side, 2);
      }
  };
  
  class Triangle : public TwoDimensionalShape
  {
  protected:
      double _base, _height;
  public:
      Triangle(double b, double h) : _base{b}, _height{h}
      {};
      double getArea()
      {
          // b*h/2
          return _base * _height / 2;
      }
  };
  
  class Sphere : public ThreeDimensionalShape
  {
  protected:
      double _radius;
  public:
      Sphere(double r) : _radius{r}
      {}
      double getArea()
      {
          cout &lt;&lt; 4 * M_PI * pow(_radius, 2) &lt;&lt; endl;
          return 4 * M_PI * pow(_radius, 2);
      }
      double getVolume()
      {
          return (4/3) * M_PI * pow(_radius, 3);
      }
  };
  
  class Cube : public ThreeDimensionalShape
  {
  protected:
      double _side;
  public:
      Cube(double s) : _side{s}
      {};
      double getArea()
      {
          // surface area = 6*a^2
          return 6 * pow(_side, 2);
      }
      double getVolume()
      {
          // a^3
          return pow(_side, 3);
      }
  };
  
  class Tetrahedron : public ThreeDimensionalShape
  {
  protected:
      double _side;
  public:
      Tetrahedron(double s) : _side{s}
      {};
      double getArea()
      {
          // sqrt(3)*a^2
          return sqrt(3) * pow(_side, 2);
      }
      double getVolume()
      {
          // a^3/6sqrt(2)
          return pow(_side, 3) / (6 * sqrt(2));
      }
  };
  
  int main()
  {
      Shape arr[2];
      arr[0] = Circle{10};
      arr[1] = Sphere{10};
  
      // This one is accessing the right method.
      cout &lt;&lt; "Area of circle: " &lt;&lt; arr[0].getArea() &lt;&lt; endl; 
      // This one should access the parent, but accesses the grand parent!
      // even if it is overridden in parent.
      cout &lt;&lt; "Volume of circle: " &lt;&lt; arr[0].getVolume() &lt;&lt; endl; 
  
      // Both of these are accessing methods on grand parent rather than their own!!
      cout &lt;&lt; "Area of sphere: " &lt;&lt; arr[1].getArea() &lt;&lt; endl;
      cout &lt;&lt; "Volume of sphere: " &lt;&lt; arr[1].getVolume() &lt;&lt; endl; 
  
      return 0;
  }
  </code></pre>
  
  <p>I don't know why the array methods keep accessing the grand parent functions in the last three lines, but the right method on the first.</p>