教材9-1:完成Point类的成员函数
构造一个Point类保存两个点的坐标值,实现点的坐标设置、显示、移动等功能以及必要的构造函数。
完成相关的成员函数定义。
Point类的构造函数与成员函数接口定义:
Point(double xx=0, double yy=0);
void Set(double xx, double yy);
void Move(double xx,double yy;
void Output();
xx与yy都是double型数组形式参数,接收点的x和y坐标值
声明时使用默认参数值为0,函数定义时时头部要写成Point(double xx, double yy)
Point函数是构造构造函数,调用Set函数实现对私有成员x和y的坐标赋值
Set函数实现对私有成员x和y的坐标赋值
Move函数实现移动点的坐标:x轴方向移动xx,y轴方向移动yy
Output函数输出私有成员x和y的值,输出格式为“x值,y值”,如“2.5,3”
裁判测试程序样例:
#include<iostream>
using namespace std;
class Point{
public:
Point(double xx=0, double yy=0);
void Set(double xx, double yy);
void Move(double xx,double yy);
void Output();
private:
double x;
double y;
};
/* 请在这里填写构造函数定义和3个成员函数定义 */
int main()
{
Point x;
x.Output();
x.Set(1,2);
x.Output();
double a,b;
cin >> a >> b;
Point y(a,b);
y.Output();
y.Move(1,5);
y.Output();
return 0;
}
输入样例1:
2 5
输出样例1:
0,0
1,2
2,5
3,10
输入样例2:
8 4
输出样例2:
0,0
1,2
8,4
9,9
c++解析:
Point::Point(double xx , double yy )
{
this->x = xx;
this->y = yy;
}
void Point::Set(double xx, double yy)
{
this->x = xx;
this->y = yy;
}
void Point::Move(double xx, double yy)
{
this->x += xx;
this->y += yy;
}
void Point::Output()
{
cout << this->x << "," << this->y << endl;
}