Write a C++ Program to illustrates the use of Constructors in multiple inheritance. Here’s a Simple C++ Program to illustrates the use of Constructors in multiple inheritance in C++ Programming Language.
Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.
When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
Types of Inheritance : :
There are different types of inheritance : :
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid (Virtual) Inheritance
Below is the source code for C++ Program to illustrates the use of Constructors in multiple inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to illustrates the use of Constructors in multiple inheritance */
#include<iostream>
using namespace std;
class A // First Base class
{
private:
int x ;
public:
A( ) // Constructor of the base class A without any argument
{
x = 0 ;
cout << "\n Constructor of class A without any argument is invoked" ;
}
A(int X) // Constructor of the base class A with one argument
{
x = X ;
cout << "\n Constructor of class A with one argument is invoked" ;
}
void Enter_x(void)
{ cout << "\n\n\t Enter the value of x: " ; cin >> x ; }
void Display_x(void)
{ cout << "\n\t x = " << x ; }
};
class B // Second Base class
{
private:
int y ;
public:
B( )
{
y = 0 ; // Constructor of the base class B without any argument
cout << "\n Constructor of class B without any argument is invoked" ;
}
B(int Y) // Constructor of the base class B with one argument
{
y = Y ;
cout << "\n Consrtuctor of class B with one argument isinvoked" ;
}
void Enter_y(void)
{ cout << "\t Enter the value of y: " ; cin >> y ; }
void Display_y(void)
{ cout << "\n\t y = " << y ; }
};
class C : public B, public A //Derived class, inherited from base classes A & B
{
private:
int z ;
public:
C( ) : A( ), B( ) // Constructor of the derived class C without any argument
{
z = 0 ;
cout << "\n Constructor of class C without any argument is invoked" ;
}
// *****Constructor of the derived class C with three arguments*****
C(int X, // Argument for the constructor A
int Y, // Argument for the constructor B
int Z) // Argument for the constructor C
: A(X), B(Y) // Calls for the constructors A and B
{
z = Z ;
cout << "\n Consrtuctor of class C with three arguments is invoked\n" ;
}
void Enter_z(void)
{ cout << "\t Enter the value of z: " ; cin >> z ; }
void Display_z(void)
{ cout << "\n\t z = " << z ; }
};
int main()
{
cout << "\n The first object c1 is in use********\n" ;
C c1 ;
c1.Enter_x( );
c1.Enter_y( );
c1.Enter_z( );
c1.Display_x( );
c1.Display_y( );
c1.Display_z( );
cout << "\n\n The second object c2 is in use********\n" ;
C c2(5, 6, 7) ;
c2.Display_x( );
c2.Display_y( );
c2.Display_z( );
return 0;
}
OUTPUT : :
/* C++ Program to illustrates the use of Constructors in multiple inheritance */
The first object c1 is in use********
Constructor of class B without any argument is invoked
Constructor of class A without any argument is invoked
Constructor of class C without any argument is invoked
Enter the value of x: 4
Enter the value of y: 5
Enter the value of z: 6
x = 4
y = 5
z = 6
The second object c2 is in use********
Consrtuctor of class B with one argument isinvoked
Constructor of class A with one argument is invoked
Consrtuctor of class C with three arguments is invoked
x = 5
y = 6
z = 7
Process returned 0
Above is the source code and output for C++ Program to illustrates the use of Constructors in multiple inheritance which is successfully compiled and run on Windows System to produce desired output.
Types of Inheritance : :
There are different types of inheritance : :
Below is the source code for C++ Program to illustrates the use of Constructors in multiple inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code and output for C++ Program to illustrates the use of Constructors in multiple inheritance which is successfully compiled and run on Windows System to produce desired output.
need an explanation for this answer? contact us directly to get an explanation for this answer