Explain with an example why constructor is important.
The constructor is the initialization point of the any newly created object. It is a kind of member function, which will be executed at the same moment when any new object of any class has been created.
It is has the exactly same name as class object. It does not have any return type. It plays important role in the initial values for certain member variable function.
Class Student{
public:
void studentDetails(){}
//default constructor
Student(){}
//parametrized constructor
Student(string name, string roll_number){
this->name=name;
this->roll_number=roll_number;
}
}
Comments
Leave a comment