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.

Arjun Mandavkar said:   2 years ago
@Amit Kumar Giri.

Your Explanation is very accurate. Thanks.

Wild said:   3 years ago
Constructor is a member function!

K Goyal said:   5 years ago
B is wrong because if we have defined one or more argument constructor then the compiler will never provide zero argument constructor we have to provide it explicitly.

#include <iostream>
#include <string>
using namespace std;

class name
{ public:
name(string s)
{cout<<s<<endl;
}
};

int main()
{ name s1("abc");
name s2; // complier will show an error -> we must add constructor name(){ ...} too.
return 0;
}

Yudi said:   6 years ago
Using singleton the constructor could be private.

e.g:
class TestSingleton{

private:
static TestSingleton * mInstance;
int mValue;
TestSingleton() {
mValue = 0;
}
public:

static TestSingleton *get_instance(){
if (TestSingleton::mInstance == nullptr){
TestSingleton::mInstance = new TestSingleton();
}
return TestSingleton::mInstance;
}

void set(int value){
this->mValue = value;
}

int get(){
return this->mValue;
}
};
TestSingleton *TestSingleton::mInstance = nullptr;


int main()
{
TestSingleton * sin = TestSingleton::get_instance();
TestSingleton * sin2 = TestSingleton::get_instance();
sin->set(10);
printf("value = %d\n", sin->get());
printf("value = %d\n", sin2->get());
}

Answer C is wrong (It is necessary that a constructor in a class should always be public.)

Zbik said:   7 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!

Gaurank Verma said:   8 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.

Divya said:   8 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.

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

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

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


Post your comments here:

Your comments will be displayed after verification.