forked from natto1784/ooplab
		
	
							
								
								
									
										19
									
								
								lab8/22.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								lab8/22.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								lab8/23.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								lab8/23.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
std::enable_if_t<std::is_arithmetic<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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										19
									
								
								lab8/24.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								lab8/24.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
std::enable_if_t<std::is_integral<T>::value, T> fn(T x) {
 | 
			
		||||
  return x * x;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
std::enable_if_t<std::is_floating_point<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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										73
									
								
								lab8/file.org
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								lab8/file.org
									
									
									
									
									
										Normal file
									
								
							@@ -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 <iostream>
 | 
			
		||||
 | 
			
		||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::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 <iostream>
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
std::enable_if_t<std::is_arithmetic<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 <iostream>
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
std::enable_if_t<std::is_integral<T>::value, T> fn(T x) {
 | 
			
		||||
  return x * x;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template <typename T>
 | 
			
		||||
std::enable_if_t<std::is_floating_point<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
 | 
			
		||||
		Reference in New Issue
	
	Block a user