根据如下程序,写出网上购书程序,模拟下订单的操作。每张订单的数据成员如下:
class order{
public:
.
.
.
private:
static int ordercount;//自动增加订单编号
int orderID;//订单编号
int buyerID;//购书人编号
int listcount;//购书数量
string orderlist[20];//记录书号的数组
};
//buy.h文件开始
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 fetpay();//取应付金额
int fetid();//取购书人编号
virtual void display()=0;//显示函数
virtual void setpay(double=0)=0;//计算购书金额
};
class member:public buyer{//会员类
int leaguer_grade;//会员级别
public:
member(string n,int b,int l,string a,double p):buyer(n,b,a,p)
{leaguer_grade=1;}//构造函数
void display();//显示函数
void setpay(double p);
};
class honoured_guest:public buyer{//贵宾类
double discount_rate;//折扣率
public:
layfolk(strring n,int b,string a,double p):buyer(n,b,a,p)
{}//构造函数
void display();//显示函数
void setpay(double p);//计算够数金额
};
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 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=.90*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<<"级别错误!";
}
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;
}
//buy.h文件结束
//book.h文件开始
class book{//图书类
protected:
string book_ID;//书号
string book_name;//书名
string author;//作者
string publishing;//出版社
double price;//定价
public:
book();//构造函数
book(string b_id,string b_a,string au,string pu,double pr);//重载构造函数
void display();
string getbook_ID();//取书号
string getbook_name();//取书名
string getauthor();//取作者
string getpublishing();//取出版社
double getprice();//取定价
};
book::book(string b_id,string b_n,string su,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<<"\t";
cout<<"出版社:"<<publishing<<"\t";
cout<<"定价:"<<price<<"\t";
}
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;//取定价
}
本题采用C++提供的新的数据类型字符串类类型(string类类型)来编程。使用string类时,必须在程序的开头将C++标准库中的string头文件包含进来,即应加上#include<strinq>//注意头文件名不是string.h
完整程序及相应的说明如下: