Anda di halaman 1dari 5

Friend Function The private member data of a class can be accessed only by the class' member functions.

Well, there is one exception. A friend function will be friendly with a class even though it is not a member of that class. By the term friendly we mean that the friend function can access the private members of the class. Check out the example below: #include <iostream.h> class car { private: int speed; char color[20]; public: void input( ) { cout<<"\nEnter the color : "; cin>>color; cout<<"\nEnter the speed : "; cin>>speed; } friend void display(car); //Friend of the class 'car' }; void display(car x) { cout<<"\nThe color of the car is : "<<x.color; cout<<"\nThe speed of the car is : "<<x.speed; } int main( ) { car mine; mine.input( ); //accessing a member function display(mine); //passing the object mine to the friend function return 0; } The output is:

Enter the color : red Enter the speed : 345 The color of the car is : red The speed of the car is : 345 First of all we've created a class named car. It has two private data: color and speed and one public function input. It also has a friend function called display ( ). Next comes the definition of display ( ): int display (car x) The parameter specifies an object of type car. The function has been defined to output: x.color; x.speed; The idea is that you will pass an object of type car to this function and the function will display the corresponding color and speed values for that particular object. In the main ( ) part, an object called mine has been created whose private data are set by the user through the input ( ) function. Next the friend function has been called: display(mine); Remember: Friend functions are not members of any class but they can access private data of the class to which they are a friend. Beware: Since they are not members of any class, you should not call them using the dot operator. You might be wondering what's the big point of friend functions. In the above example, we could have made display as a member function of the class instead of declaring it as a friend function. What's the use? A friend function can be friendly to 2 or more classes. The friend function does not belong to any class, so it can be used to access private data of two or more classes. No other function can do that! #include <iostream.h> class virus; // forward declaration of virus class class bacteria {

private: int life; public: bacteria( ) { life=1; } friend void check(bacteria, virus); }; class virus { private: int life; public: virus( ):life(0) {} friend void check(bacteria, virus); }; void check (bacteria b,virus v) { if (b.life= =1 || v.life= =1) { cout<<"\nSomething is alive"; } if (b.life = = 1) { cout<<"\nA bacteria is alive."; } if (v.life = = 1) { cout<<"\nA virus is alive."; } }

int main( ) { bacteria fever; virus cholera; check(fever, cholera); return 0; } In the second line of the program we have the statement: class virus; This is a forward declaration of the class virus. This is done because when the compiler reads through the lines in the bacteria class, it encounters the word virus in the friend function declaration. Until this point it doesn't know what virus is because we will be defining the virus class later on. So we tell the compiler in advance that virus is a class by declaring it in the starting itself. If you don't declare it, you will get errors. Just try it out. One more note: You should declare the friend function in both the classes where you want it to be a friend. In this program we want to check whether any organism is alive at present by testing the value of life. Of course you can write individual member functions in each class but the use of a friend function makes it simpler and easier to understand the logic.

Friend Classes Just like functions are made friends of classes, we can also make one class to be a friend of another class. In this way, the friend class will have access to all the private members of the other class. #include <iostream.h> class add { private: int x,y; public: add( )

{ x=y=4;| } friend class support; //support is now a friend of add }; class support { public: void sum(add ob) //it can access private members of class 'add { cout<<"The sum of the 2 members is : "<<(ob.x+ob.y); } }; int main( ) { add ad; support sup; sup.sum(ad); return 0; } The output will be: The sum of the 2 members is : 8 In this program, the class support is a friend of the class add. Thus the class support can access all the private members of the class add (i.e. x and y). Friend classes are rarely used.