contrcutor
Hi can any one tell me why we intilized the mostly class level members through contrcutor....?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted Apr 30, 2012, 10:36 AM
The constructor is used to initialize the member variables in the class. Because every class will have the default constructor.
When we try to assign the values to the member variables out side the class then constructor is prefer method. Even it supports to do constructor overloading.
There is no need of writing a method to assign the values, because it can be assign the values at the time of creating object to the class.
Thats why constructor is preferred.
VulpesPosted Apr 30, 2012, 7:04 AM
Krishna GaradPosted Apr 30, 2012, 6:56 AM
because cunstructor is the member of the class which executes before starting execution of the other class member means cunstrocture executes when we create the instance of the class and if we need to initilize any another member of the class we can do it in custrocture. This is in case of public cunstrocture but in case of static cunstrocutur we can not say when it will execute but it's fix it will execute before any other member execution starts.