Abstract Classes & Interfaces
Pure virtual functions
class Shape {
public:
virtual double area() const = 0; // = 0 makes it pure virtual
virtual ~Shape() = default;
};
- Has no implementation in the base (usually)
- Every concrete derived class must override it
Abstract class
A class with at least one pure virtual function is abstract:
- ❌ Can't create objects of it:
Shape s;→ error - ✅ Can have pointers/references to it:
Shape& s = circle; - Can still have data members and normal functions
A derived class that doesn't override all pure virtual functions is also abstract.
Interfaces
C++ has no interface keyword. An interface is an abstract class with only pure virtual functions (and a virtual destructor):
class Printable {
public:
virtual void print(std::ostream& os) const = 0;
virtual ~Printable() = default;
};
A class can implement several interfaces through multiple inheritance (safe, because interfaces have no data).
Why use them?
- Define a contract: "anything that is a PaymentMethod can pay(amount)"
- Write code against the interface, not specific classes → easy to add new types later (Open/Closed principle)
- Easy testing: swap in a fake implementation
Template Method pattern
The base defines the algorithm in a normal function; derived classes fill in the steps:
void process() { validate(); charge(); receipt(); } // fixed steps
virtual void charge() = 0; // varying step
Pure virtual with a body
A pure virtual function can have a definition (derived classes call it with Base::f()), and a pure virtual destructor must have one. Rare but legal.
Example
#include <iostream>
#include <memory>
#include <string>
#include <vector>
class PaymentMethod { // abstract base
public:
virtual ~PaymentMethod() = default;
virtual std::string name() const = 0; // pure virtual
virtual double fee(double amount) const = 0;
// Template method: the steps are fixed, the details vary
bool pay(double amount) const {
if (amount <= 0) { std::cout << " invalid amount\n"; return false; }
double total = amount + fee(amount);
std::cout << " " << name() << ": paid Rs. " << amount << " + fee " << fee(amount)
<< " = Rs. " << total << '\n';
return true;
}
};
class Cash : public PaymentMethod {
public:
std::string name() const override { return "Cash"; }
double fee(double) const override { return 0; }
};
class Card : public PaymentMethod {
public:
std::string name() const override { return "Card"; }
double fee(double amount) const override { return amount * 0.02; } // 2%
};
class Wallet : public PaymentMethod {
public:
explicit Wallet(std::string provider) : provider_(std::move(provider)) {}
std::string name() const override { return provider_ + " wallet"; }
double fee(double amount) const override { return amount > 1000 ? 10 : 0; }
private:
std::string provider_;
};
class Printable { // an interface: only pure virtual functions
public:
virtual ~Printable() = default;
virtual void print() const = 0;
};
class Receipt : public Printable {
public:
void print() const override { std::cout << " -- receipt printed --\n"; }
};
int main() {
// PaymentMethod p; // error: abstract class
std::vector<std::unique_ptr<PaymentMethod>> methods;
methods.push_back(std::make_unique<Cash>());
methods.push_back(std::make_unique<Card>());
methods.push_back(std::make_unique<Wallet>("eSewa"));
for (double amount : { 500.0, 2500.0 }) {
std::cout << "Paying " << amount << ":\n";
for (const auto& m : methods) m->pay(amount);
}
methods[1]->pay(-5);
Receipt r;
const Printable& p = r;
p.print();
return 0;
}
/* Output:
Paying 500:
Cash: paid Rs. 500 + fee 0 = Rs. 500
Card: paid Rs. 500 + fee 10 = Rs. 510
eSewa wallet: paid Rs. 500 + fee 0 = Rs. 500
Paying 2500:
Cash: paid Rs. 2500 + fee 0 = Rs. 2500
Card: paid Rs. 2500 + fee 50 = Rs. 2550
eSewa wallet: paid Rs. 2500 + fee 10 = Rs. 2510
invalid amount
-- receipt printed --
*/