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.

Vishal said:   1 decade ago
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;
}

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

Umakant Rahane said:   1 decade ago
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.

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)

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

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

Kavita said:   1 decade ago
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.

MINHAJ said:   1 decade ago
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.

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.


Post your comments here:

Your comments will be displayed after verification.