Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Subclass of subclass

Reply
Thread Tools

Subclass of subclass

 
 
Fab
Guest
Posts: n/a
 
      08-09-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
String subclass method returns subclass - bug or feature? S.Volkov Ruby 2 03-12-2006 06:46 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM
TreeNode subclass Booshmon ASP .Net 0 10-04-2005 08:57 PM
struts: OK to subclass beans? Tom Java 1 09-18-2003 06:51 PM



Advertisments