14 Replies - 1285 Views - Last Post: 27 March 2009 - 05:29 AM Rate Topic: -----

#1 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

structs vs classes

Post icon  Posted 26 March 2009 - 05:08 PM

My class recently designed our own program that would function similar to the way an iPod works. The songs were hard coded in and basically we had to use structs to hold the artist, songtitle, size of file, etc. We have the program running and we thought we did a pretty good job considering the short amount of time to do the project in. Below is the code.

The question i have regarding all of this is how i would go about changing all of this to work with classes. We started going over classes but me and my partner are both a little (or alot ) confused on how to get this working. It may be alot easier than we're thinking. I realize this may take a while but the prof is out of town, and life/work has gotten in the way the past few days to allow for much work on this. I read the object tutorial in the tutorials section but im still a lil unclear.

Any and all help is highly appreciated. Classes aren't my favorite thing right now.

test.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "tsuPod.h"

using namespace std;



int main() 
{
  const int MEMORY_SIZE = 25;
		   
  cout << "Displaying successful testing of all functions." << endl << endl;
  TsuPod Song[NUM_SONGS];  
	
  initTsuPod(Song);				   // Following code initializes the tsuPod 

   double Memory_Used = 0;
	
	// Following code lists all songs located in tsuPod memeory 
	showSongList(Song);			   // Everything is blank or 0 here
	cout << endl;
	cout << "DISPLAYING MEMORY USAGE AS SONGS ARE ADDED TO OBJECT" << endl << endl;
	cout << "Memory available: " << MEMORY_SIZE - Memory_Used << "MB" << endl << endl;
	
	
	
	// Following code tests tsuPod input functionality (successful)
	int retCode[8] = {0};
	retCode[0] = addSong( 1,"Run To The Hills", "Iron Maiden", 2, Song, Memory_Used);
	retCode[1] = addSong(2,"Sober", "TOOL", 2, Song, Memory_Used);
	retCode[2]= addSong(3,"Better", "Guns N Roses", 3, Song, Memory_Used);
	retCode[3] = addSong(4,"Alias", "In Flames", 3, Song, Memory_Used);
	retCode[4] = addSong(5,"Ready or Not", "Fugees", 2, Song, Memory_Used);
	retCode[5] = addSong(6,"Christian Woman", "Type O Negative", 2, Song, Memory_Used);
	retCode[6] = addSong(7,"Battle Without Honor or Humanity", "Tomoyasu Hotel", 1, Song, Memory_Used);
	retCode[7] = addSong(8,"We're In This Together", "Nine Inch Nails", 3, Song, Memory_Used);
	cout << endl;
	cout << "Total Memory used: " << Memory_Used << "MB" << endl << endl;
	cout << "Memory available: " << MEMORY_SIZE - Memory_Used << "MB" << endl << endl;
	cout << endl;

	
	showSongList(Song);   // Songs all have title, artist, memory size and track number

   cout << endl;
   cout << "Testing removeSong functionality one song at a time: " << endl;
	// Following code tests tsuPod song removal functionality		 
   retCode[0] = removeSong("Run To The Hills", "Iron Maiden", Song,Memory_Used);
	cout << retCode[0] << " test removal" << endl << endl;
   retCode[1] = removeSong("Sober", "TOOL",Song,Memory_Used);   
	cout << retCode[1] << " test removal" << endl << endl; 
   retCode[2] = removeSong("Better", "Guns N Roses",Song,Memory_Used);  
	cout << retCode[2] << " test removal" << endl << endl; 
   retCode[3] = removeSong("Alias", "In Flames",Song,Memory_Used);
	cout << retCode[3] << " test removal" << endl << endl; 
   retCode[4] = removeSong("Ready or Not", "Fugees",Song,Memory_Used);
	cout << retCode[4] << " test removal" << endl << endl;
   retCode[5] = removeSong("Christian Woman", "Type O Negative",Song,Memory_Used); 
	cout << retCode[5] << " test removal" << endl << endl; 
   retCode[6] = removeSong("Battle Without Honor or Humanity", "Tomoyasu Hotel",Song,Memory_Used);
	cout << retCode[6] << " test removal" << endl << endl; 
   retCode[7] = removeSong("We're In This Together", "Nine Inch Nails",Song,Memory_Used);
	cout << retCode[7] << " test removal" << endl << endl;   
	cout << endl;		  
	cout << "Total Memory available after song removal: " << MEMORY_SIZE - Memory_Used 
		 << "MB" << endl << endl;
	
	showSongList(Song);		//songs should all be cleared out at this point
	cout << endl;
	// Inputting songs again to test the clearMemory functionality
	retCode[0] = addSong( 1,"Run To The Hills", "Iron Maiden", 2, Song, Memory_Used);
	retCode[1] = addSong(2,"Sober", "TOOL", 2, Song, Memory_Used);
	retCode[2]= addSong(3,"Better", "Guns N Roses", 3, Song, Memory_Used);
	retCode[3] = addSong(4,"Alias", "In Flames", 3, Song, Memory_Used);
	retCode[4] = addSong(5,"Ready or Not", "Fugees", 2, Song, Memory_Used);
	retCode[5] = addSong(6,"Christian Woman", "Type O Negative", 2, Song, Memory_Used);
	retCode[6] = addSong(7,"Battle Without Honor or Humanity", "Tomoyasu Hotel", 1, Song, Memory_Used);
	retCode[7] = addSong(8,"We're In This Together", "Nine Inch Nails", 3, Song, Memory_Used);
	cout << endl << endl;
	
	cout << "Testing clearMemory functionality" << endl;
	cout << "Format should display:	title, song, memory size " << endl;
	cout << "You should see only commas and zeros if it works properly." << endl;
	clearMemory(Song, Memory_Used);			   //clearing all memory of songs
	
	
	cout << "Total Memory used: " << Memory_Used << "MB" << endl << endl;
	cout << "Memory available: " << MEMORY_SIZE - Memory_Used << "MB" << endl << endl;
	cout << endl;
	cout << "*******************************************************"<< endl;
	cout << endl;
	
	system("pause");
	
	
	/*************************************************************************/
	/*************************************************************************/
	// Begin testing of unsuccessful input
	cout << "Testing unsuccessful input below!" << endl;	
   
	initTsuPod(Song);		 // reinitialize everything to 0 and blanks.
	Memory_Used = 0;
	// Trying to add too many songs
	cout << "TESTING: ADD TOO MANY SONGS " << endl << endl;
	retCode[0] = addSong(9,"We're In This Together", "Nine Inch Nails", 3, Song, Memory_Used);
	cout << endl;
	
	// Following code tests tsuPod input functionality (unsuccessful)
	// Track 2 = blank title, Track 3 = blank artist, Track 5 = size < 0, Track 4 = song too large
	cout << "DISPLAYING MEMORY USAGE AS SONGS ARE ADDED TO OBJECT" << endl << endl;
	cout << "Memory available: " << MEMORY_SIZE - Memory_Used << "MB" << endl << endl;
	retCode[0] = addSong( 1,"Run To The Hills", "Iron Maiden", 2, Song, Memory_Used);  
	retCode[1] = addSong(2,""/*Sober"*/, "TOOL", 2, Song, Memory_Used);		   //blank title
	retCode[2] = addSong(3,"Better", ""/*Guns N Roses"*/, 3, Song, Memory_Used);  //blank artist
	retCode[3] = addSong(4,"Alias", "In Flames", 27, Song, Memory_Used);		  //size of song > 25MB
	retCode[4] = addSong(5,"Ready or Not", "Fugees", -4, Song, Memory_Used);	  //size of song < 0
	retCode[5] = addSong(6,"Christian Woman", "Type O Negative", 2, Song, Memory_Used);
	retCode[6] = addSong(7,"Battle Without Honor or Humanity", "Tomoyasu Hotel", 1, Song, Memory_Used);
	retCode[7] = addSong(8,"We're In This Together", "Nine Inch Nails", 20, Song, Memory_Used);   //memory usage past 25MB
	cout << "Total Memory used: " << Memory_Used << "MB" << endl << endl;
	cout << "Memory available: " << MEMORY_SIZE - Memory_Used << "MB" << endl << endl;
	cout << endl;

	
	 // Following code tests tsuPod song removal functionality  (unsuccessful)	   
   //below is explanation to console of what to expect for output during testing
   cout << "TESTING REMOVESONG FUNCTIONALITY: " << endl;
   cout << "Only 1 track has a bad title name (track 2), and it retains 2MB since" 
		<<  " cannot be removed due to invalid name." << endl;
   cout << "Tracks 4 and 5 will also return -1. Track 4 size is > 25MB, track 5"
		<< " size is less than 0, so songs were never stored."
		<< endl;
   cout << "Track 8 exceeds the allowed memory usage so it is never stored"
		<< endl << "So it also will not be found." << endl << endl;
		
   int retCodeBad[7] = {0};	 
   retCodeBad[0] = removeSong("Run To The Hills", "Iron Maiden", Song,Memory_Used);
	cout << retCodeBad[0] << " test removal" << endl << endl;
   retCodeBad[1] = removeSong("Sober", "TOOL", Song,Memory_Used);   
	cout << retCodeBad[1] << " test removal" << endl << endl; 
   retCodeBad[2] = removeSong("Better", "Guns N Roses", Song,Memory_Used);  
	cout << retCodeBad[2] << " test removal" << endl << endl; 
   retCodeBad[3] = removeSong("Alias", "In Flames", Song,Memory_Used); 
	cout << retCodeBad[3] << " test removal" << endl << endl; 
   retCodeBad[4] = removeSong("Ready or Not", "Fugees",Song,Memory_Used);
	cout << retCodeBad[4] << " test removal" << endl << endl;
   retCodeBad[5] = removeSong("Christian Woman", "Type O Negative", Song,Memory_Used); 
	cout << retCodeBad[5] << " test removal" << endl << endl; 
   retCodeBad[6] = removeSong("Battle Without Honor or Humanity", "Tomoyasu Hotel", Song,Memory_Used);
	cout << retCodeBad[6] << " test removal" << endl << endl; 
   retCodeBad[7] = removeSong("We're In This Together", "Nine Inch Nails", Song,Memory_Used);
	cout << retCodeBad[7] << " test removal" << endl << endl;   
	cout << "Total Memory available after song removal: " << MEMORY_SIZE - Memory_Used 
		 << "MB" << endl << endl;
	
	showSongList(Song);
	cout << endl;

	clearMemory(Song, Memory_Used);
	system("PAUSE");  
	return 0; 
}




tsupod.cpp
#include "tsuPod.h"


void initTsuPod (TsuPod song[])	 //initializing aspects of song[i] to 0 or blanks
{
	 
	 
   for( int i =0, x =1; i <NUM_SONGS; i++, x++)
   {
   song[i].title = "";
   song[i].artist = "";
   song[i].size = 0;
   song[i].track = x;
   
   cout << "Slot #" << i <<" is Track #"<< x << endl;	//couts slot number and track number
   }
   cout << endl;  
}   //void initTsuPod function exit

////////////////////////////////////////////////////////////////////////////////
int addSong (int track, string newTitle, string newArtist, int size, TsuPod Song[],double& Memory_Used)
{  
   if(track > NUM_SONGS)// if too many songs displays error message
	 {
	  cout << "Not enough slots to add another song!" << endl << endl;
	  return -2;
	  }
				 
   track = track-1;	//using track as a value of slot# now as array position
   string Stored_Title = Song[track].title;				 //Holds song title
   string Stored_Artist = Song[track].artist;			 // Holds Artist name
   string empty = "";						   // empty srting for comparison
   if (size > 25)// Clears memeor slot if song size greater than 25MB
	 {
	  cout << endl;
	  cout << "Size of file must be less than 25MB!";
	  Song[track].title = "";  // 
	  Song[track].artist = ""; 
	  Song[track].size = 0;	
	  return -2;
	 } 
   if (size < 0)//Clears memeor slot if song less than 25MB
	 {
	  cout << endl;
	  cout << "Size of file must be greater than 0!";
	  Song[track].title = "";
	  Song[track].artist = "";
	  Song[track].size = 0;
	  return -2;
	 }
   if(Memory_Used + size > MEMORY_SIZE)	  //Memory usage tester if statement
	 {		
	 cout << endl;
	 cout << "Memory full! Can't add another song!" << endl;  //display error message if memory use exceeds 25MB
	 cout << "Memory available: " << MEMORY_SIZE - Memory_Used << "MB." << endl;
	 cout << "Memory song requires: " << size << endl << endl;
	 return -1;					   //if memory > 25mb, return -1
	 }
   if (Stored_Title.compare(empty) || Stored_Artist.compare(empty))   //Tester of song title and artist for blank value
	 {
	  cout << "Title or Artist is blank in Slot " << track << ", Track " 
		   << track+1 <<"!" << endl;   //display error message if name or artist is blank
	  Memory_Used = Memory_Used - size;
	  return -2;
	 }  
   if ((Memory_Used + size) <= MEMORY_SIZE)// adds song to TSU Pod
   { 
   Memory_Used = Memory_Used + size;  
   }
   Song[track].title = newTitle;
   Song[track].artist = newArtist;
   Song[track].size = size;
 
   
   cout << endl;
   cout << "Memory used: " << Memory_Used << "MB";	 //display total memory usage at this point
   return 0;
}  //int addSong function exit

////////////////////////////////////////////////////////////////////////////////
int removeSong (string title, string artist, TsuPod Song[],double& Memory_Used)
{
	string Stored_Song;// stores song name
						  
	for( int i = 0; i < 8; i++)// loops to try to find song name
	{
	 
	 Stored_Song = Song[i].title;
   
	 if(Stored_Song.compare(title) == 0)		   //if song title is found, begin removal of song data
	   {
	   cout << "REMOVING: " << Song[i].title << ", " << Song[i].artist << endl;									 
	   cout << "Memory used: " << Memory_Used << endl;
	   cout << "Memory freed: " << Song[i].size << endl;
	   Memory_Used = Memory_Used - Song[i].size;	
	   cout << "Memory still in use: " << Memory_Used << endl;
	   cout << "Memory available after freed: " << MEMORY_SIZE - Memory_Used 
			<< "MB" << endl;
	   
	   Song[i].title = "";
	   Song[i].artist = "";
	   Song[i].size = 0;
	   return 0;
	   }	   
	}
	// if song is not found it displays error message
 cout << "Song title not found! Could not remove!" << endl;
 return -1;   
}   //removeSong function exit

/////////////////////////////////////////////////////////////////////////////////
void clearMemory(TsuPod Song[], double& Memory_Used)
{
	Memory_Used = 0;				   //reset Memory_Used to 0
	// loops though all spots and clears.
	for(int i = 0; i < 8; i++)		 // loop through and reset all aspects of songs
	   {	 
	   Song[i].title = "";		 
	   Song[i].artist = "";		
	   Song[i].size = 0;
	   cout << Song[i].title << ", " << Song[i].artist << ", " << Song[i].size 
			<< endl;
	   }  
	cout << endl;  
}  //clearMemory function exit
////////////////////////////////////////////////////////////////////////////////
void showSongList (TsuPod Song[])
{
	 string Stored_Song;
	 cout << "Displaying showSongList data" << endl;
		for(int i =0, x = 1; i < 8; i++, x++)// loops through slots.
		{
		  
		  Stored_Song = Song[i].title;
		  
		  if( Stored_Song.compare("") == 0 )// output if slot is empty
		   cout << "Slot #" << x << " Empty" << endl;
		  
		  else							  //outputs song information
		  {
		   cout << "Slot #" << x << " " << Song[i].title <<", ";
		   cout << Song[i].artist << ", " << Song[i].size <<" MB" << endl;
		  }
		} 
}   //showSongList function exit
////////////////////////////////////////////////////////////////////////////////



tsupod.h
#ifndef tsuPod_h
#define tsuPod_h
#include <string>
#include<iostream>
using namespace std;

const int NUM_SONGS = 8;
const int MEMORY_SIZE = 25;

struct TsuPod
{
  string title,
		 artist;
		 
  int size,
	  track;
};


////////////////////////////////
/* FUNCTION - void initTsuPod

 Initialize all the slots to blank and 0 size memory
 
 input parms - none
 
 output parms - none */
void initTsuPod (TsuPod Song[]);


///////////////////////////////
/* FUNCTION - int addSong
 * attempts to add a new song to the tsuPod
		  o returns a 0 if successful
		  o returns -1 if not enough memory to add the song
		  o returns -2 for any other error (such as a blank title or artist)
 
 input parms - /int track, string newTitle, string newArtist, int size, TsuPod Song[],double& Memory_Used/
 
// output parms - /int addSong (0, 01 or -2)/			  */
int addSong (int track, string newTitle, string newArtist, int size, TsuPod Song[],double& Memory_Used);




////////////////////////////
/* FUNCTION - int removeSong
	* attempts to remove a song from the tsuPod
		  o returns 0 if successful
		  o returns -1 if nothing is removed
	
		  
input parms - /*string title, string artist, TsuPod Song[],double& Memory_Used*/
 
//output parms - /*int removeSong (0 or -1*/
int removeSong (string title, string artist, TsuPod Song[],double& Memory_Used);





////////////////////////////
/* FUNCTION - void clearMemory
* clears all the songs from memory

input parms - /*TsuPod Song[], double& Memory_Used*/
 
//output parms - /*none (void)*/
void clearMemory(TsuPod Song[], double& Memory_Used);




/////////////////////////////
/* FUNCTION - void showSongList   
	* prints the current list of songs in order from first to last to standard output
	* format - slot #, Title, Artist, size in MB (one song per line)
	* print "Empty" for any slots that do not contain a song

input parms - /*TsuPod Song[]*/
 
//output parms - /*none (void)*/  
void showSongList (TsuPod Song[]);



////////////////////////////
/* FUNCTION - void shuffle
	*  shuffles the songs into random order
		  o will do nothing if there are less than two songs in the current list

input parms - /*add nessesary parameters here*/
 
//output parms - /*add nessesary parameters here*/
void shuffle (/*add nessesary parameters here*/);

#endif

This post has been edited by Doc78610: 26 March 2009 - 05:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: structs vs classes

#2 IrishCereal  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 11-December 08

Re: structs vs classes

Posted 26 March 2009 - 05:14 PM

The main difference between structures and classes is that classes also contain methods that are associated with the data. Here's an example. If I had a structure of crates I could hold the following data.

struct crate {
  int width;
  int height;
};



But if I used a crate class I could have methods that would determine the volume, surface area, and other things.

class CCrate {
  private:
	int width;
	int height;
  public:
	void Volume();
	void Area();
};



And other things of that nature. Make sense?
Was This Post Helpful? 1
  • +
  • -

#3 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: structs vs classes

Posted 26 March 2009 - 05:17 PM

The first thing you should do is identify the objects used in the program. From what I see, the two main classes of objects are iPods and songs. By the way, the code you wrote above is very neat and easy to understand.
Was This Post Helpful? 0
  • +
  • -

#4 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Re: structs vs classes

Posted 26 March 2009 - 05:26 PM

View Postbsaunders, on 26 Mar, 2009 - 04:17 PM, said:

The first thing you should do is identify the objects used in the program. From what I see, the two main classes of objects are iPods and songs. By the way, the code you wrote above is very neat and easy to understand.


This is another thing i'm a little unclear on. Based on the new instructions i have, i need to be able to store up to 25 songs not to exceed 100 MB (imaginary memory, just an integer value representing memory). So does that mean i have to creat an object for each song? or can i create a single object array? My brain isn't wrapping itself around this idea very much, especially after doing it one way and trying to rearrange it into a new way.

And thanks for the neatness compliment :)
Was This Post Helpful? 0
  • +
  • -

#5 IrishCereal  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 11-December 08

Re: structs vs classes

Posted 26 March 2009 - 05:28 PM

View Postbsaunders, on 26 Mar, 2009 - 04:17 PM, said:

The first thing you should do is identify the objects used in the program. From what I see, the two main classes of objects are iPods and songs. By the way, the code you wrote above is very neat and easy to understand.


Who? Me?

View PostDoc78610, on 26 Mar, 2009 - 04:26 PM, said:

View Postbsaunders, on 26 Mar, 2009 - 04:17 PM, said:

The first thing you should do is identify the objects used in the program. From what I see, the two main classes of objects are iPods and songs. By the way, the code you wrote above is very neat and easy to understand.


This is another thing i'm a little unclear on. Based on the new instructions i have, i need to be able to store up to 25 songs not to exceed 100 MB (imaginary memory, just an integer value representing memory). So does that mean i have to creat an object for each song? or can i create a single object array? My brain isn't wrapping itself around this idea very much, especially after doing it one way and trying to rearrange it into a new way.

And thanks for the neatness compliment :)


I would create an object array. Or if you've used vectors, that might be good to use, since you can dynamically allocate it.
Was This Post Helpful? 0
  • +
  • -

#6 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: structs vs classes

Posted 26 March 2009 - 05:35 PM

View PostDoc78610, on 26 Mar, 2009 - 04:26 PM, said:

View Postbsaunders, on 26 Mar, 2009 - 04:17 PM, said:

The first thing you should do is identify the objects used in the program. From what I see, the two main classes of objects are iPods and songs. By the way, the code you wrote above is very neat and easy to understand.


This is another thing i'm a little unclear on. Based on the new instructions i have, i need to be able to store up to 25 songs not to exceed 100 MB (imaginary memory, just an integer value representing memory). So does that mean i have to creat an object for each song? or can i create a single object array? My brain isn't wrapping itself around this idea very much, especially after doing it one way and trying to rearrange it into a new way.

And thanks for the neatness compliment :)


Did you learn about classes and objects yet?
Was This Post Helpful? 0
  • +
  • -

#7 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Re: structs vs classes

Posted 26 March 2009 - 05:42 PM

"Did you learn about classes and objects yet?"



We are learning about them right now but me and alot of people in the class are very fuzzy on the subject. I have a text book ive read and it helps with very basic cases but nothing complicated like this. And the tutorial here is also a little helpful but i get lost in it at some point. Im confortable with structure, but classes just confuse me. Constructors, destructors, and the private vs public (in the context of this program) also leave me confused. =\



"I would create an object array. Or if you've used vectors, that might be good to use, since you can dynamically allocate it."


We briefly went over vectors but never used them in a program. As far as an object array goes, i wouldn't know how to code one.... i experimented with it a little before but couldn't find anything to help me out much for the syntax.

This post has been edited by Doc78610: 26 March 2009 - 05:53 PM

Was This Post Helpful? 0
  • +
  • -

#8 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: structs vs classes

Posted 26 March 2009 - 06:13 PM

Do you know how to declare a class in C++?
Was This Post Helpful? 0
  • +
  • -

#9 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Re: structs vs classes

Posted 26 March 2009 - 06:33 PM

View Postbsaunders, on 26 Mar, 2009 - 05:13 PM, said:

Do you know how to declare a class in C++?


I know some i suppose. I know that i need (based on what i was taught) my main.cpp, class.cpp, and class.h.
In the .h i establish the private and public sections, private is the characteristics of what the class needs to handle, and the public is like what its responsible for doing with the information in private.

The idea i think i understand, but this is just way too complicated to start with as our first program using classes i think.
Was This Post Helpful? 0
  • +
  • -

#10 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: structs vs classes

Posted 26 March 2009 - 07:32 PM

Yeah, is kind of complicated.

Do you know what objects your program will need?
Was This Post Helpful? 0
  • +
  • -

#11 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Re: structs vs classes

Posted 26 March 2009 - 07:46 PM

View Postbsaunders, on 26 Mar, 2009 - 06:32 PM, said:

Yeah, is kind of complicated.

Do you know what objects your program will need?



As of right now the program requirements say i need two classes, one for songData like artist, title, size and track number and the other class will do all the functional things like adding songs to a list, removing songs, clearing the list, shuffling songs and such.

What I'm not sure about is whether i need to create 25 diff objects for each song like Song1, Song2, Song3, etc.
I had the idea of somehow making an object array or something but im unsure of the syntax.
Was This Post Helpful? 0
  • +
  • -

#12 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: structs vs classes

Posted 26 March 2009 - 07:54 PM

For all songs, you can declare a class called Song. You would declare this class in a header file with the same name as the class, song.h:

song.h

#ifndef SONG_H
#define SONG_H

class Song
{
public:
	Song();
	~Song();
};

#endif


Does that syntax seem familiar to you?
Was This Post Helpful? 0
  • +
  • -

#13 Doc78610  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-February 09

Re: structs vs classes

Posted 26 March 2009 - 07:57 PM

View Postbsaunders, on 26 Mar, 2009 - 06:54 PM, said:

For all songs, you can declare a class called Song. You would declare this class in a header file with the same name as the class, song.h:

song.h

#ifndef SONG_H
#define SONG_H

class Song
{
public:
	Song();
	~Song();
};

#endif


Does that syntax seem familiar to you?


Yup. Looks like a constructor and deconstructor, correct me if im mistaken.
As of now, i have something like this
class Song
{
  private:
		  string songData[25];
		  string title[25];
		  string artist[25];
		  int size[25];
		  int track[25];

  public:
		  song(string, string, int, int);
		  ~Song();
		  void setTitle(string, int);
		  void setArtist(string, int);
		  void setSize(int, int);
		  string getTitle(int) const;
		  string getArtist(int) const;
		  int getSize(int) const;

};


The reason for the arrays in private is i have a limitation of up to 25 songs... im sure theres an easier way of doing this though.
Was This Post Helpful? 0
  • +
  • -

#14 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: structs vs classes

Posted 27 March 2009 - 03:52 AM

Classes represent entities; Private data members of a class represent attributes of a (one) entity; At the moment, your song entity has 25 titles, 25 artists, 25 tracks...

Think about what you're representing from a real world perspective; a Song typically has one title and one artist (although some songs might have multiple artists, but for the sake of simplicity you might want to make the assumption that one song always has one artist - at least for now). A song typically doesn't have a 'track' - a Track would be used by something which holds a collection of songs. like a music player, or a CD album. Where a track number is an ID used to distinguish between different songs stored together, and/or dictate the song playlist order.

If a song changes position on your music player, its track number on the music player might change, however the song itself is still the same - A song on your ipod does not care where it exists in relation to other songs on your playlist, but if 'track' was an attribute of a song, your song would behave differently every time you moved it to a different position in the playlist. Worse still, your song might have redundant attributes depending on the media on which you play it - for example, you might have 25 songs on a cassette tape; Cassettes don't use the notion of "track", instead they use the notion of timestamp positioning - having a song on a cassette with a track number would be meaningless.

OO Design takes a leaf out of the proverbial relational database book; Every attribute belongs to one entity, the whole entity, and nothing but the entity. (Most computing science students have usually had data modelling and normalisation drummed into them at some point in their lives - if you can think about relational databases, then you're off to a good start on OOD as well)

This post has been edited by Bench: 27 March 2009 - 03:54 AM

Was This Post Helpful? 0
  • +
  • -

#15 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: structs vs classes

Posted 27 March 2009 - 05:29 AM

This may help simplify, and jump start your approach to classes ...

#include <iostream>
using namespace std;

const int NUM_SONGS = 8;
const double MEMORY_SIZE = 25; // MB

class Song ////////////////////////////// SONG /////////////////////////////////
{
public:
    Song() { title=""; artist==""; size=0; }
    Song(string t, string a, double s) { title=t, artist=a, size=s; }
    Song( Song &s ); // copy constructor    // see 1. below ...
    Song operator=(Song& s);                // see 2. below ...
    void show();                            // see 3. below ...
    string get_title() { return title; }
    double get_size() { return size; }
protected:
  string title,
         artist;
  double size;
};

Song::Song( Song&s ) // 1.
{
    title = s.title;
    artist = s.artist;
    size = s.size;
}

Song Song::operator=(Song& s) // 2.
 {
    title = s.title;
    artist = s.artist;
    size = s.size;
    return *this;
}

void Song::show() // 3.
{
    if( title == "" ) cout << "Empty" << endl;
    else cout << title << ", " << artist << ", " << size <<" MB" << endl;
}


class TsuPod : public Song ////////////////////// TSUPOD ///////////////////////
{
public:
    TsuPod() : memory(MEMORY_SIZE) {}
    void set_pod(int, Song&);
    void set_pod(int, string, string, double); // Note: overloaded ...
    void show();
private:
    // the main 'pod' to keep all songs ... i.e. an array of 'Song'
    Song pod[NUM_SONGS];
    double memory;
    
    // Note: a private member function, so can only be used within class 'TsuPod'
    void show_free_tracks();
};

void TsuPod::set_pod(int t, Song& s) // t is for track, s is for size
{
    if( t<1 || t>NUM_SONGS )
    {
        cout << "Track " << t << " is out of range." << endl;
        return;
    }
    
    if( pod[t-1].get_title() != "" )
    {
        cout << "Track " << t << " is already used ..." << endl;
        show_free_tracks();
        return;
    }
    
    if( memory < s.get_size() )
    {
        cout << "Not enough memory left for this song." << endl;
        return;
    }

    pod[t-1] = s;
    memory = memory - s.get_size();
}

// Note: overloaded ...
void TsuPod::set_pod(int _track, string _title, string _artist, double _size )
{
    Song tmp( _title, _artist, _size );
    set_pod( _track, tmp );
}

void TsuPod::show()
{
    for( int i=0; i<NUM_SONGS; ++i )
    {
        cout << "Track " << i+1 << ", ";
        pod[i].Song::show();
    }
    cout << memory << "MB's of memory is presently free.\n" << endl;
}

void TsuPod::show_free_tracks()
{
    int freeCount = 0;
    for( int i = 0; i< NUM_SONGS; ++i )
    {
        if( pod[i].get_title() == "" )
        { cout << "Track " << i+1 << " is free.\n"; ++freeCount; }
    }
    cout << freeCount << " track(s) and "
         << memory << "MB's of memory presently free.\n" << endl;
}



int main() ////////////////////////// MAIN /////////////////////////////////////
{
    TsuPod p1;
    
    Song s1("Run To The Hills", "Iron Maiden", 2);
    s1.show();
    
    p1.set_pod( 1, s1 );
    p1.show();
    
    p1.set_pod( 2, "Sober", "TOOL", 2 );
    //p1.set_pod( 3, "Better", "Guns N Roses", 3 );
    p1.set_pod( 4, "Alias", "In Flames", 3 );
    p1.set_pod( 5, "Ready or Not", "Fugees", 2 );
    p1.set_pod( 6, "Christian Woman", "Type O Negative", 2 );
    //p1.set_pod( 7, "Battle Without Honor or Humanity", "Tomoyasu Hotel", 1 );
    p1.set_pod( 8, "We're In This Together", "Nine Inch Nails", 3 );
    p1.show();
    
    p1.set_pod( 8, "We're In This Together", "Nine Inch Nails", 3 );
    
    cout << "The end of preliminary testing of 'class TsuPod' ... "
         << "To exit press 'Enter' ... " << flush;
    cin.get();
}


Shalom,
David
http://developers-he.../index.p...opic,127.0.html
http://developers-he.../index.p...opic,106.0.html

This post has been edited by David W: 27 March 2009 - 08:14 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1