illustration of friend funcation by passing object as a parameter

A funcation which is friend for a class is set to be friend funcation 

In the fallowing example: big is friend funcation which has objects as a parameter

in the class test1 we get the value for a via geta() funcation & in the class test2 we get value for b via getb() funcation. In the main(), we are creating objects t1 & t2 for test1 & test2 respectivly & we are  passing this objects as a parameter for the friend funcation 

#include <iostream>

using namespace std;
class test2;
class test1
{
    private:
        int a;

    public:
        void geta()
        {
            cout<<"enter a value "<<endl;
            cin>>a;
        }
            friend void big(test1,test2);

};
class test2
{
    private:
        int b;

    public:
        void getb()
        {
            cout<<"enter value b "<<endl;
            cin>>b;
        }
            friend void big(test1,test2);

};

void big(test1 t1,test2 t2)
{

   t1.a;
  t2.b;
    if(t1.a>t2.b)
    {
        cout<<"a is grater"<<endl;
    }
    else if(t1.a<t2.b)
    {
        cout<<"b is greater"<<endl;

    }
    else
        {
        cout<<"both are equal"<<endl;
    }
}

int main()
{
    test1 t1;
    test2 t2;
    t1.geta();
    t2.getb();
    big(t1,t2);
    return 0;
}

output :

        enter a value

         30
        enter value b
         20
        a is grater

Posted on by