Write programme to find value using Friend Function.

Q. Write a programme to find maximum of two data or value using friend function.

Ans. 

# include < iostream.h >

     # include < conio.h >

    Using name space std;

    Class maximum field

  {

Int data;

Public:

// member function declaration

Void get data ();

Void show ();

// member function declaration

Friend void max (Max friend, max friend);

};

// member function definition

Void max friend : : get data ()

{

Cout << "\n enter an integer = ";

Cin >> data;

}

Void max friend : : show ()

{

Cout << \n \n data :"

}

// friend function definition

Void max (max friend a, max friend b)

{

If (a. Data > b. Data)

Cout << " first data" << a. Data << "is greater than" << b. Data;

Else

Cout << "second data" << b. Data << "is greater than" << a. Data;

)

Int main ()

{

Max friend num 1, num 2;

num 1. Get data ();

num 2. Get data ();

Max (num 1, num 2);

getch ();

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.