Abstract Classes

In the Polymorphism lesson, Animal::sound returned "...". There is no real sound for "some animal", so that answer was made up. It was only there because the method needed a body.

C++ has a better way. You can declare a virtual method with no body by writing = 0 at the end. This is called a pure virtual function. A class that has at least one pure virtual function is an abstract class.

#include <iostream>
using namespace std;

class Shape {
public:
    virtual int area() = 0;
};

class Rectangle : public Shape {
public:
    int width, height;

    Rectangle(int w, int h) {
        width = w;
        height = h;
    }

    int area() override {
        return width * height;
    }
};

class Square : public Shape {
public:
    int side;

    Square(int s) {
        side = s;
    }

    int area() override {
        return side * side;
    }
};

int main() {
    Rectangle r(4, 5);
    Square q(3);
    Shape* shapes[2] = {&r, &q};
    for (int i = 0; i < 2; i++) {
        cout << shapes[i]->area() << "\n";
    }
    return 0;
}

Output:

20
9

virtual int area() = 0; says two things. Every shape has an area, but Shape itself does not know how to calculate it. Each derived class writes its own area, and the loop calls the right one through the Shape* pointers, as in the last lesson.

You cannot create an abstract object

A plain Shape has no area to run, so C++ does not let you create one:

#include <iostream>
using namespace std;

class Shape {
public:
    virtual int area() = 0;
};

int main() {
    Shape s;
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:10:11: error: cannot declare variable ‘s’ to be of abstract type ‘Shape’

A pointer of type Shape* is still allowed, like the array in the first example. It points to a rectangle or a square, and those are complete objects.

Forgetting the method

A derived class that does not write area is abstract too. Here Circle forgot it:

#include <iostream>
using namespace std;

class Shape {
public:
    virtual int area() = 0;
};

class Circle : public Shape {
public:
    int radius = 2;
};

int main() {
    Circle c;
    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:15:12: error: cannot declare variable ‘c’ to be of abstract type ‘Circle’

Further down, the message names the missing method: ‘virtual int Shape::area()’.

This is the main reason to use = 0. In the Polymorphism exercise, Shape::area returned 0. With that version, a class that forgets area still compiles, and its area is quietly 0. With = 0, the compiler stops you before the program runs.

Why abstract classes help

An abstract class is a list of rules for its derived classes. Shape says "every shape must have an area". Code like the loop above only needs that rule. It works for every shape, including shapes you add later.

A virtual destructor

A base class like Shape should also have a virtual destructor. Then deleting an object through a Shape* runs the destructor of the real type:

#include <iostream>
using namespace std;

class Shape {
public:
    virtual int area() = 0;
    virtual ~Shape() {
        cout << "Shape removed" << "\n";
    }
};

class Square : public Shape {
public:
    int side = 3;

    int area() override {
        return side * side;
    }

    ~Square() {
        cout << "Square removed" << "\n";
    }
};

int main() {
    Shape* p = new Square();
    cout << p->area() << "\n";
    delete p;
    return 0;
}

Output:

9
Square removed
Shape removed

Both destructors ran: first the one of Square, then the one of Shape. When we removed the word virtual from ~Shape(), the program printed only Shape removed, and the Square destructor never ran. By the rules of C++ this is a bug, and the result is not even certain. So when a class has virtual methods, make its destructor virtual too. Usually it is empty: virtual ~Shape() {}.

In the exercise, make perimeter a pure virtual function of Shape, and write the missing perimeter method of Square.