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

#include <iostream> #include <string> using namespace std; void swap(string &a,

ID: 639150 • Letter: #

Question

#include <iostream>

#include <string>

using namespace std;

void swap(string &a, string &b)

{

      string temp = a;

      a=b;

      b=temp;

}

void swap(int &a, int &b)

{

      int temp = a;

      a=b;

      b=temp;

}

void swap(char &a, char &b)

{

      char temp = a;

      a=b;

      b=temp;

}

int main()

{

      string x="111", y="222";

      char r='1', s='2';     

      int a=111, b=222;

      cout<<"original strings: x = "<<x<<" y = "<<y<<endl;

      cout<<"original integers: a = "<<a<<" b = "<<b<<endl;

      cout<<"original characters: r = "<<r<<" s = "<<s<<endl;

      swap(x,y);

      swap(r,s);

      swap(a,b);

      cout<<"swap with strings: x = "<<x<<" y = "<<y<<endl;

      cout<<"swap with integers: a = "<<a<<" b = "<<b<<endl;

      cout<<"swap with characters: r = "<<r<<" s = "<<s<<endl;

      return 0;

}

Question 1:Please execute the program in Step 1 and explain how function overloading was used?

Question 2:Without changing the original program, would the following function cause an error if added in the source file that contains the program in Step 1? If so, list the error message(s), and explain why the error(s) occurred.

double swap(string &a, string &b)

{

      string temp = a;

      a=b;

      b=temp;

      return 0;

}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
void swap(string &a, string &b)
{
      string temp = a;
      a=b;
      b=temp;
}
void swap(int &a, int &b)
{
      int temp = a;
      a=b;
      b=temp;
}
void swap(char &a, char &b)
{
      char temp = a;
      a=b;
      b=temp;
}
int main()
{
      string x="111", y="222";
      char r='1', s='2';   
      int a=111, b=222;
      cout<<"original strings: x = "<<x<<" y = "<<y<<endl;
      cout<<"original integers: a = "<<a<<" b = "<<b<<endl;
      cout<<"original characters: r = "<<r<<" s = "<<s<<endl;
      swap(x,y);
      swap(r,s);
      swap(a,b);
      cout<<"swap with strings: x = "<<x<<" y = "<<y<<endl;
      cout<<"swap with integers: a = "<<a<<" b = "<<b<<endl;
      cout<<"swap with characters: r = "<<r<<" s = "<<s<<endl;
      return 0;
}
Question 1:Please execute the program in Step 1 and explain how function overloading was used?
when swap function called with integers function swap(int& a,int& b) is used.
when swap function called with char function swap(char& a,char& b) is used.
when swap function called with strings function swap(string& a,string& b) is used.

Question 2:Without changing the original program, would the following function cause an error if added in the source file that contains the program in Step 1?
If so, list the error message(s), and explain why the error(s) occurred.
double swap(string &a, string &b)
{
      string temp = a;
      a=b;
      b=temp;
      return 0;
}

Yes. it throws compilations error as follows.
In function `double swap(std::string&, std::string&)':
error: new declaration `double swap(std::string&, std::string&)'
error: ambiguates old declaration `void swap(std::string&, std::string&)'

because there two function with same signature but different return types. compiler gets confused between these.
compiler interpret correly only if function signature of different with different arguments.