Subclass of subclass

F

Fab

Dear All,

I have the following code:

#include <iostream>
using namespace std;

class Shape
{
public:
virtual void imA() { cout << "I'm a Shape" << endl; }
};

class Rectangle : public Shape
{
public:
void imA() { cout << "I'm a Rectangle" << endl; }
};

class Square : public Rectangle
{
public:
void imA() { cout << "I'm a Square" << endl; }
};

int main()
{
Shape* Rect = new Rectangle;
Shape* Squa = new Square;

Rect->imA();
Squa->imA();

return 0;
}

Base class is Shape and Square is a sub class of Rectangle, which is in
turn a subclass of Shape.

With the line:

Squa->Shape::imA();

I can make the square say "I'm a Shape". Now my question might not be
very useful, but why can't I do the same with:

Squa->Rectangle::imA();

since Square inherits from Rectangle?

Thanks a lot + regards
Fab
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top