-
Notifications
You must be signed in to change notification settings - Fork 7
/
Drill 10
78 lines (60 loc) · 1.7 KB
/
Drill 10
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
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
// Chapter 18 - Drill 10
/*
Replace set() with an S<T>::operator=(const T&). Hint: Much simpler than Section 19.2.5.
*/
// https://github.com/l-paz91/principles-practice/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
template<typename T>
struct S
{
// ---- constructors
S() {}
S(T t)
: val(t)
{}
// ---- methods
S& operator=(const T& newValue) { val = newValue; return *this; }
istream operator>>(T t) { cin >> val; }
void set(T t) { val = t; }
const T& get() const { return val; }
private:
// ---- variables
T val;
};
// -----------------------------------------------------------------------------
template<typename T>
const T& get(S<T>& s)
{
return s.get();
}
// -----------------------------------------------------------------------------
int main()
{
S<int> si(101);
si = 10;
cout << get(si) << endl;
S<char> sc('z');
sc = 'a';
cout << get(sc) << endl;
S<double> sd(10.4f);
sd = 83.3f;
cout << get(sd) << endl;
S<string> ss("A String.");
ss = "another string but this one is longer";
cout << get(ss) << endl;
S<vector<int>> svi(vector<int>(10, 100));
svi = vector<int>(10, 15);
for (int i : get(svi))
cout << i << ", ";
cout << endl;
keep_window_open();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------