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. 

Author: Tanmay Chakrabarty

Tanmay Chakrabarty is a former CSE student, currently working as a Senior Software Engineer with 5+ years of experience in the field of Web Application development in PHP+MySQL platform with strong skills in Javascript, JQuery, JQuery UI and CSS. He tries to write notes every week but fails due to heavy loads of duty.

Recommended Recommends

Comments

Contact Us