Passing and Returning Objects in C++ Last Updated : 17 Mar, 2021 Comments Improve Suggest changes Like Article Like Report 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.Syntax: function_name(object_name); 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. CPP // C++ program to show passing // of objects to a function #include <bits/stdc++.h> using namespace std; class Example { public: int a; // This function will take // an object as an argument void add(Example E) { a = a + E.a; } }; // Driver Code int main() { // Create objects Example E1, E2; // Values are initialized for both objects E1.a = 50; E2.a = 100; cout << "Initial Values \n"; cout << "Value of object 1: " << E1.a << "\n& object 2: " << E2.a << "\n\n"; // Passing object as an argument // to function add() E2.add(E1); // Changed values after passing // object as argument cout << "New values \n"; cout << "Value of object 1: " << E1.a << "\n& object 2: " << E2.a << "\n\n"; return 0; } OutputInitial Values Value of object 1: 50 & object 2: 100 New values Value of object 1: 50 & object 2: 150Returning Object as argument Syntax: 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. CPP // C++ program to show passing // of objects to a function #include <bits/stdc++.h> using namespace std; class Example { public: int a; // This function will take // object as arguments and // return object Example add(Example Ea, Example Eb) { Example Ec; Ec.a = Ea.a + Eb.a; // returning the object return Ec; } }; int main() { Example E1, E2, E3; // Values are initialized // for both objects E1.a = 50; E2.a = 100; E3.a = 0; cout << "Initial Values \n"; cout << "Value of object 1: " << E1.a << ", \nobject 2: " << E2.a << ", \nobject 3: " << E3.a << "\n"; // Passing object as an argument // to function add() E3 = E3.add(E1, E2); // Changed values after // passing object as an argument cout << "New values \n"; cout << "Value of object 1: " << E1.a << ", \nobject 2: " << E2.a << ", \nobject 3: " << E3.a << "\n"; return 0; } OutputInitial Values Value of object 1: 50, object 2: 100, object 3: 0 New values Value of object 1: 50, object 2: 100, object 3: 150 Comment More infoAdvertise with us Next Article Passing and Returning Objects in C++ T TusharSabhani Follow Improve Article Tags : C++ Programs C++ Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and 9 min read C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i 15+ min read C++ Polymorphism The word polymorphism means having many forms. A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So, the same person exhibits different behaviour in different situations. This is ca 5 min read Like