Understanding Parameterized Constructor in C++: Working and Examples
By Rohan Vats
Updated on Mar 10, 2025 | 12 min read | 37.6k views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Mar 10, 2025 | 12 min read | 37.6k views
Share:
Table of Contents
Think of building a custom computer. Instead of getting a one-size-fits-all model, you select the features—RAM, storage, and processor—that match your personal preferences. This is precisely what a parameterized constructor does in C++: it lets you define specific values for each object right when it’s created, tailoring it to your exact needs.
Do you know why it is important to initialize objects in a programming language? It saves your time and eliminates repetitive setup. It enhances the efficiency of your code by allowing you to set unique values for each object during its creation. In this blog, we’ll explore how to use a parameterized constructor in C++, along with its syntax. Let’s dive in!
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.
Also Read: Constructors in C++
Now that you have a basic understanding of a constructor let’s dive deeper 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:
|
Output:
|
If you understand the concept of constructor overloading in C++, let’s explore how you can write a parameterized 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:
|
Code Example:
|
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:
|
|
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:
|
Code snippet:
|
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:
|
Outside the class:
|
Code snippet:
|
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:
|
Code snippet:
|
Output:
|
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:
|
With parameterized constructor:
|
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:
|
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:
|
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:
|
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:
|
Join upGrad’s free Java Object-Oriented Programming to explore high-paying jobs in emerging technologies.
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 C++ constructor examples.
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:
|
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:
|
2. Initializing student information
To create a class that stores and displays the information of a student using a parameterized constructor.
Code Snippet:
|
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:
|
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:
|
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:
|
4. Employee salary and department
Create an Employee class that stores and displays the name, salary, and department using a parameterized constructor.
Code snippet:
|
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:
|
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.
The global job market is witnessing a surge in demand for skilled programmers. In 2024, approximately 40% of recruiters are looking for candidates proficient in programming languages such as Python, Java, and JavaScript.
upGrad’s courses focus on high-paying languages like Java and Python, positioning you for lucrative roles in the industry. These industry-oriented courses ensure that you acquire both theoretical knowledge and practical experience.
Here are some courses offered by upGrad in programming languages.
Do you need help deciding which course to choose for your career as a C++ programmer? Contact upGrad for personalized counseling and valuable insights.
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/
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