# **上船囉!C++啟航** >[name=蔡雅愛] <br> > [color=#FFD700] > ## **重點快轉** > [TOC] <br> # **第一項任務 - 你好,世界!** ## 任務目標 * 請向這個世界Say Hello! * 不須輸入資料,執行程式後,印出下方Output內容 Intput: NAN Output: Hello! World! 哈囉! C++! <br> ## 看看答案? ::: spoiler 噓~:shushing_face: 先試著寫寫看? ```cpp= #include <iostream> using namespace std; //hello world int main() { cout << "Hello! World!" << endl; cout << "哈囉! C++!" << endl; return 0; } ``` ::: <br> <br> ## 任務材料 - 重點整理 <br> ### <關鍵字(keyword)> > [color=#FFD700] > 關鍵字是語法功能的保留字 (reserved word),C++20 共有 97 個關鍵字 > 具有既定特殊的用法,<font color="#f00">**不能拿來當識別字或變數名字使用**</font> > #### 這個例子中的關鍵字:using 、 namespace 、 int 、 void 、 return > * using 與 namespace :命名空間的指令 > ```cpp > using namespace std; //第5行 > ``` > * int :基本內建型態 (primitive built-in type) 中的整數 (integer) > ```cpp > int main(void){ //第7行 > ``` > * void :用來宣告 (declare) 空的參數列 (parameter list) 或沒有回傳值 (return value)的外部函式等 > ```cpp > return 0; //第16行 > ``` > :::spoiler 名詞解釋 > > [color=#FFD700] > > * **保留字(reserved word):** 也稱為關鍵字。 > > * **基本內建型態(primitive built-in type):** 分為布林、字元、整數、浮點數四大類,使用關鍵字有 `bool`、`char`、`short`、`int`、`long`、`float`、`double` 等 > > * **整數(interger):** C++中的基本內建型態之一,關鍵字包括` short`、`int`、`long` 及 `long long`。 > > * **宣告(declare):** 在使用變數前先標記其資料型態,即告訴編譯器他是哪種變數(整數int、字元char等),在C++中必須先宣告變數的資料型態,才能夠使用變數。 > > * **參數列(parameter list):** 在定義函式(function)時提供的參數宣告,每個參數需要用逗號來隔開,用來告訴函式,有那些東西要送進函式裡處理。 > > * **回傳值(return value):** 在函式中壢用關鍵字 `return` 回傳的值。 > ::: <br> ### <註解(comment)> > [color=#FFD700] > 在程式中輔助說明的文字,不會被程式執行,愛怎麼打就怎麼打 > #### 這個例子中的註解 > ```cpp > //hello world //第3行 > ``` > #### 註解的兩種用法 > * 單行的註解 > ```cpp > //這裡可以打註解 > ``` > * 跨行的註解 > ```cpp > /*這樣可以打整坨註解 > 盡量打 > 哇哈哈*/ > ``` <br> ### <前置處理器(preprocessor)與標頭檔(header file)> > [color=#FFD700] > * 前置處理器:實際編譯前進行的工作,包括引入標頭檔、巨集擴展、條件編譯及行控制等。 > * 標頭檔:副檔名為.h,主要用途是拿來放各種東西的宣告。 > #### 這個例子中的前置處理器:#include > #### 這個例子中的標頭檔:<iostream> > ```cpp > #include <iostream> //第1行 > ``` <br> ### <識別字(identifier)> > [color=#FFD700] > 並不是程式語言的關鍵字或保留字,而是打程式的人自行命名的名稱,像是變數、函數、類別等等的名稱都是識別字,需要自行命名。 > * 系統保留的識別字:如 `main()` > * 來自標準程式庫(standard library)的識別字:如std、string、cout、endl等。 > #### 這個例子中的識別字:main() > ```cpp > int main(){ //第5行,也可以寫成 int main(void){ > ``` <br> ### <標準程式庫(standard library)> > [color=#FFD700] > 標準程式庫提供許多已經寫好的程式,要使用的話也是利用前置處理指令 `#include` 近來。 > 標準程式庫提供的東西如 `string`(用於表示字串)、 `cout`(用於輸出到標準輸出裝置,常為螢幕) 、`endl`(換行符號)。 > 由於標準程式庫中的所有名稱都是定義在 std 命名空間 (name space) 之中,因此使用 `using namespace std;` 來宣告使用命名空間,<font color="#f00">**如果沒有使用的話,程式就得這樣寫:**</font> > ```cpp > #include <iostream> > //hello world > > int main() { > std::cout << "Hello! World! << std::endl; > std::cout << "哈囉! C++! << std::endl; > > return 0; > } > ``` > :::info > * "::" 是作用域運算子 (scope operator) 。 > ::: > > :::warning > * cin不能用endl > * 錯誤用法: > ```cpp: > cin >> a >> b >> endl; > ``` > ::: <br> ### <運算子(operator)> > [color=#FFD700] > * 輸出運算子:符號與用法 `輸出物件a << 輸出的值b`,即把b輸出到a上。 > * 輸入運算子:符號與用法舉例 `cin >> a;`,將標準輸入(常為鍵盤)的值寫進a。 > #### 這個例子中的運算子:<< > ```cpp > cout << "Hello! World! << endl; //第6行 > cout << "哈囉! C++! << endl;//第7行 > ``` > :::info > 在c++中,cin的內容會因空白或換行而被隔開 > ex.在以下的程式碼的情況下,輸入"1 2 3",a會被輸入為> 1,b會被輸入為2,c會被輸入為3。 > ```cpp > cin >> a >> b >> c; > ``` > ::: > > :::warning > * cin不能用endl; > * 錯誤用法: > ```cpp > cin >> a >> b >> endl; > ``` > ::: <br> ## 任務流程 * 引入標準函式庫 ```Cpp #include <iostream> ``` * 宣告使用命名空間 ```Cpp using namespace std; ``` :::warning 結尾需要用分號,一行分號結尾的程式就是一個陳述 (statement) ,陳述是程式執行的單位, C++ 程式是一行陳述執行完畢,才接著下一行陳述繼續執行。 ::: * 宣告 `main()` 函式 ```Cpp int main() { ``` :::danger 可執行的 C++ 程式都需要有個 main() 函式,主要因為程式是由作業系統呼叫 main() 來執行的,因此我們自己設計程式時,都得把程式執行的部份放到 main() 當中。若程式檔案少了 main() 函式,這個程式檔案編譯後是無法被執行的。 ::: * 在命令列印出訊息 ```cpp cout << "Hello! World << endl; cout << "哈囉! C++! << endl; ``` :::info 最後在命令列印出訊息,這是利用標準程式庫中的 cout 物件,後面空一格接輸出運算子 << ,先是寫要印出的字串,再接空行符號 endl,最後以分號結束。 ::: * 返回0結束程式 ```cpp return 0; ``` :::warning 由於 main() 函式的資料型別為int,因此最後須要回傳一個int(整數),來表明 main() 的值為何。 ::: <br> ## 任務後升級 - 練習 1. 將上述範例的內容逐字元打到文字編輯器裡,然後將檔名存成 helloworld.cpp 。 <br> 2. 刪除 helloworld.cpp 中所有單行註解,並將檔名存成 helloworld01.cpp。 :::spoiler 參考程式碼 ```cpp #include <iostream> using namespace std; int main() { cout << "Hello! World! << endl; cout << "哈囉! C++! << endl; return 0; } ``` ::: <br> 3. 承上題,移除 helloworld01.cpp中的 using namespace std; ,將檔名存成 helloworld021.cpp 。 :::spoiler 參考程式碼 ```cpp #include <iostream> int main() { cout << "Hello! World! << endl; cout << "哈囉! C++! << endl; return 0; } ``` ::: <br> 4. 承上題,將 helloworld021.cpp 中所有cout 、 endl 加上前綴 std:: ,把檔名存成 helloworld022.cpp 。 :::spoiler 參考程式碼 ```cpp #include <iostream> int main() { std::cout << "Hello! World! << std::endl; std::cout << "哈囉! C++! << std::endl; return 0; } ``` ::: <br>