zoukankan      html  css  js  c++  java
  • c++之运算符重载一

    可以重载的运算符

    +  -  *  /  %  ^  &  |  ~

    !  =  <  >  +=  -=  *=  /=  %

    ^=  &=  |=  <<  >>  >>=  <<=  ==  !=

    <=  >=  &&  ||  ++  --  ->*  ‘  ->

    []  ()  new  delete  new[]  delete[] 

    不能重载的算符 

    .  ::  .*  ?:  sizeof

    重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:

    不改变运算符的优先级
    不改变运算符的结合性
    不改变运算符所需要的操作数
    不能创建新的运算符
     1 #include <iostream>
     2 #include <cstdlib>
     3 using namespace std;
     4 
     5 class A
     6 {
     7 public:
     8     A()
     9     {
    10         value = 0;
    11     };
    12     //声明重载的运算符函数
    13     void operator ++();
    14     void operator --();
    15     unsigned int operator ()();
    16 
    17 private:
    18     unsigned int value;
    19 };
    20 
    21 void A::operator++()
    22 {
    23     if(value<65535)
    24     {
    25         value ++;    //使用了系统预定义版本
    26     }else
    27     {
    28         cout << "
    Data overflow!"<<endl;
    29         exit(0);
    30     }
    31 }
    32 void A::operator--()
    33 {
    34     if(value>0)
    35     {
    36         value --;    //使用了系统预定义版本
    37     }else
    38     {
    39         cout << "
    Data overflow!" << endl;
    40         exit(0);
    41     }
    42 }
    43 unsigned int A::operator()()
    44 {
    45     return value;
    46 }
    47 void main2()
    48 {
    49     A a;
    50     int i;
    51     for(i=0;i<5;i++)
    52     {
    53         ++a;    //调用重载版本
    54         cout << "
    a=" << a();    //a()调用重载的()
    55     }
    56     for(i=0;i<=5;i++)
    57     {
    58         --a;    //调用重载版本
    59         cout << "
    a=" << a();    //a()调用重载的()
    60     }
    61 }

    使用成员函数来重载运算符

     1 #include <iostream>
     2 using namespace std;
     3 
     4 //用成员函数重载 + 、++ 、= 运算符,实现一个简单的三维坐标的计算
     5 class ThrCoor
     6 {
     7 public:
     8     ThrCoor(int x=0,int y=0,int z=0)
     9     {
    10         this->x = x;
    11         this->y = y;
    12         this->z = z;
    13     }
    14     void  operator ++()
    15     {
    16         x ++;
    17         y ++;
    18         z ++;
    19     }
    20     ThrCoor operator +(ThrCoor t)
    21     {
    22         ThrCoor temp;
    23         temp.x = x + t.x;
    24         temp.y = y + t.y;
    25         temp.z = z + t.z;
    26         return temp;
    27     }
    28     ThrCoor /*dengyu*/operator =(ThrCoor t)
    29     {
    30         x = t.x;
    31         y = t.y;
    32         z = t.z;
    33         return *this;
    34     }
    35     void show()
    36     {
    37         cout << x << '	' << y << '	' << z << endl;
    38     }
    39     void set(int x,int y,int z)
    40     {
    41         this->x = x;
    42         this->y = y;
    43         this->z = z;
    44     }
    45 private:
    46     int x,y,z;
    47 };
    48 
    49 void main()
    50 {
    51     ThrCoor a(1,2,3),b,c;
    52     a.show();
    53     b.show();
    54     c.show();
    55     for(int i=0;i<5;i++)
    56     {
    57         b++;
    58     }
    59     b.show();
    60     c.set(3,3,3);
    61     c = a + b + c;
    62     c.show();
    63     c = b = a;
    64     //c.dengyu(b.dengyu(a));
    65     c.show();
    66 }
     1 #include <iostream>
     2 using namespace std;
     3 
     4 //用成员函数重载 + 、++ 、= 运算符,实现一个简单的三维坐标的计算
     5 class ThrCoor
     6 {
     7 public:
     8     ThrCoor(int x=0,int y=0,int z=0)
     9     {
    10         this->x = x;
    11         this->y = y;
    12         this->z = z;
    13     }
    14     void  operator ++()
    15     {
    16         x ++;
    17         y ++;
    18         z ++;
    19     }
    20     ThrCoor operator +(ThrCoor t)
    21     {
    22         ThrCoor temp;
    23         temp.x = x + t.x;
    24         temp.y = y + t.y;
    25         temp.z = z + t.z;
    26         return temp;
    27     }
    28     ThrCoor /*dengyu*/operator =(ThrCoor t)
    29     {
    30         x = t.x;
    31         y = t.y;
    32         z = t.z;
    33         return *this;
    34     }
    35     void show()
    36     {
    37         cout << x << '	' << y << '	' << z << endl;
    38     }
    39     void set(int x,int y,int z)
    40     {
    41         this->x = x;
    42         this->y = y;
    43         this->z = z;
    44     }
    45 private:
    46     int x,y,z;
    47 };
    48 
    49 void main()
    50 {
    51     ThrCoor a(1,2,3),b,c;
    52     a.show();
    53     b.show();
    54     c.show();
    55     for(int i=0;i<5;i++)
    56     {
    57         b++;
    58     }
    59     b.show();
    60     c.set(3,3,3);
    61     c = a + b + c;
    62     c.show();
    63     c = b = a;
    64     //c.dengyu(b.dengyu(a));
    65     c.show();
    66 }

    使用友元函数重载运算符

    在第一个参数需要隐式转换的情形下,使用友元函数重载

        运算符是正确的选择

    友元函数没有 this 指针,所需操作数都必须在参数表显式

        声明,很容易实现类型的隐式转换

    c++中不能用友元函数重载的运算符有

      =    ()    []    ->

     1 #include <iostream>
     2 using namespace std;
     3 //用友元函数重载运算符
     4 //友元函数重载运算符常用于运算符的左右操作数的类型不同的情况
     5 
     6 class Complex
     7 {
     8 public:
     9     Complex(double real=0,double image=0)
    10     {
    11         this->real = real;
    12         this->image = image;
    13     }
    14     Complex(int a)
    15     {
    16         this->real = a;
    17         this->image = 0;
    18     }
    19     friend Complex operator +(const Complex &real,const Complex &image);
    20     friend Complex operator -(Complex &real,Complex &image);
    21     friend Complex operator -(Complex &real);
    22     void print();
    23 private:
    24     double real,image;
    25 };
    26 
    27 Complex operator+(const Complex &real,const Complex &image)
    28 {
    29     double r = real.real + image.real;
    30     double i = real.image + image.image;
    31     return Complex(r,i);
    32 }
    33 Complex operator-(Complex &real,Complex &image)
    34 {
    35     double r = real.real - image.real;
    36     double i = real.image - image.image;
    37     return Complex(r,i);
    38 }
    39 Complex operator-(Complex &real)
    40 {
    41     double r = -real.real;
    42     double i = -real.image;
    43     //构造返回对象
    44     return Complex(r,i);
    45 }
    46 void Complex::print()
    47 {
    48     cout << "(" << real << "," << image << ")" << endl;
    49 }
    50 
    51 void main()
    52 {
    53     Complex c1(2.5,3.7),c2(4.2,6.5),c;
    54     c = c1 - c2;
    55     c.print();
    56     //构造形参对象,把整形常数25,转换成Complex(25,0);
    57     c = 25 + c2;
    58     c.print();
    59     c = c2 + 25;
    60     c.print();
    61     c = -c1;
    62     c.print();
    63 }
  • 相关阅读:
    List of Examples Converting XNA 3.1 to XNA 4.0
    XNA程序开发常用到的一些代码汇总
    在WCF中使用Flag Enumerations
    能飞过海洋的却只有海鸥【转载】
    Variant类型转换成CString代码
    一种漂亮的自绘菜单
    转 在OpenCV中用cvCalibrateCamera2进行相机标定(附程序)
    COM组件设计与应用之VC6中用ATL写组件
    vc的菜单,工具栏
    (餐饮)写一些开店的经验转
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4322118.html
Copyright © 2011-2022 走看看