Table of Contents
Copy Constructor in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
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 helps:
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 ++
Frequently Asked Questions
What is 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.
Why is Copy Constructor in C ++ important?
A clear understanding of Copy Constructor in C ++ helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach Copy Constructor in C ++?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.