Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please read this CAREFULLY. In short, I need the source code to be written in C++ for the problem below. I have included two files

Please read this CAREFULLY. In short, I need the source code to be written in C++ for the problem below. I have included two files to assist you (FeetInches.cpp and FeetInches.h must work with the files you create). You will need to modify the FeetInches class, and create RoomDimension and RoomCarpet. Everything will be included below, you just need to read the headings to see what is what. Thanks in advance and please see as follows:

Problem

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 arguments feet attribute and do the same for the inches attributes.

The multiply function should accept a FeetInches object as an argument. The argument objects feet and inches attributes will be multiplied by the calling objects 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 // 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 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

KEY QUESTION Refer to columns 1 and 6 in the table for question

Answered: 1 week ago