Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello and thank you for reviewing this question. I need source code for a soluti

ID: 3853518 • Letter: H

Question

Hello and thank you for reviewing this question. I need source code for a solution to a program to be written in C++. If you could be patient to explain to me through good code comments what is going on in the program, it would mean a lot. I don't necessarily understand but would like anyone looking at the program to pick up on what is happening. You will find below the problem and then you will see two sections that follow with the necessary files (.cpp and .h) that the program must work with. Please be detailed and if possible, include a way to contact you if I have further questions or assistance. Thank you in advance.

Problem

This problem starts with the FeetInches class that is provided in the course Content area on the assignment page for this week. This program will show how classes will interact with each other as data members within another class. Modify the FeetInches class by overloading the following operators which should all return a bool.

<=

>=

!=

Next add a copy constructor to the FeetInches class and a multiply function.

The copy constructor should accept a FeetInches object as an argument. It will assign the feet attribute the value in the argument’s feet attribute and do the same for the inches attributes.

The multiply function should accept a FeetInches object as an argument. The argument object’s feet and inches attributes will be multiplied by the calling object’s feet and inches attributes. It will return a FeetInches object containing the result of the multiplication.

Next create a class called RoomDimension which will have its class declaration in RoomDimension.h and its implementation in RoomDimension.cpp. This class will have two data members which have a data type of FeetInches, one for the length of the room and another for the width of the room. The multiply function in FeedInches will be used to calculate the area of the room. RoomDimension will have a function that returns the area of the room as a FeetInches object.

Next create a class called RoomCarpet class that has a RoomDimension object as an attribute. This class will have its class declaration in RoomCarpet.h and its implementation in RoomCarpet.cpp. It should also have an attribute for the cost of the carpet per square foot. It will have a member function that returns the total cost of the carpet. For example, a room that is 12 feet long and 10 feet wide has an area of 120 square feet. If the cost per square foot is $8 then the cost to carpet the room will be $960 (120 x 8).

The main for this program will create an instance of RoomCarpet and ask the user for the dimensions of the room and the price per square foot for the carpet. The application should then display the total cost of the carpet. It should allow the user to continue doing more calculations until the user indicates they are done.

First Code You Need to Work With (FeetInches.cpp)

// Implementation file for the FeetInches class
#include <cstdlib> // Needed for abs()
#include "FeetInches.h"

//************************************************************
// Definition of member function 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.   *
//************************************************************

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 << 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;
}


Second Code You Need to Work With (FeetInches.h)

// 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 &);

   // Conversion functions
   operator double();
   operator int();

   // Friends
   friend ostream &operator << (ostream &, const FeetInches &);
   friend istream &operator >> (istream &, FeetInches &);
};

#endif

Explanation / Answer

First Code You Need to Work With (FeetInches.cpp)

// Implementation file for the FeetInches class
#include <cstdlib> // Needed for abs()
#include "FeetInches.h"

//************************************************************
// Definition of member function 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.   *
//************************************************************

void FeetInches::simplify() //this method normalizes the feet-inch relation
{
   if (inches >= 12) //if the inche value is more than 12, we can simply add 1 foot for every 12 inchs
   {
      feet += (inches / 12); // add 1 foot for every 12 inchs
      inches = inches % 12; //normalizes the inch i.e. after adding 1 foot for every 12 inches, this is the left-over
   }
   else if (inches < 0) //this is for -ve inche value
   {
      feet -= ((abs(inches) / 12) + 1);// subtract 1 foot for every 12 inchs and we add 1 extra foot too
      inches = 12 - (abs(inches) % 12); // that extra foot subtracted abobe is compenseted here, and inch value is always +ve
   }
}

//**********************************************
// Overloaded binary + operator.               *
//**********************************************

FeetInches FeetInches::operator + (const FeetInches &right) // adds two FeetInch
{
   FeetInches temp; //just a temporary object to perform calculation

   temp.inches = inches + right.inches; //add inchess and store in temp
   temp.feet = feet + right.feet; //add feet and store in temp
   temp.simplify(); //normalize the temporary objet as I have said above
   return temp; // returns the temporary object
}


//**********************************************
// Overloaded binary - operator.               *
//**********************************************

FeetInches FeetInches::operator - (const FeetInches &right) //substracts two FeetInch
{
   FeetInches temp;
   // ever line of this exactly same as above. Only change is we substract the values  
   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 ++ () //this is a prefix orperator
{                                       // it perform also changes within itself
   ++inches; // add inchs by 1
   simplify(); // simplify 'this' object
   return *this; // and return this NOTE: WE RETURN THE UPDATED VALUE
}

//***************************************************************
// Overloaded postfix ++ operator. Causes the inches member to *
// be incremented. Returns the value of the object before the   *
// increment.                                                   *
//***************************************************************

FeetInches FeetInches::operator ++ (int) //this is a postfix operator
{
   FeetInches temp(feet, inches); //create a temp object using parameters

   inches++; // increase inch value by 1 of 'this' object
   simplify(); // normalize this object
   return temp; // return the object that was not updated
                // doing so helps the Left Hand variable/operator receive non-updated value
}

//************************************************************
// Overloaded > operator. Returns true if the current object *
// is set to a value greater than that of right.             *
//************************************************************

bool FeetInches::operator > (const FeetInches &right) //this is greater than operator
{
   bool status;

   if (feet > right.feet) //if feet value of Left hand operand is not equel to feet value of right hand operand we dont need to chk the inches
      status = true;// i.e. if feet of left is more than the feet of right, we do not need to chk the inches
   else if (feet == right.feet && inches > right.inches) //if equal we compare the inches
      status = true;
   else
      status = false;

   return status; //return the 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) //this is less than operator
{
   // this works exactly the same way greater than operator works, with minor differences
   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) //this chk is both objects are equal
{
   bool status;

   if (feet == right.feet && inches == right.inches) //objects are equal if and only if feet of both object are same and inch of both object are same
      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) //this simply updates the outstream object
{
   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) //this method accept user input
{
   // Prompt the user for the feet.
   cout << "Feet: "; //request feet (print in stdout)
   strm >> obj.feet; //accept from inputstream

   // Prompt the user for the inches.
   cout << "Inches: ";//request feet (print in stdout)
   strm >> obj.inches;//accept from inputstream

   // Normalize the values.
   obj.simplify(); //normalize the same object

   return strm;
}

//*************************************************************
// Conversion function to convert a FeetInches object         *
// to a double.                                               *
//*************************************************************

FeetInches::operator double() //this will convert object to double value
{
   double temp = feet; //copy the feet to temporary varible

   temp += (inches / 12.0); //add the inches divided by 12 (feet = inch/12)
   return temp;
}

//*************************************************************
// Conversion function to convert a FeetInches object         *
// to an int.                                                 *
//*************************************************************

FeetInches:: operator int()
{
   return feet; //in integer part inches are not considered
}


Second Code You Need to Work With (FeetInches.h)

// Specification file for the FeetInches class
#ifndef FEETINCHES_H
#define FEETINCHES_H //these two lines determine that the following lines are not repated even if we include the header file multiple times
//header file contains method headers in most cases
#include <iostream>
using namespace std;

class FeetInches; // Forward Declaration

// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const FeetInches &); //global opeators
istream &operator >> (istream &, FeetInches &);

// The FeetInches class holds distances or measurements
// expressed in feet and inches.

class FeetInches //class begins here
{
private:
   int feet;        // To hold a number of feet
   int inches;      // To hold a number of inches
   void simplify(); // Defined in FeetInches.cpp
public: //public declaration are accessable outside the class
   // 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 &); //function header
   FeetInches operator - (const FeetInches &);//function header
   FeetInches operator ++ ();    // Prefix ++ function header
   FeetInches operator ++ (int); // Postfix ++ function header
   bool operator > (const FeetInches &);//function header
   bool operator < (const FeetInches &);//function header
   bool operator == (const FeetInches &);//function header

   // Conversion functions
   operator double();//function header
   operator int();//function header

   // Friends
   friend ostream &operator << (ostream &, const FeetInches &); //friend function header
   friend istream &operator >> (istream &, FeetInches &); //friend function header
};

#endif

If you face any problem with the comments, please comment below.... I shall reply as soon as possible

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote