Hello Phil,
11 Mar 98, Phil Ohl of 1:3804/180 wrote to Darin Mcbride
PO> I guess by your definition I program in (almost) C++.
Hmm, for what it's worth, you have to start somewhere. ;-)
PO> I am still very new at the language and the whole Object
PO> thing is newer still. They don't teach much in a high school
PO> class so I'm trying to learn on my own therefore i haven't
PO> had real good training using CLASS' and such, and frankly it
PO> kinda' scares me. (probably a false fear though)
Indeed, it is. I don't know if this the correct definition of a class as
spoken by professionals, but in lame mans words a class, or a struct which is
almost the same in c++, is in fact a combination of a number of variables and
a set of functions that have acces to those variables as to manipulate them
in certain ways, with in mind, that only those functions will observe the
integrity and correct handling of those variables. This behaviour can be made
even more restricted if the variables in a class are made private (by
default) or protected. In that case only the class member functions allow the
user to manipulate its variables.
Let's wander back for a moment to your highscore list in a game. This is
certainly a good subject for a class, or two, as I come to think of it.
First you have a score. Here you at least need space for a name and a value,
so lets do that:
class Score {
private:
char *name;
unsigned value;
public:
Score(int size=20) : value(0) { name = new char[size+1]; }
~Score() { delete[] name; }
void SetName(char *s) { strncpy(name, s, size); }
char *GetName() { return name; }
void SetNum(int v) { value = v; }
unsigned GetNum() { return value; }
};
// note that I skipped the error checking for memory and value overflow to //
keep it simple
After this you need an array to show the number of highscores in your game.
You could of course do this in a 'C' manner by doing something like:
Score Highscore[6]; and then filling it, but lets go a step further and
create another class that does this for you internally and also keeps track
of the placement from high to low in the list.
class ScList {
private:
Score *table;
int size;
public:
ScList(char *s, unsigned vals, int sz);
/* *s = an initial name to fill all entries with
vals = an initial score to fill all values with
sz = the size of the highscore table */
~ScList();
int Check(unsigned val);
int Check(Score *sc); // if score value is in, return true
void Update(Score *sc); // insert a new score in table
void Show(); // display the table
void Read(char *fname); // read fname scores from disk
void Write(char *fname); // write fname scores to disk
};
To give all the code for the individual member functions is probably a little
to much to post here now, and although I hate the following remark, you
could try to implement this as an excercise.
If you are not able to figure it out however, just ask.
PO> anyway any tips you could give me would be helpful and thank
PO> you for answering the "Writing to a file" question.
Hope this gave you some additional ideas... ;-))
PO> Thanx,
PO> Phil
Greetings from sunny Amsterdam,
Jan
email:bijster@worldonline.nl
http://home.worldonline.nl/~bijster
--- MBM v3.41e
---------------
* Origin: Snuffelaar bij DosBoss West (2:500/121.5122)
|