Pure Virtual Function in C++

C++ Pure Virtual Functions

Pure Virtual functions are one of the ways we conceptualise Abstract classes. It is essentially a virtual function declared in the Parent Classes that doesn’t have any implementation and only has declaration.

  • Declared in Base Class
  • Is Virtual Function
  • Assigned to value 0
  • Doesn’t have implementation
  • Class containing Pure Virtual Function is called as Abstract Class

Example –

class PrepInsta
{
public:
virtual void open() = 0;
};
Pure Virtual Functions in C++

Concept of Pure Virtual Functions

This was long demanded feature by programmers using other object oriented languages, first developed in C++ –

  1. Programmers most of the time wanted to just give good architecture of the code and create classes with no implementation. But, to fully use inheritance concept without running the troubles of different instances of RunTime vs Compile time alternations in how code performed.
  2. With Pure Virtual functions this problem was solved as it would guarantee the same result. That is implementation of the code will always be performed by Derived classes and they need only be declared in Base classes.

Abstract Class

Abstract class is any class that just has the declarations but no initialisations. These initialisations and implementation details can be provided in the inherited derived classes. Pure Virtual function help us in achieving abstract classes.

Note – We can’t create objects of Abstract Classes or classes that contain Pure Virtual Functions.

 

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display() = 0;
};
class Derived : public Base
{
public:
void display()
{
cout<<"Inside the Derived Class\n";
}
};

int main()
{

// Pointer and Reference and basic derived class usage
Derived derived1;
Base *base;
Derived derived2;
base = &derived2;

derived1.display();
base->display();

return 0;
}

Output –

Inside the Derived Class

Inside the Derived Class

  • Since, the base class has Pure Virtual function it ignores the static resolution and performs run time polymorphism and displays the derived class function call

Some interesting Facts about Pure Virtual Functions

We can’t create a direct object of base class containing pure virtual function which is abstract class.

We must do this via derived class objects, or pointer reference of base class pointed towards object of the derived class.

Example –

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display() = 0;
};
class Derived : public Base
{
public:
void display()
{
cout<<"Inside the Derived Class\n";
}
};

int main()
{

// this will cause error as we have created
// object of base class

Base base;
base.display();
return 0;
}

Output

error: cannot declare variable ‘base’ to be of abstract type ‘Base’
 Base base;

We can solve this via two ways

Either create object of only the derived class or pointed references as we had done in the first example –

// derived class object
Derived derived1;
// pointer references between base and derived clases
Base *base;
Derived derived2;
base = &derived2;

derived1.display();
base->display();

Overriding the base class pure virtual function is necessary as if we don’t do that. Then, the derived class also is classified as abstract class let us see what error comes if we do this,

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display() = 0;
};
class Derived : public Base
{
public:
// not defining base class virtual function (pure) here
};

int main()
{

// this will cause error as we have created
// object of base class

Derived derived;
derived.display();
return 0;
}
error: cannot declare variable ‘derived’ to be of abstract type ‘Derived’
 Derived derived;

​The base class can have it’s own constructor

#include <iostream>
using namespace std;

class Base
{
public:
int x, y;
virtual void display() = 0;
Base(){
x=1; y=1;
}
};
class Derived : public Base
{
public:
Derived(){
x=10; y=10;
}
void display()
{
cout<<"Values are -\n"<< x << " and "<< y;
}
};

int main()
{

Derived derived;
derived.display();
return 0;
}

Output

Values are –

10 and 10

Inline Definition of Pure Virtual Functions is illegal. But, still you can define it outside the class

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display() = 0;
};

void Base :: show()

//Pure Virtual definition, can define
//and wont cause error but wont be printed

{
cout << "Pure Virtual definition\n";
}
class Derived : public Base
{
public:
void display()
{
cout<<"Inside the Derived Class\n";
}
};

int main()
{

Derived derived1;

derived1.display();

return 0;
}