建構式 & 解構式 (Constructor & Destructor)

建構式 & 解構式 (Constructor & Destructor)

建構式 & 解構式 教學與筆記。

建構式 (Constructor)

當新增物件變數時,將會自動執行建構式。

建立建構式初始化

原始方式:

1
2
3
4
5
6
7
8
9
10
class Car {
private:
string _name = "Benz";
int _speed = 100;
public:
Car() {
cout << _name << endl;
cout << _speed << endl;
}
};

第二種方式有同樣效果:

1
2
3
4
5
6
7
8
9
10
class Car {
private:
string _name;
int _speed;
public:
Car() : _name("Benz"), _speed(100) {
cout << _name << endl;
cout << _speed << endl;
};
};

建構式多載 (Overload)

當建立物件時無引數或有多個引數,可允許使用多個建構式相對應。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Car {
private:
string _name = "Benz";
int _speed = 100;
public:
Car() {
cout << _name << endl;
cout << _speed << endl;
}

Car(string name, int speed) {
_name = name;
_speed = speed;
cout << _name << endl;
cout << _speed << endl;
}
};

解構式 (Destructor)

當物件消滅時將會自動執行解構式。解構式只允許有一個。

1
2
3
4
5
6
class Car {
public:
~Car() {
cout << "Car " << _name << " was eliminated." << endl;
}
};

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

class Car {
private:
string _name = "Benz";
int _speed = 100;
public:
Car() {
cout << _name << endl;
cout << _speed << endl;
}

Car(string name, int speed) {
_name = name;
_speed = speed;
cout << _name << endl;
cout << _speed << endl;
}

~Car() {
cout << "Car " << _name << " was eliminated." << endl;
}
};

int main() {
Car MyCar;
cout << endl;
Car MyCar2("BMW",200);

system("PAUSE");
return 0;
}

結果圖:

建構式 & 解構式 (Constructor & Destructor)

https://meowlucian.github.io/C++/OOP/Constructor-Destructor/

Author

Meow Lucian

Posted on

2019-05-15

Updated on

2022-07-04

Licensed under

Comments