【发布时间】:2021-11-10 09:06:28
【问题描述】:
刚接触 C++ 中的类和对象并尝试学习一些基础知识
我有 TStudent 类,其中存储了 student 的姓名、姓氏和年龄,我还有在 main 中访问并插入数据的构造函数。
我想要做的是:拥有TRegistru 类,我必须在其中添加我的对象数据,以便我可以将其存储在那里,然后我可以将数据保存在data.bin 中并从数据,然后我想把数据放回类中并打印出来。
问题是:以什么方式以及在第二类中添加我的对象的最佳方式是什么,以便我最终可以按照我在 cmets 中描述的方式使用它们,这样我就不会无需更改 main 中的任何内容
到目前为止,这是我的代码:
#include <iostream>
using namespace std;
class TStudent
{
public:
string Name, Surname;
int Age;
TStudent(string name, string surname, int age)
{
Name = name;
Surname = surname;
Age = age;
cout <<"\n";
}
};
class TRegistru : public TStudent
{
public:
Tregistru()
};
int main()
{
TStudent student1("Simion", "Neculae", 21);
TStudent student2("Elena", "Oprea", 21);
TRegistru registru(student1);//initialising the object
registru.add(student2);//adding another one to `registru`
registru.saving("data.bin")//saving the data in a file
registru.deletion();//freeing the TRegistru memory
registru.insertion("data.bin");//inserting the data back it
registru.introduction();//printing it
return 0;
}
【问题讨论】:
-
您想了解序列化。 (这看起来很像“请为我写下所有代码”的问题。期待它被关闭。)
-
参考。 Boost.JSON 和 Boost.Serialization。该建议的通用版本:每当您需要完成标准库中没有的事情时,请先检查Boost。
-
我认为您的
TRegistru课程格式不正确
标签: c++ class constructor destructor