Question

Create a class named Game using the following UML class diagram. GAME -gameName : string +Game()...

Create a class named Game using the following UML class diagram.

GAME

-gameName : string

+Game()

+setGameName(in name : string) : void

+getGameName() : string

+displayMessage() : void

This class has 2 member variables namely playerName and playerScore. The class contain following member functions: Constructor, set function, get functions, display function.

Code write in 3 files: Game.h header file, funtion.cpp file and main.cpp file.

The output should be: Player John has scored 50 points.

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

//Game.h
#ifndef GAME_H
#define GAME_H
#include<iostream>
#include<string>
using namespace std;
class Game
{
private:
   //private data members
   string playerName;
   int playerScore;
public:
   //public function declarations
   Game();
   Game(string,int);
   void setGameName(string);
   string getGameName();

   void setGameScore(int);
   int getGameScore();

   void displayMessage();
};
#endif GAME_H

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

//Game.cpp
//include header files
#include<iostream>
#include<string>
#include "Game.h"
using namespace std;
Game::Game()
{
   playerName="";
   playerScore=0;
}
Game::Game(string pName,int pScore)
{
   playerName=pName;
   playerScore=pScore;
}
void Game::setGameName(string pName)
{
   playerName=pName;
}
string Game::getGameName()
{
   return playerName;
}
void Game::setGameScore(int pScore)
{
   playerScore=pScore;
}
int Game::getGameScore()
{
   return playerScore;
}
void Game::displayMessage()
{
   cout<<"Player "
       <<playerName
       <<" has scored "
       <<playerScore<<" points."
       <<endl;
}

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

/*
Cpp file that tests Game class
and print the message output on
console.
*/
//main.cpp
//include header files
#include<iostream>
#include"Game.h"
using namespace std;
//start of main function
int main()
{
   //Declare Game class object
   Game game;
   //Calling set methods
   //To set name
   game.setGameName("John");
   //To set score value
   game.setGameScore(50);
   //calling displayMessage method
   game.displayMessage();
   //pause program output on console
   system("pause");
   return 0;
}

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

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Create a class named Game using the following UML class diagram. GAME -gameName : string +Game()...
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
  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • 1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Computer - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - an int variable named year - a string variable named model - a string variable named purpose 1c Add the following public functions: - Default constructor which sets numeric members to -1 and string members to the empty string - Destructor to print "Removing instance from memory" - A non-default constructor to set the year and...

  • How do I format this into C++? This the prompt: Design a class named Password that...

    How do I format this into C++? This the prompt: Design a class named Password that stores a password in a c-string and has member functions to test if the password complies with certain requirements as follows: The password should be between 6 and 20 characters long. The password should contain at least one uppercase and at least one lowercase letter. The password should have at least one digit. The password should have at least one punctuation character. Define a...

  • Using C++, Write a class named Employee that has the following member variables: name. A string...

    Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...

  • 1) This exercise is about Inheritance (IS-A) Relationship. A) First, draw the UML diagram for class...

    1) This exercise is about Inheritance (IS-A) Relationship. A) First, draw the UML diagram for class Student and class ComputerSystemsStudent which are described below. Make sure to show all the members (member variables and member functions) of the classes on your UML diagram. Save your UML diagram and also export it as a PNG. B) Second, write a program that contains the following parts. Write each class interface and implementation, in a different .h and .cpp file, respectively. a) Create...

  • Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string...

    Here is the UML for the class Contact -fullName : string -phoneNum: string -emailAddress : string +Contact()                     //default constructor sets all attribute strings to “unknown”; no other constructor +setName(string name) : void +setPhone(string pn) : void +setEmail(string em) : void +getName() : string +getPhone() : string +getEmail() : string +printContact() : void         //print out the name, phone numbers and email address Create New Project Name your project: CIS247_Lab7_YourLastName. For this exercise, you may use a header file or one file...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • *using c++* Construct a class named Savings containing three floating-point data members named balance, rate, and...

    *using c++* Construct a class named Savings containing three floating-point data members named balance, rate, and interest and a constructor that initializes each data member to 0. This class should be established with typical two files(.h and .cpp). Include a member function that inputs a balance and rate and then calculates an interest. The rate should be stored as a   percent, such   as   6.5   for   6.5%,   and   the   interest   is   computed   as   interest   = (balance)(rate/100). Add a member function to...

  • c++ I need help! create Student.h In this class, you are provided with a class skeleton...

    c++ I need help! create Student.h In this class, you are provided with a class skeleton for the Student type. This type should contain two member variables: a string called name a vector of doubles called grades Additionally, you should declare the following functions: A constructor which accepts a single string parameter called name A void function, addGrade which accepts a single double parameter and adds it to the grades vector A function which accepts no parameters and returns the...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

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
ADVERTISEMENT