SlideShare a Scribd company logo
1 of 9
Multiple Inheritance in C++ :Diamond Problem Fundamentals of Programming in C++ Presented By: Saket KR. Pathak Software Developer 2D/3D Graphics 1 Fundamentals of Programming C++
Two friends taking burger to the common. Hello all my mate... few days back, one of my friend close to my heart because of this Qs. :) asked me ... in a tea stall (the gathering place near my home, at evening after returning back from Office) ...  Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ...  2 Fundamentals of Programming C++
Let's see a code-snippet: Code Snippet: //Base class class Burger { public:   Burger(int);//Simple parameterized constructor   virtual ~Burger();//Normal Override Destruct-or to avoid memory leak   virtual void McVeggie();//General Virtual Function to override in Derived Classes   virtual void cafeMocha();//General Virtual Function to override in Derived Classes }; 3 Fundamentals of Programming C++
//Derived class - 1st class Guy1   :   public Burger {  public:     Guy1(int);//Simple parameterized constructor     virtual ~Guy1();//Normal Override Destruct-or to avoid memory leak     virtual void McVeggie();	//General Virtual Function to override in Derived Classes     virtual void cafeMocha();	//General Virtual Function to override in Derived Classes }; //Derived class - 2nd  class Guy2   :   public Burger {  public:     Guy2(int);	//Simple parameterized constructor     virtual ~Guy2();//Normal Override Destruct-or to avoid memory leak     virtual void McVeggie();	//General Virtual Function to override in Derived Classes     virtual void cafeMocha();	//General Virtual Function to override in Derived Classes }; 4 Fundamentals of Programming C++
//Derived class – 3rd class CommonFriend   :   public Guy1, public Guy2 {  public:     CommonFriend();	//Simple parameterized constructor     virtual ~CommonFriend();	//Normal Override Destruct-or to avoid memory leak }; Now when we call a burger of either type for the common friend like; int main() {             CommonFriendneedBurger;             needBurger.McVeggie();                                 //Error type Ambiguous             needBurger.cafeMocha();                               //Error type Ambiguous               return 0; } 5 Fundamentals of Programming C++
This will create Compiler Error for "Ambiguous Type". Now why ... ??? As we all know through the concept of overriding in inheritance we simple put a new definition for the function having same signature from base class, these are maintained by compiler by providing virtual function table (vtable) for classes Guy1 and Guy2.   But here in 3rd level of inheritance that is indeed multiple inheritances, the provided virtual function table has the pointer for both Guy1 and Guy2 classes which contains their respective pointer of Burger. As the result when we are calling for Mc.Veggie and cafeMocha, the compiler gets ambiguous to make it happen. To avoid this we can do it ... we need to do ...  6 Fundamentals of Programming C++
class Guy1   :   public virtual Burger {   public:     Guy1(int);		//Simple parameterized constructor     virtual ~Guy1();		//Normal Override Destruct-or to avoid memory leak     virtual void McVeggie();	//General Virtual Function to override in Derived Classes     virtual void cafeMocha();	//General Virtual Function to override in Derived Classes };    //Derived class - 2nd  class Guy2   :   public virtual Burger {   public:     Guy2(int);		//Simple parameterized constructor     virtual ~Guy2();		//Normal Override Destruct-or to avoid memory leak     virtual void McVeggie();	//General Virtual Function to override in Derived Classes     virtual void cafeMocha();	//General Virtual Function to override in Derived Classes }; 7 Fundamentals of Programming C++
Now when the virtual function table for CommonFriendwill be created then Compiler will mind the above structure and keep single instance of all the above base classes i.eBueger, Guy1, Guy2.  That's all I think we need to do with multiple inheritance and Diamond problem.  Yes people may surprise with the naming conventions of class, functions and instances. It's my choice I had tried to represent the problem in layman terms that will be easily imaginable as well as understandable instead of tricky words or random Letters ... that's my way ... yup I will expect comments from you C++ buddies ... so welcome ...  8 Fundamentals of Programming C++
References: http://en.wikipedia.org/wiki/Multiple_inheritance http://www.cprogramming.com/tutorial/virtual_inheritance.html http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf 9 Fundamentals of Programming C++

More Related Content

What's hot

Java concepts and questions
Java concepts and questionsJava concepts and questions
Java concepts and questions
Farag Zakaria
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
AbdullahJana
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
MomenMostafa
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 

What's hot (20)

Java concepts and questions
Java concepts and questionsJava concepts and questions
Java concepts and questions
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
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
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
Course1
Course1Course1
Course1
 
Function notes
Function notesFunction notes
Function notes
 
Qno 2 (a)
Qno 2 (a)Qno 2 (a)
Qno 2 (a)
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Introduction to Julia Language
Introduction to Julia LanguageIntroduction to Julia Language
Introduction to Julia Language
 

Viewers also liked

Multiple Inheritance For C++
Multiple Inheritance For C++Multiple Inheritance For C++
Multiple Inheritance For C++
elliando dias
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchy
myrajendra
 

Viewers also liked (20)

Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Multiple Inheritance For C++
Multiple Inheritance For C++Multiple Inheritance For C++
Multiple Inheritance For C++
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Wan Important Questions
Wan Important QuestionsWan Important Questions
Wan Important Questions
 
Wan notes
Wan notesWan notes
Wan notes
 
A Guy and gal in STL
A Guy and gal in STLA Guy and gal in STL
A Guy and gal in STL
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchy
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Container ship
Container ship Container ship
Container ship
 

Similar to Multiple inheritance in c++

CUDA by Example : Introduction to CUDA C : Notes
CUDA by Example : Introduction to CUDA C : NotesCUDA by Example : Introduction to CUDA C : Notes
CUDA by Example : Introduction to CUDA C : Notes
Subhajit Sahu
 

Similar to Multiple inheritance in c++ (20)

C++ diamond problem
C++ diamond problemC++ diamond problem
C++ diamond problem
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
Дмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репортДмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репорт
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)
 
Modern c++
Modern c++Modern c++
Modern c++
 
How to build the Web
How to build the WebHow to build the Web
How to build the Web
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android Update
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
GR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul KingGR8Conf 2009: Industrial Strength Groovy by Paul King
GR8Conf 2009: Industrial Strength Groovy by Paul King
 
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" DependenciesAnalyzing FreeCAD's Source Code and Its "Sick" Dependencies
Analyzing FreeCAD's Source Code and Its "Sick" Dependencies
 
ICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdfICT1002-W8-LEC-Introduction-to-C.pdf
ICT1002-W8-LEC-Introduction-to-C.pdf
 
Making archive IL2C #6-55 dotnet600 2018
Making archive IL2C #6-55 dotnet600 2018Making archive IL2C #6-55 dotnet600 2018
Making archive IL2C #6-55 dotnet600 2018
 
CUDA by Example : Introduction to CUDA C : Notes
CUDA by Example : Introduction to CUDA C : NotesCUDA by Example : Introduction to CUDA C : Notes
CUDA by Example : Introduction to CUDA C : Notes
 
Moving from Jenkins 1 to 2 declarative pipeline adventures
Moving from Jenkins 1 to 2 declarative pipeline adventuresMoving from Jenkins 1 to 2 declarative pipeline adventures
Moving from Jenkins 1 to 2 declarative pipeline adventures
 
Oop Extract
Oop ExtractOop Extract
Oop Extract
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii
 
5 Things I Wish I Knew About Gitlab CI
5 Things I Wish I Knew About Gitlab CI5 Things I Wish I Knew About Gitlab CI
5 Things I Wish I Knew About Gitlab CI
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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_...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 

Multiple inheritance in c++

  • 1. Multiple Inheritance in C++ :Diamond Problem Fundamentals of Programming in C++ Presented By: Saket KR. Pathak Software Developer 2D/3D Graphics 1 Fundamentals of Programming C++
  • 2. Two friends taking burger to the common. Hello all my mate... few days back, one of my friend close to my heart because of this Qs. :) asked me ... in a tea stall (the gathering place near my home, at evening after returning back from Office) ...  Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ...  2 Fundamentals of Programming C++
  • 3. Let's see a code-snippet: Code Snippet: //Base class class Burger { public: Burger(int);//Simple parameterized constructor   virtual ~Burger();//Normal Override Destruct-or to avoid memory leak   virtual void McVeggie();//General Virtual Function to override in Derived Classes   virtual void cafeMocha();//General Virtual Function to override in Derived Classes }; 3 Fundamentals of Programming C++
  • 4. //Derived class - 1st class Guy1   :   public Burger {  public:     Guy1(int);//Simple parameterized constructor     virtual ~Guy1();//Normal Override Destruct-or to avoid memory leak     virtual void McVeggie(); //General Virtual Function to override in Derived Classes     virtual void cafeMocha(); //General Virtual Function to override in Derived Classes }; //Derived class - 2nd  class Guy2   :   public Burger {  public:     Guy2(int); //Simple parameterized constructor     virtual ~Guy2();//Normal Override Destruct-or to avoid memory leak     virtual void McVeggie(); //General Virtual Function to override in Derived Classes     virtual void cafeMocha(); //General Virtual Function to override in Derived Classes }; 4 Fundamentals of Programming C++
  • 5. //Derived class – 3rd class CommonFriend   :   public Guy1, public Guy2 {  public:     CommonFriend(); //Simple parameterized constructor     virtual ~CommonFriend(); //Normal Override Destruct-or to avoid memory leak }; Now when we call a burger of either type for the common friend like; int main() {             CommonFriendneedBurger;             needBurger.McVeggie();                                 //Error type Ambiguous             needBurger.cafeMocha();                               //Error type Ambiguous               return 0; } 5 Fundamentals of Programming C++
  • 6. This will create Compiler Error for "Ambiguous Type". Now why ... ??? As we all know through the concept of overriding in inheritance we simple put a new definition for the function having same signature from base class, these are maintained by compiler by providing virtual function table (vtable) for classes Guy1 and Guy2.   But here in 3rd level of inheritance that is indeed multiple inheritances, the provided virtual function table has the pointer for both Guy1 and Guy2 classes which contains their respective pointer of Burger. As the result when we are calling for Mc.Veggie and cafeMocha, the compiler gets ambiguous to make it happen. To avoid this we can do it ... we need to do ...  6 Fundamentals of Programming C++
  • 7. class Guy1   :   public virtual Burger {   public:     Guy1(int); //Simple parameterized constructor     virtual ~Guy1(); //Normal Override Destruct-or to avoid memory leak     virtual void McVeggie(); //General Virtual Function to override in Derived Classes     virtual void cafeMocha(); //General Virtual Function to override in Derived Classes };    //Derived class - 2nd  class Guy2   :   public virtual Burger {   public:     Guy2(int); //Simple parameterized constructor     virtual ~Guy2(); //Normal Override Destruct-or to avoid memory leak     virtual void McVeggie(); //General Virtual Function to override in Derived Classes     virtual void cafeMocha(); //General Virtual Function to override in Derived Classes }; 7 Fundamentals of Programming C++
  • 8. Now when the virtual function table for CommonFriendwill be created then Compiler will mind the above structure and keep single instance of all the above base classes i.eBueger, Guy1, Guy2.  That's all I think we need to do with multiple inheritance and Diamond problem.  Yes people may surprise with the naming conventions of class, functions and instances. It's my choice I had tried to represent the problem in layman terms that will be easily imaginable as well as understandable instead of tricky words or random Letters ... that's my way ... yup I will expect comments from you C++ buddies ... so welcome ...  8 Fundamentals of Programming C++
  • 9. References: http://en.wikipedia.org/wiki/Multiple_inheritance http://www.cprogramming.com/tutorial/virtual_inheritance.html http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf 9 Fundamentals of Programming C++