Question: Programming Assignment 2 Change the implementation of the FeetInches class by replacing the two data members feet and inches with one data member named total_inches.

Programming Assignment 2
Change the implementation of the FeetInches class by replacing the two data members feet and inches with one data member named total_inches. The implementations of the methods will need to be changed also, but all methods should behave exactly the same as before. This means all methods must take the same parameters and produce the same return values.
The key idea here is we are changing how the methods work without changing what the methods do.
I have provides a version of the FeetInches class including a main method here (click on Programming Assignment 2). You should run this program as is, then modify it as described above and run it again, verifying that it produces exactly the SAME RESULTS.
Must have the same answer as before changing the code.
#include
#include
#include
using namespace std;
/**
The FeetInches class holds distances measured in
feet and inches.
*/
class FeetInches
{
private:
int feet; // The number of feet
int inches; // The number of inches
void simplify()
{
if (inches > 11)
{
feet = feet + (inches / 12);
inches = inches % 12;
}
}
/**
This constructor assigns 0 to the feet
and inches fields.
*/
public:
FeetInches()
{
feet = 0;
inches = 0;
}
/**
This constructor accepts two arguments which
are assigned to the feet and inches fields.
The simplify method is then called.
@param f The value to assign to feet.
@param i The value to assign to inches.
*/
FeetInches(int f, int i)
{
feet = f;
inches = i;
simplify();
}
/**
The following is a copy constructor. It accepts a
reference to another FeetInches object. The feet
and inches fields are set to the same values as
those in the argument object.
@param object2 The object to copy.
*/
FeetInches (const FeetInches& object2)
{
feet = object2.feet;
inches = object2.inches;
}
/**
The simplify method adjusts the values
in feet and inches to conform to a
standard measurement.
*/
/**
The setFeet method assigns a value to
the feet field.
@param f The value to assign to feet.
*/
void setFeet(int f)
{
feet = f;
}
/**
The setInches method assigns a value to
the inches field.
@param i The value to assign to inches.
*/
void setInches(int i)
{
inches = i;
simplify();
}
/**
getFeet method
@return The value in the feet field.
*/
int getFeet()
{
return feet;
}
/**
getInches method
@return The value in the inches field.
*/
int getInches()
{
return inches;
}
/**
print method
prints the distance as feet/inches
*/
void print()
{
cout << feet << " feet " << inches << " inches";
}
/**
toString method
@return a reference to a String stating
the feet and inches.
*/
/* does not work in all compilers
string toString()
{
char *s_feet = new char(20), *s_inches = new char(20);
sprintf(s_feet, "%d", feet);
sprintf(s_inches, "%d", inches);
return string(s_feet) + " feet " + string(s_inches) + " inches";
}
*/
/**
The add method returns a FeetInches object
that holds the sum of this object and another
FeetInches object.
@param object2 The other FeetInches object.
@return A reference to a FeetInches object.
*/
FeetInches add(const FeetInches& object2)
{
int totalFeet, // To hold the sum of feet
totalInches; // To hold the sum of inches
totalFeet = feet + object2.feet;
totalInches = inches + object2.inches;
return FeetInches(totalFeet, totalInches);
}
/**
The equals method compares this object to the
argument object. If both have the same values,
the method returns true.
@return true if the objects are equal, false
otherwise.
*/
bool equals(FeetInches object2)
{
return feet == object2.feet && inches == object2.inches;
}
};
int main()
{
FeetInches a(3,2), b(4,5), c;
a.setFeet(5);
a.print();
b.setInches(9);
b.print();
c = a.add(b);
c.print();
cout << c.getFeet() << " ft " << c.getInches() << " in" << endl;
cout << a.equals(a) << " " << a.equals(b) << endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!