C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - General Questions (Q.No. 8)
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
Explanation:
No answer description is available. Let's discuss.
Discussion:
34 comments Page 1 of 4.

Daniel Sandor said:   9 years ago
A class without any public constructor is not totally pointless, if it is a base class, and it has a protected constructor. Its' child class may be instantiated.

A class without any public or protected constructor is totally pointless. Neither this class nor its' child class is instatiatable, because the constructor of the child class must call the constructor of the base class, which is inaccessible from the child class if that constructor is private.

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

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

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.

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.

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.

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

JJ A said:   9 years ago
It can be any of them.

Sujata said:   9 years ago
But compiler can automatically provide the default constructor.

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


Post your comments here:

Your comments will be displayed after verification.