Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Abstract Class in C++

C++ Abstract Classes

Abstract classes are special C++ Classes which are declared but they can not be initialised, i.e. in other words you can not create the objects for the same. However, you can derive other child classes from the abstract class and then create the objects.

  • Some website refer the definition as an abstract being a class which has atleast one Pure Virtual Function in it. Now, that is incorrect.
  • Pure Virtual functions are one way from which we can fulfil the implementation of abstract class
  • But, there are other methods too in C++, which are too advanced for the topic of discussion.

For layman terms you can though consider that an abstract class can have atleast one Pure Virtual function (Don’t worry we will learn what Pure Virtual Functions are, in this post)

Abstract Class in C++

Why we may need Abstract Classes

  1. Sometimes, from a programmers point of view he may just only want to create a class for better visualization and not use to store data or use functions
  2. This may also help in reducing the reducing the size of code is architecture is good

To achieve this C++ has the concept of pure virtual function and abstract classes.

For example, shape class which may be necessary to define data members that can be inherited by Rectangle or Circle class. But, the shape class can’t have the definition of the calculateArea() function as we don’t have specific information. But, the derived class will have their own implementation of calculateArea().

Must have feature of Abstract Classes

  • We can only declare an abstract class, but can instantiate it.
  • However, creation of pointed references is allowed, to support Runtime polymorphism
  • Derived class generally use abstract classes to create interfaces
  • They are used for upcasting
  • If we use Pure Virtual functions to implement derived class then, we must implement all those functions in the derived classes without fail.
  • Else by inheritance they will be abstract classes too.

Pure Virtual Functions

You must already know what Virtual functions are, they are used to support Runtime polymorphism and avoid early binding caused by static resolution at compile time.

  • Any function in the base class which is preceded with keyword virtual are essentially Virtual functions.
  • Pure Virtual functions are extension to Virtual functions and help us implement the concept of abstract classes properly.

The following is condition for a function to be a virtual function –

  • should end with = 0;

For a virtual function be to pure virtual function it must have virtual keyword as

virtual void start() = 0

Let’s have a look at the whole program and understand the same.

class Vehicle
{
public:
virtual void features() = 0;
};

Example of a virtual function would be –

#include <iostream>
using namespace std;

class Parent
{
public:
virtual void print() = 0;
};
class Child : public Parent
{
public:
void print()
{
cout<<"Printing from Child(derived) class\n";
}
};

int main()
{

// we will try to implement this with both
//pointer references and normal child class objects
Child child1;
Parent *parent;
Child child2;
parent = &child2;

child1.print();
parent->print();

return 0;
}

Output –

Printing from Child(derived) class

Printing from Child(derived) class

The output in both cases is the same.

  • In the first one since, object is of child class its obvious that child class
  • In the second one runtime polymorphism is caused and it overrides the problem of static resolution at compile time early binding.