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.

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.)

Nadim Ahmad said:   1 decade ago
Guys it is not mandatory to constructor should be always public. in general it is public, but as our of need we can declare as private also. see example.

-----------------------------------------------------------

#pragma once
#include <iostream>
using namespace std;

class Sample
{
private:
Sample(void); // Private Constructor
public:
~Sample(void); // Destructor
static void CreateInstant(void); // Static function
};

Sample::Sample(void) // Ctor implementation
{
cout<<"Object Created...!!!\n";
}

Sample::~Sample(void)
{
}

void Sample::CreateInstant(void)//Static function implementation
{
Sample S;
}

int main() // main
{
Sample::CreateInstant(); calling using class name
return 0;
}

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.

Ghanshyam said:   1 decade ago
Constructor must always public only...
define it with private or protected always cause compile time error...
try following code and check..
/* Note: GCC Compiler (32 Bit Linux Platform). */

#include<iostream.h>
class one
{
private:
int a;
protected:
one(){a=0;}
public:
void show()
{
cout<<"yes..";
}
};

int main()
{
one obj;
obj.show();
getch();
return 0;
}

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;
}

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.

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.

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.

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.

Balaji said:   1 decade ago
Yes,

The compiler provides a default constructor with 0 arguments,

It is the one through which objects are constructed when we don't explicitly provide constructors.

Note:

We should compulsory provide constructors with 0 arguments when we create constructors with multiple arguments.


Post your comments here:

Your comments will be displayed after verification.