简体   繁体   中英

C++ OOP Class for vector of vectors

I am quite new to the concept of OOP and I want to create a generic class Board which can create multiple different matrices objects.

Here is my current code:

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int> >board;

void setup(int board_size) {
    // fill inner vector
    for (int i = 0; i < board_size; i++) {
        vector<int>temp;
        for (int j = 0; j < board_size; j++) {
            temp.push_back(1);
        }
        board.push_back(temp);
    }
}

void display() {
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[i].size(); j++) {
            cout << board[i][j];
        }
    cout << endl;
    }
}
int main()
{
    setup(3);
    setup(5);
    display();

    return 0;
}

I want to improve my code in an OOP way so that I have a class Board which can create multiple different matrices by using setup function.

And that I can later display each matrices by using the display function.

For example:

Board board_1;
Board board_2;

board_1.display();
board_2.display();

I am not quite sure how to achieve this, any help would be very appreciated. Thank you in advance.

You're almost there, all you have to do is wrap a class around the code you already have:

#include <iostream>
#include <vector>
using namespace std;
class Board {
vector<vector<int> >board;
public:
Board(int board_size) {//constructor as per suggestion
  setup(board_size);
}

void setup(int board_size) {//setup now also allows you to change the board-size later
    board.clear();//removing existing elements
    // fill inner vector
    for (int i = 0; i < board_size; i++) {
        vector<int>temp;
        for (int j = 0; j < board_size; j++) {
            temp.push_back(1);
        }
        board.push_back(temp);
    }
}

void display() {
    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board[i].size(); j++) {
            cout << board[i][j];
        }
    cout << endl;
    }
}
};

And that's it. Now you can call it like this:

Board board_1 = Board(3);
Board board_2 = Board(5);


board_1.display();
board_2.display();

//change board-size and display them again
board_1.setup(6);
board_2.setup(10);


board_1.display();
board_2.display();

Also, maybe separate declaration (in .hpp-file) from implementation (in .cpp-file)

I have few suggestions about your code:

  1. Using std::vector means that the size of the board can change during runtime, which shouldn't be true (in most games...). In order to create a board with fixed size, but that can be initialize in different sizes (meaning, you pick the size once, and it cannot change), you can use std::array with class template, that is determined on compile time, and cannot be changed!
  2. Your setup function, is actually a constructor of the class Board, therefore, instead of creating a board using it, make it your default constructor.

Look at the following example, with usage examples:

#include <array>
#include <iostream>
#include <stdint.h>

template <uint64_t SIZE>
class Board
{
public:
    Board()
    {
        // Here you initialize the board
        for (uint64_t i = 0; i < SIZE; ++i)
        {
            for (uint64_t j = 0; j < SIZE; ++j)
            {
                board_rep[i][j] = 1;
            }
        }
    };

    void display() const
    {
        for (uint64_t i = 0; i < SIZE; ++i)
        {
            std::cout << "| ";
            for (uint64_t j = 0; j < SIZE; ++j)
            {
                std::cout << board_rep[i][j] << " |";
            }

            std::cout << std::endl;
        }
    }

private:
    std::array<std::array<int, SIZE>, SIZE> board_rep;
};


int main()
{
    Board<3> small_board{};
    Board<5> big_board{};

    std::cout << "small board:\n\n" << std::endl;
    small_board.display();
    std::cout << "big board:\n\n" << std::endl;
    big_board.display();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM