题目:(CCP-2023-029运算符重载)6-5 Point类的运算
Gowns and heels will not stop me. I will jump into the woods, as I have done this countless times.
礼服与鞋跟不会阻挡我的步伐,我能在林间跳跃,因为我曾经无数次地这么做过。
一, 题目要求:
1.总体要求
1.定义Point类,有坐标x,y两个私有成员变量;
2.对Point类重载“+”(相加)、“-”(相减)和“==”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。
3.对Point类重载<<运算符,以使得代码 Point p; cout<<p<<endl;可以输出该点对象的坐标。
2.裁判测试程序样例
/* 请在这里填写答案 */
int main(int argc, char const *argv[])
{
Point p1(2,3);
cout<<p1<<endl;
Point p2(4,5);
cout<<p2<<endl;
Point p3 = p1+p2;
cout<<p3<<endl;
p3 = p2-p1;
cout<<p3<<endl;
p1 += p2;
cout<<p1<<endl;
cout<<(p1==p2)<<endl;
return 0;
}
3.函数接口定义
实现Point类。
4.输入形式
无
5.输出形式
2,3
4,5
6,8
2,2
6,8
0
6.补充
*根据主函数内容,"+="也需要进行重载(做题时注意不要遗漏)
**"<<",">>"使用友元函数实现,其他使用成员函数实现
***最后一次输出为bool值判断,使用友元
****测试程序未给出头文件,需自己调用!!!
#include <iostream>
#include <string>
using namespace std;
二, 题目解析
1.完全体
#include <iostream>
#include <string>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(){}
Point(int n,int m){x=n;y=m;}
friend ostream& operator << (ostream& output, Point& P);
Point operator+(Point& P);
Point operator-(Point& P);
Point operator+=(Point& P);
friend bool operator==(Point &P1, Point &P2);
};
Point Point::operator+(Point& P){
int a,b;
a=x+P.x;
b=y+P.y;
Point *p= new Point(a,b);
return (*p);
}
Point Point::operator-(Point& P){
int a,b;
a=-P.x+x;
b=-P.y+y;
Point *p= new Point(a,b);
return (*p);
}
ostream& operator <<(ostream& output, Point& P){
output<<P.x<<","<<P.y;
return output;
}
Point Point::operator+=(Point& P){
x=x+P.x;
y=y+P.y;
return Point(x,y);
}
bool operator==(Point &P1, Point &P2)
{
if(P1.x==P2.x && P1.y==P2.y) return 1;
else return 0;
}
int main(int argc, char const *argv[])
{
Point p1(2,3);
cout<<p1<<endl;
Point p2(4,5);
cout<<p2<<endl;
Point p3 = p1+p2;
cout<<p3<<endl;
p3 = p2-p1;
cout<<p3<<endl;
p1 += p2;
cout<<p1<<endl;
cout<<(p1==p2)<<endl;
return 0;
}
2.函数答案
#include <iostream>
#include <string>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(){}
Point(int n,int m){x=n;y=m;}
friend ostream& operator << (ostream& output, Point& P);
Point operator+(Point& P);
Point operator-(Point& P);
Point operator+=(Point& P);
friend bool operator==(Point &P1, Point &P2);
};
Point Point::operator+(Point& P){
int a,b;
a=x+P.x;
b=y+P.y;
Point *p= new Point(a,b);
return (*p);
}
Point Point::operator-(Point& P){
int a,b;
a=-P.x+x;
b=-P.y+y;
Point *p= new Point(a,b);
return (*p);
}
ostream& operator <<(ostream& output, Point& P){
output<<P.x<<","<<P.y;
return output;
}
Point Point::operator+=(Point& P){
x=x+P.x;
y=y+P.y;
return Point(x,y);
}
bool operator==(Point &P1, Point &P2)
{
if(P1.x==P2.x && P1.y==P2.y) return 1;
else return 0;
}
3.分步解析
1)"<<"的友元重载
类内:
friend ostream& operator << (ostream& output, Point& P);
*注意:友元参数数量=实际参数数量
成员函数参数数量=实际参数数量-1
类外:
ostream& operator <<(ostream& output, Point& P){
output<<P.x<<","<<P.y;
return output;
}
*注意:类内外声明与定义语句差 "friend"
2)"+","-","+="成员函数重载
类内:
Point operator+(Point& P);
Point operator-(Point& P);
Point operator+=(Point& P);
类外:
Point Point::operator+(Point& P){
int a,b;
a=x+P.x;
b=y+P.y;
Point *p= new Point(a,b);
return (*p);
}//+
Point Point::operator-(Point& P){
int a,b;
a=-P.x+x;
b=-P.y+y;
Point *p= new Point(a,b);
return (*p);
}//-
Point Point::operator+=(Point& P){
x=x+P.x;
y=y+P.y;
return Point(x,y);
}//+=
[需要探讨的问题(以"+"重载为例)]
"+"重载常见两种代码:
代码1:
Complex& Complex::operator+(Complex& com){
com.realPart=realPart+com.realPart;
com.imgPart=imgPart+com.imgPart;
return com;
}
代码2:
Point Point::operator+(Point& P){
int a,b;
a=x+P.x;
b=y+P.y;
Point *p= new Point(a,b);
return (*p);
**问题:代码1实际上在进行"+"运算时更改了调用的参数数值
其功能与"+="一致,但在"+"运算后继续调用此参数会计算出错
**改进:代码2通过新声明两个新变量储存"+"结果,不改变参数的大小
并通过新声明指针p指向Point(a,b)后返回*p值形成a,b值的返回
3)"=="重载
类内:
friend bool operator==(Point &P1, Point &P2);
类外:
bool operator==(Point &P1, Point &P2)
{
if(P1.x==P2.x && P1.y==P2.y) return 1;
else return 0;
}
**判断字符使用bool,否则return 1将报错
三, 常见问题
1.未调用头文件
9 12 C:\Users\yangx\Desktop\0919C++作业\cumminh.cpp [Error] 'ostream' does not name a type
2.使用"代码一"进行重载
2,3
4,5
6,8
-2,-3
10,13
0
3.未将"+="重载
51 8 C:\Users\yangx\Desktop\0919C++作业\cumminh.cpp [Error] no match for 'operator+=' (operand types are 'Point' and 'Point')