C++ Advanced Programming
CSIS 112
C++ Programming: Dynamic Memory Assignment Instructions
Overview
The objective of this lab is to demonstrate dynamic allocation and deallocation of memory. This program leverages functionality that is useful in real world applications where information is not always available at design time, and structures must be created at run time. This program also highlights the need for effective memory management to prevent issues with memory leaks, security, and overall program performance.
Instructions
Write a class called Game that contains a video game’s name, genre, and difficulty Level. Include a default constructor and destructor for the class. The constructor should print out the following message: “Creating a new game”. The destructor should print out the following message: “In the Game destructor.” Include appropriate get/set functions for the class.
In main(), prompt the user to enter the number of games he or she has played in the past year. Dynamically create a built-in array based on this number (not a vector or object of the array class) to hold pointers to Game objects.
Construct a loop in main() that executes once for each of the number of games that the user indicated. Within this loop, ask the user to enter the name and genre of each game. Using a random number generator, generate a difficulty Level between 1-10 (inclusive). Seed this random number generator with 100. Next, dynamically create a Game object (remember that this requires the use of the “new” keyword which returns a pointer to the location in memory where this game object was created.) Create each object using the default constructor of the class, and call the set functions to store the name, genre, and difficulty level of each game. Store each Game pointer in the array.
After all of the Game objects have been constructed and added to the array, print out the contents of the array.
Because the program uses dynamic memory to store the array as well as the objects in the array, be sure to de-allocate all of the memory before exiting.
A sample of the program running is shown below:
Deliverables :
· Complete the programming assignment described above and submit your completed assignment in accordance with the assignment submission policies.
To give you an idea of the general criteria that will be used for grading, here is a checklist that you might find helpful:
|
Compiles and Executes without crashing |
|
Word document contains screen shots and integrity statements |
|
Appropriate internal documentation |
|
Style: |
|
No global variables |
|
Code is modular |
|
Appropriate Pre-processing and using directives |
|
Member functions and variables are declared with appropriate protection (i.e. private or public) |
|
Three separate files are created for the program: Game.h(header file), Game.cpp (class implementation file), and GameDriver.cpp (driver file) |
|
Requirements: |
|
Class Creation |
|
Data members: name, genre, difficultyLevel |
|
Constructor/destructor |
|
Getters/setters as appropriate |
|
Main: The following items must be implemented in main() or by functions called from main() They must not be implemented in the Game class |
|
Prompts user for the number of games |
|
Creates the game array using dynamic memory allocation |
|
Dynamically creates game objects and correctly stores them in the array |
|
Generates a random difficulty level (between 1-10 inclusive) for each game Seeds the random number generator appropriately |
|
De-allocates memory (both the array and the contents of the array) before exiting |
|
Processing and Outputs |
|
Prints contents of array as illustrated in diagram |
Page 2 of 2