c++程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第1頁(yè)
c++程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第2頁(yè)
c++程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第3頁(yè)
c++程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第4頁(yè)
c++程序設(shè)計(jì)實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩36頁(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、實(shí)驗(yàn)報(bào)告七 類與對(duì)象1. 實(shí)驗(yàn)?zāi)康模?) 掌握類的定義和實(shí)現(xiàn)。(2) 掌握對(duì)象創(chuàng)建及使用的基本方法。2. 實(shí)驗(yàn)設(shè)備 硬件環(huán)境:微型計(jì)算機(jī)軟件環(huán)境: 操作系統(tǒng): windows 語(yǔ)言環(huán)境: visual c+ 3. 實(shí)驗(yàn)內(nèi)容(1)下面程序定義了一個(gè)以hours, minutes和seconds作為數(shù)據(jù)成員的time類。設(shè)計(jì)了成員函數(shù)將兩個(gè)time對(duì)象相加(即時(shí)間相加),并進(jìn)行相應(yīng)的檢查,查看增加的分鐘數(shù)及秒數(shù)是否大于59。如果秒數(shù)大于59,則分鐘數(shù)向前遞增1。類似地,如果分鐘數(shù)大于59,則小時(shí)數(shù)向前增1。#include class timeprivate: int hours, minutes

2、, seconds;public: void get_time() cinhoursminutesseconds; void display_time() couthours:minutes:seconds=60) seconds-=60; minutes+; if(minutes=60) minutes-=60; hours+; ;void main() time one, two, three; coutnenter the first time(hours minutes seconds):; one.get_time(); coutnenter the second time(hour

3、s minutes seconds):; two.get_time(); three.add_time(one,two); coutthe result is:yearmonthday; date mydate(year,month,day); int &myyear=mydate.getyear();int &mymonth=mydate.getmonth();int &myday=mydate.getday(); coutmyyearendlmymonthendlmydayendl;myyear=8888;cout mydate.getyear();基本要求仔細(xì)閱讀上面程序,如果有錯(cuò)誤,請(qǐng)

4、更正。上機(jī)錄入、調(diào)試上面程序。分析和思考main函數(shù)中int &myyear=mydate.getyear(); 、int &mymonth=mydate.getmonth(); 和int &myday=mydate.getday();語(yǔ)句表達(dá)的是什么思想?這樣做的目的是什么?這種方法是否“好”呢?為什么?如果“不好”應(yīng)該怎樣修改?4. 源代碼1.#include class timeprivate: int hours, minutes, seconds;public: time () time (int x,int y,int z)hours=x;minutes=y;seconds=z;

5、/* void get_time() cinhoursminutesseconds; */ void display_time() couthours:minutes:seconds=60) seconds-=60; minutes+; while(minutes=60) minutes-=60; hours+; ;void main() time one( 2 , 67 , 100), two( 1 , 56 , 200), three; three.add_time(one,two); coutthe result is:endl; three.display_time();2.#incl

6、udeclass date public: date(); date(int year,int month,int day); date(); int &getyear()return year; int &getmonth()return month; int &getday()return day; private: int year; int month; int day; static bool isleapyear;/是否閏年 ;bool date:isleapyear=true; date:date(int year,int month,int day) (*this).year=

7、year; (*this).month=month; (*this).day=day;void main() int year,month,day; cinyearmonthday; date mydate(year,month,day); int &myyear=mydate.getyear();int &mymonth=mydate.getmonth();int &myday=mydate.getday(); coutmyyearendlmymonthendlmydayendl;myyear=8888;cout=60) seconds-=60; minutes+; if(minutes=6

8、0) minutes-=60; hours+; 用if時(shí)當(dāng)seconds和minutes=60時(shí),程序只減一次60,如果seconds和minutes是60的兩倍或以上的話,明顯減的不夠。所以改用while的話就可以很好的解決這個(gè)問(wèn)題了。2、int &myday=mydate.getday();是對(duì)mydate.getday()的引用,相當(dāng)于給它起了個(gè)別名叫做myday,所以當(dāng)myyear=8888;時(shí),cout mydate.getyear();輸出的也是8888.7.思考題解答main函數(shù)中int &myyear=mydate.getyear(); 、int &mymonth=mydate

9、.getmonth(); 和int &myday=mydate.getday();語(yǔ)句表達(dá)的是什么思想?這樣做的目的是什么?這種方法是否“好”呢?為什么?如果“不好”應(yīng)該怎樣修改?答:int &myyear=mydate.getyear(); 、int &mymonth=mydate.getmonth(); 和int &myday=mydate.getday();是引用,相當(dāng)于給右邊的變量起了個(gè)別名。這樣做,“myyear=8888;cout mydate.getyear();”輸出的就是8888了。這樣不好,破壞了類的封裝性,導(dǎo)致類的私有成員數(shù)據(jù)在類外可以被隨意修改。 實(shí)驗(yàn)報(bào)告八 繼承與派生

10、類1. 實(shí)驗(yàn)?zāi)康模?) 掌握單繼承程序設(shè)計(jì)的基本方法。(2) 掌握多繼承程序設(shè)計(jì)的基本方法。2. 實(shí)驗(yàn)設(shè)備 硬件環(huán)境:微型計(jì)算機(jī)軟件環(huán)境: 操作系統(tǒng): windows 語(yǔ)言環(huán)境: visual c+ 3. 實(shí)驗(yàn)內(nèi)容(1) 下面程序定義一個(gè)vehicle類,并派生出car和truck兩個(gè)派生類。#includeclass vehicleprotected: int wheels; double weight;public: void initialize(int whls, double wght); int get_wheels() return wheels; double get_weig

11、ht() return weight; double wheel_loading() return weight/wheels; ;class car: public vehicleprivate: int passenger_load;public: void initialize(int whls, double wght, int people =4); int passengers() return passenger_load; ;class truck: public vehicleprivate: int passenger_load; double payload;public

12、: void init_truck(int number =2, double max_load =24000.0); double efficiency(); int passengers() return passenger_load; ;void vehicle:initialize(int whls, double wght) wheels=whls; weight=wght;void car:initialize(int whls, double wght, int people) wheels=whls; weight=wght; passenger_load=people;voi

13、d truck:init_truck(int number, double max_load) passenger_load=number; payload=max_load;double truck:efficiency() return payload/(payload+weight);void main() vehicle bicycle; bicycle.initialize(2,25); coutthe bicycle has bicycle.get_wheels() wheels.n; coutthe bicycle weighs bicycle.get_weight() poun

14、ds.n; coutthe bicycles wheel loading is bicycle.wheel_loading() pounds per tire.nn; car audi; audi.initialize(4,3500.0,5); coutthe audi has audi.get_wheels() wheels.n; coutthe audi weighs audi.get_weight() pounds.n; coutthe audis wheel loading is audi.wheel_loading() pounds per tire.nn; truck jief;

15、jief.initialize(18,12500.0); jief.init_truck(2,33675.0); coutthe jief has jief.get_wheels() wheels.n; coutthe jief weighs jief.get_weight() pounds.n; coutthe jiefs efficiency is 100.0*jief.efficiency() percent.n;基本要求l 上機(jī)錄入、調(diào)試上面程序。l 運(yùn)行程序,觀察運(yùn)行結(jié)果是否正確且滿足題意要求。l 將class car: public vehicle和class truck: pub

16、lic vehicle分別改為:class car: private vehicle和class truck: private vehicle程序運(yùn)行結(jié)果有無(wú)變化,為什么?分析與思考l 定義并實(shí)現(xiàn)vehicle類、car類和truck類的構(gòu)造函數(shù),完成vehicle類、car類和truck類的數(shù)據(jù)成員初始化工作。l 將vehicle中數(shù)據(jù)成員wheels和weight改為private性質(zhì),如何修改程序以達(dá)到相同的輸出結(jié)果。person(2)下面程序?qū)?yīng)圖1所示的類層次繼承結(jié)構(gòu):person#include #include graduateteacher#include class pers

17、on protected:char name20;in-service_graduateint birth_year;public:person(char *na, int year) strcpy(name,na);birth_year=year;int cal_age(int this_year) return this_year-birth_year;class graduate :public person protected:int grade;char specialty20;public:graduate(char *na, int y, int g, char *spec):p

18、erson(na,y) grade=g;strcpy(specialty,spec);void display(int this_year) cout graduate age grade specialtyn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(7)gradesetw(17)specialtyendl;class teacher :public person protected:char title15;char specialty20;public:teacher(char *na, int y, char *ti, cha

19、r *spec):person(na,y) strcpy(title,ti);strcpy(specialty,spec);void display(int this_year) cout teacher age title specialtyn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(14)titlesetw(17)specialtyendl;class in_service_graduate:public teacher, public graduatepublic: in_service_graduate(char *na,

20、int y, char *ti, char *spec1, int g, char *spec2): teacher(na, y, ti, spec1), graduate(na, y, g, spec2) void display(int this_year) cout in_service_graduate age title work_specialty grade study_specialtyn;coutsetw(20)namesetw(5)cal_age(this_year)setw(10)title;coutsetw(15)teacher:specialtysetw(7)grad

21、esetw(17)graduate:specialtyendl; ;void main()graduate gr(zhang_ling,1978,2001,computer);teacher te(wang_qiang, 1976,tutor,electronics);in_service_graduate sg(liu_hua,1975,lectuer,automation,2002,computer);gr.display(2002);coutendl;te.display(2002);coutendl;sg.display(2002);基本要求l 閱讀程序,完成in_service_gr

22、aduate類中的display()函數(shù)的程序。l 上機(jī)錄入、調(diào)試上面程序。l 運(yùn)行程序,觀察運(yùn)行結(jié)果是否正確且滿足題意要求。分析與思考l 在上面程序中類person中的數(shù)據(jù)成員name和birth_year在in_service_graduate類中有兩個(gè)副本,請(qǐng)使用虛基類使它們?cè)趇n_service_graduate類中只有一個(gè)副本。注意同時(shí)修改程序的其他相關(guān)部分。4. 源代碼1.#includeclass vehicleprotected: int wheels; double weight;public: vehicle(int x,double y)wheels=x;weight=y

23、; int get_wheels() return wheels; double get_weight() return weight; double wheel_loading() return weight/wheels; ;class car: public vehicleprivate: int passenger_load;public:car(int x,double y,int z=4):vehicle(x,y)passenger_load=z; / void initialize(int whls, double wght, int people=4 ); int passen

24、gers() return passenger_load; ;class truck: public vehicleprivate: int passenger_load; double payload;public: truck(int x,double y,int z=2,double w=24000.0):vehicle(x,y)passenger_load=z;payload=w; / void init_truck(int number =2, double max_load =24000.0); double efficiency(); int passengers() retur

25、n passenger_load; ;double truck:efficiency() return payload/(payload+weight);void main() vehicle bicycle(2,25); coutthe bicycle has bicycle.get_wheels() wheels.n; coutthe bicycle weighs bicycle.get_weight() pounds.n; coutthe bicycles wheel loading is bicycle.wheel_loading() pounds per tire.nn; car a

26、udi(4,3500.0,5); coutthe audi has audi.get_wheels() wheels.n; coutthe audi weighs audi.get_weight() pounds.n; coutthe audis wheel loading is audi.wheel_loading() pounds per tire.nn; truck jief(18,12500.0,2,33675.0); coutthe jief has jief.get_wheels() wheels.n; coutthe jief weighs jief.get_weight() p

27、ounds.n; coutthe jiefs efficiency is 100.0*jief.efficiency() percent.n;2.#include #include #include class person protected:char name20;int birth_year;public:person(char *na, int year) strcpy(name,na);birth_year=year;int cal_age(int this_year) return this_year-birth_year;class graduate :virtual publi

28、c person protected:int grade;char specialty20;public:graduate(char *na, int y, int g, char *spec):person(na,y) grade=g;strcpy(specialty,spec);void display(int this_year) cout graduate age grade specialtyn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(7)gradesetw(17)specialtyendl;class teacher :

29、virtual public person protected:char title15;char specialty20;public:teacher(char *na, int y, char *ti, char *spec):person(na,y) strcpy(title,ti);strcpy(specialty,spec);void display(int this_year) cout teacher age title specialtyn;coutsetw(20)namesetw(5)cal_age(this_year);coutsetw(14)titlesetw(17)sp

30、ecialtyendl;class in_service_graduate:public teacher, public graduatepublic: in_service_graduate(char *na, int y, char *ti, char *spec1, int g, char *spec2): teacher(na, y, ti, spec1), graduate(na, y, g, spec2),person(na,y) void display(int this_year) cout in_service_graduate age title work_specialt

31、y grade study_specialtyn;coutsetw(20)namesetw(5)cal_age(this_year)setw(10)title;coutsetw(15)teacher:specialtysetw(7)gradesetw(17)graduate:specialtyendl; ;void main()graduate gr(zhang_ling,1978,2001,computer);teacher te(wang_qiang, 1976,tutor,electronics);in_service_graduate sg(liu_hua,1975,lectuer,a

32、utomation,2002,computer);gr.display(2002);coutendl;te.display(2002);coutendl;sg.display(2002);5. 代碼測(cè)試1.2.6.測(cè)試過(guò)程和運(yùn)行結(jié)果分析 1、將class car: public vehicle和class truck: public vehicle分別改為:class car: private vehicle和class truck: private vehicle程序運(yùn)行無(wú)法正常運(yùn)行了,因?yàn)槔^承訪問(wèn)控制變成私有之后,基類的共有成員在派生類中就會(huì)變成私有的了,繼承的函數(shù)在主函數(shù)中就不能訪問(wèn)了。

33、2、兩個(gè)基類的virtual都要添上,否則name等還是有二義性。不添virtual時(shí)不需要在初始化列表中添加person(na,y),但是只要添了一個(gè)virtual,就得加person(na,y)。?7.思考題解答將vehicle中數(shù)據(jù)成員wheels和weight改為private性質(zhì),如何修改程序以達(dá)到相同的輸出結(jié)果。答:將vehicle中數(shù)據(jù)成員wheels和weight改為private性質(zhì)后,double truck:efficiency() return payload/(payload+weight);中的weight就無(wú)法被訪問(wèn)了??梢远x一個(gè)指向weight的指針來(lái)訪問(wèn)。最

34、簡(jiǎn)單的方法-把weight換成get_weight()就萬(wàn)事大吉了。實(shí)驗(yàn)報(bào)告九 多態(tài)性與虛函數(shù)1. 實(shí)驗(yàn)?zāi)康模?) 掌握虛函數(shù)定義及實(shí)現(xiàn)。(2) 掌握具有多態(tài)性的面向?qū)ο蟪绦蛟O(shè)計(jì)的基本方法。(3) 掌握純虛函數(shù)與抽象類的定義、實(shí)現(xiàn)及應(yīng)用。2. 實(shí)驗(yàn)設(shè)備 硬件環(huán)境:微型計(jì)算機(jī)軟件環(huán)境: 操作系統(tǒng): windows 語(yǔ)言環(huán)境: visual c+ 3. 實(shí)驗(yàn)內(nèi)容有一個(gè)整數(shù)鏈表,現(xiàn)從此鏈表派生出一個(gè)整數(shù)集合類,在集合類中增加一個(gè)元素個(gè)數(shù)的數(shù)據(jù)項(xiàng)。集合類的插入操作與鏈表相似,只是不插入重復(fù)元素,并且插入后,元素個(gè)數(shù)的數(shù)據(jù)成員需增加。集合類的刪除操作是在鏈表刪除操作的基礎(chǔ)上對(duì)元素個(gè)數(shù)做減1操作。而查找和

35、輸出操作是相同的,因此在集合類中不需要重復(fù)定義。#include #include / enum bool false,true;struct element /定義鏈表中的結(jié)點(diǎn)結(jié)構(gòu) int val; element *next;class list /定義鏈表類 element *elems; public: list() elems=0; list(); virtual bool insert(int); /此虛函數(shù)在派生類中可重新定義 virtual bool deletes(int); /此虛函數(shù)在派生類中可重新定義 bool contain(int); void print();cl

36、ass set:public list /將集合類set定義為鏈表類list的派生類 int card; public: set() card=0; bool insert(int); /重定義此函數(shù) bool deletes(int); /重定義此函數(shù);list:list() /list類得析構(gòu)函數(shù)定義,循環(huán)釋放各元素所占的存儲(chǔ) element *tmp=elems; for(element *elem=elems; elem!=0;) tmp=elem; elem=elem-next; delete tmp; bool list:insert(int val) /定義list類中插入元素的

37、成員函數(shù) element *elem=new element; /為新元素分配存儲(chǔ) if (elem!=0) elem-val=val; /將新元素插入到鏈表頭 elem-next=elems; elems=elem; return true; else return false;bool list:deletes(int val) /定義list類中刪除元素的成員函數(shù) if(elems=0) return false; /若表為空,返回false element *tmp=elems; if(elems-val=val) /若待刪除的元素為表頭元素 elems=elems-next; del

38、ete tmp; return true; else for(element *elem=elems; elem-next!=0; elem=elem-next) if(elem-next-val=val) /循環(huán)查找待刪除元素 tmp=elem-next; elem-next=tmp-next; delete tmp; return true; return false;bool list:contain(int val) /判元素val在鏈表中是否存在 if(elems=0)return false; if(elems-val=val) return true; else for(elem

39、ent *elem=elems; elem-next!=0; elem=elem-next) if(elem-next-val=val) return true; return false;void list:print() /輸出鏈表中各元素 if(elems=0) return; for(element *elem=elems; elem!=0; elem=elem-next) coutval ; coutinsert(30); ptr-insert(40); ptr-insert(543); ptr-insert(40); ptr-print(); ptr=&set1; ptr-inse

40、rt(23); ptr-insert(672); ptr-insert(456); ptr-insert(23); ptr-print(); getch(); return 1;基本要求l 閱讀程序,根據(jù)題意要求在 處填上合適的內(nèi)容完成程序。l 上機(jī)錄入、調(diào)試上面程序。l 運(yùn)行程序,設(shè)計(jì)測(cè)試數(shù)據(jù),觀察運(yùn)行結(jié)果是否正確且滿足題意要求。分析與思考l 在list類中設(shè)計(jì)并實(shí)現(xiàn)一個(gè)從鏈表頭取一個(gè)節(jié)點(diǎn)(取出后該結(jié)點(diǎn)刪除,下一個(gè)結(jié)點(diǎn)作為頭結(jié)點(diǎn))并返回節(jié)點(diǎn)值(整數(shù)值)的成員函數(shù)。l 在集合類中增加求集合并、交、差的成員函數(shù)。4. 源代碼#include #include / enum bool false,true;struct element /定義鏈表中的結(jié)點(diǎn)結(jié)構(gòu) int val; eleme

溫馨提示

  • 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)論