Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – Feet
ID: 3812802 • Letter: T
Question
Template Exercise (50 pts)
Modified FeetInches Specification file (30pts) – FeetInches.h
Driver program with function template (20 pts) – TempDriver.cpp
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
_________________________________________________________
Here is the code for FeetInches.h
#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.cpp
public:
// 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
//implementation
//feetInches.h
//Here is the code for FeetInches.h
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
#ifndef FEETINCHES_H
#define FEETINCHES_H
// The FeetInches class holds distances or measurements
// expressed in feet and inches.
template<typename T>
class FeetInches
{
private:
T feet; // To hold a number of feet
T inches; // To hold a number of inches
void simplify()// Defined in FeetInches.cpp
{
}
public:
// Constructor
FeetInches(T f = 0, T i = 0)
{
feet = f;
inches = i;
simplify();
}
// Mutator functions
void setFeet(T f)
{
feet = f;
}
void setInches(T i)
{
inches = i;
simplify();
}
// Accessor functions
int getFeet() const
{
return feet;
}
int getInches() const
{
return inches;
}
// Overloaded operator functions
FeetInches operator + (const FeetInches &obj)// Overloaded +
{
FeetInches tmp;
tmp.getFeet = this->getFeet() + obj.getFeet();
tmp.getInches = this->getInches() + obj.getInches();
return tmp;
}
FeetInches operator - (const FeetInches &obj)// Overloaded -
{
FeetInches tmp;
tmp.getFeet = getFeet() - obj.getFeet();
tmp.getInches = getInches() - obj.getInches();
return tmp;
}
bool FeetInches::operator > (const FeetInches &right)
{
// change to proper code
bool b = false;
if (feet > right.getFeet())
b = true;
if (feet == right.getFeet())
{
if (inches > right.getInches())
b = true;
else
b = false;
}
return b;
}
bool FeetInches::operator < (const FeetInches &right)
{
// change to proper code
bool b = false;
if (feet < right.getFeet())
b = true;
if (feet == right.getFeet())
{
if (inches < right.getInches())
b = true;
else
b = false;
}
return b;
}
friend ifstream& operator>>(ifstream &in, FeetInches &obj)
{
T f, i;
in >> f;
in >> i;
obj.setFeet(f);
obj.setInches(i);
return in;
}
friend ofstream& operator<<(ostream &out, FeetInches &obj)
{
out << "Feet = " << obj.getFeet() << " Inches = " << obj.getInches() << endl;
return out;
}
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.