multiple inheritance
multiple inheritance
Introduction
Multiple inheritance is a feature in object-oriented programming that allows a class to inherit from more than one parent class. This enables a class to combine functionality from multiple classes.
what is multiple inheritance
Multiple inheritance in C++ is a feature that lets a class inherit properties from more than one base class. It's a powerful tool in object-oriented programming (OOP) that can save time and effort by avoiding code duplication.
Diagram
How does multiple inheritance work?
A derived class can inherit properties and behaviors from multiple base classes.
A comma-separated list is used to specify the base classes that a class inherits from.
The derived class can access the data members of all the base classes.
Benefits of multiple inheritance
It can be useful when a class needs to combine functionality from multiple classes.
It can provide flexibility and reusability in OOP.
Drawbacks of multiple inheritance
It can introduce ambiguities and conflicts that require careful handling.
For example, if multiple base classes have the same method name, the derived class may not know which method to call. This is known as the "diamond problem".
How to use multiple inheritance
You can use virtual functions and multiple constructors to implement multiple inheritance.
You can use templates to implement multiple inheritance.
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B’s constructor is called before A’s constructor.
A class can be derived from more than one base class.
Eg:
(i) A CHILD class is derived from FATHER and MOTHER class
(ii) A PETROL class is derived from LIQUID and FUEL class.
Syntax:
class A
{
... .. ...
};
class B
{
... .. ...
};
class C: public A,public B
{
... ... ...
};
For example, consider the following program.
#include<iostream>
using namespace std;
class Person {
// Data members of person
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
};
class Faculty : public Person {
// data members of Faculty
public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
};
class Student : public Person {
// data members of Student
public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
};
class TA : public Faculty, public Student {
public:
TA(int x):Student(x), Faculty(x) {
cout<<"TA::TA(int ) called"<< endl;
}
};
int main() {
TA ta1(30);
}
OUT PUT
Person::Person(int ) called
Faculty::Faculty(int ) called
Person::Person(int ) called
Student::Student(int ) called
TA::TA(int ) called
CREATED BY...
K.Bharath (24USCO04)
R.kishore (24USCO12)
1st B.SC Computer Science
SRMV CAS(UN ADIED)
Good 👍
ReplyDeleteGood
ReplyDelete👍👍
ReplyDelete👍
ReplyDeleteGood
ReplyDelete👍
ReplyDeleteGood
ReplyDeleteGood
ReplyDeleteGood
ReplyDeleteGood
ReplyDeleteGood
ReplyDeleteGood
ReplyDeleteUseful
ReplyDeleteGood
ReplyDeleteGood
ReplyDelete👍👍
ReplyDelete