Parametrized constructor: understanding the concept

Share the Article

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 error since the default constructor is not available

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; }
error because the base class is not having a default constructor

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 };

Main Funda: If user provides a parametrized constructor then its good practice to also provide default constructor

Related Topics:

Parametrized constructor
Virtual Destructor & Pure Virtual Destructor
Smart Pointers: unique_ptr<T>
Diamond problem – Overhead of virtual base
Multiple Inheritance has multiple this pointers
Understanding multiple inheritance & virtual base classes
Understanding the copy constructor
What is move constructor ?
Understanding the order of calling constructors and destructors
What is an explicit constructor ?
Smart Pointers: shared_ptr <T> 
What happens when exception thrown from a constructor?
Why a destructor should never throw exception?
Compiler Generated Destructor is always non-virtual
Which member functions are generated by compiler in class?
Understanding array version of new[] & delete[]

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *