C++面向?qū)ο蟪绦蛟O(shè)計(jì)_類與對象(2)_第1頁
C++面向?qū)ο蟪绦蛟O(shè)計(jì)_類與對象(2)_第2頁
C++面向?qū)ο蟪绦蛟O(shè)計(jì)_類與對象(2)_第3頁
C++面向?qū)ο蟪绦蛟O(shè)計(jì)_類與對象(2)_第4頁
C++面向?qū)ο蟪绦蛟O(shè)計(jì)_類與對象(2)_第5頁
已閱讀5頁,還剩95頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、作業(yè)作業(yè)第4章習(xí)題一、選擇題(1-16)二、填空題(1-7)三、簡答題(1-5)四、程序分析題(1-2)五、程序設(shè)計(jì)題(1-3)第一到第四大題做在課本上,老師下周三隨機(jī)抽查第五大題的程序請?zhí)峤坏絟ttp:/ 名勝名勝 人人 物品物品 動物動物 ,植物,植物 身邊的對象布蘭尼布蘭尼朱麗葉朱麗葉顧客顧客姓名:朱麗葉姓名:朱麗葉年齡:年齡:28體重:體重:52千克千克操作:操作:購買商品購買商品姓名:布蘭尼姓名:布蘭尼職銜:收銀員職銜:收銀員年齡:年齡:35體重:體重:60千克千克操作:操作:收款收款打印賬單打印賬單對象的特征屬性屬性對象具有的各種特征每個(gè)對象的每個(gè)屬性都擁有特定值 例如:布蘭尼和朱

2、麗葉的體重不一樣 60kg 布蘭尼布蘭尼屬性屬性 35歲歲收銀員布蘭尼收銀員布蘭尼 對象的特征方法方法對象執(zhí)行的操作 打單打單 收銀收銀 方法方法 刷卡刷卡 收銀員布蘭尼收銀員布蘭尼對象的屬性和方法列出尼古拉斯凱奇駕駛的這輛法拉利F360 Spider的屬性和方法列出小狗對象的屬性和方法屬性:屬性: 品牌:法拉利品牌:法拉利 型號:型號:F360 Spider 顏色:黃色顏色:黃色 價(jià)格:價(jià)格:380萬元萬元方法:方法: 發(fā)動發(fā)動 停止停止 加速加速屬性:屬性: 顏色:白色顏色:白色方法:方法: 叫叫 跑跑 吃吃封裝對象同時(shí)具有屬性和方法兩項(xiàng)特性對象的屬性和方法通常被封裝封裝在一起,共同體現(xiàn)事

3、物的特性, 二者相輔相承,不能分割誰看見過只有誰看見過只有“完好的零件和完好的零件和顏色顏色”而不能開而不能開動的汽車?動的汽車?從對象抽象出“類”抽取出下列對象的屬性和方法的共同特征抽取出下列對象的屬性和方法的共同特征 顧客顧客 類顧客類轎車類 類是模子,確定對象將會擁有的類是模子,確定對象將會擁有的特征(屬性)和行為(方法)特征(屬性)和行為(方法) 類是對象的類型類是對象的類型 不同于不同于int類型:具有方法類型:具有方法 各種口味的球狀冰淇淋各種口味的球狀冰淇淋 球狀冰淇淋模子球狀冰淇淋模子類和對象有什么區(qū)別呢? 類是抽象的概念,僅僅是模板,比如說:類是抽象的概念,僅僅是模板,比如說

4、:“人人” 對象是一個(gè)你能夠看得到、摸得著的具體實(shí)體,比如:對象是一個(gè)你能夠看得到、摸得著的具體實(shí)體,比如:“張麗張麗”張麗張麗王勇王勇劉晨劉晨“人人”類類特征(屬性)特征(屬性)年齡年齡體重體重行為(方法)行為(方法)衣衣食食住住行行面向?qū)ο螅∣O)的優(yōu)點(diǎn)便于程序模擬現(xiàn)實(shí)世界中的實(shí)體 用“類”表示實(shí)體的特征和行為隱藏細(xì)節(jié) 對象的行為和屬性被封裝在類中,外界通過調(diào)用類的方法來獲得,不需關(guān)注內(nèi)部細(xì)節(jié)如何實(shí)現(xiàn)可重用 可以通過類的模板,創(chuàng)建多個(gè)類的對象例題聲明一個(gè)銀行賬號類Account,該類有賬號(id)、余額(balance)兩個(gè)數(shù)據(jù)成員,有獲取賬號、獲取余額、存款和取款的函數(shù),以及必要的構(gòu)造函

5、數(shù)。請按上述要求聲明該銀行賬號類并在main函數(shù)中定義該類的多個(gè)對象,然后對它們進(jìn)行存取款和查詢余額的操作。#include #include /包含頭文件命令包含頭文件命令usingusing namespacenamespace std; std;/使用名字空間使用名字空間stdstd#include #include classclass AccountAccount publicpublic: : Account( Account(stringstring Id ); Id ); /構(gòu)造函數(shù)構(gòu)造函數(shù) Account(Account(stringstring Id, Id, double

6、double b); b); /構(gòu)造函數(shù)的重載構(gòu)造函數(shù)的重載 stringstring getIdgetId(); (); /獲取賬號獲取賬號 doubledouble getBalancegetBalance(); (); /返回當(dāng)前賬戶余額返回當(dāng)前賬戶余額 voidvoid depositdeposit( (doubledouble amount); amount); /存款操作存款操作 boolbool withdrawwithdraw( (doubledouble amount); amount); /取款操作取款操作privateprivate: : stringstring id;

7、 id; /賬號賬號doubledouble balance; balance;/余額余額 ;Account:Account(Account:Account(stringstring Id ) Id ) /構(gòu)造函數(shù)構(gòu)造函數(shù) id=Id; balance= id=Id; balance=0 0; ; Account:Account(Account:Account(stringstring Id, Id, doubledouble Balance) Balance) /構(gòu)造函數(shù)構(gòu)造函數(shù) id=Id; balance=Balance; id=Id; balance=Balance; stringst

8、ring Account:getId() Account:getId() returnreturn id; id; doubledouble Account:getBalance() Account:getBalance() returnreturn balance; balance; voidvoid Account:deposit(Account:deposit(doubledouble amount) amount)/存款操作存款操作 balance+=balance+=amount;amount; coutcout 存款成功!存款成功! endl;endl; cout cout 存款金

9、額:存款金額: amountendlamountamount)( balanceamount)/如果余額大于取款金額如果余額大于取款金額 balance-=amount; balance-=amount; cout cout 取款成功!取款成功! endl;endl; cout cout 取款金額:取款金額: amountendl;amountendl;returnreturn truetrue; ; elseelse/如果余額小于取款金額如果余額小于取款金額 cout cout 余額不足!余額不足! endl; endl; returnreturn falsefalse; ; intint

10、mainmain()() Account Account account1account1( (123456789123456789, ,20002000);); cout cout 當(dāng)前賬戶信息:當(dāng)前賬戶信息: endl;endl; cout cout 賬號:賬號: account1.getId()endl;account1.getId()endl; cout cout 余額:余額: account1.getBalance()endlendl;account1.getBalance()endlendl; account1.deposit( account1.deposit(200200);)

11、; cout cout 余額:余額: account1.getBalance()endlendl;account1.getBalance()endlendl; account1.withdraw( account1.withdraw(100100);); cout cout 余額:余額: account1.getBalance()endlendl;account1.getBalance()endlendl; Account Account account2account2( (987654321987654321);); cout cout 當(dāng)前賬戶信息:當(dāng)前賬戶信息: endlendl;en

12、dlendl; cout cout 賬號:賬號: account2.getId()endlendl;account2.getId()endlendl; cout cout 余額:余額: account2.getBalance()endlendl;account2.getBalance()endlendl; account2.withdraw( account2.withdraw(100100);); cout cout 余額:余額: account2.getBalance()endlendl;account2.getBalance()endlendl; account2.deposit( ac

13、count2.deposit(200200);); cout cout 余額:余額: account2.getBalance()endlendl;account2.getBalance()endlendl; account2.withdraw( account2.withdraw(100100);); cout cout 余額:余額: account2.getBalance()account2.getBalance()length=source.length; this-width=source.width; this-height=source.height; return *this; 例

14、如:Box a,b(10); a=b;.2 對象的拷貝在創(chuàng)建對象時(shí)使用已有對象快速拷貝出完全相同的對象對象拷貝的一般形式 類名類名 對象對象2(2(對象對象1);1); 類名類名 對象對象2 = 2 = 對象對象1;1;拷貝構(gòu)造函數(shù)只有一個(gè)形參,這個(gè)形參就是本類對象的引用拷貝構(gòu)造函數(shù)的代碼主要是將形參中對象引用的各數(shù)據(jù)成員值賦給自己的數(shù)據(jù)成員Box(const Box &c) /Box類類的的拷貝拷貝構(gòu)造構(gòu)造函數(shù)函數(shù)4.5.2 對象的復(fù)制以以Box類為例,看看復(fù)制構(gòu)造函數(shù)的形式:類為例,看看復(fù)制構(gòu)造函數(shù)的形式:Box:Box(const Box &c) /Box類類的的拷貝拷貝

15、構(gòu)造構(gòu)造函數(shù)函數(shù) length =c.length; width = c.width; height = c.height; #include #include usingusing namespacenamespace std; std;classclass BoxBox publicpublic: : Box() Box()/無參數(shù)的構(gòu)造函數(shù)無參數(shù)的構(gòu)造函數(shù) coutcout 無參數(shù)的構(gòu)造函數(shù)被調(diào)用!無參數(shù)的構(gòu)造函數(shù)被調(diào)用! endlendl; ; Box( Box(floatfloat L, L,floatfloat w, w,floatfloat h) h) /帶有參數(shù)的構(gòu)造函數(shù)帶有參

16、數(shù)的構(gòu)造函數(shù) length =L; width = w; height = h; length =L; width = w; height = h; cout cout 有參數(shù)的構(gòu)造函數(shù)被調(diào)用!有參數(shù)的構(gòu)造函數(shù)被調(diào)用! endlendl; ; Box( Box(constconst Box &c) Box &c) /Box/Box類類的拷貝構(gòu)造的拷貝構(gòu)造函數(shù)函數(shù) length =c.length; length =c.length; width = c.width; width = c.width; height = c.height; height = c.height; coutcoutC

17、opy Copy 構(gòu)造函數(shù)被調(diào)用!構(gòu)造函數(shù)被調(diào)用! endlendl; ; floatfloat VolumeVolume() () returnreturn length length * * width width * * height; height; privateprivate: : floatfloat length,width,height length,width,height; ; ;intint mainmain() () Box Box box1box1( (4 4, ,2 2, ,3 3);); Box box2(box1); Box box2(box1); Box b

18、ox3 Box box3; ; box3=box1; box3=box1; cout coutbox1box1的體積為:的體積為: box1.Volume()endl;box1.Volume()endl; cout coutbox2box2的體積為:的體積為: box2.Volume()endl;box2.Volume()endl; cout coutbox3box3的體積為:的體積為: box3.Volume()endl;box3.Volume()endl; returnreturn 0 0;四種不同的構(gòu)造對象的方法4.5.2 對象的拷貝在沒有涉及指針類型的數(shù)據(jù)成員時(shí),默認(rèn)拷貝構(gòu)造函數(shù)能夠很

19、好地工作。當(dāng)一個(gè)類有指針類型的數(shù)據(jù)成員時(shí),默認(rèn)拷貝構(gòu)造函數(shù)常會產(chǎn)生問題?!纠?-8】默認(rèn)拷貝構(gòu)造函數(shù)引起的問題#include #include #include #include usingusing namespacenamespace std; std;classclass BoxBox publicpublic: : Box(); Box(); Box( Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h); h); Box operator=(const Box &source Box oper

20、ator=(const Box &source);/);/賦值運(yùn)算符重載賦值運(yùn)算符重載 charchar * * getName(); getName();/獲取名字獲取名字 voidvoid setName( setName(charchar * *Name);Name);/設(shè)置名字設(shè)置名字 floatfloat Volume(); Volume();/計(jì)算體積計(jì)算體積 Box();Box();privateprivate: :floatfloat length,width,height; length,width,height;charchar * *name;name;增加了Name數(shù)據(jù)

21、程序BoxBox:Box()Box()/無參數(shù)的構(gòu)造函數(shù)無參數(shù)的構(gòu)造函數(shù) namename= =newnew charchar 10+110+1; length length =0=0; width ; width = = 0 0; height ; height = = 0 0; ; cout cout 無參數(shù)的構(gòu)造函數(shù)被調(diào)用!無參數(shù)的構(gòu)造函數(shù)被調(diào)用! endl;endl; BoxBox:Box(Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h) h) /帶有參數(shù)帶有參數(shù)的構(gòu)造函數(shù)的構(gòu)造函數(shù) name

22、name= =newnew charcharstrlen(Name)strlen(Name)+1+1; strcpy(name,Name); strcpy(name,Name); length length = =L; width L; width = = w; height w; height = = h; h; cout cout 有參數(shù)的構(gòu)造函數(shù)被調(diào)用!有參數(shù)的構(gòu)造函數(shù)被調(diào)用! endl;endl; /賦值構(gòu)造函數(shù)賦值構(gòu)造函數(shù), ,賦值運(yùn)算符重載,可以省略賦值運(yùn)算符重載,可以省略/若省略系統(tǒng)會調(diào)用默認(rèn)的賦值運(yùn)算符重載函數(shù)若省略系統(tǒng)會調(diào)用默認(rèn)的賦值運(yùn)算符重載函數(shù) Box BoxBox Bo

23、x:operatoroperator= =( (constconst Box Box & &source) source) length length= =source.length;source.length; width width= =source.width;source.width; height height= =source.height;source.height; strcpy( name,); strcpy( name,); cout cout 賦值構(gòu)造函數(shù)被調(diào)用賦值構(gòu)造函數(shù)被調(diào)用 endl;endl; returnreturn

24、* *thisthis; ; charchar * * Box Box: getName() getName() returnreturn name; name; voidvoid Box Box:setName(setName(charchar * *Name)Name) strcpy(name,Name); strcpy(name,Name);floatfloat Box Box:Volume() Volume() returnreturn length length * * width width * * height; height; Box Box:Box() Box() intin

25、t main() main() Box box1( Box box1( 長方體長方體1 1號號 , ,4 4, ,2 2, ,3 3);); Box box2(box1Box box2(box1); ); /調(diào)用默認(rèn)的拷貝構(gòu)造函數(shù)調(diào)用默認(rèn)的拷貝構(gòu)造函數(shù) Box box3; Box box3; box3 box3= =box1;box1; box2.setName( box2.setName( 長方體長方體2 2號號 );); box3.setName(box3.setName( 長方體長方體3 3號號 );); coutcoutbox1.getName()box1.getName()box1.

26、Volume()box1.Volume()endl;endl; cout coutbox2.getName()box2.getName()box2.Volume()box2.Volume()endl;endl; cout coutbox3.getName()box3.getName()box3.Volume()box3.Volume()endl;endl; returnreturn 0 0;長方體1的Name也被改變了nameheight3box2nameheight3box1長方體1號namenameBox box1(Box box1( 長方體長方體1 1號號 , ,4 4, ,2 2, ,

27、3 3););Box Box box2(box1); box2(box1); /調(diào)用默認(rèn)的拷貝構(gòu)造函數(shù)調(diào)用默認(rèn)的拷貝構(gòu)造函數(shù)nameheight3box2nameheight3box1長方體2號namename解決的方法:重寫解決的方法:重寫拷貝構(gòu)造函數(shù)拷貝構(gòu)造函數(shù)Box box1(長方體1號,4,2,3);Box box2(box1); /調(diào)用默認(rèn)的拷貝構(gòu)造調(diào)用默認(rèn)的拷貝構(gòu)造函數(shù)函數(shù) box2.setName(box2.setName(長方體長方體2 2號號););#include #include #include #include usingusing namespacenamespac

28、e std; std;classclass BoxBox publicpublic: : Box(); Box(); Box( Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h h);); Box (const Box &c); /Box類的拷貝構(gòu)造函數(shù) Box operator=(const Box &source); Box operator=(const Box &source); charchar * * getName(); getName();/獲取名字獲取名字 voidvoid setNa

29、me( setName(charchar * *Name);Name);/設(shè)置名字設(shè)置名字 floatfloat Volume(); Volume();/計(jì)算體積計(jì)算體積 Box();Box();privateprivate: :floatfloat length,width,height; length,width,height;charchar * *name;name;增加了拷貝構(gòu)造函數(shù)【例例4-9】 自定義拷貝構(gòu)造函數(shù)自定義拷貝構(gòu)造函數(shù)BoxBox:Box()Box()/無參數(shù)的構(gòu)造函數(shù)無參數(shù)的構(gòu)造函數(shù) namename= =newnew charchar 10+110+1; leng

30、th length =0=0; width ; width = = 0 0; height ; height = = 0 0; ; cout cout 無參數(shù)的構(gòu)造函數(shù)被調(diào)用!無參數(shù)的構(gòu)造函數(shù)被調(diào)用! endl;endl; BoxBox:Box(Box(charchar * *Name,Name,floatfloat L, L,floatfloat w, w,floatfloat h h) ) /帶有參帶有參數(shù)的構(gòu)造數(shù)的構(gòu)造函數(shù)函數(shù) namename= =newnew charcharstrlen(Name)strlen(Name)+1+1; strcpy(name,Name); strcpy

31、(name,Name); length length = =L; width L; width = = w; height w; height = = h; h; cout cout 有參數(shù)的構(gòu)造函數(shù)被調(diào)用!有參數(shù)的構(gòu)造函數(shù)被調(diào)用! endlendl; ; Box Box:Box (Box (constconst Box Box & &c) c) /Box/Box類的復(fù)制構(gòu)造函數(shù)類的復(fù)制構(gòu)造函數(shù) name name= =newnew charcharstrlen()strlen()+1+1; strcpy( name,); strcpy( name,c.na

32、me); length length = =c.length; c.length; width width = = c.width; c.width; height height = = c.height; c.height; cout cout 復(fù)制構(gòu)造函數(shù)被調(diào)用!復(fù)制構(gòu)造函數(shù)被調(diào)用! endlendl; ; 增加了拷貝構(gòu)造函數(shù)/賦值構(gòu)造函數(shù)賦值構(gòu)造函數(shù), ,賦值運(yùn)算符重載,可以省略賦值運(yùn)算符重載,可以省略/若省略系統(tǒng)會調(diào)用默認(rèn)的賦值運(yùn)算符重載函數(shù)若省略系統(tǒng)會調(diào)用默認(rèn)的賦值運(yùn)算符重載函數(shù) Box BoxBox Box:operatoroperator= =( (constconst Box

33、Box & &source) source) length length= =source.length;source.length; width width= =source.width;source.width; height height= =source.height;source.height; strcpy( name,); strcpy( name,); cout cout 賦值構(gòu)造函數(shù)被調(diào)用賦值構(gòu)造函數(shù)被調(diào)用 endl;endl; returnreturn * *thisthis; ; charchar * * Box Box: ge

34、tName() getName() returnreturn name; name; voidvoid Box Box:setName(setName(charchar * *Name)Name) strcpy(name,Name); strcpy(name,Name);floatfloat Box Box:Volume() Volume() returnreturn length length * * width width * * height; height; Box:Box() Box:Box() delete name;name=NULLdelete name;name=NULL;

35、; 析構(gòu)函數(shù)將動態(tài)申請的空間釋放intint main() main() Box box1( Box box1( 長方體長方體1 1號號 , ,4 4, ,2 2, ,3 3);); Box box2(box1Box box2(box1); ); /調(diào)用調(diào)用自定義自定義的拷貝構(gòu)造函數(shù)的拷貝構(gòu)造函數(shù) Box box3; Box box3; box3 box3= =box1;box1; box2.setName( box2.setName( 長方體長方體2 2號號 );); box3.setName(box3.setName( 長方體長方體3 3號號 );); coutcoutbox1.getNa

36、me()box1.getName()box1.Volume()box1.Volume()endl;endl; cout coutbox2.getName()box2.getName()box2.Volume()box2.Volume()endl;endl; cout coutbox3.getName()box3.getName()box3.Volume()box3.Volume()endl;endl; returnreturn 0 0;長方體長方體1的的Name沒有沒有被修改被修改內(nèi)存里的情況內(nèi)存里的情況4.5.3 對象的賦值與復(fù)制的比較相同點(diǎn)主要有:相同點(diǎn)主要有:(1)對象的賦值)對象的賦值

37、和和拷貝拷貝大部分大部分情況下都是把一個(gè)對象的情況下都是把一個(gè)對象的數(shù)據(jù)成員依次賦給另外一個(gè)同類對象的相應(yīng)數(shù)據(jù)成員。數(shù)據(jù)成員依次賦給另外一個(gè)同類對象的相應(yīng)數(shù)據(jù)成員。(2)如果不重載賦值運(yùn)算符或不)如果不重載賦值運(yùn)算符或不提供提供拷貝拷貝構(gòu)造構(gòu)造函數(shù),系函數(shù),系統(tǒng)都可以提供缺省的代碼。統(tǒng)都可以提供缺省的代碼。(3)系統(tǒng)會根據(jù)情況自動的調(diào)用對象的賦值運(yùn)算符重載)系統(tǒng)會根據(jù)情況自動的調(diào)用對象的賦值運(yùn)算符重載函數(shù)或?qū)ο蠛瘮?shù)或?qū)ο蟮牡目截惪截悩?gòu)造構(gòu)造函數(shù)。函數(shù)。不同點(diǎn)主要有:不同點(diǎn)主要有:(1)對象的賦值是在兩個(gè)對象都已經(jīng)創(chuàng)建的基礎(chǔ)上)對象的賦值是在兩個(gè)對象都已經(jīng)創(chuàng)建的基礎(chǔ)上進(jìn)行的;而對象進(jìn)行的;而對象

38、的的拷貝拷貝則則在用一個(gè)已有在用一個(gè)已有對象對象拷貝拷貝一一個(gè)個(gè)新對象時(shí)進(jìn)行的。新對象時(shí)進(jìn)行的。(2)它們兩個(gè)所對應(yīng)調(diào)用的函數(shù)不同,對象的賦值)它們兩個(gè)所對應(yīng)調(diào)用的函數(shù)不同,對象的賦值系統(tǒng)調(diào)用的是賦值運(yùn)算符重載函數(shù);而對象系統(tǒng)調(diào)用的是賦值運(yùn)算符重載函數(shù);而對象的的拷貝拷貝系系統(tǒng)統(tǒng)調(diào)用的調(diào)用的是是拷貝拷貝構(gòu)造構(gòu)造函數(shù)。函數(shù)。4.5.3 對象的賦值與復(fù)制的比較 對于AB類,其拷貝構(gòu)造函數(shù)的原型為_。A) AB:AB();B) AB:AB(AB);C) AB:AB(AB &);D) AB:AB(const AB &);提交單選題ABCD依據(jù)已存在的對象建立一個(gè)新的對象時(shí),會自動調(diào)用該類的_。A) 構(gòu)

39、造函數(shù)B) 賦值構(gòu)造函數(shù)C) 拷貝構(gòu)造函數(shù)D) 析構(gòu)函數(shù)提交單選題ABCD類與對象類的聲明和對象的定義類的成員函數(shù)對象成員的訪問構(gòu)造函數(shù)和析構(gòu)函數(shù)對象的賦值和拷貝對象對象數(shù)組數(shù)組指向?qū)ο蟮闹羔樝蚝瘮?shù)傳遞對象對象與const靜態(tài)成員與友元【例例4-10】創(chuàng)建含有創(chuàng)建含有3個(gè)長方體元素的長方體數(shù)組,個(gè)長方體元素的長方體數(shù)組,并顯示長方體構(gòu)造函數(shù)的調(diào)用情況。并顯示長方體構(gòu)造函數(shù)的調(diào)用情況。4.6.1 對象數(shù)組#include #include #include #include usingusing namespacenamespace std; std;classclass BoxBox publ

40、icpublic: : BoxBox( ) ( ) /無參數(shù)的構(gòu)造函數(shù)無參數(shù)的構(gòu)造函數(shù) length length = = 1 1; width ; width = = 1 1; height ; height = = 1 1; ; cout cout Box(Box( length length , , width width , , height height ); ; cout cout is constructed! is constructed! endl; endl; Box(Box(floatfloat L, L, floatfloat W, W, floatfloat H H)

41、 ) /帶有帶有3 3個(gè)形參的構(gòu)造函數(shù)個(gè)形參的構(gòu)造函數(shù) length length = =L; width L; width = = W; height W; height = = H; H; cout cout Box(Box( length length , , width width , , height height ); ; cout cout is constructed! is constructed! endl; endl; void setValue(void setValue(floatfloat L, L, floatfloat W, W, floatfloat H H)

42、 ) /給數(shù)據(jù)成員賦值給數(shù)據(jù)成員賦值 length length = =L; width L; width = = W; height W; height = = H H;floatfloat Volume( ) Volume( ) returnreturn length length* *widthwidth* *heightheight; ; /求體積求體積 Box( )Box( ) cout cout Box(Box( length length , , width width , , height height ); ; cout cout is destructed! is dest

43、ructed! endl; endl; privateprivate: : floatfloat length, width, height; length, width, height; ; ;intint main( ) main( ) Box Box boxsboxs3 3;/創(chuàng)建含有創(chuàng)建含有3 3個(gè)元素的對象數(shù)組個(gè)元素的對象數(shù)組boxsboxs forfor ( (intint i i=0=0;i;i33;i;i+) ) boxsi.setValue(i,i boxsi.setValue(i,i+10+10,i,i+20+20);); forfor ( i ( i=0=0;i;i33;

44、i;i+) ) cout coutboxsboxsi i 的體積為:的體積為: boxsi.Volume()boxsi.Volume()endl;endl; return return 0 0;4.6.2 對象數(shù)組的初始化對象數(shù)組在建立時(shí)還可以給出實(shí)參以實(shí)現(xiàn)對數(shù)組元素進(jìn)行不同的初始化 類有只有一個(gè)參數(shù)一個(gè)參數(shù)的構(gòu)造函數(shù),可以使用如下形式的數(shù)組初始化形式: Box boxs3 = 10,20,30; 如果對象的構(gòu)造函數(shù)需要多個(gè)參數(shù),則在初始化的花括號里要分別寫明構(gòu)造函數(shù),并指定實(shí)參 【例例4-11】定義對象數(shù)組并初始化,觀察對象數(shù)組定義對象數(shù)組并初始化,觀察對象數(shù)組建立的情況。建立的情況。4.6

45、.2 對象數(shù)組的初始化#include #include #include #include usingusing namespacenamespace std; std;classclass BoxBox publicpublic: : BoxBox( ) ( ) /無參數(shù)的構(gòu)造函數(shù)無參數(shù)的構(gòu)造函數(shù) length length = = 1 1; width ; width = = 1 1; height ; height = = 1 1; ; cout cout Box(Box( length length , , width width , , height height ); ; cou

46、t cout is constructed! is constructed! endl; endl; Box(Box(floatfloat L, L, floatfloat W, W, floatfloat H H) ) /帶有帶有3 3個(gè)形參的構(gòu)造函數(shù)個(gè)形參的構(gòu)造函數(shù) length length = =L; width L; width = = W; height W; height = = H; H; cout cout Box(Box( length length , , width width , , height height ); ; cout cout is constructe

47、d! is constructed! endl; endl; void setValue(void setValue(floatfloat L, L, floatfloat W, W, floatfloat H) H) length length = =L; width L; width = = W; height W; height = = H; H;floatfloat Volume( ) Volume( ) returnreturn length length* *widthwidth* *height; height; Box( )Box( ) cout cout Box(Box( l

48、ength length , , width width , , height height ); ; cout cout is destructed! is destructed! endl; endl; privateprivate: : floatfloat length, width, height; length, width, height; ; ;intint main() main() Box boxsBox boxs3 3 = = Box(Box(1 1, ,3 3, ,5 5),Box(),Box(2 2, ,4 4, ,6 6),Box(),Box(3 3, ,6 6,

49、,9 9) ) ; ; /創(chuàng)建含有三個(gè)元素的對象數(shù)組創(chuàng)建含有三個(gè)元素的對象數(shù)組boxsboxs forfor( (intint i i=0=0;i;i33;i;i+) ) cout coutboxsboxsi i的體積的體積為為 boxsi.Volume()boxsi.Volume()endl;endl; returnreturn 0 0;int main() Box boxs3= Box(1,3,5), Box(),Box(3,6,9); /創(chuàng)建含有三個(gè)元素的對象數(shù)組創(chuàng)建含有三個(gè)元素的對象數(shù)組boxs for(int i=0;i3;i+) coutboxsi的體積為的體積為boxsi.Vol

50、ume()endl; return 0;采用默認(rèn)構(gòu)造函數(shù)來構(gòu)造對象#include #include usingusing namespacenamespace std; std;classclass TestTest publicpublic: : Test(); Test();/默認(rèn)構(gòu)造函數(shù)默認(rèn)構(gòu)造函數(shù) Test(Test(intint n); n); /帶一個(gè)參數(shù)構(gòu)造函數(shù)帶一個(gè)參數(shù)構(gòu)造函數(shù) privateprivate: : intint num num; ; ;TestTest:Test()Test()coutcoutInit defaInit defaendl;endl;numnum

51、=0=0; ; TestTest:Test(Test(intint n) n) coutcoutInitInit n nendl;endl;numnum= =n;n;intint main() main()Test xTest x2 2; ; /語句語句1 1Test y(Test y(1515); ); /語句語句2 2returnreturn 0 0; ; 寫出程序的輸出結(jié)果,并分析結(jié)果寫出程序的輸出結(jié)果,并分析結(jié)果對象數(shù)組的動態(tài)創(chuàng)建與釋放#include #include usingusing namespacenamespace std; std;classclass TestTest

52、 publicpublic: : Test(); Test();/默認(rèn)構(gòu)造函數(shù)默認(rèn)構(gòu)造函數(shù) Test(Test(intint n); n); /帶一個(gè)參數(shù)構(gòu)造函數(shù)帶一個(gè)參數(shù)構(gòu)造函數(shù) set_num(set_num(intint Num)num Num)num= =Num;Num; get_num() get_num()returnreturn num num; Test()coutObject is destructing!endl; Test()coutObject is destructing!endl;privateprivate: : intint num; ; num; ;TestT

53、est:Test()Test()coutcout 調(diào)用默認(rèn)構(gòu)造函數(shù)調(diào)用默認(rèn)構(gòu)造函數(shù) endl;endl;numnum=0=0; ; TestTest:Test(Test(intint n) n) coutcout 調(diào)用帶參數(shù)的構(gòu)造函數(shù)調(diào)用帶參數(shù)的構(gòu)造函數(shù) n nendl;endl;numnum= =n;n;intint main() main()coutcout 用用newnew動態(tài)創(chuàng)建一個(gè)對象動態(tài)創(chuàng)建一個(gè)對象, ,此對象由默認(rèn)構(gòu)造函數(shù)初此對象由默認(rèn)構(gòu)造函數(shù)初始化始化 endl;endl;Test Test * *p p= =newnew Test; Test; cout cout 用用new

54、new動態(tài)創(chuàng)建一個(gè)對象動態(tài)創(chuàng)建一個(gè)對象, ,此對象由有參數(shù)的構(gòu)造函數(shù)此對象由有參數(shù)的構(gòu)造函數(shù)初始化初始化 endl;endl;Test Test * *q q= =newnew Test( Test(1010););coutcout 用用newnew動態(tài)創(chuàng)建一個(gè)有動態(tài)創(chuàng)建一個(gè)有5 5個(gè)對象元素組成的對象數(shù)組個(gè)對象元素組成的對象數(shù)組, ,數(shù)組中的對象由默認(rèn)構(gòu)造函數(shù)初始化數(shù)組中的對象由默認(rèn)構(gòu)造函數(shù)初始化 endl;endl;Test Test * *s s= =newnew Test Test5 5;/Test /Test * *t=new Test5(10); t=new Test5(10);

55、錯(cuò)誤錯(cuò)誤intint i; i;coutcout 調(diào)用對象數(shù)組中每個(gè)對象的調(diào)用對象數(shù)組中每個(gè)對象的set_numset_num成員函數(shù)成員函數(shù) endl;endl;Test Test * *s2s2= =s;s;forfor(i(i=0=0;i;i5-set_num(i);set_num(i); s2 s2+; ; coutcout 調(diào)用對象數(shù)組中每個(gè)對象的調(diào)用對象數(shù)組中每個(gè)對象的get_numget_num成員函數(shù)成員函數(shù) endl;endl;forfor(i(i=0=0;i;i55;i;i+) )coutcout 第第 i i+1+1 個(gè)對象的個(gè)對象的num=num=si.get_num(

56、)si.get_num()”操作符。如:box1.Volume( ); box1.Volume( ); 與與q-Volume() ;q-Volume() ;等價(jià)xobobset_x(a)show_x()2 2?p p&ob運(yùn)行結(jié)果:22 2【例例4-124-12】用指針引用單個(gè)對象的成員#include #include classclass exeexe publicpublic: :voidvoid set_x( set_x(intint a) xa) x= =a;a;voidvoid show_x() show_x()coutcoutx x-show_x();show_x(); 對象的動

57、態(tài)創(chuàng)建和釋放使用new和delete運(yùn)算符實(shí)現(xiàn)對對象的動態(tài)創(chuàng)建與釋放 在程序執(zhí)行到new語句時(shí),系統(tǒng)會從內(nèi)存堆中分配一塊內(nèi)存空間,存放類的對象,調(diào)用構(gòu)造函數(shù)初始化對象。如果內(nèi)存分配成功,返回分配的內(nèi)存的首地址;如果分配內(nèi)存失敗,則會返回一個(gè)NULL。Box *pc=new Box;對象的動態(tài)創(chuàng)建和釋放可以在使用new運(yùn)算符創(chuàng)建對象時(shí)給出實(shí)參,調(diào)用帶有參數(shù)的構(gòu)造函數(shù)初始化對象 如:Box Box * *pc = new Box(2,2,2); pc = new Box(2,2,2); 為了保險(xiǎn)起見,在使用對象指針之前一般先判斷指針的值是否為NULL 。如下所示: Box Box * *pc =

58、new Box(2,2,2);pc = new Box(2,2,2); if(pc!=NULL)if(pc!=NULL) pc-Volume(); pc-Volume(); 對象的動態(tài)創(chuàng)建和釋放使用delete運(yùn)算符釋放該對象 delete運(yùn)算符的使用格式是: delete delete 指針變量名指針變量名; ;如:如: delete pc;delete pc; 通過new運(yùn)算符動態(tài)創(chuàng)建的對象只能通過delete運(yùn)算符動態(tài)的釋放 #include #include #include #include usingusing namespacenamespace std; std;classcl

59、ass C C string string namename; ; /私有的數(shù)據(jù)成員私有的數(shù)據(jù)成員publicpublic: :C()C()namename= =anonymousanonymous; ; coutcoutnamename Constructing. Constructing.endlendl; ; C(C(constconst charchar * *n)n)namename= =n;n; coutcoutnamename Constructing. Constructing.endlendl; ; C()C()coutcoutnamename Destructing. De

60、structing.endlendl; ; ;intint main main()() C C c0(c0(hortensehortense);); C c1C c1; ; C C c2(c2(foofoo);); C C * *ptrptr= =newnew C(); C();deletedelete ptr;ptr; return return 0 0;給出此程序的輸出結(jié)果給出此程序的輸出結(jié)果輸出結(jié)果:輸出結(jié)果:hortense Constructing.anonymous Constructing.foo Constructing.foo Destructing.anonymous De

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論