SlideShare a Scribd company logo
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?

More Related Content

What's hot

Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
Sujan Mia
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Encapsulation
EncapsulationEncapsulation
Encapsulation
Burhan Ahmed
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
 
C++
C++C++
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
Docent Education
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
SudhanshuVijay3
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
sangrampatil81
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 

What's hot (20)

Multiple inheritance in c++
Multiple inheritance in c++Multiple inheritance in c++
Multiple inheritance in c++
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
 
C++
C++C++
C++
 
Structure in c
Structure in cStructure in c
Structure in c
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 

Similar to Hybrid inheritance

Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
zindadili
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
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)
Julio Cesar Nunes de Souza
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
Nipam Medhi
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-iiDeepak Singh
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
BhavyaJain137
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Inheritance
InheritanceInheritance
Inheritance
Misbah Aazmi
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphismFALLEE31188
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
C test
C testC test
Learn Java Part 11
Learn Java Part 11Learn Java Part 11
Learn Java Part 11
Gurpreet singh
 
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.
Mumbai B.Sc.IT Study
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
RanjanaThakuria1
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
ssuser6f3c8a
 
Technical questions
Technical questionsTechnical questions
Technical questions
Kirthan S Holla
 

Similar to 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
 

More from zindadili

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

More from 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
 

Recently uploaded

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

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; }