覆載函式 (function overloading)

48
覆覆覆覆 (function overloadin g) double maxdouble(double array[], int len) { double max = array[0] for(int i=1; i<len; i++) if (max < array[i]) max = array[i]; retrun max; } how about long, int, …etc.

Upload: deanna-hancock

Post on 30-Dec-2015

72 views

Category:

Documents


0 download

DESCRIPTION

覆載函式 (function overloading). double maxdouble(double array[], int len) { double max = array[0] for(int i=1; i

TRANSCRIPT

Page 1: 覆載函式 (function overloading)

覆載函式 (function overloading)double maxdouble(double array[], int len){ double max = array[0]

for(int i=1; i<len; i++)if (max < array[i]) max = array[i];

retrun max;}

how about long, int, …etc.

Page 2: 覆載函式 (function overloading)

覆載函式 (cont.)

相同名稱的一系列函式 各有不同的參數列 注意 ! 僅有傳回值不同 , 並不足以區分函式 !!

Page 3: 覆載函式 (function overloading)

Example Ex6_06.cppint max(int array[], int len); // Prototypes forlong max(long array[], int len); // a set of overloadeddouble max(double array[], int len); // functions

Page 4: 覆載函式 (function overloading)

覆載函式的意義 相同運算式使用相同函式名稱處理不同運算元

Ex: “+” 對 int, long, double, … 實質上相同 , 但引數型態不同 , 都應覆載 , 使

用相同函式名稱

Page 5: 覆載函式 (function overloading)

函式樣板 (Function Templates)

函式具有不同變數及參數型態 程式碼相同 一群特定程式碼 依照參數型態不同自動產生不同的版本

Page 6: 覆載函式 (function overloading)

使用函式樣板改寫 max()

關鍵字 template 表示樣板定義 < 參數型態 > 由樣板建立的函式實體稱為實例 (instantiation)

// Template for function to compute the maximum element of an arraytemplate<class T> T max(T x[], int len){ T max = x[0]; for(int i=1; i<len; i++) if(max<x[i]) max = x[i]; return max;}

Page 7: 覆載函式 (function overloading)

Example Ex6_07.cpp#include <iostream>using namespace std;

// Template for function to compute the maximum element of an arraytemplate<class T> T max(T x[], int len){ T max = x[0]; for(int i=1; i<len; i++) if(max<x[i]) max = x[i]; return max;}

int main(void){ int small[] = { 1,24,34,22}; long medium[] = { 23,245,123,1,234,2345}; double large[] = { 23.0,1.4,2.456,345.5,12.0, 21.0}; int lensmall = sizeof small/sizeof small[0]; int lenmedium = sizeof medium/sizeof medium[0]; int lenlarge = sizeof large/sizeof large[0];

cout << endl << max(small, lensmall); cout << endl << max(medium, lenmedium); cout << endl << max(large, lenlarge);

cout << endl; return 0;}

Page 8: 覆載函式 (function overloading)

Homework

reading example p224~239 6-1, 6-2

Page 9: 覆載函式 (function overloading)

Chapter 7

舊式 Windows 的滋味

Page 10: 覆載函式 (function overloading)

目標 結構 視窗基本架構 Windows API Windows message Windows 程式常用表示法 Windows 程式基本架構

Page 11: 覆載函式 (function overloading)

何謂結構 新的變數型態 使用關鍵字 struct

struct 結構名稱{

… // 結構成員};

Page 12: 覆載函式 (function overloading)

Example

新的變數型態 ---BOOK

struc BOOK{ char Title[80]; char Author[80]; char Publisher[80]; int Year;};

BOOK Novel;

Page 13: 覆載函式 (function overloading)

結構初始化 初始值放在大括號中 以逗點區隔

BOOK Novel={ “Paneless Programming”, “I.C. Fingers”, “Gutter Press”, 1981};

Page 14: 覆載函式 (function overloading)

指定結構的成員 成員選擇運算子 (member selection operatio

n) 或成員存取運算子 (member access operator)

一個點 Ex: Novel.Year+=2;

Page 15: 覆載函式 (function overloading)

Ex7_01.cpp

結構的定義通常放在 “ .h” 檔中 使用 #include 假指令載入

Page 16: 覆載函式 (function overloading)

鏈結串列 (Linked List)

Figure (p251)

Page 17: 覆載函式 (function overloading)

配合結構使用指標 結構成員不允許是結構本身的型態 可以是指向此結構型態的指標

struct ListElement{ RECT aRect; ListElement* pNextElement;};

Page 18: 覆載函式 (function overloading)

間接成員選擇運算子 “->” (*pRect).Top += 10; pRect->Top += 10;

Page 19: 覆載函式 (function overloading)

Windows 程式設計 p252 ~ p275 WinMain() and WindowProc() WinMain()- 程式初始化 , 程式執行起始點 WindowProc()- 處理應用程式的訊息

Page 20: 覆載函式 (function overloading)

Chapter 8

運用類別建構自定資料

Page 21: 覆載函式 (function overloading)

目標 何謂類別 宣告類別 建立使用類別的物件 類別成員存取控制權 內定建構子 參照類別內容 何謂拷貝建構子?如何製作

Page 22: 覆載函式 (function overloading)

類別 = 結構 ?

關鍵字 class, public 類別成員 : 負責儲存資料的變數

定義新的資料型態

Page 23: 覆載函式 (function overloading)

Public 的意義 與結構不同處 表示類別成員存取權為共用 ( 結構亦同 ) 類別成員存取權也可以被限制

Page 24: 覆載函式 (function overloading)

命名習慣 -MFC

類別名稱前加 C 類別資料成員前加 m_

Page 25: 覆載函式 (function overloading)

類別定義

class 類別名稱{

public:

… // 類別成員};

Page 26: 覆載函式 (function overloading)

Example

CBox bigBox;

class CBox{ public:

double m_Length;double m_Breath;double m_Height;

}

Cbox 為類別的物件或實體

Page 27: 覆載函式 (function overloading)

類別中的運算式 類別成員

資料 函式 : 成員函式 (member function)

Ex:

Cbox box1;Cbox box2;

if (box1>box2)box1.fill();

elsebox2.fill();

類別物件運用在 C++標準運算式中

Page 28: 覆載函式 (function overloading)

了解類別 類別成員

資料成員 函式成員

定義類別並非真的定義資料 必須透過宣告物件

Page 29: 覆載函式 (function overloading)

類別存取控制權 public, private and protected 類別成員存取屬性 : 省略 - 內定為 private 結構 : 內定為 public

Page 30: 覆載函式 (function overloading)

Ex8_01.cpp

Page 31: 覆載函式 (function overloading)

Ex8_02.cpp

成員函式中使用資料成員 , 不必指定物件名稱 , 因為同屬一個物件

為指定物件名稱時 , 會存取目前正在執行的函式物件的資料成員

每次宣告都會將類別函式 copy 一份 且類別函式在使用 sizeof 運算子時 , 不列入計

Page 32: 覆載函式 (function overloading)

定義成員函式的位置 成員函式不一定要放在類別定義中 放在其他地方 , 在類別定義加入函式原型即可

class Cbox{ public:

double m_length;double m_Breadth;double m_Height;double Volume(void);

}

Page 33: 覆載函式 (function overloading)

定義成員函式的位置 (cont.)

函式名稱前加上範圍解析運算子 - 兩個冒號“ ::”

Double Cbox::Volume(){ return m_Length*m_Breadth*m_Height;}

Page 34: 覆載函式 (function overloading)

定義成員函式的位置 (cont.)

行內涵式 (inline function)-- 成員函式放在類別定義中

編譯器會將行內涵式展開 行內涵式適合程式碼較短且簡單的程式 執行速度較快 強制編譯器將函式當作行內涵式處理

將函式標題前加入關鍵字 inline

Page 35: 覆載函式 (function overloading)

Example

inline double Cbox::Volume(){

return m_Length*m_Breadth*m_Height;}

Page 36: 覆載函式 (function overloading)

類別建構子 (constructor)

類別中特別的函式 名稱與類別相同 每次宣告物件都會呼叫建構子 Cbox()

Page 37: 覆載函式 (function overloading)

類別建構子 (cont.)

Ex8_03.cpp

Cbox(double lv, double bv, double hv){ cout << endl << “Constructor called.”; m_Length = lv; m_Breadth = bv; m_Height = hv;}

CBox box1(78.0, 24.0, 18.0 )CBox cigarBox(8.0, 5.0, 1.0 )

Page 38: 覆載函式 (function overloading)

內定的建構子

在 Ex8_03.cpp 加入 CBox box2 ()

產生錯誤 - 編譯器找不到 box2 的內定建構子 建構子引數

沒有引數 : 定義時無指定 引數可有可無 : 未設定成資料的初始值

Ex8_03.cpp 類別定義已提供建構子 , 編譯器認為使用找要自行負責建構子工作 , 而不提供內定建構子

Page 39: 覆載函式 (function overloading)

最簡化的建構子

Ex8_04.cpp 建構子可覆載

兩個建構子的差別只有參數列不同 一個為三個 double 的引數 一個沒有參數

Cbox(){}

Page 40: 覆載函式 (function overloading)

在建構子中指定內定值 Ex8_05.cpp

Page 41: 覆載函式 (function overloading)

在建構子中使用初始化串列 宣告時以函式表示法來指定初始值

Cbox(double lv =1.0, double bv = 1.0, double hv = 1.0):m_Length(lv), m_Breadth(bv), m_Height(hv)

{cout << endl << “Constructor called.”;

}

Page 42: 覆載函式 (function overloading)

類別的私有成員 關鍵字 private 凡 private 類的成員只允許該類別的成員函式

存取 一般函式無法存取 存取 private 資料成員唯一的方式

建構子 成員函式

Page 43: 覆載函式 (function overloading)

Ex8_06.cpp

Page 44: 覆載函式 (function overloading)

存取 private 類別成員 保護 < > 保密 撰寫一成員函式讀取 private 成員的值

inline double Cbox::GetLength(){

return m_Length;}

len=box2.GetLength();

Page 45: 覆載函式 (function overloading)

類別的夥伴函式 (friend function)

夥伴函式 : 有特權的全域函式 非成員函式 擁有特權存取“類別所有成員”的權利

關鍵字 friend 類別定義中加入夥伴函式的原型 或夥伴函式的整個函式定義 行內涵式

Page 46: 覆載函式 (function overloading)

Ex8_07.cpp

夥伴函式位於 public 及 private 所有類別成員後

並非類別成員 , 不能直接使用資料成員名稱 , 需加上物件名稱

Ex: aBox.m_Length

Page 47: 覆載函式 (function overloading)

夥伴函式使用注意事項 全域函式避免將夥伴函式定義放在類別中

ClassView 中看不到

Page 48: 覆載函式 (function overloading)

內定拷貝建構子