@Chaosenemy:
Lets take that spaceship example. Instead of using classes couldn't you just declare variables like int sp_armor and int sp_speed, then make a function that could be used when any ship accelerates? It would be the same thing...
You can... but your code would be out of control. The beauty of having an object is that all of your variables relating to that object (sp_armor and sp_speed) are all located inside one place. What would you do if you wanted to have two spaceships? You have have to create int sp_armor1, int sp_speed1, int sp_armor2 and int sp_speed2. What if you wanted to have 100 spaceships? Your code would have so many variables and functions, you would probably give up on the project. Creating classes allows you to create one class, and then create multiple instances of that class, all of which have their own properties. Its kind of like opening up notepad and writing two different text files. They are both text files, but they have different data, and different properties.
@sum1nowhere:
At the risk of sounding like an idiot... I'm going to have to say that I still do not understand this. What is the difference between a class and a function?
You don't sound like an idiot. Thats a legitimate question.
A class encapsulates both functions and variables into one thing -- an object.
Classes contain only variables and functions that relate to the overall design of your object. Say we are making a game, and we have a creature call a turtle. Our turtle has health, and can walk. Lets look at the overall design of the turtle:
Turtle:
Health
Walk
Because walking requires that multiple statements be run, that would be a function of the turtle. Because health is a scalar variable, we would make that a simple integer variable. Lets look at the turtle in class form:
Code:
class turtle{
public:
void walk(){
//Increment Turtles Position
}
private:
int health;
}
Do you see how our turtle object combines both integer and functions? It differs from functions because it isn't just called and then destroyed. When we call an object, it remains in the program until we destroy it. It takes functions that are relevant to the object, and places them all in one place.
I hope this explains it a little better. If not, try reading this article I wrote on my website:
http://www.digitalbrink.com/articles/index.php?article=understanding_classes.html