In Her Tance 123

Embed Size (px)

Citation preview

  • 8/16/2019 In Her Tance 123

    1/71

  • 8/16/2019 In Her Tance 123

    2/71

    CS202 3- 1

  • 8/16/2019 In Her Tance 123

    3/71

    Single Inheritance

    Introduction to Inheritance"Using" versus "Containing" Relationships"Containing" Relationships...through inheritance

    Inheritance and Derived ClassesCreating a Derived Class...the synta !or single inheritanceCreating a Derived Class...constructors destructorsConstructor Initiali#ation $ists

    %hat can &e Inherited'

    Multiple and Virtual Inheritance

    Creating a Derived Class...the synta !or (ultiple inheritance)irtual Inheritance

    Object Oriented Programming

    So !ar *e have used classes and o&+ects to represent generali#ed a&stractions.

    %e learned ho* to ena&le these a&stractions to &e used in the sa(e conte ts as &uilt-intypes.%e learned *hat design tradeo,s to (a e to eep our a&stractions as e cient as possi&le./ut even though *e *ere using o&+ects *e *ere not using o&+ect-oriented progra((ing.

    %e *ere si(ply one step closer &y understanding the synta o! classes and o&+ects.ur a&stractions *ere li(ited to stand alone classes.

  • 8/16/2019 In Her Tance 123

    4/71

    Object Oriented Programming

    In the o&+ect-oriented progra((ing paradig( *e &egin to consider using classes in

    con+unction *ith one another.%e should no longer thin a&out classes or o&+ects in isolation !ro( one another.Instead o! si(ply creating user de ned data types *e create a hierarchy o! related andinterdependent classes and o&+ects !ollo*ing the natural structure o! the pro&le(.

    his is &ecause o&+ect-oriented progra((ing e tends the concept o! data a&straction toapply across a&stractions.

    Object Oriented Programming

    &+ect-oriented progra((ing can involve a natural *ay o! thin ing a&out solutions.%e organi#e in!or(ation in *ays that t an application as it e ists in the real *orld.Unli e procedural a&straction *here *e !ocus on *hat actions ta e place 4i.e. ver&s5 ino&+ect-oriented progra((ing *e !ocus on the co(ponent parts 4i.e. nouns5 and therelationships &et*een these parts.

    his (eans *e (ust thin a&out creating solutions in an entirely ne* (anner.%e rst consider nouns then ver&s.

    Object Oriented Programming

    &+ect-oriented solutions are designed &ased on an inheritance hierarchy *hich de nesthe relationships &et*een classes *here one class shares the structure and6or&ehavior o! one or (ore classes.

    o provide this type o! design re7uires that *e understand ho* to i(ple(ent suchrelationships &et*een o&+ects. here!ore our rst step in understanding o&+ect-orientedprogra((ing is to learn the synta and se(antics o! inheritance hierarchies.

    %e *ill also learn ho* to e tend a&stractions *ith ne* !unctionality even *hen the code !orthose a&stractions is not availa&le.

  • 8/16/2019 In Her Tance 123

    5/71

    Inheritance Hierarchies

    /y de ning a class that is &ased on another class using inheritanceone class is a speciali#ation o! another.

    Such a class is said to &e a derived class . he class it is derived !ro( is a base class . he derived class inherits the &ase class8 (e(&ers. he &ene t o! this type o! relationship is that it allo*s reuse o!e isting code !ro( the &ase class and allo*s us to !ocus on the ne* orspeciali#ed &ehavior in the derived class.9n e isting progra( should not &e a*are that a ne* derived

    class has &een created i! the speciali#edrelationship is properly de ned and encapsulated.

    Inheritance Hierarchies

    :very hierarchy has a root 4e.g. &ase class5 *hich has #ero or (orechildren.

    :ach child 4e.g. derived class5 is either a lea! or &ranchesinto children o! its o*n.:ach class is inherently related to its parent as *ell as to itsancestors.In C;; the root o! each hierarchy or su&-hierarchy is called a &ase

    class.I! the &ase class is the parent o! the class in 7uestion then it is a

    direct &ase class. ther*ise i! it is an ancestor then it is anindirect &ase class.

  • 8/16/2019 In Her Tance 123

    6/71

  • 8/16/2019 In Her Tance 123

    7/71

  • 8/16/2019 In Her Tance 123

    8/71

  • 8/16/2019 In Her Tance 123

    9/71

    Single I nheritance

    o speci!y a derived class *e de ne the class as *e learned &ut *e also add the &ase class8na(e as part o! the derived class8 de nition.

    %e don8t need to alter the &ase classes to speci!y *hich classes are derived !ro( the(.=or public derivation *here derived is the na(e o! the derived class and base is the na(eo! the &ase class>

    class checking : public account //derivation{

    public:...

    };

    Single I nheritance

    %e speci!y in the derived class *hich class is to &e its parent. It is this parent8s (e(&ersthat are then inherited &y the derived class.Saying class derived : public base esta&lishes a single inheritance hierarchy. he ey*ord public speci es that all pu&lic (e(&ers o! the &ase class re(ain pu&lic in the

    derived class. his is called pu&lic derivation and is ho* *e speci!y an "is a" relationship &et*een t*oclasses.

    /base class class account {public:

    account();void statement(); private:char name[32 ; //account o!ner "loat balance; //account balance

    };

    //checking class derived "rom account class checking : public class account {public:

    checkin

  • 8/16/2019 In Her Tance 123

    10/71

  • 8/16/2019 In Her Tance 123

    11/71

    Single I nheritance

    &+ects o! the chec ing and savings classes contain all o! the (e(&ers o! an account o&+ectand can &e used *here ever an account o&+ect can &e used.

    savings object

    account::statement()name[]balance

    a c c o u n t savings::get _ interest()interest

    c h e c k i n g savingschecking object

    account::statement()name[]balance

    checking::get _ charges()charges

  • 8/16/2019 In Her Tance 123

    12/71

    Single I nheritance

    :ven though inheritance hierarchies allo* derived classes to inherit (e(&ers !ro( their&ase classes it does not (ean that those (e(&ers *ill &e accessi&le *ithin a derived class.

    his is &ecause (e(&ers *ithin a hierarchy have their o*n visi&ility.9s *e have seen pu&lic (e(&ers o! a &ase class are visi&le and !ully accessi&le &y classesderived !ro( the(. 9nd data and (e(&er !unctions in the private section are only availa&le tothe class in *hich they are de ned. hey are not accessi&le to any other class 4*ith thee ception o! !riends5.

    Single I nheritance

    Derived classes do not have access to a &ase class8 private data and (e(&er !unctions eventhough they are inherited.

    :ven though (e(ory is allocated !or such data (e(&ers they (ay only &e accessed !ro((e(&ers *ithin the &ase class itsel! 4or !riends5.

    his is i(portant &ecause giving any other class 4&esides a !riend5 access to private in!or(ation*ould co(pro(ise our a&ility to ensure data hiding and encapsulation.Such a co(pro(ise *ould decrease the value o! progra((ing *ith o&+ects.

    Single I nheritance

    ?reviously *e reco((ended that data (e(&ers &e speci ed in the private section./y !ollo*ing this guideline *hen designing hierarchies all derived classes *ould e plicitly needto use the &ase class8 pu&lic (e(&er !unctions to access inherited data.

    his isn8t practical *hen &uilding classes that are intended to *or in har(ony *ith one another.9nd it reduces our a&ility to e tend the !unctionality o! a given class in the !uture./y declaring (e(&ers as protected, derived classes have access to &ase class (e(&ers*hile restricting access &y client applications.

  • 8/16/2019 In Her Tance 123

    13/71

  • 8/16/2019 In Her Tance 123

    14/71

    Constructors i n Hierarchies

    9 &ase class constructor is al*ays invo ed &e!ore a derived class constructor in aninheritance hierarchy.

    his (eans that a derived class8 constructor can assu(e that the &ase class (e(&ers have &eeninitiali#ed &y the ti(e it is e ecuted. he &ody o! a derived class constructor is e ecuted last a!ter the &ase class and all indirect &aseclass constructors *ithin the hierarchy have e ecuted./ut *hen *e have a derived class *e are not e plicitly using the &ase class8 constructor.Instead the &ase class@ constructor is implicitly invo ed &y the derived class constructor thatinitiali#es the &ase class (e(&ers.

    Constructors i n Hierarchies

    %hen the &ase class has a de!ault constructor it is auto(atically invo ed *hen an o&+ect o! thederived class is de ned. his happens *hether or not the derived class constructor is a de!aultconstructor or re7uires argu(ents.Supplying a de!ault constructor in our &ase classes allo*s !or the (ost straight!or*ard classdesign. 9nd supplying a de!ault constructor in a derived class (a es it easier to use i! classesare su&se7uently derived !ro( it.

    Constructors

    $include %iostream.h& class account {public:

  • 8/16/2019 In Her Tance 123

    15/71

    account();private:

    char name[32 ; "loat balance;};

    class checking : public account { public:checking();

    private:"loat charges;

    };

    class savings : public account { public:savings();

    private:"loat interest;

    };

    $include 'account.h' account::account() : balance( ) {strncp (name* 'none'* 32); name[3+ , - -;cout %%'account constructor called' %%endl;

    }checking::checking() : charges( ) {

    cout %%'checking constructor called' %%endl;}savings::savings() : interest( ) {

    cout %%'savings constructor called' %%endl;}After the client saying checking c;

    savings s;

    account constructor called checking constructor called account constructor called savingsconstructor called

  • 8/16/2019 In Her Tance 123

    16/71

    ...

    Constructors i n Hierarchies

    I! a &ase class constructor e pects an argu(ent list the derived class (ust e plicitly speci!ythe &ase class constructor8s argu(ents.

    I! it doesn8t then the &ase class is e pected to have a de!ault constructor *hich is i(plicitlycalled.%e e plicitly speci!y the &ase class constructor8s argu(ents &y listing the &ase class constructor inthe derived class8 initiali#ation list along *ith the actual argu(ents e pected &y the &ase classconstructor.

    Client progra(> derived o&+410 205A

    derived::derived(int i* int 0) {

    Constructors in Hierarchies

    he argu(ents to the &ase class constructor can only consist o! values supplied as argu(ents to thederivedclass constructor constants literals glo&al varia&les or e pressions (ade up !ro( such values.

    %e cannot use derived class data (e(&ers as argu(ents to a &ase class constructor nor can *einvo e a (e(&er !unction o! the derived class and use its return value as one o! the actualargu(ents

    &ecause the derived class has not yet &een initiali#ed.

  • 8/16/2019 In Her Tance 123

    17/71

    Constructors i n Hierarchies

    he initiali#ation list causes the &ase class constructor to &e invo ed *ith the correctargu(entsB

    he order o! the argu(ents !or the &ase class is very i(portant. hey (ust &e listed in the sa(eorder as the &ase class constructor e pects.I! *e had not included the &ase class constructor in the initiali#ation list o! the derived classthen the de!ault &ase class constructor *ould &e invo ed.

    I! no de!ault &ase class constructor e ists then a co(pile error results.

    Member Hiding

    iding applies the sa(e !or data (e(&ers as it does !or (e(&er !unctions.9ny &ase class data (e(&ers that are pu&lic or protected are accessi&le &y thederived class.I! the derived class de nes data (e(&ers o! the sa(e na(e 4even though the types (ay &edi,erent5 any &ase class data (e(&ers o! that na(e are hidden.It is the derived class data (e(&er that is accessed and not the hidden &ase class (e(&erregardless o! the data type.

    Destructors in Hierarchies

    Destructors are invo ed at the end o! the li!eti(e o! an o&+ect.Destructors are invo ed in the opposite order !ro( *hich their constructors are invo ed. his (eans that the derived class destructor is invo ed &e!ore its &ase class destructor.I! there are indirect &ase classes this se7uence continues until the !urthest &ase classdestructor is invo ed.9 derived class destructor is guaranteed that its &ase class (e(&ers are still availa&le !oruse.

  • 8/16/2019 In Her Tance 123

    18/71

    Timing of Constructor U se

    a c c o u n t : : a c c o u n t ( c o n s tc h a r * n , f l o a t b ) :

    b a l a n c e ( b )

    {s t r n c p y ( n a m e ,n , ! ) "n a m e [ # ] $ % & ' % "

    s a v i n g s : : s a v i n g s ( c o n s tc h a r * n , f l o a t b ) :

    a c c o u n t ( n , b ) ,

    i n t e r e s t ( ' )

    {

    s a v i n g s * s "

    s $ n e s a v i n g s ( * n a m e * , + ' ' ) ", , ,

    d e l e t e s "

  • 8/16/2019 In Her Tance 123

    19/71

  • 8/16/2019 In Her Tance 123

    20/71

    Introduction to C++

    ! tending

    ehavior

    CS202 3- 3

  • 8/16/2019 In Her Tance 123

    21/71

    Member Hiding%hat happens *hen (e(&ers in ourhierarchy have the sa(e na(e' Doesoverloading occur' < B

    verloading (eans that *e have uni7uesignatures !or the sa(e na(ed !unction *ithinthe sa(e scope. In a hierarchy each class hasits o*n separate class scope. verloadingdoesn8t apply &et*een classes.

    Instead inheritance allo*s (e(&ers in a &aseclass to &e hidden &y (e(&ers o! the sa(ena(e in a derived class. /y hiding &ase class(e(&ers the &ehavior o! those !unctions can&e rede ned &y the derived class *ithoutchanging the &ase class or a,ecting e istingclient applications.

  • 8/16/2019 In Her Tance 123

    22/71

    CS202 3- 3E

  • 8/16/2019 In Her Tance 123

    23/71

    Member HidingFe(&ers are hidden anyti(e *e speci!y adata (e(&er or a (e(&er !unction in aderived class that has the sa(e na(e as a(e(&er in a &ase class.

    9 (e(&er !unction in a derived classes hidesa (e(&er !unction in a &ase class even i! thesignatures are di,erent.

    %hen that (e(&er is accessed either !ro( aclient o! the derived class or *ithin the derivedclass itsel! it is the derived (e(&er that isused.

    I! the argu(ent list used &y the client doesnot (atch any o! the !unctions de ned *ithinthe derived class a co(pile error *ill occureven i! a &ase class has a

  • 8/16/2019 In Her Tance 123

    24/71

    (atching !unction.CS202 3- 3G

  • 8/16/2019 In Her Tance 123

    25/71

  • 8/16/2019 In Her Tance 123

    26/71

  • 8/16/2019 In Her Tance 123

    27/71

    Overloaded Membersverloaded !unctions and overloaded

    operators are inherited in the sa(e (anneras any other (e(&er !unction de ned *ithinthe &ase class.

    I! a derived class has the sa(e na(ed !unctionor operator as in the &ase class then the &aseclass overloaded !unction or overloadedoperator is hidden even i! the signatures di,er.

    o*ever the constructor destructor copyconstructor assign(ent operator address-o!operator and co((a operator are notoverloaded. hese &ase class !unctions andoverloaded operators are hidden and are notdirectly accessi&le *ithin the derived class orthrough

  • 8/16/2019 In Her Tance 123

    28/71

    an o&+ect o! the derived class.CS202 3- 3

  • 8/16/2019 In Her Tance 123

    29/71

    Copy Constructors, = ops

  • 8/16/2019 In Her Tance 123

    30/71

    CS202 3- 3J

  • 8/16/2019 In Her Tance 123

    31/71

    Copy Constructors%hen *e e plicitly de ne either a copy

    constructor or an assign(ent operator in aderived class *e are responsi&le !or ensuringthat the &ase class copy constructor and

    assign(ent operator are called. his is &ecause*hen *e i(ple(ent our o*n copy constructor orassign(ent operator the co(piler no longerprovides an i(plicit one and cannot guaranteethat the &ase class copy constructor or

    assign(ent op are called.=or the copy constructor *e speci!y the &aseclass8 copy constructor in the initiali#ation list o! the derivedclass8 copy constructor.student::student(const student 1s) : checking(s) {

  • 8/16/2019 In Her Tance 123

    32/71

    CS202 3-0

    ... his *or s &ecause a student o&+ect is a chec ing o&+ectA

    chec ing@s constructor (y &e i(plicit ore plicitly de ned.

  • 8/16/2019 In Her Tance 123

    33/71

    Copy Constructors=or the assign(ent operator this is not as

    si(ple. %e (ust invo e the &ase class8assign(ent operator !ro( *ithin the &ody o! ourderived class8 assign(ent op.

    o do so re7uires that *e cast the currento&+ect4accessi&le &y Kthis5 into a &ase class o&+ect as!ollo*s>

    student 1student::operator,(const student 1s) {

    ... static#cast%checking 1&( this) , s;

    he staticLcast operator !orces the type o! thestudent o&+ect to &e a re!erence to a chec ingo&+ect. It causes the overloaded chec ingassign(ent op. to &e used !or the o&+ect *e areassigning to. %hen *e assign the student

  • 8/16/2019 In Her Tance 123

    34/71

    o&+ect to this re!erence the overloadedchec ing assign(ent operator is called *ith the

    CS202 3- 1 chec ing part o! the student o&+ect.

  • 8/16/2019 In Her Tance 123

    35/71

    Copy Constructors%hy is the cast i(portant'

    I! *e didn8t use a cast to change the type o! the o&+ect *e are assigning to

    *e *ould then have a recursive callto the overloaded student assign(entoperatorB

    In the !ollo*ing e a(ple it *e had not calledthe chec ing class8 copy constructor andassign(ent operator !ro( the student classcopy constructor and assign(ent operator *e*ould correctly copy the student part &ut notthe chec ing and account parts.

  • 8/16/2019 In Her Tance 123

    36/71

    CS202 3- 2

  • 8/16/2019 In Her Tance 123

    37/71

    Dyn. Memory - Page 1 of 3class account

    { public:account(const char ,'none'* "loat, );account(const account 1); account();account 1operator,(const account 1);void statement();

    private: char name; "loat balance; };

    class checking : public account{ public:

    checking(const char ,'none'* "loat, *"loat, ); void statement();

    private: "loat charges; };

    class student : public checking{ public:student(const char ,'none'* "loat, * const char

    ,''); student(const student 1); student();student 1operator,(const student 1);void statement();

    private:char school;

    };CS202 3- 3

  • 8/16/2019 In Her Tance 123

    38/71

    Dyn. Mem - Page 2 of 3

    account::account(const char n* "loat b) : balance(b){ name , ne! char[strlen(n) 4 + ;strcp (name* n);

    }account::account(const account 1a)

    { balance , a.balance;name , ne! char[strlen(a.name) 4+ ; strcp (name* a.name);cout %%'account cop constructor called' %%endl;

    }account:: account() { delete[ name; } account1account::operator,(const account 1a) {

    i" (this 5, 1a) { balance, a.balance; delete[name;

    name , ne! char[strlen(a.name) 4+ ; strcp (name* a.name);}cout %%'account assignment op called' %%endl;return( this);

    }

    CS202 3-

  • 8/16/2019 In Her Tance 123

    39/71

    Dyn. Mem - Page 3 of 3

    student::student(const student 1s) :checking(s) { //call cop ctor school , ne!char[strlen(s.school) 4 + ; strcp (school*s.school);cout %%'student cop constructor called' %%endl;

    }student:: student() {

    delete[ school;}student 1student::operator,(const student 1s) {

    i" (this 5, 1s) {static#cast%checking 1&( this) , s; //call assign opdelete[ school;school , ne! char[strlen(s.school) 4+ ; strcp (school* s.school);

    }cout %%'student assignment op called' %%endl;return( this);

    }

    CS202 3- E

  • 8/16/2019 In Her Tance 123

    40/71

    Using Declarations9 using declaration can &ring any hiddenpu&lic or protected &ase class (e(&er intothe scope o! the derived class.

    hese (e(&ers act as overloaded(e(&ers o! the derived class.o*ever such (e(&ers re(ain hidden in

    situations *here the argu(ent list is the sa(eas the sa(e na(ed (e(&er in the derivedclass.

    In the !ollo*ing e a(ple the usingdeclaration (a es the !unction ta ing thedou&le accessi&le to clients o! the derived class./ut the !unction ta ing the int is still hidden inthe derived class &y declaring a derived class(e(&er !unction that has the sa(e signature.

  • 8/16/2019 In Her Tance 123

    41/71

    CS202 3- G

  • 8/16/2019 In Her Tance 123

    42/71

    Using Declarationsclass base{ public:

    void "un(int) {cout %%'base::"un(int)' %%endl;

    }void "un(double) {

    cout %%'base::"un(double)' %%endl;

    }};

    class derived : public base{ public:

    using base::"un; //"un(int) 1 "un(double) no! in scopevoid "un(int) { //hides "un(int) brought into scope

    cout %%'derived::"un(int)' %%endl;}void "un(char ) { //de"ines ne! "un(char )

    cout %%'derived::"un(char )' %%endl;}

    };

    CS202 3- H

    IdiC

  • 8/16/2019 In Her Tance 123

    43/71

    Introduction to C++

    Multiple

    Inheritance

    CS202 3-

  • 8/16/2019 In Her Tance 123

    44/71

  • 8/16/2019 In Her Tance 123

    45/71

  • 8/16/2019 In Her Tance 123

    46/71

    Multiple Inheritance o speci!y a derived class *hen there is (orethan one &ase class *e add each direct &aseclass na(e as part o! the derived class8 header.

    his is the sa(e as *ith single inheritancee cept there is a co((a separatedlist o! &ase classes.

    class assets : public savings* public e6uit {public:

    ...

    };

    he de nition cannot &e cyclical sv .ng s e - u i t y

    9 direct &ase class cannot &especi ed (ore than once !or anyderived class. s s e t s

    a c c o u n t

  • 8/16/2019 In Her Tance 123

    47/71

    CS202 3- E0

  • 8/16/2019 In Her Tance 123

    48/71

    Multiple Inheritance9ll &ase class constructors are al*ays

    i(plicitly invo ed prior to the derived class8constructor.

    he order in *hich the constructors are

    invo ed is &ased on the order o! the &ase classdeclarations in the derived class. class assets :public savings* public e6uit (eans that the savingsconstructor is invo ed rst !ollo*ed &y thee7uity constructor. hus the order o!

    constructor invo ation !or these classes issavings e7uity and assets.

    9s long as each &ase class has a de!aultconstructor the derived class *ill auto(aticallyinvo e the(. I! not an e plicit call to a &ase class

    constructor *6 argu(ents

    ( t & i th i iti li# ti li t ! th d i d

  • 8/16/2019 In Her Tance 123

    49/71

    (ust &e in the initiali#ation list o! the derivedclass.

    CS202 3- E1

  • 8/16/2019 In Her Tance 123

    50/71

    Multiple InheritanceI! one o! the &ase class constructors e pects

    an argu(ent list the derived class constructor(ust supply the actual argu(ents e pected &ythe &ase class constructor in its initiali#ationlist.

    =or e a(ple i! the savings and e7uityconstructors e pect argu(ents the assetsclass constructor (ust provide the( as!ollo*s>

    class assets : public savings* public e6uit{ public:

    assets(const char n* "loats) : savings(n* s)*e6uit (n* e) {

    }

    ...

    CS202 3- E2

  • 8/16/2019 In Her Tance 123

    51/71

  • 8/16/2019 In Her Tance 123

    52/71

    Multiple Inheritance%hen (e(&ers o! t*o di,erent &ase

    classes have the sa(e na(e an a(&iguityresults *hen used.

    his a(&iguity cannot &e resolved &y the

    co(piler &ut (ust &e resolved at the ti(e theaccess is (ade either &y the class or &y theclient.

    he a(&iguity can &e resolved &y using the&ase class na(e and the scope resolution

    operator *hen accessing the a(&iguous(e(&er 4e.g. savings>>getLna(e5. his is theonly *ay such an a(&iguity can &e resolved*ithin the derived class *hen accessing the&ase class (e(&er.

  • 8/16/2019 In Her Tance 123

    53/71

    CS202 3- E3

  • 8/16/2019 In Her Tance 123

    54/71

    Multiple InheritanceFe(&ers in a &ase class are hidden anyti(e

    *e speci!y a data (e(&er or a (e(&er!unction in a derived class that has the sa(ena(e as a (e(&er in one o! the &ase classes.%hen that (e(&er is accessed either !ro( aclient o! the derived class or *ithin the derivedclass itsel! it is the derived class (e(&er thatis used.

    o avoid pro&le(s *ith a(&iguous resolutions i!

    there are (e(&ers *ith the sa(e na(e in the&ase classes then *e should (a e a rule tohide those (e(&ers *ith a (e(&er o! the sa(ena(e in our derived class. his is a si(ple ruleto !ollo* and *ill avoid pro&le(s o! a(&iguity!or clients o! our classes.

  • 8/16/2019 In Her Tance 123

    55/71

    CS202 3- E

  • 8/16/2019 In Her Tance 123

    56/71

    Common Base Classes:very o&+ect o!class chec ing isco(prised o! &othchec ing andaccount (e(&ersand every o&+ecto! class savings isco(prised o! &othsavings andaccount (e(&ers.

    Since an o&+ecto! class i&c inherits!ro( &oth chec ingand savings thereare t*o i(plicito&+ects o! class

    accountcontained inan i&co&+ect.

    CS202 3- EE

    a c c o u n tc h e c k i ng : : g e t _ c h

  • 8/16/2019 In Her Tance 123

    57/71

    a c c o u t

    c h e c k i n g s a v i n g s

    i b cibc object

    a c c o u n t : : g e t _ n a m e ( )

    a c c o u n t : : g e t _ b a l a n ce ( ) n a m e [ ]b a l a n c e

    g ga r g e s ( ) c ha r g e s

    a c c o u n t : : g e t _ na m e ( )

    a c c o u nt : : g e t _ ba l a n c e ( )n a m e [ ]b a l a n c e

    s a v i n g s : : ge t _ i n t e r e st ( ) i n t e r e st

    i b c : : g et _ m i n i mu m ( ) m i ni m u m

    lh

  • 8/16/2019 In Her Tance 123

    58/71

    Virtual Inheritance)irtual inheritance is an e tension o!

    (ultiple inheritance.

    Its o&+ective is to allo* e cient use o! (e(oryand eli(ination o! duplicate state spaces *hen

    designing inheritance hierarchies that share aco((on &ase class.

    he only ti(e one (ight use virtualinheritance *ithin a single inheritancehierarchy is *hen *e can !oresee !uture

    e tensions that (ay result in a derived classsharing a co((on &ase class.

    CS202 3- EG

  • 8/16/2019 In Her Tance 123

    59/71

    VilIhi

  • 8/16/2019 In Her Tance 123

    60/71

    Virtual InheritanceIt allo*s the direct &ase classes o! a derivedclass to &e derived !ro( a co((on direct orindirect &ase class *ithout duplicating data(e(&ers o! the &ase class.

    9n o&+ect o! class i&c has (e(oryallocated !or all o! the data (e(&ers o! classi&c chec ing savings but +ust one account.

    ibc object

    checking::get _ charges()charges

    savings::get _ interest()interest

    ibc::get _ minimum()minimum

    CS202 3- EH

    account::get name()

  • 8/16/2019 In Her Tance 123

    61/71

    account::get _ name()account::get _ balance()name[]balance

    VitlIhit

  • 8/16/2019 In Her Tance 123

    62/71

    Virtual Inheritance9 virtual &ase class is speci ed in our class

    derivation lists *ith the ey*ord virtual &e!oreeach co((on&ase class na(e as part o! the &ase classheader.

    class common#base {...};class base#+ : virtual public common#base {...};class base#2 : virtual public common#base {...};class derived : public base#+* public base#2 {...};

    :ach path leading !ro( the derived class tothe co((on &ase class (ust speci!y that theco((on &ase class is a virtual &ase class.

    I! the co((on &ase class is not a direct &aseclass o! the derived class then the derived classdoes not need to use the ey*ord virtual in thederivation list

  • 8/16/2019 In Her Tance 123

    63/71

    CS202 3- E

    VitlIhit

  • 8/16/2019 In Her Tance 123

    64/71

    Virtual Inheritance9 virtual &ase class is speci ed in our class

    derivation lists *ith the ey*ord virtual &e!oreeach co((on&ase class na(e as part o! the &ase classheader.

    class common#base {...};class base#+ : virtual public common#base {...};class base#2 : virtual public common#base {...};class derived : public base#+* public base#2 {...};

    :ach path leading !ro( the derived class tothe co((on &ase class (ust speci!y that theco((on &ase class is a virtual &ase class.

    I! the co((on &ase class is not a direct &aseclass o! the derived class then the derived classdoes not need to use the ey*ord virtual in thederivation list

    CS202 3 EJ

  • 8/16/2019 In Her Tance 123

    65/71

    CS202 3- EJ

    VirtualInheritance

  • 8/16/2019 In Her Tance 123

    66/71

    Virtual InheritanceI! a path e ists in the (ultiple inheritance

    hierarchy that derives the co((on &ase classas not virtual 4i.e. leaving out the ey*ordvirtual5 then this turns o, virtual inheritance!or that path and (ore than one instance o!the co((onL&ase *ill &e !or(ed.)irtual &ase classes are constructed &e!ore anyo! their derived classes. hey are alsoconstructed &e!ore any non virtual &aseclasses. 9nd destructors are still invo ed in thereverse order o! constructors.

    9ny direct or indirect &ase classes thathave initiali#ation lists that invo e thevirtual &ase class constructor are ignored.

    CS202 3- G0

  • 8/16/2019 In Her Tance 123

    67/71

    VirtualInheritance

  • 8/16/2019 In Her Tance 123

    68/71

    Virtual Inheritance9l*ays supply de!ault constructors *ith virtual&ase classes. his *ill avoid pro&le(s *heninitiali#ing virtual &ase class (e(&ers. I! avirtual &ase class has argu(ents then *e (uste pect the (ost derived class to have !ullno*ledge o! the indirect &ase classconstructor.

    9rgu(ents speci ed !or virtual &ase classconstructors (ust co(e !ro( the derived classthat is actually creating an o&+ect.

    )irtual &ase class constructorinvo ations !ro( inter(ediate &aseclasses are ignored.

    CS202 3- G1

  • 8/16/2019 In Her Tance 123

    69/71

  • 8/16/2019 In Her Tance 123

    70/71

  • 8/16/2019 In Her Tance 123

    71/71