Wednesday, April 17, 2013

Friend Function Demo in C++


#include<iostream.h>
#include<conio.h>
class end;
class free
{
private:
int roll;
public:
void accept();
friend void friend_demo(free f,end e);
};

void free :: accept()
{
cout<<"Enter the Roll Number :"<<endl;
cin>>roll;
}


class end
{
private :
char name[20];
public :
void accept()
{
cout<<"Enter the Name "<<endl;
cin>>name;
}
friend void friend_demo(free f,end e);
};

void main()
{
clrscr();
free f;
end e;
f.accept();
e.accept();

friend_demo(f,e);

getch();
}

void friend_demo(free f,end e)
{
cout<<"Roll number is :"<<f.roll<<endl<<"Name is "<<e.name;
}

---------------------------------------------------------------------------------------


#include<iostream.h>
#include<conio.h>

class B;
class A
{
private:
int roll;
public:
void acc()
{
cout<<"Enter roll :";
cin>>roll;
}
friend void disp(A a,B b);

};
class B
{
private:
char name[10];
public:
void acc()
{
cout<<"Enter name :";
cin>>name;
}
friend void disp(A a,B b);
};

void disp(A a,B b)
{
cout<<"Name :"<<b.name<<endl;
cout<<"Roll :"<<a.roll;
}

void main()
{
clrscr();
A a;
B b;
a.acc();
b.acc();
disp(a,b);
getch();
}

No comments:

Post a Comment