This code is based on the FeetInches.h and FeetInches.cpp version 4, found on pp. 856 through 858. The modification consists of three additional overloads written for this modification, and, for consitency, kept in the same style as is found in the other functions provided by the book. The driver is demonstrating only the new functionality introduced by this modification.
FeetInches.h
#ifndef FEET_INCHES_H_
#define FEET_INCHES_H_
#include <iostream>
using std::istream;
using std::ostream;
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:
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; }
// Overload 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 &); // Greater Equal
bool operator <= (const FeetInches &); // Lesser Equal
bool operator != (const FeetInches &); // Inequal
// Friends
friend ostream &operator << (ostream &, const FeetInches &);
friend istream &operator >> (istream &, FeetInches &);
};
#endif
FeetInches.cpp
#include "FeetInches.h"
#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
/* **********************************************************
FeetInches::simplify()
This function checks for values in the inches member
greater than twelve or less than zero. If such a value is
found, the numbers in feet and inches are adjusted to
conform to a standard feet & inches expression. For
example:
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.
The standard library function abs() is used to get the
absolute value of the inches member. The abs() function
requires to #include <cstdlib>.
********************************************************** */
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;
}
/* **********************************************************
Overloaded prefix ++ operator
Causes the inches member to be incremented. Returns the
incremented object.
********************************************************** */
FeetInches FeetInches::operator++()
{
++inches;
simplify();
return *this;
}
/* **********************************************************
Overloaded postfix ++ operator
Causes the inches member to be incremented. Returns the
value of the object before the increment.
********************************************************** */
FeetInches FeetInches::operator++(int)
{
FeetInches temp(feet, inches);
inches++;
simplify();
return temp;
}
/* **********************************************************
Overloaded > operator
Returns true if the current object is set to a value
greater than that of right.
********************************************************** */
bool FeetInches::operator > (const FeetInches &right)
{
bool status;
if (feet > right.feet)
{
status = true;
}
else if (feet == right.feet && inches > right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
/* **********************************************************
Overloaded < operator
Returns true if the current object is set to a value less
than that of right.
********************************************************** */
bool FeetInches::operator < (const FeetInches &right)
{
bool status;
if (feet < right.feet)
{
status = true;
}
else if (feet == right.feet && inches < right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
/* **********************************************************
Overloaded == operator
Returns true if the current object is set to a value equal
to that of right.
********************************************************** */
bool FeetInches::operator == (const FeetInches &right)
{
bool status;
if (feet == right.feet && inches == right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
/* **********************************************************
Overloaded Greater Equal => operator
Returns true if the current object is set to a value
greater than or equal to that of right.
********************************************************** */
bool FeetInches::operator >= (const FeetInches &right)
{
bool status;
if (feet >= right.feet)
{
return true;
}
else if (feet == right.feet && inches >= right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
/* **********************************************************
Overloaded Less Equal <= operator
Returns true if the current object is set to a value less
than or equal to that of right.
********************************************************** */
bool FeetInches::operator <= (const FeetInches &right)
{
bool status;
if (feet <= right.feet)
{
status = true;
}
else if (feet == right.feet && inches <= right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
/* **********************************************************
Overloaded Inequal != operator
Returns true if the current object is set to a value not
equal to that of right.
********************************************************** */
bool FeetInches::operator != (const FeetInches &right)
{
bool status;
if (feet != right.feet)
{
status = true;
}
else if (feet == right.feet && inches != right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
/* **********************************************************
Overloaded << operator
Gives cout the ability to directly display FeetInches
objects.
********************************************************** */
ostream &operator << (ostream &strm, const FeetInches &obj)
{
strm << obj.feet << " feet, " << obj.inches << " inches";
return strm;
}
/* **********************************************************
Overloaded >> operator
Gives cin the ability to directly store user input into
FeetInches objects.
********************************************************** */
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;
}
FeetInchesModificationDm.cpp
#include "FeetInches.h"
#include <iostream>
using std::cin;
using std::cout;
int main()
{
FeetInches first, second;
cout << "FEET INCHES MODIFICATION\n\n"
<< "This program asks you to enter two lenghts, measured in feet and inches.\n"
<< "It then tells you whether one distance is:\n\n"
<< "\tGreater than or equal to the other\n"
<< "\tLess than or equal to the other\n"
<< "\tNot the same as the other.\n\n";
for (int i = 0; i < 3; i++)
{
cout << "Enter a distance in feet and inches.\n";
cin >> first;
cout << "Enter another distance in feet and inches.\n";
cin >> second;
cout << "\nThe values you entered are:\n";
cout << first << " and " << second << "\n\n";
if (first >= second)
{
cout << "True: First is greater than or equal to second.\n";
}
else
{
cout << "False: First is not greater than or equal to second.\n";
}
if (first <= second)
{
cout << "True: First is less than or equal to second.\n";
}
else
{
cout << "False: First is not less than or equal to second.\n";
}
if (first != second)
{
cout << "True: First does not equal second.\n";
}
else
{
cout << "False: First equals second.\n";
}
cout << "\n";
}
cout << "\nThank you for using this demo. Have a nice day!";
cin.get();
cin.ignore();
return 0;
}
No comments:
Post a Comment