国庆作业10.3

 运算符重载

#include 

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-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<=0)
    {
       L<c2)
    {
        cout<<"yes"<

你可能感兴趣的:(c++,算法,开发语言)