• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Classes » c++ – Returning object from function
Next →
← Prev

c++ – Returning object from function

By Dinesh Thakur

A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function.

The syntax for defining a function that returns an object by value is

class_name function_name (parameter_list) {
 // body of the function
}

To understand the concept of returning an object by value from a function, consider this example.

Example: A program to demonstrate the concept of returning objects from a function

#include<iostream.h>
class weight {
  int kilogram;
  int gram;
  public:
    void getdata ();
    void putdata ();
    void sum_weight (weight,weight) ;
    weight sum_weight (weight) ;
} ;
void weight :: getdata() {
  cout<<"/nKilograms:";
  cin>>kilogram;
  cout<<"Grams:";
  cin>>gram;
}
 void weight :: putdata () {
   cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n";
 }
 weight weight :: sum_weight(weight w2) {
  weight temp;
  temp.gram = gram + w2.gram;
  temp.kilogram=temp.gram/1000;
  temp.gram=temp.gram%1000;
  temp.kilogram+=kilogram+w2.kilogram;
  return(temp);
}
int main () {
  weight w1,w2 ,w3;
  cout<<"Enter weight in kilograms and grams\n";
  cout<<"\n Enter weight #1" ;
  w3 = w1.sum_weight (w2);
  w1.getdata();
  cout<<" \n Enter weight #2" ;
  w2.getdata();
  w3.sum_weight(wl,w2);
  cout<<"/n Weight #1 = ";
  w1.putdata();
  cout<<"Weight #2 = ";
  w2.putdata();
  cout<<"Total Weight = ";
  w3.putdata();
  return 0;
}

As the return type of function is weight (an object of class weight), a temporary object temp is created within the function for holding return values. These values are accessed as temp kilogram and temp gram by the function. When the control returns to main (), the object temp is assigned to the object w3 in main( ).

Figure represents accessing of member variables within the function and returning a result from the temporary object.

Result return from temp object

In the case of returning an object by reference, no new object is created, rather a reference to the original object in the called function is returned to the calling function.

The syntax for defining a function that returns an object by reference is

class_name&amp; function_name (parameter_list) {
  //body of the function
}

However, a reference to the local object (object declared inside the function) cannot be returned from the function since a reference to local data points to data within the function. Hence, when the function terminates and local data is destroyed, this reference points to nothing.

weight& weight::sum_weight(weight& w2) {
  weight temp; //object local to function
  temp.gram=gram+w2.gram;
  temp.kilogram=temp.gram/1000;
  temp.gram=temp.gram%1000;
  temp.kilogram+=kilogram+w2.kilogram;
  return temp; //invalid
}

To understand this concept, consider the function sum_weight () that is modified as shown in this code segment..

In this code segment, an attempt has been made to return the reference to an object of type weight (that is temp). However, it is not possible as the object temp is local to the sum_weight()function and a reference to this object remains effective only within the function. Thus, returning the reference to temp object outside the function generates a compile-time error.

You’ll also like:

  1. Write A C++ Program for Returning Objects For Addition Of Distances.
  2. Difference between the REQUESTDISPATCHER Object and SERVLETCONTEXT Object
  3. Returning Objects from Methods in Java
  4. Returning an Array from a Method in Java
  5. A program to demonstrate the concept of functions returning values
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW