You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++如何将TStudent类对象存入TRegistru类且不修改main函数

C++ TRegistru类实现方案

你的原有设计里TRegistru继承TStudent是不合理的:TRegistru是学生对象的管理容器,和TStudent是组合关系而非继承关系,我们只需要在TRegistru内部维护学生对象的存储结构即可,全程不需要修改你提供的main函数逻辑。

核心实现要点

  • 二进制读写std::string不能直接做内存拷贝,需要先写入字符串长度,再写入字符串实际内容,读取时按相同逻辑解析
  • 所有方法名、参数要和main函数里的调用完全匹配
  • deletion方法仅清空容器内的内存数据,不修改磁盘上的data.bin文件内容

完整可运行代码

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

class TStudent
{
public:
    string Name, Surname;
    int Age;

    // 补充默认构造,方便容器初始化
    TStudent(string name = "", string surname = "", int age = 0)
    {
        Name = name;
        Surname = surname;
        Age = age;
        cout << "\n";
    }
};

class TRegistru
{
private:
    TStudent* student_list; // 存储学生对象的动态数组
    int student_count; // 当前存储的学生数量
public:
    // 匹配main里的初始化逻辑:传入第一个学生对象
    TRegistru(const TStudent& first_stu) {
        student_count = 1;
        student_list = new TStudent[1];
        student_list[0] = first_stu;
    }

    // 匹配add方法:添加新的学生对象
    void add(const TStudent& new_stu) {
        // 扩容动态数组
        TStudent* new_list = new TStudent[student_count + 1];
        for (int i = 0; i < student_count; i++) {
            new_list[i] = student_list[i];
        }
        new_list[student_count] = new_stu;
        delete[] student_list;
        student_list = new_list;
        student_count++;
    }

    // 匹配saving方法:保存数据到二进制文件
    void saving(const char* filename) {
        ofstream ofs(filename, ios::binary);
        // 先写学生总数量
        ofs.write((char*)&student_count, sizeof(student_count));
        // 逐个写入学生信息
        for (int i = 0; i < student_count; i++) {
            // 写入姓名
            int name_len = student_list[i].Name.size();
            ofs.write((char*)&name_len, sizeof(name_len));
            ofs.write(student_list[i].Name.c_str(), name_len);
            // 写入姓氏
            int sur_len = student_list[i].Surname.size();
            ofs.write((char*)&sur_len, sizeof(sur_len));
            ofs.write(student_list[i].Surname.c_str(), sur_len);
            // 写入年龄
            ofs.write((char*)&student_list[i].Age, sizeof(student_list[i].Age));
        }
        ofs.close();
    }

    // 匹配deletion方法:释放TRegistru占用的内存
    void deletion() {
        delete[] student_list;
        student_list = nullptr;
        student_count = 0;
    }

    // 匹配insertion方法:从文件读取数据回写
    void insertion(const char* filename) {
        // 先清空现有内存避免内存泄漏
        deletion();
        ifstream ifs(filename, ios::binary);
        // 读取学生总数量
        ifs.read((char*)&student_count, sizeof(student_count));
        student_list = new TStudent[student_count];
        // 逐个读取学生信息
        for (int i = 0; i < student_count; i++) {
            // 读取姓名
            int name_len;
            ifs.read((char*)&name_len, sizeof(name_len));
            char* name_buf = new char[name_len + 1];
            ifs.read(name_buf, name_len);
            name_buf[name_len] = '\0';
            student_list[i].Name = name_buf;
            delete[] name_buf;
            // 读取姓氏
            int sur_len;
            ifs.read((char*)&sur_len, sizeof(sur_len));
            char* sur_buf = new char[sur_len + 1];
            ifs.read(sur_buf, sur_len);
            sur_buf[sur_len] = '\0';
            student_list[i].Surname = sur_buf;
            delete[] sur_buf;
            // 读取年龄
            ifs.read((char*)&student_list[i].Age, sizeof(student_list[i].Age));
        }
        ifs.close();
    }

    // 匹配introduction方法:打印所有学生数据
    void introduction() {
        cout << "当前登记的学生信息如下:" << endl;
        for (int i = 0; i < student_count; i++) {
            cout << "第" << i+1 << "名学生:" << endl;
            cout << "名:" << student_list[i].Name << endl;
            cout << "姓:" << student_list[i].Surname << endl;
            cout << "年龄:" << student_list[i].Age << endl << endl;
        }
    }

    // 析构函数避免内存泄漏
    ~TRegistru() {
        deletion();
    }
};

int main()
{
    TStudent student1("Simion", "Neculae", 21);
    TStudent student2("Elena", "Oprea", 21);
    TRegistru registru(student1);//初始化对象
    registru.add(student2);//向registru添加另一个学生对象
    registru.saving("data.bin");//将数据保存到文件(原有代码漏了分号,补充即可)
    registru.deletion();//释放TRegistru占用的内存
    registru.insertion("data.bin");//从文件读取数据回写
    registru.introduction();//打印数据

    return 0;
}

内容的提问来源于stack exchange,提问作者Vlad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 18:06:02