C Virtual Functions
C Virtual Functions
In this tutorial, we will learn about C++ virtual function and its use with the help
of examples.
Basically, a virtual function is used in the base class in order to ensure that the
function is overridden. This especially applies to cases where a pointer of base
class points to an object of a derived class.
class Base {
public:
void print() {
// code
}
};
int main() {
Derived derived1;
Base* base1 = &derived1;
return 0;
}
In order to avoid this, we declare the print() function of the Base class as
virtual by using the virtual keyword.
class Base {
public:
virtual void print() {
// code
}
};
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Run Code
Output
Derived Function
So, this function is overridden even when we use a pointer of Base type that
points to the Derived object derived1 .
This identifier specifies the member functions of the derived classes that override
the member function of the base class.
For example,
class Base {
public:
virtual void print() {
// code
}
};
If we use a function prototype in Derived class and define that function outside
of the class, then we use the following code:
// function definition
void Derived::print() {
// code
}
When using virtual functions, it is possible to make mistakes while declaring the
member functions of the derived classes.
Using the override identifier prompts the compiler to display error messages
when these mistakes are made.
Otherwise, the program will simply compile but the virtual function will not be
overridden.
Functions with incorrect names: For example, if the virtual function in the
base class is named print() , but we accidentally name the overriding
function in the derived class as pint() .
Functions with different return types: If the virtual function is, say, of void
Suppose each class has a data member named type . Suppose these variables
are initialized through their respective constructors.
class Animal {
private:
string type;
... .. ...
public:
Animal(): type("Animal") {}
... .. ...
};
Now, let us suppose that our program requires us to create two public functions
for each class:
We could create both these functions in each class separately and override
them, which will be long and tedious.
Or we could make getType() virtual in the Animal class, then create a single,
separate print() function that accepts a pointer of Animal type as its
argument. We can then use this single function to override the virtual function.
class Animal {
... .. ...
public:
... .. ...
virtual string getType {...}
};
... .. ...
... .. ...
This will make the code shorter, cleaner, and less repetitive.
#include <iostream>
#include <string>
using namespace std;
class Animal {
private:
string type;
public:
// constructor to initialize type
Animal() : type("Animal") {}
public:
// constructor to initialize type
Dog() : type("Dog") {}
public:
// constructor to initialize type
Cat() : type("Cat") {}
int main() {
Animal* animal1 = new Animal();
Animal* dog1 = new Dog();
Animal* cat1 = new Cat();
print(animal1);
print(dog1);
print(cat1);
return 0;
}
Run Code
Output
Animal: Animal
Animal: Dog
Animal: Cat
Here, we have used the virtual function getType() and an Animal pointer ani in
order to avoid repeating the print() function in every class.
. When print(animal1) is called, the pointer points to an Animal object. So, the
virtual function in Animal class is executed inside of print() .
. When print(dog1) is called, the pointer points to a Dog object. So, the virtual
function is overridden and the function of Dog is executed inside of print() .
. When print(cat1) is called, the pointer points to a Cat object. So, the virtual
function is overridden and the function of Cat is executed inside of print() .