using C++ i am trying to make a class that displays a text file arranged like th
ID: 3835406 • Letter: U
Question
using C++ i am trying to make a class that displays a text file arranged like the example below. this is my code so far. it does not compile. please use the language C++.
EX:
Size: M
Gender: M
Color: R
Sleeve type: Ss
Fading: Y
Stretching: Y
Shrinking: N
Warming: Y
Reflecting: Y
Soaking: N
----------------------------------------------------------
#include <cstdlib>
#include <iostream>
using namespace std;
class shirt
{private:
char size;
char gender;
char color;
char sleeveType;
bool fading;
bool stretch;
bool shrinks;
bool warms;
bool reflect;
bool soaks;
public:
char insertSize(char S, char M, char L){
size = s;
cout<<"Size: "<<size;
}
char insertGender(char M, char F){
gender = g;
cout<<"Gender: "<<gender;
}
char insertColor(char R, char Bl, char G, char P, char Bk, char Y, char Br){
color = c;
cout<<"Color: "<<color;
}
char insertSleeveType(char Ss, char Ls, char Ns){
sleeveType = st;
cout<<"Sleeve Type: "<<sleeveType;
}
bool insertFading(bool y, bool n){
fading = f;
cout<<"Fading?: "<<fading;
}
bool insertStretch(bool y, bool n){
stretch = sr;
cout<<"Stretching?: "<<stretch;
}
bool insertShrinks(bool y, bool n){
shrinks = sh;
cout<<"Shrinking?: "<<shrinks;
}
bool insertWarms(bool y, bool n){
warms = w;
cout<<"Warming up?: "<<warms;
}
bool insertReflect(bool y, bool n){
reflect = r;
cout<<"Reflecting light: "<<reflect;
}
bool insertSoaks(bool y, bool n){
soak = sk;
cout<<"Soaking: "<<soaks;
}
int main()
{
shirt obj;
cout<<"Enter size of shirt. S, M, or L? :";
cin>>obj.size;
cout<<"Enter gender of shirt. M or F :";
cin>>obj.gender;
cout<<"Enter color of shirt. R, Bl, G, P, Bk, Y, or Br? :";
cin<<obj.color;
cout<<"Enter sleeve type. Ss, Ls, or Ns? :";
cin<<obj.sleeveType;
cout<<"Is the shirt fading? y or n? :";
cin<<obj.fading;
cout<<"Is the object stretched? y or n? :";
cin<<obj.stretch;
cout<<"Is you object shrinking? y or n? :";
cin<<obj.shrinks;
cout<<"Is it warming up? y ot n? :";
cin<<obj.warms;
cout<<"Is it reeflecting light? y or n? :";
cin<<obj.reflect;
cout<<"Is it soaked? y or n? :";
cin<<obj.soaks;
cout<<size<<endl;
cout<<gender<<endl;
cout<<color<<endl;
cout<<sleeveType<<endl;
cout<<fading<<endl;
cout<<stretch<<endl;
cout<<shrinks<<endl;
cout<<warms<<endl;
cout<<reflect<<endl;
cout<<soaks<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Explanation / Answer
#include <cstdlib>
#include <iostream>
using namespace std;
class shirt {
private: /*if u used public here then u can use class.dataMember = for initialization */
char sizek; /*size will led to namespace collision as size() is a predefined function*/
char gender;
string color;
string sleeveType;
char fading;
char stretch;
char shrinks;
char warms;
char reflect;
char soaks;
public:
void insertSize(char sz){
sizek = sz;
}
void insertGender(char gendr){
gender = gendr;
}
void insertColor(string colour){
color = colour;
}
void insertSleeveType(string clev){
sleeveType = clev;
}
void insertFading(char fadin){
fading = fadin;
}
void insertStretch(char strch){
stretch = strch;
}
void insertShrinks(char shrnk){
shrinks = shrnk;
}
void insertWarms(char warm){
warms = warm;
}
void insertReflect(char reflet){
reflect = reflet;
}
void insertSoaks(char sok){
soaks = sok;
}
void displayShirtDetails(){
cout<<"Size : "<<sizek<<endl;
cout<<"Gender : "<<gender<<endl;
cout<<"Color : "<<color<<endl;
cout<<"Sleeve Type : "<<sleeveType<<endl;
cout<<"Fading : "<<fading<<endl;
cout<<"Stretching : "<<stretch<<endl;
cout<<"Shrinking : "<<shrinks<<endl;
cout<<"Warming : "<<warms<<endl;
cout<<"Reflecting : "<<reflect<<endl;
cout<<"Soaking : "<<soaks<<endl;
}
};
int main()
{
shirt obj;
/* NOTE : since u made member function for data initialization
so initialize with member function
And also u declared data member private (which is a good practice)
so u can not access it from outside directly.
if u want to do so
make data member public also */
cout<<"Enter size of shirt. S, M, or L? :";
char temp;
cin>>temp;
obj.insertSize(temp);
cout<<"Enter gender of shirt. M or F :";
cin>>temp;
obj.insertGender(temp);
cout<<"Enter color of shirt. R, Bl, G, P, Bk, Y, or Br? :";
string tmp;
cin>>tmp;
obj.insertColor(tmp);
cout<<"Enter sleeve type. Ss, Ls, or Ns? :";
cin>>tmp;
obj.insertSleeveType(tmp);
cout<<"Is the shirt fading? Y or N? :";
cin>>temp;
obj.insertFading(temp);
cout<<"Is the object stretched? Y or N? :";
cin>>temp;
obj.insertStretch(temp);
cout<<"Is your object shrinking? Y or N? :";
cin>>temp;
obj.insertShrinks(temp);
cout<<"Is it warming up? Y or N? :";
cin>>temp;
obj.insertWarms(temp);
cout<<"Is it reflecting light? Y or N? :";
cin>>temp;
obj.insertReflect(temp);
cout<<"Is it soaked? Y or N? :";
cin>>temp;
obj.insertSoaks(temp);
obj.displayShirtDetails();
system("PAUSE");
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.