5 Replies - 3829 Views - Last Post: 20 April 2012 - 10:29 AM Rate Topic: -----

#1 archibalduk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-April 12

A vector of class objects vs A class with a vector of a struct

Posted 14 April 2012 - 03:43 AM

Hi! Before I ask my question, I just want to say that this forum has proved invaluable in helping me to learn C/C++. This is a really great place to learn about coding. :)

I've recently started learning about C++ classes and how object-oriented programming is advantageous. I'm now working on rewriting one of my ongoing projects into classes. What I'm looking for is just some general advice on what is generally the better method of dealing with classes and vectors. Before I ask my question, it might help if I explain what I'm tying to achieve:

I have been writing a data editor for an ice hockey management game named Eastside Hockey Manager 2007 (the game's developers have released the structs of each of the database files in order to help people modify the database). I have written an editor that will read in data from a CSV file and will update two of the game's database files based upon this data. This means that I can create a list of player contractual changes in the CSV file and my editor will apply these changes to the game's database.

Each row of data from the CSV file is loaded into the following struct (the SI_DATE mentioned below is itself a struct that stores the day of the year, the year and whether it is a leap year):

struct CONTRACT {
		long		StaffID;
		string		FirstName;
		string		SecondName;
		SI_DATE		DateOfBirth;
		string		ClubContractedName;
		long		ClubContractedID;
		string		ClubPlayingName;
		long		ClubPlayingID;
		SI_DATE		DateJoinedClub;
		SI_DATE		ContractExpiresClub;
		long		EstimatedWage;
		long		EstimatedValue;
};


I have created a vector of this struct to store each line/entry from the CSV file. My editor then carries out various processes on the CONTRACT struct (e.g. checking that the data is valid, calculating the ClubContractedID number based upon the ClubContractedName, etc). The data from the struct is written to two binary database files (because the game's data in relation to players/staff is split across two files) which each have their own different structs.

So in essence I'm dealing with three different structs (one for my CSV update file and two different structs for each of the target database files).

My aim is to write a single STAFF class which deals with the updating of the two database files (because both files relate to staff/player data). I had planned for the class to read in the CSV file, process the data and then write it to the two database files. At present, I have created as a private member of the class a vector of the CONTRACT struct.

My question is whether I have taken the correct approach in creating a struct vector within a class. Is this generally the correct method/approach? Or ought I change my CONTRACT struct to a class and then create a vector of this class?

My concern is that by creating a vector of structs within a class, I'm missing the whole point of having a class.

If I were to go down the route of having a vector of the class, then when it comes to writing each iteration of the class to the target files, presumably the loop would look something like this:

For each iteration of the vector<myClass>: Open file -> write iteration -> close file


Thus with each iteration of the vector, I would be repeatedly opening and closing the file. Would this be a problem/undesirable?

I hope I've made some sense! And thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: A vector of class objects vs A class with a vector of a struct

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: A vector of class objects vs A class with a vector of a struct

Posted 14 April 2012 - 04:06 AM

Its quite alright to have a vector of objects within a class as this does demonstrate encapsulation of members which is essential in oop, if you use reference or pointer types is up to you and how you wish to interact with the heap and stack in terms of the memory used. If you use pointers you will need to ensure you implement the appropriate destructor. Opening each file whilst iterating is probably not right if the file is in fact the 'same' file (I am little unclear if this is the case) so if each file is differently named then i am guessing that this is the only means of achieving the input - output required.

This post has been edited by DaneAU: 14 April 2012 - 04:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 archibalduk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-April 12

Re: A vector of class objects vs A class with a vector of a struct

Posted 14 April 2012 - 05:29 AM

View PostDaneAU, on 14 April 2012 - 04:06 AM, said:

Opening each file whilst iterating is probably not right if the file is in fact the 'same' file (I am little unclear if this is the case) so if each file is differently named then i am guessing that this is the only means of achieving the input - output required.


Thank you very much! You're correct; it would be the same file opened each time (well to be accurate, it would be the same two files opened with each iteration).

My main concern was that by placing a struct vector within a class I'd really be moving a collection/category of functions into a class (i.e being more orientated around the nature of functions) rather than being purely object-oriented. Having thought about it some more, using a vector of class objects might make things simpler in some ways. However, I'd need to think about how I could write a class function that would write its members to the two relevant files without the aforementioned opening and closing with each iteration...

View Postarchibalduk, on 14 April 2012 - 05:26 AM, said:

However, I'd need to think about how I could write a class function that would write its members to the two relevant files without the aforementioned opening and closing with each iteration...


Edit: Perhaps it would be better to have a function outside of the class to open and close the file. I.e. open file -> iterate through the vector of class objects and write them to the file -> close file.
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: A vector of class objects vs A class with a vector of a struct

Posted 14 April 2012 - 05:34 PM

Quote

...Edit: Perhaps it would be better to have a function outside of the class to open and close the file. I.e. open file -> iterate through the vector of class objects and write them to the file -> close file.


A good concept to remember in coding is 'keep it simple ...' especially at first.

It seems like you may have a total of 3 types of records / struct's / classes ... so then you would have 3 types of file structures of data to manage.

So ... you may also want to look at what kind of container ... like a STL vector or list or map ... may best suit your data processing needs.

If you have a common unique id 'key' ( name and dob ? ) among all 3 data bases, you may find STL map(s) quite useful.

You may like to know that It is considered 'good practice' by some here at DIC, (as you may see from perusing several old posts), to keep you data input / output separate from your struct / class / data record object. For small, student type, data processing, say editing a csv file of records of class Student, this may not be a big issue. But, your cross referencing of 3 data bases, would seem to put your project somewhat beyond beginning student type problems :)

This post has been edited by David W: 14 April 2012 - 05:36 PM

Was This Post Helpful? 1
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1304
  • View blog
  • Posts: 4,488
  • Joined: 19-February 09

Re: A vector of class objects vs A class with a vector of a struct

Posted 14 April 2012 - 08:26 PM

The vector would be in a separate class. If you have a CD, book or DVD you can have a collection of CD's etc.


struct CD
{
  string title;
  string artist;
};


class CDCollection
{
  private:
    vector<CD> cdvector;

  public:
    bool readfile(char *filename)
    {
      // read from file
      // add records to vector
    }
};



The data will be read into the vector and can be written to other files with different functions.
Was This Post Helpful? 1
  • +
  • -

#6 archibalduk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-April 12

Re: A vector of class objects vs A class with a vector of a struct

Posted 20 April 2012 - 10:29 AM

View PostDavid W, on 15 April 2012 - 01:34 AM, said:

Quote

...Edit: Perhaps it would be better to have a function outside of the class to open and close the file. I.e. open file -> iterate through the vector of class objects and write them to the file -> close file.


A good concept to remember in coding is 'keep it simple ...' especially at first.

It seems like you may have a total of 3 types of records / struct's / classes ... so then you would have 3 types of file structures of data to manage.

So ... you may also want to look at what kind of container ... like a STL vector or list or map ... may best suit your data processing needs.

If you have a common unique id 'key' ( name and dob ? ) among all 3 data bases, you may find STL map(s) quite useful.

You may like to know that It is considered 'good practice' by some here at DIC, (as you may see from perusing several old posts), to keep you data input / output separate from your struct / class / data record object. For small, student type, data processing, say editing a csv file of records of class Student, this may not be a big issue. But, your cross referencing of 3 data bases, would seem to put your project somewhat beyond beginning student type problems :)


Thank you! This is really useful guidance. :) I am taking a look at maps and I think it is something that will be very useful for parts of project.

I think the fact that I'm using three structs makes it harder for me to explain (in essence, I'm reading from one struct/class and writing it to two different structs!). #define's CD collection analogy is much easier to explain and is very similar to what I want to achieve.

I'm a real newcomer to OOP (have been programming in C for eight months but have only just dipped into OOP) but, to me, having a vector of a struct within a class feels "messy". In my novice view, it would make more sense (in terms of tidiness) to have vector/array of classes to represent a CD collection rather than having a vector of a struct within a class.

Thank you for the advice about what is considered good practice. This is exactly what I wanted to know! I will follow your advice and keep the input/output separate from the class. I think this will help keep things simple and ultimately make my life easier (as I plan on adding additional classes / data processing as my project grows).

Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1