Understanding Parameterized Constructor in C++: Working and Examples
By Rohan Vats
Updated on May 29, 2025 | 12 min read | 37.95K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on May 29, 2025 | 12 min read | 37.95K+ views
Share:
Table of Contents
Latest Update: According to the TIOBE Index, C++ will be the second most popular programming language worldwide in 2025, with an 11.37% share, trailing only Python! |
A parameterized constructor in C++ is a special type of constructor that allows you to provide specific values when creating an object. Instead of using default values, you can pass arguments to the constructor, which will be used to initialize the object. This method ensures that each object is customized and set up according to its intended purpose, saving time and avoiding repetitive code.
In this blog, you will explore the syntax and practical use of parameterized constructors in C++. It will explain how to pass values to the constructor, how to manage multiple constructors within a class, and provide examples that show how parameterized constructors can make your code more efficient and flexible.
Ready to learn C++ and create high-performance applications? Begin your journey with online software engineering courses and develop the skills required to write efficient code, optimize performance, and design scalable software systems.
A constructor in C++ is a special method in a class that is automatically called when you create an object of that class. Its function is to set things up for the object, such as giving it default values or initializing the resources it needs to work correctly.
In layman's terms, the constructor in C++ is like the setup instructions you have to follow when assembling a piece of furniture or a gadget.
In 2025, C++ skills are crucial as industries rely on its efficiency and performance for high-performance applications. To advance your knowledge in C++ and related fields, consider these top courses:
There are three types of constructors in C++ namely, default constructor, parameterized constructor, and copy constructor. Here is a detailed look into each of these:
Feature |
Default Constructor |
Parameterized Constructor |
Copy Constructor |
Purpose | Used to create an object with default values or no initialization. | Used to initialize an object with custom values passed as arguments at the time of creation. | Used to create a new object that is a copy of an existing object. |
Arguments | Does not take any parameters or accepts no arguments. | Takes one or more arguments, allowing for flexible initialization of objects with specific values. | Takes a reference to another object of the same class, allowing duplication of data. |
Usage | Ideal for cases when you don’t need to set any specific values initially, or when default values are sufficient. | Perfect for creating objects with custom initialization based on input data. | Often used when objects are passed by value to functions or returned from functions, ensuring a copy of the original object. |
Syntax | ClassName(); (Empty constructor with no parameters) | ClassName(type arg1, type arg2, ...); (Constructor with parameters to initialize attributes) | ClassName(const ClassName &other); (Constructor that copies data from another object) |
Memory Allocation | Allocates memory for the object but doesn’t modify it. The object's members are often given default values. | Allocates memory and initializes the object’s members with values provided via arguments. | Allocates memory for the new object and copies the values from the source object into the new one. |
When It Is Called: | Called automatically when an object is created without any arguments, ensuring it has default values or no specific initialization. | Called when creating an object with arguments, allowing for custom initialization values from the start. | Called when an object is passed by value (e.g., to a function) or returned by value, making a copy of the original object. |
Example | Person() { name = "Unknown"; age = 0; } (Defaults to "Unknown" and age 0) | Person(string name, int age) { this->name = name; this->age = age; } (Sets values based on input) | Person(const Person &p) { this->name = p.name; this->age = p.age; } (Creates a copy of another Person) |
Reccomended Read: Constructors in C++
Now that you have a basic understanding of a constructor let’s look into parameterized constructors in C++.
A parameterized constructor in C++ is a type of constructor that takes arguments (parameters). Giving arguments allows you to initialize an object with specific values when you create it instead of always providing the object's default values.
Here are the reasons you must use a parameterized constructor in C++.
The parameterized constructor allows an object to start with its own unique set of values. For example, you can create one Car object with a top speed of 180 and another with a top speed of 240.
You have to manually set the values after creating the object if you don’t use parameterized constructors.
You can reduce repetitive code by using the parameterized constructor. Instead of writing separate statements to set each data member, you can initialize them all in the constructor.
For illustration, consider the following example.
Code snippet:
#include <iostream>
using namespace std;
class Car {
string brand;
int topSpeed;
public:
// Parameterized constructor
Car(string b, int s) {
brand = b;
topSpeed = s;
}
void displayInfo() {
cout << "Brand: " << brand << ", Top Speed: " << topSpeed << " km/h" << endl;
}
};
int main() {
Car car1("Toyota", 180); // Pass values to the constructor
Car car2("BMW", 240); // Different values for another object
car1.displayInfo();
car2.displayInfo();
return 0;
}
Output:
Brand: Toyota, Top Speed: 180 km/h
Brand: BMW, Top Speed: 240 km/h
Explanation:
Let us now explore how you can write a parametrized constructor in C++.
You can use a parameterized constructor in C++ in two ways: inside the class and outside the class. In inside the class method, you declare and define the constructor inside the class. The implementation is directly within the class body. This approach keeps everything compact and is useful for small classes.
Here‘s the syntax of inside the class approach.
Syntax:
class ClassName {
public:
// Declaration and definition inside the class
ClassName(DataType parameter1, DataType parameter2) {
// Initialization logic here
}
};
Code Example:
In the following example, the parameterized constructor is written directly in the Person class body.
class Person {
string name;
int age;
public:
// Parameterized constructor defined inside the class
Person(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
Explanation:
The outside-the-class approach declares the constructor in the class and defines it outside. This approach is suitable for larger classes or when you want to organize your code for better readability.
Syntax:
class ClassName {
public:
// Declaration of parameterized constructor
ClassName(DataType parameter1, DataType parameter2);
};
// Definition of parameterized constructor outside the class
ClassName::ClassName(DataType parameter1, DataType parameter2) {
// Initialization logic here
}
Code syntax:
class Person {
string name;
int age;
public:
// Declaration of parameterized constructor
Person(string n, int a);
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Definition of parameterized constructor outside the class
Person::Person(string n, int a) {
name = n;
age = a;
}
Explanation:
Also Read: 10 Best Computer Programming Courses to Get a Job in 2025
After a basic understanding of syntax, let’s check out the working of a parameterized constructor in C++.
Parameterized constructors allow you to pass arguments to set the initial state of an object. This flexibility will enable you to create objects with different properties without the need for repetitive manual initialization. By combining object creation and initialization in a single step, parameterized constructors improve code efficiency, readability, and maintainability.
Here’s a step-by-step explanation of how parameterized constructors work in C++.
1. Declaration
You can declare a parameterized constructor in the class by specifying its name (same as the class name) followed by a parameter list. These parameters define the values that the constructor will accept during object creation.
Syntax:
class ClassName {
public:
ClassName(DataType parameter1, DataType parameter2); // Constructor declaration
};
Code snippet:
class Person {
public:
Person(string name, int age); // Declares a parameterized constructor
};
2. Definition
The parameterized constructor can be defined either inside or outside the class. If defined outside, the ClassName::ConstructorName syntax associates the definition with the class.
Syntax:
Inside the class:
class ClassName {
public:
ClassName(DataType parameter1, DataType parameter2) {
// Initialization logic here
}
};
Outside the class:
ClassName::ClassName(DataType parameter1, DataType parameter2) {
// Initialization logic here
}
Code snippet:
class Person {
string name;
int age;
public:
Person(string n, int a); // Constructor declaration
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Constructor definition outside the class
Person::Person(string n, int a) {
name = n;
age = a;
}
3. Object Creation
When you create an object of the class, you pass the required arguments to the constructor. The constructor automatically assigns these values to the object’s data members.
Syntax:
ClassName objectName(parameter1_value, parameter2_value);
Code snippet:
int main() {
Person person1("Rahul", 25); // Creates a Person object and initializes it
person1.display();
Person person2("Priya", 30); // Another object with different values
person2.display();
return 0;
}
Output:
Name: Rahul, Age: 25
Name: Priya, Age: 30
4. Initialization
A parameterized constructor automates the initialization process by assigning the passed values to the object's data members. In this way, every object is correctly initialized as soon as it is created without needing additional function calls.
Code snippet:
Without parameterized constructor:
person1.setName("Raj");
person1.setAge(25);
With parameterized constructor:
Person person1("Raj", 25);
Also Read: What are the Advantages of Object-Oriented Programming?
In the following section, you can explore different ways of using a parameterized constructor in C++.
Parameterized constructors make your programs more robust, efficient, and easier to maintain, especially as the complexity rises. Here’s how you can make your code more efficient and flexible using a parameterized constructor in C++.
Parameterized constructors work alongside default constructors and other overloaded constructors, providing different ways to initialize objects based on the context. This flexibility enables you to define constructors with varying parameters to suit different use cases.
Code snippet:
class Car {
public:
Car() { cout << "Default Car\n"; } // Default constructor
Car(string brand, int speed) { // Parameterized constructor
cout << brand << " with speed " << speed << " created.\n";
}
};
Car car1; // Calls the default constructor
Car car2("Toyota", 180); // Calls the parameterized constructor
2. Assigning Different Values to Objects
Parameterized constructors allow each object to start with unique attributes, making it possible to create multiple objects with distinct values concisely.
Code snippet:
class Car {
string brand;
int speed;
public:
Car(string b, int s) { // Parameterized constructor
brand = b;
speed = s;
}
void display() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h\n";
}
};
Car car1("Toyota", 180);
Car car2("BMW", 240);
car1.display();
car2.display();
3. Ensuring mandatory initialization of attributes
Using a parameterized constructor in C++, you can enforce the initialization of essential attributes by requiring specific arguments during object creation. This avoids uninitialized objects, reducing potential runtime errors.
Code snippet:
class Person {
string name;
int age;
public:
Person(string n, int a) { // Mandatory parameters
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
Person person("Alice", 25); // Ensures name and age are always set
person.display();
4. Supporting polymorphic behavior
Parameterized constructors, used along with inheritance, can contribute to polymorphism by allowing base and derived classes to initialize objects with specific attributes dynamically.
Code snippet:
class Vehicle {
protected:
int speed;
public:
Vehicle(int s) : speed(s) {}
virtual void display() { cout << "Speed: " << speed << " km/h\n"; }
};
class Car : public Vehicle {
string brand;
public:
Car(string b, int s) : Vehicle(s), brand(b) {}
void display() override {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h\n";
}
};
Vehicle* v = new Car("Tesla", 200);
v->display(); // Polymorphic behavior
Join upGrad’s free Java Object-Oriented Programming to explore high-paying jobs in emerging technologies.
Parameterized Constructor in C++ has multiple key benefits, some which are covered below.
A parameterized constructor in C++ allows you to initialize an object with specific values as soon as it’s created. This feature helps improve code clarity, efficiency, and flexibility by setting attributes directly during object instantiation.
Here are the key benefits:
Now that you understand how to use parameterized constructors in C++, let's explore some C++ constructor examples.
Understanding parameterized constructors in C++ is easier when you see practical examples of how they are used.
Here are some parameterized constructor example C++
1. Car information with speed
Objective:
To create a Car class that stores and displays the car brand and speed using a parameterized constructor.
Code snippet:
#include <iostream>
using namespace std;
class Car {
string brand;
int speed;
public:
// Parameterized constructor
Car(string b, int s) {
brand = b;
speed = s;
}
void display() {
cout << "Car Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
}
};
int main() {
Car car1("Maruti Suzuki", 150); // Initializing with parameters
car1.display();
return 0;
}
Explanation:
The Car class has a constructor that takes the car’s brand and speed as arguments. When the object car1 is created, the constructor sets its values to "Maruti Suzuki" and 150.
Flow:
Output:
Car Brand: Maruti Suzuki, Speed: 150 km/h
2. Initializing student information
To create a class that stores and displays the information of a student using a parameterized constructor.
Code Snippet:
#include <iostream>
using namespace std;
class Student {
string name;
int rollNo;
public:
// Parameterized constructor
Student(string n, int r) {
name = n;
rollNo = r;
}
void display() {
cout << "Student Name: " << name << ", Roll No: " << rollNo << endl;
}
};
int main() {
Student student1("Rahul Kumar", 101); // Initializing with parameters
student1.display();
return 0;
}
Explanation:
The Student class has a parameterized constructor that accepts two parameters—name and rollNo. The constructor initializes these attributes when the object is created.
Flow:
Output:
Student Name: Rahul Kumar, Roll No: 101
3. Calculating the area of a rectangle
Objective:
Calculate and display the area of a rectangle using a parameterized constructor that accepts the length and breadth.
Code snippet:
#include <iostream>
using namespace std;
class Rectangle {
int length, breadth;
public:
// Parameterized constructor
Rectangle(int l, int b) {
length = l;
breadth = b;
}
void area() {
cout << "Area of Rectangle: " << length * breadth << " square units" << endl;
}
};
int main() {
Rectangle rect1(10, 5); // Initializing with length=10 and breadth=5
rect1.area();
return 0;
}
Explanation:
The Rectangle class has a constructor that takes the length and breadth as parameters. The area() function calculates the triangle’s area based on these values.
Flow:
Output:
Area of Rectangle: 50 square units
4. Employee salary and department
Create an Employee class that stores and displays the name, salary, and department using a parameterized constructor.
Code snippet:
#include <iostream>
using namespace std;
class Employee {
string name;
float salary;
string department;
public:
// Parameterized constructor
Employee(string n, float s, string d) {
name = n;
salary = s;
department = d;
}
void display() {
cout << "Employee Name: " << name << ", Salary: " << salary << ", Department: " << department << endl;
}
};
int main() {
Employee emp1("Amit Sharma", 50000.50, "IT"); // Initializing with parameters
emp1.display();
return 0;
}
Explanation:
The Employee class uses a parameterized constructor to initialize the employee’s name, salary, and department upon creation. The display() function outputs this information.
Flow:
Output:
Employee Name: Amit Sharma, Salary: 50000.5, Department: IT
Also Read: What is Programming Language? Syntax, Top Languages, Example
After going through examples, let’s check how the default constructor differs from parameterized construction in C++.
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
There are two primary types of constructors in C++: default constructors and parameterized constructors. Both are essential for object initialization, but they differ in how they assign values to the object's attributes and their definition.
A default constructor does not take any parameters and provides default values for the object’s attributes. The compiler automatically generates a default constructor if no constructor is explicitly defined. On the other hand, a parameterized constructor takes one or more parameters and allows the initialization of an object with specific values at the time of its creation.
Do you know how a default constructor and a parameterized constructor differ from each other? Find out in the following table.
Parameters | Default Constructor | Parameterized Constructor |
Initialization | Initializes objects with default values (ex: 0 for integers and nullptr for pointers) | Initializes objects with specific values passed as arguments. |
Invocation | Automatically invoked when an object is created with no arguments. | Explicitly invoked with arguments during object creation. |
Use Case | Used when no initial values are needed. | Used when you need to set specific values for object attributes. |
Constructor overloading | Cannot be overloaded (since it doesn't take any parameters). | Can be overloaded, allowing multiple constructors with different sets of parameters. |
Flexibility | Less flexible, as it can only assign default values. | Highly flexible, as it allows different values at object creation. |
Example usage | cpp<br>Car car1; | cpp<br>Car car1("Toyota", 180); |
Default Provided by Language | Automatically provided by the compiler. | Not provided by the compiler. |
Typical Implementation | Constructor with no parameters. | Constructor with one or more parameters. |
Also Read: Difference Between Overloading and Overriding
Now that you understand a parameterized constructor in C++, let's explore how to prepare for a career in programming with the help of upGrad.
Parameterized constructors in C++ provide flexibility by allowing you to initialize objects with specific values at the time of creation, making your code more efficient and customizable. This feature enhances object-oriented programming by enabling objects to be set up with relevant data right from the start.
Whether you’re just starting or looking to refine your skills, upGrad offers a variety of in-depth programs that focus on C++ and programming languages. These courses provide hands-on experience, practical projects, and expert mentorship to help you excel.
Here are some courses offered by upGrad in programming languages.
Discover our free software development courses designed to enhance your skills and accelerate your career growth in the tech industry.
Dive into our popular software engineering courses and gain the expertise needed to excel in the ever-evolving tech landscape.
References:
https://www.statista.com/statistics/1296727/programming-languages-demanded-by-recruiters/
https://www.index.dev/blog/most-popular-programming-languages-
408 articles published
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources