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 1 of 3.

Sagar said:   2 years ago
By default only the constructors are called, but not the destructors because when we create the object of the class it calls the constructor only not the zero argument destructor.

Anil said:   6 years ago
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;
}

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

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

Venkat said:   9 years ago
Why constructor error generates at compile time?

M Uzair said:   9 years 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)

Shubam13 said:   9 years 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.

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.

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.

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).


Post your comments here:

Your comments will be displayed after verification.