• C++运算符重载


    运算符重载的定义

      运算符重载的方法是定义一个重载运算符的函数,在需要执行被重载的运算符的时候,系统就自动调用该函数,实现相应的运算;也就是说,运算符重载是通过定义函数实现的。运算符重载实质上就是函数的重载。

    运算符重载的规则:

    • C++中的运算符除了少数几个之外,全部可以重载。而且只能重载C++中已有的运算符
    • 重载之后运算符的优先级和结合性不会改变
    • 运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造。一般来讲,重载的功能应当与原邮件功能相类似,不能改变原有运算符的操作对象个数,同时至少要有一个操作对象是自定义类型。
    • 不能重载的运算符只有5个,他们是:成员运算符“.”、指针运算符“*”、作用运算符“::"、sizeof、条件运算符"?:"

    运算符重载形式:

    • 重载为类的成员函数
    • 重载为类的友元函数

    运算符重载为类的成员函数的一般语法形式

    函数类型 operator 运算符(形参表)

    {

    函数体;

    }

    运算符重载为类的友元函数的一般语法形式

    friend 函数类型 operator 运算符(形参表)

    {

    函数体;

    }

    实例

    1 重载为类的成员函数

    class complex
    {
    public:
    complex()
    {
    real=imag=0;
    }
    complex(double r,double i)
    {
    real=r;
    imag=i;
    }
    complex operator + (const complex &c);
    complex operator - (const complex &c);
    complex operator * (const complex &c);
    complex operator / (const complex &c);
    friend void print(const complex &c);
    private:
    double real,imag;
    };
    inline complex complex::operator+(const complex &c)
    {
    return complex(real+c.real,imag+c.imag);
    }
    inline complex complex::operator-(const complex &c)
    {
    return complex(real-c.real,imag-c.imag);
    }
    inline complex complex::operator*(const complex &c)
    {
    return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
    }
    inline complex complex::operator/(const complex &c)
    {
    return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),
    (imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
    }
    void print(const complex &c)
    {
    if(c.imag<0)
    cout<<c.real<<c.imag<<'i';
    else
    cout<<c.real<<'+'<<c.imag<<'i';
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    complex c1(2.0, 3.0), c2(4.0, -2.0), c3;
    c3 = c1 + c2;
    cout<<"c1+c2= ";
    print(c3);
    c3 = c1 - c2;
    cout<<"c1-c2= ";
    print(c3);
    c3 = c1 * c2;
    cout<<"c1*c2= ";
    print(c3);
    c3 = c1 / c2;
    cout<<"c1/c2= ";
    print(c3);
    c3 = (c1+c2) * (c1-c2) * c2/c1;
    cout<<"(c1+c2)*(c1-c2)*c2/c1= ";
    print(c3);
    cout<<endl;
    return 0;
    }

    输出结果:

    c1+c2=6+1i

    c1-c2=-2+5i

    c1*c2=14+8i

    c1/c2=0.45+0.8i

    (c1+c2)*(c1-c2)*c2/c1=9.61538+25.2308i

    2 重载为类的友元函数

    #include <iostream.h>

    class complex

    {

    public:

    complex() { real=imag=0; }

    complex(double r, double i)

    {

    real = r, imag = i;

    }

    friend complex operator +(const complex &c1, const complex &c2);

    friend complex operator -(const complex &c1, const complex &c2);

    friend complex operator *(const complex &c1, const complex &c2);

    friend complex operator /(const complex &c1, const complex &c2);

    friend

    void print(const complex &c);

    private:

    double real, imag;

    };

    complex operator +(const complex &c1, const complex &c2)

    {

    return complex(c1.real + c2.real, c1.imag + c2.imag);

    }

    complex operator -(const complex &c1, const complex &c2)

    {

    return complex(c1.real - c2.real, c1.imag - c2.imag);

    }

    complex operator *(const complex &c1, const complex &c2)

    {

    return complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real);

    }

    complex operator /(const complex &c1, const complex &c2)

    {

    return complex((c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag),

    (c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag));

    }

    void print(const complex &c)

    {

    if(c.imag<0)

    cout<<c.real<<c.imag<<'i';

    else

    cout<<c.real<<'+'<<c.imag<<'i';

    }

    void main()

    {

    complex c1(2.0, 3.0), c2(4.0, -2.0), c3;

    c3 = c1 + c2;

    cout<<"c1+c2= ";

    print(c3);

    c3 = c1 - c2;

    cout<<"c1-c2= ";

    print(c3);

    c3 = c1 * c2;

    cout<<"c1*c2= ";

    print(c3);

    c3 = c1 / c2;

    cout<<"c1/c2= ";

    print(c3);

    c3 = (c1+c2) * (c1-c2) * c2/c1;

    cout<<"(c1+c2)*(c1-c2)*c2/c1= ";

    print(c3);

    cout<<endl;

    return 0;

    }

    该程序的运行结果与上例相同。前面已讲过,对又目运算符,重载为成员函数时,仅一个参数,另一个被隐含;重载为友元函数时,有两个参数,没有隐含参数。

    两种运算符重载的比较

      一般说来,单目运算符最好被重载为成员;对双目运算符最好被重载为友元函数,双目运算符重载为友元函数比重载为成员函数更方便此,但是,有的双目运算符还是重载为成员函数为好,例如,赋值运算符。因为,它如果被重载为友元函数,将会出现与赋值语义不一致的地方。

    原文出自http://blog.csdn.net/dingyuanpu/article/details/5852825

  • 相关阅读:
    c#实现冒泡、快速、选择和插入排序算法
    数据库>SQL Server>循环游标读取例子
    《道德经》程序员版第二章
    跨域访问JQuery+.NET实现的一种思路,以及极简单Demo
    《道德经》程序员版第三章
    ASP.NET初级>传智播客.net>第十一季asp.net中级 文字总结(未完)
    <div>里包含<p>标签疑问
    互联网音乐赚钱模式
    JQuery调用WebService,以及JS把单斜杠转换成双斜杠
    《道德经》程序员版第一章
  • 原文地址:https://www.cnblogs.com/DannyShi/p/4594146.html
Copyright © 2020-2023  润新知