Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – Feet
ID: 3812647 • Letter: T
Question
Template Exercise (50 pts)
Modified FeetInches Specification file (30pts) – FeetInches.h
Driver program with function template (20 pts) – TempDriver.cpp
Template Exercise
Write template for two functions called minimum and maximum. Each function should accept two arguments and return the lesser or greater of the two values.
Test these templates in a driver program. The template with the following types: int, double, string and FeetInches (which is an object).
You will need:
The FeetInches class (which was provided in Week 5 – Example 2: it is on page 4 of the file).
Make changes to the code so that it does the following:
Replace the + and – overloaded functions with < and > overloaded functions instead. The definition of those functions should take the following form:
//Use the function header syntax below
bool FeetInches::operator > (const FeetInches &right)
{
// change to proper code
create boolean variable
if feet is greater than right.feet
Set variable to true
otherwise if feet is equal to right.feet and inches is
greater than right.inches)
Set variable to true
otherwise
Set variable to false
return Boolean variable
}
//Repeat and modify for < operator
Add the overloaded functions for >> and << to accept Feet and inches as feet, inches.
Examples of overloaded functions were included in Assignment 3.
You may not have to modify the >> operator definition, unless you want to make your input more sophisticated:
(Example: You may want to input in ”coordinate form” like (3,7) or you may want the user to type “3 feet, 7 inches”.)
The << operator should be modified to output the comma, the word feet or inches as needed
You can omit the simplify function for this exercise.
Two template functions (these can be created in your driver program).
Variables to store ints, doubles, strings.
Objects to store FeetInches values .
A sample of the output is shown below:
Enter two integers: 1 2
The minimum of 1 and 2 is: 1
The maximum of 1 and 2 is: 2
Enter two floating point numbers: 7.8 4.3
The minimum of 7.8 and 4.3 is: 4.3
The maximum of 7.8 and 4.3 is: 7.8
Enter the first string: Hello
Enter the second string: Hullo
The minimum of Hello and Hullo is: Hello
The maximum of Hello and Hullo is: Hullo
Enter the first distance (in feet, inches format): 3, 7
Enter the second distance (in feet, inches format): 4, 9
The minimum of 3 feet , 7 inches and 4 feet , 9 inches is: 3 feet , 7 inches
The minimum of 3 feet , 7 inches and 4 feet , 9 inches is: 4 feet , 9 inches
----------------------------------------------------------------------------------------------------------------------------------------
#ifndef FEETINCHES_H
#define FEETINCHES_H
// The FeetInches class holds distances or measurements
// expressed in feet and inches.
class FeetInches
{
private: int feet; // To hold a number of feet int inches; // To hold a number of inches void simplify(); // Defined in FeetInches.cpppublic: // Constructor FeetInches(int f = 0, int i = 0) { feet = f; inches = i; simplify(); } // Mutator functions void setFeet(int f) { feet = f; } void setInches(int i) { inches = i; simplify(); } // Accessor functions int getFeet() const { return feet; } int getInches() const { return inches; } // Overloaded operator functions FeetInches operator + (const FeetInches &); // Overloaded + FeetInches operator - (const FeetInches &); // Overloaded -};#endif
Explanation / Answer
Here are the functions for FeetInches.h:
//Use the function header syntax below
bool FeetInches::operator > (const FeetInches &right)
{
// change to proper code
//create boolean variable
bool isGreater;
//if feet is greater than right.feet
if(feet > right.getFeet())
//Set variable to true
isGreater = true;
//otherwise if feet is equal to right.feet and inches is greater than right.inches)
else if(feet == right.feet && inches > right.getInches())
//Set variable to true
isGreater = true;
//otherwise
else
//Set variable to false
isGreater = false;
//return Boolean variable
return isGreater;
}
////Repeat and modify for < operator
bool FeetInches::operator < (const FeetInches &right)
{
// change to proper code
//create boolean variable
bool isLesser;
//if feet is lesser than right.feet
if(feet < right.getFeet())
//Set variable to true
isLesser = true;
//otherwise if feet is equal to right.feet and inches is lesser than right.inches)
else if(feet == right.feet && inches < right.getInches())
//Set variable to true
isLesser = true;
//otherwise
else
//Set variable to false
isLesser = false;
//return Boolean variable
return isLesser;
}
//Add the overloaded functions for >> and << to accept Feet and inches as feet, inches.
ostream &operator<<( ostream &output, FeetInches &right )
{
output<< "(" << right.getFeet() << ", " << right.getInches() << ")";
return output;
}
istream &operator >> (istream &strm, FeetInches &right)
{
int feet, inches;
cout <<"Enter the feet: ";
strm >> feet;
cout << "Enter the inches: ";
strm >> inches;
return strm;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.