Constructor
When an object of a certain class is created a function called constructor is
called. The operations performed by such a function can be specified. This
usually turns out to be necessary when some form of data initialization or
other operation is required at the moment the object is created.
The constructor's name is the same as the class' name.
Example
#include <iostream>
using namespace std;
class intClass
{
int a;
public:
intClass(int x); // constructor prototype. Constructors
// do not return values.
};
intClass::intClass(int x) // constructor code.
{
a = x;
}
int main()
{
intClass obj1(12); // declares an object of type intClass.
// The argument required
// by the constructor is passed between
// parentheses as shown.
}