C++ OOP Summary

Object Oriented Programming (C++)
  • Course: CSCI 211

  • Lab: Lab 18

  • Instructor: Yun Wang

  • Institution: Queens College

Fancy 8 Queens
  • The program's core objective is to solve the classic 8 Queens Problem, which involves placing eight chess queens on an 8×88 \times 8 chessboard such that no two queens threaten each other. This means no two queens can share the same row, column, or diagonal.

  • The implementation represents an 8×88 \times 8 chessboard visually, with each square being represented by a 5×75 \times 7 2D character array for detailed graphical output.

  • Black and white boxes (squares on the chessboard) are visually constructed using these 5×75 \times 7 2D character arrays, allowing for a structured and stylized representation of the board.

Typedef in C++
  • typedef is a C++ keyword that allows programmers to create a new name for an existing data type, effectively creating an alias.

  • Syntax: typedef existing_type new_type_name;

  • Application in this program: A typedef is used to define box as an alias for a char[5][7] data type. This simplifies code readability and maintenance by providing a clear, descriptive name for the complex 2D character array structure used to represent a single chessboard square.

Chessboard Representation
  • The datatype box is defined as typedef char box[5][7]; where char[5][7] is a 2D array capable of storing characters for a 5×75 \times 7 grid.

  • Two primary box instances are utilized: bb (black box) and wb (white box).

    • Each element in bb contains the ASCII character (char)219 (a solid block character) to visually represent a black square.

    • Each element in wb contains a space character to represent an empty white square.

  • The actual chessboard, board, is declared as a 8×88 \times 8 2D array of pointers to box type (e.g., box *board[8][8];). This design choice is critical for memory efficiency because instead of creating 64 individual box objects (each being 5×75 \times 7 characters), only two box objects (bb and wb) are created.

  • Each element board[i][j] on the chessboard array stores the address of either the bb or wb box, depending on whether the square at position (i,j)(i, j) is black or white, respectively. This minimizes memory usage by pointing multiple board squares to the same two box character arrays.

Printing the Board
  • The board is printed using a series of nested loops: two outer loops iterate through the 8×88 \times 8 board squares (ii for rows, jj for columns) and two inner loops iterate through the 5×75 \times 7 dimensions of each box (kk for box rows, ll for box columns).

  • Accessing box elements through pointers when printing: The expression cout << (*board[i][j])[k][l]; is used to print each character.

    • board[i][j] retrieves the pointer to a box (either &bb or &wb) stored at the current board position.

    • *board[i][j] dereferences this pointer, yielding the actual box char[5][7] array.

    • [k][l] then accesses the specific character within that box at row kk and column ll.

Sample Output
  • A representation of a solution for the 8 queens problem, indicating the column position of the queen for each row. For example, The Holy solution is | 825164 signifies a solution where:

    • Queen in row 0 is in column 8 (this seems like a typo, typically columns are 1-8 or 0-7; assuming 1-based, 8 implies out-of-bounds or specific problem encoding)

    • Queen in row 1 is in column 2

    • Queen in row 2 is in column 5

    • Queen in row 3 is in column 1

    • Queen in row 4 is in column 6

    • Queen in row 5 is in column 4

    • (Implicitly, rows 6 and 7 would follow, or the solution is incomplete/truncated).