Object as Function Argument in C++ - Passing Objects to Functions with Examples
Introduction to Object as Function Argument in C++
In C++, an object can be passed as an argument to a function just like other variables such as integers, floats and strings. This allows a function to access the data members and member functions of the object.
C++ में किसी object को भी integer, float और string जैसे variables की तरह function में argument के रूप में pass किया जा सकता है। इससे function उस object के data members और member functions को access कर सकता है।
An object can be passed to a function in different ways:
Pass by Value
Pass by Reference
Pass by Constant Reference
1. Passing Object by Value
When an object is passed by value, a copy of the original object is created and passed to the function. Any changes made to the copied object do not affect the original object.
जब किसी object को Pass by Value किया जाता है, तो original object की एक copy बनाकर function में भेजी जाती है। Function में copy किए गए object में किए गए बदलाव original object को प्रभावित नहीं करते।
Example:
#include <iostream>using namespace std;class Number {public:int value;Number(int v) {value = v;}void display() {cout << "Value = " << value << endl;}};void change(Number n) {n.value = 100;}int main() {Number obj(50);cout << "Before function call: ";obj.display();change(obj);cout << "After function call: ";obj.display();return 0;}
Output:
Before function call: Value = 50After function call: Value = 50
Explanation:
The object
objis passed to thechange()function.A copy of
objis received by the parametern.The value of
n.valueis changed to 100.The original object
objremains unchanged.
व्याख्या:
Object
objकोchange()function में pass किया गया है।Function का parameter
n, objectobjकी copy प्राप्त करता है।n.valueकी value 100 कर दी जाती है।Original object
objकी value 50 ही रहती है।
2. Passing Object by Reference
When an object is passed by reference, the function receives a reference to the original object instead of creating a separate copy. Therefore, changes made inside the function affect the original object.
जब किसी object को Pass by Reference किया जाता है, तो function को original object का reference मिलता है, अलग copy नहीं बनती। इसलिए function के अंदर किए गए changes original object को भी प्रभावित करते हैं।
Example:
#include <iostream>using namespace std;class Number {public:int value;Number(int v) {value = v;}void display() {cout << "Value = " << value << endl;}};void change(Number &n) {n.value = 100;}int main() {Number obj(50);cout << "Before function call: ";obj.display();change(obj);cout << "After function call: ";obj.display();return 0;}
Output:
Before function call: Value = 50After function call: Value = 100
Explanation:
The function parameter
Number &nis a reference to the original object.No separate object copy is created for the parameter.
Changing
n.valuealso changesobj.value.
व्याख्या:
Function का parameter
Number &n, original object का reference है।Parameter के लिए अलग object copy नहीं बनती।
n.valueबदलने परobj.valueभी बदल जाता है।
3. Passing Object by Constant Reference
An object can also be passed by constant reference using the const keyword. It avoids copying the object while preventing the function from modifying it through that reference.
Object को const keyword की सहायता से Constant Reference के रूप में भी pass किया जा सकता है। इससे object की copy नहीं बनती और function उस reference के माध्यम से object में बदलाव नहीं कर सकता।
Example:
#include <iostream>using namespace std;class Student {public:int rollNo;float marks;Student(int r, float m) {rollNo = r;marks = m;}};void display(const Student &s) {cout << "Roll Number: " << s.rollNo << endl;cout << "Marks: " << s.marks << endl;}int main() {Student s1(101, 85.5);display(s1);return 0;}
Output:
Roll Number: 101Marks: 85.5
Explanation:
The function receives the object through a constant reference.
It can read the object's data members.
It cannot modify the object through this reference.
This approach is efficient for large objects because it avoids copying.
व्याख्या:
Function को object का constant reference मिलता है।
Function object के data members को read कर सकता है।
इस reference के माध्यम से object में बदलाव नहीं कर सकता।
बड़े objects के लिए यह तरीका उपयोगी है क्योंकि इसमें object की copy नहीं बनती।
Difference Between Passing Object by Value and Reference
| Pass by Value | Pass by Reference |
|---|---|
| A copy of the object is passed. | A reference to the original object is passed. |
| Changes do not affect the original object. | Changes affect the original object. |
| Copying may require additional time and memory. | Avoids creating a separate object copy. |
Syntax: void show(Student s) | Syntax: void show(Student &s) |
Advantages of Passing Objects as Function Arguments
- Code Reusability: Functions can operate on objects of a class without repeating the same code.Code Reusability: एक ही function का उपयोग class के objects पर बार-बार किया जा सकता है।
- Data Processing: Functions can process the data stored in objects.Data Processing: Functions objects में stored data को process कर सकते हैं।
- Object Modification: Passing by reference allows functions to modify original objects.Object Modification: Reference द्वारा object pass करने पर function original object में बदलाव कर सकता है।
- Efficiency: Passing by constant reference avoids unnecessary copying, especially for large objects.Efficiency: Constant reference से बड़े objects की अनावश्यक copying से बचा जा सकता है।
In C++, objects can be passed to functions by value, by reference or by constant reference. Pass by value protects the original object from direct changes made to the parameter, while pass by reference allows modification of the original object. Constant reference is useful when a function needs to read an object without modifying it.
C++ में objects को function में value, reference या constant reference के रूप में pass किया जा सकता है। Pass by Value में original object में बदलाव नहीं होता, जबकि Pass by Reference में original object को modify किया जा सकता है। Constant Reference तब उपयोगी होता है जब function को object का data पढ़ना हो लेकिन उसमें बदलाव नहीं करना हो।
Comments
Post a Comment