Virtual Functions & Late Binding in C++ OOP, Exercises of Object Oriented Programming

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

2021/2022

Uploaded on 09/27/2022

scrooge
scrooge 🇬🇧

4.5

(11)

247 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Oriented Programming Using C++ Unit 7th Semester 4th
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++
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..
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
pf3
pf4
pf5

Partial preview of the text

Download Virtual Functions & Late Binding in C++ OOP and more Exercises Object Oriented Programming in PDF only on Docsity!

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++

  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..

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 using namespace std; class Animals { public: void sound() { cout << "Genric animal sound" << endl; } }; class Cats: public Animals { public: void sound() { cout << "Cat meow" << endl; } }; int main() { Animals *a; Cats c; a= &c; a -> sound(); // early binding return 0; }

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 using namespace std; class Employee // abstract base class { virtual int getSalary() = 0; // pure virtual function }; class Employee_1: public Employee { int salary; public: Employee_1(int s) { salary = s; } int getSalary() { return salary; } }; class Employee_2: public Employee { int salary; public: Employee_2(int t) { salary = t; } int getSalary()

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 using namespace std; class base { public: void function_1() { cout << "base class function 1\n"; } virtual void function_2() { cout << "base class function 2\n"; } virtual void function_3() { cout << "base class function 3\n"; } virtual void function_4() { cout << "base class function 4\n";