2011-07-07T12:59:36-04:00
Write a program to swap 2 numers using class
1
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 !
Learn more about our help with Assignments:
C++
Comments