运算符重载函数(更新版)

#include <iostream>

using namespace std;

class complex
{
public:
    complex()
    {
        real=0;
        imag=0;
    }
    //complex():real(0),imag(0){}//无参/缺省构造函数
    complex(double real, double imag);//带参构造函数
    complex(const complex &c);//拷贝函数
    complex &operator=(const complex &right);//赋值函数,重载在成员函数上
    ~complex(){}//析构函数,自动调用,只有在释放内存的时候会在delete后面先析构,其余的在return之前一步析构
    complex & operator++();//所有一元符号都重载在成员函数上(前置)先加后用
    complex  operator++(int);//(后置)
    complex &operator+=(const complex &righr);//具有自赋值性质的二元运算符重载在重载在成员函数上
    friend complex operator+(const complex &right,const complex &right1);//没有自赋值的二元运算符重载在友元函数上,友元函数只是比成员函数多了一个fried和一个参数
    friend istream &operator>>(istream &input , complex &right); //输入和输出符运算符重载
    friend ostream  &operator<<(ostream &output , const complex &right);
private:
    int real;
    int imag;

};
ostream  &operator << (ostream &output , const complex &right)
{
    if((right.real!=0)||(right.real==0&&right.imag==0))
    {
        output << right.real;
    }

    if(right.imag==1)
    {
        output << "+" << "i" ;
    }
    else if(right.imag==-1)
    {
        output << "-" << "i" ;
    }
    if(right.imag > 0)
    {
        if(right.real==0)
         {
            output << right.imag << "i";
         }
        else
        output << "+" << right.imag << "i" ;
    }
    else if(right.imag < 0)
    {
        output << right.imag << "i" ;
    }
    else //right.imag==0;
        ;
    output <<endl;
    return output;
}
istream &operator>>(istream&input, complex &right)
{
    input  >> right.real >>right .imag;
    return input;
}
complex operator+(const complex &right,const complex &right1)
{
    complex temp;
    temp.real = right.real + right1.real;
    temp.imag = right.imag + right1.imag;
    return temp;
}
complex & complex::operator +=(const complex &right)
{
    real += right.real;
    imag += right.imag;
    return *this;
}
complex complex::operator++(int)
{
    complex temp = *this;//调用拷贝函数,使得temp拥有了返回对象本身的意义,最后返回了temp,一来一去等于没变,就实现了先用后加
    ++real;
    ++imag;
    return temp;//局部变量用完会消失,因此不用引用符号(但是会返回出去,并不冲突)
}
complex & complex::operator++()
{
    ++real;
    ++imag;
    return *this;//this指向调用它的函数
}

complex::complex(double real, double imag):real(real),imag(imag){}
//complex( complex *this double real, double imag)
//{
//    this->real=real;
//    this->imag=imag;
//}

complex::complex(const complex &right)
{
    real=right.real;
    imag=right.imag;
}

complex& complex::operator=(const complex &right)
{
    if(this==&right)
    {
        return *this;
    }
    real=right.real;
    imag=right.imag;

    return *this;//返回指向的对象*this
}

int main()
{
    complex complex1;
    complex complex3,complex4,complex5,complex7,complex6;//complex6(2,3);
    cin >> complex1 >> complex6;
    cout << complex1;//2+3i
    complex complex2 = complex1;//拷贝运算符
    cout << complex2;//2+3i
    complex3 = complex1;//赋值运算符
    cout << complex3;//2+3i
    complex4 = ++complex1;//前置自加
    cout << complex4;//3+4i
    cout << complex1;//3+4i
    complex5 = complex1++;//后置自加
    cout << complex5;//3+4i
    cout << complex1;//4+5i
    complex6 +=complex1;//自赋值运算符
    cout << complex6;//6+8i
    cout << complex1;//4+5i
    complex7 = complex2 + complex3;//加号运算符,没有自赋值性质的要重载在友元函数上
    cout << complex7;//4+6i

    return 0;
}

 

posted @ 2021-03-17 20:02  z79  阅读(95)  评论(0)    收藏  举报