Create a new file (in Dev C++) and save it as lab11_XYZ.cpp (replace XYZ with your initials).Create ANOTHER new file (in Dev C++) and save it as swap_XYZ.h (replace XYZ with your initials).Use the swap_XYZ.h header file to define a set of functions for swapping two items of the same type.Given the following prototypes, define (overloaded) swap functionsvoid swap(int&, int&);  //swap two integersvoid swap(double&, double&);  //swap two doublesvoid swap(float&, float&);  //swap two floatsvoid swap(char&, char&);  //swap two charactersvoid swap(string&, string&);  //swap two stringsThe header file should contain the following:header comments (similar to normal program header comments)function definitions (for you to do, based on provided prototypes above...)In general, a swap works like this:void swap(type first&, type second&){  type temp = first;  first = second;  second = temp;}Once your header file is complete, make sure to include it in your main CPP program. Use double quotes instead of angle brackets. Make sure the header file (H file) is located in the same folder as the CPP file.#include <iostream>#include "swap_XYZ.h"using namespace std;Write a driver program to test your header file. The program should test each version of the swap function and display the results.The program should:display a hello message consider using displayMessage()!for each version of swap(),provide or prompt for two items consider using prompt()!display which version is being calleddisplay values before swapcall swap functiondisplay values after swapdisplay a goodbye message use displayMessage() again!Language is C++Please make it as simple as possible!

Question
Asked Nov 6, 2019
200 views

Create a new file (in Dev C++) and save it as lab11_XYZ.cpp (replace XYZ with your initials).

Create ANOTHER new file (in Dev C++) and save it as swap_XYZ.h (replace XYZ with your initials).

Use the swap_XYZ.h header file to define a set of functions for swapping two items of the same type.

Given the following prototypes, define (overloaded) swap functions

  • void swap(int&, int&);  //swap two integers
  • void swap(double&, double&);  //swap two doubles
  • void swap(float&, float&);  //swap two floats
  • void swap(char&, char&);  //swap two characters
  • void swap(string&, string&);  //swap two strings

The header file should contain the following:

  • header comments (similar to normal program header comments)
  • function definitions (for you to do, based on provided prototypes above...)

In general, a swap works like this:

void swap(type first&, type second&){  type temp = first;  first = second;  second = temp;

}

Once your header file is complete, make sure to include it in your main CPP program. Use double quotes instead of angle brackets. Make sure the header file (H file) is located in the same folder as the CPP file.

#include <iostream>#include "swap_XYZ.h"

using namespace std;

Write a driver program to test your header file. The program should test each version of the swap function and display the results.

The program should:

  • display a hello message consider using displayMessage()!
  • for each version of swap(),
    • provide or prompt for two items consider using prompt()!
    • display which version is being called
    • display values before swap
    • call swap function
    • display values after swap
  • display a goodbye message use displayMessage() again!

Language is C++

Please make it as simple as possible! 

check_circle

Expert Answer

Step 1

Program:

Filename: lab11_XYZ.cpp

// header files

 

#include <iostream>

 

#include "swap_XYZ.h"

 

using namespace std;

 

const string HELLO = "Hello!Welcome to the program... ";

 

const string GOODBYE = " Goodbye!exiting the program...";

 

// prototypes

 

void displayMessage(string);

 

int main(){

 

     displayMessage(HELLO);

 

   

 

     int a = 3, b = 6;

 

     cout<<"Swap version: swap(int& a, int& b)"<<endl;

 

     cout << "Before swapping." << endl;

 

    cout << "a = " << a << ", b = " << b << endl;

 

    swap(a,b);

 

    cout << "After swapping." << endl;

 

    cout << "a = " << a << ", b = " << b << endl;

 

  

 

    double c = 2.3, d = 5.369;

 

     cout<<" Swap version: swap(double&, double&)"<<endl;

 

     cout << "Before swapping." << endl;

 

    cout << "c = " << c << ", d = " << d << endl;

 

    swap(c,d);

 

    cout << "After swapping." << endl;

 

    cout << "c = " << c << ", d = " << d << endl;

 

  

 

    float e = 8.2, f = 3.4;

 

     cout<<" Swap version: swap(float&, float&)"<<endl;

 

     cout << "Before swapping." << endl;

 

    cout << "e = " << e << ", f = " << f << endl;

 

    swap(e,f);

 

    cout << "After swapping." << endl;

 

    cout << "e = " << e << ", f = " << f << endl;

 

  

 

    char g = 'r', h = 'e';

 

     cout<<" Swap version: swap(char&, char&)"<<endl;

 

     cout << "Before swapping." << endl;

 

    cout << "g = " << g << ", h = " << h << endl;

 

    swap(g,h);

 

    cout << "After swapping." << endl;

 

    cout << "g = " << g << ", h = " << h << endl;

 

  

 

    string i = "hi", j = "hello";

 

     cout<<" Swap version: swap(string& a, string& b)"<<endl;

 

     cout << "Before swapping." << endl;

 

    cout << "i = " << i << ", j = " << j << endl;

 

    swap(i,j);

 

    cout << "After swapping." << endl;

 

    cout << "i = " << i << ", j = " << j << endl;

 

  

 

    displayMessage(GOODBYE);

 

}

 

void displayMessage(string msg){

 

     cout<<msg<<endl;

 

}

Step 2

Filename: swap_XYZ.h

// this header file implements the various versions of swap function

 

// header file

 

#include<iostream>

 

#include<string> // for string class

 

using namespace std;

 

//prototypes

 

void swap(int&, int&);  //swap two integers

 

void swap(double&, double&);  //swap two doubles

 

void swap(float&, float&);  //swap two floats

 

void swap(char&, char&);  //swap two characters

 

void swap(string&, string&);  //swap two strings

 

 

 

void swap(int& a, int& b){

 

     int temp;

 

    temp = a;

 

    a = b;

 

    b = temp;

 

}

 

void swap(double& a, double& b){

 

     double temp;

 

    temp = a;

 

    a = b;

 

    b = temp;

 

}

 

void swap(...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Create class SavingsAccount. Use a static variable annualInterestRate to store the annual interest r...

A: The following source code will perform the functions as specified in the question.

question_answer

Q: Problem 1 consists of multiple parts. You should finish one part and ensure that it works before mov...

A: Hello. Since your question has multiple sub-parts, we will solve first three sub-parts for you. If y...

question_answer

Q: An uppercase character is a character whose ASCII value is between 65(A) and 90(Z). Write a C++ prog...

A: Following is the c++ program:For loop is used to from 0 to length of string to read every character ...

question_answer

Q: I have to code the game mastermind, but i only got halfway. I got the user input, 8 colors and the 3...

A:  solving the duplication problem:Create a temporary array named "temp" and initiate that array with ...

question_answer

Q: Explain pipelining and think of a real-world analogy to the concept as if you were explaining it to ...

A: Pipelining:Pipelining is the technique of dividing a sequential process into sub-processes, each sub...

question_answer

Q: I have written a C++ class called 'Birthday' and I want to write a method that can compare with anot...

A: Programming instructions:First, create the constructor to initialize the variables with the initial ...

question_answer

Q: What was Networking?

A: Networking:Networking, also known as computer networking, is the practice of transporting and exchan...

question_answer

Q: What does it mean to choose numbers at random

A: RandomRandom is the word in which express that some item, number, quantity, etc. are chosen from the...

question_answer

Q: A matrix named mach contains three columns of data concerning the energy output of several machines....

A: JAVA programClass name is Matrix. It contains function named Transfer.Create a matrix named mach of ...