Multiple Inheritance in C++This section will discuss the Multiple Inheritances in the C++ programming language. When we acquire the features and functionalities of one class to another class, the process is called Inheritance. In this way, we can reuse, extend or modify all the attributes and behaviour of the parent class using the derived class object. It is the most important feature of object-oriented programming that reduces the length of the program. A class that inherits all member functions and functionality from another or parent class is called the derived class. And the class from which derive class acquires some features is called the base or parent class. Multiple Inheritance is the concept of the Inheritance in C++ that allows a child class to inherit properties or behaviour from multiple base classes. Therefore, we can say it is the process that enables a derived class to acquire member functions, properties, characteristics from more than one base class. Diagram of the Multiple InheritanceFollowing is the diagram of the Multiple Inheritances in the C++ programming language. In the above diagram, there are two-parent classes: Base Class 1 and Base Class 2, whereas there is only one Child Class. The Child Class acquires all features from both Base class 1 and Base class 2. Therefore, we termed the type of Inheritance as Multiple Inheritance. Syntax of the Multiple InheritanceIn the above syntax, class A and class B are two base classes, and class C is the child class that inherits some features of the parent classes. Let's write the various program of Multiple Inheritance to inherit the member functions and functionality from more than one base class using the derived class. Example 1: Program to use the Multiple InheritanceProgram1.cpp Output It is the first function of the Base class It is the second function of the Base class It is the function of the derived class In the above program, we created two base classes and one child class. The child_class invoke member function display() and display2() from both parent classes Base_class and Base_class2 with the help of child class's object ch. Example 2: Use Multiple Inheritance to perform the arithmetic operationLet's create a derived class to inherit the member functions from multiple base classes in C++ programming. Program2.cpp Output The Modulus of 12 and 5 is 2 The sum of 20 and 30 is 50 The Multiplication of 20 and 30 is 600 The Division of 150 and 30 is 5 The Subtraction of 50 and 30 is 20 Example 3: Get the average marks of six subjects using the Multiple InheritanceLet's create another program to print the average marks of six subjects using the multiple Inheritance in the C++ programming language. Program3.cpp Output Enter the Roll No: 25 Enter the marks of five subjects 90 85 98 80 75 Enter the sports mark: 99 Roll No: 25 Total: 527 Average Marks: 87 Ambiguity Problem in Multiple InheritanceIn Multiple Inheritance, when a single class is derived from two or more base or parent classes. So, it might be possible that both the parent class have the same-named member functions, and it shows ambiguity when the child class object invokes one of the same-named member functions. Hence, we can say, the C++ compiler is confused in selecting the member function of a class for the execution of a program. Program to demonstrate the Ambiguity Problem in Multiple InheritanceLet's write a simple to invoke the same member function of the parent class using derived class in C++ programming. Program4.cpp When the above program is compiled, it throws the show() member function is ambiguous. Because of both the base class A and B, defining the same member function show(), and when the derived class's object call the shows() function, it shows ambiguity in multiple inheritances. Therefore, we need to resolve the ambiguous problem in multiple Inheritance. The ambiguity problem can be resolved by defining the class name and scope resolution (::) operator to specify the class from which the member function is invoked in the child class. Syntax of the Ambiguity Resolution For example, After making some changes, now we again execute the above program that returns the given below Output. Next TopicC++ Bitwise XOR Operator |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India