Question
Class Assignment 2 (a): -Game Class: Create a class named Game using the following UML class diagram. Ensure that the program executes with no errors.
Class Assignment 2 (a): -Game Class:
Create a class named Game using the following UML class diagram. Ensure that the program executes with no errors.
Game
-gameName: string
+Game()
+setGameName(in name : string) : void
+getGameName() : string
+displayMessage() : void
The objective of the above program is to help the student write a class on their own with one or more attributes. You are free to modify the above version of the program or use a different program to achieve the objective.
Class in a Separate File for Reusability
Explain how to Place a Class in a Separate File for Reusability
If client code does know how a class is implemented, the client code programmer might write client code based on the class's implementation details. Ideally, if implementation changes then the class's client should not have to change. Hiding the class's implementation details makes it easier to change the class's implementation while minimizing, and hopefully eliminating, changes to client code.
One of the benefits of creating class definitions is that, when packaged properly, our classes can be reused by programmers potentially worldwide.
Header Files (.h)
When building an object-oriented C++ program, it is customary to define reusable source code (such as a class) in a file that by convention has a .h filename extension known as a header file. Programs use #include preprocessor directives to include header files and take advantage of reusable software components, such as type string provided in the C++ Standard Library and user-defined types like class Game. This file should contain the class definition.
Separating Interface from Implementation - Source Files (.cpp)
By convention, member-function definitions are placed in a source-code file of the same base name (e.g., Game) as the class's header file but with a .cpp filename extension. Since the interface of the class is part of the class definition in the Game.h header file, the source files must have access to this file and #include in it.
Client File
The client code needs to know only Game's interface to use the class and must be able to link its object code. Since the interface of the class is part of the class definition in the Game.h header file, the client-code programmer must have access to this file and #include it in the client's source-code file. When the client code is compiled, the compiler uses the class definition in Game.h to ensure that the main function creates and manipulates objects of class Game correctly.
Class Assignment 2(b)- Player Class:
-
- Using the Game program as a reference
- Draw a UML class diagram for a class called Player.
- This class has 2 member variables namely playerName and playerScore. (Use appropriate data types to declare the member variables).
- The class contains following member functions: Constructor, set functions, get functions, display function.
- Now using Visual Studio write code:
- To initialize the member variables use rand() to generate a random score for the player.
- Write appropriate code for the other member functions.
- Create two objects of the Player class and execute the program.
The output should be as follows:
Player Mark has scored 45 points
Things to note about Classes:
- The class declaration must end with a semi-colon.
- The public and private members are grouped. It is generally considered a good idea to put the public members first, but that is not a C++ requirement.
- The class's constructor function must have the same name as the class, and no return type (not even void).
- In general, member functions that do not change any of the data members of the class should be declared const
- A class can contain static data members. Every instance of the class will include its own copy of each of the non-static data but there will be only one copy of each static data member for the whole class.
- Only static data members can be initialized as part of the class declaration. Other data members are initialized by the class's constructor function(s).
Once an object of a class is created (instantiated) you can access the public members of that class using a dot(.) operator.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started