C++ Program to find the largest of three numbers using friend function


In this program, You will learn how to find the largest of three numbers using friend function in C++.


friend void find(Test t);

Example: How to find the largest of three numbers using friend function in C++.

#include<iostream>
using namespace std;

class Test {
private:
   int x, y, z;
public:

   void input() {
       cout << "Enter three numbers:";
       cin >> x >> y>>z;
   }

   friend void find(Test t);
};

void find(Test t) {
   if (t.x > t.y && t.x > t.z) {
       cout << "Largest is:" << t.x;
   } else if (t.y > t.z) {
       cout << "Largest is:" << t.y;
   } else {
       cout << "Largest is:" << t.z;
   }
}

int main() {
   Test t;
   t.input();

   find(t);
   return 0;
}

Output:

Enter three numbers:10 30 20
Largest is:30