1.4 KiB
1.4 KiB
- Write a program to find the biggest of three numbers using friend function.
- Write a program to demonstrate the use of friend function with Inline assignment.
- Write a program to find the greatest of two given numbers in two different classes using friend function.
Write a program to find the biggest of three numbers using friend function.
#include <algorithm>
#include <iostream>
using namespace std;
class Trio {
private:
int a, b, c;
public:
Trio(int, int, int);
friend int biggest(Trio);
};
Trio::Trio(int a, int b, int c) {
this->a = a;
this->b = b;
this->c = c;
}
int biggest(Trio t) { return (max({t.a, t.b, t.c})); }
int main() {
Trio t(444, 4, -44);
cout << biggest(t);
}
444
Write a program to demonstrate the use of friend function with Inline assignment.
All friend functions are inline functions.
Write a program to find the greatest of two given numbers in two different classes using friend function.
#include <algorithm>
#include <iostream>
using namespace std;
class Foo;
class Bar;
class Foo {
private:
int foo;
public:
Foo(int n) { foo = n; }
friend int bigger(Foo, Bar);
};
class Bar {
private:
int bar;
public:
Bar(int n) { bar = n; }
friend int bigger(Foo, Bar);
};
int bigger(Foo a, Bar b) { return max(a.foo, b.bar); }
int main() {
Foo a(5);
Bar b(55);
cout << bigger(a, b);
}
55