Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2304: Multiple Inheritance

Similar presentations


Presentation on theme: "CS 2304: Multiple Inheritance"— Presentation transcript:

1 CS 2304: Multiple Inheritance
35 minutes Gusukuma 2015

2 Multiple – Inheritance (cprogramming.com)
Classes can extend multiple interfaces OR concrete classes Use scope resolution operator for functions with the same name from different base classes The Diamond Problem Problem Ambiguity of multiple overrides, which override is inherited? C++ implements multiple copies of the same base class unless… You use virtual before class name when extending a class Aka: virtual inheritance (C++ specific) Gusukuma 2015

3 Diamond Inheritance Problems
class Ancestor{ virtual foo() {//base}; } Diamond Inheritance Problems class DescendantA: virtual public Ancestor{ virtual foo(){implement}//override bar(){implement} } Ancestor DescendantA DescendantB At Descendants A and B, t class Grandchild : virtual public DescendantA, virtual public DescendantsB{ virtual foo??? bar??? } class DescendantsB: virtual public Ancestor{ virtual foo(){implement} //override bar(){implement} } Grandchild Gusukuma 2015

4 Diamond Inheritance Problems
int main() { Grandchild testObj; testObj.DescendantA::bar(); //okay, note, bar can’t be virtual/polymorphic Ancestor& trouble = testObj; trouble.foo(); //compile error, override of foo is ambiguous, polymorphism prevented } Gusukuma 2015

5 Multiple vs. Diamond/Virtual Inheritance
Diamond Inheritance is a specific case of Multiple inheritance Multiple/Diamond Inheritance Multiple Inheritance Vehicle LandVehicle WaterVehicle LandVehicle WaterVehicle AmphibiousVehicle AmphibiousVehicle Gusukuma 2015

6 Multiple Inheritance – When? (isocpp.com)
Removal of if/switch statements Remember: Inheritance is NOT for code-reuse, but for flexibility Composition (has-a relationship) is for code-reuse The parents are both abstract and have few, or no defined members (data AND functions) Hence why Java uses interfaces for multiple inheritance The has-a relationship doesn’t solve the problem/Make sense Specifically the bridge design pattern (sourcemaking.com, isocpp.com) When you actually want a granularity of control NxM classes Where there are N variations of the first class and M variations of the second class Gusukuma 2015

7 Multiple Inheritance Example
Credit Debit personalPayment PersonalCreditPayment PersonalDebitPayment So in this example you have personal payments and you have commercial payments, each of these have a different protocol based on whether they are a personal transaction or a commercial transaction. Additionally, there are different protocols based on whether the payee is using credit or debit to complete the transaction. In this particular case, multiple inheritance can be used to express these relationships. In this case, it doesn’t make sense for a credit card to have a “personal” payment method or a “commercial” payment method, so a has-a relationship doesn’t work. If you wanted to process the different transactions without classes, you’d have to have if-else statements saying okay, is it a credit payment? Or is it a debit payment? Additionally, you get a fine control on which combinations of payments do what. Because of this, multiple inheritance is actually useful. Note that this does not have diamond inheritance. The most important aspect of this is that you can create subsections of code that can deal with only businesses or only individuals. Like wise, you can create subsections of code that deal with only credit payment, or only debit payment commercialPayment CommercialCreditPayment CommercialDebitPayment Gusukuma 2015

8 Diamond/Virtual Inheritance – When? (cprogramming.com)
Sister class/cross delegation Make another descendant of the same base class implement some functionality Descendant A implements function A Descendant B implements function B USING function A, but does not define function A Grandchild inherits from Descendant A and Descendant B getting a fully functional Function B The two descendants are BOTH abstract Gusukuma 2015

9 Cross Delegation Illustration
class Ancestor{ virtual foo() = 0; virtual bar() = 0; } Cross Delegation Illustration class DescendantA: virtual public Ancestor{ virtual foo(){ doSomething(); bar(); }// } Ancestor DescendantA DescendantB At Descendants A and B, t class Grandchild : virtual public DescendantA, virtual public DescendantsB{ //other functionality } class DescendantsB: virtual public Ancestor{ virtual bar(){ beAwesome(); } Grandchild Gusukuma 2015

10 Cross Delegation (cont)
int main() { Ancestor& cool = Grandchild(); cool.foo(); //executes doSomething() AND beAwesome() } Gusukuma 2015

11 Cross Delegation Illustration
class Ancestor{ virtual foo() = 0; virtual bar() = 0; } Cross Delegation Illustration class DescendantA: virtual public Ancestor{ virtual foo(){ doSomething(); bar(); }} Ancestor DescendantA DescendantB At Descendants A and B, t class Grandchild : virtual public DescendantA, virtual public DescendantsB{ //other functionality } class DescendantsB: virtual public Ancestor{ virtual bar(){ beAwesome(); } Grandchild Gusukuma 2015

12 Cross Delegation Illustration
class DescendantsC: virtual public Ancestor{ virtual bar(){ beCool(); } Cross Delegation Illustration class DescendantsB: virtual public Ancestor{ virtual bar(){ beAwesome(); } Ancestor DescendantC DescendantA DescendantB At Descendants A and B, t Grandchild2 Grandchild Gusukuma 2015

13 Cross Delegation (cont)
int main() { Ancestor& cool = Grandchild(); Ancestor& coolKid = Grandchild2(); cool.foo(); //executes doSomething() AND beAwesome() coolKid.foo(); //executes doSomething() AND beCool() } Gusukuma 2015

14 Multiple/Diamond Inheritance
Pros Cons Fine grained control Up to NxM classes Statically detect bad class combinations As opposed to a has-a relationship with polymorphic members which have no inherent checking Polymorphic on all dimensions Code can bulk up easily Has NxM classes (as opposed to bridge pattern’s N+M classes) Scales poorly Exponentially Coding complexity Have to keep track of which methods are and aren’t overloaded Gusukuma 2015


Download ppt "CS 2304: Multiple Inheritance"

Similar presentations


Ads by Google