13.1 What is Inheritance
13.1.1 How to declare a derived class
13.1.2 The Protected Keyword
13.2.3 A demonstration on creating a base class and derived classes
\ue000Understand the concepts behind inheritance
\ue000Understand how to derive one class from another.
\ue000Understand the correct use of the \u2018protected\u2019 access specifier.
\ue000Understanding ho to use constructors and destructors in inheritance
In the previous sessions you learnt how to declare classes and create objects using them. In all the sessions we have created only a single class. In OOP it is possible to declare many classes in a single programme.
Inheritance is a way of creating new classes using existing classes that has been already defined. The new classes that are created are called derived classes and the already existing class is called the base class. A base class will have more than one or more
Inheritance is sometimes called generalization, because the relationship between the base class and the derived classes represent a hierarchy between classes of objects. The concept of inheritance can be easily understood using simple real world examples.
In example 13.1, in order to create data members it is vital to consider the characteristics of a bird. All the birds have feathers and beaks. Hence two attributes of the birds class can be \u2018feathers\u2019 and \u2018beaks\u2019. If birds like crow, parrot or eagle are derived from the birds class they inherit those attributes as well. As a result these three derived classes have the characteristics or data members, \u2018feathers\u2019 and \u2018beaks\u2019. In the real world also it is obvious that those birds have feathers and beaks.
In considering the behavior, birds can fly; they eat using the beak etc. These can be defined as methods of the base class. Then all the derived classes inherit these methods and the programmer doesn\u2019t have to state explicitly that the crow, parrot and the eagle fly, because it inherit that from the bird class.
The derived classes can be further divided. There are several types of parrots. For example Eclectus parrot, African gray parrot, Hawkheaded parrot etc. They also have all the characteristics of a parrot but they differ in other aspects like color, type of food they eat etc. Then the classes like Eclectus parrot, African gray parrot, Hawkheaded parrot becomes the derived classes of parrot and the parrot class in the base class for these derived classes.
The keyword \u2018class\u2019 followed by the name of the derived class, needs to be specified first, just like declaring a normal class. Then a colon after the class name should be used followed by type of derivation (public, private or protected) and then the class from which it derives.
When the data members are declared using the protected access specifier, the members are accessible from within the methods of the same class and from the members of their derived classes. Protected data members and functions are fully visible to derived classes, but are otherwise private.
This action might not be possible to undo. Are you sure you want to continue?