diff --git a/file.org b/file.org index 4330733..1e8dc7b 100644 --- a/file.org +++ b/file.org @@ -36,3 +36,7 @@ * Lab 7 #+INCLUDE: ./lab7/file.org #+LATEX: \clearpage + +* Lab 8 +#+INCLUDE: ./lab8/file.org +#+LATEX: \clearpage diff --git a/file.pdf b/file.pdf index f028272..c3ea654 100644 Binary files a/file.pdf and b/file.pdf differ diff --git a/lab8/22.cpp b/lab8/22.cpp new file mode 100644 index 0000000..dd33454 --- /dev/null +++ b/lab8/22.cpp @@ -0,0 +1,19 @@ +#include + +template ::value, T> = 0> +void swap(T &x, T &y) { + x += y; + y = x - y; + x -= y; +} + +int main() { + char x = '['; + char y = 'w'; + + swap(x, y); + + std::cout << "x = " << x << ", y = " << y; + + return 0; +} diff --git a/lab8/23.cpp b/lab8/23.cpp new file mode 100644 index 0000000..787c17e --- /dev/null +++ b/lab8/23.cpp @@ -0,0 +1,14 @@ +#include + +template +std::enable_if_t::value, T> sq(T x) { + return x * x; +} + +int main() { + float x = 4.4; + + std::cout << "double: " << sq((double)5.3) << "\nlong: " << sq(4l); + + return 0; +} diff --git a/lab8/24.cpp b/lab8/24.cpp new file mode 100644 index 0000000..44f4bea --- /dev/null +++ b/lab8/24.cpp @@ -0,0 +1,19 @@ +#include + +template +std::enable_if_t::value, T> fn(T x) { + return x * x; +} + +template +std::enable_if_t::value, T> fn(T x) { + return --x; +} + +int main() { + float x = 4.4; + + std::cout << "floating: " << fn((double)5.3) << "\nintegral: " << fn(4l); + + return 0; +} diff --git a/lab8/file.org b/lab8/file.org new file mode 100644 index 0000000..f9546a5 --- /dev/null +++ b/lab8/file.org @@ -0,0 +1,73 @@ +* Write a program to define the function template for swapping two items of the various data types such as integer ,float,and characters. + +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src cpp :tangle 22.cpp :results output :exports both :wrap src text +#include + +template ::value, T> = 0> +void swap(T &x, T &y) { + x += y; + y = x - y; + x -= y; +} + +int main() { + char x = '['; + char y = 'w'; + + swap(x, y); + + std::cout << "x = " << x << ", y = " << y; + + return 0; +} +#+end_src + +#+LATEX: \clearpage + +* Write a program to define the function template for calculating the square of given numbers with different data types. + +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src cpp :tangle 23.cpp :results output :exports both :wrap src text +#include + +template +std::enable_if_t::value, T> sq(T x) { + return x * x; +} + +int main() { + float x = 4.4; + + std::cout << "double: " << sq((double)5.3) << "\nlong: " << sq(4l); + + return 0; +} +#+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 24.cpp :results output :exports both :wrap src text +#include + +template +std::enable_if_t::value, T> fn(T x) { + return x * x; +} + +template +std::enable_if_t::value, T> fn(T x) { + return --x; +} + +int main() { + float x = 4.4; + + std::cout << "floating: " << fn((double)5.3) << "\nintegral: " << fn(4l); + + return 0; +} +#+end_src