logging in or signing up Multiple Inheritance in C++ intnam Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 166 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: October 17, 2011 This Presentation is Public Favorites: 0 Presentation Description Discussion, concern to the Diamond Problem of Multiple Inheritance in C++. Comments Posting comment... Premium member Presentation Transcript Multiple Inheritance in C++ :Diamond Problem: 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++Slide 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++Slide 3: Let's see a 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 }; Code Snippet: 3 Fundamentals of Programming C++Slide 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++Slide 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() { CommonFriend needBurger ; needBurger.McVeggie (); //Error type Ambiguous needBurger.cafeMocha (); //Error type Ambiguous return 0; } 5 Fundamentals of Programming C++Slide 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++Slide 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++Slide 8: Now when the virtual function table for CommonFriend will be created then Compiler will mind the above structure and keep single instance of all the above base classes i.e Bueger , 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++Slide 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++ You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Multiple Inheritance in C++ intnam Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 166 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: October 17, 2011 This Presentation is Public Favorites: 0 Presentation Description Discussion, concern to the Diamond Problem of Multiple Inheritance in C++. Comments Posting comment... Premium member Presentation Transcript Multiple Inheritance in C++ :Diamond Problem: 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++Slide 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++Slide 3: Let's see a 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 }; Code Snippet: 3 Fundamentals of Programming C++Slide 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++Slide 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() { CommonFriend needBurger ; needBurger.McVeggie (); //Error type Ambiguous needBurger.cafeMocha (); //Error type Ambiguous return 0; } 5 Fundamentals of Programming C++Slide 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++Slide 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++Slide 8: Now when the virtual function table for CommonFriend will be created then Compiler will mind the above structure and keep single instance of all the above base classes i.e Bueger , 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++Slide 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++