Public Group

# Explanations please? Arrays, data members, member functions

This topic is 2612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites

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.
 int Nr0; int Nr1; int Nr2; int Nr3; 
You can just write
 int Nr[4]; 
Working with the numbers is also very similar:
 Nr1 = 5; 
will become:
 Nr[1] = 5; 
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 on other sites
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 on other sites
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 on other sites

In the above line I make the 2th number (counting starts from 0, so 1 is the second) equal to 5.

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 on other sites

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.

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 on other sites

[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.

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 most other languages) use Zero Based Indexing

##### Share on other sites

*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.

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

##### Share on other sites
One more question regarding multidimensional arrays. in the example multi[a] 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 on other sites

One more question regarding multidimensional arrays. in the example multi[a] 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.

(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.

1. 1
2. 2
Rutin
24
3. 3
JoeJ
19
4. 4
5. 5

• 17
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631730
• Total Posts
3001917
×