Member Avatar for trebor-pl

Hi,
I am writing a program that allows to keep track of some racing records and to do this I need to be able to save objects on a external file and then load them with next startup of the program. As I am just a beginner I need your help to do so.

I did some research read about persistence and serialization. Kinda know the concept and implementation, however I'm not sure. I dont exactly know how to preserve the pointers and pointees and just pull it off.

I have two classes, driver and team class, I want to save their objects on an external file to load it on the next use. Please help.

driver.h

#ifndef DRIVER.H
#def DRIVER.H
#include "team.h"

class CDriver
{
      private:
              int driver_id;
              char fname[20];
              char lname[20];
              short unsigned int new_point_system;
              short unsigned int old_point_system;
              short unsigned int place_point_system;
              unsigned int driver_championships;
              unsigned int last_racepoints;
              bool hide_driver;
              bool reserve_driver;
              CTeam* pTeam;
              
      public:
             //-------------------------------constructors------------------------------------------------
             CDriver();
             CDriver(int theDriver_id, char theFname[], char theLname[], unsigned int theDriver_championships, bool theReserve_driver); 
                         
             //-------------------------------destructors-------------------------------------------------
             ~Cdriver();
             ~CDriver(int theDriver_id, char theFname[], char theLname[], unsigned int theDriver_championships, bool theReserve_driver);
             
             //-------------------------------methods-----------------------------------------------------             
             void setDriver_id(int theDriver_id);                                //set id number of the driver, id comes assigned 
             int getDriver_id();                                                                  
             
             void setFname(char theFname[]);                                     //sets the first name of the driver
             char* getFname();                                                   //returns the first name of the driver
             
             void setLname(char theLname[]);                                     //sets the last name of the driver
             char* getLname();                                                   //returns the last name of the driver
             
             char* getTeam_name(CTeam* pTeam);                                   //outputs the name of the team
             
             void addNew_system_points(int finished_place);                      //adding points to new base system
             int getNew_system_points();                                         //displaying the points of new base system
             
             void addOld_system_points(int finished_place);                      //adding points to old base system
             int getOld_system_points();                                         //displaying the points of old base system
             
             void addPlace_system_points(int finished_place);                    //adding points to place base system
             int getPlace_system_points();                                       //displaying the points of place based system, function mailnly used for charts, records, etc.   
             
             void setDriver_status(bool theHide_driver);                         //sets if the driver's career is true or false (if still driving or nor)
             bool getDriver_status();                                            //outputs if true or false
             
             void setReserve_status(bool theReserve_driver);                     //sets if the driver's career is true or false (if beeing reserve drivaer or not)
             bool getReserve_status();                                           //outputs if true or false
             
             void addChampionship(bool driver_championship);                     //increments the driver championships count
             int getChampionship();
             
             void setLast_racepoints(int points);                                //sets the value for last race points, used in team class
             int getLast_racepoints();             
};
#endif

team.h

#ifndef TEAM.H
#def TEAM.H
#include "driver.h"

class CTeam
{
      private:
              int team_id
              char team_name[50];
              char car_name[20];
              unsigned int constuctor_championships;
              unsigned int constructor_points;
              CDriver* pDriver1, pDriver2, pDriver3, pDriver4;
      
      public:
             //----------------------------------constructors----------------------------------------------------
             CTeam();
             CTeam(int theTeam_id, char theTeam_name[], char theCar_name[], unsigned int theConstructor_championships, unsigned int theConstructor_points, CDriver* thepDriver1, CDriver* thepDriver2);
             
             //----------------------------------destructors-----------------------------------------------------
             ~CTeam();
             ~CTeam(int theTeam_id, char theTeam_name[], char theCar_name[], unsigned int theConstructor_championships, unsigned int theConstructor_points, CDriver* thepDriver1, CDriver* thepDriver2)
             
             //----------------------------------methods---------------------------------------------------------             
             void setTeam_id(int theTeam_id);                                        //set id number of the team, id comes assigned 
             int getTeam_id();  
             
             void setTeam_name(char theTeam_name[]);                                 //sets the name of the team
             char* getTeam_name();                                                   //returns the name of the team
             
             void setCar_name(char theCar_name[]);                                   //sets the name of the car
             char* getCar_name();                                                    //returns the name of the car
             
             void addConstructor_championships(bool first_place);                    //adding a constructor champ count
             int getConstructor_championships();
             
             void addConstructor_points(CDriver* pDriver1, CDriver* pDriver2);       //adding a constructor champ count
             int getConstructor_points(); 
             
                          
};
#enddif

I dont have the main done as yet, as I want to make sutre that I have the save/load part done.

thx for help

If you look at my last answer to this thread, you will see that you are in case number 5, i.e. you have an object hierarchy with cross-references between objects (via pointers). As with many programming problems there are three ways to solve it: the good, the bad and the ugly.

The Good: Use a library that is already established and highly regarded, if it serves your need. In this case, Boost.Serialization. I know, the code in it is not exactly "simple" for a beginner. But if you can bare it, use it.

The Bad: Make your own home-brewed serializer. This is bad because it is reinventing the wheel and it is tedious to make it robust (or you have to leave it in a fragile state, which is worse). Anyhow, for this, you would have to have a class that handles all the I/O with the file. That class should keep a record of all objects (non-primitive) (by pointer) which are saved so that it doesn't repeat or go into an infinite cycle. I could go on explaining it, but I think you could be looking for the Ugly way instead.

The Ugly: Write a quick and dirty, single serving solution. In this case, you can assume that all drivers can saved as part of the save function of the team object. So, the driver class probably shouldn't save the team pointer that is has. So, in a save/load function in CTeam, you just save all the simple stuff first (primitive types) and then call save/load on each driver you have. When loading, you should set the CTeam pointer in each driver after you have loaded them.

Member Avatar for trebor-pl

Cheers, I'll propably go with Boost Serialization as when I'll learn it I will then be able to use it for my later projects, so I rather spend few nights on that now get it done and dusted and do it the right way.


thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.