example:
Code:
class A
{
void Type(void) { cout << "A"; }
}
class B: public A
{
void Type(void) { cout << "B"; }
}
class C: public B
{
void Type(void) { cout << "C"; }
}
int main(void)
{
A a;
B b;
C c;
a.Type(); // prints "A"
b.Type(); // prints "B"
c.Type(); // prints "C"
A* aptr = &a;
aptr->Type(); // prints "A"
aptr = &b
aptr->Type(); // prints "A", should print "B"
aptr = &c;
aptr->Type(); // prints "A", should print "C"
B* bptr = &b;
bptr->Type(); // prints "B"
bptr = &c;
bptr->Type(); // prints "B", should print "C"
return(0);
}
to make the functions work properly:
Code:
class A
{
virtual void Type(void) { cout << "A"; }
}
class B: public A
{
virtual void Type(void) { cout << "B"; }
}
class C: public B
{
virtual void Type(void) { cout << "C"; }
}
int main(void)
{
A a;
B b;
C c;
a.Type(); // prints "A"
b.Type(); // prints "B"
c.Type(); // prints "C"
A* aptr = &a;
aptr->Type(); // prints "A"
aptr = &b
aptr->Type(); // prints "B"
aptr = &c;
aptr->Type(); // prints "C"
B* bptr = &b;
bptr->Type(); // prints "B"
bptr = &c;
bptr->Type(); // prints "C"
return(0);
}
hope this helps!
U.