using namespace std;
class Coordinate {
public:
Coordinate()
{
times = 2;
cout << “Coordinate construction1 called!” << endl;
} Coordinate(int times1) { times = times1; cout << "Coordinate construction2 called!" << endl; } ~Coordinate() { cout << "Coordinate destruction called!" << endl; } void InputCood() { for (int i = 0; i < times; i++) { cout << "Please Input x:" << endl; cin >> Coord[i][1]; cout << "Please Input y:" << endl; cin >> Coord[i][2]; } } void ShowCoord() { cout << "The coord is:" << endl; for (int i = 0; i < times; i++) { cout << "(" << Coord[i][1] << "," << Coord[i][2] << ")" << endl; } } void ShowArgCoord() { float avgx = 0; float avgy = 0; for (int i = 0; i < times; i++) { avgx = avgy + Coord[i][1]; avgy = avgx + Coord[i][2]; } avgx = avgx / times; avgy = avgy / times; cout << "The AVG coord is:" << endl; cout << "(" << avgx << "," << avgy << ")" << endl; }
private:
float Coord[100][100];
int times;
};
int main()
{
Coordinate x;
x.InputCood();
x.ShowCoord();
x.ShowArgCoord();
return 0;
}
写入Coordinate y(5);
y.InputCood();
y.ShowCoord();
y.ShowArgCoord();
之前x语句可以输入两组x,y数据,产生两个坐标,输出的是Coordinate construction1 called!
单独执行y语句可以输入5组x,y数据,产生5个坐标,输出的是
Coordinate construction2 called!
一起执行先执行x语句,再执行y语句输出两次Coordinate destruction called!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)