//202130312034
//main.cpp
#include <iostream>
using namespace std;
class Coordinate {
private:
float coord[100][100];
int times;
public:
Coordinate();
Coordinate(int);
~Coordinate();
void InputCoord();
void ShowCoord();
void ShowAvgCoord();
};
int main()
{
Coordinate x;
x.InputCoord();
x.ShowCoord();
x.ShowAvgCoord();
return 0;
}
Coordinate::Coordinate()
{
times = 2;
cout << "Coordinate construction1 called!" << endl;
}
Coordinate::Coordinate(int times1)
{
times = times1;
cout << "Coordinate construction2 called!" << endl;
}
Coordinate::~Coordinate()
{
cout << "Coordinate destruction called!" << endl;
}
void Coordinate::InputCoord()
{
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 Coordinate::ShowCoord()
{
cout << "The coord is:" << endl;
for (int i = 0; i < times; i++)
{
cout << "(" << coord[i][1] << "," << coord[i][2] << ")" << endl;
}
}
void Coordinate::ShowAvgCoord()
{
float avgx = 0; float avgy = 0;
for (int i = 0; i < times; i++)
{
avgx += coord[i][1];
avgy += coord[i][2];
}
avgx /= times; avgy /= times;
cout << "The AVG coord is:" << endl;
cout << "(" << avgx << "," << avgy << ")" << endl;
}
在main中插入新代码如下:
Coordinate y(5);
y.InputCoord();
y.ShowCoord();
y.ShowAvgCoord();
运行后得到