SlideShare uma empresa Scribd logo
1 de 16
Hybrid Inheritance
Ambiguity Resolution in Inheritance
Ambiguity can be occurred in using the multiple inheritance when a function with
the same name occurs in more than one base class.
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. public:
6. void display()
7. {
8. std::cout << "Class A" << std::endl;
9. }
Ambiquity Example
10. };
11. class B
12. {
13. public:
14. void display()
15. {
16. std::cout << "Class B" << std::endl;
17. }
18. };
19. class C : public A, public B
20. {
21. void view()
22. {
23. display();
24. }
25. };
26. int main()
27. {
28. C c;
29. c.display();
30. return 0;
31. }
Output:
error: reference to 'display' is ambiguous
display();
Ambiguity Resolution
 The above issue can be resolved by
using the class resolution operator
with the function. In the above
example, the derived class code can
be rewritten as:
1. class C : public A, public B
2. {
3. void view()
4. {
5. A :: display(); // Calling the display()
function of class A.
6. B :: display(); // Calling the display()
function of class B.
7.
8. }
9. };
Ambiguity Example in Simple
Inheritance
 An ambiguity can also occur in single
inheritance.
Consider the following situation:
1. class A
2. {
3. public:
4. void display()
5. {
6. cout<<?Class A?;
7. }
8. } ;
9. class B
10. {
11. public:
12. void display()
13. {
14. cout<<?Class B?;
15. }
16. } ;
Ambiguity Example in Simple
Inheritance
 In the above case, the function of the
derived class overrides the method of
the base class. Therefore, call to the
display() function will simply call the
function defined in the derived class.
If we want to invoke the base class
function, we can use the class
resolution operator.
1. int main()
2. {
3. B b;
4. b.display(); // Calling the display()
function of B class.
5. b.B :: display(); // Calling the display()
function defined in B class.
6. }
C++ Hybrid Inheritance
 Hybrid inheritance is a combination of more than one type of inheritance.
C++ Hybrid Inheritance
 The inheritance in which the derivation of a class involves more than one
form of any inheritance is called hybrid inheritance. Basically C++ hybrid
inheritance is combination of two or more types of inheritance. It can also
be called multi path inheritance.
C++ Hybrid Inheritance Syntax
class A
{ .........
};
class B : public A
{
..........
} ;
class C
{
...........
};
class D : public B, public C
{.........
};
C++ Hybrid Inheritance Example1
 Let's see a simple example:
1. #include <iostream>
2. using namespace std;
3. class A
4. {
5. protected:
6. int a;
7. public:
8. void get_a()
9. {
10. std::cout << "Enter the value of 'a' : " <<
std::endl;
11. cin>>a;
12. }
13. };
14.
15. class B : public A
16. {
17. protected:
18. int b;
 19. public:
C++ Hybrid Inheritance Example1
20. void get_b()
21. {
22. std::cout << "Enter the value of 'b' : "
<< std::endl;
23. cin>>b;
24. }
25. };
26. class C
27. {
28. protected:
29. int c;
30. public:
31. void get_c()
32. {
33. std::cout << "Enter the value of c is : "
<< std::endl;
34. cin>>c;
35. }
36. };
C++ Hybrid Inheritance Example1
38. class D : public B, public C
39. {
40. protected:
41. int d;
42. public:
43. void mul()
44. {
45. get_a();
46. get_b();
47. get_c();
48. std::cout << "Multiplication of a,b,c is : "
<<a*b*c<< std::endl;
49. }
};
51. int main()
52. {
53. D d;
54. d.mul();
55. return 0;
56. }
Output:
Enter the value of 'a' :
10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000
C++ Hybrid Inheritance Example2
#include<iostream>
#include<conio>
using namespace std;
class arithmetic
{
protected:
int num1, num2;
public:
void getdata()
{
cout<<"For Addition:";
cout<<"nEnter the first number: ";
cin>>num1;
cout<<"nEnter the second number:
";
cin>>num2;
}
};
Example2
};
class plus:public arithmetic
{
protected:
int sum;
public:
void add()
{
sum=num1+num2;
}
};
class minus
{
protected:
int n1,n2,diff;
public:
void sub()
{
cout<<"nFor Subtraction:";
cout<<"nEnter the first number: ";
cin>>n1;
cout<<"nEnter the second number: ";
cin>>n2;
diff=n1-n2;
}
};
Example2
class result:public plus, public minus
{
public:
void display()
{
cout<<"nSum of "<<num1<<" and
"<<num2<<"= "<<sum;
cout<<"nDifference of "<<n1<<"
and "<<n2<<"= "<<diff;
}
};
int main()
{
result z;
z.getdata();
z.add();
z.sub();
z.display();
return 0;
}
Any Questions?

Mais conteúdo relacionado

Mais procurados

Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
Kamal Acharya
 

Mais procurados (20)

Inheritance
InheritanceInheritance
Inheritance
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Templates
TemplatesTemplates
Templates
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Getters_And_Setters.pptx
Getters_And_Setters.pptxGetters_And_Setters.pptx
Getters_And_Setters.pptx
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
virtual function
virtual functionvirtual function
virtual function
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 

Semelhante a Hybrid inheritance

Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
Deepak Singh
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
Deepak Singh
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
FALLEE31188
 

Semelhante a Hybrid inheritance (20)

Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Revisão OCPJP7 - Class Design (parte 02)
Revisão OCPJP7 - Class Design (parte 02) Revisão OCPJP7 - Class Design (parte 02)
Revisão OCPJP7 - Class Design (parte 02)
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Exam for c
Exam for cExam for c
Exam for c
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
C test
C testC test
C test
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
 
Write a program to show the example of hybrid inheritance.
Write a program to show the example of hybrid inheritance.Write a program to show the example of hybrid inheritance.
Write a program to show the example of hybrid inheritance.
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
Technical questions
Technical questionsTechnical questions
Technical questions
 

Mais de zindadili (19)

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
 
Multiple inheritance
Multiple inheritanceMultiple inheritance
Multiple inheritance
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
 
Abstraction
AbstractionAbstraction
Abstraction
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Inheritance
InheritanceInheritance
Inheritance
 
Friend function
Friend functionFriend function
Friend function
 
Enum
EnumEnum
Enum
 

Último

Último (20)

General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Hybrid inheritance

  • 2. Ambiguity Resolution in Inheritance Ambiguity can be occurred in using the multiple inheritance when a function with the same name occurs in more than one base class. 1. #include <iostream> 2. using namespace std; 3. class A 4. { 5. public: 6. void display() 7. { 8. std::cout << "Class A" << std::endl; 9. }
  • 3. Ambiquity Example 10. }; 11. class B 12. { 13. public: 14. void display() 15. { 16. std::cout << "Class B" << std::endl; 17. } 18. }; 19. class C : public A, public B 20. { 21. void view() 22. { 23. display(); 24. } 25. }; 26. int main() 27. { 28. C c; 29. c.display(); 30. return 0; 31. } Output: error: reference to 'display' is ambiguous display();
  • 4. Ambiguity Resolution  The above issue can be resolved by using the class resolution operator with the function. In the above example, the derived class code can be rewritten as: 1. class C : public A, public B 2. { 3. void view() 4. { 5. A :: display(); // Calling the display() function of class A. 6. B :: display(); // Calling the display() function of class B. 7. 8. } 9. };
  • 5. Ambiguity Example in Simple Inheritance  An ambiguity can also occur in single inheritance. Consider the following situation: 1. class A 2. { 3. public: 4. void display() 5. { 6. cout<<?Class A?; 7. } 8. } ; 9. class B 10. { 11. public: 12. void display() 13. { 14. cout<<?Class B?; 15. } 16. } ;
  • 6. Ambiguity Example in Simple Inheritance  In the above case, the function of the derived class overrides the method of the base class. Therefore, call to the display() function will simply call the function defined in the derived class. If we want to invoke the base class function, we can use the class resolution operator. 1. int main() 2. { 3. B b; 4. b.display(); // Calling the display() function of B class. 5. b.B :: display(); // Calling the display() function defined in B class. 6. }
  • 7. C++ Hybrid Inheritance  Hybrid inheritance is a combination of more than one type of inheritance.
  • 8. C++ Hybrid Inheritance  The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. Basically C++ hybrid inheritance is combination of two or more types of inheritance. It can also be called multi path inheritance.
  • 9. C++ Hybrid Inheritance Syntax class A { ......... }; class B : public A { .......... } ; class C { ........... }; class D : public B, public C {......... };
  • 10. C++ Hybrid Inheritance Example1  Let's see a simple example: 1. #include <iostream> 2. using namespace std; 3. class A 4. { 5. protected: 6. int a; 7. public: 8. void get_a() 9. { 10. std::cout << "Enter the value of 'a' : " << std::endl; 11. cin>>a; 12. } 13. }; 14. 15. class B : public A 16. { 17. protected: 18. int b;  19. public:
  • 11. C++ Hybrid Inheritance Example1 20. void get_b() 21. { 22. std::cout << "Enter the value of 'b' : " << std::endl; 23. cin>>b; 24. } 25. }; 26. class C 27. { 28. protected: 29. int c; 30. public: 31. void get_c() 32. { 33. std::cout << "Enter the value of c is : " << std::endl; 34. cin>>c; 35. } 36. };
  • 12. C++ Hybrid Inheritance Example1 38. class D : public B, public C 39. { 40. protected: 41. int d; 42. public: 43. void mul() 44. { 45. get_a(); 46. get_b(); 47. get_c(); 48. std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl; 49. } }; 51. int main() 52. { 53. D d; 54. d.mul(); 55. return 0; 56. } Output: Enter the value of 'a' : 10 Enter the value of 'b' : 20 Enter the value of c is : 30 Multiplication of a,b,c is : 6000
  • 13. C++ Hybrid Inheritance Example2 #include<iostream> #include<conio> using namespace std; class arithmetic { protected: int num1, num2; public: void getdata() { cout<<"For Addition:"; cout<<"nEnter the first number: "; cin>>num1; cout<<"nEnter the second number: "; cin>>num2; } };
  • 14. Example2 }; class plus:public arithmetic { protected: int sum; public: void add() { sum=num1+num2; } }; class minus { protected: int n1,n2,diff; public: void sub() { cout<<"nFor Subtraction:"; cout<<"nEnter the first number: "; cin>>n1; cout<<"nEnter the second number: "; cin>>n2; diff=n1-n2; } };
  • 15. Example2 class result:public plus, public minus { public: void display() { cout<<"nSum of "<<num1<<" and "<<num2<<"= "<<sum; cout<<"nDifference of "<<n1<<" and "<<n2<<"= "<<diff; } }; int main() { result z; z.getdata(); z.add(); z.sub(); z.display(); return 0; }