本系列博客是我假期在B站花了一天时间看了一个快速入门C++的视频,大概是4个小时,不过来回截图,整理笔记也花了一大整天;
13. 运算符重载
在平时项目中,有时候需要自定义一些常规的运算符给某个功能用,那么就必须重新定义这个符号的功能了。
有些运算符可以作为成员函数,有些运算符只能作为外部函数;
比如输入输出流函数只可作为外部函数;加法运算符既可以作为成员函数,也可以作为外部函数。
示例1-问题引入:
int main(){
student stu("Li Ping", 80.5);
stu.print();
cout << stu;
}
cout << stu
:这个语句想通过输出流运算符直接将结构体信息输出,如按正常的功能,是不可以的;
示例1-解决(struct):
ostream& operator<<(ostream &o, student s){
//cout << "结构体-输出流符号重载"; //用于测试
return o;
}
int main(){
student stu("Li Ping", 80.5);
stu.print();
cout << stu; //等价于operator<<(cout, stu)
}
**示例1-解决(class):**那么我们可以在class内定义一个友元函数:
class student{
string name;
double score;
public:
student(string n, double s){
name = n; score = s;
}
//友元函数
friend ostream& operator<<(ostream &o, student s);
};
ostream& operator<<(ostream &o, student s){
cout << name << "," << score << endl;
return o;
}
int main(){
student stu("Li Ping", 80.5);
stu.print();
cout << stu;
}
示例2-输入流符号重载(class):
class student{
string name;
double score;
public:
student(string n, double s){
name = n; score = s;
}
//友元函数
friend ostream& operator>>(ostream &in, student &s);
};
ostream& operator>>(ostream &in, student &s){
cin >> s.name >> s.score;
return in;
}
int main(){
student stu("Li Ping", 80.5);
stu.print();
cin >> stu; //operator>>(cin,stu)
cout << stu;//operator<<(cout,stu)
}
示例3-下标运算符重载:
#include<iostream>
#include<string>
class Point{
double x,y;
public: //[]下标运算符
double operator[](int i){
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
//自定义构造函数
Point(double x_, double y_){
x = x_; y = y_;
}
friend ostream& operator>>(ostream &in, student &s);
friend ostream& operator<<(ostream &o, student s);
};
ostream& operator>>(ostream &in, student &s){
cin >> s.x >> s.y;
return in;
ostream& operator<<(ostream &o, student s){
cout << x << "," << y << endl;
return o;
}
int main(){
Point p(3.5, 4.8);
cin >> p;
cout << p;
cout << p[0] << " " << p[1]; //p.operator[](0)
}
若想直接通过下标运算符赋值(即给私有成员赋值),那么:
#include<iostream>
#include<string>
class Point{
double x,y;
public:
double operator[](int i) const{ //这个是不改变成员的操作
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
double& operator[](int i){ //这个是引用形式的,可以修改成员
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
//自定义构造函数
Point(double x_, double y_){
x = x_; y = y_;
}
friend ostream& operator>>(ostream &in, student &s);
friend ostream& operator<<(ostream &o, student s);
};
ostream& operator>>(ostream &in, student &s){
cin >> s.x >> s.y;
return in;
}
ostream& operator<<(ostream &o, student s){
cout << x << "," << y << endl;
return o;
}
int main(){
Point p(3.5, 4.8);
cout << p;
cout << p[0] << " " << p[1]; //p.operator[](0)
p[0] = 3.45; p[1] = 5.67;
cout << p;
}
示例3-加法运算符重载:
- 外部函数实现:
int main(){
Point p(3.5, 4.8), q(2.0, 3.0);
Point s = p+q;
cout << s;
}
#include<iostream>
#include<string>
class Point{
double x,y;
public:
double operator[](int i) const{ //这个是不改变成员的操作
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
double& operator[](int i){ //这个是引用形式的,可以修改成员
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
//自定义构造函数
Point(double x_, double y_){
x = x_; y = y_;
}
friend ostream& operator>>(ostream &in, student &s);
friend ostream& operator<<(ostream &o, student s);
};
Point operator+(const Point p, const Point q){
return Point(p[0]+q[0], p[1]+q[1]);
}
ostream& operator>>(ostream &in, student &s){
cin >> s.x >> s.y;
return in;
}
ostream& operator<<(ostream &o, student s){
cout << x << "," << y << endl;
return o;
}
int main(){
Point p(3.5, 4.8);
cout << p;
cout << p[0] << " " << p[1]; //p.operator[](0)
p[0] = 3.45; p[1] = 5.67;
cout << p;
Point s = p+q; // 对两个坐标相加
}
- 内部函数实现:
class Point{
double x,y;
public:
double operator[](int i) const{ //这个是不改变成员的操作
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
double& operator[](int i){ //这个是引用形式的,可以修改成员
if(i==0) return x;
else if(i==1) return y;
else throw "下标非法";
}
//自定义构造函数
Point(double x_, double y_){
x = x_; y = y_;
}
//添加这一个内部函数,仅需一个参数即可,第二个参数其实是本类内的私有成员
Point operator+(const Point q){
return Point(this->x+q[0], this->y+q[1]);
}
friend ostream& operator>>(ostream &in, student &s);
friend ostream& operator<<(ostream &o, student s);
};
Point s = p+q;
这句就相当于p.operator+(q)
,operator+(p,q)