国庆作业10.3

 运算符重载

#include <iostream>

using namespace std;
//定义一个复数类5+3i
class Complex
{
private:
    int real;
    int vir;
public:
    Complex(){}
    Complex(int r,int v):real(r),vir(v){}//有参
    //定义展示
    void show()
    {
        if(vir>=0)
        {
            cout<<real<<"+"<<vir<<"i"<<endl;
        }else
        {
               cout<<real<<vir<<"i"<<endl;
        }


    }
    //全局函数实现
     friend const Complex operator+ (const Complex &L,const Complex &R);
    //成员函数实现算术运算符重载
     const Complex operator-(const Complex &R)const
     {
            Complex c;
            c.real=this->real-R.real;
            c.vir=this->vir-R.vir;
            return  c;
     }
     //成员函数版实现关系运算符的重载:实部>实部 &&虚部>虚部
     bool operator>(const Complex &R)const
     {
         return  this->real>R.real&&this->vir>R.vir;
     }
     //重载中括号运算符
     int &operator[](int index)
     {
         if(index==0)
         {
             return real;//返回实部
         }
         else if(index==1) {
             return  vir;//返回虚部
         }

     }
     //重载+=运算符:实部+=实部 虚部+=虚部
     Complex & operator+=(const Complex &R)
     {
         this->real+=R.real;
         this->vir+=R.vir;
         return  *this;//返回自身的引用
     }
     //重载负号重载符
     Complex operator-()
     {
         Complex c;
         c.real=-this->real;
         c.vir=-this->vir;
         return  c;


     }
     //重载前置自增运算符重载函数 实部=实部+1 虚部=虚部+1
     Complex &operator++()
     {
         ++this->real;
         ++this->vir;
         return  *this;
     }
     //重载后置自增运算符重载函数 实部=实部+1 虚部=虚部+1
     Complex operator++(int)
     {
         Complex c;
        c.real= this->real++;
         c.vir=this->vir++;
         return  c;
     }
     friend ostream &operator<<(ostream &L,Complex &c);//将全局重载输出运算符设置有缘

//重载小括号仿函数
     void operator()(string s)
     {
         cout<<s<<endl;
     }
     void operator()()
     {
         cout<<520<<" "<<1314<<endl;
     }

};
//全局函数实现运算符重载
const Complex operator+ (const Complex &L,const Complex &R)
{
    Complex c;
    c.real=L.real+R.real;
    c.vir=L.vir+R.vir;
    return  c;
}
//重载输出运算符函数
ostream &operator<<(ostream &L,Complex &c)
{

    if(c.vir>=0)
    {
       L<<c.real<<"+"<<c.vir<<"i"<<endl;
    }else
    {
          L<<c.real<<c.vir<<"i"<<endl;
    }
    return L;
}
int main()
{
    Complex c1(5,3);
    c1.show();
    Complex c2(2,-1);
    c2.show();
    Complex c3=c1-c2;
    c3.show();
    if(c1>c2)
    {
        cout<<"yes"<<endl;
    }
    else
    {
        cout<<"no"<<endl;
    }
    c3[0]=5;//将实部进行修改成5
    c3.show();
     c3+=c2;
     c3.show();
    Complex c4=-c3;
     c4.show();
    Complex c5=++c3;
    c5.show();
    c3.show();
    Complex c6=c3++;//调用后置自增
    c6.show();
    c3.show();
    cout<<c3;
    c3("hello world");
    c3();
    cout<<c3;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值