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


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


friend void find(Test t);


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

Run it Now
#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 << "\nThe Largest is :" << t.x;
   } else if (t.y > t.z) {
       cout << "\nThe Largest is :" << t.y;
   } else {
       cout << "\nThe Largest is :" << t.z;
   }
}

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

   find(t);
   return 0;
}

Output:

Enter Three Numbers :8 9 7
The Largest is :9