Wednesday, November 15, 2023

Virtual Functions

Polymorphism is one of the important features in object-oriented programming language. Polymorphism is the ability to take more than form.

In Greek, “poly” means many “morphism” means forms. It means one function or operator contains more than one forms.

Polymorphism is of two types:

1. Compile-time polymorphism (Function Overloading & Operator Overloading)

2. Run-time polymorphism (Virtual Function)

Polymorphism supports two types of concepts – overloading and overriding. Overloading is possible through compile-time polymorphism. Overriding is possible through compile-time polymorphism.

When working with inheritance, if both base and derived classes are defined with the same member function with the same signature, then member function overriding may occur. To overcome this, dynamic polymorphism is used. The dynamic polymorphism requires virtual functions.

Virtual function:

A virtual function is a member function that is dynamically bound to function calls. In dynamic binding , C++ determines which function to call at runtime, depending on the type of the object responsible for the call.

Virtual functions are declared by placing the key word virtual before the return type in the base class’s function declaration, such as

public:

virtual returntype fun_name(arguments)

{

----------

statements;

}

This declaration tells the compiler to expect fun_name to be redefined in a derived class. The compiler does not bind calls to the function with the actual function. Instead, it allows the program to bind calls, at runtime, to the version of the function that belongs to the same class as the object responsible for the call.

Demonstrating Virtual Functions

#include<iostream.h>

#include<conio.h>

class Base

{

public:

void print()

{

cout<<"\nprint() in Base class";

}

virtual void show()

{

cout<<"\nshow() in Base class";

}

};

class Derived : public Base

{

public:

void print()

{

cout<<"\nprint() in Derived class";

}

void show()

{

cout<<"\nshow() in Derived class";

}

};

void main()

{

clrscr();

Base *ptr;                                                                                                                            

Base b;                                                                                                                                

ptr=&b;            

ptr->print();                                                            

Derived d;                                                                                                                           

ptr=&d;            

ptr->show();                                                                       

getch();                                                                                                                                

}

Output:

print() in Base class

show() in Derived class

Rules For Virtual Functions:

  • The virtual functions should not be static and must be member of a class.
  • A virtual function may be declared as friend of another class.
  • Constructors can’t be declared as virtual but destructors can be virtual.
  • The virtual function must be defined in the public section of the class.
  • The prototype of virtual functions in base and derived classes should be exactly the same.
  • In case of mismatch the compiler neglects the virtual function mechanism and treats them as overloaded function.
  • If a base class contain virtual function and if the same function is not redefined in the derived classes in that case the base class function is invoked.
  • The keyword virtual prevents the compiler to perform early binding. Binding is postponed until run time.
  • The operator keyword used for operator overloading also supports virtual mechanism.


0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top