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 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:
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:
According to Tutorialspoint
Previous lesson: Contructor and Destructor in C ++
Next lesson: Friend function in C ++
You should read it
- PHP functions
- Summary of trigonometric functions in Excel
- Complete financial functions in Excel you should know
- Recursive function in Python
- Comparison functions in Excel - How to use comparison functions and examples using comparison functions
- Differentiate between SUM, SUMIF, SUMIFS and DSUM functions
- 6 useful functions in Google Sheets you may not know yet
- MINA and MAXA functions in Excel
- Syntax and description of search and reference functions in Excel
- How to use math functions in C#
- Input & Output in C
- Summary of information functions in Excel