Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ - Modify the FeetInches class, discussed in lecture, so that it overloads th

ID: 3706509 • Letter: C

Question

C++

- Modify the FeetInches class, discussed in lecture, so that it overloads the following

operators:
<=

>=
!=
* (multiplication)

- Demonstrate the class’s capabilities in a simple program.

FeetInches.cpp:

// Implementation file for the FeetInches class
#include        // Needed for abs()
#include "FeetInches.h"

//************************************************************
// Definition of member function simplify. This function     *
// checks for values in the inches member greater than       *
// twelve or less than zero. If such a value is found,       *
// the numbers in feet and inches are adjusted to conform    *
// to a standard feet & inches expression. For example,      *
// 3 feet 14 inches would be adjusted to 4 feet 2 inches and *
// 5 feet -2 inches would be adjusted to 4 feet 10 inches.   *
//************************************************************

void FeetInches::simplify()
{
   if (inches >= 12)
   {
      feet += (inches / 12);
      inches = inches % 12;
   }
   else if (inches < 0)
   {
      feet -= ((abs(inches) / 12) + 1);
      inches = 12 - (abs(inches) % 12);
   }
}

//**********************************************
// Overloaded binary + operator.               *
//**********************************************

FeetInches FeetInches::operator + (const FeetInches &right)
{
   FeetInches temp;

   temp.inches = inches + right.inches;
   temp.feet = feet + right.feet;
   temp.simplify();
   return temp;
}

//**********************************************
// Overloaded binary - operator.               *
//**********************************************

FeetInches FeetInches::operator - (const FeetInches &right)
{
   FeetInches temp;

   temp.inches = inches - right.inches;
   temp.feet = feet - right.feet;
   temp.simplify();  
   return temp;
}

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

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 -
bool operator<=(const FeetInches &);
bool operator>=(const FeetInches &);
bool operator!=(const FeetInches &);
FeetInches operator * (const FeetInches &);

};
#endif

FeetInches.cpp

//overload >=
bool FeetInches::operator>=(const FeetInches &right)
{
if(inches>=right.inches && feet>=right.feet)
return true;
else
return false;
}
//overload <=
bool FeetInches::operator<=(const FeetInches &right)
{
if(inches<=right.inches && feet<=right.feet)
return true;
else
return false;
}
//overload !=
bool FeetInches::operator!=(const FeetInches &right)
{
if(inches!=right.inches && feet!=right.feet)
return true;
else
return false;
}

//overload * operator
FeetInches FeetInches::operator * (const FeetInches &right)
{
FeetInches temp;
temp.inches = inches * right.inches;
temp.feet = feet * right.feet;
temp.simplify();
return temp;
}

//**********************************************
// Overloaded binary + operator. *
//**********************************************

FeetInches FeetInches::operator + (const FeetInches &right)
{
FeetInches temp;

temp.inches = inches + right.inches;
temp.feet = feet + right.feet;
temp.simplify();
return temp;
}

//**********************************************
// Overloaded binary - operator. *
//**********************************************

FeetInches FeetInches::operator - (const FeetInches &right)
{
FeetInches temp;

temp.inches = inches - right.inches;
temp.feet = feet - right.feet;
temp.simplify();
return temp;
}

void FeetInches::simplify()
{
if (inches >= 12)
{
feet += (inches / 12);
inches = inches % 12;
}
else if (inches < 0)
{
feet -= ((abs(inches) / 12) + 1);
inches = 12 - (abs(inches) % 12);
}
}