C++ - How to set up an array class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beacon
    Contributor
    • Aug 2007
    • 579

    C++ - How to set up an array class

    Hi everybody,

    I asked my professor for some practice problems to work on over the summer to prep me for Data Structures this fall and he gave me Conway's Game of Life to recreate. I haven't had any trouble with what I've completed so far, but I want to stop where I'm at and put everything in a class so get some more practice with OOP.

    Problem is, I've never setup an array class before and I haven't really haven't found any examples online or in any books I have. I think I'm off to a good start, but there are some areas that I'm concerned about, namely how to destruct a 2d array or what should be included as private data members. I've created stack classes before, but this seems a little different.

    In my Life.h file:
    [CODE=C]
    const int MAXROW = 10;
    const int MAXCOL = 10;

    class Life{
    private:
    LifeBoard[10][10]; // I'm really not sure what should be included here.
    public:
    Life(); // These are the only data members I've attempted to setup so far.
    ~Life(); // I know I need a copy constructor, but I don't think I'm
    // going to get very far while I'm unsure about the private members.
    }
    [/CODE]

    In my Life.cpp file:
    [CODE=C]
    Life(){
    for(int i=0; i<MAXROW; i++){
    for(int j=0; j<MAXCOL; j++){
    LifeBoard[i][j] = 0;
    }
    }
    }

    ~Life(){
    for(int i=0; i<MAXROW; i++){
    for(int j=0; j<MAXCOL; j++){
    delete [] LifeBoard [j]; // I know this has got to be wrong, but I'm stuck.
    }
    }
    }
    [/CODE]

    In main.cpp, I have a reset() function that basically does the same thing as the constructor and I have an init() function that randomly populates a 2d array with 1's.

    Eventually, I believe I'm going to need a method that will traverse the original array and put values in a new 2d array's index locations that determine if a cell will live, die, or do nothing.

    Once I've got the values for the second array, if I add it to the original 2d array, I should have the next state (or generation) of cells.

    Just to entertain myself, I setup the original array in a do{}while() loop so that it will print the array to the monitor, delay for 2 seconds, clear the screen, and print the updated array. Right now, it's just randomly filling the original array with 1's since I haven't added it any other array. I got a kick out of watching the animation of the array as it filled up. Hopefully I can apply this to the next generation so that it will continually update until all of the cells die.

    Oh, BTW, I don't want any clear cut answers to this problem. If I did, I would have downloaded the Java code from one of the many websites that have a Game of Life example.

    Thanks for any guidance that can be spared...

    ** I added code=c tags to my code, but it's only outputting text. Just fyi so no one thinks I didn't at least try to make this post readable.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Language-specific code tags are broken right now, the important thing is that you used them at all.

    If you're going to #define MAXROWS and MAXCOLS, you should use them as the bounds of your array, rather than hardcoding the size in. Also, you never initialize your array with new. If you're using delete, you should be using new and vice versa. Deleting a 2d array is done as follows:

    Code:
    for (int a = 0; a < NUMROWS; a++)
        delete[] matrix[a];
    delete matrix;

    Comment

    • beacon
      Contributor
      • Aug 2007
      • 579

      #3
      Whew, I thought that maybe I was doing something wrong in regards to the code tags. Thanks for clearing that up.

      When I use the new operator to build my array, should I use pointers or should I use the open/close brackets ([ ])? This, apparently, is another array that I'm lacking knowledge in. I know arrays and pointers are essentially the same, so I'm interested to know both ways to initialize a 2d array, if there's a difference, when creating an array class.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Just read this Howto. It'll explain all this fun stuff.

        Comment

        • beacon
          Contributor
          • Aug 2007
          • 579

          #5
          Thanks Laharl for that link to the array article. It's a good article, however, I read it before I posted.

          I'm not sure how to apply it in a class. I'm lost on what should be included as a private data member (whether they should be pointers or array[][]) and also what some typical methods are that should be included in an array class.

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            I'll make note of a few things, because I think what you really need to do is reread certain topics. They are confusing in any case.

            As a note, putting everything in a class doesn't mean your code uses OOP. OOP is a design paradigm, and if your code is modeled around that paradigm, using OOP syntax will just bloat your code. I realize you're trying to practice OOP, but I want you to realize what you're actually doing is just working with the syntax, not the actual design concept.

            Problem is, I've never setup an array class
            I'm not entirely sure what you mean by an array class. The two things that come to mind are C++ vectors and Boost.Array. But as I see your code, it seems your real question is, "how do I deal with arrays in my class?".

            namely how to destruct a 2d array
            You can only destruct two things. You can destruct, given a pointer to a dynamically allocated block of memory. Or you can do it for an array of something.

            I know arrays and pointers are essentially the same
            Wrong. Arrays and pointers are fundamentally different things. Confusingly though, certain syntax and semantics are mixed with arrays and pointers. Google for something called "C FAQ". Read that. There's areas where they explain the relationship between arrays and pointers. FYI, there is also a C++ FAQ online, which is an equally valuable resource.

            In any case, the syntax can become problematic, because:
            Code:
            int someVar; //easy to understand, this is an integer variable
            int someVar2[10]; //also straightforward, 10 integers in an array
            int *someVar3; //it points to an integer...like
            someVar3 = &someVar; //straightforward
            someVar3 = someVar2; //huh?
            someVar2 isn't a pointer. It's an array. But C (and therefore C++) have a difficult way of handling arrays. When you refer to someVar2, it's like getting a pointer (which points to an array). It's not a pointer, but syntactically it's treated as one. Note:

            Code:
            //correct:
            someVar3 = new int[10];
            delete someVar3;
            //But this is not a smart idea:
            delete [] someVar2;
            Once you've read the C FAQ, post here, if you understand what's going on with arrays and pointers.

            Comment

            • beacon
              Contributor
              • Aug 2007
              • 579

              #7
              I'm still confused about the proper way to setup a 2d array using classes. Most notably, creating the constructor, a copy constructor, and a destructor.

              One of the replies I received helped out with the destructor, but if I'm not setting it up properly from the beginning, the destructor really doesn't do me any good.

              I have no problem setting up a 2d, 3d, 50d array in main, however, I've never done it before in a class, so I'm a little lost.

              Just to reiterate my initial post, I'm trying to recreate Conway's Game of Life. In order to set up the game, I need two 2d arrays...one of the arrays (current state) will be initialized randomly with 1s and the second (test) will test the first array to see if a 0, 1, or -1 needs to go in the index locations. Then, the arrays can be added together to achieve the next state. This will continue as long as there are next generations or all of the index locations become -1.

              I don't want to use vectors or boost.array (or whatever it's called) because I feel it's important to have a basic understanding. I want to set everything up in a class because I need the practice.

              Hopefully this helps explain what I'm trying to achieve...

              Comment

              Working...