// 创建一个名为 Book 的类,具有成员变量 title、author 和 year,以及成员函数 void displayInfo() ,用于显示图书的信息。 创建Book类的数组,至少存放5本书,并设计全局函数,实现Book对象按year从大到小排序,并显示排序后信息。 #include using namespace std; class Book { public: string title, author; int year; // 使用 const 加 & 引用,避免不必要的拷贝 void init(const string &title, const string &author, int year) { this->title = title; this->author = author; this->year = year; } void displayinfo() { cout << "书名: " << this->title << " , 作者: " << this->author << " , 出版年份: " << this->year << endl; } }; void sortAndShow(Book book[], int n) { int i = 0; for (; i < n; i++) { for (int j = 0; j < n - i - 1; j++) { if (book[j].year < book[j + 1].year) { Book tmp = book[j]; book[j] = book[j + 1]; book[j + 1] = tmp; } } } for (i = 0; i < n; i++) { book[i].displayinfo(); } } int main() { Book books[5]; books[0].init("数学", "张三", 1998); books[1].init("语文", "李四", 2009); books[2].init("英语", "王五", 1987); books[3].init("物理", "赵六", 2008); books[4].init("化学", "郑七", 1982); sortAndShow(books, 5); return 0; }