I have 2 classes like so:
class Shape
{
public:
int width, height;
virtual int area() { return 0; }
};
class Square : public Shape
{
public:
int area() { return width*height; }
};
Then I have a vector defined like so:
std::vector<Shape*> shapes;
I try and iterate through the vector like this:
for(std::vector<Shape*>::iterator it = shapes.begin(); it != shapes.end(); it++)
{
Shape &s = **it;
s.area();
}
The problem is that whenever I call area, even if s is a Square, it calls Shape's area function. I thought that the virtual keyword was so that the inheriting class can redefine the function?
Could somebody please tell me why Shape's area function is called instead of Square's area function?