實驗二 類與對象_第1頁
實驗二 類與對象_第2頁
實驗二 類與對象_第3頁
實驗二 類與對象_第4頁
實驗二 類與對象_第5頁
已閱讀5頁,還剩16頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗二 類與對象一、實驗目的1、學習類與對象的定義,掌握類與對象的使用方法。2、學習數(shù)據(jù)成員與成員函數(shù)的訪問方式,理解構造函數(shù)和析構函數(shù)的定義與執(zhí)行過程,學會構造函數(shù)的重載方法。3、掌握數(shù)組與指針的定義與使用方法,理解數(shù)組與指針的存儲分配與表示。4、掌握用指針和引用向函數(shù)傳遞參數(shù)。5、掌握靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)的使用。6、理解友元與友元函數(shù)的作用與使用方法。二、實驗內容1、下面是一個計算器類的定義,請完成該類成員函數(shù)的實現(xiàn)。class Counter public: Counter(int number); void increment(); /給原值加1 void decrement()

2、; /給原值減1 int getValue(); /取得計數(shù)器值 int print(); /顯示計數(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ù)注釋語句的提示,實現(xiàn)類Date的成員函數(shù)。#include<iostream.h>class Date

4、public: void printDate();/顯示日期 void setDay(int d);/設置日的值 void setMonth(int m);/設置月的值 void setYear(int y);/設置年的值 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的構造函數(shù)被傳遞了兩個double值,分別表示圓柱體的半徑和高度。用類cylinder計算圓柱體的體積,并存儲在一個double變量中。在類cylinder中包含一個成員函數(shù)vol(),用來顯示每個cylinder

6、對象的體積。#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、構建一個類book,其中含有兩個私有數(shù)據(jù)成員qu和price,建立一個有5個元素的數(shù)組對象,將qu初始化為15,將price初始化為qu的10倍。顯示每個對象的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、修改上題,通過對象指針訪問對象數(shù)組,使程序以相反的順序顯示對象數(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、構建一個類Stock,含字符數(shù)組stockcode及整型數(shù)據(jù)成員quan、雙精度型數(shù)據(jù)成員price。構造函數(shù)含3個參數(shù):字符數(shù)組na及q、p。當定義Stock的類對象時,將對象的第一個字符串參數(shù)賦給數(shù)據(jù)成員stockcode,第2個

10、和第3個參數(shù)分別賦給quan和price。未設置第2個和第3個參數(shù)時,quan的值為1000,price的值為8.98。成員函數(shù)print()使用this指針,顯示對象內容。#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<<"默認:"n.print();7、參考課本例子,建立

12、一個源程序文件,在此文件中建立一個新的類,將新建的類命名為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ù)的功能。如設計一個Area_int()函數(shù),計算邊長為整型的長方形的面

13、積;設計一個Area_double()函數(shù),計算邊長為double型的長方形的面積。(2)重載構造函數(shù)。一種構造函數(shù)用整型變量記錄長方形的長和寬,另一種構造函數(shù)用double型記錄。(3)體現(xiàn)對象的構造和析構過程。例如,在構造函數(shù)中用cout<<”I am the constructor!”<<endl;在析構函數(shù)中輸出cout<<”I am the destructor”<<endl。(4)在main()函數(shù)中定義兩個Rect類的對象,一個對象用實例實現(xiàn)(就像定義普通的變量一樣),另一個對象用指針實現(xiàn)(利用關鍵字new,給指針分配內存空間)。并

14、用不同的參數(shù),以調用不同的構造函數(shù)體現(xiàn)構造函數(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的長方形的面積:"<<s<<endl;return 0;double Rect:Area_double()double k;k=dLength*dWidth;cout<<"double型的長方形的面積: "<<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、聲明一個Student,在該類中包括一個數(shù)據(jù)成員score(分數(shù))、兩個靜態(tài)數(shù)據(jù)成員total_score(總分)和count(學生人數(shù));還包括一個成員函數(shù)account()用于設置分數(shù)、累計學生的成績之和、累計學生人數(shù),一個靜態(tài)成員函數(shù)sum()用于返回學生的成績之和,另一個靜態(tài)成員函數(shù)average()用于求全班成績的平均值。在main()函數(shù)中,輸入某班學生的成績,并調用上述函數(shù)求出全班學生的成績之和和平均分。#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<<"總成績?yōu)? "<<tatal_score<<"平均成績?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、設計一個用來表示直角坐標系的Location類,在主程序中創(chuàng)建類Location的兩個對象A和B,要求A的坐標點在第3象限,B的坐標點在第2象限,分別采用成員函數(shù)和友元函數(shù)計算給定兩個坐標點之間的距離,要求按如下格式輸出結果:A(x1,y1),B(x2,y2)Distance=d其中:x1、y1、x2、y2為指定的坐標值,d為兩個坐標點之間的距離。#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個字符串按逆轉后的順序顯示出來。例如,逆轉前的5個字符串是:Germany、Japan、American、British、France按逆轉后的順序輸出字符串為: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、設計一個矩陣類Matrix,有分配空間和對矩陣賦值的功能,將這個矩陣類的對象作為參數(shù)傳送到函數(shù)Mul(Matrix a,Matrix b),用Mul(Matrix a,M

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論