Virtual Function in C++
Any member function that is defined inside an initial class and overruled by an extension class is referred to as a virtual method. Now, one will be able to invoke a virtual function that exists for an object and carry out the derivative class's instance of that specific method whenever you access an object belonging to an extracted class implementing a pointer to the root class.
What accomplishes a virtual function in the programming language C++?
A virtual function that is used in C++ is a base method with arguments that are capable of being changed or recreated in a class that was derived in order to achieve versatility. The virtual keyword can be used when defining the function to be used in the root class. Assuming the method has been established in an initial class, it is capable of being invoked using an address or reference to execute in an imaginary instance of the original class. As a result, it requests that the interpreter establish an operation's coupling (late coupling or dynamic connection) while establishing the object's subtype at execution.
Only one reference may be used in C++ programming when addressing all derived class objects. The function defined in the original class is constantly called when the pointer contains references to all of the things from which it was derived. A virtual function in C++ can help you get around this problem because it aids in the run-time execution of the virtual rendition of the class it has derived.
What are the C++ Virtual Function Rules?
You must adhere to certain restrictions when creating a virtual function in C++. They can be described as follows:
- There can be no eternal methods or functions.
- Users can obtain them by using the term virtual."
- Virtual functions that exist in C++ require that they be members of the underlying class.
- They may be an acquaintance or a member of a different class.
- The original class and any existing classes should share an identical prototype to perform these functionalities.
- It is complimentary to redefine the virtual operation within the class that emerged, but the main class must do so.
- Run-time function request resolution takes place.
- A virtual destructor can be made, but not a constructor.
Let's go ahead and proceed to a practical usage, considering you are already acquainted with the fundamentals of the C++ virtual function. In, the example below, you will develop an original class and a class derived from it. This means that you will learn how to use the two methods of print and show throughout the two classes. To see what separates a virtual function from a traditional function, you simply need to render the class that contains the print function virtual; you can depart from the displaying method unaffected.
Code:
#include <iostream>
using namespace std;
class Parent{
public:
virtual void print(){
cout << "print Parent class" << endl;
}
void Display(){
cout << "Show Parent class" << endl;
}
};
class child : public Parent{
public:
void print(){
cout << "Print child class" << endl;
}
void show()
{
cout << "Show child class" << endl;
}
};
int main(){
Parent* bpointr;
child dpointr;
bpointr = &dpointr;
// virtual function binding
bpointr->print();
// Non-virtual function binding
bpointr->Display();
}
Output:
![Virtual Function in C++](https://static.tutorialandexample.com/cpp/virtual-function-in-cpp1.png)
The result output shows that the print function generated " Print child class" and the derive function produced " Show Parent class," respectively. This is so because the parent class's non-virtual display function and the child class's virtual print function were both referred to by the pointer of the parent class.
Describe a pure virtual function in the programming language C++.
A virtual function in C++ that accomplishes absolutely nothing is referred to as a pure virtual function or the do-nothing method. It serves merely as an identifier without any description of any functions (do-nothing function). It appears in an abstract, primitive class. These categories are not permitted to set up any individual objects. The syntax displayed below is used to create a pure virtual function in C++:
Virtual void class_name()=0;
The importance of virtual functions
The resulting class must override pure virtual methods.
Polymorphism is achieved via virtual functions.
A foundation class might have virtual functionalities.
The derived class allows for the overriding of virtual functionalities.