Experiments Step 1: In this experiment you will investigate a program that conta
ID: 3814231 • Letter: E
Question
Experiments
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 swap1(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void swap1(string &a, string &b)
{
string temp = a;
a = b;
b = temp;
}
void swap1(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;
swap1(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;
swap1(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;
swap1(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 swap1 functions in Step 1:
Explanation / Answer
Question 1:
Output:
Explaination :
The program has used three overloading functions swap1,swap2 and swap3 to swap two integers,swap two strings and swap two characters .
Overloading functions work according the type of parameters passed. So for strings it will take temp variable of type string and use it to swap the strings passed .
Question 2:
Similarities:
Differences
Question 3:
template <typename a_type,typename b_type>
char Tester3(a_type x,b_type y, b_type w,b_type m);
Question 4:
#include <iostream>
#include <string>
using namespace std;
template <typename T>
void Swap(T &a, T &b)
{
T temp;
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(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(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(q,r);
cout<<"q after swap called = "<<q<<" and r after swap called = "<<r<<endl<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.