计算机程序设计C++ MOOC
测试与作业C++基础练习100题
##第九周基本练习
本周内容为类的构建
- 点类
#include "iostream"
#include "cmath"
using namespace std;
class POINT
{
private:
double x, y;
public:
void set(double,double);
double distance(POINT);
void show();
};
void POINT::set(double a, double b)
{
x = a; y = b;
}
void POINT::show()
{
cout<<'('<<x<<','<<y<<')';
}
double POINT::distance(POINT a)
{
return sqrt((x - a.x)*(x - a.x) + (y - a.y)*(y - a.y));
}
int main()
{
POINT a, b;
double x, y;
cin >> x >> y;
a.set(x, y);
cin >> x >> y;
b.set(x, y);
a.show(); cout << endl;
b.show(); cout << endl;
cout << a.distance(b) << endl;
return 0;
}
- 圆类