+ 1
Do we need to make a constructor every time?
hi I'm reading OOP for the first time. I have a question, do we need to create a constructor every time?? why do we need to make constructor??
10 ответов
+ 2
No, it depends on your purpose. If you do not want to create objects, no need to use constructor
+ 2
If you have no costructor you can't instantiate any object (with the new keyword). You still can use static methods (methods that don't need to instantiate the class), but you can't do many things
+ 1
I'm sorry for asking silly questions but I'm kinda confuse, I have assignment question to make a class circle and find it's area.
I did that and used a constructor, my colleague did it without creating any constructor.
so what is the best practice??
+ 1
How did your colleague doesn't use any constructor? Did he done it in the main method (in java)? What language are you using?
+ 1
he did it in c++
which is the correct approach ?
+ 1
There is no wrong approach but I think the task asked you to have a costructor. You're surely right but I'm not sure you're colleague is right, too
+ 1
thank you all.
+ 1
There is always a constructor, but in some cases you don't have to write it explicitly
If there is "no" constructor, there is actually an implicit one.
Same with destructors.
+ 1
In Java if you specify no constructor then a default (no-arg) constructor is created by the compiler. (I expect something similar occurs in C++) so this may be the source of your confusion.
The point of a constructor is to ensure an object is initialized into a correct/valid state, as specified by the programmer. If there is no need to specify this, then a default constructor is provided
0
No