C++ InheritanceIn C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class. Advantage of C++ InheritanceCode reusability: Now you can reuse the members of your parent class. So, there is no need to define the member again. So less code is required in the class. Types Of InheritanceC++ supports five types of inheritance:
Derived ClassesA Derived class is defined as the class derived from the base class. The Syntax of Derived class: Where, derived_class_name: It is the name of the derived class. visibility mode: The visibility mode specifies whether the features of the base class are publicly inherited or privately inherited. It can be public or private. base_class_name: It is the name of the base class.
Note:
C++ Single InheritanceSingle inheritance is defined as the inheritance in which a derived class is inherited from the only one base class. Where 'A' is the base class, and 'B' is the derived class. C++ Single Level Inheritance Example: Inheriting FieldsWhen one class inherits another class, it is known as single level inheritance. Let's see the example of single level inheritance which inherits the fields only. Output: Salary: 60000 Bonus: 5000 In the above example, Employee is the base class and Programmer is the derived class. C++ Single Level Inheritance Example: Inheriting MethodsLet's see another example of inheritance in C++ which inherits methods only. Output: Eating... Barking... Let's see a simple example. Output: Multiplication of a and b is : 20 In the above example, class A is privately inherited. Therefore, the mul() function of class 'A' cannot be accessed by the object of class B. It can only be accessed by the member function of class B. How to make a Private Member InheritableThe private member is not inheritable. If we modify the visibility mode by making it public, but this takes away the advantage of data hiding. C++ introduces a third visibility modifier, i.e., protected. The member which is declared as protected will be accessible to all the member functions within the class as well as the class immediately derived from it. Visibility modes can be classified into three categories:
Visibility of Inherited Members
C++ Multilevel InheritanceMultilevel inheritance is a process of deriving a class from another derived class. C++ Multi Level Inheritance ExampleWhen one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes. Let's see the example of multi level inheritance in C++. Output: Eating... Barking... Weeping... C++ Multiple InheritanceMultiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes. Syntax of the Derived class: Let's see a simple example of multiple inheritance. Output: The value of a is : 10 The value of b is : 20 Addition of a and b is : 30 In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode. Ambiquity Resolution in InheritanceAmbiguity can be occurred in using the multiple inheritance when a function with the same name occurs in more than one base class. Let's understand this through an example: Output: error: reference to 'display' is ambiguous display();
An ambiguity can also occur in single inheritance. Consider the following situation: In the above case, the function of the derived class overrides the method of the base class. Therefore, call to the display() function will simply call the function defined in the derived class. If we want to invoke the base class function, we can use the class resolution operator. C++ Hybrid InheritanceHybrid inheritance is a combination of more than one type of inheritance. Let's see a simple example: Output: Enter the value of 'a' : 10 Enter the value of 'b' : 20 Enter the value of c is : 30 Multiplication of a,b,c is : 6000 C++ Hierarchical InheritanceHierarchical inheritance is defined as the process of deriving more than one class from a base class. Syntax of Hierarchical inheritance: Let's see a simple example: Output: Enter the length and breadth of a rectangle: 23 20 Area of the rectangle is : 460 Enter the base and height of the triangle: 2 5 Area of the triangle is : 5
Next TopicC++ Aggregation
|