dimanche 23 août 2015

Member function of a class as friend to another class

In this code , i have made max function of class B friend of class A.I have also done forward declaration of class B.But it is giving error.

#include<iostream>

using namespace std;

class B;

class A
{
   int a;
   public:

   void get()
   {
      cin>>a;
   }

   friend void B :: max(A , B);
};

class B
{
   int b;
   public:

   void get()
   {
      cin>>b;
   }

   void max(A x, B y)
   {
      if (x.a > y.b)
         cout<< " x is greater";
      else cout<<"y is greater";
   }
};

int main()
{
   A x;
   B y,c;
   x.get();
   y.get();
   c.max(x,y);
}

Aucun commentaire:

Enregistrer un commentaire