Link to home
Create AccountLog in
Avatar of v_eman
v_eman

asked on

virtual functions

I am a beginner in c++. I understand that, virtual function can be used to resolve the conflict. Something like this,
in the given example, if virtual is not used, the parent class function is called.

but , why some one wants to declare a pointer of parent class type object and define it for a child class object.

A *obj1;        //A is the base class
obj1=new(B) // B inherits A

what is the use? any good example?..

I will go for
B *obj1;
obj1=new(B);

Then no need to worry about virtual.


# include<iostream>
using namespace std;

class A
{
public:
      virtual void disp();
};

void A::disp()
{
   cout<<"Class: A"<<endl;
}
     
     
class B : public A
{
     public:
     void disp();      
};

void B::disp()
{
   cout<<"Class: B"<<endl;
}
     
     
int main()
{
    A *obj1;
    obj1 = new(B);
    obj1->disp();
    getchar();
    return 0;
}      

Avatar of jkr
jkr
Flag of Germany image

The following example might help illustrate the advantages of such a concept:
#include <iostream>
#include <vector>
using namespace std;

#include <stdlib.h>
#include <time.h>


struct IVehicle {

virtual void Drive () = 0;
virtual void Release () = 0;
};

class Car : public IVehicle {

public:

virtual ~Car() {}

virtual void Drive () { cout << "Driving a car" << endl;}
virtual void Release () {delete this;}
};

class Bus : public IVehicle {

public:

virtual ~Bus() {}

virtual void Drive () { cout << "Driving a Bus" << endl;}
virtual void Release () {delete this;}
};

int main () {

vector<IVehicle*> vVehicles;
vector<IVehicle*>::iterator i;

srand((unsigned)time(NULL));

for ( int n = 0; n < 10; ++n) {

  int nDice = rand() % 10;

  if ( nDice >= 5)
    vVehicles.push_back(new Car);
  else
    vVehicles.push_back(new Bus);
}

for (i = vVehicles.begin(); i != vVehicles.end(); ++i) {

  (*i)->Drive();
  (*i)->Release();
 
}

return 0;
}

Open in new window

why some one wants to declare a pointer of parent class type object and define it for a child class object.

Look at it from a real world perspective

Lets say you initially design a "Father" class and then create an object of it. The Father has certain characteristics or properties. Now your client application may have been using only the Father class and referring to its behaviour by calling functions defined in the Father class.

A "Son" object is created later. There will be something in common between Father and Son to the extent that the Son can be considered as being similar to the Father or even a type of "Father".
Let's take the example of "Walk" being a characteristic which both Father and Son have. Now you have defined a function "Walk" in Father and have called it in your client application in several places.

Sometime in the future you decide that you want to replace the Father's walk with the Son's walk and so then all you need to do is use the same Father pointer in your client application but just initialize it with a "Son" object.

Now because "Walk" was a virtual function you automatically end up calling Son's Walk function wherever Father's Walk was called.


This is putting it in very layman terms.

If you want to read up about virtual functions you also need to understand inheritance.

http://www.cplusplus.com/doc/tutorial/inheritance/
http://www.cplusplus.com/doc/tutorial/polymorphism/

class Father{

public :

virtual Walk();
};

class Son : public Father {

public:
virtual Walk();
};

//below is your client app
main()
{

Father* aFamily = new Father; //or new Son();

aFamily->Walk(); //calls Father->Walk() or Son->Walk()

delete aFamily;
aFamily = 0;

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer