C程序設(shè)計(jì)綜合實(shí)驗(yàn)_第1頁(yè)
C程序設(shè)計(jì)綜合實(shí)驗(yàn)_第2頁(yè)
C程序設(shè)計(jì)綜合實(shí)驗(yàn)_第3頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、深圳大學(xué)實(shí) 驗(yàn) 報(bào) 告課程名稱(chēng): 面向?qū)ο蟪绦蛟O(shè)計(jì)實(shí)驗(yàn)序號(hào):試驗(yàn)七實(shí)驗(yàn)名稱(chēng): C+程序設(shè)計(jì)綜合實(shí)驗(yàn)班 級(jí):姓 名:隔壁老王學(xué) 號(hào):2010100001實(shí)驗(yàn)日期:2011年12月_!1日、實(shí)驗(yàn)?zāi)康模?1) 掌握類(lèi)和對(duì)象的實(shí)現(xiàn);(2) 掌握類(lèi)的靜態(tài)成員函數(shù)和友元函數(shù)的運(yùn)用;(3) 掌握類(lèi)的繼承與派生的編程特點(diǎn);(4) 掌握運(yùn)算符承載的程序設(shè)計(jì)。、實(shí)驗(yàn)環(huán)境: 硬件環(huán)境:辦公樓二樓軟件實(shí)驗(yàn)室 軟件環(huán)境: Visual C+ 6.0 集成環(huán)境三、實(shí)驗(yàn)要求:1. 定義一個(gè)課程類(lèi)CCourse,其中包含課程號(hào)(Iong no)、課程學(xué)分(float credit) 兩個(gè)數(shù)據(jù)成員,以及相應(yīng)的構(gòu)造函數(shù)、拷貝構(gòu)造

2、函數(shù)、析構(gòu)函數(shù)和打印數(shù)據(jù) 成員的成員函數(shù) print()。2. 為CCourse類(lèi)增加一個(gè)數(shù)據(jù)成員課程總數(shù)(int total_course),并增加一個(gè)成 員函數(shù) getTotalCourse()獲取total_course的值,編寫(xiě)一個(gè)友元函數(shù) getCourseNo(獲取課程號(hào)no。做如上修改后,重新實(shí)現(xiàn) CCourse類(lèi)(與第1 問(wèn)相同的不用再重復(fù),注意說(shuō)明數(shù)據(jù)成員和成員函數(shù)的存儲(chǔ)類(lèi)型,以便能夠 用類(lèi)名來(lái)調(diào)用 getTotalCourse()。3. 為CCourse類(lèi)定義小于運(yùn)算符()運(yùn)算符重載函數(shù)。CCourse類(lèi)對(duì)象大 小的比較是根據(jù)其課程學(xué)分(credit)的值的大小來(lái)實(shí)現(xiàn)的(與

3、第2問(wèn)相同的不 用再重復(fù) ) 。4. 編寫(xiě)測(cè)試程序?qū)course類(lèi)進(jìn)行測(cè)試。5. 以CCourse類(lèi)為基類(lèi),派生出面向?qū)ο蟪绦蛟O(shè)計(jì)課程類(lèi)COOP,并在該類(lèi)中增加一個(gè)表示開(kāi)課單位的指針數(shù)據(jù)成員(char *p_ope nby)和根據(jù)學(xué)生學(xué)號(hào)判 斷能否選課的成員函數(shù) bool select(const char *p_xh)(只有學(xué)號(hào)前4位為2010 的學(xué)生可選面向?qū)ο蟪绦蛟O(shè)計(jì)課程 )。寫(xiě)出 COOP 類(lèi)的完整定義 (包括構(gòu)造、 拷貝構(gòu)造、析構(gòu)和select()成員函數(shù)的實(shí)現(xiàn))。6. 編寫(xiě)測(cè)試程序進(jìn)行測(cè)試。7. 為了能夠采用動(dòng)態(tài)聯(lián)編的方式調(diào)用派生類(lèi) COOP 的 bool select( con

4、st char *p_xh)成員函數(shù),應(yīng)該在 Ccourse類(lèi)及其派生類(lèi)C00!中作何改動(dòng)?四、實(shí)驗(yàn)內(nèi)容與結(jié)果: (源程序及運(yùn)行截圖)1、class CCourseprivate: long no; float credit; char *p_name;public: CCourse(); CCourse(long n,char *na,float c); CCourse(const CCourse &course); void print(); CCourse(); ;CCourse:CCourse() no=0;p_name=new char20; strcpy(p_name,course

5、 name); credit=0.0; CCourse:CCourse(long n,char *na,float c) no=n;p_name=new char20; strcpy(p_name,na); credit=c; CCourse:CCourse(const CCourse &course) p_name=new charstrlen(course.p_name)+1;if(p_name=NULL) exit(0);strcpy(p_name,course.p_name); credit=course.credit; void CCourse:print() coutCourse

6、number:noendl; coutCourse name:p_nameendl; coutCourse credit:creditendl; CCourse:CCourse() delete p_name;2、class CCourseprivate: long no; float credit; char *p_name;static int total_course;public:CCourse();CCourse(long n,char *na,float c); CCourse(const CCourse &course); void print();CCourse();stati

7、c getTotalCourse() return total_course; friend long getCourseNo(const CCourse &course);int CCourse:total_course = 0;CCourse:CCourse() no=0; p_name=new char20; strcpy(p_name,course name); credit=0.0; CCourse:CCourse(long n,char *na,float c) no=n; p_name=new char20; strcpy(p_name,na); credit=c; total_

8、course+; CCourse:CCourse(const CCourse &course) p_name=new charstrlen(course.p_name)+1;if(p_name=NULL)exit(0); strcpy(p_name,course.p_name); credit=course.credit; total_course+; void CCourse:print() coutCourse number:noendl; coutCourse name:p_nameendl; coutCourse credit:creditendl;CCourse:CCourse()

9、delete p_name; total_course-;elselong getCourseNo(const CCourse &course) return course.no; 3、class CCoursepublic:bool operator (const CCourse &course); ;int CCourse:total_course = 0;bool CCourse:operator (const CCourse &course) if (credit course.credit)return true;elsereturn false;4、源程序:#include usi

10、ng namespace std;#include class CCourseprivate:long no;float credit;char *p_name;static int total_course;public:CCourse();CCourse(long n,char *na,float c);CCourse(const CCourse &course); void print();CCourse();static getTotalCourse() return total_course; friend long getCourseNo(const CCourse &course

11、);bool operator (const CCourse &course);bool CCourse:operator (const CCourse &course)if (credit course.credit)return true;return false;CCourse:CCourse()no=0;p_name=new char20;strcpy(p_name,course name);credit=0.0;CCourse:CCourse(long n,char *na,float c)no=n;p_name=new char20;strcpy(p_name,na);credit

12、=c; total_course+;CCourse:CCourse(const CCourse &course)p_name=newcharstrlen(course.p_name)+1;if(p_name=NULL)exit(0);strcpy(p_name,course.p_name);credit=course.credit;total_course+;void CCourse:print()coutCourse number:noendl;coutCourse name:p_nameendl; coutCourse credit:creditendl;CCourse:CCourse()

13、delete p_name;total_course-;int CCourse:total_course = 0;long getCourseNo(c onst CCourse &course)retur n course .no;void mai n()int c=0;long sc;CCourse course1(2011100,高 等數(shù)學(xué) ,5.0);course1.pri nt();CCourse course2(2011101,大 學(xué)英語(yǔ) ,2.5);course2.pri nt();CCourse course3(2011102,線(xiàn) 性代數(shù) ,3.5);course3.pri nt

14、();CCourse course4(2011103,面向?qū)ο蟪绦蛟O(shè)計(jì),4.0);course4.pri nt();c=course4.getTotalCourse(); sc=getCourseNo(course1); coutTotalcourse:ce ndlcourse1sNO:sce ndl;if(course1course2) coutcourse2s credit larger than course1s.e ndl;elsecoutcourse2s credit do notlarger tha n course1s.se naim號(hào)二咼等數(shù)學(xué)Jou.i*se credit:

15、5course nunber:2011101n為me誥大學(xué)英語(yǔ)credit :2.5Jo UPS e n mnbe r = 2011102Ui*s e n ame * 線(xiàn)性代數(shù)course credit 3ourse number : 2011103bourse name:面向?qū){裡序設(shè)計(jì)Toijii*se credit : 4Fotal coui*se-4s NO = 2011100;our,se2f s credit do not lartfer than cour,sel, s.3ress any key to comt inueH5-6、源程序:class COOP : public

16、CCourseprivate:char *p_openby;public:bool select(c onst char *p_xh)if(strncmp(p_xh,2010,4)=0) return true;elsereturn false;COOP(long n, char *na, float c, char *p_ope n) : CCourse (n,n a,c)p_openby=newcharstrle n( p_ope n)+1;strcpy(p_ope nby, p_ope n);COOP(co nst COOP & coop)p_openby=newcharstrle n(

17、 coop.p_ope nby)+1;if(p_ope nby=NULL)exit(0);strcpy(p_ope nby,coop.p_ope nby);COOP() delete p_ope nby; void prin t()CCourse:pri nt();cout 開(kāi) 課 單 位p_ope nbye ndl;void mai n()程序截圖:char stn o20;COOP coop1(2011103,面向?qū)ο蟪绦蛟O(shè) 計(jì),4.0,計(jì)算機(jī)與軟件設(shè)計(jì)學(xué)院”);coopl.pri nt();cout請(qǐng)輸入學(xué)號(hào): st no;if(coop1.select(st no)cout可以選課en

18、dl;elsecout不能選課endl;程序截圖:7、要實(shí)現(xiàn)動(dòng)態(tài)聯(lián)編成員函數(shù)必須聲明為 則派生類(lèi)中不必再聲明。在class CCourse定義中增加:public:virtual bool select(c onst char *p_xh)if(strncmp(p_xh,2010,4)=0)return true;elsereturn false; ourse n umbei* : -2078 976689 oupse n罰e:面向?qū)ο蟪绦蛟O(shè)計(jì) oursa credit:4迷單位:計(jì)算機(jī)與軟件設(shè)計(jì)學(xué)院 號(hào):!0105 00123可以選課Course Course Jour scnumber:-2078976689 name :面向?qū)ο蟪绦蛟O(shè)計(jì) credit:4

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論