Subclass of subclass

Discussion in 'C++' started by Fab, Aug 9, 2012.

  1. Fab

    Fab Guest

    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
    Fab, Aug 9, 2012
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Booshmon

    TreeNode subclass

    Booshmon, Oct 4, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    501
    Booshmon
    Oct 4, 2005
  2. Tom
    Replies:
    1
    Views:
    326
    Scott Yanoff
    Sep 18, 2003
  3. jstorta
    Replies:
    3
    Views:
    431
    jstorta
    Feb 20, 2006
  4. S.Volkov
    Replies:
    2
    Views:
    207
    S.Volkov
    Mar 12, 2006
  5. Trans
    Replies:
    8
    Views:
    310
    Robert Klemme
    Oct 23, 2008
Loading...

Share This Page