In C++ we can pass class’s objects as arguments and also return them from a function the same way we pass and return other variables. No special keyword or header file is required to do so.
Passing an Object as argument
To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables.
Example: In this Example there is a class which has an integer variable ‘a’ and a function ‘add’ which takes an object as argument. The function is called by one object and takes another as an argument. Inside the function, the integer value of the argument object is added to that on which the ‘add’ function is called. In this method, we can pass objects as an argument and alter them.
Initial Values Value of object 1: 50 & object 2: 100 New values Value of object 1: 50 & object 2: 150
Returning Object as argument
object = return object_name;
Example: In the above example we can see that the add function does not return any value since its return-type is void. In the following program the add function returns an object of type ‘Example'(i.e., class name) whose value is stored in E3.
In this example, we can see both the things that are how we can pass the objects as well as return them. When the object E3 calls the add function it passes the other two objects namely E1 & E2 as arguments. Inside the function, another object is declared which calculates the sum of all the three variables and returns it to E3.
This code and the above code is almost the same, the only difference is that this time the add function returns an object whose value is stored in another object of the same class ‘Example’ E3. Here the value of E1 is displayed by object1, the value of E2 by object2 and value of E3 by object3.
Initial Values Value of object 1: 50, object 2: 100, object 3: 0 New values Value of object 1: 50, object 2: 100, object 3: 200
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Passing by pointer Vs Passing by Reference in C++
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
- Declare a C/C++ function returning pointer to array of integer pointers
- Parameter Passing Techniques in C/C++
- Passing vector to a function in C++
- Passing a vector to constructor in C++
- Passing Reference to a Pointer in C++
- How to delete an element from the Set by passing its value in C++
- Equation of straight line passing through a given point which bisects it into two equal line segments
- C++ Classes and Objects
- Introduction to Complex Objects and Composition
- How to restrict dynamic allocation of objects in C++?
- How to make a C++ class whose objects can only be dynamically allocated?
- When are static objects destroyed?
- Comparing String objects using Relational Operators in C++
- Read/Write Class Objects from/to File in C++
- Count the number of objects using Static member function
- How to initialize Array of objects with parameterized constructors in C++
- Life cycle of Objects in C++ with Example
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : aniket173000