// Chapter 14, Programming Challenge 9: FeetInches Modification
#include <iostream>
#include "FeetInches.h"
usingnamespace std;
int main()
{
// Create three FeetInches objects.
FeetInches one, two;
// Get a distance for the first object.
cout << "Enter a distance in feet and inches: " << endl;
cin >> one;
// Get a distance for the second object.
cout << "Enter another distance in feet and inches: " << endl;
cin >> two;
// Test the != operator.
if (one != two)
cout << "The two are not equal.\n";
// Test the >= operator.
if (one >= two)
cout << one << " is >= " << two << endl;
// Test the <= operator.
if (one <= two)
cout << one << " is <= " << two << endl;
return 0;
}
// Specification file for the FeetInches class
#ifndef FEETINCHES_H
#define FEETINCHES_H
#include <iostream>
usingnamespace std;
class FeetInches; // Forward Declaration
// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const FeetInches &);
istream &operator >> (istream &, FeetInches &);
// 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 &);
FeetInches operator - (const FeetInches &);
FeetInches operator ++ (); // Prefix ++
FeetInches operator ++ (int); // Postfix ++
booloperator > (const FeetInches &);
booloperator < (const FeetInches &);
booloperator == (const FeetInches &);
// Conversion functions
operatordouble();
operatorint();
// Friends
friend ostream &operator << (ostream &, const FeetInches &);
friend istream &operator >> (istream &, FeetInches &);
// New operators
// >=
booloperator >=(const FeetInches &);
// <=
booloperator <=(const FeetInches &);
// !=
booloperator !=(const FeetInches &);
};
#endif
simplify() - adjusts number entered into as inches that are equal or higher than 12 or below 0 i.e 5 ft 13 inches == 6 ft 1
I get what the acessors and setters do as well as the constructors.
Overloading the >, <, == compares the two, and overloading istream and ostream is obvious. But what do the others do, im unsure. Why would the >=, <=, matter?
It does look like you have to implement the "new" member functions.
Think this way: the FeetInches is a number just like int. How do ints behave with relational operators? Make FeetInches behave similarly.
One does not actually need more than the relational operators < and == but if one must, one can implement the others with those two. For example, greater-or-equal should be true when less is not true.
Yes this is one of a few assignments I have to complete. This one is just confusing me. I must write the code for these new functions.
I get for the most part what this program is trying to achieve but the relational operator functions are confusing me. A FeetInches length cannot be > and >=, right?
Also what would the overloaded operator functions even do? +,-, ++
Why not? 5 is > and >= to 4 isn't it? Apply the same logic to FeetInches.
Also what would the overloaded operator functions even do? +,-, ++
Think about how you would use these operators normally.
+ should add two FeetInches (example: 3 ft. 4 inches + 7 ft. 2 inches)
- should do something similar accept with subtraction
++ should increment a the value, most like by inches (3 ft. 2 inches becomes 3 ft. 3 inches)