---
# 海山資研社課講義
## 目錄
1. C++ 介紹
2. 第一個 C++ 程式
3. 基本資料型態
4. 變數與常數
5. 運算子與表達式
6. 控制結構 (if, else, switch)
7. 迴圈 (for, while, do-while)
8. 陣列 (Arrays)
9. 函數 (Functions)
10. 指標 (Pointers)
11. 字串 (Strings)
12. 結構 (Structs)
13. 類別與物件 (Classes and Objects)
14. 繼承 (Inheritance)
15. 資料封裝與存取控制 (Encapsulation and Access Control)
---
## 1. C++ 介紹
C++ 是由 Bjarne Stroustrup 在 C 語言的基礎上發展而來的高效能、靈活的語言。它廣泛應用於系統軟體、遊戲開發、嵌入式系統等。C++ 的特色是支援面向對象編程(OOP),提供了更強大的抽象能力。
### 主要特性:
- 面向對象編程 (OOP)
- 多重繼承、多型 (Polymorphism)
- 靜態與動態記憶體管理
- 標準模板庫 (STL) 提供的容器、演算法與迭代器
---
## 2. 第一個 C++ 程式
```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
```
### 程式分析:
- `#include <bits/stdc++.h>`:引入輸出輸入流庫,用於標準輸出和輸入。
- `using namespace std;`:免去使用"std::",使程式更簡潔。
- `int main()`:主函數,程式從這裡開始執行。
- `cout`:標準輸出流,用於顯示文字到螢幕。
- `return 0;`:返回 0 表示程式正常結束。
---
## 3. 基本資料型態
C++ 提供了多種基本資料型態:
- `int`:整數型態。
- `float`:單精度浮點數。
- `double`:雙精度浮點數。
- `char`:字元型態。
- `bool`:布林值(`true` 或 `false`)。
範例:
```cpp
int a = 10;
float b = 3.14;
double c = 6.28;
char d = 'A';
bool e = true;
```
---
## 4. 變數與常數
變數:用來存放資料的具名位置。
```cpp
int age = 25;
```
常數:其值不可變動,使用 `const` 關鍵字。
```cpp
const int pi = 3.14;
```
---
## 5. 運算子與表達式
C++ 支援多種運算子:
- 算術運算子:`+`, `-`, `*`, `/`, `%`
- 賦值運算子:`=`, `+=`, `-=`, `*=`, `/=`, `%=`
- 比較運算子:`==`, `!=`, `>`, `<`, `>=`, `<=`
- 邏輯運算子:`&&`, `||`, `!`
範例:
```cpp
int a = 10, b = 20;
int sum = a + b;
bool result = (a < b);
```
---
## 6. 控制結構
### if-else
```cpp
int a = 10;
if (a > 5) {
cout << "a is greater than 5" << endl;
} else {
cout << "a is less than or equal to 5" << endl;
}
```
### switch
```cpp
int day = 3;
switch(day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
case 3: cout << "Wednesday"; break;
default: cout << "Invalid day";
}
```
---
## 7. 迴圈
### for 迴圈
```cpp
for (int i = 0; i < 10; i++) {
cout << i << " ";
}
```
### while 迴圈
```cpp
int i = 0;
while (i < 10) {
cout << i << " ";
i++;
}
```
### do-while 迴圈
```cpp
int i = 0;
do {
cout << i << " ";
i++;
} while (i < 10);
```
---
## 8. 陣列 (Arrays)
陣列是相同型態的變數集合:
```cpp
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
```
---
## 9. 函數 (Functions)
函數是可重複使用的程式片段:
```cpp
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
cout << "Sum: " << sum << endl;
}
```
---
## 10. 指標 (Pointers)
指標是用來儲存變數位址的變數:
```cpp
int a = 10;
int *ptr = &a; // ptr 是指向 a 的指標
cout << *ptr << endl; // 輸出 10
```
---
## 11. 字串 (Strings)
C++ 提供 `string` 型態來操作字串:
```cpp
#include <string>
string name = "John";
cout << "Hello, " << name << endl;
```
---
## 12. 結構 (Structs)
結構是一種用來分組相關變數的自定義資料型態:
```cpp
struct Person {
string name;
int age;
};
Person person1;
person1.name = "Alice";
person1.age = 25;
```
---
## 13. 類別與物件 (Classes and Objects)
C++ 支援面向對象編程,類別是物件的藍圖。
```cpp
class Dog {
public:
string name;
void bark() {
cout << name << " is barking!" << endl;
}
};
int main() {
Dog myDog;
myDog.name = "Buddy";
myDog.bark();
}
```
---
## 14. 繼承 (Inheritance)
繼承允許一個類別從另一個類別中繼承屬性與方法:
```cpp
class Animal {
public:
void eat() {
cout << "This animal is eating." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "This dog is barking." << endl;
}
};
int main() {
Dog myDog;
myDog.eat();
myDog.bark();
}
```
---
## 15. 資料封裝與存取控制 (Encapsulation and Access Control)
C++ 提供存取控制修飾詞 `public`, `private`, 和 `protected` 來控制資料的訪問:
```cpp
class Person {
private:
string name;
public:
void setName(string newName) {
name = newName;
}
string getName() {
return name;
}
};
```
---