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.

Ravi said:   1 decade ago
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:   1 decade ago
Can any one say how many argument is provided by compiler to a constructor by default.

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

Kavitha josephine said:   1 decade ago
Is it necessary to specify access specifier to a constructor each time its created.

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

Gaurav kumar garg said:   1 decade ago
Constractor must be public. If it is not public then compiler will give error.

Amit Singh Gaurav said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   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.