C++ Inheritance


The main thrust in a C++ project is toward developing classes that solve a given problem. These classes are generally built incrementally starting from basic, simple classes, using inheritance.

When creating a new class, instead of writing completely new data members and member functions, the programmer can designate that the new class is to inherit the data members and member functions of a previously defined base class, sometimes called the parent class. When inheriting features from a parent, you can make the derived class extend them, restrict them, change them, eliminate them, or use them without any changes. All these variations can be grouped into two basic object-oriented techniques. The first is called feature restriction, which limits or eliminates a feature of the parent. The second is called feature expansion, which adds something to a parent feature.

Anytime you build a new class, you should determine wheather you can use a pre-existing class a a base class.

In C++ not everything can be passed on through inheritance. A few special cases are inconsistent by definition with inheritance. The following cannot be inherited:

When a class is derived from a base class, all the names in the base class automatically become private in the derived class. This is the default behavior in C++, but it can be changed using base class access specifiers. There are three of these: