Introduction to Inheritance Definition: Inheritance allows a new class (derived class) to acquire the properties and behaviors (methods) of an existing class (base class). Why Inheritance? Code Reusability Extensibility Maintainability
Key Terminology Base Class (Parent Class): The class whose properties are inherited. Derived Class (Child Class): The class that inherits from the base class. Syntax:
Types of Inheritance in C++ Single Inheritance: Derived class inherits from a single base class. Multiple Inheritance: Derived class inherits from more than one base class. Multilevel Inheritance: A class derived from another derived class. Hierarchical Inheritance: One base class, multiple derived classes. Hybrid Inheritance: Combination of more than one type of inheritance.
A Special Case of Hybrid Inheritance: Multipath Inheritance In multipath inheritance, a class is derived from two base classes and these two base classes in turn are derived from one common base class. An ambiguity can arise in this type of inheritance in the most derived class. This problem is also called diamond problem due to the diamond shape formed in the UML inheritance diagram.
Single inheritance example
Access Specifiers in Inheritance Public Inheritance: Public members of the base class become public in the derived class. Protected Inheritance: Public and protected members of the base class become protected in the derived class. Private Inheritance: Public and protected members of the base class become private in the derived class.
Multilevel Inheritance Example
Advantages of Inheritance Code Reusability: Reduce redundancy. Easy Maintenance: Changes in the base class reflect in derived classes. Extensibility: New features can be added to derived classes without modifying base class.
Modes of Inheritance in C++ Public Inheritance Mode If we derive a subclass from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class. Protected Inheritance Mode If we derive a subclass from a Protected base class. Then both public members and protected members of the base class will become protected in the derived class.
Private Inheritance Mode If we derive a subclass from a Private base class. Then both public members and protected members of the base class will become private in the derived class. They can only be accessed by the member functions of the derived class. Private mode is the default mode that is applied when we don’t specify any mode. Note: The private members in the base class cannot be directly accessed in the derived class, while protected and public members can be directly accessed. To access or update the private members of the base class in derived class, we have to use the corresponding getter and setter functions of the base class or declare the derived class as friend class.
COURTESY: GEEKS FOR GEEKS
Program to Access the Private Members of the Base Class in Derived Class
CAN YOU GUYS GUESS THE OUTPUT??
The above program shows the method in which the private members of the base class remain encapsulated and are only accessible through controlled public or protected member functions. We can also access the private members of the base class by declaring the derived class as friend class in the base class EXAMPLE: SEE NEXT EXAMPLE.
Constructors and Destructors in Inheritance Constructors and Destructors are generally defined by the programmer and if not, the compiler automatically creates them so they are present in every class in C++. Now, the question arises what happens to the constructor and destructor when a class is inherited by another class. In C++ inheritance, the constructors and destructors are not inherited by the derived class, but we can call the constructor of the base class in derived class. The constructors will be called by the complier in the order in which they are inherited. It means that base class constructors will be called first, then derived class constructors will be called. The destructors will be called in reverse order in which the compiler is declared. We can also call the constructors and destructors manually in the derived class.
Polymorphism in Inheritance In Inheritance, we can redefine the base class member function in the derived class. This type of inheritance is called Function Overriding. Generally, in other programming languages, function overriding is runtime polymorphism but in C++, we can do it at both runtime and complile time. For runtime polymorphism, we have to use the virtual functions. CAN YOU WRITE AN EXAMPLE FOR THIS??