《C++程序设计》实训报告_第1页
《C++程序设计》实训报告_第2页
《C++程序设计》实训报告_第3页
《C++程序设计》实训报告_第4页
《C++程序设计》实训报告_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实训一:类和对象的定义及使用实训目的:(1)掌握类与对象的定义与使用方法,理解面向对象方法中通过对象间传递消息的工作机制。(2)正确掌握类的不同属性成员的使用方法。(3)掌握构造函数与析构函数的概念,理解构造函数与析构函数的执行过程。(4)掌握友元函数和友元类的定义和使用。(5)基本掌握指针和引用作为函数参数的应用。实训内容:定义一个时间类Time,有三个私有成员变量Hour、Minute、Second,定义构造函数、析构函数以及用于改变、获取、输出时间信息的公有函数,主函数中定义时间对象,并通过调用各种成员函数完成时间的设定、改变、获取、输出等功能。 按要求完成类的定义与实现。 修改数据成员的访问方式,观察编译结果。 在Time类中定义一个成员函数,用于实现时间增加一秒的功能,主函数中通过对象调用该函数,并输出增加一秒后的时间信息。 定义一个普通函数。void f(Time t) t. PrintTime( );在Time类中增加拷贝构造函数的定义,主函数中调用该函数,运用调试工具跟踪,分析整个程序调用构造函数(包括拷贝构造函数)和析构函数的次数;再将f函数的形式参数分别修改为引用参数和指针参数(此时函数代码修改为t- PrintTime( );,主函数中调用,再分析此时调用构造函数和析构函数的次数。实训代码:#includeusing 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();void PrintTime();void IncreaseOneSecond();Time:Time(int h,int m,int s) Hour=h; Minute=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() coutHour: Minute: Secondendl;void Time:IncreaseOneSecond() Second+;/*void Time:f(Time t) t.PrintTime(); coutcall fn;*/int main() Time a; Time b(13); Time c(13,15); Time d(13,15,45); a.PrintTime(); b.PrintTime(); c.PrintTime(); d.PrintTime(); a.ChangeTime(12,15,45); b.ChangeTime(12,15,45); c.ChangeTime(12,15,45); d.ChangeTime(12,15,45); couta.GetHour():a.GetMinute():a.GetSecond()endl; coutb.GetHour():b.GetMinute():b.GetSecond()endl; coutc.GetHour():c.GetMinute():c.GetSecond()endl; coutd.GetHour():d.GetMinute():d.GetSecond()endl; return 0;程序运行结果实训小结:构造函数与析构函数的调用方式及执行顺序是:先是构造函数然后是析构函数。调用方式是自动调用,执行顺序是先执行构造函数,待程序结束时再执行析构函数。 实训二:个人银行账户管理程序的类设计实训目的:掌握面向对象中类、继承、多态性的开发思想;掌握流的概念; 独立设计个人银行账户管理程序。实训内容:1、 个人银行账户管理程序的类关系图2、 个人银行账户管理程序的个人账户设置和应用3、 个人银行账户管理程序实训代码:1、 个人银行账户管理程序的类设计class account private: 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 &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: SavingsAccount(const Date &date,const std:string &id,double rate); double 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 credit; double rate; double fee; double getdebt() const double balance=getBalance(); return (balance0 ? balance : 0); public: creditaccount(const Date &date,const std:string &id,double credit,double rate,double fee); double getcredit() const return credit; double getrate() const return rate; double getfee() const return fee; double getavailablecredit() const if (getBalance()value=value; void reset(const Date &date,double value) lastdate=date;this-value=value;sum=0; ;class Date private: int year; int month; int day; int totaldays; public: Date(int year,int month,int day); int getyear() const return year; double getmonth() const return month; int getday() const return day; int getmaxday() const; bool isleapyear() const return 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 wutingming(date,1,0.015); creditaccount wutingmin(date,1,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,12,5);wutingming.show();coutendl;wutingmin.show();coutendl;4、 个人银行账户管理程序的重点代码#include account.h#includeusing namespace std;int main()Date date(2008,11,1); SavingsAccount sa1(date,0.015);SavingsAccount sa2(date,0.015);SavingsAccount wutingming(date,1,0.015);creditaccount ca(date,C,10000,0.0005,50);creditaccount wutingmin(date,1,2000,0.0005,50);sa1.deposit(Date(2008,11,5),5000,salary);sa2.deposit(Date(2008,11,25),10000,sell stock 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(2008,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); coutendl;sa1.show();coutendl;sa2.show();coutendl;wutingming.show();coutendl;wutingmin.show();coutendl;ca.show();coutendl;couttotal: account:gettotal()endl;return 0; #include account.h#includeusing namespace std;int main()Date date(2008,11,1); SavingsAccount sa1(date,s,0.015);SavingsAccount sa2(date,0.015);SavingsAccount wutingming(date,1,0.015);creditaccount ca(date,C,10000,0.0005,50);account*accounts=&sa1,&sa2,&wutingming,&ca;const int n=sizeof(accounts)/sizeof(account *);cout(d)deposit (w)withdraw (s)show (c)chang day (n)next month (e)exitendl;char cmd;do date.show();coutttotal: account:gettotal()endl;cout;int index,day,i;double amount;string desc;cincmd;switch (cmd)case d:cinindexamount;getline(cin,desc);accountsindex-deposit(date,amount,desc); break;case w:cinindexamount;getline(cin,desc);accountsindex-withdraw(date,amount,desc); break;case s:for (i=0;in;i+)coutishow();coutday;if (daydate.getday()coutdate.getmaxday()coutinvalid day;else date=Date(date.getyear(),date.getmonth(),day); break;case n:if(date.getmonth()=12)date=Date(date.getyear()+1,1,1);else date=Date(date.getyear(),date.getmonth()+1,1);for(i=0;isettle(date); break; while (cmd!=e); return 0;程序运行结果实训小结:通过本实训,应用虚函数和抽象类对程序进行改进:1) 将show函数声明为虚函数,因此通过指向creditaccount类实例的account类型的指针来调用show函数时,被实际调用的将是creditaccount类定义的show函数。这样,如果创建一个account指针类型的数组,使各个元素分别指向各个账户对象,就可以通过一个循环来调用它们的show函数。2)在account类中添加deposit,withdraw,settle这3个函数的声明,且将它们都声明为纯虚函数,这使得通过基类指针可以调用派生类的相应函数,而且无须给出它们在基类中的实现。经过这一改动之后,account类就变成了抽象类。实训三、图书信息管理系统主界面实训目的:能够较好的完成程序的主体设计,界面友好,功能齐全;程序思路清晰易懂,能够充分利用所学工具实现各项操作。独立力完成实训报告,内容充实、观点明确、新颖。实训内容:图书信息管理系统,使之能提供以下功能:1、 系统以菜单方式工作。2、 借书3、 还书4、 图书维护5、 读者维护6、 退出:包括返回主界面和退出系统等功能。实训代码:#include#include#include#includestruct books_list char author20; /*作者名*/ char bookname20; /*书名*/ char publisher20; /*出版单位*/ char pbtime15; /*出版时间*/ char loginnum10; /*登陆号*/ float price; /*价格*/ char classfy10; /*分类号*/ 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 info_change(struct books_list * head);/*修改*/void save(struct books_list * head);/*保存数据至文件*/*新建链表头节点*/struct books_list * Create_Books_Doc() struct books_list * head; head=(struct books_list *)malloc(sizeof(struct books_list); /*分配头节点空间*/ head-next=NULL; /*头节点指针域初始化,定为空*/ return head; /*保存数据至文件*/void save(struct books_list * head) struct books_list *p; FILE *fp; p=head; fp=fopen(data.txt,w+); /*以写方式新建并打开 data.txt文件*/ fprintf(fp,n); /*向文件输出表格*/ fprintf(fp,登录号 书 名 作 者 出版单位 出版时间 分类号 价格 n); fprintf(fp,n); /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/ while(p-next!= NULL) p=p-next; fprintf(fp,%-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,n); fclose(fp); printf( 已将图书数据保存到 data.txt 文件n);/*插入*/void InsertDoc(struct books_list *head) /*定义结构体指针变量 s指向开辟的新结点首地址 p为中间变量*/ struct books_list *s, *p; char flag=Y; /*定义flag,方便用户选择重复输入*/ p=head; /*遍历到尾结点,p指向尾结点*/ while(p-next!= NULL) p=p-next; /*开辟新空间,存入数据,添加进链表*/ while(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); printf(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; /*将新增加的节点添加进链表*/ p=s; /*p指向尾节点,向后移*/ s-next=NULL; printf( 添加成功!); printf(n 继续添加?(Y/N):); fflush(stdin); scanf(%c,&flag); printf(n); if(flag=N|flag=n) if(flag=Y|flag=y); save(head); /*保存数据至文件*/ return;/*查询操作*/void search_book(struct books_list *head) struct books_list * p; char temp20; p=head; if(head=NULL | head-next=NULL) /*判断数据库是否为空*/ printf( 图书库为空!n); else printf(请输入您要查找的书名: ); fflush(stdin); scanf(%s,temp); /*指针从头节点开始移动,遍历至尾结点,查找书目信息*/ while(p-next!= NULL) p=p-next; if(strcmp(p-bookname,temp)=0) printf(n图书已找到!n); printf(n); printf(登录号: %stn,p-loginnum); printf(书名: %stn,p-bookname); printf(作者名: %stn,p-author); printf(出版单位: %stn,p-publisher); printf(出版时间: %stn,p-pbtime); printf(分类号: %stn,p-classfy); printf(价格: %.2ftn,p-price); if(p-next=NULL) printf(n查询完毕!n); return; /*浏览操作*/ void Print_Book_Doc(struct books_list * head) struct books_list * p; if(head=NULL | head-next=NULL) /*判断数据库是否为空*/ printf(n 没有图书记录! nn); return; p=head; printf(n); printf(登录号 书 名 作 者 出版单位 出版时间 分类号 价格 n); printf(n); /*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/ while(p-next!= NULL) p=p-next; printf(%-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); /*循环输出表格*/ printf(n); printf(n); /*修改操作*/void info_change(struct books_list * head) 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(n 请输入图书登陆卡号:); fflush(stdin); scanf(%s,p-loginnum); printf(n 请输入图书书名:); fflush(stdin); 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(n 没有图书记录! nn); return;/*删除操作*/void Delete

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论