Monday, January 20, 2014

C++ Program for InterChanging Values Using Friend Function

with 0 Comment
#include<iostream.h>
#include<conio.h>
class ex2;
class ex1
{
int a;
public:
void getdata(int a1)
{
a=a1;
}
void display()
{
cout<<"\n First Value:"<<a;
}
friend void exchange(ex1 &x,ex2 &y);
};
class ex2
{
int b;
public:
void getdata(int b1)
{
b=b1;
}
void display()
{
cout<<"\n Second Value:"<<b;
}
friend void exchange(ex1 &x,ex2 &y);
};
void exchange(ex1 &x,ex2 &y)
{
int temp=x.a;
x.a=y.b;
y.b=temp;
}
void main()
{
clrscr();
cout<<"\n Enter the First Value:";
cin>>ex1;
cout<<"\n Enter the Second Value:";
cin>>ex2;
ex1 c1;
ex2 c2;
cout<<"\n InterChanging Values";
cout<<"\n ************";
c1.getdata(ex1);
c2.getdata(ex2);
cout<<"\n Values before exchange"<<"\n";
c1.display();
c2.display();
exchange(c1,c2);
cout<<"Value after exchange"<<"\n";
c1.display();
c2.display();
getch();
}


------------
OUTPUT:
------------
Interchanging Values
*****************
Enter the First Value      : 10
Enter the Second Value  :20

Before Interchange

Value of ex1  :10
Value of ex2  : 20

After InterChange

Value of ex1:20
Value of ex2:10

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive