Characteristics of Copy Constructor

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

2. Copy constructor takes a reference to an object of the same class as an argument. If you pass the object by value in the copy constructor, it would result in a recursive call to the copy constructor itself. This happens because passing by value involves making a copy, and making a copy involves calling the copy constructor, leading to an infinite loop. Using a reference avoids this recursion. So we use reference of Objects to avoid infinite calls.

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

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

4. 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.

5. 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:

C++




// C++ program to demonstrate the working
// of a COPY CONSTRUCTOR
#include <iostream>
using namespace std;
 
class Point {
private:
    int x, y;
 
public:
    Point(int x1, int y1)
    {
        x = x1;
        y = y1;
    }
 
    // Copy constructor
    Point(const Point& p1)
    {
        x = p1.x;
        y = p1.y;
    }
 
    int getX() { return x; }
    int getY() { return y; }
};
 
int main()
{
    Point p1(10, 15); // Normal constructor is called here
    Point p2 = p1; // Copy constructor is called here
 
    // Let us access values assigned by constructors
    cout << "p1.x = " << p1.getX()
         << ", p1.y = " << p1.getY();
    cout << "\np2.x = " << p2.getX()
         << ", p2.y = " << p2.getY();
    return 0;
}


Output

p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15

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:...