Copy constructor in C ++

Copy constructor is a constructor that creates an object by initializing it with an object of the same class, which was created earlier.

Copy constructor is a constructor that creates an object by initializing it with an object of the same class, which was created earlier. Copy constructor is used to:

Initialize an object from another object with the same type.

Copy an object to pass it as a parameter to a function.

Copy an object to return it from a function.

If a copy constructor is not defined in a class, the compiler will automatically define a copy of the constructor. If the class has pointer variables and there are some dynamic memory allocations, then it is a necessity to have a copy of the constructor. The most common form of copy constructor in C ++ is:

 ten_lop ( const ten_lop & obj ) { // phan than cua constructor } 

Here, obj is a reference to an object that is being used to initialize another object.

 #include using namespace std ; class Line { public : int layChieuDai ( void ); Line ( int dai ); // Day la mot constructor don gian Line ( const Line & obj ); // Day la copy constructor ~ Line (); // Day la destructor private : int * contro ; }; // Phan dinh nghia cac ham thanh vien, bao gom constructor, copy constructor, destructor Line :: Line ( int dai ) { cout << "Constructor: cap phat bo nho cho con tro contro" << endl ; // cap phat bo nho cho con tro contro = new int ; * contro = dai ; } Line :: Line ( const Line & obj ) { cout << "Copy constructor: cap phat bo nho cho con tro contro" << endl ; contro = new int ; * contro = * obj . contro ; // sao chep gia tri } Line ::~ Line ( void ) { cout << "Giai phong bo nho!" << endl ; delete contro ; } int Line :: layChieuDai ( void ) { return * contro ; } void hienThi ( Line obj ) { cout << "Chieu dai cua line la: " << obj . layChieuDai () << endl ; } // ham main cua chuong trinh int main ( ) { Line line ( 50 ); hienThi ( line ); return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Copy constructor in C ++ Picture 1Copy constructor in C ++ Picture 1

You follow the same example above with a small change to create another object using an object that already exists with the same type.

 #include using namespace std ; class Line { public : int layChieuDai ( void ); Line ( int dai ); // Day la mot constructor don gian Line ( const Line & obj ); // Day la copy constructor ~ Line (); // Day la destructor private : int * contro ; }; // Phan dinh nghia cac ham thanh vien, bao gom constructor, copy constructor, destructor Line :: Line ( int dai ) { cout << "Constructor: cap phat bo nho cho con tro contro" << endl ; // cap phat bo nho cho con tro contro = new int ; * contro = dai ; } Line :: Line ( const Line & obj ) { cout << "Copy constructor: cap phat bo nho cho con tro contro" << endl ; contro = new int ; * contro = * obj . contro ; // sao chep gia tri } Line ::~ Line ( void ) { cout << "Giai phong bo nho!" << endl ; delete contro ; } int Line :: layChieuDai ( void ) { return * contro ; } void hienThi ( Line obj ) { cout << "Chieu dai cua line la: " << obj . layChieuDai () << endl ; } // Ham main cua chuong trinh int main ( ) { Line line1 ( 45 ); Line line2 = line1 ; // Lenh nay cung goi copy constructor hienThi ( line1 ); hienThi ( line2 ); return 0 ; } 

Compiling and running the above C ++ program will produce the following results:

Copy constructor in C ++ Picture 2Copy constructor in C ++ Picture 2

According to Tutorialspoint

Previous lesson: Contructor and Destructor in C ++

Next lesson: Friend function in C ++

4 ★ | 2 Vote