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

In c++ required condition, Complete the GameEntry class by referring to the prog

ID: 3707496 • Letter: I

Question

In c++

required condition,

Complete the GameEntry class by referring to the program described in Code 3.1, Code 3.2. However, design and implement the header file and the implementation file separately.

Complete the Scores class for best grades in video games with reference to 3.3 to 3.6. However, design and implement the header file and the implementation file separately.

Refer to the main () function and add member functions of the Scores class

3.1

class GameEntry{

public:

GameEntry(const string& n="", int s=0);

string getName() const;

int getScore() const;

private:

string name;

int score;

};

3.2

GameEntry::GameEntry(const string& n, int s)

: name(n), score(s){}

stringGameEntry::getName() const {return name;}

int GameEntry::getScore() const{return score}

3.3

class Scores{

public:

Scores(int maxEnt = 10);

~Scores();

void add(const GameEntry& e);

GameEntry remove(int i)

throw(IndexOutOfBounds);

private:

int maxEntries;

int numEntries;

GameEntry* entries;

};

3.4

Score::Scores(int maxEnt){

maxEntries = maxEnt;

entries = new GameEntry[maxEntires];

numEntries = 0;

}

Score::~Scores(){

delete[] entries;

}

3.5

void Scores::add(const GameEntry& e){

int newScore = e.getScore();

if (numEntries == maxEntries){

if(newScore <= entries[maxEntries-1].getScore())

return;

}

else numEntries++;

int i =numEntries-2++;

while(i>=0 && newScore > entries[i].getScore(){

entries[i+1]= entries[i];

i--;

}

entries[i+1] = e;

}

3.6

GameEntry Score::remove(int i) throw(IndexOutOfBounds){

if((i<0)||(i>=numEntries))

throw IndexOutOfBounds("Invalid index");

GameEntry e = entries[i];

for(int j = i+1; j < numEntries;j++)

entries[j-1] = entries[j];

numEntries--;

return e;

}

void main() {

Scores newList(20);

GameEntry item1("Rob", 750); newList.add(item1);

GameEntry item3("Anna", 660); newList.add(item3);

GameEntry item5("Jack", 510); newList.add(item5);

GameEntry item4("Rose", 590); newList.add(item4);

GameEntry item2("Paul", 720); newList.add(item2);

GameEntry item0("Mike", 1105); newList.add(item0);

cout << "After inserting initial items " << endl; newList.printAll();

GameEntry newItem("Jill", 740); newList.add(newItem);

Scores test(newList);

GameEntry item6("Park", 595); test.add(item6);

GameEntry item7("Lee", 725); test.add(item7);

GameEntry item8("Choi", 1115); test.add(item8);

GameEntry item9("Cho", 110); test.add(item9);

newList = test;

cout << "After copying, insertions, and assignment " << endl; newList.printAll();

GameEntry deletedItem = newList.remove(13);

cout << "After delete 14th item " << endl; newList.printAll();

Explanation / Answer

// File Name: GameEntry.h
#ifndef GAMEENTRY_H
#define GAMEENTRY_H
#include <string>
using namespace std;

// Class GameEntry definition
class GameEntry
{
public:
// Prototype of member function
GameEntry(const string& n="", int s=0);
string getName() const;
int getScore() const;
void setName(string);
void setScore(int);
private:
// Private data member to store data
string name;
int score;
};// End of class

#endif

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

// File Name: GameEntry.cpp
#include "GameEntry.h"
using namespace std;

// Parameterized constructor to assign parameter to data member
GameEntry::GameEntry(const string& n, int s) : name(n), score(s)
{}

// Function to return name
string GameEntry::getName() const
{
return name;
}// End of function

// Function to return score
int GameEntry::getScore() const
{
return score;
}// End of function

// Function to set name
void GameEntry::setName(string na)
{
name = na;
}// End of function

// Function to set score
void GameEntry::setScore(int sco)
{
score = sco;
}// End of function

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

// File Name: Scores.h
#ifndef SCORES_H
#define SCORES_H
#include "GameEntry.h"
using namespace std;

// Class IndexOutOfBounds definition
class IndexOutOfBounds
{
public:
// Parameterized constructor
IndexOutOfBounds(string);
};// End of class

// Class Scores definition
class Scores
{
public:
// Prototype of member functions
Scores(int maxEnt = 10);
Scores(const Scores &sco);
~Scores();
void add(const GameEntry& e);
GameEntry remove(int i) throw(IndexOutOfBounds);
void printAll();
void operator = (const Scores &test);

private:
// Private data member to store data
int maxEntries;
int numEntries;
GameEntry *entries;
};// End of class

#endif

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

// File Name: Scores.cpp
#include<iostream>
#include<stdlib.h>
#include "Scores.h"
#include "GameEntry.cpp"
using namespace std;

// Parametrized constructor definition
Scores::Scores(int maxEnt)
{
// Assigns the parameter value maximum size to data member
maxEntries = maxEnt;
// Dynamically allocates memory to entries pointer
entries = new GameEntry[maxEntries];
// Sets the number of entries to zero
numEntries = 0;
}// End of parametrized constructor

// Copy constructor definition
Scores::Scores(const Scores &sco)
{
// Assigns the parameter object sco maximum size to implicit object data member
maxEntries = sco.maxEntries;
// Dynamically allocates memory to implicit object's entries pointer of maxEntries size
entries = new GameEntry[maxEntries];
// Assigns sco object's number of entries of to implicit object's number of entries
numEntries = sco.numEntries;

// Loops till number of records in object sco
for(int x = 0; x < sco.numEntries; x++)
{
// Extracts sco object's entries x index position name by calling getName() function
// and assigns it to implicit object's entries x index position name by calling setName() function
entries[x].setName(sco.entries[x].getName());

// Extracts sco object's entries x index position score by calling getName() function
// and assigns it to implicit object's entries x index position score by calling setName() function
entries[x].setScore(sco.entries[x].getScore());
}// End of for loop
}// End of copy constructor

// Overloads = operator
void Scores::operator = (const Scores &test)
{
// Assigns the parameter object test maximum size to implicit object data member
maxEntries = test.maxEntries;
// Dynamically allocates memory to implicit object's entries pointer of maxEntries size
entries = new GameEntry[maxEntries];
// Assigns test object's number of entries of to implicit object's number of entries
numEntries = test.numEntries;

// Loops till number of records in object test
for(int x = 0; x < test.numEntries; x++)
{
// Extracts test object's entries x index position name by calling getName() function
// and assigns it to implicit object's entries x index position name by calling setName() function
entries[x].setName(test.entries[x].getName());

// Extracts test object's entries x index position score by calling getName() function
// and assigns it to implicit object's entries x index position score by calling setName() function
entries[x].setScore(test.entries[x].getScore());
}// End of for loop
}// End of function

// Destructor definition
Scores::~Scores()
{
delete[] entries;
}// End of destructor

// Function to add a GameEntry object
void Scores::add(const GameEntry& e)
{
// Extracts the sore of parameter object e
int newScore = e.getScore();
// Checks if the number of entries is equals to maximum entries
if (numEntries == maxEntries)
{
// Checks if the new score is less than or equals to the implicit object's maximum entries minus one index position score
if(newScore <= entries[maxEntries-1].getScore())
return;
}// End of if condition
// Otherwise
else
// Increase the number of entries by one
numEntries++;
// Stores number of entries -2 value to i
int i = numEntries - 2;

// Loops till i value is greater than zero and
// new score is greater than the implicit object's entries i index position score
while(i >= 0 && newScore > entries[i].getScore())
{
// Assigns the entries i index position data to entries next index position of i
entries[i+1]= entries[i];
// Decrease the i value by one
i--;
}// End of while loop
// Assigns the parameter object e to implicit object's entries next index position of i
entries[i + 1] = e;
}// End of function

// Function to delete a record and throws exception if invalid index is given
GameEntry Scores::remove(int i) throw (IndexOutOfBounds)
{

// Checks if the parameter i value is negative or greater than or equals to current number of entries
if((i < 0) || (i >= numEntries))
// Throws an exception
throw IndexOutOfBounds("Invalid index");
// Stores entries i index position object in GameEntry object e
GameEntry e = entries[i];

// Loops till number of entries
for(int j = i + 1; j < numEntries; j++)
// Moves all the entries one position back
entries[j-1] = entries[j];
// Decrease the current number of entries by one
numEntries--;
// Returns the deleted object
return e;
}// End of function

// Function to print all the objects
void Scores::printAll()
{
// Loops till number of entries
for(int x = 0; x < numEntries; x++)
{
// Displays the name and scores
cout<<" Name: "<<entries[x].getName();
cout<<" Score: "<<entries[x].getScore();
}// End of for loop
}// End of function

// Parameterized constructor to display error
IndexOutOfBounds::IndexOutOfBounds(string error)
{
cout<<endl<<error;
//exit(0);
}// End of constructor

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

// File Name: GameEntryDriver.cpp
#include<iostream>
#include "scores.cpp"
using namespace std;

// main function definition
int main()
{
// Creates a Score class object of size 20 using parameterized constructor
Scores newList(20);
// Creates GameEntry class object using parameterized constructor
GameEntry item1("Rob", 750);
// Adds the objects to Scores class object
newList.add(item1);
GameEntry item3("Anna", 660);
newList.add(item3);
GameEntry item5("Jack", 510);
newList.add(item5);
GameEntry item4("Rose", 590);
newList.add(item4);
GameEntry item2("Paul", 720);
newList.add(item2);
GameEntry item0("Mike", 1105);
newList.add(item0);

cout << " After inserting initial items " << endl;
// Calls the function to display records
newList.printAll();

GameEntry newItem("Jill", 740);
newList.add(newItem);

// Calls the copy constructor
Scores test(newList);

GameEntry item6("Park", 595);
test.add(item6);
GameEntry item7("Lee", 725);
test.add(item7);
GameEntry item8("Choi", 1115);
test.add(item8);
GameEntry item9("Cho", 110);
test.add(item9);

// Assigns object test to newList using = operator overloading
newList = test;
cout << " After copying, insertions, and assignment " << endl;
newList.printAll();

// try block begins
try
{
// Calls the function to delete a record
GameEntry deletedItem = newList.remove(2);
cout << " After delete 2nd item " << endl;
newList.printAll();
deletedItem = newList.remove(14);
}// End of try block
// Catch block to handle the exception
catch(IndexOutOfBounds ie)
{
// Calls the parameterized constructor using anonymous object
new IndexOutOfBounds("Index out of bound exception.");
}// End of catch block
cout << " After delete 14th item " << endl;
newList.printAll();
return 0;
}// End of main function

Sample Output:

After inserting initial items

Name: Mike Score: 1105
Name: Rob Score: 750
Name: Paul Score: 720
Name: Anna Score: 660
Name: Rose Score: 590
Name: Jack Score: 510

After copying, insertions, and assignment

Name: Choi Score: 1115
Name: Mike Score: 1105
Name: Rob Score: 750
Name: Jill Score: 740
Name: Lee Score: 725
Name: Paul Score: 720
Name: Anna Score: 660
Name: Park Score: 595
Name: Rose Score: 590
Name: Jack Score: 510
Name: Cho Score: 110

After delete 2nd item

Name: Choi Score: 1115
Name: Mike Score: 1105
Name: Jill Score: 740
Name: Lee Score: 725
Name: Paul Score: 720
Name: Anna Score: 660
Name: Park Score: 595
Name: Rose Score: 590
Name: Jack Score: 510
Name: Cho Score: 110
Invalid index
Index out of bound exception.

After delete 14th item

Name: Choi Score: 1115
Name: Mike Score: 1105
Name: Jill Score: 740
Name: Lee Score: 725
Name: Paul Score: 720
Name: Anna Score: 660
Name: Park Score: 595
Name: Rose Score: 590
Name: Jack Score: 510
Name: Cho Score: 110

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