#include<iostream> using namespace std; class Point{ private: int x; int y; public: Point& operator++() { ++x; ++y; return*this; } Point& operator+(Point &p1) { Point temp; temp.x=p1.x+x; temp.y=p1.y+y; return temp; } Point(int x = 0,int y =0) { this->x=x; this->y=y; } friend Point operator--(Point&,int); friend Point operator-(Point,Point); friend ostream& operator<<(ostream &stm,Point &p); }; Point operator--(Point &p2,int) { p2.x++; p2.y++; return p2; } Point operator-(Point &p1,Point &p2) { Point temp; temp.x=p1.x-p2.x; temp.y=p1.y-p2.y; return temp; } ostream& operator<<(ostream &stm,Point &p) { stm<<"("<<p.x<<","<<p.y<<")"<<endl; } int main() { Point p1, p2, p; cin >> p1 >> p2; p = ++p1; cout << "p=++p1后,p的坐标为:" << p << ",p1坐标为:" << p1 << endl; p = p2--; cout << "p=p2--后,p的坐标为:" << p << ",p2坐标为:" << p2 << endl; p = p1 + p2; cout << "p=p1+p2后,p的坐标为:" << p << endl; p = p1 - p2; cout << "p=p1-p2后,p的坐标为:" << p << endl; return 0; }找出错误

时间: 2024-03-06 09:48:14 浏览: 43
这段代码存在一些错误,包括: 1. 在运算符重载函数 operator+ 中,返回的 Point 对象使用了局部变量 temp,函数执行完毕后,temp 对象将被销毁,因此返回的是一个无效的对象。应该使用 new 在堆上创建对象,或者直接返回一个引用。 2. 在运算符重载函数 operator-- 中,后缀自减操作应该返回原始值,而不是自减后的值。 3. 在运算符重载函数 operator<< 中,应该返回输出流对象 stm,否则无法进行链式输出。 以下是修改后的代码: ```c++ #include<iostream> using namespace std; class Point{ private: int x; int y; public: Point& operator++() { ++x; ++y; return *this; } const Point operator+(const Point& p1) const { return Point(x + p1.x, y + p1.y); } Point operator--(int) { Point temp = *this; x--; y--; return temp; } const Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); } Point(int x = 0, int y = 0) { this->x = x; this->y = y; } friend ostream& operator<<(ostream& stm, const Point& p); }; ostream& operator<<(ostream& stm, const Point& p) { stm << "(" << p.x << "," << p.y << ")" << endl; return stm; } int main() { Point p1, p2, p; cin >> p1 >> p2; p = ++p1; cout << "p=++p1后,p的坐标为:" << p << ",p1坐标为:" << p1 << endl; p = p2--; cout << "p=p2--后,p的坐标为:" << p << ",p2坐标为:" << p2 << endl; p = p1 + p2; cout << "p=p1+p2后,p的坐标为:" << p << endl; p = p1 - p2; cout << "p=p1-p2后,p的坐标为:" << p << endl; return 0; } ```

相关推荐

编写程序声明Point类,在类中声明整型的私有成员变量x、y,声明成员函数Point& operator++( );Point operator++( int);以实现对Point类重载“++”(自增)运算符,声明成员函数Point& operator--( );Point operator--( int);以实现对Point类重载“--”(自减)运算符,实现对坐标值的改变。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ 在主函数中完成充分测试! 输入 输出 1 1 a的值为:1 , 1 after a++,a的值为:2 , 2 after ++a,a的值为:3 , 3 after a--,a的值为:2 , 2 after --a,a的值为:1 , 1 #include<iostream> using namespace std; class Point { private: int x,y; public: Point(int nx=0,int ny=0):x(nx),y(ny){ } Point& operator++( ); Point operator++(int); Point& operator--( ); Point operator--(int); int getx(){return x;} int gety(){return y;} }; ........ int main() { int m,n; cin>>m>>n; Point a(m,n); cout<<"a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; a++; cout<<"after a++,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; ++a; cout<<"after ++a,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; a--; cout<<"after a--,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; --a; cout<<"after --a,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; return 0; }

#include <iostream> #include <iomanip> #include <string> using namespace std; class Point { private: string type; double x, y; public: Point(string t = "", double xx = 0, double yy = 0) : type(t), x(xx), y(yy) {} Point(const Point& p) : type(p.type), x(p.x), y(p.y) {} virtual ~Point() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Point& p) { os << "Type: " << p.type << "\nX: " << fixed << setprecision(2) << p.x << "\nY: " << fixed << setprecision(2) << p.y; return os; } virtual void PrintName() { cout << "This is a Point object." << endl; } }; class Circle : public Point { private: double r; public: Circle(string t = "", double xx = 0, double yy = 0, double rr = 0) : Point(t, xx, yy), r(rr) {} Circle(const Circle& c) : Point(c), r(c.r) {} ~Circle() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Circle& c) { os << static_cast<const Point&>(c) << "\nRadius: " << fixed << setprecision(2) << c.r; return os; } virtual void PrintName() { cout << "This is a Circle object." << endl; } }; class Cylinder : public Circle { private: double h; public: Cylinder(string t = "", double xx = 0, double yy = 0, double rr = 0, double hh = 0) : Circle(t, xx, yy, rr), h(hh) {} Cylinder(const Cylinder& cy) : Circle(cy), h(cy.h) {} ~Cylinder() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Cylinder& cy) { os << static_cast<const Circle&>(cy) << "\nHeight: " << fixed << setprecision(2) << cy.h; return os; } virtual void PrintName() { cout << "This is a Cylinder object." << endl; } }; int main() { Point p1("Point1", 1, 2); Point p2("Point2", 3, 4); Circle c1("Circle1", 5, 6, 7); Circle c2("Circle2", 8, 9, 10); Cylinder cy1("Cylinder1", 11, 12, 13, 14); Cylinder cy2("Cylinder2", 15, 16, 17, 18); cout << p1 << endl; cout << p2 << endl; cout << c1 << endl; cout << c2 << endl; cout << cy1 << endl; cout << cy2 << endl; Point* ptr; ptr = &p1; ptr->PrintName(); ptr = &c1; ptr->PrintName(); ptr = &cy1; ptr->PrintName(); return 0; }c:\program files (x86)\microsoft visual c++6.0\myprojects\shiayn6\6.cpp(27) : error C2248: 'type' : cannot access private member declared in class 'Point' c:\program files (x86)\microsoft visual c++6.0\myprojects\shiayn6\6.cpp(8) : see declaration of 'type' 执行 cl.exe 时出错.

最新推荐

recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

小程序项目源码-美容预约小程序.zip

小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序v
recommend-type

MobaXterm 工具

MobaXterm 工具
recommend-type

grpcio-1.48.0-cp37-cp37m-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

扁平风格PPT可修改ppt下载(11).zip

扁平风格PPT可修改ppt下载(11).zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。