course of C++ programming language

lecture 9: inheritance


Inheritance and composition

You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone else has built and debugged.

You can simply create objects of your existing class inside the new class. This is called composition because the new class is composed of objects of existing classes.

You can create a new class as a type of an existing class. You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called inheritance, and most of the work is done by the compiler. Inheritance is one of the cornerstones of object-oriented programming.

Syntax

Actually, you've been using composition all along to create classes. You've just been composing classes primarily with built-in types. It turns out to be almost as easy to use composition with user-defined types.

class X { int i; public: X () { i = 0; } void set (int ii) { i = ii % 100; } int read () const { return i; } int permute () { return i = i * 41 % 100 ; };

Accessing the member functions of the embedded object (referred to as a subobject) simply requires another member selection.

class Y { int i; X x; // embedded object public: Y () { i = 1; } void s (int ii) { i = ii; x.set(ii); } int r () const { return i * x.read(); } int permute () { return x.permute(); } };

When you inherit, you are saying: "this new class is like that old class". You state this in code by giving the name of the class as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or base classes, separated by commas, for multiple inheritance). When you do this, you automatically get all the data members and member functions in the base class.

class Z : public X { int i; // different from X's i public: Z () { i = 1; } int change () { return i = permute(); // different name call } void set (int ii) { i = ii; X::set(ii); // same-name function call } };

You can see Z being inherited from X, which means that Z will contain all the data elements in X and all the member functions in X. In fact, Z contains a subobject of X just as if you had created a member object of X inside Z instead of inheriting from X. Both member objects and base class storage are referred to as subobjects.

Granting access

When a base class is inherited as private, all public and protected members of that class become private members of the derived class. However, in certain circumstances, you may want to restore one or more inherited members to their original access specification.

Order of constructor and destructor calls

...

Member functions that don't automatically inherit

...

Upcasting

...


References
  1. B.Stroustrup: The C++ programming language. Third edition.
    Section 12: derived classes; pp. 301-325.
  2. Bruce Eckel: Thinking in C++. Second edition.
    Section 14: inheritance and composition; pp. 583-623.