C++

Size: px
Start display at page:

Download "C++"

Transcription

1 第五講 泛型程式設計 講師 : 李根逸 (Ken-Yi Lee), [email protected]

2 130 課程 大綱 模版 (template) 簡介實作多個相似內容的函式函式模版類別模版 自製陣列模版 static 的 用法修飾全域變數或 一般函式修飾區域變數修飾類別成員 C++ STL 的陣列模版 (std::vector)

3 131 實作多個相似內容的函式 [1] 寫 C/C++ 程式時常常會遇到許多實作內容相似卻不完全 一樣的函式 在以前, 我們會將這些函式不 一樣的部份 用函式參數的 方式來傳遞, 進 而寫成同 一個函式 : 例如 : void Set0(int &x) { x = 0; } void Set5(int &x) { x = 5; } 可以設計成 : // Set0(a); // Set5(a); 這樣設計的好處我們只要寫 一次函式定義, 比較容易統 一相似的部分 缺點是每次呼叫的時候都可能需要複製 一次 v void Set(int &x, int v) { // Set(a, 3); x = v; // Set(a, 5); }

4 132 實作多個相似內容的函式 [2] 但是依照之前的作法, 相似內容函式間不同的地 方必須要能當做參數傳 入, 所以會有些限制 : void Set(double &x, double v) { x = v; // Set(a, 5); } void Set(int &x, int v) { x = v; // Set(b, 5); } 要怎麼把這兩個函式寫在 一起? 這兩個函式不 一樣的地 方是參數的 型態 而不是參數的 值

5 133 模版 [1] template ( 模版 ) 是進 行泛型程式設計 (generic programming) 的主要語法 : 使 用模版可以定義 一組相似內容的類別或函式 在 C 裡 面可能使 用 #define 來模仿這個功能 ( 搜尋取代 ) 宣告或定義語法 : template< 模版參數型態模版參數名稱,...> 函式宣告定義或類別宣告定義例如, 定義函式模版 : template<typename T> void Set(T &x, T v) { x = v; } 呼叫模版產 生的函式 : Set<int>(a, 5); Set<double>(b, 5.5); typename 表 示這裡的 T 是個型態名稱 語法上這裡也可以使 用 class, 意義相同 這裡的 T 是我們 自 己取的模版參數名稱 [ 範例 ] template.cpp

6 134 模版 [2] 函式模版本 身不是函式, 而是函式的模版 範本 在編譯的時候, 編譯器視程式碼中使 用的情況 而 自動依據模版產 生需要的函式 : template<typename T> void Swap(T &a, T &b) { T t = a; a = b; b = t; } 呼叫函式時, 如果可以從函式參數直接判斷模版參數值的話, 可以省略模版參數不 用給 : int a = 3, b = 5; double c = 3.5; Swap<int>(a, b); // 會 自動產 生 一個叫 Swap<int> 的函式 Swap<>(a, b); Swap(a, b); // 這也可以 Swap(a, c); // [ 編譯錯誤 ] Swap 是函式模版, 而 Swap<int> 或 Swap<double> 等就是依據這個模組產 生的函式們 在 <algorithm> 內有 std::swap 這個函式模版 // 我們可以從 a, b 的型態判斷 Type [ 範例 ] swap.cpp

7 135 練習 求最 大值的函式 試著 用模組寫 一個可以求各種資料類型的最 大值函式 請修改原本的 Max 函式成為 一個函式 模版 可以使 用這個函式模版去計算各種不同資料型態的最 大值 [ 練習 ] ex5a.cpp

8 136 類別模版 我們也可以定義類別的模版 : template<typename T> class Grade { public: Grade(); Grade(const T &v); 為什麼我們使 用 const T & 而不是 T void Set(const T &v); 作為參數或回傳值型態? const T &Get() const; const Grade<T> operator+(const Grade<T> &rhs) const; private: T data_; }; Grade<int> 是個 用 int 型態儲存成績的類別 而 Grade<double> 是個 用 double 型態儲存成績的類別 在定義類別模版的成員函式時, 很多時候可以省略模版參數 例如 Grade<T> 可寫成 Grade 但為了避免困惑, 我們這 門課 大部分情況都不省略 特別 建構式名稱還是 Grade 而不是 Grade<T> [ 範例 ] grade.cpp

9 137 範例 自製陣列模版 [1] template<typename ElemType> class Array { 使 用模版可以更有延伸性 public: Array(); Array(const Array<ElemType> &rhs); explicit Array(int n); ~Array(); int Size() const; ElemType &At(int i); const ElemType &At(int i); ElemType &operator[](int i); const ElemType &operator[](int i) const; Array<ElemType> &operator=(const Array<ElemType> &rhs); }; [ 範例 ] array_1.cpp

10 138 範例 自製陣列模版 [2] 如果陣列的 大 小在編譯期就可以決定的話, 我們可以將陣列 大 小也設定為 一個模版參數, 可以簡化程式碼和可能增加效率 : template<typename ElemType, int knumofelems> class Array { public: int Size() const; ElemType &At(int i); const ElemType &At(int i); const; ElemType &operator[](int i); const ElemType &operator[](int i) const; private: ElemType data[knumofelems]; }; 大三法則 ( 解構式 複製建構式和指定運算 子 ) 到哪去了? Array<int, 10> a; a 是 一個 大 小為 10 的 int 陣列物件 [ 範例 ] array_2.cpp

11 139 static 的 用法 [1] static 可以修飾全域變數或函式的宣告或定義 : 該全域變數或函式只能在同個檔案內使 用 在 C++ 中, 這功能可以使 用匿名命名空間取代 int a; // 在所有檔案都可以使 用 static int b; // 就只有這個檔案可以使 用 namespace { int c; // 就只有這個檔案可以使 用 }; int main() { cout << a << endl << b << endl << c << endl; return 0; }

12 140 static 的 用法 [2] static 可以修飾區域變數的定義 : 該區域變數只會在程式中產 生與死亡 一次 在第 一次定義的時候初始化, 在程式結束的時候死亡 int &static_var() { static int a = 3; // 在整個程式中這只會做 一次 return ++a; } 思考 這裡可以回傳參考嗎? int main() { for (int i = 0; i < 10; ++i) { cout << static_var() << endl; } return 0; } [ 範例 ] static_1.cpp

13 141 static 的 用法 [3] static 可以修飾類別內的成員宣告 : 該成員變數或成員函式就成為類別變數或類別函式, 屬於該類別 而不是個別物件, 可以直接使 用類別名稱去存取 ( 類別名稱 :: 變數或函式名稱 ) 相對地, 類別函式內部不能存取 一般物件的成員變數或成員函式 class Grade { public: static const int kmaxgrade = 100; static const int kmingrade = 0; int data; }; int main() { cout << Grade::kMaxGrade << << Grade::kMinGrade << endl; cout << Grade::data << endl; // [ 編譯錯誤 ] return 0; } 我們不 用產 生任何物件就可以讀取類別變數或執 行類別函式 [ 範例 ] static_2.cpp

14 142 範例 自製陣列模版 [3] 成員函式的運作不牽涉到物件的成員變數或其他成員函式, 可以宣告其為 static, 也就是類別函式 : template<typename ElemType, int knumofelems> class Array { public: static int Size(); ElemType &At(int i); const ElemType &At(int i) const; ElemType &operator[](int i); const ElemType &operator[](int i) const; private: ElemType data[knumofelems]; }; Array<int, 10>::Size() 是 10 [ 範例 ] array_3.cpp

15 143 練習 可變 大 小陣列的模版 試著做 一個可變 大 小的陣列模版 : template<typename ElemType> class Vector { public: Vector(); explicit Vector(int n); Vector(const Vector<ElemType> &rhs);... }; [ 練習 ] ex5b.cpp

16 144 範例 使 用 STL 的陣列 在 C++ STL 内有 std::vector<type>, 是可變 大 小陣列的模版 需要 #include <vector> 請試著練習查看說明 文件 std::vector<type> 的提供的操作 : 快速存取任意位置的元素 (operator[]) 快速新增或刪除元素於尾端 (push_back, pop_back) 可任意改變 大 小 (resize), 但可能需重新配置記憶體 可以清除全部的陣列內容 (clear) 事先保留 足夠的空間 (reserve) 可以避免重新配置記憶體 新增 (insert) 或刪除 (erase) 任意位置的元素 比較慢 這兩個操作需要 用到迭代器 (iterator), 我們在後 面 一章節再解釋 [ 範例 ] stl.cpp

17 145 練習 通訊錄 使 用 std::vector<type> 來完成 一個通訊錄程式 使 用 std::string 儲存名字與電話號碼 struct Record { string name; // 姓名 string mobile; // 電話號碼 }; [ 練習 ] ex5c.cpp

18 146

19 147

20 148

21 149

22 150

23 151

24 152

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

運算子多載 Operator Overloading

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

More information

C/C++ Programming

C/C++ Programming 265 第九講 結構 講師 : 李根逸 (Ken-Yi Lee), E-mail: [email protected] 266 課程 大綱 結構 (struct) 結構宣告 [P.267] 結構定義 [P.268] 結構變數宣告 [P.269] 結構變數的初始化 [P.272] 存取結構成員 [P.244] 傳送 大型資料型態參數 [P.277] 267 結構宣告 結構是 一種衍 生的 自訂資料型態,

More information

Microsoft PowerPoint - C_Structure.ppt

Microsoft PowerPoint - C_Structure.ppt 結構與其他資料型態 Janet Huang 5-1 結構的宣告 struct 結構名稱 struct 結構名稱變數 1, 變數 2,, 變數 m; struct 結構名稱 變數 1, 變數 2,, 變數 m; student; student; 5-2 1 結構變數初值的設定 struct 結構名稱 struct 結構名稱變數 = 初值 1, 初值 2,, 初值 n student="janet","1350901",100,95

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

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

CC213

CC213 : (Ken-Yi Lee), E-mail: [email protected] 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

!249 第 八講 進階指標 講師 : 李根逸 (Ken-Yi Lee),

!249 第 八講 進階指標 講師 : 李根逸 (Ken-Yi Lee), 249 第 八講 進階指標 講師 : 李根逸 (Ken-Yi Lee), E-mail: [email protected] 250 課程 大綱 陣列的複製 [P.252] 字串的特殊性 [P.255] const 修飾字 [P.256] 指標陣列 [P.257] 字串陣列 [P.258] 指標與 二維陣列 [P.260] 動態記憶體配置與釋放 C 語 言中動態記憶體的配置 [P.266] C 語

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++入門編 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

踏出C++的第一步

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

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

CHAPTER VC#

CHAPTER VC# 1. 2. 3. 4. CHAPTER 2-1 2-2 2-3 2-4 VC# 2-5 2-6 2-7 2-8 Visual C# 2008 2-1 Visual C# 0~100 (-32768~+32767) 2 4 VC# (Overflow) 2-1 2-2 2-1 2-1.1 2-1 1 10 10!(1 10) 2-3 Visual C# 2008 10! 32767 short( )

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

第二章 簡介類別

第二章  簡介類別 Instructor Hsueh-Wen Tseng 曾學文,[email protected] 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

FY.DOC

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

More information

提问袁小兵:

提问袁小兵: C++ 面 试 试 题 汇 总 柯 贤 富 管 理 软 件 需 求 分 析 篇 1. STL 类 模 板 标 准 库 中 容 器 和 算 法 这 部 分 一 般 称 为 标 准 模 板 库 2. 为 什 么 定 义 虚 的 析 构 函 数? 避 免 内 存 问 题, 当 你 可 能 通 过 基 类 指 针 删 除 派 生 类 对 象 时 必 须 保 证 基 类 析 构 函 数 为 虚 函 数 3.

More information

運算子多載 Operator Overloading

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

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

Excel VBA Excel Visual Basic for Application

Excel VBA  Excel Visual Basic for Application Excel VBA Jun5,00 Sub 分頁 () Dim i As Integer Dim Cname As String Dim Code As Variant Set score=thisworkbook.sheets("sheet") Code=Array(" 專北一 "," 專北二 "," 專北三 "," 專桃園 "," 專桃竹 "," 專中苗 ", " 專台中 "," 專台南 ","

More information

第二章 簡介類別

第二章  簡介類別 Instructor 曾學文 [email protected] 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

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

tmp b 08d6780 you tmp b you 08d a swap a hello 08d6770 hello 3 08d6770 MyString 的 copy functions 已設計為深拷貝 ( 很對 ), 如何讓它另擁有一份淺拷貝版本 ( 所謂 move 版本 )

tmp b 08d6780 you tmp b you 08d a swap a hello 08d6770 hello 3 08d6770 MyString 的 copy functions 已設計為深拷貝 ( 很對 ), 如何讓它另擁有一份淺拷貝版本 ( 所謂 move 版本 ) hello hello 08d67b0 temp b 08d67a0 you 08d67b0 temp b 08d67a0 you T temp(a); 1 a 例 MyString without move swap a a = b; 2 Until 2011 08d6790 namespace std { template void swap(t& a, T& b) {

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

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e

單步除錯 (1/10) 打開 Android Studio, 點選 Start a new Android Studio project 建立專案 Application name 輸入 BMI 點下 Next 2 P a g e Android Studio Debugging 本篇教學除了最基本的中斷點教學之外, 還有條件式中斷的教學 條件式中斷是進階的除錯技巧, 在某些特定情況中, 我們有一個函數可能會被呼叫數次, 但是我們只希望在某種條件成立時才進行中斷, 進而觀察變數的狀態 而條件式中斷這項技巧正是符合這項需求 本教學分兩部分 單步除錯 (Page2~11, 共 10) 條件式中斷點 (Page12~17, 共 6)

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 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

CC213

CC213 : (Ken-Yi Lee), E-mail: [email protected] 9 [P.11] : Dev C++ [P.12] : http://c.feis.tw [P.13] [P.14] [P.15] [P.17] [P.23] Dev C++ [P.24] [P.27] [P.34] C / C++ [P.35] 10 C / C++ C C++ C C++ C++ C ( ) C++

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

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

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

Strings

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

More information

Microsoft Word - 投影片ch11

Microsoft Word - 投影片ch11 Java2 JDK5.0 教學手冊第三版洪維恩編著博碩文化出版書號 pg20210 第十一章抽象類別與介面 本章學習目標認識抽象類別學習介面的使用認識多重繼承與介面的延伸 抽象類別與介面 11-2 11.1 抽象類別 抽象類別的目的是要依據它的格式來修改並建立新的類別 11.1.1 定義抽象類別 定義抽象類別的語法如下 : abstract class 類別名稱 { 宣告資料成員 ; // 定義抽象類別

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

第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 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

Strings

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

More information

Microsoft Word - 第3章.doc

Microsoft Word - 第3章.doc Java C++ Pascal C# C# if if if for while do while foreach while do while C# 3.1.1 ; 3-1 ischeck Test() While ischeck while static bool ischeck = true; public static void Test() while (ischeck) ; ischeck

More information

投影片 1

投影片 1 資料庫管理程式 ( 補充教材 -Part2) 使用 ADO.NET 連結資料庫 ( 自行撰寫程式碼 以實現新增 刪除 修改等功能 ) Private Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click ' 宣告相關的 Connection

More information

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 4: 4 月 19 日晚 9 点 复习 类的复用 组合 (composition): has-a 关系 class MyType { public int i; public double d; public char c; public void set(double x) { d

More information

IO

IO 1 C/C++ C FILE* fscanf fgets fread fprintf fputs fwrite C++ ifstream ofstream >>

More information

1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity

1 C++ 2 Bjarne Stroustrup C++ (system programming) 6 (infrastructure) C++ 7 Herb Sutter 8 C++ (efficiency) (flexibility) 9 (abstraction) (productivity 1 C++ 1 C++ Primer C++ ([email protected]) 2012-7-11 Creative Commons - - 3.0 Unported (cc by-nc-nd) http://creativecommons.org/licenses/by-nc-nd/3.0/ 1 C++ 2009 Stanley Lippman C++ C++ Java/C#/Python

More information

Microsoft PowerPoint - 08_Class

Microsoft PowerPoint - 08_Class 第八章認識類別 認識類別的基本架構在類別裡使用資料成員與成員函數學習 this 關鍵字的用法在類別裡設計 method 的多載學習如何使用類別裡的公有與私有成員 1 8.1 認識類別 類別的基本概念 每一個 Java 程式, 至少會存在一個或一個以上的類別 類別是由資料成員與成員函數封裝而成 矩形有寬 (width) 與高 (height) 兩個基本屬性 根據這兩個屬性, 可求出面積 (area)

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

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new ListView 自訂排版 主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new int[]{r.drawable.dog1, R.drawable.dog2,

More information

<4D6963726F736F667420576F7264202D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

<4D6963726F736F667420576F7264202D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF> 全 国 计 算 机 技 术 与 软 件 专 业 技 术 资 格 ( 水 平 考 试 2009 年 上 半 年 软 件 设 计 师 下 午 试 卷 ( 考 试 时 间 14:00~16:30 共 150 分 钟 请 按 下 述 要 求 正 确 填 写 答 题 纸 1. 在 答 题 纸 的 指 定 位 置 填 写 你 所 在 的 省 自 治 区 直 辖 市 计 划 单 列 市 的 名 称 2. 在 答

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

CC213

CC213 : (Ken-Yi Lee), E-mail: [email protected] 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

Microsoft Word - ACL chapter02-5ed.docx

Microsoft Word - ACL chapter02-5ed.docx 第 2 章神奇的質數 2.1.1 什麼是質數 1 1 1 打下好基礎 - 程式設計必修的數學思維與邏輯訓練 1 1 0 10 2 3 5 7 4 6 8 9 10 4 10000 1229 1000 168 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131

More information

新・解きながら学ぶJava

新・解きながら学ぶJava 481! 41, 74!= 40, 270 " 4 % 23, 25 %% 121 %c 425 %d 121 %o 121 %x 121 & 199 && 48 ' 81, 425 ( ) 14, 17 ( ) 128 ( ) 183 * 23 */ 3, 390 ++ 79 ++ 80 += 93 + 22 + 23 + 279 + 14 + 124 + 7, 148, 16 -- 79 --

More information

我的生命哲學 五觀三一 陳學霖

我的生命哲學 五觀三一    陳學霖 無 可 取 代 的 寶 石 商 設 系 1011413031 吳 孟 蓁 在 每 個 人 心 中, 都 有 一 顆 最 寶 貝 也 最 珍 貴 的 寶 石, 願 意 用 盡 自 己 的 生 命 去 守 護 著, 不 願 意 它 受 到 任 何 傷 害, 而 我 最 想 保 護 也 最 深 愛 的 寶 石, 就 是 我 的 家 人 家 人, 是 我 的 生 命 來 源, 也 是 我 的 活 力 來 源,

More information

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1

C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1 C++ 程序设计 实验 2 - 参考答案 MASTER 2017 年 5 月 21 日 1 1 CRECT 类 1 CRect 类 设计矩形类, 包含 长度和宽度信息 基本构造函数 基础属性的访问接口 ( 读 / 写, Read/Write, Get/Set) 计算周长和面积 ( 注 : 基本构造函数, 一个无参数的默认构造函数, 以及一个初始化数据成员的构造函数如果数据成员的初始化有多种形式, 就提供多个构造函数

More information

untitled

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

More information

PowerPoint Presentation

PowerPoint Presentation C++ 與資料結構 NTU CSIE 大綱 使用類別 (Class) 建立資料結構 使用繼承 (Inheritance) 建立資料結構 C++ 物件導向 以物件為基礎的程式設計, 將程式中互動的單元視為一個個的物件 封裝 (Encapsulation) 封裝物件資訊是第一步, 您要瞭解如何使用類別定義物件, 像是定義物件的屬性 方法 ( 行為 ) 等等, 類別是建構物件時所依賴的規格書 Example

More information

Microsoft PowerPoint - Class4.pptx

Microsoft PowerPoint - Class4.pptx C++ 程式初探 IV 2015 暑期 ver. 1.0.2 C++ 程式 IV 大綱 1. 時間函式 2. 格式化輸出 3. 遞迴函式 (recursion) 4. 字串 5. 字串轉型 2 補充語法 時間計算 引入標頭檔 #include #include #include #include using namespace

More information

Strings

Strings Templates Cheng-Chin Chiang Basic Concepts 死會 的函數與類別 定義一個函數時傳遞的參數型態都被固定了 定義類別時, 資料成員的型態都被固定了 劈腿 的函數與類別 將函數與類別設計得 樣樣型態通通吃 型態便成了定義時的一種參數 ( 提供函數或類別定義時的一張空白支票 ) 資料型態 編譯時活會 執行時死會 Function Templates 範例 : void

More information

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 C C [email protected] C C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C 40 30 10 Project 30 C Project 3 60 Project 40 Week3 C Week5 Week5 Memory & Pointer

More information

The Embedded computing platform

The Embedded computing platform 嵌入式系統及實驗 Embedded System and Experiment 詹曉龍 長庚大學電機系 Java 的類別與物件 : 宣告類別 建構子 public class Customer { private String name; private String address; // Customer 類別宣告 // 成員資料 public int age; // 建構子 : 使用參數設定成員資料初始值

More information

Microsoft PowerPoint - 07-overloaded.ppt

Microsoft PowerPoint - 07-overloaded.ppt Overloaded Functions 前言 處理多載函式宣告的規則 處理多載函式呼叫的規則 多載函式與 scope 函式呼叫的議決 前言 C 語言規定 : 函式的名稱不可相同 這樣的規定使得我們必須為功能相近但參數型態相異的函式取不同的名稱, 譬如 : int imax (int, int); double dmax (double, double ); // max function for

More information

Microsoft PowerPoint - 13_指標、資料傳遞2.pptx

Microsoft PowerPoint - 13_指標、資料傳遞2.pptx 1 2 指標 Lecture 13 指標函式呼叫的資料傳遞 (III) 傳址指標與陣列 Pointer 3 4 指標 / 指位器 (Pointer) 變數 int a; 整數型別, 名稱為 a 變數是為了使用記憶體資源來儲存資料與進行運算 所有的變數都佔有記憶體空間 記憶體 可視為一個很大的一維陣列, 單位是 byte 問題 一個 4KB 的電腦, 其記憶體位置 ( 編號 ) 從 0 至? 4 x

More information

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6: Chapter 15. Suppressed Exception CH14 Finally Block Java SE 7 try-with-resources JVM cleanup try-with-resources JVM cleanup cleanup Java SE 7 Throwable getsuppressed Throwable[] getsuppressed() Suppressed

More information

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 OOP with Java Yuanbin Wu cs@ecnu OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课 复习 Java 包 创建包 : package 语句, 包结构与目录结构一致 使用包 : import restaurant/ - people/ - Cook.class - Waiter.class - tools/ - Fork.class

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

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00

840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00 Excel - - Excel - -4-5 840 提示 Excel - Excel -- Excel (=) Excel ch0.xlsx H5 =D5+E5+F5+G5 (=) = - Excel 00 ( 0 ) 智慧標籤 相關說明提示 -5 -- Excel 4 5 6 7 8 + - * / % ^ = < >= & 9 0 (:) (,) ( ) Chapter - :,

More information

The golden pins of the PCI card can be oxidized after months or years

The golden pins of the PCI card can be oxidized after months or years Q. 如何在 LabWindows/CVI 編譯 DAQ Card 程式? A: 請參考至下列步驟 : 步驟 1: 安裝驅動程式 1. 安裝 UniDAQ 驅動程式 UniDAQ 驅動程式下載位置 : CD:\NAPDOS\PCI\UniDAQ\DLL\Driver\ ftp://ftp.icpdas.com/pub/cd/iocard/pci/napdos/pci/unidaq/dll/driver/

More information

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

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc 2 5 8 11 0 1. 13 2. 15 3. 18 1 1. 22 2. 25 3. 27 2 1. 35 2. 38 3. 41 4. 43 5. 48 6. 50 3 1. 56 2. 59 3. 63 4. 65 5. 69 13 22 35 56 6. 74 7. 82 8. 84 9. 87 10. 97 11. 102 12. 107 13. 111 4 114 1. 114 2.

More information

105A 資管一程式設計實驗 06 函式定義謝明哲老師 2 程式設計實驗 6.3: 自行定義一個可以接受兩個整數並傳回其最大公因數的函式, 接著利用該函式自 行定義一個可以接受兩個整數並傳回其最小公倍數函式 // gcd_fcn.cpp int gcd(int m,

105A 資管一程式設計實驗 06 函式定義謝明哲老師 2 程式設計實驗 6.3: 自行定義一個可以接受兩個整數並傳回其最大公因數的函式, 接著利用該函式自 行定義一個可以接受兩個整數並傳回其最小公倍數函式 // gcd_fcn.cpp int gcd(int m, 105A 資管一程式設計實驗 06 函式定義謝明哲老師 [email protected] 1 程式設計實驗 06 函式定義 模擬問題 03 在模擬問題 02, 小組已完成擬定一個與學習或日常生活有關的問題, 並依據在 Ch5 所 學到的流程控制與檔案存取技術發展小組的第二版個別化資訊服務程式 現在請小組對第二版程式的 結構進行分析, 檢查是否有哪些功能可以使用在 Ch6 所學到的函式定義來加以模組化,

More information

C++11概要 ライブラリ編

C++11概要 ライブラリ編 C++11 Egtra 2012 6 23 1 Boost. #9 1.1 C++11 1.2 http://creativecommons.org/licenses/by-sa/2.1/jp/ - 2.1 2 Misc 2.1 C++11 unique_ptr shared_ptr // #include std::unique_ptr up(new int(1));

More information

chap07.key

chap07.key #include void two(); void three(); int main() printf("i'm in main.\n"); two(); return 0; void two() printf("i'm in two.\n"); three(); void three() printf("i'm in three.\n"); void, 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# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii 前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii C# 7 More Effective C# C# C# C# C# C# Common Language Runtime CLR just-in-time

More information

C++ 程序设计 实验 1 - 参考答案 MASTER 2017 年 5 月 21 日 1

C++ 程序设计 实验 1 - 参考答案 MASTER 2017 年 5 月 21 日 1 C++ 程序设计 实验 1 - 参考答案 MASTER 2017 年 5 月 21 日 1 1 简单图形 1 简单图形 输入图形的行数 ( 如下图 7 行 ), 输出如下图所示图形 * *** ***** ******* ***** *** * 2 1 简单图形 1 #inc lude 2 using namespace std ; 3 4 // 注意变量命名的方式 5 //

More information

Microsoft Word - 01.DOC

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

More information

105Tr_CIS1

105Tr_CIS1 准考證號碼 : 國立臺中教育大學 105 學年度學士班日間部轉學招生考試 計算機概論試題 適用學系 : 資訊工程學系二 三年級 一 選擇題 (40%, 每題 2%) 1. 在物件導向程式設計中, 一個抽象類別 (Abstract Class) 為 A. 一個沒有方法 (method) 的類別 B. 一個不能被繼承 (inherit) 的類別 C. 一個不能被實體化 (instantiated) 的類別

More information