Search This Blog

Wednesday 24 February 2016

Write a program to exchange the private values of two classes.(use friend function and call by reference).

Practical 16


#include<iostream>
using namespace std;

class two;
class one
{
    int a;
    public:
    void setdata(int i)
    {
        a=i;
    }
    void display()
    {
        cout<<"value1 = "<<a<<endl;
    }
    friend void exch(one &,two &);
};

class two
{
    int b;
    public:
    void setdata(int i)
    {
        b=i;
    }
    void display()
    {
        cout<<"value2 = "<<b<<endl;
    }
    friend void exch(one &,two &);
};

void exch(one &x,two &y)
{
    int temp;
    temp = x.a;
    x.a = y.b;
    y.b = temp;
}

int main()
{
    one p;
    p.setdata(1089);
    two q;
    q.setdata(275);
    cout<<"Values before swapping are:"<<endl;
    p.display();
    q.display();
    exch(p,q);
    cout<<"Values after swapping are:"<<endl;
    p.display();
    q.display();
    return 0;
}

output:
values before swaping
value 1 = 1089
value 2 = 275
values after swaping

value 1 = 275
value 2 = 1089

No comments:

Post a Comment