Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

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 CarBike 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 CarBike 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 at least 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 references 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.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!