C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : 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?
Compile-time error.
Preprocessing error.
Runtime error.
Runtime exception.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 2 of 3.

Bharat said:   1 decade ago
#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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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.
(3)

Venkat said:   1 decade ago
Why constructor error generates at compile time?

Srikanth.m said:   8 years ago
Why constructor error generates at compile time?

Sisman said:   7 years ago
First, what is the difference between run time and compile time? Please explain me.


Post your comments here:

Your comments will be displayed after verification.