[solved]Check this code in cpp | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Mar 2020, 7:19 PM
Akshay Jain
Akshay Jain - avatar
14 Answers
+ 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.
4th Mar 2020, 2:01 PM
Kevin ★
3rd Mar 2020, 7:52 PM
Kevin ★
+ 5
HonFu You are 100% right it's Sololearn 👼
3rd Mar 2020, 8:06 PM
Kevin ★
+ 5
HonFu 🤫I googled it
3rd Mar 2020, 8:35 PM
Kevin ★
+ 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.
3rd Mar 2020, 7:43 PM
Kevin ★
+ 2
Kevin Star, but doesn't it belong to std? I tried by deleting the 'using std' line, but it still didn't work ...
3rd Mar 2020, 7:45 PM
HonFu
HonFu - avatar
+ 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...
3rd Mar 2020, 7:56 PM
HonFu
HonFu - avatar
+ 2
Kevin Star, did you cross-check on your PC?
3rd Mar 2020, 8:16 PM
HonFu
HonFu - avatar
+ 2
Hacker!! 😁
3rd Mar 2020, 8:37 PM
HonFu
HonFu - avatar
+ 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
4th Mar 2020, 3:06 AM
Akshay Jain
Akshay Jain - avatar
+ 2
Thanks HonFu
4th Mar 2020, 3:07 AM
Akshay Jain
Akshay Jain - avatar
+ 2
Albert Margaryan, please don't use the question threads of other people for your advertising!
4th Mar 2020, 12:35 PM
HonFu
HonFu - avatar
+ 2
Thanks Kevin Star
4th Mar 2020, 7:39 PM
Akshay Jain
Akshay Jain - avatar
+ 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...
5th Mar 2020, 10:29 AM
root@kali:~#
root@kali:~# - avatar