Thread: Inheritance and virtual functions

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    2

    Inheritance and virtual functions

    I am writing some graphic software and I've run into a problem that I cannot resolve. Neithor text nor online reference has provided me with a solution.

    Say I have a class called Circle. This class has a member function called Draw(). Inside this function it makes a call to another virtual member function called Behave(). The reason I have it set up like this is so when I derive a class from Circle, say Circle2, I can add more functionality to Draw() by inserting code into Behave(), without actually redefining Draw().

    Of course to call Behave() from Draw I have to write Circle::Behave();. The result is the original Behave() is called, and not the one redefined in Circle2, even though it is a virtual function.

    Can anyone help me with this? I know this is not how virtual functions are designed to be used, but it could greatly help the effieciency of my code. I will admit that I am a bit new to C++ (and programming in general), so any help from the C++ gurus here will be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    36

    Hum..

    The virtual functions should behave the way you wanted it to if your derived class, Circle in this situation inherits the base class that defines the virtual function behave().
    thats all i know..
    will be there one day!

    I usually use VStudio 6!

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    well in Circle you have a virtual function Behave...so i am guessing it is declared like

    Code:
    //inside the class definition
    virtual void Behave();
    if you have a derive class from this..then you should also have another function name Behave() in the derive class...let say Circle2..

    so now..
    Code:
    Circle2 aCircle2;
    Circle aCircle1;
    Circle* pCircle;
    
    pCircle = aCircle2;
    pCircle->Behave(); //should call the dervive Behave();
    
    pCircle = aCircle;
    pCircle->Behave(); //should call the base class Behave();
    hehe..you are getting into some polymorphism stuff here
    nextus, the samurai warrior

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Inheritance and virtual functions

    Originally posted by Cryptomega
    Of course to call Behave() from Draw I have to write Circle::Behave();
    No you don't. That will force static binding (so it will always call the Cirvle version). Just write

    Behave()

    Also, if you aren't going to be redefining Draw (IE if you're just going to be redefining Behave), then the draw function does not have to be made virtual.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    Polymorphic, your right. I did not know that you could call a member function that way from another member function. (Like I said I'm new at this).

    Thanks for the help. I appreciate all the replies

  6. #6
    Terrance11
    Guest
    Originally posted by Cryptomega
    Polymorphic, your right. I did not know that you could call a member function that way from another member function. (Like I said I'm new at this).

    Thanks for the help. I appreciate all the replies
    there's a implicit this pointer, so it really looks like this:

    this->behave();


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance, polymorphism, and virtual functions
    By cs_student in forum C++ Programming
    Replies: 8
    Last Post: 08-04-2008, 08:47 AM
  2. Virtual function and multiple inheritance
    By George2 in forum C++ Programming
    Replies: 68
    Last Post: 02-13-2008, 01:15 AM
  3. Information Regarding Pure Virtual Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2003, 04:43 AM
  4. inheritance & virtual functions
    By xagiber in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 12:10 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM