C++快速入门13-运算符重载

本系列博客是我假期在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-加法运算符重载:

  1. 外部函数实现:
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;  // 对两个坐标相加
}
  1. 内部函数实现:
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)


在这里插入图片描述
戳我,让你走向成功的世界

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值