Copy Constructor in C++

A copy constructor is a member function that initializes an object using another object of the same class.

Syntax of Copy Constructor

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

ClassName (ClassName &obj)
{
// body_containing_logic
}

Just like the default constructor, the C++ compiler also provides an implicit copy constructor if the explicit copy constructor definition is not present. Here, it is to be noted that, unlike the default constructor where the presence of any type of explicit constructor results in the deletion of the implicit default constructor, the implicit copy constructor will always be created by the compiler if there is no explicit copy constructor or explicit move constructor is present.

Examples of Copy Constructor

The below examples demonstrate how to use the copy constructors in C++.

Example 1: Illustration of Implicit Copy Constructor

C++




// C++ program to illustrate the use of Implicit copy
// constructor
#include <iostream>
using namespace std;
 
class Sample {
    int id;
 
public:
    // parameterized constructor
    Sample(int x) { id = x; }
    void display() { cout << "ID=" << id; }
};
 
int main()
{
    Sample obj1(10);
    obj1.display();
    cout << endl;
 
    // creating an object of type Sample from the obj
    Sample obj2(obj1); // or obj2=obj1;
    obj2.display();
    return 0;
}


Output

ID=10
ID=10

Example 2: Defining of Explicit Copy Constructor

C++




// C++ Program to demonstrate how to define the explicit
// copy constructor
#include <iostream>
using namespace std;
 
class Sample {
    int id;
 
public:
    // default constructor with empty body
    Sample() {}
 
    // parameterized constructor
    Sample(int x) { id = x; }
 
    // copy constructor
    Sample(Sample& t) { id = t.id; }
 
    void display() { cout << "ID=" << id; }
};
 
// driver code
int main()
{
    Sample obj1(10);
    obj1.display();
    cout << endl;
 
    // copy constructor called
    Sample obj2(obj1); // or obj2=obj1;
    obj2.display();
 
    return 0;
}


Output

ID=10
ID=10

Example 3: Defining of Explicit Copy Constructor with Parameterized Constructor

C++




// C++ program to demonstrate copy construction along with
// parameterized constructor
#include <iostream>
#include <string.h>
using namespace std;
 
// class definition
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

Uses of Copy Constructor

  • Constructs a new object by copying values from an existing object.
  • Can be used to perform deep copy.
  • Modify specific attributes during the copy process if needed.

Constructors in C++

Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. It constructs the values i.e. provides data for the object which is why it is known as a constructor.

  • Constructor is a member function of a class, whose name is the same as the class name.
  • Constructor is a special type of member function that is used to initialize the data members for an object of a class automatically when an object of the same class is created.
  • Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as a constructor.
  • Constructors do not return value, hence they do not have a return type.
  • A constructor gets called automatically when we create the object of the class.
  • Constructors can be overloaded.
  • A constructor can not be declared virtual.

Similar Reads

Syntax of Constructors in C++

The prototype of the constructor looks like this:...

Examples of Constructors in C++

The below examples demonstrate how to declare constructors for a class in C++:...

Characteristics of Constructors in C++

...

Types of Constructor in C++

...

1. Default Constructor in C++

The following are some main characteristics of the constructors in C++:...

2. Parameterized Constructor in C++

Constructors can be classified based on in which situations they are being used. There are 4 types of constructors in C++:...

3. Copy Constructor in C++

A default constructor is a constructor that doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor....

4. Move Constructor in C++

...

Destructors in C++

...

Frequently Asked Questions on C++ Constructors

Parameterized Constructors make it possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object....