Introduction
Parametrized constructor is a user-defined constructor which takes some parameter as argument and does construction of object.
In following example, the class MainFunda has a parameterized constructor which accepts an integer argument.
#include <iostream> //main header
using namespace std; //namespace
class MainFunda
{
public:
MainFunda(int ix)
{
cout << "Parametrized Constructor Called";
}
};
int main()
{
MainFunda a1(4); //Calls parametrized constructor
return 0;
}
Implication of providing a parametrized constructor
The implication is that if a class declares only a parameterized constructor, then compiler shall not generate a default constructor. In the following example, the MainFunda class has neither the user’s default constructor nor compiler-generated default constructor. Hence, any object creation without arguments will fail.
#include <iostream> //main header
using namespace std; //namespace
class MainFunda
{
public:
MainFunda(int ix)
{
cout << "Parametrized Constructor Called";
}
};
int main()
{
MainFunda a1; //No default constructor, Error
return 0;
}
The output will be:
Compiler still generates other special functions
C++ compiler will still generate other special functions –
- copy constructor,
- assignment operator
- destructor.
The following example demonstrates that for the given class in the compiler is able to copy and assign an object (with auto-generated functions):
#include <iostream> //main header
using namespace std; //namespace
class MainFunda
{
public:
MainFunda(int ix)
{
cout << "Parametrized Constructor"
<< endl;
}
};
int main()
{
MainFunda a1(0); //Calls Parametrized Constructor,
//this is OK
MainFunda a2 = a1; //Calls Copy Constructor,
//this is OK
MainFunda a3(1); //Calls Parametrized Constructor,
//this is OK
a3 = a1; //Calls assignment operator,
//this is ok
return 0;
}
The compiler still allows “copy construction” , “destruction” as well as “assignment”. This means all these special member functions are generated by compiler.
The output is:
Parametrized Constructor
Parametrized Constructor
Implication on derived classes
When the base has only parametrized constructor, the derived classes must explicitly call such constructors. In absence of such call, the compiler shall attempt to call default constructor and this shall fail.
The compilation of following program fails because MFDerived calls default constructor of base class MainFunda.
#include <iostream> //main header
using namespace std; //namespace
class MainFunda
{
public:
MainFunda(int ix)
{
cout << "Parametrized Constructor Called";
}
};
class MFDerived : public MainFunda
{
};
int main()
{
MFDerived a1; //No default constructor, Error
return 0;
}
Resolution
One way to resolve the above error is to explicitly providing some constructor in MFDerived which calls parametrized constructor in Base.
Following code snippet shows 2 constructors. Both of them call the parametrized constructor in base. Any one of the option, depending on use-cases is sufficient to resolve the error.
class MFDerived : public MainFunda
{
public:
MFDerived() : MainFunda(0) {} //option 1
MFDerived(int ix) : MainFunda(ix) {}//option 2
};