版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第三章 細(xì)部檢視類別3-1指定物件3-2傳遞物件給函數(shù)3-3從函數(shù)中傳回物件3-4簡(jiǎn)介夥伴函數(shù)第三章 細(xì)部檢視類別3-1指定物件13-1指定物件物件之間的指定(assignment)運(yùn)算intx=5,y;y=x;myclassob1,ob2;ob1=ob2;//whathappen?3-1指定物件物件之間的指定(assignment)運(yùn)算2Whathappen?structtest{inta,b;};voidmain(){ testt1,t2; t1.a=10;t1.b=20;
t2=t1; cout<<t2.a<<““<<t2.b<<endl;}bit-wisecopy(逐位元的複製)a10b20t1t2a10b20Whathappen?structtest{int3物件間的指定(assignment)#include<iostream>usingnamespacestd;classmyclass{ inta,b;public:myclass(){} myclass(intx,inty){a=x;b=y;} voidshow(){cout<<a<<""<<b;}};voidmain(){ myclassob1(5,3),ob2;
ob2=ob1
;ob2.show();}Q1:Output?Ans:bitwisecopyforall datamembersQ2:這個(gè)程式有無(wú)bug?物件間的指定(assignment)#include<io4範(fàn)例一:指定敘述是否需同一類型?classmyclass{inta,b;public:voidshow();}; voidmyclass::show(){cout<<a<<b<<endl;}classyourclass{inta,b;public:voidshow();}; voidyourclass::show(){cout<<a<<b<<endl;}intmain(){ myclassob1; yourclassob2; ob1=ob2; return0;}範(fàn)例一:指定敘述是否需同一類型?classmyclass5範(fàn)例二:物件中有複合資料classstack{ intstk[10]; inttos;public: stack(){ tos=0; for(inti=0;i<10;i++) stk[i]=0; }voidpush(intnum) { stk[tos]=num; tos++; }
intpop(){ returnstk[--tos];
}voidprint() { for(inti=0;i<tos;i++) cout<<stk[i]<<endl;
}};範(fàn)例二:物件中有複合資料classstack{int6範(fàn)例二:物件中有複合資料intmain(){ stacks1,s2; s1.push(10);s1.push(20);s1.print(); s2=s1;s2.print();return0;}範(fàn)例二:物件中有複合資料intmain(){7範(fàn)例三:如果物件中有指標(biāo)classstrtype{ char*p;intlen;public: strtype(){ p=newchar[10]; len=1; p[0]='\0'; } strtype(char*s){ p=newchar[strlen(s)+1]; len=strlen(s)+1; p=strcpy(p,s); } ~strtype(){delete[]p;} voidsetChar(intpos,charc){p[pos]=c;}voidshow(){cout<<len<<endl;}};voidmain(){ strtypes1(“Hello”),s2;
s2=s1; s2.setChar(1,‘x’);s1.show();}Q:以下程式會(huì)發(fā)生哪些問(wèn)題?範(fàn)例三:如果物件中有指標(biāo)classstrtype{Q:8範(fàn)例三:Problem1p:0x00f0cflen:5s1Hellos2(1)執(zhí)行s2=s1;p:0x00f0cflen:5(2)執(zhí)行s2.setChar(1,’x’)xWhat’swrong?strtypes1(“Hello”),s2;範(fàn)例三:Problem1p:0x00f0cflen:9範(fàn)例三:Problem2voidmain(){ strtypes1(“Hello”); strtypes2; s2=s1;}p:0x00f0cflen:5s1Hellos2p:0x00f0cflen:5s1,s2何時(shí)被消滅?物件消滅時(shí)會(huì)發(fā)生何事?範(fàn)例三:Problem2voidmain(){p:10解決方式voidmain(){ strtypes1(“Hello”); strtypes2; s2=s1;}voidmain(){ strtypes1(“Hello”); strtypes2; s2.set(s1);}p:0x00f0cflen:5s1Hellop0x00c800len:5s2Hellonew!解決方式voidmain(){voidmain(){11解決方式請(qǐng)寫operator=classstrtype{ …… strtype&operator=(conststrtype&s){ p=newchar[s.len+1]; strcpy(p,s.p);len=s.len; return*this; }};解決方式請(qǐng)寫operator=12解決方式classstrtype{ char*p;intlen;public: strtype(){ p=newchar[10]; len=1; p[0]='\0'; } strtype(char*s){p=newchar[strlen(s)+1]; len=strlen(s)+1; p=strcpy(p,s); }Q:以下程式會(huì)發(fā)生哪些問(wèn)題?~strtype(){delete[]p;}
strtype&
operator=(conststrtype&s){p=newchar[s.len+1];strcpy(p,s.p);len=s.len;return*this; }voidsetChar(intpos,charc){p[pos]=c;}voidshow(){cout<<len<<endl;}};解決方式classstrtype{Q:以下程式會(huì)發(fā)生哪13解決方式voidmain(){ strtypes1(“Hello”),s2; s2=s1; s2.setChar(1,‘x’);s1.show();}解決方式voidmain(){143-2傳遞物件給函數(shù)回想從前:參數(shù)傳遞的方式voidmain(){ intx=5;fun1(x);fun2(&x);fun3(x);}voidfun1(inta){…}//callby???voidfun2(int*p){…}//callby???voidfun3(int&r){…}//callby???3-2傳遞物件給函數(shù)回想從前:參數(shù)傳遞的方式15基礎(chǔ):
物件的生成與constructorsclasssamp{ inti;public: samp(){i=0;} samp(intn){i=n;} samp(constsamp&s){i=s.i;} show(){cout<<i<<endl;}};voidmain(){ sampob1,ob2(3),ob3(ob2); ……}基礎(chǔ):
物件的生成與constructorsclass16範(fàn)例一:1classsamp{2 inti;3 public:4 samp(intn){i=n;}5 intget_i(){returni;}6};7intsqr_it(sampo){returno.get_i()*o.get_i();}8voidmain(){9 sampa(10);10 cout<<sqr_it(a)<<endl;11cout<<a.get_i()<<endl;}呼叫sqr_it(a)後生成物件oo將會(huì)以a為初值同sampo(a);
會(huì)呼叫那一種constructor?沒(méi)有對(duì)應(yīng)的constructor該如何?call-by-valuei:10i:10aosqr_it(a)sqr_it(sampo)範(fàn)例一:1classsamp{呼叫sqr_it(a)17Constructorvs.函數(shù)呼叫1classsamp{2 inti;3 public:samp(intn){i=n;}samp(constsamp&s){cout<<“copy\n”;i=s.i;}intget_i(){returni;}7};8intsqr_it(sampo){returno.get_i()*o.get_i();}9voidmain(){10 sampa(10);11 cout<<sqr_it(a)<<endl;12}呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?(5)CopyconstructorConstructorvs.函數(shù)呼叫1classsam18EX:哪一個(gè)constructor被呼叫?1classsamp{2 inti;3 public:samp(intn){i=n;}samp(constsamp&s){cout<<“copy\n”;i=s.i;}intget_i(){returni;}7};8intsqr_it(sampo){returno.get_i()*o.get_i();}9voidmain(){10 sampa(10);11 cout<<sqr_it(12)<<endl;12}呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?(4)EX:哪一個(gè)constructor被呼叫?1class19範(fàn)例二:程式的輸出為何?classsamp{ inti;public: samp(intn){i=n;} voidset(intn){i=n;} intget_i(){returni;} voidprint(){cout<<i;}};voidsqrt_it(sampo){o.set(o.get_i()*o.get_i());}voidmain(){ sampa(10);sqrt_it(a);a.print();}範(fàn)例二:程式的輸出為何?classsamp{20範(fàn)例三:程式的輸出為何?classsamp{ inti;public: samp(intn){i=n;} voidset(intn){i=n;} intget_i(){returni;} voidprint(){cout<<i;}};voidsqrt_it(samp*o){o->set(o->get_i()*o->get_i());}voidmain(){ sampa(10);sqrt_it(&a);a.print();}呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?100指標(biāo)之外,有無(wú)其他選擇?範(fàn)例三:程式的輸出為何?classsamp{呼叫時(shí),哪21EX:call-by-reference呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?100classsamp{ inti;public: samp(intn){i=n;} voidset(intn){i=n;} intget_i(){returni;} voidprint(){cout<<i;}};voidsqrt_it(samp&o){o.set(o.get_i()*o.get_i());}voidmain(){ sampa(10);sqrt_it(a);a.print();}EX:call-by-reference呼叫時(shí),哪一個(gè)建22範(fàn)例四:
CopyConstructor的重要性classstrtype{ char*p;intlen;public: strtype(char*s){p=newchar[strlen(s)+1];……} ~strtype(){delete[]p;} voidsetChar(intpos,charc){p[pos]=c;}};voidshowMsg(strtypes){/*dosomething…*/}voidmain(){ strtypes1(“NTUwelcomesyou!”); showMsg(s1);}nocopyconstructor:bitwisecopy範(fàn)例四:
CopyConstructor的重要性cla23結(jié)論如果class中的資料成員牽涉到動(dòng)態(tài)記憶體配置,則以下成員函數(shù)不可省:copyconstructoroperator=()或set()儘量使用call-by-reference,why?voidshowMsg(conststrtype&str);voidshowMsg(strtypestr);結(jié)論如果class中的資料成員牽涉到動(dòng)態(tài)記憶體配置,則以下成243-3從函數(shù)中傳回物件回想從前voidfun1(){…}intfun2(){…}doublefun3(){…}char*fun4(){…}myclassfun5(){…….}3-3從函數(shù)中傳回物件回想從前25範(fàn)例一classsamp{ chars[80];public: voidshow(){cout<<s<<endl;} voidset(char*str){strcpy(s,str);}};sampinput(){ chars[80];cin>>s; sampstr;str.set(s);
returnstr;}voidmain(){sampob;ob=input();………}產(chǎn)生str產(chǎn)生暫時(shí)物件使用暫時(shí)物件設(shè)定obreturnstr;返回mainob=input();呼叫input()暫時(shí)物件消滅下一道指令範(fàn)例一classsamp{產(chǎn)生str產(chǎn)生使用return26描述當(dāng)物件被函數(shù)傳回時(shí)會(huì)自動(dòng)建立一個(gè)暫時(shí)物件(temporaryobject),內(nèi)容即為回傳值(returnvalue)。描述當(dāng)物件被函數(shù)傳回時(shí)會(huì)自動(dòng)建立一個(gè)暫時(shí)物件(tempor27深入剖析物件的回傳classsamp{ chars[80];public: samp(){cout<<“default\n”;} samp(constsamp&ob){ cout<<“copy\n”;strcpy(s,ob.s);} ~samp(){cout<<“destroy\n”;} voidshow(){cout<<s<<endl;} voidset(char*str){strcpy(s,str);}};sampinput(){chars[80];cin>>s; sampstr;str.set(s);returnstr;(copyconstructor)}voidmain(){sampob;ob=input();ob.show();}深入剖析物件的回傳classsamp{28範(fàn)例二:拜託你寫copyconstructor好嗎?範(fàn)例二:拜託你寫copyconstructor好嗎?293-4簡(jiǎn)介夥伴函數(shù)
(friendfunctions)什麼是夥伴函數(shù)?classmyclass{ intn,d;public: myclass(inti,intj){n=i;d=j;}};boolisfactor(myclassob){return!(ob.n%ob.d);}voidmain(){ myclassob(10,2); if(isfactor(ob))cout<<“2是10因數(shù)”;}3-4簡(jiǎn)介夥伴函數(shù)
(friendfunctions)30甚麼是夥伴函數(shù)?classmyclass{ intn,d;public: myclass(inti,intj){n=i;d=j;}
friendboolisfactor(myclassob);};boolisfactor(myclassob){return!(ob.n%ob.d);}voidmain(){ myclassob(10,2); if(isfactor(ob))cout<<“2是10因數(shù)”;}甚麼是夥伴函數(shù)?classmyclass{31夥伴函數(shù)的用途運(yùn)算子超載(operatoroverloading)operator+()oprrator<<(),operator>>()讓函數(shù)能存取二個(gè)多個(gè)不同類別的私用成員破壞封裝…….夥伴函數(shù)的用途運(yùn)算子超載(operatoroverloa32NOTE:classmyclass{ intn,d;public: myclass(inti,intj){n=i;d=j;}
friendboolisfactor(myclassob);};boolisfactor(myclassob){return!(ob.n%ob.d);}voidmain(){ myclassob(10,2); if(isfactor(ob))cout<<“2是10因數(shù)”;}NOTE:classmyclass{33NOTE:夥伴函數(shù)不會(huì)被繼承一個(gè)函數(shù)可以是多個(gè)類別的夥伴函數(shù)NOTE:夥伴函數(shù)不會(huì)被繼承34範(fàn)例一:多個(gè)類別的夥伴classtruck;//whythis?classcar{intspeed;public: car(intsp){speed=sp;}
friendintsp_greater(carc,truckt);};classtruck{ intspeed;public: truck(intsp){speed=sp;} friendintsp_greater(carc,truckt);};intsp_greater(carc,truckt){returnc.speed-t.speed;}範(fàn)例一:多個(gè)類別的夥伴classtruck;//w35範(fàn)例一:討論一定要如此嗎?破壞封裝精神main(){carc(60);truckt(50);cout<<sp_greater(c,t)<<endl;}範(fàn)例一:討論一定要如此嗎?破壞封裝精神36範(fàn)例二:將其他類別的成員函數(shù)做為夥伴函數(shù)classcar;classtruck{ intspeed;public: truck(intsp){speed=sp;} friendclasscar;};classcar{intspeed;public: car(intsp){speed=sp;} intsp_greater(truckt){ returnspeed-t.speed;}};main(){carc(60);truckt(50);cout<<c.sp_greater(t)<<endl;}範(fàn)例二:將其他類別的成員函數(shù)做為夥伴函數(shù)classcar37範(fàn)例三:friend的有用之處#include<iostream.h>classfrac{ intq,p;public: frac(intn,intm){q=n;p=m;} friendostream&operator<<(ostream&out,constfrac&ob);
};ostream&operator<<(ostream&out,constfrac&ob){ out<<ob.q<<"/"<<ob.p; returnout;}voidmain(){ fracf(3,5); cout<<f<<endl;//f.print();}範(fàn)例三:friend的有用之處#include<iost38NOTE:#include<iostream.h>//usingnamespacestd;classcoord{intx,y;public:coord(){x=0;y=0;} coord(inta,intb){x=a;y=b;}friendostream&operator<<(ostream&stream,coord&ob);friendistream&operator>>(istream&stream,coord&ob);};NOTE:#include<iostream.h>39NOTE:ostream&operator<<(ostream&stream,coord&ob) {stream<<ob.x<<","<<ob.y<<'\n'; returnstream;}istream&operator>>(istream&stream,coord&ob) {cout<<"Entercoordinates\n"; stream>>ob.x>>ob.y; returnstream;}intmain(){coorda(1,1),b(2,3);cout<<a<<b;cin>>a;cout<<a;return0;}NOTE:ostream&operator<<(ostre40課後練習(xí)&綜合學(xué)習(xí)課後練習(xí)&綜合學(xué)習(xí)41第三章 細(xì)部檢視類別3-1指定物件3-2傳遞物件給函數(shù)3-3從函數(shù)中傳回物件3-4簡(jiǎn)介夥伴函數(shù)第三章 細(xì)部檢視類別3-1指定物件423-1指定物件物件之間的指定(assignment)運(yùn)算intx=5,y;y=x;myclassob1,ob2;ob1=ob2;//whathappen?3-1指定物件物件之間的指定(assignment)運(yùn)算43Whathappen?structtest{inta,b;};voidmain(){ testt1,t2; t1.a=10;t1.b=20;
t2=t1; cout<<t2.a<<““<<t2.b<<endl;}bit-wisecopy(逐位元的複製)a10b20t1t2a10b20Whathappen?structtest{int44物件間的指定(assignment)#include<iostream>usingnamespacestd;classmyclass{ inta,b;public:myclass(){} myclass(intx,inty){a=x;b=y;} voidshow(){cout<<a<<""<<b;}};voidmain(){ myclassob1(5,3),ob2;
ob2=ob1
;ob2.show();}Q1:Output?Ans:bitwisecopyforall datamembersQ2:這個(gè)程式有無(wú)bug?物件間的指定(assignment)#include<io45範(fàn)例一:指定敘述是否需同一類型?classmyclass{inta,b;public:voidshow();}; voidmyclass::show(){cout<<a<<b<<endl;}classyourclass{inta,b;public:voidshow();}; voidyourclass::show(){cout<<a<<b<<endl;}intmain(){ myclassob1; yourclassob2; ob1=ob2; return0;}範(fàn)例一:指定敘述是否需同一類型?classmyclass46範(fàn)例二:物件中有複合資料classstack{ intstk[10]; inttos;public: stack(){ tos=0; for(inti=0;i<10;i++) stk[i]=0; }voidpush(intnum) { stk[tos]=num; tos++; }
intpop(){ returnstk[--tos];
}voidprint() { for(inti=0;i<tos;i++) cout<<stk[i]<<endl;
}};範(fàn)例二:物件中有複合資料classstack{int47範(fàn)例二:物件中有複合資料intmain(){ stacks1,s2; s1.push(10);s1.push(20);s1.print(); s2=s1;s2.print();return0;}範(fàn)例二:物件中有複合資料intmain(){48範(fàn)例三:如果物件中有指標(biāo)classstrtype{ char*p;intlen;public: strtype(){ p=newchar[10]; len=1; p[0]='\0'; } strtype(char*s){ p=newchar[strlen(s)+1]; len=strlen(s)+1; p=strcpy(p,s); } ~strtype(){delete[]p;} voidsetChar(intpos,charc){p[pos]=c;}voidshow(){cout<<len<<endl;}};voidmain(){ strtypes1(“Hello”),s2;
s2=s1; s2.setChar(1,‘x’);s1.show();}Q:以下程式會(huì)發(fā)生哪些問(wèn)題?範(fàn)例三:如果物件中有指標(biāo)classstrtype{Q:49範(fàn)例三:Problem1p:0x00f0cflen:5s1Hellos2(1)執(zhí)行s2=s1;p:0x00f0cflen:5(2)執(zhí)行s2.setChar(1,’x’)xWhat’swrong?strtypes1(“Hello”),s2;範(fàn)例三:Problem1p:0x00f0cflen:50範(fàn)例三:Problem2voidmain(){ strtypes1(“Hello”); strtypes2; s2=s1;}p:0x00f0cflen:5s1Hellos2p:0x00f0cflen:5s1,s2何時(shí)被消滅?物件消滅時(shí)會(huì)發(fā)生何事?範(fàn)例三:Problem2voidmain(){p:51解決方式voidmain(){ strtypes1(“Hello”); strtypes2; s2=s1;}voidmain(){ strtypes1(“Hello”); strtypes2; s2.set(s1);}p:0x00f0cflen:5s1Hellop0x00c800len:5s2Hellonew!解決方式voidmain(){voidmain(){52解決方式請(qǐng)寫operator=classstrtype{ …… strtype&operator=(conststrtype&s){ p=newchar[s.len+1]; strcpy(p,s.p);len=s.len; return*this; }};解決方式請(qǐng)寫operator=53解決方式classstrtype{ char*p;intlen;public: strtype(){ p=newchar[10]; len=1; p[0]='\0'; } strtype(char*s){p=newchar[strlen(s)+1]; len=strlen(s)+1; p=strcpy(p,s); }Q:以下程式會(huì)發(fā)生哪些問(wèn)題?~strtype(){delete[]p;}
strtype&
operator=(conststrtype&s){p=newchar[s.len+1];strcpy(p,s.p);len=s.len;return*this; }voidsetChar(intpos,charc){p[pos]=c;}voidshow(){cout<<len<<endl;}};解決方式classstrtype{Q:以下程式會(huì)發(fā)生哪54解決方式voidmain(){ strtypes1(“Hello”),s2; s2=s1; s2.setChar(1,‘x’);s1.show();}解決方式voidmain(){553-2傳遞物件給函數(shù)回想從前:參數(shù)傳遞的方式voidmain(){ intx=5;fun1(x);fun2(&x);fun3(x);}voidfun1(inta){…}//callby???voidfun2(int*p){…}//callby???voidfun3(int&r){…}//callby???3-2傳遞物件給函數(shù)回想從前:參數(shù)傳遞的方式56基礎(chǔ):
物件的生成與constructorsclasssamp{ inti;public: samp(){i=0;} samp(intn){i=n;} samp(constsamp&s){i=s.i;} show(){cout<<i<<endl;}};voidmain(){ sampob1,ob2(3),ob3(ob2); ……}基礎(chǔ):
物件的生成與constructorsclass57範(fàn)例一:1classsamp{2 inti;3 public:4 samp(intn){i=n;}5 intget_i(){returni;}6};7intsqr_it(sampo){returno.get_i()*o.get_i();}8voidmain(){9 sampa(10);10 cout<<sqr_it(a)<<endl;11cout<<a.get_i()<<endl;}呼叫sqr_it(a)後生成物件oo將會(huì)以a為初值同sampo(a);
會(huì)呼叫那一種constructor?沒(méi)有對(duì)應(yīng)的constructor該如何?call-by-valuei:10i:10aosqr_it(a)sqr_it(sampo)範(fàn)例一:1classsamp{呼叫sqr_it(a)58Constructorvs.函數(shù)呼叫1classsamp{2 inti;3 public:samp(intn){i=n;}samp(constsamp&s){cout<<“copy\n”;i=s.i;}intget_i(){returni;}7};8intsqr_it(sampo){returno.get_i()*o.get_i();}9voidmain(){10 sampa(10);11 cout<<sqr_it(a)<<endl;12}呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?(5)CopyconstructorConstructorvs.函數(shù)呼叫1classsam59EX:哪一個(gè)constructor被呼叫?1classsamp{2 inti;3 public:samp(intn){i=n;}samp(constsamp&s){cout<<“copy\n”;i=s.i;}intget_i(){returni;}7};8intsqr_it(sampo){returno.get_i()*o.get_i();}9voidmain(){10 sampa(10);11 cout<<sqr_it(12)<<endl;12}呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?(4)EX:哪一個(gè)constructor被呼叫?1class60範(fàn)例二:程式的輸出為何?classsamp{ inti;public: samp(intn){i=n;} voidset(intn){i=n;} intget_i(){returni;} voidprint(){cout<<i;}};voidsqrt_it(sampo){o.set(o.get_i()*o.get_i());}voidmain(){ sampa(10);sqrt_it(a);a.print();}範(fàn)例二:程式的輸出為何?classsamp{61範(fàn)例三:程式的輸出為何?classsamp{ inti;public: samp(intn){i=n;} voidset(intn){i=n;} intget_i(){returni;} voidprint(){cout<<i;}};voidsqrt_it(samp*o){o->set(o->get_i()*o->get_i());}voidmain(){ sampa(10);sqrt_it(&a);a.print();}呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?100指標(biāo)之外,有無(wú)其他選擇?範(fàn)例三:程式的輸出為何?classsamp{呼叫時(shí),哪62EX:call-by-reference呼叫時(shí),哪一個(gè)建構(gòu)子會(huì)被呼叫?100classsamp{ inti;public: samp(intn){i=n;} voidset(intn){i=n;} intget_i(){returni;} voidprint(){cout<<i;}};voidsqrt_it(samp&o){o.set(o.get_i()*o.get_i());}voidmain(){ sampa(10);sqrt_it(a);a.print();}EX:call-by-reference呼叫時(shí),哪一個(gè)建63範(fàn)例四:
CopyConstructor的重要性classstrtype{ char*p;intlen;public: strtype(char*s){p=newchar[strlen(s)+1];……} ~strtype(){delete[]p;} voidsetChar(intpos,charc){p[pos]=c;}};voidshowMsg(strtypes){/*dosomething…*/}voidmain(){ strtypes1(“NTUwelcomesyou!”); showMsg(s1);}nocopyconstructor:bitwisecopy範(fàn)例四:
CopyConstructor的重要性cla64結(jié)論如果class中的資料成員牽涉到動(dòng)態(tài)記憶體配置,則以下成員函數(shù)不可省:copyconstructoroperator=()或set()儘量使用call-by-reference,why?voidshowMsg(conststrtype&str);voidshowMsg(strtypestr);結(jié)論如果class中的資料成員牽涉到動(dòng)態(tài)記憶體配置,則以下成653-3從函數(shù)中傳回物件回想從前voidfun1(){…}intfun2(){…}doublefun3(){…}char*fun4(){…}myclassfun5(){…….}3-3從函數(shù)中傳回物件回想從前66範(fàn)例一classsamp{ chars[80];public: voidshow(){cout<<s<<endl;} voidset(char*str){strcpy(s,str);}};sampinput(){ chars[80];cin>>s; sampstr;str.set(s);
returnstr;}voidmain(){sampob;ob=input();………}產(chǎn)生str產(chǎn)生暫時(shí)物件使用暫時(shí)物件設(shè)定obreturnstr;返回mainob=input();呼叫input()暫時(shí)物件消滅下一道指令範(fàn)例一classsamp{產(chǎn)生str產(chǎn)生使用return67描述當(dāng)物件被函數(shù)傳回時(shí)會(huì)自動(dòng)建立一個(gè)暫時(shí)物件(temporaryobject),內(nèi)容即為回傳值(returnvalue)。描述當(dāng)物件被函數(shù)傳回時(shí)會(huì)自動(dòng)建立一個(gè)暫時(shí)物件(tempor68深入剖析物件的回傳classsamp{ chars[80];public: samp(){cout<<“default\n”;} samp(constsamp&ob){ cout<<“copy\n”;strcpy(s,ob.s);} ~samp(){cout<<“destroy\n”;} voidshow(){cout<<s<<endl;} voidset(char*str){strcpy(s,str);}};sampinput(){chars[80];cin>>s; sampstr;str.set(s);returnstr;(copyconstructor)}voidmain(){sampob;ob=input();ob.show();}深入剖析物件的回傳classsamp{69範(fàn)例二:拜託你寫copyconstructor好嗎?範(fàn)例二:拜託你寫copyconstructor好嗎?703-4簡(jiǎn)介夥伴函數(shù)
(friendfunctions)什麼是夥伴函數(shù)?classmyclass{ intn,d;public: myclass(inti,intj){n=i;d=j;}};boolisfactor(myclassob){return!(ob.n%ob.d);}voidmain(){ myclassob(10,2); if(isfactor(ob))cout<<“2是10因數(shù)”;}3-4簡(jiǎn)介夥伴函數(shù)
(friendfunctions)71甚麼是夥伴函數(shù)?classmyclass{ intn,d;public: myclass(inti,intj){n=i;d=j;}
friendboolisfactor(myclassob);};boolisfactor(myclassob){return!(ob.n%ob.d);}voidmain(){ myclassob(10,2); if(isfactor(ob))cout<<“2是10因數(shù)”;}甚麼是夥伴函數(shù)?classmyclass{72夥伴函數(shù)的用途運(yùn)算子超載(operatoroverloading)operator+()oprrator<<(),operator>>()讓函數(shù)能存取二個(gè)多個(gè)不同類別的私用成員破壞封裝…….夥伴函數(shù)的用途運(yùn)算子超載(operatoroverloa73NOTE:classmyclass{
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 陜西省西安市臨潼區(qū)2022-2023學(xué)年九年級(jí)上學(xué)期期中考試數(shù)學(xué)試卷(含答案)
- 2024-2025學(xué)年高中政治第四單元當(dāng)代國(guó)際社會(huì)9.1國(guó)際社會(huì)的主要成員:主權(quán)國(guó)家和國(guó)際組織課時(shí)作業(yè)含解析新人教版必修2
- 2024年高考地理一輪復(fù)習(xí)課后限時(shí)集訓(xùn)34生態(tài)脆弱區(qū)環(huán)境問(wèn)題的防治與治理荒漠化的防治與水土流失治理含解析新人教版
- 2024-2025學(xué)年八年級(jí)英語(yǔ)下冊(cè)Unit8OurClothesTopic3HesaidthefashionshowwaswonderfulSectionA課時(shí)作業(yè)無(wú)答案新版仁愛(ài)版
- 五年級(jí)語(yǔ)文楚才杯萬(wàn)能藥丸獲獎(jiǎng)作文3
- 初中英語(yǔ)45套安徽和全國(guó)版難度
- 重陽(yáng)節(jié)敬老院活動(dòng)方案合集15篇(敬老院重陽(yáng)節(jié)活動(dòng)方案)
- 網(wǎng)絡(luò)視頻直播平臺(tái)服務(wù)合同
- 網(wǎng)絡(luò)營(yíng)銷推廣策略及執(zhí)行方案設(shè)計(jì)
- 網(wǎng)絡(luò)安全培訓(xùn)教材編寫合同
- 小記者第一課我是一名小記者
- 團(tuán)結(jié)友愛(ài)和睦相處主題班會(huì)
- 2024年采購(gòu)部年度工作總結(jié)
- 2024年福建省托育服務(wù)職業(yè)技能競(jìng)賽理論考試題庫(kù)(含答案)
- 2024下半年江蘇蘇州城市學(xué)院招聘管理崗位工作人員27人歷年(高頻重點(diǎn)提升專題訓(xùn)練)共500題附帶答案詳解
- 二年級(jí)乘除法口算題大全500題(可直接打印)
- 建造節(jié)活動(dòng)策劃書(shū)
- sk239g報(bào)警器說(shuō)明書(shū)
- 半導(dǎo)體芯片項(xiàng)目創(chuàng)業(yè)計(jì)劃書(shū)(參考范文)
- 困難職工基本情況匯總統(tǒng)計(jì)表
- 檔案統(tǒng)計(jì)臺(tái)帳
評(píng)論
0/150
提交評(píng)論