+ All Categories
Home > Documents > Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming:...

Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming:...

Date post: 27-Dec-2015
Category:
Upload: harvey-eaton
View: 229 times
Download: 0 times
Share this document with a friend
Popular Tags:
13
Chapter 12 Chapter 12 Object-Oriented Object-Oriented Programming: Inheritance Programming: Inheritance Part I
Transcript
Page 1: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

Chapter 12 Chapter 12 Object-Oriented Object-Oriented Programming: Inheritance Programming: Inheritance Part I

Page 2: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.1 Introduction12.1 IntroductionInheritance

◦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 than the base class

Behaviors inherited from base class Additional behaviors

Page 3: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.1 Introduction12.1 IntroductionClass 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 More details in chapter 24

Page 4: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

Fig. 12.2Fig. 12.2 | Inheritance hierarchy for | Inheritance hierarchy for university university CommunityMemberCommunityMembers. s.

Page 5: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.1 Introduction12.1 Introduction

Focus on commonalities among objects in system

“is-a” vs. “has-a”◦ “is-a”

Inheritance Derived class object can be treated as base class

object Example: Car is a vehicle

Vehicle properties/behaviors also apply to a car

◦ “has-a” Composition Object contains one or more objects of other

classes as members Example: Car has a steering wheel

Page 6: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.1 Introduction12.1 IntroductionThree types of inheritance

◦public◦private◦protected

Page 7: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

Software Engineering Observation Software Engineering Observation 12.112.1

Member functions of a derived class cannot directly access private members of the base class.◦This is for ensuring information

hiding.

Page 8: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.2 Base Classes and Derived 12.2 Base Classes and Derived ClassesClassesBase 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 is the base class Rectangle is the derived class

◦ Base class typically represents larger set of objects than derived classes Example:

Base class: Vehicle Includes cars, trucks, boats, bicycles, etc.

Derived class: Car Smaller, more-specific subset of vehicles

Page 9: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

Fig. 12.3Fig. 12.3 | Inheritance hierarchy for | Inheritance hierarchy for ShapeShapess. .

Page 10: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.2 Base Classes and Derived 12.2 Base Classes and Derived ClassesClassespublic inheritance

◦ Specify with:

Class TwoDimensionalShape inherits from class Shape

◦ Base class private members not accessible directly but still inherited

Manipulated through inherited public member functions

◦ Base class public and protected members Inherited with original member access

◦ friend functions Not inherited

Class TwoDimensionalShape : public Shape{…};

Page 11: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

12.3 12.3 protectedprotected Members Membersprotected access

◦ Intermediate level of protection between public and private

◦ protected members are accessible to Base class members Base class friends Derived class members Derived class friends

Derived-class members◦ Refer to public and protected members of base

class Simply use member names

◦ Redefined base class members can be accessed by using base-class name and binary scope resolution operator (::)

Page 12: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

ExampleExample class Student { friend ostream &operator<< (ostream &out, const Student s); public: Student(string, string); ~Student(); string GetName(); protected: string name, id; private: … }

class GraduateStudent : public Student { public: GraduateStudent(string, string); ~ GraduateStudent(); string GetSupervisor(); protected: string supervisor; private: … }

class UndergraduateStudent : public Student { public: UndergraduateStudent(string, string); ~ UndergraduateStudent(); string GetPreceptor(); protected: string preceptor; private: … }

inherit

inherit

Page 13: Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.

ExampleExample

void main() { UndergraduateStudent stu1(“Joe”, “00001”); string name = stu1.GetName(); cout << stu1 << endl; }


Recommended