Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // forward declaration
  5. class B;
  6. class A {
  7.     private:
  8.       int numA;
  9.     public:
  10.       A(): numA(12) { }
  11.       // friend function declaration
  12.       friend int add(A, B);
  13. };
  14.  
  15. class B {
  16.     private:
  17.        int numB;
  18.     public:
  19.        B(): numB(1) { }
  20.        // friend function declaration
  21.        friend int add(A , B);
  22. };
  23.  
  24. // Function add() is the friend function of classes A and B
  25. // that accesses the member variables numA and numB
  26. int add(A objectA, B objectB)
  27. {
  28.    return (objectA.numA + objectB.numB);
  29. }
  30.  
  31. int main()
  32. {
  33.     A objectA;
  34.     B objectB;
  35.     cout<<"Sum: "<< add(objectA, objectB);
  36.     return 0;
  37. }
  38. O
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement