裁判测试程序样例中展示的是一段二维向量类TDVector的定义以及二维向量求和的代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。
函数接口定义:
提示:需要补充的函数有:
1. 带参构造函数
2. getX
3. getY
4. setX
5. setY
6. 运算符重载函数
提示:需要补充的函数有: 1. 带参构造函数 2. getX 3. getY 4. setX 5. setY 6. 运算符重载函数
裁判测试程序样例:
#include
#include
using namespace std;
class TDVector{
private:
double x;
double y;
public:
TDVector(){
x = y = 0;
}
};
int main(){
TDVector a;
double x, y;
cin >> x >> y;
TDVector b(x, y);
cin >> x >> y;
TDVector c;
c.setX(x);
c.setY(y);
TDVector d;
d = a + b + c;
cout << fixed << setprecision(2) << d.getX() << ' ' << d.getY();
return 0;
}
输入样例:
1.1 2.2
3.3 4.4
输出样例:
4.40 6.60
#include
#include
using namespace std;
class TDVector{
private:
double x;
double y;
public:
TDVector(){
x = y = 0;
}
};
int main(){
TDVector a;
double x, y;
cin >> x >> y;
TDVector b(x, y);
cin >> x >> y;
TDVector c;
c.setX(x);
c.setY(y);
TDVector d;
d = a + b + c;
cout << fixed << setprecision(2) << d.getX() << ' ' << d.getY();
return 0;
}
1.1 2.2 3.3 4.4
输出样例:
4.40 6.60
代码长度限制
16 KB
时间限制
100 ms
内存限制
4 MB
这里直接给出完整代码:
#include#include using namespace std; class TDVector{ private: double x; double y; public: TDVector(){ x = y = 0; } TDVector(double x,double y):x(x),y(y) { } void setX(double x) { this->x=x; } void setY(double y) { this->y=y; } double getX() { return x; } double getY() { return y; } TDVector operator +(TDVector c) //加法运算符重载 { this->x=this->x+c.x; //相当于x=x+C.x, y=y+C.y this->y=this->y+c.y; return *this; } }; int main(){ TDVector a; double x, y; cin >> x >> y; TDVector b(x, y); cin >> x >> y; TDVector c; c.setX(x); c.setY(y); TDVector d; d = a + b + c; //这里的加法就不是普通的加法了,对应的是加法重载函数的运算方式 cout << fixed << setprecision(2) << d.getX() << ' ' << d.getY(); return 0; }



