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 questionsFarag Zakaria
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 
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 Kamlesh Makvana
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, RecursionSreedhar Chowdam
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and OperatorsSunil OS
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asmaAbdullahJana
 
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
 
20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism Intro C# Book
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
Web application architecture
Web application architectureWeb application architecture
Web application architectureIlio Catallo
 

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
Multiple InheritanceMultiple Inheritance
Multiple Inheritanceadil raja
 
Multiple Inheritance For C++
Multiple Inheritance For C++Multiple Inheritance For C++
Multiple Inheritance For C++elliando dias
 
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...cprogrammings
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Laxman Puri
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
Wan Important Questions
Wan Important QuestionsWan Important Questions
Wan Important QuestionsSaket Pathak
 
A Guy and gal in STL
A Guy and gal in STLA Guy and gal in STL
A Guy and gal in STLSaket Pathak
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in CSaket Pathak
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignmentSaket Pathak
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++Abhishek Pratap
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classesDocent Education
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchymyrajendra
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 

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++ Diamond Problem Explained

C++ diamond problem
C++ diamond problemC++ diamond problem
C++ diamond problemSaket Pathak
 
[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 - 13Chris Ohk
 
Дмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репортДмитрий Демчук. Кроссплатформенный краш-репорт
Дмитрий Демчук. Кроссплатформенный краш-репортSergey Platonov
 
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)Unity Technologies
 
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?Doug Hawkins
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerGarth Gilmour
 
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 KingGR8Conf
 
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" DependenciesPVS-Studio
 
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.pdfssuser33f16f
 
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 2018Kouji Matsui
 
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 : NotesSubhajit Sahu
 
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 adventuresFrits Van Der Holst
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_iiNico Ludwig
 
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 CISebastian Witowski
 

Similar to Multiple Inheritance in C++ Diamond Problem Explained (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

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Multiple Inheritance in C++ Diamond Problem Explained

  • 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++