You are on page 1of 5

C++

Chapter 7: Friend Function

-----------------------------------------------------------------------Chapter Contents Introduction to friend function Defining Operator as a friend function Overloading of operators insertion(<<) and extraction(>>)

-----------------------------------------------------------------------Introduction to friend function Member function can access private members of the class along with other members of the same class. on member functions can not access private members of the class. !riend functions can access private members of the class along with other members of the class though it is not a member function. !riend function is a special non member function whose declaration part should be in the class bod" preceded b" #e"word friend. $his ma#es clear to the compiler that this function is friend to the class in which it is declared. Definition part should be #ept outside the class bod". %emember& not to put membership label while defining friend function as it is not a member function.

'onsider the following example( #include<iostream.h> class complex { private: int a,b; public: void getdata(); void showdata(); riend void dost(); !; void complex:: getdata() { cout<<"#nter two numbers"; cin>>a>>b; ! void complex:: showdata() {

cout<<"a$"<<a<<"b$"<<b; ! void complex::dost() { cout<<a<<b; ! void main() { complex c%; c%.getdata(); c%.dost(); c%.showdata(); ! Don)t trust on the above code. *bove program is to ma#e "ou understand about few of the common mista#es. !irst of all ob+ect cannot call friend function as dost() is not a member function. ,o dost() should be called independentl" li#e an" other non member function. ,econd mista#e is during definition of function dost(). %emember membership label should be used for member functions. $hird mista#e is& friend function can access private members of the class but not independentl" (without using ob+ect and dot operator). !or an" member function ob+ect is re-uired to invo#e it and the variables used in member functions are of the ob+ect who invo#es the member function. .ere& dost() is not a member& hence can not be called b" an" ob+ect then whose variable a and b are being used b" function dost(). $his forces the programmer to pass ob+ect during function call so that friend function can access ob+ect members. $he following example is correction in the previous program( #include<iostream.h> class complex { private: int a,b; public: void getdata(); void showdata(); riend void dost(complex); !; void complex:: getdata() { cout<<"#nter two numbers"; cin>>a>>b; ! void complex:: showdata() {

cout<<"a$"<<a<<"b$"<<b; ! void dost(complex c) { cout<<c.a<<c.b; ! void main() { complex c%; c%.getdata(); dost(c%); c%.showdata(); ! Defining operator as a friend function #include<iostream.h> class complex { private: int a,b; public: void getdata(); void showdata(); riend complex operator &(complex, complex); !; complex operator &(complex d%, complex d') { complex temp; temp.a$d%.a&d'.a; temp.b$d%.b&d'.b; return(temp); ! void complex:: getdata() { cout<<"#nter two numbers"; cin>>a>>b; ! void complex:: showdata() { cout<<"a$"<<a<<"b$"<<b; ! void main() { complex c%,c',c(; c%.getdata(); c'.getdata();

c($c%&c'; c(.showdata(); ! Observe main() function& speciall" the line c/0c12c34. 'alling convention is same as we did in operator overloading without friend function. 5ut the interpretation is not same. .ere& the valid interpretation is& operator 62) is called independentl" (as it is non member) and c1 and c3 ob+ects are passed. $hese ob+ects are then received in d1 and d3 (see definition part of 62)) Ob+ect temp is used to temporar" hold the addition result and return bac# to main() where the value of temp copied into ob+ect c/. Overloading insertion (<<) and extraction ( ) operator

#include<iostream.h> class complex { private: int a,b; public: void getdata(); void showdata(); riend ostream ) operator <<(ostream ), complex); riend istream ) operator >>( istream ),complex )); !; ostream ) operator <<(ostream )dout, complex c) { dout<<c.a<<" *<<c.b; return(dout); ! istream ) operator >>(istream )din, complex )c) { din>>c.a>>c.b; return(din); ! void complex:: getdata() { cout<<"#nter two numbers"; cin>>a>>b; ! void complex:: showdata() { cout<<"a$"<<a<<"b$"<<b; ! void main() {

complex c%,c'; cout<<"#nter real and img part: *; cin>>c%; cout<<"#nter real and img part: "; cin>>c'; cout<<"+alues o ob,ect c%:" cout<<c%; cout<<"+alues o ob,ect c':" cout<<c'; ! 7e cannot create ob+ect of class istream and ostream but we can ma#e references of these classes (as we do in friend functions). .ere we overload two operators inserter (<<) and extractor (>>). otice the line cin c! in main()& operator >> is called and references of cin and c! are passed. $his is then received in reference ob+ect din and reference variable c. ,imilarl"& notice the line cout<<c!& operator << is called and reference of cout and c! are passed. $his is then received in reference ob+ect dout and variable c. 'oding inside inserter and extractor are self explanator".

You might also like