Answer to Question #34060 in C++ for Harshit Garg
write a program to swap two numbers by using classes?
1
2013-09-04T11:59:46-0400
#include <iostream>
using namespace std;
class numbers{
public:
//method for swa[]
void swap(){
int c = a;
a = b;
b = c;
}
//method for input information
void getNumbers(){
cout << "Input two numbers: ";
cin >> a >> b;
}
//method for print numbers
void printNumbers(){
cout << "Number a is " << a << endl << "Number b is " << b << endl;
}
private:
int a, b;
};
int main(){
//object of class
numbers myNum;
myNum.getNumbers();
myNum.printNumbers();
myNum.swap();
cout << "After swap" << endl;
myNum.printNumbers();
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
Leave a comment