How to create an abstract class in C++

Hi guys, today we will learn about abstract classes, pure virtual functions, and how to create an abstract class in C++ language.

Before moving to the code, let us define some important words i.e. abstract class and pure virtual functions.

Abstract Class: A class containing at least one pure virtual function is called an abstract class.

Pure Virtual Function: A function is said to be a pure virtual function iff the function is only declared and has no implementation. It is declared by assigning 0 to it.

Creating An Abstract Class

Besides, I would like to mention some properties of an abstract class.

  • We can not create an object of an abstract class.
  • An abstract class can have constructors
  • We can create a pointer of abstract class
  • If we do not override the pure virtual function in derived class then derived class also becomes an abstract class.

However, we will see examples of all the above-mentioned properties later.

Now, let us see how to create an abstract class in C++.

#include<bits/stdc++.h>
using namespace std;
class parent
{
    public:
    virtual int sum()=0;
};
class child:public parent
{
    int a,b;
    public:
    child(int x, int y)
    {
        a=x;
        b=y;
    }
    int sum()
    {
        return a+b;
    }
};
int main()
{
    child c(3,4);
    cout<<"sum = "<<c.sum();
    return 0;
}

This is the method to create an abstract class.

In this code, I have created an abstract class parent that contains a pure virtual function sum. After that, I have created a derived class named as child. And in this child class, I have implemented the sum(). So, after implementation, the sum() returns the sum of two numbers a and b.

In the main(), I have created an object of child class and use that object to call the sum().

So, the output for this code will be:

sum = 7

Now, let us see some examples of the properties mentioned in the beginning. I will be using the same code with some changes.

PROPERTY 1: We can not create an object of an abstract class

#include<bits/stdc++.h>
using namespace std;
class parent
{
public:
    virtual int sum()=0;
};
class child:public parent
{
    int a,b;
public:
    child(int x, int y)
    {
        a=x;
        b=y;
    }
    int sum()
    {
        return a+b;
    }
};
int main()
{
    parent p;                    //this statement will raise an error
    child c(3,4);
    cout<<"sum = "<<c.sum();
    return 0;
}

In the above code, we try to create an object of abstract class so it will raise an error. Let us see the output for the above code.

Compiler Error: cannot declare variable 'p' to be of abstract
 type 'parent' note: because the following virtual functions are pure 
within 'parent': note:     virtual int parent::sum()

PROPERTY 2: An abstract class can have constructors

#include<bits/stdc++.h>
using namespace std;
class parent
{
public:
    int c;
    parent(int z)
    {
        c=z;
    }
    virtual int sum()=0;
};
class child:public parent
{
    int a,b;
public:
    child(int x, int y,int z):parent(z)
    {
        a=x;
        b=y;
    }
    int sum()
    {
        return a+b+c;
    }
};
int main()
{
    child c(3,4,5);
    cout<<"sum = "<<c.sum();
    return 0;
}

This time, we created default constructors for the parent as well as child class, and hence the program will print the sum of 3 numbers. Output for the above code is:

sum = 12

PROPERTY 3: We can create a pointer of abstract class

#include<bits/stdc++.h>
using namespace std;
class parent
{
public:
    virtual int sum()=0;
};
class child:public parent
{
    int a=5,b=7;
public:
    int sum()
    {
        return a+b;
    }
};
int main()
{
    parent *temp= new child;
    cout<<"sum = "<<temp->sum();
    return 0;
}

In this case, I created a temp pointer of integer type which will store the address of a new object created of the child class. Hence, it will print the sum of 2 numbers.

Output:

sum = 12

PROPERTY 4: If we do not override the pure virtual function in derived class then derived class also becomes an abstract class

#include<bits/stdc++.h>
using namespace std;
class parent
{
public:
    virtual int sum()=0;
};
class child:public parent
{
    int a,b;
};
int main()
{
    child c;       //this statement will raise an error
    return 0;
}

Since we do not implement the function in the child class, so the child class also becomes the abstract class, and hence we can not create its object. Therefore, when we write child c; it will raise an error.

Compiler Error: cannot declare variable 'c' to be of abstract
 type 'child' note: because the following virtual functions are pure
 within 'child': note: virtual int parent:: sum()

You can learn more about pointers and constructors from this websites PointersConstructors

Leave a Reply

Your email address will not be published. Required fields are marked *