Saturday, 23 March 2019

CPP-EX45

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.
#include<iostream.h>
#include<conio.h>
 class Integer
   {
    public:
    int x;
    int y;
    void getdata();
    void display();
    void swap(int &x,int &y);
   };
 void Integer::getdata()
   {
    cout<<"\n\n************** Enter Data ****************";
    cout<<"\nEnter the value of x:";
    cin>>x;
    cout<<"\nEnter the value of y:";
    cin>>y;
   }
 void Integer::display()
   {
swap(x,y);
    cout<<"\n\n**************** Display Data **************";
    cout<<"\nx="<<x;
    cout<<"\ny="<<y;
   }
 void Integer::swap(int &p,int &q)
   {
    int temp;
    temp=p;
    p=q;
    q=temp;
    //cout<<"\n************* After Swap *****************";
   // cout<<"\nx="<<x;
    //cout<<"\ny="<<y;
   }
 int main()
   {
    int x,y;
    Integer i;
    clrscr();
    i.getdata();
    i.display();
    //i.swap(x,y);
    getch();
    return 0;
   }