Multiple Inheritance in C++

Introduction

In object-oriented programming, inheritance is a powerful concept that allows a class to inherit properties and behaviors from another class. C++ supports multiple inheritance, which means a class can inherit from multiple base classes. This article will explain the code provided, which demonstrates the concept of multiple inheritance in C++.

CPP

Key Concepts

  • Base Class: A base class is a class from which other classes can inherit properties and behaviors.
  • Derived Class: A derived class is a class that inherits properties and behaviors from one or more base classes.
  • Multiple Inheritance: Multiple inheritance is a feature in C++ that allows a derived class to inherit from multiple base classes.

Code Structure

The code provided demonstrates the concept of multiple inheritance. It consists of three classes: Base1, Base2, and Derived. The Derived class inherits from both Base1 and Base2 classes.

Code Examples

C++
#include<iostream>
using namespace std;
class Base1
{
	protected:
	int B1;
	public:
		void getB1(int x)
		{
			 B1=x;
		}
};
class Base2
{
	protected:
		int B2;
	public:
		void getB2(int y)
		{
			B2=y;
		}
};
class Derived:public Base1,public Base2
{
	int d;
	public:
		void getderived(int z)
		{
			d=z;
		}
		void show()
		{
			cout<<"Base1 : "<<B1<<endl;
			cout<<"Base2 : "<<B2<<endl;
			cout<<"Derived : "<<d<<endl;
		}
};
int main()
{
	Derived obj;
	obj.getB1(100);
	obj.getB2(200);
	obj.getderived(300);
	obj.show();
	return 0;
}

Code Output

Base1 : 100
Base2 : 200
Derived : 300

Explanation

  • The code begins with the inclusion of the iostream library, which allows input and output operations.
  • The Base1 class is defined, which has a protected member variable B1 and a public member function getB1(). The getB1() function sets the value of B1.
  • The Base2 class is defined, which has a protected member variable B2 and a public member function getB2(). The getB2() function sets the value of B2.
  • The Derived class is defined, which inherits from both Base1 and Base2 classes using the public access specifier. It has a private member variable d and two member functions: getderived() and show().
  • The getderived() function sets the value of d.
  • The show() function displays the values of B1, B2, and d.
  • In the main() function, an object obj of the Derived class is created.
  • The getB1(), getB2(), and getderived() functions are called to set the values of B1, B2, and d, respectively.
  • The show() function is called to display the values of B1, B2, and d.
  • Finally, the program returns 0, indicating successful execution.

Conclusion

The code provided demonstrates the concept of multiple inheritance in C++. The Derived class inherits properties and behaviors from both the Base1 and Base2 classes. By using multiple inheritance, we can create complex class hierarchies and reuse code from multiple sources. Understanding multiple inheritance is essential for building robust and flexible object-oriented programs in C++.
Next Post Previous Post
No Comment
Add Comment
comment url