In: Computer Science
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts.
Based on write a program that uses the class "fabric" to
display the square footage of a piece of large fabric.
class fabric
{
private:
int length;
int width;
int squareFoot;
public:
int area();
}
/* * C++ Program for fabric and garment class */ #include <iostream> using namespace std; class fabric { private: int length; int width; int squareFoot; public: fabric(); fabric(int, int); int getLength(); int getWidth(); int area(); }; fabric :: fabric() : length(0), width(0), squareFoot(0) {} fabric :: fabric(int l, int w) : length(l), width(w), squareFoot(l*w) {} int fabric :: getLength() { return length; } int fabric :: getWidth() { return width; } int fabric :: area() { return squareFoot; } class garment: public fabric { public: garment(); garment(int l, int w); void display(); }; garment :: garment() : fabric(0, 0) {} garment :: garment(int l, int w) : fabric(l, w) {} void garment :: display() { for (int i = 0; i < getLength(); i++) { for (int j = 0; j < getWidth(); j++) { cout << "*"; } cout << endl; } } int main() { const int len = 7; const int wid = 5; garment bedsheet(7, 5); cout << "Print Bedsheet of " << len << "X" << wid << " -" << endl; bedsheet.display(); cout << "Area: " << bedsheet.area() << endl; return 0; }
Note: The fabric class methods are not provided hence they are completed by some assumptions, please drop me a comment for queries or optimizations.