The basic idea behind classes is to group variables which belong together. For example, Vec2 has the values x and y which together make up a 2D position. Without this class, you would have to declare two separate floats to remember a position.
A software library is, for the most part, a collection of classes that work well together. The library developer provides classes which can be used to make your job easier. It would be hard to imagine a game without positions, so it makes sense that Esenthel engine provides you with a class to store such a position. In a most basic form, this class could look like this:
class Vec2 {
float x;
float y;
}
5
When this class is defined, it can be used anywhere in your application:
Vec2 pos;
Vec2 pos2 = pos; // assigns the values of pos to pos2
pos.x = 3; // change the x value of pos
pos.y = pos2.x; // assign the x value of pos2 to the
5 // y value of pos
In every application except from the small exercises we have done in the previous chapters, you should design your own classes. Almost every aspect of your program should be contained within a class. So it’s about time you learn more about them.
Please note that there’s a difference between a class and an object. In the example above, Vec2 is a class, but pos and pos2 are objects of this class. You cannot use Vec2 as if it were an object, but you can use this class to create one:
Vec2.x = 3; // wrong!
Vec2 pos;
pos.x = 3; // correct.