170 likes | 278 Views
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
E N D
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 • 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.
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
Encapsulation Direct encapsulation: runs faster, compilation slow Indirect encapsulation:runs slower, compilation fast
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.
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.
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
Superclass1 (parent) Superclass2 (parent) Subcl 1.1 child Subcl 1. 2 child Subclass2.1 child Subclass2.1 child Derived classes
Superclass1 (parent) Superclass2 (parent) Subclass1.1 child Subcl 1. 2 child Subclass2.1 child Subclass2.1 child Multiple Inheritance
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
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.
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
Implementation of derived classes Delegation-based approach – the object uses the data storage of the base class. Data sharing, Indirect encapsulation
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(); • }
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
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.
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;