-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassbyreference.cpp
More file actions
45 lines (36 loc) · 879 Bytes
/
passbyreference.cpp
File metadata and controls
45 lines (36 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
class Family{
public :
int age ;
string name ;
char gender ;
};
// This function modifies the object passed by reference
// so the changes will reflect in the original object
//because we are passing by reference by using '&'
// this means no copy is made and the original object is modified
void change(Family &d){
d.age = 20;
d.name = "khushi";
d.gender = 'F';
}
void print(Family d){
cout<<endl;
cout << "Name : " << d.name << endl;
cout << "Age : " << d.age << endl ;
cout <<"gender : " << d.gender << endl;
cout<<endl;
}
int main(){
Family d1 ;
d1.age = 22 ;
d1.name ="ayush";
d1.gender = 'M';
print(d1);
change(d1); // This will not change d1's values
print(d1); // d1 remains unchanged
return 0;
}