[c++]运算符重载

类的声明:

#ifndef new_c___homework_operator_h
#define new_c___homework_operator_h

#include<iostream>
using namespace std;

class complex
{
public:
    double real;
    double imag;
    
    complex(double r = 0,double i = 0)
    {real = r; imag = i;}
};
complex operator+(complex co1,complex co2)//运算符重载函数
{
    complex temp;
    temp.real = co1.real+co2.real;
    temp.imag = co1.imag+co2.imag;
    return temp;
}//运算符重载函数写在类外,因为类中的两个成员被定义为公有
    

#endif



#include "operator.h"

int main()
{
    complex com1(1.1,2.2);
    complex com2(3.3,4.4);
    complex total1;
    complex total2;
    
    total1 = operator+(com1,com2);//以该方式传值更能直观的表现出重载的作用
    cout<<"real1 = "<<total1.real<<endl;
    cout<<"imag1 = "<<total1.imag<<endl;
    total2 = com1 + com2;
    cout<<"real2 = "<<total2.real<<endl;
    cout<<"imag2 = "<<total2.imag<<endl;
    return 0;
}


1.不能重载的运算符:

.    ::   *    ?:       #      sizeof

2.重载运算符可以对运算符做出新的解释,但原有基本语义不变:

不改变运算符原有优先级和结合性

单目重载单目,双目重载双目,不能混

不能创建新的运算符,只有系统预定义的运算符才能重载

经重载的运算符,其操作数中至少有一个是自定义类型

3.运算符通常是对类中私有成员进行操作,故重载运算符应能访问类中的私有成员,所以重载运算符一半采用成员函数友元函数的形式


下面是用成员函数实现:

#include<iostream>
using namespace std;

class complex
{
private:
    double real;
    double imag;
public:
    complex(double r = 0,double i = 0)
    {real = r; imag = i;}
    complex operator+(complex c);//运算符重载函数
    complex operator-(complex c);
    complex operator*(complex c);
    complex operator/(complex c);
    void print();
};
complex complex:: operator+(complex c)
{
    complex temp;
    temp.real = real+c.real;
    temp.imag = imag+c.imag;
    return temp;
}
complex complex:: operator-(complex c)
{
    complex temp;
    temp.real = real-c.real;
    temp.imag = imag-c.imag;
    return temp;
}
complex complex:: operator*(complex c)
{
    complex temp;
    temp.real = real*c.real-imag*c.imag;
    temp.imag = real*c.imag+imag*c.real;
    return temp;
    
}
complex complex:: operator/(complex c)
{
    complex temp;
    double t;
    t = real*c.real+imag*c.imag;
    temp.real = (real*c.real-imag*c.imag)/t;
    temp.imag = (real*c.imag+imag*c.real)/t;
    return temp;
    
}
void complex:: print()
{
    cout<<real;
    if(imag > 0)
    cout<<"+";
    if(imag != 0)
    cout<<imag<<"i"<<endl;
}

main:

#include "operator.h"

int main()
{
    complex com1(1.1,2.2);
    complex com2(3.3,4.4);
    complex A1;
    complex A2;
    complex A3;
    complex A4;
    
    A1 = com1 + com2;
    A2 = com1 - com2;
    A3 = com1 * com2;
    A4 = com1 / com2;
    A1.print();
     A2.print();
     A3.print();
     A4.print();
    return 0;
}

单目运算符:

complex complex:: operator++(int)<span style="color: rgb(29, 148, 33); font-size: 18px; font-family: Menlo;">//(int)</span><span style="color: rgb(29, 148, 33); font-family: 'Heiti SC Light'; font-size: 18px;">只是一个为了与前缀自增的运算符重载函数有所区分因此可不必写参数名
</span>{
    real++;
    imag++;
    return *this;
}
complex complex:: operator++()
{
    ++real;
    ++imag;
    return *this;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值