1 / 17

Chapter 7: Inheritance

Chapter 7: Inheritance. Inheritance: implicit passing of information between program components. The concept is usually used in the context of complex data objects. Abstract Data Types Object Hierarchy Inheritance of Methods. Abstract Data Types. Data components

tavon
Télécharger la présentation

Chapter 7: Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7: Inheritance Inheritance: implicit passing of information between program components. The concept is usually used in the context of complex data objects. • Abstract Data Types • Object Hierarchy • Inheritance of Methods

  2. Abstract Data Types • Data components • Operations to manipulate the data components • Implementation of ADT: classes (C++), packages (ADA), objects (Smalltalk) • Basic idea:The data components and the programs that implement the operations are hidden from the external world. The objectisencapsulated.

  3. Encapsulation Direct encapsulation: Actual storage is maintained in the activation record of subprograms that use the object Indirect encapsulation: Actual storage is maintained only with the specification. Other programs use pointers to the actual storage

  4. Encapsulation Direct encapsulation: runs faster, compilation slow Indirect encapsulation:runs slower, compilation fast

  5. Generic abstract data types Generic abstract data types use templates This is the case when the type of the data components is not fixed in the specification. The operations stay the same no matter what the type is, e.g. a list of integers, a list of characters. Instantiation occurs at compiling time A generic type may be instantiated many times.

  6. Implementation of GADT • the generic definition is used as a template • specified data types (given in the calling program) are inserted in the definition • the definition is compiled • Disadvantages: instantiation results in a copy of the entire class (package) definition.

  7. Object hierarchy An object may be a special case of a more general object. Some of the properties are the same - these properties can be inherited

  8. Superclass1 (parent) Superclass2 (parent) Subcl 1.1 child Subcl 1. 2 child Subclass2.1 child Subclass2.1 child Derived classes

  9. Superclass1 (parent) Superclass2 (parent) Subclass1.1 child Subcl 1. 2 child Subclass2.1 child Subclass2.1 child Multiple Inheritance

  10. Generalization and specialization Down the hierarchy the objects become more specialized, up the hierarchy - more generalized Instantiation– The process of creating instances of a class

  11. Derived classes Derived classes inherit data components and/or methods Further, they can specify their own data components and their own specific methods. The specific parts may have same names as in the parent - they override the definition in the parent class.

  12. Implementation of derived classes • Copy-based approach - each instance of a class object has its own data storage containing all data components - specific plus inherited. • Direct encapsulation

  13. Implementation of derived classes Delegation-based approach – the object uses the data storage of the base class. Data sharing, Indirect encapsulation

  14. Inheritance of methodsVirtual functions • class Figure • { • public: • Figure(); • virtual void draw(); • virtual void erase(); • void center(); • void set_color(T Color); • void position_center(); • }; • void Figure:: center() • { • erase(); • position_center(); • draw(); • }

  15. class Box : public Figure • { • public: • Box(); • void draw(); • void erase(); • }; • in MAIN: • Box a_box; • a_box.draw(); // overrides base class • a_box.set_color(C); // inherits the method • a_box.center(); // makes use of virtual • // functions

  16. Implementation of virtual methods • A slot in the record defining the class. • The constructor fills in the location of the new virtual procedure if there is one. • If not, it fills in the location of the virtual procedure from the base class.

  17. Abstract Classes Abstract Classes - can serve only as templates, no data objects can be declared with the name of the class. Specified by NULL virtual functions virtual void TypeName() = 0;

More Related