C++ Programming - Constructors and Destructors - Discussion

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

2. 

What happens when a class with parameterized constructors and having no default constructor is used in a program and we create an object that needs a zero-argument constructor?

[A]. Compile-time error.
[B]. Preprocessing error.
[C]. Runtime error.
[D]. Runtime exception.

Answer: Option A

Explanation:

No answer description available for this question.

Kavita said: (Aug 20, 2012)  
Constructor errors are always genrated at compile time. If we write a constructor, then compiler only deals with that constructor, not go for default constructor. So wehen we create a object having no parameter. It generates error.

Lalit said: (Oct 12, 2012)  
Because, when we build an object, then the constructor is call.

Naveen said: (Nov 24, 2012)  
It depends on compiler also. Some compilers allow us to create a class without default constructors and it will created automatically during run time.

Navin said: (Jan 9, 2013)  
I agree with Naveen. Compiler use to create default constructor if default constructor is not provided.

Vishal said: (Feb 18, 2013)  
Output : Compile-time error

Explanation: By default Compiler provides zero argument constructor & copy constructor implicitly, if we not define them explicitly. Compiler will not call default constructor(zero argument constructor), if we define parameterized constructor explicitly. Thus creating class object without parameters gives compile time error.

Note: Default Constructor is a constructor with no parameters (or) all parameters of the constructor with default arguments.

class X
{
private:
int x;
public:
X(int m) // Parameterized constructor
{
x = m;
}
};

int main()
{
X p; // error
X q(10); // No error

return 0;
}

Solution:
(1) Define a zero argument constructor.

class X
{
private:
int x;
public:
X() // Default constructor(zero argument constructor)
{
}
X(int m) // Parameterized constructor
{
x = m;
}
};

int main()
{
X p; // No error
X q(10); // No error

return 0;
}

(2) Define a Parameterized constructor with default arguments.A constructor with all parameters having default values known as default constructor

class X
{
private:
int x;
public:
X(int m=0)// Parameterized constructor with default arguments
{
x = m;
}
};

int main()
{
X p; // No error
X q(10); // No error

return 0;
}

Minhaj said: (Jul 19, 2013)  
Constructor errors are always generated at compile time. If we write a constructor, the compiler only deals with that constructor, not go for default constructor. So when we create a object having no parameter. It generates error.

Pragya said: (Aug 16, 2013)  
Compile time error. Because constructor error always generated at compile time. Compiler use to create default constructor if default constructor is not provided.

Salman Khan said: (Sep 11, 2013)  
Why error is coming in c++?

Dwarika said: (Sep 15, 2013)  
Compile time error, because constructor error generally generated at compile time.

Umakant Rahane said: (Dec 12, 2013)  
Basically that constructor error occur at compile time. But the que is that When that error occurs? The answer is when we called constructor explicitly within the body of any function then at that time compile error occur. So the answer is compile error.

But in other case. It depends on compiler also. Some compilers allow us to create a class without default constructors and it will created automatically during run time.

Bharat said: (Jan 8, 2014)  
#include<iostream.h>
#include<conio.h>
class bharat
{
private:
int a;
public:
bharat(int b)
{
a=b;
cout<<"values is"<<b<<endl;
}
};
int main()
{
bharat b();
getch();
return 0;
}


Guys my code is like this but I do not give any error..!

Goral Patel said: (Jan 10, 2014)  
Basically constructor error generated at compile time. When we create constructor then the compiler deals with that constructor. So that we can't create object with parameter of those constructor there will be error.

Sonu Rajpoot said: (Jul 16, 2014)  
Compile time error:- (Why), As we know constructor fired when object is created, at the same time compiler check for default constructor if you are not declare any constructor in your code, otherwise, on same time compiler ignore call for default constructor it make a call for user defined constructor.

Object is created at compile time, that's why error occur at compile time. (Some time object may be create at run time for to achieve run time polymorphism).

Manish said: (Aug 17, 2014)  
When object is created constructor initialize the object. When we create parameterized constructor it requires argument and when compiler finds no arguments passing through the obj it leads compilation error.

Anand Prasad said: (Sep 26, 2014)  
The answer would be different if the class has unparameterized constructor because default constructor are generate only when there are no parameterized constructors.

Shubam13 said: (Jun 24, 2015)  
Constructors initializes the object and the initialization of the object is done at compile time as we have created the parametrized constructors the class will not use its default constructors and the object we created is with zero arguments.

So during the compile time when constructor tries to initialize the object it will show and error and as this error took place during compilation the option A is correct.

M Uzair said: (Jul 6, 2015)  
When a class create object by default called implicit default constructor which may be parameterlessor with all parameter with default value, and if parameterized constructor is provided but no call made to that parameterized constructor explicit then compiler cannot create object and give compile time error.

Venkat said: (Jul 13, 2015)  
Why constructor error generates at compile time?

Srikanth.M said: (Sep 15, 2017)  
Why constructor error generates at compile time?

Sisman said: (Dec 13, 2018)  
First, what is the difference between run time and compile time? Please explain me.

Anil said: (Feb 9, 2019)  
As per my knowledge; it should be.

#include<iostream.h>
#include<conio.h>
class Anil
{
private:
int a;
public:
Anil(int A)
{
a=A;
cout<<"values is"<<A<<endl;
}
};
int main()
{
Anil A();
getch();
return 0;
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.