0% found this document useful (0 votes)
8 views1 page

C++ Program to Find Greatest of Two Numbers

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

C++ Program to Find Greatest of Two Numbers

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program 3

Write a program in c++ to find greatest of two given numbers in two different classes
using friend function
#include <iostream>
using namespace std;
class B;
class A {
int num1;
public:
void setData(int n) {
num1 = n;
}
friend void findGreatest(A, B);
};
class B {
int num2;
public:
void setData(int n) {
num2 = n;
}
friend void findGreatest(A, B);
};

void findGreatest(A a, B b) {
if (a.num1 > b.num2)
cout << "Greatest number is: " << a.num1 << endl;
else if (b.num2 > a.num1)
cout << "Greatest number is: " << b.num2 << endl;
else
cout << "Both numbers are equal: " << a.num1 << endl;
}
int main() {
A a;
B b;
int n1, n2;

cout << "Enter first number: ";


cin >> n1;
cout << "Enter second number: ";
cin >> n2;

[Link](n1);
[Link](n2);
findGreatest(a, b);
return 0;
}

You might also like