490 likes | 601 Views
Chapter 2 & Chapter 3. 變數型態和型態轉換. 原則 : 範圍小的型態轉成大的型態 page 69. 變數型態和型態轉換. double value = 31.0; int count = 16; float many = 20.0f; char num = 4; value = (value-count)*(count-num)/many+num/many;. Explicit casts 明確轉換. static_cast<the_type_to_convert_to>(expression).
E N D
變數型態和型態轉換 • 原則: 範圍小的型態轉成大的型態 • page 69
變數型態和型態轉換 double value = 31.0; int count = 16; float many = 20.0f; char num = 4; • value = (value-count)*(count-num)/many+num/many;
Explicit casts 明確轉換 • static_cast<the_type_to_convert_to>(expression)
Keywords about casting • static_cast: 程式編譯時靜態檢查轉換 • dynamic_cast: 程式執行時動態檢查 • const_cast: 常數轉換 • reinterpret_cast: 重新解釋的轉換-無條件的型態轉換
運算子的優先權 • table (page 67) • 當運算子同時具有一元運算及二元運算的意義, 一元運算優先權高於二元運算
運算種類 • 算術運算 • 關係運算 • 邏輯運算 • 以位元為單位 • 以運算元為單位
Example char letter1=‘A’, letter2=‘Z’, result=0; result=letter1 &letter2;
位移位元運算子 • 16387=>0100000000000011 向左shift 2位 • 0000000000001100=12
位移位元運算子 • 16387=>0100000000000011 向右shift 2位 • 0001000000000000=4096
範圍與生命週期 • 空間 • 時間 • 有效範圍,指的是變數本身的作用及影響範圍 • 生命週期是變數於程式執行階段,實際存在的時間區段
變數的範圍(scope) • 變數名稱在程式裡的有效區域
變數的生命週期 (storage duration) • 變數生命週期尤其儲存期間(storage duration)決定 • storage duration分三種 • automatic • static • dynamic (describe in ch4)
自動變數 (auto) • auto 為預設值,當變數宣告時,沒有額外以儲存類別關鍵字宣告,即表示其為 auto 類別。 • 自動變數的生命期和宣告的位置有關 • 區塊結束變數生命結束 • 區塊重新執行, 變數重新被建立並指定初值
Example (EX2_06.cpp) // EX2_06.CPP // Demonstrating variable scope #include <iostream> using namespace std; int main() { // Function scope starts here int count1 = 10; int count3 = 50; cout << endl << "Value of outer count1 = " << count1 << endl; { // New scope starts here... int count1 = 20; // This hides the outer count1 int count2 = 30; cout << "Value of inner count1 = " << count1 << endl; count1 += 3; // This affects the inner count1 count3 += count2; } // ...and ends here
Example (EX2_06.cpp) (cont.) cout << "Value of outer count1 = " << count1 << endl << "Value of outer count3 = " << count3 << endl; // cout << count2 << endl; // uncomment to get an error return 0; } // Function scope ends here
靜態變數 (static) • 區域靜態變數 • 全域靜態變數
區域靜態變數 • 當函式結束, 區域靜態變數的生命期並不會跟著結束, 同時還會保有原來的值, 直到整個程式結束時, 變數的生命期才會結束 • 區域靜態變數若無設定初值,則預設為 0 • 區域靜態變數只能被該宣告者函式使用
全域靜態變數 • 全域靜態變數的有效範圍只限於該檔案內的函式
Example #include <iostream> using namespace std; static int i; void main() { void f(void); f(); cout << "main:i=" << i << endl; } void f(void) { static int i=2; cout << "\nf:i=" << i << endl; }
全域變數與區域變數 • 區域變數(local variable)只在某個範圍裡有效,超出範圍就變成無效了 • 全域變數(global variable)的有效範圍是變數宣告之後的所有函式
Figure (page 82) Example.cpp long value1; Value1 int main() { int calue2; … { int value3; } } value2 value3 int value4; value4 int function(int) { long value5; int value1; … } value5 value1
區域變數的獨立性 main() { int age; /*區域變數*/ printf("main:How old are you?"); scanf("%d", &age); sub(); printf("main:You are %d years old.\n", age); } sub() { int age; /*區域變數*/ printf("sub:How old are you?"); scanf("%d", &age); printf("sub:You are %d years old.\n", age); }
隱藏全域變數 int i=123; /*全域變數*/ main() { int i=456; /*區域變數*/ printf("%d",i); } • 全域變數和區域變數可以同名,然而在函式中使用的變數和全域變數同名時,全域變數的有效範圍會被遮蔽 • 不建議使用,因為很容易搞混變數名稱而出錯
全域、區域變數的關係 • 相同有效範圍的變數,名稱不可重複。相對地,不同範圍的區域變數,名稱可以相同。區域變數和全域變數的名稱也可以相同。 • 不同有效範圍的同名區域變數不會相互影響。 • 區域變數和全域變數同名時,在區域變數的有效範圍,全域變數會受到遮蔽。
使用全域變數時的注意事項 • 在全域變數宣告之後所定義的函式,才能使用該全域變數。 • 儘量少用全域變數。 • 小心使用全域變數,並避免誤改全域變數值。 • 全域變數在宣告後,預設值為0。
Namespace • 是否可以在程式中宣告名稱為cout的變數
Example-Modify from EX2_08.cpp //Modify the example of Ex2_08.cpp #include<iostream> int cout =0; int main() { std::cout << "enter an integer: "; std::cin >> cout; std::cout << "\nYou entered " << cout << std:: endl; return 0; } 未使用假指令 using namespace std;
宣告名稱空間 namespace mystuff { // Code that I want to have in the namespace mystuff… }
Example (Ex2_09.cpp) // EX2_09.CPP // Declaring a namespace #include <iostream> namespace myStuff { int value = 0; } int main() { std::cout << "enter an integer: "; std::cin >> myStuff::value; std::cout << "\nYou entered " << myStuff::value << std::endl; return 0; }
Example (Ex2_10.cpp) // EX2_10.CPP // Using a using directive #include <iostream> namespace myStuff { int value = 0; } using namespace myStuff; int main() { std::cout << "enter an integer: "; std::cin >> value; std::cout << "\nYou entered " << value << std::endl; return 0; }
Example (Modify from Ex2_10.cpp) // Modify from EX2_10.CPP // Using a using directive #include <iostream> namespace myStuff { int value = 0; } using namespace myStuff; using namespace std; int main() { cout << "enter an integer: "; cin >> value; cout << "\nYou entered " << value << endl; return 0; }
Chapter 3 決策與迴圈
In this chapter • 資料的比較 • 由比較結果決定程式執行順序 • 邏輯運算子及邏輯運算式 • 多重選擇 • 迴圈的撰寫與使用
流程控制 • 結構化程式設計 • 循序結構 • 選擇結構 • 重複結構
循序結構 • Example: cout << endl; cout << num1 << num2; cout << endl;
選擇結構 • if 敘述 if (測試條件) { //敘述 }
if(letter >= 'a') // Test for 'a' or larger if(letter <= 'z') // Test for 'z' or smaller { cout << endl << "You entered a small letter." << endl; return 0; } cout << endl << "You did not enter a letter." << endl; return 0; } Example (Ex3_01.cpp) // EX3_01.CPP // A nested if demonstration #include <iostream> using namespace std; int main() { char letter = 0; // Store input in here cout << endl << "Enter a letter: "; // Prompt for the input cin >> letter; // then read a character if(letter >= 'A') // Test for 'A' or larger if(letter <= 'Z') // Test for 'Z' or smaller { cout << endl << "You entered a capital letter." << endl; return 0; }
選擇結構 • if-else敘述 if (測試條件) { 敘述區塊1 } else { 敘述區塊2 }
選擇結構 • 巢狀 If 格式 If 條件1 Then If 條件2 Then 敘述區塊1-1 Else 敘述區塊1-2 End If ……. Else 敘述區塊2 End If