福建農(nóng)林大學(xué)計(jì)算機(jī)與信息學(xué)院實(shí)驗(yàn)報(bào)告_第1頁(yè)
福建農(nóng)林大學(xué)計(jì)算機(jī)與信息學(xué)院實(shí)驗(yàn)報(bào)告_第2頁(yè)
福建農(nóng)林大學(xué)計(jì)算機(jī)與信息學(xué)院實(shí)驗(yàn)報(bào)告_第3頁(yè)
福建農(nóng)林大學(xué)計(jì)算機(jī)與信息學(xué)院實(shí)驗(yàn)報(bào)告_第4頁(yè)
福建農(nóng)林大學(xué)計(jì)算機(jī)與信息學(xué)院實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、福建農(nóng)林大學(xué)計(jì)算機(jī)與信息學(xué)院實(shí)驗(yàn)報(bào)告一、實(shí)驗(yàn)?zāi)康暮鸵?掌握類(lèi)、類(lèi)的數(shù)據(jù)成員、類(lèi)的成員函數(shù)的定義方式,理解類(lèi)成員的訪問(wèn)控制方式。2掌握對(duì)象的定義和操作對(duì)象的方法。3掌握構(gòu)造函數(shù)和析構(gòu)函數(shù)的概念,掌握幾種特殊構(gòu)造函數(shù)。4理解對(duì)象數(shù)組、對(duì)象指針的概念并能正確應(yīng)用。5理解類(lèi)的組合關(guān)系(對(duì)象成員)。6理解this、const、new/delete等關(guān)鍵字的用法并能夠正確應(yīng)用。7掌握靜態(tài)成員的基本使用方法。8掌握友元的使用方法,進(jìn)一步理解其概念和作用。二、實(shí)驗(yàn)內(nèi)容和原理1.下面程序sy3_1.cpp中用ERROR標(biāo)明的語(yǔ)句有錯(cuò),在不刪除和增加代碼行的情況下,改正錯(cuò)誤語(yǔ)句,使其正確與運(yùn)行。/sy3_1.c

2、pp#include<iostream>using namespace std;class Aapublic:Aa(int i=0) a=i;cout<<"Constructor"<<a<<endl;Aa()cout<<"Destructor"<<a<<endl;void print()cout<<a<<endl;private:int a;int main()Aa a1(1),a2(2);a1.print();cout<<a2.a&l

3、t;<endl; /ERRORreturn 0; 2.調(diào)試下列程序。/sy3_2.cpp#include<iostream>using namespace std;class TPoint public:TPoint(int x,int y)X=x;Y=y;TPoint(TPoint &p);TPoint()cout<<"Destructor is calledn"int getx()return X;int gety()return Y;private:int X,Y;TPoint:TPoint(TPoint &p)X=p.X

4、;Y=p.Y;cout<<"Copy-initializention Constructor is calledn"int main()TPoint p1(4,9);TPoint p2(p1);TPoint p3=p2;cout<<"p3=("<<p3.getx()<<", "<<p3.gety()<<")n"return 0; 在該程序中,將TPoint類(lèi)的帶有兩個(gè)參數(shù)的構(gòu)造函數(shù)進(jìn)行修改,在函數(shù)體內(nèi)增添下述語(yǔ)句:cout<<&qu

5、ot;Contructor is called.n"(1) 寫(xiě)出程序的輸出結(jié)果,并解釋輸出結(jié)果。(2) 按下列要求進(jìn)行調(diào)試;在主函數(shù)體內(nèi),添加下列說(shuō)明語(yǔ)句:Tpoint p4,p5(2);調(diào)試程序會(huì)出現(xiàn)什么現(xiàn)象?為什么?如何解決?(提示:對(duì)已有的構(gòu)造函數(shù)進(jìn)行適當(dāng)修改)結(jié)合運(yùn)行結(jié)果分析如何使用不同的構(gòu)造函數(shù)創(chuàng)建不同的對(duì)象。3.對(duì)教材中Li3_11.cpp的主函數(shù)做如下修改:(1)將Heapclass *pa1,*pa2改為Heapclass *pa1,*pa2,*p3; (2)在語(yǔ)句pa=new heapclass;后增加語(yǔ)句pa3= new Heapclass(5); (3)將語(yǔ)句i

6、f(!pa1|!pa2)改為 if(!pa1|!pa2|!pa3);(4)在語(yǔ)句delete pa2;后增加語(yǔ)句delete pa3;寫(xiě)出程序的輸出結(jié)果,并解釋輸出結(jié)果。4請(qǐng)定義一個(gè)矩形類(lèi)(Rectangle),私有數(shù)據(jù)成員為矩形的長(zhǎng)度(len)和寬度(wid),無(wú)參構(gòu)造函數(shù)置len和wid為0,有參構(gòu)造函數(shù)置len和wid為對(duì)應(yīng)形參的值,另外還包括求矩形周長(zhǎng),求矩形面積,取矩形長(zhǎng)度和寬度,修改矩形長(zhǎng)度和寬度為對(duì)應(yīng)形參的值,輸出矩形尺寸等公有成員函數(shù)。要求輸出矩形尺寸的格式為”length:長(zhǎng)度,width:寬度”。(sy3_3.cpp)5調(diào)試下列程序,寫(xiě)出輸出結(jié)果,并分析輸出結(jié)果。/sy4_

7、1.cpp#include<iostream>using namespace std;class Mypublic:My(int aa)A=aa;B-=aa;static void fun(My m);private:int A;static int B;void My:fun(My m)cout<<"A="<<m.A<<endl; cout<<"B="<<m.B<<endl;int My:B=100;int main()My P(6),Q(8);My:fun(P);Q.

8、fun(Q);return 0;6.分析并調(diào)試程序,完成下列問(wèn)題。/sy4_2.cpp#include<iostream>#include<cmath>using namespace std;class Mypublic:My(double i=0)x=y=i;My(double i,double j)x=i;y=j;My(My&m) x=m.x;y=m.y;friend double dist(My&a,My&b);private:double x,y;double dist(My&a,My&b)double dx=a.x-b.

9、x;double dy=a.y-b.y; return sqrt(dx*dx+dy*dy);int main()My m1,m2(15),m3(13,14);My m4(m3);cout<<"The distancel:"<<dist(m1,m3)<<endl; cout<<"The distance2:"<<dist(m2,m3)<<endl;cout<<"The distance3:"<<dist(m3,m4)<<endl;

10、cout<<"The distance4:"<<dist(m1,m2)<<endl; return 0;(1) 指出所有的構(gòu)造函數(shù),它們?cè)诒境绦蛑蟹謩e起什么作用?(2) 指出設(shè)置默認(rèn)參數(shù)的構(gòu)造函數(shù)。(3) 指出友元函數(shù),將友元函數(shù)放到私有部分,觀察結(jié)果是否有變化。(4) 寫(xiě)出輸出結(jié)果,并分析輸出結(jié)果。7.定義一個(gè)Student類(lèi),在該類(lèi)的定義中包含一個(gè)數(shù)據(jù)成員score(分?jǐn)?shù)),兩個(gè)靜態(tài)數(shù)據(jù)成員total(總分)和學(xué)生人數(shù)count;成員函數(shù)scoretotalcount(float s)用于設(shè)置分?jǐn)?shù),求總分和累計(jì)學(xué)生人數(shù);靜態(tài)成員函數(shù)s

11、un()用于返回總分;靜態(tài)成員函數(shù)average()用于求平均值。在main函數(shù)中,輸入某班同學(xué)的成績(jī),并調(diào)用上述函數(shù)求全班學(xué)生的總分和平均分(sy4_3)8.聲明Book和Ruler兩個(gè)類(lèi),二者都有weight屬性,定義二者的一個(gè)友元函數(shù)totalWeight(),計(jì)算兩者的重量和。(sy4_4.cpp)三、實(shí)驗(yàn)環(huán)境微機(jī)及Visual C+6.0。四、算法描述及實(shí)驗(yàn)步驟1.將cout<<a2.a<<endl;改為a2. .print();然后進(jìn)行調(diào)試運(yùn)行。2. 在該程序中,將TPoint類(lèi)的帶有兩個(gè)參數(shù)的構(gòu)造函數(shù)進(jìn)行修改,在函數(shù)體內(nèi)增添下述語(yǔ)句:cout<<

12、;”Contructor is called.n”(1) 寫(xiě)出程序的輸出結(jié)果,并解釋輸出結(jié)果。(2)按下列要求進(jìn)行調(diào)試;在主函數(shù)體內(nèi),添加下列說(shuō)明語(yǔ)句:Tpoint p4,p5(2);觀察調(diào)試時(shí)出現(xiàn)的現(xiàn)象,解釋原因并解決3.寫(xiě)出修改后的程序的結(jié)果,并解釋輸出結(jié)果修改后的程序:#include<iostream>using namespace std;class Heapclasspublic:Heapclass(int x);Heapclass();Heapclass();private:int i;Heapclass:Heapclass(int x)i=x;cout<<

13、;"Contstructor is called."<<i<<endl;Heapclass:Heapclass()cout<<"Default Contstructor is called."<<endl;Heapclass:Heapclass()cout<<"Default is called."<<endl;int main()Heapclass *pa1,*pa2,*pa3;pa1=new Heapclass(4); pa2=new Heapclass;pa

14、3=new Heapclass(5);if(!pa1|!pa2|!pa3) cout<<"Out of Memory!"<<endl;return 0;cout<<"Exit main"<<endl;delete pa1;delete pa2;delete pa3;return 0;4#include<iostream>using namespace std;class Rectanglepublic:Rectangle()len=0;wid=0;Rectangle(double Len, do

15、uble Wid) len=Len;wid=Wid;double Circumference() return 2*(len+wid);double Area() return len*wid;double getl()return len;double getw()return wid;void charge(double a,double b) len=a;wid=b;print()cout<<"length:"<<len<<"width:"<<wid;private:int len,wid;int m

16、ain()Rectangle p1;Rectangle p2(4.0,5.0);cout<<"p1的矩形尺寸:"p1.print();cout<<"p2的矩形尺寸:"p2.print();cout<<"p2周長(zhǎng):"<<p2. Circumference()<<endl;cout<<"p2面積:"<<p2. Area()<<endl;cout<<"p2的長(zhǎng)度:"<<p2. get

17、l()<<endl; cout<<"p2的寬度:"<<p2. getw()<<endl;p2.charge(5.0,6.0);cout<<"修改后的矩形的尺寸:"p2.print();return 0;5.調(diào)試程序所有sy4_1,寫(xiě)出輸出結(jié)果,分析輸出結(jié)果。6.分析調(diào)試sy4_2,回答sy4_2的問(wèn)題。7.程序如下:/sy4_3#include<iostream>using namespace std;class student public: void scoretotalcoun

18、t(float s);static float sum(); static float average();private:float score;static float total;static int count;float student:total=0;int student:count=0;void student:scoretotalcount(float s)score=s;total+=score;count+;float student:sum()return total;float student:average()return total/count;int main(

19、)float s;int n; student a10;cout<<"輸入學(xué)生的個(gè)數(shù)(10個(gè)以內(nèi)):"cin>>n;cout<<"輸入學(xué)生成績(jī):"for(int i(0);i<n;i+) cin>>s; ai.scoretotalcount( s); cout<<"班級(jí)總分為:" cout<<student:sum()<<endl;cout<<"班級(jí)平均分為:" cout<<student:averag

20、e()<<endl; return 0;8.程序如下:/sy4_4#include<iostream>using namespace std;class Ruler;class Bookpublic:Book(int i=0) weight=i;friend float totalWeight(Book&m,Ruler&n);private: float weight;class Rulerpublic:Ruler(int j=0) weight=j;friend float totalWeight(Book&m,Ruler&n);pri

21、vate: float weight;float totalWeight(Book& m,Ruler& n)return m.weight+n.weight;int main() int i,j;cout<<"Book weight:"cin>>i;cout<<"Ruler weight:"cin>>j;Book B(i);Ruler R(j); cout<<"totalweight"<<totalWeight(B,R)<<endl;r

22、eturn 0;五、調(diào)試過(guò)程及實(shí)驗(yàn)結(jié)果1.應(yīng)將cout<<a2.a<<endl改為a2.print(),然后進(jìn)行調(diào)試試驗(yàn)結(jié)果為:2.(1)在TPoint類(lèi)的帶有兩個(gè)參數(shù)的構(gòu)造函數(shù)中添加cout<<”Contructor is called.n”試驗(yàn)結(jié)果: (2) 在主函數(shù)體內(nèi),添加Tpoint p4,p5(2);調(diào)試過(guò)程:出錯(cuò)原因:因?yàn)樵陬?lèi)中沒(méi)有定義不帶參數(shù)和帶一個(gè)參數(shù)的構(gòu)造函數(shù)解決方法:可以將帶兩個(gè)參數(shù)的結(jié)構(gòu)函數(shù)改為缺省的構(gòu)造函數(shù),即可以將TPoint(int x,int y)改為T(mén)Point(int x=0,int y=0)輸出結(jié)果:在運(yùn)行的過(guò)程中TPoint p1(4,9); TPoint p4,p5(2);調(diào)用了構(gòu)造函數(shù),而TPoint p2(p1); TPoint p3=p2;使用了拷貝構(gòu)造函數(shù)。3.實(shí)驗(yàn)結(jié)果:用3個(gè)指針*p1,*p2,*p3指向三個(gè)新創(chuàng)建的對(duì)

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論