Chapter9-Object-OrientedProgrammingInheritance详解.ppt
文本预览下载声明
Chapter 9 - Object-Oriented Programming: Inheritance 9.1 Introduction Inheritance 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 Derived class More specialized group of objects Behaviors inherited from base class Can customize Additional behaviors 9.1 Introduction Class hierarchy Direct base class Inherited explicitly (one level up hierarchy) Indirect base class Inherited two or more levels up hierarchy Single inheritance Inherits from one base class Multiple inheritance Inherits from multiple base classes Base classes possibly unrelated Chapter 22 9.1 Introduction Three types of inheritance public Every object of derived class also object of base class Base-class objects not objects of derived classes Example: All cars vehicles, but not all vehicles cars Can access non-private members of base class Derived class can effect change to private base-class members Through inherited non-private member functions private Alternative to composition Chapter 17 protected Rarely used 9.1 Introduction Abstraction Focus on commonalities among objects in system “is-a” vs. “has-a” “is-a” Inheritance Derived class object treated as base class object Example: Car is a vehicle Vehicle properties/behaviors also car properties/behaviors “has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel 9.2 Base Classes and Derived Classes Base classes and derived classes Object of one class “is an” object of another class Example: Rectangle is quadrilateral. Class Rectangle inherits from class Quadrilateral Quadrilateral: base class Rectangle: derived class Base class typically represents larger set of objects than derived classes Example: Base class: Vehicle Cars, trucks, boats, bicycles, … Derived class: Car Smaller, more-specific subset of vehicles 9.2 Base Classes and Derived Classes Inher
显示全部