Copy 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.
Example:
#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;
}
A user-defined copy constructor is generally needed when an object owns
pointers or non-shareable references, such as to a file, in which case a
destructor and an assignment operator should also be written.
When is the copy constructor called?
In C++, a Copy Constructor may be called in the following cases:
• When an object of the class is returned by value.
• When an object of the class is passed (to a function) by value as an argument.
• When an object is constructed based on another object of the same class.
• When the compiler generates a temporary object.
When is a user-defined copy constructor needed?
If we don’t define our own copy constructor, the C++ compiler creates a default
copy constructor for each class which does a member-wise copy between
objects. The compiler-created copy constructor works fine in general. We need
to define our own copy constructor only if an object has pointers or any runtime
allocation of the resource like a file handle, a network connection, etc.
Example – Class Where a Copy Constructor is Required
Following is a complete C++ program to demonstrate the use of the Copy
constructor. In the following String class, we must write a copy constructor.
// C++ program to demonstrate the
// Working of Copy constructor
#include <cstring>
#include <iostream>
using namespace std;
class String {
private:
char* s;
int size;
public:
String(const char* str = NULL); // constructor
~String() { delete[] s; } // destructor
String(const String&); // copy constructor
void print()
cout << s << endl;
} // Function to print string
void change(const char*); // Function to change
};
String::String(const char* str)
size = strlen(str);
s = new char[size + 1];
strcpy(s, str);
void String::change(const char* str)
delete[] s;
size = strlen(str);
s = new char[size + 1];
strcpy(s, str);
String::String(const String& old_str)
size = old_str.size;
s = new char[size + 1];
strcpy(s, old_str.s);
}
int main()
String str1("Quiz");
String str2 = str1;
str1.print(); // what is printed?
str2.print();
str2.change("Test");
str1.print(); // what is printed now?
str2.print();
return 0;
What would be the problem if we remove the copy constructor from the
above code?
If we remove the copy constructor from the above program, we don’t get the
expected output. The changes made to str2 reflect in str1 as well which is never
expected.