Constructors are an important part of a class/struct. Think of it as a function that automatically fires the moment you initialise a class. Let's go straight into an example since that is the best way to show it.
Example: Setting private variables to 0
I want a struct called "point_3D" which stores coordinates x, y, and z as doubles.
Code (C++)
struct point_3D {
double x, y, z; //Variables
};
When I initialise the struct, the values are not declared. So let's make a constructor that will set all three axis points to 0.
Code (C++)
struct point_3D {
point_3D(); //Constructor
double x, y, z; //Variables
};
Congratulations, you prototyped it. Notice, it doesn't return anything. In fact,
a return type isn't even there. If you try to compile it now, it will fail. Wonderful, let's actually define this constructor.
Code (C++)
point_3D::point_3D() {
x = 0;
y = 0;
z = 0;
}
Now try compiling the code. It will work! And furthermore, it will set all of the values to 0 when an instance of "point_3D" is initialised.
Continued Example: Constructors that take variables
So you initialise "point_3D" and the values are set to 0. That's fine and all, but I would then have to set the values manually by going into the struct and go "point.x = ??;", and so forth. That can get very messy. You can make constructors that take arguments, since they are functions. So let's make a constructor that takes 3 doubles and sets x, y, and z to them.
Code (C++)
struct point_3D {
point_3D(); //Constructor
point_3D(double, double, double); //Also a constructor
double x, y, z; //Variables
}
So you probably noticed that there are two functions named "point_3D". Yup, don't freak out.
Functions can have the same name as long as the variable types you throw in are different. So I can have 2 functions called "add" where one takes 2 ints, and another takes 2 doubles and have them do two completely different things.
Moving on though, let's define the second constructor.
Code (C++)
point_3D::point_3D(double a, double b, double c) {
x = a;
y = b;
z = c;
}
So what do we have now? We have two constructors for the struct "point_3D" which set x, y, and z automatically.
Continued Example: Declare a struct or class with constructor that takes variables
So you have your constructor that takes multiple arguments, but you probably have no idea how to actually use it. Let's look at this main function:
Code (C++)
int main() {
point_3D p; //Initialises p
}
The above code will initialise "p", and set p.x, p.y and p.z all to 0 because it calls the constructor function. No questions asked. Now how about that second one that sets points automatically?
Code (C++)
int main() {
point_3D p(1, 2, 3); //Initialises p with predefined points
}
This is how you call it, and it will initialise "p" to set p.x to "1", p.y to "2", and p.z to "3". Now you probably also see why the constructor has no return value. There is nothing the value can return to.
I bring up constructors right now because the next section talks about Vectors, which is a class with its own constructors to help make your code look much cleaner.