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

Step 1: In this experiment you will investigate a program that contains two func

ID: 3761792 • Letter: S

Question

Step 1: In this experiment you will investigate a program that contains two functions which are perfect candidates to be implemented as a function template. Enter, save, compile and execute the following program in MSVS. Call the new project “FunctionTemplateExp” and the program “FunctionTemplate.cpp”.    Answer the questions below:

#include <iostream>

#include <string>

using namespace std;

void swap_value(int &a, int &b)

{

       int temp = a;

       a = b;

       b = temp;

}

void swap_value(string &a, string &b)

{

       string temp = a;

       a = b;

       b = temp;

}

void swap_value(char &a, char&b)

{

       char temp = a;

       a = b;

       b = temp;

}

int main( )

{

       string x = "first", y = "second";

       int m = 10, n = 20;

       char q = 'Q', r = 'R';

       cout<<"x before swap called = "<<x<<" and y before swap called = "<<y<<endl;

       swap_value(x,y);

       cout<<"x after swap called = "<<x<<" and y after swap called = "<<y<<endl<<endl;

       cout<<"m before swap called = "<<m<<" and n before swap called = "<<n<<endl;

       swap_value(m,n);

       cout<<"m after swap called = "<<m<<" and n after swap called = "<<n<<endl<<endl;

       cout<<"q before swap called = "<<q<<" and r before swap called = "<<r<<endl;

       swap_value(q,r);

       cout<<"q after swap called = "<<q<<" and r after swap called = "<<r<<endl<<endl;

       return 0;

}

Question 1:Please explain the output of the program in Step 1?

Question 2:Please examine the three different swap functions in Step 1, and state any similarities and/or differences you observe?

Question 3:Write a function template declaration for a function with the following characteristics:

the function name is “Tester3”;

the function return type is character;

there are two template parameters, a_type and b_type;

there are four formal parameters: x (a_type), y (b_type), w (b_type) and m (b_type).

Question 4:Write a function template declaration replacing the three different swap functions in Step 1:

Explanation / Answer

1.

x before swap called = first and y before swap called = second
x after swap called = second and y after swap called = first

m before swap called = 10 and n before swap called = 20
m after swap called = 20 and n after swap called = 10

q before swap called = Q and r before swap called = R
q after swap called = R and r after swap called = Q


2. It's overloaded function. Same function name but parameters are different. each takes int, String and char types as parameters reapectively. All functions does the same functionality interchaning values. i.e; Swaping of values.

3.

template <a_type T1>
template <b_type T2>

char Tester3(T1 &x, T2 &y, T2 &w, T2 &m)
{
// code logic here...
return 'C'; // returns char
}

4.

template <typename T>
inline T Swap (T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}

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