The code must be in C++ Carpet Calculator The Westfield Carpet Company has asked
ID: 3817377 • Letter: T
Question
The code must be in C++
Carpet Calculator
The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor (width times length) by the price per square foot of carpet. For example, the area of floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet that costs $8 per square foot would cost $960. (12 x 10 x 8 = 960.)
First, you should create a class named RoomDimension that has two FeetInches
objects as attributes: one for the length of the room and one for the width. (You should use the version of the FeetInches class that you created in Programming Challenge 11 with the addition of a multiply member function. You can use this function to calculate the area of the room.) The RoomDimension class should have a member function that returns the area of the room as a FeetInches object.
Next, you should create a RoomCarpet class that has a RoomDimension object as an attribute. It should also have an attribute for the cost of the carpet per square foot. The RoomCarpet class should have a member function that returns the total cost of the carpet.
888 Chapter 14 More About Classes
Once you have written these classes, use them in an application that asks the user to enter the dimensions of a room and the price per square foot of the desired carpeting. The application should display the total cost of the carpet.
Explanation / Answer
#include <iostream>
using namespace std;
class FeetInches
{
int feet;
int inches;
public:
FeetInches()
{
feet = 0;
inches = 0;
}
FeetInches(double f)
{
feet = (int)f;
inches = (f - feet) * 12;
}
FeetInches(int f, int i)
{
feet = f;
inches = i;
}
void setFeetInches(double f)
{
feet = (int)f;
inches = (f - feet) * 12;
}
int getFeet() { return feet; }
int getInches() { return inches; }
FeetInches multiply(FeetInches other)
{
int result = (feet * 12 + inches) * (other.feet * 12 + other.inches);
return FeetInches(result/12, result%12);
}
};
class RoomDimension
{
FeetInches length;
FeetInches width;
public:
RoomDimension()
{
this->length.setFeetInches(0);
this->width.setFeetInches(0);
}
RoomDimension(double length, double width)
{
this->length.setFeetInches(length);
this->width.setFeetInches(width);
}
RoomDimension operator= (RoomDimension &rd)
{
this->length = rd.length;
this->width = rd.width;
return *this;
}
FeetInches areaOfRoom()
{
FeetInches area = length.multiply(width);
return area;
}
};
class RoomCarpet
{
RoomDimension carpet;
double costPerSquareFoot;
public:
RoomCarpet(RoomDimension rd)
{
carpet = rd;
}
double totalCostOfCarpet()
{
FeetInches carpetArea = carpet.areaOfRoom();
int inches = carpetArea.getFeet();
if(carpetArea.getInches() > 0)
inches++;
return costPerSquareFoot * inches;
}
};
int main()
{
double length, width;
cout << "Enter the length of room (in feet): ";
cin >> length;
cout << "Enter the width of room (in feet): ";
cin >> width;
RoomDimension room (length, width);
RoomCarpet carpet (room);
cout << "The cost of the carpet is: " << carpet.totalCostOfCarpet() << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.