In these exercises I am using Visual Studio 2017 and the std_lib_facilities header provided by Stroustrup.
Define template<typename T> struct S { T val; };.
.
Chapter 19 // Drill 2
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%202
Add a constructor, so that you can initialise with a T.
.
Chapter 19 // Drill 3
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%203
Define variables of types S<int>, S<char>, S<double>, S<string>, and S<vector<int>>; initialise them with values of your choice.
.
Chapter 19 // Drill 4
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%204
Read those values and print them.
I assume he meant read them in using cin?
Chapter 19 // Drill 5
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%205
Add a function template get() that returns a reference to val.
.
Chapter 19 // Drill 6
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%206
Put the definition of get() outside the class.
.
Chapter 19 // Drill 7
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%207
Make val private.
.
Chapter 19 // Drill 8
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%207
Do 4 again using get().
I guess he didn't mean to use cin earlier...literally just print them? Why use read? See code for drill 7 as this does the same thing.
Chapter 19 // Drill 9
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%209
Add a set() function template so that you can change val.
.
Chapter 19 // Drill 10
Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%2010
Replace set() with an S<T>::operator=(const T&). Hint: Much simpler than Section 19.2.5.
.
Chapter 19 // Drill 11
Provide const and non-const versions of get().
.
Chapter 19 // Drill 12
Define a function template<typename T> read_val(T& v) that reads from cin into v.
Ok, so he definitely didn't mean to read in values in drill 4. I provided the operator overload version in 4 but I implemented the function here as well.
Chapter 19 // Drill 13
Use read_val() to read into each of the variables from 3 except the S<vector<int>> variable.
Github: .
See drill 12 as I did that there...I also already read into the vector...
No comments:
Post a Comment