Question

Using your Games class, create an array of Games’ objects on the heap (three for the...

Using your Games class, create an array of Games’ objects on the heap (three for

the SIZE) and then pass the pointer that points to that array to a function that

loads it up with the same info from part A. Print the array’s values using the

pointer. Make sure the program properly uses the accessors and mutators. When

completed the program should cleanup.

//This is my Games class

class Games
{
private:
string gameName, gameType;
float gameCost;
public:
//constructor to initialize all three data members
Games(string gName, string gType, float gCost): gameName(gName), gameType(gType), gameCost(gCost){}

//Get methods for each data members to get values of each data member
string getGameName()
{
return gameName;
}
string getGameType()
{
return gameType;
}
float getGameCost()
{
return gameCost;
}

//Set methods for each data members to change values of each data member
void setGameName(string gName)
{
gameName = gName;
}
void setGameType(string gType)
{
gameType = gType;
}
void setGameCost(float gCost)
{
gameCost = gCost;
}
};

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here is the code. There were some missing parts which was not included in the question like the info from Part A.

Nevertheless, I have coded the loadData() function and the printData() functions. Let me know for any changes or modifications, no need to post the question again, you can comment and I will share the updated code.

Here is the code

==================================================================

# include <iostream>

using namespace std;

class Games

{

private:

string gameName, gameType;

float gameCost;

public:

//constructor to initialize all three data members

Games(string gName, string gType, float gCost): gameName(gName), gameType(gType), gameCost(gCost){}

//Get methods for each data members to get values of each data member

string getGameName()

{

return gameName;

}

string getGameType()

{

return gameType;

}

float getGameCost()

{

return gameCost;

}

//Set methods for each data members to change values of each data member

void setGameName(string gName)

{

gameName = gName;

}

void setGameType(string gType)

{

gameType = gType;

}

void setGameCost(float gCost)

{

gameCost = gCost;

}

};

// loads the data into the objects using the mutator functions

void loadData(Games *pGames, int SIZE) {

               

                string games[] ={"Hockey","BasketBall","Football"};

                string types[] ={"Outdoor","Indoor","Outdoor"};

                float costs[] ={1200,1500,2000};

                for(int i=0; i<SIZE; i++) {

                               

                                (pGames+i)->setGameName(games[i]);

                                (pGames+i)->setGameType(types[i]);

                                (pGames+i)->setGameCost(costs[i]);

                }

}

// prints the data in the objects using the accessor functions

void printData(Games *pGames, int SIZE) {

               

                for(int i=0; i<SIZE; i++) {

                                cout<<"Game #"<<i+1<<" Name = "        

                                <<(pGames+i)->getGameName() <<" Type = "

                                <<(pGames+i)->getGameType() <<" Cost: $ "

                                <<(pGames+i)->getGameCost();

                               

                                cout<<endl;

                }

}

int main(){

               

                const int SIZE = 3;

                Games *pGames = new Games[SIZE]{Games("","",0.0),Games("","",0.0),Games("","",0.0)};

                loadData(pGames,SIZE); // load data

                printData(pGames,SIZE); // print data

               

                delete [] pGames; // release memory from heap

               

}

==================================================================

Add a comment
Know the answer?
Add Answer to:
Using your Games class, create an array of Games’ objects on the heap (three for the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have...

    Min heap class implementation in Python. Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...

  • Java Code: 2. Define a class ACCOUNT with the following members: Private members Acno of type...

    Java Code: 2. Define a class ACCOUNT with the following members: Private members Acno of type int Name of type String Balance of type float Public members void Init) method to enter the values of data members void Show) method to display the values of data members void Deposit(int Amt) method to increase Balance by Amt void Withdraw(int Amt) method to reduce Balance by Amt (only if required balance exists in account) float RBalance() member function that returns the value...

  • Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design...

    Ship, CruiseShip, and CargoShip Classes (in C++ language i use visual studios to code with) design a Ship class that has the following members: - A member variable for the name of the ship (a string) - A member variable for the year that the ship was built (a string) - A contsructor and appropriate accessors and mutators - A virtual print function that displays the ship's name and the year it was built (nobody seems to get this part...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • You are now going to create a LinkedList class, that will work very similarly to the...

    You are now going to create a LinkedList class, that will work very similarly to the Stack class seen in the book (and used in the previous exercise). Then write new methods as follows: add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last...

  • An array of class objects is similar to an array of some other data type. To...

    An array of class objects is similar to an array of some other data type. To create an array of Points, we write Point parray [4]; To access the object at position i of the array, we write parray [i] and to call a method on that object method, we write parray [i]. methodName (arg1 , arg2 , ...) ; To initialize an array of objects whose values are known at compile time, we can write Point parray [4] =...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT