Presentation is loading. Please wait.

Presentation is loading. Please wait.

BEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example.

Similar presentations


Presentation on theme: "BEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example."— Presentation transcript:

1 bEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example

2 bEgInSlIdE OOP-2 Strict Inheritance and Class Facets uType: (the class interface, how it can be used from outside) lExtended by inheritance. Subtyping! uImplementation: method bodies. lExtended, only for the added methods. uMold: structure, i.e., list of fields. lExtended, to add new fields. uFactory: The interface for creating objects. lNot inherited at all uMill: The implementation of constructors. lExtended

3 bEgInSlIdE OOP-3 "Implementation Inheritance" vs. Strict Inheritance uType: (the class interface, how it can be used from outside) lExtended by inheritance. Subtyping! lUnrelated / new type uImplementation: method bodies. lExtended, only for the added methods. lChange by overriding uMold: structure, i.e., list of fields. lExtended, to add new fields. uFactory: The interface for creating objects. lNot inherited at all uMill: The implementation of constructors. lExtended

4 bEgInSlIdE OOP-4 A C++ Array Class Here is a simple version of an Array class that implements an array of integers with no bound checking. class Array { public: explicit Array(int len_): len(len_), buff(new int[len]){} ~Array(void) { delete[] buff; } int size(void) const { return len; } int& operator[](int i) { return buff[i]; } int operator[](int i) const { return buff[i]; } private: const int len; int * const buff; Array(const Array &); Array & operator =(const Array &); }; class Array { public: explicit Array(int len_): len(len_), buff(new int[len]){} ~Array(void) { delete[] buff; } int size(void) const { return len; } int& operator[](int i) { return buff[i]; } int operator[](int i) const { return buff[i]; } private: const int len; int * const buff; Array(const Array &); Array & operator =(const Array &); };

5 bEgInSlIdE OOP-5 A Checked Array uWe may also need an array whose bounds are checked in every reference. uCode reuse: define a new derived class CheckedArray that inherits the characteristics of the base class Array : class CheckedArray: private Array { class RangeError{}; public: explicit CheckedArray(int len): Array(len) {} int& operator[](int) throw (RangeError); int operator[](int) const throw (RangeError); using Array::size(); }; int& CheckedArray :: operator[](int i) throw (CheckedArray :: RangeError) { if (0 > i || i >= size()) throw RangeError(); return Array :: operator[] (i); } class CheckedArray: private Array { class RangeError{}; public: explicit CheckedArray(int len): Array(len) {} int& operator[](int) throw (RangeError); int operator[](int) const throw (RangeError); using Array::size(); }; int& CheckedArray :: operator[](int i) throw (CheckedArray :: RangeError) { if (0 > i || i >= size()) throw RangeError(); return Array :: operator[] (i); }

6 bEgInSlIdE OOP-6 An Entirely New Type uWhen a class inherits from a class / struct, an access level definition must be used Class CheckedArray has an anonymous private sub- object of type Array ; the fact that CheckedArray inherits from Array is private. class CheckedArray: private Array {... class CheckedArray: private Array {... Inherited methods cannot be used from outside, but a using clause makes it possible to reuse these. class CheckedArray: private Array { public: using Array::size()... class CheckedArray: private Array { public: using Array::size()...

7 bEgInSlIdE OOP-7 Factory and Mill are Not Inherited! uConstructors are not inherited. If we haven’t defined a constructor for CheckedArray, then the compiler would try to generate an empty default constructor and fail in doing so since there is no default constructor of the base class Array uThe construction (initialization) of the inherited class Array must be done in the header of the constructor of the derived class CheckedArray... explicit CheckedArray(int len): Array(len) {}... explicit CheckedArray(int len): Array(len) {}...

8 bEgInSlIdE OOP-8 Overriding... return Array :: operator[] (i);....... return Array :: operator[] (i);....... if (0 = size())....... if (0 = size())....... int& CheckedArray :: operator[](int i)....... int& CheckedArray :: operator[](int i).... uSince CheckedArray doesn’t override the size function of Array, the following calls the inherited function u CheckedArray overrides the array reference function of Array. uAn overridden function can be called as follows:

9 bEgInSlIdE OOP-9 Non-Strict Inheritance uAKA Inheritance uMore Powerful Abstraction Mechanism: a subclass is just like the superclass, except for an explicit list of changes: lAdditional operations. lAdditional structure elements. lOperations implemented differently, also called overridden operations. uCan be used for lReimplementation lType extension. uNote: there is usually no way to “re-implement” structure elements.


Download ppt "BEgInSlIdE OOP-1 Inheritance for Code Reuse uAn Array Example uImproving the Array Example."

Similar presentations


Ads by Google