Question

In: Computer Science

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 that shirts are dry clean only.
    • Include additional method hang()
  • Driver/Tester file
    • For Pants class
      • Include tests for all constructors
      • Include tests for all getters/accessors
      • Include tests for all setters/mutators
      • Be sure to print new Pants objects
    • For Shirt class
      • Include tests for all constructors
      • Include tests for all getters/accessors
      • Include tests for all setters/mutators
      • Be sure to print new Shirt objects
  • Clothing class
  • #ifndef CLOTHING_H
    #define CLOTHING_H

    #include <string>

    using std::string;
    using std::cout;

    class Clothing
    {
       private:
           string size;
           string color;
          
       public
           Clothing()
           {
               size = "null";
               color = "null";
           }
           Clothing(string s, string c)
           {
               size = s;
               color = c;
           }
           string getSize()
           {
               return size;
           }
           string getColor()
           {
               return color;
           }
           void setSize(string s)
           {
               size = s;
           }
           void setColor(string c)
           {
               color = c;
           }
           void wash()
           {
               cout << "Wash with some dirt to try how powerful our detergent is :P\n";
           }
           void pack()
           {
               cout << "Our packaging service is really bad but we make up for it with the amazing washing detergent. Just fold them anyway you want\n";
           }
           void displayClothing()
           {
               cout << "clothing [size = " << size << ", color = " << color << "]\n";
           }
    };
    #endif

Thanks.

Solutions

Expert Solution

Dear Learner,

Below is the code for the solution. As mentioned in the question, we have a separate file "Clothing.h" for the class clothing and a separate file types.cpp for the child classes, shirts and pants. The inputs here are static but will work the same for dynamic inputs as well.

#include<iostream>
#include <string>
#include "clothing.h" //including the clothing class
using namespace std;

class Pants : public Clothing //defining the Pants class with public mode of inheritance
{
   public:

      
       void wash() //overriding wash function
       {
           cout<<"\nPants are dry clean only.\n";
       }
       void displayClothing()
       {
           Clothing::displayClothing(); //calling the display function of the parent class
       }
};

class Shirt : public Clothing //defining the Shirt class with public mode of inheritance
{
   string sleeves;

   public:
Shirt() //constructor to initialize the data member
       {
           string sleeves="null";
       }
       void wash() //overriding wash function
       {
           cout<<"\nShirts are dry clean only.\n";
       }
       string getSleeve()
       {
           return sleeves;
       }
      
       int setSleeve(string Sleeve)
       {
           if(Sleeve=="short" || Sleeve=="long" || Sleeve=="none") //here we are checking for the constrains in the values
           {
               sleeves = Sleeve; // and if that constrain is satisfied, we will set the value,
               return 1;
           }
   else
   return 0;   //this is to return if the user passes some other value that we are not expecting
       }
       string getSize() //defining a function in Shirts with the same name to setSize
   {
    return Clothing::getSize(); //notice that using the '::' we are calling the function of the parent class
        }

int setSize(string Size)
       {   if(Size=="S" || Size== "M" || Size=="L") //just as above we are checking the constrains
          {
              Clothing::setSize(Size); //notice that again, we are calling the parents function.
          return 1;
          }
else
return 0;
       }
      
       void displayClothing()
       {
           cout<<"\nSleeve size is :-"<<sleeves;
           Clothing::displayClothing();
       }
};
main()
{
   cout<<"hello, welcome!\n";
   cout<<"Clothing class\n";
   Clothing cloth1;
   cloth1.setColor("black");
   cloth1.setSize("XL");
   cloth1.wash();
   cout<<endl;
   cloth1.displayClothing();
   cout<<endl;
   cout<<"\nPants Class \n";
   Pants p1;
   p1.setColor("Blue");
   p1.setSize("L");
   p1.wash();
   cout<<endl;
   p1.displayClothing();
   cout<<"\nShirts Class \n";
   Shirt s1;
   s1.setSleeve("short");
   s1.setSize("S");
   s1.setColor("Black");
   s1.displayClothing();
}

SAMPLE OUTPUT :-

I hope I have answered the question the way you were expecting. If you still have any doubts or want any other explanation, feel free to ask us in the comment section.

Please leave a like if this was helpful.

Thanks,

Happy Studying.


Related Solutions

Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the...
Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the char getLetterGrade() method in each of the child classes. Use Student.java class defined below: (complete and compile) class Student {    private int id;    private int midtermExam;    private int finalExam;    public double calcAvg() {       double avg;       avg = (midtermExam + finalExam) / 2.0;       return avg;    }    public char getLetterGrade() {       char letterGrade = ‘ ‘;...
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...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
1. define a class that has three different constructors. 2. write code that overrides the following...
1. define a class that has three different constructors. 2. write code that overrides the following method public double sqr(double num1) { return num1*num1; }
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
1.) A class template can be derived from a non-template class True or False 2.) Inaccessible...
1.) A class template can be derived from a non-template class True or False 2.) Inaccessible pointer is a potential problem on simple linked list True or False 3.) Array based lists are faster in term of acing data True or False 4.) Simple linked lists use less space than double linked lists True or False 5.) For large lists "array based lists" are more efficient for insertion and deleting operational True or False 6.) We can remove data only...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT