Question: In C++ Part A: Refactor Board into a Class [10% code + 30% test program] The goal is to refactor the Board type to be
In C++
Part A: Refactor Board into a Class [10% code + 30% test program]
The goal is to refactor the Board type to be an encapsulated class. An encapsulated class is a class where all variables that represent the internal state of a software component are private. The functions associated with the Board type will become member functions. You will also a new helper function to determine if the class invariant is true.
By the end of Part A, your Board class will have public member functions with the following prototypes:
Board (); // was named boardClear
char getAt (int row_in, int column_in) const;
void print () const;
int countWithValue (char value_in) const;
void setAt (int row_in, int column_in, char value_in);
void load (const std::string& filename_in);
The Board class will also have private member functions with the following prototypes:
bool isAlignedForStarPoint (int index_in) const;
void printRowNumber (int row_in) const;
void printColumnLetters () const;
bool isInvariantTrue () const; // new
Perform the following steps:
In Board.h, change the Board record (struct) to be an encapsulated class.
Reminder: In an encapsulated class, all member fields should be private.
Change the Board-related functions to be member functions. Remove board from the function names and make the new first character lowercase. Remove the Board parameter from each function. If the Board parameter was const, the member function should be const.
Reminder: To make a member function const place the keyword const after the parenthesized parameter list.
Add the prototype for the isInvariantTrue function.
Change the clear function (previously named boardClear) to be the default constructor for the Board class.
Reminder: A default constructor has the same name as the class, including capitalization. It has no return type.
Warning: Initialize the board values directly using array notation. If you call setAt, you will have problems when adding the class invariant.
In the Board.cpp file, update the function implementations to match the prototypes. Leave converting the clear function into the default constructor until the next step.
Reminder: Add Board:: immediately before each function name. It must be after the return type.
Example: The first line for the getAt function in file Board.cpp should appear as:
char Board::getAt (int row_in, int column_in) const
Convert the clear function to be the default constructor. The first line of the function will change, but the steps inside the function will not.
Reminder: Constructor have no return type, so Board:: will be at the beginning of the line for constructors.
Test your Board module with the TestBoard3A.cpp program provided. You will need the TestHelper.h and TestHelper.cpp files. It should give you a mark of 25 / 50 (example output).
Hint: g++ Board.cpp BoardSize.cpp BoardValue.cpp TestHelper.cpp TestBoard3A.cpp -o parta
Note: The remaining test program marks are for Part D.
Add an implementation for the isInvariantTrue function. It should return true if every place on the board has a valid value. Otherwise, it should return false.
Add an assert to the beginning every public function except the constructor that ensures that the class invariant is true. If a function has asserts for preconditions, the assert for the invariant should be before them.
Hint: Use the isInvariantTrue function.
Hint: The functions you should check the class invariant at the start of are getAt, print, calculateScore, setAt, and load. In Part D, you will also check it at the start of four more functions.
Reminder: Do not assert the class invariant at the start of the constructor. The class invariant (probably) is not true until the constructor has set the member variables.
Reminder: Do not assert the class invariant in the isInvariantTrue function. If you do, the function will call itself over and over forever, or at least until your program runs out of memory and crashes. This is termed infinite recursion, and it is like an infinite loop but even worse. How to use recursion safely is covered in CS 210.
FYI: We put the assert for the class invariant before the asserts for the precondition because preconditions can call functions that depend on the class invariant. For example, before getting an array element, you might call a member function that checked if the array was large enough. In contrast, the class invariant will never depend on any preconditions, so if you say it is checked first, it really will be checked first.
Add an assert at the end of execution of every public member function to ensure the class invariant is still true when the function is about to return.
Hint: The functions where you should check the class invariant at the end are the default constructor, setAt, and load. In Part D, you will also check it at the end of four more functions.
Check your program with the TestBoard3A.cpp test program. You will need the TestHelper.h and TestHelper.cpp files. The resulting program should give you a mark of 30 / 55 (example output).
Note: You will need the empty.txt, ear.txt, and shapes.txt data files. These are the same files as in Assignment 2.
Hint: g++ Board.cpp BoardSize.cpp BoardValue.cpp TestHelper.cpp TestBoard3A.cpp -o parta
Note: The remaining test program marks are for Part D.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
