Thread: When to call virtual

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    When to call virtual

    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

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.
    Last edited by Uraldor; 01-15-2002 at 08:56 PM.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Thanx. I understand now. I thought that I had read that before but was not sure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Platform game gravity problem
    By Akkernight in forum Game Programming
    Replies: 33
    Last Post: 02-22-2009, 01:10 PM
  2. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  3. Interesting virtual function problem.
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2003, 10:08 PM
  4. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM