设计一个三维点坐标的类Point3D,要求如下:
(1)包含三个int类型数据成员my_x, my_y, my_z;
(2)默认构造函数,默认三维点的坐标值为0, 0, 0;
(3)SetPoint函数,设置坐标值;
(4)用成员函数重载”+”运算符,实现两个Point3D类对象的加法运算;
(4)用友元函数重载前置“++”运算符以实现Point3D类对象的z值加1;
(5)重载运算符”<<”,实现点类对象的输出,坐标值x,y,z间以”,”分隔。
(6)在main( ) 函数中测试类Point3D“+”和前置“++”的功能,并输出结果。
三维点坐标类Point3D?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答
- 柯本 2023-12-28 11:26关注
参考:
#include <algorithm> #include <iostream> using namespace std; class Point3D { private: int my_x, my_y, my_z; //包含三个int类型数据成员my_x, my_y, my_z; public: Point3D() //默认构造函数,默认三维点的坐标值为0, 0, 0; { my_x = 0; my_y = 0; my_z = 0; } void SetPoint(int x, int y, int z) //SetPoint函数,设置坐标值; { my_x = x; my_y = y; my_z = z; } Point3D operator+(const Point3D &p) //用成员函数重载”+”运算符,实现两个Point3D类对象的加法运算; { my_x += p.my_x; my_y += p.my_y; my_z += p.my_z; return *this; } Point3D operator++() //函数重载前置“++”运算符以实现Point3D类对象的z值加1;这个是自加,不用友元的 { my_z++; return *this; } friend ostream &operator<<(ostream &os, const Point3D &p) // 重载运算符”<<”,实现点类对象的输出,坐标值x,y,z间以”,”分隔。,这个要用友元 { os << p.my_x << ", " << p.my_y << ", " << p.my_z ; return os; } }; int main() { Point3D p1, p2; p1.SetPoint(0, 0, 0); p2.SetPoint(1, 1, 1); cout << "p1: " << p1 << endl; p1 = p1 + p2; cout << "p1 after addition: " << p1 << endl; ++p1; cout << "p1 after pre-increment: " << p1 << endl; return 0; }
解决 无用评论 打赏 举报
悬赏问题
- ¥50 buildozer打包kivy app失败
- ¥30 在vs2022里运行python代码
- ¥15 不同尺寸货物如何寻找合适的包装箱型谱
- ¥15 求解 yolo算法问题
- ¥15 虚拟机打包apk出现错误
- ¥15 用visual studi code完成html页面
- ¥15 聚类分析或者python进行数据分析
- ¥15 三菱伺服电机按启动按钮有使能但不动作
- ¥15 js,页面2返回页面1时定位进入的设备
- ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复