#27_Friend Classes and Member Friend Function in C++

 #include <iostream>

using namespace std;
//Friend Classes and Member Friend Function
class complex; //Forward declaration
class sumcomplex
{
public:
    void SumRealComplex(complex o1complex o2);
    void SumComplexPart(complex o1complex o2);
};

class complex
{
    int c1c2;

public:
    // friend void sumcomplex::SumRealComplex(complex, complex); 
        --->Member friend function
    // friend void sumcomplex::SumComplexPart(complex, complex);
    friend class sumcomplex; //Friend class
    void setcomplex(int aint b)
    {
        c1 = a;
        c2 = b;
    }
};

void sumcomplex::SumRealComplex(complex o1complex o2)
{
    cout << "The sum of Real Parameters is : " << o1.c1 + o2.c1 << endl;
}

void sumcomplex::SumComplexPart(complex o1complex o2)
{
    cout << "The sum of Complex parameter is : " << o1.c2 + o2.c2 << endl;
}

int main()
{
    complex o1o2;
    o1.setcomplex(42);
    o2.setcomplex(75);
    sumcomplex sum1sum2;
    sum1.SumRealComplex(o1o2);
    sum2.SumComplexPart(o1o2);
    return 0;
}

Comments

Popular posts from this blog