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:

Copy 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 2

According to Tutorialspoint

Previous lesson: Contructor and Destructor in C ++

Next lesson: Friend function in C ++

4 ★ | 2 Vote

May be interested

  • How to copy on the computer, copy and paste website contentHow to copy on the computer, copy and paste website content
    in today's article, let's learn with taimienphi how to copy and paste text, files and images on windows and mac computers! the method is extremely simple and quick.
  • How to copy data on a laptop?How to copy data on a laptop?
    laptops (also known as laptops) have a relatively short usage life. over time, internal hardware components will be outdated quickly and damage is inevitable. therefore, this is the time you should prepare to copy the data stored on your laptop to a new device.
  • How to copy a webpage to prevent copying (Ctrl + C)How to copy a webpage to prevent copying (Ctrl + C)
    you found a satisfactory document but that site did not allow you to copy content to your computer. here's how to easily copy your content!
  • Fixing errors cannot Copy Paste in WindowsFixing errors cannot Copy Paste in Windows
    copy paste is one of the most basic and handy functions in windows. if you can't copy and paste on windows, follow these steps.
  • Textify tool copies all content on WindowsTextify tool copies all content on Windows
    normally, to copy a certain content, you still use copy or ctrl + c. however, this command only works for the text content, but for the types of bulletin boards and boxes. voice, ... we can't use this copy.
  • How to Copy and Paste on a MacHow to Copy and Paste on a Mac
    this wikihow teaches you how to copy and paste text or files on your mac computer. while your mac's built-in menu bar is the preferred way to copy and paste information, you can also use your mac's trackpad or keyboard to copy and paste....
  • How to copy content between iPhone and PCHow to copy content between iPhone and PC
    magic copy is an application that supports copying content between ios, android, windows, and mac devices.
  • How to Copy and Paste on MacHow to Copy and Paste on Mac
    this article shows you how to copy and paste text or files on a mac computer. although the mac's built-in menu bar is often used to copy and paste data, you can still use your trackpad or keyboard to do this.
  • Please download WinX DVD Copy Pro, DVD copying software and convert to ISO for $ 67.95, free of chargePlease download WinX DVD Copy Pro, DVD copying software and convert to ISO for $ 67.95, free of charge
    winx dvd copy pro is a tool to assist users to copy dvd for safe storage and convert to iso easily without loss of image quality.
  • How to Become a Copy EditorHow to Become a Copy Editor
    copy editors spend a lot of time looking over written material for publications. while this involves basic proofreading skills, the role of a copy editor also involves a great understanding of grammar, style guides, and how to write well....