When would I need to call a function virtual? Since derived functions will already override a base class function, when would I need to include virtual? Thanx for ur help. =D
Printable View
When would I need to call a function virtual? Since derived functions will already override a base class function, when would I need to include virtual? Thanx for ur help. =D
example:
to make the functions work properly: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);
}
hope this helps!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);
}
U.
Thanx. I understand now. I thought that I had read that before but was not sure.