Answer to Question #190950 in C++ for Aadee

Question #190950

Solve this Question using Inheritance only:

A Company named Aurora rugs hires you to write an application for them. The application should 

calculate the cost of carpet for a room. The formula is: Area (w X l) multiply with the cost of 1 square foot. e.g. suppose the area of room is: length is 12 feet and width is 12 feet so that makes the area 120 

square feet. Cost of carpet is $10 for a single foot which will make an expense of (120*10=1200) 

Dollar 1200. Now define a class named RD i.e. Dimensions for the Room that contains 2 private data 

members: To store the length which should be of Floating point to store the decimal value and 

another to store the width which also should be of floating point. Implement an overloaded constructor that takes two parameters and initializes the object by 

initializing data members.  

.



1
Expert's answer
2021-05-09T10:40:03-0400
#include <iostream>
#include <string>
using namespace std;
 
class RD
{
public:
	RD(double w, double l);
	double GetArea() { return width * length; }
	double GetWidth() { return width; }
	double GetLength() { return length; }
private:
	double width;
	double length;
};
 
RD::RD(double w, double l) : width(w), length(l)
{}
 
class RDP : public RD
{
public:
	RDP(double w, double l);
	void Display();
private:
	const double pricePerFeet = 10;
	double price;
};
 
RDP::RDP(double w, double l) : RD(w, l)
{
	price = GetArea() * pricePerFeet;
}
 
void RDP::Display()
{
	cout << "Carpet " << GetLength() << " x " << GetWidth() << " cost is: " << price << " Dollars." << endl;
}
 
int main()
{
	double w, l;
	cout << "Enter width: ";
	cin >> w;
	cout << "Enter length: ";
	cin >> l;
	RDP Item(w, l);
	Item.Display();
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment