forked from natto1784/ooplab
25
lab4/10.cpp
Normal file
25
lab4/10.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#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);
|
||||
}
|
33
lab4/12.cpp
Normal file
33
lab4/12.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#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);
|
||||
}
|
81
lab4/file.org
Normal file
81
lab4/file.org
Normal file
@@ -0,0 +1,81 @@
|
||||
* 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
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
444
|
||||
#+end_src
|
||||
#+LATEX: \clearpage
|
||||
|
||||
* 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.
|
||||
#+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
|
||||
|
||||
#+RESULTS:
|
||||
#+begin_src text
|
||||
55
|
||||
#+end_src
|
Reference in New Issue
Block a user