Question: Modify the FeetInches class so it overloads the following operators: = != Demonstrate the classs capabilities in a simple program (this program must have a

Modify the FeetInches class so it overloads the following operators:

<=

>=

!=

Demonstrate the class’s capabilities in a simple program (this program must have a main file)

Code to be modified below:

//header

#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

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

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

//main

#include <iostream>
#include "FeetInches.h"
using namespace std;

int main()
{
int count; // Loop counter

// Define a FeetInches object with the default
// value of 0 feet, 0 inches.
FeetInches first;

// Define a FeetInches object with 1 foot 5 inches.
FeetInches second(1, 5);

// Use the prefix ++ operator.
cout << "Demonstrating prefix ++ operator.";
for (count = 0; count < 12; count++)
{
first = ++second;
cout << "first: " << first.getFeet() << " feet, ";
cout << first.getInches() << " inches. ";
cout << "second: " << second.getFeet() << " feet, ";
cout << second.getInches() << " inches.";
}

// Use the postfix ++ operator.
cout << "Demonstrating postfix ++ operator.";
for (count = 0; count < 12; count++)
{
first = second++;
cout << "first: " << first.getFeet() << " feet, ";
cout << first.getInches() << " inches. ";
cout << "second: " << second.getFeet() << " feet, ";
cout << second.getInches() << " inches.";
}

return 0;
}

Step by Step Solution

3.43 Rating (150 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To modify the FeetInches class to overload the operators and and to demonstrate the classs capabilities follow these steps Step 1 Overloading Operator... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mechanical Engineering Questions!