C++ Programming - Constructors and Destructors - Discussion
Discussion Forum : Constructors and Destructors - General Questions (Q.No. 8)
8.
Which of the following statement is incorrect?
Discussion:
34 comments Page 3 of 4.
Radhika said:
1 decade ago
Constructor is a special member function of class. So ALL the statements are correct.
Kapil Bablani said:
1 decade ago
Construtor cannot be private nor protected. It can only be public. Its a fact.
Ashish said:
1 decade ago
Compiler provides default constructor with no argument.
Then how can 2nd option be incorrect?
Then how can 2nd option be incorrect?
Aaryya said:
1 decade ago
Answer provided here is wrong because constructor must be declared in public section.
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.
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.
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).
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.
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).
Gaurav kumar garg said:
1 decade ago
Constractor must be public. If it is not public then compiler will give error.
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;
}
-----------------------------------------------------------
#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;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers