-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_swap_private_values.cpp
70 lines (57 loc) · 1.51 KB
/
03_swap_private_values.cpp
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
70
// // Using the concept of pointers, write a function that swaps the private data values of two objects of the same class type.
// // Header files
#include <iostream>
// // use namespace
using namespace std;
// // define class Integer
class Integer
{
protected:
// // instance member variable
int data;
public:
// // constructors
Integer() {}
Integer(int data) : data(data) {}
// // instance member function to set data
void setData(int data)
{
this->data = data;
}
// // instance member function to get data
int getData() const
{
return data;
}
// // instance member function to swap values of private data members of two objects
void swap(Integer *obj2)
{
// // Check if the pointer is valid (not nullptr)
if (obj2)
{
int temp = data;
data = obj2->data;
obj2->data = temp;
}
else
{
cout << "\n!!! Invalid Pointer provided for Swapping." << endl;
}
}
};
int main()
{
// // create instances of Integer
Integer i1(2), i2(6);
cout << "\n>>>>>>> Before Swapping <<<<<<<<";
cout << "\ni1.data => " << i1.getData();
cout << "\ni2.data => " << i2.getData();
// // swap value of private data members
i1.swap(&i2);
cout << "\n\n>>>>>>> After Swapping <<<<<<<<";
cout << "\ni1.data => " << i1.getData();
cout << "\ni2.data => " << i2.getData();
cout << endl; // Add new line
cin.ignore();
return 0;
}