Copy Constructors in C++

Copy constructors are constructors which are used to create an object as a copy of another object.

Copy Constructors in C++

In this blog, we are going to learn about Copy Constructors. I hope you have read my previous blog on Constructor. Copy constructors are constructors which are used to create an object as a copy of another object.

Making a copy constructor:

Cars(Cars &car) {
            cout << "Copy Constructor called " << car.wheels << endl;
        }

Here, we have a class named Cars and we have created a copy constructor which takes another object of the Cars class as an argument. After this, it is doing some simple print things.

Object to a copy constructor is passed by reference. The reason being, if we pass by value, a copy of the object will be created on the stack. To create this local copy the copy constructor will be called again and we will be stuck in a recursive loop.

Code snippet to show the working of copy constructor:

// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;

class Cars{
    int wheels = 4;
    public:
    // default constructor:
    Cars() {
        cout << "Default constructor called "<< wheels <<endl;
    }

    // parameterized Constructor:
    Cars(int wheel) {
        cout << "Parameterized constructor called "<< wheel <<endl;
        wheels = wheel;
    }

    // copy Constructor:
        Cars(Cars &car) {
            cout << "Copy Constructor called " << car.wheels << endl;
        }
};

int main() {

    Cars a;
    Cars b(6);
    Cars c(b);

    return 0;
}

Here we have three instances of the class "Cars". You are familiar with "a" and "b". The only new thing here is "c" which takes another object "b" as an argument. So this shows that "c" is a copy of "b". Now this will call the copy constructor where we are just printing the number of wheels which we have initiated as 4. Now, since "b" has 6 wheels and "c" is a copy of "b" it will also have 6 wheels.

Output:
Default constructor called 4
Parameterized constructor called 6
Copy Constructor called 6

Now, what if we haven't defined any copy constructor in the class? Does the compiler provide us with a default copy constructor?

// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;

class Cars{
    int wheels = 4;
    public:
    // default constructor:
    Cars() {
        cout << "Default constructor called "<< wheels <<endl;
    }

    // parameterized Constructor:
    Cars(int wheel) {
        cout << "Parameterized constructor called "<< wheel <<endl;
        wheels = wheel;
    }

    // // copy Constructor:
    //     Cars(Cars &car) {
    //         cout << "Copy Constructor called " << car.wheels << endl;
    //     }
    void display() {
        cout << "No. of wheels are " << wheels << endl;
        return ;
    }
};

int main() {
    // Write C++ code here
    Cars a;
    Cars b(6);
    Cars c(b)
    c.display();

    return 0;
}

Here I have commented the copy constructor code and added a member function display(). Now when I initialize "c" it worked fine and printed the no. of wheels same as that of "b". So if we now know that we have a default copy constructor with us. So we define a copy constructor then the compiler will run this function else the default one will be called.

Output:
Default constructor called 4
Parameterized constructor called 6
No. of wheels are 6

When will the copy constructor gets invoked?

Many a time, we can get confused about whether a copy constructor will get called or not. For example:

int main() {

    Cars b(6),c; 
    Cars d(b); // 1
    c = b;  // 2
    Cars e = b; // 3


    return 0;
}

So in the above code, when do you think the copy constructor will get called? Let us see:

Parameterized constructor called 6 // for "b"
Default constructor called 4   // for "c"
Copy Constructor called 6      // for "d"
Copy Constructor called 6     // for "e"

So the copy constructor would not be called in case *2. This is because we have already initiated "c".

So this was all about copy constructors. I hope you learned something from it. In the next blog, I will come up with "Destructors".