
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass Objects to Functions in C++
The C++ functions can receive objects in multiple ways, depending upon how you want the function to interact with the object. You can either define functions that can modify the original objects or define functions that will make a copy of original object and modify the copy without affecting the original object. In this article, we will explain all the ways to pass an object a C++ function.
Pass Objects to Function in C++
Here is the list of all the ways to pass an object to a function in c++ program, which we will be discussing in this article with stepwise explanation and complete example codes.
Pass By Value Method to Pass Objects
In pass by value method, a copy of the object is made and passed into the function such that any changes made to the object inside the function will not affect the original object. This method is recommended when you don't need to modify original object while running the function. The copying is an expensive operation, so use this method wisely.
Syntax
Following is syntax for pass by value method.
void display(Person P){ cout << P.name; }
Example
In the code below, we created a simple class Person, which will display name of a person and created an object p1 of it with name of person as Jhon. Then we used changeName function to change name of person to Alice and displayed name of p1 object. The name of p1 object will remain as "Jhon" because we used pass by value method.
#include <iostream> using namespace std; class Person { public: string name; Person(string n) { name = n; } void display() { cout << "Name: " << name << endl; } }; // Function accepting object by value void changeName(Person p) { p.name = "Alice"; // This change won't reflect outside } int main() { Person p1("John"); changeName(p1); p1.display(); // Output: Name: John return 0; }
The output of the above code will be:
Name: Jhon
Pass By Reference Method to Pass Objects
In the pass by reference method, the reference of the object is passed into the function instead of a copy. So, any changes made to the object inside the function will directly reflect on the original object. This method is useful when you want the function to modify the original object, and also want to avoid extra memory used in copying.
Syntax
Following is syntax for pass by reference method.
void display(Person &P){ cout << P.name; }
Example
In the code below, we again use Person class and create object p1 with name Jhon. Then we pass it to changeName function by reference. The function will change the name to Alice and the change will reflect in original object.
#include <iostream> using namespace std; class Person { public: string name; Person(string n) { name = n; } void display() { cout << "Name: " << name << endl; } }; // Function accepting object by reference void changeName(Person &p) { p.name = "Alice"; // This change affects the original object } int main() { Person p1("John"); changeName(p1); p1.display(); // Output: Name: Alice return 0; }
The output of the above code will be:
Name: Alice
Pass By Pointer Method to Pass Objects
In pass by pointer method, a pointer to the object is passed to the function. The function can then use pointer syntax to modify the object. This method is useful when working with dynamic memory or when null pointers may be passed.
Syntax
Following is syntax for pass by pointer method.
void display(Person* P){ cout << P->name; }
Example
In this example, we pass the address of object p1 to the changeName function using pointer. The function will change the name using pointer syntax and the original object will be updated.
#include <iostream> using namespace std; class Person { public: string name; Person(string n) { name = n; } void display() { cout << "Name: " << name << endl; } }; // Function accepting object by pointer void changeName(Person* p) { p->name = "Alice"; // Using pointer syntax to modify } int main() { Person p1("John"); changeName(&p1); p1.display(); // Output: Name: Alice return 0; }
The output of the above code will be:
Name: Alice
Summary
In C++, you can pass objects to functions in three different ways: by value, by reference, and by pointer. The method you choose depends on whether you want the function to modify the original object or not, and whether you want to avoid copying large objects to save memory.
Method | Syntax | Changes Original? | Memory Usage |
---|---|---|---|
Pass by Value | void func(Class obj) |
No | High |
Pass by Reference | void func(Class &obj) |
Yes | Low |
Pass by Pointer | void func(Class* obj) |
Yes | Low |