Multiple inheritance in c++

1,305 views

Published on

Discussion, concern to the Diamond Problem of Multiple Inheritance in C++.

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,305
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Multiple inheritance in c++

  1. 1. Multiple Inheritance in C++ :Diamond Problem<br />Fundamentals of Programming in C++<br />Presented By:<br />Saket KR. Pathak<br />Software Developer<br />2D/3D Graphics<br />1<br />Fundamentals of Programming C++<br />
  2. 2. Two friends taking burger to the common.<br />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) ... <br />Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ... <br />2<br />Fundamentals of Programming C++<br />
  3. 3. Let's see a code-snippet:<br />Code Snippet:<br />//Base class<br />class Burger<br />{<br />public:<br /> Burger(int);//Simple parameterized constructor<br />  virtual ~Burger();//Normal Override Destruct-or to avoid memory leak<br />  virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />  virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />};<br />3<br />Fundamentals of Programming C++<br />
  4. 4. //Derived class - 1st<br />class Guy1   :   public Burger<br />{<br /> public:<br />    Guy1(int);//Simple parameterized constructor<br />    virtual ~Guy1();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie(); //General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha(); //General Virtual Function to override in Derived Classes<br />};<br />//Derived class - 2nd <br />class Guy2   :   public Burger<br />{<br /> public:<br />    Guy2(int); //Simple parameterized constructor<br />    virtual ~Guy2();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie(); //General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha(); //General Virtual Function to override in Derived Classes<br />};<br />4<br />Fundamentals of Programming C++<br />
  5. 5. //Derived class – 3rd<br />class CommonFriend   :   public Guy1, public Guy2<br />{<br /> public:<br />    CommonFriend(); //Simple parameterized constructor<br />    virtual ~CommonFriend(); //Normal Override Destruct-or to avoid memory leak<br />};<br />Now when we call a burger of either type for the common friend like;<br />int main()<br />{<br />            CommonFriendneedBurger;<br />            needBurger.McVeggie();                                 //Error type Ambiguous<br />            needBurger.cafeMocha();                               //Error type Ambiguous<br /> <br />            return 0;<br />}<br />5<br />Fundamentals of Programming C++<br />
  6. 6. This will create Compiler Error for "Ambiguous Type". Now why ... ???<br />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.<br /> <br />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.<br />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 ... <br />6<br />Fundamentals of Programming C++<br />
  7. 7. class Guy1   :   public virtual Burger<br />{<br />  public:<br />    Guy1(int); //Simple parameterized constructor<br />    virtual ~Guy1(); //Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie(); //General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha(); //General Virtual Function to override in Derived Classes<br />}; <br /> <br />//Derived class - 2nd <br />class Guy2   :   public virtual Burger<br />{<br />  public:<br />    Guy2(int); //Simple parameterized constructor<br />    virtual ~Guy2(); //Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie(); //General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha(); //General Virtual Function to override in Derived Classes<br />};<br />7<br />Fundamentals of Programming C++<br />
  8. 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. <br />That's all I think we need to do with multiple inheritance and Diamond problem. <br />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 ... <br />8<br />Fundamentals of Programming C++<br />
  9. 9. References:<br />http://en.wikipedia.org/wiki/Multiple_inheritance<br />http://www.cprogramming.com/tutorial/virtual_inheritance.html<br />http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf<br />9<br />Fundamentals of Programming C++<br />

×