+ 1

C++ constructor under various access specifiers

I was asked to "fill in the blanks to declare a class Person with one private member variable age, and a constructor." The words that I unnecessarily capitalized were where the blanks were in the correct answer below. CLASS Person { PRIVATE: int age; PUBLIC: Person(int a) { age = a; }; I've noticed that the constructor is under the public access specifier. Would it be incorrect to switch the third blank (PUBLIC) to private? According to https://stackoverflow.com/questions/30995942/do-constructors-always-have-to-be-public constructors can be public, private, protected or default.

24th Jul 2020, 5:03 AM
Solus
Solus - avatar
1 Odpowiedź
0
Constructor can be private, public, protected also... So use it according to the need..
24th Jul 2020, 1:21 PM
Jayakrishna 🇮🇳