MENU
Question -

What is a Copy Constructor ? Give a suitable example of Copy Constructor using a class in C+ + .



Answer -

Copy Constructor
• A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object.
• A copy constructor is a constructor of the form classname (classname &). The compiler will use the copy constructors
• whenever you initialize an instance using the values of another instance of the same type.
• Copying of objects is achieved by the use of a copy constructor and a assignment operator.
Example:

class Sample {int i, j ; 
public:
Sample(int a, int b) // constructor {i=a;j =b;}
Sample (Sample & s)
//copy constructor
{j=s.j ; i=s.j;
cout << "\n Copy constructor
working \n";
}
void print (void)
{cout<
}
};

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
×