+ 1
[solved]Check this code in cpp
Why the constructor needs to be public and why the code throws error if I changed "Time" to "time" everywhere? https://code.sololearn.com/cdRdO0SRY42j/?ref=app
14 odpowiedzi
+ 7
Actually in Java there are access modifiers for constructors. Java constructors are public by default but you can set them private. In c++ you should give a explicit access specifier for constructors.
For me there was no reason to do so (private constructors).
But then I read this:
https://www.google.com/amp/s/www.geeksforgeeks.org/can-constructor-private-cpp/amp/
I found some real life examples of private constructors.
+ 5
HonFu 🤷🏻♂️
But...
https://code.sololearn.com/c38cIW82xBr2/?ref=app
+ 5
HonFu You are 100% right it's Sololearn 👼
+ 5
HonFu 🤫I googled it
+ 4
Public members may be accessed from anywhere outside of the class, while access to private members is limited to their class and friend functions.
Your constructor must be public in order to call it outside your class.
time is not(Thank you HonFu for pointing it out) a native function/built-in of the language.
+ 2
Kevin Star, but doesn't it belong to std?
I tried by deleting the 'using std' line, but it still didn't work ...
+ 2
Hm, is it maybe a Sololearn thing?
Time isn't really a keyword, but a function, from ctime, isn't it?
So normally, in my thinking, it should sit in std.
But Sololearn environment sometimes seems to auto-include stuff...
+ 2
Kevin Star, did you cross-check on your PC?
+ 2
Hacker!! 😁
+ 2
Kevin Star But in Java also we see that there is nothing like access modifiers for constructors. Because they should always be called whenever an object is created 🤔...
Also thanks for that time keyword
+ 2
Thanks HonFu
+ 2
Albert Margaryan, please don't use the question threads of other people for your advertising!
+ 2
Thanks Kevin Star
+ 2
The constructor has to be public to make it visible outside the class you have declared it.
If it were private you cannot call it from inside the main...