#include<iostream>
#include<string>
using namespace std;
class Book
{
public:
void setBook(string na,string w,string pu,double pr,int num,int NO);
void set_NO(int n);
void get_NO();
void borrow();
void restore();
void print();
private:
string name;
string writer;
string publicer;
double price;
int number;
int NO;
};
void Book::setBook(string na,string w,string pu,double pr,int num,int n)
{
name=na;
writer=w;
publicer=pu;
price=pr;
number=num;
NO=n;
}
void Book::borrow()
{
number--;
cout<<"number:"<<number<<endl;
}
void Book::restore()
{
number++;
cout<<"number:"<<number<<endl;
}
void Book::print()
{
cout<<"书名:"<<name<<endl;
cout<<"作者:"<<writer<<endl;
cout<<"出版社:"<<publicer<<endl;
cout<<"价格:"<<price<<endl;
}
void Book::set_NO(int n)
{
NO=n;
}
void Book::get_NO()
{
cout<<NO<<endl;
}
void main()
{ int n;
Book t1;
t1.setBook("目送","龙应台","理想国",43,23,56789);
t1.print();
cout<<endl<<"借出一本书后"<<endl;
t1.borrow();
t1.print();
cout<<endl<<"还回一本书后"<<endl;
t1.restore();
t1.print();
cout<<"将书号改为:";
cin>>n;
t1.set_NO(n);
t1.print();
cout<<endl<<"更改书号后:"<<endl;
}
二 运行结果
三心得体会
关于类的知识还需练习,通过对比自己与同学的程序,发现还是有许多输出考虑不周。
四知识点总结
在定义string是要在头文件定义函数,同时在setbook输出数据信息是应赋予变量,在输出时才使人一目了然。公用的函数应放在public,私用成员放在private,这点切记。