C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - General Questions (Q.No. 8)
Which of the following statement is incorrect?
Constructor is a member function of the class.
The compiler always provides a zero argument constructor.
It is necessary that a constructor in a class should always be public.
Both B and C.
Answer: Option
No answer description is available. Let's discuss.
34 comments Page 2 of 4.

Kishore said:   9 years ago
When we want to execute the particular part of the program we can declare constructor as private or protected.

Sundar said:   9 years ago
Option C is correct.

1) Compiler doesn't ALWAYS provide a zero-argument constructor. It provides a zero-arg constructor only when the user hasn't defined any constructor explicitly.

2) Constructor need not be public always. If it private/protected we can create another public member function which can call the private/protected constructor.

The keyword "ALWAYS" changes the answer here.

Utsav said:   9 years ago
How can constructor can't be private? If we consider design patterns, for singleton pattern implementation, constructor should be private.

Ashutosh Londhe said:   9 years ago
Compiler always provide parameter-less constructor, so option B is correct. But we can declare constructor as private so that we can create object of class outside member function for ex:constructor and copy constructor in singleton class is private.

Abirami said:   9 years ago
The option B will be correct as the compiler will not provide default constructor when user defined any constructor.

Prem said:   9 years ago
Compiler does not always provide zero value constructor. It does provide one such constructor when no other constructors are defined.

Pran said:   10 years ago
How this answer is possible, as a constructor is always defined in public section of a class ?

Alex said:   10 years ago
Have you heard of Singleton pattern? The constructor is private and the object is created via a static function.

Manish said:   10 years ago
Can we create static constructor?

Amit Kumar Giri said:   1 decade ago
My dear friends. Option D is correct.

Compiler by default provide public constructor. Constructor can be private. To implement singleton class we have to make constructor private. It is one of the design pattern.

Compiler provides default constructor with argument. In case of copy constructor called, by default compiler provides the constructor with the argument type of that class.

Post your comments here:

Your comments will be displayed after verification.