Tuesday 1 March 2022

Question: Describe concept of multiple inheritance with suitable examples in c++. उपयुक्‍त उदाहरण के साथ सी++ में मल्‍टीपल इनहेरीटेंस को समझाइए?

Multiple Inheritance (A Class drived from Multiple classes अनेकों क्‍लासेस से बनी एक क्‍लास):- Multiple Inheritance in C++ allows a child class to inherit properties or behavior from multiple base classes. Therefore, it is the process that enables a derived class to acquire member functions, properties, characteristics from more than one base class.  

सी++ में मल्‍टीपल इनहेरिटेंस चाइल्‍ड क्‍लास को अनेको पेरेंट क्‍लासेस की प्रोपर्टी रखने के लिए अलाउ करता है। अत: मल्‍टीपल इन्‍हेरीटेंस एक प्रोसेस है जिसमें चाइल्‍ड क्‍लास पेंरेन्‍ट् क्‍लास के फंक्‍शनस, प्रोपर्टीज को एक्‍सेस करती है।

Multiple Inheritance in Python - GeeksforGeeks 

Example:-

 

#include <iostream>

using namespace std;

 

class A {

   public:

   int a = 5;

   A() {

      cout << "Constructor for class A" << endl;

   }

};

 

class B {

   public:

   int b = 10;

   B() {

      cout << "Constructor for class B" << endl;

   }

};

 

class C: public A, public B {

   public:

   int c = 20;

   C() {

      cout << "Constructor for class C" << endl;

      cout<<"Class C inherits from class A and class B" << endl;

   }

};

 

int main() {

   C obj;

   cout<<"a = "<< obj.a <<endl;

   cout<<"b = "<< obj.b <<endl;

   cout<<"c = "<< obj.c <<endl;

   return 0;

}

 

Output:-

Constructor for class A
Constructor for class B
Constructor for class C
Class C inherits from class A and class B
a = 5
b = 10
c = 20

What is Inheritance:-

Ø  When we acquire the features and functionalities of one class to another class, the process is called Inheritance.

Ø  A class that inherits all member functions and functionality from another or parent class is called the derived class.

Ø  The class from which derive class acquires some features is called the base or parent class.

 

No comments:

Post a Comment