You are on page 1of 4

Virtual Function in C++

o A C++ virtual function is a member function in the base class that you redefine in a derived class.
It is declared using the virtual keyword.

o It is used to tell the compiler to perform dynamic linkage or late binding on the function.

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

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


 A pure virtual function is a virtual function that has no definition within the class.
 A class having pure virtual function cannot be used to create direct objects of its own. It means
that the class is containing any pure virtual function then we cannot create the object of that
class. This type of class is known as an abstract class.

Syntax
There are two ways of creating a virtual function:

virtual void display() = 0;

or

virtual void display() {}

Example:
#include <iostream>

using namespace std;

class Base

public:

virtual void show() = 0;


};

class Derived : public Base

public:

void show()

std::cout << "Derived class is derived from the base class." << std::endl;

};

int main()

Base *bptr;

//Base b;

Derived d;

bptr = &d;

bptr->show();

return 0;

Output:
Derived class is derived from the base class.

Differences between the Virtual function and Pure virtual function


Virtual function Pure virtual function

A virtual function is a member function in a base class A pure virtual function is a member function in a base
that can be redefined in a derived class. class whose declaration is provided in a base class and
implemented in a derived class.

The classes which are containing virtual functions are The classes which are containing pure virtual function are
not abstract classes. the abstract classes.

In case of a virtual function, definition of a function is In case of a pure virtual function, definition of a function
provided in the base class. is not provided in the base class.

The base class that contains a virtual function can be The base class that contains a pure virtual function
instantiated. becomes an abstract class, and that cannot be
instantiated.

If the derived class will not redefine the virtual If the derived class does not define the pure virtual
function of the base class, then there will be no effect function; it will not throw any error but the derived class
on the compilation. becomes an abstract class.

All the derived classes may or may not redefine the All the derived classes must define the pure virtual
virtual function. function.

You might also like