小样的第六次学习博客——运算符的重载
声明
本人是一名计算机专业(23级)大一学生,写博客是为了强迫自己对所学知识进行总结,同时也把博客当作笔记。
如有错误,还请指出,大佬轻喷
参考教材:《C++面向对象程序设计》(第3版)谭浩强编著
一些注意事项
在C++中,运算符重载既可以是类的成员函数,也可以是友元函数。具体选择哪种方式取决于你想要实现的功能和操作的上下文。
成员函数:
- 当运算符重载为类的成员函数时,意味着至少有一个操作数是该类的对象。
- 成员函数形式的运算符重载会隐式地传入一个参数,即调用该操作的对象本身,因此成员函数的参数数量会比操作的操作数少一个。
- 这种方式适用于那些与类紧密相关的操作,比如对类内部数据的修改或者类对象之间的操作。
友元函数:
- 当运算符重载为友元函数时,它不是类的一部分,但可以访问类的私有和保护成员。
- 友元函数形式的运算符重载的参数数量与操作的操作数相同。
- 这种方式适用于那些需要从外部访问类内部数据,或者不直接与类对象关联的操作。
总的来说,如果运算符重载的操作主要针对类对象,并且需要修改对象的内部状态,那么使用成员函数可能更合适。如果操作需要访问类的私有成员,但又不需要改变对象的状态,或者操作涉及非类对象的其他类型,那么使用友元函数可能更加适宜。设计时应根据具体情况和需求来决定使用哪种形式。
实例
定义一个表示点的类Point,类中有两个私有成员变量x和y;
使用成员函数和友元函数两种方法,为该类重载以下运算符:
加+
自增++(包括前增和后增)
等于==
主函数中分别使用以上运算符。
#include<iostream>
using namespace std;
class Point {
public:
Point(int x_,int y_):x(x_),y(y_){} //构造函数初始化
Point operator++(); //声明前自增重载函数
Point operator++(int); //声明后自增重载函数
friend Point operator+(Point&,Point&); //声明+重载函数,且是作为友元函数
friend bool operator==(Point&, Point&); //声明==重载函数,且是作为友元函数
void display(); //输出函数
private:
int x;
int y;
};
Point Point::operator++() { //前自增重载,返回值也是Point类型哦
++x;
++y;
return *this;
}
Point Point::operator++(int) { //后自增重载,返回值也是Point类型哦
Point t(*this);
x++;
y++;
return t;
}
Point operator+(Point& p1, Point& p2) { //+重载,友元函数,返回值也是Point类型哦
Point p3(0, 0);
p3.x = p1.x + p2.x;
p3.y = p1.y + p2.y;
return p3;
}
bool operator==(Point& p1, Point& p2) { //==重载,友元函数,返回值是bool型
if (p1.x == p2.x && p1.y == p2.y)
return 1;
return 0;
}
void Point::display() { //输出函数
cout << "(" << x << ", " << y << ")" << endl;
}
int main() {
Point p1(1, 2);
Point p2(3, 4);
p1.display();
p2.display();
Point p11 = p1++;
p11.display();
p1.display();
Point p22 = ++p2;
p2.display();
p22.display();
Point p3 = p1 + p2;
p3.display();
int a = p1 == p2;
cout << a << endl;
return 0;
}
提醒
可能会有些地方看不懂,我第一次看也有很多地方看不懂,不明白为什么要这样写,为什么不那样写,但当你真正的自己敲一遍(是自己敲,不是抄)你就明白它为什么这样写了
小附加
流插入运算符>>也是可以重载的,重载后的>>可直接用于输出类对象
#include<iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
// 重载加法运算符 +
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
// 重载前增量运算符 ++
Point& operator++() {
++x;
++y;
return *this;
}
// 重载后增量运算符 ++
Point operator++(int) {
Point temp(*this);
++(*this);
return temp;
}
bool operator==(const Point& other) const;
Point operator=(const Point& p2) {
this->x = p2.x;
this->y = p2.y;
return *this;
}
friend ostream& operator<<(ostream&, Point&); //声明>>重载函数
};
ostream& operator<<(ostream& out, Point& p) { //定义>>重载函数
out << "(" << p.x << ", " << p.y << ")";
return out;
}
bool Point::operator==(const Point& other) const {
return (x == other.x) && (y == other.y);
}
int main() {
Point p1(1, 2);
Point p2(3, 4);
// 使用重载的加法运算符 +
Point p3 = p1 + p2;
cout << p3 << endl;
// 使用重载的前增量运算符 ++
++p1;
cout << p1 << endl;
// 使用重载的后增量运算符 ++
Point p4 = p2++;
cout << p4 << endl;
cout << p2 << endl;
// 使用重载的相等运算符 ==
bool isEqual = (p1 == p2);
cout << isEqual << endl;
// 使用友元函数重载的赋值运算符 =
p1 = p2;
cout << p2 << endl;
cout << p1 << endl;
return 0;
}```