Presentation is loading. Please wait.

Presentation is loading. Please wait.

第四章 小技巧.

Similar presentations


Presentation on theme: "第四章 小技巧."— Presentation transcript:

1 第四章 小技巧

2  4.3 重載 例: 算乘積的函數 算兩個相乘 : 算三個相乘 : int Multiple2( int A, int B ) {
return A * B; } 算三個相乘 : int Multiple3( int A, int B, int C ) { return A * B * C; }

3  4.3 重載(續) Compiler會自動選取適當的函數 相同函數名稱 // 函數的重載
// 函數的重載 // 你可以看到有兩個都叫Multiple的函數 #include <iostream.h> int Multiple( int A, int B ) { return A * B; } int Multiple( int A, int B, int C ) return A * B * C; // 主程式 void main() cout << Multiple( 3, 3 ) << endl; cout << Multiple( 4, 2, 5 ) << endl; 相同函數名稱

4  4.3 重載(續) 重載函數要有明確的區分 ? #include <iostream.h>
void Function ( double aDouble) { cout<<“double= “<<aDouble<<endl; } void Function ( float aFloat) cout<<“float= “<<aFloat<<endl; void main() Function(48); ?

5 4.4 預設參數值 例: Ex : Multiple ( 5, 3, 2) ; Multiple ( 5, 3) ;

6  4.4 預設參數值(續) 例: viod Draw(int x, int y, int Radius, int Color);

7 4.4 預設參數值(續) 例:

8 4.4 預設參數值(續) 預設值必須擺最後 不合法的宣告

9 4.4 預設參數值(續) 注意 : Show (5) ? 依編譯器而定

10 第五章 定義自己的物件

11 5.1 物件導向 需要那些功能? 程序導向 需要那些物件? 物件導向

12 5.1 物件導向(續) 例:自動提款機系統 程序導向 

13 5.1 物件導向(續) 例:自動提款機系統 物件導向 

14 5.1 物件導向(續) 軟體IC Visual C  MFC Borland C++  OWL

15  5.2 物件 定義物件 : 存取權限 可被任意物件存取 只可被成員函數存取 // 自己定義的物件 class Employee {
// 自己定義的物件 class Employee { public : char Sex; // 性別 long ID; // 員工編號 private : long Salary; // 薪水 }; 只可被成員函數存取

16  5.2 物件(續) 應用實例 : 本行會有問題:由main()存取Employee 結構運算子:存取物件成員 // 如何定義物件的函數
// 如何定義物件的函數 #include <iostream.h> // 自己定義的物件 class Employee { public : char Sex; // 性別 long ID; // 員工編號 private : long Salary; // 薪水 }; // 主程式 void main() Employee Peter; Peter.Sex = 'M'; Peter.ID = ; Peter.Salary=30000; } 本行會有問題:由main()存取Employee 結構運算子:存取物件成員

17  5.2 物件(續) 定義成員函數(一) // 如何定義物件的函數 #include <iostream.h>
// 如何定義物件的函數 #include <iostream.h> // 自己定義的物件 class Employee { public : char Sex; // 性別 long ID; // 員工編號 void SetSalary( long newSalary ); long GetSalary(); private : long Salary; // 薪水 }; // SetSalary( newSalary ) // 設定薪水的值 // 參數:long newSalary:欲設定的薪水 // 傳回值:無

18 5.2 物件(續) 指定函數的作用範圍 void Employee::SetSalary( long newSalary ) {
Salary = newSalary; } // GetSalary() // 傳回薪水的值 // 參數:無 // 傳回值:long:薪水的值 long Employee::GetSalary() return Salary; // 主程式 void main() Employee Peter; Peter.Sex = 'M'; Peter.ID = ; Peter.SetSalary( ); cout << Peter.GetSalary() << endl; 指定函數的作用範圍

19  5.2 物件(續) 定義成員函數 (二) class Employee { public : char Sex; // 性別
long ID; // 員工編號 void SetSalary( long newSalary ); Salary = newSalary; } long GetSalary(); return Salary; private : long Salary; // 薪水 };

20  5.3 實例 向量函數 class Vector { private : double x; // X 座標
double y; // Y 座標 }

21 5.3 實例(續) 向量函數 向量的乘法

22  5.3 實例(續) 向量函數 // 自己定義的物件 #include <iostream.h> // 定義一個向量
// 自己定義的物件 #include <iostream.h> // 定義一個向量 class Vector { private : double x; // X 座標 double y; // Y 座標 public : // Set( x1, y1) // 設定向量初始值 void Set( double x1, double y1 ) x = x1; y = y1; }

23 5.3 實例(續) // Show() // 顯示向量 // 參數:無 // 傳回值:無 void Show() {
// 顯示向量 // 參數:無 // 傳回值:無 void Show() { cout << "[" << x << "," << y << "]\n"; } // Add( x1, y1 ) // 加另一個向量 // 參數: double x1:另一個向量的x值 // double y1:另一個向量的y值 void Add( double x1, double y1 ) x += x1; y += y1; // Add( Vector AnotherVec ) // 參數:Vector AnotherVec:另一個向量

24 5.3 實例(續) // 傳回值:無 void Add( Vector AnotherVec ) // 主程式 void main() {
// 傳回值:無 void Add( Vector AnotherVec ) { x += AnotherVec.x; y += AnotherVec.y; } // Multiple( Constant ) // 乘一個常數 // 參數:double Constant:欲乘的常數 void Multiple( double Constant ) x *= Constant; y *= Constant; }; // 主程式 void main() { Vector Vec1( 3, 5 ); Vector Vec2( 1, 1 ); Vec1.Set (3, 5); Vec2.Set (1,1); Vec1.Add( 2, 2 ); Vec2.Multiple( 5 ); Vec1.Show(); Vec2.Show(); }

25  5.3 實例(續) 向量相加: 重載函數 Void Vector::AddVector(Vector AnotherVec) {
x+=AnotherVec.x; y+=AnotherVec.y; } Void Vector::Add(Vector AnotherVec) { x+=AnotherVec.x; y+=AnotherVec.y; }

26   5.3 建構與解構元(續) 建構元 : 與物件同名的函數 解構元 : 函數被破壞時呼叫
Vector::Vector( double x1 = 0, double y1 = 0 ) { // 函數被呼叫時要做的工作 x = x1; y = y1; } 解構元 : 函數被破壞時呼叫 Vector::~Vector() { //函數被破壞時要做的工作; }

27  5.3 建構與解構元(續) 向量函數 // 自己定義的物件 #include <iostream.h> // 定義一個向量
// 自己定義的物件 #include <iostream.h> // 定義一個向量 class Vector { private : double x; // X 座標 double y; // Y 座標 public : // 建構元 // 參數:double x1:另一個向量的x值 //  double y1:另一個向量的y值 Vector( double x1 = 0, double y1 = 0 ) x = x1; y = y1; }

28 5.3 建構與解構元(續) // Show() // 顯示向量 // 參數:無 // 傳回值:無 void Show() {
// 顯示向量 // 參數:無 // 傳回值:無 void Show() { cout << "[" << x << "," << y << "]\n"; } // Add( x1, y1 ) // 加另一個向量 // 參數: double x1:另一個向量的x值 // double y1:另一個向量的y值 void Add( double x1, double y1 ) x += x1; y += y1; // Add( Vector AnotherVec ) // 參數:Vector AnotherVec:另一個向量

29 5.3 建構與解構元(續) // 傳回值:無 void Add( Vector AnotherVec ) // 主程式
// 傳回值:無 void Add( Vector AnotherVec ) { x += AnotherVec.x; y += AnotherVec.y; } // Multiple( Constant ) // 乘一個常數 // 參數:double Constant:欲乘的常數 void Multiple( double Constant ) x *= Constant; y *= Constant; }; // 主程式 void main() { Vector Vec1( 3, 5 ); Vector Vec2( 1, 1 ); Vec1.Add( 2, 2 ); Vec1.Add( Vec2 ); Vec2.Multiple( 3 ); Vec1.Show(); Vec2.Show(); }

30 5.4 挑戰 向量內積 內積(dot)=a*b+c*d 向量的乘法

31 5.4 挑戰(續) 以物件導向的觀念 加入向量內積(dot)的功能


Download ppt "第四章 小技巧."

Similar presentations


Ads by Google