#include"Student.h" #include"StudentManager.h" #include<iostream> #include<fstream> #include<string> StudentManager::StudentManager(Student* s, int n) { this->n = n; student = new Student[n]; for (int i = 0; i < n; i++) { *(student + i) = *(s + i); } } void StudentManager::save() { ofstream outfile("StudentInfor.dat",ios_base::out|ios_base::binary); if (!outfile) { cout << "Failed to open file!" << endl; } for (int i = 0; i < n; i++) { outfile.write(reinterpret_cast<char*>(student + i), sizeof(*(student + i))); } outfile.close(); } void StudentManager::read() { delete[]student; student = new Student[n]; ifstream infile("StudentInfor.dat", ios_base::in | ios_base::binary); if (infile) { for (int i = 0; i < n; i++) { infile.read(reinterpret_cast<char*>(student + i), sizeof(*(student + i))); } } else { cout << "Failed to open file!" << endl; } infile.close(); for (int i = 0; i < n; i++) { cout << (student + i)->getNum() << "," << (student + i)->getName() << "," << (student + i)->getGender() << "," << (student + i)->getMath() << "," << (student + i)->getEnglish() << "," << (student + i)->getComputer() << "," << (student + i)->getAver() << endl; } }如何改进能避免读取访问权限冲突问题
时间: 2023-12-14 14:38:03 浏览: 152
可以将Student类中的私有成员变量改为protected类型,这样StudentManager类就可以直接访问这些成员变量,避免了访问权限冲突问题。同时,也可以将读取文件部分的代码封装到Student类中,让Student类自己负责从文件读取数据,这样也可以避免访问权限冲突问题。例如,在Student类中添加如下代码:
```
void readFromFile(ifstream& infile) {
infile.read(reinterpret_cast<char*>(this), sizeof(*this));
}
```
然后,在StudentManager类的read()函数中调用这个函数:
```
if (infile) {
for (int i = 0; i < n; i++) {
(student + i)->readFromFile(infile);
}
}
```
相关问题
用c++设计学生信息管理系统c++学生成绩管理系统有若干学生成绩记录组成,每个记录由学号、姓名、性别和成绩四部分组成,学号不超过11个字符,姓名不超过20个字符,性别为’m’(male)或’f’(female),成绩为double型数据。该系统具有以下功能: ⑴程序开始执行时显示系统首界面及版权信息后,显示以下主菜单: *******学生成绩管理系统************* 1.添加学生成绩记录 2.显示学生成绩记录 3.查找学生成绩记录 4.删除学生成绩记录 5.保存学生成绩
记录 6.读取学生成绩记录 7.统计学生成绩信息 0.退出系统 ************************************ ⑵ 添加学生成绩记录:输入学生信息,程序自动生成唯一的学号,将学生信息添加到系统中。
⑶ 显示学生成绩记录:按学生学号、姓名、性别、成绩等方式查询并显示学生成绩记录。
⑷ 查找学生成绩记录:根据学号查询并显示学生信息。
⑸ 删除学生成绩记录:根据学号删除学生信息。
⑹ 保存学生成绩记录:将当前系统中学生信息保存到文件中。
⑺ 读取学生成绩记录:从文件中读取学生信息。
⑻ 统计学生成绩信息:统计系统中所有学生的总人数、平均成绩、最高分、最低分等信息。
⑼ 退出系统:程序结束执行。
以下是一个简单的 C++ 实现示例,仅供参考:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生信息结构体
struct Student {
string id; // 学号
string name; // 姓名
char gender; // 性别
double score; // 成绩
};
// 学生信息管理类
class StudentManager {
private:
vector<Student> students; // 学生信息列表
int count; // 学生总人数
double avgScore; // 平均成绩
double maxScore; // 最高分
double minScore; // 最低分
public:
StudentManager() {
count = 0;
avgScore = 0;
maxScore = 0;
minScore = 100;
}
// 添加学生信息
void addStudent() {
Student student;
cout << "请输入学生姓名:" << endl;
cin >> student.name;
cout << "请输入学生性别(m/f):" << endl;
cin >> student.gender;
cout << "请输入学生成绩:" << endl;
cin >> student.score;
student.id = "S" + to_string(count + 1); // 自动生成学号
students.push_back(student);
count++;
avgScore = (avgScore * (count - 1) + student.score) / count;
maxScore = max(maxScore, student.score);
minScore = min(minScore, student.score);
cout << "添加成功!" << endl;
}
// 显示学生信息
void showStudents() {
if (students.empty()) {
cout << "暂无学生信息!" << endl;
return;
}
cout << "学号\t姓名\t性别\t成绩" << endl;
for (auto s : students) {
cout << s.id << "\t" << s.name << "\t" << s.gender << "\t" << s.score << endl;
}
}
// 查找学生信息
void searchStudent() {
if (students.empty()) {
cout << "暂无学生信息!" << endl;
return;
}
string id;
cout << "请输入要查找的学生学号:" << endl;
cin >> id;
for (auto s : students) {
if (s.id == id) {
cout << "学号\t姓名\t性别\t成绩" << endl;
cout << s.id << "\t" << s.name << "\t" << s.gender << "\t" << s.score << endl;
return;
}
}
cout << "未找到该学生信息!" << endl;
}
// 删除学生信息
void deleteStudent() {
if (students.empty()) {
cout << "暂无学生信息!" << endl;
return;
}
string id;
cout << "请输入要删除的学生学号:" << endl;
cin >> id;
for (auto it = students.begin(); it != students.end(); it++) {
if (it->id == id) {
avgScore = (avgScore * count - it->score) / (count - 1);
count--;
students.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生信息!" << endl;
}
// 保存学生信息到文件
void saveToFile() {
if (students.empty()) {
cout << "暂无学生信息!" << endl;
return;
}
ofstream outFile("students.txt");
for (auto s : students) {
outFile << s.id << "," << s.name << "," << s.gender << "," << s.score << endl;
}
cout << "保存成功!" << endl;
}
// 从文件中读取学生信息
void readFromFile() {
ifstream inFile("students.txt");
if (!inFile.is_open()) {
cout << "文件不存在!" << endl;
return;
}
string line;
while (getline(inFile, line)) {
Student student;
int pos = line.find(",");
student.id = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find(",");
student.name = line.substr(0, pos);
line = line.substr(pos + 1);
pos = line.find(",");
student.gender = line[0];
line = line.substr(pos + 1);
student.score = stod(line);
students.push_back(student);
count++;
avgScore = (avgScore * (count - 1) + student.score) / count;
maxScore = max(maxScore, student.score);
minScore = min(minScore, student.score);
}
cout << "读取成功!" << endl;
}
// 统计学生信息
void statistics() {
if (students.empty()) {
cout << "暂无学生信息!" << endl;
return;
}
cout << "学生总人数:" << count << endl;
cout << "平均成绩:" << avgScore << endl;
cout << "最高分:" << maxScore << endl;
cout << "最低分:" << minScore << endl;
}
};
int main() {
StudentManager manager;
int choice;
while (true) {
cout << "*******学生成绩管理系统*************" << endl;
cout << "1.添加学生成绩记录" << endl;
cout << "2.显示学生成绩记录" << endl;
cout << "3.查找学生成绩记录" << endl;
cout << "4.删除学生成绩记录" << endl;
cout << "5.保存学生成绩记录" << endl;
cout << "6.读取学生成绩记录" << endl;
cout << "7.统计学生成绩信息" << endl;
cout << "0.退出系统" << endl;
cout << "************************************" << endl;
cout << "请选择操作(0-7):" << endl;
cin >> choice;
switch (choice) {
case 1:
manager.addStudent();
break;
case 2:
manager.showStudents();
break;
case 3:
manager.searchStudent();
break;
case 4:
manager.deleteStudent();
break;
case 5:
manager.saveToFile();
break;
case 6:
manager.readFromFile();
break;
case 7:
manager.statistics();
break;
case 0:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
}
}
}
```
3. 定义一个学生类, 包含学生的学号、 姓名和年龄等基本信息, 将学生信息写入二进 制文件 student. dat 中, 实现对学生信息的显示、 查询和删除等基本功能。
好的,这是一个比较具体的编程问题。下面给出一个简单的 C++ 代码实现:
```cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 学生类
class Student {
public:
Student(int id, const string& name, int age) :
m_id(id), m_name(name), m_age(age) {}
int getId() const { return m_id; }
const string& getName() const { return m_name; }
int getAge() const { return m_age; }
private:
int m_id;
string m_name;
int m_age;
};
// 学生管理类
class StudentManager {
public:
// 添加学生信息到文件
void addStudent(const Student& student) {
ofstream ofs("student.dat", ios::binary | ios::app);
ofs.write(reinterpret_cast<const char*>(&student), sizeof(Student));
ofs.close();
}
// 显示学生信息
void showStudents() {
ifstream ifs("student.dat", ios::binary);
if (!ifs) {
cout << "Failed to open file!" << endl;
return;
}
Student student(0, "", 0);
while (ifs.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
cout << "ID: " << student.getId() << ", Name: " << student.getName() << ", Age: " << student.getAge() << endl;
}
ifs.close();
}
// 查询学生信息
void queryStudent(int id) {
ifstream ifs("student.dat", ios::binary);
if (!ifs) {
cout << "Failed to open file!" << endl;
return;
}
Student student(0, "", 0);
bool found = false;
while (ifs.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
if (student.getId() == id) {
cout << "ID: " << student.getId() << ", Name: " << student.getName() << ", Age: " << student.getAge() << endl;
found = true;
break;
}
}
if (!found) {
cout << "Student not found!" << endl;
}
ifs.close();
}
// 删除学生信息
void deleteStudent(int id) {
ifstream ifs("student.dat", ios::binary);
if (!ifs) {
cout << "Failed to open file!" << endl;
return;
}
ofstream ofs("temp.dat", ios::binary);
Student student(0, "", 0);
bool found = false;
while (ifs.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
if (student.getId() != id) {
ofs.write(reinterpret_cast<const char*>(&student), sizeof(Student));
} else {
found = true;
}
}
ifs.close();
ofs.close();
if (found) {
remove("student.dat");
rename("temp.dat", "student.dat");
cout << "Student deleted!" << endl;
} else {
remove("temp.dat");
cout << "Student not found!" << endl;
}
}
};
int main() {
StudentManager sm;
sm.addStudent(Student(1, "Tom", 18));
sm.addStudent(Student(2, "Jerry", 20));
sm.showStudents();
sm.queryStudent(2);
sm.deleteStudent(1);
sm.showStudents();
return 0;
}
```
上述代码中,`Student` 类表示一个学生对象,包含学生的学号、姓名和年龄等基本信息。`StudentManager` 类表示学生管理器,其中的 `addStudent` 方法可以将一个学生对象写入二进制文件中,`showStudents` 方法可以显示所有学生信息,`queryStudent` 方法可以根据学号查询学生信息,`deleteStudent` 方法可以根据学号删除学生信息。在 `main` 函数中,我们创建一个 `StudentManager` 对象,添加两个学生信息,然后显示所有学生信息,查询学号为 2 的学生信息,最后删除学号为 1 的学生信息。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)