0
Why constructor name is similar to the class name?
3 ответов
+ 1
constructor is controlled by class.
class a{}
class b{
b(){} }
when a class a is calls class b before starting execution of any function in class b constructors are first excuted.
since constructors are used for calling private functions of other class or to reassign variable.
so they should get excuted 1st.
for this reason and to differentiate from other functions .
class name n constructors name are same
0
when constructor is not defined manually then default constructor is created automatically to initialize variables. Every class has constructor.
How would you know which constructor is for which class when you'll be working with multiple classes.. ?
For the sake of simplicity constructors are of same name as class... !
0
If, by convention, the compiler insists that all methods with the same name as the class are constructors of that class, it means you don't have to explicitly identify methods as being constructors. In other OOP languages, there is a "constructor' keyword in the method signature and the constructor method can have any name. This can be useful because you can then name constructors more meaningfully, instead of relying on the different parameter lists (of overloaded constructors) to identify a constructor's purpose.