C++ Programming - Constructors and Destructors - Discussion

Discussion :: Constructors and Destructors - General Questions (Q.No.8)

8. 

Which of the following statement is incorrect?

[A]. Constructor is a member function of the class.
[B]. The compiler always provides a zero argument constructor.
[C]. It is necessary that a constructor in a class should always be public.
[D]. Both B and C.

Answer: Option D

Explanation:

No answer description available for this question.

Ravi said: (Aug 9, 2012)  
For option C, if a constructor is private, then what is the use of that if we can't create an object of that class.

If we try to create object of a class whose constructor is private, it will give error :: constructor () is private.

Myth said: (Aug 13, 2012)  
Can any one say how many argument is provided by compiler to a constructor by default.

Ghanshyam said: (Aug 19, 2012)  
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;
}

Kavitha Josephine said: (Aug 23, 2012)  
Is it necessary to specify access specifier to a constructor each time its created.

Nadim Ahmad said: (Sep 15, 2012)  
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;
}

Gaurav Kumar Garg said: (Oct 10, 2012)  
Constractor must be public. If it is not public then compiler will give error.

Amit Singh Gaurav said: (Oct 16, 2012)  
Constructor may be private. According to your need, in some scenario where you need to create objects depends on your need then you have to declare the constructor as private. (for ex in Singleton design pattern we use constructor as private).

Krish said: (Nov 13, 2012)  
What about the second option?Compiler by default provides a zero argument constructor only right?Can any one comment if compiler provides a parameterized constructor too.

Navin said: (Jan 9, 2013)  
Compiler by default provide public constructor. Constructor can be private. To implement singleton class we have to make constructor private. (Google for how to make singleton class).

Balaji said: (Feb 18, 2013)  
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.

Aaryya said: (Jun 28, 2013)  
Answer provided here is wrong because constructor must be declared in public section.

Ashish said: (Sep 3, 2013)  
Compiler provides default constructor with no argument.

Then how can 2nd option be incorrect?

Kapil Bablani said: (Oct 25, 2013)  
Construtor cannot be private nor protected. It can only be public. Its a fact.

Radhika said: (Nov 4, 2013)  
Constructor is a special member function of class. So ALL the statements are correct.

Amit Kumar Giri said: (Mar 19, 2014)  
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.

Manish said: (Aug 18, 2014)  
Can we create static constructor?

Alex said: (Sep 9, 2014)  
Have you heard of Singleton pattern? The constructor is private and the object is created via a static function.

Pran said: (Sep 27, 2014)  
How this answer is possible, as a constructor is always defined in public section of a class ?

Prem said: (Mar 1, 2015)  
Compiler does not always provide zero value constructor. It does provide one such constructor when no other constructors are defined.

Abirami said: (Mar 14, 2015)  
The option B will be correct as the compiler will not provide default constructor when user defined any constructor.

Ashutosh Londhe said: (Apr 25, 2015)  
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: (May 27, 2015)  
How can constructor can't be private? If we consider design patterns, for singleton pattern implementation, constructor should be private.

Sundar said: (Jun 16, 2015)  
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: (Jun 26, 2015)  
When we want to execute the particular part of the program we can declare constructor as private or protected.

Jj A said: (Aug 6, 2015)  
It can be any of them.

Sujata said: (Oct 22, 2015)  
But compiler can automatically provide the default constructor.

Daniel Sandor said: (Nov 20, 2015)  
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: (Nov 25, 2015)  
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: (Oct 7, 2016)  
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: (Feb 25, 2017)  
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!

Yudi said: (Oct 2, 2018)  
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.)

K Goyal said: (Jan 15, 2019)  
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;
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.