14 Inheritance Extending Classes: L L L L L
14 Inheritance Extending Classes: L L L L L
14 Inheritance Extending Classes: L L L L L
14.1 Introduction
This lesson discusses about inheritance, the capability of one class to inherit properties from another class as a child inherits some properties from his/her parents. The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money and efforts of writing the code again. Without redefining the old class, you can add new properties to desired class and redefine an inherited class member function.
14.2 Objectives
After going through this lesson, you would be able to explain the concept of inheritance describe the five forms of inheritance define three types of inheritance explain all three visibility modes describe the concept of abstract class & virtual class
(ii)
(iii)
B (ii) Multiple inheritance > A derived class with several base classes is called multiple inheritance.
C (iii) Multilevel inheritance > The mechanism of deriving a class from another derived class is called multilevel inheritance.
(iv)
Hierarchical inheritance > One class may be inherited by more than one classes. This process is known as hierarchical inheritance.
(v)
When we say that members of a class are inheritable, it means that the derived class can access them directly. However, the derived class has access privilege only to the non-private members of the base class. Although the private members of the base class cannot be accessed directly, yet the objects of derived class are able to access them through the non-private inherited members.
(ii)
(iii)
Base class Access specifier public private public public Not inherited protected The Public Visibility mode protected
Derived class private private Not inherited protected protected protected Not inherited Protected
The following example and figure illustrate the public derivation in classes. class student { private : int x; void getdata ( ); public: int y; void putdata ( ); protected: int z; void check ( ); }; class marks : public student { private : int a ; void readdata ( ); public : int b; void writedata ( ); protected :
int c; void checkvalue ( ); }; class student Private section x getdata ( ) class marks private section a readdata ( )
Inherited from base class student Fig. 14.1 public derivation of a class The public derivation does not change the access specifiers for inherited members in the derived class. The private data of base class student cannot be inherited. The Private Visibility Mode We are using the same example, but the derivation is done in private mode. Class student { // same as in previous example
}; class marks : private student { // }; The following figure illustrates the private derivation in the classes. class student private section x getdata ( ) class marks private section a y z public section y putdata ( ) readdata ( ) putdata ( ) check ( )
Inherited from class student Fig 14.2 Private derivation of a class As it is clear from the figure that the data present in public and protected section of base class become the private members of derived class. The data in private section of base class cannot be inherited. The Protected visibility mode We are using the same example but the derivation is done in protected mode. class student {
// same as in previous example }; class marks : protected student { }; The following figure illustrates the protected derivation in the classes. class student private section x getdata ( ) class marks private section a readdata ( )
Inherited from class student Fig. 14.3 Protected derivation of a class The data present in private section of base class cannot be inherited. The difference between private and protected section is that data present in protected section can be inherited. Otherwise both the section cannot be accessed by the object of the class.
concept in program development and provides a base upon which other classes may be built. In the previous example, the class student is an abstract class since it was not used to create any object.
fees
academics
child
Fig. 14.4 The child has two direct base classes fees and academics which themselves have a common base class school. The child inherits the traits of school via two separate paths. It can also inherit directly as shown by the broken line. The school is sometimes referred to as indirect base class. All the public and protected members of school are inherited into child twice, first via fees and again via academics. This means, child would have duplicate sets of the members inherited from school. This introduces ambiguity and should be avoided. The duplication of inherited members due to these multiple paths can be avoided by making the common base class as virtual class by declaring the base class as shown below : class school { }; class fees : virtual public school { }; -----------------------------------------------------------
class academics : public virtual school { }; class child : public fees, public academics { / / only one copy of school will be inherited. }; Note : you can write either virtual public or public virtual. --------------------------------------
(c) (d)
8.
State whether the following are True or False. (a) (b) Inheritance means child receiving certain traits from parents. The default base class is visible as public mode in derived class.
When a derived class is derived from more than one base class then the inheritance is called hierarchical inheritance. The base class is called abstract class Private data of base class can be inherited
Consider the following class declaration and answer the questions given below: class zoo { char location [20]; protected; int no_of_animals; public; void inputdata (char, int); vod outputdata ( ); }; class animal : protected zoo { int tail; protected; int legs; public: void readdata (int, int) ; void writedata ( ); }; class carnivorous : private animal { int paw_size; public : void fetchdata (int); void displayed ( ) ; }; (a) (b) Name the base class and derived class of the class animal. Name the data member(s) that can be accessed from function displayed ( ).
(c)
Name the data member(s) that can be accessed by an object of carnivorous class. Is the member function outputdata accessible to the objects of animal class ?
(d)
}; class bus : private heavy_vehicle { char make [20]; public : void fetchdata (int); void displaydata ( ) ; }; (i) (ii) Name the base class and derived class of the class heavy_vehicle. Name the data member(s) that can be accessed from function displaydata. Name the data member(s) that can be accessed by an object of bus class? Is the member function outputdata accessible to the objects of heavy_vehicle class?
(iii)
(iv)
2.
5. 6. 7.
The data in protected section can be inherited. The base class is called an abstract class. (a) (b) (c) (d) Abstract class public, private, protected private multiple T F F T F base class - zoo derived class - carnivorous (b) (c) (d) legs, no-o-animals, paw_size None No
8.
9.
(a)