Published on November 6, 2016
1. P R E P A R E D B Y : S H R E Y A S S K E S H A V V U N D E R T H E G U I D A N C E O F : M R S . A R C H A N A S A I D S E C O M P , A I S S M S ’ I O I T , P U N E INHERITANCE
2. BASICS Object Oriented Programming has different unique and important features like classes, objects, inheritance and polymorphism. Inheritance is the concept using which one class can derive the properties of another class. This provides reusability in the program code. Due to this, cost of program development is reduced.
3. EXAMPLE Car is a classification of Four Wheeler. Here Car acquires the properties of a four-wheeler. Other classifications could be a jeep, tempo, van etc. Four Wheeler defines a class of vehicles that have four wheels, and specific range of engine power, load carrying capacity etc. Car (termed as a sub- class) acquires these properties from Four Wheeler (termed as a super-class), and has some specific properties, which are different from other classifications of Four Wheeler, such as luxury, comfort, shape, size, usage etc. A car can have further classification such as an open car, small car, big car etc, which will acquire the properties from both Four Wheeler and Car, but will still have some specific properties. This way the level of hierarchy can be extended to any level.
4. BASE CLASS AND DERIVED CLASS Base class is the class whose properties are inherited by another class. Derived class is the class which inherits the properties of another class. Base class is also known as parent class or old class. Derived class is also known as child class or new class. A derived class inherit the properties of base class and can have its own extra properties as well.
5. ACCESS SPECIFIERS Access specifiers specify the scope of the data declared in them, i.e. they tell us in which all parts of the program can those data members or member functions be accessed. C++ has provided us with 3 access specifiers, namely private, protected and public. Members declared as private in a class can only be accessed by the methods of the same class which are declared in public section. Members declared as protected are the same as private members. The only difference is that protected members can be inherited in adjacent derived classes whereas private members are not inherited at all. Members declared as public can be accessed anywhere in the program.
6. MODES OF INHERITANCE A class(derived class) can derive the properties of another class(base class) in 3 modes: private, protected and public. If the derived class inherits in private mode, both the protected as well as the public members of the base class become the private members of the derived class. If the derived class inherits in protected mode, both the protected and the public members of the base class become the protected members of the derived class. If the derived class inherits in public mode, then the protected members of the base class will become the protected members of the derived class, whereas the public members of the base class will become the public members of the derived class. Private members of the base class will not get inherited at any cost.
7. TYPES OF INHERITANCE There are 5 types of inheritance: 1. Single Inheritance 2. Multiple Inheritance 3. Multilevel Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
8. SINGLE INHERITANCE In this type of inheritance, a single derived class inherits the properties of a single base class. In the above example, B is the derived class inheriting the properties of base class A. A B
9. MULTIPLE INHERITANCE In this type of inheritance, a single derived class inherits the properties of more than one base classes. In the above example, C is a derived class inheriting from two base classes, A and B. A B C
10. MULTILEVEL INHERITANCE In this type of inheritance, a derived class acts as a base class to another class. In the above example, B is a derived class inheriting from base class A, but B also acts as a base class to class C. A B C
11. HIERARCHICAL INHERITANCE In this type of inheritance, more than one derived classes inherit the properties of a single base class. In the above example, B and C are two different derived classes inheriting from a single base class A. A B C
12. HYBRID INHERITANCE It is a mixture of two or more types of inheritance. In the above example, classes B and C inherit from base class A(hierarchical inheritance) and further, class D inherits from classes B and C(multiple inheritance). So, it becomes hybrid inheritance. A D B C
13. ORDER OF CALLING CONSTRUCTORS AND DESTRUCTORS Order of calling constructors and destructors in inheritance is as follows: First, constructor of base class is invoked. Then, constructor of derived class is invoked. After that, destructor of derived class is invoked. At last, destructor of base class is invoked.
14. THE END THANK YOU!