fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Number
  5. {
  6. int num;
  7. public:
  8. Number()
  9. {
  10. cout<<"Enter number: ";
  11. cin>>num;
  12. }
  13. void display()
  14. {
  15. cout<<num<<' ';
  16. }
  17. friend void Swap(Number&, Number&);
  18. };
  19.  
  20. void Swap(Number& obj1,Number& obj2)
  21. {
  22. int temp=obj1.num;
  23. obj1.num=obj2.num;
  24. obj2.num=temp;
  25. }
  26.  
  27. int main()
  28. {
  29. Number obj1,obj2;
  30. cout<<"\nBefore swap :";
  31. obj1.display();
  32. obj2.display();
  33.  
  34. Swap(obj1,obj2);
  35. cout<<"\nAfter swap :";
  36. obj1.display();
  37. obj2.display();
  38.  
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Enter number: Enter number: 
Before swap :-246106688 32766 
After swap :32766 -246106688