Mon Nov 25 2019
Swapping
C++ Programming1307 views
File Name: swapping.cpp
#include<iostream>
using namespace std;
class swapping {
int temp;
public:
/* Pointer as parameter of the Constructor */
swapping(int *a, int *b) {
/* Swapping values using reference of the variables */
temp = *a;
*a = *b;
*b = temp;
}
};
int main() {
int x, y;
cout << "Enter a value for X" << endl;
cin >> x;
cout << "Enter a value for Y" << endl;
cin >> y;
cout << "Before swapping" << endl;
cout << "X = "<< x << "\nY = " << y << endl;
/* Passing reference(memory address) of the variables */
swapping sp = swapping(&x, &y);
cout << "After swapping" << endl;
cout << "X = " << x << "\nY = " << y << endl;
return 0;
}
/* Output */
Enter a value for X
5
Enter a value for Y
6
Before swapping
X = 5
Y = 6
After swapping
X = 6
Y = 5
Reference:
Author:Geekboots