C++ Constructors


In C++, a constructor is a special member function that automatically runs when an object of a class is created. Its main role is to initialize the object’s data members so that the object starts in a valid state.

Unlike regular functions, a constructor has the same name as the class and does not have any return type, not even void.

Constructors save time by setting up objects automatically, without the need to call a separate initialization function.

Syntax of a Constructor

The general form of a constructor in C++ looks like this:

class ClassName {
public:
    ClassName() {
        // Code to initialize data members
    }
};

When you create an object of that class, the constructor is called automatically:

ClassName obj;  // Constructor is called

Types of Constructors in C++

C++ supports three main types of constructors:

  1. Default Constructor

  2. Parameterized Constructor

  3. Copy Constructor

Let’s look at each type with examples.

1. Default Constructor

A default constructor does not take any arguments. It usually assigns fixed or default values to class data members.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    // Default constructor
    Student() {
        name = "Unknown";
        age = 18;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Student s1;  // Default constructor is called
    s1.display();
    return 0;
}

Output:

Name: Unknown, Age: 18

Here, the constructor automatically sets default values when the object is created.

2. Parameterized Constructor

A parameterized constructor takes one or more arguments to initialize class members with specific values. It helps create multiple objects with different data.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    // Parameterized constructor
    Student(string n, int a) {
        name = n;
        age = a;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Student s1("Priya", 20);
    Student s2("Meena", 22);

    s1.display();
    s2.display();
    return 0;
}

Output:

Name: Priya, Age: 20
Name: Meena, Age: 22

Each object is initialized with different data values through the constructor.

3. Copy Constructor

A copy constructor is used to create a new object as a copy of another existing object. It initializes one object with the values of another.

C++ automatically provides a default copy constructor, but you can define one manually if you want more control.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    Student(string n, int a) {
        name = n;
        age = a;
    }

    // Copy constructor
    Student(const Student &s) {
        name = s.name;
        age = s.age;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Student s1("Kavya", 21);
    Student s2 = s1;  // Copy constructor is called

    s1.display();
    s2.display();
    return 0;
}

Output:

Name: Kavya, Age: 21
Name: Kavya, Age: 21

Here, s2 is a new object created as a copy of s1.

Constructor Overloading

C++ allows you to define multiple constructors in the same class, as long as they have different parameter lists. This is called constructor overloading.

It lets you initialize objects in different ways depending on the available data.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    // Default constructor
    Student() {
        name = "Unknown";
        age = 18;
    }

    // Constructor with one argument
    Student(string n) {
        name = n;
        age = 18;
    }

    // Constructor with two arguments
    Student(string n, int a) {
        name = n;
        age = a;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Student s1;
    Student s2("Ananya");
    Student s3("Riya", 22);

    s1.display();
    s2.display();
    s3.display();
    return 0;
}

Output:

Name: Unknown, Age: 18
Name: Ananya, Age: 18
Name: Riya, Age: 22

Here, three different constructors handle three different initialization cases.

Constructor with Default Arguments

C++ also allows constructors with default arguments, so you can create objects with or without providing all parameters.

Example:

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    Student(string n = "Unknown", int a = 18) {
        name = n;
        age = a;
    }

    void display() {
        cout << "Name: " << name << ", Age: " << age << endl;
    }
};

int main() {
    Student s1;
    Student s2("Nisha");
    Student s3("Tanya", 23);

    s1.display();
    s2.display();
    s3.display();
    return 0;
}

Output:

Name: Unknown, Age: 18
Name: Nisha, Age: 18
Name: Tanya, Age: 23

This approach reduces the need for multiple constructors while keeping flexibility.

Constructor in Class Composition

When a class contains another class object as a data member, the inner class’s constructor runs first, before the outer class’s constructor.

Example:

#include <iostream>
using namespace std;

class Engine {
public:
    Engine() {
        cout << "Engine initialized." << endl;
    }
};

class Car {
public:
    Engine e;  // Object of another class

    Car() {
        cout << "Car ready to drive." << endl;
    }
};

int main() {
    Car c1;
    return 0;
}

Output:

Engine initialized.
Car ready to drive.

This shows the constructor order when one class contains another.

Key Points About Constructors

  • Constructors have the same name as the class.

  • They do not have a return type.

  • They are called automatically when an object is created.

  • Constructors can be overloaded.

  • You can use default arguments in constructors.

  • If you don’t define any constructor, the compiler automatically creates a default one.

Summary of the Tutorial

Constructors in C++ are special functions that help initialize objects automatically. They make object creation cleaner, safer, and more consistent.
In this tutorial, you learned about the default, parameterized, and copy constructors, along with constructor overloading and default arguments.
Understanding constructors is essential to writing reliable and object-oriented C++ programs because they form the foundation of proper initialization and memory management.


Practice Questions

  1. Write a C++ program to create a class Student that uses a default constructor to initialize name and age with fixed values and display them.

  2. Create a class Employee with a parameterized constructor that accepts employee name and salary, then display the details.

  3. Write a program that defines a class Book with a copy constructor to copy details from one object to another.

  4. Define a class Rectangle with two constructors: one default and one parameterized. Calculate and display the area in both cases.

  5. Write a C++ program that demonstrates constructor overloading using a class Car with different numbers of parameters.

  6. Create a class Person with a constructor that uses default arguments for name and age. Display values for multiple objects using different argument combinations.

  7. Write a program to show the order of constructor calls when one class (Car) contains another class (Engine) as a member.

  8. Create a class BankAccount that has a parameterized constructor for initializing account number and balance, and a method to display account details.

  9. Write a C++ program that demonstrates how a copy constructor works when passing an object by value to a function.

  10. Create a class Box with length, width, and height initialized using a constructor. Add a function to calculate and display the volume.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top