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?
Discussion:
22 comments Page 2 of 3.
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).
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).
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.
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..!
#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..!
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.
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.
Salman khan said:
1 decade ago
Why error is coming in c++?
DWARIKA said:
1 decade ago
Compile time error, because constructor error generally generated at compile time.
Pragya said:
1 decade ago
Compile time error. Because constructor error always generated at compile time. Compiler use to create default constructor if default constructor is not provided.
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.
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;
}
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;
}
Navin said:
1 decade ago
I agree with Naveen. Compiler use to create default constructor if default constructor is not provided.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers