Loading...
C++ Abstract Class and Pure Virtual Function

C++ Abstract Class and Pure Virtual Function

In this tutorial, we will learn about pure virtual functions and abstract classes with the help of examples.

Pure Virtual Function

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function start with virtual keyword and ends with = 0.

Syntax of Pure virtual function :

virtual f() = 0;

When to use Pure virtual Function :

Pure virtual functions are used

  • if a function doesn't have any use in the base class
  • but the function must be implemented by all its derived classes

Let's take an example,

Suppose, we have derived Car, Bike and Cycle classes from the Vehicle class, and we want to calculate the Speed of all these Vehicles.

In this case, we can create a pure virtual function named calculateSpeed() in the Vehicle. Since it's a pure virtual function, all derived classes Car, Bike and Cycle must include the calculateSpeed() function with implementation.

A pure virtual function doesn't have the function body and it must end with = 0. For example,

class Vehicle {
    public:

      // creating a pure virtual function
      virtual void calculateSpeed() = 0;
};

Note: The = 0 syntax doesn't mean we are assigning 0 to the function. It's just the way we define pure virtual functions.


Abstract Class

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. A class that contains a pure virtual function is known as an abstract class.
In the above example, the class Vehicle is an abstract class.

We cannot create objects of an abstract class. However, we can derive classes from them, and use their data members and member functions (except pure virtual functions).


Characteristics of Abstract Class

  1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can be created.
  2. Abstract class can have normal functions and variables along with a pure virtual function.
  3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
  4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.

Example 1: C++ Abstract Class and Pure Virtual Function

#include <iostream>
using namespace std;

// Abstract class
class Base {
   public:
    
    // pure virtual Function
    virtual void show() = 0;
};

// Derived class
class Derived : public Base {
   public:
    void show() {
        cout << "Implementation of Abstract class and Pure virtual function.";
    }
};

int main() {
    Derived obj;
    obj.show();
    return 0;
}

Output

Implementation of Abstract class and Pure virtual function.

Example 2: C++ Abstract Class and Pure Virtual Function

// C++ program to calculate the area of a square and a circle

#include <iostream>
using namespace std;

// Abstract class
class Shape {
   protected:
    float dimension;

   public:
    void getDimension() {
        cin >> dimension;
    }

    // pure virtual Function
    virtual float calculateArea() = 0;
};

// Derived class
class Square : public Shape {
   public:
    float calculateArea() {
        return dimension * dimension;
    }
};

// Derived class
class Circle : public Shape {
   public:
    float calculateArea() {
        return 3.14 * dimension * dimension;
    }
};

int main() {
    Square square;
    Circle circle;

    cout << "Enter the length of the square: ";
    square.getDimension();
    cout << "Area of square: " << square.calculateArea() << endl;

    cout << "\nEnter radius of the circle: ";
    circle.getDimension();
    cout << "Area of circle: " << circle.calculateArea() << endl;

    return 0;
}

Output

Enter length to calculate the area of a square: 5
Area of square: 25

Enter radius to calculate the area of a circle: 7
Area of circle: 153.86

In this program,
virtual float calculateArea() = 0; inside the Shape class is a pure virtual function.

That's why we must provide the implementation of calculateArea() in both of our derived classes, or else we will get an error.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Abstract Class & Pure Virtual Function in C++.

Keep Learning : )

In the next tutorial, you'll learn about C++ Encapsulation.

- Related Topics