CS 311 Fall 2024 > Assignment 1
CS 311 Fall 2024
Assignment 1
Assignment 1 is due at 5 pm Tuesday, September 10. It is worth 60 points.
Procedures
This assignment is to be done individually.
Turn in answers to the exercises below on the UA Canvas site, under Assignment 1 for this class.
- Your answers must consist of
the source code for Exercise A
(files
player.hpp
,player.cpp
). Attach these files to your homework submission. - Send only the above! I do not want project files or executables.
- I may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (60 pts total)
Exercise A — “Player” Class
Purpose
In this exercise, you will write a simple class of the kind that might have been done as an exercise in operator overloading in CS 202. However, a number of the concepts covered in CS 311 will need to be applied. Most importantly, quality standards will be quite high. In particular, your code will need to work with a provided test program, passing all tests.
Instructions
Implement a C++ class that holds information about a player in some game: the player’s real name and username, and the number of games they have played.
Be sure to follow the coding standards. You do not need to follow the standards in part 4 (“Additional Standards”); these come later.
- Name your class “
Player
”, and implement it in filesplayer.hpp
andplayer.cpp
. - An object of type
Player
will act as if it maintains three pieces of data:- A string holding a game player’s real name.
- A string holding the player’s username.
- A nonnegative integer, giving the number of games the player has played.
- Include the following functions, and no others,
in the public interface of your package:
- Copy constructor, move constructor,
copy assignment, move assignment, destructor.
- These five functions are automatically generated—written for you by the compiler. Do not write them yourself. (We will discuss how this works in class.)
- Default constructor.
- The default constructor sets both the player’s
real name and username to
“
UNKNOWN
”, and the number of games to zero.
- The default constructor sets both the player’s
real name and username to
“
- 3-parameter constructor: parameters are two strings (real name, username) and an integer (games played). The values for the constructed object are set to those given.
- “Get” member functions for the data stored.
Each of these takes no parameters
and returns either a string or a number, as appropriate.
Call these
getRealName
,getUsername
, andgetGames
. - “Set” member functions for the data stored.
Each of these takes a single parameter and returns nothing.
Call these
setRealName
,setUsername
, andsetGames
. - Member function
inactive
, which returnsbool
. This istrue
if the number of games played is zero, andfalse
otherwise. - Member function
toString
. This takes no parameters and returns a string representation of the stored information: the real name, followed by a blank, a left parenthesis (“(
”), the username, a right parenthesis (“)
”), a colon (“:
”), a blank, and the number of games played as a base-ten integer.- For example, if the player’s real name is
“
Selena Gomez
”, the username is “Mabel Mora
”, and 15 games have been played, thentoString
returns “Selena Gomez (Mabel Mora): 15
”.
- For example, if the player’s real name is
“
- Equality (“
==
”) and inequality (“!=
”) operators for comparing twoPlayer
objects. They are equal if the real names are equal, the usernames are equal, and the numbers of games played are equal. - Pre- and postincrement operators (“
++
”) and pre- and postdecrement operators (“––
”). These increment or decrement the number of games played, as appropriate—except that, if the number is zero, then the decrement operators do not reduce the number to minus-one, but keep it at zero. Each operator returns the appropriatePlayer
object. - A stream insertion (“
<<
”) operator. This outputs to the given stream the same string as that generated bytoString
.
- Copy constructor, move constructor,
copy assignment, move assignment, destructor.
- Externally, all strings are handled using
the C++ Standard Library class
std::string
, and all numbers are handled using typeint
.
Other Requirements
- Your code must compile and execute with the provided test program (see Test Program, below), and it must pass all tests. Code that does not pass all tests will not be graded.
- For each function written in the package:
at the beginning of the function body,
write assertions using
assert
so that, if the assertions all pass, then the function is guaranteed to work properly. - Code dealing with
bool
values must not be unnecessarily verbose. For example, ifb
is abool
variable, then do not write something like the following.if (b) return true; else return false;
Instead, write this:
return b;
- Avoid unnecessary duplication of code
(for example, in pre
operator++
and postoperator++
).
Test Program
A test program
is available:
player_test.cpp
.
If you compile and run the test program (unmodified!) with your code,
then it will test
whether your code works properly.
The test program requires doctest.h
,
the header for the doctest unit-testing framework,
version 2.
This file may be downloaded from the
doctest GitHub site.
Do not turn in the test program or the doctest framework.
Note. The test program cannot check the following completely, but you still need to do them correctly.
- Whether extra public functions, other than those in the instructions, are defined.
- Whether any function performs any actions it does not need to do (like debugging printout).
- Whether the most appropriate parameter-passing and return methods are used.
- Whether operators are correctly defined as global or member functions.
- Whether each function body begins with appropriate assertions.
Thoughts
- This is to be high-quality code. That takes time, even for experienced programmers.
- When grading, I plan to go through and check whether you have met each of the requirements above, as well as each of the coding standards. I suggest that, before you turn in your work, you do the same.
- See the finished versions of
files
timeofday.hpp
andtimeofday.cpp
for examples of the kind of code I am looking for.