Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

8. The Game Class We will be creating and using a class to keep track of your score when you play board games. In Questions

8.

The Game Class

We will be creating and using a class to keep track of your score when you play board games.

In Questions 8-13, you will be writing the code that would go in the following files.

  • game.cpp
  • game.h
  • main.cpp

To Do:

Write class declaration for the Game class that includes the following.

o a private data member called my_score_ that is an integer

o a default constructor

make this an inline method

have it set my_score_ to zero

// game.h : C++ header file to contain declaration for Game class

#ifndef GAME_H

#define GAME_H

class Game

{

private:

int my_score_; // data member

public:

// default constructor to set my_score_ to zero

Game(){ my_score_ = 0;}

};

#endif

// end of game.h

9.

This declaration would go in the file game.h. You can assume the following code is already in game.h. You will need to add the code listed above.

#ifndef GAME_H

#defineGAME_H

#endif

10.

Add the following to the Game class declared in the previous question.You do not need to rewrite the code from previous questions. Just write the code listed in the To Do section of this question.

To Do:

  • Write setter called set_score for the my_score_ data member. The value of my_score_ must be zero or greater. If a value sent is less than zero, set my_score_ to zero.
    • write the method declaration that would go in the game.h file
    • write the method implementation that would go in the game.cpp file

11.

Add the following to the Game class declared in the previous questions. You do not need to rewrite the code from previous questions. Just write the code listed in the To Do section of this question.

To Do:

  • Write second constructor that takes an input parameter of type int which sets my_score_ to the value sent.
    • write the method declaration that would go in the game.h file
    • write the method implementation that would go in the game.cpp file
      • use the set_score method to make sure the score is always positive

12.

Add the following to the Game class declared in the previous questions. You do not need to rewrite the code from previous questions. Just write the code listed in the To Do section of this question.

To Do:

  • write getter called get_score that returns the value of my_score_. You can choose whether to make this an inline method or not.
    • If it is an inline method
      • write the method implementation that would go in the game.h file
    • If it is not an inline method
      • write the method declaration that would go in the game.h file
      • write the method implementation that would go in the
      • game.cpp file

13.

Add the following to the Game class declared in the previous questions. You do not need to rewrite the code from previous questions. Just write the code listed in the To Do section of this question.

To Do:

  • make method called Roll. This method is to select a random number in the range of 1- 6 and update my_score_ by adding the random number to the current score.
    • write the method declaration that would go in the game.h file
    • write the method implementation that would go in the game.cpp file
      • use the std::rand() function to select a random number
      • make sure the random number is in the range of 1- 6 (including both 1 and 6)

14.

create/ use Game objects. This is code that would be in main.cpp.

  • Using the default constructor, create Game object and print out the initial score
  • Using this object, roll twice and print out the current score after each roll
  • Using second constructor, create Game object with a score other than zero and print out the initial score
  • Using this second object, roll twice and print out the current score after each roll
  • When outputting any value, include a label that identifies what the value is

Sample output from main:

// Using default constructor Initial score is 0 After one roll the current score is 2

After a second roll the current score is 7

// Using the non-default constructor Initial score 10

After one roll the current score is 14

After a second roll the current score is 19

This code would go in the file main.cpp. You can assume the following code is already in main.cpp. You will need to add the code listed above.

#include

#include"game.h"

using namespace std;

int main() {

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions