EXPLORATION 37

image

Inheritance

The previous Exploration introduced general OOP principles. Now it’s time to see how to apply those principles to C++.

Deriving a Class

Defining a derived class is just like defining any other class, except that you include a base class access level and name after a colon. See Listing 37-1 for an example of some simple classes to support a library. Every item in the library is a work of some kind: a book, a magazine, a movie, and so on. To keep things simple, the class work has only two derived classes, book and periodical.

Listing 37-1.  Defining a Derived Class

class work{public:  work() = default;  work(work const&) ...

Get Exploring C++ 11, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.