
C++ 课程设计是提升编程能力的重要实践环节。本文带来三个有趣又实用的项目,每个项目都整合了 C++ 核心知识点,且所有代码都合并到单个 main.cpp 中,可直接编译运行,方便大家动手操作。
这个图书管理系统就像一个智能图书管家,能实现图书添加、查询、借阅和归还等功能,让你在实践中掌握类与对象、容器等知识。

@startuml
class Book {
- string title
- string author
- string isbn
- bool isBorrowed
+ Book(string t, string a, string i)
+ string getTitle()
+ string getAuthor()
+ string getIsbn()
+ bool getIsBorrowed()
+ void setBorrowed(bool status)
}
class Reader {
- string name
- int readerId
- vector<Book*> borrowedBooks
+ Reader(string n, int id)
+ string getName()
+ int getReaderId()
+ vector<Book*> getBorrowedBooks()
+ void borrowBook(Book* book)
+ void returnBook(Book* book)
}
class Library {
- vector<Book*> books
- vector<Reader*> readers
+ void addBook(Book* book)
+ void addReader(Reader* reader)
+ Book* findBookByIsbn(string isbn)
+ vector<Book*> findBooksByTitle(string title)
+ vector<Book*> findBooksByAuthor(string author)
+ bool borrowBook(int readerId, string isbn)
+ bool returnBook(int readerId, string isbn)
+ void displayAllBooks()
}
Library "1" -- "*" Book : contains
Library "1" -- "*" Reader : manages
Reader "1" -- "*" Book : borrows
@enduml

#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Book类
class Book {
private:
string title; // 书名
string author; // 作者
string isbn; // ISBN号
bool isBorrowed; // 是否被借阅
public:
// 构造函数
Book(string t, string a, string i) : title(t), author(a), isbn(i), isBorrowed(false) {}
// 获取书名
string getTitle() { return title; }
// 获取作者
string getAuthor() { return author; }
// 获取ISBN号
string getIsbn() { return isbn; }
// 获取借阅状态
bool getIsBorrowed() { return isBorrowed; }
// 设置借阅状态
void setBorrowed(bool status) { isBorrowed = status; }
};
// Reader类
class Reader {
private:
string name; // 读者姓名
int readerId; // 读者ID
vector<Book*> borrowedBooks; // 借阅的图书列表
public:
// 构造函数
Reader(string n, int id) : name(n), readerId(id) {}
// 获取读者姓名
string getName() { return name; }
// 获取读者ID
int getReaderId() { return readerId; }
// 获取借阅的图书列表
vector<Book*> getBorrowedBooks() { return borrowedBooks; }
// 借阅图书
void borrowBook(Book* book) {
borrowedBooks.push_back(book);
book->setBorrowed(true);
}
// 归还图书
void returnBook(Book* book) {
for (auto it = borrowedBooks.begin(); it != borrowedBooks.end(); ++it) {
if (*it == book) {
borrowedBooks.erase(it);
book->setBorrowed(false);
break;
}
}
}
};
// Library类
class Library {
private:
vector<Book*> books; // 图书列表
vector<Reader*> readers; // 读者列表
public:
// 添加图书
void addBook(Book* book) {
books.push_back(book);
}
// 添加读者
void addReader(Reader* reader) {
readers.push_back(reader);
}
// 根据ISBN查找图书
Book* findBookByIsbn(string isbn) {
for (auto book : books) {
if (book->getIsbn() == isbn) {
return book;
}
}
return nullptr;
}
// 借阅图书
bool borrowBook(int readerId, string isbn) {
Reader* reader = nullptr;
for (auto r : readers) {
if (r->getReaderId() == readerId) {
reader = r;
break;
}
}
if (!reader) {
return false;
}
Book* book = findBookByIsbn(isbn);
if (!book || book->getIsBorrowed()) {
return false;
}
reader->borrowBook(book);
return true;
}
// 归还图书
bool returnBook(int readerId, string isbn) {
Reader* reader = nullptr;
for (auto r : readers) {
if (r->getReaderId() == readerId) {
reader = r;
break;
}
}
if (!reader) {
return false;
}
Book* book = findBookByIsbn(isbn);
if (!book) {
return false;
}
reader->returnBook(book);
return true;
}
// 显示所有图书
void displayAllBooks() {
cout << "图书馆所有图书:" << endl;
for (auto book : books) {
cout << "书名:" << book->getTitle() << ",作者:" << book->getAuthor()
<< ",ISBN:" << book->getIsbn() << ",状态:"
<< (book->getIsBorrowed() ? "已借出" : "可借阅") << endl;
}
}
};
// 主函数
int main() {
// 创建图书馆
Library library;
// 添加图书
Book* book1 = new Book("C++ Primer", "Stanley B. Lippman", "9787115273262");
Book* book2 = new Book("Effective C++", "Scott Meyers", "9787115179384");
library.addBook(book1);
library.addBook(book2);
// 添加读者
Reader* reader1 = new Reader("张三", 1001);
library.addReader(reader1);
// 显示所有图书
library.displayAllBooks();
// 借阅图书
bool borrowSuccess = library.borrowBook(1001, "9787115273262");
if (borrowSuccess) {
cout << "借阅成功!" << endl;
} else {
cout << "借阅失败!" << endl;
}
// 显示所有图书
library.displayAllBooks();
// 归还图书
bool returnSuccess = library.returnBook(1001, "9787115273262");
if (returnSuccess) {
cout << "归还成功!" << endl;
} else {
cout << "归还失败!" << endl;
}
// 显示所有图书
library.displayAllBooks();
// 释放资源
delete book1;
delete book2;
delete reader1;
return 0;
}

该系统能帮助老师高效录入、查询和统计学生成绩,综合运用了容器、字符串操作等知识点,让成绩管理更轻松。
@startuml
class Student {
- string name
- int studentId
- map<string, double> scores
+ Student(string n, int id)
+ string getName()
+ int getStudentId()
+ void addScore(string course, double score)
+ double getScore(string course)
+ map<string, double> getAllScores()
}
class ScoreManager {
- vector<Student*> students
+ void addStudent(Student* student)
+ Student* findStudentById(int id)
+ void inputScores()
+ void queryScores(int studentId)
+ void statisticsScores(string course)
+ void displayAllStudents()
}
ScoreManager "1" -- "*" Student : manages
@enduml

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
// Student类
class Student {
private:
string name; // 学生姓名
int studentId; // 学号
map<string, double> scores; // 各科成绩
public:
// 构造函数
Student(string n, int id) : name(n), studentId(id) {}
// 获取学生姓名
string getName() { return name; }
// 获取学号
int getStudentId() { return studentId; }
// 添加成绩
void addScore(string course, double score) {
scores[course] = score;
}
// 获取某科成绩
double getScore(string course) {
if (scores.find(course) != scores.end()) {
return scores[course];
}
return -1; // 表示未找到该课程成绩
}
// 获取所有成绩
map<string, double> getAllScores() { return scores; }
};
// ScoreManager类
class ScoreManager {
private:
vector<Student*> students; // 学生列表
public:
// 添加学生
void addStudent(Student* student) {
students.push_back(student);
}
// 根据学号查找学生
Student* findStudentById(int id) {
for (auto student : students) {
if (student->getStudentId() == id) {
return student;
}
}
return nullptr;
}
// 录入成绩
void inputScores() {
int studentId;
cout << "请输入学生ID:";
cin >> studentId;
Student* student = findStudentById(studentId);
if (!student) {
cout << "学生不存在!" << endl;
return;
}
string course;
double score;
char continueInput;
do {
cout << "请输入课程名称:";
cin >> course;
cout << "请输入成绩:";
cin >> score;
student->addScore(course, score);
cout << "是否继续录入成绩(y/n):";
cin >> continueInput;
} while (continueInput == 'y' || continueInput == 'Y');
cout << "成绩录入完成!" << endl;
}
// 查询成绩
void queryScores(int studentId) {
Student* student = findStudentById(studentId);
if (!student) {
cout << "学生不存在!" << endl;
return;
}
cout << student->getName() << "(学号:" << student->getStudentId() << ")的成绩如下:" << endl;
map<string, double> scores = student->getAllScores();
if (scores.empty()) {
cout << "该学生暂无成绩记录!" << endl;
return;
}
for (auto& pair : scores) {
cout << pair.first << ":" << pair.second << endl;
}
}
// 统计成绩(计算某课程平均分)
void statisticsScores(string course) {
int count = 0;
double total = 0.0;
for (auto student : students) {
double score = student->getScore(course);
if (score != -1) {
total += score;
count++;
}
}
if (count == 0) {
cout << "没有该课程的成绩记录!" << endl;
return;
}
cout << course << "课程的平均分是:" << total / count << endl;
}
// 显示所有学生
void displayAllStudents() {
cout << "所有学生信息:" << endl;
for (auto student : students) {
cout << "姓名:" << student->getName() << ",学号:" << student->getStudentId() << endl;
}
}
};
// 主函数
int main() {
// 创建成绩管理器
ScoreManager scoreManager;
// 添加学生
Student* student1 = new Student("李四", 2001);
Student* student2 = new Student("王五", 2002);
scoreManager.addStudent(student1);
scoreManager.addStudent(student2);
// 显示所有学生
scoreManager.displayAllStudents();
// 录入成绩
scoreManager.inputScores();
// 查询成绩
scoreManager.queryScores(2001);
// 统计成绩
scoreManager.statisticsScores("数学");
// 释放资源
delete student1;
delete student2;
return 0;
}
这个系统模拟银行账户操作,支持存款、取款、查询余额等功能,通过继承实现不同账户类型,还运用异常处理应对错误情况。
@startuml
abstract class Account {
- string accountNumber
- double balance
+ Account(string num, double bal)
+ string getAccountNumber()
+ double getBalance()
+ virtual void deposit(double amount)
+ virtual void withdraw(double amount) throw(exception)
+ virtual void displayBalance()
}
class SavingsAccount extends Account {
- double interestRate
+ SavingsAccount(string num, double bal, double rate)
+ void calculateInterest()
+ void withdraw(double amount) throw(exception)
}
class CreditAccount extends Account {
- double creditLimit
+ CreditAccount(string num, double bal, double limit)
+ void withdraw(double amount) throw(exception)
}
@enduml

#include <iostream>
#include <string>
#include <vector>
#include <exception>
using namespace std;
// 抽象基类Account
class Account {
protected:
string accountNumber; // 账号
double balance; // 余额
public:
// 构造函数
Account(string num, double bal) : accountNumber(num), balance(bal) {}
// 获取账号
string getAccountNumber() { return accountNumber; }
// 获取余额
double getBalance() { return balance; }
// 存款
virtual void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "存款成功!当前余额:" << balance << endl;
} else {
cout << "存款金额必须大于0!" << endl;
}
}
// 取款,纯虚函数
virtual void withdraw(double amount) throw(exception) = 0;
// 显示余额
virtual void displayBalance() {
cout << "账号:" << accountNumber << ",当前余额:" << balance << endl;
}
// 析构函数
virtual ~Account() {}
};
// 储蓄账户类
class SavingsAccount : public Account {
private:
double interestRate; // 利率
public:
// 构造函数
SavingsAccount(string num, double bal, double rate) : Account(num, bal), interestRate(rate) {}
// 计算利息
void calculateInterest() {
double interest = balance * interestRate;
balance += interest;
cout << "计算利息成功!利息:" << interest << ",当前余额:" << balance << endl;
}
// 重写取款方法
void withdraw(double amount) throw(exception) {
if (amount <= 0) {
throw invalid_argument("取款金额必须大于0!");
}
if (amount > balance) {
throw runtime_error("取款金额超过余额!");
}
balance -= amount;
cout << "取款成功!取款金额:" << amount << ",当前余额:" << balance << endl;
}
};
// 信用账户类
class CreditAccount extends Account {
private:
double creditLimit; // 信用额度
public:
// 构造函数
CreditAccount(string num, double bal, double limit) : Account(num, bal), creditLimit(limit) {}
// 重写取款方法
void withdraw(double amount) throw(exception) {
if (amount <= 0) {
throw invalid_argument("取款金额必须大于0!");
}
if (amount > balance + creditLimit) {
throw runtime_error("取款金额超过信用额度!");
}
balance -= amount;
cout << "取款成功!取款金额:" << amount << ",当前余额:" << balance << endl;
}
};
// Bank类
class Bank {
private:
vector<Account*> accounts; // 账户列表
public:
// 添加账户
void addAccount(Account* account) {
accounts.push_back(account);
}
// 根据账号查找账户
Account* findAccount(string accountNumber) {
for (auto account : accounts) {
if (account->getAccountNumber() == accountNumber) {
return account;
}
}
return nullptr;
}
// 显示所有账户
void displayAllAccounts() {
cout << "所有账户信息:" << endl;
for (auto account : accounts) {
account->displayBalance();
}
}
};
// 主函数
int main() {
// 创建银行
Bank bank;
// 添加储蓄账户和信用账户
Account* savingsAccount = new SavingsAccount("6222021234567890123", 10000, 0.02);
Account* creditAccount = new CreditAccount("6222031234567890124", 5000, 3000);
bank.addAccount(savingsAccount);
bank.addAccount(creditAccount);
// 显示所有账户
bank.displayAllAccounts();
// 储蓄账户存款
savingsAccount->deposit(5000);
// 储蓄账户取款
try {
savingsAccount->withdraw(3000);
} catch (exception& e) {
cout << "取款失败:" << e.what() << endl;
}
// 储蓄账户计算利息
dynamic_cast<SavingsAccount*>(savingsAccount)->calculateInterest();
// 信用账户取款
try {
creditAccount->withdraw(6000);
} catch (exception& e) {
cout << "取款失败:" << e.what() << endl;
}
// 显示所有账户
bank.displayAllAccounts();
// 释放资源
delete savingsAccount;
delete creditAccount;
return 0;
}
以上三个项目涵盖了 C++ 的类与对象、继承、多态、容器、异常处理等核心知识点。每个项目的代码都整合在单个 main.cpp 中,大家可以直接复制编译运行。通过这些项目实践,能有效提升 C++ 编程能力,解决实际问题。动手试试吧,遇到问题可以多调试分析哦!