Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Write a program using class and reference variables as arguments of a function to swap the values of a pair of integers.

Source Code :

#include<iostream.h>
class var {
      public:
      void swap(int &a,int &b) {
                  a=a+b;
                  b=a-b;
                  a=a-b;
                  cout<<endl<<"After swapping : ";
                  cout<<endl<<"Value of 'a' :"<<a;
                  cout<<endl<<"Value of 'b' :"<<b;
            }
};

int main() {
      int a,b;
      var ob1;
      cout<<"Value of 'a' :";
      cin>>a;
      cout<<"Value of 'b' :";
      cin>>b;
      ob1.swap(a,b);
      return 0;
}

Output :

Value of ‘a’ : 23
Value of ‘b’ : 45

After swapping :
Value of ‘a’ : 45
Value of ‘b’ : 23

No comments:

Post a Comment