Skip to main content

Friend Function in Class in C++

 


#include <bits/stdc++.h>

using namespace std;

class make
{
    int a, b;
    friend make sum(make o, make i);

public:
    void set(int n, int m)
    {
        a = n;
        b = m;
    }
    void print()
    {
        cout << "Your number is " << a << " + " << b << endl;
    }
};
make sum(make o, make i)
{
    make t;
    t.set((o.a + i.a) , (o.b + i.b));
    return t;
}
int main()
{
    make x, y, s;
    x.set(4, 2);
    y.set(2, 4);
    s = sum(x, y);
    s.print();
    return 0;
}

Comments