Introduction of Inheritance in C++
- Inheritance in C++ is one of the object-oriented paradigm’s most important, useful, essential, fundamental & prime features.
Definition of Inheritance in C++
- Inheritance is the process by which one or many new classes (derived/sub/ extended/child/descendant classes) are created from existing classes (called base/super/parent /ancestor /master classes).
- In other words, Inheritance is the procedure of creating a new class from one or more existing (old) classes is termed inheritance.
- Inheritance is a technique of organizing information in a hierarchical form.
- It is a relation between classes that allows for the definition and implementation of new classes based on the definition of existing classes.
Characteristics/Features of Inheritance in C++
- In Inheritance, a class can be derived from more than one class and it is also possible to derive a class from a previously derived class.
- Inheritance is represented as an “is-a” relationship.
- Here, the existing classes are the main components of inheritance.
- In inheritance, the main properties of existing classes are simply extended to the new classes.
- In Inheritance, the object of the derived class can access members of the base as well as the derived class. On the other hand, the object of the base class can not access members of derived classes.
-
The base classes –
-
-
Do not know about their sub-classes.
- The superclass is the class from where a subclass inherits the features.
-
- The derived classes –
- A subclass is a class which inherits the other class.
- It may inherit some/none/all the features (capabilities) of the base class and can also add new specific features to the newly created derived class as per requirement which is decided by the use of visibility mode(public/private/protected). The base class remains unchanged.
- A derived class can also inherit properties from more than one class or more than one level. A derived class has its own set of member variables and functions.
- The subclass can reuse the data member and methods of the superclass that were already implemented and it can also extend or replace the behavior in the superclass by overriding methods.
- The super Keyword –
-
- The keyword ‘super’ refers to the super/base class of the current object.
- It performs a similar function, but it refers to the immediate superclass of the current class.
- This is useful when a class needs to access: (1) its superclasses constructor, and (2) its superclasses methods.
- There are two uses of the super keyword.
1. It is used for calling the superclass constructor.
2. It is used to access those members of the superclass that are hidden by the members of the sub-class. - While sub-classes inherit the attributes and methods of their superclass, they do not inherit their superclass’s constructors. To invoke a super class’s constructor, a subclass must use the super keyword as –
super (argumentList);
(Here, the argument list provides the arguments required by the super class’s constructor and may be empty for the default constructor. It is advised that a call to the super class’s constructor must be the first statement in a sub class’s constructor.) - Superclass constructors are not inherited by the subclass.
-
- We can create an instance of a subclass and then assign it to a superclass variable, this is called upcasting.
- When an instance of a Superclass is assigned to a Subclass variable, then it’s called downcasting. We need to explicitly cast this to Subclass.
Syntax
class DerivedClassName : [Access Specifier] BaseClassName
{
// member of derived class
};
Example
Access Specifier of Class Members in Inheritance
- There are three types of visibility modes/modifiers used in inheritance. They are private, public and protected.
-
Base class visibility modes Derived class visibility modes Private derivation Public derivation Protected derivation private Not inherited private Not inherited Not inherited public private public protected protected protected protected
Types of Inheritance
(A) The inheritance is classified into the following types based on its derivation process from the base class –
- Single Inheritance:
-
- When a class is derived/inherits the features from only one base class, it is called Single Inheritance.
- A derived class can be declared only if its base class is already declared.
- Multiple Inheritance:
-
- When a class is derived/inherits the features from more than one parent class (base classes), it is called multiple inheritance.
- Multi-level Inheritance:
-
- When a class is derived/inherits the features from another derived class, is called Multi-level Inheritance.
- Hierarchical Inheritance:
-
- The derivation of several classes from a single base class is called hierarchical inheritance.
- Hybrid Inheritance:
-
- The derivation of a class involving more than one form of inheritance is called hybrid inheritance.
- Multi-path Inheritance:
-
- Derivation of a class from other derived classes, which are derived from the same base class is called multi-path inheritance.
(B)The inheritance is further classified into the following types based on the use of base class members by derived class –
A class can be derived publicly or privately third type exists.
(A) Public Inheritance:
- When a public access specifier is used in the inheritance of the derived class then the public members of the base class are public members of the derived class. Similarly protected and private members of the base class are protected and private members of the derived class.
- When a class is derived publicly, all the public members of a base class can be accessed directly (by the object of the derived class) in the derived class and all the private members of the base class can be accessed (by the object of derived class) using only public member functions of the base class.
- Syntax = class B : public A
(B) Private/Protected Inheritance:
- It is by default derivation inheritance.
- When a private access specifier is used in the inheritance of the derived class then the public and protected/private members of the base class are private members of the derived class.
- When a class is derived privately/protected, an object of the derived class has no permission to access even public members of the base class directly. In such a case, the public members of the base class can be accessed using public member functions of the derived class and private members of the base class can be accessed using the public member function of the base class via the public member function of the derived class.
- Syntax = class B : A or Syntax = class B : private A or Syntax = class B : protected A.
Benefits/Advantages of Inheritance in C++
- The most important feature of using inheritance is for deriving a new class using an existing class which provides a re-usability concept. Here, the existing class remains unchanged.
- When inherited from another class, the derived class does not need to be rewritten.
- Code sharing easily occurs at several levels.
- Multiple inheritance provides sufficient guarantee that the behavior they inherit will be the same in all cases.
- The derived class extends the properties of base classes to generate a more dominant object.
- The same base class can be used to transfer similar properties into multiple derived classes.
- The multiple base class can be used to transfer their properties into a single class to make it more usable/powerful.
- It allows the programmer to utilize the previously defined classes with newer ones.
Demerits/Disadvantages of Inheritance in C++
- Inappropriate use of inheritance makes a program more complicated.
- Calling of member functions by an object produces more compiler overheads.
- In a class hierarchy, multiple data elements remain unused for a long time.
Use of Inheritance in C++
- The concept of Inheritance is used in reusability, i.e. reuse of properties of the base class in the derived class without rewriting again. Reusability permits to reuse of the members of the base class. Reusability is achieved using inheritance and both are not different from each other rather the outcome of inheritance is reusability. By reusability, the development time of software is reduced.
- Inheritance helps to organize software components into categories and subcategories resulting in the classification of software.
- There are many benefits of inheritance i.e. they help in code reuse, ease of code maintenance and extension, and reduction in the development time.
Inheritance with Constructor & Destructor
0 Comments