Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I."— Presentation transcript:

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

2 12.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 than the base class  Behaviors inherited from base class  Additional behaviors

3 12.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  More details in chapter 24

4 Fig. 12.2 | Inheritance hierarchy for university CommunityMember s.

5 12.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

6 12.1 Introduction Three types of inheritance ◦ public ◦ private ◦ protected

7 Software Engineering Observation 12.1 Member functions of a derived class cannot directly access private members of the base class. ◦ This is for ensuring information hiding.

8 12.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 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

9 Fig. 12.3 | Inheritance hierarchy for Shapes.

10 12.2 Base Classes and Derived Classes public 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 { … };

11 12.3 protected Members protected access ◦ Intermediate level of protection between public and private ◦ protected members are accessible to  Base class members  Base class friend s  Derived class members  Derived class friend s 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 ( :: )

12 Example 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

13 Example void main() { UndergraduateStudent stu1(“Joe”, “00001”); string name = stu1.GetName(); cout << stu1 << endl; } Undergraduate 繼承自 Student ,因此承襲其 public 與 protected 成員變數與成 員函式 試說明此行的錯誤。


Download ppt "Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I."

Similar presentations


Ads by Google