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.


Lab Objectives

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


P1. Implementing a Chess Board

Implement a class called ChessBoard that will represent a typical 8x8 board for playing chess. The class definition is provided below. Do not provide implementations for any other functions or provide variables not explicitly listed. The constructor should create all 64 entries initialized to an initial chess board layout. Use the following characters in the initialization of the black pieces: space for empty, r for rook, n for knight, b for bishop, q for queen, and k for king. Use similar notation for the white pieces except all letters should be capitalized. The notations for the pieces were taken from http://www.uschess.org/beginners/read/. Note that there is no standard notation for a pawn. Therefore, for this application, just use the letter p as its representation.

class ChessBoard

{

public:

ChessBoard();

void setPiece(int x, int y, char piece);

vector<char> boardArray;

private:

};

 

Submit all of the code for your answer in the space below.


R1. Encapsulation

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.

 

class ChessBoard

{

public:

ChessBoard();

void setPiece(int x, int y, Piece piece);

vector<Piece> boardArray;

private:

};

 

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.

 


P2. Simple Iterator Class

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

{

public:

ChessBoardIterator(ChessBoard& input);

Piece operator*();

void operator++();

 

private:

int beginning;

int currentPosition;

int end;

};

Provide all code in the space below.


R2. Scope vs. Lifetime

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.

 

Submit all code in the space below.


R3. Protected Scope

 

Suppose you are writing a new chess software package. You already have the code written to represent a generic piece but would like to add the additional functionality of specifying the style of each piece. Styles may include, but are not limited to plastic, marble, granite, modern, etc. First, modify the Piece class such that its data members are now protected as opposed to private. Then, write a new class called SyledPiece that inherits from Piece. The class definitions are provided below.

 

class Piece

{

public:

Piece(int ipos, char itype, int icolor);

protected:

int position;

char type;

int color;

};

 

class SyledPiece : public Piece

{

public:

StyledPiece(int ipos, char itype, int icolor, string syle);

private:

string style;

};

 

Submit code for SyledPiece in the space below.


P4. Nested Classes

You will now modify the iterator class such that it is a nested within your ChessBoard class. Make your nested iterator class publicly accessible. Provide a new member function to the ChessBoard class called begin(). This function should return an iterator starting at the beginning of the chessboard. The class definition is provided below.

class ChessBoard

{

public:

// Nested class definition

class ChessBoardIterator

{

public:

ChessBoardIterator(ChessBoard& input);

char operator[](int i);

void operator++();

 

private:

char* beginning;

char* currentPosition;

char* end;

};

 

ChessBoard();

void setPiece(int x, int y, char piece);

ChessBoardIterator begin();

 

private:

vector<char> boardArray;

};

Submit all code in the space below.


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.