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 3 of 4.

Ashutosh Londhe said:   1 decade 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:   1 decade ago
How can constructor can't be private? If we consider design patterns, for singleton pattern implementation, constructor should be private.

Sundar said:   1 decade 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:   1 decade ago
When we want to execute the particular part of the program we can declare constructor as private or protected.

JJ A said:   1 decade ago
It can be any of them.

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

Daniel Sandor said:   10 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.

Divya said:   10 years ago
A is true, from the standard (paraphrased). The default constructor is a special member function. Special member functions are still member functions.

B is false. The compiler only provides a zero member default constructor if you don't provide a constructor yourself.

Example:

class B {
private:
int i;
public:
B(int x) { i = x; }
};

int main() {
B b; // COMPILER ERROR HERE...
return 0;
}

c) is also false. The following is valid c++
class A {
A() {}
};


@Daniel Sandor. I think you're wrong when you say that: "A class without any public or protected constructor is totally pointless".

You can do this and create instances using public factory methods.

Gaurank Verma said:   9 years ago
Here option (B) seems to be ambiguous.

The compiler will provide zero argument constructor by default only when we haven't declared any parametrized constructor.

But.

If has declared any parametrized constructor. In our C++ program, then in that case compiler won't provide any zero argument constructor by default. There will be a compilation error.

Zbik said:   9 years ago
It seems to that the answer B is incorrect. Let us assume that we define a constructor with parameters. In such situation a compiler would not generates the default constructor!


Post your comments here:

Your comments will be displayed after verification.