A copy constructor is similar to a constructor except that it takes a reference to its class type. A Copy constructor is used to copy an object into another object of its type.

Like a constructor, a copy constructor has no return type.

Although Copy constructors can be overloaded we usually don’t do so.

The Compiler calls copy constructor implicitly as soon as an object is initialized to another object of its type.

A copy constructor is called when:

  •  Initializing an object to another object.
  •  Passing an object to a function.
  •  Returning an object from a function

Copy constructor on the other hand , is the complete opposite of the Assignment Constructor.

This time, it is used to initialize an already nonexistent(or non-previously initialized) object.

This means it copies all the data from the object you are assigning it to , without actually initializing the object that is being copied onto.

Now Let’s take a look at the same code as before but modify the assignment constructor to be a copy constructor :

// Assignment Operator
include<iostream>
include<string>
using std::cout;
using std::endl;
class Foo
{
public:
Foo(int data)
{
this->data = data;
}
~Foo(){};
Foo(const Foo& rhs)
{
data = rhs.data;
}
int data;
};
int main()
{
Foo foo(2); //Foo(int data) called
Foo foo2 = foo;
cout << foo2.data << endl;
}

You can see here Foo foo2 = foo; in the main function I immediately assign the object before actually initializing it, which as said before means it’s a copy constructor.

And notice that I didn’t need to pass the parameter int for the foo2 object since I automatically pulled the previous data from the object foo.

One More program for Copy Constructor.

Simple Example Program For Copy Constructor In C++

#include<iostream>
using namespace std;
class Copy_Constructor{ 
int a;
public: Copy_Constructor(int x){ 
cout<<"Constructor called "<<endl;
a = x; } 
Copy_Constructor(Copy_Constructor &s){ 
cout<<"Copy Constructor called"<<endl;
a=s.a; }
void print(){ 
cout<<"The value of a is :"<<a<<endl;
} }; 
int main(){ 
Copy_Constructor s1(8); //Constructor 
Copy_Constructor s2(s1); //Copy Constructor 
Copy_Constructor s3 = s1; // Copy Constructor (another way) 
s1.print();
s2.print();
s3.print(); 
return 0;
}

Output:-Constructor called
Copy Constructor called
Copy Constructor called
The value of a is :8
The value of a is :8
The value of a is :8

Explanation of the program

In this program, object s2 passes the object s1 as the argument. Hence the compiler automatically invokes the copy constructor.

Also, when a new object s3 is created, the object s1 is copied into it which again means that the argument passed will be an object.

Hence one again the copy constructor is invoked.

This article is written by our awesome writer
Comments to: Copy Constructor In C++ with example

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password