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
This is a discussion on When to call virtual within the C++ Programming forums, part of the General Programming Boards category; When would I need to call a function virtual? Since derived functions will already override a base class function, when ...
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.
Last edited by Uraldor; 01-15-2002 at 07:56 PM.
Quidquid latine dictum sit, altum sonatur.
Whatever is said in Latin sounds profound.
Thanx. I understand now. I thought that I had read that before but was not sure.