Recitation Course 0610 Speaker: Liu Yu-Jiun.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
COP 3003 Object-Oriented Programming - Inheritance Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Slide 1 Inheritance: Part I. Slide 2 Structuring ‘classes’ class Employee { string firstName; string familyName; … } class Manager { Employee e; list.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2009 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
More on Object-Oriented Programming: Inheritance 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Spring CISC6795: Object Oriented Programming II.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
Slide 1 Polymorphism: Part I. Slide 2 Summary: Derived Class * ‘is-a’ relationship n Different from ‘has-a’ or ‘uses-a’, or … by class-in-class * Public.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Inheritance.
Outline Class and Object-Oriented Programing –Encapsulation and inheritance Example –Commission Employee –Encapsulation and inheritance Strings and Formatting.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Object Oriented Programming: Inheritance Chapter 9.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Inheritance: Part I.
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Polymorphism: Part I.
Object-Oriented Programming: Inheritance and Polymorphism
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Chapter 9 Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Classes: A Deeper Look, Part 1
Introduction to Classes and Objects
Presentation transcript:

Recitation Course 0610 Speaker: Liu Yu-Jiun

Chap 12 Inheritance

Introduction 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 Relationship Composition: has a relationship Inheritance: is a relationship

is-a Base class Derived class Direct base class Indirect base class Base and derived class is-a Base class Derived class Direct base class Indirect base class

12.2 base class and derived class public inheritance: Specify with: class derived_class_name : public base_class_name Example: class TwoDimensionalShape : public Shape Base class private members Still inherited, but not accessible directly Base class public and protected members Inherited with original member access friend function Not inherited

12.3 protected Members protected access Derived-class members 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 (::) 衍生類別如果想用基本類別中的public和protected成員,只要直接使用就好。 如果衍生類別重新定義了基本類別的成員(衍生類別有成員和基本類別成員的名稱相同,但是功能不同),只要加上基本類別的名稱和::

12.4 Relationship between Base Classes and Derived Classes Five examples: Class CommissionEmployee Class BasePlusCommissionEmployee (v1) Without inheritance Class BasePlusCommissionEmployee (v2) Inherit from class CommissionEmployee Use private member in base class Class BasePlusCommissionEmployee (v3) Let private members be protected members in base class Class BasePlusCommissionEmployee (v4) Use public member function in base class to access private members in base class

12.4.1 class CommissionEmployee

5 pairs of set and get functions for accessing each data member // Fig. 12.4: CommissionEmployee.h #ifndef COMMISSION_H #define COMMISSION_H #include <string> using namespace std; class CommissionEmployee{ public: CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0); void setFirstName(const string &); string getFirstName() const; void setLastName(const string &); void setSocialSecurityNumber(const string &); void setGrossSales(double); double getFirstName() const; void setCommissionRate(double); double earnings() const; void print() const; private: string firstName; string lastName; string socialSecurityNumber; double grossSales; double commissionRate; }; #endif 5 pairs of set and get functions for accessing each data member

10 Outline Commission Employee.cpp (1 of 4) Initialize data members 10

Outline (2 of 4) Function setGrossSales validates gross sales amount 11 Outline Commission Employee.cpp (2 of 4) Function setGrossSales validates gross sales amount 11

Outline Function setCommissionRate validates commission rate (3 of 4) 12 Outline Function setCommissionRate validates commission rate Commission Employee.cpp (3 of 4) 12

Outline Function earnings calculates earnings (4 of 4) 13 Outline Function earnings calculates earnings Commission Employee.cpp (4 of 4) Function print displays CommissionEmployee object 13

Outline (1 of 2) Instantiate CommissionEmployee object 14 Outline fig12_06.cpp (1 of 2) Instantiate CommissionEmployee object Use CommissionEmployee’s get functions to retrieve the object’s instance variable values 14

15 Outline Use CommissionEmployee’s set functions to change the object’s instance variable values fig12_06.cpp (2 of 2) Call object’s print function to display employee information Call object’s earnings function to calculate earnings 15

Class BasePlusCommissionEmployee 16 12.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance Class BasePlusCommissionEmployee Much of the code is similar to CommissionEmployee private data members public methods constructor Additions private data member baseSalary Methods setBaseSalary and getBaseSalary 16

17 Outline BasePlus Commission Employee.h (1 of 2) Constructor takes one more argument, which specifies the base salary 與類別CommissionEmployee重複 17

Outline Define get and set functions for data member baseSalary 18 Define get and set functions for data member baseSalary Outline BasePlus Commission Employee.h (2 of 2) Add data member baseSalary 18

19 Outline BasePlus Commission Employee.cpp (1 of 4) Constructor takes one more argument, which specifies the base salary Use function setBaseSalary to validate data 19

20 Outline BasePlus Commission Employee.cpp (2 of 4) 20

21 Outline BasePlus Commission Employee.cpp (3 of 4) 21

22 Outline Function setBaseSalary validates data and sets instance variable baseSalary BasePlus Commission Employee.cpp (4 of 4) Function getBaseSalary returns the value of instance variable baseSalary Update function earnings to calculate the earnings of a base-salaried commission employee Update function print to display base salary 22

Outline (1 of 3) Instantiate BasePlusCommissionEmployee object 23 fig12_09.cpp (1 of 3) Instantiate BasePlusCommissionEmployee object 23

24 Outline Use BasePlusCommissionEmployee’s get functions to retrieve the object’s instance variable values fig12_09.cpp (2 of 3) Use BasePlusCommissionEmployee’s setBaseSalary function to set base salary Call object’s print function to display employee information Call object’s earnings function to calculate employee’s earnings 24

25 Outline fig12_09.cpp (3 of 3) 25

12.4.3 Creating a CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Class BasePlusCommissionEmployee Derived from class CommissionEmployee Is a CommissionEmployee Inherits all public members Constructor is not inherited Use base-class initializer syntax to initialize base-class data member Has data member baseSalary 26

Include the base-class header file in the derived-class header file: Know that base class exist. Know size of inherited data members.(併入衍生類別物件的總記憶體需求量) Ensure that inherited class members are used properly. Class BasePlusCommissionEmployee derives publicly from class CommissionEmployee 27

在成員初始值列表初始化成員物件並明確的呼叫基本類別建構子,可以避免重複初始化的問題。 Initialize base class data member by calling the base-class constructor using base-class initializer syntax. 在成員初始值列表初始化成員物件並明確的呼叫基本類別建構子,可以避免重複初始化的問題。 28

Compiler generates errors because base class’s data member commissionRate and grossSales are private Compiler generates errors because the base class’s data members firstName, lastName, socialSecurityNumber, grossSales and commissionRate are private 29

30

12.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data Use protected data Enable class BasePlusCommissionEmployee to directly access base class data members Base class’s protected members are inherited by all derived classes of that base class 31

CommissionEmployee.h相同 // Fig. 12.4: CommissionEmployee.h #ifndef COMMISSION_H #define COMMISSION_H #include <string> using namespace std; class CommissionEmployee{ public: CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0); void setFirstName(const string &); string getFirstName() const; void setLastName(const string &); void setSocialSecurityNumber(const string &); void setGrossSales(double); double getFirstName() const; void setCommissionRate(double); double earnings() const; void print() const; private: string firstName; string lastName; string socialSecurityNumber; double grossSales; double commissionRate; }; #endif protected: 幾乎與12.4.1的 CommissionEmployee.h相同

CommissionEmployee.cpp完全相同 33 Outline Commission Employee.cpp (1 of 3) 與12.4.1的 CommissionEmployee.cpp完全相同 33

CommissionEmployee.cpp完全相同 34 Outline Commission Employee.cpp (2 of 3) 與12.4.1的 CommissionEmployee.cpp完全相同 34

CommissionEmployee.cpp完全相同 35 Outline Commission Employee.cpp (3 of 3) 與12.4.1的 CommissionEmployee.cpp完全相同 35

BasePlusCommissionEmployee.h完全相同 36 Outline BasePlus Commission Employee.h (1 of 1) BasePlusCommissionEmployee still inherits publicly from CommissionEmployee 與12.4.3的 BasePlusCommissionEmployee.h完全相同 36

BasePlusCommissionEmployee.cpp完全相同 37 Outline BasePlus Commission Employee.cpp (1 of 2) Call base-class constructor using base-class initializer syntax 與12.4.3的 BasePlusCommissionEmployee.cpp完全相同 37

BasePlusCommissionEmployee.cpp完全相同 38 Outline BasePlus Commission Employee.cpp (2 of 2) Directly access base class’s protected data 與12.4.3的 BasePlusCommissionEmployee.cpp完全相同 38

39 Outline Fig12_16.cpp (1 of 2) 39

40 Outline Fig12_16.cpp (2 of 2) 與12.4.2的程式功能相同, 但是程式碼變短,也比較容易維護 40

41 12.4.4 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data (Cont.) Advantages Derived class can modify values directly Avoid set/get method call overhead Slight increase in performance Disadvantages No validity checking Derived class can assign illegal value Implementation dependent Derived class functions more likely dependent on base class implementation Base class implementation changes may result in derived class modifications Fragile (brittle) software 41

12.4.5 CommissionEmployee-BasePlusCommissionEmployee Inheritance Hierarchy Using private Data Reexamine hierarchy Use the best software engineering practice Declare data members as private Provide public get and set functions Use get method to obtain values of data members 42

CommissionEmployee.h相同 // Fig. 12.4: CommissionEmployee.h #ifndef COMMISSION_H #define COMMISSION_H #include <string> using namespace std; class CommissionEmployee{ public: CommissionEmployee(const string &, const string &, const string &, double = 0.0, double = 0.0); void setFirstName(const string &); string getFirstName() const; void setLastName(const string &); void setSocialSecurityNumber(const string &); void setGrossSales(double); double getFirstName() const; void setCommissionRate(double); double earnings() const; void print() const; private: string firstName; string lastName; string socialSecurityNumber; double grossSales; double commissionRate; }; #endif 與12.4.1的 CommissionEmployee.h相同

44 Outline Commission Employee.cpp (1 of 3) Use member initializers to set the values of members firstName, lastname and socialSecurityNumber 44

45 Outline Commission Employee.cpp (2 of 3) 45

46 Outline Commission Employee.cpp (3 of 3) Use get functions to obtain the values of data members 46

12.5 Constructors and Destructors in Derived Classes Instantiating derived-class object Chain of constructor calls Derived-class constructor invokes base class constructor Implicitly or explicitly Base of inheritance hierarchy Last constructor called in chain First constructor body to finish executing Example: CommissionEmployee/BasePlusCommissionEmployee hierarchy CommissionEmployee constructor called last CommissionEmployee constructor body finishes execution first Initializing data members Each base-class constructor initializes its data members that are inherited by derived class 47

12.5 Constructors and Destructors in 12.4.3~4 類別BasePlusCommissionEmployee constructor的主體(使用域、scope) 48

12.5 Constructors and Destructors in Derived Classes (Cont.) Destroying derived-class object Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next Continue up hierarchy until final base reached After final base-class destructor, object removed from memory Base-class constructors, destructors, assignment operators Not inherited by derived classes 49

12.6 public, protected and private Inheritance 50 12.6 public, protected and private Inheritance public inheritance Base class public members  derived class public members Base class protected members  derived class protected members Base class private members are not accessible protected inheritance (not is-a relationship) Base class public and protected members  derived class protected members private inheritance (not is-a relationship) Base class public and protected members  derived class private members 50

51

Exercise 12.9 Package + 12 pairs set and get functions senderName:string -recipientName:string senderAddress:string -recipientAddress:string senderCity:string -recipientCity:string senderState:string -recipientState:string senderZip:int -recipientZip:int weight:double -costPerOunce:double + 12 pairs set and get functions + calculateCost() : double TwoDayPackage OvernightPackage - flatFee : double - overnightFeePerOunce : double + setFlatFee(double) + getFlatFee(): double + calculateCost() : double + setOvernightFeePerOunce(double) + getOvernightFeePerOunce(): double + calculateCost(): double