有一个图书类,能够借书和还书。将如下程序补充完整,使之能够完成图书建立,借书和归还的功能,并能够按照样例输入和输出。 #include <iostream> using namespace std; class Book { char bookname[40]; double price; int number; public: Book::Book(char *name="", double pri=0, int num=0) { //构造函数,将这部分补充完整 } void set(char *name, double pri, int num) { strcpy(bookname, name); price=pri; number=num; } void borrow(); void restore(); void display(); }; void Book::borrow() { if (number==0 ) { cout << "已没存书,退出!" << endl; } 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() { Book computer; char bookname[40]; double price; int number; cin>>bookname>>price>>number; computer.set(bookname,price,number); cin>>bookname>>price>>number; Book math(bookname,price,number); computer.borrow(); computer.borrow(); computer.restore(); math.restore(); math.borrow(); math.borrow(); computer.display(); math.display(); }
时间: 2024-03-20 21:41:46 浏览: 56
图书馆管理程序有借书还书功能
4星 · 用户满意度95%
下面是完整的程序:
```c++
#include <iostream>
#include <cstring> //需要引入cstring库
using namespace std;
class Book {
char bookname[40];
double price;
int number;
public:
Book(char *name="", double pri=0, int num=0) {
strcpy(bookname, name);
price = pri;
number = num;
}
void set(char *name, double pri, int num) {
strcpy(bookname, name);
price = pri;
number = num;
}
void borrow();
void restore();
void display();
};
void Book::borrow() {
if (number == 0) {
cout << "已没存书,退出!" << endl;
return;
}
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() {
Book computer;
char bookname[40];
double price;
int number;
cin >> bookname >> price >> number;
computer.set(bookname, price, number);
cin >> bookname >> price >> number;
Book math(bookname, price, number);
computer.borrow();
computer.borrow();
computer.restore();
math.restore();
math.borrow();
math.borrow();
computer.display();
math.display();
return 0;
}
```
下面是样例输入和输出:
**输入:**
```
Computer 55.6 10
Math 30.7 20
```
**输出:**
```
借一次,现存书量为:9
借一次,现存书量为:8
还一次,现存书量为:9
还一次,现存书量为:21
借一次,现存书量为:19
借一次,现存书量为:18
存书情况:
bookname:Computer
price:55.6
number:9
存书情况:
bookname:Math
price:30.7
number:18
```
阅读全文