Enter 4, 1 for the 1st entry, and 3,6 for the 2nd, will display the correct output.
But 4,1 and then 4,0, will say that the 1st entry is less than which is false.
I implemented the <= overload, in lines 55 of header & 48 of implementation.
header
#ifndef FEETINCHES_H
#define FEETINCHES_H
#include <iostream>
using namespace std;
// The FeetInches class holds distances or measurements
// expressed in feet and inches.
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: allows the feet and inches members to be set. Default values for these members is 0.
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 -
FeetInches operator ++ (); // Prefix ++
FeetInches operator ++ (int); // Postfix ++
bool operator > (const FeetInches &); // Overloaded>
bool operator < (const FeetInches &); // Overloaded <
bool operator == (const FeetInches &); // Overloaded ==
bool operator <= (const FeetInches &); //E
bool operator != (const FeetInches &);
//************************************************************************************************
//The function has 2 parameters: an ostream reference object and a const FeetInches reference object.
//ostream parameter will be a reference to the actual ostream object on the left side of rhe << operator.
//2nd parameter, is a reference to a FeetInches object. Will reference the object on the right side of the
//<< operator.
//************************************************************************************************
friend ostream &operator << (ostream &strm, const FeetInches &obj);
friend istream &operator >> (istream &strm, FeetInches &obj);
};
#endif
Implementation
// Implementation file for the Feetlnches class
#include <cstdlib> // Needed for abs()
#include "FeetInches.h"
//*****************************************************************
// 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() //simplify function is for normalizing the values held in feet and inches. This
{ //function adjusts any set of values where the inches member is > 12 or < 0
if (inches >= 12)
{
feet += (inches / 12);
inches = inches % 12;
}
else if (inches < 0)
{
feet -= ((abs(inches) / 12) + 1); //abs() function, requires that cstdlib be included.
inches = 12 - (abs(inches) % 12);
}
}
//******************************************
// Overloaded binary + operator.
//******************************************
FeetInches FeetInches::operator + (const FeetInches &right)
{
FeetInches temp; //this object is a temporary location for holding the results of the addition
temp.inches = inches + right.inches; //adds inches to right.inches and stores the result to temp.inches
temp.feet = feet + right.feet; //adds feet to right. feet and stores the result in temp, feet
temp.simplify(); // adjust the values so they conform to a normal value expressed in feet and inches.
return temp; //return the value stored in 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;
}
bool FeetInches::operator <= (const FeetInches &r)
{
bool status;
if (feet <= r.feet)
status = true;
else if (feet == r.feet && inches <= r.inches)
status = true;
else
status = false;
return status;
}
bool FeetInches::operator != (const FeetInches &r)
{
if (feet != r.feet)
return true;
else
return false;
}
ostream &operator << (ostream &strm, const FeetInches &obj)
{
strm << obj.feet << " feet, " << obj.inches << " inches";
return strm;
}
//************************************************************************************************
//The function returns a reference to an istream object so several of these expressions may be chained together.
//************************************************************************************************
istream &operator >> (istream &strm, FeetInches &obj)
{
// Prompt the user for the feet.
cout << "Feet: ";
strm >> obj.feet;
// Prompt the user for the inches.
cout << "Inches: ";
strm >> obj.inches;
// Normalize the values.
obj.simplify();
return strm;
}
Driver
// This program demonstrates the FeetInches class's overloaded
// + and - operators.
#include <iostream>
#include "FeetInches.h"
using namespace std;
int main()
{
//Create 2 FeetInches objects. The default arguments
//for the constructor will be used.
FeetInches first, second;
//Get a distance for the 1st object
cout << "Enter a distance in feet and inches.\n";
cin >> first;
//Get a distance for the 2nd object
cout << "Enter another distance in feet and inches.\n";
cin >> second;
// Display the values in the objects.
cout << "The values you entered are:\n";
cout << first << " and " << second << endl;
if (first <= second)
cout << "\nFirst values entered were not greater.\n";
else
cout << "\nFirst values were indeed greater.\n";
system("pause");
return 0;
}

New Topic/Question
Reply



MultiQuote






|