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 2 of 4.
Divya said:
10 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.
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:
9 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.
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:
9 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!
Yudi said:
7 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.)
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:
7 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;
}
#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;
}
Wild said:
4 years ago
Constructor is a member function!
Arjun Mandavkar said:
4 years ago
@Amit Kumar Giri.
Your Explanation is very accurate. Thanks.
Your Explanation is very accurate. Thanks.
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.
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;
}
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;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers