Inheritance & Polymorphism
Abstract Classes & Interfaces
Pure virtual functions, abstract base classes that cannot be instantiated, interfaces, and the final keyword — designing a clean class hierarchy.
Pure virtual functions
Sometimes a base class cannot sensibly implement a function. What is the area of a generic "Shape"? Declare it pure virtual with = 0: it has no body in the base, and every concrete derived class must override it.
virtual double area() const = 0; // pure virtualAbstract classes
A class with at least one pure virtual function is an abstract class:
- You cannot create objects of it:
Shape s;is a compile error. - You can have pointers and references to it.
- A derived class that does not override every pure virtual function is abstract too.
#include <iostream>
#include <cmath>
using namespace std;
class Shape { // abstract base class
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual const char *name() const = 0;
void describe() const { // concrete function using the abstract ones
cout << name() << ": area " << area() << ", perimeter " << perimeter() << endl;
}
virtual ~Shape() = default;
};
class Circle : public Shape {
double r;
public:
explicit Circle(double r) : r(r) {}
double area() const override { return M_PI * r * r; }
double perimeter() const override { return 2 * M_PI * r; }
const char *name() const override { return "Circle"; }
};
class Square : public Shape {
double s;
public:
explicit Square(double s) : s(s) {}
double area() const override { return s * s; }
double perimeter() const override { return 4 * s; }
const char *name() const override { return "Square"; }
};
int main() {
// Shape s; // error: cannot declare variable 's' to be of abstract type
Circle c(1);
Square q(2.5);
const Shape &a = c, &b = q;
a.describe();
b.describe();
return 0;
}Circle: area 3.14159, perimeter 6.28319 Square: area 6.25, perimeter 10
Interfaces
C++ has no interface keyword (Java does). An interface in C++ is simply an abstract class with only pure virtual functions and a virtual destructor. A class can implement several interfaces through multiple inheritance:
#include <iostream>
#include <string>
using namespace std;
class Printable { public: virtual void print() const = 0; virtual ~Printable() = default; };
class Payable { public: virtual double amountDue() const = 0; virtual ~Payable() = default; };
class Invoice : public Printable, public Payable {
string client;
double hours, rate;
public:
Invoice(string c, double h, double r) : client(c), hours(h), rate(r) {}
double amountDue() const override { return hours * rate; }
void print() const override { cout << "Invoice for " << client << ": Rs " << amountDue() << endl; }
};
void printAll(const Printable *items[], int n) { for (int i = 0; i < n; i++) items[i]->print(); }
int main() {
Invoice a("Acme Labs", 12, 1500), b("Nova Foods", 4.5, 2000);
const Printable *docs[] = {&a, &b};
printAll(docs, 2);
const Payable &p = a;
cout << "Total due from Acme: " << p.amountDue() << endl;
return 0;
}Invoice for Acme Labs: Rs 18000 Invoice for Nova Foods: Rs 9000 Total due from Acme: 18000
final
final stops further overriding or inheritance:
class Base { virtual void f(); };
class Mid : public Base { void f() override final; }; // no class may override f again
class Leaf final : public Mid { }; // no class may inherit from LeafAbstract class vs concrete class
| Abstract class | Concrete class |
|---|---|
| has at least one pure virtual function | all functions have bodies |
| cannot be instantiated | can create objects |
| defines a contract for derived classes | provides a full implementation |
| used through pointers/references | used directly or through pointers |
Test yourself
4 questions · pick an answer to check it instantly
- 1
What makes a class abstract?
A pure virtual function (= 0) makes the class abstract. - 2
Which statement about an abstract class
Shapeis true?Abstract classes cannot be instantiated but can be used through pointers and references, and they may have constructors and ordinary functions. - 3
A class derives from an abstract class but overrides only some of its pure virtual functions. It is...
It still has an unimplemented pure virtual function. - 4
How is an "interface" usually written in C++?
C++ has no interface keyword; a pure abstract class plays that role.