[prog] C++

Jacinta Richardson jarich at perltraining.com.au
Thu Mar 4 15:42:10 EST 2004


How'd the PHP stuff go?

> write a program to track a music cd library, allow the user to enter
> cds. Each item should include the artist, album title, year and genre.
> All records should be displayed in order by artist. Design this using a
> class and represent each cd as an object of that class ( This is the
> part that is really confusing me). Store the data in a text file so it
> can be retrieved and viewed. 
> 
> Since I use OSX I was sort of following itunes as an example, I have my
> cds listed in a database in mysql, but I don't know if you can connect
> to that with C++? 

You can connect to databases through C++ but I don't think it's talking
about anything that serious.  I think this is probably what you need to be
doing:

a) design a CD class:

Class CD:
	object private variables:
		artist, album title, year, genre

	methods:
		yet to be determined.

b) write code to allow users to enter cds (perhaps using insertion sort)

c) write code to write cd list to a file (maybe best to sort first)
d) write code to extract cd list from a file.

e) write code to allow the user to view the cds etc.


A possible program for this might look like this:

	Ask user if they want to 
		a) add a new cd, 
		b) read in cds from a file
		c) list all cds
		d) end session
		
	If a) then
		ask for artist name
		ask for album title
		ask for year
		ask for genre

		create new C++ object with these values
		add new object to list of objects.  [1]

		return to top.

	If b) then
		ask for name of file

		read in first cd		    [2]
		create new C++ object with these values [3]
		add new object to list of objects

		read in next cd and repeat

		return to top

	If c) then
		walk over list of (sorted) cds and print
		out each one

		return to top

	If d) then
		ask for a file name
		write out list of (sorted) cds to file
		quit

	Else
		invalid response
		return to top.

[1] - I highly recommend using insertion sort to add your elements in.
What this means is that you always add elements in such a way as to keep
your list sorted.  For example adding 4 into the following list:
	2, 20, 40, 300
gives us
	2, 4, 20, 40, 300
If you always add elements into this list using insertion sort then you
will never have to actually sort the list.  There are 3 cases in
insertion sort and it's best (at the start) to treat them separately.
The cases are:
	1.  Inserting right at the front of the list
	2.  Inserting somewhere in the middle of the list
	3.  Inserting right at the end of the list

[2] - You'll have to think hard about how you want to represent individual
cds in your file.  You could do one cd per line with distinct separators,
such as:

author%%%title%%%year%%genre

or you could write them out in little blocks:
a:author
t:title
y:year
g:genre

a:author
t:title
y:
g:genre

but you'll need to document your assumptions about the integrity of
the file.

[3] One of the nice things about object oriented programming is that you
can hide the representation of things inside the objects.  For example it
would  make sense to create a "write_to_file(FILE file)" method for the CD
Class.  This would be called on each CD object and that object would write
itself to the file.

Likewise a "read_from_file(FILE file)" method would allow CD objects to
read themselves from the file.  This is really handy because you might
want to use this CD class (and its stored CDs) somewhere else in other
code.

In fact, it might even be a good idea to have the object ask the user for
new cd details (in part a).  This means that if you ever decide to store
more information about a cd (such as a play list or a filename pointing to
your mp3 collection) you only need to change the class, not the rest of
the code.

Of course you're going to want to have the CD print itself out too.  So
this gives us the following class model:

Class CD:
        object private variables:
                artist, album title, year, genre
                
        methods:
		new		// you always need a constructor
		read_from_user()	  // ask user for cd details
		read_from_file(FILE file) // read details for 1 cd from file
		print_to_file(FILE file)
		display_self()

and (for example) part a and b become:

        If a) then
		object = CD.new();
		if(! object.read_from_user()) {
			user didn't give us the right kind of data
			return to top.
		} 
		add object to list.
                return to top.

        If b) then
                ask for name of file

		while ( 1 ) {
			object = CD.new();
			if(! object.read_from_file(filename)) {
				invalid file or no more entries
				break;
			}
			add object to list
		}
		return to top.	


I hope this helps.

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list