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;

}

cpp 复制代码
#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;
}
相关推荐
灰子学技术1 天前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
那个村的李富贵1 天前
CANN加速下的AIGC“即时翻译”:AI语音克隆与实时变声实战
人工智能·算法·aigc·cann
二十雨辰1 天前
[python]-AI大模型
开发语言·人工智能·python
power 雀儿1 天前
Scaled Dot-Product Attention 分数计算 C++
算法
Yvonne爱编码1 天前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚1 天前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂1 天前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
pas1361 天前
41-parse的实现原理&有限状态机
开发语言·前端·javascript
琹箐1 天前
最大堆和最小堆 实现思路
java·开发语言·算法
renhongxia11 天前
如何基于知识图谱进行故障原因、事故原因推理,需要用到哪些算法
人工智能·深度学习·算法·机器学习·自然语言处理·transformer·知识图谱