What is the proper decent way to use constructors (default as well). what purpose do they serve?
I can use constructors on classes, but I am only retyping code not really sure what it means
Thanks
What is the proper decent way to use constructors (default as well). what purpose do they serve?
I can use constructors on classes, but I am only retyping code not really sure what it means
Thanks
Ok so I use a constructor to ensure that there isnt junk values stored in a variable. rather use the constructor to initialize a variable to zero than a random value like 43535
Or whatever you want them to be before starting to use the instance of the class.
The variables have a meaning and it's the constructor's job to make sure they get their meaning and not have some junk stored inside them.
But this is not all a constructor can do.
Say there's some API you want want to call inside the class.
Your class's constructor can call the initialize function for the API so the rest of the class can call and use that API later.
The destructor will call terminate, if there's such a function in the API.
It depends on your need - but the constructor's job is to initialize the class state to make it ready before using it.