netx_iit

used of friend function in C++

Jul 7th, 2020 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. /* perform addition of two number which is private for seprate class using friend function*/
  2. #include<iostream>
  3. using namespace std;
  4. class B;//forward declaration
  5. class A
  6. {
  7.     int x;
  8.     public:
  9.     A()//default cons
  10.     {
  11.         x=10;  
  12.     }  
  13.     friend void add(A,B);//declaration
  14. };
  15. class B
  16. {
  17.     int y;
  18.     public:
  19.     B()
  20.     {
  21.         y=20;  
  22.     }  
  23.     friend void add(A,B);//declaration
  24. };
  25. void add(A a1,B b1)
  26. {
  27.     cout<<"Sum of two diff class private member is "<<a1.x+b1.y;
  28. }
  29. int main()
  30. {
  31.     A a1;
  32.     B b1;
  33.     add(a1,b1);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment