项目四 用类解决关于图书馆的书的问题

编写一个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>

测试图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值