您的位置:首页 > 其它

基础的图书馆管理系统

2016-03-09 21:44 274 查看
编写图书馆管理系统。要求编写一个类。

类中的数据成员: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.类的编写首先确定其要实现的具体功能(即成员函数的确定),然后确定其数据成员。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: