
Background The C++ Programming Object-oriented programming is often de- ned as the combination of Abstract Data Language Typ es ADTs with Inheritance and Dy- namic Binding Single and Multiple Inheritance in Each concept addresses a di erent asp ect of system decomp osition: C++ 1. ADTs decomp ose systems into two-dimensional grids of mo dules { Each mo dule has public and private inter- faces Douglas C. Schmidt 2. Inheritance decomp oses systems into three-dimensional www.cs.wustl.edu/schmidt/ hierarchies of mo dules { Inheritance relationships form a \lattice" [email protected] 3. Dynamic binding enhances inheritance Washington University, St. Louis { e.g., defer implementation decisions until late in the design phase or even until run-time! 2 1 Motivation for Inheritance Data Abstraction vs. Inheritance Inheritance allows you to write co de to DATA ABSTRACTION certain cases and allows other de- (2 DIMENTIONAL) handle (2 DIMENTIONAL) velop ers to write co de that handles more sp ecialized cases, while your co de contin- ues to work Inheritance partitions a system architec- ture into semi-disjoint comp onents that are related hierarchically Therefore, we may be able to mo dify and/or reuse sections of the inheritance hierarchy without disturbing existing co de, e.g., { Change sibling subtree interfaces i.e., a consequence of inheritance INHERITANCE (3 DIMENTIONAL) { Change implementation of ancestors i.e., a consequence of data abstraction 3 4 Inheritance Overview Visualizing Inheritance A typ e called a sub class or derived typ e can inherit the characteristics of another typ es called a sup erclass or base typ e Base The term sub class is equivalent to derived typ e { Derived 2 A derived typ e acts just like the base typ e, Derived for an explicit list of: except 1 Derived 1. Sp ecializations 5 { Change implementations without changing base class interface the Derived Most useful when combined with dynamic Derived 4 binding 3 Derived 6 2. Generalizations/Extensions { Add new op erations or data to derived classes 5 6 Typ es of Inheritance Inheritance Trees vs. Inheritance DAGs Inheritance comes in two forms, dep end- ing on numb er of parents a sub class has Single Inheritance SI 1. Base Derived Derived Only one parent per derived class { 1 2 { Form an inheritance \tree" INHERITANCE Derived { SI requires a small amount of run-time over- when used with dynamic binding head Derived TREE 4 3 { e.g., Smalltalk, Simula, Object Pascal 2. Multiple Inheritance MI More than one parent per derived class { Base Base 1 Derived 2 { Forms an inheritance \Directed Acyclic Graph" AG D 1 { Compared with SI, MI adds additional run- overhead also involving dynamic bind- time INHERITANCE ing Derived Derived 3 DAG 4 { e.g., C++, Ei el, Flavors a LISP dialect 7 8 Inheritance Bene ts Inheritance Liabilities 1. Increase reuse and software quality 1. May create deep and/or wide hierarchies Programmers reuse the base classes instead of that are dicult to understand and navi- writing new classes gate without class browser to ols { Integrates black-b ox and white-b ox reuse by allowing extensibility and mo di cation with- out changing existing co de 2. May decrease p erformance slightly Using well-tested base classes helps reduce bugs i.e., when combined with multiple inheritance in applications that use them and dynamic binding Reduce object co de size 3. Without dynamic binding, inheritance has only limited utility 2. Enhance extensibility and comprehensibil- ity Likewise, dynamic binding is almost totally use- less without inheritance Helps supp ort more exible and extensible ar- chitectures along with dynamic binding { i.e., supp orts the op en/closed principle 4. Brittle hierarchies, which may imp ose de- Often useful for mo deling and classifying hierarchically- p endencies up on ancestor names related domains 10 9 Key Prop erties of C++ Inheritance The base/derived class relationship is ex- Inheritance in C++ plicitly recognized in C++ by prede ned standard conversions { i.e., a p ointer to a derived class may always be Deriving a class involves an extension to assigned to a p ointer to a base class that was the C++ class declaration syntax inherited publically But not vice versa::: The class head is mo di ed to allow a deriva- tion list consisting of base classes When combined with dynamic binding, this sp ecial relationship between inherited class typ es promotes a typ e-secure, p olymor- phic style of programming e.g., { i.e., the programmer need not know the actual typ e of a class at compile-time class Foo f /* :::g; class Bar : public Foo f /* :::g; { Note, C++ is not truly p olymorphic class Foo : public Foo, public Bar f /* :::g; i.e., op erations are not applicable to objects that don't contain de nitions of these op- erations at some p oint in their inheritance hierarchy 11 12 Simple Screen Class Sub classing from Screen The following co de is used as the base class: class Screen can be a public base class of class Window class Screen f public: Screen int = 8, int = 40, char = ' '; e.g., ~Screen void; short height void const f return this->height ; g class Window : public Screen f short width void const f return this->width ; g public: = h; g void height short h f this->height Window const Point &, int rows = 24, void width short w f this->width = w; g int columns = 80, Screen &forward void; char default char = ' '; Screen &up void; void set foreground color Color &; Screen &down void; void set background color Color &; Screen &home void; void resize int height, int width; Screen &b ottom void; // ::: Screen &display void; private: Screen &copy const Screen &; Point center ; // ::: Color foreground ; private: Color background ; short height , width ; // ::: char *screen , *cur pos ; g; g; 13 14 The Screen Inheritance Hierarchy Multiple Levels of Derivation A derived class can itself form the basis for further derivation, e.g., Point Menu : public Window f class Screen public: lab el const char *l; set void Color Menu const Point &, int rows = 24, int columns = 80, char default char = ' '; // ::: rivate: p Window char *lab el ; Menu // ::: g; class Menu inherits data and metho ds from b oth Window and Screen { i.e., sizeof Menu >= sizeof Window >= sizeof Screen Screen/Window/Menu hierarchy 15 16 Using the Screen Hierarchy Variations on a Screen: : : e.g., ps1 : Screen f public: virtual void dump ostream &; = 0 g ps2 : class Window : public Screen f Screen Screen class public: virtual void dump ostream &; g; class Menu : public Window f public: virtual void dump ostream &; g; // stand-alone function void dump Screen *s, ostream &o f w : image Some pro cessing omitted Window Menu // s->dump o; // *s->vptr[1] s, o; g Screen s; Window w; Menu m; Vector bv; Bit // OK: Window is a kind of Screen A p ointer to a derived class can be as- dump image &w, cout; signed to a p ointer to any of its public // OK: Menu is a kind of Screen base classes without requiring an explicit image &m, cout; dump cast: // OK: argument typ es match exactly image &s, cout; dump Menu m; Window &w = m; Screen *ps1 = &w; Vector is not a kind of Screen! // Error: Bit Screen *ps2 = &m; dump image &bv, cout; 18 17 Sp ecialization Example Using Inheritance for Sp ecialization Inheritance may be used to obtain the fea- tures of one data typ e in another closely A derived class sp ecializes a base class by related data typ e adding new, more sp eci c state variables and metho ds For example, class Date represents an ar- { Metho d use the same interface, even though bitrary Date: they are implemented di erently class Date f i.e., \overridden" public: Date int m, int d, int y; virtual void print ostream &s const; // ::: { Note, there is an imp ortant distinction between private: overriding, hiding, and overloading::: , day , year ; int month g; Class Birthday derives from Date, adding a name eld representing the p erson's birth- A variant of this is used in the template day, e.g., metho d pattern class Birthday : public Date f { i.e., b ehavior of the base class relies on func- public: tionality supplied by the derived class Birthday const char *n, int m, int d, int y : Date m, d, y, p erson strdup n fg ~Birthday void f free p erson ; g virtual void print ostream &s const; { This is directly supp orted in C++ via abstract // ::: base classes and pure virtual functions private: ; const char *p erson g; 20 19 Alternatives to Sp ecialization Implementation and Use-case Note that we could also use object com- Birthday::print could print the p erson's name p osition instead of inheritance for this ex- as well as the date, e.g., ample, e.g., void Birthday::print ostream &s const f class Birthday f << " was born on "; s << this->p erson public Date::print s; Birthday char *n, int m, int d, int y: s << "\n"; date m, d, y, p erson n fg g // same as b efore private: ; Date date e.g., ; char *p erson g; const Date july 4th 7, 4, 1993; Birthday my birthday "Douglas C. Schmidt", 7, 18, 1962; However, in this case we would not be able to utilize the dynamic binding facilities for july 4th.print cerr; base classes and derived classes // july 4th, 1993 birthday.print cout; my { e.g., // Douglas C. Schmidt was born on july 18th, 1962 Date *dp = &my birthday; Date *dp = &my birthday; // ERROR, Birthday is not a sub class of date! dp->print cerr; { While this do es not necessarily a ect reusabil- // ??? what gets printed ??? ity, it do es a ect extensibility::: // *dp->vptr[1]dp, cerr; 21 22 Extension/Generalization Using Inheritance for Example
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages16 Page
-
File Size-