问题及代码:
#include <iostream>
#include <string>
using namespace std;
class Book
{
private:
string name;
string writer;
string public_name;
double price;
int number;
int NO;
public:
void setBook(string n,string w,string pn,double p,int num,int N);
void borrow(int);
void restore(int);
void set_NO(int);
int get_NO();
void print();
};
void Book::setBook(string n,string w,string pn,double p,int num,int N)
{
name=n;
writer=w;
public_name=pn;
price=p;
number=num;
NO=N;
}
void Book::borrow(int n)
{
if(number-n>=0)
{
number=number-n;
cout<<"借书成功,借书"<<n<<"本"<<endl;;
}
else
cout<<"借书失败,本书馆中还剩"<<number<<"本"<<endl;
}
void Book::restore(int n)
{
if(n)
{
number=number+n;
cout<<"还书成功,总计"<<n<<"本"<<endl;
}
else
cout<<"还书数量不足1本。";
}
void Book::set_NO(int n)
{
NO=n;
}
int Book::get_NO()
{
return NO;
}
void Book::print()
{
cout<<"书名:"<<name<<endl;
cout<<"作者:"<<writer<<endl;
cout<<"出版社:"<<public_name<<endl;
cout<<"价格:"<<price<<endl;
cout<<"馆中剩余:"<<number<<endl;
cout<<"编号:"<<NO<<endl;
cout<<"----------"<<endl;
}
int main()
{
Book JE;
JE.setBook("JaneEyre","CB","China",12.8,10,1850);
JE.print();
JE.set_NO(1888);
JE.print();
JE.borrow(20);
JE.borrow(5);
JE.print();
JE.restore(5);
JE.print();
cout<<"编号为:"<<JE.get_NO()<<endl;
return 0;
}
学习小结:
总体顺利
在set_Book写实参时前三个string型的实参一直出错,提示变量未被声明,翻阅了下书,发现赋值时需要加双引号。