定义一个图书类(Book)

时间:2025-03-10 16:06:35
#include <> #include <iostream> using namespace std; class Book { public: Book(string book, string writer, int num, int money); Book(); void bring(); void ret(); void show(); private: string book; string writer; int num; int money; }; Book::Book(string book, string writer, int num, int money) { this->book = book; this->writer = writer; this->num = num; this->money = money; } Book::Book() { book = "未命名"; writer = "佚名"; num = 0; money = 0; } void Book::bring() { if (num <= 0) cout << "借阅失败"; else num--; cout << book << "剩余" << num << "本" << endl; } void Book::ret() { num++; cout << book << "剩余" << num << "本" << endl; } void Book::show() { cout << book << "作者:" << writer << money << "剩余" << num << "本" << endl; } int main() { Book book1("人类简史", "赫拉利", 68, 10); Book book2; book1.show(); book2.show(); book1.bring(); book1.bring(); book2.bring(); book2.ret(); book1.show(); book2.show(); return 0; }