Question

In: Computer Science

Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the...

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

Solutions

Expert Solution

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


Related Solutions

1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >=...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >= as member operators. The comparison operators will compare students by last name first name and id number. Also, overload << and >> as friend operators in the Student class. Overload << and >> operators in Roster class as well to output Rosters in a nice format as well as input Roster. Provide a function sort in a Roster class as a private function to...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
A hacker overloads the KOI website and shutdowns the service so that legitimate users such as...
A hacker overloads the KOI website and shutdowns the service so that legitimate users such as student and staff can no longer access it. Mention the name and describe the type of attack carried out on the KOI website with an explanation
A hacker overloads the KOI website and shutdowns the service so that legitimate users such as...
A hacker overloads the KOI website and shutdowns the service so that legitimate users such as student and staff can no longer access it. Mention the name and describe the type of attack carried out on the KOI website with an explanation
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so...
​​​​​​This is an assignment that I'm having trouble figuring out in python: Modify Node class so it has a field called frequency and initialize it as one. Add a search function. If a number is in the list, return its frequency. Modify Insert function. Remove push, append, and insertAfter as one function called insert(self, data). If you insert a data that is already in the list, simply increase this node’s frequency. If the number is not in the list, add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT