Introduction to Virtual Functions in C++
Virtual function in C++ is a member function defined in the base class which you can redefine in the derived class. So if in the programming you want to refer to an object of the derived class using the reference of the base class or pointer you can use the virtual function for calling that object and then you can execute the function of the derived class. A virtual keyword is used to define a virtual function in the programming. To achieve Runtime polymorphism in the software programs virtual functions are used. The function call will be resolved at runtime only.
Here is the syntax for defining a virtual function in C + + programming language:
Syntax:
virtual void function_name()
- Virtual is the mandatory keyword to be used before function name then function name that can be anything of user choice.
Some important rules to keep in mind while using and working with virtual functions in the C + + programming language:
- A class cannot have a virtual constructor in the code but the same class can have a virtual destructor in the code.
- Data type or we call it the prototype of the virtual function defined in the program must be the same in both the base class and derived class.
- Virtual functions cannot be static in nature.
- The most important rule about virtual function is that they must be defined in the base class only and must be overridden through the derived class.
- To achieve run time polymorphism in your code you must access virtual function through either pointer or the base class reference.
How does Virtual Function Work?
The compiler does two things whenever there is virtual function mentioned in the class of any given program:
If a class contains a virtual function then compiler itself does two things:
- The compiler will insert a virtual pointer (VPTR) as a data member of the class if an object of that class is created to point at the VTABLE of that particular class. Therefore, the compiler will do this every time a new object is created so that the new virtual pointer will be inserted as a data member of the class.
- Another thing compiler will do is that even if the object is not created a static array of the function pointer will call VTABLE in which each table cell will contain the address of each and every virtual function defined in that class.
Examples of Virtual Functions in C++
As we have seen the syntax and working of virtual function now we will see how it actually works when we implement it in the code:
Example #1
Here is the C + + code to demonstrate the working example of the virtual function in the C + + programming language.
Code:
#include <iostream>
using namespace std;
class Automobile
{
public :
virtual void functionalities()
{ cout << " Loading Automobile functionalities. \n " ; }
} ;
class wheel : public Automobile
{
public:
void functionalities()
{ this -> Automobile :: functionalities();
cout << " Loading wheel functionalities. \n" ;
}
} ;
class glass : public Automobile
{
public:
void functionalities ()
{
this -> Automobile :: functionalities () ;
cout << " Loading glass functionalities. \n" ;
}
};
class RunAutomobile
{
public:
void loadfunctionalities ( Automobile *Automobile )
{
Automobile -> functionalities () ;
}
};
int main()
{
RunAutomobile *r = new RunAutomobile;
Automobile *w;
wheel b ;
glass g ;
w = &b ;
r -> loadfunctionalities ( w );
w = &g;
r -> loadfunctionalities ( w );
return 0;
}
Output:
As you can see in the above code Automobile is the base class while wheel and glass are the derived class and we have also defined a virtual function with name functionalities. Another class with name RunAutomobile is defined in which a function is also defined with name load functionalities to load the above-defined functions in the base class. Then in the main class, an object is defined. The defined object is separate for derived classes wheel and glass whereas a pointer object is defined for base class Automobile.
Example #2
Here is the C ++ code to demonstrate the working example of the virtual function in the C++ programming language.
Code:
#include <iostream>
using namespace std;
// Declaring an Abstract class
class Pattern
{
protected:
float l;
public:
void getInput ()
{
cin >> l ;
}
// virtual Function
virtual float totalArea () = 0;
};
class Squared : public Pattern
{
public:
float totalArea ()
{ return l * l ; }
};
class Circle : public Pattern
{
public:
float totalArea ()
{ return 3.14 * l * l ; }
};
int main()
{
Squared s ;
Circle c;
cout << " Please enter the length for calculating the area of a square : " ;
s.getInput () ;
cout << " Hence the Area of square : " << s.totalArea () ;
cout << " \n Please enter radius for calculating the area of a circle : " ;
c.getInput () ;
cout << " Hence the Area of circle: " << c.totalArea () ;
return 0;
}
Output:
As you can see in the above code Pattern is the base abstract class while Squared and Circle are the derived class and we have also defined a virtual function with name totalArea(). Another function is also defined with name getInput() to get the input from the user. Then in the main class, an object is defined. The defined object is separate for derived classes squared and circle with object name s and c respectively to calculate the area of both.
Conclusion
Virtual functions play an important role in making programming experience effective and efficient. They are the direct supporter of object-oriented programming. A virtual function is used to perform late binding as well as dynamic linkage operations by telling the compiler. Therefore, it’s used to point to the base class.
Recommended Articles
This is a guide to Virtual Functions in C++. Here we discuss the Introduction and how virtual functions work along with examples and code implementation. You may also look at the following articles to learn more –