Prior to C++ programming or Object oriented programming; procedural languages such as C language were used. Procedural language program have typical structure where variables needs to be declared to hold data and functions need to be declared to operate on these variables. To share variables among various parts of program (or with number of functions) either one needs to pass variables as parameters or should declare variable as global.
In every programming scenario any variable before using it must be initialized to some values. Reason is anything declared but not initialize may contain garbage or junk values. Performing operations on such values may bring your program down.
So original code author may write some function to initialize these variables or he will initialize at the places where “He” is using them before actually performing some operation. Think about the programmers other than original code author, there are pretty good chances that he will use them without initialising or if there is any function to initialize them, might skip to call it. This will run your program into generating incorrect output, abnormal behaviour or in the worst case program crash.
So there must be a way to initialize the variables always. Or some initialization function must be called while creating variables. C++ programming language provides a concept called constructors. Constructors guarantee that they will be called as soon as an object of class is created to initialize its member variables. They are named as constructors because they are getting called when an object is constructed.
I will describe more on constructor in subsequent blog posts.