OOP
Object-Oriented Programming
什麼是OOP
Object-Oriented
- Everything is an object
- Objects do everything
Why?
- 方便管理程式碼
- 程式碼的擴充性強
- 抽象化 (Abstraction)
-
跟資奧其實沒什麼關係,但很重要
名詞解析
- Object (物件)
- Struct
- Class
名詞解析
- Attribute (Variable)
- Method (Function)
Basics
宣告
struct Person{
int id;
string name;
int height, weight;
};
宣告
struct Person{
int id;
string name;
int height, weight;
};
Person teacher, students[30];
宣告
struct Person{
int id;
string name;
int height, weight;
};
Person teacher, students[30];
這時候它們的 id 和各個數值會是多少?
本來的預設值
宣告
struct Person{
int id;
string name;
int height, weight;
};
Person teacher, students[30];
teacher.id = 1;
teacher.name = "yungyao";
teacher.height = 180;
teacher.weight = 60;
for (int i=0;i<30;++i){
students[i] = ...
...
}
是不是要寫的東西有點多?
建構子
struct Person{
int id;
string name;
int height, weight;
Person(int _id, string _name, int _height, int _weight){
id = _id;
name = _name;
height = _height;
weight = _weight;
}
};
Person teacher, students[30];
teacher = Person(1, "yungyao", 180, 60);
for (int i=0;i<30;++i){
students[i] = Person(...);
}
id 需要我們手動輸入嗎?
- Maybe?
- 但更多時候不需要!
建構子/Static (靜態變數)
struct Person{
static int personCount = 0;
int id;
string name;
int height, weight;
Person(string _name, int _height, int _weight){
id = personCount;
personCount++;
name = _name;
height = _height;
weight = _weight;
}
};
Person teacher, students[30];
teacher = Person("yungyao", 180, 60);
for (int i=0;i<30;++i){
students[i] = Person(...);
}
Static Variable (靜態變數)
- 有點類似全域變數的性質
- 但還是屬於包括它的 struct/class
讓 object 去做事情
struct Person{
static int personCount = 0;
int id;
string name;
int height, weight;
Person(string _name, int _height, int _weight){
...
}
void speakName(){
cout << "I am " << name << ".\n";
}
};
...
teacher.speakName(); // I am yungyao.
讓 object 去做事情
struct Person{
static int personCount = 0;
int id;
string name;
int height, weight;
Person(string _name, int _height, int _weight){
...
}
double BMI(){
return weight / height / height;
}
};
...
return teacher.BMI();
mutator
struct Person{
static int personCount = 0;
int id;
string name;
int height, weight;
Person(string _name, int _height, int _weight){
...
}
void gainHeight(int gainedHeight){
height += gainedHeight;
}
};
...
cout << teacher.height << '\n'; // 180
teacher.gainedHeight(-20);
cout << teacher.height << '\n'; // 160
Object as attribute
struct Classroom{
Person teacher;
Person student[40];
int studentCount;
Classroom(int _studentCount, Person _teacher){
teacher = _teacher;
studentCount = _studentCount;
for (int i=0;i<studentCount;++i){
student = Student("unknown", 140, 40);
}
}
};
Struct / Class
(for C++)
Accessibility
- Private
- Public
- 從外部能不能看進去
不能讓 Object 外部看到的資訊
struct Person{
static int personCount = 0;
int id;
string name;
private:
int height, weight;
Person(string _name, int _height, int _weight){
...
}
double BMI(){
return weight / height / height;
}
};
...
cout << teacher.height; // Compile Error: Can not access private
Struct / Class 差在哪
- struct 預設是 public
- class 預設是 private
繼承
子型別
- 人如果是一種型別
- 學生是一個子型別
- 老師是另一個子型別
- 都有名字,但只有學生有學號
母型別
- 很多型別間會有共同的功能
- 把這些功能只寫一次
子型別
- 只屬於特定型別的功能,在子型別裡完成
Code Example
class Person{
string name;
public:
void printName(){
cout << name << endl;
}
};
class Student: public Person{
int number;
public:
void printNumber(){
cout << number << endl;
}
};
Code Example (多型)
class Person{
string name;
public:
void printName(){
cout << name << endl;
}
};
class Student: public Person{
int number;
public:
void printName(){
cout << name << ' ' << number << endl;
}
};
用途
Project
- 要怎麼處理每個東西
oop
By yungyao
oop
- 42