question about virtual functions

ive read alot of material to try and figure out what virtual functions are...or what they can be used for, yet i have not found a really good definition or example...
can anyone explain it to me?
thanks.
chris->

Comments

  • : ive read alot of material to try and figure out what virtual functions are...or what they can be used for, yet i have not found a really good definition or example...
    : can anyone explain it to me?
    : thanks.
    : chris->
    :
    [purple]
    When you create a base class with a virtual function:

    [code]
    class Mammal
    {
    virtual Speak() { cout << "Mammal speak" );
    }
    [/code]

    the virtual keyword means that the function can be overridden by a derived class with it's own implimentation thus:

    [code]
    class Dog : public Mammal
    {
    Speak() { cout << "Bark" };
    }
    [/code]

    Then when you want your dog to bark and you call Dog.bark(); the correct member function will be called. The Keyword 'virtual' is optional because all the base class's member functions are virtual by default.

    If you want a Speak(); function in your Dog class then the Mammal class [b]must[/b] also have a Speak(); function.
    [purple]
  • so in essence, using a virtual function would be the same as if you omit the virtual function in the base class and declare the talk function in each class seperatly.
    by using the virtual keyword, its just a timesaver.

    : : ive read alot of material to try and figure out what virtual functions are...or what they can be used for, yet i have not found a really good definition or example...
    : : can anyone explain it to me?
    : : thanks.
    : : chris->
    : :
    : [purple]
    : When you create a base class with a virtual function:
    :
    : [code]
    : class Mammal
    : {
    : virtual Speak() { cout << "Mammal speak" );
    : }
    : [/code]
    :
    : the virtual keyword means that the function can be overridden by a derived class with it's own implimentation thus:
    :
    : [code]
    : class Dog : public Mammal
    : {
    : Speak() { cout << "Bark" };
    : }
    : [/code]
    :
    : Then when you want your dog to bark and you call Dog.bark(); the correct member function will be called. The Keyword 'virtual' is optional because all the base class's member functions are virtual by default.
    :
    : If you want a Speak(); function in your Dog class then the Mammal class [b]must[/b] also have a Speak(); function.
    : [purple]
    :

  • [b][red]This message was edited by stober at 2003-6-2 6:56:53[/red][/b][hr]
    : so in essence, using a virtual function would be the same as if you omit the virtual function in the base class and declare the talk function in each class seperatly.
    : by using the virtual keyword, its just a timesaver.
    :

    [blue]The virtual keyword allows you to change the behavior of a base class function or it allows a derived class to change the behavior of your function. When you (or someone else) writes a class, you don't know at that time whether it is going to become a base class or not. You also don't know how someone might want to change the behavior of a function. So when you write the base class make the function virtual to make the class as flexible as possible for others to use.

    Pure virtual functions are not implement by the base class. The derived class is required to do that. Pure virtual functions allow the base class to call a function that is implenented by the derived class.[/blue]


  • : [b][red]This message was edited by stober at 2003-6-2 6:56:53[/red][/b][hr]
    : : so in essence, using a virtual function would be the same as if you omit the virtual function in the base class and declare the talk function in each class seperatly.
    : : by using the virtual keyword, its just a timesaver.
    : :
    :
    : [blue]The virtual keyword allows you to change the behavior of a base class function or it allows a derived class to change the behavior of your function. When you (or someone else) writes a class, you don't know at that time whether it is going to become a base class or not. You also don't know how someone might want to change the behavior of a function. So when you write the base class make the function virtual to make the class as flexible as possible for others to use.
    :
    : Pure virtual functions are not implement by the base class. The derived class is required to do that. Pure virtual functions allow the base class to call a function that is implenented by the derived class.[/blue]
    :
    :
    :
    The real value of a virtual function is its application to runtime polymorphism.

    The traditional example is...

    [code]
    class Mammal
    {
    public:
    virtual void Speak() {cout << "Mammal speak()";}
    };

    class Dog: public Mammal
    {
    public:
    void Speak(){cout << "Bark";}
    };

    class Cat: public Mammal
    {
    public:
    void Speak(){cout << "Meow";}
    };
    [/code]

    We can write someting like this to show compile time binding..

    [code]
    Mammal m;
    Dog d;
    Cat c;
    m.Speak(); // Prints Mammal speak
    d.Speak(); // Prints Bark
    c.Speak(); //Prints Meow
    [/code]

    The real power lies in runtime binding...
    [code]
    Mammal *m = new Dog();
    Mammal *m1 = new Cat();
    m->Speak(); // Prints Bark;
    m->Speak(); // Prints Meow;
    [/code]

    We can use this polymorphism to develop a container of generic objects...

    [code]
    vector vm;
    vm.push_back(m);
    vm_push_back(m1);

    We now have a container of two distinct objects. This would not be possible with instances of the objects themselves.

    [/code]

    Hope this helps a bit.

  • 8-D
    gotcha...that cleared alot of it up...especially the polymorphism example. thanks for the help.
    -chris-

    : : [b][red]This message was edited by stober at 2003-6-2 6:56:53[/red][/b][hr]
    : : : so in essence, using a virtual function would be the same as if you omit the virtual function in the base class and declare the talk function in each class seperatly.
    : : : by using the virtual keyword, its just a timesaver.
    : : :
    : :
    : : [blue]The virtual keyword allows you to change the behavior of a base class function or it allows a derived class to change the behavior of your function. When you (or someone else) writes a class, you don't know at that time whether it is going to become a base class or not. You also don't know how someone might want to change the behavior of a function. So when you write the base class make the function virtual to make the class as flexible as possible for others to use.
    : :
    : : Pure virtual functions are not implement by the base class. The derived class is required to do that. Pure virtual functions allow the base class to call a function that is implenented by the derived class.[/blue]
    : :
    : :
    : :
    : The real value of a virtual function is its application to runtime polymorphism.
    :
    : The traditional example is...
    :
    : [code]
    : class Mammal
    : {
    : public:
    : virtual void Speak() {cout << "Mammal speak()";}
    : };
    :
    : class Dog: public Mammal
    : {
    : public:
    : void Speak(){cout << "Bark";}
    : };
    :
    : class Cat: public Mammal
    : {
    : public:
    : void Speak(){cout << "Meow";}
    : };
    : [/code]
    :
    : We can write someting like this to show compile time binding..
    :
    : [code]
    : Mammal m;
    : Dog d;
    : Cat c;
    : m.Speak(); // Prints Mammal speak
    : d.Speak(); // Prints Bark
    : c.Speak(); //Prints Meow
    : [/code]
    :
    : The real power lies in runtime binding...
    : [code]
    : Mammal *m = new Dog();
    : Mammal *m1 = new Cat();
    : m->Speak(); // Prints Bark;
    : m->Speak(); // Prints Meow;
    : [/code]
    :
    : We can use this polymorphism to develop a container of generic objects...
    :
    : [code]
    : vector vm;
    : vm.push_back(m);
    : vm_push_back(m1);
    :
    : We now have a container of two distinct objects. This would not be possible with instances of the objects themselves.
    :
    : [/code]
    :
    : Hope this helps a bit.
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories