In: Computer Science
Create 2 derived classes from 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.
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.