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;
}      

C++

Avatar of undefined
Last Comment
phoffric
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo