Thông tin tài liệu
2003 Prentice Hall, Inc. All rights reserved.
1
Chapter 9 - Object-Oriented
Programming: Inheritance
Outline
9.1 Introduction
9.2 Base Classes and Derived Classes
9.3 protected Members
9.4 Relationship between Base Classes and Derived Classes
9.5 Case Study: Three-Level Inheritance Hierarchy
9.6 Constructors and Destructors in Derived Classes
9.7 “Uses A” and “Knows A” Relationships
9.8 public, protected and private Inheritance
9.9 Software Engineering with Inheritance
2003 Prentice Hall, Inc. All rights reserved.
2
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
2003 Prentice Hall, Inc. All rights reserved.
3
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
2003 Prentice Hall, Inc. All rights reserved.
4
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
2003 Prentice Hall, Inc. All rights reserved.
5
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
2003 Prentice Hall, Inc. All rights reserved.
6
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
2003 Prentice Hall, Inc. All rights reserved.
7
9.2 Base Classes and Derived Classes
•
Inheritance examples
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
2003 Prentice Hall, Inc. All rights reserved.
8
9.2 Base Classes and Derived Classes
•
Inheritance hierarchy
–
Inheritance relationships: tree-like hierarchy structure
–
Each class becomes
•
Base class
–
Supply data/behaviors to other classes
OR
•
Derived class
–
Inherit data/behaviors from other classes
2003 Prentice Hall, Inc. All rights reserved.
9
Single
inheritance
CommunityMember
Employee Student
Administrator Teacher
AdministratorTeacher
StaffFaculty
Alumnus
Single
inheritance
Single
inheritance
Multiple
inheritance
Fig. 9.2 Inheritance hierarchy for university CommunityMembers.
2003 Prentice Hall, Inc. All rights reserved.
10
Shape
TwoDimensionalShape ThreeDimensionalShape
Circle Square Triangle Sphere Cube Tetrahedron
Fig. 9.3 Inheritance hierarchy for Shapes.
[...]... • Base class and derived class relationship – Example: Point/circle inheritance hierarchy • Point – x-y coordinate pair • Circle – x-y coordinate pair – Radius © 2003 Prentice Hall, Inc All rights reserved 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 9. 4: point.h // Point class definition represents an x-y coordinate pair #ifndef POINT_H #define POINT_H Outline point.h (1... private: int x; int y; // x part of coordinate pair // y part of coordinate pair Maintain x- and ycoordinates as private data members }; // end class Point #endif © 2003 Prentice Hall, Inc All rights reserved 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig 9. 5: point.cpp // Point class member-function definitions #include point.cpp (1 of 3) using std::cout; #include "point.h"... is 115 The new location of point is [10, 10] pointtest.cpp (2 of 2) pointtest.cpp output (1 of 1) © 2003 Prentice Hall, Inc All rights reserved 19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Fig 9. 7: circle.h // Circle class contains x-y coordinate pair and radius #ifndef CIRCLE_H #define CIRCLE_H Outline circle.h (1 of 2) class Circle { public: // default constructor Circle(... reserved 26 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 circle.setX( 2 ); circle.setY( 2 ); circle.setRadius( 4.25 ); // set new x-coordinate // set new y-coordinate // set new radius Use set functions to modify // display new point value cout . Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9. 1 Introduction 9. 2 Base Classes and Derived Classes 9. 3 protected Members 9. 4 Relationship between Base. Classes 9. 5 Case Study: Three-Level Inheritance Hierarchy 9. 6 Constructors and Destructors in Derived Classes 9. 7 “Uses A” and “Knows A” Relationships 9. 8 public, protected and private Inheritance 9. 9. reserved. 9 Single inheritance CommunityMember Employee Student Administrator Teacher AdministratorTeacher StaffFaculty Alumnus Single inheritance Single inheritance Multiple inheritance Fig. 9. 2 Inheritance
Ngày đăng: 02/04/2014, 06:20
Xem thêm: Chapter 9 - Object-Oriented Programming Inheritance docx, Chapter 9 - Object-Oriented Programming Inheritance docx