how to pass an object by reference in c++
C++
// 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;
}
Also in C++:
- Title
- how to turn int into string c++
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- c++ loop through int array
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- nth_element c++
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- how to open an input file in c++
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- range based for loop c++ with reference
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- qt graphics scene map cursor position
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- c++ comment
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- c++ vector constructors
- Category
- C++
- Title
- c++ program to find gcd of 3 numbers
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- file format not recognized treating as linker script c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- time conversion hackerrank solution in c++
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- array search c++
- Category
- C++
- Title
- worker class c++
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- accept the noun and the output of plural c++
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- recursive in c++
- Category
- C++
- Title
- c++ compare char array
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- delete memory c++
- Category
- C++
- Title
- c++ raw string
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- pointers in cpp
- Category
- C++
- Title
- c++ sort array of ints
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- c++ remove space from string
- Category
- C++
- Title
- binary exponentiation
- Category
- C++
- Title
- run cmd command c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- sort function in vector c++ stl
- Category
- C++
- Title
- c++ clamp
- Category
- C++
- Title
- how to cin multiple lines of strings c++
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++