BIG C++ Cay Horstmann & Timothy Budd
Laboratory Notebook Chapter 20 – Name Scope Management
Your name:
Your email address:
Your student ID number:
Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.
To gain experience with
· understanding how the management of names is tied to the management of values
· understanding the concepts of scope and visibility, and the various scopes found in c++ programs
· learning how to use protected visibility to create an interface for derived classes that is different from the public interface
· understanding the use of alternative techniques for the management of names, such as friends, nested classes, and private inheritance
· learning how to avoid ambiguity and duplicate names by using independent name spaces
Submit all of the code for your answer in the space below.
In this review problem, you will learn the benefits of encapsulation. To begin with, write a short global function that will print a chessboard, represented by a public vector of characters, to the screen.
Submit all code in the space below.
Write a main function that creates an initial chessboard and prints the board to the screen using the code just constructed. Test your program to ensure its functionality.
Now, implement a class called Piece that represents a simple chessboard piece. The class should encapsulate data items that represent a piece’s type, color, and position. The class definition is provided below. The position is an integer that ranges between zero and 64, the type stores a character indicating the type of piece, as was done above, and the color is set to zero for black and one for white.
class Piece
{
public:
Piece(int ipos, char itype, int icolor);
private:
int position;
char type;
int color;
};
Submit your code in the space below.
Modify the chessboard class from P1 such that it stores a vector of pieces instead of characters. The new class definition is supplied below. Ensure that the default constructor initializes the board appropriately and that setPiece() accepts a Piece object and not a character.
Submit the modified ChessBoard class in the space below.
Re-compile and execute your main function without any modifications. Will your program compile? If yes, what is the result?
You should observe that public data members within a class can make it difficult for others to use that class if modifications occur. A better way to program is to make the data private and provide public functions for accessing that data. In this way, the internal workings of the code can be modified while ensuring that dependent code still functions as long as the public members retain their functionality.
You will now write a simple iterator class so that you can traverse the chessboard constructed in P1. The iterator class should hold integer indexes to the beginning and end of the chessboard, in addition to an integer indicating the iterator’s current position. The index values will all be initialized within the iterator’s constructor. Furthermore, you will implement the * and ++ operators. The * operator will return the value of the array at the iterator’s current position. The ++ operator will move the current position within the chessboard to the next element. The iterator should have the special feature such that, once the end of the chessboard is passed, it loops back to the beginning (circular).
class ChessBoardIterator
ChessBoardIterator(ChessBoard& input);
Piece operator*();
void operator++();
int beginning;
int currentPosition;
int end;
Trace through all of the code that you have created thus far and, in the space below, describe both the lifetime and scope of each variable encountered.
Explain the general methodology you used to determine the lifetime and scope of each variable.
P3. Friends Forever
It is not typically desirable to make important data within a class public. Therefore, change the vector within the ChessBoard class such that it is declared private. Also, modify the ChessBoard class such that it declares the iterator class you constructed in P2 as a friend. Once you declare the iterator as a friend, it will regain access to the vector contained within ChessBoard even though it is now a private variable.
protected:
char operator[](int i);
char* beginning;
char* currentPosition;
char* end;
R4. Namespaces
Provide an appropriate namespace and alias for the code you have written in this lab. Use the naming tips provided in the text.
P5. Private Inheritance
Modify your ChessBoard class such that, instead of encapsulating a vector<char> object to implement the chessboard, it privately inherits from the vector<char> class. Ensure that your class retains all public member functionality including its circular iterator.
Do not forget to send your answers when you are finished.