0% found this document useful (0 votes)
14 views16 pages

Virtual Function

Uploaded by

Ankur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Virtual Function

Uploaded by

Ankur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Virtual Function

Unit 3
Virtual Function

• A virtual function is a member function that is declared within a base


class and is re-defined (overridden) by a derived class.

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

• Virtual functions ensure that the correct function is called for an


object, regardless of the type of reference (or pointer) used for the
function call.

• They are mainly used to achieve Runtime polymorphism.

• Functions are declared with a virtual keyword in a base class.

• The resolving of a function call is done at runtime.


Rules for Virtual Functions

The rules for the virtual functions in C++ are as follows:


• Virtual functions cannot be static.
• A virtual function can be a friend function of another class.
• Virtual functions should be accessed using a pointer or
reference of base class type to achieve runtime polymorphism.
• The prototype of virtual functions should be the same in the
base as well as the derived class.
• They are always defined in the base class and overridden in a
derived class. It is not mandatory for the derived class to
override (or re-define the virtual function), in that case, the
base class version of the function is used.
• A class may have a virtual destructor but it cannot have a
virtual constructor.
Virtual Function

• Virtual Function is used to tell the compiler to perform


dynamic linkage or late binding on the function.
• There is a necessity to use the single pointer to refer to all
the objects of the different classes. So, we create the
pointer to the base class that refers to all the derived
objects. But, when base class pointer contains the address
of the derived class object, always executes the base class
function. This issue can only be resolved by using the
'virtual' function.
• A 'virtual' is a keyword preceding the normal declaration of
a function.
• When the function is made virtual, C++ determines which
function is to be invoked at the runtime based on the type
of the object pointed by the base class pointer.
Late binding or Dynamic linkage

• In late binding function call is resolved during runtime.


Therefore compiler determines the type of object at runtime,
and then binds the function call.
Example: Overridden function
#include <iostream>
using namespace std;
class A
{
int x=5;
public:
void display()
{
cout << "Value of x is : " << x<<endl;
}
};
class B: public A
{
int y = 10;
public:
void display()
{
cout << "Value of y is : " <<y<< endl;
}
};
int main()
{
A *a;
B b;
a = &b;
a->display();
return 0;
}
Output

• Value of x is : 5
• In the above example, *a is the base class pointer. The
pointer can only access the base class members but not
the members of the derived class.
• Although C++ permits the base pointer to point to any
object derived from the base class, it cannot directly
access the members of the derived class.
• Therefore, there is a need for virtual function which
allows the base pointer to access the members of the
derived class.
virtual function Example

#include <iostream>
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A *a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}
Output
• Derived Class is invoked
Pure Virtual Function
• A virtual function is not used for performing any task. It only
serves as a placeholder.
• When the function has no definition, such function is known as
"do-nothing" function.
• The "do-nothing" function is known as a pure virtual
function.
• A pure virtual function is a function declared in the base class
that has no definition relative to the base class.
• A class containing the pure virtual function cannot be used to
declare the objects of its own, such classes are known as abstract
base classes.
• The main objective of the base class is to provide the heirarchy
to the derived classes and to create the base pointer used for
achieving the runtime polymorphism.
• Pure virtual function can be defined as:
virtual void display() = 0;
Example
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class." << endl;
}
};
int main()
{
Base *bptr;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
Output

• Derived class is derived from the base class.


• In the above example, the base class contains
the pure virtual function. Therefore, the base
class is an abstract base class. We cannot
create the object of the base class.

You might also like