class Shape
{
public:
void draw() const {cout<<"draw shape"<<endl;}
};
class Point : public Shape
{
public:
Point( int a= 0, int b = 0 ) {x=a; y=b;}
int getx() const {return x;}
int gety() const {return y;}
void draw() const {cout<<"draw point("<<x<<","<<y<<")\n";}
private:
int x, y;
};
class Circle : public Point
{
public:
Circle( double r = 0.0, int x = 0, int y = 0 ):Point(x,y)
{ radius = r; }
void draw() const
{ cout<<"draw circle("<<getx()<<","<<gety()<<","<<radius<<")\n"; }
private:
double radius;
};
Wouldn't the draw() method in the derived class take priority over the draw() method in the base class ? I've read of the concept name hiding. Confused.
You don't use virtual in the base class. In that case you would have to reference an object of the derived class to have the derived class' draw() function called. That's not what you are doing.
arrayOfShapes are pointers to the base class (Shape). Shape's draw() function is going to be called because Shape::draw() is not virtual.
Make Shape::draw() virtual and you will get the behavior you expect. Note that you need to make Point::draw virtual also as Circle derives from Point.
Just a comment: Your inheritance structure is a little odd. Typically, a Circle IS A Shape and a Circle HAS A point.
so long as the array type is Shape, will all methods called be from Shape ?
How many times do I have to say this? For an object of type Shape or derived from Shape, Shape's methods will be called as long as they are not virtual.