cpp
#include <iostream>
#include <cstring>
#define BOOK_MAX_NAME_LEN 100
#define BOOK_MAX_AUTHOR_LEN 100
// 定义结构体 BookBox
struct BookBox {
bool isEmpty;
int bookTotalNum;
int bookBorrowedNum;
char bookName[BOOK_MAX_NAME_LEN];
char bookAuthor[BOOK_MAX_AUTHOR_LEN];
int category; // 0: Philosophical, 1: Sciences
int position; // 0: 1st floor, 1: 2nd floor
long bookTotalBorrowedCounter;
// 构造函数,设置默认值
BookBox(bool empty = true, int totalNum = 0, int borrowedNum = 0,
const char* name = "Unknown", const char* author = "Unknown",
int cat = 0, int pos = 0, long counter = 0)
: isEmpty(empty), bookTotalNum(totalNum), bookBorrowedNum(borrowedNum),
category(cat), position(pos), bookTotalBorrowedCounter(counter) {
strncpy(bookName, name, BOOK_MAX_NAME_LEN - 1);
bookName[BOOK_MAX_NAME_LEN - 1] = '\0'; // 确保以空字符结尾
strncpy(bookAuthor, author, BOOK_MAX_AUTHOR_LEN - 1);
bookAuthor[BOOK_MAX_AUTHOR_LEN - 1] = '\0'; // 确保以空字符结尾
}
};
int main() {
// 不设置 bookTotalNum 和 author,使用默认值
BookBox book1(false, 0, 0, "1984", nullptr, 1, 0, 0);
// 使用默认作者 "Unknown"
std::cout << "Book Name: " << book1.bookName << std::endl;
std::cout << "Author: " << book1.bookAuthor << std::endl;
std::cout << "Total Books: " << book1.bookTotalNum << std::endl;
// 使用不同的名字,但保留其他默认值
BookBox book2(true, 1, 0, "Brave New World");
std::cout << "Book Name: " << book2.bookName << std::endl;
std::cout << "Author: " << book2.bookAuthor << std::endl; // 默认作者
std::cout << "Total Books: " << book2.bookTotalNum << std::endl;
return 0;
}