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
Step: 1
FeetInchesh ifndef FEETINCHESH define FEETINCHESH The FeetInches class holds distance...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started