virtual , pure virtual function in cpp

Virtual function in cpp



In this article, you will learn about the virtual function, why it is needed, and how to use it.


The member function of the base class redefined in the derived classes is known as a virtual function.

Why we need for virtual function in cpp?


In the inheritance of the classes, we may use functions with the same name in different classes. when we use these functions, we call the function by creating the object or pointers of the classes.


Example of virtual function:

#include <iostream> using namespace std; class baseclass { // ctreate the base public: void display(){ //define and declare the display function cout<" we are in the Parent"<<endl; } }; class derivedclass: public baseclass{ //create derived public: void display() { //define and declare the display again cout<<" we are in the derived"<<endl; } }; class derivedclass2: public baseclass{ //create derived agian one more public: void display() { //define and declare the display again cout<<"we are in the derived 2"<<endl; } }; int main() { baseclass *obj= new baseclass ; //create an object of the derived
derivedclass *derobj= new derivedclass; derivedclass2 *der2obj= new derivedclass2; obj->display(); //call the dispaly derobj->display(); der2obj->display(); return 0; }
Output:
we are in the Parent
we are in the derived
we are in the derived 2

Look at the above example, every time function call then we create the pointer of the class.

There is a facility provided by cpp language that makes the virtual function. The virtual function gives the opportunity to create the pointer of one class and use it with the other class function.


How to use a virtual function?

The keyword :

Virtual is used.


Example:


#include <iostream> using namespace std; class baseclass { // ctreate the base public: virtual void display(){ //define and declare the display function cout<<"Function in the Parent Class"<<endl; } }; class derivedclass: public baseclass{ //create derived public: void display() { //define and declare the display function again cout<<"we are in the derived"<<endl; } }; class derivedclass2: public baseclass{ //create derived public: void display() { //define and declare the display function again cout<<" we are in the derived 2"<<endl; } }; int main() { derivedclass derobj; derivedclass2 der2obj; baseclass *oobj= new baseclass; //call the dispaly oobj=&derobj; oobj->display(); oobj=&der2obj; oobj->display(); return 0;
}


Output:
we are in the derived
 we are in the derived  2

What is pure virtual function?
Pure virtual function


The virtual function declaration ended with the =0 is known as a pure virtual function in the cpp programming language.

The syntax of pure virtual function:

class baseclass{

pubic: 

virtual void display()=0;

}
Example of the pure virtual function:

#include <iostream>
using namespace std;



class baseclass { // create the base 
public:
  virtual void display(){ //define and declare the display 
      cout<<"we are in the  Parent "<<endl;
   }
};


class derivedclass: public baseclass{ //create derived 
public:
   void display() { //define and declare the display again
      cout<<" we are in the derived "<<endl;
   }
};


class derivedclass2: public baseclass{ //create derived class
public:
   void display() { //define and declare the display again
      cout<<" we are in the derived 2"<<endl;
   }
};



int main() {
 
   derivedclass  derobj;
   derivedclass2 der2obj;


baseclass *oobj;
//call the display
   
   oobj=&derobj;
   
   oobj->display();
   
   oobj=&der2obj;
   
   oobj->display();
   
   return 0;
}

Output:

 we are in the derived 
 we are in the derived 2

Same example repeated but with pure virtual function.The baseclass is an abstract class it has a pure function. We make pointer of the baseclass oobj, then refer the oobj to the derobj object of the derived class and get display function. 
Again oobj refers to the object of another derived class der2obj and get display function.
 

What is an abstract class?

The class in which one pure function is used is known as abstract class.

In the above code snippet, the base class becomes an abstract because it contains the virtual function.

Another thing about the abstract is that object creation is not allowable. The pointer of the abstract is created for using the abstract class.


why we can not create the object of abstract class?


We can not create the object of the abstract because it is incomplete without the body of the class, only we use abstract for interference. The abstract makes the interference unique.


Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout