Introduction

In C++, a constructor is a special member function that is automatically called when an object of a class is created. It is used to initialize the object’s data members and perform any necessary setup operations. Constructors are crucial for creating objects with proper initial states and for encapsulating the object initialization process. In C++, There are mainly three types of constructor- Default constructor, parameterized constructor and copy constructor

Types of constructor

In C++, There are mainly three types of constructor-

  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor

Default constructor

In C++, a default constructor is a special type of constructor that is automatically generated by the compiler if you haven’t explicitly defined any constructors for a class. It’s responsible for initializing the object’s member variables to their default values. If you don’t provide any constructors for your class, the compiler will generate a default constructor for you.

#include <iostream>

class MyClass {
public:
    int value;

    // Default constructor (automatically generated if not provided)
    MyClass() {
        value = 0;
        std::cout << "Default constructor called" << std::endl;
    }
};

int main() {
    MyClass obj;  // Calls the default constructor
    std::cout << "Value: " << obj.value << std::endl;
    
    return 0;
}

Parameter Constructor

In C++, a parameterized constructor is a constructor of a class that accepts parameters when an object of that class is being instantiated. This allows you to initialize the object’s member variables with specific values passed as arguments to the constructor. Parameterized constructors are useful for setting initial values to an object’s state based on the values provided during object creation.

Here’s an example of how you can define and use a parameterized constructor in C++:

#include <iostream>

class MyClass {
private:
    int value;

public:
    // Parameterized constructor
    MyClass(int val) {
        value = val;
    }

    // Member function to display the value
    void display() {
        std::cout << "Value: " << value << std::endl;
    }
};

int main() {
    // Creating an object using the parameterized constructor
    MyClass obj1(42);

    // Calling the member function to display the value
    obj1.display();

    return 0;
}

In this example, the MyClass class has a parameterized constructor that takes an integer argument val and initializes the private member variable value with the provided value. When you create an object obj1 using this constructor with the argument 42, the value member is set to 42. The display member function is then used to print the value.

Remember that constructors can have multiple parameters of different types as needed. You can also provide default values to parameters to make them optional, which allows you to create objects with fewer arguments.

Copy Constructor

A copy constructor in C++ is a special member function that is used to create a new object as a copy of an existing object of the same class. It’s often used to initialize an object with the values of another object. The copy constructor has the following characteristics:

  1. It has the same name as the class.
  2. It takes a single parameter, which is a reference to an object of the same class.
  3. It should be declared as a member function of the class.

The basic syntax for a copy constructor looks like this:

class MyClass {
public:
    // Default constructor
    MyClass() { /* Initialization code here */ }
    
    // Copy constructor
    MyClass(const MyClass& other) {
        // Copy the data members from 'other' to the new object
        // You can customize this part based on your class's data members
    }
    
    // Other member functions and data members here
};

Here’s an example that demonstrates the usage of a copy constructor:

#include <iostream>

class MyClass {
public:
    int value;

    // Default constructor
    MyClass() : value(0) {}

    // Copy constructor
    MyClass(const MyClass& other) : value(other.value) {
        std::cout << "Copy constructor called" << std::endl;
    }
};

int main() {
    MyClass obj1;
    obj1.value = 42;

    MyClass obj2 = obj1; // Copy constructor is called here

    std::cout << "Value of obj2: " << obj2.value << std::endl;

    return 0;
}

In this example, when obj2 is initialized with obj1, the copy constructor of MyClass is called, which copies the value from obj1 to obj2. Keep in mind that if you don’t explicitly provide a copy constructor, the compiler will generate a default copy constructor that performs a shallow copy of the data members. If your class contains dynamically allocated resources (e.g., pointers), you might need to define your own copy constructor to ensure proper deep copying.


more related content on Object Oriented Programming

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.