-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstraction.cpp
103 lines (84 loc) · 1.57 KB
/
abstraction.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
#include <iostream>
#include <functional>
#include <sstream>
class A {
int _value;
public:
A(int value) : _value(value) {}
void print()
{
std::cout << _value << std::endl;
}
};
class B {
float _value;
public:
B(float value) : _value(value) {}
void print()
{
std::cout << _value << std::endl;
}
};
class C {
bool _value;
public:
C(bool value) : _value(value) {}
void print()
{
std::cout << (_value ? "true" : "false") << std::endl;
}
};
template <typename T>
class D {
T _value;
public:
D(T value) : _value(value) {}
void print()
{
std::cout << _value << std::endl;
}
};
template <>
class D<bool>
{
bool _value;
public:
D(bool value) : _value(value) {}
void print()
{
std::cout << (_value ? "true" : "false") << std::endl;
}
};
template <typename T>
class E {
T _value;
public:
E(T value) : _value(value) {}
void print(std::function<std::string(T)> format)
{
std::cout << format(_value) << std::endl;
}
};
struct person {
std::string name;
int age;
};
int main(void)
{
//D<int> a(10);
//D<float> b(20.0f);
//D<bool> c(false);
//
//a.print();
//b.print();
//c.print();
E<bool> e(true);
e.print([](bool v) -> std::string{ return v ? "true" : "false"; });
E<person> f( { "Bill", 59 });
f.print([](person v) -> std::string{
std::stringstream ss;
ss << v.name << " is " << v.age << " years old";
return ss.str();
});
return 0;
}