Object Assignment in C++ - Meaning, Working and Examples
In C++, object assignment is the process of copying the values of one existing object into another existing object of the same class. It is generally performed using the assignment operator (=). Object assignment is an important concept in Object-Oriented Programming (OOP). It allows programmers to transfer the state of one object to another object without creating a new object.
C++ में Object Assignment वह प्रक्रिया है जिसमें एक existing object की values को उसी class के दूसरे existing object में copy किया जाता है। यह सामान्यतः Assignment Operator (=) की सहायता से किया जाता है। Object Assignment Object-Oriented Programming (OOP) की एक महत्वपूर्ण अवधारणा है। इसकी सहायता से programmer एक object की state को दूसरे object में transfer कर सकता है, बिना नया object बनाए।
What Is Object Assignment in C++?
Object assignment means assigning one object's data members to another object of the same class. When the assignment operator is used with objects, C++ performs memberwise assignment through the class's copy assignment operator.
Object Assignment का अर्थ है एक object के data members को उसी class के दूसरे object में assign करना। जब objects के साथ assignment operator का उपयोग किया जाता है, तब C++ class के copy assignment operator के माध्यम से memberwise assignment करता है।
Syntax:
object2 = object1;Here:
object1is the source object.object2is the destination object.The assignment operator (
=) copies the state ofobject1intoobject2.
यहाँ:
object1source object है।object2destination object है।Assignment operator (
=),object1की state कोobject2में copy करता है।
Simple Example of Object Assignment in C++
The following program demonstrates how one object can be assigned to another object of the same class.
निम्न program दिखाता है कि एक object को उसी class के दूसरे object में कैसे assign किया जा सकता है।
#include <iostream>using namespace std;class Student {public:int rollNo;float marks;void display() const {cout << "Roll Number: " << rollNo << endl;cout << "Marks: " << marks << endl;}};int main() {Student s1, s2;s1.rollNo = 101;s1.marks = 85.5;// Object Assignments2 = s1;cout << "First Student:\n";s1.display();cout << "\nSecond Student:\n";s2.display();return 0;}
Output:
First Student:Roll Number: 101Marks: 85.5Second Student:Roll Number: 101Marks: 85.5
Explanation:
Two objects,
s1ands2, are created.Values are assigned to the data members of
s1.The statement
s2 = s1copies the data members ofs1intos2.Both objects now contain the same member values.
Both objects remain separate objects in memory.
हिन्दी में समझें:
s1औरs2नाम के दो objects बनाए गए हैं।s1के data members में values assign की गई हैं।s2 = s1statement,s1के data members कोs2में copy करता है।अब दोनों objects में समान member values हैं।
दोनों objects memory में अलग-अलग objects बने रहते हैं।
How Does Object Assignment Work in C++?
When an assignment statement such as obj2 = obj1 is executed, the copy assignment operator of the class is called. If the class does not provide its own copy assignment operator, the compiler generally provides an implicitly defined one that performs memberwise assignment, subject to the class's member types and rules.
जब obj2 = obj1 जैसा assignment statement execute होता है, तब class का copy assignment operator call होता है। यदि class अपना copy assignment operator provide नहीं करती, तो compiler सामान्यतः implicitly defined operator provide करता है, जो class के member types और rules के अनुसार memberwise assignment करता है।
Consider the following class:
निम्न class को समझिए:
class Employee {public:int id;float salary;};
When we write:
जब हम लिखते हैं:
Employee e1, e2;e1.id = 10;e1.salary = 25000;e2 = e1;
The assignment operator copies the values of id and salary from e1 into e2.
Assignment operator, e1 के id और salary की values को e2 में copy करता है।
Conceptually, the operation is similar to:
Conceptually, यह operation निम्नलिखित के समान है:
e2.id = e1.id;e2.salary = e1.salary;
This is an illustration of memberwise assignment for this simple class, not a replacement for the actual compiler-generated operator.
यह इस simple class के लिए memberwise assignment को समझाने का एक तरीका है। यह वास्तविक compiler-generated operator का replacement नहीं है।
Object Assignment Using a User-Defined Copy Assignment Operator
C++ allows programmers to define their own copy assignment operator. This is useful when a class needs special logic while assigning one object to another, particularly when managing resources.
C++ programmer को अपना copy assignment operator define करने की अनुमति देता है। यह तब उपयोगी होता है जब एक object को दूसरे object में assign करते समय किसी विशेष logic की आवश्यकता हो, विशेष रूप से resources manage करते समय।
Syntax:
ClassName& operator=(const ClassName& other) {// Assignment logicreturn *this;}
Here:
operator=overloads the assignment operator.const ClassName& otherreceives the source object by constant reference.return *thisreturns the current destination object by reference.
यहाँ:
operator=assignment operator को overload करता है।const ClassName& othersource object को constant reference के रूप में receive करता है।return *thiscurrent destination object को reference के रूप में return करता है।
Example:
#include <iostream>using namespace std;class Student {private:int rollNo;float marks;public:Student(int r, float m) {rollNo = r;marks = m;}// Copy Assignment OperatorStudent& operator=(const Student& other) {if (this != &other) {rollNo = other.rollNo;marks = other.marks;}return *this;}void display() const {cout << "Roll Number: " << rollNo << endl;cout << "Marks: " << marks << endl;}};int main() {Student s1(101, 90.5);Student s2(102, 75.0);// Object Assignments2 = s1;cout << "Student 1:\n";s1.display();cout << "\nStudent 2:\n";s2.display();return 0;}
Output:
Student 1:Roll Number: 101Marks: 90.5Student 2:Roll Number: 101Marks: 90.5
Explanation:
The class defines a user-defined copy assignment operator.
The statement
s2 = s1calls this operator.The values of
rollNoandmarksare copied froms1intos2.The self-assignment check
this != &otheravoids unnecessary assignment when an object is assigned to itself.return *thisallows assignment expressions to return the destination object.
हिन्दी में समझें:
Class में user-defined copy assignment operator define किया गया है।
s2 = s1statement इस operator को call करता है।s1केrollNoऔरmarksकी valuess2में copy होती हैं।this != &otherself-assignment check यह सुनिश्चित करता है कि object को स्वयं assign करने पर अनावश्यक assignment न हो।return *thisassignment expression के माध्यम से destination object को return करता है।
Difference Between Object Assignment and Copy Constructor
Object assignment and copy construction both copy information from one object to another, but they are used in different situations.
Object Assignment और Copy Constructor दोनों एक object से दूसरे object में information copy करते हैं, लेकिन इनका उपयोग अलग-अलग परिस्थितियों में होता है।
| Object Assignment | Copy Constructor |
|---|---|
| Assigns data to an already existing object. | Initializes a new object from an existing object. |
Uses the assignment operator (=). | Uses the copy constructor. |
Example: obj2 = obj1; | Example: ClassName obj2 = obj1; |
| The destination object already exists. | The destination object is being created. |
| May require a user-defined copy assignment operator. | May require a user-defined copy constructor. |
हिन्दी में अंतर:
| Object Assignment | Copy Constructor |
|---|---|
| पहले से मौजूद object में data assign करता है। | Existing object से नया object initialize करता है। |
Assignment operator (=) का उपयोग करता है। | Copy constructor का उपयोग करता है। |
उदाहरण: obj2 = obj1; | उदाहरण: ClassName obj2 = obj1; |
| Destination object पहले से मौजूद होता है। | Destination object नया बनाया जा रहा होता है। |
| User-defined copy assignment operator की आवश्यकता हो सकती है। | User-defined copy constructor की आवश्यकता हो सकती है। |
Example:
Student s1(101, 90.5);// Copy ConstructorStudent s2 = s1;// Object AssignmentStudent s3(102, 75.0);s3 = s1;
In the first statement, s2 is initialized using s1, so the copy constructor is used. In the second case, s3 already exists, so its copy assignment operator is used.
पहले statement में s2 को s1 की सहायता से initialize किया जाता है, इसलिए copy constructor उपयोग होता है। दूसरे case में s3 पहले से मौजूद है, इसलिए उसका copy assignment operator उपयोग होता है।
Self-Assignment in C++
Self-assignment occurs when an object is assigned to itself. For example, obj = obj is a self-assignment statement.
Self-Assignment तब होता है जब किसी object को उसी object में assign किया जाता है। उदाहरण के लिए, obj = obj एक self-assignment statement है।
Example:
Student s1(101, 90.5);s1 = s1;
In a simple class, self-assignment may not cause any visible problem. However, in classes that manage dynamic memory or other resources, a poorly implemented assignment operator can cause serious problems.
Simple class में self-assignment से कोई दिखाई देने वाली समस्या नहीं हो सकती। लेकिन dynamic memory या अन्य resources manage करने वाली classes में गलत तरीके से implement किया गया assignment operator गंभीर समस्याएँ उत्पन्न कर सकता है।
A self-assignment check is commonly used in a user-defined copy assignment operator:
User-defined copy assignment operator में सामान्यतः self-assignment check का उपयोग किया जाता है:
if (this != &other) {// Perform assignment}
This check prevents the assignment logic from unnecessarily modifying or releasing resources when the source and destination are the same object.
यह check तब assignment logic को अनावश्यक रूप से resources modify या release करने से रोकता है, जब source और destination एक ही object हों।
Chained Object Assignment in C++
In C++, assignment operators can be chained. This is possible because the assignment operator normally returns a reference to the destination object.
C++ में assignment operators को chain किया जा सकता है। ऐसा इसलिए संभव है क्योंकि assignment operator सामान्यतः destination object का reference return करता है।
Example:
#include <iostream>using namespace std;class Number {public:int value;Number(int v) {value = v;}};int main() {Number n1(100);Number n2(200);Number n3(300);n3 = n2 = n1;cout << "n1 = " << n1.value << endl;cout << "n2 = " << n2.value << endl;cout << "n3 = " << n3.value << endl;return 0;}
Output:
n1 = 100n2 = 100n3 = 100
Explanation: Assignment operators are evaluated from right to left. First, n1 is assigned to n2. Then the result of that assignment is assigned to n3.
व्याख्या: Assignment operators का evaluation right to left होता है। पहले n1 को n2 में assign किया जाता है। इसके बाद उस assignment के result को n3 में assign किया जाता है।
Object Assignment and Dynamic Memory
When a class contains pointers that own dynamically allocated memory, the default memberwise copy assignment can be unsafe. It may copy pointer addresses and cause two objects to refer to the same allocation.
जब किसी class में ऐसे pointers होते हैं जो dynamically allocated memory के owner होते हैं, तब default memberwise copy assignment असुरक्षित हो सकता है। यह pointer addresses copy करके दो objects को एक ही memory allocation refer करने के लिए मजबूर कर सकता है।
In such cases, a properly designed copy assignment operator should manage memory carefully. If a class defines its own destructor, copy constructor, or copy assignment operator to manage resources, the Rule of Three should be considered. Modern C++ generally recommends RAII and standard library types to avoid manual resource management.
ऐसी स्थिति में properly designed copy assignment operator को memory सावधानीपूर्वक manage करनी चाहिए। यदि कोई class resources manage करने के लिए अपना destructor, copy constructor या copy assignment operator define करती है, तो Rule of Three पर ध्यान देना चाहिए। Modern C++ में manual resource management से बचने के लिए सामान्यतः RAII और standard library types की सलाह दी जाती है।
Advantages of Object Assignment
Object assignment provides several benefits in C++ programming.
C++ programming में Object Assignment के कई लाभ हैं।
Code Reusability: It allows the state of one object to be copied into another object.
Code Reusability: यह एक object की state को दूसरे object में copy करने की सुविधा देता है।
Easy Data Transfer: It makes transferring object data straightforward.
Easy Data Transfer: यह objects के data को transfer करना आसान बनाता है।
Operator Overloading: Programmers can customize assignment behavior using a user-defined copy assignment operator.
Operator Overloading: Programmer user-defined copy assignment operator की सहायता से assignment behavior को customize कर सकता है।
Convenient Programming: It makes working with objects simpler and more readable.
Convenient Programming: यह objects के साथ काम करना सरल और अधिक readable बनाता है।
Frequently Asked Questions (FAQs)
1. What is Object Assignment in C++?
Object assignment is the process of copying the state of one existing object into another existing object of the same class using the assignment operator.
Object Assignment वह प्रक्रिया है जिसमें assignment operator की सहायता से एक existing object की state को उसी class के दूसरे existing object में copy किया जाता है।
2. Which operator is used for object assignment in C++?
The assignment operator (=) is used for object assignment in C++.
C++ में Object Assignment के लिए Assignment Operator (=) का उपयोग किया जाता है।
3. What is a copy assignment operator?
A copy assignment operator is a special member function that assigns the state of one existing object to another existing object of the same class.
Copy Assignment Operator एक special member function है जो एक existing object की state को उसी class के दूसरे existing object में assign करता है।
4. What is the difference between object assignment and copy constructor?
Object assignment copies data into an already existing object, whereas a copy constructor initializes a new object from an existing object.
Object Assignment पहले से मौजूद object में data copy करता है, जबकि Copy Constructor किसी existing object से नया object initialize करता है।
5. What is self-assignment in C++?
Self-assignment occurs when an object is assigned to itself, such as obj = obj.
Self-Assignment तब होता है जब किसी object को उसी object में assign किया जाता है, जैसे obj = obj।
6. Can we overload the assignment operator in C++?
Yes. C++ allows programmers to overload the assignment operator to define custom assignment behavior for their classes.
हाँ। C++ programmer को अपनी classes के लिए custom assignment behavior define करने हेतु assignment operator overload करने की अनुमति देता है।
7. Does object assignment create a new object?
No. Object assignment updates an already existing destination object. Creating a new object from another object generally involves copy construction. Object Assignment is an important feature of C++ that allows programmers to copy the state of one object into another existing object. It is performed using the assignment operator (=), and the class's copy assignment operator determines how the assignment takes place. Understanding object assignment, copy constructors, self-assignment, and resource management helps programmers write reliable and efficient C++ programs.
नहीं। Object Assignment पहले से मौजूद destination object को update करता है। किसी दूसरे object से नया object बनाने के लिए सामान्यतः Copy Construction का उपयोग होता है। Object Assignment C++ का एक महत्वपूर्ण feature है, जो programmer को एक object की state दूसरे existing object में copy करने की सुविधा देता है। यह Assignment Operator (=) की सहायता से किया जाता है और class का Copy Assignment Operator यह निर्धारित करता है कि assignment किस प्रकार होगा। Object Assignment, Copy Constructors, Self-Assignment और Resource Management को समझने से programmers अधिक reliable और efficient C++ programs लिख सकते हैं।
Comments
Post a Comment