




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
ObjectivesTobeabletodefinemixedarithmeticofuser-definedtypesandbuilt-intypesTobeabletooverloadcombinatorialoperatorsTo
understandhowtoconvertobjectsfromuser-definedtypestobuilt-intypes01CombinatorialOperators03TypeConversion02MixedArithmetic04CaseStudy:Operators<<and>>01OverloadingCombinatorialOperatorsCombinatorialOperatorsForabuilt-intype,forexample,aisaninteger,ifa+=
5,itmeansa=a+5.Forexample,complexc1,c2;c2+=c1;Themeaningsofsomebuilt-inoperatorsaredefinedtobeequivalenttosomecombinationofotheroperatorsonthesameparameters.Canthecombinationaloperatorsworkontheuser-definedtypes?OverloadingCombinatorialOperatorsCaseStudy1Accordingtothetestprogram.Definethemeaningofoperator+=.intmain(){complexc1(20.3,11.3);complexc2;c2+=c1;c2.print();return0;}class
complex{public:complex(double
r=0.0,double
i=0.0):real(r),imag(i){}complexoperator+(complex&c);complex&operator=(const
complex&c);complex&operator+=(complex&c);voidprint()const;private:
doublereal,imag;};OverloadingCombinatorialOperatorscomplex
complex::operator+(complex&c){complextemp;temp.real=this->real+c.real;temp.imag=this->imag+c.imag;returntemp;}complex&complex::operator=(const
complex&c){if(this!=&c){this->real=c.real;this->imag=c.imag;}return*this;}complex&complex::operator+=(complex&c){*this=*this+c;return*this;}complex&complex::operator+=(complex&c){this->real+=c.real;this->imag+=c.imag;return*this;}02MixedArithmeticcomplexc1,c2(3.0,4.0);doubled=5.2;c2=c1+d;c2=d+c1;MixedArithmeticdouble(abuilt-intype)complex(user-definedtype)c1+dMemberfunction:c1.operator+(d)Non-memberfunction:operator+(c1,d)d+c1Memberfunction:d.operator+(c1)Non-memberfunction:operator+(d,c1)Mixedauser-definedtypes
withabuilt-intypeAnoperatorfunctioncanbedefinedforthemixedarithmeticofauser-definedtypeandbuilt-intype.MixedArithmeticAnoperatorfunctionmusteitherbememberofauser-definedtypesornon-membertakingatleastoneparameterofauser-definedtypes.Thisensuresthatausercannotchangethemeaningofanexpression.
MemberfunctionNon-memberfunctionintmain(){complexc1,c2(3.0,4.0);doubled=5.2;c2=c1+d;c2=d+c1;return0;}c2=c1.operator+(d)c2=operator+(d,c1)HowtoDefineMixedArithmeticclass
complex{public:complex(double
r=0.0,double
i=0.0):real(r),imag(i){}complex
operator+(double);friend
complex
operator+(double,complex&);private:doublereal,imag;};complex
complex::operator+(double
dd){return
complex(this->real+dd,this->imag);}complex
operator+(double
dd,complex&c){return
complex(c.real+dd,c.imag);}intmain(){complexc1,c2(3.0,4.0);doubled=5.2;c2=c1+d;c2=d+c1;return0;}03TypeConversionTypeConversioninti=5,j;doublea=3.4;j=i+a;doubleb=i/2;Forabuilt-intype,inti=5,j;doublea=3.4;j=i+(int)a;doubleb=(double)i/2;inti=5,j;doublea=3.4;j=i+static_cast<int>(a);doubleb=static_cast<double>(i)/2;C-styleC++styleUsingstatic_castoperatorinC++TypeConversionForauser-definedtypecomplexc1;doublea,b;b=c1+a;Thecompiler
cannotautomaticallyknowhowtoconvertuser-definedtypesintobuilt-intypesorbuilt-intypestouser-definedtypes.complexc1;complex
c2=
2.4;TypeConversionTherearetwowaysfortypeconversion:Fromabuilt-intypetoauser-definedtype.Fromauser-definedtypetoabuilt-intype.
Ifaclasshasaconstructorwhichcanbecalledwithasingleargument,thenthisconstructorbecomesconversionconstructorbecausesuchaconstructorallowsautomaticconversiontotheclassbeingconstructed.
Fromabuilt-intypetoauser-definedtypeTypeConversionclass
complex{public:complex(double
r,double
i):real(r),imag(i){}private:
doublereal,imag;};intmain(){complexc1(20.3,11.3);complexc2=10.5;return0;}complex::complex(double
r){real=r;imag=0.0;}complex(double);ConversionconstructorConversionConstructorsareconstructorsthatconverttypesofitsparameterintoatypeoftheclass.Thecompilerusestheseconstructorstoperformimplicitclass-typeconversions.Built-intypeUser-definedtypeTypeConversionTheconversionconstructorcannotspecify:animplicitconversionfromauser-definedtypetoabuilt-intype.Forexample,complex(classtype)int2.aconversionfromanewclasstoapreviouslydefinedclass(withoutmodifyingthedeclarationfortheoldclass).Forexample, complex(classtype)Date(classtype)TypeConversionoperatorT()constFromauser-definedtypetoabuilt-intypeSyntaxtodefineatypeconversionoperator
Atypeconversionoperatorinaclasscanconvertthe
classtypetoTtype
(built-intypesoruser-definedtypes).Itmustbedefinedasamemberfunction.TypeConversionclass
complex{public:complex(double
r,double
i):real(r),imag(i){}complex(doubler);operator
double()const;operator
int()const;voidprint()const;private:doublereal,imag;};complex::operator
double()const{returnreal;}complex::operator
int()const{return
static_cast<int>(real);}intmain(){complexc1=5.4;c1.print();doubled=(double)c1+3.4;cout<<d<<endl;intm=static_cast<int>(c1)+10;cout<<m<<endl;return0;}complex(classtype)Built-intypeTypeConversionclass
Test1{public:Test1(int
s){a=s;}
intget()const{returna;}private:
inta;};class
Test2{public:Test2(int
i):x(i){}
operator
Test1(){return
Test1(x);}
intget()const{returnx;}private:
intx;};Test2(class)->Test1(class)intmain(){
Test2t2Obj(20);cout<<
"Test2'sobject:"
<<t2Obj.get()<<endl;
Test1t1Obj_0=40;//willcompilecout<<
"Test1'sobject:"
<<t1Obj_0.get()<<endl;
Test1t1Obj_1=(Test1)t2Obj;//conversion->copycout<<
"Test1'sobject:"
<<t1Obj_1.get()<<endl;
return0;}04CaseStudy:OverloadingOperators<<and>>CaseStudy:OverloadingOperators<<and>>class
complex{public:complex(double
r=0,double
im=0):real(r),imag(im){}friend
ostre
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 信息技術推動下的健康教育創新
- 以區塊鏈為基礎的教育行業IP保護新思路研究
- 從患者角度探討醫療器械安全管理
- AI賦能的遠程醫療服務體系構建
- 企業業務創新與區塊鏈技術的融合應用
- 2025至2030年中國腸桿市場分析及競爭策略研究報告001
- 企業如何在醫療大數據中盈利并保護隱私
- 以客戶為中心的數字化醫療品牌形象構建
- 2025年中國啟式燃氣止回閥市場調查研究報告
- 保護患者隱私與數據安全策略
- 山東省高中名校2025屆高三4月校際聯合檢測大聯考生物試題及答案
- 2025年武漢數學四調試題及答案
- 【MOOC】數學建模精講-西南交通大學 中國大學慕課MOOC答案
- 職業病防護設施與個體防護用品的使用和維護
- 中國紡織文化智慧樹知到期末考試答案2024年
- (正式版)HGT 6313-2024 化工園區智慧化評價導則
- 《給教師的100條建議》電子書
- 老視的機制及治療的研究進展
- VDA6.3的P2-7條款
- 工程聯系單表格(模板)
- 混凝土攪拌機設計論文
評論
0/150
提交評論