What is constructor?

Last updated on June 13th, 2020 at 08:34 pm

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. 

Leave a Reply

Your email address will not be published. Required fields are marked *