Sign in to follow this  
JeremyYox

Explanations please? Arrays, data members, member functions

Recommended Posts

JeremyYox    105
Hi again,

I'm back once more to ask for a bit of clarification on arrays, data members, and member functions. The book I'm following doesn't really explain them outside an example that states 'spacecraft' is an object, with a data member 'energy' and a member function 'fireWeapons()'. Then it moves on and for the next chapter pretty much never brings it up again? I'm lost on that whole topic now.

As for arrays, if I get it correctly, they're essentially (as I see it in my mind atm) a storage for any list of a single type of data? They're created like any other variable with the addition of [x] (being the number of 'items' in the 'list')?

I'm liking this book so far, but its explanations could be a bit clearer, or perhaps I'm bad at catching the obvious - and I have a tendency to obsess on the one thing I don't understand fully until it makes it impossible to move on.. :) Any further explanation/clarification on these items would be greatly appreciated.

Share this post


Link to post
Share on other sites
Texus    248
[quote]
As for arrays, if I get it correctly, they're essentially (as I see it in my mind atm) a storage for any list of a single type of data? They're created like any other variable with the addition of [x] (being the number of 'items' in the 'list')?
[/quote]
That is correct.
Instead of writing:
[code]
int Nr0;
int Nr1;
int Nr2;
int Nr3;
[/code]
You can just write
[code]
int Nr[4];
[/code]
Working with the numbers is also very similar:
[code]
Nr1 = 5;
[/code]
will become:
[code]
Nr[1] = 5;
[/code]
In the above line I make the 2th number (counting starts from 0, so 1 is the second) equal to 5.
As you can see, working with arrays isn't that hard.
But if there is still something unclear, just ask.

Share this post


Link to post
Share on other sites
BCullis    1955
There are (being general here) three things you can put inside a class declaration:

Constructors/Destructors (which could be considered member functions, I suppose)
Data
Methods

Data would be the actual "values" of a class, member functions/class methods would be the functions that operate on and with that data. Take the following (in this case, C++):

[source]
class Spaceship
{
public:
Spaceship();
~Spaceship();

void fire_weapons();
int get_health();

private:
int health;
Player pilot;
Weapon mainGun;
};
[/source]

fire_weapons() and get_health() are member functions. Data members are health, pilot, and mainGun. Notice that data can be primitives (like int, bool, float) or other classes themselves, in which case they too will likely have member functions and data members of their own. This is used heavily in design that favors composition over inheritance, but you can put that idea aside until it comes up in your reading material.

You're spot-on with the arrays: the compiler needs to know what type of data or object the array is supposed to store, as it needs to allocate blocks of memory big enough to hold that type of data in each array cell. You can't fill an array with varying data types.

Share this post


Link to post
Share on other sites
Khaiy    2148
I'm assuming that you're using C++ based on your declaration of an array. It makes a difference though, since different languages have somewhat different approaches to things.

A data member is a piece of data that is included in a class definition. In your book's example, a spaceship is an object, which will always have a component called "energy". Every instantiation of spaceship will have its own energy member, however that is defined.

A member function is exactly what it sounds like-- it's a function that is a member of a class, in the same way that a data member is a part of a class. It's part of the definition, and every instantiation of the object will have the ability to call that function I believe more explanation of classes and data members and such are in your book. It's just further than one chapter ahead.

Being a member means that there is a strong relationship between the member and the object it's a part of. In your book's example, a spaceship can fire weapons, which takes energy. So a spaceship object must have its own energy, and a function to reduce that ship's energy when firing its weapon.

You're pretty much right on arrays. They are storage for data of a type, although what you can do with them is more important than what they "are".

It sounds like you're using an Intro to Game Programming with [Language] book (I'm still assuming C++, but all of those books are pretty much the same in content and approach). That's a fine book, but it's not in depth. You will need to work with the information presented in the book as it is presented if you don't want to look elsewhere for clarification. You don't sound like you're in the chapter on classes yet, so you shouldn't expect to get the full explanations about classes yet.

If you have a burning desire to know (and I applaud you if you do), go to the internet. There are tons of tutorial sites which are happy to explain basic concepts like classes. Cplusplus.com is a pretty good site, if I recall correctly.

Share this post


Link to post
Share on other sites
BCullis    1955
[quote name='Texus' timestamp='1306171970' post='4814659']
In the above line I make the 2th number (counting starts from 0, so 1 is the second) equal to 5.
[/quote]

That seemed a little unclear, (no offense), so just in case: array indexing always* starts at 0, so if you declare an array of size 4, like

int myIntegers[4];

You would access the first element in that array with myIntegers[0] and the last element would be at myIntegers[3]. If you try to access myIntegers[4] you either get an outOfBounds exception, or undefined behavior (depending on which language you're working with).



*to my knowledge, I'm sure there are languages out there that start their indexing at 1 but I have yet to work with one.

Share this post


Link to post
Share on other sites
JeremyYox    105
[quote name='Khaiy' timestamp='1306172360' post='4814664']
I'm assuming that you're using C++ based on your declaration of an array. It makes a difference though, since different languages have somewhat different approaches to things.

A data member is a piece of data that is included in a class definition. In your book's example, a spaceship is an object, which will always have a component called "energy". Every instantiation of spaceship will have its own energy member, however that is defined.

A member function is exactly what it sounds like-- it's a function that is a member of a class, in the same way that a data member is a part of a class. It's part of the definition, and every instantiation of the object will have the ability to call that function I believe more explanation of classes and data members and such are in your book. It's just further than one chapter ahead.

Being a member means that there is a strong relationship between the member and the object it's a part of. In your book's example, a spaceship can fire weapons, which takes energy. So a spaceship object must have its own energy, and a function to reduce that ship's energy when firing its weapon.

You're pretty much right on arrays. They are storage for data of a type, although what you can do with them is more important than what they "are".

It sounds like you're using an Intro to Game Programming with [Language] book (I'm still assuming C++, but all of those books are pretty much the same in content and approach). That's a fine book, but it's not in depth. You will need to work with the information presented in the book as it is presented if you don't want to look elsewhere for clarification. You don't sound like you're in the chapter on classes yet, so you shouldn't expect to get the full explanations about classes yet.

If you have a burning desire to know (and I applaud you if you do), go to the internet. There are tons of tutorial sites which are happy to explain basic concepts like classes. Cplusplus.com is a pretty good site, if I recall correctly.


[/quote]


You're right on both accounts, I'm using the Intro to Game Programming in C++. I just wanted to make sure I wasn't missing anything seeing as how the information was tossed out then dropped in a single page. I have four books based around introductory C++, the Intro to Game Programming, Sam's C++ in 24 Hours, Accelerated C++, and the Idiot's Guide - all of which I plan to go through at some point for redundancy sake. I'll be sure to look into those tutorials as well! I'm really hoping to get a firm foundation on all of this, but so far to be only 6 chapters in, I've met a fair share of 'what the...' moments.

All of your explanations have really helped. As far as classes and member functions and all that ... I'm beginning to see, but as you said I'm not even completely sure what classes are at this point. However, now that I know its not something I've missed, I can move on without my mind nagging me that I've overlooked a critical point. :)

Arrays seem pretty simple with your explanations, the book pretty much just gives an example and moves on to multidimensional arrays - but even those don't seem too bad now that I can decrypt them. I'll keep in mind that the use is more to concentrate on than the definition as I go. Again, thanks everyone.

Share this post


Link to post
Share on other sites
ryan20fun    2635
[quote name='BCullis' timestamp='1306172838' post='4814667']
[quote name='Texus' timestamp='1306171970' post='4814659']
In the above line I make the 2th number (counting starts from 0, so 1 is the second) equal to 5.
[/quote]

That seemed a little unclear, (no offense), so just in case: array indexing always* starts at 0, so if you declare an array of size 4, like

int myIntegers[4];

You would access the first element in that array with myIntegers[0] and the last element would be at myIntegers[3]. If you try to access myIntegers[4] you either get an outOfBounds exception, or undefined behavior (depending on which language you're working with).



*to my knowledge, I'm sure there are languages out there that start their indexing at 1 but I have yet to work with one.
[/quote]

Yes, you are right.
C++ and C# (and proberbly [b]most[/b] other languages) use Zero Based Indexing

Share this post


Link to post
Share on other sites
Nanoha    2682
[quote name='BCullis' timestamp='1306172838' post='4814667']
*to my knowledge, I'm sure there are languages out there that start their indexing at 1 but I have yet to work with one.
[/quote]

One I can think of is lua, which starts at 1.

Share this post


Link to post
Share on other sites
JeremyYox    105
One more question regarding multidimensional arrays. in the example multi[a][b] will 'a' always display across and 'b' down? That may not be very clear but in the 2d sense will 'b' always represent the ...height I guess...of the array? Like multi[5][9] means I'll be able to have 9 lines of 5 characters, or 5 rows and 9 columns?

Thanks again.

Share this post


Link to post
Share on other sites
BCullis    1955
[quote name='ebontide' timestamp='1306179278' post='4814709']
One more question regarding multidimensional arrays. in the example multi[a][b] will 'a' always display across and 'b' down? That may not be very clear but in the 2d sense will 'b' always represent the ...height I guess...of the array? Like multi[5][9] means I'll be able to have 9 lines of 5 characters, or 5 rows and 9 columns?

Thanks again.
[/quote]

(C++ here) It's actually backwards: your first index will be your vertical direction, growing DOWN, and the second will be your horizontal direction, growing right. This is because you're declaring an array of arrays, so given a MD array "int integers[2][3];" :

the contents of integers[0] is an entire array of length 3, same for integers[1]. Since arrays are contiguous in memory, the actual contents arrange like: [0][0], [0][1], [0][2], [1][0], [1][1], [1][2]. However, for the purposes of human thinking and the usual applications of a 2d array, you'd view it like this:

[0][0],[0][1],[0][2],
[1][0],[1][1],[1][2]

Meaning the top left corner of your coordinate space is 0,0 and the bottom right (given a MD array of size (y,x)) is y-1,x-1.

Share this post


Link to post
Share on other sites
JeremyYox    105
Ahh, I think I get what you're saying.

In your example of int intergers[2][3] it would first create one of two arrays (vertical) holding a set of three arrays (horizontal) that are filled.
Then, like a typewriter, it moves on to set two of two (vertical) filling in those three (horizontal).

Thats how I picture it, which seems to be exactly how you said. :)

Thanks for clarifying.

Share this post


Link to post
Share on other sites
BCullis    1955
[quote name='ebontide' timestamp='1306182681' post='4814733']
...holding a set of three [b]arrays [/b](horizontal) that are filled...
[/quote]

If you meant to say "holding a set of three [i]elements[/i]", then yes. Otherwise there are one too many arrays :) Basically, in a 2-dimensional array, each element of the outer array (the first bracket) is an array itself. So you can picture the outer array as the far left-hand column of the entire grid, hence why it operates as the vertical coordinate.

Share this post


Link to post
Share on other sites
JeremyYox    105
Ah okay, so the first value is the number of arrays -each getting their own line, the second is the number of values (elements?) those arrays contain? I'm not 100% on terminology which may be making this more of an issue than it needs to be. :)

Share this post


Link to post
Share on other sites
Khaiy    2148
[quote name='ebontide' timestamp='1306186458' post='4814765']
Ah okay, so the first value is the number of arrays -each getting their own line, the second is the number of values (elements?) those arrays contain? I'm not 100% on terminology which may be making this more of an issue than it needs to be. :)
[/quote]


Yes, as long as you're not too married to each array being a "line", like BCullis said. When accessing a particular element, the first index is which array you mean, and the second which element of that particular array. Array[4][9] would be the 5th array in your 2D array grid, and the 10th item in that 5th array.

For terminology, "element" is a generic term for a thing in an array. It could be an integer or float, for which the word "value" would be logical, or something like a user-defined class, for which "value" would not be a meaningful description.

Share this post


Link to post
Share on other sites
BCullis    1955
Yea, I'm probably running you in circles, I apologize.

A multidimensional array declared "int mdArray[5][9]" is an array of 5 elements, [i]each of these elements [/i]is an array of 9 ints. So you're saying "I want to declare a group of arrays. Specifically, the group is 5 large, and each of the arrays contained in that group is 9 ints large".

Then, when you access that 2d array with specific numbers like "mdArray[2][5] = 20;" you're saying:
"Go to index 2 of mdArray. Since the content at that index is an array itself, access index 5 of that array. That spot is what I want to give the value 20."

Hence why it can represent a 2d grid if you work with it in the typewriter metaphor you mentioned: each array can be visualized as its own line in a 2d grid, being written out in sequence. There will be 5 lines of length 9.

Just remember zero-based indexing ;)

Share this post


Link to post
Share on other sites
JeremyYox    105
Okay, I think I understand so long as I can picture it as a grid. I can really only see it as a (y, x) value for now but its enough to get my mind over the hurdle and able to move on. I appreciate you both going so far out of your ways to help me with this. Hopefully I'll learn more as I go. For now I think I'll move on and save some of your patience for my future questions (I give you 5 -10 minutes at best :P ).

Thanks a lot.

Share this post


Link to post
Share on other sites
JeremyYox    105
Alright, I lied. The book is giving the following example and I have no clue what is going on with the enumerations and arrays:

[code]// Word Jumble
// The classic word jumble game where the player can ask for a hint.

#include <iostream>
#include <string>

#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
{"jumble", "It's what the game is all about"},
};

srand(static_cast<unsigned int>(time(0))); int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; //word to guess
string theHint = WORDS[choice][HINT]; //hint for word

string jumble = theWord; // jumbled version of word
int length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}

cout << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;

string guess;
cout << "\n\nYour guess: ";
cin >> guess;

while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
}
else
{
cout << "Sorry, that's not it.";
}

cout << "\n\nYour guess: ";
cin >> guess;
}

if (guess == theWord)
{
cout << "\nThat's it! You guessed it!\n";
}

cout << "\nThanks for playing.\n";

return 0;
}[/code]

I'm really trying to understand why things work and how it all fits together but I can't seem to do it - so at this point I'm considering going to another book and starting over again to either cover missed points or reinforce basics. In the other books I own, arrays are taught fairly late, so maybe covering more ground prior is what I need, maybe another form of explanation - but as is...I'm at a complete loss on this one and feeling a bit defeated. :)

Share this post


Link to post
Share on other sites
oler1s    585
Don't just throw out "I don't get it". If you're completely unfamiliar with the syntactical constructs, you do need to go back to basics. Otherwise, go line by line and ask what you are unable to understand or figure out.

Share this post


Link to post
Share on other sites
JeremyYox    105
I have been asking about what I don't understand - and did point out that I'm having difficulties with the enumerations and arrays in the example. I appreciate the feedback and am going to read up on another book to strengthen my understanding of the fundamentals.

Again, thanks.

Edit: Woo! I honestly believe I've deciphered the code and understand it better for having mulled it over for a couple of hours trying to explain better what I didn't understand, ha. The main part that was throwing me off was the combining enumerations into MD arrays. Particularly:

[code] srand(static_cast<unsigned int>(time(0))); int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; //word to guess
string theHint = WORDS[choice][HINT]; //hint for word
[/code]

But, if I get this correctly, theWord is assigned a value from the 'WORDS' MD array of a value 'choice' = what random number is assigned from srand (using modulus you'll get a remainder 0-4 which matches the zero base indexing) which tells it which of the arrays to pull the first value from, and [WORD] or [HINT] (x, 0) or (x, 1) assign either the first or second value or the array to the string.

I may suck at explaining what I'm thinking but I really think its clicked now. I'll even go ahead and say thanks again to everyone for their explanations and to oler1s for calling me out on being a punk. :)

To Doopydoo, I've read those over a few times but for some reason they just didn't all add up in my mind. I got what they were trying to convey but when I saw them in action I couldn't piece it all together meaningfully. But now, with any luck, I think I have it nailed down.

Share this post


Link to post
Share on other sites
Doopydoo22    121
An additional resource that should help you out with the fundamentals: [url="http://www.cplusplus.com/doc/tutorial/"]http://www.cplusplus.com/doc/tutorial/[/url]

Enumerations specifically: [url="http://www.cplusplus.com/doc/tutorial/other_data_types/#enum"]http://www.cplusplus.com/doc/tutorial/other_data_types/#enum[/url]

Share this post


Link to post
Share on other sites
JeremyYox    105
Doopydoo, I think I hit my epiphany moments before you took the time to help, but I appreciate it nonetheless. If my assessment is wrong, please let me know and I'll grudgingly go back to trying to make heads or tails of it all. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this