您好,欢迎访问三七文档
//buy.h文件开始classbuyer//基类{protected:stringname;//姓名intbuyerid;//购书人编号stringaddress;//地址doublepay;//购书金额public:buyer();buyer(stringn,intb,stringa,doublep);stringgetbuyname();//取姓名stringgetaddress();//取地址doublegetpay();//取应付金额intgetid();//取购书人编号virtualvoiddisplay()=0;//显示函数virtualvoidsetpay(double=0)=0;//计算购书金额};classmember:publicbuyer//会员类{intleaguer_grade;//会员级别public:member(stringn,intb,intl,stringa,doublep):buyer(n,b,a,p){leaguer_grade=l;}//构造函数voiddisplay();//显示函数voidsetpay(doublep);//计算购书金额};classhonoured_guest:publicbuyer//贵宾类{doublediscount_rate;//贵宾折扣率public:honoured_guest(stringn,intb,doubler,stringa,doublep):buyer(n,b,a,p){discount_rate=r;}//构造函数voiddisplay();//显示函数voidsetpay(doublep);//计算购书金额};classlayfolk:publicbuyer//普通人员类{public:layfolk(stringn,intb,stringa,doublep):buyer(n,b,a,p){}//构造函数voiddisplay();//显示对象voidsetpay(doublep);//计算购书金额};buyer::buyer()//基类的构造函数{name=;buyerid=0;address=;pay=0;}buyer::buyer(stringn,intb,stringa,doublep)//基类的构造函数{name=n;buyerid=b;address=a;pay=p;}doublebuyer::getpay()//取购书人金额{returnpay;}stringbuyer::getaddress()//取购书人地址{returnaddress;}stringbuyer::getbuyname()//取购书人姓名{returnname;}intbuyer::getid()//取购书人编号{returnbuyerid;}voidmember::display()//会员类的显示函数{cout购书人姓名:name\t;cout购书人编号:buyerid\t;cout购书人为会员,级别:leaguer_grade\n;cout地址:address\n;}voidmember::setpay(doublep)//会员类的计算购书金额{if(leaguer_grade==1)//会员级别为1pay=0.95*p+pay;elseif(leaguer_grade==2)//会员级别为2pay=0.90*p+pay;elseif(leaguer_grade==3)//会员级别为3pay=0.85*p+pay;elseif(leaguer_grade==4)//会员级别为4pay=0.8*p+pay;elseif(leaguer_grade==5)//会员级别为5pay=0.7*p+pay;elsecout级别错误!;}voidhonoured_guest::display()//贵宾类的显示函数{cout购书人姓名:name\t;cout购书人编号:buyerid\t;cout购书人为贵宾,折扣率为:discount_rate*100%\n;cout地址:address\n\n;}voidhonoured_guest::setpay(doublep)//贵宾类的计算购书金额{pay=pay+(1-discount_rate)*p;}voidlayfolk::display()//普通类的显示函数{cout购书人姓名name\t;cout购书人编号:buyerid\t;cout购书人为普通人\n;cout地址:address\n\n;}voidlayfolk::setpay(doublep)//普通类的计算购书金额{pay=pay+p;}//buy.h文件结束//book.h文件开始classbook//图书类{protected:stringbook_id;//书号stringbook_name;//书名stringauther;//作者stringpublishing;//出版社doubleprice;//定价public:book();//构造函数book(stringb_id,stringb_n,stringau,stringpu,doublepr);/*重载构造函数*/voiddisplay();stringgetbook_id();//取书号stringgetbook_name();//取书名stringgetauther();//取作者stringgetpublishing();//取出版社doublegetprice();//取定价};book::book(stringb_id,stringb_n,stringau,stringpu,doublepr){book_id=b_id;book_name=b_n;auther=au;publishing=pu;price=pr;}book::book(){book_id=;book_name=;auther=;publishing=;price=0;}voidbook::display(){cout书号:book_id\t;cout书名:book_name\t;cout作者:auther\t;cout出版社:publishing\t;cout定价:price\n;}stringbook::getbook_id()//取书号{returnbook_id;}stringbook::getbook_name()//取书名{returnbook_name;}stringbook::getauther()//取作者{returnauther;}stringbook::getpublishing()//取出版社{returnpublishing;}doublebook::getprice()//取定价{returnprice;}//book.h文件结束//strclass.h文件开始#includestring.hclassstring{friendostream&operator(ostream&S,conststring&Str);//重载friendistream&operator(istream&S,string&Str);//重载public:string();string(conststring&Str);voidoperator=(conststring&Str);//重载=~string();string(char*p);private:shortm_Length;//字符串长度char*m_Data;//字符串开始地址};string::string(){m_Length=1;m_Data=newchar[m_Length];memcpy(m_Data,,m_Length);};string::string(conststring&Str){m_Length=Str.m_Length;m_Data=newchar[m_Length];memcpy(m_Data,Str.m_Data,m_Length);};string::string(char*p){m_Length=strlen(p)+1;m_Data=newchar[m_Length];memcpy(m_Data,p,m_Length);};voidstring::operator=(conststring&Str){if(&Str!=this){delete[]m_Data;m_Length=Str.m_Length;m_Data=newchar[m_Length];memcpy(m_Data,Str.m_Data,m_Length);}return;};string::~string(){delete[]m_Data;};ostream&operator(ostream&S,conststring&Str){shorti;for(i=0;iStr.m_Length-1;i++)SStr.m_Data[i];returnS;};istream&operator(istream&S,string&Str){constshortBUFLEN=256;charBuf[BUFLEN];if(S.peek()=='\n')S.ignore();S.getline(Buf,BUFLEN,'\n');Str=Buf;returnS;};//strclass.h文件结束//buy_book.cpp文件开始#includeiostream.h#includestrclass.h#includebuy.h#includebook.hvoidmain(){inti=0,buyerid,flag;book*c[2];//用指针数组存放book对象的地址layfolkb1(林小茶,1,北京,0);honoured_guestb2(王遥遥,2,.6,上海,0);memberb3(赵红艳,3,5,广州,0);buyer*b[3]={&b1,&b2,&b3};//用指针数组存放继承了buyer类的3个对象地址bookc1(7-302-04504-6,C++程序设计,谭浩强,清华,25);bookc2(7-402-03388-9,数据结构,许卓群,北大,20);c[0]=&c1;c[1]=&c2;cout购书人信息:\n\n;for(i=0;i3;i++)//显示3个继承了buyer类的对象b[i]-display();cout\n图书信息:\n\n;//显示两个book对象的信息for(i=0;i2;i++)c[i]-display();cout\n\n:请输入购书人编号;cinbuyerid;flag=0;for(i=0;i3;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());coutendl购书人需要付费:b[i]-getpay()\n\n;}}//buy_book.cpp文件结束
本文标题:网上购书C++
链接地址:https://www.777doc.com/doc-4375483 .html