Files
ooplab/lab4/file.org
2022-12-26 16:30:38 +05:30

1.4 KiB

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);
}

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);
}