Copy Constructor – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

copy constructorCopy Constructor – Reference Note
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Copy Constructor
A copy constructor is used to declare and initialize an object from another object. For example, the statement integer 12(11); would define the objects 12 and at the same time initialize it to the value of 11. Another form of this statement is integer 12 = 11;

Thus, the process of initialization through a copy constructor is known as copy initialization. A copy constructor is always used when the compiler has to create a temporary object of a class object. The copy constructors are used in the following situations;

  • The initialization of an object by another object of the same class.
  • Return of objects as a function value.
  • Stating the objects as by value parameters of a function.

The syntax of copy constructor is;
class_name :: class_name(class_name &ptr)
Let us take one example
class Test
{
private:
...........
public:
Test (Test &t);
};
Test t1;
Test t2(t1);

(Visited 150 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary