Summary: In this tutorial, we will learn what pure virtual function and abstract class are in C++, and why and when we should use them in our program.

Prerequisite: Virtual Function

Introduction to Pure Virtual Function and Abstract Class

A virtual function whose declaration ends with =0 and has no body is called a pure virtual function.

Example:

virtual void functionName() =0;

A class that contains at least one pure virtual function is called an abstract class.

Example:

//Abstract Class
class Account{
public:
    double balance;
    Account(){
        balance = 0.0;
    }
 
    void deposit(double amt){
        balance += amt;
        cout << amt <<" Deposited" << endl;
    }
    //Pure Virtual Function
    virtual void withdraw(double amt) =0 ;
};

Note: If a class contains only pure virtual functions, then it acts as an interface.

It is important to note that abstract class cannot be instantiated (i.e. its object cannot be created).

To use the functions and qualities of the abstract class we need to inherit it in another class.

What is the use of the Abstract Class?

If an abstract class is inherited by some other class, then it is necessary for the derived class to provide the implementation (i.e, override) for its pure virtual functions, otherwise, the derived class will also become abstract and as a result, the derived class also cannot be instantiated.

Therefore, an abstract class is mainly used as a common class to create a more specific class with different implementation details.

For example, If we want to create SavingAccount and CurrentAccount class in C++, it will be best to first create an abstract Account class that will provide the general details and functionality such as balance, withdrawal and deposit, etc which is common in both classes. Then inherit it to create a variety of accounts by providing the implementation of pure virtual functions present in the Account Class.

C++ Example: Abstract Class and Pure Virtual Function

Here is a C++ example, where we create an abstract Account class and inherit it to create different types of classes:

#include <iostream>
 
using namespace std;
 
//Abstract Class
class Account{
public:
    double balance;
    Account(){
        balance = 0.0;
    }
 
    void deposit(double amt){
        balance += amt;
        cout << amt <<" Deposited" << endl;
    }
    //Pure Virtual Function
    virtual void withdraw(double amt) =0 ;
};
 
class SavingsAccount :public Account{
public:
    double interestRate;
 
    SavingsAccount(){
        balance = 0.0;
        interestRate = 5.1;
    }
 
    //Providing implementation
    void withdraw(double amt){
        balance -= amt;
        cout << amt<<" Withdrawn \n";
    }
};
 
int main()
{
   SavingsAccount sa;
   sa.deposit(1000);
   sa.withdraw(400);
 
   cout << "Savings Account Balance: "<< sa.balance << endl;
}

Output:

Abstract Class Program

In the above example, we are overriding the withdraw() method of the Account in the SavingAccount to provide its implementation and call the same after creating an object in the main method.

Conclusion

Pure virtual function is a virtual function that ends with = 0 and has no body.

A class must contain at least one pure virtual function to become abstract.

We cannot create an object of an abstract class, rather we inherit it in another concrete class to use its properties and functions.

Leave a Reply