0

I want to have a vector with objects "Rectangle" and "Triangle". I want to run a function from specific index of vector. Why it calls function from parent class? Code will explain my problem better than my description :)

#include <iostream>
#include <vector>



class Shape {

   public:
    virtual void info() {std::cout << "IM BASE CLASS - SHAPE" <<std::endl;}
};
class Rectangle: public Shape {

   public:
      void info () {std::cout << "IM RECTANGLE :" <<std::endl;}
};

class Triangle: public Shape {
   public:

      void info () {std::cout << "IM TRIANGLE :" <<std::endl;}
};

int main() {

   Rectangle rec;
   Triangle  tri;

   std::vector<Shape> v;
   v.push_back(rec);
   v.push_back(tri);





 std::vector<Shape>::iterator it;
   for( it=v.begin(); it!=v.end(); ++it )
   {
     it->info();
   }
   return 0;
}
LuQ232
  • 25
  • 1
  • 6

0 Answers0