C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - General Questions (Q.No. 19)
19.
Which of the following statements is correct?
Data items in a class must be private.
Both data and functions can be either private or public.
Member functions of a class must be private.
Constructor of a class cannot be private.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
12 comments Page 1 of 2.

Soumya G said:   2 years ago
A constructor can be private and we can create an object for that class using the friend class.

Suphoebe said:   5 years ago
A constructor can be Private. For example: singleton mechanism.

Nagarjuna said:   6 years ago
For providing data security to make data and function must be either public and private in a single class private and protected acts as same, So option B is correct.

Ilayaraja said:   6 years ago
I agree, thanks @Soumya Raj.

Mayur said:   8 years ago
Because the caller of constructor is compiler, and compiler is 3rd party so it constructor should not be private.

Limon said:   8 years ago
A private constructor is a special instance constructor. It is generally used in classes that contain static members only. So (D) cannot be the correct answer.

Sam said:   8 years ago
If a Constructor is Private then the object created in the main function cannot be initialized as private members of a class cannot be called by the main() function.

So, Constructor cannot be Private.

And sorry for offending @ Jay. But explain me how a Private Constructor is useful to construct an Object. How come you can create an Object in a Class without creating the actual instance of that Class.

ROUA said:   9 years ago
I know that Constructor cannot be private. But how?

Soumya Raj said:   10 years ago
The answer (B) is incorrect. Because any data and function.

Within the class can be public, protected or private, they aren't restricted only to be private or public. The access specifier 'protected' isn't mentioned in option (B). Hence the option (B) is either partially correct or incorrect.

Whereas option (D) is correct. Because it isn't possible to declare constructor as either private or protected. It must be declared as public. Hence the statement of option (D) is totally correct.

Here the correct answer of question (19) is:

D. Constructor of a class cannot be private.
(1)

Uday said:   1 decade ago
Both data and functions can be either private or public.

Why not protected please explain any one?


Post your comments here:

Your comments will be displayed after verification.