



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth explanation of virtual functions, function binding, and late binding in Object-Oriented Programming using C++. It covers the importance of virtual functions, the concept of binding, early binding vs. late binding, and the use of virtual functions in C++ with examples. It also discusses pure virtual functions and abstract base classes.
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Importance of virtual function, function call binding, virtual functions, implementing late binding,
need for virtual functions, abstract base classes and pure virtual functions, virtual destructors
Importance of virtual function: A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
Virtual Functions are used to support "Run time Polymorphism". You can make a function virtual by preceding its declaration within the class by keyword 'virtual'. When a Base Class has a virtual member function, any class that inherits from the Base Class can redefine the function with exactly the same prototype i.e. only functionality can be redefined, not the interface of the function. A Base class pointer can be used to point to Base Class Object as well as Derived Class Object. When the virtual function is called by using a Base Class Pointer, the Compiler decides at Runtime which version of the function i.e. Base Class version or the overridden Derived Class version is to be called. This is called Run time Polymorphism.
What is Virtual Function?
A virtual function is a member function within the base class that we redefine in a derived class. It is declared using the virtual keyword. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
Virtual Function is a member function of the base class which is overridden in the derived class. The compiler performs late binding on this function.
To make a function virtual, we write the keyword virtual before the function definition. A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function. Virtual functions ensure that the correct function is called for an object, regardless of the expression used to make the function call. Functions in derived classes override virtual functions in base classes only if their type is the same.
Rules for Virtual Function in C++
What is binding?
Binding for functions means that wherever there is a function call, the compiler needs to know which function definition it should be matched to. This depends upon the signature of each
function declaration & the assignments that are taken. Also, the compiler needs to know that when this matching between the function call and choosing the correct definition will happen.
Early Binding
Early binding is a phenomenon wherein the decision to match various function calls happens at the compile time itself and the compiler directly associates the link with addresses. It is also known as Static Binding or Compile-time Binding.
Then the compiler converts this into low-level language that computer can understand, mostly machine language at the time of compilation
In early binding, the compiler directly provides the address of function declaration instruction to the function call instruction
Thus as the name suggests the binding happens very early before the program runs.
Example
#include
Output: Genric animal sound
Pure Virtual Function A pure virtual function in C++ is a virtual function for which we don’t have an implementation, we only declare it. A pure virtual function is declared by assigning 0 in the declaration.
virtual void sound() = 0;
Here sound() is a pure virtual fuction.
Abstract Class
An abstract class is defined as a class with one or more pure virtual functions. As explained above pure virtual function is a virtual member function that is marked as having no implementation. It has no implementation possible with the information provided in the class, including any base classes. An abstract class is also known as an abstract base class.
Example #include
return salary; } }; int main() { Employee_1 e1(5000); Employee_2 e2(3000); int a, b; a = e1.getSalary(); b = e2.getSalary(); cout << "Salary of Developer : " << a << endl; cout << "Salary of Driver : " << b << endl; return 0; }
Output:- Salary of Developer : 5000 Salary of Driver : 3000
Explanation The ‘getSalary()’ function in the class Employee is a pure virtual function. Since the Employee class contains the pure virtual function, therefore it is an abstract base class. Since the pure virtual function is defined in the subclasses, therefore the function ‘getSalary()’ is defined in both the subclasses of the class Employee i.e in Employee_1 and Employee_2.
Example for Virtual Function
#include