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.
Example:
C++
// 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 }; // In this the pointer returns the CHAR ARRAY // in the same sequence of string object but // with an additional null pointer '\0' 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( "GeeksQuiz" ); String str2 = str1; str1.print(); // what is printed ? str2.print(); str2.change( "w3wiki" ); str1.print(); // what is printed now ? str2.print(); return 0; } |
GeeksQuiz GeeksQuiz GeeksQuiz w3wiki
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.
C++
#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 void print() { cout << s << endl; } void change( const char *); // Function to change }; String::String( const char * str) { size = strlen (str); s = new char [size + 1]; strcpy (s, str); } // In this the pointer returns the CHAR ARRAY // in the same sequence of string object but // with an additional null pointer '\0' void String::change( const char * str) { strcpy (s, str); } int main() { String str1( "GeeksQuiz" ); String str2 = str1; str1.print(); // what is printed ? str2.print(); str2.change( "w3wiki" ); str1.print(); // what is printed now ? str2.print(); return 0; } |
Output:
GeeksQuiz GeeksQuiz w3wiki w3wiki
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:
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; } |
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; } |
1001 Manjeet 10000 1001 Manjeet