forked from natto1784/ooplab
2.2 KiB
2.2 KiB
- 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.
- 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.
- Write a program to read a set of lines from the keyboard and to store it on a specified file.
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.
#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;
}
x: 4.3
y: 9.1
13.4
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.
#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;
}
x: 3.4
y: 9.1
9.1
Destructor called, nothing to do here.
Write a program to read a set of lines from the keyboard and to store it on a specified file.
#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;
}
file.close();
return 0;
}
Running program till the string "EOH" is received via stdin
i have a
flayed dismembered
corpse in front of my
lawn
EOH