Translate

C++ FRIEND FUNCTION & INHERITANCE.

 FRIEND FUNCTION

  • In encapsulation and data hiding: a non member function has no permission to access private data of the class. 
  •  The private data members of the class are accessed only from member function of the same class. Any non-member function cannot access the private data of the class. 
  •  C++ allows a mechanism in which a non-member function can access the private data members of the class i.e by declaring a non-member function friend of the class whose private data has to be accessed. 

RULES


  •  Not a member of any class therefore no need for scope resolution operator. 

void sum::putdata(void)  void disp(void) 

  • Friend function can be called like normal function, no need of object.(since it is not a member of any class)

a.disp();      disp(); 

  • Definition also like normal function.(no need to mention the name of the class). 
  • Arguments assed while calling friend function should be the object of the class. 

Q) Write a program to access private data using non-member function. Use friend function.
Q) Write a program to declare friend function in two classes. Calculate the sum of integers of both the classes using friend sum() function. 

Assignment :

Q) Write a program to exchange values between two classes using friend functions. (assignment) .

Q) Write a program to declare three classes. Declare integer array as data member in each class. perform addition of two data member arrays into array of third class. Use friend function.(assignment) 


 



Next Post Previous Post
No Comment
Add Comment
comment url