#include <iostream>
using namespace std;
class Book
{ public:
void setBook(char*,double,int);
void borrow();
void restore();
void display();
private:
char bookname[40];
double price;
int number;
};
//在类外定义Book类的成员函数
void Book::setBook(char *name, double pri, int num)
{ strcpy(bookname, name);
price=pri;
number=num;
}
void Book::borrow()
{ if (number==0 )
{ cout << "已没存书,退出!" << endl;
abort();
}
number = number - 1;
cout << "借一次,现存书量为:" << number << endl;
}
void Book::restore()
{ number = number + 1;
cout << "还一次,现存书量为:" << number << endl;
}
void Book::display()
{ cout << "存书情况:" << endl
<< "bookname:" << bookname << endl
<< "price:" << price << endl
<< "number:" << number << endl;
}
int main()
{ char flag, ch;
Book computer;
computer.setBook( "c++程序设计基础" , 32, 1000 );
computer.display();
ch = 'y';
while ( ch == 'y' )
{ cout << "请输入借阅或归还标志(b/r):";
cin >> flag;
switch ( flag )
{ case 'b': computer.borrow(); break;
case 'r': computer.restore();
}
cout << "是否继续?(y/n)";
cin >> ch;
computer.display();
}
}
相关分类