Thursday, July 30, 2009

A c++ question dealing with an array of class objects?

Hmm, how would I sum this up.


Basically I have a class that does many things; adds songs, removes songs, deletes songs, shows the song list, etc.





At the moment I just got finished completing the add song member function in the iPod class, so that I can store songs. However, I'm trying to hold 8 songs. What I have done is declared an array of objects, like this.





iPod song[9];





Then I'll use the addSong function, in this way.





song[1].addSong(title1, artist1, size1);





Problem is, I'm trying to make a showSongList member function in the iPod class that shows all the songs I have input into storage. I have no idea how to go about this except for running the showSongList nine different times, using


song[1].showSongList();





Does anyone know of a way in which I could display my entire object array of songs while still keeping the display function inside of the iPod class?





In additional details I'll add some snippets of code if that will help.

A c++ question dealing with an array of class objects?
Your iPod class represents an iPod. When you use the addSong() method, it is adding a song to its collection of songs. What you need to have in this class is a collection where every element represents a song and its details such as title, artist and size.





Note that the iPod itself does not have a title artist and size. So, these attributes on the iPod class makes no sense. iPod has other info like capacity, make, model, cost etc. So, add attributes such as these to the iPod class.





What you may wanna do is to modify the iPod class to have a collection object, say songlist(list, vector etc or song objects) that keeps growing as you keep adding songs. The song class can have attributes such as title, artist, size. As and when you add a new song, create a new song object and add it to your songlist collection.





Then, the method showSongList() will just have to iterate through the songlist and display them one after the other.

buy flowers

No comments:

Post a Comment