For a school assignment I have to create a C++ program that will create and manage a binary flat file database containing students and undergrad students. The undergrad students are basically the same as normal students, but with some added fields. A given hint is that we could use templates.
My problem is in structuring this design. I’ve tried the following:
- Creating a
Personclass containingstringvariables with astudentand anundergradclass that inherit from it and add more variables for fields. This works but it becomes very hard to add more types of people later on, so I tried: - Creating a
Personclass that contains amap<string, string>from whichUndergradandStudentinherit, and adatabaseclass containing avector<Person>to which those are added. In thePerson::map<string, string>I added the different fields and their values, like name, address etc.
I’m not sure if I’m going the right way with this. Is there a standard design pattern that’s used for things like this?
The correct approach depends more on what you need the system to do. Your first approach is more standard c++ architecture if you know the system will be specialized around the requirements you’ve listed and you want better runtime performance.
The map way would allow you to implement a more generalized approach, but at a cost of clarity, specialized accessors, and more so I’m guessing it is not the approach your teacher is seeking.
If you know all the requirements of the system ( what kind of queries will be made, what is it expected to support, etc ) it will better inform your decision, so you should get clarity on that for sure.
Good luck!