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,...
UseMath Write a program called UseMath. It should contain a class called UseMath that contains the...
UseMath Write a program called UseMath. It should contain a class called UseMath that contains the method main. The program should ask for a number from the user and then print the information shown in the examples below. 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 UseMath enter a number: 0 the square root of 0.0 is: 0.0 rounded to the...
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
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT