[prog] Re: C++

Sue Stones suzo at spin.net.au
Sat Mar 13 20:09:11 EST 2004


Sorry if I sent my last reply to you privately.  It happens when the list gets  
'CC'ed rather than on the main recipiants line.

This is looking good.  No let me see if I can explain objects.

Objects are supposed to correspond to objects in real life.  They are 
self-containted encapulated pieces of code that includes the variables to 
hold the information about that object as well as the functions that you want 
to do to that object.

In this case the object are CD.  In real life each CD is a separate object, 
each CD has it own title, artist, year and genre.  And these things are 
independent of other CD's, the situation doesn't change if you have 1 CD or 
1000 CDs.

At the moment you have only one artist, title, year and genre.  you are going 
through a loop and getting info on 10 CDs but each time the data overwrites 
the last data, so you loose every thing except the info on the last CD.  What 
you need is a way of keeping the info for each CD separare.  Enter objects.

But before we create objects and leave them floating in the computers memory 
like so many helium filled balloons, we need a some way of keeping track of 
them.  In real life you might keep your CDs in a CD rack, if you don't they 
may get lost all over the house and you would only be able to play them when 
you happend to find them.  But in the computer if we loose them we will never 
find them again.  

So we need to create CD objects and we need some place to keep track of all 
the CD's.

Ok now how do we create objects.  There are 2 ways (that I know of), I'll 
present the one that I am more familliar with.  That is to make a separate 
class for the objects...


On Sat, 13 Mar 2004 02:45 pm, wolf wrote:
>
> #include <iostream>
> using namespace std;
>
class cd {
private:
> 	char artist[30];  
> 	char title[50];
> 	int yr;
>	char genre[10];
>
> public:
  	 cd();  // construct a new blank CD.
> 	void getdata(void);
> 	void showdata(void);  // probably show data and put data are the same thing 
				// I sugest that you get rid of this.
> 	void putdata(void);
> };
> void getdata()
> {
> 	/* get the data from the user and write it down to a text file. use cin */
> 		cout << "Enter the Artist's Name: "<< endl;
> 		cin.getline >> artist;
> 		cout << "Enter the Compact Disk Title: " << endl;
> 		cin.getline >> title;
> 		cout << "Enter the Year the CD was recorded: "<< endl;
> 		cin >> yr;
> 		cout << "Enter the genre for this CD: " << endl;
> 		cin.getline >> genre;
> }
>
> void putdata()
> {
> 	cout << artist << endl;
> 	cout << title << endl;
> 	cout << yr << endl;
> 	cout << genre << endl;
> }
}

 class cdlib {
> int static main()
> {
	cd rack[10]; // some place to keep 10 CDs
> 	
	for(int i=0; i<10; i++)
	{
		rack[i] = new cd();	// create a new CD
		rack[i].getdata();		// fill it with data
		rack[i].putdata();		// print out the data to confirm its right.
	} // for
	for(int i=0; i<10; i++)
	{
		rack[i].putdata();		// print out the data to confirm its right.
	} // for
> 	return 0;
> } // main
 
This should give the basic idea.  I am not certain that I remeber the syntax 
correctly, you may need to check how to do things in you book.  And this is 
certainly not the end of what you will need to do make this a usefull, 
complete exercise.

What it does:  create a cd rack to hold 10 CDs.  In a loop create a CD, fill 
in the fields with data. then print it out to be sure its there.  Finaly go 
though the whole cd rack and make sure we still have all the data.

The thinng to do next is to get this to compile, and run.  Once its going then 
you can start to think about what else you need to do to it.

Ask any questions of bits that make no sence.

sue


More information about the Programming mailing list