Answer to Question #3421 in C++ for Shikha Sharma

Question #3421
Write a program to swap 2 numers using class
1
Expert's answer
2011-07-11T10:25:14-0400
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Numbers
{
public:
& Numbers ():
X(0), Y(0) {}
& Numbers (int x, int y):
X(x), Y(y) {}
& void ShowNumbers ();
& void Swap ();
private:
& int X;
& int Y;
};

void Numbers::ShowNumbers ()
{
cout<< "X =\t"<< X<< endl;
cout<< "Y =\t"<< Y<< endl;
}

void Numbers::Swap ()
{
int temp;
temp = X;
X = Y;
Y = temp;
}


int main ()
{
int x, y;
cout<< "Enter two numbers separated by space (X Y)\n> ";
cin >> x >> y;
Numbers num(x, y);
cout<< "\nBefore swap:"<< endl;
num.ShowNumbers ();
num.Swap ();
cout<< "\nAfter swap:"<< endl;
num.ShowNumbers();
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS