Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a generic getter function (i.e. return the two dimensional array i.e. boar

ID: 3634510 • Letter: W

Question

Write a generic getter function (i.e. return the two dimensional array i.e. board)
and generic setter function (i.e. set the two dimensional array i.e. board) for board class in the following code:

#include <iostream>
#include <iomanip>

using namespace std;

template<class T>

class Board{
private:
T **board;
int rows, columns;
public:
Board(int rows, int columns);
void fill();
void print();
};


template <class T>
Board<T>::Board(int rows, int columns)
{
this->rows=rows;
this->columns=columns;
board = new T* [rows];

for (int row = 0; row < rows; row++)
board[row] = new T[columns];
}


template <class T>
void Board<T>::fill( )
{
for (int i = 0; i<rows; i++)
{
cout << "Enter " << columns << " number(s) for row "<< "number " << i << ": ";
for (int j = 0; j< columns ; j++)
cin >> board[i][j];
cout << endl;
}
}


template <class T>
void Board<T>::print()
{
for (int i = 0; i<rows; i++)
{
for (int j = 0; j<columns ; j++)
cout << setw(5) << board[i][j];
cout << endl;
}
}


int main()
{
int rows;
int columns;
cout << "Enter the number of rows "<<"and columns: ";
cin >> rows >> columns;
cout << endl;

Board<char> b(rows, columns);

b.fill();
cout << "Board:" << endl;

b.print();
system("pause");
return 0;
}

Explanation / Answer

Dear,

#include <iostream>
#include <iomanip>

using namespace std;

template<class T>

class Board{
private:
    T **board;
    int rows, columns;
public:
    Board(int rows, int columns);   
    void fill();
    void print();
    // setter
    void setBoard(Board b);
    // getter
    Board<T> getBoard();
};


template <class T>
Board<T>::Board(int rows, int columns)
{
    this->rows=rows;
    this->columns=columns;
    board = new T* [rows];

    for (int row = 0; row < rows; row++)
        board[row] = new T[columns];
}


template <class T>
void Board<T>::fill( )
{
    for (int i = 0; i<rows; i++)
    {
        cout << "Enter " << columns << " number(s) for row "<< "number " << i << ": ";
        for (int j = 0; j< columns ; j++)
            cin >> board[i][j];
        cout << endl;
    }
}


template <class T>
void Board<T>::print()
{
    for (int i = 0; i<rows; i++)
    {
        for (int j = 0; j<columns ; j++)
            cout << setw(5) << board[i][j];
        cout << endl;
    }
}

// sets the board
template <class T>
void Board<T>::setBoard(Board b)
{
    board = b.board;
    rows = b.rows;
    columns = b.columns;
}

// returns the board
template <class T>
Board<T> Board<T>::getBoard()
{
    return *this;
}

int main()
{
    int rows;
    int columns;
    cout << "Enter the number of rows "<<"and columns: ";
    cin >> rows >> columns;
    cout << endl;

    Board<char> b(rows, columns);
   Board<char> c(rows, columns);

    b.fill();
    cout << "Board:" << endl;
    b.print();
   
    cout << "copy: " <<endl;
    c.setBoard(b.getBoard());
    c.print();
    system("pause");
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote