Explanations please? Arrays, data members, member functions

Started by
19 comments, last by JeremyYox 12 years, 10 months ago
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.
Advertisement

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:

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.
TGUI, a C++ GUI for SFML
texus.me
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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~


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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)


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.

[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

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.


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

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

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.

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement