Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Subclass of subclass (http://www.velocityreviews.com/forums/t949324-subclass-of-subclass.html)

Fab 08-09-2012 09:54 AM

Subclass of subclass
 
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


All times are GMT. The time now is 08:26 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.