Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab Exercise There are two parts to this lab exercise. In the first part, you create two function templates. In the second part, you create

Lab Exercise
There are two parts to this lab exercise. In the first part, you create two function templates. In the second part, you create a class template.
Part 1--Function Templates
Copy this file to your computer.
The file: swapper.cpp
swapper.cpp contains a "swapper" function that will swap two integer values
Your primary tasks for this exercise are:
Create a function template which will swap two values of any type.
Create a function template which will print any two values passed to it.
Steps include:
Convert the function "swapper" (from swapper.cpp) into a templated function.
Try and use it for swapping integers. Build and Run the executable.
Now, create another templated function which will print any two values passed to it. You can name it "printem".
Add a call to "printem" before and after the call to your swap routine (replacing the current "cout" statements). Build and Run the executable.
Add variable pairs of different types so that your program can handle
int
char
double
Include "swapper" and "printem" calls to ensure that these functions are working for these types as well. Build and Run the executable. Your output should look something like the following:
First Value is: 3 Second Value is: 48
AFTER SWAP
First Value is: 48 Second Value is: 3
First Value is: 5.5 Second Value is: 8.5
AFTER SWAP
First Value is: 8.5 Second Value is: 5.5
First Value is: a Second Value is: b
AFTER SWAP
First Value is: b Second Value is: a
Press any key to continue
Part 2--Class Templates
Download the following file.
The file: swapperclass.cpp
swapperclass.cpp contains a class and implementation for swapping two integer values
Your primary task for this exercise is:
Create a class template which will swap two values of any type and print them.
Steps include:
Implement your code in three files: swapperclass.cpp, swapperclass.h, main.cpp.
The purpose for this is to see the problem that occurs when using templates. See Section 6(below).
Your class should be capable of initializing two data members
Your class should have a function for swapping the two data members.
Your class should also have a function for printing the two data members.
Try different types so that your program can handle
int
char
double
Your output may look similar to the output from Part 1.
When you are finished, you should have:
A file named swapper.cpp that swaps three types of data, such as int, float, and char. Remember that one swap should work with the same datatype - don't swap an int and a char
swapperclass.cpp should be divided into three parts, swapperclass.cpp, swapperclass.h, and main.cpp. These files should execute a swap member function
Both outputs may look similar, since they are both going to be doing the same thing
An example output would look like this:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions