《C++程序设计》实训报告Word下载.docx
《《C++程序设计》实训报告Word下载.docx》由会员分享,可在线阅读,更多相关《《C++程序设计》实训报告Word下载.docx(24页珍藏版)》请在冰点文库上搜索。
};
Time:
:
Time(inth,intm,ints)
Hour=h;
Minute=m;
Second=s;
Time(constTime&
ob)
Hour=ob.Hour;
Minute=ob.Minute;
Second=ob.Second;
~Time()
voidTime:
ChangeTime(inth,intm,ints)
intTime:
GetHour()
returnHour;
GetMinute()
returnMinute;
GetSecond()
returnSecond;
PrintTime()
cout<
<
Hour<
"
"
Minute<
Second<
endl;
IncreaseOneSecond()
Second++;
/*voidTime:
f(Timet)
t.PrintTime();
callf\n"
;
}*/
intmain()
Timea;
Timeb(13);
Timec(13,15);
Timed(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);
a.GetHour()<
a.GetMinute()<
a.GetSecond()<
b.GetHour()<
b.GetMinute()<
b.GetSecond()<
c.GetHour()<
c.GetMinute()<
c.GetSecond()<
d.GetHour()<
d.GetMinute()<
d.GetSecond()<
return0;
程序运行结果
实训小结:
构造函数与析构函数的调用方式及执行顺序是:
先是构造函数然后是析构函数。
调用方式是自动调用,执行顺序是先执行构造函数,待程序结束时再执行析构函数。
实训二:
个人银行账户管理程序的类设计
掌握面向对象中类、继承、多态性的开发思想;
掌握流的概念;
独立设计个人银行账户管理程序。
1、个人银行账户管理程序的类关系图
2、个人银行账户管理程序的个人账户设置和应用
3、个人银行账户管理程序
1、个人银行账户管理程序的类设计
classaccount{
private:
std:
stringid;
doublebalance;
staticdoubletotal;
protected:
account(constDate&
date,conststd:
string&
id);
voidrecord(constDate&
date,doubleamount,conststd:
desc);
voiderror(conststd:
msg)const;
public:
conststd:
getId()const{returnid;
doublegetBalance()const{returnbalance;
staticdoublegettotal(){returntotal;
}
voidshow()const;
};
classSavingsAccount:
publicaccount{
accumulatoracc;
doublerate;
SavingsAccount(constDate&
id,doublerate);
doublegetrate()const{returnrate;
voiddeposit(constDate&
voidwithdraw(constDate&
voidsettle(constDate&
date);
classcreditaccount:
doublecredit;
doublefee;
doublegetdebt()const{
doublebalance=getBalance();
return(balance<
0?
balance:
0);
}
creditaccount(constDate&
id,doublecredit,doublerate,doublefee);
doublegetcredit()const{returncredit;
doublegetfee()const{returnfee;
doublegetavailablecredit()const{
if(getBalance()<
0)
returncredit+getBalance();
Elsereturncredit;
}
classaccumulator{
Datelastdate;
doublevalue;
doublesum;
accumulator(constDate&
date,doublevalue)
:
lastdate(date),value(value),sum(0){}
doublegetsum(constDate&
date)const{
returnsum+value*date.distance(lastdate);
voidchange(constDate&
date,doublevalue){
sum=getsum(date);
lastdate=date;
this->
value=value;
voidreset(constDate&
sum=0;
}};
classDate{
intyear;
intmonth;
intday;
inttotaldays;
Date(intyear,intmonth,intday);
intgetyear()const
{returnyear;
doublegetmonth()const
{returnmonth;
intgetday()const
{returnday;
intgetmaxday()const;
boolisleapyear()const{
returnyear%4==0&
&
year%100!
=0||year%400==0;
intdistance(constDate&
date)const{
returntotaldays-date.totaldays;
2、个人银行账户管理程序的类关系图
3、个人银行账户管理程序的个人账户设置和应用
SavingsAccountwutingming(date,"
090102060101"
0.015);
creditaccountwutingmin(date,"
2000,0.0005,50);
wutingming.deposit(Date(2008,11,5),1000,"
buybook"
);
wutingmin.withdraw(Date(2008,11,5),2000,"
buyMP3"
wutingming.settle(Date(2008,12,5));
wutingmin.settle(Date(2008,12,5));
wutingming.show();
cout<
wutingmin.show();
4、个人银行账户管理程序的重点代码
#include"
account.h"
intmain(){
Datedate(2008,11,1);
SavingsAccountsa1(date,"
03755217"
SavingsAccountsa2(date,"
02342342"
SavingsAccountwutingming(date,"
creditaccountca(date,"
C5392394"
10000,0.0005,50);
sa1.deposit(Date(2008,11,5),5000,"
salary"
sa2.deposit(Date(2008,11,25),10000,"
sellstock0323"
ca.withdraw(Date(2008,11,15),2000,"
buyacell"
ca.settle(Date(2008,12,1));
ca.deposit(Date(2008,12,1),2016,"
repaythecredit"
sa1.deposit(Date(2008,12,5),5500,"
sa1.settle(Date(2009,1,1));
sa2.settle(Date(2009,1,1));
sa1.show();
sa2.show();
ca.show();
total:
account:
gettotal()<
s3755217"
account*accounts[]={&
sa1,&
sa2,&
wutingming,&
ca};
constintn=sizeof(accounts)/sizeof(account*);
(d)deposit(w)withdraw(s)show(c)changday(n)nextmonth(e)exit"
charcmd;
do{date.show();
cout<
\ttotal:
command>
intindex,day,i;
doubleamount;
stringdesc;
cin>
>
cmd;
switch(cmd){
case'
d'
cin>
index>
amount;
getline(cin,desc);
accounts[index]->
deposit(date,amount,desc);
break;
w'
withdraw(date,amount,desc);
s'
for(i=0;
i<
n;
i++){
cout<
["
]"
accounts[i]->
show();
}break;
c'
day;
if(day<
date.getday())
youcannotspecifyapreviousday"
elseif(day>
date.getmaxday())
invalidday"
elsedate=Date(date.getyear(),date.getmonth(),day);
n'
if(date.getmonth()==12)
date=Date(date.getyear()+1,1,1);
elsedate=Date(date.getyear(),date.getmonth()+1,1);
for(i=0;
i++)
settle(date);
}while(cmd!
='
e'
通过本实训,应用虚函数和抽象类对程序进行改进:
1)将show函数声明为虚函数,因此通过指向creditaccount类实例的account类型的指针来调用show函数时,被实际调用的将是creditaccount类定义的show函数。
这样,如果创建一个account指针类型的数组,使各个元素分别指向各个账户对象,就可以通过一个循环来调用它们的show函数。
2)在account类中添加deposit,withdraw,settle这3个函数的声明,且将它们都声明为纯虚函数,这使得通过基类指针可以调用派生类的相应函数,而且无须给出它们在基类中的实现。
经过这一改动之后,account类就变成了抽象类。
实训三、图书信息管理系统主界面
能够较好的完成程序的主体设计,界面友好,功能齐全;
程序思路清晰易懂,能够充分利用所学工具实现各项操作。
独立力完成实训报告,内容充实、观点明确、新颖。
图书信息管理系统,使之能提供以下功能:
1、
系统以菜单方式工作。
2、
借书
3、
还书
4、
图书维护
5、
读者维护
6、
退出:
包括返回主界面和退出系统等功能。
stdio.h>
math.h>
string.h>
stdlib.h>
structbooks_list
charauthor[20];
/*作者名*/
charbookname[20];
/*书名*/
charpublisher[20];
/*出版单位*/
charpbtime[15];
/*出版时间*/
charloginnum[10];
/*登陆号*/
floatprice;
/*价格*/
charclassfy[10];
/*分类号*/
structbooks_list*next;
/*链表的指针域*/
structbooks_list*Create_Books_Doc();
/*新建链表*/
voidInsertDoc(structbooks_list*head);
/*插入*/
voidDeleteDoc(structbooks_list*head,intnum);
/*删除*/
voidPrint_Book_Doc(structbooks_list*head);
/*浏览*/
voidsearch_book(structbooks_list*head);
/*查询*/
voidinfo_change(structbooks_list*head);
/*修改*/
voidsave(structbooks_list*head);
/*保存数据至文件*/
/*新建链表头节点*/
structbooks_list*Create_Books_Doc()
structbooks_list*head;
head=(structbooks_list*)malloc(sizeof(structbooks_list));
/*分配头节点空间*/
head->
next=NULL;
/*头节点指针域初始化,定为空*/
returnhead;
voidsave(structbooks_list*head)
structbooks_list*p;
FILE*fp;
p=head;
fp=fopen("
data.txt"
"
w+"
/*以写方式新建并打开data.txt文件*/
fprintf(fp,"
┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n"
/*向文件输出表格*/
┃登录号┃书名┃作者┃出版单位┃出版时间┃分类号┃价格┃\n"
┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n"
/*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/
while(p->
next!
=NULL)
{
p=p->
next;
┃%-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);
┗━━━┻━━━━━┻━━━