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.
.
#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;
}
Comments
Leave a comment