#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 18:38:03 浏览: 146
可以将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;
}
}
}
```
定义一个学生类, 包含学生的学号、 姓名和年龄等基本信息, 将学生信息写入二进 制文件 student. dat 中, 实现对学生信息的显示、 查询和删除等基本功能。用c++
下面是一个示例的学生类定义和实现:
```cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX_NAME_LEN = 20;
struct Student {
int id;
char name[MAX_NAME_LEN];
int age;
};
class StudentManager {
public:
void addStudent(int id, const char* name, int age);
void displayAllStudents();
void findStudentById(int id);
void removeStudentById(int id);
private:
fstream file;
void writeStudentToFile(const Student& student);
Student readStudentFromFile();
bool matchById(const Student& student, int id);
};
void StudentManager::addStudent(int id, const char* name, int age) {
Student student = {id};
strncpy(student.name, name, MAX_NAME_LEN - 1);
student.age = age;
writeStudentToFile(student);
}
void StudentManager::displayAllStudents() {
file.open("student.dat", ios::in | ios::binary);
Student student;
while (file.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
cout << "id: " << student.id << ", name: " << student.name << ", age: " << student.age << endl;
}
file.close();
}
void StudentManager::findStudentById(int id) {
file.open("student.dat", ios::in | ios::binary);
Student student;
bool found = false;
while (file.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
if (matchById(student, id)) {
cout << "id: " << student.id << ", name: " << student.name << ", age: " << student.age << endl;
found = true;
break;
}
}
if (!found) {
cout << "Student with id " << id << " not found" << endl;
}
file.close();
}
void StudentManager::removeStudentById(int id) {
file.open("student.dat", ios::in | ios::out | ios::binary);
Student student;
int pos = 0;
while (file.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
if (matchById(student, id)) {
file.seekp(pos);
Student emptyStudent = {0};
file.write(reinterpret_cast<char*>(&emptyStudent), sizeof(Student));
cout << "Student with id " << id << " removed" << endl;
break;
}
pos = file.tellg();
}
if (file.eof()) {
cout << "Student with id " << id << " not found" << endl;
}
file.close();
}
void StudentManager::writeStudentToFile(const Student& student) {
file.open("student.dat", ios::out | ios::app | ios::binary);
file.write(reinterpret_cast<const char*>(&student), sizeof(Student));
file.close();
}
Student StudentManager::readStudentFromFile() {
Student student;
file.read(reinterpret_cast<char*>(&student), sizeof(Student));
return student;
}
bool StudentManager::matchById(const Student& student, int id) {
return student.id == id;
}
```
在这个实现中,我们使用了 `fstream` 类来读写二进制文件。具体来说,对于写入操作,我们使用了 `ios::out` 和 `ios::app` 模式打开文件,以便在文件末尾添加新的学生信息;对于读取操作,我们使用了 `ios::in` 模式打开文件。在读写操作中,我们使用了 `reinterpret_cast` 来进行指针类型的转换,以便正确地读写二进制数据。
另外,我们还实现了一些基本的功能,比如添加学生、显示所有学生、按照学号查找学生和按照学号删除学生。在查找和删除操作中,我们使用了一个辅助函数 `matchById` 来判断学生信息是否与给定的学号匹配。
最后,我们可以在 `main` 函数中测试我们的实现:
```cpp
int main() {
StudentManager manager;
manager.addStudent(1, "Alice", 20);
manager.addStudent(2, "Bob", 21);
manager.addStudent(3, "Charlie", 22);
cout << "All students:" << endl;
manager.displayAllStudents();
cout << "Find student with id 2:" << endl;
manager.findStudentById(2);
cout << "Remove student with id 3:" << endl;
manager.removeStudentById(3);
cout << "All students after removal:" << endl;
manager.displayAllStudents();
return 0;
}
```
输出结果应该为:
```
All students:
id: 1, name: Alice, age: 20
id: 2, name: Bob, age: 21
id: 3, name: Charlie, age: 22
Find student with id 2:
id: 2, name: Bob, age: 21
Remove student with id 3:
Student with id 3 removed
All students after removal:
id: 1, name: Alice, age: 20
id: 2, name: Bob, age: 21
```
阅读全文