CLASS EXPLANATION
To understand classes, first, understand structs.
A struct is a collection of several different variables. The idea is they are logically linked so that you would always want to treat them as though they were in a group.
For example:
1 2 3 4 5 6
|
struct AlienStats
{
std::string name;
int life;
int exp;
};
|
Notice: You declare the variables inside the struct. These are called "members", as they are members of the struct. This means that every "instance" of the struct, or every "object", will have its own copy of those members.
You create objects just like you create normal variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int foo; // <- a normal variable named 'foo'
AlienStats bar; // <- an AlienStats object named 'bar'
// since 'bar' is an object, it has its own copy of 'name', 'life', and 'exp'
// You can access bar's members with the dot (.) operator
bar.name = "Jeff";
bar.life = 6;
bar.exp = 100;
// You can create any number of different objects, and each object will have its own copy of
// its members
AlienStats baz; // <- baz.name is a different string from bar.name, etc.
// What you CAN'T do, is try to access name/life/exp without an object
// An object must be left of the dot (.) operator to access individual members:
name = "Booger"; // <- this doesn't make sense... WHOSE name? You have to give it an object. This is an ERROR
bar.name = "Booger"; // <- bar's name! This is OK
|
A class is the exact same idea, only they're usually coupled with member functions which act on the current object (also known as "this" object, as "this" is a special keyword in C++ to refer to the current object).
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
class Alien
{
private:
// member variables: things that EACH alien has:
std::string name;
int life; // <- how much life this alien has
bool alive; // <- whether or not this alien is alive
public:
// member functions: functions which act on an individual object / individual Alien
void hurt() // <- function to hurt this alien
{
life -= 1; // take some of 'this' alien's life away (note: keyword 'this' explained more below)
if(life <= 0)
alive = false; // <- no more life = dead
}
};
// you could then use this class like so:
Alien biff;
biff.hurt(); // <- hurt the biff Alien (and if his life drops below zero, kill him)
// but again... you always need an object:
hurt(); // <- doesn't make sense. Who are you hurting? ERROR
Alien::hurt(); // <- also doesn't make sense. Which Alien are you hurting? ERROR
|
But wait.... up above when I was talking about structs I told you that you couldn't access members unless they had an object with a dot operator.
1 2
|
bar.name = "kdlsfjd"; // OK
name = "whatever"; // ERROR
|
BUT, in my example for the hurt() function, I use 'life' and 'alive' without using an object! Isn't that an error?
No! Because we
do have an object, it's just hidden. Since hurt() is a
member function (that is, it is inside the class), there is a hidden 'this' object. We can access members without specifying an object, and the compiler will assume we are using 'this' object's members:
1 2 3 4 5
|
Alien ab; // a couple of Alien objects
Alien cd;
ab.hurt(); // <- hurt the ab object. This will call hurt() with this=ab. So it will subtract from ab.life and set ab.alive
cd.hurt(); // <- This will call hurt() with this=cd, so it will subtract from cd.life, etc.
|
Hopefully that explains it a bit better. Feel free to ask if you have more questions or if anything is unclear.