麗水數(shù)理c++實驗報告二_第1頁
麗水數(shù)理c++實驗報告二_第2頁
麗水數(shù)理c++實驗報告二_第3頁
麗水數(shù)理c++實驗報告二_第4頁
麗水數(shù)理c++實驗報告二_第5頁
已閱讀5頁,還剩19頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗一 c+類的定義及成員引用一、實驗目的:1. 掌握類和對象的概念及特性;2. 掌握c+類和對象的定義;3. 掌握對象成員的基本引用方法;4. 掌握簡單的面向對象程序的編寫;二、實驗任務題目1 圖形類設計設計三個圖形類:circle(圓)、rectangle(矩形)、triangle(三角形);1、cirlce 類基本信息:圓心坐標、半徑;rectangle 類基本信息:長、寬;triangle 類基本信息:三個頂點坐標;其中:成員變量為 private 屬性,成員函數(shù)為public 屬性;2、每個圖形類有計算圖形的面積getarea(),顯示圖形的基本信息函數(shù)show(),修改基本信息的函

2、數(shù)set(形參)。以circle 類為例:通過getarea()計算圓的面積,show()函數(shù)中顯示圓心坐標、直徑、周長、面積等基本信息;set(int x,int y, int r)函數(shù)可以修改圓心坐標和半徑。3、當輸入數(shù)據(jù)為不合理數(shù)據(jù)時(例如:輸入的三角形的頂點是否能組成一個三角形),提示用戶輸入錯誤;測試數(shù)據(jù)1、circle 類測試數(shù)據(jù):圓心:(20,30) 半徑:5(-20,40) 30(40,-10) -52、rectangle 類測試數(shù)據(jù):頂點坐標:20,5040,5-9,1043,-83、triangle 類測試數(shù)據(jù):頂點:(20,30)、(40,50)、(40,20)(10,1

3、0)、(50,10)、(35,10)源程序:#include#include#includeusing namespace std;/圓的面積 class circleprivate:int x;int y;int r;public:circle ()int x=0;int y=0;int r=0;float circle_getarea();void circle_show(); void circle_set(int a,int b,int c)x=a;y=b;r=c;float circle:circle_getarea()float s;s=3.14*r*r;return s;void

4、 circle:circle_show() float s;if(r0)cout(x,y)setw(6)rsetw(6)2*rsetw(6)2*3.14*rsetw(6)sendl;elsecout輸入的數(shù)據(jù)錯誤!=b) a=b; if(a=c)a=c;if(b=c)b=c; if(x1-x4)*(x1-x2)+(y1-y4)*(y1-y2)=0&(x1-x2=x4-x3)&(y1-y2=y4-y3) length=a; width=b; ;float rectangle: rectangle_getarea() float z;z=length*width;return z;void rec

5、tangle:rectangle_show() float z;if(z0)cout矩形的長:lengthsetw(2) 寬: widthsetw(2) 面積: zendl; elsecout輸入數(shù)據(jù)錯誤!l3&l1+l3l2&l2+l3l1) d=(l1+l2+l3)/2; s=sqrt(d*(d-l1)*(d-l2)*(d-l3); else s=0;void triangle_show();void triangle_set(int x1,int y1,int x2,int y2,int x3,int y3) m1=x1;n1=y1;m2=x2;n2=y2;m3=x3;n3=y3; fl

6、oat a,b,c;a=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);b=sqrt(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); c=sqrt(x3-x2)*(x3-x2)+(y3-y2)*(y3-y2); l1=a; l2=b; l3=c; ;void triangle:triangle_show()float s;int c;c=!s;switch(c)case 0: cout三角形三點坐標;setw(2)(m1,n1)setw(2)(m2,n2)setw(2)(m3,n3)endl; cout三角形邊長:setw(2)l1 l2 l3endl;

7、 cout三角形的面積:setw(2)sendl; break;case 1: cout輸入數(shù)據(jù)錯誤!x1y1r1; cinx2y2r2;cinx3y3r3;cina1b1;cina2b2;cina3b3;cina4b4;cink1l1k2l2k3l3;cink4l4k5l5k6l6; c1.circle_set( x1, y1,r1); c2.circle_set(x2, y2, r2); c3.circle_set( x3, y3, r3);r.rectangle_set(a1,b1,a2,b2,a3,b3,a4,b4);t1.triangle_set(k1,l1,k2,l2,k3,l3)

8、;t2.triangle_set(k4,l4,k5,l5,k6,l6);cout-圓-endl;cout圓心坐標setw(6)直徑setw(6)半徑setw(6)周長setw(6)面積endl;c1.circle_getarea();c1.circle_show();c2.circle_getarea();c2.circle_show();c3.circle_getarea();c3.circle_show();cout-矩形-endl;r.rectangle_getarea();r.rectangle_show();cout-三角形- endl;t1.triangle_getarea();t

9、1.triangle_show();t2.triangle_getarea();t2.triangle_show();return 0;運行結果: 題目2 學生類設計設計一個 studnet(學生)類1、基本信息:學號、姓名、性別、出生日期、年級、班級、院系、專業(yè);其中:基本信息為 private 屬性,成員函數(shù)為public 屬性;2、student 類有多個構造函數(shù):缺省構造函數(shù)、帶參數(shù)的構造函數(shù)、帶默認參數(shù)的構造函數(shù)3、基本功能:1)可以從鍵盤輸入學生的基本信息;2)setinfo(形參表)函數(shù)可以修改學生的基本信息函數(shù),例如:班級、專業(yè)等;3)show()函數(shù)顯示學生信息;4) 當輸入

10、數(shù)據(jù)為不合理數(shù)據(jù)時,提示用戶輸入錯誤;測試數(shù)據(jù)1、學號:2005001 姓名:zhangesan 出生日期:85 年11 月年級:05 級班級:jy01 院系:computer 專業(yè):application2、學號:2005002 姓名:lihuan 出生日期:84 年11 月3、學號:2005003 姓名:wang 出生日期:85 年7 月年級:05 級班級:jy01 院系:computer 專業(yè):software4、學號:2005004 姓名:zhan 出生日期:85 年11 月年級:05 級班級:jy025、學號:2005005 姓名:song 出生日期:85 年11 月思考與擴展采用什

11、么方式可以讓學號自動按輸入順序生成?源程序代碼:#include#include#includeusing namespace std;class studentprivate:int number;string name;string year;string grade;string clas;string department;string profession;public:student( )grade=05級; student(string cla); /student:student(int num=2005001):number(num)void setinto( int b,

12、string b1,string b2,string b3,string b4,string b5,string b6) number=b;name=b1;year=b2;grade=b3;clas=b4;department=b5;profession=b6;void show();student:student(string cla)clas=cla; void student:show() cout學號:numbersetw(6)姓名:namesetw(10)出生日期:yearsetw(6)年級:gradesetw(6)班級:classetw(6)院系:departmentsetw(6)

13、專業(yè):professionendlendl; int main() student stud5; int i,j,num;for(i=0,num=2005001;ia1a2a3a4a5a6;studi.setinto(num,a1, a2, a3,a4, a5,a6);studi.show(); return 0;操作結果:實驗二 c+中的構造函數(shù)與析構函數(shù)一、實驗目的:5. 掌握c+中使用構造函數(shù)創(chuàng)建對象;6. 掌握c+中使用析構函數(shù)釋放對象;7. 掌握構造函數(shù)的重載特性;8. 掌握類相關的指針及數(shù)組的使用方法;9. 掌握const成員及static成員的特性;10. 掌握友元的特性。二、實

14、驗任務1. 人員管理設計某小型公司的 employee(人員)類(1)類1)employee 類:基本信息:編號、姓名、性別、出生日期、職位等;出生日期使用自定義的 date(日期)類;其中:基本信息為 private 屬性,成員函數(shù)為public 屬性;多個構造函數(shù):缺省構造函數(shù)、帶參數(shù)的構造函數(shù)、帶默認參數(shù)的構造函數(shù);可以從外部訪問類成員的友員函數(shù);2)date 類:成員變量:年、月、日成員函數(shù):setyear(int year)、setmonth(int month)、setday(int day)getyear()、getmonth()、getday()(2)基本功能:1)職工信息的錄

15、入;2)職工信息的顯示;3)用對象數(shù)組保存已輸入的職工對象;4)可以修改人員的基本信息,如:姓名、職位等;5)可以通過編號或姓名進行人員查詢;(3)實驗提示1)注意帶參數(shù)的構造函數(shù)和帶默認參數(shù)的構造函數(shù)的聲明與定義;2)定義employee 類的成員變量時,應注意變量類型的聲明;3)在查詢時,通過申明成employee 類的友元函數(shù)來訪問類的成員變量;*/#include #include #define n 100using namespace std;class dateprivate: int year; int month; int day;public: void setyear(i

16、nt x) year=x; void setmonth(int x) month=x; void setday(int x) day=x; int getyear() return year; int getmonth() return month; int getday() return day; date():year(2000),month(1),day(1) date(int x, int y,int z) year=x; month=y; day=z; ;class employeeprivate: int no; char name16; char sex8; date birth

17、; char pos32;public: void setname(char* n) strcpy(name,n); void setpos(char* n) strcpy(pos,n); employee():no(0),birth(2000,1,1) strcpy(name,andy); strcpy(sex,male); strcpy(pos,intern); employee& operator=(employee& x) no=x.no; birth.setyear(x.birth.getyear(); birth.setmonth(x.birth.getmonth(); birth

18、.setday(x.birth.getday(); strcpy(name,); strcpy(sex,x.sex); strcpy(pos,x.pos); employee(int n, char* str, char* y,date day, char* p) no = n; strcpy(name,str); strcpy(sex,y); birth=day; strcpy(pos,p); friend string getname(employee x) string name; int i; for(i=0;i!=0;i+) name+=i; re

19、turn name; friend int getno(employee x) return x.no; friend string getsex(employee x) string sex; int i; for(i=0;x.sexi!=0;i+) sex+=x.sexi; return sex; friend date getbirth(employee x) return x.birth; friend string getpos(employee x) string pos; int i; for(i=0;x.posi!=0;i+) pos+=x.posi; return pos;

20、;void main() employee *personsn; employee temp; date day; int num=0; int i,choose=1,choose1=0; int n,y,m,d; char str16; char s8; char p32; coutntt=endl; couttt1:錄入 2:顯示 3:修改 4:查找 0:退出endl; couttt=endl; coutchoose; while(choose!=0) switch(choose) case 1: if(num=15) coutfull!n; continue; coutn; coutymd; coutstr; couts; coutp; personsnum+=new employee(n,str,s,date(y,m,d),p); break; case 2: cout編號t姓名t性別t生日tt職務endl; for(i=0;inum;i+) coutgetno(*personsi)tgetname(*personsi)tgetsex(*personsi)tgetbirth(*personsi).getyear() getbirth(*personsi).getmonth() getbirth(*personsi).getday()tgetpos(*personsi)

溫馨提示

  • 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

提交評論