74 lines
1.4 KiB
Org Mode
74 lines
1.4 KiB
Org Mode
* Write a program to find the biggest of three numbers using friend function.
|
|
|
|
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
#+begin_src cpp :tangle 10.cpp :results output :exports both :wrap src text
|
|
#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);
|
|
}
|
|
#+end_src
|
|
|
|
#+LATEX: \clearpage
|
|
|
|
* Write a program to demonstrate the use of friend function with Inline assignment.
|
|
All friend functions are inline functions.
|
|
|
|
#+ATTR_LATEX: :options frame=single,breaklines=true
|
|
* Write a program to find the greatest of two given numbers in two different classes using friend function.
|
|
#+begin_src cpp :tangle 12.cpp :results output :exports both :wrap src text
|
|
#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);
|
|
}
|
|
#+end_src
|