Copy constructor vs Assignment Operator

The main difference between Copy Constructor and Assignment Operator is that the Copy constructor makes a new memory storage every time it is called while the assignment operator does not make new memory storage.

Which of the following two statements calls the copy constructor and which one calls the assignment operator? 

MyClass t1, t2;
MyClass t3 = t1;  // ----> (1)
t2 = t1;          // -----> (2) 

A copy constructor is called when a new object is created from an existing object, as a copy of the existing object. The assignment operator is called when an already initialized object is assigned a new value from another existing object. In the above example (1) calls the copy constructor and (2) calls the assignment operator. See this for more details.

Copy Constructor in C++

Pre-requisite: Constructor in C++ 

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.  

Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object.

Copy constructor takes a reference to an object of the same class as an argument.

Sample(Sample &t)
{
    id=t.id;
}

The process of initializing members of an object through a copy constructor is known as copy initialization.

It is also called member-wise initialization because the copy constructor initializes one object with the existing object, both belonging to the same class on a member by member copy basis.

The copy constructor can be defined explicitly by the programmer. If the programmer does not define the copy constructor, the compiler does it for us.
 

Example:

Syntax of Copy Constructor

C++




#include <iostream>
#include <string.h>
using namespace std;
class student {
    int rno;
    char name[50];
    double fee;
 
public:
    student(int, char[], double);
    student(student& t) // copy constructor
    {
        rno = t.rno;
        strcpy(name, t.name);
        fee = t.fee;
    }
    void display();
};
 
student::student(int no, char n[], double f)
{
    rno = no;
    strcpy(name, n);
    fee = f;
}
 
void student::display()
{
    cout << endl << rno << "\t" << name << "\t" << fee;
}
 
int main()
{
    student s(1001, "Manjeet", 10000);
    s.display();
 
    student manjeet(s); // copy constructor called
    manjeet.display();
 
    return 0;
}


Output

1001    Manjeet    10000
1001    Manjeet    10000

C++




#include <iostream>
#include <string.h>
using namespace std;
class student {
    int rno;
    char name[50];
    double fee;
 
public:
    student(int, char[], double);
    student(student& t) // copy constructor (member wise
                        // initialization)
    {
        rno = t.rno;
        strcpy(name, t.name);
    }
    void display();
    void disp() { cout << endl << rno << "\t" << name; }
};
student::student(int no, char n[], double f)
{
    rno = no;
    strcpy(name, n);
    fee = f;
}
 
void student::display()
{
    cout << endl << rno << "\t" << name << "\t" << fee;
}
 
int main()
{
    student s(1001, "Manjeet", 10000);
    s.display();
 
    student manjeet(s); // copy constructor called
    manjeet.disp();
 
    return 0;
}


Output

1001    Manjeet    10000
1001    Manjeet

Similar Reads

Characteristics of Copy Constructor

...

Types of Copy Constructors

...

When is the copy constructor called?

1. The copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object....

When is a user-defined copy constructor needed?

...

Copy constructor vs Assignment Operator

1. Default Copy Constructor...

Example – Class Where a Copy Constructor is Required

...

Can we make the copy constructor private?

...

Why argument to a copy constructor must be passed as a reference?

...

Why argument to a copy constructor should be const?

In C++, a Copy Constructor may be called in the following cases:...