zoukankan      html  css  js  c++  java
  • C++运算符重载(Operator overloading)

    运算符重载(Operator overloading)

    目的:重载加号运算符能让对象之间进行运算

    定义重载运算符相当于定义一个函数,要用 operator 关键字开始

    SYNTAX

    返回类型 operator 运算符(参数列表)

      

    注:重载后不改变运算顺序

    重载后没有修改运算符优先级

    不改变运算变量的个数

    例:重载加号“+”运算符(作为成员函数)

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Integer
     5 {
     6     public:
     7         void setvalue(int val)
     8         {
     9             value = val;
    10         }
    11         int getvalue(void)
    12         {
    13             return value;
    14         }
    15         Integer operator+(const Integer&in)
    16         {
    17             Integer integer;
    18             integer.value = this->value + in.value;
    19             return integer;
    20         }
    21     private:
    22         int value;
    23 
    24 };
    25 int main()
    26 {
    27     Integer int1,int2,int3;
    28     int val;
    29     int1.setvalue(3);
    30     val = int1.getvalue();
    31     cout << val << endl;
    32 
    33     int2.setvalue(5);
    34     val = int2.getvalue();
    35     cout << val << endl;
    36 
    37     int3 = int1 + int2;
    38     val = int3.getvalue();
    39     cout << val << endl;
    40     return 0;
    41 }

     例:作为友元函数重载“+”运算符

    #include<iostream>
    using namespace std;
    
    class Integer
    {
        public:
            void setvalue(int val)
            {
                value = val;
            }
            int getvalue(void)
            {
                return value;
            }
            friend Integer operator+(const Integer&a,const Integer&b);//友元函数
        private:
            int value;
    };
    Integer operator+(const Integer&a,const Integer&b)
    {
                Integer integer;
                integer.value = a.value + b.value;
                return integer;
    }
    int main()
    {
        Integer int1,int2,int3;
        int value;
        int1.setvalue(3);
        value = int1.getvalue();
        cout << value << endl;
    
        int2.setvalue(5);
        value = int2.getvalue();
        cout << value << endl;
    
        int3 = int2 + int1;
        value = int3.getvalue();
        cout<<value<<endl;
        return 0;
    }

    例:重载“=”运算符

    #include<iostream>
    using namespace std;
    
    class Integer
    {
        public:
            void setvalue(int val)
            {
                value = val;
            }
            int getvalue(void)
            {
                return value;
            }
            Integer operator=(const Integer&in)
            {
                value = in.value;
            }
        private:
            int value;
    };
    
    int main()
    {
        Integer int1,int2;
        int value;
        int1.setvalue(4);
        value = int1.getvalue();
        cout << value << endl;
    
        int2 = int1;
        value = int2.getvalue();
        cout << value << endl;
        return 0;
    }

    例:重载输入“>>”、输出“<<” 运算符

    #include<iostream>
    using namespace std;
    
    class Time
    {
        private:
            int hour;
            int minute;
        public:
            Time()
            {
                hour = 0;
                minute = 0;
            }
            Time(int h,int m)
            {
                hour = h;
                minute = m;
            }
            friend ostream &operator<<(ostream &output, const Time &T)
            //ostream是一个类,在重载运算符的时候作为某个类的友元函数
            //cout是ostream的一个实例
            {
                output << "hour: " << T.hour << "  minute: " << T.minute ;
                return output;
            }
            friend istream &operator>>(istream &input, Time &T) //不能加const
            {
                input >> T.hour >> T.minute ;
                return input;
            }
    };
    int main()
    {
        Time T1(10,20),T2(18,25),T3;
        cout << T1 << endl;
        cout << T2 << endl;
        cin >> T3;
        cout << T3 << endl;
        return 0;
    }

    不能重载的运算符

    .            (点运算符)

    ->  (成员指针访问运算符)

    ::   (域运算符)

    sizeof (长度运算符)

    ? :       (三元运算符)

    只能通过成员函数重载的运算符

    =:赋值运算符

    ():函数调用运算符

    [ ]: 下标运算符

    -> : 通过指针访问类成员的运算符

  • 相关阅读:
    Linux下的 .o、.a、.so文件
    第三章、主机规划与磁盘分区
    debian linux中文桌面系统安装
    C++开源库,欢迎补充。
    C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编号)
    C# CPU,硬盘,mac地址灯本地信息查询
    打造属于自己的支持版本迭代的Asp.Net Web Api Route
    PreApplicationStartMethodAttribute的使用
    Web Api in Orchard
    Dependency Injection in ASP.NET Web API 2 Using Unity
  • 原文地址:https://www.cnblogs.com/Hfolsvh/p/12805127.html
Copyright © 2011-2022 走看看