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

Instead of copying all characters from one to another with get and set how would

ID: 3598744 • Letter: I

Question

Instead of copying all characters from one to another with get and set how would I use strcpy instead?

public:

Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr);

Character(), ~Character();

void getName(char *name);

void setName(char *name);

void getClass(int& cl);

void setClass(int cl);

void getAlignment(int& al);

void setAlignment(int al);

void getHitPoints(int& hp);

void setHitPoints(int hp);

void getStrength(int *str);

void setStrength(int str);

void getDexterity(int *dex);

void setDexterity(int dex);

void getConstitution(int *cn);

void setConstitution(int cn);

void getIntelligence(int *itl);

void setIntelligence(int itl);

void getWisdom(int *wis);

void setWisdom(int wis);

void getCharisma(int *chr);

void setCharisma(int chr);

void printAll();

};

Explanation / Answer

Need more details. Do you want to copy the name from input parameter to the private class member variable ?

Case1 : If the class Character has a fixed size array say name[50] or something like that, you can use

Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr)

{

strcpy(this->name, name);

//assign other members

}

In this case destructor does not have to do anyting.

But if name is declared as a pointer like char *name inside Character class, then you will use

Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr)

{

this->name = new char[strlen(name) +1];

strcpy(this->name, name);

//assignm other members

}

In this case, the destructor will have to free the dynamic memory created. The destructor for case 2 will look like

~Character(){delete [] name;}

For both the cases , you will need to #include <cstring>