C++ 图书管理系统:面向对象、STL 与数据持久化
一个基于 C++ 面向对象思想开发的图书管理系统。系统采用 STL 容器(vector/map)进行数据存储,无需外部数据库,通过文件实现数据持久化。核心功能包括图书与读者的增删改查、借阅归还流程闭环、以及运营数据统计。设计上运用了封装、继承和多态特性,定义了 Book、Reader 及其派生类,并通过 LibrarySystem 类统一管理业务逻辑。代码包含完整的控制台交互逻辑、输入校验及文件 IO 处理,适合 C++ 课程实训参考。

一个基于 C++ 面向对象思想开发的图书管理系统。系统采用 STL 容器(vector/map)进行数据存储,无需外部数据库,通过文件实现数据持久化。核心功能包括图书与读者的增删改查、借阅归还流程闭环、以及运营数据统计。设计上运用了封装、继承和多态特性,定义了 Book、Reader 及其派生类,并通过 LibrarySystem 类统一管理业务逻辑。代码包含完整的控制台交互逻辑、输入校验及文件 IO 处理,适合 C++ 课程实训参考。

本图书管理系统基于 C++ 面向对象编程思想开发,是一套轻量化、无数据库依赖的控制台端图书馆运营管理解决方案。系统核心功能覆盖图书全生命周期管理、读者分级管控、借阅归还流程闭环、数据持久化存储及运营数据统计五大维度。
| 技术点 | 应用场景 |
|---|---|
| 面向对象(OOP) | 图书/读者类的封装、继承、多态 |
| STL 容器(vector/map) | 数据存储(vector 遍历、map 快速查找) |
| 文件 IO 流 | 文本文件实现数据持久化 |
| 字符串处理 | CSV 格式数据解析、分割 |
| 动态类型转换(dynamic_cast) | 派生类对象的类型判断 |
| 控制台交互优化 | 输入校验、清屏、暂停等 |
代码按'头文件 + 实现文件'分离规范组织,目录结构如下:
Library Management System/
├─ Book.h // 图书基类 + 派生类头文件
├─ Book.cpp // 图书类实现
├─ Reader.h // 读者基类 + 派生类头文件
├─ Reader.cpp // 读者类实现
├─ BorrowRecord.h // 借阅记录类头文件
├─ BorrowRecord.cpp // 借阅记录类实现
├─ LibrarySystem.h // 图书馆系统核心类头文件
├─ LibrarySystem.cpp // 系统类实现
├─ Utils.h // 工具类头文件
├─ Utils.cpp // 工具类实现
├─ main.cpp // 主菜单交互逻辑
└─ data/ // 数据文件目录(books.txt/readers.txt/records.txt)
本系统通过基类抽象共性、派生类扩展特性,结合纯虚函数实现多态,是面向对象思想的核心体现。
功能说明:封装所有图书的通用属性(编号、名称、作者、借阅状态),派生类扩展特有属性(小说类型、教材适用年级),通过纯虚函数 showInfo() 实现多态展示。
核心代码(Book.h):
#pragma once
#include "Utils.h"
// 图书基类(抽象类,纯虚函数 showInfo)
class Book {
public:
Book(const string& bookId, const string& bookName, const string& author, bool isBorrow);
virtual void showInfo() = 0; // 纯虚函数,强制派生类重写
// 封装的 set/get 方法
void setBookName(const string& bookName);
void setAuthor(const string& author);
void setIsBorrowed(bool key);
string getBookId() const;
string getBookName() const;
string getAuthor() const;
bool getIsBorrowed() const;
~Book();
private:
string bookId; // 图书编号
string bookName; // 书名
string author; // 作者
bool isBorrowed; // 借阅状态
};
// 小说类(派生自 Book)
class NovelBook : public Book {
public:
NovelBook(const string& bookId, const string& bookName, const string& author, bool isBorrow, const string& novelType);
void showInfo() override; // 重写纯虚函数
string getNovelType() const;
private:
string novelType; // 小说类型(科幻/言情等)
};
// 教材类(派生自 Book)
class TextBook : public Book {
public:
TextBook(const string& bookId, const string& bookName, const string& author, bool isBorrow, const string& grade);
void showInfo() override; // 重写纯虚函数
string getGrade() const;
private:
string grade; // 适用年级
};
核心代码(Book.cpp):
#include "Book.h"
// 基类构造函数
Book::Book(const string& bookId, const string& bookName, const string& author, bool isBorrow) {
this->bookId = bookId;
this->bookName = bookName;
this->author = author;
this->isBorrowed = isBorrow;
}
// 小说类构造 + 重写 showInfo
NovelBook::NovelBook(const string& bookId, const string& bookName, const string& author, bool isBorrowed, const string& novelType)
: Book(bookId, bookName, author, isBorrowed), novelType(novelType) {}
void NovelBook::showInfo() {
cout << "编号:" << getBookId() << endl;
cout << "书名:" << getBookName() << endl;
cout << "作者:" << getAuthor() << endl;
if (getIsBorrowed()) cout << "借阅状态:已借出" << endl;
else cout << "借阅状态:未借出" << endl;
cout << "小说类型:" << novelType << endl;
}
// 教材类构造 + 重写 showInfo
TextBook::TextBook(const string& bookId, const string& bookName, const string& author, bool isBorrowed, const string& grade)
: Book(bookId, bookName, author, isBorrowed), grade(grade) {}
{
cout << << () << endl;
cout << << () << endl;
cout << << () << endl;
(()) cout << << endl;
cout << << endl;
cout << << grade << endl;
}
{ ->bookName = bookName; }
{ ->author = author; }
{ ->isBorrowed = key; }
{ bookId; }
{ bookName; }
{ author; }
{ isBorrowed; }
Book::~() {}
{ novelType; }
{ grade; }
代码讲解:
Book 用 纯虚函数 showInfo() 抽象'展示图书信息'的行为,派生类必须重写;showInfo() 时,会自动匹配对象的实际类型(小说/教材),实现多态;set/get 方法访问,体现封装性。功能说明:封装读者通用属性(编号、姓名、已借阅数),派生类扩展特有属性(学号/教师工号),通过 borrowLimit() 实现不同读者的借阅上限(学生 3 本、教师 5 本)。
核心代码(Reader.h):
#pragma once
#include "Utils.h"
// 读者基类(抽象类)
class Reader {
public:
Reader(const string& readerId, const string& name, int borrowCount);
virtual int borrowLimit() = 0; // 纯虚函数:借阅上限
virtual void showInfo() = 0; // 纯虚函数:展示信息
// 封装的方法
void setName(const string& name);
void BorrowCountAdd();
void BorrowCountSub();
string getReaderId() const;
string getName() const;
int getBorrowCount() const;
~Reader();
private:
string readerId; // 读者编号
string name; // 姓名
int borrowCount;
};
: Reader {
:
( string& readerId, string& name, borrowCount, string& studentId);
;
;
;
:
string studentId;
};
: Reader {
:
( string& readerId, string& name, borrowCount, string& teacherId);
;
;
;
:
string teacherId;
};
核心代码(Reader.cpp):
#include "Reader.h"
// 基类构造函数
Reader::Reader(const string& readerId, const string& name, int borrowCount) {
this->readerId = readerId;
this->name = name;
this->borrowCount = borrowCount;
}
// 基类方法实现
void Reader::setName(const string& name) { this->name = name; }
void Reader::BorrowCountAdd() { borrowCount++; }
void Reader::BorrowCountSub() { borrowCount--; }
string Reader::getReaderId() const { return readerId; }
string Reader::getName() const { return name; }
int Reader::getBorrowCount() const { return borrowCount; }
Reader::~Reader() {}
// 学生读者实现
StudentReader::StudentReader(const string& readerId, const string& name, int borrowCount, const string& studentId)
: Reader(readerId, name, borrowCount), studentId(studentId) {}
int { ; }
{
cout << << () << endl;
cout << << () << endl;
cout << << endl;
cout << << studentId << endl;
cout << << () << << endl;
}
{ studentId; }
TeacherReader::( string& readerId, string& name, borrowCount, string& teacherId)
: (readerId, name, borrowCount), (teacherId) {}
{ ; }
{
cout << << () << endl;
cout << << () << endl;
cout << << endl;
cout << << teacherId << endl;
cout << << () << << endl;
}
{ teacherId; }
功能说明:记录图书借阅的核心信息(图书编号、读者编号、借阅日期、归还日期),支持'未归还'状态的标记。
核心代码(BorrowRecord.h):
#pragma once
#include "Utils.h"
class BorrowRecord {
public:
// 构造函数(未归还/已归还)
BorrowRecord(const string &bookId, const string &readerId, const string &borrowDate);
BorrowRecord(const string &bookId, const string &readerId, const string &borrowDate, const string &returnDate);
void setReturnDate(string returnDate); // 设置归还日期
// get 方法
string getBookId() const;
string getReaderId() const;
string getBorrowDate() const;
string getReturnDate() const;
private:
string bookId;
string readerId;
string borrowDate;
string returnDate; // 空字符串表示未归还
};
核心代码(BorrowRecord.cpp):
#include "BorrowRecord.h"
// 未归还记录构造
BorrowRecord::BorrowRecord(const string& bookId, const string& readerId, const string& borrowDate) {
this->bookId = bookId;
this->readerId = readerId;
this->borrowDate = borrowDate;
this->returnDate = "";
}
// 已归还记录构造
BorrowRecord::BorrowRecord(const string& bookId, const string& readerId, const string& borrowDate, const string& returnDate) {
this->bookId = bookId;
this->readerId = readerId;
this->borrowDate = borrowDate;
this->returnDate = returnDate;
}
// 设置归还日期
void BorrowRecord::setReturnDate(string returnDate) { this->returnDate = returnDate; }
// get 方法实现
string BorrowRecord::getBookId() const { return bookId; }
string BorrowRecord::getReaderId() const { return readerId; }
string BorrowRecord::getBorrowDate() const { return borrowDate; }
string BorrowRecord::getReturnDate() const { return returnDate; }
功能说明:封装控制台常用工具方法(清屏、暂停、输入缓冲区清理、获取当前日期),提升交互体验。
核心代码(Utils.h):
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <limits>
#include <ctime>
#include <map>
#include <sstream>
using namespace std;
class Utils {
public:
// 清空输入缓冲区(解决 cin+getline 输入异常)
static void clearInputBuffer();
// 暂停控制台(按任意键继续)
static void pauseConsole();
// 获取当前日期(格式:YYYY-MM-DD)
static string getCurrentDate();
};
核心代码(Utils.cpp):
#include "Utils.h"
// 清空输入缓冲区
void Utils::clearInputBuffer() {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// 暂停控制台
void Utils::pauseConsole() {
cout << "\n按任意键继续...";
cin.get();
}
// 获取当前日期
string Utils::getCurrentDate() {
time_t now = time(0);
tm* ltm = localtime(&now);
char date[20];
sprintf(date, "%04d-%02d-%02d", 1900 + ltm->tm_year, 1 + ltm->tm_mon, ltm->tm_mday);
return string(date);
}
LibrarySystem 是系统的'大脑',整合了图书管理、读者管理、借阅归还、数据持久化四大核心功能,通过 STL 容器实现高效数据操作。
头文件(LibrarySystem.h)
#pragma once
#include "Utils.h"
#include "Book.h"
#include "Reader.h"
#include "BorrowRecord.h"
#include <vector>
#include <map>
#include <fstream>
#include <algorithm>
class LibrarySystem {
public:
LibrarySystem();
~LibrarySystem();
// 图书管理
void addBook(Book* book);
Book* findBook(string id);
void deleteBook(const string& bookId);
void showAllBooks() const;
// 读者管理
void addReader(Reader* reader);
Reader* findReader(string readerId);
void deleteReader;
;
;
;
;
;
;
;
;
;
;
:
vector<Book*> books;
map<string, Book*> bookMap;
vector<Reader*> readers;
map<string, Reader*> readerMap;
vector<BorrowRecord> borrowRecords;
;
};
;
实现文件(LibrarySystem.cpp)
#include "LibrarySystem.h"
LibrarySystem::LibrarySystem() {
loadData();
}
LibrarySystem::~LibrarySystem() {}
//========= 图书管理相关========//
// 添加图书
void LibrarySystem::addBook(Book* book) {
if (findBook(book->getBookId()) != nullptr) {
cout << "图书编号 [" << book->getBookId() << "] 已存在,添加失败" << endl;
return;
}
books.push_back(book);
bookMap[book->getBookId()] = book;
cout << "图书《" << book->getBookName() << "》添加成功!" << endl;
}
// 按编号查找图书
Book* LibrarySystem::findBook(string id) {
auto it = bookMap.find(id);
if (it != bookMap.end()) {
return it->second;
}
return nullptr;
}
// 删除图书
void LibrarySystem::deleteBook(const string& bookId) {
Book* book = findBook(bookId);
if (book == nullptr) {
cout << "图书编号 [" << bookId << "] 不存在,删除失败!" << endl;
return;
}
if (book->()) {
cout << << book->() << << endl;
;
}
books.((books.(), books.(), book), books.());
bookMap.(bookId);
book;
cout << << bookId << << endl;
}
{
(books.()) {
cout << << endl;
;
}
cout << << endl;
index = ;
(Book* book : books) {
cout << index << ;
book->();
index++;
cout << endl;
}
cout << << endl;
}
{
((reader->()) != ) {
cout << << reader->() << << endl;
;
}
readers.(reader);
readerMap[reader->()] = reader;
cout << << reader->() << << endl;
}
{
it = readerMap.(readerId);
(it != readerMap.()) {
it->second;
}
;
}
{
Reader* reader = (readerId);
(reader == ) {
cout << << readerId << << endl;
;
}
(reader->() > ) {
cout << << reader->() << << endl;
;
}
readers.((readers.(), readers.(), reader), readers.());
readerMap.(readerId);
reader;
cout << << readerId << << endl;
}
{
(readers.()) {
cout << << endl;
;
}
cout << << endl;
index = ;
(Reader* reader : readers) {
cout << index << ;
reader->();
index++;
cout << endl;
}
cout << << endl;
}
{
Book* book = (bookId);
Reader* reader = (readerId);
(book == || reader == ) {
;
}
(book->()) {
;
}
(reader->() >= reader->()) {
;
}
book->();
reader->();
string date = Utils::();
borrowRecords.(bookId, readerId, date);
;
}
{
Book* book = (bookId);
(book == ) {
;
}
(!book->()) {
;
}
string date = Utils::();
(& record : borrowRecords) {
(record.() == bookId && record.().()) {
record.(date);
Reader* reader = (record.());
(reader) reader->();
book->();
;
}
}
;
}
{
hasRecord = ;
cout << << readerId << << endl;
(& record : borrowRecords) {
(record.() == readerId) {
cout << << record.() << << record.() <<
<< (record.().() ? : + record.() + ) << endl;
hasRecord = ;
}
}
(!hasRecord) {
cout << << endl;
}
}
{
;
(Book* book : books) {
NovelBook* novel = <NovelBook*>(book);
TextBook* text = <TextBook*>(book);
(novel) {
bookFile << novel->() << << novel->() << << novel->() <<
<< novel->() << << (novel->() ? : ) << endl;
} (text) {
bookFile << text->() << << text->() << << text->() <<
<< text->() << << (text->() ? : ) << endl;
}
}
bookFile.();
;
(Reader* reader : readers) {
StudentReader* stu = <StudentReader*>(reader);
TeacherReader* tea = <TeacherReader*>(reader);
(stu) {
readerFile << stu->() << << stu->() << << stu->() << << stu->() << endl;
} (tea) {
readerFile << tea->() << << tea->() << << tea->() << << tea->() << endl;
}
}
readerFile.();
;
(& record : borrowRecords) {
recordFile << record.() << << record.() << << record.() << << record.() << endl;
}
recordFile.();
}
{
cout << << endl;
;
(bookFile.()) {
string line;
((bookFile, line)) {
(line.()) ;
vector<string> fields = (line, );
(fields.() != ) ;
string bookId = fields[];
string bookName = fields[];
string author = fields[];
string typeField = fields[];
isBorrowed = (fields[] == );
(typeField == || typeField == || typeField == ) {
books.( (bookId, bookName, author, isBorrowed, typeField));
bookMap[bookId] = books.();
} {
books.( (bookId, bookName, author, isBorrowed, typeField));
bookMap[bookId] = books.();
}
}
bookFile.();
cout << << books.() << << endl;
} {
cout << << endl;
}
;
(readerFile.()) {
string line;
((readerFile, line)) {
(line.()) ;
vector<string> fields = (line, );
(fields.() != ) ;
string readerId = fields[];
string name = fields[];
string idField = fields[];
borrowCount = (fields[]);
(idField.() == string::npos) {
readers.( (readerId, name, borrowCount, idField));
readerMap[readerId] = readers.();
} {
readers.( (readerId, name, borrowCount, idField));
readerMap[readerId] = readers.();
}
}
readerFile.();
cout << << readers.() << << endl;
} {
cout << << endl;
}
;
(recordFile.()) {
string line;
((recordFile, line)) {
(line.()) ;
vector<string> fields = (line, );
(fields.() != && fields.() != ) ;
string bookId = fields[];
string readerId = fields[];
string borrowDate = fields[];
string returnDate = ;
(fields.() == ) {
returnDate = fields[];
}
((bookId) != && (readerId) != ) {
borrowRecords.(bookId, readerId, borrowDate, returnDate);
}
}
recordFile.();
cout << << borrowRecords.() << << endl;
} {
cout << << endl;
}
}
{
readers.();
}
{
books.();
}
{
count = ;
(Book* book : books) {
(book->()) count++;
}
count;
}
{
vector<string> result;
;
string field;
((ss, field, delim)) {
result.(field);
}
result;
}
{
totalBooks = sys.books.();
borrowedBooks = sys.();
totalReaders = sys.readers.();
overdueCount = ;
(& record : sys.borrowRecords) {
(record.().()) overdueCount++;
}
cout << << endl;
cout << << endl;
cout << << totalBooks << endl;
cout << << borrowedBooks << << (totalBooks == ? : (borrowedBooks * / totalBooks)) << << endl;
cout << << totalReaders << endl;
cout << << overdueCount << endl;
cout << << endl;
}
代码讲解:
vector 用于遍历展示,map 用于快速查找,兼顾易用性和效率;dynamic_cast 判断派生类类型,保证数据准确性;getReaderCount/getBookCount 返回值写反,已修正。采用分层菜单设计(主菜单→子菜单),支持图书管理、读者管理、借阅归还、系统管理四大模块,输入校验避免非法操作。
核心代码(main.cpp):
#include "LibrarySystem.h"
#include "Utils.h"
// ==================== 图书管理子菜单=====================
void bookManageMenu(LibrarySystem& sys) {
int choice = 0;
string bookId, bookName, author, novelType, grade;
Book* book = nullptr;
while (true) {
system("cls");
cout << "================ 图书管理子菜单 ================" << endl;
cout << "1. 添加小说图书" << endl;
cout << "2. 添加教材图书" << endl;
cout << "3. 修改图书信息" << endl;
cout << "4. 删除图书" << endl;
cout << "5. 按编号查找图书" << endl;
cout << "6. 查看所有图书" << endl;
cout << "0. 返回主菜单" << endl;
cout << "================================================" << endl;
cout << "请输入操作选项:";
while (!(cin >> choice)) {
Utils::clearInputBuffer();
cout << "输入无效!请输入数字:";
}
Utils::clearInputBuffer();
switch (choice) {
case 1: // 添加小说图书
cout << "\n----- 新增小说图书 -----" << endl;
cout << "图书编号:";
getline(cin, bookId);
cout << "图书名称:";
getline(cin, bookName);
cout << "作者:";
getline(cin, author);
cout << ;
(cin, novelType);
sys.( (bookId, bookName, author, , novelType));
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << ;
(cin, bookId);
cout << ;
(cin, bookName);
cout << ;
(cin, author);
cout << ;
(cin, grade);
sys.( (bookId, bookName, author, , grade));
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << ;
(cin, bookId);
book = sys.(bookId);
(!book) {
cout << << endl;
Utils::();
;
}
cout << << book->() << ;
(cin, bookName);
(!bookName.()) book->(bookName);
cout << << book->() << ;
(cin, author);
(!author.()) book->(author);
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << ;
(cin, bookId);
sys.(bookId);
Utils::();
;
:
cout << << endl;
cout << ;
(cin, bookId);
book = sys.(bookId);
(book) {
cout << << endl;
(NovelBook* novel = <NovelBook*>(book)) novel->();
(TextBook* text = <TextBook*>(book)) text->();
} {
cout << << endl;
}
Utils::();
;
:
cout << << endl;
sys.();
Utils::();
;
:
;
:
cout << << endl;
Utils::();
;
}
}
}
{
choice = ;
string readerId, name, studentId, teacherId;
Reader* reader = ;
() {
();
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << ;
(!(cin >> choice)) {
Utils::();
cout << ;
}
Utils::();
(choice) {
:
cout << << endl;
cout << ;
(cin, readerId);
cout << ;
(cin, name);
cout << ;
(cin, studentId);
sys.( (readerId, name, , studentId));
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << ;
(cin, readerId);
cout << ;
(cin, name);
cout << ;
(cin, teacherId);
sys.( (readerId, name, , teacherId));
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << ;
(cin, readerId);
reader = sys.(readerId);
(!reader) {
cout << << endl;
Utils::();
;
}
cout << << reader->() << ;
(cin, name);
(!name.()) reader->(name);
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << ;
(cin, readerId);
sys.(readerId);
Utils::();
;
:
cout << << endl;
cout << ;
(cin, readerId);
reader = sys.(readerId);
(reader) {
cout << << endl;
(StudentReader* stu = <StudentReader*>(reader)) stu->();
(TeacherReader* tea = <TeacherReader*>(reader)) tea->();
} {
cout << << endl;
}
Utils::();
;
:
cout << << endl;
sys.();
Utils::();
;
:
;
:
cout << << endl;
Utils::();
;
}
}
}
{
choice = ;
string bookId, readerId;
Book* book = ;
Reader* reader = ;
() {
();
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << ;
(!(cin >> choice)) {
Utils::();
cout << ;
}
Utils::();
(choice) {
:
cout << << endl;
cout << ;
(cin, bookId);
cout << ;
(cin, readerId);
(sys.(bookId, readerId)) {
cout << << Utils::() << endl;
} {
cout << << endl;
}
Utils::();
;
:
cout << << endl;
cout << ;
(cin, bookId);
(sys.(bookId)) {
cout << << Utils::() << endl;
} {
cout << << endl;
}
Utils::();
;
:
cout << << endl;
cout << ;
(cin, readerId);
sys.(readerId);
Utils::();
;
:
cout << << endl;
cout << ;
(cin, bookId);
book = sys.(bookId);
(book) {
cout << << book->() << << (book->() ? : ) << endl;
} {
cout << << endl;
}
Utils::();
;
:
;
:
cout << << endl;
Utils::();
;
}
}
}
{
choice = ;
() {
();
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << ;
(!(cin >> choice)) {
Utils::();
cout << ;
}
Utils::();
(choice) {
:
cout << << endl;
(sys);
Utils::();
;
:
sys.();
cout << << endl;
Utils::();
;
:
cout << << endl;
cout << << sys.() << endl;
cout << << sys.() << endl;
cout << << sys.() << endl;
Utils::();
;
:
;
:
cout << << endl;
Utils::();
;
}
}
}
{
LibrarySystem sys;
choice = ;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
Utils::();
() {
();
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << << endl;
cout << ;
(!(cin >> choice)) {
Utils::();
cout << ;
}
Utils::();
(choice) {
:
(sys);
;
:
(sys);
;
:
(sys);
;
:
(sys);
;
:
sys.();
cout << << endl;
;
:
cout << << endl;
Utils::();
;
}
}
}
代码讲解:
while(!(cin >> choice)) 拦截非数字输入,clearInputBuffer 解决输入缓冲区异常;system("cls") 清屏、pauseConsole 暂停,提升用户体验;saveData,保证数据不丢失。本项目为期末大作业基础版本,后续可扩展:
unique_ptr 替代裸指针,避免内存泄漏。通过本次图书管理系统的开发,深入理解了 C++ 面向对象的核心思想(封装、继承、多态),掌握了 STL 容器的场景化应用和文件 IO 的实际操作。开发过程中踩过不少坑:比如输入缓冲区异常导致的交互 bug、派生类类型判断错误、文件路径书写错误等,通过调试和查阅资料逐一解决,不仅提升了代码能力,也培养了'容错思维'——好的程序不仅要实现功能,还要能处理各种异常情况。这份系统完全满足 C++ 期末大作业的要求,也让我体会到'工程化代码'的重要性:规范的文件结构、清晰的注释、完善的容错逻辑,能大幅提升代码的可维护性。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online