编写一个Book类,包含name(书名)、writer(著者)、public_name(出版社)、price(价格)、number(数量)、NO(书号)等数据成员。定义成员函数setBook,用来为书籍的数据成员赋值,定义成员函数borrow和restore,分别办理借出和还回(借、还书时,暂时只完成数量的增1减1,这里的书仍是一种书的概念,随着课程进展,我们继续做真正的图书管理系统,每本书都能追踪到是谁何时借了,何时还了)业务,定义成员函数print用于输出—有关书的信息,定义set_NO与get_NO来实现修改类对象的书号与获得类对象的书号。自己写main函数,用来测试你编写的类。
代码如下:
<span style="font-size:14px;"><strong>/*
*copyright (c) 2015,
*All rights reserved
*The Author:王争取
*Finished Time:2015.3.15
*/
#include <iostream>
using namespace std;
class Book
{
public:
void set_book();
void borrow();
void restore();
void set_NO();
void get_NO();
void print();
private:
string name;
string writer;
string publication_name;
double price;
int number;
int No;
};
void Book::set_book()
{
cout<<"please set a name of the book:";
cin>>name;
cout<<"the writer name:";
cin>>writer;
cout<<"the publication name:";
cin>>publication_name;
cout<<"it's price :";
cin>>price;
cout<<"the number of this book:";
cin>>number;
cout<<"the NO:";
cin>>No;
}
void Book::borrow()
{
int n;
cout<<"how many books you want to borrow? ";
while(1)
{
cin>>n;
if(n>number)
cout<<"the book isn't enough, please make sure how many you want."<<endl;
else break;
}
number-=n;
}
void Book::restore()
{
int n;
cout<<"Please write the number of the book you restore:";
cin>>n;
number+=n;
}
void Book::print()
{
cout<<"name:"<<name<<endl<<" writer:"<<writer<<endl<<" publication name:"<<publication_name<<endl<<" the price:"<<price;
cout<<endl<<" number:"<<number<<endl<<" the NO"<<No;
}
void Book::set_NO()
{
cout<<"please write the NO you change:";
cin>>No;
}
void Book::get_NO()
{
cout<<"it's "<<No<<" book";
}
int main()
{
Book book;
book.set_book();
book.borrow();
book.restore();
book.set_NO();
book.get_NO();
cout<<endl<<"the introduce of this book"<<endl;
book.print();
return 0;
}
</strong></span>
测试图如下: