标签:namespace info 出版 数组 cout 基类 open 生活 思想
一、 系统环境:
Windows xp以上版本,电脑
二、 设计内容:
程序一共定义五个类,继承关系如图所示
首先我进行了主菜单主函数,会员登录,书记信息查看,购书。又在这些主菜单上进行相对应的子菜单的函数。主菜单如下:
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
class buyer { //基类
protected :
string name ; //姓名
int buyerID ; //购书人编号
string address ; //地址
double pay ; //购书费用
public :
buyer ();
buyer ( string n ,int b ,string a ,double p );
string getbuyname (); //取姓名
string getaddress(); //取地址
double getpay (); //取应付费用
int getid (); //取购书人编号
virtual void display ( )= 0; //显示对象
virtual void setpay ( double=0)=0; //计算购书费用
friend ostream& operator << (ostream& out, buyer &b){
out << b.name << " " << b.buyerID << " " << b.address << " " << b.pay << endl;
return out;
}
friend istream& operator >>(istream& in, buyer &b){
in >> b.name >> b.buyerID >> b.address >> b.pay;
return in;
}
};
这个是主函数。在主函数的应用中我主要是参考了书上的题目。
class member :public buyer { //会员类
private :
int leaguer_grade ; //会员级别
public :
member ( string n ,int b ,int l,string a ,double p ):buyer ( n,b ,a ,p )
{leaguer_grade = l;} //构造函数
void display (); //显示函数
void setpay (double p);
friend ostream& operator << (ostream& out, member &b){
out << b.name << " " << b.buyerID << " " << b.address << " " << b.pay << " "<< b.leaguer_grade <<endl;
return out;
}
friend istream& operator >>(istream& in, member &b){
in >> b.name >> b.buyerID >> b.address >> b.pay >> b.leaguer_grade;
return in;
}
};
class honoured_guest : public buyer { //贵宾类
double discount_rate ; //折扣率
public :
honoured_guest ( string n ,int b ,double r ,string a ,double p ):buyer (n,b ,a,p)
{discount_rate = r;} //构造函数
void display (); //显示对象
void setpay ( double p); //计算购书费用
friend ostream& operator << (ostream& out, honoured_guest &b){
out << b.name << " " << b.buyerID << " " << b.address << " " << b.pay << " " << b.discount_rate << endl;
return out;
}
friend istream& operator >>(istream& in, honoured_guest &b){
in >> b.name >> b.buyerID >> b.address >> b.pay >> b.discount_rate;
return in;
}
};
class layfolk :public buyer {
public: //普通人类
layfolk(string n,int b, string a, double p ):buyer( n,b ,a ,p )
{} //构造函数
void display ( ); //显示对象
void setpay ( double p); //计算购书费用
friend ostream& operator << (ostream& out, layfolk &b){
out << b.name << " " << b.buyerID << " " << b.address << " " << b.pay << endl;
return out;
}
friend istream& operator >>(istream& in, layfolk &b){
in >> b.name >> b.buyerID >> b.address >> b.pay;
return in;
}
};
buyer::buyer() { //基类构造函数
name ="";
buyerID = 0;
address ="";
pay = 0;
}
buyer::buyer( string n ,int b ,string a ,double p ) //基类的构造函数
{
name =n;
buyerID =b;
address =a;
pay = p;
}
double buyer::getpay() //收取书费用
{
return this->pay;
}
string buyer::getaddress () //取购书人地址
{
return address ;
}
string buyer::getbuyname() //取购书人名字
{
return name ;
}
int buyer::getid () //取购书人编号
{
return buyerID ;
}
void member::display () //会员类的显示函数
{
cout<<"购书人姓名:" << name << "\t";
cout<<"购书人编号:"<<buyerID <<"\t";
cout<<"购书人为会员,级别:"<<leaguer_grade <<"\n";
cout<<"地址:"<<address <<"\n";
}
void member::setpay ( double p) //会员类的计算购书费用
{
if (leaguer_grade == 1) //会员级别为1
pay =.95*p +pay;
else if ( leaguer_grade ==2) //会员级别为2
pay =.95*p +pay;
else if (leaguer_grade ==3) //会员级别为3
pay =.85*p +pay;
else if(leaguer_grade ==4) //会员级别为4
pay =.8*p +pay;
else if (leaguer_grade ==5) //会员级别为5
pay =.7 *p +pay;
else cout<<"级别错误!" << endl;
}
void honoured_guest::display() //贵宾类的显示函数
{
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID <<"\t";
cout<<"购书人为贵宾!折扣率为:"<<discount_rate*100 <<"% \n";
cout<<"地址:"<<address <<"\n\n";
}
void honoured_guest::setpay (double p) //贵宾类计算购书费用
{
pay =pay+(1-discount_rate) *p;
}
void layfolk::display() //普通类显示函数
{
cout<<"购书人姓名:"<<name<<"\t";
cout<<"购书人编号:"<<buyerID <<"\t";
cout<<"购书人为普通人"<<"\n";
cout<<"地址:"<<address <<"\n\n";
}
void layfolk::setpay(double p) //普通类计算购书费用
{
pay = pay+p;
}
class book { //图书类
protected :
string book_ID; //书号
string book_name; //书名
string author; //作者
string publishing; //出版社
double price ; //定价
public :
book(); //构造函数
book (string b_id ,string b_n ,string au ,string pu ,double pr);
//重载构造函数
void display ();
string getbook_ID (); //取书号
string getbook_name (); //取书名
string getauthor (); //取作者
string getpublishing (); //取出版社
double getprice (); //取定价
friend ostream& operator <<(ostream& out, book &b){
out << b.book_ID << " " << b.book_name << " " << b.author << " " << b.publishing << " " << b.price << endl;
return out;
}
friend istream& operator >>(istream& in, book &b){
in >> b.book_ID >> b.book_name >> b.author >> b.publishing >> b.price;
return in;
}
};
book::book(string b_id, string b_n, string au ,string pu, double pr){
book_ID = b_id; //书号
book_name = b_n; //书名
author = au; //作者
publishing = pu; //出版社
price = pr; //定价
}
book::book(){
book_ID = ""; //书号
book_name = ""; //书名
author = ""; //作者
publishing = ""; //出版社
price = 0; //定价
}
void book::display()
{
cout<<"书号:"<<book_ID <<"\t";
cout<<"书名:"<<book_name <<"\t";
cout<<"作者:"<<author <<"\n";
cout<<"出版社:"<<publishing <<"\t";
cout<<"定价:"<<price <<"\n";
}
string book::getbook_ID () //取书号
{
return book_ID ;
}
string book::getbook_name () //取书名
{
return book_name ;
}
string book::getauthor() //取作者
{
return author ;
}
string book::getpublishing() //取出版社
{
return publishing ;
}
double book::getprice() //取定价
{
return price ;
}
int main ()
{
int i =0,buyerid ,flag ;
book *c[2]; //用指针数组存放book对象的地址
layfolk b1("林小慧",1,"新疆",0);
honoured_guest b2("赵丽宏",2,.6,"海南",0);
member b3("王国维",3,5,"江西",0);
buyer * b[3]= {&b1,&b2,&b3}; //使用指针数组存放继承了buyer类
//的三个对象的地址
book c1("7-302-04504-6", "Java程序设计","谭浩强","华中",25);
book c2("7-402-03388-9","离散数学","马华强","武大",20);
c[0] = &c1;
c[1] = &c2;
cout<<"购书人信息:\n\n";
for(i=0;i<3;i++) //显示三个继承了buyer 类的对象
b[i]->display();
cout<<"\n图书信息:\n\n"; //显示两个book对象的信息
for(i=0;i<2;i++)
c[i]->display();
cout<<"\n\n请输入购书人编号:";
cin >>buyerid ;
flag = 0;
for(i=0;i<3;i++)
if(b[i]->getid() == buyerid){flag = 1;break;}
if(!flag){cout << "编号不存在" << endl;}
else
{ b[i]->setpay(c[0]->getprice()); //计算购书费用
b[i]->setpay(c[1]->getprice());
cout << endl << "购书人需要付费:" <<b[i]->getpay()<<"\n\n";
}
//保存数据
ofstream out;
out.open("book.txt");
out << c1 << c2;
out.close();
out.open("buyer.txt");
out << b1 <<b2 << b3;
out.close();
return 0;
}
三、 测试内容及结果:
总文件的保存
程序运行后主界面如图所示,上面是购书人,购书人从这个文件读取。
四、增加程序和改进
int main ()
{
int i =0,buyerid ,flag ;
book *c[2]; //用指针数组存放book对象的地址
layfolk b1("林小慧",1,"新疆",0);
honoured_guest b2("赵丽宏",2,.6,"海南",0);
member b3("王国维",3,5,"江西",0);
buyer * b[3]= {&b1,&b2,&b3}; //使用指针数组存放继承了buyer类
//的三个对象的地址
book c1("7-302-04504-6", "Java程序设计","谭浩强","华中",25);
book c2("7-402-03388-9","离散数学","马华强","武大",20);
c[0] = &c1;
c[1] = &c2;
cout<<"购书人信息:\n\n";
for(i=0;i<3;i++) //显示三个继承了buyer 类的对象
b[i]->display();
cout<<"\n图书信息:\n\n"; //显示两个book对象的信息
for(i=0;i<2;i++)
c[i]->display();
cout<<"\n\n请输入购书人编号:";
cin >>buyerid ;
flag = 0;
for(i=0;i<3;i++)
if(b[i]->getid() == buyerid){flag = 1;break;}
if(!flag){cout << "编号不存在" << endl;}
else
{ b[i]->setpay(c[0]->getprice()); //计算购书费用
b[i]->setpay(c[1]->getprice());
cout << endl << "购书人需要付费:" <<b[i]->getpay()<<"\n\n";
}
//保存数据
ofstream out;
out.open("book.txt");
out << c1 << c2;
out.close();
out.open("buyer.txt");
out << b1 <<b2 << b3;
out.close();
return 0;
}
在购书系统中我加入了一个保存系统,以便日后的查询和数据的丢失。因为在日常生活中我们的数据没有很完善,所以很容易丢失。
五、学习心得
我在这一次的总结学习中又一次的体会到了函数的思想,在模块化的设计中,每一个主函数的子函数都需要做到极致化,在运行中才能更细节话,运行的体验也会更强烈。在这次设计中,我也体会到了程序工作的不容易,每一次的小细节都可能使你的程序编译不成功。一个小问题让你的整个程序就会出现了大问题。在每一次的修改和完善中,才能看出以前所在的问题,或许是因为学习能力的有限,知识储备的不够完善,当我们再次回过头去看解决了这些问题时,又是一次大的进步。
标签:namespace info 出版 数组 cout 基类 open 生活 思想
原文地址:https://www.cnblogs.com/gn33/p/14536884.html