• 运算符重载的三种实现方法


    一、重载为一般函数
    格式:返回类型 operator 运算符(参数列表)

    struct Complex{//定义一个复数结构:包括实部与虚部两部分 
    	double real;//实部 
    	double imag;//虚部 
    };
    Complex operator+(Complex c1,Complex c2){//对加法运算的重载:将运算符'+' 定义为两复数相加 
    	Complex c;
    	c.real=c1.real+c2.real;
    	c.imag=c1.imag+c2.imag;
    	return c;
    }
    ostream&operator<<(ostream&out,Complex c){
    	out<<"("<<c.real<<"+"<<c.imag<<"i)";
    	return out;
    }
    int main()
    {
    	Complex c1={1,2},c2={3,4},c3;//结构体变量的声明
    	c3=c1+c2; 
    	cout<<c1<<"+"<<c2<<"="<<c3<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    运行结果
    在这里插入图片描述
    二、重载为友元函数
    需要访问类中的私有成员,则要将该重载函数在类中声明为友元类。
    格式:friend 类型 重载函数名(参数);

    #include
    using namespace std;
    class Complex{//定义一个复数类包括实部与虚部两部分 
        private:
    	   double real,imag;
    	public:
    		Complex(double r=0,double i=0):real(r),imag(i){
    		}
    		friend Complex operator+(Complex c1,Complex c2);
    		friend ostream&operator<<(ostream&out,Complex c);
    };
    Complex operator+(Complex c1,Complex c2){//对加法运算的重载:将运算符'+' 定义为两复数相加,需要对复数类的私有属性进行访问 
    	Complex c;
    	c.real=c1.real+c2.real;
    	c.imag=c1.imag+c2.imag;
    	return c;
    }
    ostream&operator<<(ostream&out,Complex c){//需要对复数类的私有属性进行访问,声明成友元类 
    	out<<"("<<c.real<<"+"<<c.imag<<"i)";
    	return out;
    }
    int main()
    {
    	Complex c1(1,2),c2(3,4),c3;
    	c3=c1+c2; 
    	cout<<c1<<"+"<<c2<<"="<<c3<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    三、重载为成员函数
    直接声明为成员函数

    #include
    using namespace std;
    class Complex{//定义一个复数类包括实部与虚部两部分 
        private:
    	   double real,imag;
    	public:
    		Complex(double r=0,double i=0):real(r),imag(i){
    		}
    		Complex operator+(Complex c);//直接在类中声明重载运算符 
    		friend ostream&operator<<(ostream&out,Complex c);
    };
    Complex Complex::operator+(Complex c){//定义重载运算符 
    	return Complex (real+c.real,imag+c.imag);
    }
    ostream&operator<<(ostream&out,Complex c){//需要对复数类的私有属性进行访问,声明成友元类 
    	out<<"("<<c.real<<"+"<<c.imag<<"i)";
    	return out;
    }
    int main()
    {
    	Complex c1(1,2),c2(3,4),c3;
    	c3=c1+c2; 
    	cout<<c1<<"+"<<c2<<"="<<c3<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    [JAVA]排序
    2.1.C++项目:网络版五子棋对战之前置知识
    2022-08-06 学习日记(26th day)集合框架---Set
    Rust语言基础:从Hello World开始
    假设检验:正态性检验的那些bug——为什么对同一数据,normaltest和ktest会得到完全相反的结果?
    Linux漏洞SSL/TLS协议信息泄露漏洞(CVE-2016-2183) - 非常危险(7.5分) 解决办法!升级openssl
    Java之线程详解(一)——线程概念知识、创建线程的几种方式
    用图说话——流程图进阶
    Linux中安装Jenkins
    Python算法例8 将整数A转换为B
  • 原文地址:https://blog.csdn.net/2301_76371717/article/details/133971337