C++重载

作者: 帅碧 | 来源:发表于2016-11-14 20:51 被阅读0次

    重载

    C++语言规定:

    • 重载的运算符要保持原运算符的意义。
    1. 只能对已有的运算符重载,不能增加新的运算符。
    2. 重载的运算符不会改变原先的优先级和结合性。
    • 运算符重载的形式
    1. 成员函数
    2. 友元函数
    • 允许重载的运算符
    (::,.,*,->,?:这5个符号不能重载)
    //*为读值符号,不是乘号
    
    
    #include<iostream>
    using namespace std;
    int a1,b1,c1;
    class jxb
    {
        int m_z;
            public:
                jxb(int z)
                {
                    m_z=z;
                }
                jxb()
                {
                    m_z=10;
                }
                int getz()
                {
                    return m_z; 
                }
                void setz(int z)
                {
                    m_z=z;
                }
                friend int operator+(jxb t1,jxb t2);
    };
    jxb a2(11),b2(1),c2;
    int operator+(jxb t1,jxb t2)
    {
    //  return t1.getz()+t2.getz();
        return t1.m_z+t2.m_z;
    }
    int main()
    {
        c1=a1+b1;//operator+(a1,b1);=>operator+(int i1,int i2);
        c1=a2+b2;//c1=operator+(a2,b2);=>int operator+(jxb t1,jxb t2);
        cout<<"c1 = "<<c1<<endl;
    }
    //结果为:
    //c2=12;
    
    
    #include<iostream>
    using namespace std;
    int a1,b1,c1;
    class jxb
    {
        int m_z;
            public:
                jxb(int z)
                {
                    m_z=z;
                }
                jxb()
                {
                    m_z=10;
                }
                int getz()
                {
                    return m_z; 
                }
                void setz(int z)
                {
                    m_z=z;
                }
    };
    jxb a2(11),b2(1),c2;
    jxb operator+(jxb t1,jxb t2)
    {
        return t1.getz()+t2.getz();
    }
    int main()
    {
        c1=a1+b1;//operator+(a1,b1);=>operator+(int i1,int i2);
        c2=a2+b2;//c1=operator+(a2,b2);=>int operator+(jxb t1,jxb t2);
        cout<<"c2 = "<<c2.getz()<<endl;
    }
    //结果为:
    //c2 =12;
    
    
    #include<iostream>
    using namespace std;
    int a1,b1,c1;
    class jxb
    {
        int m_z;
            public:
                jxb(int z)
                {
                    m_z=z;
                }
                jxb()
                {
                    m_z=10;
                }
                int getz()
                {
                    return m_z; 
                }
                void setz(int z)
                {
                    m_z=z;
                }
                int operator+(jxb t2)
                {
                    return m_z+t2.m_z;
                }
    };
    jxb a2(14),b2(1),c2;
    int main()
    {
        c1=a1+b1;//operator+(a1,b1);=>operator+(int i1,int i2);
        c1=a2+b2;//c1=operator+(a2,b2);=>int operator+(jxb t1,jxb t2);
        cout<<"c1 = "<<c1<<endl;
    }
    //结果为:
    //c1=15;
    
    
    • 总结: 成员函数的operator比全局的operator少一个形参.

    哪些符号不能重载

    ::,.,->,*,?:这5个符号不能重载
    
    
    • 前缀++
    
    #include<iostream>
    using namespace std;
    int a1,b1,c1;
    class jxb
    {
        int m_z;
            public:
                jxb(int z)
                {
                    m_z=z;
                }
                jxb()
                {
                    m_z=10;
                }
                int getz()
                {
                    return m_z; 
                }
                void setz(int z)
                {
                    m_z=z;
                }
            /*  int operator+(jxb t2)
                {
                    return m_z+t2.m_z;
                }*/
                friend jxb& operator++(jxb& t);
    };
    jxb a2(14),b2(1),c2;
    jxb& operator++(jxb& t)
    {
        ++t.m_z;
        return t;
    }
    int main()
    {   
        c2=++a2;
        cout<<"a2 = "<<a2.getz()<<endl;
        cout<<"c2 = "<<c2.getz()<<endl;
    }
    //结果为:
    //a2 = 15;
    //c2 = 15;
    
    
    • 后缀++
    #include<iostream>
    using namespace std;
    int a1,b1,c1;
    class jxb
    {
        int m_z;
            public:
                jxb(int z)
                {
                    m_z=z;
                }
                jxb()
                {
                    m_z=10;
                }
                int getz()
                {
                    return m_z; 
                }
                void setz(int z)
                {
                    m_z=z;
                }
            /*  int operator+(jxb t2)
                {
                    return m_z+t2.m_z;
                }*/
                friend jxb& operator++(jxb& t);
                friend jxb operator++(jxb& t,int);
    };
    jxb a2(14),b2(1),c2;
    jxb& operator++(jxb& t)
    {
        ++t.m_z;
        return t;
    }
    jxb operator++(jxb& t,int)
    {
        jxb z(t.m_z);
        ++t.m_z;
        return z;
    }
    int main()
    {   
    //  c2=++a2;
        c2=a2++;
        cout<<"a2 = "<<a2.getz()<<endl;
        cout<<"c2 = "<<c2.getz()<<endl;
    }
    //结果为:
    //a2 = 15;
    //c2 = 14;
    
    
    • 后缀++的链式输出
    #include<iostream>
    using namespace std;
    int a1,b1,c1;
    class jxb
    {
        int m_z;
            public:
                jxb(int z)
                {
                    m_z=z;
                }
                jxb()
                {
                    m_z=10;
                }
                int getz()
                {
                    return m_z; 
                }
                void setz(int z)
                {
                    m_z=z;
                }
                friend jxb& operator++(jxb& t);
                friend jxb operator++(jxb& t,int);
                friend ostream& operator<<(ostream& c,jxb& z);
    };
    jxb a2(14),b2(1),c2;
    jxb& operator++(jxb& t)
    {
        ++t.m_z;
        return t;
    }
    jxb operator++(jxb& t,int)
    {
        jxb z(t.m_z);
        ++t.m_z;
        return z;
    }
    ostream& operator<<(ostream& c,jxb& z)
    {
        c<<z.m_z;
        return c;
    }
    int main()
    {   
        c2=a2++;
        cout<<a2<<endl;//operator<<(cout,a2);=>operator<<(ostream&,jxb&);
    }
    //结果为:
    //a2 = 15;
    
    

    +由值返回,++由引用返回

    1. 对于operator+(),两个对象相加, 不改变其中任一个对象。而且它必须生成一个结果对象来存放加法的结果,并将该结果对象以值的方式返回给调用者。
    1. operator++()确实修改了它的参数, 而且其返回值要求是左值,这个条件决定了它不能以值返回

    前增量与后增量的区别

    1. 使用前增量时,对对象(操作数)进行增量修改,然后再返回该对象。
    2. 使用后增量时,必须在增量之前返回原有的对象值。
    3. 后增量运算符中的参数int只是为了区别前增量与后增量, 除此之外没有任何作用。
    4. 前后增量操作的意义,决定了其不同的返回方式。前增量运算符返回引用,后增量运算符返回值。

    阶段总结

    1. 操作符重载把操作符的作用扩展到对象类型
    2. 为了访问类的保护对象,需要把重载函数定义成友员
    3. 操作符重载可以是成员函数
    4. 前增量和后增量通过一个形参区分
    • 作业,实现一个虚数
    • 虚数=实部+虚部
    #include<iostream>
    #include<math.h>
    using namespace std;
    class complex
    {
        private:
            double real;
            double imag;
        public:
            complex()
            {
                real=5;
                imag=5;
            }
            complex(double m)
            {
                real=m;
                imag=0;
            }
            complex(double r,double n)
            {
                real=r;
                imag=n;
            }
            double displayreal()
            {
                return real;
            }
            double displayimag()
            {
                return imag;
            }
            complex operator+(complex c);
            complex operator-(complex c);
    
    };
    complex complex::operator+(complex c)
    {
         complex tm;
         tm.real=real+c.real;
         tm.imag=imag+c.imag;
        return tm;
    }
    complex complex::operator-(complex c)
    {
        complex tm;
        tm.real=real-c.real;
        tm.imag=imag-c.imag;
        return tm;
    }
    int main()
    {
        complex a1,b1,c1;
        c1=a1+b1;
        cout<<c1.displayreal()<<"+"<<c1.displayimag()<<"*"<<"i"<<endl;
        return 0;
    }
    //根据定义的参数形式,寻找相匹配的函数
    //此时结果为:10+10*i;
    
    

    相关文章

      网友评论

          本文标题:C++重载

          本文链接:https://www.haomeiwen.com/subject/ucckpttx.html