-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates_learning.cpp
155 lines (127 loc) · 3.04 KB
/
templates_learning.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "templates_learning.h"
/*
* Function templates
*/
template <typename T> T myMax(T x, T y){
return x > y ? x : y;
}
void simple_max_func_template(){
cout << myMax<int>(3, 7) << endl;
cout << myMax<char>('g', 'e')<<endl;
}
//buble sort function
template <typename T> void bublesort(T a[], int n){
for (int i =0; i < n; i++){
for (int j = i+1; j < n; j++){
if (a[i] > a[j]){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void buble_sort_func_template(){
int arr[] ={ 5, 2, 1, 4, 9, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bublesort(arr, n);
for (int i = 0; i < n; i++){
cout<<arr[i] <<" ";
}
cout << endl;
}
//template function with static variables
template<typename T> void func_static_var(const T& x){
static int i = 0;
i += 1;
cout << i << endl;
return;
}
void static_temp_func_test(){
func_static_var<int>(10);
func_static_var<double>(20.01);
}
/*
* Class templates
* */
template<typename T> Array<T>::Array(T arr[], int s){
ptr = new T[s];
size = s;
for (int i = 0; i < size; i++){
ptr[i] = arr[i];
}
}
template<typename T> void Array<T>::print(){
for (int i = 0; i < size; i++){
cout << *(ptr+i) << " ";
}
}
// multiple parameters
template<class T, class U> A<T, U>::A(){
cout << "multi arg default constructor called" << endl;
}
//default parameters
template<class T, typename U> DP<T, U>::DP(){
cout << "DP defualut constructor called"<<endl;
}
void class_temp_check(){
int arr[5] = {1, 2, 3, 4, 5};
Array<int> a(arr, 5);
a.print();
cout << endl;
A< char, int> a_obj1;
A<float, int> a_obj2;
DP <int> dp_obj1;
DP <float, double> dp_obj2;
}
template<class T>static_test<T>::static_test(){
count++;
}
template<class T> int static_test<T>::count = 0;
void static_temp_class_test(){
static_test<int> a;
cout << static_test<int>::count << endl;
static_test<double> b;
cout << static_test<double>::count << endl;
static_test<int> c;
cout << static_test<int>::count << endl;
}
/* class Array definations
*/
template<typename T> nArray<T>::nArray(int i){
len_ = i;
cout << "in arg constructor: " << len_ << endl;
}
template<typename T> nArray<T>::nArray(const nArray& arrObj){
}
template<typename T> nArray<T>::~nArray(){
cout << "int nArray destructor" << endl;
}
template<typename T> int nArray<T>::len()const{
}
template<typename T> T const& nArray<T>::operator[](int i) const{
}
template<typename T> T& nArray<T>::operator[](int i){
}
template<typename T> nArray<T>& nArray<T>::operator=(const nArray<T>& tmobj){
}
//driver function
int main(){
//function template test
cout << "### function templates testing ###" << endl;
simple_max_func_template();
buble_sort_func_template();
cout << "func template with static variable test case" <<endl;
static_temp_func_test();
cout << endl;
//class template test
cout << "### class templates testing ###" << endl;
class_temp_check();
cout << "class template with static variable test case"<<endl;
static_temp_class_test();
nArray<int> a_int;
nArray<double> a_d;
nArray<float> a_f;
nArray<string> a_str;
nArray<nArray<int>> arr_arr;
}