SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Multiple Inheritance
Multiple Inheritance
• If a class is derived from two or more base classes then it is called
multiple inheritance. In C++ multiple inheritance a derived class
has more than one base class.
• Multiple inheritance enables a derived class to inherit members
from more than one parent.
Difference in Multipe Inheritance & Multilevel
Inheritance
• Though but multiple and multilevel sounds like same but they
differ hugely in meaning. In multilevel inheritance, we have
multiple parent classes whereas in in multiple inheritance we have
multiple base classes.
• To put it in simple words, in multilevel inheritance, a class is
derived from a class which is also derived from another base class.
And these levels of inheritance can be extended. On the contrary,
in multiple inheritance, a class is derived from two different base
classes.
For example
• Multilevel inheritance : Inheritance of characters by a
child from father and father inheriting characters from
his father (grandfather)
• Multiple inheritance : Inheritance of characters by a
child from mother and father
C++ Multiple Inheritance Block Diagram
C++ Multiple Inheritance Syntax
• class A
• {
• ..........
• };
• class B
• {
• ...........
• } ;
• class C : acess_specifier A,access_specifier A // derived class from A and B
• {
• ...........
• } ;
C++ Multiple Inheritance Example
#include
using namespace std;
class A
{public:
int x;
void getx()
{
cout << "enter value of x: "; cin >>
x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "enter value of y: "; cin
>> y;
}
};
C++ Multiple Inheritance Example
class C : public A, public B //C is
derived from class A and class B
{
public:
void sum()
{cout << "Sum = " << x + y;
}
};
int main()
{
C obj1; //object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
} //end of program
C++ Multiple Inheritance Example
Output
enter value of x: 5
enter value of y: 4
Sum = 9
C++ Multiple Inheritance Example 2
Let’s say we wanted to write a program to keep track of a
bunch of teachers. A teacher is a person. However, a
teacher is also an employee (they are their own employer
if working for themselves). Multiple inheritance can be
used to create a Teacher class that inherits properties
from both Person and Employee. To use multiple
inheritance, simply specify each base class (just like in
single inheritance), separated by a comma.
C++ Multiple Inheritance Example 2
C++ Multiple Inheritance Example 2
• std::string getName() { return m_name; }
• int getAge() { return m_age; }
• };
• class Employee
• {
• private:
• std::string m_employer;
• double m_wage;
• public:
• Employee(std::string employer, double wage)
• : m_employer(employer), m_wage(wage)
• {
• }
#include <string>
class Person
{
private:
std::string m_name;
int m_age;
public:
Person(std::string name, int age)
: m_name(name), m_age(age)
{
}
C++ Multiple Inheritance Example 2
public:
Teacher(std::string name, int age,
std::string employer, double wage, int
teachesGrade)
: Person(name, age), Employee(employer,
wage), m_teachesGrade(teachesGrade)
{
}
};
std::string getEmployer() { return
m_employer; }
double getWage() { return m_wage; }
};
// Teacher publicly inherits Person and
Employee
class Teacher: public Person, public
Employee
{
private:
int m_teachesGrade;
Problems with multiple inheritance
• While multiple inheritance seems like a simple extension
of single inheritance, multiple inheritance introduces a
lot of issues that can markedly increase the complexity
of programs and make them a maintenance nightmare.
Let’s take a look at some of these situations.
• First, ambiguity can result when multiple base classes contain a
function with the same name. For example:
Problems with multiple inheritance
Example
• long getID() { return m_id; }
• };
• class NetworkDevice
• {
• private:
• long m_id;
• public:
• NetworkDevice(long id)
• : m_id(id) {
• }
•
#include <iostream>
class USBDevice
{
private:
long m_id;
public:
USBDevice(long id)
: m_id(id)
{
}
Problems with multiple inheritance
Example
{
}
};
int main()
{
WirelessAdapter c54G(5442, 181742);
std::cout << c54G.getID(); // Which
getID() do we call?
return 0;
}
long getID() { return m_id; }
};
class WirelessAdapter: public
USBDevice, public
NetworkDevice
{
public:
WirelessAdapter(long usbId,
long networkId)
: USBDevice(usbId),
NetworkDevice(networkId)
Problems with multiple inheritance
Example
• When c54G.getID() is compiled, the compiler looks to see if
WirelessAdapter contains a function named getID(). It doesn’t.
The compiler then looks to see if any of the parent classes have a
function named getID(). See the problem here? The problem is
that c54G actually contains TWO getID() functions: one inherited
from USBDevice, and one inherited from NetworkDevice.
Consequently, this function call is ambiguous, and you will receive
a compiler error if you try to compile it.
Solution
• However, there is a way to work around this problem: you can
explicitly specify which version you meant to call:
int main()
{
WirelessAdapter c54G(5442, 181742);
std::cout << c54G.USBDevice::getID();
return 0;
}
Solution
• While this workaround is pretty simple, you can see how things
can get complex when your class inherits from four or six base
classes, which inherit from other classes themselves. The
potential for naming conflicts increases exponentially as you
inherit more classes, and each of these naming conflicts needs to
be resolved explicitly.
Diamond Problem
• Second, and more serious is the diamond problem, which your
author likes to call the “diamond of doom”. This occurs when a
class multiply inherits from two classes which each inherit from a
single base class. This leads to a diamond shaped inheritance
pattern.
Diamond Problem Example

Mais conteúdo relacionado

Mais procurados (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Friend Function
Friend FunctionFriend Function
Friend Function
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Object and class
Object and classObject and class
Object and class
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 

Semelhante a Multiple inheritance

Semelhante a Multiple inheritance (20)

INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
29csharp
29csharp29csharp
29csharp
 
29c
29c29c
29c
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
Inheritance
Inheritance Inheritance
Inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)Object Oriented Programming using C++(UNIT 1)
Object Oriented Programming using C++(UNIT 1)
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Inheritance
InheritanceInheritance
Inheritance
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Inheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdfInheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdf
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
C++primer
C++primerC++primer
C++primer
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 

Mais de zindadili

Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 
Virtual function
Virtual functionVirtual function
Virtual functionzindadili
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaingzindadili
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Polymorphism
PolymorphismPolymorphism
Polymorphismzindadili
 
Function overloading
Function overloadingFunction overloading
Function overloadingzindadili
 
Abstraction1
Abstraction1Abstraction1
Abstraction1zindadili
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
Friend function
Friend functionFriend function
Friend functionzindadili
 

Mais de zindadili (17)

Namespaces
NamespacesNamespaces
Namespaces
 
Namespace1
Namespace1Namespace1
Namespace1
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Templates2
Templates2Templates2
Templates2
 
Templates1
Templates1Templates1
Templates1
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Aggregation
AggregationAggregation
Aggregation
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
 
Abstraction
AbstractionAbstraction
Abstraction
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Friend function
Friend functionFriend function
Friend function
 
Enum
EnumEnum
Enum
 

Último

KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...M56BOOKSTORE PRODUCT/SERVICE
 
Riddhi Kevadiya. WILLIAM SHAKESPEARE....
Riddhi Kevadiya. WILLIAM SHAKESPEARE....Riddhi Kevadiya. WILLIAM SHAKESPEARE....
Riddhi Kevadiya. WILLIAM SHAKESPEARE....Riddhi Kevadiya
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
Department of Health Compounder Question ‍Solution 2022.pdf
Department of Health Compounder Question ‍Solution 2022.pdfDepartment of Health Compounder Question ‍Solution 2022.pdf
Department of Health Compounder Question ‍Solution 2022.pdfMohonDas
 
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSyedNadeemGillANi
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxvidhisharma994099
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
Optical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptxOptical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptxPurva Nikam
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17Celine George
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashasashalaycock03
 

Último (20)

KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
 
Riddhi Kevadiya. WILLIAM SHAKESPEARE....
Riddhi Kevadiya. WILLIAM SHAKESPEARE....Riddhi Kevadiya. WILLIAM SHAKESPEARE....
Riddhi Kevadiya. WILLIAM SHAKESPEARE....
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
Department of Health Compounder Question ‍Solution 2022.pdf
Department of Health Compounder Question ‍Solution 2022.pdfDepartment of Health Compounder Question ‍Solution 2022.pdf
Department of Health Compounder Question ‍Solution 2022.pdf
 
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptxSOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
SOLIDE WASTE in Cameroon,,,,,,,,,,,,,,,,,,,,,,,,,,,.pptx
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
Protein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptxProtein Structure - threading Protein modelling pptx
Protein Structure - threading Protein modelling pptx
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
Optical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptxOptical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Work Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sashaWork Experience for psp3 portfolio sasha
Work Experience for psp3 portfolio sasha
 

Multiple inheritance

  • 2. Multiple Inheritance • If a class is derived from two or more base classes then it is called multiple inheritance. In C++ multiple inheritance a derived class has more than one base class. • Multiple inheritance enables a derived class to inherit members from more than one parent.
  • 3. Difference in Multipe Inheritance & Multilevel Inheritance • Though but multiple and multilevel sounds like same but they differ hugely in meaning. In multilevel inheritance, we have multiple parent classes whereas in in multiple inheritance we have multiple base classes. • To put it in simple words, in multilevel inheritance, a class is derived from a class which is also derived from another base class. And these levels of inheritance can be extended. On the contrary, in multiple inheritance, a class is derived from two different base classes.
  • 4. For example • Multilevel inheritance : Inheritance of characters by a child from father and father inheriting characters from his father (grandfather) • Multiple inheritance : Inheritance of characters by a child from mother and father
  • 5. C++ Multiple Inheritance Block Diagram
  • 6. C++ Multiple Inheritance Syntax • class A • { • .......... • }; • class B • { • ........... • } ; • class C : acess_specifier A,access_specifier A // derived class from A and B • { • ........... • } ;
  • 7. C++ Multiple Inheritance Example #include using namespace std; class A {public: int x; void getx() { cout << "enter value of x: "; cin >> x; } }; class B { public: int y; void gety() { cout << "enter value of y: "; cin >> y; } };
  • 8. C++ Multiple Inheritance Example class C : public A, public B //C is derived from class A and class B { public: void sum() {cout << "Sum = " << x + y; } }; int main() { C obj1; //object of derived class C obj1.getx(); obj1.gety(); obj1.sum(); return 0; } //end of program
  • 9. C++ Multiple Inheritance Example Output enter value of x: 5 enter value of y: 4 Sum = 9
  • 10. C++ Multiple Inheritance Example 2 Let’s say we wanted to write a program to keep track of a bunch of teachers. A teacher is a person. However, a teacher is also an employee (they are their own employer if working for themselves). Multiple inheritance can be used to create a Teacher class that inherits properties from both Person and Employee. To use multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.
  • 12. C++ Multiple Inheritance Example 2 • std::string getName() { return m_name; } • int getAge() { return m_age; } • }; • class Employee • { • private: • std::string m_employer; • double m_wage; • public: • Employee(std::string employer, double wage) • : m_employer(employer), m_wage(wage) • { • } #include <string> class Person { private: std::string m_name; int m_age; public: Person(std::string name, int age) : m_name(name), m_age(age) { }
  • 13. C++ Multiple Inheritance Example 2 public: Teacher(std::string name, int age, std::string employer, double wage, int teachesGrade) : Person(name, age), Employee(employer, wage), m_teachesGrade(teachesGrade) { } }; std::string getEmployer() { return m_employer; } double getWage() { return m_wage; } }; // Teacher publicly inherits Person and Employee class Teacher: public Person, public Employee { private: int m_teachesGrade;
  • 14. Problems with multiple inheritance • While multiple inheritance seems like a simple extension of single inheritance, multiple inheritance introduces a lot of issues that can markedly increase the complexity of programs and make them a maintenance nightmare. Let’s take a look at some of these situations. • First, ambiguity can result when multiple base classes contain a function with the same name. For example:
  • 15. Problems with multiple inheritance Example • long getID() { return m_id; } • }; • class NetworkDevice • { • private: • long m_id; • public: • NetworkDevice(long id) • : m_id(id) { • } • #include <iostream> class USBDevice { private: long m_id; public: USBDevice(long id) : m_id(id) { }
  • 16. Problems with multiple inheritance Example { } }; int main() { WirelessAdapter c54G(5442, 181742); std::cout << c54G.getID(); // Which getID() do we call? return 0; } long getID() { return m_id; } }; class WirelessAdapter: public USBDevice, public NetworkDevice { public: WirelessAdapter(long usbId, long networkId) : USBDevice(usbId), NetworkDevice(networkId)
  • 17. Problems with multiple inheritance Example • When c54G.getID() is compiled, the compiler looks to see if WirelessAdapter contains a function named getID(). It doesn’t. The compiler then looks to see if any of the parent classes have a function named getID(). See the problem here? The problem is that c54G actually contains TWO getID() functions: one inherited from USBDevice, and one inherited from NetworkDevice. Consequently, this function call is ambiguous, and you will receive a compiler error if you try to compile it.
  • 18. Solution • However, there is a way to work around this problem: you can explicitly specify which version you meant to call: int main() { WirelessAdapter c54G(5442, 181742); std::cout << c54G.USBDevice::getID(); return 0; }
  • 19. Solution • While this workaround is pretty simple, you can see how things can get complex when your class inherits from four or six base classes, which inherit from other classes themselves. The potential for naming conflicts increases exponentially as you inherit more classes, and each of these naming conflicts needs to be resolved explicitly.
  • 20. Diamond Problem • Second, and more serious is the diamond problem, which your author likes to call the “diamond of doom”. This occurs when a class multiply inherits from two classes which each inherit from a single base class. This leads to a diamond shaped inheritance pattern.