【C++】运算符重载

运算符重载

通过简单案例学习运算符’<<‘、’>>’ 和’+'的重载

概述

重载运算符就是对已有的运算符重新定义,根据实际需求赋予其另一种功能,以适应不同的数据类型。重载运算符的本质是重新实现函数,只是函数名为(operator+运算符)。

在重载运算符时需要明确运算符有几个运算对象,运算对象的个数决定了函数的参数个数。

运算符的左操作对象如果是自定义对象可以使用全局函数或者成员函数实现(推荐使用成员函数)。

运算符的左操作对象如果不是自定义对象则必选使用全局函数。(在使用全局函数访问私有数据时,需要设置该函数为类的友元)。

案例

首先定义一个Person类包含numname两个成员变量以及构造方法因为成员没有指针变量所以可以不实现拷贝构造函数和析构函数

class Person
{
private:
    int num;
    char name[32];

public:
    Person(){}
    Person(int num,char *name)
    {
        this->num=num;
        strcpy(this->name,name);
    }

};

重载运算符<<

调用有参构造函数实例化对象lucytom

int main(int argc, char *argv[])
{
    Person lucy(100,"Lucy");
    Person tom(200,"Tom");
    cout<<lucy;//无法实现
    
    return 0;
}
  • 错误信息
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person')
     cout<<lucy;
         ^

该错误信息表示编译器在尝试将 Person 类型的对象输出到 std::ostream 对象(如 std::cout)时找不到合适的 operator<< 重载函数。

在C++中,operator<< 是一个插入运算符,用于将数据插入到输出流中。对于内置类型(如 int, double, char 等),operator<< 已经由标准库提供了。但是,对于自定义类型(如 Person),你需要自己提供 operator<< 的重载版本。

extern ostream cout;		/// Linked to standard output
void operator<<(ostream &out,Person &ob)
 {
     out<<ob.num<<" "<<ob.name;
 }
  • 错误信息
error: 'int Person::num' is private
     int num;
         ^

全局函数无法访问私有数据,在Person类中添加友元声明

friend void operator<<(ostream &out,Person &ob);

在这里插入图片描述

链式操作

cout<<lucy<<tom;
error: no match for 'operator<<' (operand types are 'void' and 'Person')
     cout<<lucy<<tom;
               ^

要实现链式操作,返回值类型为对象引用

 ostream& operator<<(ostream &out,Person &ob)
 {
     out<<ob.num<<" "<<ob.name;
     return out;
 }

更新友元声明

friend ostream& operator<<(ostream &out,Person &ob);

在这里插入图片描述

重载运算符>>

int main(int argc, char *argv[])
{
    Person bob;
    cout<<"输入num和name:";
    cin>>bob;
	cout<<bob;
    
    return 0;
}
  • 错误信息
error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'Person')
     cin>>bob;
        ^
extern istream cin;		/// Linked to standard input

重载实现

void operator>>(istream &in,Person &ob)
{
    in>>ob.num;
    in>>ob.name;
}

友元声明

friend void operator>>(istream &in,Person &ob);

在这里插入图片描述

链式操作

 Person bob;
    Person jack;
    cout<<"输入两个num和name:";
    cin>>bob>>jack;
    cout<<bob<<" "<<jack;
istream& operator>>(istream &in,Person &ob)
{
    in>>ob.num;
    in.getline(ob.name,sizeof(ob.name));
    return in;
}

friend istream& operator>>(istream &in,Person &ob);

在这里插入图片描述

重载运算符+

int main(int argc, char *argv[])

{
    Person lucy(100,"Lucy");
    Person tom(200,"Tom");
    cout<<lucy+tom; // num相加name追加
    return 0;
}
  • 错误信息
error: no match for 'operator+' (operand types are 'Person' and 'Person')
     cout<<lucy+tom;
               ^
法一:全局函数
int main(int argc, char *argv[])

{
    Person lucy(100,"Lucy");
    Person tom(200,"Tom");
    cout<<lucy+tom;


    return 0;
}
Person operator+(const Person &ob1,const Person &ob2)
{
    Person tmp;
    tmp.num=ob1.num+ob2.num;
    strcpy(tmp.name,ob1.name);
    strcat(tmp.name,ob2.name);
    return tmp;
}
  • 错误信息
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Person')
     cout<<lucy+tom;
         ^

operator<<重载函数出现了问题

Person operator+(const Person &ob1,const Person &ob2)的返回值是Person类而ostream& operator<<(ostream &out,Person &ob)的参数类型为Person类普通引用。可以将其修改为常引用类型。

 ostream& operator<<(ostream &out,const Person &ob)
 {
     out<<ob.num<<" "<<ob.name;
     return out;
 }

更新友元

friend ostream& operator<<(ostream &out,const Person &ob);

在这里插入图片描述

法二:成员函数实现(推荐)

成员函数不需要友元声明即可访问私有数据并且只有一个参数

Person operator+(const Person &ob)
    {
        //lucy+tom 其中num和name就是lucy.num和lucy.name ob==tom
        Person tmp;
        tmp.num=num+ob.num;
        strcpy(tmp.name,name);
        strcat(tmp.name,ob.name);
        return tmp;
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值