Showing posts with label friend. Show all posts
Showing posts with label friend. Show all posts

Friday, 29 April 2016

Friend Function Example.

#include<iostream.h>
#include<conio.h>
class square
{
int no;
public:
void accept()
{
cout<<"Enter no for square:";
cin>>no;
}
friend void sq(square s);
};
void sq(square s)
{
int ans;
ans=s.no*s.no;
cout<<"Square="<<ans;
}
void main()
{
clrscr();
square s;
s.accept();
sq(s);
getch();
}
/*Output:
Enter no for square:4
Square=16 */

Thursday, 28 April 2016

Write a program showing Friend Function

 #include<iostream.h>
 #include<conio.h>
 class b;
 class a
 {
int no1;
public:
void accept()
{
cout<<"Enter no 1:";
cin>>no1;
}
void display()
{
cout<<"No 1 is greater";
}
friend void great(a,b);
 };
 class b
 {
int no2;
public:
void accept()
{
cout<<"Enter no 2:";
cin>>no2;
}
void display()
{
cout<<"No 2 is greater";
}
friend void great(a,b);
 };
 void great(a m,b n)
 {
if(m.no1>n.no2)
{
m.display();
}
else
{
n.display();
}
 }
 void main()
 {
clrscr();
a m;
b n;
m.accept();
n.accept();
great(m,n);
getch();
}
/*Output:
Enter no 1:23
Enter no 2:21                                                                  
No 1 is greater */