Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

FeetInchesh ifndef FEETINCHESH define FEETINCHESH The FeetInches class holds distance... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Numerical Methods For Engineers

Authors: Steven C. Chapra, Raymond P. Canale

5th Edition

978-0071244299, 0071244298

More Books

Students also viewed these Mechanical Engineering questions

Question

Why did it have to change its original operating model?

Answered: 1 week ago

Question

Use the shooting method to solve Prob. 27.1.

Answered: 1 week ago

Question

31. How does Antabuse workpg99

Answered: 1 week ago