Topic 12

Abstract Classes & Interfaces (Pure Virtual Functions)

3 min read3 questions to test yourself

1Definition

An Abstract Class is a class designed specifically to serve as a base blueprint and cannot be directly instantiated. In C++, a class becomes abstract when it contains at least one Pure Virtual Function (declared by appending = 0). An Interface is a pure abstract class containing only pure virtual functions with no data members or implemented methods.

2Simple Explanation

Think of an Abstract Class as a Master Contractual Standard. If a governing authority issues a rule stating "Every motorized road vehicle must have a brake() method", the authority does not build the actual cars. Instead, every automobile manufacturer (derived class) is legally forced to implement their own working brake() system to satisfy the contract.

3Real-Life Example / Analogy

  • Shape Contract: The concept of a generic Shape. You cannot draw a physical "Shape" on paper—you can only draw concrete forms like a Circle, Rectangle, or Square. Shape defines an abstract method draw() = 0, forcing derived classes to provide concrete implementations.

4Diagram / Visual Representation

Abstract base: Shape
virtual void draw() = 0

Rectangle
implements draw()

Square
implements draw()

5Working / How It Works

  1. Declare a pure virtual function in the base class: virtual void functionName() = 0;.
  2. Any attempt to create an object of this abstract base class (Base b;) triggers a compilation error.
  3. Derived classes must override every pure virtual function; otherwise, the derived class itself becomes abstract.

6Types / Components / Characteristics

  • Abstract Class vs Interface:
Feature Abstract Class (C++) Pure Interface (C++)
Methods Mix of pure virtual & concrete methods 100% Pure Virtual Functions (= 0)
Data Members Can contain instance variables Contains zero data members (only constants)
Purpose Partial code reusability + contract Pure architectural interface contract

7Solved Examples / Problems

Problem Statement: Write a C++ program implementing an Abstract Base Class Shape with pure virtual functions inherited by Rectangle and Square.

C++
#include <iostream>
using namespace std;
// Abstract Base Class
class Shape {
public:
    virtual void draw() = 0; // Pure Virtual Function
};
class Rectangle : public Shape {
public:
    void draw() override {
        cout << "Drawing Rectangle" << endl;
    }
};
class Square : public Shape {
public:
    void draw() override {
        cout << "Drawing Square" << endl;
    }
};
int main() {
    // Shape s; // Error! Cannot instantiate abstract class
    Shape* s1 = new Rectangle();
    Shape* s2 = new Square();
    s1->draw();
    s2->draw();
    delete s1;
    delete s2;
    return 0;
}

Output:

Output
Drawing Rectangle
Drawing Square

8Advantages, Disadvantages & Applications

  • Advantages:
    • Enforces strict interface compliance across polymorphic subclasses.
    • Decouples client code from concrete implementations.
  • Disadvantages:
    • Forces child classes to implement all pure virtual methods even if unneeded.
  • Applications: Cross-platform plugin interfaces, graphics driver frameworks.

9Important Points to Remember

  • A class with at least one pure virtual function cannot be instantiated.
  • Base pointers can point to derived objects of abstract classes.

10Test Your Understanding

  1. Conceptual Question: How many objects can be instantiated directly from an Abstract Base Class?
    Show answer
    Exactly Zero. Abstract classes cannot be instantiated.
  2. MCQ: What C++ syntax marks a virtual function as a pure virtual function?
    • A) virtual void show() = null;
    • B) virtual void show() = 0;
    • C) pure virtual void show();
    • D) void show() = pure;
    Show answer
    B) virtual void show() = 0;
  3. Application Question: What happens if a derived class fails to override a pure virtual function from its parent?
    Show answer
    The derived class also becomes an abstract class and cannot be instantiated.

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic