{"id":26168,"date":"2021-08-18T14:37:00","date_gmt":"2021-08-18T09:07:00","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/"},"modified":"2024-09-03T11:08:45","modified_gmt":"2024-09-03T05:38:45","slug":"copy-constructor-in-cpp","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/","title":{"rendered":"Copy Constructor in C++ - Everything you Need to know About"},"content":{"rendered":"\n<p id=\"index\">Among different types of constructors, the copy constructor is the type that uses another object within the same class to initialize the data members of the class within the class. In other words, it creates a copy of an existing object of the class.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a data-type=\"internal\" data-id=\"#d\" href=\"#d\">Constructor<\/a><\/li><li><a href=\"#copy-constructor\">What is a Copy Constructor in C++?<\/a><\/li><li><a data-type=\"internal\" data-id=\"#c\" href=\"#c\">Classification of Constructors<\/a><\/li><li><a data-type=\"internal\" data-id=\"#Uses\" href=\"#Uses\">Uses of Copy Constructor<\/a><\/li><li><a data-type=\"internal\" data-id=\"#Example\" href=\"#Example\">Example of Copy Constructor in C++&nbsp;<\/a><\/li><li><a data-type=\"internal\" data-id=\"#cc\" href=\"#cc\">Constructor Copies<\/a><ul><li><a data-type=\"internal\" data-id=\"#ShallowCopy\" href=\"#ShallowCopy\">Shallow Copy<\/a><\/li><li><a data-type=\"internal\" data-id=\"#DeepCopy\" href=\"#DeepCopy\">Deep Copy<\/a><\/li><\/ul><\/li><li><a data-type=\"internal\" data-id=\"#Difference\" href=\"#Difference\">Difference between Copy Constructor and Assignment Operator<\/a><\/li><\/ol>\n\n\n\n<p>Let us begin by first understanding what a constructor is before we dive in what copy constructor in C++ is. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"constructor\"><strong>Constructor<\/strong><\/h2>\n\n\n\n<p>A constructor is a special member function used to initialize objects of its class.&nbsp;It will be called automatically when the object is created.&nbsp;<\/p>\n\n\n\n<p>Let us see <strong>how a constructor is automatically <\/strong>executed at the time of object creation.<\/p>\n\n\n\n<p><strong>Constructor Program in C++:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream&gt; \nusing namespace std; \nclass ABC { \n public:ABC(){\n    cout&lt;&lt;\"Constructor is called automatically\";\n    cout&lt;&lt;\"at the time of execution\"&lt;&lt;endl;\n }\n void display(){\n        cout&lt;&lt;\"hello world\";\n } \n}; \n\nint main() { \n ABC ob1,ob2;\n ob1.display();\n ob2.display();\n return 0;  \n} \n<\/code><\/pre>\n\n\n\n<p><strong>Output:&nbsp;<\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32.png\"><img decoding=\"async\" width=\"1024\" height=\"221\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-1024x221.png\" alt=\"\" class=\"wp-image-36715\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-1024x221.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-300x65.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-768x166.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-696x150.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-1068x231.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32-150x32.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-32.png 1235w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the above program, we created two objects ob1 and ob2, for the same class ABC. As you can see, we have called the display() function to execute, but we didn\u2019t call for constructor ABC().&nbsp;<\/p>\n\n\n\n<p>A constructor is called automatically when we created the object ob1 and ob2, but for display(), we need to call unless we call it won\u2019t execute explicitly. This is the advantage of using the constructor. It is easily accessible, and the user can easily understand the flow of the program.<\/p>\n\n\n\n<p>Let us see the program to <strong>find the sum of N natural numbers.&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>Program:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream&gt; \nusing namespace std; \nclass sum { \n private: int n,s; \n public: sum(){\n    s=0;\n    cout&lt;&lt;\"enter limit= \";  \n    cin&gt;&gt;n;\n    for(int i=1; i&lt;=n; i++){\n        s+=i;\n        cout&lt;&lt;\"Sum of natural numbers= \"&lt;&lt;s;\n    }\n } \n}; \n\nint main() { \n sum ob1;\n return 0;  \n} <\/code><\/pre>\n\n\n\n<p><strong>Output:&nbsp;<\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31.png\"><img decoding=\"async\" width=\"1024\" height=\"244\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-1024x244.png\" alt=\"\" class=\"wp-image-36714\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-1024x244.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-300x71.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-768x183.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-696x166.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-1068x255.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31-150x36.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-31.png 1071w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>In the above example, we created an object <strong>ob1 <\/strong>which calls the sum() function automatically.&nbsp;<\/li><li>We find the sum of the first five natural numbers, i.e., 1 to 5.<\/li><li>Sum() is the constructor which is automatically called at the time of object creation. It gives us the <strong>result as 15 <\/strong>[1+2+3+4+5=15].&nbsp;<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-copy-constructor-in-c\"><strong>What is a Copy Constructor in C++?<\/strong><\/h2>\n\n\n\n<p>Copy constructors are the member functions of a class that initialize the data members of the class using another object of the same class. It copies the values of the data variables of one object of a class to the data members of another object of the same class. A copy constructor can be defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class class_name {\n    Class_name(Class_name &amp;old_object){                   \/\/copy constructor\n         Var_name = old_object.Var_name;\n                \u2026.\n                \u2026.\n                          }\n         }\n<\/code><\/pre>\n\n\n\n<p>A copy constructor in C++ is further categorized into two types:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Default Copy Constructor<\/li><li>User-defined Copy Constructor<\/li><\/ul>\n\n\n\n<p><strong><em>Default Copy Constructors:<\/em><\/strong> When a copy constructor is not defined, the C++ compiler automatically supplies with its self-generated constructor that copies the values of the object to the new object.<\/p>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;iostream&gt;\nusing namespace std;\n\nclass A {\n    int x, y;\n  \npublic:\n    A(int i, int j)\n    {\n        x = i;\n        y = j;\n    }\n    int getX() { return x; }\n    int getY() { return y; }\n};\n  \nint main()\n{\n    A ob1(10, 46);\n    A ob2 = ob1;                                    \/\/ 1\n    cout &lt;&lt; \"x = \" &lt;&lt; ob2.getX() &lt;&lt; \" y = \" &lt;&lt; ob2.getY();\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p><em>Output:<\/em><\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-4.png\"><img decoding=\"async\" width=\"279\" height=\"131\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-4.png\" alt=\"copy constructor\" class=\"wp-image-73391\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-4.png 279w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-4-150x70.png 150w\" sizes=\"(max-width: 279px) 100vw, 279px\" \/><\/figure>\n\n\n\n<p><em>Here, in line 1, even without the copy constructor, the values of ob1\u2019s variable members copy fine to the member variables of ob2.<\/em><\/p>\n\n\n\n<p><strong><em>User-defined Copy Constructors:<\/em><\/strong><em> <\/em>In case of a user-defined copy constructor, the values of the parameterised object of a class are copied to the member variables of the newly created class object. The initialization or copying of the values to the member variables is done as per the definition of the copy constructor.<\/p>\n\n\n\n<p><strong><em>Example: <\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;  \nclass Example  \n{  \n   public:  \n    int a;  \n    Example(int x)                \/\/ parameterized constructor  \n    {  \n      a=x;  \n    }  \n    Example(Example &amp;ob)               \/\/ copy constructor  \n    {  \n        a = ob.a;  \n    }  \n};  \nint main()  \n{  \n  Example e1(36);               \/\/ Calling the parameterized constructor  \n Example e2(e1);                \/\/  Calling the copy constructor  \n cout&lt;&lt;e2.a;  \n  return 0;  \n}  \n<\/code><\/pre>\n\n\n\n<p><em><strong>Output<\/strong><\/em><\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-5.png\"><img decoding=\"async\" width=\"159\" height=\"114\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-5.png\" alt=\"c++\" class=\"wp-image-73393\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-5.png 159w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-5-150x108.png 150w\" sizes=\"(max-width: 159px) 100vw, 159px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"classification-of-constructors\"><strong>Classification of Constructors<\/strong><\/h2>\n\n\n\n<p>Constructors are classified as follows:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Parameterized Constructor&nbsp;<\/li><li>Constructor overloading<\/li><li>Copy Constructors<\/li><li>Default Constructor&nbsp;<\/li><li>Constructor with default arguments.&nbsp;<\/li><\/ul>\n\n\n\n<p>Let me give a brief introduction to the above topics.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"parameterised-constructor\"><strong>Parameterised constructor<\/strong>: <\/h4>\n\n\n\n<p>The constructor that takes arguments or parameters at the time of object creation is known as parameterized constructor.&nbsp; The syntax for calling a parameterised constructor is given below:<\/p>\n\n\n\n<p>Syntax:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ClassName objectName (arg1, arg2 arg3,\u2026, argN);\n<\/code><\/pre>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;\n\nclass Transport  \n{  \n   public:  \n    int wheels;  \n    Transport(int n)                \/\/ parameterized constructor  \n    {  \n      wheels=n;  \n    }  \n};  \n\nint main()  \n{  \n  Transport car(4);               \/\/ Calling the parameterized constructor for the 1st object  \n  Transport bike(2);             \/\/ Calling the parameterized constructor for the 2nd object  \n  cout&lt;&lt;\"Car has \"&lt;&lt;car.wheels&lt;&lt;\" wheels\\n\";  \n  cout&lt;&lt;\"Bike has \"&lt;&lt;bike.wheels&lt;&lt;\" wheels\\n\";\n  return 0;  \n             }  \n\n<\/code><\/pre>\n\n\n\n<p><em>Output:<\/em><\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-6.png\"><img decoding=\"async\" width=\"273\" height=\"132\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-6.png\" alt=\"\" class=\"wp-image-73394\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-6.png 273w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-6-150x73.png 150w\" sizes=\"(max-width: 273px) 100vw, 273px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"constructor-overloading\"><strong>Constructor overloading: <\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>When a class contains more than one constructor, it is known as constructor overloading. The constructors are identical in names, so they are distinguished from each other on the basis of the number of arguments and type of arguments in their parameters.&nbsp;<\/li><\/ul>\n\n\n\n<p>Constructor overloading in C++ represents the polymorphism feature of OOP. The syntax for calling different constructors for different objects of the same class is shown below.<\/p>\n\n\n\n<p><strong><em>Syntax:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ClassName object1 (arg1, arg2, arg3);\nClassName object2 (arg);\nClassName object3();\n<\/code><\/pre>\n\n\n\n<p>The syntax for the definition of multiple constructors corresponding to the above calling of constructors has been given below:<\/p>\n\n\n\n<p><strong><em>Syntax:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName (datatype1 arg1, datatype2 arg2, datatype3 arg3){\n\t\/\/body of constructor1\n}\nClassName object2 (arg){\n\t\/\/body of constructor2\n}\nClassName object3(){\n\t\/\/body of constructor3\n}\n<\/code><\/pre>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;\n\nclass Transport  \n{  \n   public:  \n    int wheels;  \n    string name;\n    string color;\n    string type;\n    \n    \/\/Constructor Overloading\n    Transport(int n)                                                    \/\/ Constructor1  \n    {  \n      wheels=n;  \n    }  \n     Transport(string str1, string str2)                   \/\/Constructor2\n     {\n         name=str1;\n         color=str2;\n     }\n     Transport(string str)                                       \/\/Constructor3\n     {\n         type=str;\n     }\n     \n};  \n\nint main()  \n{  \n  Transport car(4);                 \/\/ Calling the 1st constructor  \n  Transport bike(\"Honda\", \"black\");   \/\/  Calling the 2nd constructor \n  Transport cycle(\"roadways\");        \/\/Calling the 3rd constructor\n  cout&lt;&lt;\"Car has \"&lt;&lt;car.wheels&lt;&lt;\" wheels\\n\";  \n  cout&lt;&lt;\"Name of the bike is \"&lt;&lt;bike.name&lt;&lt;\" and its color is \"&lt;&lt;bike.color&lt;&lt;\"\\n\";\n  cout&lt;&lt;\"Cycle is a \"&lt;&lt;cycle.type&lt;&lt;\" mode of transportation\";\n  return 0;  \n            }  \n<\/code><\/pre>\n\n\n\n<p><em><strong>Output:<\/strong><\/em><\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot.png\"><img decoding=\"async\" width=\"537\" height=\"154\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot.png\" alt=\"copy constructor \" class=\"wp-image-73381\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot.png 537w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-300x86.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-533x154.png 533w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-534x154.png 534w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-150x43.png 150w\" sizes=\"(max-width: 537px) 100vw, 537px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"copy-constructors\"><strong>Copy Constructors:<\/strong><\/h4>\n\n\n\n<p>A special type of constructor that can be used for directly creating a copy of an existing object of a class. To be precise, it initializes the member variables of the new object with the values of the member variables of the existing object that is being copied. The syntaxes for defining and calling the copy constructors have been given below:<\/p>\n\n\n\n<p><strong>Syntax: <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Definition\nClassName objectName (ClassName &amp;existingObject)\n{\n     Var1 = existingObject.Var1;\n     Var2 = existingObject.Va2;\n}\n\n\/\/Calling\nClassName ob1;\nClassName ob2(&amp;ob1);\n<\/code><\/pre>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;\n\nclass Transport  \n{  \n   public:  \n    int wheels;  \n    string name;\n    string color;\n    string type;\n    \n    Transport(int n)                \n    {  \n      wheels=n;  \n    }  \n     Transport(Transport &amp;ob)           \/\/Defining a copy constructor\n     {\n         wheels=ob.wheels;\n     }\n     \n};  \n\nint main()  \n{  \n  Transport car1(4);                 \/\/ Calling the 1st constructor \n  Transport car2(car1);              \/\/Method1 of calling a copy constructor\n  Transport car3=car2;               \/\/Method2 of calling a copy constructor\n  cout&lt;&lt;\"Car2 has \"&lt;&lt;car2.wheels&lt;&lt;\" wheels\\n\";\n  cout&lt;&lt;\"Car3 has \"&lt;&lt;car3.wheels&lt;&lt;\" wheels\";\n  return 0;  \n}  \n\n<\/code><\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-1.png\"><img decoding=\"async\" width=\"264\" height=\"117\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-1.png\" alt=\"c++\" class=\"wp-image-73384\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-1.png 264w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-1-150x66.png 150w\" sizes=\"(max-width: 264px) 100vw, 264px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"default-constructor\"><strong>Default Constructor<\/strong><\/h3>\n\n\n\n<p>It is possible that some constructors might not have any arguments at all. Such types of constructors are called default constructors. In the scenarios where no user-defined constructor exists for a class and one is needed, the compiler makes an implicit declaration of an inline default constructor that does not have any parameters. These constructors do not have any constructor initializer or body.&nbsp;<\/p>\n\n\n\n<p><strong><em>Syntax:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName {\n        \/\/member variables\n       \/\/member functions\n  };\n\nint main()  \n    {\n     ClassName object;      \/\/Calling of default constructor\n      \u2026.\n      \u2026.\n     return 0;\n      }\n<\/code><\/pre>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;\n\nclass Book  \n{  \n   public:  \n    int isbn;  \n    string title;\n    string author;\n    string type;\n    \n};  \n\nint main()  \n{  \n  Book B1;\n    \/\/Default Constructor has been created implicitly\n  cout&lt;&lt;\"Success!\";\n  return 0;  \n            }  \n\n<\/code><\/pre>\n\n\n\n<p><strong><em>Output<\/em><\/strong>:<\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-2.png\"><img decoding=\"async\" width=\"305\" height=\"91\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-2.png\" alt=\"c++\" class=\"wp-image-73386\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-2.png 305w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-2-300x90.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-2-150x45.png 150w\" sizes=\"(max-width: 305px) 100vw, 305px\" \/><\/figure>\n\n\n\n<p><strong><em>NOTE: <\/em><\/strong><em>Every default constructor can be treated as zero argument constructor, but every zero argument constructor cannot be called a default constructor.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"constructor-with-default-arguments\"><strong>Constructor with Default Arguments<\/strong><\/h3>\n\n\n\n<p>A constructor that holds default values for its parameters is known as a constructor with default arguments.&nbsp; The calling of a constructor with default arguments can be done with either one argument or no argument at all.&nbsp; The parameters of a constructor may or may not have default arguments. You can skip passing the values for the variable members with default values while creating the object. It becomes mandatory to pass the values for the variable members when calling a constructor of the class that has arguments without default values. Take a look at its syntax given below:<\/p>\n\n\n\n<p><strong><em>Syntax:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass ClassName(){\n      type variable1;\n      type variable2;\n          \u2026.\n          \u2026.\n\nClassName(type argument1=value1, type argument2=value2, \u2026.)         \/\/Constructor definition\n     {\n                variable1=argument1;\n                variable2=argument2;\n               \u2026.\n               \u2026.\n             }\n     \u2026.\n     \/\/Other member functions\n     \u2026.\n   };\n<\/code><\/pre>\n\n\n\n<p><strong><em>Example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;\n\nclass Book  \n{  \n   public:  \n    int isbn;  \n    string title;\n    string author;\n    string type;\n    \n    \/\/ Constructor with default arguments\n    Book(int n=1244, string name=\"Sally's Nightmare\", string writer=\"M.Jose\", string category=\"Fiction\")                  \n    {  \n      isbn = n;\n      title=name;\n      author=writer;\n      type=category;\n    }  \n     \n};  \n\nint main()  \n{  \n  Book B1(15467, \"The Picture of Dorian Gray\", \"Oscar Wilde\", \"Fiction\");\n  Book B2;   \/\/1\n  cout&lt;&lt;\"Details of first book:\\n\"&lt;&lt; B1.isbn &lt;&lt; endl&lt;&lt; B1.title&lt;&lt; endl&lt;&lt; B1.author&lt;&lt; endl&lt;&lt; B1.type&lt;&lt; endl;\n  cout&lt;&lt;\"\\nDetails of second book: \\n\"&lt;&lt; B2.isbn&lt;&lt; endl&lt;&lt; B2.title&lt;&lt; endl&lt;&lt; B2.author&lt;&lt; endl&lt;&lt; B2.type;\n  return 0;  \n      }  \n\n<\/code><\/pre>\n\n\n\n<p><em>In line 1, no parameter has been passed while creating the B2 object. Hence, the calling of default constructor leads to assigning the default values to the member variables of the object B2.<\/em><\/p>\n\n\n\n<p><strong><em>Output<\/em><\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-full zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-3.png\"><img decoding=\"async\" width=\"442\" height=\"292\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-3.png\" alt=\"copy constructor\" class=\"wp-image-73389\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-3.png 442w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-3-300x198.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/08\/Screenshot-3-150x99.png 150w\" sizes=\"(max-width: 442px) 100vw, 442px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"uses-of-copy-constructor\"><strong>Uses of Copy Constructor<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>When we initialize an object by another object of the same class.&nbsp;<\/li><li>When we return an object as a function value.&nbsp;<\/li><li>When the object is passed to a function as a non-reference parameter.&nbsp;<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-copy-constructor-in-c\"><strong>Example of Copy Constructor in C++<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt; \nusing namespace std; \nclass ABC  \n{  \n  public: int x;  \n    ABC (int a){ \/\/ this is parameterized constructor  \n      x=a;\n    }  \n    ABC (ABC &amp;i){ \/\/ this is copy constructor   \n      x = i.x;  \n    } \n}; \n  \nint main ()  \n{  \n  ABC a1(40); \/\/ Calling the parameterized constructor.  \n  ABC a2(a1); \/\/ Calling the copy constructor.  \n  cout&lt;&lt;a2.x;  \n  return 0;  \n}  <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output\"><strong>Output:&nbsp;<\/strong><\/h4>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-28.png\"><img decoding=\"async\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-28.png\" alt=\"\" class=\"wp-image-36704\" width=\"564\" height=\"129\"><\/figure>\n\n\n\n<p>In the above example, we are using both parameterised constructor and copy constructor.&nbsp;The variable 'i' is the object which stores the copy of variable 'x'.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"constructor-copies\"><strong>Constructor Copies<\/strong><\/h2>\n\n\n\n<p>There are two ways in which copy constructor copies, and they are:&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Shallow copy.&nbsp;<\/li><li>Deep copy.&nbsp;<\/li><\/ol>\n\n\n\n<p>Let\u2019s go through these topics one by one.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"shallow-copy\"><strong>Shallow Copy&nbsp;<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>It is the process of creating a copy of an object by copying data of all the member variables as it is.&nbsp;<\/li><li>Only a default Constructor produces a shallow copy.&nbsp;<\/li><li>A Default Constructor is a constructor which has no arguments.<\/li><\/ul>\n\n\n\n<p>Let us understand deeply how it works through programming example <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;  \nusing namespace std;  \nclass Opp { \n int a;  \n int b; \n int *z;  \n public: Opp() { \n   z=new int; \n } \n void input(int x, int y, int l) { \n    a=x;  \n    b=y;  \n    *z=l;  \n } \n void display() { \n cout&lt;&lt;\"value of a:\" &lt;&lt;a&lt;&lt;endl;  \n cout&lt;&lt;\"value of b:\" &lt;&lt;b&lt;&lt;endl;  \n cout&lt;&lt;\"value of z:\" &lt;&lt;*z&lt;&lt;endl;  \n } \n}; \n \nint main()  {  \n Opp obj1;  \n obj1.input(4,8,12);  \n Opp obj2 = obj1;  \n obj2.display();  \n return 0;  \n}  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-29.png\"><img decoding=\"async\" width=\"713\" height=\"261\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-29.png\" alt=\"\" class=\"wp-image-36710\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-29.png 713w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-29-300x110.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-29-696x255.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-29-150x55.png 150w\" sizes=\"(max-width: 713px) 100vw, 713px\" \/><\/figure>\n\n\n\n<p>Explanation for above one is given in figure below:&nbsp;<\/p>\n\n\n\n<p>&nbsp;obj1 Shallow copy obj 2&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>A&nbsp; B&nbsp; C&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>4&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>8&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>2&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>12<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;<\/strong>x&nbsp; <strong>y&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;z&nbsp;<\/strong><strong><\/strong><\/p>\n\n\n\n<p><strong>Fig: <\/strong>illustration of shallow copy&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In the above figure, both 'obj1' and 'obj2' will be having the same input and both the object variables will be pointing to the <strong>same memory locations.&nbsp;<\/strong><\/li><li>The <strong>changes <\/strong>made to one object will <strong>affect <\/strong>another one.&nbsp;<\/li><li>This particular problem is solved by using the <strong>user-defined constructor, <\/strong>which uses <strong>deep copy.&nbsp;&nbsp;&nbsp;<\/strong><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"deep-copy\"><strong>Deep Copy<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>It dynamically allocates memory for the copy first and then copies the actual value.&nbsp;<\/li><li>In a deep copy, both objects which have to copy and another which has to be copied will be having different memory locations.&nbsp;<\/li><li>So, the changes made to one will not affect another.&nbsp;<\/li><li>This is used by a user-defined copy constructor.&nbsp;<\/li><\/ul>\n\n\n\n<p>The syntax for the user-defined copy constructor is mentioned above refer to that. Let us see the example for the same below&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;iostream&gt; \nusing namespace std; \nclass Number { \n private: int a; \n public: Number(){} \/\/default constructor  Number (int n) { \n Number(int n){\n    a=n; \n } \n Number(Number &amp;x) { \n    a=x.a; \n    cout&lt;&lt;\"copy constructor is invoked\";  \n } \n void display() { \n    cout&lt;&lt;\"value of a:\"&lt;&lt;a&lt;&lt;endl;  \n } \n}; \n\nint main() { \n Number N1(100); \/\/ create an object and assign value to member variable  \n Number N2(N1); \/\/ invoke user defined copy constructor  \n N1.display (); \n N2.display (); \n return 0;  \n} <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output\"><strong>&nbsp;Output:&nbsp;<\/strong><\/h4>\n\n\n<figure class=\"wp-block-image size-large is-resized zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-30.png\"><img decoding=\"async\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-30.png\" alt=\"\" class=\"wp-image-36713\" width=\"468\" height=\"113\"><\/figure>\n\n\n\n<p>&nbsp;N1 N2&nbsp;<\/p>\n\n\n\n<p><strong>n <\/strong><strong><sub>100 <\/sub><\/strong><strong>x<\/strong><strong><sup>100<\/sup><\/strong><\/p>\n\n\n\n<p>\u27a2 In the above example, N1 and N2 are the two objects. \u2018N2\u2019 is the object which stores the value of objec\u2019N1\u2019. \u2018N1\u2019 takes 100 as input and will initialise to \u2018N2\u2019.&nbsp; \u27a2 Both N1 and N2 will have <strong>different locations.&nbsp;<\/strong><\/p>\n\n\n\n<p>\u27a2 Changes made to one will not affect the other.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-copy-constructor-and-assignment-operator\"><strong>Difference between Copy Constructor and Assignment Operator<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\" style=\"margin-top:0px\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\"><strong>Copy Constructor<\/strong><\/td><td class=\"has-text-align-center\" data-align=\"center\"><strong>Assignment Operator<\/strong><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">It is an overloaded constructor.<\/td><td class=\"has-text-align-center\" data-align=\"center\">It is an operator.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">The new object is initialized with an object already existing.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Value of one object is assigned to another object both of which exists already.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Here, both the objects use different or separate memory locations.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Here, different variables points to the same location but only one memory location is used.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">The compiler provides a copy constructor if there is no copy constructor defined in the class.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Bitwise copy will be made if the assignment operator is not overloaded.<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Copy Constructor is used when a new object is being created with the help of the already existing element.<\/td><td class=\"has-text-align-center\" data-align=\"center\">Assignment operator is used when we need to assign an existing object to a new object<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Let us now look at the differences between assignment and initialisation.&nbsp;<\/p>\n\n\n\n<p>Consider the following code segment:&nbsp;<\/p>\n\n\n\n<p>&nbsp;<strong>MYClass a;&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;MYClass b=a;&nbsp;<\/strong><\/p>\n\n\n\n<p>\u27a2 Here, the variable <strong>b <\/strong>is <strong>initialized <\/strong>to a because it is created as a <strong>copy of another variable<\/strong>. When b is created, it will go from containing <strong>garbage data <\/strong>directly to holding a copy of the <strong>value of a <\/strong>with no intermediate step.&nbsp;&nbsp;<\/p>\n\n\n\n<p>However, if we re-write the code as&nbsp;<\/p>\n\n\n\n<p>&nbsp;<strong>MYClass a, b;&nbsp;<\/strong><\/p>\n\n\n\n<p><strong>&nbsp;b=a;&nbsp;<\/strong><\/p>\n\n\n\n<p>\u27a2 Then two is assigned the value of one. Note that before we reach the line b=a, B already contains a value. This is the difference between assignment and initialisation.&nbsp; When a variable is created is set to hold a new value, it is being assigned.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"note\">&nbsp;<strong>Note: <\/strong><\/h4>\n\n\n\n<p>We can make the copy constructor private, but when we make the constructor private, that class\u2019s objects cannot be copied. This is useful when our class has pointer variables or dynamically allocated resources. In such a situation, we can either write our copy constructor or make a private copy constructor so that the user gets compiler errors rather than something at runtime.&nbsp;<\/p>\n\n\n\n<p>This brings us to the end of the blog on Copy Constructor in C++. We hope that you were able to gain some knowledge from the same. If you wish to learn more such concepts about constructor in C++ or any other programming languages, you can join&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\">Great Learning Academy\u2019s Free Online Courses<\/a>&nbsp;today.<\/p>\n\n\n\n<p>Also, if you are preparing for Interviews, check out these&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-interview-questions\/\" target=\"_blank\">Interview Questions for C++<\/a>&nbsp;to ace it like a pro.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Among different types of constructors, the copy constructor is the type that uses another object within the same class to initialize the data members of the class within the class. In other words, it creates a copy of an existing object of the class. Constructor What is a Copy Constructor in C++? Classification of Constructors [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":27516,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[],"content_type":[],"class_list":["post-26168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Copy Constructor in C++ -Types, Examples &amp; Definition | Great Learning<\/title>\n<meta name=\"description\" content=\"Copy Constructor in C++: A method or member function which initialize an object using another object within the same class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy Constructor in C++ - Everything you Need to know About\" \/>\n<meta property=\"og:description\" content=\"Copy Constructor in C++: A method or member function which initialize an object using another object within the same class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-18T09:07:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:38:45+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Copy Constructor in C++ - Everything you Need to know About\",\"datePublished\":\"2021-08-18T09:07:00+00:00\",\"dateModified\":\"2024-09-03T05:38:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/\"},\"wordCount\":1780,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1702515220.jpg\",\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/\",\"name\":\"Copy Constructor in C++ -Types, Examples & Definition | Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1702515220.jpg\",\"datePublished\":\"2021-08-18T09:07:00+00:00\",\"dateModified\":\"2024-09-03T05:38:45+00:00\",\"description\":\"Copy Constructor in C++: A method or member function which initialize an object using another object within the same class.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1702515220.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/shutterstock_1702515220.jpg\",\"width\":1000,\"height\":667,\"caption\":\"copy constructor in C++\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/copy-constructor-in-cpp\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Copy Constructor in C++ &#8211; Everything you Need to know About\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Copy Constructor in C++ -Types, Examples & Definition | Great Learning","description":"Copy Constructor in C++: A method or member function which initialize an object using another object within the same class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Copy Constructor in C++ - Everything you Need to know About","og_description":"Copy Constructor in C++: A method or member function which initialize an object using another object within the same class.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-08-18T09:07:00+00:00","article_modified_time":"2024-09-03T05:38:45+00:00","og_image":[{"width":1000,"height":667,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Copy Constructor in C++ - Everything you Need to know About","datePublished":"2021-08-18T09:07:00+00:00","dateModified":"2024-09-03T05:38:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/"},"wordCount":1780,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg","articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/","url":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/","name":"Copy Constructor in C++ -Types, Examples & Definition | Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg","datePublished":"2021-08-18T09:07:00+00:00","dateModified":"2024-09-03T05:38:45+00:00","description":"Copy Constructor in C++: A method or member function which initialize an object using another object within the same class.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg","width":1000,"height":667,"caption":"copy constructor in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/copy-constructor-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Copy Constructor in C++ &#8211; Everything you Need to know About"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg",1000,667,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220-768x512.jpg",768,512,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg",1000,667,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg",1000,667,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220.jpg",1000,667,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220-640x667.jpg",640,667,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/03\/shutterstock_1702515220-150x100.jpg",150,100,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Among different types of constructors, the copy constructor is the type that uses another object within the same class to initialize the data members of the class within the class. In other words, it creates a copy of an existing object of the class. Constructor What is a Copy Constructor in C++? Classification of Constructors&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/26168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=26168"}],"version-history":[{"count":16,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/26168\/revisions"}],"predecessor-version":[{"id":79844,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/26168\/revisions\/79844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/27516"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=26168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=26168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=26168"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=26168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}