freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

c程序設(shè)計(jì)實(shí)踐教程(下)ppt(編輯修改稿)

2024-11-13 00:26 本頁(yè)面
 

【文章內(nèi)容簡(jiǎn)介】 }。 單參數(shù) 構(gòu)造函數(shù) void main( ) { Money m1(25,5,7),m2()。 ( )。 ( )。 Money m3=。 ( )。 m3= 。 ( )。 } 隱式類(lèi)型轉(zhuǎn)換:調(diào)用單參數(shù)構(gòu)造函數(shù),創(chuàng)建 Money類(lèi)型的臨時(shí)對(duì)象。 并不做隱式類(lèi)型轉(zhuǎn)換,等價(jià)于: Money m3()。 拷貝構(gòu)造函數(shù) ? 拷貝構(gòu)造函數(shù):特殊的構(gòu)造函數(shù),其形參是本類(lèi)的對(duì)象的引用,其作用是使用一個(gè)已經(jīng)存在的對(duì)象 (由拷貝構(gòu)造函數(shù)的參數(shù)指定的對(duì)象 )去初始化一個(gè)新的同類(lèi)的對(duì)象。 ? 定義一個(gè)拷貝構(gòu)造函數(shù)的一般形式為: ClassName::ClassName(ClassName amp。c) { ... } //函數(shù)體完成對(duì)應(yīng)數(shù)據(jù)成員的賦值 ? 例 使用完成拷貝功能的構(gòu)造函數(shù)。 include include class Point{ int x,y。 public: Point(int a=0,int b=0){ x=a。y=b。 } Point(Pointamp。 t) //拷貝構(gòu)造函數(shù) { x=。 y=。 cout調(diào)用了拷貝構(gòu)造函數(shù) !\n。 } int GetX( ){ return x。 } int GetY( ){ return y。 } void Show( ){ coutx=x,y=y39。\n39。 } }。 double Distance(Point p1,Point p2) { int dx=()()。 int dy=()()。 return sqrt(dx*dx+dy*dy)。 } Point Mid(Pointamp。 p1,Pointamp。 p2) { int x=(()+())/2。 int y=(()+())/2。 return Point(x,y)。 } void main( ) { Point p0,p1(8,15)。 Point p2(p1)。//調(diào)用拷貝構(gòu)造函數(shù) Point p3=p1。 //等價(jià): Point p3(p1)。 ()。 ()。 ()。 p3=Mid(p0,p3)。 coutDistance(p0,p3)endl。 } ? 函數(shù)Distance(Point,Point)的形參為類(lèi)的對(duì)象,當(dāng)執(zhí)行函數(shù)調(diào)用 distance(p0,p3)時(shí),實(shí)參初始化形參,兩次調(diào)用拷貝構(gòu)造函數(shù),分別用實(shí)參對(duì)象 p0和 p3初始化形參對(duì)象 p1和 p2。 ? 若函數(shù)形參是對(duì)象的引用,則調(diào)用該函數(shù)時(shí),不調(diào)用拷貝構(gòu)造函數(shù),如本例的Mid函數(shù)所示。 ?結(jié)果 (VC6 with sp6): 調(diào)用了拷貝構(gòu)造函數(shù) ! 調(diào)用了拷貝構(gòu)造函數(shù) ! x=8,y=15 x=8,y=15 x=8,y=15 調(diào)用了拷貝構(gòu)造函數(shù) ! 調(diào)用了拷貝構(gòu)造函數(shù) ! ? 默認(rèn)拷貝構(gòu)造函數(shù): – 若定義類(lèi)時(shí)未顯式定義該類(lèi)的拷貝構(gòu)造函數(shù),則編譯系統(tǒng)會(huì)在編譯該類(lèi)時(shí)自動(dòng)生成一個(gè)默認(rèn)拷貝構(gòu)造函數(shù)。 – 默認(rèn)拷貝構(gòu)造函數(shù)的功能是把一個(gè)已經(jīng)存在的對(duì)象的每個(gè)數(shù)據(jù)成員的值逐個(gè)復(fù)制到新建立對(duì)象對(duì)應(yīng)的數(shù)據(jù)成員中。 – 例 Point的拷貝構(gòu)造函數(shù)與默認(rèn)的拷貝構(gòu)造函數(shù)功能一樣,不必顯式定義。 – 何時(shí)顯式定義拷貝構(gòu)造函數(shù)? 若類(lèi)中包含指向動(dòng)態(tài)內(nèi)存的指針時(shí),用默認(rèn)的拷貝構(gòu)造函數(shù)初始化對(duì)象,將帶來(lái)嚴(yán)重問(wèn)題,如例 。 ? 例 默認(rèn)拷貝的構(gòu)造函數(shù)。 include include class String{ char *p。 public: String(char*c=0) { if(c) { p=new char[strlen(c)+1]。 strcpy(p,c)。} else p=NULL。 } ~String( ){ delete []p。 } void Show( ){ coutstring=p39。\n39。 } }。 void main( ) { String s1(student)。 String s2(s1)。//A ( )。 ( )。 } s1 堆內(nèi)存 s2 – 執(zhí)行 A行語(yǔ)句:調(diào)用默認(rèn)拷貝構(gòu)造函數(shù),用 s1對(duì)象初始化 s2對(duì)象,使 s2對(duì)象的成員指針 p與 s1對(duì)象的成員指針 p指向同一片內(nèi)存。 – 主函數(shù)結(jié)束:先撤消 s2, s2對(duì)象釋放其所指動(dòng)態(tài)內(nèi)存;其后撤銷(xiāo) s1對(duì)象,但因s1對(duì)象所指動(dòng)態(tài)內(nèi)存已被 s2對(duì)象釋放,造成重復(fù)釋放同一塊動(dòng)態(tài)內(nèi)存,出現(xiàn)運(yùn)行錯(cuò)誤。 – 定義拷貝構(gòu)造函數(shù),避免上述錯(cuò)誤: String(Stringamp。s) { if(){ p=new char[strlen()+1]。 strcpy(p,)。 }else p=0。 } s t u d e n \0 t 對(duì)象成員與構(gòu)造函數(shù) ? 對(duì)象成員:一個(gè)類(lèi)的對(duì)象可做另一個(gè)類(lèi)的數(shù)據(jù)成員。 ? 例如: class Date{ int year,month,day。 public: Date(int y,int m,int d){ year=y。month=m。day=d。 } }。 class Person{ char name[12]。 char sex[4]。 Date birthday。 //對(duì)象成員 public: Person(char*, char*,int,int,int)。 … }。 ? 對(duì)象成員的初始化 –通過(guò)初始化表進(jìn)行,如: Person::Person(char*n,char*s,int y,int m,int d) :birthday(y,m,d) { strcpy(name,n)。 strcpy(sex,s)。 } –若未在初始化表中初始化對(duì)象成員,則系統(tǒng)自動(dòng)調(diào)用該對(duì)象成員的缺省的構(gòu)造函數(shù)來(lái)初始化。 – 若類(lèi)中包含對(duì)象成員,則在創(chuàng)建該類(lèi)的對(duì)象時(shí),先調(diào)用對(duì)象成員的構(gòu)造函數(shù),初始化相應(yīng)的對(duì)象成員,后執(zhí)行該類(lèi)的構(gòu)造函數(shù),初始化該類(lèi)的其他成員。 – 如果一個(gè)類(lèi)包含多個(gè)對(duì)象成員,對(duì)象成員的構(gòu)造函數(shù)的調(diào)用順序由它們?cè)谠擃?lèi)中的說(shuō)明順序決定,而與它們?cè)诔跏蓟碇械捻樞驘o(wú)關(guān)。 ? 例 初始化對(duì)象成員。 include class Point{ int x,y。 public: Point(int a,int b) { x=a。 y=b。 coutConstructor of class Point is called.\n。 } void Show( ) { coutx=x,y=yendl。 } }。 class Rectangle{ Point p。 int length,width。 public: Rectangle(int l,int w,int a,int b):p(a,b) { length=l。 width=w。 coutConstructor of class Rectangle is called.\n。 } void Show( ) { ( )。 coutlength=length,width=widthendl。 } }。 void main( ){ Rectangle r(5,4,45,55)。 ( )。 } 程序運(yùn)行結(jié)果: Constructor of class Point is called. Constructor of class Rectangle is called. x=45,y=55 length=5,width=4 友元 ? 有時(shí)需要在對(duì)象外部頻繁訪問(wèn)對(duì)象私有的或保護(hù)的數(shù)據(jù)成員,若通過(guò)調(diào)用公有成員函數(shù)間接訪問(wèn),由于參數(shù)傳遞、類(lèi)型檢查等都需占用時(shí)間,必然降低程序的運(yùn)行效率。 ? 使用友元可在對(duì)象外部直接訪問(wèn)對(duì)象私有的和保護(hù)的數(shù)據(jù)成員。 ? 友元以類(lèi)的封裝性的有限破壞為代價(jià)來(lái)提高程序的運(yùn)行效率,應(yīng)謹(jǐn)慎使用。 ? 友元分為:友元函數(shù)和友元類(lèi)。 友元函數(shù) ? 友元函數(shù)的說(shuō)明格式: friend type FuncName(args)。 ? 例 用友元函數(shù)的方法計(jì)算兩點(diǎn)距離。 include include class Point{ int x,y。 public: Point(int i=0,int j=0){ x=i。y=j。 } int GetX( ){ return x。 } int GetY( ){ return y。 } friend double distance(Pointamp。,Pointamp。)。 }。 普通函數(shù) distance( )聲明為 Point類(lèi)的友元函數(shù)。 double distance(Pointamp。 p1,Pointamp。 p2) { int dx=,dy=。 return sqrt(dx*dx+dy*dy)。 } void main( ) { Point a(8,15),b(3,7)。 coutThe distance is:distance(a,b)endl。 } 程序運(yùn)行結(jié)果: The distance is: 友元函數(shù) distance( )可通過(guò)對(duì)象名直接訪問(wèn) Point類(lèi)的對(duì)象 p1和 p2的私有數(shù)據(jù)成員 x和 y,提高了程序的效率。 ? 友元函數(shù)說(shuō)明: – 友元函數(shù)應(yīng)在類(lèi)中說(shuō)明,可放在類(lèi)的私有、公有或保護(hù)部分,效果一樣。友元函數(shù)體可在類(lèi)內(nèi)或類(lèi)外定義。 – 友元函數(shù)不是類(lèi)的成員,不帶 this指針,因此必須將對(duì)象名或?qū)ο蟮囊米鳛橛言瘮?shù)的參數(shù),并在函數(shù)體中用運(yùn)算符 “ .”來(lái)訪問(wèn)對(duì)象的成員。如上例中的 。 – 友元函數(shù)可直接訪問(wèn)相關(guān)類(lèi)中的所有成員。 – 一個(gè)類(lèi)的成員函數(shù)也可作為另一個(gè)類(lèi)的友元函數(shù),例如: class A{ … int f(… )。 }。 class B{ … //成員定義 friend int A::f(… )。//聲明類(lèi) A的成員函數(shù) f( ) }。 // 是類(lèi) B的友元 在聲明這個(gè)友元函數(shù)時(shí)需要在函數(shù)名前面加上它的類(lèi)名和作用域運(yùn)算符 “ ::”。 友元類(lèi) ? 若聲明 A類(lèi)為 B類(lèi)的友元,則 A類(lèi)的所有成員函數(shù)都成為 B類(lèi)的友元函數(shù)。有時(shí)友元類(lèi)的使用也是必要的選擇。 ? 例 友元類(lèi)。 include include class Date{ int year,month,day。 public: Date(int y=0,int m=0,int d=0){ year=y。month=m。day=d。} void display(){ coutyearmonthday。 } friend class Person。 }。 class Person{ char name[12]。 char sex[4]。 Date birthday。 public: Person(char*n,int y,int m,int d,char*s) { strcpy(name,n)。 strcpy(sex,s)。 =y。 =m。 =d。 } void display( ) { coutname:name,sex:sex。 cout,birthday:39。.? 39。.39。endl。 } }。 void main( ) { Person p(“張三 ” ,1983,8,20,”男 ” )。 ( )。 } 程序運(yùn)行結(jié)果: name:張三 ,sex:男 ,birthday: 因 Person類(lèi)為 Date類(lèi)的友元,在 Person類(lèi)的成員函數(shù)可直接操作 Date類(lèi)的對(duì)象 birthday的數(shù)據(jù)成員。 ? 友元的其它注意事項(xiàng): – 友元關(guān)系沒(méi)有傳遞性。例如,說(shuō)明類(lèi) A是類(lèi) B的友元,類(lèi) B是類(lèi) C的友元時(shí),類(lèi) A并不一定是類(lèi) C的友元。 – 友元關(guān)系不具有交換性,即說(shuō)明類(lèi) A為類(lèi) B的友元時(shí),類(lèi)
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫(kù)吧 www.dybbs8.com
備案圖片鄂ICP備17016276號(hào)-1