Virtual function and pure virtual function in C++ / CPP with examples

(Last Updated On: April 4, 2021)

virtual function in C++

Virtual function Description:

Virtual function in C++ – this is a very detail tutorial about virtual function and pure virtual function. In this tutorial you will learn how to use these two functions in C++ programming.

Without any further delay, let’s get started!!!

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What is Virtual Function in c++

 A virtual function in c++ is a special type of member function. It is defined in the base class and maybe redefined in any class derived from this base class. Its name in the base class and in the derived classes remains the same Its definition in these classes may be different.

The virtual function in the derived class is executed through the pointer of the public base class.

A virtual function in c++ is declared by writing the word virtual before function declaration in the base class. The functions with the same name are declared in the derived classes. The use of the word virtual before function declaration in derived classes is optional. Once a function is declared virtual in a base class, it becomes virtual in all derived classes; even when the keyword virtual is not written in its definition in the derived classes.

The derived classes may have different versions of a virtual function, A virtual function may be redefined in the derived classes. A redefined function is said to override the base class function.

Example how to write a program using a virtual function in c++:

Programming Explanation:

 In the above program, the keyword “virtual appears only in the definition of the base class bb: it has not been repeated in the derived classes dl & d2 that are derived from the base class bb. Each derived class d1 and d2 and the base class bb have a member function with the same name. i.e. ppp.

The statement “p = &a;“ assigns the address of object bb to the pointer object “p”. When the statement “p->ppp(); is executed for the first time, the pointer object of the base class contains the address of the object of the derived class d1 and the virtual member function of the derived class dl is executed.

Similarly, when the “p->pppo);” statement is executed for the

the second time, the pointer object ‘p’ of the base class contains the address of the derived class d2 and thus the virtual member function of the derived class d2 is executed

in executing the virtual function in c++, the computer selects the function to be called based on the contents of the pointer and not on the basis of the type of the pointer.

Late Binding in c++

The events that take place at the time of execution of the program are called late binding. It is also called dynamic binding. With late binding, the compiler does not know which, the method will actually respond to the message because the type of the pointer is not known at compile time.

If the keyword virtual is used the system uses late binding.

 The execution of virtual functions through pointers is an example of late binding. While executing a virtual function through a pointer variable, the computer decides at the time of execution of the program. It executes the function depending upon the value in the pointer during the execution of the program.

Pure Virtual Function in c++

The virtual function that is only declared but not defined in the base class is called the pure virtual functions.

A function is made pure virtual by preceding its declaration with the keyword virtual and by post fixing, it with = 0.

The pure virtual function in c++ simply tells the compiler that the function is pure. The class that contains the pure virtual function in c++ exists only to act as a parent or base of the derived classes. This function is implemented in the derived classes.

Example how to write a program using a pure virtual function in c++:

Virtual Abstract Base Class & virtual Concrete Derived Class

The base class that has one or more pure virtual functions is called the Abstract Base Class. These classes designed as a framework or layout for derived classes. An abstract base class cannot be instantiated, i.e. an object of its type cannot be defined. But, a pointer to an abstract base class can be defined. The derived class that does not have any pure virtual function is called Concrete Derived Class. The pure virtual function of the abstract. Base Class is implemented in the Concrete Derived Class. The derived classes usually have their own versions of the virtual functions. in Object-Oriented Programming, a hierarchy of classes is defined. The Abstract Base Class is defined at the top of this hierarchy. The Concrete Derived Classes are derived from the Abstract Base Class. The pure virtual functions are declared in the base class. Each derived class contains its own version of these pure virtual functions. In this way, Inferiority is achieved within the hierarchy of the class.

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: