forked from natto1784/ooplab
10
file.org
10
file.org
@@ -5,9 +5,9 @@
|
|||||||
|
|
||||||
#+LATEX: \clearpage
|
#+LATEX: \clearpage
|
||||||
|
|
||||||
|----------------------------------------------------------------|
|
|------------------------------------------------------------------------------------------------------------|
|
||||||
| Programs are followed by their respective outputs (and inputs) |
|
| Programs are followed by their respective inputs and outputs i.e, both stdin and stdout are shown together |
|
||||||
|----------------------------------------------------------------|
|
|------------------------------------------------------------------------------------------------------------|
|
||||||
|
|
||||||
* Lab 1
|
* Lab 1
|
||||||
#+INCLUDE: ./lab1/file.org
|
#+INCLUDE: ./lab1/file.org
|
||||||
@@ -40,3 +40,7 @@
|
|||||||
* Lab 8
|
* Lab 8
|
||||||
#+INCLUDE: ./lab8/file.org
|
#+INCLUDE: ./lab8/file.org
|
||||||
#+LATEX: \clearpage
|
#+LATEX: \clearpage
|
||||||
|
|
||||||
|
* Lab 9
|
||||||
|
#+INCLUDE: ./lab9/file.org
|
||||||
|
#+LATEX: \clearpage
|
||||||
|
26
lab9/25.cpp
Normal file
26
lab9/25.cpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
template <typename T,
|
||||||
|
typename = enable_if_t<std::is_arithmetic<T>::value, T>>
|
||||||
|
class Foo {
|
||||||
|
T x, y;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Foo() {
|
||||||
|
cout << "x: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << "y: ";
|
||||||
|
cin >> y;
|
||||||
|
}
|
||||||
|
|
||||||
|
T sum() { return x + y; };
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Foo<float> bar;
|
||||||
|
|
||||||
|
cout << bar.sum();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
28
lab9/26.cpp
Normal file
28
lab9/26.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
template <typename T,
|
||||||
|
typename = std::enable_if_t<std::is_arithmetic<T>::value, T>>
|
||||||
|
class Foo {
|
||||||
|
T x, y;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Foo() {
|
||||||
|
cout << "x: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << "y: ";
|
||||||
|
cin >> y;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Foo() { cout << "Destructor called, nothing to do here."; }
|
||||||
|
|
||||||
|
T mx() { return (x > y ? x : y); };
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Foo<float> bar;
|
||||||
|
|
||||||
|
cout << bar.mx() << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
25
lab9/27.cpp
Normal file
25
lab9/27.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
const string FILE_PATH = "./27.out";
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
string line;
|
||||||
|
|
||||||
|
cout << "Running program till the string \"EOH\" is received via stdin"
|
||||||
|
<< endl;
|
||||||
|
|
||||||
|
ofstream file(FILE_PATH);
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
file.open(FILE_PATH, ios_base::app);
|
||||||
|
|
||||||
|
while (getline(cin, line)) {
|
||||||
|
if (line == "EOH")
|
||||||
|
break;
|
||||||
|
file << line << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
122
lab9/file.org
Normal file
122
lab9/file.org
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
* Write a program to illustrate how to define and declare a class template for reading two data items from the keyboard and to find their sum.
|
||||||
|
|
||||||
|
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||||
|
#+begin_src cpp :tangle 25.cpp :wrap src text
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
template <typename T,
|
||||||
|
typename = enable_if_t<std::is_arithmetic<T>::value, T>>
|
||||||
|
class Foo {
|
||||||
|
T x, y;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Foo() {
|
||||||
|
cout << "x: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << "y: ";
|
||||||
|
cin >> y;
|
||||||
|
}
|
||||||
|
|
||||||
|
T sum() { return x + y; };
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Foo<float> bar;
|
||||||
|
|
||||||
|
cout << bar.sum();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+begin_src text
|
||||||
|
x: 4.3
|
||||||
|
y: 9.1
|
||||||
|
13.4
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+LATEX: \clearpage
|
||||||
|
|
||||||
|
* Write a program to demonstrate the use of special functions, constructor and destructor in the class template. The program is used to find the biggest of two entered numbers.
|
||||||
|
|
||||||
|
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||||
|
#+begin_src cpp :tangle 26.cpp :wrap src text
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
template <typename T,
|
||||||
|
typename = std::enable_if_t<std::is_arithmetic<T>::value, T>>
|
||||||
|
class Foo {
|
||||||
|
T x, y;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Foo() {
|
||||||
|
cout << "x: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << "y: ";
|
||||||
|
cin >> y;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Foo() { cout << "Destructor called, nothing to do here."; }
|
||||||
|
|
||||||
|
T mx() { return (x > y ? x : y); };
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Foo<float> bar;
|
||||||
|
|
||||||
|
cout << bar.mx() << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+begin_src text
|
||||||
|
x: 3.4
|
||||||
|
y: 9.1
|
||||||
|
9.1
|
||||||
|
Destructor called, nothing to do here.
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+LATEX: \clearpage
|
||||||
|
|
||||||
|
* Write a program to illustrate how template functions can be overloaded.
|
||||||
|
|
||||||
|
#+ATTR_LATEX: :options frame=single,breaklines=true
|
||||||
|
#+begin_src cpp :tangle 27.cpp :wrap src text
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
const string FILE_PATH = "./27.out";
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
string line;
|
||||||
|
|
||||||
|
cout << "Running program till the string \"EOH\" is received via stdin"
|
||||||
|
<< endl;
|
||||||
|
|
||||||
|
ofstream file(FILE_PATH);
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
file.open(FILE_PATH, ios_base::app);
|
||||||
|
|
||||||
|
while (getline(cin, line)) {
|
||||||
|
if (line == "EOH")
|
||||||
|
break;
|
||||||
|
file << line << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+begin_src text
|
||||||
|
Running program till the string "EOH" is received via stdin
|
||||||
|
i have a
|
||||||
|
flayed dismembered
|
||||||
|
corpse in front of my
|
||||||
|
lawn
|
||||||
|
EOH
|
||||||
|
#+end_src
|
Reference in New Issue
Block a user