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
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