Parameterized Constructors in C++

Last Updated : 7 May 2026

Parameterized constructors in C++ are constructors that accept arguments to initialize objects with specific values during object creation. They help create objects with different initial values instead of using default initialization.

In this chapter, you will learn about parameterized constructors in C++, their syntax, declaration, definition, and how they are used to initialize objects with custom values.

What is a Parameterized Constructor in C++?

In the C++ programming language, a parameterized constructor is a constructor that can take one or more arguments. These constructors are mainly used to pass values for initializing an object during its creation. They allow the object to be initialized with specific values instead of depending on default initialization. Inside the constructor body, the parameters can be used to initialize the data members of the object.

Parameterized Constructors in C++

Declaring and Defining a Parameterized Constructor

A parameterized constructor is declared and defined inside or outside the class using parameters that are passed during object creation.

Syntax of Parameterized Constructor

The following syntax is used to declare and define a parameterized constructor in C++:

In this syntax,

  • ClassName: It represents the name of the class.
  • public: It represents the access modifiers of the class.
  • data_type: It is used to represent the type of data.
  • arg1, arg2, ...: These represent the arguments of the class.
  • Constructor name must be the same as the class name.

Example of Parameterized Constructor

Consider the following example demonstrating the use of a parameterized constructor.

Output:

Employee Name: Jhonson, Age: 21, Salary: 45000
Employee Name: Michael, Age: 19, Salary: 53000

Explanation:

In this example, we have taken an Employee class that has data members name, age, and salary. We initialize these data members directly using the constructor during object creation. After that, we initialize two employees, emp1 and emp2, with specific values, and the show() function displays their details.

Parameterized Constructor with Destructor

A parameterized constructor can also be used together with a destructor in a class. The constructor is used to initialize the object with specific values during object creation, while the destructor is automatically called when the object is destroyed. This helps in understanding the complete lifecycle of an object.

Example

Consider the following example demonstrating the use of a parameterized constructor with a destructor.

Output:

The values for t1.x is: 20
The values for t2.x is: 15
After destructing the objects which are having the value of x is as follows: 15 
After destructing the objects which are having the value of x is as follows: 20

Explanation:

In this example, the class ParamCode uses a parameterized constructor to initialize the variable x when the objects t1 and t2 are created with the values 20 and 15. After displaying the values of x, the destructor is automatically called when the program ends, and it prints the value of x for each object as the objects are destroyed.

Default Arguments with Parameterized Constructor

In the C++ programming language, the parameterized constructor can also have a default constructor. It allows us to define default values for the arguments of parameterized constructors. If we don't pass an argument during the object creation, the constructor can automatically utilize the specified default value. It helps to reduce the requirement for multiple overhead constructors, which makes the code simpler, effective, and flexible.

Syntax

It has the following syntax:

In this syntax,

  • ClassName: It represents the name of the class.
  • type1 arg1: It represents the argument with its data type.
  • default_value: It represents the default value of the argument that is assigned to the parameter.

Example

Let us take an example to illustrate the default arguments with a parameterized constructor in C++.

Output:

The resultant output is : 
7
2

Explanation:

In this example, we have taken a class Tpoint that has a constructor. After that, it takes an integer parameter p, which defaults to 2 if no value is provided. When obj1 is created with 7, its value is set to 7, while obj2 is created without an argument, so it takes the default value 2. Finally, it displays the output in the console.

Member Initializer Lists in Parameterized Constructors

In the C++ programming language, the member initializer lists offer a method to initialize class data members directly before the execution of the constructor body. We need to use the colon (:) to initialize the value during object creation, which is followed by the initialization list.

Syntax

It has the following syntax:

Example

Let us take an example to illustrate the member initializer lists in a parameterized constructor in C++.

Output:

x = 15, y = 30
x = 8, y = 20

Explanation:

In this example, we have taken the class Tpoint that has two private variables, x and y, which are directly initialized using the initializer list when objects are created. After that, we initialize two objects t1 and t2 with values (15, 30) and (8, 20). Finally, the show() function prints these values.