Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year

Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year


Object as Function Argument & Returning Object from Function in C++

Object as Function Argument C++ का एक महत्वपूर्ण Object Oriented Programming Concept है जिसमें किसी Class के Object को Function में Argument के रूप में Pass किया जाता है। इससे Function Object के Data Members तथा Member Functions का उपयोग करके आवश्यक Processing कर सकता है।

इसी प्रकार C++ में Function किसी Object को Return भी कर सकता है। इसे Returning Object from Function कहा जाता है। यह तकनीक Object Oriented Design, Data Processing तथा Complex Applications में व्यापक रूप से उपयोग की जाती है।

RGPV B.Tech First Year के Basic Computer Engineering Subject में Object as Function Argument तथा Returning Object from Function अत्यंत महत्वपूर्ण Topics हैं। University Examination, Practical तथा Viva में Object Passing, Returning Objects तथा इनके Programs से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।

Object



Function Call



Processing



Return Object

Introduction to Object as Function Argument

सामान्यतः Functions में Integer, Float तथा Character जैसे Primitive Data Types Pass किए जाते हैं। लेकिन Object Oriented Programming में पूरे Object को भी Function में Argument के रूप में भेजा जा सकता है।

जब Object Function में भेजा जाता है, तब Function उस Object के Data Members का उपयोग करके आवश्यक Operations कर सकता है। इससे Code Reusability तथा Modularity बढ़ती है।


What is Object as Function Argument?

Object as Function Argument वह प्रक्रिया है जिसमें किसी Class के Object को Function के Parameter के रूप में Pass किया जाता है। Function उस Object की सहायता से Data Access करता है तथा आवश्यक Processing करता है।

Student s; display(s);

ऊपर दिए गए उदाहरण में s Object को display() Function में Argument के रूप में Pass किया गया है।


Need of Passing Objects to Functions

Object को Function में Pass करने से Program अधिक Modular तथा Reusable बनता है। बड़े Software Projects में विभिन्न Functions एक ही Object पर अलग-अलग Operations कर सकते हैं।

  • Object Data पर Processing करने के लिए।
  • Code Reusability बढ़ाने के लिए।
  • Program को Modular बनाने के लिए।
  • Complex Calculations को अलग-अलग Functions में विभाजित करने के लिए।
  • Large Applications में Object Sharing के लिए।
  • Object Oriented Design को Implement करने के लिए।
  • Professional Software Development में Maintainable Code लिखने के लिए।

Advantages of Passing Objects to Functions

Advantage Description
Code Reusability एक ही Function कई Objects पर कार्य कर सकता है।
Modularity Program छोटे-छोटे Functions में विभाजित किया जा सकता है।
Easy Maintenance Code को Modify तथा Debug करना आसान हो जाता है।
Better Organization Object आधारित Programming अधिक व्यवस्थित बनती है।
Professional Design Object Oriented Software Development को Support करता है।

Methods of Passing Object to Function

C++ में किसी Object को Function में तीन प्रमुख तरीकों से Pass किया जा सकता है। प्रत्येक Method का अपना उपयोग, लाभ तथा सीमाएँ होती हैं। आवश्यकता के अनुसार Programmer उचित Method का चयन करता है।

  • Passing Object by Value
  • Passing Object by Reference
  • Passing Object by Pointer

Passing Object by Value

जब Object को By Value Function में Pass किया जाता है, तब पूरे Object की एक Copy Function को प्राप्त होती है। Function द्वारा किए गए परिवर्तन Original Object को प्रभावित नहीं करते।

void display(Student s) { s.show(); }

इस Method में Object Copy होने के कारण अतिरिक्त Memory तथा Processing Time लगता है। इसलिए बड़े Objects के लिए यह Method कम उपयुक्त माना जाता है।


Passing Object by Reference

जब Object को Reference द्वारा Pass किया जाता है, तब Object की Copy नहीं बनती। Function सीधे Original Object पर कार्य करता है, जिससे Memory की बचत होती है तथा Program अधिक Efficient बनता है।

void display(Student &s) { s.show(); }

यह Method बड़े Objects (Large Objects) के लिए सबसे अधिक उपयोगी माना जाता है क्योंकि इसमें Object Copy नहीं होता।


Passing Object by Pointer

इस Method में Function को Object का Address भेजा जाता है। Function Pointer के माध्यम से Object के Members को Arrow Operator (->) द्वारा Access करता है।

void display(Student *ptr) { ptr->show(); }

Pointer द्वारा Object Pass करने से Memory की बचत होती है तथा Dynamic Objects को आसानी से Handle किया जा सकता है।


Comparison of Object Passing Methods

By Value By Reference By Pointer
Object Copy होता है। Object Copy नहीं होता। केवल Object का Address Pass होता है।
Memory अधिक उपयोग होती है। Memory की बचत होती है। Memory सबसे कम उपयोग होती है।
Original Object सुरक्षित रहता है। Original Object Modify हो सकता है। Original Object Modify हो सकता है।
छोटे Objects के लिए उपयुक्त। Large Objects के लिए उपयुक्त। Dynamic Objects के लिए उपयुक्त।

Example of Passing Object by Value

#include<iostream> using namespace std; class Student { public: int marks; }; void display(Student s) { cout << s.marks; } int main() { Student s; s.marks = 95; display(s); return 0; }

Output

95

इस उदाहरण में Student Object को Function में By Value Pass किया गया है। Function को Object की Copy प्राप्त होती है, इसलिए Function के भीतर किए गए परिवर्तन Original Object को प्रभावित नहीं करेंगे।


Returning Object from Function

C++ में Function केवल Primitive Data Types ही नहीं बल्कि किसी Class के Object को भी Return कर सकता है। इस तकनीक को Returning Object from Function कहा जाता है। इसका उपयोग Object Oriented Programming में Result Object तैयार करके Caller Function को वापस भेजने के लिए किया जाता है।

यह Concept विशेष रूप से Mathematical Operations, Business Applications, Operator Overloading तथा Data Processing में अत्यंत उपयोगी होता है।


Returning Object by Value

जब Function किसी Object की Copy Return करता है, तब इसे Returning Object by Value कहा जाता है। Returned Object Original Object से अलग होता है।

Student createObject() { Student s; return s; }

इस Method में Function द्वारा Object की Copy Return होती है। छोटे Objects के लिए यह Method उपयुक्त माना जाता है।


Returning Object by Reference

यदि Function किसी Object का Reference Return करता है, तो Object की Copy नहीं बनती। इससे Memory की बचत होती है तथा Program की Performance बेहतर होती है।

Student& getObject() { return obj; }

Reference Return करते समय यह सुनिश्चित करना आवश्यक है कि Returned Object Function समाप्त होने के बाद भी Memory में उपलब्ध रहे।


Returning Object Pointer

Function किसी Object का Address भी Return कर सकता है। Returned Address को Object Pointer में Store किया जाता है तथा Pointer द्वारा Object के Members को Access किया जाता है।

Student* createStudent() { Student *ptr = new Student; return ptr; }

यदि Function Dynamic Object Return करता है, तो Program समाप्त होने से पहले उस Object की Memory को delete द्वारा Release करना आवश्यक होता है।


Example of Returning Object

#include<iostream> using namespace std; class Number { public: int value; Number add(Number n) { Number temp; temp.value = value + n.value; return temp; } }; int main() { Number n1, n2, result; n1.value = 20; n2.value = 30; result = n1.add(n2); cout << result.value; return 0; }

Output

50

ऊपर दिए गए Program में add() Function दो Objects की Values जोड़कर एक नया Object Return करता है। Returned Object को result Variable में Store किया गया है।


Working of Returning Object

Object Passed



Function Processing



New Object Created



Return Object



Store Result

जब Function Object Return करता है, तब Processing के बाद नया Object Caller Function को वापस भेजा जाता है। Caller उस Object को किसी अन्य Object Variable में Store करके आगे उपयोग कर सकता है।


Advantages of Returning Object from Function

Function से Object Return करने की सुविधा Object Oriented Programming का एक महत्वपूर्ण Feature है। इसकी सहायता से Functions Complex Data को Object के रूप में Return कर सकते हैं, जिससे Program अधिक Modular तथा Reusable बनता है।

  • Complete Object को एक साथ Return किया जा सकता है।
  • Object Oriented Programming को अधिक प्रभावी बनाता है।
  • Code Reusability बढ़ती है।
  • Program अधिक Modular बनता है।
  • Complex Calculations का Result Object के रूप में Return किया जा सकता है।
  • Operator Overloading में अत्यंत उपयोगी है।
  • Professional Software Development में व्यापक उपयोग होता है।

Limitations of Passing and Returning Objects

यदि Objects को उचित तरीके से Pass अथवा Return नहीं किया जाए, तो Program की Performance प्रभावित हो सकती है। इसलिए सही Method का चयन करना आवश्यक होता है।

  • Passing Object by Value में पूरे Object की Copy बनती है।
  • Large Objects को By Value Pass करने से Performance कम हो सकती है।
  • Reference Return करते समय Invalid Reference Return नहीं करना चाहिए।
  • Pointer Return करने पर Memory Management की जिम्मेदारी Programmer की होती है।
  • Dynamic Objects को Delete न करने पर Memory Leak हो सकता है।
  • गलत Pointer Handling से Runtime Errors उत्पन्न हो सकते हैं।

Object Passing vs Primitive Data Passing

Object Passing Primitive Data Passing
पूरे Object को Function में भेजा जाता है। केवल Primitive Data Type भेजा जाता है।
एक साथ Multiple Data Members उपलब्ध होते हैं। केवल एक Variable की Value उपलब्ध होती है।
Object Oriented Programming के लिए उपयुक्त। सामान्य Functions के लिए उपयुक्त।
Memory अधिक उपयोग हो सकती है (By Value)। Memory कम उपयोग होती है।
Complex Data को Handle कर सकता है। Simple Values के लिए उपयुक्त।

Applications of Object Passing and Returning

Application Purpose
Banking System Account Objects को Functions में Pass करना।
Student Management Student Records पर Processing करना।
Inventory System Product Objects को Return करना।
Graphics Programming Point एवं Shape Objects Return करना।
Game Development Player एवं Enemy Objects Handle करना।
Database Applications Record Objects को Retrieve एवं Return करना।

Best Practices

  • Large Objects को By Reference अथवा By Pointer Pass करें।
  • छोटे Objects के लिए By Value का उपयोग किया जा सकता है।
  • Reference Return करते समय Local Object Return न करें।
  • Dynamic Object Return करने पर Memory को समय पर delete करें।
  • Unnecessary Object Copying से बचें।
  • Readable एवं Modular Code लिखें।
  • Memory Management का विशेष ध्यान रखें।

Real Life Example

मान लीजिए किसी Employee Management System में Employee Object को Salary Calculate करने वाले Function में Pass किया जाता है। Function आवश्यक Processing करके Updated Employee Object Return करता है। इससे पूरा Employee Data एक ही Object में सुरक्षित रहता है तथा Program अधिक व्यवस्थित एवं Maintainable बनता है।


Applications of Object as Function Argument and Returning Object

Object Passing तथा Returning Object का उपयोग आधुनिक Object Oriented Programming में व्यापक रूप से किया जाता है। Enterprise Applications, Banking Software, ERP Systems तथा Game Development में Objects को Functions के बीच Pass एवं Return करके Code को अधिक Modular तथा Reusable बनाया जाता है।

Application Purpose
Banking System Account Objects को Transaction Functions में Pass करना।
Student Management System Student Records को Process करके Updated Object Return करना।
Payroll System Employee Objects से Salary Calculate करना।
Inventory Management Product Objects को Update एवं Return करना।
Game Development Player तथा Enemy Objects पर Operations करना।
Graphics Programming Shape एवं Point Objects Return करना।
Database Applications Database Records को Object के रूप में Retrieve एवं Return करना।

Advantages of Passing and Returning Objects

  • Code Reusability में वृद्धि होती है।
  • Program अधिक Modular एवं Organized बनता है।
  • Object Oriented Design को प्रभावी रूप से लागू किया जा सकता है।
  • Large Projects में Maintenance आसान हो जाता है।
  • Complex Data को एक Object के रूप में आसानी से Handle किया जा सकता है।
  • Function Reusability बढ़ती है।
  • Professional Software Development के लिए उपयुक्त Technique है।

Limitations

  • Passing Object by Value में पूरे Object की Copy बनती है।
  • Large Objects की Copy बनने से Performance कम हो सकती है।
  • Reference Return करते समय Local Object Return करना गलत Practice है।
  • Pointer Return करने पर Memory Management Programmer को करना पड़ता है।
  • Dynamic Objects को Delete न करने पर Memory Leak हो सकता है।
  • Improper Object Handling से Runtime Errors उत्पन्न हो सकते हैं।
  • Pointer एवं Reference का गलत उपयोग Program को Unstable बना सकता है।

Best Practices

  • Large Objects को Reference अथवा Pointer द्वारा Pass करें।
  • यदि Object में परिवर्तन नहीं करना हो, तो const Reference का उपयोग करें।
  • Reference Return करते समय केवल Valid Objects Return करें।
  • Dynamic Objects Return करने पर Memory को समय पर delete करें।
  • Unnecessary Object Copying से बचें।
  • Functions को Single Responsibility Principle के अनुसार Design करें।
  • Readable तथा Maintainable Object Oriented Code लिखें।

Real Life Example

मान लीजिए किसी Online Shopping System में Order Object को calculateBill() Function में Pass किया जाता है। Function Product Price, Tax तथा Discount की गणना करके Updated Order Object Return करता है। इससे Order की सभी जानकारी एक ही Object में सुरक्षित रहती है तथा पूरा Program अधिक Modular एवं Reusable बनता है।


Working of Object Passing and Returning

Object Created



Object Passed to Function



Function Processing



Updated Object Created



Object Returned



Result Used by Caller

Summary

Object as Function Argument तथा Returning Object from Function C++ Object Oriented Programming के महत्वपूर्ण Concepts हैं। Object को Function में By Value, By Reference अथवा By Pointer Pass किया जा सकता है। इसी प्रकार Function किसी Object को By Value, By Reference अथवा Object Pointer के रूप में Return कर सकता है। Large Objects के लिए Reference अथवा Pointer का उपयोग अधिक Efficient माना जाता है क्योंकि इसमें Object की Copy नहीं बनती। यह Concept Code Reusability, Modularity तथा Efficient Memory Management को बढ़ावा देता है।

Concept Description
Object as Function Argument Object को Function के Parameter के रूप में Pass करना।
Passing by Value Object की Copy Function को भेजी जाती है।
Passing by Reference Original Object का Reference भेजा जाता है।
Passing by Pointer Object का Address Function को भेजा जाता है।
Returning Object Function Object अथवा उसका Reference/Pointer Return कर सकता है।
Applications Banking, Student Management, Database, ERP एवं Game Development।
Major Benefit Reusable, Modular तथा Efficient Object Oriented Programming।
Best Method Large Objects के लिए Passing by Reference या Pointer।

Important Viva Questions

  1. Object as Function Argument क्या है?
  2. Object को Function में कितने तरीकों से Pass किया जा सकता है?
  3. Passing by Value क्या है?
  4. Passing by Reference क्या है?
  5. Passing by Pointer क्या है?
  6. Function Object कैसे Return करता है?
  7. Returning Object by Reference का क्या अर्थ है?
  8. Returning Object Pointer क्या होता है?
  9. Large Objects के लिए कौन-सी Passing Method बेहतर है और क्यों?
  10. Object Passing के मुख्य Applications क्या हैं?

University Examination Important Questions

  1. Object as Function Argument क्या है? उदाहरण सहित समझाइए।
  2. Passing Object by Value, Reference तथा Pointer में अंतर लिखिए।
  3. Returning Object from Function को उदाहरण सहित समझाइए।
  4. Passing by Reference बड़े Objects के लिए अधिक उपयुक्त क्यों है?
  5. Object Passing तथा Primitive Data Passing में अंतर लिखिए।
  6. Returning Object by Reference के लाभ एवं सावधानियाँ लिखिए।
  7. Object Pointer Return करने का Program लिखिए।
  8. Object Passing के Advantages तथा Limitations लिखिए।
  9. Professional Software Development में Object Passing के Applications लिखिए।
  10. Passing Object by Pointer का Program लिखिए।

Exam Tips

  • हमेशा याद रखें कि Passing by Value में Object की Copy बनती है।
  • Passing by Reference तथा Passing by Pointer में Object Copy नहीं होता।
  • Large Objects के लिए Reference अथवा Pointer Passing सबसे उपयुक्त होती है।
  • Reference Return करते समय Local Object Return न करें।
  • Dynamic Object Return करने पर Memory को delete करना न भूलें।
  • RGPV Practical में Object Passing तथा Returning Object के Programs अक्सर पूछे जाते हैं।
  • Passing by Value, Reference तथा Pointer का Comparison परीक्षा की दृष्टि से अत्यंत महत्वपूर्ण है।

Conclusion

Object as Function Argument तथा Returning Object from Function C++ Object Oriented Programming के अत्यंत महत्वपूर्ण Concepts हैं। इनकी सहायता से Objects को विभिन्न Functions के बीच Efficient तरीके से Share किया जा सकता है तथा आवश्यक Processing के बाद Updated Objects Return किए जा सकते हैं। Passing by Reference एवं Passing by Pointer Memory की बचत करते हैं और Large Applications में बेहतर Performance प्रदान करते हैं। Banking Systems, ERP Software, Inventory Management, Graphics Programming, Database Management तथा Game Development जैसे क्षेत्रों में इन Concepts का व्यापक उपयोग किया जाता है। RGPV B.Tech First Year के विद्यार्थियों के लिए यह Topic Theory, Practical एवं Viva तीनों दृष्टिकोण से अत्यंत महत्वपूर्ण है।

Related Articles

Arrays of Objects in C++ Notes | Object Array, Declaration, Initialization & Examples | Basic Computer Engineering | RGPV BTech First Year

Arrays ...

Read More →

Pointers to Objects in C++ Notes | Object Pointer, Accessing Members using Pointer & Examples | Basic Computer Engineering | RGPV BTech First Year

Pointer...

Read More →

Dynamic Memory Allocation in C++ Notes | new & delete Operators, Heap Memory, Dynamic Objects & Examples | Basic Computer Engineering | RGPV BTech First Year

Dynamic...

Read More →

this Pointer in C++ Notes | this Keyword, Uses, Advantages & Examples | Basic Computer Engineering | RGPV BTech First Year

this Po...

Read More →

Static Data Members & Static Member Functions in C++ Notes | Static Keyword, Static Variables, Static Functions & Examples | Basic Computer Engineering | RGPV BTech First Year

Static ...

Read More →