用c++图书馆借书的两类人的信息,包括学生读者、教师读者。输出每位读者信息,包括卡号,姓名、单位、已借阅数量、已借阅记录。
时间: 2024-05-13 11:17:01 浏览: 17
下面是一个简单的C++代码示例,实现了学生读者和教师读者的信息管理功能:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 定义读者类
class Reader {
public:
Reader(int id, string name, string unit) : m_id(id), m_name(name), m_unit(unit), m_borrowed(0) {}
// 借书
void borrowBook(string bookName) {
m_borrowed++;
m_borrowedBooks.push_back(bookName);
}
// 打印读者信息
void printInfo() {
cout << "卡号:" << m_id << endl;
cout << "姓名:" << m_name << endl;
cout << "单位:" << m_unit << endl;
cout << "已借阅数量:" << m_borrowed << endl;
if (m_borrowed > 0) {
cout << "已借阅记录:" << endl;
for (int i = 0; i < m_borrowedBooks.size(); i++) {
cout << m_borrowedBooks[i] << endl;
}
}
cout << endl;
}
protected:
int m_id; // 卡号
string m_name; // 姓名
string m_unit; // 单位
int m_borrowed; // 已借阅数量
vector<string> m_borrowedBooks; // 已借阅记录
};
// 学生读者类
class StudentReader : public Reader {
public:
StudentReader(int id, string name, string unit, int grade) : Reader(id, name, unit), m_grade(grade) {}
// 打印学生读者信息
void printInfo() {
cout << "*** 学生读者 ***" << endl;
Reader::printInfo();
cout << "年级:" << m_grade << endl;
cout << endl;
}
protected:
int m_grade; // 年级
};
// 教师读者类
class TeacherReader : public Reader {
public:
TeacherReader(int id, string name, string unit, string title) : Reader(id, name, unit), m_title(title) {}
// 打印教师读者信息
void printInfo() {
cout << "*** 教师读者 ***" << endl;
Reader::printInfo();
cout << "职称:" << m_title << endl;
cout << endl;
}
protected:
string m_title; // 职称
};
int main() {
// 创建学生读者和教师读者对象
StudentReader s1(1001, "张三", "计算机科学与技术", 2);
TeacherReader t1(2001, "李四", "软件工程", "副教授");
// 借书
s1.borrowBook("《C++ Primer》");
s1.borrowBook("《算法导论》");
t1.borrowBook("《软件工程》");
// 打印读者信息
s1.printInfo();
t1.printInfo();
return 0;
}
```
这个示例中,我们定义了一个 `Reader` 类作为基类,然后派生出 `StudentReader` 和 `TeacherReader` 类分别表示学生读者和教师读者。每个读者对象都有自己的卡号、姓名、单位、已借阅数量和已借阅记录。在 `Reader` 类中,我们定义了借书和打印读者信息的方法。在 `StudentReader` 和 `TeacherReader` 类中,我们通过继承 `Reader` 类并添加自己的成员变量和打印信息的方法来实现了不同类型读者的信息管理。
在 `main` 函数中,我们创建了一个学生读者对象和一个教师读者对象,并分别借了一些书。然后打印了他们的信息。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)