Question

Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
}

Add a comment
Know the answer?
Add Answer to:
Template Exercise (50 pts) Modified FeetInches Specification file (30pts) – FeetInches.h Driver program with function template...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured...

    #include <iostream> #include <string> #include <stdio.h> using namespace std; /** The FeetInches class holds distances measured in feet and inches. */ class FeetInches { private: int feet; // The number of feet int inches; // The number of inches /** The simplify method adjusts the values in feet and inches to conform to a standard measurement. */ void simplify() { if (inches > 11) { feet = feet + (inches / 12); inches = inches % 12; } } /**...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • I'm just not sure how to tackle all the template class this header wants me to...

    I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way. #include <iostream> #include <cassert> using namespace std; #ifndef ARRAYLIST_H #define ARRAYLIST_H template<typename T> class arrayList { public:    arrayList(); //Constructor with default parameter. //Sets maxSize = 100 and length = 0 if no parameter...

  • Study the c++ code below and answer the question on templates after // A polymorphic swap...

    Study the c++ code below and answer the question on templates after // A polymorphic swap function for any type of object (an example) template<typename T> void MySwap( T& x, T& y) { T temp; temp = x; x = y; y = temp; } // A polymorphic class holding an [x,y] position of any type template<class T> class Position { public: Position(T x=0, T y=0) : x_(x), y_(y) {} friend std::ostream& operator<<(std::ostream& os, const Position& p) { return os...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • I need help implementing 3 function prototypes with using operator overloading in a class. I have...

    I need help implementing 3 function prototypes with using operator overloading in a class. I have bolded the section I need help with since the cpp file is bigger than I expected. Any comments throughout would be extremely helpful. Time.cpp file #include "Time.h" #include #include #include // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • Need This Code finished Please // Program Template for Cookie Calorie program // Author - //...

    Need This Code finished Please // Program Template for Cookie Calorie program // Author - // Header file for input output functions #include using namespace std; // main function - // where the execution of program begins int main() { // Variable declarations const int COOKIES_IN_BAG = 40; const int SERVINGS_IN_BAG = 10; const int CALORIES_PER_SERVING = 300; int cookies_eaten; cout<<"How many cookies did you eat? "; cin >> cookies_eaten; // remaining code goes here return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT