book(string b_id,string b_n,string au,string pu,double pr);
void display();
string getbook_ID();
string getbook_name();
string getauther();
string getpublishing();
double getprice();
};
book::book(string b_id,string b_n,string au,string pu,double pr)
{
book_ID=b_id;
book_name=b_n;
auther=au;
publishing=pu;
price=pr;
}
book::book()
{
book_ID="";
book_name="";
auther="";
publishing="";
price=0;
}
void book::display()
{
cout<<"书号:"<
cout<<"书名:"<
cout<<"作者:"<
cout<<"出版社:"<
cout<<"定价:"<
}
string book::getbook_ID()
{return book_ID;}
string book::getbook_name()
{return book_name;}
string book::getauther()
{return auther;}
string book::getpublishing()
{return publishing;}
double book::getprice()
{return price;}
//book.h 结束
//strclass.h 文件结束
#include "string.h"
class string
{
friend ostream& operator<<(ostream& S,const string& Str);
friend istream& operator>>(istream& S,string& Str);
public:
string();
string(const string& Str);
void operator=(const string& Str);
~string();
string(char * p);
private:
short m_Length;
char * m_Data;
};
string::string()
{
m_Length=1;
m_Data=new char [m_Length];
memcpy(m_Data,"",m_Length);
};
string::string(const string& Str)
{
m_Length=Str.m_Length;
m_Data=new char [m_Length];
memcpy(m_Data,Str.m_Data,m_Length);
};
string::string(char *p)
{
m_Length=strlen(p)+1;
m_Data=new char[m_Length];
memcpy(m_Data,p,m_Length);
};
void string::operator=(const string& Str)
{
if(&Str!=this)
{
delete [] m_Data;
m_Length=Str.m_Length;
m_Data=new char [m_Length];
memcpy(m_Data,Str.m_Data,m_Length);
}
return;
};
string::~string()
{delete [] m_Data;};
ostream& operator<<(ostream& S,const string& Str)
{
short i;
for(i=0;i
S << Str.m_Data[i];
return S;
};
istream& operator >> (istream& S,string& Str)
{
const short BUFLEN=256;
char Buf[BUFLEN];
memset(Buf,0,BUFLEN);
if(S.peek()=='\n')
S.ignore();
S.getline(Buf,BUFLEN,'\n');
Str=Buf;
return S;
};
//strclass.h结束
//buy_book.cpp开始
#include
#include "strclass.h"
#include "buyer.h"
#include "book.h"
void main()
{
int i=0,buyerid,flag;
book *c[2];
layfolk b1("王林",1,"武汉",0);
honoured_guest b2("张丽",2,.6,"长沙",0); //
member b3("张强",3,5,"广州",0);
buyer *b[3]={&b1,&b2,&b3};
book c1("7-302-04504-6","计算机二级辅导","张林","清华",25);
book c2("7-402-03388-9"," 数据结构","许桌群","北大",20);
c[0]=&c1;
c[1]=&c2;
cout<<"购书人信息:\n\n";
for(i=0;i<3;i++)
b[i]->display();
cout<<"\n图书信息:\n\n";
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<<"编号不存在"<
else
{
b[i]->setpay(c[0]->getprice());
b[i]->setpay(c[1]->getprice());
cout<
}
}
//buy_book.cpp文件结束
运行结果如下:
实训心得:
转眼,C++一周的实训已经结束了,在这一周的过程中,通过上机练习C++编程,深切的体会到了编程的乐趣,在做题的过程中,不仅提高了我动手实践的能力,我懂得了它不是一个人的任务,是大家共同努力的结果。
不过我也真正的认识到,要学好C++理论是基本,要想更好的,更快的有所突破还是要通过上机练习。
在此次实训中,我也看到了许多自己的不足,不仅是知识上的,还有思考能力等都需要提高,再编程过程中,思维是占主要的。所以不断的编程,是提高自己能力的途径。
在此更要感谢老师对我们的孜孜不倦的教导!