Copy Constructor in C++ - Definition, Syntax and Examples
A copy constructor in C++ is a special constructor that initializes a new object using an existing object of the same class. It is commonly used when one object is created as a copy of another object. C++ can provide an implicit copy constructor, or the programmer can define a custom copy constructor.
C++ में Copy Constructor एक विशेष constructor होता है, जिसका उपयोग किसी existing object की सहायता से उसी क्लास का नया object initialize करने के लिए किया जाता है।
What is a Copy Constructor in C++?
A copy constructor is a special member function that initializes a new object using another object of the same class.
Definition: A copy constructor in C++ is a constructor that creates a new object by copying the values of an existing object of the same class.
परिभाषा: Copy Constructor C++ का एक विशेष constructor है, जो किसी existing object के values को copy करके नया object initialize करता है।
For example:
Student s1(101, 85.5);Student s2(s1);
Here, s2 is initialized using the existing object s1. The copy constructor is called to initialize s2.
यहाँ s1 एक existing object है और s2 को उसी object की copy के रूप में बनाया गया है। इस प्रक्रिया में copy constructor कॉल होता है।
Syntax of Copy Constructor
class ClassName{public:ClassName(const ClassName &obj){// Copy data members}};
Explanation:
ClassNameis the name of the class.The constructor has the same name as the class.
It accepts a reference to an object of the same class.
constprevents the constructor from modifying the source object through this reference.The constructor initializes the new object using the source object's data.
यहाँ const ClassName &obj उसी क्लास के existing object का constant reference है। इसके माध्यम से नए object को initialize किया जाता है।
Example 1: Simple Copy Constructor in C++
The following program demonstrates a user-defined copy constructor.
#include <iostream>using namespace std;class Student{private:int rollNo;float marks;public:Student(int r, float m){rollNo = r;marks = m;}Student(const Student &obj){rollNo = obj.rollNo;marks = obj.marks;}void display(){cout << "Roll Number: " << rollNo << endl;cout << "Marks: " << marks << endl;}};int main(){Student s1(101, 85.5);Student s2(s1);cout << "First Student:" << endl;s1.display();cout << "Copied Student:" << endl;s2.display();return 0;}
Output:
First Student:Roll Number: 101Marks: 85.5Copied Student:Roll Number: 101Marks: 85.5
Explanation:
The
Studentclass contains two data members:rollNoandmarks.The parameterized constructor initializes the first object
s1.The copy constructor accepts
s1as a constant reference.It copies the values of
rollNoandmarksinto the new objects2.Both objects contain the same initial values.
व्याख्या:
Studentक्लास मेंrollNoऔरmarksदो data members हैं।Parameterized constructor की सहायता से
s1object बनाया जाता है।Copy constructor,
s1को reference के रूप में प्राप्त करता है।यह
s1के data members कोs2में copy करता है।दोनों objects में समान प्रारंभिक values होती हैं।
Example 2: Copy Constructor Called Automatically
A copy constructor can be invoked when a new object is initialized from an existing object.
जब किसी existing object से नया object initialize किया जाता है, तब copy constructor कॉल हो सकता है।
#include <iostream>using namespace std;class Demo{private:int number;public:Demo(int n){number = n;}Demo(const Demo &obj){number = obj.number;cout << "Copy constructor called." << endl;}void display(){cout << "Number = " << number << endl;}};int main(){Demo d1(100);Demo d2 = d1;d2.display();return 0;}
Output:
Copy constructor called.Number = 100
Explanation:
The statement:
Demo d2 = d1;initializes a new object d2 using the existing object d1. Therefore, the copy constructor is invoked.
यहाँ Demo d2 = d1; assignment operator जैसा दिखाई देता है, लेकिन यह नया object initialize करता है। इसलिए copy constructor कॉल होता है, copy assignment operator नहीं।
Example 3: Copy Constructor with Multiple Objects
We can create multiple objects by copying an existing object.
एक existing object की सहायता से कई नए objects बनाए जा सकते हैं।
#include <iostream>using namespace std;class Employee{private:int id;double salary;public:Employee(int i, double s){id = i;salary = s;}Employee(const Employee &e){id = e.id;salary = e.salary;}void display(){cout << "Employee ID: " << id << endl;cout << "Salary: " << salary << endl;}};int main(){Employee e1(101, 35000);Employee e2(e1);Employee e3(e1);cout << "Employee 1:" << endl;e1.display();cout << "Employee 2:" << endl;e2.display();cout << "Employee 3:" << endl;e3.display();return 0;}
Output:
Employee 1:Employee ID: 101Salary: 35000Employee 2:Employee ID: 101Salary: 35000Employee 3:Employee ID: 101Salary: 35000
Explanation:
The objects e2 and e3 are initialized using e1. The copy constructor copies the data members of e1 into each new object.
इस प्रोग्राम में e2 और e3 को e1 से initialize किया गया है। प्रत्येक नए object के लिए copy constructor का उपयोग होता है।
Compiler-Generated Copy Constructor
If a class does not declare its own copy constructor, the compiler generally implicitly declares one when needed.
The implicitly defined copy constructor performs member-wise copying of the base-class subobjects and non-static data members.
यदि प्रोग्रामर स्वयं copy constructor define नहीं करता है, तो compiler सामान्यतः आवश्यकता पड़ने पर implicit copy constructor उपलब्ध कराता है।
Example:
#include <iostream>using namespace std;class Demo{public:int number;};int main(){Demo d1;d1.number = 50;Demo d2 = d1;cout << "Original Object: " << d1.number << endl;cout << "Copied Object: " << d2.number << endl;return 0;}
Output:
Original Object: 50Copied Object: 50
Explanation:
No copy constructor is explicitly defined in the Demo class. The compiler-generated copy constructor copies the data member number from d1 to d2.
इस उदाहरण में programmer ने copy constructor define नहीं किया है। Compiler द्वारा उपलब्ध कराए गए copy constructor की सहायता से d1 का number नए object d2 में copy होता है।
Characteristics of Copy Constructor
- A copy constructor has the same name as the class.इसका नाम क्लास के नाम के समान होता है।
- It does not have a return type.इसका कोई return type नहीं होता है।
- It initializes a new object using an existing object of the same class.यह उसी क्लास के existing object की सहायता से नया object initialize करता है।
- It generally accepts a reference to an object of the same class.यह सामान्यतः उसी क्लास के object का reference स्वीकार करता है।
- It is commonly declared with a
constreference parameter.इसे सामान्यतःconstreference parameter के साथ declare किया जाता है। - The compiler can provide an implicit copy constructor if the class does not declare one.यदि क्लास में copy constructor declare नहीं किया गया है, तो compiler implicit copy constructor उपलब्ध करा सकता है।
- A user-defined copy constructor can perform deep copying when required.आवश्यकता होने पर programmer user-defined copy constructor द्वारा deep copy कर सकता है।
- It is called in situations where a new object is initialized from another object, subject to language rules and copy elision.जब किसी नए object को दूसरे object से initialize किया जाता है, तब यह उपयोग हो सकता है। हालांकि कुछ स्थितियों में compiler copy elision कर सकता है।
When is a Copy Constructor Called?
A copy constructor may be called in the following situations:
When a new object is initialized from an existing object.
Student s2(s1);When a new object is initialized using copy initialization syntax.
Student s2 = s1;When an object is passed to a function by value.
void display(Student s){// Function body}When an object is returned from a function by value, depending on copy elision and the language rules.
Student createStudent(){Student s(101);return s;}
In modern C++, some copies are eliminated by the compiler. Therefore, a copy constructor may not execute in every situation where an object appears to be copied.
Modern C++ में compiler कुछ copying operations को eliminate कर सकता है। इसलिए हर ऐसी स्थिति में copy constructor का execute होना आवश्यक नहीं है जहाँ object की copy दिखाई देती है।
Frequently Asked Questions (FAQs)
1. What is a copy constructor in C++?
A copy constructor is a special constructor that initializes a new object using another object of the same class.
2. What is the syntax of a copy constructor?
ClassName(const ClassName &obj){// Copy data members}
3. Does the compiler provide a copy constructor automatically?
Yes. If the class does not declare a copy constructor, the compiler can implicitly declare one. Its member-wise copying may not be suitable for classes that own dynamically allocated resources.
4. What is the difference between shallow copy and deep copy?
Shallow copy copies pointer values, so two objects may point to the same memory. Deep copy allocates separate memory and copies the actual data.
5. Can a copy constructor be private?
Yes. A copy constructor can be private, although this restricts copying from outside the class and may be useful in certain controlled-object designs.
6. Can a copy constructor accept an object by value?
No. A copy constructor should accept the source object by reference. Passing it by value would itself require copying the argument, resulting in recursive copy-constructor calls.
7. Is a copy constructor the same as an assignment operator?
No. A copy constructor initializes a new object from an existing object, whereas a copy assignment operator assigns values to an already existing object.
A copy constructor is an important feature of C++ used to initialize an object from another object of the same class. It can be provided by the compiler or defined by the programmer. Understanding copy constructors, shallow copying and deep copying is essential for writing safe C++ programs, particularly when a class manages dynamically allocated memory.
Comments
Post a Comment