用C++设计一个点类Point,实现点对象之间的+和-运算。 #include<iostream> using namespace std; 提供补充代码 int main() { Point p1,p2,psum,psub; int x,y; cin>>x>>y; p1.setP(x,y); cin>>x>>y; p2.setP(x,y); psum=p1+p2; psub=p1-p2; cout<<"两点相加:"; psum.show(); cout<<"两点相减:"; psub.show(); } 【输入形式】 两个点坐标 【输出形式】 两个点相加及相减的结果
时间: 2024-03-20 08:43:25 浏览: 119
下面是点类Point的实现代码:
```cpp
#include<iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point() {}
Point(int x, int y) {
this->x = x;
this->y = y;
}
void setP(int x, int y) {
this->x = x;
this->y = y;
}
void show() {
cout << "(" << x << "," << y << ")" << endl;
}
Point operator+(const Point& p) const {
Point res;
res.x = this->x + p.x;
res.y = this->y + p.y;
return res;
}
Point operator-(const Point& p) const {
Point res;
res.x = this->x - p.x;
res.y = this->y - p.y;
return res;
}
};
int main() {
Point p1, p2, psum, psub;
int x, y;
cin >> x >> y;
p1.setP(x, y);
cin >> x >> y;
p2.setP(x, y);
psum = p1 + p2;
psub = p1 - p2;
cout << "两点相加:";
psum.show();
cout << "两点相减:";
psub.show();
return 0;
}
```
输入格式为两个点的坐标,例如:
```
1 2
3 4
```
输出格式为相加和相减的结果,例如:
```
两点相加:(4,6)
两点相减:(-2,-2)
```
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)