物件指標 (Object Pointer)

物件指標 (Object Pointer)

物件指標 教學與筆記。

說明

將類別宣告物件指標並指向某個物件,可使用 -> 運算子存取該物件的屬性和方法。

完整程式碼

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
#include <iostream>
using namespace std;

class Car {
private:
int _speed;
public:
void SetSpeed(int speed) {
_speed = speed;
}

int GetSpeed() {
return _speed;
}
};

int main() {
Car MyCar;
Car *ptr = &MyCar;
ptr->SetSpeed(200);
cout << ptr->GetSpeed() << endl;

system("PAUSE");
return 0;
}
Author

Meow Lucian

Posted on

2019-05-18

Updated on

2022-07-04

Licensed under

Comments