1 / 22

2 C++ 程式概論

2.1 C++ 程式結構 2-2 2.1.1 程式註解 // 2-3 2.1.2 插入標題檔 #include 2-3 2.1.3 main() 函數 2-7 2.1.4 輸出函數 cout 2-8 2.2 常數與變數 2-12 2.2.1 宣告變數 2-12 2.2.2 指定資料 = 2-13 2.2.3 宣告常數 const 2-15. 2.2.4 宣告符號 #define 2-17 2.2.5 C++ 保留字 2-18 2.3 C++ 資料型態 2-19

Download Presentation

2 C++ 程式概論

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2.1C++ 程式結構 2-2 2.1.1 程式註解 // 2-3 2.1.2 插入標題檔 #include 2-3 2.1.3 main() 函數 2-7 2.1.4 輸出函數cout 2-8 2.2常數與變數 2-12 2.2.1 宣告變數 2-12 2.2.2 指定資料 = 2-13 2.2.3 宣告常數const 2-15 2.2.4 宣告符號 #define 2-17 2.2.5 C++ 保留字 2-18 2.3C++ 資料型態 2-19 2.3.1 整數資料int 2-19 2.3.2 字元資料char 2-21 2.3.3 浮點資料float, double 2-23 2.3.4 邏輯資料bool 2-25 2.3.5 取得型態大小sizeof 2-26 2 C++ 程式概論

  2. 2.1 C++ 程式結構

  3. 2.1.1 程式註解 // • // 註解 • /* 註解 */ • 範例 //儲存檔名:d:\C++02\C0201.cpp /* 宣告整數變數練習 */

  4. 2.1.2 插入標題檔 #include • #include <標題檔名> // 第一式 • #include “標題檔名”// 第二式 • 範例 • #include <iostream.h> //插入iostream.h • #include "user.h" //插入使用者標題檔

  5. 2.1.2 插入標題檔 #include (續) • 插入舊型標題檔 #include <iostream.h> //插入iostream.h標題檔 #include <string.h> //插入string.h標題檔 • 插入新型標題檔 #include <iostream> //插入iostream標題檔 using namespace std; //宣告程式使用新型標題檔

  6. 傳回型態 main(參數){. return 傳回值;} 不傳回任何值給系統 void main(void) void main() 傳回整數值給系統 int main(void) int main() 2.1.3 main() 函數

  7. 2.1.3 main() 函數 (續) • 範例 int main( )或int main(int argc, char *argv[ ]) { //main函數起始點 //敘述區 return 0; //傳回整數0給作業系統 } //main函數結束點

  8. 2.1.4 輸出函數cout • cout<< 變數或字串1 << 變數或字串2 << . . . << 變數或字串n; • 範例 cout << num1; //顯示變數num1的值 cout << "ANSI/ISO C++"; //顯示字串ANSI/ISO C++ cout << "有號整數:" << num1 << endl; //顯示字串、數值、跳行

  9. 2.1.4 輸出函數cout (續)

  10. 2.2 常數與變數 • 變數(variable)代表電腦記憶體中的一個儲存位置。 • 常數(constant)在程式執行中是不可改變的資料項目。

  11. 2.2.1 宣告變數 • 資料型態 變數名稱1, 變數名稱2, …; • int intVar; //宣告整數型態的變數intVar

  12. 2.2.2 指定資料 = • 資料型態 變數名稱1, 變數名稱2, …;變數名稱1 = 初值1;變數名稱2 = 初值2;…; • 範例 short shortVar; //宣告短整數變數shortVar shortVar = 5; //shortVar的初值等於5 . shortVar = 10; //改變shortVar的值為10

  13. 2.2.2 指定資料 = (續) • 資料型態 變數名稱1=初值, 變數名稱2=初值, …; short shortVar = 5;

  14. 2.2.3 宣告常數const • const 資料型態 常數符號1=數值1, 常數符號2=數值2, …; • 範例 const float fPI = 3.14159f; //宣告浮點常數符號fPI const double dPI = 3.141592653; //宣告倍精常數符號dPI

  15. 2.2.4 宣告符號 #define • #define 對等符號 對等資料 • 範例 #define PI 3.14159 void main(void) { float circumference, radius = 10; circumference = 2 * PI * radius; }

  16. 2.2.5 C++ 保留字

  17. 2.3 C++ 資料型態 • C++ 的內建資料型態(build-in data type)包括整數型態、字元型態、浮點數型態、與邏輯型態等。 • 整數又分為短整數、整數、與長整數等型態。 • 浮點數又分為單精度、倍精度、與長倍精度等型態。

  18. 2.3.1 整數資料int

  19. 2.3.2 字元資料char

  20. 2.3.3 浮點資料float, double

  21. 2.3.4 邏輯資料bool

  22. 2.3.5 取得型態大小sizeof • sizeof(資料型態|變數名稱) • 範例 double dType; cout << sizeof(int); //取得int型態大小 cout << sizeof(short); //取得short型態大小 cout << sizeof(bool); //取得bool型態大小 cout << sizeof(dType); //取得dType變數大小

More Related