c++-重载运算符(+-,++,--,+=,-=,cin,cout…

2019-12-21 16:02:00来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

c++-重载运算符(+-,++,--,+=,-=,cin,cout)

操作符重载

  • 自定义类型需要操作符重载
  • 运算符重载入门技术推演
  • 友元函数和成员函数实现2元运算符重载
  • 友元函数和成员函数实现1元运算符重载(前置++,前置--,后置++,后置--)
  • 友元函数实现运算符重载应用场景
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


class Complex
{
public:
    Complex(int a, int b)
    {
        this->a = a;
        this->b = b;
    }

    void printComplex()
    {
        cout << "( " << this->a << ", " << this->b << "i )" << endl;
    }

    friend Complex complexAdd(Complex &c1, Complex &c2);
    //friend Complex operator+(Complex &c1, Complex &c2);
    //friend Complex  operator-(Complex &c1, Complex &c2);

    Complex complexAdd(Complex &another)
    {
        Complex temp(this->a + another.a, this->b + another.b);
        return temp;
    }

    Complex operator+(Complex &another)
    {
        Complex temp(this->a + another.a, this->b + another.b);
        return temp;
    }
    Complex operator-(Complex &another)
    {
        Complex temp(this->a - another.a, this->b - another.b);
        return temp;
    }

private:
    int a;//实数
    int b;//虚数
};


Complex complexAdd(Complex &c1, Complex &c2)
{
    Complex temp(c1.a + c2.a, c1.b + c2.b);
    return temp;
}

//操作符重载写在全局
#if 0
Complex operator+(Complex &c1, Complex &c2)
{
    Complex temp(c1.a + c2.a, c1.b + c2.b);
    return temp;
}

Complex operator-(Complex &c1, Complex &c2)
{
    Complex temp(c1.a - c2.a, c1.b - c2.b);
    return temp;
}

#endif

int main(void)
{
    Complex c1(1, 2);
    Complex c2(2, 4);

    c1.printComplex();
    c2.printComplex();

    //Complex c3 = complexAdd(c1, c2);
    //Complex c3 = c1.complexAdd(c2);
    //Complex c3 = c1 + c2; //operator+(c1, c2)  全局的调用方式
                        //c1.operator+(c2)
    //Complex c3 = operator+(c1, c2);

    Complex c3 = c1.operator+(c2);

    c3.printComplex();

    Complex c4 = c1 + c2;



    c4.printComplex();


    return 0;
}

双目运算符重载(-=,+=)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

class Complex
{
public:
    Complex(int a, int b)
    {
        this->a = a;
        this->b = b;
    }

    void printComplex()
    {
        cout << "( " << this->a << ", " << this->b << "i )" << endl;
    }

    //friend Complex & operator+=(Complex &c1, Complex &c2);
    friend Complex &operator-=(Complex &c1, Complex &c2);

    Complex &operator+=(Complex &another)
    {
        this->a += another.a;
        this->b += another.b;

        return *this;
    }
private:
    int a;//实数
    int b;//虚数
};

//全局
#if 0
Complex & operator+=(Complex &c1, Complex &c2)
{
    c1.a += c2.a;
    c1.b += c2.b;

    return c1;
}
#endif

Complex &operator-=(Complex &c1, Complex &c2)
{
    c1.a -= c2.a;
    c1.b -= c2.b;

    return c1;
}

int main(void)
{
    Complex c1(1, 2);
    Complex c2(2, 4);

    (c1 += c2)+=c2;//c1.operator+=(c2)  .operator(c2)


    c1.printComplex();
    c2.printComplex();

    c1 -= c2;
    c1.printComplex();
    
    return 0;
}

单目运算符

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

class Complex
{
public:
    Complex(int a, int b)
    {
        this->a = a;
        this->b = b;
    }

    void printComplex()
    {
        cout << "( " << this->a << ", " << this->b << "i )" << endl;
    }

    //friend Complex & operator++(Complex &c);
    //friend const Complex operator++(Complex &c1, int);

    Complex &operator++()
    {
        this->a++;
        this->b++;

        return *this;
    }

    const Complex operator++(int)//亚元
    {
        Complex temp(this->a, this->b);
        this->a++;
        this->b++;
        return temp;
    }


private:
    int a;//实数
    int b;//虚数
};

#if 0
//重载的是前++运算符
Complex & operator++(Complex &c)
{
    c.a++;
    c.b++;
    return c;
}
#endif

//重载的是后++运算符
#if 0
const Complex operator++(Complex &c1, int)
{
    Complex temp(c1.a, c1.b);

    c1.a++;
    c1.b++;

    return temp;
}
#endif

int main(void)
{


    Complex c1(1, 2);

    //++++c1;

    c1++;

    c1.printComplex();

    //++++c1;

    return 0;
}

左移右移操作符重载

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

class Complex
{
public:
    Complex(int a, int b)
    {
        this->a = a;
        this->b = b;
    }

    void printComplex()
    {
        cout << "( " << this->a << ", " << this->b << "i )" << endl;
    }

    friend ostream& operator<<(ostream & os, Complex &c);
    friend istream & operator>>(istream &is, Complex &c);

    //<<操作符只能写在全局,不能够写在成员方法中。否则调用的顺序会变饭,c1<<cout;
#if 0
    ostream& operator<<(ostream &os) //c1.operator<<(cout)
    {
        os << "( " << this->a << ", " << this->b << "i )";
        return os;
    }
#endif

private:
    int a;//实数
    int b;//虚数
};

#if 1
ostream& operator<<(ostream & os, Complex &c)
{
    os << "( " << c.a << ", " << c.b << "i )";

    return os;
}

istream & operator>>(istream &is, Complex &c)
{
    cout << "a:";
    is >> c.a;
    cout << "b:";
    is >> c.b;

    return is;
}
#endif

int main(void)
{
    Complex c1(1, 2);
    
    cin >> c1;//operaotr>>(cin, c1)

    cout << c1;
    //c1 << cout;
    //cout.operator<<(c1);

    //cout << c1 << "   " <<c1<< endl;//operator<<(cout, c1);
    
    return 0;
}

原文链接:https://www.cnblogs.com/ygjzs/p/12076776.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:c++-友元函数和友元类

下一篇:c++-重载等号,数组,指针,字符串类