




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)二 類與對(duì)象一、實(shí)驗(yàn)?zāi)康?、學(xué)習(xí)類與對(duì)象的定義,掌握類與對(duì)象的使用方法。2、學(xué)習(xí)數(shù)據(jù)成員與成員函數(shù)的訪問方式,理解構(gòu)造函數(shù)和析構(gòu)函數(shù)的定義與執(zhí)行過程,學(xué)會(huì)構(gòu)造函數(shù)的重載方法。3、掌握數(shù)組與指針的定義與使用方法,理解數(shù)組與指針的存儲(chǔ)分配與表示。4、掌握用指針和引用向函數(shù)傳遞參數(shù)。5、掌握靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的使用。6、理解友元與友元函數(shù)的作用與使用方法。二、實(shí)驗(yàn)內(nèi)容1、下面是一個(gè)計(jì)算器類的定義,請(qǐng)完成該類成員函數(shù)的實(shí)現(xiàn)。class Counter public: Counter(int number); void increment(); /給原值加1 void decrement()
2、; /給原值減1 int getValue(); /取得計(jì)數(shù)器值 int print(); /顯示計(jì)數(shù) private: int value;#include<iostream.h>class countpublic:counter(int number);void increment();void decrement();int getvalue(int);int print();private:int value;void count:increment() int a=value+1;void count:decrement()int b=value-1;int count:
3、getvalue(int s)value=s;return 0;int count:print()cout<<value<<"+1="<<value+1<<endl;cout<<value<<"-1="<<value-1<<endl;return 0;void main()count s;s.getvalue(5);s.print();/2、根據(jù)注釋語句的提示,實(shí)現(xiàn)類Date的成員函數(shù)。#include<iostream.h>class Date
4、public: void printDate();/顯示日期 void setDay(int d);/設(shè)置日的值 void setMonth(int m);/設(shè)置月的值 void setYear(int y);/設(shè)置年的值 private: int day,month,year;void Date:printDate()cout<<year<<"年"<<month<<"月"<<day<<"日" void Date:setDay(int d)day=d;void D
5、ate:setMonth(int m)month=m;void Date:setYear(int y)year=y;int main()Date testDay;testDay.setDay(5);testDay.setMonth(10);testDay.setYear(2014);testDay.printDate();return 0;3、建立類cylinder,cylinder的構(gòu)造函數(shù)被傳遞了兩個(gè)double值,分別表示圓柱體的半徑和高度。用類cylinder計(jì)算圓柱體的體積,并存儲(chǔ)在一個(gè)double變量中。在類cylinder中包含一個(gè)成員函數(shù)vol(),用來顯示每個(gè)cylinder
6、對(duì)象的體積。#include<iostream.h>class cylinderprivate:double r;double h;double v;public:cylinder(); double vol(); cylinder(double,double);cylinder:cylinder(double m,double n):r(m),h(n)cylinder:cylinder()cout<<"Constructor called"<<endl;double cylinder:vol()double v;v=3.14*r*r*h
7、;return v;double main()cylinder a(1.1,2.2);cout<<"體積="<<a.vol()<<endl;return 0;4、構(gòu)建一個(gè)類book,其中含有兩個(gè)私有數(shù)據(jù)成員qu和price,建立一個(gè)有5個(gè)元素的數(shù)組對(duì)象,將qu初始化為15,將price初始化為qu的10倍。顯示每個(gè)對(duì)象的qu*price值。#include<iostream.h>class bookprivate:int qu;int price;int s;public:book(int p,int q):qu(p),pri
8、ce(q)void print()cout<<qu*price<<endl;int main()book a(1,10);a.print();book b(2,20);b.print();book c(3,30);c.print();book d(4,40);d.print();book e(5,50);e.print();return 0;5、修改上題,通過對(duì)象指針訪問對(duì)象數(shù)組,使程序以相反的順序顯示對(duì)象數(shù)組的qu*price值。#include<iostream.h>class bookprivate:int qu;int price;int s;pub
9、lic:book(int p)qu=p;price=qu*10;int print()return(qu*price);int main() book a5=1,2,3,4,5;book *p;p=&a4;for(int i=4;i>=0;i-)cout<<p->print()<<endl;p-; return 0;6、構(gòu)建一個(gè)類Stock,含字符數(shù)組stockcode及整型數(shù)據(jù)成員quan、雙精度型數(shù)據(jù)成員price。構(gòu)造函數(shù)含3個(gè)參數(shù):字符數(shù)組na及q、p。當(dāng)定義Stock的類對(duì)象時(shí),將對(duì)象的第一個(gè)字符串參數(shù)賦給數(shù)據(jù)成員stockcode,第2個(gè)
10、和第3個(gè)參數(shù)分別賦給quan和price。未設(shè)置第2個(gè)和第3個(gè)參數(shù)時(shí),quan的值為1000,price的值為8.98。成員函數(shù)print()使用this指針,顯示對(duì)象內(nèi)容。#include<iostream.h>#include<string>class Stockchar stockcode10;int quan;double price;public: Stock(char na10,int q=1000,double p=8.98);void print();Stock:Stock(char na10,int q,double p) strcpy(stockco
11、de,na);quan=q;price=p;void Stock:print()cout<<this->stockcode<<","<<this->quan<<","<<this->price<<endl;void main()Stock m("sdgjgj",798,9.89);m.print();Stock n("sljf");cout<<"默認(rèn):"n.print();7、參考課本例子,建立
12、一個(gè)源程序文件,在此文件中建立一個(gè)新的類,將新建的類命名為Rect。class Rectpublic: int Area_int(); double Area_double(); Rect(double length,double width); Rect(int length,int width); virtual Rect();private: int nLength; int nWidth; double dLength; double dWidth;;【要求】(1)向Rect類中添加數(shù)據(jù)成員及成員函數(shù),并完善成員函數(shù)的功能。如設(shè)計(jì)一個(gè)Area_int()函數(shù),計(jì)算邊長(zhǎng)為整型的長(zhǎng)方形的面
13、積;設(shè)計(jì)一個(gè)Area_double()函數(shù),計(jì)算邊長(zhǎng)為double型的長(zhǎng)方形的面積。(2)重載構(gòu)造函數(shù)。一種構(gòu)造函數(shù)用整型變量記錄長(zhǎng)方形的長(zhǎng)和寬,另一種構(gòu)造函數(shù)用double型記錄。(3)體現(xiàn)對(duì)象的構(gòu)造和析構(gòu)過程。例如,在構(gòu)造函數(shù)中用cout<<”I am the constructor!”<<endl;在析構(gòu)函數(shù)中輸出cout<<”I am the destructor”<<endl。(4)在main()函數(shù)中定義兩個(gè)Rect類的對(duì)象,一個(gè)對(duì)象用實(shí)例實(shí)現(xiàn)(就像定義普通的變量一樣),另一個(gè)對(duì)象用指針實(shí)現(xiàn)(利用關(guān)鍵字new,給指針分配內(nèi)存空間)。并
14、用不同的參數(shù),以調(diào)用不同的構(gòu)造函數(shù)體現(xiàn)構(gòu)造函數(shù)的重載。#include<iostream.h>class Rectpublic: int Area_int(); double Area_double(); Rect(double length,double width); Rect(int length,int width); virtual Rect();private: int nLength; int nWidth; double dLength; double dWidth;int Rect:Area_int()int s;s=nLength*nWidth;cout<
15、<"int的長(zhǎng)方形的面積:"<<s<<endl;return 0;double Rect:Area_double()double k;k=dLength*dWidth;cout<<"double型的長(zhǎng)方形的面積: "<<k<<endl;return 0;Rect:Rect(int length,int width)nLength=length;nWidth=width;cout<<"I am the constructor!"<<endl;Rec
16、t:Rect() cout<<"I am the destructor"<<endl;Rect:Rect(double length,double width) dLength=length;dWidth=width; cout<<"I am the constructor!"<<endl;void main()Rect a(2,2),b(2.3,3.4);a.Area_int();b.Area_double(); Rect *p=new Rect(3,4); p->Area_int();delete
17、 p;Rect *q=new Rect(3.2,3.4);q->Area_double();delete q;8、聲明一個(gè)Student,在該類中包括一個(gè)數(shù)據(jù)成員score(分?jǐn)?shù))、兩個(gè)靜態(tài)數(shù)據(jù)成員total_score(總分)和count(學(xué)生人數(shù));還包括一個(gè)成員函數(shù)account()用于設(shè)置分?jǐn)?shù)、累計(jì)學(xué)生的成績(jī)之和、累計(jì)學(xué)生人數(shù),一個(gè)靜態(tài)成員函數(shù)sum()用于返回學(xué)生的成績(jī)之和,另一個(gè)靜態(tài)成員函數(shù)average()用于求全班成績(jī)的平均值。在main()函數(shù)中,輸入某班學(xué)生的成績(jī),并調(diào)用上述函數(shù)求出全班學(xué)生的成績(jī)之和和平均分。#include<iostream.h>cla
18、ss studentdouble score;static double tatal_score;static int count;static double ave;public:void account(double);static double sum();static double average();void print();void student:account(double m)score=m;tatal_score=tatal_score+m;+count;double student:sum()return(tatal_score);double student:avera
19、ge()ave=tatal_score/count;return ave;void student:print()cout<<"人數(shù)為:"<<count<<endl; cout<<"總成績(jī)?yōu)? "<<tatal_score<<"平均成績(jī)?yōu)椋?quot;<<ave<<endl;int student:count=0;double student:tatal_score=0.0;double student:ave=0.0;void main()stud
20、ent a;a.account(97);a.sum();a.average();student b;b.account(87);b.sum();b.average();b.print();9、設(shè)計(jì)一個(gè)用來表示直角坐標(biāo)系的Location類,在主程序中創(chuàng)建類Location的兩個(gè)對(duì)象A和B,要求A的坐標(biāo)點(diǎn)在第3象限,B的坐標(biāo)點(diǎn)在第2象限,分別采用成員函數(shù)和友元函數(shù)計(jì)算給定兩個(gè)坐標(biāo)點(diǎn)之間的距離,要求按如下格式輸出結(jié)果:A(x1,y1),B(x2,y2)Distance=d其中:x1、y1、x2、y2為指定的坐標(biāo)值,d為兩個(gè)坐標(biāo)點(diǎn)之間的距離。#include<iostream.h>#in
21、clude<math.h>class location double x; double y;public:location(double,double);void print(location m);friend void print(location &,location &);location:location(double m,double n)x=m;y=n;void location:print(location m)double dx=x-m.x;double dy=y-m.y; double d=sqrt(dx*dx+dy*dy);cout<&
22、lt;"AB的distance:"<<d<<endl;void print(location &a,location &b)double dx=a.x-b.x;double dy=a.y-b.y;double d=sqrt(dx*dx+dy*dy);cout<<"AB的distance:"<<d<<endl;void main()location A(-3.0,-2.0);location B(-4.3,4.3);A.print(B);print(A,B);10、使用C+的str
23、ing類,將5個(gè)字符串按逆轉(zhuǎn)后的順序顯示出來。例如,逆轉(zhuǎn)前的5個(gè)字符串是:Germany、Japan、American、British、France按逆轉(zhuǎn)后的順序輸出字符串為:France、British、American、Japan、Germany#include<iostream>#include<string>using namespace std;void main()string s5="Germany","Japan","American","British","France"for(int i=4;i>=0;i-)cout<<si<<endl;11、設(shè)計(jì)一個(gè)矩陣類Matrix,有分配空間和對(duì)矩陣賦值的功能,將這個(gè)矩陣類的對(duì)象作為參數(shù)傳送到函數(shù)Mul(Matrix a,Matrix b),用Mul(Matrix a,M
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 釋意理論指導(dǎo)下的模擬交替?zhèn)髯g實(shí)踐報(bào)告
- AAOA-MBR工藝處理南方低碳氮比城市污水的試驗(yàn)研究
- 《社會(huì)財(cái)務(wù)共享服務(wù)實(shí)務(wù)》課件-領(lǐng)域1任務(wù)2-07.票據(jù)錄入-工資類票據(jù)
- 中班健康安全小衛(wèi)士
- 教育的多元化發(fā)展路徑
- 顱腦損傷護(hù)理課件
- 《網(wǎng)頁設(shè)計(jì)與制作》課件-第9章頁面布局與風(fēng)格
- 愛國(guó)教育午會(huì)
- 預(yù)防哮喘班會(huì)課件
- 牧原企業(yè)文化培訓(xùn)總結(jié)
- 港口裝卸作業(yè)培訓(xùn)
- 2025年湖北省武漢市中考數(shù)學(xué)真題(無答案)
- 鉗工考試試題及答案
- 2025至2030中國(guó)牙科氧化鋯塊行業(yè)發(fā)展趨勢(shì)分析與未來投資戰(zhàn)略咨詢研究報(bào)告
- 拖欠維修費(fèi)車輛以車抵債協(xié)議范本
- 呼倫貝爾農(nóng)墾集團(tuán)有限公司招聘筆試題庫2025
- 醫(yī)院檢驗(yàn)科實(shí)驗(yàn)室生物安全程序文件SOP
- 校園文化建設(shè)方案(共60張PPT)
- 藍(lán)色海洋經(jīng)濟(jì)海事航海漁業(yè)水產(chǎn)養(yǎng)殖港口碼頭海運(yùn)PPT模板
- 不飽和聚酯樹脂化學(xué)品安全技術(shù)說明書MSDS
- 機(jī)動(dòng)車排放檢驗(yàn)比對(duì)試驗(yàn)報(bào)告
評(píng)論
0/150
提交評(píng)論