Constructors in C++: Default, Parameterized & Copy Constructors

Introduction to Constructors in C++

In C++, a constructor is a special member function in a class that is automatically called when an object of the class is created. Constructors play a vital role in initializing the object’s data members and setting up the object’s state. They ensure that the object is properly initialized before any other operations are performed on it. In this article, we will explore the concept of constructors in C++ classes, understand their syntax, and delve into three types of constructors: Default, Parameterized, and Copy Constructors, with the help of illustrative examples.

Syntax of Constructors in C++:

In C++, the constructor is declared within the class definition and has the same name as the class. It has no return type, not even void. Here’s the general syntax of a constructor:

class ClassName {

public:

    // Constructor declaration

    ClassName() {

        // Constructor body

        // Initialization code here

    }

};

Default Constructor in C++:

The default constructor is called when an object is created without any arguments. If no constructor is explicitly defined, the compiler generates a default constructor automatically. However, if you define your own constructor(s), the compiler will not generate the default constructor. It is essential for initializing data members with default values.

#include <iostream>

class Point {

public:

    // Default constructor
    Point() {
        x = 0;
        y = 0;
    }

    void display() {
        std::cout << "x = " << x << ", y = " << y << std::endl;
    }

private:
    int x, y;
};

int main() {
    Point p1; // Calls the default constructor
    p1.display(); // Output: x = 0, y = 0

    return 0;

}

Parameterized Constructor in C++:

The parameterized constructor accepts arguments and initializes data members based on the provided values. It allows you to create objects with customized initial values.

#include <iostream>

class Rectangle {

public:

    // Parameterized constructor
    Rectangle(int len, int wid) {
        length = len;
        width = wid;
    }

    void display() {
        std::cout << "Length = " << length << ", Width = " << width << std::endl;
    }


private:
    int length, width;

};

int main() {

    Rectangle r1(5, 3); // Calls the parameterized constructor with length = 5 and width = 3
    r1.display(); // Output: Length = 5, Width = 3

    return 0;

}

Copy Constructor in C++:

The copy constructor creates a new object as a copy of an existing object. It is used when an object is passed by value, returned from a function by value, or explicitly used to create a copy of an object.

#include <iostream>

class Car {

public:

    // Parameterized constructor
    Car(std::string make, std::string model) {
        carMake = make;
        carModel = model;
    }

    // Copy constructor

    Car(const Car& otherCar) {
        carMake = otherCar.carMake;
        carModel = otherCar.carModel;
    }

    void display() {
        std::cout << "Make: " << carMake << ", Model: " << carModel << std::endl;
    }



private:
    std::string carMake;
    std::string carModel;

};



int main() {

    Car myCar("Toyota", "Camry"); // Calls the parameterized constructor
    Car copiedCar = myCar; // Calls the copy constructor to create a copy of myCar
    copiedCar.display(); // Output: Make: Toyota, Model: Camry

    return 0;

}

Conclusion

Constructors in C++ classes are essential for initializing objects and ensuring they have valid initial states. They help create objects with default, customized, or copied values. Understanding the different types of constructors, namely Default, Parameterized, and Copy Constructors, allows you to design classes with proper initialization and efficient object creation.

By leveraging the power of constructors, you can write more organized and efficient C++ code, making your programs robust and easier to maintain.