SHARE
TWEET

Untitled

a guest Jan 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top