Constructor in C++: A Beginner’s Guide to Understanding and Using It!
By Rohan Vats
Updated on Jul 03, 2025 | 12 min read | 38.08K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Jul 03, 2025 | 12 min read | 38.08K+ 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. It helps set up the object by assigning default values or initializing resources it needs to function correctly.
In simple terms, a constructor in C++ is like the setup guide you follow when assembling furniture; it gets everything in place so it works as expected. One key feature is that constructors can be overloaded, allowing different ways to initialize objects with various inputs.
Take your C++ expertise to the next level with industry-relevant AI and ML programs. These following courses from upGrad are tailored for developers building high-performance solutions.
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 | Creates an object with default values or no initialization. | Initializes an object with custom values passed as arguments. | Creates a new object as a copy of an existing one. |
Arguments | Takes no parameters. | Takes one or more parameters for flexible object setup. | Takes a reference to another object of the same class. |
Usage | Used when default or no specific values are needed. | Used when custom values are needed during object creation. | Used when passing/returning objects by value. |
Syntax | ClassName(); | ClassName(type arg1, type arg2, ...); | ClassName(const ClassName &other); |
Memory Allocation | Allocates memory; members may have default values. | Allocates memory and initializes members with provided values. | Allocates memory and copies member values from another object. |
When Called | Automatically called when object is created without arguments. | Called when object is created with arguments. | Called when object is passed or returned by value. |
Example | Person() { name = "Unknown"; age = 0; } | Person(string name, int age) { this->name = name; this->age = age; } | Person(const Person &p) { this->name = p.name; this->age = p.age; } |
Reccomended Read: Constructor in C++: A Comprehensive Guide with Examples
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++.
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: Top 15 Coding Courses in India in 2025: Explore Free and Advanced Computer Language Courses
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: The Surprising 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
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();
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();
Parameterized constructors, used along with inheritance, can contribute to polymorphism in OOP 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 12-hour 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++
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
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
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
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? Definition, Types and More
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?
In C++, constructors are used to initialize objects, and two common types are default and parameterized constructors. While the default constructor assigns preset values without input, the parameterized constructor lets you pass specific values during object creation. Here's a clear comparison to understand their key differences.
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. |
If you want to gain expertise in web development and constructors, check out upGrad’s AI-Powered Full Stack Development Course by IIITB. The program will help you learn programming foundations, data structures and algorithms.
Also Read: Difference Between Overloading and Overriding in Java: Understanding the Key Concepts in 2025
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.
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
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