| by Arround The Web | No comments

C++ Multiple Inheritance

Inheritance is the technique through which we transfer the characteristics and functionalities of one class to another class. This enables us to utilize the object of the derived class to reuse and change the parent class’ characteristics. Multiple inheritance is a C++ concept that enables a child class to derive the attributes or behavior from multiple parent classes. The term “derived class” refers to a class that obtains all of its members, functionality, and capabilities from a parent class. The base or parent class is the one from which the derived classes inherit certain characteristics.

Syntax:

class A  
{  
--
}  
class B  
{  
--
}  
class C: public A, public B (access modifier class_name)  
{  
--
}

Basic Program to Use the Multiple Inheritance

In this example, we demonstrate the basic implementation of the multiple inheritance concept in C++. We create two base classes and call their constructors and then derive a third class from the previously created base classes.

#include<iostream>
using namespace std;

class Alpha
{
public:
Alpha()
{ cout << "This is class Alpha" << endl; }
};

class Beta
{
public:
Beta()
{ cout << "This is class Beta" << endl; }
};

class Gamma: public Alpha, public Beta
{
public:
Gamma()
{ cout << "This is class Gamma" << endl; }
};

int main()
{
    Gamma obj;
    return 0;
}

We start the program by including the “iostream” library. This allows us to perform the input/output operations. Then, we use the "namespace" standard library. Now, we create our first base class. So, we have the first class “Alpha” here. The body of the class starts with the curly bracket. We define the access specifier of the class if we need to make the body of the class public, private, or protected.

Here, we apply the “public” access specifier as it allows the accessibility of the members outside the class with a colon(:). Now, whatever we do in the class is made public. After that, we call the constructor of the "Alpha()”class. Inside the curly braces, we use the cout object to print the text – "This is class Alpha".

After calling the constructor, we close the body of the class with parentheses followed by a semicolon.

The second base class is then created with the “Beta” name. The members of the class are made public using the “public:” access specifier of the class. The “Beta()”constructor is called and a statement is defined inside its body with a cout object as “This is class Beta”. Then, we close this class.

Now, we create a derived class that inherits both the base classes "Alpha" and "Beta". We first write the class keyword with the class name, “Gamma”. Then, insert a colon (:) and write the base classes name with the public which specifies that the members of the base class are public, separated by a comma (,). Coming into the body of the derived class, the constructor of the derived class is called "Gamma()”. Inside the body of the constructor, a text statement is provided to be displayed as “This is class Gamma”.

Then, we close the body of the class and start our main() function. The system gets access to the code through this main() function. All the functions that are created in the base or derived class must be called in the main() function to execute them.

So, we create an object of the derived class as “Gamma obj;” inside the body of the main() function. Now, we can access the base classes’ functions with this object. Since we previously learned that the constructor of a class is automatically called when the object of the class is created, the constructors are already called as we generate the object. As the derived class inherits the characteristics of the base classes, the constructor of the base classes are also called and the code inside them is executed.

The return “0” shows that the programs ran successfully.

You can see in the following snapshot that the statements that are defined in the body of the constructors are displayed on the console:

Arithmetic Operations in Multiple Inheritance

Arithmetic operations can also be performed using multiple inheritance in C++. We create two base classes for subtraction and multiplication. Then, we inherit them to the third derived class which performs the addition of two numbers as well as inherit the characteristics of the base classes.

#include <iostream>  
using namespace std;  

class Minus  
{  
    public:  
        int c = 14;  
        int d = 8;  
        void subtraction()  
        {  
            cout << " Subtracting " << c << " and " << d << " gives " <<c-d << endl;  
        }  
};    
class Multiply  
{  
    public:  
        int e = 9;  
        int f = 12;  
        void multiplication()  
        {  
            cout << " Multiplying " << e << " and " << f << " gives " <<e*f << endl;  
        }  
};    
class Sum: public Minus, public Multiply
{  
    public:  
        int a = 9;  
        int b = 7;  
        void addition()  
        {  
            cout << " Adding " << a << " and " << b << " gives " <<a+b << endl;  
        }  
};  
 
int main ()  
{  
    Sum s;  
    s.addition();  
    s.multiplication();  
    s.subtraction();  
}

After including the required libraries, we create a base class “Minus”. We first specify the access as “public:” inside the body of the class. Then, we initialize two variables – “c” and “d” – with the integer datatype. The value for “c” is 14 and the value for “d” is 8. Then, we invoke a “subtraction()”function with the void type which means that no argument is returned by the function. In the “subtraction()” function, we use the cout object with the insertion operator to print a text and perform the subtraction using the arithmetic operator “-” between both the variables “c” and “d” as “c-d”.

We have “Multiply” in the next class. The access specifier is made public. We initialize two variables with some values as “int e=9” and “int f=12”. Then, we create a “void multiplication()” function. In this function, we use a cout statement to print some text and carry out the multiplication of two variables as “e*f” and display the output.

Then, we derive the “Sum” class which inherits both “Minus” and “Multiply” classes. So, we write it as “class Sum: public Minus, public Multiply”. The access is made public and two variables are initialized as “int a=9” and “int b=7”. We invoke a “void addition()” function which performs the addition of these two variables using the cout statement.

Coming into the main() function, we build an object of the derived class as “Sum s”. The functions of each class are called one by one using the created “s” object as shown in the code.

The following snapshot shows the generated output from the execution of the program:

Conclusion

This article demonstrates the use of multiple inheritance in C++. We provided you with the syntax to implement multiple inheritance. To practically implement this technique, we carried out two examples. The first illustration provides us with the basic implementation of multiple inheritance, whereas the second example depicts how to perform the arithmetic operations.

Share Button

Source: linuxhint.com

Leave a Reply