版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第7章運(yùn)算符重載
7.1運(yùn)算符重載規(guī)則
7.2用成員或友元函數(shù)重載運(yùn)算符
7.3幾個(gè)典型運(yùn)算符重載
7.4類類型轉(zhuǎn)換
小結(jié)
運(yùn)算符重載使得用戶自定義的數(shù)據(jù)以一種更簡潔的方式工作例如
intx,y ; y=x+y;matrixm1,m2;
//矩陣類對象
m1=Madd(m1,m2);
//調(diào)用函數(shù)計(jì)算兩個(gè)矩陣的和complexc1,c2; //復(fù)數(shù)類對象c1=Cadd(c1,c2); //調(diào)用函數(shù)計(jì)算兩個(gè)復(fù)數(shù)的和能表示為c1=c1+c2;
?能表示為m1=m1+m2;
?第7章運(yùn)算符重載定義運(yùn)算符重載函數(shù)7.1運(yùn)算符重載規(guī)則7.1.1重載運(yùn)算符的限制可以重載的運(yùn)算符
+ - * / % ^ & | ~! = < > += -= *= /= %^= &= |= << >> >>= <<= == !=<= >= && || ++ -- ->* ‘ ->[] () new delete new[] delete[]不能重載的算符
. :: .* ?: sizeof7.1運(yùn)算符重載規(guī)則7.1.1重載運(yùn)算符的限制重載運(yùn)算符函數(shù)可以對運(yùn)算符作出新的解釋,但原有基本語義不變:不改變運(yùn)算符的優(yōu)先級不改變運(yùn)算符的結(jié)合性不改變運(yùn)算符所需要的操作數(shù)不能創(chuàng)建新的運(yùn)算符7.1.2運(yùn)算符重載的語法形式
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operatorop(參數(shù)表
) {
//相對于該類定義的操作 }
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operatorop(參數(shù)表
) {
//相對于該類定義的操作 }7.1.2運(yùn)算符重載的語法形式函數(shù)返回類型
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operatorop(參數(shù)表
) {
//相對于該類定義的操作 }7.1.2運(yùn)算符重載的語法形式重載該運(yùn)算符的類
7.1.2運(yùn)算符重載的語法形式
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operatorop(參數(shù)表
) {
//相對于該類定義的操作 }被重載的運(yùn)算符
7.1.2運(yùn)算符重載的語法形式關(guān)鍵字
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operator
op(參數(shù)表
) {
//相對于該類定義的操作 }7.1.2運(yùn)算符重載的語法形式函數(shù)名
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operator
op(參數(shù)表
) {
//相對于該類定義的操作 }
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operator
op(參數(shù)表
) {
//相對于該類定義的操作 }7.1.2運(yùn)算符重載的語法形式運(yùn)算符要求的操作數(shù)7.1.2運(yùn)算符重載的語法形式
一個(gè)運(yùn)算符被重載后,原有意義沒有失去,只是定義了相對一特定類的一個(gè)新運(yùn)算符
運(yùn)算符函數(shù)是一種特殊的成員函數(shù)或友元函數(shù)
成員函數(shù)的語法形式為:
類型
類名
::operator
op(參數(shù)表
) {
//相對于該類定義的操作 }#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}
例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
7.1.2運(yùn)算符重載的語法形式#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}
重載了3個(gè)算符7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}
voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}重載了3個(gè)算符7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}
voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}voidCalculator::
operator++
(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}
voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}voidCalculator::
operator++
(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}使用系統(tǒng)預(yù)定義版本7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}使用系統(tǒng)預(yù)定義版本7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}++Counter;調(diào)用重載版本voidoperator++();7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}調(diào)用重載版本7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
++Counter;voidoperator++();#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}調(diào)用重載版本Counter.operator++();7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
voidoperator++();#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}--Counter;調(diào)用重載版本voidoperator--();7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}調(diào)用重載版本7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
--Counter;voidoperator--();#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}調(diào)用重載版本Counter.operator--();7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
voidoperator--();#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}Counter();調(diào)用重載版本voidoperator()();7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
Counter();voidoperator()();被重載的運(yùn)算符#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}Counter();Counter.operator()();7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
voidoperator()();函數(shù)參數(shù)表#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}unsignedintCalculator::operator()(){returnvalue;}7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
#include<iostream>#include<cstdlib>usingnamespacestd;classCalculator{public:Calculator(){value=0;};voidoperator++();voidoperator--();unsignedintoperator()
();private:unsignedintvalue;};intmain(){CalculatorCounter;inti;for(i=0;i<5;i++){++Counter;cout<<"\nCounter="<<Counter();}for(i=0;i<=5;i++){--Counter;cout<<"\nCounter="<<Counter();}}voidCalculator::operator++(){if(value<65535)value++;else{cout<<"\nDataoverflow!"<<endl;exit(0);}}voidCalculator::operator--(){if(value>0)value--;else{cout<<"\nDataoverflow!"<<endl;
exit(0);}}unsignedintCalculator::operator()(){returnvalue;}7.1.2運(yùn)算符重載的語法形式例7-1設(shè)計(jì)一個(gè)安全計(jì)數(shù)器
運(yùn)算符函數(shù)可以重載為成員函數(shù)或友元函數(shù)關(guān)鍵區(qū)別在于成員函數(shù)具有this指針,友元函數(shù)沒有this指針不管是成員函數(shù)還是友元函數(shù)重載,運(yùn)算符的使用方法相同。但傳遞參數(shù)的方式不同,實(shí)現(xiàn)代碼不同,應(yīng)用場合也不同7.2用成員或友元函數(shù)重載運(yùn)算符重載為成員函數(shù),解釋為:Object.operatorop()操作數(shù)由對象Object通過this指針隱含傳遞重載為友元函數(shù),解釋為:operatorop(Object)
操作數(shù)由參數(shù)表的參數(shù)Object提供7.2用成員或友元函數(shù)重載運(yùn)算符1.一元運(yùn)算符Object
op
或op
Object重載為成員函數(shù),解釋為:ObjectL.operatorop(ObjectR)
左操作數(shù)由ObjectL通過this指針傳遞,右操作數(shù)由參數(shù)ObjectR傳遞重載為友元函數(shù),解釋為:operatorop(ObjectL,ObjectR)
左右操作數(shù)都由參數(shù)傳遞
7.2用成員或友元函數(shù)重載運(yùn)算符
2.二元運(yùn)算符ObjectL
op
ObjectR
當(dāng)一元運(yùn)算符的操作數(shù),或者二元運(yùn)算符的左操作數(shù)是類的對象時(shí),定義重載算符函數(shù)為成員函數(shù)
7.2.1用成員函數(shù)重載運(yùn)算符例7-2
建立一個(gè)描述3維坐標(biāo)的類Tri_Coor,重載運(yùn)算符“+”、“++”、和“=”
,實(shí)現(xiàn)簡單的算術(shù)運(yùn)算
7.2.1用成員函數(shù)重載運(yùn)算符7.2.1用成員函數(shù)重載算符7.2.1用成員函數(shù)重載算符#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y;temp.z=z+t.z;returntemp;}TriCooroperator=(TriCoort){x=t.x;y=t.y;z=t.z;return*this;}TriCooroperator++(){x++;y++;z++;return*this;}voidshow(){cout<<x<<","<<y<<","<<z<<"\n";}voidassign(intmx,intmy,intmz){x=mx;y=my;z=mz;}private:intx,y,z; //3_dcoordinates};
intmain(){TriCoora(1,2,3),b,c;a.show();b.show();c.show();for(inti=0;i<5;i++)++b;b.show();c.assign(3,3,3);c=a+b+c;c.show();c=b=a;c.show();}
例7-2#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y;temp.z=z+t.z;returntemp;}
TriCooroperator=(TriCoort){x=t.x;y=t.y;z=t.z;return*this;}TriCooroperator++(){x++;y++;z++;return*this;}voidshow(){cout<<x<<","<<y<<","<<z<<"\n";}voidassign(intmx,intmy,intmz){x=mx;y=my;z=mz;}private:intx,y,z; //3_dcoordinates};
intmain(){TriCoora(1,2,3),b,c;a.show();b.show();c.show();for(inti=0;i<5;i++)++b;b.show();c.assign(3,3,3);c=a+b+c
;c.show();c=b=a;c.show();}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=this->x+t.x;temp.y=this->y+t.y;temp.z=this->z+t.z;returntemp;}c=a+b+c;
a+b
a.operator+(b)
↓
Object1通過匿名對象Object1返回運(yùn)算結(jié)果7.2.1用成員函數(shù)重載算符例7-27.2.1用成員函數(shù)重載算符#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y;temp.z=z+t.z;returntemp;}
TriCooroperator=(TriCoort){x=t.x;y=t.y;z=t.z;return*this;}TriCooroperator++(){x++;y++;z++;return*this;}voidshow(){cout<<x<<","<<y<<","<<z<<"\n";}voidassign(intmx,intmy,intmz){x=mx;y=my;z=mz;}private:intx,y,z; //3_dcoordinates};
intmain(){TriCoora(1,2,3),b,c;a.show();b.show();c.show();for(inti=0;i<5;i++)++b;b.show();c.assign(3,3,3);c=a+b+c
;c.show();c=b=a;c.show();}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=this->x+t.x;temp.y=this->y+t.y;temp.z=this->z+t.z;returntemp;}c=a+b+c;
a+ba.operator+(b)↓
Object1②
a+b+c
Object1+c
Object1.operator+(c)通過匿名對象Object2返回運(yùn)算結(jié)果
↓
Object2例7-2#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y;temp.z=z+t.z;returntemp;}
TriCooroperator=(TriCoort){x=t.x;y=t.y;z=t.z;return*this;}TriCooroperator++(){x++;y++;z++;return*this;}voidshow(){cout<<x<<","<<y<<","<<z<<"\n";}voidassign(intmx,intmy,intmz){x=mx;y=my;z=mz;}private:intx,y,z; //3_dcoordinates};
intmain(){TriCoora(1,2,3),b,c;a.show();b.show();c.show();for(inti=0;i<5;i++)++b;b.show();c.assign(3,3,3);c=a+b+c
;c.show();c=b=a;c.show();}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=this->x+t.x;temp.y=this->y+t.y;temp.z=this->z+t.z;returntemp;}c=a+b+c;
a+ba.operator+(b)↓
Object1②
a+b
+c
Object1+c
Object1.operator+(c)↓
Object2③
c=
a+b+c;
c=
Object2;7.2.1用成員函數(shù)重載算符例7-2#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y;temp.z=z+t.z;returntemp;}
TriCooroperator=(TriCoort){x=t.x;y=t.y;z=t.z;return*this;}TriCooroperator++(){x++;y++;z++;return*this;}voidshow(){cout<<x<<","<<y<<","<<z<<"\n";}voidassign(intmx,intmy,intmz){x=mx;y=my;z=mz;}private:intx,y,z; //3_dcoordinates};
intmain(){TriCoora(1,2,3),b,c;a.show();b.show();c.show();for(inti=0;i<5;i++)++b;b.show();c.assign(3,3,3);c=a+b+c
;c.show();c=b=a;c.show();}
TriCooroperator=(TriCoort){this->x=t.x;this->y=t.y;this->z=t.z;return*this;}c=b=a;b=ab.operator=(a)②c=bc.operator=(b)返回調(diào)用函數(shù)的對象
7.2.1用成員函數(shù)重載算符例7-2#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}
TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y;temp.z=z+t.z;returntemp;}
TriCooroperator=(TriCoort){x=t.x;y=t.y;z=t.z;return*this;}
TriCooroperator++(){x++;y++;z++;return*this;}voidshow(){cout<<x<<","<<y<<","<<z<<"\n";}voidassign(intmx,intmy,intmz){x=mx;y=my;z=mz;}private:intx,y,z; //3_dcoordinates};
intmain(){TriCoora(1,2,3),b,c;a.show();b.show();c.show();for(inti=0;i<5;i++)++b;b.show();c.assign(3,3,3);c=a+b+c
;c.show();
c=b=a;c.show();}
TriCooroperator++(){this->x++;this->y++;this->z++;return*this;}++b;++bb.operator++()沒有參數(shù)7.2.1用成員函數(shù)重載算符例7-2#include<iostream>usingnamespacestd;classTriCoor{public:TriCoor(intmx=0,intmy=0,intmz=0){x=mx;y=my;z=mz;}TriCooroperator+(TriCoort){TriCoortemp;temp.x=x+t.x;temp.y=y+t.y
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024建筑工程防水工程分包合同
- 2023-2029年中國麻仁丸行業(yè)市場深度分析及投資策略咨詢報(bào)告
- 2024年貸款續(xù)期:借款合同標(biāo)準(zhǔn)格式3篇
- 二零二五年GRC裝飾構(gòu)件行業(yè)論壇與學(xué)術(shù)交流合同
- 2025版農(nóng)業(yè)種子保險(xiǎn)與風(fēng)險(xiǎn)規(guī)避合同3篇
- 二零二五年度idc機(jī)房租賃與數(shù)據(jù)中心網(wǎng)絡(luò)安全保障合同3篇
- 2024汽車安全帶維修與更換服務(wù)合同
- 2025版離婚協(xié)議書專業(yè)起草與婚姻解除后財(cái)產(chǎn)分配合同3篇
- 2025年無機(jī)基礎(chǔ)化學(xué)原料項(xiàng)目發(fā)展計(jì)劃
- 2024年高端魚種魚苗批發(fā)與養(yǎng)殖合作框架協(xié)議3篇
- 體檢營銷話術(shù)與技巧培訓(xùn)
- TSG 07-2019電梯安裝修理維護(hù)質(zhì)量保證手冊程序文件制度文件表單一整套
- 2023-2024學(xué)年浙江省杭州市西湖區(qū)五年級(上)期末數(shù)學(xué)試卷
- 建設(shè)工程造價(jià)案例分析-形成性考核2(占形考總分25%)-國開(SC)-參考資料
- 《期貨市場發(fā)展之》課件
- 酒店旅游業(yè)OTA平臺整合營銷推廣策略
- 淋巴水腫康復(fù)治療技術(shù)
- 2024年國家公務(wù)員考試《申論》真題(副省級)及參考答案
- 系統(tǒng)解剖學(xué)(南方醫(yī)科大學(xué))智慧樹知到期末考試答案章節(jié)答案2024年南方醫(yī)科大學(xué)
- 《生物質(zhì)熱電聯(lián)產(chǎn)工程設(shè)計(jì)規(guī)范》
- 科研倫理與學(xué)術(shù)規(guī)范期末考試
評論
0/150
提交評論