基础的图书馆管理系统

编写图书馆管理系统。要求编写一个类。

类中的数据成员:name(书名)、writer(著者)、public_name(出版社)、price(价格)、number(数量)、no(书号)

类中的成员函数:setbook(为数据成员赋值)

                              borrow和restore(借入和借出,表现为number的加一和减一)

                              print函数

                              set_no和get_no设置索书号和获得索书号

#include <iostream>
#include <string>
using namespace std;  
class book
{
        string name;
	string writer;
	string public_name;
	double price;
	int number;
	int no;
public:
	void setbook(string,string,string,double,int,int);
	void borrow();
	void restore();
	void print();
	void set_no(int);
	int get_no();
};
void book::setbook (string n,string w,string p,double pri,int num,int noo)
{
	name=n;
	writer=w;
	public_name=p;
	price=pri;
	number=num;
	no=noo;
}
void book::borrow()
{
	--number;
}
void book::restore()
{
	++number;
}
void book::print()
{
	cout<<"name: "<<name<<endl;  
    cout<<"writer: "<<writer<<endl;  
    cout<<"public name: "<<public_name<<endl;  
    cout<<"price: "<<price<<endl;  
    cout<<"number: "<<number<<endl;  
    cout<<"NO: "<<no<<endl<<endl;  
}
void book::set_no(int n)
{
	no=n;
}
int book::get_no()
{
	return no;
}
int main()
{
	book b;
	b.setbook("自动控制原理","王建辉","东大出版社",20,3,3589);
	b.print();
	b.borrow();
	b.print();
	b.restore();
	b.print();
	b.set_no(39048);
	cout<<"索书号为"<<b.get_no()<<endl;
}
这个类包含的只有数据成员和成员函数。实现了图书馆管理系统中一些最基础的功能。

注意:1。类中的数据成员一般设置为私有。私有的意思是只有类内的成员函数才可以访问这些成员。

当时编写的时候,进行void book::print()函数的定义时,忘记写book,这时print函数变成类外的函数,不能直接访问数据成员,出现数据成员未定义的错误。

           2.函数一般在需要从键盘输入数据的时候需要形参。

           3.类的编写首先确定其要实现的具体功能(即成员函数的确定),然后确定其数据成员。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值