C Question Consider the following class called Myscores that

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.

Solution

#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++;

}

};

C++ Question: Consider the following class called Myscores that stores all the scores for a game. class Myscores { public: Myscores() { // constructor nScores =
C++ Question: Consider the following class called Myscores that stores all the scores for a game. class Myscores { public: Myscores() { // constructor nScores =

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site