Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ 程式語言. 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)

Similar presentations


Presentation on theme: "C++ 程式語言. 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)"— Presentation transcript:

1 C++ 程式語言

2 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)

3 3 指標 (pointer) int var = 4; int* ptr = &var; cout<<&var<<endl; //12FF88 cout<<ptr<<endl; //12FF88 Cout<<*ptr<<endl; //4 Cout<< var<<endl; //4 int* ptr2; ptr2 = &var; int* ptr; int* ptr = &var; 指標 & 取址運算子 var 0012FF88 4 name address value ptr 0012FF88 0012FF80 name address value

4 4 指標 int var = 4; int* ptr = &var; cout<<var<<endl; //4 *ptr = 5; cout<<var<<endl; //5 Var = 6; cout<<*ptr<<endl; //6 int* ptr; int* ptr = &var; 指標 & 取址運算子 * 取值運算子 var 0012FF88 4 name address value ptr 0012FF88 0012FF80 name address value

5 5 問題 1 //*(&var) 等於 *(ptr) cout<<*&var<<endl; //4 cout<<&*var<<endl; //invalid cout<<*&ptr<<endl; //0012FF88 //&(*ptr) 等於 &(var) cout<<&*ptr<<endl; //0012FF88 var 0012FF88 4 name address value ptr 0012FF88 0012FF80 name address value

6 6 參照 (reference) int var = 4; int& ref = var; cout<<var<<endl; //4 cout<<ref<<endl; //4 ref = 5; cout<<var<<endl; //5 var = 6; cout<<ref<<endl; //6 int& ref = var; ( 一定要初始化 ) 參照 ref & 取址運算子 * 取值運算子 var 0012FF88 4 name address value

7 7 指標 – 指標做為函式引數 void swap_ref(int& a, int& b) { int temp = a; a = b; b= temp; } void swap_ptr(int* a, int* b) { int temp = *a; *a = *b; *b= temp; }

8 8 指標 – 指標做為函式引數 #include // 主函式 int main(void) { int a =3, b=5; swap_ref(a,b); swap_ptr(&a,&b); system(“PAUSE”); return 0; // 回傳 0 ,程式結束 }

9 9 指標 – 指標運算 int var = 4; int* ptr = &var; cout<<ptr<<endl; //12FF88 cout<<ptr+5<<endl; //12FF8D cout<<ptr-5<<endl; //12FF83 cout<<ptr++<<endl; //12FF89 cout<<ptr--<<endl; //12FF87 ptr=ptr+5; ptr=ptr-5; ptr+=5; Ptr-=5; ptr++; Ptr--; 指標運算 var 0012FF88 4 ptr 0012FF88 0012FF80

10 10 指標 – 陣列與指標間的關係 #include // 主函式 int main(void) { int a[]={1,2,3,4,5,6,7,8,9,10}; cout<<*a<<endl; // 1 cout<<*(a+1)<<endl; // 2 cout<<*(a+2)<<endl; // 3 cout<<*a+1<<endl; // for(int i=0, i<9; i++){ cout<<*(a+i); } // system(“PAUSE”); return 0; } 12345678910 a[0]a[1]a[2]a[3]a[4]a[5]a[6]a[7]a[8]a[9] 12345678910 aa+1a+2a+3a+4a+5a+6a+7a+8a+9

11 11 結構 (structure) struct customer { double x; double y; int demand; }; main(){ customer client; client.x = 10.5; client.y = 4.3; client.demand = 40; cout<<“(”<< client.x <<“,”<< client.y <<“)” <<endl; cout<<“’s demand is ”<< client.demand <<endl; getchar(); } struct name {...... }; 結構宣告

12 12 結構 – 結構陣列 struct customer { double x; double y; int demand; }; main(){ customer client[5]; getchar(); }

13 13 結構與類別 (class) class customer { public: double x; double y; int demand; }; // 兩者的宣告意義相同 struct customer { double x; double y; int demand; }; class name { public:...... protected:...... private:...... }; 結構宣告

14 14 練習 1 練習建構一個學生資料的類別 ( 結構 ) ,包含: 1. 學生學號 2. 學生姓名 3. 國文成績 4. 數學成績

15 15 結構 – 成員函式 struct customer { double x; double y; int demand; int distTo0(){return sqrt(x*x+y*y);} }; main(){ customer client; client.x = 10.5; client.y = 4.3; client.demand = 40; cout<<“(”<< client.x <<“,”<< client.y <<“)” <<endl; cout<<“’s demand is ”<< client.demand <<endl; cout<<client.distTo0(); getchar(); }

16 16 練習 2 承上題的學生資料的類別 ( 結構 ) ,增加一個會回傳平均成績 的成員函式。

17 17 結構 – 建構子 (constructor) struct customer { double x; double y; int demand; customer(); customer(double xx, double yy, int dd){ x=xx; y=yy; demand=d; } int distTo0(){return sqrt(x*x+y*y);} }; main(){ customer client(10.5, 4.3, 40); cout<<“(”<< client.x <<“,”<< client.y <<“)” <<endl; cout<<“’s demand is ”<< client.demand <<endl; cout<<client.distTo0(); getchar(); }

18 18 補充 – 命名空間 (namespace) namesapce math { int POWER(int a, int b); double SQRT(double n); int ABS(int n); }; main(){ int a=2; math::POWER(2,6); math::SQRT(2,6); } namespace name {...... }; 命名空間 using namespace name; 使用命名空間

19 19 補充 – 命名空間 (namespace) namesapce math { int POWER(int a, int b); double SQRT(double n); int ABS(int n); }; using namesapce math; main(){ int a=2; POWER(2,6); SQRT(2,6); } namespace name {...... }; 命名空間 using namespace name; 使用命名空間

20 20 標準程式庫 (standard template library; STL) #include // 不用.h #include using namesapce std; main(){ cout<<“STL”<<endl; getchar(); } using namespace std; STL 命名空間

21 21 標準程式庫 (STL) – vector 動態陣列 #include int main(void) { vector vct1(10); vector vct2(10,2); vector vct3; vct3.resize(10); int size=10; vector vct4(size); vct4.resize(12); system(“PAUSE”); return 0; } vector var vector 宣告

22 22 標準程式庫 (STL) – vector 動態陣列 #include using namespace std; int main(void) { vector vct(10); int size = vct.size(); // 陣列大小 vct[0]=100; // 存入陣列 cout<<vct[0]<<endl; // 取得陣列數值 system(“PAUSE”); return 0; }

23 23 標準程式庫 (STL) – vector 動態陣列 #include using namespace std; int main(void) { vector vct(3,2); vct.push_back(6); vct.push_front(6); vct.insert(vct.begin()+3, 5); vct.erase(vct.begin()+4); system(“PAUSE”); return 0; }

24 24 標準程式庫 (STL) – algorithm 方法說明 sort 排序 random_shuffle 亂數排序 next_enumeration 下一個列舉數 prev_enumeration 上一個列舉數 find 搜尋 #include


Download ppt "C++ 程式語言. 2 C++ 程式語言 – 大綱 1. 指標 2. 結構與類別 3. 標準函式庫 (STL)"

Similar presentations


Ads by Google