Please answer part i and ii separately. Also, try to provide comment for each li
ID: 3789244 • Letter: P
Question
Please answer part i and ii separately. Also, try to provide comment for each line.
Part A: Consider the following class called Myscores that stores all the scores for a game.
class Myscores {
public:
Myscores() { // constructor
nScores = 0;
}
void addScore(int newscore) {
score[nScores] = newscore;
nScores++;
}
private:
int score[10];
int nScores; // number of scores stored
};
i) The score member variable can store at most 10 scores. Change the code so that score can store as many scores as needed when a Myscores object is created. (Hint: use dynamic memory). Change/add constructor and destructors as needed.
ii) Add a copy constructor for the above case.
Part A Consider the following class called Mys that stores all the scores for a scores game. class My scores public: My scores constructor Scores 0; void addscore (int new score score In Scores news core; Scores private int score [101 int n es number of scores stored i) The score member variable can store at most 10 scores. Change the code so that score can store as many scores as needed when aMyscores object is created. (Hint: use dynamic memory. Change/add constructor and destructors as needed ll Add a copy constructor for the above case.Explanation / Answer
i) we have done the changes for dynamic memory allocation whenever create Myscore object by using perameter constructor
class Myscores
{
public:
Myscores() // this is default constructor
{
nScores = 0;
}
Myscores(int len) // this is perametor constructor
{
score = new int; /allocate memory for pointer
*score = len; // copy the length of the array
}
void addScore(int newscore) //this is function
{
score[nScores] = newscore;
nScores++;
}
~Myscores() // this is distructor
{
delete score; //to delete the memory once we complete the usage of the pointer
}
private:
int *score; //need to create pointer to allocate memory dynamicaaly
int nScores; // number of scores stored
};
ii) Copy construct calling in two types
First one is, by initialize oject of a class while creating new object for the same class ,
It means if we take the above one as exmpale then we can call to the copy constructor in main function when ever we create the abject as below
Myscores a[10]; \this will go to parameter constraint
Myscores b(a); \ this will go to copy costructor
Second one is , by assigning operator , we can call copy construct
if we take above one is an example then we can call as below in main function as below
Myscores a[10]; \this will go to parameter constraint
Myscores b = a \ this is also calls copy cosnstructor
Line::Line(const Line &obj) {
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
And if we add in above case then the final code will be as below
class Myscores
{
public:
Myscores() // this is default constructor
{
nScores = 0;
}
Myscores(int len) // this is perametor constructor
{
score = new int; // memory allocate memory for pointer
*score = len; // copy the length of the array
}
Myscores(cons Myscores &obj) // this is copy constructor
{
score = new int; // memory allocate memory for pointer
*score = *obj.score; //copying the value to the array
}
void addScore(int newscore) //this is function
{
score[nScores] = newscore;
nScores++;
}
~Myscores() // this is distructor
{
delete score; //to delete the memory once we complete the usage of the pointer
}
private:
int *score; //need to create pointer to allocate memory dynamicaaly
int nScores; // number of scores stored
};