Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / This program nests one class inside another. It has a class / / with a member variable that is an instance of another

// This program nests one class inside another. It has a class
// with a member variable that is an instance of another class.
#include
using namespace std;
class Rectangle
{
private:
double length;
double width;
public:
void setLength(double len)
{ length = len; }
void setWidth(double wid)
{ width = wid; }
double getLength()
{ return length; }
double getWidth()
{ return width; }
double getArea()
{ return length * width; }
};
class Carpet
{
private:
double pricePerSqYd;
Rectangle size; // size is an instance of
// the Rectangle class
public:
void setPricePerYd(double p)
{ p = pricePerSqYd; }
void setDimensions(double len, double wid)
{ size.setLength(len);
size.setWidth (wid);
}
double getTotalPrice()
{ return (size.getArea()* size.getLength()); }
};
//************** Client Program *****************
int main()
{
Carpet purchase; // This variable is a Carpet object
double pricePerYd;
double length;
double length;
cout << "Room length in feet: ";
cin >> length;
cout << "Room width in feet : ";
cin >> width;
cout << "Carpet price per sq. yard: ";
cin >> pricePerYd;
purchase.setDimensions(length, width);
purchase.setPricePerYd(pricePerYd);
cout <<"
The total price of my new "<< length <<" x "<< width
<<" carpet is $"<< purchase.getTotalPrice()<< endl;
return 0;
}

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

Mastering Big Data Interview 751 Comprehensive Questions And Expert Answers

Authors: Mr Bhanu Pratap Mahato

1st Edition

B0CLNT3NVD, 979-8865047216

More Books

Students also viewed these Databases questions