In: Computer Science
Modify the FeetInches class so that it overloads the following operators:
<=
>=
!=
Demonstrate the class's capabilities in a simple program.
this is what needs to be modified
// Specification file for the FeetInches class
#ifndef FEETINCHES_H
#define FEETINCHES_H
#include <iostream>
using namespace 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 ++
bool operator > (const FeetInches &);
bool operator < (const FeetInches &);
bool operator == (const FeetInches &);
// New operators
bool operator >= (const FeetInches &);
bool operator <= (const FeetInches &);
bool operator != (const FeetInches &);
// Conversion functions
operator double();
operator int();
// Friends
friend ostream &operator << (ostream &, const FeetInches &);
friend istream &operator >> (istream &, FeetInches &);
};
#endif
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// FeetInches.h
#ifndef FEETINCHES_H
#define FEETINCHES_H
class FeetInches
{
private:
int feet;
int inches;
void simplify();
public:
FeetInches(int f = 0, int i = 0)
{
this -> feet = f;
this -> inches = i;
simplify();
}
void set_feet(int f)
{ feet = f; }
void set_inches(int i)
{ inches = i; }
unsigned int get_feet() const
{ return feet; }
unsigned int get_inches() const
{ return inches; }
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 ==
// Modification
bool operator <= (const FeetInches &);
bool operator >= (const FeetInches &);
bool operator != (const FeetInches &);
// Conversion functions
operator double();
operator int();
// Friends
friend ostream &operator << (ostream &, const
FeetInches &);
friend istream &operator >> (istream &, FeetInches
&);
};
#endif
_________________________
// FeetInches.cpp
#include <iostream>
#include <cmath>
using namespace std;
#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 - (fmod(abs(inches),12));
}
}
FeetInches FeetInches::operator + (const FeetInches
&right)
{
FeetInches temp;
temp.inches = inches + right.inches;
temp.feet = feet + right.feet;
temp.simplify();
return temp;
}
FeetInches FeetInches::operator - (const FeetInches
&right)
{
FeetInches temp;
temp.inches = inches - right.inches;
temp.feet = feet - right.feet;
temp.simplify();
return temp;
}
FeetInches FeetInches::operator ++ ()
{
++inches;
simplify();
return *this;
}
FeetInches FeetInches::operator ++ (int)
{
FeetInches temp(feet, inches);
inches++;
simplify();
return temp;
}
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;
}
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;
}
bool FeetInches::operator == (const FeetInches &right)
{
bool status;
if (feet == right.feet && inches == right.inches)
status = true;
else
status = false;
return status;
}
bool FeetInches::operator <= (const FeetInches &right)
{
bool status;
if(feet < right.feet || inches == right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
bool FeetInches::operator >= (const FeetInches &right)
{
bool status;
if(feet > right.feet || inches == right.inches)
{
status = true;
}
else
{
status = false;
}
return status;
}
bool FeetInches::operator != (const FeetInches &right)
{
bool status;
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 *
// store user input directly 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;
}
//*************************************************************
// Conversion function to convert a FeetInches object *
// to a double. *
//*************************************************************
FeetInches::operator double()
{
double temp = feet;
temp += (inches / 12.0);
return temp;
}
//*************************************************************
// Conversion function to convert a FeetInches object *
// to an int. *
//*************************************************************
FeetInches:: operator int()
{
return feet;
}
__________________________________
// main.cpp
#include <iostream>
using namespace std;
#include "FeetInches.h"
int main()
{
double d; // To hold double input
int i; // To hold int input
// Define a FeetInches object.
FeetInches distance;
// Get a distance from the user.
cout << "Enter a distance in feet and inches:\n";
cin >> distance;
// Convert the distance object to a double.
d = distance;
// Convert the distance object to an int.
i = distance;
// Display the values.
cout << "The value " << distance;
cout << " is equivalent to " << d << "
feet\n";
cout << "or " << i << " feet, rounded
down.\n";
FeetInches f1(3,9),f2(5,8);
cout<<"FeetInches#1"<<endl;
cout<<f1<<endl;
cout<<"FeetInches#2"<<endl;
cout<<f2<<endl;
FeetInches f3 = f1+f2;
if(f1<=f2)
{
cout<<"f1 is less than f2"<<endl;
}
else{
cout<<"f1 is not less than f2"<<endl;
}
if(f1==f2)
{
cout<<"f1 is equal f2"<<endl;
}
else{
cout<<"f1 is not equal to f2"<<endl;
}
if(f1>=f2)
{
cout<<"f1 is greater than or equal to f2"<<endl;
}
else{
cout<<"f1 is not is greater than or equal to
f2"<<endl;
}
if(f1<f2)
{
cout<<"f1 is less than f2"<<endl;
}
else{
cout<<"f1 is not less than f2"<<endl;
}
if(f1>f2)
{
cout<<"f1 is greater than f2"<<endl;
}
else{
cout<<"f1 is not greater than f2"<<endl;
}
if(f1!=f2)
{
cout<<"f1 is not equal to f2"<<endl;
}
else{
cout<<"f1 is equal to f2"<<endl;
}
f1++;
cout<<"FeetInches (f1) after post-Increment
"<<f1<<endl;
++f2;
cout<<"FeetInches (f2) after pre-Increment
"<<f2<<endl;
return 0;
}
______________________________
Output:
_______________________Thank You