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

C++ Question: Consider the following class called Myscores that stores all the s

ID: 3786406 • Letter: C

Question

C++ Question:

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.

Explanation / Answer

#include <iostream>

using namespace std;

class Myscores
{
private:
int n;
int *scores;
int nScore;
  
public:
Myscores()
{
nScore=0;
}
Myscores(int n)
{
scores = new int[n];
}
~Myscores()
{
delete[] scores;
}
Myscores(const Myscores &m)
{
nScore=m.nScore;
  
if(m.scores)
{
scores= new int[nScore];
  
for(int i=0;i<nScore;i++)
{
scores[i]=m.scores[i];
}
}
else
scores=0;
}
void addScore(int newscore)
{
scores[nScore] = newscore;
nScore++;
}
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote