CHAPTER
16
INHERITANCE:
REUSABILITY AND EXTENDABILITY
The world around us is made of objects that share many similarities. These similarities can be classified into common groups. For example, in biology taxonomy organisms are classified into a hierarchy of group, from general to specific. In C++, objects are created from classes; and a class may share (inherit) some common data members and member functions that belong to another class or classes. As a result, a new class can be created based on an existing class rather than creating it from scratch. An inherited class can have its own data and function members and can modify or override the inherited members. Inheritance is an important tool of Object-Oriented Programming (OOP), because it promotes reusability and ease of extensibility by building on what is already there and customizing it as desired. A programmer can build a hierarchy that goes from a general class to a specific class by incorporating inheritance. With class hierarchy a program is easier to follow, debug, and modify. With inheritance, a class is built on an existing class that has already been tested; therefore, inheritance reduces the time and the cost of development as well as minimizes errors in the program. Inheritance is also known as derivation or specialization. In fact, inheritance is not something new. For example, humans have organized knowledge into hierarchical structures such as the animal kingdoms.
The general form of a class inherited from another class is shown in fig 9.1. The access specifier (access control) can be public, private or protected. The syntax for class inheritance is the same as a regular class except that after the class name (derivedname) there is a single colon, the derivation access type (accessspecifier), and the name of the class (baseclassname) from which it is derived.
class derivedname : accessspecifier baseclassname{
accessspecifier: memberdata;
accessspecifier: memberfunctions; };
Figure 16.1 – Class inheritance syntax