Recitation Course 0617 Speaker Liu YuJiun Chap 13

  • Slides: 42
Download presentation
Recitation Course 0617 Speaker: Liu Yu-Jiun

Recitation Course 0617 Speaker: Liu Yu-Jiun

Chap 13 Polymorphism

Chap 13 Polymorphism

Commission. Employee. h 有設定初始� ,所以是 default建構子 Function earnings will be redefined in derived classes

Commission. Employee. h 有設定初始� ,所以是 default建構子 Function earnings will be redefined in derived classes to calculate the employee’s earnings Function print will be redefined in derived class to print the employee’s information

Outline Commission Employee. cpp (2 of 3) 9

Outline Commission Employee. cpp (2 of 3) 9

Outline Commission Employee. cpp (3 of 3) Calculate earnings based on commission rate and

Outline Commission Employee. cpp (3 of 3) Calculate earnings based on commission rate and gross sales Display name, social security number, gross sales and commission rate 10

Outline Base. Plus Commission Employee. h (1 of 1) Redefine functions earnings and print

Outline Base. Plus Commission Employee. h (1 of 1) Redefine functions earnings and print 11

Outline Base. Plus Commission Employee. cpp (1 of 2) Base. Plus. Commission. Employee類 別內有把這些資料成員繼承過

Outline Base. Plus Commission Employee. cpp (1 of 2) Base. Plus. Commission. Employee類 別內有把這些資料成員繼承過 來,只是隱藏了(private)。明確 呼叫Commission. Employee建構 子來設定初始� 。 12

Outline Base. Plus Commission Redefined earnings Employee. cpp function incorporates base salary (2 of

Outline Base. Plus Commission Redefined earnings Employee. cpp function incorporates base salary (2 of 2) Redefined print function displays additional Base. Plus. Commission. Employee details 84 // print Commission. Employee object 85 void Commission. Employee: : print() const 86 { 87 cout << "commission employee: " 88 << get. First. Name() << ' ' << get. Last. Name() 89 << "nsocial security number: " << get. Social. Security. Number() 90 << "ngross sales: " << get. Gross. Sales() 91 << "ncommission rate: " << get. Commission. Rate(); 92 } // end function print 13

Outline fig 13_05. cpp (1 of 4) 14

Outline fig 13_05. cpp (1 of 4) 14

Outline fig 13_05. cpp (2 of 4) Aiming base-class pointer at base-class object and

Outline fig 13_05. cpp (2 of 4) Aiming base-class pointer at base-class object and invoking base-class functionality 15

Outline fig 13_05. cpp (3 of 4) Aiming derived-class pointer at derived-class object and

Outline fig 13_05. cpp (3 of 4) Aiming derived-class pointer at derived-class object and invoking derived-class functionality Aiming base-class pointer at derived-class object and invoking base-class functionality 16

Outline fig 13_05. cpp (4 of 4) 17

Outline fig 13_05. cpp (4 of 4) 17

18 13. 3. 2 Aiming Derived-Class Pointers at Base-Class Objects • Aim a derived-class

18 13. 3. 2 Aiming Derived-Class Pointers at Base-Class Objects • Aim a derived-class pointer at a base-class object – C++ compiler generates error • Commission. Employee (base-class object) is not a Base. Plus. Commission. Employee (derived-class object) – If this were to be allowed, programmer could then attempt to access derived-class members which do not exist • Could modify memory being used for other data – Example: 底薪加抽成員 「是一種」抽成員

Cannot assign base-class object to derived-class pointer because is-a relationship does not apply

Cannot assign base-class object to derived-class pointer because is-a relationship does not apply

13. 3. 3 Derived-Class Member-Function Calls via Base-Class Pointers • Aiming base-class pointer at

13. 3. 3 Derived-Class Member-Function Calls via Base-Class Pointers • Aiming base-class pointer at derived-class object – Calling functions that exist in base class causes base-class functionality to be invoked – Calling functions that do not exist in base class (may exist in derived class) will result in error • Derived-class members cannot be accessed from baseclass pointers • However, this can be accomplished using downcasting (Section 13. 8)

Cannot invoke derived-class-only members from base-class pointer

Cannot invoke derived-class-only members from base-class pointer

Outline Commission Employee. h (1 of 2) 23

Outline Commission Employee. h (1 of 2) 23

Outline Commission Employee. h Declaring earnings and print as virtual allows them to be

Outline Commission Employee. h Declaring earnings and print as virtual allows them to be overridden, not redefined (2 of 2) 24

Outline 25 Base. Plus Commission Employee. h (1 of 1) Functions earnings and print

Outline 25 Base. Plus Commission Employee. h (1 of 1) Functions earnings and print are already virtual – good practice to declare virtual even when overriding function

Outline fig 13_10. cpp (1 of 5) 26

Outline fig 13_10. cpp (1 of 5) 26

Outline fig 13_10. cpp (2 of 5) Aiming base-class pointer at base-class object and

Outline fig 13_10. cpp (2 of 5) Aiming base-class pointer at base-class object and invoking base-class functionality 27

Outline fig 13_10. cpp (3 of 5) Aiming derived-class pointer at derived-class object and

Outline fig 13_10. cpp (3 of 5) Aiming derived-class pointer at derived-class object and invoking derived-class functionality Aiming base-class pointer at derived-class object and invoking derived-class functionality via polymorphism and virtual functions 28

Outline fig 13_10. cpp (4 of 5) 29

Outline fig 13_10. cpp (4 of 5) 29

Outline fig 13_10. cpp (5 of 5) 30

Outline fig 13_10. cpp (5 of 5) 30

13. 5 Abstract Classes and Pure virtual Functions • 抽象類別(Abstract Classes) – 不會產生物件的類別 –

13. 5 Abstract Classes and Pure virtual Functions • 抽象類別(Abstract Classes) – 不會產生物件的類別 – 通常用作繼承階層中的基本類別(抽象基本 類別—abstract base classes) – 不完整,衍生類別需要各自補足 Shape 2 DShape Square Circle 3 DShape Triangle Cube Sphere Cylinder

Chap 15 & 17

Chap 15 & 17

Read and Write Data from File • First, include the header file <fstream>. •

Read and Write Data from File • First, include the header file <fstream>. • How to read data from a file? – ifstream in("sample_input_4. txt"); – in >> a; in >> b; in >> c;

How to write data to a file? • Creating an ofstream object – Opens

How to write data to a file? • Creating an ofstream object – Opens a file for output – Constructor takes two arguments • A filename – If the file doe not exist, it is first created • A file-open mode – ios: : out – the default mode » Overwrites preexisting data in the file – ios: : app » Appends data to the end of the file • ofstream out. Client. File( “clients. dat”, ios: : out); – Can also use member function open on existing object • Takes same arguments as the constructor • ofstream out. Client. File; • out. Client. File. open(“clients. dat”, ios: : out);

Chap 8 Pointer-Based Strings

Chap 8 Pointer-Based Strings

Pass the variables • Call by value • Call by reference • Call by

Pass the variables • Call by value • Call by reference • Call by pointer – 1 2 3 4 pointer non-constant data non-constant

Chap 9 & 10 A Deeper Look

Chap 9 & 10 A Deeper Look

Chap 12 Inheritance

Chap 12 Inheritance