Thread: Virtual Functions

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    26

    Virtual Functions

    Hi!
    what the difference between overriding a function and virtual functions?! im really stuck!
    for example i have this:
    PHP Code:
    #include <iostream.h>
    class one{
    public:
    one(){}
    virtual void override(){
    cout <<"virtual function one"<<endl;
    }
    };
    class 
    two: public one{
    public:
    two(){}
    virtual void override(){
    cout <<"virtual function two"<<endl;
    }
    };
    int main ()
    {  
    one test;
       
    two test2;

       
    test.override();
       
    test2.override();
       
    system ("PAUSE");
      return 
    0;
       } 

    and this:

    PHP Code:
    #include <iostream.h>
    class one{
    public:
    one(){
    }
    void override(){
    cout <<"virtual function one"<<endl;
    }
    };
    class 
    two: public one{
    public:
    two(){
    }
    void override(){
    cout <<"virtual function two"<<endl;
    }
    };
    int main ()
    {  
    one test;
       
    two test2;

       
    test.override();
       
    test2.override();
       
    system ("PAUSE");
      return 
    0;
       } 
    what the difference between them?! they both show me the same output!
    thanks!

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    First off, you need to get rid of the virtual identifier in class two. It only needs to be defined in the parent class.

    Both examples here should output the same thing but there's some differences.

    Let's say you declared in your main something like this:
    Code:
    int main()
    {
       one *A;
       A = new one;
       A->override();
       delete A;
    
       A = new two;
       A->override();
       delete A;
    
       return 0;
    }
    After fixing your virtual function as I stated above, what does the output give you for your first case? How about the second case?

    Here's a good tutorial too:
    http://byerley.cs.waikato.ac.nz/c++/chap10.html
    Last edited by Cshot; 10-03-2002 at 02:36 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Cshot
    First off, you need to get rid of the virtual identifier in class two. It only needs to be defined in the parent class.
    He doesn't 'need' to do this. Perhaps it's good to explicity state that a function is virtual?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Sorry, poor choice of wording. I still prefer that way though. What do you guys use?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    If its virtual I type it, if I dont then I'll forget
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    >>If its virtual I type it, if I dont then I'll forget

    Dito. Since I dont use virtual functions to any large extent, it is good to easily and instantly note any case where I have. AFAIK theres no harm to it. Doesnt sound like bad coding practice either.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by lightatdawn
    >>If its virtual I type it, if I dont then I'll forget

    Dito. Since I dont use virtual functions to any large extent, it is good to easily and instantly note any case where I have. AFAIK theres no harm to it. Doesnt sound like bad coding practice either.
    You dont have to keep defining virtual in every derived class...if its virtual in the base....its virtual all the way up....

    Personally I think its good to keep redefining it virtual, as you may find that someone who reuses your class may be doing so directly from a derived class...and not pay attention to the definition of your base class where you actually defined a member function virtual.....for all they know, your function is not virtual....just gives them a little extra info IMO

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    thank you all, i think i got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need a good example of virtual functions
    By Anddos in forum C++ Programming
    Replies: 10
    Last Post: 02-15-2006, 11:48 AM
  2. Virtual Functions
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2004, 04:13 PM
  3. Thread creation on virtual functions
    By gustavosserra in forum C++ Programming
    Replies: 13
    Last Post: 10-14-2004, 08:03 AM
  4. recursive virtual functions
    By ygfperson in forum C++ Programming
    Replies: 0
    Last Post: 05-25-2003, 08:00 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM