Hi,
I am first year B.Sc.It student. I just started learning c++ language. When reading c++ book I stop at friend function. I unable to understand this concept. If you any of you have any idea or example about friend function please help me.
Hi,
I am first year B.Sc.It student. I just started learning c++ language. When reading c++ book I stop at friend function. I unable to understand this concept. If you any of you have any idea or example about friend function please help me.
A friend function is usually used for taking non-public members of a class. You can access private member using friend functions. It is easy to define friend function, you only have to used friend keyword with any function. You can only used friend in function declaration. You can declare friend function in number of classes. You can declare friend function either with private access modifier or public access modifier.
Just go through following example step by step.
Code:#include class crystal { private: int p,q; public: void test() { p=100; q=200; } friend int compute(crystal e2) }; int compute(crystal e2) { return int(e2.p+e3.q)-5; } main() { crystal es; es.test(); cout<<"The result is:"< }
This is the example of friend function to exchange the private values of two classes
Code:#include<iostream.h> class P; class Q; class P { private: int v1; public: void in(int p) { v1=p; } void display() { cout<<v1; } friend void exchange(P&, Q&); }; class Q { private: int v2; public: void in(int p) { v2=p; } void display() { cout<< v2; } friend void exchange (P&, Q&); }; void exchange(P& w, Q& z) { int temp=w.v1; w.v1=z.v2; z.v2=temp; } int main() { P obj1; Q obj2; obj1.in(1000); obj2.in(2000); cout<<"\n Before"; obj1.display(); obj2.display(); cout<<"\n After"; exchange(obj1, obj2); obj1.display(); obj2.display(); return 0; } #include <iostream> using namespace std; class Q; class P { private: int p; public: P() { p=0; } friend void show(P& w, Q& z); }; class Q { private: int q; public: Q() { q=6; } friend void show(A& w, Q& z); }; void show(A& x, Q& y) { cout << "P::p=" << w.p << endl; cout << "Q::q=" << z.q << endl; } int main() { P p; Q q; show(p,q); } #include <iostream> using namespace std; class Q; class P { private: int p; public: P() { p=0; } void show(P& w, Q& z); }; class Q { private: int q; public: Q() { q=6; } friend void A::show(P& x, Q& y); }; void P::show(P& w, Q& z) { cout << "P::p=" << w.p << endl; cout << "Q::q=" << z.q << endl; } int main() { P p; Q q; a.show(p,q); return 0; }
This is the most simple example of friend function.
Code:#include <iostream> using namespace std; class Point { friend void CP( Point & ); public: Point( void ) : m(0) {} void PP( void ){cout << m<< endl; } private: int m; }; void CP ( Point &i ) { p.m++; } int main() { Point sPoint; sPoint.PP(); CP(sPoint); sPoint.PP(); }
Code:#include <iostream.h> class car { private: int SP; char c[30]; public: void input( ) { cout<<"\n color : "; cin>>c; cout<<"\n speed : "; cin>>SP; } friend void display(car1); }; void display(car1 x) { cout<<"\nThe color of the car is : "<<x.c; cout<<"\nThe speed of the car is : "<<x.SP; } int main( ) { car1 m; m.input( ); display(m); return 0; }
Bookmarks