在三维空间中描绘几何图形,经常需要记录图形的坐标点信息。请设计一个点类Point,它包括x、y、z三个私有数据成员,为它设计恰当的成员函数,使得它支持下面的程序正常运行。 int main() Point p1(1,2,3),p2, p3; cout<<pl; cin>>p2; p3=pl+p2; cout<<p3; if(p1= =p2) cout<<"点相同”; else cout<<"点不同”; return 0; }
时间: 2024-03-18 17:39:53 浏览: 40
好的,我来设计一个点类Point,满足上述要求。
```cpp
#include <iostream>
using namespace std;
class Point {
private:
double x;
double y;
double z;
public:
Point(double x=0, double y=0, double z=0) : x(x), y(y), z(z) {}
double getX() const { return x; }
double getY() const { return y; }
double getZ() const { return z; }
void setX(double x) { this->x = x; }
void setY(double y) { this->y = y; }
void setZ(double z) { this->z = z; }
friend ostream& operator<<(ostream& out, const Point& p);
friend istream& operator>>(istream& in, Point& p);
friend Point operator+(const Point& p1, const Point& p2);
bool operator==(const Point& p) const {
return x == p.x && y == p.y && z == p.z;
}
};
ostream& operator<<(ostream& out, const Point& p) {
out << "(" << p.x << ", " << p.y << ", " << p.z << ")";
return out;
}
istream& operator>>(istream& in, Point& p) {
in >> p.x >> p.y >> p.z;
return in;
}
Point operator+(const Point& p1, const Point& p2) {
Point p(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
return p;
}
int main() {
Point p1(1, 2, 3), p2, p3;
cout << p1 << endl;
cin >> p2;
p3 = p1 + p2;
cout << p3 << endl;
if (p1 == p2) {
cout << "点相同" << endl;
} else {
cout << "点不同" << endl;
}
return 0;
}
```
程序输出:
```
(1, 2, 3)
4 5 6
(5, 7, 9)
点不同
```
阅读全文