Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

apparel.h #ifndef APPAREL_H #define APPAREL_H #define MAX_STR 200 class Apparel

ID: 3860734 • Letter: A

Question

apparel.h

#ifndef APPAREL_H
#define APPAREL_H

#define MAX_STR 200

class Apparel
{
private:
char name[200];
char type[200];
char color[200];
float size; // float integer
int stock; // positive integer

public:
Apparel();
Apparel(char*, char* , char*, float, int);
void setName(char *);
void setType(char *);
void setColor(char *);
void setSize(float);
void setInStock(int);
void getName(char *);
void getType(char *);
void getColor(char *);
float getSize();
int getInStock();
void print();
};

#endif

apparel.cpp

#include <iostream>

#include "apparel.h"

using namespace std;

Apparel::Apparel()

{

name[0] = '';

type[0] = '';

color[0] = '';

size = 0;

stock = 0;

}

Apparel::Apparel(char tname[], char ttype[], char tcolor[], float tsize = 0, int tstock = 0)

{

setName(tname);

setType(ttype);

setColor(tcolor);

size = tsize;

stock = tstock;

}

void Apparel::setName(char tname[])

{

int i = 0;

while(tname[i] != '')

{

name[i] = tname[i];

i++;

}

name[i] = '';

}

void Apparel::setType(char ttype[])

{

int i = 0;

while(ttype[i] != '')

{

type[i] = ttype[i];

i++;

}

type[i] = '';

}

void Apparel::setColor(char tcolor[])

{

int i = 0;

while(tcolor[i] != '')

{

color[i] = tcolor[i];

i++;

}

color[i] = '';

}

void Apparel::setSize(float tsize)

{

if (tsize < 0)

{

cout << "Invalid size. Needs to be larger than or equal to 0." << endl;

}

else

{

size = tsize;

}

}

void Apparel::setInStock(int tstock)

{

if (tstock < 0)

{

cout << "Invalid stock. Needs to be larger than or equal to 0." << endl;

}

else

{

stock = tstock;

}

}

void Apparel::getName(char tname[])

{

int i = 0;

while(name[i] != '')

{

tname[i] = name[i];

i++;

}

tname[i] = '';

}

void Apparel::getColor(char tcolor[])

{

int i = 0;

while(color[i] != '')

{

tcolor[i] = color[i];

i++;

}

tcolor[i] = '';

}

void Apparel::getType(char ttype[])

{

int i = 0;

while(type[i] != '')

{

ttype[i] = type[i];

i++;

}

ttype[i] = '';

}

float Apparel::getSize()

{

return size;

}

int Apparel::getInStock()

{

return stock;

}

void Apparel::print()

{

cout << "======Apparel======" << endl;

cout << "name: " << name << endl;

cout << "type: " << type << endl;

cout << "color: " << color << endl;

cout << "size: " << size << endl;

cout << "in stock: " << stock << endl;

};

deptmain.cpp

#include <cstdlib>

#include <iostream>

#include <cstring>

#include "apparel.h"

using namespace std;

int main()

{

Apparel array[2]; // note that this uses the default constructor

  

char name[MAX_STR];

char type[MAX_STR];

char color[MAX_STR];

float size;

int stock;

  

cout << "Just after array is created" << endl;

for(int i=0; i<2; i++)

{

cout << "printing " << i << " apparel in array" << endl;

array[i].print();

}

  

// note that this uses the non-default constructor

Apparel shoe1("Keds Tennis Shoes","shoe","white", 7.5, 6);

  

cout << "Printing the shoe1 info " << endl;

shoe1.print();

  

cout << "Changing size to -3 (this should print an error and not change the value)" << endl;

shoe1.setSize(-3);

cout << "size is now " << shoe1.getSize() << endl;

  

cout << "setting array values and getting values of array" << endl;

for(int i=0; i<2; i++)

{

cout << "for apparel " << i << endl;

cout << "name?";

cin.getline(name, MAX_STR);

array[i].setName(name);

cout << "type?";

cin.getline(type, MAX_STR);

array[i].setType(type);

cout << "color?";

cin.getline(color, MAX_STR);

array[i].setColor(color);

cout << "size?";

cin >> size;

cin.ignore();

array[i].setSize(size);

cout << "how many in stock?";

cin >> stock;

cin.ignore();

array[i].setInStock(stock);

}

  

for(int i=0; i<2; i++)

{

cout << "getting apparel " << i << " information " << endl;

array[i].getName(name);

cout << "name: " << name << endl;

array[i].getType(type);

cout << "type: " << type << endl;

array[i].getColor(color);

cout << "color: " << color << endl;

cout << "size: " << array[i].getSize() << endl;

cout << "in stock: " << array[i].getInStock() << endl;

}

return 0;

}

Just edit deptmain.cpp and leave apparel.h and apparel.cpp as it is to fulfill the requirements

We now want make the deptmain.cpp file more dynamic in how many apparel items to create Note that we don't want a fixed array size; we want to only allocate enough memory for the exact number of items we have. So first your program should ask the user how many items. Then it should create an array of Apparel objects of exactly that size To create a dynamic array, start by declaring the array as a pointer Apparel* list; Then create the array of Apparel objects after asking the size of the array: list = new Apparel [size]; Then use a for loop to ask for the information for each item. Then you will need to call the setters for each field (name, type, color, size and stock) to set the values in the Apparel object. Now, again using another for loop, print out each apparel item in the list using the print function. When done printing, remember to delete the list: delete [1 list; Make C-strings dynamic inside Apparel class To make the program fully dynamic, we should also create the C-strings dynamically. Start by changing the name, type and color member variables to pointer to char: char* name: II and so on.. . Then in the constructor, set these to NULL. This will help determine if these have been previously set to a value name NULL; // and so on.. Now, in the getters, we don't need to copy into a passed in C-string: we can now

Explanation / Answer

//Changed apparel.h file to handle C-String dynamic array instead of char array, u can find code changes with //cheggEA

//apparel.h

//apparel.h

#ifndef APPAREL_H

#define APPAREL_H

#define MAX_STR 200

class Apparel

{

private:

//Make C-Strings as dynamic, cheggEA

//char name[200];

char *name;

//char type[200];

char *type;

//char color[200];

char *color;

float size; // float integer

int stock; // positive integer

public:

Apparel();

Apparel(char*, char*, char*, float, int);

void setName(char *);

void setType(char *);

void setColor(char *);

void setSize(float);

void setInStock(int);

void getName(char *);

void getType(char *);

void getColor(char *);

float getSize();

int getInStock();

void print();

};

#endif

---------------------------------------

//apparel.cpp

#include <iostream>

#include "apparel.h"

using namespace std;

Apparel::Apparel()

{

//commented blow code and initializd dynamic C-string with NULL, CheggA

/*name[0] = '';

type[0] = '';

color[0] = '';*/

name = 0;

type = 0;

color = 0;

size = 0;

stock = 0;

}

Apparel::Apparel(char tname[], char ttype[], char tcolor[], float tsize = 0, int tstock = 0)

{

setName(tname);

setType(ttype);

setColor(tcolor);

size = tsize;

stock = tstock;

}

void Apparel::setName(char tname[])

{

int i = 0;

//Added by cheggEA

if (name != NULL)

delete name;

//Allocate memory for Dynamic C-String bfore calling set Function for stting nam,type and color, CheggA

name = new char[strlen(tname) + 1];

while (tname[i] != '')

{

name[i] = tname[i];

i++;

}

name[i] = '';

}

void Apparel::setType(char ttype[])

{

int i = 0;

//Added by cheggEA

if (type != NULL)

delete type;

//Allocate memory for Dynamic C-String bfore calling set Function for stting nam,type and color, CheggA

type = new char[strlen(ttype) + 1];

while (ttype[i] != '')

{

type[i] = ttype[i];

i++;

}

type[i] = '';

}

void Apparel::setColor(char tcolor[])

{

int i = 0;

//Added by cheggEA

if (color != NULL)

delete color;

//Allocate memory for Dynamic C-String bfore calling set Function for stting nam,type and color, CheggA

color = new char[strlen(tcolor) + 1];

while (tcolor[i] != '')

{

color[i] = tcolor[i];

i++;

}

color[i] = '';

}

void Apparel::setSize(float tsize)

{

if (tsize < 0)

{

cout << "Invalid size. Needs to be larger than or equal to 0." << endl;

}

else

{

size = tsize;

}

}

void Apparel::setInStock(int tstock)

{

if (tstock < 0)

{

cout << "Invalid stock. Needs to be larger than or equal to 0." << endl;

}

else

{

stock = tstock;

}

}

void Apparel::getName(char tname[])

{

int i = 0;

while (name[i] != '')

{

tname[i] = name[i];

i++;

}

tname[i] = '';

}

void Apparel::getColor(char tcolor[])

{

int i = 0;

while (color[i] != '')

{

tcolor[i] = color[i];

i++;

}

tcolor[i] = '';

}

void Apparel::getType(char ttype[])

{

int i = 0;

while (type[i] != '')

{

ttype[i] = type[i];

i++;

}

ttype[i] = '';

}

float Apparel::getSize()

{

return size;

}

int Apparel::getInStock()

{

return stock;

}

void Apparel::print()

{

cout << "======Apparel======" << endl;

cout << "name: " << name << endl;

cout << "type: " << type << endl;

cout << "color: " << color << endl;

cout << "size: " << size << endl;

cout << "in stock: " << stock << endl;

};

------------------------------------------------------------------------------------------------------------------------------------

//main.cpp

//deptmain.cpp

#include <cstdlib>

#include <iostream>

#include <cstring>

#include "apparel.h"

using namespace std;

int main()

{

Apparel *list; // note that this uses the default constructor

char name[MAX_STR];

char type[MAX_STR];

char color[MAX_STR];

float size;

int stock;

//Chegg Ea, declare n for number of items

int n;

cout << "How many apparel items: " ;

cin >> n;

//Chegg EA, Allocate memory for Dynamic object of type Apparel

list = new Apparel[n];

for (int i = 0; i<n; i++)

{

/*cout << "printing " << i << " apparel in array" << endl;

array[i].print();*/

//Cheg EA, Added below lines of code

cout << "for apparel " << i << endl;

cin.ignore();

cout << "name?" ;

cin.getline(name, MAX_STR);

cout << "type?" ;

cin.getline(type, MAX_STR);

cout << "color?" ;

cin.getline(color, MAX_STR);

cout << "size?" ;

cin >> size;

cout << "how many in stock?" ;

cin >> stock;

list[i].setName(name);

list[i].setType(type);

list[i].setColor(color);

list[i].setSize(size);

list[i].setInStock(stock);

}

for (int i = 0; i < n; i++)

{

cout << "=========Apparel==========" << endl;

list[i].getName(name);

cout << "name: " << name << endl;

list[i].getType(type);

cout << "type: " << type << endl;

list[i].getColor(color);

cout << "color: " << color << endl;

cout << "size: " << list[i].getSize() << endl;

cout << "in stock: " << list[i].getInStock() << endl;

}

//delete dynimically allocated object list

delete[] list;

cout << "deleting the array..." << endl;

return 0;

}

---------------------------------------------------------------------------------------------------------------------------

//output

How many apparel items: 3
for apparel 0
name?Jean's Long Socks
type?socks
color?red and green
size?9
how many in stock?20
for apparel 1
name?Lui's Sandals
type?shoe
color?Beige
size?8.5
how many in stock?10
for apparel 2
name?Levi's Jeans
type?pants
color?stone-washed
size?14
how many in stock?7
=========Apparel==========
name: Jean's Long Socks
type: socks
color: red and green
size: 9
in stock: 20
=========Apparel==========
name: Lui's Sandals
type: shoe
color: Beige
size: 8.5
in stock: 10
=========Apparel==========
name: Levi's Jeans
type: pants
color: stone-washed
size: 14
in stock: 7
deleting the array...