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

Slides:



Advertisements
Similar presentations
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Advertisements

Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
Introduction to Java Programming Lecture 13 Classes I OO Programming.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
T-SQL 運算子介紹 11/29. 運算子的總類 指定運算子 算術運算子 比較運算子 邏輯運算子 位元運算子 字串連結運算子 單一運算元運算子.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
1.1 線性方程式系統簡介 1.2 高斯消去法與高斯-喬登消去法 1.3 線性方程式系統的應用(-Skip-)
五小專案 黃詩晴 章乃云. 目錄 計算機 智慧盤 拼圖 記憶大挑戰 數學題庫 心得 參考文獻.
1 Advanced Chemical Engineering Thermodynamics Chapter 1 The phase equilibrium problem.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
1 真理大學運輸管理學系 實務實習說明 目錄  實務實習類別  實務實習條例  校外實習單位  實務實習成績計算方式  校外實習甄選 / 自洽申請流程  附錄:相關表格.
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
第 13 章 常數、靜態成員與朋友關 係 13-1 常數物件與成員 13-1 常數物件與成員 13-2 物件成員與巢狀類別 13-2 物件成員與巢狀類別 13-3 靜態成員資料與函數 13-3 靜態成員資料與函數 13-4 朋友關係 13-4 朋友關係.
STAT0_corr1 二變數的相關性  變數之間的關係是統計研究上的一大目標  討論二分類變數的相關性,以列聯表來表示  討論二連續隨機變數時,可以作 x-y 散佈圖觀察它 們的關係強度  以相關係數來代表二者關係的強度.
第 18 章 名稱空間與例外處理 18-1 名稱空間 18-1 名稱空間 18-2 例外處理 18-2 例外處理.
程式註解說明. 2 程式註解格式 塊狀註解 對檔案、 class 、 method 、資料結構、一段程式 …. 等程式區塊 做說明。 第一行的開頭必需為 “/*” 且沒有其他文字,最後一行的開頭 必需以 “*/” 做為結束,在中間每一行的開頭都必需是一個 “*” 。 單行註解 佔據一整行的說明。 以.
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
溫丹瑋 10/29/2004 No. 7 查詢 溫丹瑋 10/29/2004.
民意調查的分析 II 蔡佳泓 政大選舉研究中心 副研究員. 課程大綱 假設的檢定 研究假設 H1: 研究假設 ( 例: X 與 Y 相關 ) H0: 虛無假設 ( 例: X 與 Y 無關 ) 檢定結果:接受虛無假設或拒斥虛無假 設,但不代表接受研究假設.
第 7 章 程序與函數 7-1 模組化程式設計 7-1 模組化程式設計 7-2 VB.NET 的程序與函數 7-2 VB.NET 的程序與函數 7-3 程序與函數的變數範圍 7-3 程序與函數的變數範圍 7-4 VB.NET 常用的內建函數 7-4 VB.NET 常用的內建函數 7-5 遞迴函數 7-5.
資料結構實習-四.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
Introduction to pointers in C/C++. Pointers 特殊變數 存放變數在記憶體中的位址 MinGW C++ 中佔用 4 bytes 間接定址取執法 位址 指標變數 變數內容 變數.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
國立中央大學電機工程學系 99 學年度第 2 學期 助教會議 中央大學電機工程學系 工程認證 1.
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
: Tight words ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: : Tight Words 解題者:鐘緯駿、林一帆 解題日期: 2006 年 03 月 14 日 題意: 給定數字 k 與 n (0 ≦ k.
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
類別函式庫. Java Bean 一個單獨的 AWT 元件都是一個 bean. Java Bean 也有一個 bean 的 class 可繼承, 但是並不強 迫一定要繼承. Bean 的結構 Properties 屬性 : 描述 bean 的屬性 如顏色, 大小,label 等. 透過一對 methods.
Chapter 12 Pointers and Dynamic Arrays. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Pointers (12.1) Dynamic Arrays.
大華技術學院九十三學年度 下學期 資工系資料結構教學大綱 吳弘翔. Wu Hung-Hsiang2 科目名稱:資料結構 適用班別:進修部二年級 授課老師:吳弘翔 修別:必修 老師信箱:
第十章 再論結構. 指標與結構的關係 /* File name: ex10-1a.c */ #include int main() { struct student { char *name; int score; }; struct student st= {"Brian", 97}; struct.
程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0 程式設計 Visual Basic 6.0 Visual Basic 6.0 Visual Basic 6.0許翠婷
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
資料結構實習-二.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
校園網頁整合平台介紹 電算中心綜合業務組. 大綱 設計理念 功能介紹 實做 FAQ 特殊案例 Q&A.
第 17 章 樣板 17-1 樣板的基礎 17-1 樣板的基礎 17-2 樣板函數 17-2 樣板函數 17-3 樣板類別 17-3 樣板類別 17-4 非型態參數的樣板類別 17-4 非型態參數的樣板類別 17-5 樣板類別的繼承 17-5 樣板類別的繼承 17-6 特化樣板與靜態成員 17-6 特化樣板與靜態成員.
Chapter 10 m-way 搜尋樹與B-Tree
第12章 多重表單與模組的應用程式 12-1 VB.NET的方案與專案 12-2 模組化VB.NET應用程式 12-3 多重表單的應用程式
Chapter 11 Strings and Vectors. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview An Array Type for Strings (11.1) The Standard.
本章重點 2-1 有序串列(Ordered List) 2-2 介紹陣列(array) 2-3 矩陣(matrix)的應用
第 17 章 泛型類別與.NET Framework 17-1.NET Framework 類別函式庫 17-1.NET Framework 類別函式庫 17-2 使用.NET FCL 的類別 17-2 使用.NET FCL 的類別 17-3 My 名稱空間的類別 17-3 My 名稱空間的類別 17-4.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
1 第二十章 大型程式的發展 使用名稱空間 熟悉大型程式的開發方式 條件式編譯指令的撰寫. 2 名稱空間 名稱空間就像是一個管理介面,可以將定義的識別 字放在名稱空間之下,而不會有相互衝突的發生 名稱空間的語法如下 把變數 var 放在名稱空間 name1 內 要存取使用名稱空 間 name1 的變數.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
2 C++ 程式概論 2.1 C++ 程式結構 程式註解 // 插入標題檔 #include 2-3
SQL 進階查詢.
3 數學運算 3.1 鍵盤輸入 鍵盤輸入 輸入函數 cin 輸入函數 cin 多重輸入 cin 多重輸入 cin 輸出格式化 輸出格式化 設定輸出長度 setw.
Miscellanea Switch Bitwise operations ?,. break; continue; (example) (example) do { a = getc(stdin); if (a=='q') break; else if (a=='c') continue; n++;
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
Visual C++重點複習.
資料結構實習-六.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
11 Ch05 遞迴 淡江大學 周清江 1. 2  遞迴函數乃是一個自己反覆呼叫自己的函數  一個典型的遞迴演算法 n! = n * (n-1)! = n * (n-1) * (n-2)! = n * (n-1) * (n-2) * (n-3)! = … = n * (n-1) * (n-2)
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
C 語言練習題 2010/12/2. C 程式的格式 一、 C 程式的格式 (1). /* …. */ 是 C 程式的 ______ main() 的內容是由敘述構成的 (2). 敘述要以 __ 為結束符號 (3). 變數、函數都要做 ____ 的宣告 (4). ‘=’ 是 ____ 用的算符 (5).
第8章 字串與陣列 8-1一維陣列的處理 8-2 字串處理 8-3 多維陣列的處理 8-4 動態陣列與參數傳遞 8-5 陣列排序與搜尋.
Introduction to Java Programming Lecture 16 Super and Sub Classes.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
VHDL語法(3).
第 8 章 記憶體指標. 8.0 變數、數值、位址 8.1 指標與變數 對 C++ 語言而言,指標( pointers )是存放 變數或陣列的位址,因此也可以藉由指標 間接取得變數或陣列中的值。 對 C++ 語言而言,指標( pointers )是存放 變數或陣列的位址,因此也可以藉由指標 間接取得變數或陣列中的值。
Chapter 3 Visual Basic.Net Visual Basic, like most programming languages, uses variables for storing values. Variables have a name (the word you use to.
Other Thread Synchronization Functions 井民全製作. Introduction.
1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.
Presentation transcript:

C++ 程式語言

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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