Returning Objects from Functions in C++ - Definition, Syntax and Examples
Introduction to Returning Objects from Functions in C++
In C++, a function can return an object of a class just like it returns values of built-in data types such as int, float and double. This technique is known as Returning Objects from Functions. It is useful when a function creates a new object or performs an operation on existing objects and returns the resulting object to the calling function.
C++ में कोई function int, float और double जैसे built-in data types की तरह किसी class का object भी return कर सकता है। इस प्रक्रिया को Returning Objects from Functions कहते हैं। इसका उपयोग तब किया जाता है जब कोई function नया object बनाता है या existing objects पर operation करके resulting object को calling function में वापस भेजता है।
Syntax of Returning an Object from a Function
The return type of the function must be the class name whose object is being returned.
किसी object को return करने वाले function का return type उस class का नाम होना चाहिए जिसका object return किया जा रहा है।
Syntax:
ClassName functionName() {ClassName obj;// Perform operationsreturn obj;}
Here, ClassName is the return type, and obj is the object returned by the function.
यहाँ ClassName function का return type है और obj वह object है जिसे function return करता है।
Example 1: Simple Program to Return an Object from a Function
The following program demonstrates how a function creates and returns an object of a class.
निम्न program दिखाता है कि कोई function class का object कैसे बनाता है और उसे return करता है।
#include <iostream>using namespace std;class Number {public:int value;void setValue(int v) {value = v;}void display() {cout << "Value = " << value << endl;}};Number getNumber() {Number n;n.setValue(100);return n;}int main() {Number obj;obj = getNumber();obj.display();return 0;}
Output:
Value = 100Explanation:
The class
Numbercontains a data membervalue.The function
getNumber()hasNumberas its return type.It creates an object
nand assigns 100 to its data member.The statement
return n;returns the object.In
main(), the returned object is assigned toobj.
व्याख्या:
Numberclass मेंvalueनाम का data member है।getNumber()function का return typeNumberहै।यह function
nobject बनाकर उसके data member में 100 store करता है।return n;statement object को वापस भेजता है।main()में returned object कोobjमें assign किया जाता है।
Example 2: Returning an Object After Adding Two Objects
A function can receive objects as arguments, perform an operation on them and return a new object containing the result.
कोई function objects को arguments के रूप में प्राप्त करके उन पर operation कर सकता है और result को एक नए object के रूप में return कर सकता है।
Program:
#include <iostream>using namespace std;class Number {public:int value;Number(int v = 0) {value = v;}Number add(Number n) {Number temp;temp.value = value + n.value;return temp;}void display() {cout << "Value = " << value << endl;}};int main() {Number n1(10);Number n2(20);Number result;result = n1.add(n2);result.display();return 0;}
Output:
Value = 30Explanation:
Two objects
n1andn2are created with values 10 and 20.The function
add()receivesn2as an argument.It adds the values of the calling object and the argument object.
A temporary object
tempstores the result.The function returns
temp, which is assigned toresult.
व्याख्या:
n1औरn2नाम के दो objects क्रमशः 10 और 20 values के साथ बनाए गए हैं।add()function मेंn2को argument के रूप में pass किया जाता है।Function calling object और argument object की values को जोड़ता है।
Result को
tempनाम के object में store किया जाता है।Function
tempobject को return करता है और उसेresultमें assign किया जाता है।
Example 3: Returning an Object Using a Separate Function
A separate function can also perform an operation on two objects and return the result.
एक अलग function भी दो objects पर operation करके result को object के रूप में return कर सकता है।
#include <iostream>using namespace std;class Rectangle {public:int length;int breadth;Rectangle(int l = 0, int b = 0) {length = l;breadth = b;}int area() {return length * breadth;}};Rectangle createRectangle() {Rectangle r(10, 5);return r;}int main() {Rectangle r1;r1 = createRectangle();cout << "Length = " << r1.length << endl;cout << "Breadth = " << r1.breadth << endl;cout << "Area = " << r1.area() << endl;return 0;}
Output:
Length = 10Breadth = 5Area = 50
Explanation:
The function createRectangle() creates a Rectangle object and returns it to main(). The returned object is assigned to r1, whose data members can then be accessed.
createRectangle() function एक Rectangle object बनाता है और उसे main() में return करता है। Returned object को r1 में assign किया जाता है, जिसके बाद उसके data members को access किया जा सकता है।
Advantages of Returning Objects from Functions
- Code Reusability: Functions can create and return objects that can be used in different parts of a program.Code Reusability: Functions objects बनाकर return कर सकते हैं, जिनका उपयोग program के अलग-अलग भागों में किया जा सकता है।
- Simplifies Operations: Complex operations can be performed inside a function and the result can be returned as an object.Operations को सरल बनाना: Complex operations को function के अंदर perform करके result को object के रूप में return किया जा सकता है।
- Improves Modularity: Object creation and processing can be separated from the main program.Modularity: Object creation और processing को main program से अलग रखा जा सकता है।
- Supports Object-Oriented Programming: Returning objects makes it easier to work with class-based data and operations.Object-Oriented Programming: Objects return करने से class-based data और operations के साथ काम करना आसान होता है।
Difference Between Passing Objects and Returning Objects
| Passing Objects to Functions | Returning Objects from Functions |
|---|---|
| An object is supplied to a function as an argument. | An object is sent back from a function as its return value. |
| Used to provide data to a function. | Used to receive the result of a function as an object. |
Example: show(obj); | Example: obj = getObject(); |
Conclusion
Returning objects from functions is an important feature of C++ that allows functions to return complete objects instead of individual data values. It is useful for object creation, mathematical operations and processing class-based data.
C++ में functions से objects return करना एक महत्वपूर्ण सुविधा है। इसके द्वारा function केवल individual data values ही नहीं, बल्कि complete objects भी return कर सकता है। इसका उपयोग object creation, mathematical operations और class-based data processing में किया जाता है।
Modern C++ Note: Returning a local object by value is safe. Modern C++ compilers commonly apply Return Value Optimization (RVO), and in many cases the object is constructed directly in the destination without an extra copy.
Comments
Post a Comment