View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

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:

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.

What Is a Constructor in C++?

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; }

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

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++.

What Is a Parameterized Constructor 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++.

  • Customization: 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.
  • Efficiency: You have to manually set the values after creating the object if you don’t use parameterized constructors.
  • Cleaner code: 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.

  • Imagine a class Car where every car has a brand and a top speed. A parameterized constructor allows you to specify the brand and top speed for each car.
  • The Car class has a parameterized constructor Car(string b, int s), which takes two arguments: a brand and a top speed.
  • When you create objects like car1 and car2, you provide specific values for their brand and topSpeed.
  • Each object gets initialized with its own unique values.

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:

  • The Car class has a parameterized constructor that accepts two arguments: string b (brand) and int s (top speed).
  • When creating car1 and car2, values are passed to the constructor to initialize each object's brand and topSpeed.
  • The displayInfo() method is called for both objects to print their respective details: brand and top speed.
  • This demonstrates how the parameterized constructor allows each object to have customized attributes based on the provided arguments during object creation.
  • The output will display the brand and top speed for each car, showing that each object is initialized differently depending on the values passed during instantiation.

Let us now explore how you can write a parametrized constructor in C++. 

Syntax of 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:

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 Person class defines a parameterized constructor directly inside the class body, which accepts string n (name) and int a (age) as arguments.
  • The constructor initializes the name and age attributes of the object with the values passed during object creation.
  • The display() method is used to print the values of name and age for each object.
  • This demonstrates how a parameterized constructor can be written directly within the class to initialize object attributes at the time of creation.
  • When an object of type Person is instantiated, it receives unique values for name and age based on the arguments passed to the constructor.

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:

  • The Person class demonstrates a declaration of a parameterized constructor inside the class, which takes string n (name) and int a (age) as parameters.
  • The definition of the constructor is provided outside the class, where the parameters are used to initialize the object's name and age attributes.
  • The display() method prints the name and age of the object.
  • This shows how the parameterized constructor can be declared inside the class and defined separately, which is useful for better code organization and readability.
  • When an object of type Person is created, the constructor initializes the name and age attributes based on the values passed during instantiation.

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++.

How Parameterized Constructor in C++ Work?

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++.

What is the Use of 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++.

1. Constructor Overloading

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 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.

Key Benefits of Parameterized Constructor in C++

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:

  • Custom Initialization: You can pass specific values to the constructor, ensuring each object starts with the exact data it needs. This is particularly useful when objects have different attributes that need to be initialized differently.
  • Code Efficiency: Instead of writing separate setter functions or initialization code after creating an object, a parameterized constructor sets the values all at once, making the code shorter and more efficient.
  • Improved Readability: Initializing the object directly in the constructor makes the code cleaner and easier to understand. It avoids extra lines of code and clarifies the intended starting state of the object.
  • Flexibility: You can define multiple parameterized constructors (constructor overloading) to handle different ways of initializing objects, depending on what information is available when the object is created.
  • Time-Saving: By setting all attributes in one go during object creation, you save time compared to initializing them one by one in different parts of your code.
  • Prevents Uninitialized Variables: A parameterized constructor ensures that all object attributes are initialized right from the start, avoiding potential issues caused by uninitialized data that could lead to errors or undefined behavior.
  • Enhanced Object Creation: It allows you to create objects in a more structured and organized manner, especially when objects require complex or multiple properties to be set at the time of their creation.

Want to turbocharge your C++ skills? upGrad’s Data Structures & Algorithms course helps you write faster, smarter code and crack challenging problems like a pro. The 50-hour program provides hands-on learning to enhance your performance and confidence!

Now that you understand how to use parameterized constructors in C++, let's explore some C++ constructor examples.

Real-World Parameterized Constructor Example C++

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:

  • The Car class is defined with a parameterized constructor that accepts brand and speed.
  • When car1 is created in main(), the constructor is invoked with the parameters "Maruti Suzuki" and 150.
  • The display() method is called to print the car's information to the console.

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:

  • The Student class is defined with a parameterized constructor.
  • The constructor initializes the data member's name and rollNo using the arguments passed when the object is created.
  • In the main() function, an object student1 is created and initialized with "Rahul Kumar" and 101.
  • The display() method is called on student1 to print the student's details.

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:

  • The Rectangle class is defined with a parameterized constructor that initializes the length and breadth attributes.
  • The rect1 object is created in main() with values 10 and for length and breadth, respectively.
  • The area() method is called on rect1 to compute and display the area of the rectangle.

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 namesalary, and department upon creation. The display() function outputs this information.

Flow:

  • The Employee class is defined with a parameterized constructor that accepts the employee's namesalary, and department.
  • The emp1 object is created in main() with values "Amit Sharma"50000.50, and "IT".
  • Emp1 calls the display() method to print the employee's name, salary, and department.

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?

 

Differences Between Default Constructor and Parameterized Constructor

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.

How Can upGrad Help You Ace Your Programming Career?

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.

Feeling unsure about where to begin with your programming career? Connect with upGrad’s expert counselors or visit your nearest upGrad offline centre to explore a learning plan tailored to your goals. Transform your programming journey today with upGrad!

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-

Frequently Asked Questions (FAQs)

1. What is a pointer in C++?

2. How to pass an object to the constructor in C++?

3. What are the parameters in C++?

4. What is a default parameterized constructor in C++?

5. What are the advantages of constructors in C++?

6. Can a constructor be private in C++?

7. What are the three types of constructors in C++?

8. What is constructor overloading in C++?

9. What is a dynamic constructor in C++?

10. What is a destructor in C++?

11. What is the difference between shallow copy and deep copy in C++?

Rohan Vats

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

+91

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

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad KnowledgeHut

upGrad KnowledgeHut

Angular Training

Hone Skills with Live Projects

Certification

13+ Hrs Instructor-Led Sessions

upGrad

upGrad

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks