Advanced Topics on Inheritance - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Advanced Topics on Inheritance

Description:

Advanced Topics on Inheritance Multiple inheritance allows a derived class to inherit from more than one direct base class class Bear: public ZooAnimal {/*...*/}; – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 11
Provided by: RuthM153
Category:

less

Transcript and Presenter's Notes

Title: Advanced Topics on Inheritance


1
Advanced Topics on Inheritance
  • Multiple inheritance allows a derived class to
    inherit from more than one direct base class
  • class Bear public ZooAnimal /.../
  • class Panda public Bear, public Endangered
    /.../
  • Virtual Inheritance allows the derived class to
    share the base class, as a single virtual base
    class
  • Regardless how often a base class shows up on
    different inheritance paths, only one base class
    will be included

2
Single Inheritance
  • class A
  • public
  • A(int num) aInt(num)
  • cout ltlt "Aconstructor"ltltendl
  • A()
  • cout ltlt"Adestructor"ltltendl
  • protected
  • int aInt
  • class B public A
  • public
  • B(int numa, int numb)
  • A(numa), bInt(numb)
  • cout ltlt "Bconstructor"ltltendl
  • B()
  • cout ltlt "Bdestructor"ltltendl
  • protected
  • int bInt
  • int main (int, char )
  • B b(2, 3)
  • return 0
  • Output
  • Aconstructor
  • Bconstructor
  • Bdestructor
  • Adestructor
  • Order of execution
  • constructors base class first then derived class
  • destructors reverse of constructors

A
B
3
Single Inheritance, continued
  • // A and B are as declared
  • // in the previous slide
  • class C public B
  • public
  • C(int numa, int numb, string s )
  • B(numa, numb), cStr(s)
  • cout ltlt "Cconstructor"ltltendl
  • C()
  • cout ltlt "Cdestructor"ltltendl
  • protected
  • string cStr
  • int main (int, char )
  • C c(4, 7, "hello")
  • return 0
  • Output
  • Aconstructor
  • Bconstructor
  • Cconstructor
  • Cdestructor
  • Bdestructor
  • Adestructor

A
B
C
4
Multiple Inheritance
  • class X
  • public
  • X() xChar('D') cout ltlt "Xdefault
    constructor"ltltendl
  • X(char c) xChar(c) cout ltlt "Xconstructor"ltlten
    dl
  • X()
  • cout ltlt "Xdestructor"ltltendl
  • protected
  • char xChar
  • class Y
  • public
  • Y(char c) yChar(c)
  • cout ltlt "Yconstructor"ltltendl
  • Y() cout ltlt "Ydestructor"ltltendl
  • protected
  • char yChar
  • class Z public X, public Y
  • public
  • Z(char xC,char yC, int num)
  • X(xC), Y(yC), zInt(num)
  • cout ltlt "Zconstructor"ltltendl
  • Z()
  • cout ltlt "Zdestructor"ltltendl
  • protected
  • int zInt
  • int main (int, char ))
  • Z zObj('z', 'b', 8)
  • return 0
  • Output
  • Xconstructor
  • Yconstructor
  • Zconstructor
  • Zdestructor

Y
X
Z
5
Multiple Inheritance, continued
  • class MI public C, public Z
  • public
  • MI(int numa, int numb, string s, char xC, char
    yC, int numZ )
  • C(numa, numb, s), Z(xC, yC, numZ) cout ltlt
    "MIconstructor"ltltendl
  • MI() cout ltlt "MIdestructor"ltltendl
  • protected
  • string cStr
  • int main (int, char )
  • MI mi(2,4,"eve", 'r', 's', 26)
  • return 0
  • Output
  • Aconstructor
  • Bconstructor
  • Cconstructor
  • Xconstructor
  • Yconstructor
  • Zconstructor
  • MIconstructor
  • MIdestructor
  • Zdestructor
  • Ydestructor
  • Xdestructor
  • Cdestructor

A
Y
X
B
Z
C
MI
6
All Base Class Constructors are Called
  • // X and Y as declared previously
  • class R public X, public Y
  • public
  • R(char xC, char yC, int numx) Y(yC), X(xC),
    rInt(numx) cout ltlt
  • "Rconstructor" ltlt endl
  • R()
  • cout ltlt "Rdestructor" ltltendl
  • protected
  • int rInt
  • class S public Y, public X
  • public
  • S(char yC, int numx) Y(yC), sInt(numx) cout ltlt
    "Sconstructor"ltltendl
  • S() cout ltlt "Sdestructor"ltltendl
  • protected
  • int sInt
  • int main (int, char )
  • R r ('x', 'y', 8)
  • return 0
  • Output
  • Xconstructor
  • Yconstructor
  • Rconstructor
  • Rdestructor
  • Ydestructor
  • Xdestructor
  • int main (int, char )
  • S s('y', 10)
  • return 0
  • Output
  • Yconstructor
  • Xdefault constructor

7
Base Pointer/Reference Type Restricts Interface
  • // Based on LLM Ch. 18.3 and
  • // todays studio exercises
  • Bear bear_ptr
  • new Panda ("bao_bao")
  • bear_ptr -gtprint() // ok
  • bear_ptr -gttoes() // ok
  • bear_ptr -gtcuddle() // not ok
  • bear_ptr -gtgrowl() // not ok
  • delete bear_ptr // ok
  • Endangered endangered_ptr
  • new Grizzly
  • endangered_ptr-gtprint() // ok
  • endangered_ptr-gttoes() // not ok
  • endangered_ptr -gtcuddle()// not ok
  • endangered_ptr -gtgrowl() // not ok
  • delete endangered_ptr // ok

Method Classes Declaring It
print Animal Bear Endangered Panda Grizzly
toes Bear Panda Grizzly
growl Grizzly
cuddle Panda
(virtual) destructor Animal Bear Endangered Panda Grizzly
8
Member Inheritance from Multiple Base Classes
  • When a class has multiple base classes, a derived
    class can inherit a member with the same name
    from two or more base classes
  • Unqualified uses of that name are ambiguous
  • // Loosely based on LLM Ch. 18.3
  • double Gryphonmax_weight() const
  • return stdmax (Eaglemax_weight_, //
    scoping necessary
  • Lionmax_weight_) //
    scoping necessary
  • This in turn motivates the use of virtual base
    classes
  • // Single Animal instance shared by Eagle and
    Lion parts of Gryphon
  • class Eagle virtual public Animal /.../
  • class Lion virtual public Animal /.../
  • class Gryphon public Eagle, public Lion
    /.../

9
More About Virtual Base Classes
  • Still Polymorphic
  • Can convert between uses as Derived vs. Base
  • Members of virtual Base class normally can be
    uniquely identified
  • base class is instantiated only once
  • if the variable is in both base and derived
    class, then derived class has higher precedence
  • If the member is in 2 derived classes, then it is
    still ambiguous
  • move members up to avoid that, e.g.,
    Animalmax_weight_
  • The most derived class controls the
    initialization of the shared virtual base class

10
Virtual Base Class Constructor/Destructor Order
  • Constructors
  • All virtual base classes
  • up each branch of the inheritance lattice,
    according to order in which immediate base
    classes were declared
  • Non-virtual base classes
  • up each branch of the inheritance lattice,
    according to order in which immediate base
    classes were declared
  • Most derived class last
  • Destructors (in reverse order of constructors)
  • Most derived class first
  • Then each non-virtual base class, moving up
    lattice
  • Then each virtual base class, moving up lattice
Write a Comment
User Comments (0)
About PowerShow.com