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

Pass an integer argument to the template at the time of the creation of the obje

ID: 3634511 • Letter: P

Question

Pass an integer argument to the template at the time of the creation of the object in the following code. This integer value specifies how much space is to be given in setW function of the print function.

#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;
}

I hope this will helps you...