swap.h :
#pragma once
class Swap
{
public:
Swap(void);
public:
~Swap(void);
public:
void SwapNumbers(int & number1, int & number2);
public:
void SwapNumbersViaPointer(int * pNumber1, int * pNumber2);
};
swap.cpp :
#include "Swap.h"
Swap::Swap(void)
{
}
Swap::~Swap(void)
{
}
void Swap::SwapNumbers(int & number1, int & number2)
{
int tmp = number2;
number2 = number1;
number1 = tmp;
}
void Swap::SwapNumbersViaPointer(int * pNumber1,int * pNumber2)
{
int tmp = *pNumber1;
*pNumber1 = *pNumber2;
*pNumber2 = tmp;
}
start.cpp :
#include <iostream>
#include "Swap.h"
using namespace std;
int main()
{
Swap swapper;
int number1;
int number2;
cout << "enter two numbers to swap" << endl;
cout << "enter first number" << endl;
cin >> number1;
cout << "enter second number" << endl;
cin >> number2;
swapper.SwapNumbers(number1,number2);
cout << "number1 : " << number1 << endl;
cout << "number2 : " << number2 << endl;
cout << "using pointers to restore the numbers" << endl;
swapper.SwapNumbersViaPointer(&number1,&number2);
cout << "number1 : " << number1 << endl;
cout << "number2 : " << number2 << endl;
return 0;
}
output :
enter two numbers to swap
enter first number
23
enter second number
24
number1 : 24
number2 : 23
using pointers to restore the numbers
number1 : 23
number2 : 24
Press any key to continue . . .