In C++ please and if you could do and explain each step thank you 1.Add a main m
ID: 3873371 • Letter: I
Question
In C++ please and if you could do and explain each step thank you 1.Add a main method to the FeetInches class. The main method should create three FeetInches objects. 2.Create one object named sum_length using the no-parameters constructor. 3.For the other two, prompt the user to enter a number of feet and a number of inches, and construct a FeetInches object using the two parameter constructor. 4.Do this twice, to create the two objects named length1 and length2. 5.Then test the equals method by using it to compare the objects. 6.ALso, test the add method by adding the two lengths length1 and length2 and assiigning the result to the sum_length object. 7.Print the result of the length comparison and print the sum_length. Test the setFeet and setInches methods on sum_length and print it after each set operation to verify it changed properly. #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; } };
Explanation / Answer
#include <iostream>
#include <string>
#include <stdio.h>
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 sum_length; // no parameter constructor object
FeetInches obj2(5,13); // parameterised constructor object
FeetInches obj3(obj2); // copy constructor object
return 0;
}