區域類別中的成員通常宣告為"public",作用常是為了方便資料的群組管理,區域類別的定義常是很簡單的,例如:
#include <iostream>
using namespace std;
int main() {
// 區域類別
class Point {
public:
int x;
int y;
Point(int x, int y) {
this->x = x;
this->y = y;
}
};
Point p(10, 10);
cout << "(x, y) = ("
<< p.x << ", "
<< p.y << ")"
<< endl;
return 0;
}
執行結果:
| (x, y) = (10, 10) |
區域類別不可以直接存取所在函式中的變數,可以存取外圍類別的私用成員。
沒有留言:
張貼留言