Question

In: Computer Science

3. write a program that uses a class called "garment" that is derived from the class...

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();
           }

Solutions

Expert Solution


/*
 *  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.


Related Solutions

Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
Write a OOP class called BookList that uses a Book class and allows multiple books to...
Write a OOP class called BookList that uses a Book class and allows multiple books to be entered into a BookList object. Include normally useful methods and boolean addBook(Book b), Book searchBook(String title, String author). addBook returns true if b is successfully added to the list. searchBook returns a book matching either the title or author from the current list. You do not need to write Book (assume there are suitable constructors and methods) or any test code (main). You...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the...
AskInfoPrintInfo Write a program called AskInfoPrintInfo. It should contain a class called AskInfoPrintInfo that contains the method main. The program should ask for information from the user and then print it. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java AskInfoPrintInfo enter your name: jon doe enter your address (first line): 23 infinite loop lane enter your address (second line): los angeles,...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Java program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
Give an example of a program that uses the nongeneric version of a class from the...
Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT