-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.cpp
62 lines (53 loc) · 1.14 KB
/
template.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
#include <glog/logging.h>
#include <string>
template <typename T>
T abs (T x)
{
return (x > 0)? x : -x;
}
// end function template
// this is a class template
template <typename T>
class food {
private:
T label;
T color;
public:
food (T label, T color); // constructor
void dump();
};
//template <typename T>
//food<T>::food(T label, T color):label(label),color(color){}
template <typename T>
food<T> :: food(T _label, T _color) {
label = _label;
color = _color;
}
template <typename T>
void food<T>::dump()
{
LOG(INFO) << "Dump: label = " << label << "; color = " << color;
return;
}
// end class template
int
main (int argc, char **argv) {
// Log messeages into stderr
FLAGS_logtostderr = 1;
// init
google::InitGoogleLogging (argv[0]);
LOG(INFO) << "testing function template";
LOG(INFO) << abs(-1.7);
LOG(INFO) << abs(1.1);
LOG(INFO) << abs(-10);
LOG(INFO) << abs('a');
LOG(INFO) << "function template test done.";
LOG(INFO) << "";
LOG(INFO) << "testing class template";
food<int> apple (1, 2);
apple.dump();
food<std::string> banana ("ba", "yell");
banana.dump();
LOG(INFO) << "class template test done.";
return 0;
}