To find largest of three numbers using friend function

 

 

Use of friend functions should be done only in unavoidable circumstances ad not as a general practice.

#include<iostream.h>

#include<conio.h>

 class biggest

  {

    private:

          int a,b,c,large;

   public:

          void getdata();

friend int big(biggest abc);

   };

 void biggest::getdata()

  {

     cout<<"Enter any three number"<<endl;

     cin>>a>>b>>c;

   }

 int big(biggest abc)

  {

       abc.large=abc.a;

       if(abc.b>abc.large)

       {

           abc.large=abc.b;

        }

       if(abc.c>abc.large)

       {

          abc.large=abc.c;

        }

     cout<<endl;

     cout<<" Biggest  no answer is.........="<<abc.large;

     return 0;

  }

int main()

  {

     class biggest obj;

     clrscr();

     obj.getdata();

     big(obj);

     getch();

    return 0;

  }

Explanation....

A friend function to a class may be declared or defined within the scope of class definition. The general syntax of a friend function declaration is given below;

friend return_type user_define_name(arguments list)

In above syntax friend is a keyword which informs the compiler that it is not member function of the class.

Return_type is the type of the value that the function returns.

User_define_name is the name of the function defined by the user,arguments list is the set of parameters defined inside the parenthesis.

Output...

largest of three numbers using friend function