Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer this using c++ \"Template drill\" 1. Define a template<typename T> struct

ID: 3734526 • Letter: A

Question

Answer this using c++

"Template drill"

1. Define a template<typename T> struct S{T val}

2. Add a constructr, so that you can initialize with a T

3. Define variables of types S<int>, S<char>, S<double>, S<string>, S<vector>. Initialize them with values of your choice.

4. Read those values and print them

5. Add a function template get() that returns a reference to val

6. Put the definiton of get() outside the class

7. Make val private

8. Do 4 again using get()

9. Add a set() function template so that you can change val

I do not understand how to write the code for this question.

Explanation / Answer

Below is your code Till point 4..

#include<iostream>

#include<cstdlib>

#include<vector>

using namespace std;

//Initilization of Structure

template<typename T>

struct S{

//variable declaration

T val;

//constructor

S(T vl) :val(vl) {

}

};

//Main method

int main() {

//Variables holding entered values

int intV;

char charV;

double doubleV;

string strV;

//GEtting integer from user

cout<<"Enter an integer : ";

cin>>intV;

//Initilizing struct S with int

S<int> intVar(intV);

//Getting character from user

cout<<"Enter a character : ";

cin>>charV;

//Initializing with char

S<char> charVar(charV);

//Getting double from user

cout<<"Enter a double : ";

cin>>doubleV;

//Initializing with double

S<double> doubleVar(doubleV);

//Getting string from user

cout<<"Enter a string : ";

cin>>strV;

//Initializing with string

S<string> stringVar(strV);

//Getting vector from user

vector<int> vect;

int tempV;

cout<<"Enter 3 numbers for vectors : ";

for(int i = 1; i<= 3; i++) {

cout<<" Enter number #"<<i<<" : ";

cin>>tempV;

vect.push_back(tempV);

}

//Initializing with vector

S<vector<int>> vectVar(vect);

//printing the variables using public val

cout<<" Below are your entered values: ";

cout<<" Integer entered : "<<intVar.val;

cout<<" Char entered : "<<charVar.val;

cout<<" Double entered : "<<doubleVar.val;

cout<<" String entered : "<<stringVar.val;

//printing content of Vector

cout<<" Vector contents : ";

for (auto i = vectVar.val.begin(); i != vectVar.val.end(); ++i)

cout << *i << ' ';

}

Output

Enter an integer : 23
Enter a character : Y
Enter a double : 109.34
Enter a string : "Never
Enter 3 numbers for vectors :
Enter number #1 : 1

Enter number #2 : 2

Enter number #3 : 3


Below are your entered values:

Integer entered : 23
Char entered : Y
Double entered : 109.34
String entered : "Never
Vector contents : 1 2 3

Code from point 5 to point 9

#include<iostream>

#include<cstdlib>

#include<vector>

using namespace std;

//Initilization of Structure

template<typename T>

struct S{

//constructor

S(T vl) :val(vl) {

}

S() {

}

//declaration of get() method

T get();

//declaration of set() method

void set(T vl);

//variable declaration

private: T val;

};

//method declaration of get method

template<typename T>

T S<T>::get() {

return val;

}

//method declaration of set method

template<typename T>

void S<T>::set(T vl) {

val = vl;

}

//Main method

int main() {

//Variables holding entered values

int intV;

char charV;

double doubleV;

string strV;

//GEtting integer from user

cout<<"Enter an integer : ";

cin>>intV;

//Initilizing struct S with int

S<int> intVar;

//setting value of variable

intVar.set(intV);

//Getting character from user

cout<<"Enter a character : ";

cin>>charV;

//Initializing with char

S<char> charVar;

//setting value of variable

charVar.set(charV);

//Getting double from user

cout<<"Enter a double : ";

cin>>doubleV;

//Initializing with double

S<double> doubleVar;

//setting value of variable

doubleVar.set(doubleV);

//Getting string from user

cout<<"Enter a string : ";

cin>>strV;

//Initializing with string

S<string> stringVar;

//setting value of variable

stringVar.set(strV);

//Getting vector from user

vector<int> vect;

int tempV;

cout<<"Enter 3 numbers for vectors : ";

for(int i = 1; i<= 3; i++) {

cout<<" Enter number #"<<i<<" : ";

cin>>tempV;

vect.push_back(tempV);

}

//Initializing with vector

S<vector<int>> vectVar;

//setting value of variable

vectVar.set(vect);

//printing the variables using private val and get() method

cout<<" Below are your entered values: ";

cout<<" Integer entered : "<<intVar.get();

cout<<" Char entered : "<<charVar.get();

cout<<" Double entered : "<<doubleVar.get();

cout<<" String entered : "<<stringVar.get();

//printing content of Vector

cout<<" Vector contents : ";

vector<int> vectToPrint = vectVar.get();

for (auto i = vectToPrint.begin(); i != vectToPrint.end(); ++i)

cout << *i << ' ';

}

Output

Enter an integer : 12
Enter a character : N
Enter a double : 12.233
Enter a string : NeverSayNever
Enter 3 numbers for vectors :
Enter number #1 : 1

Enter number #2 : 2

Enter number #3 : 3


Below are your entered values:

Integer entered : 12
Char entered : N
Double entered : 12.233
String entered : NeverSayNever
Vector contents : 1 2 3

I have added comments and made sure all the points you asked are incorporated. Let me know in comments if you still have any issues.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote