#include <iostream>
Write a program for carpeting a rectangular room.
Create a class named Dimension that has three fields:
· one for the length of the room
· one for the width of the room
It also has a parameterized constructor to set the values of length and width with user-defined values and set area to zero.
Next create a class Carpet that is friend class of the Dimension class. It should have a field for the cost of the carpet per square foot and a parameterized constructor to set its value. The Carpet class should also have a method that calculates the area of the carpet (l*w) and then calculate the cost of carpeting the room by multiplying cost per sq foot with area. It will also return total cost of carpeting the room.
In main(), create instance of Dimension and set values. Then calculate and display total cost of carpeting the room through Carpet class object.
#include <iostream>
# include<stdio.h>
#include<stdlib.h>
using namespace std;
/*
Write a program for carpeting a rectangular room.
Create a class named Dimension that has three fields:
· one for the length of the room
· one for the width of the room
It also has a parameterized constructor to set the values of length and width with user-defined values and set area to zero.
Next create a class Carpet that is friend class of the Dimension class.
It should have a field for the cost of the carpet per square foot and a parameterized constructor to set its value.
The Carpet class should also have a method that calculates the area of the carpet (l*w) and then,
calculate the cost of carpeting the room by multiplying cost per sq foot with area.
It will also return total cost of carpeting the room.
In main(), create instance of Dimension and set values. Then calculate and display total cost of carpeting the room through Carpet class object.
*/
class Dimension
{
public:
int length;
int width;
int area;
setDim(int l, int w)
{
length = l;
width = w;
cout<<"\n\tLength = "<<length<<" fts.";
cout<<"\n\tWidth = "<<width<<" fts.";
area = 0;
}
};
class Carpet : public Dimension
{
public:
float CostPerSqfeet;
float Area;
float Cost;
setCost(float f)
{
CostPerSqfeet = f;
cout<<"\n\tCost per square feet = US $ "<<CostPerSqfeet<<" per sq. feet";
}
float CalculateArea(void)
{
Area = length*width;
cout<<"\n\n\tTotal Carpetting Area = "<<Area<<" sq. feets";
return(Area);
}
float CalculateCost()
{
Cost = Area*CostPerSqfeet;
cout<<"\n\tTotal Carpetting Cost = US $ "<<Cost;
return(Cost);
}
};
int main()
{
class Carpet C;
C.setDim(12,7);
C.setCost(2);
C.CalculateArea();
C.CalculateCost();
}
Comments
Leave a comment