🟣 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.
Level
🟣 Object-Oriented Programming
Example File
multiple-inheritance.cpp
Main Focus
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

TermMeaning / Use
multiple inheritanceMultiple inheritance used in Multiple Inheritance programming.
ambiguityAmbiguity used in Multiple Inheritance programming.
base classBase class used in Multiple Inheritance programming.
derived classDerived 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

  1. Create class Result from Test and Sports.
  2. 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.
Level
🟣 Object-Oriented Programming
Example File
multiple-inheritance.cpp
Main Focus
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

TermMeaning / Use
multiple inheritanceMultiple inheritance used in Multiple Inheritance programming.
ambiguityAmbiguity used in Multiple Inheritance programming.
base classBase class used in Multiple Inheritance programming.
derived classDerived 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

  1. Create class Result from Test and Sports.
  2. 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.

← Back to C++ Tutorial