Write a program to swap two numbers using friend function

#include<iostream.h>
#include<conio.h>
class sample
{
   private:
       int x,y;
   public:
       void setdata(int a,int b)
       {
          x=a;
          y=b;
       }
       void showdata()
       {
          cout<<"x="<<x<<"\ny="<<y;
       }
   friend void swap(sample &s);
};
   void swap(sample &s)
   {
      int temp;
      temp=s.x;
      s.x=s.y;
      s.y=temp;
   }
void main()
{
    sample s;
    int x1,x2;
    clrscr();
    cout<<"\nInput 2 Numbers:";
    cin>>x1>>x2;
    s.setdata(x1,x2);
    cout<<"\nBefore Swapping\n";
    s.showdata();
    cout<<"\nAfter Swapping\n";
    s.showdata();
    getch();
}

6 comments:

  1. bro there no swaping ,,,,,,u didnt call swap function..

    ReplyDelete
  2. its useless this program does not swap the nos

    ReplyDelete
  3. simply waste our time rubbish if you don"t know how write program y you publish it

    ReplyDelete
  4. #include
    using namespace std;
    class second;
    class first{
    int x;
    public:void get(){
    cout<<"x is:";
    cin>>x;
    }
    void display(){
    cout<<"result x"<>y;
    }

    void display(){
    cout<<"result y"<<y<<endl;
    }friend void swap(first &,second &);
    };void swap(first &a,second &b){
    int c;
    c=a.x;
    a.x=b.y;
    b.y=c;
    }
    main(){
    first a1;
    second b1;
    a1.get();
    b1.get();
    a1.display();
    b1.display();
    swap(a1,b1);
    a1.display();
    b1.display();

    }

    ReplyDelete
  5. sami colon missing after void swap function line no.15.

    ReplyDelete
  6. #include
    #include
    class sample
    {
    private:
    int x,y;
    public:
    void setdata(int a,int b)
    {
    x=a;
    y=b;
    }
    void showdata()
    {
    cout<<"x="<>x1>>x2;
    s.setdata(x1,x2);
    cout<<"\nBefore Swapping\n";
    s.showdata();
    swap(s);
    cout<<"\nAfter Swapping\n";
    s.showdata();
    getch();
    }


    Guys this is the correct program you'll surely get the output

    ReplyDelete