A parameterized constructor is a constructor that accepts one or more arguments to initialize an object when it is created. It allows different objects of the same class to be initialized with different values.
- Accepts arguments during object creation.
- Initializes data members with user-provided values.
- Eliminates the need for separate initialization statements.
- Makes object initialization more convenient and flexible.
Example: The following program demonstrates how a parameterized constructor initializes an object during creation.
#include <iostream>
using namespace std;
class A {
public:
int x;
// Parameterized constructor
A(int val) {
x = val;
}
};
int main() {
// Creating object and calling parameterized constructor
A a(10);
cout << a.x;
return 0;
}
Output
10
Explanation: The constructor accepts an integer parameter and initializes the data member x. When the object a is created, the value 10 is passed to the constructor, so x is initialized with 10.
Syntax
class ClassName {
public:
className (parameters...) {
// body
}
};
Where:
- ClassName must have the same name as the class.
- parameter_list contains one or more constructor parameters.
- Constructors do not have any return type.
Parameterized Constructor Defined Outside the Class
A parameterized constructor can be declared inside the class and defined outside using the scope resolution operator (::).
- Improves code organization and readability.
- Separates the class declaration from its implementation.
- Commonly used in large C++ programs.
#include <iostream>
#include <string.h>
using namespace std;
class Student {
int rno;
string name;
double fee;
public:
// Declaration of parameterized constructor
Student(int, string, double);
void display();
};
// Parameterized constructor outside class
Student::Student(int no, string n, double f) {
rno = no;
name = n;
fee = f;
}
void Student::display() {
cout << rno << "\t" << name << "\t" << fee << endl;
}
int main() {
// Creating object with members initialized to
// given values
Student s(1001, "Ram", 10000);
s.display();
return 0;
}
Output
1001 Ram 10000
Explanation: The constructor is declared inside the class but defined outside using Student::Student(). When the object is created, the constructor initializes all three data members.
Parameterized Constructor with Default Arguments
Just like normal functions, parameterized constructors can also have default argument values. If every parameter has a default value, the constructor can also behave as a default constructor.
- Allows objects to be created with or without arguments.
- Reduces the need for multiple overloaded constructors.
- Makes object creation more flexible.
#include <iostream>
using namespace std;
class A {
public:
int data;
A(int x = 5)
{
data = x;
}
};
int main()
{
A a1;
A a2(25);
cout << a1.data << endl;
cout << a2.data;
return 0;
}
Output
5 25
Explanation: Since the constructor parameter has a default value, a1 is initialized with 5, while a2 is initialized with the value passed to the constructor.
Member Initializer List
A member initializer list provides a cleaner and more efficient way to initialize data members directly before the constructor body executes.
- Initializes members directly instead of assigning values later.
- Required for initializing const members and references.
- Preferred for better readability and efficiency.
Syntax
ClassName(parameters)
: member1(value1),
member2(value2)
{
}
#include <iostream>
using namespace std;
// class definition
class A {
public:
// Member initialization list
A(int v1, double v2) : x(v1) , y(v2){}
int x;
double y;
};
int main() {
// Creating and initializing the object
A a(10, 11.5);
// Printing object data members' values
cout << a.x << " " << a.y;
return 0;
}
Output
10 11.5
Explanation: The member initializer list initializes x and y directly before the constructor body executes, making initialization more efficient than assigning values inside the constructor body.
Applications of Parameterized Constructor
Parameterized constructors are commonly used whenever objects require different initial values.
- Initialize objects with different values at the time of creation.
- Reduce separate assignment statements after object creation.
- Improve code readability by performing initialization in a single step.
- Support constructor overloading for multiple initialization methods.