1、C+程序设计实训报告材料实训一:类和对象的定义及使用实训目的:(1)掌握类与对象的定义与使用方法,理解面向对象方法中通过对象间传递消息的工作机制。(2)正确掌握类的不同属性成员的使用方法。(3)掌握构造函数与析构函数的概念,理解构造函数与析构函数的执行过程。(4)掌握友元函数和友元类的定义和使用。(5)基本掌握指针和引用作为函数参数的应用。实训内容:定义一个时间类Time,有三个私有成员变量Hour、Minute、Second,定义构造函数、析构函数以及用于改变、获取、输出时间信息的公有函数,主函数中定义时间对象,并通过调用各种成员函数完成时间的设定、改变、获取、输出等功能。 按要求完成类的定
2、义与实现。 修改数据成员的访问方式,观察编译结果。 在Time类中定义一个成员函数,用于实现时间增加一秒的功能,主函数中通过对象调用该函数,并输出增加一秒后的时间信息。 定义一个普通函数。void f(Time t) t. PrintTime( );在Time类中增加拷贝构造函数的定义,主函数中调用该函数,运用调试工具跟踪,分析整个程序调用构造函数(包括拷贝构造函数)和析构函数的次数;再将f函数的形式参数分别修改为引用参数和指针参数(此时函数代码修改为t- PrintTime( );,主函数中调用,再分析此时调用构造函数和析构函数的次数。实训代码:#includeusing namespace
3、 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;
4、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: Second
5、endl;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.ChangeTi
6、me(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;程序运行结果实训小结:构造函数与析构函数的调用方式及执行顺序是:先是构造函数然后是析构函数。调用方式是自动调用,执行顺序是先执行构造函数,待程序结束时再执行析构函
7、数。 实训二:个人银行账户管理程序的类设计实训目的:掌握面向对象中类、继承、多态性的开发思想;掌握流的概念; 独立设计个人银行账户管理程序。实训内容: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(co
8、nst 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: accumul
9、ator 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 &dat
10、e); ; 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 ge
11、tcredit() 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 t
12、otaldays; 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) cons
13、t return totaldays-date.totaldays; ;2、 个人银行账户管理程序的类关系图3、 个人银行账户管理程序的个人账户设置和应用SavingsAccount wutingming(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.
14、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,03755217,0.015); SavingsAccount sa2(date,02342342,0.015); SavingsAc
15、count 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 0323); wutingming.deposit(Date(2008,11,5),1000,buy book); wut
16、ingmin.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
17、); 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 da
18、te(2008,11,1); SavingsAccount sa1(date,s3755217,0.015); SavingsAccount 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)dep
19、osit (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: ci
20、nindexamount; 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(
21、)+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指针类型的数组,使各个元素分别指向各个账户对象,就可以通过一个循环来调用它们
22、的show函数。2)在account类中添加deposit,withdraw,settle这3个函数的声明,且将它们都声明为纯虚函数,这使得通过基类指针可以调用派生类的相应函数,而且无须给出它们在基类中的实现。经过这一改动之后,account类就变成了抽象类。实训三、图书信息管理系统主界面实训目的:能够较好的完成程序的主体设计,界面友好,功能齐全;程序思路清晰易懂,能够充分利用所学工具实现各项操作。独立力完成实训报告,内容充实、观点明确、新颖。实训内容:图书信息管理系统,使之能提供以下功能:1、 系统以菜单方式工作。2、 借书3、 还书4、 图书维护5、 读者维护6、 退出:包括返回主界面和退
23、出系统等功能。实训代码:#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_
24、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 *
25、 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; f
26、p=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,