C++ Abstract Classes and Interfaces

An abstract class has at least one pure virtual function (written with = 0). You can not create an object of it. It defines a contract that the derived classes must follow.

#include <iostream>
#include <memory>
#include <vector>

class Payment {                                       // an interface
public:
    virtual ~Payment() = default;
    virtual void pay(double amount) const = 0;        // pure virtual
};

class CreditCard : public Payment {
public:
    void pay(double amount) const override { std::cout << "Paid " << amount << " by card\n"; }
};

class Upi : public Payment {
public:
    void pay(double amount) const override { std::cout << "Paid " << amount << " by UPI\n"; }
};

void checkout(const Payment& method, double total) {
    method.pay(total);
}

int main() {
    checkout(CreditCard{}, 1500);
    checkout(Upi{}, 250.5);
}
Output

Paid 1500 by card
Paid 250.5 by UPI

You can not create an object

struct Shape {
    virtual double area() const = 0;
};

int main() {
    Shape s;
}
Output

Abstract.cpp: In function 'int main()':
Abstract.cpp:6:11: error: cannot declare variable 's' to be of abstract type 'Shape'
6 | Shape s;
| ^
Abstract.cpp:1:8: note: because the following virtual functions are pure within 'Shape':
1 | struct Shape {
| ^~~~~
• 'virtual double Shape::area() const'
Abstract.cpp:2:20:
2 | virtual double area() const = 0;
| ^~~~

A derived class must implement all pure virtual functions

struct Shape {
    virtual double area() const = 0;
};

struct Square : Shape { };        // forgot area()

int main() {
    Square q;
}
Output

Forgot.cpp: In function 'int main()':
Forgot.cpp:8:12: error: cannot declare variable 'q' to be of abstract type 'Square'
8 | Square q;
| ^
Forgot.cpp:5:8: note: because the following virtual functions are pure within 'Square':
5 | struct Square : Shape { }; // forgot area()
| ^~~~~~
• 'virtual double Shape::area() const'
Forgot.cpp:2:20:
2 | virtual double area() const = 0;
| ^~~~

Abstract class with some code

A pure virtual function can still have a body, and an abstract class can have normal functions and data. This is the “template method” idea:

#include <iostream>

class Report {
public:
    virtual ~Report() = default;
    void print() const {                                 // fixed steps
        std::cout << "--- " << title() << " ---\n" << body() << "\n--- end ---\n";
    }
private:
    virtual std::string title() const = 0;
    virtual std::string body() const = 0;
};

class SalesReport : public Report {
    std::string title() const override { return "Sales"; }
    std::string body() const override { return "Total: 120 items"; }
};

int main() {
    SalesReport().print();
}
Output

— Sales —
Total: 120 items
— end —

Note: C++ has no interface keyword. An abstract class with only pure virtual functions is an interface. In modern C++, concepts (C++20) are another way to describe a contract for templates. Next: C++ Operator Overloading.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.