pgrm_011.cpp
Problem Statement
pgrm_011.cpp
call by value, call by address, call by reference
Source Code
cpp
#include<iostream>
using namespace std;
class Swap {
private:
int a, b;
public:
void getData() {
cout << "Enter two numbers: ";
cin >> a >> b;
}
// call by value
void swapValue(int x, int y) {
int temp = x;
x = y;
y = temp;
cout << "Inside swapValue(): " << x << " " << y << endl;
}
// call by address
void swapAddress(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
cout << "Inside swapAddress(): " << *x << " " << *y << endl;
}
// call by reference
void swapReference(int &x, int &y) {
int temp = x;
x = y;
y = temp;
cout << "Inside swapValue(): " << x << " " << y << endl;
}
void display() {
cout << "Current values: " << a << " " << b << endl;
}
void test() {
cout << "\nOriginal values: " << endl;
display();
cout << "\n== Call by Value ==" << endl;
swapValue(a, b);
cout << "After swapValue(): " << endl;
display();
cout << "\n== Call by Address ==" << endl;
swapAddress(&a, &b);
cout << "After swapAddress(): " << endl;
display();
cout << "\n== Call by Reference ==" << endl;
swapReference(a, b);
cout << "After swapReference(): " << endl;
display();
}
~Swap() {}
};
int main() {
Swap obj;
obj.getData();
obj.test();
return 0;
}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
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
● Author - Amit Dutta · Updated - 10 Jul 2026