🟣 OOP · Lesson 46
Multiple Inheritance
Multiple Inheritance
What is Multiple Inheritance?
Multiple Inheritance
Multiple inheritance allows a derived class to inherit from more than one base class. It is powerful but should be used carefully to avoid ambiguity.
Multiple inheritance allows a derived class to inherit from more than one base class. It is powerful but should be used carefully to avoid ambiguity.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
multiple-inheritance.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| multiple inheritance | Multiple inheritance used in Multiple Inheritance programming. |
| ambiguity | Ambiguity used in Multiple Inheritance programming. |
| base class | Base class used in Multiple Inheritance programming. |
| derived class | Derived class used in Multiple Inheritance programming. |
Syntax / Pattern
class C : public A, public B { };
Example Program
#include <iostream>
using namespace std;
class Sports{ public: int sports=10; };
class Exam{ public: int marks=80; };
class Result : public Sports, public Exam{};
int main(){ Result r; cout << r.marks + r.sports; }
Expected Output
90
Program Explanation
- Result inherits members from Sports and Exam.
- Total is calculated using both inherited values.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- combined result systems
- interfaces
- mix-in behavior
Common Mistakes
- Name ambiguity when two base classes have same member.
- Overusing multiple inheritance.
Practice Tasks
- Create class Result from Test and Sports.
- Explain diamond problem in simple words.
Summary
Multiple Inheritance is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is Multiple Inheritance?
Multiple Inheritance
Multiple inheritance allows a derived class to inherit from more than one base class. It is powerful but should be used carefully to avoid ambiguity.
Multiple inheritance allows a derived class to inherit from more than one base class. It is powerful but should be used carefully to avoid ambiguity.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
multiple-inheritance.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| multiple inheritance | Multiple inheritance used in Multiple Inheritance programming. |
| ambiguity | Ambiguity used in Multiple Inheritance programming. |
| base class | Base class used in Multiple Inheritance programming. |
| derived class | Derived class used in Multiple Inheritance programming. |
Syntax / Pattern
class C : public A, public B { };
Example Program
#include <iostream>
using namespace std;
class Sports{ public: int sports=10; };
class Exam{ public: int marks=80; };
class Result : public Sports, public Exam{};
int main(){ Result r; cout << r.marks + r.sports; }
Expected Output
90
Program Explanation
- Result inherits members from Sports and Exam.
- Total is calculated using both inherited values.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- combined result systems
- interfaces
- mix-in behavior
Common Mistakes
- Name ambiguity when two base classes have same member.
- Overusing multiple inheritance.
Practice Tasks
- Create class Result from Test and Sports.
- Explain diamond problem in simple words.
Summary
Multiple Inheritance is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.