-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcycle1.2_q_3.cpp
More file actions
69 lines (64 loc) · 1.31 KB
/
cycle1.2_q_3.cpp
File metadata and controls
69 lines (64 loc) · 1.31 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
3. Write a function called swap() that interchanges two int values belonging
to an object, passed as parameter to it by the calling program. Write a C++
program to demonstrate call by value, call by reference and call by
address.
*/
#include <iostream>
#include <cctype>
using namespace std;
class swaping{
int x,y;
public:
void get_data(){
cout<<"Enter the X and Y \n";
cin >> x >> y;
}
void swap_value(swaping s);
void swap_ref(swaping &s);
void swap_add(swaping *s);
void output(){
cout <<"x ="<<x<<"\n"<<"y= "<<y<<endl;
}
};
void swaping :: swap_value(swaping s){
int temp;
temp=s.x;
s.x=s.y;
s.y=temp;
}
void swaping::swap_ref(swaping &s){
int temp;
temp=s.x;
s.x=s.y;
s.y=temp;
}
void swaping::swap_add(swaping *s){
int temp;
temp=s->x;
s->x=s->y;
s->y=temp;
}
int main (){
char fun;
swaping s1,s2;
s1.get_data();
while(fun!='E'){
cout<<"Enter \n V for Call_by_value \n R for Call_by_Reference\n A for Call_by_Address \n E for exit"<<endl;
cin>>fun;
fun=toupper(fun);
cout<<"Before Swapping \n" ;
s1.output();
switch(fun){
case 'V':s2.swap_value(s1);
break;
case 'R':s2.swap_ref(s1);
break;
case 'A':s2.swap_add(&s1);
break;
default:cout<<"\n INVALID CHOICE \n";
}
cout<<"After Swapping \n" ;
s1.output();
}
}