web analytics

What is constructor?

Constructor:A constructor is a ‘special’ function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. A constructor is executed whenever object of its class is created. It is called constructor because it constructs the values of data members of the class.

A constructor is declared and defined as follows,

........................
class useconst{
int m,n;
useconst(); //constructor function declared
};
........................
useconst::useconst(){ //constructor function defined
m=1;
n=0;
}
........................
void main(){
useconst ob1; /*object created & constructor function has been automatically executed for this object*/
}
........................


Thus when a object is created the constructor function is also executed for that object. 
Please follow and like us:
Pin Share
RSS
Follow by Email
Scroll to Top