九九热这里直有精品,1区二区三区在线播放,玖玖爱在线观看资源,国产aⅴ综合网,午夜福利男女,日本亚洲欧美三级,日韩无码黄色导航,内射少妇13区,中文字幕高清网

您身邊的軟件定制專家--9年開發(fā)經(jīng)驗為您護航

18678812288
0531-88887250

C++ 操作符重載-濟南軟件開發(fā)

文章作者:濟南軟件開發(fā) 時間:2016年11月08日

  1.什么是操作符重載

  可以使用分詞將操作符重載理解為:操作符+重載。

  C++中的操作符很多,如+,-,*,\等等。

  C++中的重載也是C++中面向?qū)ο蠖鄳B(tài)的體現(xiàn)。

  簡單說操作符重載:

  C++中有:int a=2+3;  那么a=5

  操作符重載可以實現(xiàn)對自定義類型的操作:

  復(fù)制代碼

  1 #include <iostream>

  2 using namespace std;

  3

  4 class Point{

  5 public:

  6     int x;

  7     int y;

  8     Point(int _x,int _y):x(_x),y(_y){

  9     }

  10

  11     Point operator+(Point &p){

  12         int t1=this->x+p.x;

  13         int t2=this->y+p.y;

  14         Point t(t1,t2);

  15         return t;

  16     }

  17 };

  18

  19 int main()

  20 {

  21     Point p1(1,3);

  22     Point p2(2,6);

  23     Point p3 = p1+p2;

  24     cout<<"p3:("<<p3.x<<","<<p3.y<<")";     ///執(zhí)行輸出:p3:(3,9)

  25     return 0;

  26 }

  復(fù)制代碼

  2.操作符重載的方式

  操作符重載的實現(xiàn)方式有兩種,即通過“友元函數(shù)”或者“類成員函數(shù)”。如下面代碼顯示了這兩種操作符重載:

  復(fù)制代碼

  1 class Point{

  2 public:

  3     int x;

  4     int y;

  5     Point(int _x,int _y);

  6

  7     Point operator+(Point &p);  ///類成員函數(shù),類成員函數(shù)可以使用this指針獲取自身對象

  8

  9     friend int operator*(Point &p1,Point &p2);  ///友元函數(shù)

  10 };

  復(fù)制代碼

  可以看出,一個重載“+”,一個重載“*”,都是雙目運算符,但是類成員函數(shù)只有一個參數(shù)。這是因為類成員函數(shù)可以使用this指針獲取自身的對象,而友元函數(shù)則不行。

  所以類成員實現(xiàn)操作符重載需要的形式參數(shù)比原來少一個,這樣使用類成員函數(shù)實現(xiàn)一元操作符就不需要參數(shù)了。

  3.操作符重載例子

  1 #include <iostream>

  2 using namespace std;

  3

  4 class Point{

  5 public:

  6     int x;

  7     int y;

  8     Point(int _x,int _y):x(_x),y(_y){

  9     }

  10

  11     Point operator+(Point &p){  ///實現(xiàn)坐標向量加

  12         int t1=this->x+p.x;

  13         int t2=this->y+p.y;

  14         Point t(t1,t2);

  15         return t;

  16     }

  17

  18     friend int operator*(Point &p1,Point &p2);  ///實現(xiàn)內(nèi)積

  19 };

  20

  21 int operator*(Point &p1,Point &p2)

  22 {

  23     return (p1.x*p2.x)+(p1.y*p2.y);

  24 }

  25

  26 int main()

  27 {

  28     Point p1(1,3);

  29     Point p2(2,6);

  30     cout<<p1*p2<<endl;  ///輸出內(nèi)積:20

  31

  32     Point p3 = p1+p2;

  33     cout<<"p3:("<<p3.x<<","<<p3.y<<")"<<endl;   ///輸出坐標和:(3,9)

  34     return 0;

  35 }

  復(fù)制代碼

  4.重載操作符注意事項

  (1)重載操作符必須有一個類類型的參數(shù)。也就是說不能 int  operator+(int,int);

  (2)操作符的優(yōu)先級和結(jié)核性是固定的。

  (3)不在具有短路求值特性。如&&、||等,兩個數(shù)都會進行求值,而且順序不定。

  (4)不要重載具有內(nèi)置含義的操作符。重載操作符主要是彌補普通操作符對類類型數(shù)據(jù)的作用。像賦值操作符、取地址操作符、逗號操作符等對類類型操作數(shù)都有默認的含義。

  (5)只能對已有的C++運算符進行重載,不允許用戶自己定義新的運算符。

  (6)絕大部分的運算符可重載,除了成員訪問運算符.,作用域運算符::,長度運算符sizeof以及條件運算符?:。

  (7)運算符重載后不能改變運算符的操作對象(操作數(shù))的個數(shù)。

  5.特例操作符"++"、"--"的重載

  自增、自減操作符有前后之分,通過一個無意義的整數(shù)參數(shù)來區(qū)分。無參數(shù)的是前置運算符,帶參數(shù)的是后置運算符。

  如:

  int operator++();  //前置

  int operator++(int x);  //后置

  自增操作符實例

  1 #include <iostream>

  2 using namespace std;

  3

  4 class Array{

  5 public:

  6     Array(int _num){    ///初始化一個數(shù)組,大小為_num

  7         this->a=new int[_num];

  8         this->num=_num;

  9         this->pos=0;

  10         for(int i=0;i<num;i=i+1)

  11             *((this->a)+i)=i;

  12     }

  13

  14     ~Array(){

  15         delete [] a;

  16     }

  17

  18     int operator++(){  ///++前置

  19         if(pos == num-1){

  20             cout<<"Fuck"<<endl;

  21             return 0;

  22         }

  23         return *((this->a)+(++pos));

  24     }

  25

  26     int operator++(int x){   ///++后置

  27         if(pos == num-1){

  28             cout<<"Fuck"<<endl;

  29             return 0;

  30         }

  31         return *((this->a)+(pos++));

  32     }

  33

  34     int *a;

  35     int pos;

  36     int num;

  37 };

  38

  39 int main()

  40 {

  41     Array array(100);

  42     cout<<"pos="<<array.pos<<endl;

  43     cout<<"pre_position:"<<(++array)<<endl;

  44     cout<<"pos="<<array.pos<<endl;

  45     cout<<"********************************************************"<<endl;

  46

  47     cout<<"pos="<<array.pos<<endl;

  48     cout<<"post_position:"<<(array++)<<endl;

  49     cout<<"pos="<<array.pos<<endl;

  50     cout<<"********************************************************"<<endl;

  51     return 0;

  52 }


想要了解更多詳情歡迎來電咨詢18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。

建德市| 永修县| 张掖市| 鄂伦春自治旗| 高邮市| 栖霞市| 合阳县| 宁陵县| 翁牛特旗| 万荣县| 洛阳市| 安宁市| 顺义区| 克山县| 睢宁县| 祁阳县| 大冶市| 新安县| 霍州市| 明光市| 泉州市| 勐海县| 含山县| 苍南县| 安宁市| 紫金县| 化隆| 邯郸市| 方山县| 绥宁县| 松江区| 金堂县| 岳池县| 什邡市| 嘉黎县| 壤塘县| 颍上县| 资阳市| 沧州市| 南阳市| 旬阳县|