#include <iostream>
#include <cassert>
using namespace std;
class Point {
public://外部接口
Point() :x(0), y(0) {
cout << "Default Constructor called"<<endl;
}
Point(int x ,int y) : x(x), y(y) {//构造函数
cout << "Constructor called" << endl;
}
~Point() { cout << "Destructor called"<<endl; }
void move(int newX, int newY) {
x = newX;
y = newY;
}
private:
int x, y;
};
//动态数组类
class ArrayOfPoints {
public:
ArrayOfPoints(int size) :size(size) {
points = new Point[size];
}
~ArrayOfPoints(){
cout << "Deleting ...";
delete[] points;
}
//获取下标为index的数据元素
Point& element(int index) {
assert(index >= 0 && index < size);//如果数组下标越界,程序终止
return points[index];
}
private:
Point* points;//指向动态数组首地址
int size;//数组大小
};
int main() {//主函数
int count;
cout << "Please enter the count of points:";
cin >> count;
ArrayOfPoints points(count);
points.element(0).move(5, 0);//访问数组元素的成员
points.element(1).move(15, 20); // 访问数组元素的成员
return 0;
}
