c++程序设计定义一个 (图书)类,在该类定义中包括

定义一个 (图书)类,在该类定义中包括:

(1) 成员变量:bookname(书名)、price(价格) 和number(存书数量)。

(2) 成员函数:display()显示图书的情况;borrow()将存书数量减1。并显示当前存书数量;restore( )将存书数量加1,并显示当前存书数量。

(3)定义合适的构造函数。

4)在main函数中,要求建立某几种图书对象,并对该图书进行简单的显示、借阅和归还管理。

已知测试函数如下:

int main()

{  Book b1(“English”,20,3), b2(“math”,24,5);

   b1.display();

   b1.borrow();

   b2.display();

   b2.restore();

   return 0;

}

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstring>
using namespace std;
class Book
{
public:
	Book(string b, double p, int n);
	~Book();
	void display();
	void borrow();
	void restore();

private:
	string bookname;
	double price;
	int number;

};

Book::Book(string b, double p, int n)
{
	bookname = b;
	price = p;
	number = n;
}

Book::~Book()
{
}
void Book::display()
{
	cout << "bookname:" << bookname << " " << "price:" << price << " " << "number:" << number << endl;
}
void Book::borrow()
{
	number--;
	cout << "number:" << number << endl;
}
void Book::restore()
{
	number++;
	cout << "number:" << number << endl;
}
int main()
{
	Book b1("English", 20, 3), b2("math", 24, 5);
	b1.display();
	b1.borrow();
	b2.display();
	b2.restore();
	return 0;
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值