L07_InPoly - Inheritance and Polymorphism Nyhoff 14 Deitel...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Inheritance and Polymorphism Nyhoff: 14 Deitel: 12-13,25 COMP152 Spring 2010 D.Y. Yeung Outline Inheritance and Object-Oriented Design Types of Inheritance Building Derived Classes Order of Construction and Destruction Multiple Inheritance Polymorphism Virtual Functions Abstract Class COMP152 2 Encapsulation Languages such as Pascal and C facilitated the development of structured programs Need for ability to extend and reuse software became evident This leads to object-oriented programming Data and basic operations for processing the data are encapsulated into a single “entity”. This is made possible with introduction of Modules Libraries Packages Implementation details separated from definition Client code must use only public operations Implementation may be changed without affecting client code COMP152 3 Inheritance Concepts Some basic class features may be re-used in other classes A class can be derived from another class New class inherits data and function members from the original class Reusable for the new class Example: Consider the need to add, for example, max() and min() functions to a stack Could simply add these functions to the class But … alters already proven code It is better to build “on top” of the proven stack The new class is inherited from the stack class The new class is derived from the stack class COMP152 4 Inheritance Features and Advantages Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Derived class inherits from base class More specialized group of objects Behaviors inherited from base class Can customize Additional behaviors COMP152 Inheritance Features and Advantages Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Derived class inherits from base class More specialized group of objects Behaviors inherited from base class Can customize Additional behaviors COMP152 5 Inheritance Example (Some Examples with Arrows Reversed) COMP152 6 Another Inheritance Example COMP152 7 Inheritance for Stack Adapter approach Build a new class RevStack Contains Stack object as its member But … Strictly speaking or conceptually, we cannot say a RevStack is a Stack It only contains a Stack To access the functions of Stack, we need to call: RevStk.Stk.push(); we’d like to simply call RevStk.push() We cannot modify or overide the original push, pop or other member operations of Stack COMP152 8 max(), min() and other new operations including revised max() and min() Stack myStack push(), pop()… myTop, … new data members … RevStack class Inheritance for Stack To modify the member functions, we may use Copy-and-paste approach Build a...
View Full Document

This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

Page1 / 99

L07_InPoly - Inheritance and Polymorphism Nyhoff 14 Deitel...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online