Exercise
C++ Builder
C++ Builder
Note
盡量不要使用global variable,原因:維護困難、可能撞名,可以用創建一個class Global來代替,裡頭設setNumber(然後裏頭的變數要用static)和getNumber的函式,這樣只要調用一次class Global就能使用static的變數了,不用的時候,再destry class即可。
Sample code
conio.h 函式庫(Unix, Linux 沒有包含這個函式庫)
裏頭的getch()不需要緩衝區,可以直接從鍵盤讀取輸入的字元。
typedef筆記
用typedef來定義struct的example:
Using "struct" by "typedef"
一些Functions介紹
連結
Header
標頭(header)
Pointer
指標(pointer)
Overload
Overload
const
const 注意
const *a; // 內容不可改變
* const a; // 位址不能改變
void display(const int array[], int size); // array的內容不能改變
Template
template T的型態可以改變, 也可以使用template <class T, class U>實現多個不同型態變數。
#include <iostream>
using namespace std;
template <class T>
T GetNumber (T a, T b) {
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
return a + b;
}
int main () {
int ret;
ret = GetNumber<int>(2, 3);
std::cout << "Return value: " << ret << std::endl;
// Output:
// a: 2
// b: 3
// Return value: 5
return 0;
}