Write a C++ program to create a class which contains two data members. Write member functions to accept display and swap two entered numbers using call by reference.

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<conio.h>
#include<iostream.h>
class test
{
int temp;
public:
   void swap(int *a,int *b) //swapping of 2 no using call by reference
   {
    temp=*a;
      *a=*b;
      *b=temp;
    cout<<"After swapping: "<<endl;
      cout<<"a : "<<*a<<endl<<"b : "<<*b<<endl;
   }
};
int main()
{
int a,b;
test t; // object created
   cout<<"\nEnter value of a: ";
   cin>>a;
   cout<<"\nEnter value of b: ";
   cin>>b;
   t.swap(&a,&b);
   getch();
   return(0);
}

0 Comments