C++ Virtual Function:-
-> When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual precending its normal declaration. when a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer.
-> Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function. Below program illustrates this point.
Rules of Virtual Function
1.Virtual functions must be members of some class.
2.Virtual functions cannot be static members.
3.They are accessed through object pointers.
4.They can be a friend of another class.
5.A virtual function must be defined in the base class, even though it is not used.
6.The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
7.We cannot have a virtual constructor, but we can have a virtual destructor.
// Cpp program to demonstrate virtual function.
#include <iostream>
using namespace std;
class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}
OUTPUT:
Derived Class is invoked
-> When we use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword virtual precending its normal declaration. when a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type of the pointer.
-> Thus, by making the base pointer to point to different objects, we can execute different versions of the virtual function. Below program illustrates this point.
Rules of Virtual Function
1.Virtual functions must be members of some class.
2.Virtual functions cannot be static members.
3.They are accessed through object pointers.
4.They can be a friend of another class.
5.A virtual function must be defined in the base class, even though it is not used.
6.The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.
7.We cannot have a virtual constructor, but we can have a virtual destructor.
// Cpp program to demonstrate virtual function.
#include <iostream>
using namespace std;
class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}
OUTPUT:
Derived Class is invoked