Abstract Class in 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 in C++ and then create the objects.

abstract classes in C++
  • 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)

Why we may need Abstract Classes in C++?

  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
virtual void features() = 0;
Example of a virtual function would be –
using namespace std;
class Parent
   virtual void print() = 0;
class Child : public Parent
   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;
   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.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription