C++程序設計實訓報告材料_第1頁
C++程序設計實訓報告材料_第2頁
C++程序設計實訓報告材料_第3頁
C++程序設計實訓報告材料_第4頁
C++程序設計實訓報告材料_第5頁
已閱讀5頁,還剩32頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實用標準文案實訓一:類和對象的定義及使用實訓目的:1)掌握類與對象的定義與使用方法,理解面向對象方法中通過對象間傳遞消息的工作機制。正確掌握類的不同屬性成員的使用方法。掌握構造函數(shù)與析構函數(shù)的概念,理解構造函數(shù)與析構函數(shù)的執(zhí)行過程。掌握友元函數(shù)和友元類的定義和使用?;菊莆罩羔樅鸵米鳛楹瘮?shù)參數(shù)的應用。實訓內容:定義一個時間類 Time ,有三個私有成員變量 Hour 、Minute 、Second ,定 義構造函數(shù)、析構函數(shù)以及用于改變、獲取、輸出時間信息的公有函數(shù),主函數(shù) 中定義時間對象,并通過調用各種成員函數(shù)完成時間的設定、改變、獲取、輸出 等功能。 按要求完成類的定義與實現(xiàn)。 修改數(shù)據(jù)

2、成員的訪問方式,觀察編譯結果。 在 Time 類中定義一個成員函數(shù), 用于實現(xiàn)時間增加一秒的功能, 主函數(shù) 中通過對象調用該函數(shù),并輸出增加一秒后的時間信息。 定義一個普通函數(shù)。void f(Time t)文檔大全t. PrintTime( );在 Time 類中增加拷貝構造函數(shù)的定義,主函數(shù)中調用該函數(shù),運用調試工具跟蹤,分析整個程序調用構造函數(shù)(包括拷貝構造函數(shù))和析構函數(shù)的次數(shù);再將 f 函數(shù)的形式參數(shù)分別修改為引用參數(shù)和指針參數(shù)(此時函數(shù)代碼修改為t-> PrintTime( );, 主函數(shù)中調用, 再分析此時調用構造函數(shù)和析構函數(shù)的次數(shù)。實訓代碼:#include<ios

3、tream>using namespace std;class Timeprivate:int Hour,Minute,Second;public:Time(int h=0,int m=0,int s=0);Time(const Time &ob);Time();void ChangeTime(int h,int m,int s);int GetHour();int GetMinute();int GetSecond();35void PrintTime();void IncreaseOneSecond();Time:Time(int h,int m,int s)Hour=h;M

4、inute=m;Second=s;Time:Time(const Time &ob)Hour=ob.Hour;Minute=ob.Minute;Second=ob.Second;Time:Time()void Time:ChangeTime(int h,int m,int s)Hour=h;Minute=m;Second=s;int Time:GetHour()return Hour;int Time:GetMinute()return Minute;int Time:GetSecond()return Second;void Time:PrintTime()cout<<H

5、our<<": "<<Minute<<": "<<Second<<endl;void Time:IncreaseOneSecond()Second+;/*void Time:f(Time t)t.PrintTime();cout<<"call fn"*/int main()Time a;Time b(13);Time c(13,15);Time d(13,15,45);a. PrintTime();b. PrintTime();c. PrintTime();d.

6、PrintTime();a. ChangeTime(12,15,45);b. ChangeTime(12,15,45);c. ChangeTime(12,15,45);d. ChangeTime(12,15,45);cout<<a.GetHour()<<":"<<a.GetMinute()<<":"<<a.GetSecond()<<endl;cout<<b.GetHour()<<":"<<b.GetMinute()<<

7、;":"<<b.GetSecond()<<endl;cout<<c.GetHour()<<":"<<c.GetMi nute()<<":"<<c.GetSeco nd()<<e ndl;cout<<d.GetHour()<<":"<<d.GetMi nu te()<<":"<<d.GetSeco nd()<<e ndl;retur

8、n 0;程序運行結果4 + + 6.0R14070519neh 1pxpkJ :0:曲13: O: &13: 15: 013= 15: 4512:lS:4b12=15=451 2 :1 5 : 4512=15:45Press dny key to continue實訓小結:構造函數(shù)與析構函數(shù)的調用方式及執(zhí)行順序是:先是構造函數(shù)然后是析構函數(shù)。調用方式是自動調用,執(zhí)行順序是先執(zhí)行構造函數(shù),待程序結束時再執(zhí)行析構函數(shù)。實訓二:個人銀行賬戶管理程序的類設計獨立設計個實訓目的:掌握面向對象中類、繼承、多態(tài)性的開發(fā)思想;掌握流的概念; 人銀行賬戶管理程序。實訓內容:1、個人銀行賬戶管理程序的類關

9、系圖2、個人銀行賬戶管理程序的個人賬戶設置和應用3、個人銀行賬戶管理程序實訓代碼:1、個人銀行賬戶管理程序的類設計class accountprivate:std:string id;double balance;static double total;protected:account(const Date &date,const std:string &id);void record(const Date &date,double amount,const std:string &desc);void error(const std:string &

10、msg) const;public:const std:string&getId() const return id; double getBalance() const return balance;static double gettotal() return total;void show() const;class SavingsAccount:public account private:accumulator acc;double rate;public:&id,double rate);SavingsAccount(const Date &date,con

11、st std:stringdouble getrate() const return rate;void deposit(const Date &date,double amount,const std:string &desc);void withdraw(const Date &date,double amount,const std:string &desc);void settle(const Date &date);class creditaccount:public account private:accumulator acc;double

12、 credit;double rate;double fee;double getdebt() const double balance=getBalance();return (balance<0 ? balance : 0); public:creditaccount(constDate &date,conststd:string&id,doublecredit,double rate,double fee);double getcredit() const return credit;double getrate() const return rate;double

13、 getfee() const return fee;double getavailablecredit() constif (getBalance()<0)return credit+getBalance();Else return credit; void deposit(const Date &date,double amount,const std:string &desc);void withdraw(const Date &date,double amount,const std:string &desc);void settle(const

14、Date &date);void show() const;class accumulatorprivate:Date lastdate; double value; double sum;public:accumulator(const Date &date,double value):lastdate(date),value(value),sum(0)double getsum(const Date &date)const return sum+value*date.distance(lastdate);void change(const Date &dat

15、e,double value)sum=getsum(date);lastdate=date;this->value=value;void reset(const Date &date,double value)lastdate=date;this->value=value;sum=0; ;class Dateprivate:int year; int month;int day; int totaldays;public:Date(int year,int month,int day);int getyear() constreturn year;double getmon

16、th() constreturn month;int getday() constreturn day;int getmaxday() const;bool isleapyear() constreturn year%4=0 && year%100!=0 | year%400=0; void show() const;int distance (const Date& date) const return totaldays-date.totaldays; ;2、個人銀行賬戶管理程序的類關系圖3、個人銀行賬戶管理程序的個人賬戶設置和應用SavingsAccount wu

17、tingming(date,"090102060101",0.015);creditaccount wutingmin(date,"090102060101",2000,0.0005,50);wutingming.deposit(Date(2008,11,5),1000,"buy book");wutingmin.withdraw(Date(2008,11,5),2000,"buy MP3");wutingming.settle(Date(2008,12,5);wutingmin.settle(Date(2008,

18、12,5);wutingming.show();cout<<endl;wutingmin.show();cout<<endl;4、個人銀行賬戶管理程序的重點代碼#include "account.h" #include<iostream> using namespace std;int main()Date date(2008,11,1);SavingsAccount sa1(date,"03755217",0.015);SavingsAccount sa2(date,"02342342",0.01

19、5);SavingsAccount wutingming(date,"090102060101",0.015);creditaccount ca(date,"C5392394",10000,0.0005,50);creditaccount wutingmin(date,"090102060101",2000,0.0005,50);sa1.deposit(Date(2008,11,5),5000,"salary");sa2.deposit(Date(2008,11,25),10000,"sell stock

20、 0323");wutingming.deposit(Date(2008,11,5),1000,"buy book");wutingmin.withdraw(Date(2008,11,5),2000,"buy MP3");ca.withdraw(Date(2008,11,15),2000,"buy a cell");ca.settle(Date(2008,12,1);ca.deposit(Date(2008,12,1),2016,"repay the credit");sa1.deposit(Date(2

21、008,12,5),5500,"salary");sa1.settle(Date(2009,1,1);sa2.settle(Date(2009,1,1);wutingming.settle(Date(2008,12,5);wutingmin.settle(Date(2008,12,5);ca.settle(Date(2008,12,1);cout<<endl;sa1.show();cout<<endl;sa2.show();cout<<endl;wutingming.show();cout<<endl;wutingmin.sh

22、ow();cout<<endl;ca.show();cout<<endl;cout<<"total: "<<account:gettotal()<<endl;return 0; #include "account.h" #include<iostream> using namespace std;int main()Date date(2008,11,1);SavingsAccount sa1(date,"s3755217",0.015);SavingsAccou

23、nt sa2(date,"02342342",0.015);SavingsAccount wutingming(date,"090102060101",0.015);creditaccount ca(date,"C5392394",10000,0.0005,50);account*accounts=&sa1,&sa2,&wutingming,&ca;const int n=sizeof(accounts)/sizeof(account *);cout<<"(d)deposit(w)

24、withdraw (s)show (c)chang day (n)next month (e)exit"<<endl;char cmd;do date.show();cout<<"ttotal: "<<account:gettotal()<<endl;cout<<"command>"int index,day,i;double amount;string desc;cin>>cmd;switch (cmd)case 'd':cin>>ind

25、ex>>amount;getline(cin,desc);accountsindex->deposit(date,amount,desc); break;case 'w':cin>>index>>amount;getline(cin,desc);accountsindex->withdraw(date,amount,desc); break;case 's':for (i=0;i<n;i+)cout<<""<<i<<""account

26、si->show();cout<<endl; break;case 'c':cin>>day;if (day<date.getday()cout<<"you cannot specify a previous day"else if (day>date.getmaxday()cout<<"invalid day"else date=Date(date.getyear(),date.getmonth(),day); break;case 'n':if(date

27、.getmonth()=12)date=Date(date.getyear()+1,1,1);else date=Date(date.getyear(),date.getmonth()+1,1);for(i=0;i<n;i+)accountsi->settle(date); break; while (cmd!='e');return 0;程序運行結果SludiaWyPE vjcbtsM Dcl>ue1. exe"Qi PrDGCu FilcsXlirUcusurt Visu*!2008-11 2008-11 2008-11 2H08-11 20H8

28、-11 2008-11 2008-11 2008-11 2008 11 2609-11 2009-12 2009=123AR9-1-12007- 1-12008- 12-5 2008-12-5111115255G1石11石#03755217 created 02342342 created ttB?BlB2e6eiei created tteb3923y4 created flHVHlU2U&U1U1 W03755Z17 #02342342#0701azeceiei ttaveiQzeceiei flC539239 1IC539239-JW»37F;5217 WH375521

29、7 W02342342 no9eio2e&eiei tta9eia2eGeieicreated sees IBGGB leee16 2eie 弱伽 17.7715.16-305Q0Q 10000 loee-2000 -201 s0lRfi17,S10015-2 1000.4 -2030salary, sell stock 0323 buy lioolt buy nra buy a c«Il int«r«ctt>i9 ci*»dit 審 flippy int鼻p鼻st interest intepesc interestBalance: Ba

30、lance: Balance: Balance: Balance :10517.810015.2-2H3Hp3?S521702342342total: 19503.bFless any hcg tn continueauaiiahle credit: -30 auallaJble credit: IHUHU實訓小結:通過本實訓,應用虛函數(shù)和抽象類對程序進行改進:1)將show函數(shù)聲明為虛函數(shù),因此通過指向creditacco unt類實例的account類型的指針來調用 show 函數(shù)時,被實際調用的將是 creditaccountshow函數(shù)。類定義的show函數(shù)。這樣,如果創(chuàng)建一個 acc

31、ount指針類型的數(shù)組,使各個元素分別指向各個賬戶對象,就可以通過一個循環(huán)來調用它們的2)在account類中添加deposit ,withdraw ,settle這3個函數(shù)的聲明,且將它們都聲明為純虛函數(shù),這使得通過基類指針可以調用派生類的相應函數(shù),而且無須給出它們在基類中的實現(xiàn)。經過這一改動之后,account類就變成了抽象類。實訓三、 圖書信息管理系統(tǒng)主界面實訓目的:能夠較好的完成程序的主體設計,界面友好,功能齊全;程序思路清晰易懂,能夠充分利用所學工具實現(xiàn)各項操作。獨立力完成實訓報告,內容充實、觀點明確、新穎。實訓內容:圖書信息管理系統(tǒng),使之能提供以下功能:1、系統(tǒng)以菜單方式工作。2、

32、借書3、還書4、圖書維護5、讀者維護6、退出:包括返回主界面和退出系統(tǒng)等功能。實訓代碼:#include<stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>struct books_list/* 作者名 */* 書名 */* 出版單位 */* 出版時間 */ /* 登陸號 */char author20; char bookname20;char publisher20;char pbtime15;char loginnum10;float price;char classfy

33、10;struct books_list * next; ;/* 價格 */* 分類號 */* 鏈表的指針域 */struct books_list * Create_Books_Doc(); void InsertDoc(struct books_list * head); /* void DeleteDoc(struct books_list * head , int num);/* void Print_Book_Doc(struct books_list * head);/* void search_book(struct books_list * head); /* void inf

34、o_change(struct books_list * head);/* void save(struct books_list * head);/*/* 新建鏈表 */ 插入 */刪除 */ 瀏覽*/ 查詢*/ 修改*/ 保存數(shù)據(jù)至文件 */* 新建鏈表頭節(jié)點 */struct books_list * Create_Books_Doc()struct books_list * head;head=(struct books_list *)malloc(sizeof(struct books_list); /* head->next=NULL; /* 頭節(jié)點指針域初始化,定為空 */

35、 return head;分配頭節(jié)點空間 */* 保存數(shù)據(jù)至文件 */void save(struct books_list * head)struct books_list *p;FILE *fp;p=head;fp=fopen("data.txt","w+"); /* fprintf(fp,"*/fprintf(fp,"格|n");fprintf(fp,"/* 指針從頭節(jié)點開始移動,遍歷至尾結點,依次輸出圖書信息 while(p->next!= NULL)p=p->next;fprintf(fp,&

36、quot;| %-6.6s | %-10.10s |%-10.10s | %-10.10s | %-12.12s | %-6.6s |%.2f|n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p-> price);fprintf(fp,"fclose(fp);printf("以寫方式新建并打開 data.txt 文件 */n11rr); " I向文件輸出表格I登錄號丨書 名丨作者| 出版單位 | 出版時間 |分類號| 價

37、n")r-4*/An");已將圖書數(shù)據(jù)保存到 data.txt 文件 n");/* 插入 */void InsertDoc(struct books_list *head)/* 定義結構體指針變量 s 指向開辟的新結點首地址 p 為中間變量 */ struct books_list *s, *p;char flag='Y' /*p=head;/* 遍歷到尾結點,定義 flag, 方便用戶選擇重復輸入 */p 指向尾結點 */while(p->next!= NULL)p=p->next;/* 開辟新空間 ,存入數(shù)據(jù) ,添加進鏈表 */ w

38、hile(flag='Y'|flag='y') s=(struct books_list *)malloc(sizeof(struct books_list); printf("n fflush(stdin);scanf("%s",s->loginnum); printf("n fflush(stdin);scanf("%s",s->bookname); printf("n fflush(stdin);scanf("%s",s->author); prin

39、tf("n fflush(stdin);scanf("%s",s->publisher); printf("n fflush(stdin);scanf("%s",s->pbtime); printf("n fflush(stdin);scanf("%s",s->classfy); printf("n fflush(stdin);scanf("%f",&s->price);printf("n");p->next=s; /

40、* 將新增加的節(jié)點添加進鏈表 p=s; /*p 指向尾節(jié)點,向后移 */ s->next=NULL; printf(" printf("n fflush(stdin);scanf("%c",&flag);請輸入圖書登陸號 :");請輸入圖書書名 :");請輸入圖書作者名 :");請輸入圖書出版社 :");請輸入圖書出版時間 :");請輸入圖書分類號 :");請輸入圖書價格 :");*/添加成功!繼續(xù)添加? (Y/N):");printf("n"

41、;);if(flag='N'|flag='n')if(flag='Y'|flag='y');save(head); /* 保存數(shù)據(jù)至文件 */ return;/* 查詢操作 */void search_book(struct books_list *head)struct books_list * p; char temp20; p=head;if(head=NULL | head->next=NULL) /* printf(” else printf(" 請輸入您要查找的書名 : "); fflush(

42、stdin);scanf("%s",temp);/* 指針從頭節(jié)點開始移動,遍歷至尾結點,查找書目信息 while(p->next!= NULL) p=p->next;if(strcmp(p->bookname,temp)=0) printf("n 圖書已找到 !n"); printf("n");printf("printf("printf("printf("printf("printf("printf("判斷數(shù)據(jù)庫是否為空 */圖書庫為空!n&q

43、uot;);*/登錄號 : %stn",p->loginnum); 書名 : %stn",p->bookname); 作者名 : %stn",p->author); 出版單位 : %stn",p->publisher); 出版時間 : %stn",p->pbtime); 分類號 : %stn",p->classfy); 價格: %.2ftn",p->price);if(p->next=NULL) printf("n 查詢完畢 !n"); return;/*

44、瀏覽操作 */void Print_Book_Doc(struct books_list * head)struct books_list * p; if(head=NULL | head->next=NULL)prin tf("nreturn;p=head;printf("printf("In");printf("/* 指針從頭節(jié)點開始移動,遍歷至尾結點,依次輸出圖書信息 while(p->next!= NULL)p=p->next;printf(”|%-6.6s |%-10.10s |%-10.10s |%-10.10s

45、|%-12.12s |%-6.6s |%.2f|n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p-> price); /* 循環(huán)輸出表格 */printf(”Lprintf("n");/* 判斷數(shù)據(jù)庫是否為空 */沒有圖書記錄 !WT);登錄號| 書 名 | 作-n1f-n1者| 出版單位 | 出版時間 |分類號| 價格*/* 修改操作 */ void info_change(struct books_list * head)

46、struct books_list * p;int panduan=0; /* 此變量用于判斷是否找到書目 */ char temp20;p=head;printf(" 請輸入要修改的書名 :"); scanf("%s",temp);while(p->next!= NULL) p=p->next; if(strcmp(p->bookname,temp)=0)printf("nfflush(stdin); scanf("%s",p->loginnum); printf("n fflush(st

47、din); scanf("%s",p->bookname); printf("n fflush(stdin); scanf("%s",p->author); printf("n fflush(stdin); scanf("%s",p->publisher); printf("n fflush(stdin); scanf("%s",p->pbtime); printf("n fflush(stdin); scanf("%s",p->classfy); printf("n fflush(stdin); scanf("%f",&p->price); printf("n");panduan=1;if(panduan=0)printf("nreturn;請輸入圖書登陸卡號 :");請輸入圖書書名 :");請輸入圖書作者名 :");請輸入圖書出版社 :");請輸入圖書出版時間 :");請輸入圖書分類號 :");請輸入圖書價格 :");沒有圖書記錄 !WT);/* 刪除操作 *

溫馨提示

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

最新文檔

評論

0/150

提交評論