/
CS 2304: CS 2304:

CS 2304: - PowerPoint Presentation

jane-oiler
jane-oiler . @jane-oiler
Follow
366 views
Uploaded On 2017-10-10

CS 2304: - PPT Presentation

Multiple Inheritance Gusukuma 2015 Multiple Inheritance cprogrammingcom Classes can extend multiple interfaces OR concrete classes Use scope resolution operator for functions with the same name from different base classes ID: 594787

ancestor virtual inheritance class virtual ancestor class inheritance public gusukuma 2015 foo bar multiple descendanta grandchild diamond descendantsb cross

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS 2304:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CS 2304:Multiple Inheritance

Gusukuma 2015Slide2

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 ProblemProblemAmbiguity 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 classAka: virtual inheritance (C++ specific)

Gusukuma 2015Slide3

Diamond Inheritance Problems

Gusukuma 2015

Ancestor

DescendantA

DescendantB

Grandchild

c

lass Ancestor{

virtual foo() {//base};

}

class

DescendantA

: virtual public Ancestor{

virtual foo(){implement}//override

bar(){implement}

}

class

DescendantsB

: virtual public Ancestor{ virtual foo(){implement} //override bar(){implement}}

c

lass Grandchild : virtual public

DescendantA

, virtual public

DescendantsB

{

virtual foo???

bar???

}Slide4

Diamond Inheritance Problems

Gusukuma 2015

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}Slide5

Multiple vs. Diamond/Virtual Inheritance

Diamond Inheritance is a specific case of Multiple inheritance

Gusukuma 2015

LandVehicle

WaterVehicle

AmphibiousVehicle

Vehicle

LandVehicle

WaterVehicle

AmphibiousVehicle

Multiple Inheritance

Multiple/Diamond InheritanceSlide6

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-reuseThe parents are both abstract and have few, or no defined members (data AND functions)Hence why Java uses interfaces for multiple inheritanceThe has-a relationship doesn’t solve the problem/Make senseSpecifically 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 2015Slide7

Multiple Inheritance Example

Gusukuma 2015

commercialPayment

personalPayment

Credit

Debit

CommercialCreditPayment

P

ersonalCreditPayment

CommercialDebitPayment

PersonalDebitPaymentSlide8

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 ADescendant B implements function B USING function A, but does not define function AGrandchild inherits from Descendant A and Descendant B getting a fully functional Function BThe two descendants are BOTH abstractGusukuma 2015Slide9

Cross Delegation Illustration

Gusukuma 2015

Ancestor

DescendantA

DescendantB

Grandchild

c

lass Ancestor{

virtual foo() = 0;

virtual bar() = 0;

}

class

DescendantA

: virtual public Ancestor{

virtual foo(){

doSomething

(); bar(); }//}class DescendantsB: virtual public Ancestor{

virtual bar(){

beAwesome

();

}

}

c

lass Grandchild : virtual public

DescendantA

, virtual public

DescendantsB

{

//other functionality

}Slide10

Cross Delegation (cont)

Gusukuma 2015

int

main

() {

Ancestor

& cool

=

Grandchild

();

cool.foo

();

//executes

doSomething

() AND beAwesome()

}Slide11

Cross Delegation Illustration

Gusukuma 2015

Ancestor

DescendantA

DescendantB

Grandchild

c

lass Ancestor{

virtual foo() = 0;

virtual bar() = 0;

}

class

DescendantA

: virtual public Ancestor{

virtual foo(){

doSomething

(); bar(); }}class DescendantsB: virtual public Ancestor{

virtual bar(){

beAwesome

();

}

}

c

lass Grandchild : virtual public

DescendantA

, virtual public

DescendantsB

{

//other functionality

}Slide12

Cross Delegation Illustration

Gusukuma 2015

Ancestor

DescendantA

DescendantB

Grandchild

class

DescendantsB

: virtual public Ancestor{

virtual bar(){

beAwesome

();

}

}

Grandchild2

DescendantC

class

DescendantsC

: virtual public Ancestor{

virtual bar(){

beCool

();

}

}Slide13

Cross Delegation (cont)

Gusukuma 2015

int

main

() {

Ancestor

& cool =

Grandchild

();

Ancestor

&

coolKid

=

Grandchild2

();cool.foo();

//executes

doSomething() AND beAwesome()

coolKid.foo(); //executes doSomething() AND beCool

()}Slide14

Multiple/Diamond Inheritance

Pros

Fine grained control

Up to

NxM classesStatically detect bad class combinationsAs opposed to a has-a relationship with polymorphic members which have no inherent checkingPolymorphic on all dimensionsConsCode can bulk up easilyHas

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