Chapter 8 - Operator Overloading

Size: px
Start display at page:

Download "Chapter 8 - Operator Overloading"

Transcription

1 1 第八章 - 運算子多載 Operator Overloading 8.1 簡介 8.2 運算子多載基本原理 8.3 運算子多載的限制 8.4 運算子函式 - 成員函式與非成員函式 8.5 多載 << 與 >> 運算子 8.6 多載單運算元運算子 (Unary Operators) 8.7 多載雙運算元運算子 (Binary Operators) 8.8 案例研讀 : Array 類別 8.9 物件型別轉換 8.10 案例研讀 : String 類別 8.11 多載 ++ 與 案例研讀 : Date 類別 8.13 標準函式庫類別 string 與 vector

2 2 8.1 簡介 使用運算子來處理物件基本運算 運算子多載 (operator overloading) 例如 :+, -, *, /, %, <<, >> 程式碼較清楚 使用上較方便 object2 = object1.add(object2); object2 = object2 + object1; object2+= object1;

3 3 8.2 運算子多載的基本原理 資料型態 (Types) 內建資料型態 bool, char, short, int, long, float, double 等 可以使用 +, -, *, /, <<, >>,... 等運算子 使用者自己定義的物件 不能使用運算子做運算 要使用運算子必須對運算子做多載

4 4 8.2 運算子多載的基本原理 運算子多載 可自行定義使用者物件的運算子運作方式 只能對現有的運算子做多載 不能自己定義新的運算子

5 5 8.2 運算子多載的基本原理 運算子多載的方法 函式命名方式 : operator 後面跟著運算子 例如 : 加法的多載函式名稱為 Operator+ 減法的多載函式名稱為 Operator- 乘法的多載函式名稱為 Operator*...

6 6 8.3 運算子多載的限制 運算子多載 不能改變內建資料型態運算子的運作方式 bool, char, short, int, long, float, double 等 不能改變運算子的計算優先順序 可使用括號限制運算子的計算順序 不能改變運算方向 由左至右或由右至左 不能改變運算元個數 例如 :+,-,*,/ 有兩個運算元, ++, -- 有一個運算元 每個運算子都必須明確的被多載才能使用 例如 : 多載 +, 並不會自動多載 +=

7 7 8.3 運算子多載的限制 可被多載的運算子 Operators that can be overloaded + - * / % ^ & ~! = < > += -= *= /= %= ^= &= = << >> >>= <<= ==!= <= >= && >*, -> [] () new delete new[] delete[] 不可被多載的運算子 Operators that cannot be overloaded..* ::?: sizeof

8 8 8.4 運算子函式 - 成員函式與非成員函式 運算子函式有兩種實作方式 成員函式 (Member functions) 使用 this 來取得主要運算元 其餘運算元位於參數列上 最左邊的物件型態必須跟運算子的類別相同 例如 :Date1+Date2, 則 Date1 的 operator+(date2) 函式被呼叫

9 9 8.4 運算子函式 - 成員函式與非成員函式 運算子函式有兩種實作方式 非成員函式 (Non member functions) 每個運算元皆位於參數列上 最左邊的物件型態可以跟運算元的類別不同 如要存取 private 或 protected 資料成員, 則必須是 friend 例如 :Date1+Date2, 則 operator+(date1, Date2) 函式被呼叫

10 運算子函式 - 成員函式與非成員函式 運算子函式呼叫原則 兩個運算元 : 呼叫左邊物件的運算子函式 例如 :Date1+Date2, 則 Date1 的 operator+(date2) 函式被呼叫 單一運算元 : 該物件的運算子被呼叫 例如 :Date1++, 則 Date1 的 operator++() 函式被呼叫

11 運算子函式 - 成員函式與非成員函式 交換率 (Commutative operators) 例如 : a + b b + a 要滿足交換率 假設 a 與 b 有相同的資料型態 只需寫一個運算子重載函式 假設 a 與 b 有不同的資料型態 針對 a + b 建立一個運算子重載函式 針對 b+ a 建立另一個運算子重載函式

12 多載 << 與 >> 運算子 << 與 >> 運算子 可供內建資料型態使用 可供使用者自訂類別多載使用 多載 << 運算子 左邊的運算元必須是型態 ostream & 例如 : cout << Date1 多載 >> 運算子 左邊的運算元必須是型態 istream & 例如 : cin << Date1 多載這兩個運算子必須使用非成員函式

13 多載 << 與 >> 運算子 多載 << 與 >> 運算子範例程式 程式中定義一個類別 Class PhoneNumber 可供記錄一組電話號碼 多載 >> 運算子 讓 PhoneNumber 類別可接受輸入格式 :(123) 多載 << 運算子 讓 PhoneNumber 類別可列印輸出格式 :(123)

14 1 // Fig. 8.3: fig08_03.cpp 2 // 多載串流輸入與輸出運算子 >> 與 << 運算子 3 // 4 #include <iostream> 5 6 using std::cout; 7 using std::cin; 8 using std::endl; 9 using std::ostream; 10 using std::istream; #include <iomanip> using std::setw; // 定義 PhoneNumber 類別 17 class PhoneNumber { 18 friend ostream &operator<<( ostream&, const PhoneNumber & ); 19 friend istream &operator>>( istream&, PhoneNumber & ); private: 22 char areacode[ 4 ]; // 三個數字區域碼 +null 23 char exchange[ 4 ]; // 三個數字交換碼 +null 24 char line[ 5 ]; // 四個數字線路碼 +null }; // end class PhoneNumber fig08_03.cpp (1 of 3) 14

15 27 28 // 多載運算子 <<, 使用非成員函式 29 // 讓物件可以透過 << 輸出到 cout 30 // cout << somephonenumber; 31 ostream &operator<<( ostream &output, const PhoneNumber &num ) 32 { 33 output << "(" << num.areacode << ") " 34 << num.exchange << "-" << num.line; return output; // enables cout << a << b << c; } // end function operator<< // 多載運算子 >>, 使用非成員函式 41 // 讓物件可以透過 >> 由 cin 讀取資料 42 // cin >> somephonenumber; 43 istream &operator>>( istream &input, PhoneNumber &num ) 44 { 45 input.ignore(); // 跳過一個輸入字元 ( 46 input >> setw( 4 ) >> num.areacode; // 輸入 3 個區域碼,setw(4) 含字串結尾 47 input.ignore( 2 ); // 跳過二個輸入字元 ) 48 input >> setw( 4 ) >> num.exchange; // 輸入 3 個交換碼 49 input.ignore(); // 跳過一個輸入字元 - 50 input >> setw( 5 ) >> num.line; // 輸入 4 個線路碼 return input; // enables cin >> a >> b >> c; fig08_03.cpp (2 of 3) 15

16 53 54 } // end function operator>> int main() 57 { 58 PhoneNumber phone; // 建立一個屬於 PhoneNumber 類別的物件 cout << "Enter phone number in the form (123) :\n"; // cin >> phone 會呼叫 operator>> 非成員函式 63 // operator>>( cin, phone ) 64 cin >> phone; // 輸入一個電話號碼 cout << "The phone number entered was: " ; // cout << phone 會呼叫 operator<< 非成員函式 69 // operator<<( cout, phone ) 70 cout << phone << endl; // 輸出一個電話號碼 return 0; } // end main fig08_03.cpp (3 of 3) fig08_03.cpp output (1 of 1) 16 Enter phone number in the form (123) : (800) The phone number entered was: (800)

17 多載單運算元運算子 多載單運算元運算子 (unary operators) 成員函式 沒有輸入參數 需為非靜態 (static) 成員函式 例如 : 靜態成員函式只能存取靜態資料 多載! 用來判斷字串是不是空字串!s 變成 s.operator!() class String { public: bool operator!() const;... };

18 多載單運算元運算子 多載單運算元運算子 (unary operators) 非成員函式 需要一個輸入參數 參數必須是類別物件或類別物件的參考 (reference) 例如 : 多載! 用來判斷字串是不是空字串!s 變成 operator!(s) class String { friend bool operator!( const String & )... }

19 多載雙運算元運算子 多載雙運算元運算子 (Binary Operators) 成員函式 輸入一個參數 需為非靜態 (static) 成員函式 例如 : 靜態成員函式只能存取靜態資料 多載字串相加運算子 class String { public: const String &operator+=( const String & );... }; y += z 等於 y.operator+=( z )

20 多載雙運算元運算子 非成員函式 需要兩個輸入參數 例如 : 多載字串相加運算子 class String { friend const String &operator+=(string &, const String & );... }; y += z 等於 operator+=( y, z )

21 案例研讀 : Array class C++ 的陣列 沒有範圍檢查 不能使用 == 比較 不能用陣列指派命令 = 不能一次輸入 / 輸出整個陣列

22 案例研讀 : Array class 實作一個具以下功能的陣列類別 可檢查範圍 知道陣列大小 可指派陣列 ( 使用 =) 可使用 [] 存取陣列元素 可使用 << 與 >> 輸出 / 輸入陣列 可使用 == 與!= 做陣列比較

23 1 // Fig. 8.4: array1.h 2 // 定義一個可供存放整數的陣列類別 Array 3 #ifndef ARRAY1_H 4 #define ARRAY1_H 5 6 #include <iostream> 7 8 using std::ostream; 9 using std::istream; class Array { 12 friend ostream &operator<<( ostream &, const Array & ); // 多載 << 13 friend istream &operator>>( istream &, Array & ); // 多載 >> public: 16 Array( int = 10 ); // 內定建構子 17 Array( const Array & ); // 複製建構子 (copy constructor) 18 ~Array(); // 解構子 19 int getsize() const; // 傳回陣列大小 // assignment operator 22 const Array &operator=( const Array & ); // 多載 = // equality operator 25 bool operator==( const Array & ) const; // 多載 == 26 array1.h (1 of 2) 23

24 27 // inequality operator; returns opposite of == operator 28 bool operator!=( const Array &right ) const // 多載!= 29 { 30 return! ( *this == right ); // 叫用 Array::operator== } // end function operator!= // subscript operator for non-const objects returns lvalue 35 int &operator[]( int ); // 多載等號左邊的 [] // subscript operator for const objects returns rvalue 38 const int &operator[]( int ) const; // 多載等號右邊的 [] private: 41 int size; // 陣列大小 42 int *ptr; // 陣列指標 }; // end class Array #endif array1.h (2 of 2) 24

25 1 // Fig 8.5: array1.cpp 2 // Array 類別的成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <iomanip> using std::setw; #include <new> // 使用 C++ 的記憶體配置功能 #include <cstdlib> // 使用 exit() 函式 #include array1.h // Array 類別定義 // Array 類別的內定建構子 ( 內定大小為 10) 20 Array::Array( int arraysize ) 21 { 22 // 檢查陣列大小 23 size = ( arraysize > 0? arraysize : 10 ); ptr = new int[ size ]; // 配置記憶體供存放陣列資料 26 array1.cpp (1 of 7) 25

26 27 for ( int i = 0; i < size; i++ ) 28 ptr[ i ] = 0; // 初始化陣列 } // end Array default constructor // Array 類別的複製建構子 33 // 輸入必須是一個 reference 以避免無窮迴圈 ( 因為傳值呼叫會呼叫複製建構子 ) 34 Array::Array( const Array &arraytocopy ) 35 : size( arraytocopy.size ) 36 { 37 ptr = new int[ size ]; // 配置記憶體空間存放陣列資料 for ( int i = 0; i < size; i++ ) 40 ptr[ i ] = arraytocopy.ptr[ i ]; // 複製陣列資料 } // end Array copy constructor // Array 類別的解構子 45 Array::~Array() 46 { 47 delete [] ptr; // 釋放陣列的記憶體空間 } // end destructor 50 array1.cpp (2 of 7) 26

27 51 // 傳回陣列大小 52 int Array::getSize() const 53 { 54 return size; } // end function getsize // 多載陣算子 =(assignment operator) 59 // 傳回型態為 const 可避免 : ( a1 = a2 ) = a3 情形下 a1 的值被改 60 const Array &Array::operator=( const Array &right ) 61 { 62 if ( &right!= this ) { // 檢查傳進來的值是否是自己本身 // 檢查輸入陣列的大小是否與本身大小相同 65 // 如果不同則要重新配置記憶體大小 66 if ( size!= right.size ) { 67 delete [] ptr; // 釋放原來的記憶體空間 68 size = right.size; // 改變陣列大小 69 ptr = new int[ size ]; // 重新配置記憶體 } // end inner if for ( int i = 0; i < size; i++ ) 74 ptr[ i ] = right.ptr[ i ]; // 複製陣列 } // end outer if array1.cpp (3 of 7) 27

28 77 78 return *this; // enables x = y = z, for example } // end function operator= // 多載運算子 == 83 // 檢查兩個陣列內容是否一樣, 一樣則傳回 true, 否則傳回 false 84 bool Array::operator==( const Array &right ) const 85 { 86 if ( size!= right.size ) 87 return false; // 先檢查兩個陣列的大小是否相同 for ( int i = 0; i < size; i++ ) if ( ptr[ i ]!= right.ptr[ i ] ) 92 return false; // 檢查兩個陣列的內容是否相同 return true; // 兩個陣列的內容相同 } // end function operator== 97 array1.cpp (4 of 7) 28

29 98 // 多載等號左邊的運算子 [] 99 // 取出註標 (subscript) 指定位置的陣列元素參考, 可供改變該陣列元素內容 100 int &Array::operator[]( int subscript ) 101 { 102 // 檢查註標 (subscript) 是否超過陣列範圍 103 if ( subscript < 0 subscript >= size ) { 104 cout << "\nerror: Subscript " << subscript 105 << " out of range" << endl; exit( 1 ); // 註標如果超過陣列範圍則終止程式執行 } // end if return ptr[ subscript ]; // 傳回陣列元素參考 } // end function operator[] 114 array1.cpp (5 of 7) 29

30 115 // 多載等號右邊的運算子 [] 116 // 取出註標 (subscript) 指定位置的陣列元素參考, 不可改變該陣列元素內容 117 const int &Array::operator[]( int subscript ) const 118 { 119 // 檢查註標 (subscript) 是否超過陣列範圍 120 if ( subscript < 0 subscript >= size ) { 121 cout << "\nerror: Subscript " << subscript 122 << " out of range" << endl; exit( 1 ); // 註標如果超過陣列範圍則終止程式執行 } // end if return ptr[ subscript ]; // 傳回陣列元素參考 } // end function operator[] // 多載 >> 運算子 133 // 輸入整個陣列的值 134 istream &operator>>( istream &input, Array &a ) 135 { 136 for ( int i = 0; i < a.size; i++ ) 137 input >> a.ptr[ i ]; return input; // enables cin >> x >> y; } // end function array1.cpp (6 of 7) 30

31 // 多載 << 運算子 144 ostream &operator<<( ostream &output, const Array &a ) 145 { 146 int i; // 輸出陣列內容 149 for ( i = 0; i < a.size; i++ ) { 150 output << setw( 12 ) << a.ptr[ i ]; if ( ( i + 1 ) % 4 == 0 ) // 一列放滿四個數字就換行 153 output << endl; } // end for if ( i % 4!= 0 ) // 最後一行換行, 只有在最後一行不是 4 個數字時才換行 158 output << endl; return output; // enables cout << x << y; } // end function operator<< array1.cpp (7 of 7) 31

32 1 // Fig. 8.6: fig08_06.cpp 2 // Array 類別測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include "array1.h" int main() 12 { 13 Array integers1( 7 ); // 建立一個可供存放 7 個元素的陣列 14 Array integers2; // 建立一個可供存放 10 個元素的陣列 // 印出陣列 integers1 的大小與內容 17 cout << "Size of array integers1 is " 18 << integers1.getsize() 19 << "\narray after initialization:\n" << integers1; // 印出陣列 integers2 的大小與內容 22 cout << "\nsize of array integers2 is " 23 << integers2.getsize() 24 << "\narray after initialization:\n" << integers2; 25 fig08_06.cpp (1 of 3) 32

33 26 // 輸入 17 個數字給陣列 integers1 與陣列 integers2 27 cout << "\ninput 17 integers:\n"; 28 cin >> integers1 >> integers2; cout << "\nafter input, the arrays contain:\n" 31 << "integers1:\n" << integers1 32 << "integers2:\n" << integers2; // 使用陣列運算子!= 判斷陣列 integers1 與陣列 integers2 是否相同 35 cout << "\nevaluating: integers1!= integers2\n"; if ( integers1!= integers2 ) 38 cout << "integers1 and integers2 are not equal\n"; // 使用陣列 integers1 建立陣列 integers3 41 // 列印陣列 integers3 42 Array integers3( integers1 ); // 呼叫複製建構子 cout << "\nsize of array integers3 is " 45 << integers3.getsize() 46 << "\narray after initialization:\n" << integers3; 47 fig08_06.cpp (2 of 3) 33

34 48 // 使用運算子 = 設定陣列 integers1 的值 49 cout << "\nassigning integers2 to integers1:\n"; 50 integers1 = integers2; // note target is smaller cout << "integers1:\n" << integers1 53 << "integers2:\n" << integers2; // 使用運算子 == 判斷陣列 integers1 是否等於陣列 integers2 56 cout << "\nevaluating: integers1 == integers2\n"; if ( integers1 == integers2 ) 59 cout << "integers1 and integers2 are equal\n"; // 使用運算子 [] 讀取陣列內容 62 cout << "\nintegers1[5] is " << integers1[ 5 ]; // 使用運算子 [] 設定陣列內容 65 cout << "\n\nassigning 1000 to integers1[5]\n"; 66 integers1[ 5 ] = 1000; 67 cout << "integers1:\n" << integers1; // 使用超出範圍的值 70 cout << "\nattempt to assign 1000 to integers1[15]" << endl; 71 integers1[ 15 ] = 1000; // ERROR: out of range return 0; } // end main fig08_06.cpp (3 of 3) 34

35 Size of array integers1 is 7 // 印出陣列 integers1 的大小與內容 Array after initialization: Size of array integers2 is 10 // 印出陣列 integers2 的大小與內容 Array after initialization: fig08_06.cpp output (1 of 3) 35 Input 17 integers: // 輸入 17 個數字給陣列 integers1 與陣列 integers After input, the arrays contain: integers1: integers2:

36 Evaluating: integers1!= integers2 // 判斷陣列 integers1 與陣列 integers2 是否相同 integers1 and integers2 are not equal 36 Size of array integers3 is 7 // 使用陣列 integers1 建立陣列 integers3 Array after initialization: fig08_06.cpp output (2 of 3) Assigning integers2 to integers1: // 使用運算子 = 設定陣列 integers1 的值 integers1: integers2: Evaluating: integers1 == integers2 // 判斷陣列 integers1 是否等於陣列 integers2 integers1 and integers2 are equal integers1[5] is 13 // 使用運算子 [] 讀取陣列內容

37 Assigning 1000 to integers1[5] // 使用運算子 [] 設定陣列內容 integers1: Attempt to assign 1000 to integers1[15] // 使用超出範圍的值 fig08_06.cpp output (3 of 3) 37 Error: Subscript 15 out of range

38 型別轉換 型別轉換 通常用於內定資料型別轉換 float to int double to int intto float... 使用者自訂資料型別 ( 類別 ) 轉換 要自己定義

39 型別轉換 轉型運算子 (Cast operator;conversion operator) 由使用者自訂類別轉到內建資料型態或其它類別 class A (char) A, (int) A, (float) A,... 必須是非靜態 (non-static) 成員函式 不能使用非成員函式 不用指定傳回值的資料型態

40 型別轉換 例如 將 class A 轉型成 char * 假設 s 是 class A 的物件 則 (char *)s 會呼叫 s.operator char*() Prototype A::operator char *() const; 同樣的可將 class A 轉型成其它內建資料型態或類別 A::operator int() const; A::operator OtherClass() const;

41 型別轉換 轉型可減少多載的使用 假設類別 String 可轉型成 char * cout << s; // s is a String 編譯器會自動將 S 轉型成 char * 不需要為 S 多載運算子 << 編譯器只能做一個轉型動作

42 案例研讀 : A String Class 建立類別 String 內含字串建立與字串維護等功能 標準函式庫有提供 string 類別 轉型建構子 (Conversion constructor) 只有一個輸入參數 將其它型態的物件轉換成類別物件 例如 :String s1( hi ); 使用 char * 型態的資料建立 String 物件 任何單一輸入參數的建構子都是轉型建構子

43 1 // Fig. 8.7: string1.h 2 // String 類別定義 3 #ifndef STRING1_H 4 #define STRING1_H 5 6 #include <iostream> 7 8 using std::ostream; 9 using std::istream; class String { 12 friend ostream &operator<<( ostream &, const String & ); 13 friend istream &operator>>( istream &, String & ); public: 16 String( const char * = ); // 轉型建構子, 使用 char * 字串當作輸入 17 String( const String & ); // 複製運算子, 使用其它 string 物件當作輸入 18 ~String(); // 解構子 const String &operator=( const String & ); // 多載運算子 =, 例 :S1=S2 21 const String &operator+=( const String & ); // 多載運算子 +=, 例 :S1+=S bool operator!() const; // 多載運算子!, 用來判斷字串 // 是不是空的 24 bool operator==( const String & ) const; // 多載運算子 ==, 檢查兩個字串是否相等 25 bool operator<( const String & ) const; // 多載運算子 <, 例 :test s1 < s string1.h (1 of 3)

44 27 // 多載運算子!=, 判斷兩字串是否相等, 例 :test s1!= s2 28 bool operator!=( const String & right ) const 29 { 30 return!( *this == right ); } // end function operator!= // 多載運算子 >, 例 : s1 > s2 35 bool operator>( const String &right ) const 36 { 37 return right < *this; } // end function operator> // 多載運算子 <=, 例 : s1 <= s2 42 bool operator<=( const String &right ) const 43 { 44 return!( right < *this ); } // end function operator <= // 多載運算子 >=, 例 : s1 >= s2 49 bool operator>=( const String &right ) const 50 { 51 return!( *this < right ); } // end function operator>= string1.h (2 of 3) 44

45 54 55 char &operator[]( int ); // 多載等號左邊運算子 [] 56 const char &operator[]( int ) const; // 多載等號右邊運算子 [] String operator()( int, int ); // 多載運算子 (), 用來傳回子字串 59 // 參數個數可以是任意個 60 int getlength() const; // 傳回字串長度 private: 63 int length; // 用來存放字串長度 64 char *sptr; // 指到字串開頭的指標 void setstring( const char * ); // utility function 67 // 用來改變字串內容 68 }; // end class String #endif string1.h (3 of 3) 45

46 1 // Fig. 8.8: string1.cpp 2 // 類別 class 的成員函式定義 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; #include <new> // 使用 C++ 的記憶體配置功能 #include <cstring> // 使用 strcpy 與 strcat 函式 15 #include <cstdlib> // 使用 exit 函式 #include string1.h // 使用 String 類別 // String 的轉型建構子 : 將 char * 型式的字串轉成 String 20 String::String( const char *s ) 21 : length( strlen( s ) ) 22 { 23 cout << "Conversion constructor: " << s << '\n'; 24 setstring( s ); // 呼叫設定字串內容函式 } // end String conversion constructor string1.cpp (1 of 8) 46

47 27 28 // String 的複製建構子 29 String::String( const String &copy ) 30 : length( copy.length ) 31 { 32 cout << "Copy constructor: " << copy.sptr << '\n'; 33 setstring( copy.sptr ); // 設定字串內容 } // end String copy constructor // 解構子 38 String::~String() 39 { 40 cout << "Destructor: " << sptr << '\n'; 41 delete [] sptr; // 釋放記憶體 } // end ~String destructor // 多載運算子 =, 傳回值使用 const String & 型態, 避免進入無窮迴圈 46 const String &String::operator=( const String &right ) 47 { 48 cout << "operator= called\n"; if ( &right!= this ) { // 檢查是不是自己 51 delete [] sptr; // 釋放記憶體 52 length = right.length; // 改變字串大小 53 setstring( right.sptr ); // 設定字串內容 54 } string1.cpp (2 of 8) 47

48 55 56 else 57 cout << "Attempted assignment of a String to itself\n"; return *this; // 啟動多重指派功能, 例 :S1=S2=S3; } // end function operator= // 多載運算子 +=, 64 // 將輸入字串串到原字串後面, 例 :S1= ABC, S2= 123, S1+=S2, 則 S1= ABC const String &String::operator+=( const String &right ) 66 { 67 size_t newlength = length + right.length; // 改變長度 68 char *tempptr = new char[ newlength + 1 ]; // 配置記憶體供存放新字串 strcpy( tempptr, sptr ); // 設定 tempptr 內容為目前字串物件的內容 71 strcpy( tempptr + length, right.sptr ); // 將輸入參數的字串內容 72 // 加到 tempptr 後面 73 delete [] sptr; // 釋放目前物件的記憶體內容 74 sptr = tempptr; // 令 sptr 指標指到 tempptr 所指到的記憶體 75 length = newlength; // 改變物件的 length 內容 return *this; // 啟動多重指派功能, 例 :S1+=S2+=S3; } // end function operator+= 80 string1.cpp (3 of 8) 48

49 81 // 多載運算子!, 用來判斷這個字串是不是空字串 82 bool String::operator!() const 83 { 84 return length == 0; } // end function operator! // 多載運算子 ==, 用來判斷物件的字串是不是跟輸入字串相同 89 bool String::operator==( const String &right ) const 90 { 91 return strcmp( sptr, right.sptr ) == 0; } // end function operator== // 多載運算子 <, 用來比較兩字串的大小 96 bool String::operator<( const String &right ) const 97 { 98 return strcmp( sptr, right.sptr ) < 0; } // end function operator< 101 string1.cpp (4 of 8) 49

50 102 // 多載等號左邊的運算子 [] 103 char &String::operator[]( int subscript ) 104 { 105 // 檢查註標是否超出範圍 106 if ( subscript < 0 subscript >= length ) { 107 cout << "Error: Subscript " << subscript 108 << " out of range" << endl; exit( 1 ); // 結束程式 111 } return sptr[ subscript ]; // 傳回參考 } // end function operator[] // 多載等號右邊的運算子 [] 118 const char &String::operator[]( int subscript ) const 119 { 120 // 檢查註標是否超出範圍 121 if ( subscript < 0 subscript >= length ) { 122 cout << "Error: Subscript " << subscript 123 << " out of range" << endl; exit( 1 ); // 結束程式 126 } return sptr[ subscript ]; // 傳回常數參考 } // end function operator[] string1.cpp (5 of 8) 50

51 // 傳回由 index 位置開始長度為 length 的子字串 133 // 例 :S1= ABC , 呼叫 S1(3,3) 會傳回字串 C String String::operator()( int index, int sublength ) 135 { 136 // 檢查 index 是否超出字串範圍, 或者 sublength<0 137 // 如果是的話傳回空字串 138 if ( index < 0 index >= length sublength < 0 ) 139 return ""; // 傳回空字串 // 取得子字串長度, 如果 index+sublength 超過字串範圍, 則子字串長度需調整 142 int len; if ( ( sublength == 0 ) ( index + sublength > length ) ) 145 len = length - index; 146 else 147 len = sublength; // 配置一塊空間供暫時存放子字串 151 char *tempptr = new char[ len + 1 ]; // 將子字串複製到 tempptr 並終止字串 154 strncpy( tempptr, &sptr[ index ], len ); 155 tempptr[ len ] = '\0'; string1.cpp (6 of 8) 51

52 // 建構一個存放子字串的 String 物件 158 String tempstring( tempptr ); 159 delete [] tempptr; // 釋放記憶體 return tempstring; // 傳回存放子字串的 String 物件 } // end function operator() // 傳回字串長度 166 int String::getLength() const 167 { 168 return length; } // end function getlenth // 改變字串內容 173 void String::setString( const char *string2 ) 174 { 175 sptr = new char[ length + 1 ]; // 配置記憶體 176 strcpy( sptr, string2 ); // 複製字串 } // end function setstring string1.cpp (7 of 8) 52

53 // 多載運算元 << 181 ostream &operator<<( ostream &output, const String &s ) 182 { 183 output << s.sptr; return output; // 允許多重呼叫, 例 :cout << S1 << S2 << S } // end function operator<< // 多載運算元 >> 190 istream &operator>>( istream &input, String &s ) 191 { 192 char temp[ 100 ]; // 宣稱一塊記憶體存放輸入字串 input >> setw( 100 ) >> temp; 195 s = temp; // 將 char * 型態的字串指派給 String 物件 s, 會使用轉型建構子 196 // const String &String::operator=( const String &right ) 197 return input; // 允許多重呼叫, 例 :cin >> S1 >> S2 >> S } // end function operator>> string1.cpp (8 of 8) 53

54 1 // Fig. 8.9: fig08_09.cpp 2 // String 類別測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include "string1.h" // 使用 String 類別 9 10 int main() 11 { 12 String s1( "happy" ); 13 String s2( " birthday" ); 14 String s3; // 測試 String 的 ==,!=,>,<,>=,<=,<< 運算子 17 cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 18 << "\"; s3 is \"" << s3 << '\"' 19 << "\n\nthe results of comparing s2 and s1:" 20 << "\ns2 == s1 yields " 21 << ( s2 == s1? "true" : "false" ) 22 << "\ns2!= s1 yields " 23 << ( s2!= s1? "true" : "false" ) 24 << "\ns2 > s1 yields " 25 << ( s2 > s1? "true" : "false" ) fig08_09.cpp (1 of 4) 54

55 26 << "\ns2 < s1 yields " 27 << ( s2 < s1? "true" : "false" ) 28 << "\ns2 >= s1 yields " 29 << ( s2 >= s1? "true" : "false" ) 30 << "\ns2 <= s1 yields " 31 << ( s2 <= s1? "true" : "false" ); // 測試 String 的! 運算子 34 cout << "\n\ntesting!s3:\n"; if (!s3 ) { 37 cout << "s3 is empty; assigning s1 to s3;\n"; 38 s3 = s1; 39 cout << "s3 is \"" << s3 << "\""; 40 } // 測試 String 的 += 運算子 43 cout << "\n\ns1 += s2 yields s1 = "; 44 s1 += s2; 45 cout << s1; // 測試 String 的轉型建構子 48 cout << "\n\ns1 += \" to you\" yields\n"; 49 s1 += to you ; // 測試 String 的轉型建構子, S1+=(char *) cout << "s1 = " << s1 << "\n\n"; fig08_09.cpp (2 of 4) 55

56 51 52 // 測試 String 的 () 運算子 53 cout << "The substring of s1 starting at\n" 54 << "location 0 for 14 characters, s1(0, 14), is:\n" 55 << s1( 0, 14 ) << "\n\n"; // 測試 String 的 () 運算子, 由指定位置到字串結尾 58 cout << "The substring of s1 starting at\n" 59 << "location 15, s1(15, 0), is: " 60 << s1( 15, 0 ) << \n\n ; // 0 表示到字串結尾 // 測試 String 的複製建構子 63 String *s4ptr = new String( s1 ); 64 cout << "\n*s4ptr = " << *s4ptr << "\n\n"; // 測試 String 的 = 運算子, 自己指派給自己 67 cout << "assigning *s4ptr to *s4ptr\n"; 68 *s4ptr = *s4ptr; 69 cout << "*s4ptr = " << *s4ptr << '\n'; // 測試解構子 72 delete s4ptr; 73 fig08_09.cpp (3 of 4) 56

57 74 // 測試 String 的 [] 運算子 75 s1[ 0 ] = 'H'; 76 s1[ 6 ] = 'B'; 77 cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " 78 << s1 << "\n\n"; // 測試註標超過範圍的情況 81 cout << "Attempt to assign 'd' to s1[30] yields:" << endl; 82 s1[ 30 ] = 'd'; // ERROR: subscript out of range return 0; } // end main fig08_09.cpp (4 of 4) 57

58 // 建構 s1, s2, s3 三個 String 物件 Conversion constructor: happy Conversion constructor: birthday Conversion constructor: fig08_09.cpp (1 of 3) 58 // 測試 String 的 ==,!=,>,<,>=,<=,<< 運算子 s1 is "happy"; s2 is " birthday"; s3 is "" The results of comparing s2 and s1: s2 == s1 yields false s2!= s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true // 測試 String 的! 運算子 Testing!s3: s3 is empty; assigning s1 to s3; operator= called s3 is "happy // 測試 String 的 +=, 運算子 s1 += s2 yields s1 = happy birthday

59 // 測試 String 的轉型建構子 s1 += " to you" yields Conversion constructor: to you Destructor: to you s1 = happy birthday to you fig08_09.cpp (2 of 3) 59 // 測試 String 的 () 運算子 Conversion constructor: happy birthday // 在 () 運算子中會建立一個 tempstring Copy constructor: happy birthday //() 運算子 return 命令會回傳一個 string 物件 Destructor: happy birthday // 解構 tempstring The substring of s1 starting at location 0 for 14 characters, s1(0, 14), is: happy birthday Destructor: happy birthday // 解構 () 運算子回傳的 string 物件 // 測試 String 的 () 運算子, 由指定位置到字串結尾 Conversion constructor: to you Copy constructor: to you Destructor: to you The substring of s1 starting at location 15, s1(15, 0), is: to you Destructor: to you

60 60 // 測試 String 的複製建構子 Copy constructor: happy birthday to you fig08_09.cpp (3 of 3) *s4ptr = happy birthday to you // 測試 String 的 = 運算子, 自己指派給自己 assigning *s4ptr to *s4ptr operator= called Attempted assignment of a String to itself *s4ptr = happy birthday to you // 測試解構子 Destructor: happy birthday to you // 測試 String 的 [] 運算子 s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you // 測試註標超過範圍的情況 Attempt to assign 'd' to s1[30] yields: Error: Subscript 30 out of range

61 多載運算子 ++ 與 -- 多載前置 ++ 與前置 -- 運算子 ++object, --object 使用成員函式 函式原型 Date &operator++(); ++d1 會呼叫 d1.operator++() 函式 使用非成員函式 函式原型 Friend Date &operator++( Date &); ++d1 會呼叫 operator++( d1 ) 函式

62 多載運算子 ++ 與 -- 多載後置 ++ 與後置 -- 運算子 object++ 或 object-- 使用成員函式 僅用來區別 ++ 或 -- 在後面, 參數值沒有特殊意義 函式原型 Date operator++( int ); d1++ 會呼叫 d1.operator++( 0 ) 使用非成員函式 函式原型 friend Date operator++( Data &, int ); d1++ 會呼叫 operator++( d1, 0 )

63 多載運算子 ++ 與 -- 傳回值 (Return values) ++object 或 --object 傳參考 ( 例 :Date &) 可在等號左邊 object ++ 或 object -- 傳值 傳回加 1 或減 1 前的值 不能在等號左邊

64 案例研究 : A Date Class Date 類別範例 多載前置 ++ 與後置 ++ 運算子 改變 day, month, 與 year 多載 += 運算子 檢查是否為閏年 檢查是否為月份的最後一天 檢查是否為年份的最後一天

65 1 // Fig. 8.10: date1.h 2 // Date 類別定義 3 #ifndef DATE1_H 4 #define DATE1_H 5 #include <iostream> 6 7 using std::ostream; 8 9 class Date { 10 friend ostream &operator<<( ostream &, const Date & ); public: 13 Date( int m = 1, int d = 1, int y = 1900 ); // 建構子 14 void setdate( int, int, int ); // 設定日期 Date &operator++(); // 多載前置 ++ 運算子 17 Date operator++( int ); // 多載後置 ++ 運算子 const Date &operator+=( int ); // 多載 += 運算子 bool leapyear( int ) const; // 檢查是否是閏年 22 bool endofmonth( int ) const; // 檢查是否是一個月的最後一天 date1.h (1 of 2) 65

66 23 24 private: 25 int month; 26 int day; 27 int year; static const int days[]; // 存放每個月的天數, 整個類別只有一份 30 void helpincrement(); // 將日期加 1 並做適當調整 }; // end class Date #endif date1.h (2 of 2) 66

67 1 // Fig. 8.11: date1.cpp 2 // Date 類別成員函式定義 3 #include <iostream> 4 #include "date1.h" // 初始化靜態成員 8 const int Date::days[] = 9 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 日期建構子 12 Date::Date( int m, int d, int y ) 13 { 14 setdate( m, d, y ); } // end Date constructor // 設定月, 日, 年 19 void Date::setDate( int mm, int dd, int yy ) 20 { 21 month = ( mm >= 1 && mm <= 12 )? mm : 1; 22 year = ( yy >= 1900 && yy <= 2100 )? yy : 1900; 23 date1.cpp (1 of 5) 67

68 24 // 檢查是不是閏年 25 if ( month == 2 && leapyear( year ) ) 26 day = ( dd >= 1 && dd <= 29 )? dd : 1; 27 else 28 day = ( dd >= 1 && dd <= days[ month ] )? dd : 1; } // end function setdate // 多載前置 ++ 運算子 ( 例 :++d1) 33 Date &Date::operator++() 34 { 35 helpincrement(); // 將日期加 1 並做適當調整 return *this; // 傳參考 } // end function operator // 多載後置 ++ 運算子 ( 例 :d1++) 42 // 整數參數不需要給名稱 43 Date Date::operator++( int ) 44 { 45 Date temp = *this; // 複製目前物件狀態 46 helpincrement(); // 將日期加 1 並做適當調整 // 傳回未加 1 的版本 49 return temp; // 傳值 } // end function operator++ date1.cpp (2 of 5) 68

69 52 53 // 多載 += 運算子 54 const Date &Date::operator+=( int additionaldays ) 55 { 56 for ( int i = 0; i < additionaldays; i++ ) 57 helpincrement(); return *this; // 允許多重呼叫 } // end function operator+= // 假如是閏年傳回 true 64 // 否則傳回 false 65 bool Date::leapYear( int testyear ) const 66 { 67 if ( testyear % 400 == 0 68 ( testyear % 100!= 0 && testyear % 4 == 0 ) ) 69 return true; // 是閏年 70 else 71 return false; // 不是閏年 } // end function leapyear 74 date1.cpp (3 of 5) 69

70 75 // 檢查是不是該月的最後一天 76 bool Date::endOfMonth( int testday ) const 77 { 78 if ( month == 2 && leapyear( year ) ) 79 return testday == 29; // 閏年的最後一天是 29 日 80 else 81 return testday == days[ month ]; } // end function endofmonth // 將日期加 1 並做適當調整 86 void Date::helpIncrement() 87 { 88 // 日期不是該月的最後一天 89 if (!endofmonth( day ) ) 90 ++day; else // 日期是該月的最後一天, 而且月份是 12 月 95 if ( month < 12 ) { 96 ++month; 97 day = 1; 98 } 99 date1.cpp (4 of 5) 70

71 100 // 日期是該年度的最後一天 101 else { year; 103 month = 1; 104 day = 1; 105 } } // end function helpincrement // 多載輸運算子 110 ostream &operator<<( ostream &output, const Date &d ) 111 { 112 static char *monthname[ 13 ] = { "", "January", 113 "February", "March", "April", "May", "June", 114 "July", "August", "September", "October", 115 "November", "December" }; output << monthname[ d.month ] << ' ' 118 << d.day << ", " << d.year; return output; // 允許多重呼叫 } // end function operator<< date1.cpp (5 of 5) 71

72 1 // Fig. 8.12: fig08_12.cpp 2 // Date 類別測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include date1.h // Date 類別定義 9 10 int main() 11 { // 建立日期物件 12 Date d1; // 內定值是 1990 年 1 月 1 日 13 Date d2( 12, 27, 1992 ); 14 Date d3( 0, 99, 8045 ); // 不正確的日期 15 // 印出日期內容, 使用 << 運算子 16 cout << "d1 is " << d1 << "\nd2 is " << d2 17 << "\nd3 is " << d3; 18 // 使用 += 運算子 19 cout << "\n\nd2 += 7 is " << ( d2 += 7 ); 20 // 使用前置 ++ 運算子 21 d3.setdate( 2, 28, 1992 ); 22 cout << "\n\n d3 is " << d3; 23 cout << "\n++d3 is " << ++d3; Date d4( 7, 13, 2002 ); fig08_12.cpp (1 of 2) 72

73 26 27 cout << "\n\ntesting the preincrement operator:\n" 28 << " d4 is " << d4 << '\n'; 29 cout << "++d4 is " << ++d4 << '\n'; 30 cout << " d4 is " << d4; 31 // 使用後置 ++ 運算子 32 cout << "\n\ntesting the postincrement operator:\n" 33 << " d4 is " << d4 << '\n'; 34 cout << "d4++ is " << d4++ << '\n'; 35 cout << " d4 is " << d4 << endl; return 0; } // end main fig08_12.cpp (2 of 2) 73

74 // 建立日期物件 d1 is January 1, 1900 d2 is December 27, 1992 d3 is January 1, 1900 // 使用 += 運算子 d2 += 7 is January 3, 1993 fig08_12.cpp output (1 of 1) 74 // 使用後置 ++ 運算子 d3 is February 28, d3 is February 29, 1992 Testing the preincrement operator: d4 is July 13, d4 is July 14, 2002 d4 is July 14, 2002 // 使用後置 ++ 運算子 Testing the postincrement operator: d4 is July 14, 2002 d4++ is July 14, 2002 d4 is July 15, 2002

75 標準函式庫類別 string 與 vector C++ 內建的類別 可供程式設計者使用 string 跟我們的 String 類別類似 vector 可動態改變大小的陣列 使用 string 與 vector 重做 String 與 Array 範例

76 標準函式庫類別 string 與 vector 類別 string Header <string>, namespace std 有提供轉型建構子 string s1( hi ); 有多載 << cout << s1 有多載關係運算子 ==!= >= > <= < 有多載 = 運算子 有多載 += 運算子

77 標準函式庫類別 string 與 vector 類別 string 有多載 [] 運算子 可存取一個字元 沒有範圍檢查 提供子字串函式 substr s1.substr(0, 14); 由第 0 個位置開始, 取得 14 個字元 S1.substr(15) 由第 15 個位置開始到字串結尾 提供 at 函式 s1.at(10) 傳回第 10 個字元 有範圍檢查, 如果超出範圍會結束程式執行

78 1 // Fig. 8.13: fig08_13.cpp 2 // 標準函式庫字串類別測試程式 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <string> // 使用 string 類別 9 10 using std::string; int main() 13 { // 建立三個 string 物件 14 string s1( "happy" ); 15 string s2( " birthday" ); 16 string s3; // 測試 string 的 ==,!=,>,<,>=,<=,<< 運算子 19 cout << "s1 is \"" << s1 << "\"; s2 is \"" << s2 20 << "\"; s3 is \"" << s3 << '\"' 21 << "\n\nthe results of comparing s2 and s1:" 22 << "\ns2 == s1 yields " 23 << ( s2 == s1? "true" : "false" ) 24 << "\ns2!= s1 yields " 25 << ( s2!= s1? "true" : "false" ) fig08_13.cpp (1 of 4) 78

79 26 << "\ns2 > s1 yields " 27 << ( s2 > s1? "true" : "false" ) 28 << "\ns2 < s1 yields " 29 << ( s2 < s1? "true" : "false" ) 30 << "\ns2 >= s1 yields " 31 << ( s2 >= s1? "true" : "false" ) 32 << "\ns2 <= s1 yields " 33 << ( s2 <= s1? "true" : "false" ); // 測試 string 的 empty() 運算子 36 cout << "\n\ntesting s3.empty():\n"; if ( s3.empty() ) { 39 cout << "s3 is empty; assigning s1 to s3;\n"; 40 s3 = s1; // assign s1 to s3 41 cout << "s3 is \"" << s3 << "\""; 42 } // 測試 string 的 += 運算子, 字串串接 45 cout << "\n\ns1 += s2 yields s1 = "; 46 s1 += s2; // test overloaded concatenation 47 cout << s1; 48 fig08_13.cpp (2 of 4) 79

80 49 50 // 測試 string 的 += 運算子, 串接 C-style 字串 51 cout << "\n\ns1 += \" to you\" yields\n"; 52 s1 += " to you"; 53 cout << "s1 = " << s1 << "\n\n"; // 測試 substr 函式 s1.substr( 0, 14 ) 56 cout << "The substring of s1 starting at location 0 for\n" 57 << "14 characters, s1.substr(0, 14), is:\n" 58 << s1.substr( 0, 14 ) << "\n\n"; // 測試 substr 函式 s1.substr( 15 ) 61 cout << "The substring of s1 starting at\n" 62 << "location 15, s1.substr(15), is:\n" 63 << s1.substr( 15 ) << '\n'; // 測試複製建構子 66 string *s4ptr = new string( s1 ); 67 cout << "\n*s4ptr = " << *s4ptr << "\n\n"; // 測試 = 運算子, 將自己指定給自己 70 cout << "assigning *s4ptr to *s4ptr\n"; 71 *s4ptr = *s4ptr; 72 cout << "*s4ptr = " << *s4ptr << '\n'; 73 fig08_13.cpp (3 of 4) 80

81 74 // 測試解構子 75 delete s4ptr; // 測試等號左邊的 [] 運算子 78 s1[ 0 ] = 'H'; 79 s1[ 6 ] = 'B'; 80 cout << "\ns1 after s1[0] = 'H' and s1[6] = 'B' is: " 81 << s1 << "\n\n"; // 測試使用 at 函式註標超過圍的情況 84 cout << "Attempt to assign 'd' to s1.at( 30 ) yields:" << endl; 85 s1.at( 30 ) = 'd'; // ERROR: subscript out of range return 0; } // end main fig08_13.cpp (4 of 4) 81

82 // 建立三個 string 物件 // 測試 string 的 ==,!=,>,<,>=,<=,<< 運算子 s1 is "happy"; s2 is " birthday"; s3 is "" The results of comparing s2 and s1: s2 == s1 yields false s2!= s1 yields true s2 > s1 yields false s2 < s1 yields true s2 >= s1 yields false s2 <= s1 yields true // 測試 string 的 empty() 運算子 Testing s3.empty(): s3 is empty; assigning s1 to s3; s3 is "happy" // 測試 string 的 += 運算子, 字串串接 s1 += s2 yields s1 = happy birthday // 測試 string 的 += 運算子, 串接 C-style 字串 s1 += " to you" yields s1 = happy birthday to you // 測試 substr 函式 s1.substr( 0, 14 ) The substring of s1 starting at location 0 for 14 characters, s1.substr(0, 14), is: happy birthday fig08_13.cpp output (1 of 2) 82

83 // 測試 substr 函式 s1.substr( 15 ) The substring of s1 starting at location 15, s1.substr(15), is: to you // 測試複製建構子 (string *s4ptr = new string( s1 )) *s4ptr = happy birthday to you // 測試 = 運算子, 將自己指定給自己 assigning *s4ptr to *s4ptr *s4ptr = happy birthday to you // 測試解構子 // 測試等號左邊的 [] 運算子 s1 after s1[0] = 'H' and s1[6] = 'B' is: Happy Birthday to you // 測試使用 at 函式註標超過圍的情況 Attempt to assign 'd' to s1.at( 30 ) yields: fig08_13.cpp output (2 of 2) 83 abnormal program termination // 程式不正常結束

84 標準函式庫類別 string 與 vector 類別 vector Header <vector>, namespace std 可供建立各種資料型態的陣列 vector< int > myarray(10) 提供 size ( myarray.size() ) 多載運算子 [] 取得指定位置元素, myarray[3] 多載運算子!=, ==, 與 =

85 1 // Fig. 8.14: fig08_14.cpp 2 // 使用標準函式庫類別 vector 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <iomanip> using std::setw; #include <vector> // 使用標準函式庫類別 vector using std::vector; void outputvector( const vector< int > & ); 18 void inputvector( vector< int > & ); int main() 21 { 22 vector< int > integers1( 7 ); // 建構內含 7 個 int 元素的陣列 23 vector< int > integers2( 10 ); // 建構內含 10 個 int 元素的陣列 24 fig08_14.cpp (1 of 5) 85

86 25 // 印出 integers1 的大小與內容 26 cout << "Size of vector integers1 is " 27 << integers1.size() 28 << "\nvector after initialization:\n"; 29 outputvector( integers1 ); // 印出 integers2 的大小與內容 32 cout << "\nsize of vector integers2 is " 33 << integers2.size() 34 << "\nvector after initialization:\n"; 35 outputvector( integers2 ); // 輸入與印出 integers1 與 integers2 38 cout << "\ninput 17 integers:\n"; 39 inputvector( integers1 ); 40 inputvector( integers2 ); cout << "\nafter input, the vectors contain:\n" 43 << "integers1:\n"; 44 outputvector( integers1 ); 45 cout << "integers2:\n"; 46 outputvector( integers2 ); // 使用運算子不等於 (!=) 49 cout << "\nevaluating: integers1!= integers2\n"; 50 fig08_14.cpp (2 of 5) 86

87 51 if ( integers1!= integers2 ) 52 cout << "integers1 and integers2 are not equal\n"; // 使用 integers1 建構向量 integers3 55 // 印出 integers3 的大小與內容 56 vector< int > integers3( integers1 ); // 複製建構子 cout << "\nsize of vector integers3 is " 59 << integers3.size() 60 << "\nvector after initialization:\n"; 61 outputvector( integers3 ); // 使用運算子 (=) 65 cout << "\nassigning integers2 to integers1:\n"; 66 integers1 = integers2; cout << "integers1:\n"; 69 outputvector( integers1 ); 70 cout << "integers2:\n"; 71 outputvector( integers1 ); 72 fig08_14.cpp (3 of 5) 87

88 73 // 使用運算子 (==) 74 cout << "\nevaluating: integers1 == integers2\n"; if ( integers1 == integers2 ) 77 cout << "integers1 and integers2 are equal\n"; // 使用等號右邊的運算子 [] 80 cout << "\nintegers1[5] is " << integers1[ 5 ]; // 使用等號左邊的運算子 [] 83 cout << "\n\nassigning 1000 to integers1[5]\n"; 84 integers1[ 5 ] = 1000; 85 cout << "integers1:\n"; 86 outputvector( integers1 ); // 使用超出範圍的註標 89 cout << "\nattempt to assign 1000 to integers1.at( 15 )" 90 << endl; 91 integers1.at( 15 ) = 1000; // ERROR: out of range return 0; } // end main 96 fig08_14.cpp (4 of 5) 88

89 97 // output vector contents 98 void outputvector( const vector< int > &array ) 99 { 100 for ( int i = 0; i < array.size(); i++ ) { 101 cout << setw( 12 ) << array[ i ]; if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output 104 cout << endl; } // end for if ( i % 4!= 0 ) 109 cout << endl; } // end function outputvector // input vector contents 114 void inputvector( vector< int > &array ) 115 { 116 for ( int i = 0; i < array.size(); i++ ) 117 cin >> array[ i ]; } // end function inputvector fig08_14.cpp (5 of 5) 89

90 // 建構內含 7 個 int 元素的陣列 integers1 // 建構內含 10 個 int 元素的陣列 integers2 90 // 印出 integers1 的大小與內容 Size of vector integers1 is 7 vector after initialization: fig08_14.cpp output (1 of 2) // 印出 integers2 的大小與內容 Size of vector integers2 is 10 vector after initialization: // 輸入與印出 integers1 與 integers2 Input 17 integers: After input, the vectors contain: integers1: integers2:

91 // 使用運算子不等於 (!=) Evaluating: integers1!= integers2 integers1 and integers2 are not equal // 使用 integers1 建構向量 integers3 // 印出 integers3 的大小與內容 Size of vector integers3 is 7 vector after initialization: fig08_14.cpp output (2 of 2) 91 // 使用運算子 (=) Assigning integers2 to integers1: integers1: integers2: // 使用運算子 (==) Evaluating: integers1 == integers2 integers1 and integers2 are equal

92 // 使用等號右邊的運算子 [] integers1[5] is // 使用等號左邊的運算子 [] Assigning 1000 to integers1[5] integers1: fig08_14.cpp output (2 of 2) // 使用超出範圍的註標 Attempt to assign 1000 to integers1.at( 15 ) abnormal program termination

93 Card 類別 93 #ifndef _CARD_H_ #define _CARD_H_ #define NUM_OF_CARD 52 #define NUM_OF_COLOR 4 #define NUM_OF_POINT 13 // 定義撲克牌 (Card) 類別 class Card{ public: Card(int color, int point);// 建構子 void print(void) const; // 列印卡片內容 int getcolor(void) const; // 傳回花色 int getpoint(void) const; // 傳回點數 private: }; const int color ; // 花色 const int point ; // 點數 #endif

94 Dealer 類別 94 #ifndef _DEALER_H_ #define _DEALER_H_ #include "card.h" #include "Player.h" // 定義發牌員 (Dealer) 類別 class Dealer{ public: Dealer(void); // 建構子 void pushcard(card &c); // 拿一張牌給發牌員 void shuffle(void); // 洗牌 void deal(int n, Player &p); // 發 n 張牌給玩家 p void print() const; // 印出發牌員手上的牌 private: Card &popcard(void); // 從發牌員手上拿取一張牌 Card *card[num_of_card]; // 用來存放撲克牌 int numcard; // 發牌員手上的撲克牌數 }; #endif

95 Player 類別 95 #ifndef _PLAYER_H_ #define _PLAYER_H_ #include "card.h" // 定義玩家 (Player) 類別 class Player{ public: private: }; #endif Player(int m=1000); // 建構子 void pushcard(card &); // 拿一張牌給玩家 Card &popcard(); // 從玩家手上拿取一張牌 void print(void) const; // 印出玩家手上的牌 void printmoney(void) const; // 印出玩家手上的錢 void sort(void); // 將玩家手上的牌排序 void setbet(int); // 設定賭注金額 int getbet(void) const; // 取得睹注金額 int getmoney(void) const; // 取得玩家手上的錢數 int showhand(void); // 顯示並傳回此回合結果, 計算 ( 睹注 x 倍率 ) // 將 ( 睹注 x 倍率 ) 加到玩家手上的錢數 Card *card[num_of_card]; // 存放撲克牌 int numcard; // 存放玩家手上的撲克牌數 int money; // 存放玩家錢數 int bet; // 存放睹注

96 96 int PRIZE_TABLE[9]={// 倍率表 0, // 沒中獎 1000, // 同花順 500, // 鐵支 300, // 同花 200, // 葫蘆 100, // 順子 20, // 三條 5, // 二對 1 // 一對 };

新版 明解C++入門編

新版 明解C++入門編 511!... 43, 85!=... 42 "... 118 " "... 337 " "... 8, 290 #... 71 #... 413 #define... 128, 236, 413 #endif... 412 #ifndef... 412 #if... 412 #include... 6, 337 #undef... 413 %... 23, 27 %=... 97 &... 243,

More information

Microsoft PowerPoint - 11_Templates.ppt

Microsoft PowerPoint - 11_Templates.ppt 1 1. 上機考 20% 期末考 6/23( 四 ) 晚 6:30~8:30 範圍 : 第 7, 8, 9, 10 章實習內容 按座位坐, 隨機抽兩題 2. 紙上測驗 20% 6/21( 二 ) :9:30~11:00 課本 7-11, 13 章內容 2 第 11 章樣版 (Templates) 11.1 簡介 11.2 函式樣版 11.3 多載函式樣版 11.4 類別樣版 11.5 類別樣版與無型

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 多載 Overloading 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 函數多載 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 運算子多載 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 函數多載 Function

More information

C/C++程序设计 - 字符串与格式化输入/输出

C/C++程序设计 - 字符串与格式化输入/输出 C/C++ / Table of contents 1. 2. 3. 4. 1 i # include # include // density of human body : 1. 04 e3 kg / m ^3 # define DENSITY 1. 04 e3 int main ( void ) { float weight, volume ; int

More information

c_cpp

c_cpp C C++ C C++ C++ (object oriented) C C++.cpp C C++ C C++ : for (int i=0;i

More information

C/C++语言 - C/C++数据

C/C++语言 - C/C++数据 C/C++ C/C++ Table of contents 1. 2. 3. 4. char 5. 1 C = 5 (F 32). 9 F C 2 1 // fal2cel. c: Convert Fah temperature to Cel temperature 2 # include < stdio.h> 3 int main ( void ) 4 { 5 float fah, cel ;

More information

PowerPoint Presentation

PowerPoint Presentation 第六章簡介運算子超載 (Operator Overloading) 6-1 運算子超載的基礎 6-2 超載二元運算子 6-3 超載邏輯與關係運算子 6-4 超載一元運算子 6-5 使用夥伴函數 6-6 細部檢視指定運算子 6-7 超載註標運算子 6-1 運算子超載的基礎 甚麼是運算子超載? 讓運算子 ( 符號 ) 有不同的意義 EX: 運算子的預設意義 ( 以 + 與 = 為例 ) class frac

More information

C++ 程式設計

C++ 程式設計 C C 料, 數, - 列 串 理 列 main 數串列 什 pointer) 數, 數, 數 數 省 不 不, 數 (1) 數, 不 數 * 料 * 數 int *int_ptr; char *ch_ptr; float *float_ptr; double *double_ptr; 數 (2) int i=3; int *ptr; ptr=&i; 1000 1012 ptr 數, 數 1004

More information

Strings

Strings Inheritance Cheng-Chin Chiang Relationships among Classes A 類 別 使 用 B 類 別 學 生 使 用 手 機 傳 遞 訊 息 公 司 使 用 金 庫 儲 存 重 要 文 件 人 類 使 用 交 通 工 具 旅 行 A 類 別 中 有 B 類 別 汽 車 有 輪 子 三 角 形 有 三 個 頂 點 電 腦 內 有 中 央 處 理 單 元 A

More information

untitled

untitled 不 料 料 例 : ( 料 ) 串 度 8 年 數 串 度 4 串 度 數 數 9- ( ) 利 數 struct { ; ; 數 struct 數 ; 9-2 數 利 數 C struct 數 ; C++ 數 ; struct 省略 9-3 例 ( 料 例 ) struct people{ char name[]; int age; char address[4]; char phone[]; int

More information

Microsoft PowerPoint - string_kruse [兼容模式]

Microsoft PowerPoint - string_kruse [兼容模式] Strings Strings in C not encapsulated Every C-string has type char *. Hence, a C-string references an address in memory, the first of a contiguous set of bytes that store the characters making up the string.

More information

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 月 3 日 1 1 INPUTOUTPUT 1 InputOutput 题目描述 用 cin 输入你的姓名 ( 没有空格 ) 和年龄 ( 整数 ), 并用 cout 输出 输入输出符合以下范例 输入 master 999 输出 I am master, 999 years old. 注意 "," 后面有一个空格,"." 结束,

More information

C/C++ - 文件IO

C/C++ - 文件IO C/C++ IO Table of contents 1. 2. 3. 4. 1 C ASCII ASCII ASCII 2 10000 00100111 00010000 31H, 30H, 30H, 30H, 30H 1, 0, 0, 0, 0 ASCII 3 4 5 UNIX ANSI C 5 FILE FILE 6 stdio.h typedef struct { int level ;

More information

C/C++ 语言 - 循环

C/C++ 语言 - 循环 C/C++ Table of contents 7. 1. 2. while 3. 4. 5. for 6. 8. (do while) 9. 10. (nested loop) 11. 12. 13. 1 // summing.c: # include int main ( void ) { long num ; long sum = 0L; int status ; printf

More information

Microsoft PowerPoint - C-Ch10.ppt

Microsoft PowerPoint - C-Ch10.ppt 了解陣列元素的位址 陣列 指標的應用 10-1 陣列與指標的關係 可以使用位址運算子 (&) 來查詢陣列中各個元素的位址 &test[0] 這行表示陣列最前面元素的位址 &test[1] 這行表示陣列第二個元素的位址 關於陣列名稱的機制 陣列名稱可以表示陣列最前面元素的位址 #include int main(void) int test[5] = 80,60,55,22,75;

More information

FY.DOC

FY.DOC 高 职 高 专 21 世 纪 规 划 教 材 C++ 程 序 设 计 邓 振 杰 主 编 贾 振 华 孟 庆 敏 副 主 编 人 民 邮 电 出 版 社 内 容 提 要 本 书 系 统 地 介 绍 C++ 语 言 的 基 本 概 念 基 本 语 法 和 编 程 方 法, 深 入 浅 出 地 讲 述 C++ 语 言 面 向 对 象 的 重 要 特 征 : 类 和 对 象 抽 象 封 装 继 承 等 主

More information

untitled

untitled 1 Outline 數 料 數 數 列 亂數 練 數 數 數 來 數 數 來 數 料 利 料 來 數 A-Z a-z _ () 不 數 0-9 數 不 數 SCHOOL School school 數 讀 school_name schoolname 易 不 C# my name 7_eleven B&Q new C# (1) public protected private params override

More information

Microsoft Word - ch04三校.doc

Microsoft Word - ch04三校.doc 4-1 4-1-1 (Object) (State) (Behavior) ( ) ( ) ( method) ( properties) ( functions) 4-2 4-1-2 (Message) ( ) ( ) ( ) A B A ( ) ( ) ( YourCar) ( changegear) ( lowergear) 4-1-3 (Class) (Blueprint) 4-3 changegear

More information

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf (%d, & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf (%d %d 2013 18 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp, Compilation Error cin scanf Time Limit Exceeded 1: A 5 B 5 C 5 D 5 E 5 F 5 1 2013 C 1 # include 2 int main ( void ) 3 { 4 int cases, a, b,

More information

untitled

untitled 3 C++ 3.1 3.2 3.3 3.4 new delete 3.5 this 3.6 3.7 3.1 3.1 class struct union struct union C class C++ C++ 3.1 3.1 #include struct STRING { typedef char *CHARPTR; // CHARPTR s; // int strlen(

More information

untitled

untitled (encapsulation) 例 類 說 類 料 來 料 information hiding 念 (inheritance) 來說 類 類 類 類 類 類 行 利 來 (polymorphism) 不 類 數 不 1 2 3 4 類 類 不 類 不 類 5 6 7 // virtual 不見了 #include #include using namespace

More information

新・明解C言語入門編『索引』

新・明解C言語入門編『索引』 !... 75!=... 48 "... 234 " "... 9, 84, 240 #define... 118, 213 #include... 148 %... 23 %... 23, 24 %%... 23 %d... 4 %f... 29 %ld... 177 %lf... 31 %lu... 177 %o... 196 %p... 262 %s... 242, 244 %u... 177

More information

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf (%d, & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9 201 201 21 ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 1 B 1 C 5 D RPG 10 E 10 F 1 G II 1 1 201 201 C 1 # include 2 int main ( void

More information

Microsoft PowerPoint - L17_Inheritance_v4.pptx

Microsoft PowerPoint - L17_Inheritance_v4.pptx C++ Programming Lecture 17 Wei Liu ( 刘 威 ) Dept. of Electronics and Information Eng. Huazhong University of Science and Technology May. 2015 Lecture 17 Chapter 20. Object-Oriented Programming: Inheritance

More information

Microsoft PowerPoint - 20-string-s.pptx

Microsoft PowerPoint - 20-string-s.pptx String 1 String/ 1.: char s1[10]; char *s2; char s3[] = "Chan Tai Man"; char s4[20] = "Chan Siu Ming"; char s5[]={'h','e','l','l','o','\0'; 0 1 2 3 4 5 6 7 8 9 10 11 12 s3 C h a n T a i \0 M a n \0 printf

More information

{ } 09:00~11:00 15:00~17:

{ } 09:00~11:00 15:00~17: { } { } 09:00~11:00 15:00~17:00 0916-126-966 0910-254-214 2 3 { 5 } { } Foting 4 Ina { } Nakaw DIY 5 1. NT200 3 2. 12 1 NT100 1 3. NT150 1-2 Ina 4. NT150 1 14 139 Ina Ina 0913-215799 0939-436078 http://tw.myblog.yahoo.com/guanshan-amisrice/

More information

C/C++ - 数组与指针

C/C++ - 数组与指针 C/C++ Table of contents 1. 2. 3. 4. 5. 6. 7. 8. 1 float candy [ 365]; char code [12]; int states [50]; 2 int array [6] = {1, 2, 4, 6, 8, 10}; 3 // day_mon1.c: # include # define MONTHS 12 int

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 177 [P179] (1) - [P181] [P182] (2) - for [P183] (3) - switch [P184] [P187] [P189] [P194] 178 [ ]; : : int var; : int var[3]; var 2293620 var[0] var[1] 2293620

More information

第1章

第1章 第 7 章 字串 1 本章提要 7.1 前言 7.2 類別與物件 7.3 String 類別 7.4 StringBuffer 類別 7.5 綜合練習 7.6 後記 2 7.1 前言 Java 用 String 類別 (Class) 來處理字串, String 類別是 Java 類別庫內建的類別, 它是一堆已經寫好的程式, 我們可以直接拿來使用字串很像字元型別的一維陣列, 字串裡能存放的資料都屬於字元性質,

More information

C 1

C 1 C homepage: xpzhangme 2018 5 30 C 1 C min(x, y) double C // min c # include # include double min ( double x, double y); int main ( int argc, char * argv []) { double x, y; if( argc!=

More information

Microsoft PowerPoint - Class2.pptx

Microsoft PowerPoint - Class2.pptx C++ 程式初探 II 2015 暑期 C++ 程式 II 大綱 1. 變數 2. 運算式 3. 輸出 4. 條件判斷 5. 迴圈 6. 陣列 2 基本變數型態 整數 位元組 浮點數 位元組 字元 位元組 short 2 float 4 char ( 整數 ) 1 int 2 (4) double 8 long 4 (8) long double 8(10) 位元組 整數値域 浮點數値域 準確度 1-128

More information

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf(%d, &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf(% 2013 ( 28 ) ( ) 1. C pa.c, pb.c, 2. C++ pa.cpp, pb.cpp Compilation Error long long cin scanf Time Limit Exceeded 1: A 10 B 1 C 1 D 5 E 5 F 1 G II 5 H 30 1 2013 C 1 #include 2 int main(void) 3

More information

3.1 num = 3 ch = 'C' 2

3.1 num = 3 ch = 'C' 2 Java 1 3.1 num = 3 ch = 'C' 2 final 3.1 final : final final double PI=3.1415926; 3 3.2 4 int 3.2 (long int) (int) (short int) (byte) short sum; // sum 5 3.2 Java int long num=32967359818l; C:\java\app3_2.java:6:

More information

Microsoft Word - 物件導向編程精要.doc

Microsoft Word - 物件導向編程精要.doc Essential Object-Oriented Programming Josh Ko 2007.03.11 object-oriented programming C++ Java OO class object OOP Ruby duck typing complexity abstraction paradigm objects objects model object-oriented

More information

CC213

CC213 : (Ken-Yi Lee), E-mail: feis.tw@gmail.com 49 [P.51] C/C++ [P.52] [P.53] [P.55] (int) [P.57] (float/double) [P.58] printf scanf [P.59] [P.61] ( / ) [P.62] (char) [P.65] : +-*/% [P.67] : = [P.68] : ,

More information

第3章.doc

第3章.doc 3 3 3 3.1 3 IT Trend C++ Java SAP Advantech ERPCRM C++ C++ Synopsys C++ NEC C C++PHP C++Java C++Java VIA C++ 3COM C++ SPSS C++ Sybase C++LinuxUNIX Motorola C++ IBM C++Java Oracle Java HP C++ C++ Yahoo

More information

Microsoft PowerPoint - Class5.pptx

Microsoft PowerPoint - Class5.pptx C++ 程式初探 V 2015 暑期 ver. 1.0.1 C++ 程式語言 大綱 1. 大量檔案讀取 & 計算 2. 指標 3. 動態記憶體 & 動態陣列 4. 標準函式庫 (STL) vector, algorithm 5. 結構與類別 2 大量檔案讀取 & 計算 若目前有一個程式將讀取純文字文件 (.txt) 中的整數, 並將該文件中的整數有小到大排序後, 儲存到另外一個新的純文字件中 假設有

More information

ebook39-5

ebook39-5 5 3 last-in-first-out, LIFO 3-1 L i n e a r L i s t 3-8 C h a i n 3 3. 8. 3 C + + 5.1 [ ] s t a c k t o p b o t t o m 5-1a 5-1a E D 5-1b 5-1b E E 5-1a 5-1b 5-1c E t o p D t o p D C C B B B t o p A b o

More information

C C

C C C C 2017 3 8 1. 2. 3. 4. char 5. 2/101 C 1. 3/101 C C = 5 (F 32). 9 F C 4/101 C 1 // fal2cel.c: Convert Fah temperature to Cel temperature 2 #include 3 int main(void) 4 { 5 float fah, cel; 6 printf("please

More information

Strings

Strings Polymorphism and Virtual Functions Cheng-Chin Chiang Virtual Function Basics 多 型 (Polymorphism) 賦 予 一 個 函 數 多 種 意 涵, 存 在 於 同 一 類 別 之 內 祖 先 類 別 與 後 代 類 別 間 物 件 導 向 程 式 設 計 基 本 原 理 虛 擬 函 數 (Virtual Function)

More information

Microsoft PowerPoint - 13_Exception.ppt

Microsoft PowerPoint - 13_Exception.ppt 1 第 13 章例外處理 (Exception Handling) 13.1 簡介 13.2 例外處理概觀 13.3 其它錯誤處理技術 13.4 簡單的例外處理範例 - 除 0 錯誤 13.5 重新丟出例外 13.6 函式例外清單 13.7 處理非預期例外 13.8 堆疊返回 13.9 建構子, 解構子, 與例外處理 13.10 例外與繼承 13.11 處理新的錯誤 13.12 auto_ptr 類別與動態記憶體配置

More information

第二章 簡介類別

第二章  簡介類別 Instructor Hsueh-Wen Tseng 曾學文,hwtseng@nchu.edu.tw Textbook C++ 程式設計風格與藝術 (O Reilly). Requirements Assignment x? 100% TAs 第一章概觀 C++ 1-2 二種版本的 C++ 1-5 初步檢視類別 1-1 何謂物件導向程式設計 1-8 C++ 的關鍵字 1-2 二種版本的 C++ //

More information

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien

Scott Effective C++ C++ C++ Roger Orr OR/2 ISO C++ Effective Modern C++ C++ C++ Scoot 42 Bart Vandewoestyne C++ C++ Scott Effective Modern C++ Damien Effective Modern C++ C++ C++ C++11/C++14 C++ Scott Meyers Gerhard Kreuzer Siemens AG Effective Modern C++ Effective Modern C++ Andrei Alexandrescu Facebook Modern C++ Design C++ C++ Nevin Liber DRW Trading

More information

Microsoft PowerPoint - ch09_AEL0080.ppt

Microsoft PowerPoint - ch09_AEL0080.ppt 9 字 串 子曰 : 質勝文則野, 文勝質則史 文質彬彬, 然後君子 論語論語.雍也第六雍也第六 標準的 C++ 提供了方便的程式庫, 讓我們能將 字串 視為獨立的單元, 以進行各種存取和剪接的處理 1/36 字串 9.1 9.2 9.3 9.4 9.5 字串的基本概念字串的輸入與輸出字串的處理字串的指標陣列字串處理在編碼上的應用 2/36 字串的基本概念 字串 (string) 是由雙引號 所包括起來的一串文字

More information

C

C C 2017 4 1 1. 2. while 3. 4. 5. for 6. 2/161 C 7. 8. (do while) 9. 10. (nested loop) 11. 12. 3/161 C 1. I 1 // summing.c: 2 #include 3 int main(void) 4 { 5 long num; 6 long sum = 0L; 7 int status;

More information

untitled

untitled 1 7 7.1 7.2 7.3 7.4 7.5 2 7.1 VFT virtual 7.1 3 1 1. 2. public protected public 3. VFT 4. this const volatile 4 2 5. ( ) ( ) 7.1 6. no-static virtual 7.2 7. inline 7.3 5 3 8. this this 9. ( ) ( ) delete

More information

Microsoft PowerPoint - 09_Inheritance.ppt

Microsoft PowerPoint - 09_Inheritance.ppt 1 第九章 - 繼承 (Inheritance) 9.1 簡介 9.2 基本類別與衍生類別 9.3 受保護的成員 9.4 基本類別與衍生類別的關係 9.5 案例研究 : 三層繼承架構 9.6 衍生類別的建構子與解構子 9.8 public, protected, 與 private 繼承關係 9.9 繼承的優點 2 9.1 簡介 不同類別之間的關係 合成關係與繼承關係 合成關係 ( has-a )

More information

untitled

untitled 1 Outline 料 類 說 Tang, Shih-Hsuan 2006/07/26 ~ 2006/09/02 六 PM 7:00 ~ 9:30 聯 ives.net@gmail.com www.csie.ntu.edu.tw/~r93057/aspnet134 度 C# 力 度 C# Web SQL 料 DataGrid DataList 參 ASP.NET 1.0 C# 例 ASP.NET 立

More information

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023) ( CIP) /. :, 2005. 2 ( ) ISBN 7-5624-3339-9.......... TP311. 1 CIP ( 2005) 011794 : : : : * : : 174 ( A ) :400030 : ( 023) 65102378 65105781 : ( 023) 65103686 65105565 : http: / /www. cqup. com. cn : fxk@cqup.

More information

全国计算机技术与软件专业技术资格(水平)考试

全国计算机技术与软件专业技术资格(水平)考试 全 国 计 算 机 技 术 与 软 件 专 业 技 术 资 格 ( 水 平 ) 考 试 2008 年 上 半 年 程 序 员 下 午 试 卷 ( 考 试 时 间 14:00~16:30 共 150 分 钟 ) 试 题 一 ( 共 15 分 ) 阅 读 以 下 说 明 和 流 程 图, 填 补 流 程 图 中 的 空 缺 (1)~(9), 将 解 答 填 入 答 题 纸 的 对 应 栏 内 [ 说 明

More information

untitled

untitled 1 Outline ArrayList 類 列類 串類 類 類 例 理 MSDN Library MSDN Library 量 例 參 列 [ 說 ] [] [ 索 ] [] 來 MSDN Library 了 類 類 利 F1 http://msdn.microsoft.com/library/ http://msdn.microsoft.com/library/cht/ Object object

More information

Microsoft Word - 01.DOC

Microsoft Word - 01.DOC 第 1 章 JavaScript 简 介 JavaScript 是 NetScape 公 司 为 Navigator 浏 览 器 开 发 的, 是 写 在 HTML 文 件 中 的 一 种 脚 本 语 言, 能 实 现 网 页 内 容 的 交 互 显 示 当 用 户 在 客 户 端 显 示 该 网 页 时, 浏 览 器 就 会 执 行 JavaScript 程 序, 用 户 通 过 交 互 式 的

More information

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2 第 六 讲 指 针 与 字 符 串 1 内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2 指 针 什 么 是 指 针 指 针 的 定 义 与 运 算 指 针 与 一 维 数 组 指 针 数 组 行 指 针 与 二 维 数 组 指 针 与 引 用 指 针 与 函 数 3 指 针 定 义 什 么 是 指 针 指 针 变 量, 简 称 指 针, 用 来 存 放

More information

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2 Chapter 02 變數與運算式 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.2 2.2.1 2.2.2 2.2.3 type 2.2.4 2.3 2.3.1 print 2.3.2 input 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 + 2.4.6 Python Python 2.1 2.1.1 a p p l e b e a r c 65438790

More information

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc References (Section 5.2) Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 15-16, 2010 H.-T. Lin (NTU CSIE) References OOP 03/15-16/2010 0 / 22 Fun Time (1) What happens in memory? 1 i n t i ; 2

More information

第二章 簡介類別

第二章  簡介類別 Instructor 曾學文 hwtseng@nchu.edu.tw http://wccclab.cs.nchu.edu.tw/www/index.php/c ourse/2017-03-20-07-38-21/105-105-2-c TA 王昱彬 第一章概觀 C++ 1-2 二種版本的 C++ 1-5 初步檢視類別 1-1 何謂物件導向程式設計 1-8 C++ 的關鍵字 1-2 二種版本的 C++

More information

概述

概述 OPC Version 1.6 build 0910 KOSRDK Knight OPC Server Rapid Development Toolkits Knight Workgroup, eehoo Technology 2002-9 OPC 1...4 2 API...5 2.1...5 2.2...5 2.2.1 KOS_Init...5 2.2.2 KOS_InitB...5 2.2.3

More information

untitled

untitled A, 3+A printf( ABCDEF ) 3+ printf( ABCDEF ) 2.1 C++ main main main) * ( ) ( ) [ ].* ->* ()[] [][] ** *& char (f)(int); ( ) (f) (f) f (int) f int char f char f(int) (f) char (*f)(int); (*f) (int) (

More information

Strings

Strings Strings Cheng-Chin Chiang Strings Strings 一串儲存在連續記憶體之字元串 表示法 : 以雙引號圍起 This is a book, I love programming, 12234 字串須有一結束字元 \0 (NULL) 在字串尾,NULL 在 C++ 內為一個內定常數值 H i t h e r e! \0 An Array Type of Strings

More information

第七讲 继承与多态

第七讲  继承与多态 第 七 章 继 承 与 派 生 1 本 章 主 要 内 容 的 继 承 成 员 的 访 问 控 制 单 继 承 与 多 继 承 派 生 的 构 造 析 构 函 数 成 员 的 标 识 与 访 问 深 度 探 索 2 的 继 承 与 派 生 的 继 承 与 派 生 保 持 已 有 的 特 性 而 构 造 新 的 过 程 称 为 继 承 在 已 有 的 基 础 上 新 增 自 己 的 特 性 而 产 生

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 函數樣板 (Function Template) 與 類別樣板 (Class Template) 講師 : 洪安 1 資料結構與 C++ 程式設計進階班 為何需要通用函數? (1/2) int abs(int x) { return (x>0)?x:-x; 取名困難不好記 float fabs(float x) { return (x>0)?x:-x; complex cabs(complex x)

More information

運算子多載 Operator Overloading

運算子多載 Operator Overloading 多型 Polymorphism 講師 : 洪安 1 多型 編譯時期多型 ( 靜態多型 ) function overloading 如何正確呼叫同名的函數? 利用參數個數與型態 operator overloading 其實同 function overloading 執行時期多型 ( 或動態多型 ) 如何正確呼叫不同物件的相同名稱的成員函數 利用繼承與多型 2 子類別與父類別物件間的指定 (assignment)

More information

Microsoft Word - 970617cppFinalSolution.doc

Microsoft Word - 970617cppFinalSolution.doc 國 立 台 灣 海 洋 大 學 資 訊 工 程 系 C++ 程 式 設 計 期 末 考 參 考 答 案 姓 名 : 系 級 : 學 號 : 97/06/17 考 試 時 間 :10:00 12:10 試 題 敘 述 蠻 多 的, 看 清 楚 題 目 問 什 麼, 針 對 重 點 回 答 是 很 重 要 的 ; 不 確 定 的 請 一 定 要 當 場 提 出 來, 不 要 白 花 力 氣 在 誤 會

More information

untitled

untitled 1 MSDN Library MSDN Library 量 例 參 列 [ 說 ] [] [ 索 ] [] 來 MSDN Library 了 類 類 利 F1 http://msdn.microsoft.com/library/ http://msdn.microsoft.com/library/cht/ Object object 參 類 都 object 參 object Boxing 參 boxing

More information

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 OJ2 - 参考答案 MASTER 2019 年 5 月 3 日 1 1 PERSON 1 Person 题目描述 编写程序, 定义一个基类 Person, 包含 name 和 age 两个数据成员 ; 再由它派生出学生类 Student 和教师类 Teacher, 其中学生类添加学号 no 数据, 教师类添加职称 title 数据 ; 要求每个类均有构造函数 析构函数和显示数据的函数

More information

untitled

untitled Introduction to Programming ( 數 ) Lecture 3 Spring 2005 March 4, 2005 Lecture 2 Outline 數 料 If if 狀 if 2 (Standard Output, stdout): 料. ((Standard Input, stdin): 料. 類 數 數 數 說 printf 見 數 puts 串 數 putchar

More information

untitled

untitled 1 1.1 1.2 1.3 1.4 1.5 ++ 1.6 ++ 2 BNF 3 4 5 6 7 8 1.2 9 1.2 IF ELSE 10 1.2 11 1.2 12 1.3 Ada, Modula-2 Simula Smalltalk-80 C++, Objected Pascal(Delphi), Java, C#, VB.NET C++: C OOPL Java: C++ OOPL C# C++

More information

踏出C++的第一步

踏出C++的第一步 踏出 C++ 的第一步 講師 : 洪安 1 已經學會的 C 語言基本概念 基本資料型態 變數 基本輸入輸出 控制敘述 選擇控制 迴圈 陣列 函式 指標 字元與字串 結構 檔案處理 2 C v.s. C++ C 函數 程序式語言 Procedural language 結構化程式設計 Structured programming 演算法 Top-down C++ 類別 物件導向程式設計 Object-Oriented

More information

Microsoft PowerPoint - 04-array_pointer.ppt

Microsoft PowerPoint - 04-array_pointer.ppt Array 與 Pointer Array Dynamical Memory Allocation Array( 陣列 ) 陣列是用來存放同樣型態的資料陣列的大小必須在程式中預先設定在程式執行中, 陣列的大小無法改變陣列中的資料是透過索引 (index) 來存取 一維陣列的宣告 type array_name[array_size]; int iarray[100]; /* an integer array

More information

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F 1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET 2.0 2.0.NET Framework.NET Framework 2.0 ( 3).NET Framework 2.0.NET Framework ( System ) o o o o o o Boxing UnBoxing() o

More information

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc 2 5 8 11 0 13 1. 13 2. 15 3. 18 1 23 1. 23 2. 26 3. 28 2 36 1. 36 2. 39 3. 42 4. 44 5. 49 6. 51 3 57 1. 57 2. 60 3. 64 4. 66 5. 70 6. 75 7. 83 8. 85 9. 88 10. 98 11. 103 12. 108 13. 112 4 115 1. 115 2.

More information

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1

C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1 C++ 程序设计 OJ9 - 参考答案 MASTER 2019 年 6 月 7 日 1 1 CARDGAME 1 CardGame 题目描述 桌上有一叠牌, 从第一张牌 ( 即位于顶面的牌 ) 开始从上往下依次编号为 1~n 当至少还剩两张牌时进行以下操作 : 把第一张牌扔掉, 然后把新的第一张放到整叠牌的最后 请模拟这个过程, 依次输出每次扔掉的牌以及最后剩下的牌的编号 输入 输入正整数 n(n

More information

ebook39-6

ebook39-6 6 first-in-first-out, FIFO L i n e a r L i s t 3-1 C h a i n 3-8 5. 5. 3 F I F O L I F O 5. 5. 6 5. 5. 6.1 [ ] q u e n e ( r e a r ) ( f r o n t 6-1a A 6-1b 6-1b D C D 6-1c a) b) c) 6-1 F I F O L I F ADT

More information

第八﹑九章 I/O系統

第八﹑九章 I/O系統 第八 九章 I/O 系統 檔案 I/O 的基本概念 格式化 I/O 建立自訂的嵌入子 建立自訂的擷取子 自訂 I/O 與檔案 檔案 I/O 的基本概念 C++ 對 I/O 的支援是放在 中, 而 Class ios 包括有許多成員函數與變數可以用來控制和監督串流的運作. 要處理檔案 I/O, 心必須 #include, 它包含了 ifstream,ofstream

More information

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class 踩地雷遊戲 高慧君南港高中 開啟專案 MineSweep 任務一 : 產生 30X20 個磚塊編輯 Table 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.arraylist; Write a description of class MyWorld

More information

Microsoft Word - CPE考生使用手冊160524.docx

Microsoft Word - CPE考生使用手冊160524.docx 大 學 程 式 能 力 檢 定 (CPE) 考 生 使 用 手 冊 2016 年 5 月 24 日 這 份 手 冊 提 供 給 參 加 CPE 檢 定 考 試 的 考 生 內 容 包 含 考 試 環 境 的 使 用, 以 及 解 題 時 所 使 用 I/O 的 基 本 知 識 1. 如 欲 報 名 參 加 CPE 考 試, 請 先 於 CPE 網 站 完 成 帳 號 註 冊, 然 後 再 報 名 該

More information

Microsoft Word - 09.數學136-281.docx

Microsoft Word - 09.數學136-281.docx 136. 計 算 梯 型 面 積 (1 分 ) 請 以 JAVA 運 算 式 計 算 下 面 梯 形 面 積, 並 輸 出 面 積 結 果 梯 形 面 積 公 式 為 :( 上 底 + 下 底 ) 高 2 每 一 組 依 序 分 別 輸 入 梯 形 的 上 底 下 底 及 高 的 整 數 輸 出 梯 形 面 積 輸 入 輸 出 94 190 120 99 54 47 137. 計 算 三 角 形 面

More information

A.doc

A.doc A CopyRight: 2003, Mahler Works C > 1.1 C (5 min) 1.1.1C 1.1.21998 ANSIAmerican National Standards InsituteISO International Standards Organzation 1.1.3C/C++C C 1.1.4 C JAVA PLATFORM C 1.2 (5 min)

More information

Microsoft PowerPoint - ch03_AEL0080.ppt

Microsoft PowerPoint - ch03_AEL0080.ppt 3 基本資料型態 能盡物之性, 則可以贊天地之化育 可以贊天地之化育, 則可以與天地矣 中庸中庸.第二十一章第二十一章 1/88 基本資料型態 3.1 3.2 3.3 3.4 3.5 3.6 3.7 整數和浮點數變數和常數算術運算標準數學函數的運算邏輯值及其運算字元與字串位元處理運算 2/88 C++ 的資料型態 C++ 資料型態 基本資料型態 整數 int, short, long 浮點數 float,

More information

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 C++ 程序设计 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1 1 CIRCLE 1 Circle 描述 编写一个圆类 Circle, 实现半径的输入 面积的计算和输出 输入 圆的半径 (double 类型 ) 输出 圆的面积 ( 保留小数点后两位 ) 样例输入 3 样例输出 28.27 提示 圆周率的取值需要比较精确, 以保证计算结果的精度 #include

More information

Microsoft Word - 04_object_Boxing_property_indexer.doc

Microsoft Word - 04_object_Boxing_property_indexer.doc C# 程式設計人員參考 object 型別是.NET Framework 中,System.Object 的別名 您可以將 任何型別的值指派給 object 型別的變數 所有的資料型別, 包括預先定義的和使用者定義的, 都繼承自 System.Object 類別 object 資料型別是物件 Box 目標或來源的型 別 範例下列範例顯示 object 型別的變數如何接受任何資料型別的值, 以及 object

More information

nooog

nooog C : : : , C C,,, C, C,, C ( ), ( ) C,,, ;,, ; C,,, ;, ;, ;, ;,,,, ;,,, ; : 1 9, 2 3, 4, 5, 6 10 11, 7 8, 12 13,,,,, 2008 1 1 (1 ) 1.1 (1 ) 1.1.1 ( ) 1.1.2 ( ) 1.1.3 ( ) 1.1.4 ( ) 1.1.5 ( ) 1.2 ( ) 1.2.1

More information

Microsoft Word - chap10.doc

Microsoft Word - chap10.doc 78 10. Inheritance in C++ 我 們 已 介 紹 了 物 件 導 向 程 式 的 第 一 個 主 要 特 性, 即 程 式 可 模 組 化 成 為 類 別 ( 物 件 ), 類 別 具 有 資 料 封 裝 的 特 性 接 下 來 我 們 要 介 紹 物 件 導 向 程 式 的 另 一 個 主 要 特 性, 那 就 是 類 別 具 有 繼 承 的 功 能 繼 承 就 是 重 複

More information

Microsoft Word - Final Exam Review Packet.docx

Microsoft Word - Final Exam Review Packet.docx Do you know these words?... 3.1 3.5 Can you do the following?... Ask for and say the date. Use the adverbial of time correctly. Use Use to ask a tag question. Form a yes/no question with the verb / not

More information

1 4 1.1 4 1.2..4 2..4 2.1..4 3.4 3.1 Java.5 3.1.1..5 3.1.2 5 3.1.3 6 4.6 4.1 6 4.2.6 5 7 5.1..8 5.1.1 8 5.1.2..8 5.1.3..8 5.1.4..9 5.2..9 6.10 6.1.10

1 4 1.1 4 1.2..4 2..4 2.1..4 3.4 3.1 Java.5 3.1.1..5 3.1.2 5 3.1.3 6 4.6 4.1 6 4.2.6 5 7 5.1..8 5.1.1 8 5.1.2..8 5.1.3..8 5.1.4..9 5.2..9 6.10 6.1.10 Java V1.0.1 2007 4 10 1 4 1.1 4 1.2..4 2..4 2.1..4 3.4 3.1 Java.5 3.1.1..5 3.1.2 5 3.1.3 6 4.6 4.1 6 4.2.6 5 7 5.1..8 5.1.1 8 5.1.2..8 5.1.3..8 5.1.4..9 5.2..9 6.10 6.1.10 6.2.10 6.3..10 6.4 11 7.12 7.1

More information

Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx

Microsoft PowerPoint - 8. 运算符重载 Operator Overloading.pptx 运算符重载 Operator Overloading class Point { public: ; double x_, y_; Why Operator Overloading? Point (double x =0, double y = 0):x_(x),y_(y) { int main(){ Point a(1., 2), b(3,4); Point c = a + b; return 0;

More information

Microsoft PowerPoint - ch04_AEL0080.ppt

Microsoft PowerPoint - ch04_AEL0080.ppt 4 選擇 在正常的情況下, 電腦程式的執行是以敘述的排列次序逐步處理的 使用控制架構 (control structures) 可以改變這種既定的先後次序, 讓程式得以進行更複雜的運算, 或以更簡潔的指令來實現演算法 1/42 選擇 4.1 演算法的描述方式 4.2 變數的運用範圍 (Scope of variables) 4.3 if- 敘述 4.4 巢狀 if- 敘述 (Nested if statements)

More information

星星排列 _for loop Protected Sub Page_Load(ByVal sender As Object, ByVal e As Dim h As Integer = 7 'h 為變數 ' Dim i, j As Integer For i = 1 To h

星星排列 _for loop Protected Sub Page_Load(ByVal sender As Object, ByVal e As Dim h As Integer = 7 'h 為變數 ' Dim i, j As Integer For i = 1 To h 資訊系統與實習 製作 : 林郁君 一 2009.09.28 9X9 'button 被按下後 ' Dim i, j As Integer For i = 1 To 9 'i 從 1 到 9' For j = 1 To 9 'j 從 1 到 9' If j * i < 10 Then ' 如果 j 乘上 i 是為個位數 ' Response.Write(i & "*" & j & " =" & i *

More information

Chapter 9: Objects and Classes

Chapter 9: Objects and Classes Fortran Algol Pascal Modula-2 BCPL C Simula SmallTalk C++ Ada Java C# C Fortran 5.1 message A B 5.2 1 class Vehicle subclass Car object mycar public class Vehicle extends Object{ public int WheelNum

More information

Chapter12 Derived Classes

Chapter12   Derived Classes 继 承 -- 派 生 类 复 习 1. 有 下 面 类 的 说 明, 有 错 误 的 语 句 是 : class X { A) const int a; B) X(); C) X(int val) {a=2 D) ~X(); 答 案 :C 不 正 确, 应 改 成 X(int val) : a(2) { 2. 下 列 静 态 数 据 成 员 的 特 性 中, 错 误 的 是 A) 说 明 静 态 数

More information

Factory Methods

Factory Methods Factory Methods 工 厂 方 法 eryar@163.com 摘 要 Abstract: 本 文 主 要 是 对 API Design for C++ 中 Factory Methods 章 节 的 翻 译, 若 有 不 当 之 处, 欢 迎 指 正 关 键 字 Key Words:C++ Factory Pattern 一 概 述 Overview 工 厂 方 法 是 创 建 型 模

More information

2011 Annual Report

2011 Annual Report 麥當勞叔叔之家兒童慈善基金會 2011 年度報告 Ronald McDonald House Charities 2011 Annual Report To create, find and support programs that directly improve the health and well being of children. The mission of Ronald McDonald

More information

科学计算的语言-FORTRAN95

科学计算的语言-FORTRAN95 科 学 计 算 的 语 言 -FORTRAN95 目 录 第 一 篇 闲 话 第 1 章 目 的 是 计 算 第 2 章 FORTRAN95 如 何 描 述 计 算 第 3 章 FORTRAN 的 编 译 系 统 第 二 篇 计 算 的 叙 述 第 4 章 FORTRAN95 语 言 的 形 貌 第 5 章 准 备 数 据 第 6 章 构 造 数 据 第 7 章 声 明 数 据 第 8 章 构 造

More information

Microsoft Word - JAVA Programming Language Homework I ans

Microsoft Word - JAVA Programming Language Homework I ans JAVA Programming Language Homework I - OO concept Student ID: Name: 1. Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class? A. Declare

More information

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2 PowerBuilder 9 PowerBuilder Native Interface(PBNI) PowerBuilder 9 PowerBuilder C++ Java PowerBuilder 9 PBNI PowerBuilder Java C++ PowerBuilder NVO / PowerBuilder C/C++ PowerBuilder 9.0 PowerBuilder Native

More information

无类继承.key

无类继承.key 无类继承 JavaScript 面向对象的根基 周爱 民 / aimingoo aiming@gmail.com https://aimingoo.github.io https://github.com/aimingoo rand = new Person("Rand McKinnon",... https://docs.oracle.com/cd/e19957-01/816-6408-10/object.htm#1193255

More information

Ps22Pdf

Ps22Pdf C ( CIP) C /. :, 2001. 7 21 ISBN 7-5624 -2355-5. C........ C. TP312 CIP ( 2001 ) 034496 C * * : 7871092 1 /16 : 14. 25 : 356 20017 1 20017 1 : 1 6 000 ISBN 7-5624-2355-5 / TP311 : 21. 00 C, C,,,, C,, (

More information

wedding calendar

wedding calendar W TAIPEI PRESENTS YOUR WEDDING YOUR WAY W SELECTED WEDDING DAY W JANUARY FEBRUARY MARCH APRIL 4 5 6 4 5 6 7 7 8 9 10 11 12 13 8 9 10 11 12 13 14 14 15 16 17 18 19 20 15 16 17 18 19 20 21 21 22 23 24 25

More information

Microsoft PowerPoint - 10 模板 Template.pptx

Microsoft PowerPoint - 10 模板 Template.pptx 模板 Tempalte 泛型编程的需要 Why Templates? 设想你对整数类型实现了一个排序算法 : void sort(int *is,int n); 用该函数可以对实 复数或工资单排序吗? 模板可以复用源代码 - 泛型编程. inline void Swap( int &x, int &y){ int t = x; x = y; y =t; inline void Swap(double

More information

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更 AX5000 Version 1.0 2006 年 9 錄...1 說...2 說...3...4 說...5 六...6 6.1 率...7 6.2 POST PAY...8 6.3 PREPAY DEPOSIT...9 6.4...10 6.5...11 更...12...12 LCD IC LED Flash 更 兩 RJ11 ( ) DC ON OFF ON 狀 狀 更 OFF 復 狀 說

More information