Define template struct S { T val: };. Add a constructor. so that you can initial
ID: 3852269 • Letter: D
Question
Define template struct S { T val: };. Add a constructor. so that you can initialize with a T. Define variables of types S , S , S , S , and S : initialize them with values of your choice. Read those values and print them. Add a function template get () that returns a reference lo val. Put the definition of get() outside the class. Make val private. Do 4 again using gel(). Add a set() function template so that you can change val. Replace set() with an S :: operator = (const T&). Provide const and non-const versions of get(). Define a function template read_val(T& v) that reads from cin into v. Use read_val() to read into each of the variables from 3 except the S variable.Explanation / Answer
here is the code as per your reqirements
# fndef Templates_Header_h
#define Templates_Header_h
#include <iostream>
using namespace std;
template <typename T>
struct S
{
private:
T val;
public:
S() : val{} {};
T* get();
T* get() const;
T set();
T operator=(const T&);
T read_val(T& v);
void print_val() const;
};
template<typename T>
void S<T>::print_val() const
{
cout << "Value is: " << val << endl;
}
template<typename T>
T* S<T>::get()
{
T* p = &val;
return p;
}
template<typename T>
T S<T>::set()
{
T newvalue;
cout << "Type new value: ";
cin >> newvalue;
S<T>::val = newvalue;
return S<T>::val;
}
template<typename T>
T S<T>::operator=(const T&)
{
T val1;
val1 = val;
return *this;
}
template<typename T>
T* S<T>::get() const
{
T* p = &val;
return p;
}
template<typename T>
T S<T>::read_val(T &v)
{
cout << "Type Value: ";
cin >> v;
return S<T>::val = v;
}
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.