Sometimes its very difficult to understand which function would be call. So, this example explain you about how member functions are called while using virtual and without virtual.
In the case of virtual function when we call the member functions.The compiler knows which member function has to call it depends upon the type of object.
Like according to the example there is one virtual function "Show" in both the base and derived class.
And the output is :
-- I am in Test1 Class
-- I am in Test Class
First Derived class Show function called because first we pass the Derived class Object to the base class pointer. And i said earlier in case of virtual function complier knows which member function has to call depends upon the type of object.
In the case of Non virtual function when we call the member functions.The compiler knows which member function has to call it depends upon the type of pointer.
Like according to the example there is one Non virtual function "Show" in both the base and derived class.
And the output is :
-- I am in Test Class
-- I am in Test Class
In this case base class Show function called twice even though when have passed the Derived class Object to the base class pointer. Then also base class "Show" function has called twice.
This is happened because in this case compiler knows which member function has to call it depends upon the type of pointer not the type of Object.
Because i have defined base class pointer thats why base class "Show" function has called twice. If you defined Derived Class pointer then derived class member function will call twice.
1) Example of Virtual Function
In the case of virtual function when we call the member functions.The compiler knows which member function has to call it depends upon the type of object.
Like according to the example there is one virtual function "Show" in both the base and derived class.
And the output is :
-- I am in Test1 Class
-- I am in Test Class
First Derived class Show function called because first we pass the Derived class Object to the base class pointer. And i said earlier in case of virtual function complier knows which member function has to call depends upon the type of object.
Code: Cpp
#include<iostream.h>
#include<conio.h>
class Test
{
public :
virtual Show()
{
cout<<"I am in Test Class"<<endl;
}
};
class Test1 : public Test
{
public:
virtual Show()
{
cout<<"I am in Test1 Class"<<endl;
}
};
void main()
{
Test *Obj;
Test Obj1; // Base Class Object
Test1 Obj2; //Derived Class Object
Obj = &Obj2;
Obj->Show(); //In this case derived class show function called.
Obj = &Obj1;
Obj->Show(); //In this case Base class show function called.
}
2) Example of Non Virtual Function
In the case of Non virtual function when we call the member functions.The compiler knows which member function has to call it depends upon the type of pointer.
Like according to the example there is one Non virtual function "Show" in both the base and derived class.
And the output is :
-- I am in Test Class
-- I am in Test Class
In this case base class Show function called twice even though when have passed the Derived class Object to the base class pointer. Then also base class "Show" function has called twice.
This is happened because in this case compiler knows which member function has to call it depends upon the type of pointer not the type of Object.
Because i have defined base class pointer thats why base class "Show" function has called twice. If you defined Derived Class pointer then derived class member function will call twice.
Code: Cpp
#include<iostream.h>
#include<conio.h>
class Test
{
public :
void Show() //Without virtual
{
cout<<"I am in Test Class"<<endl;
}
};
class Test1 : public Test
{
public:
virtual Show()
{
cout<<"I am in Test1 Class"<<endl;
}
};
void main()
{
Test *Obj;
Test Obj1; // Base Class Object
Test1 Obj2; //Derived Class Object
Obj = &Obj2;
Obj->Show(); //In this case derived class show function called.
Obj = &Obj1;
Obj->Show(); //In this case Base class show function called.
}
