Copy Constructor In C++ With Example

Card image cap

Copy Constructor?

Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class type. It is usually of the form X (X&), where X is the class name. The compiler provides a default Copy Constructor to all the classes.


Copy Constructor Syntax:



A copy constructor is called whenever a new variable is created from an object.

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


Syntax:

     class A  
{  
    A(A &x) //  copy constructor.  
   {  
       // copyconstructor.  
   }  
}

Card image cap

Example 1:



Output :

    20
Card image cap

Example 2:



Output :

    aimtocode
    aimtocode
    aimtocode
    aimtocode.com

Example 3:


Output :

    Im Constructor
    This Is Copy Constructor
    This Is Constructor
    Values :10      20
    Values :10      20
    Values :10      20
Card image cap