473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ - How to set up an array class

beacon
579 Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. const int MAXROW = 10;
  2. const int MAXCOL = 10;
  3.  
  4. class Life{
  5. private:
  6. LifeBoard[10][10];   // I'm really not sure what should be included here.
  7. public:
  8. Life();  // These are the only data members I've attempted to setup so far.
  9. ~Life();  // I know I need a copy constructor, but I don't think I'm
  10.             // going to get very far while I'm unsure about the private members.
  11. }
  12.  
In my Life.cpp file:
Expand|Select|Wrap|Line Numbers
  1. Life(){
  2. for(int i=0; i<MAXROW; i++){
  3. for(int j=0; j<MAXCOL; j++){
  4. LifeBoard[i][j] = 0;
  5. }
  6. }
  7. }
  8.  
  9. ~Life(){
  10. for(int i=0; i<MAXROW; i++){
  11. for(int j=0; j<MAXCOL; j++){
  12. delete [] LifeBoard [j];  // I know this has got to be wrong, but I'm stuck.
  13. }
  14. }
  15. }
  16.  
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.
May 21 '08 #1
6 4091
Laharl
849 Recognized Expert Contributor
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:

Expand|Select|Wrap|Line Numbers
  1. for (int a = 0; a < NUMROWS; a++)
  2.     delete[] matrix[a];
  3. delete matrix;
  4.  
May 21 '08 #2
beacon
579 Contributor
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.
May 21 '08 #3
Laharl
849 Recognized Expert Contributor
Just read this Howto. It'll explain all this fun stuff.
May 21 '08 #4
beacon
579 Contributor
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.
May 21 '08 #5
oler1s
671 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. int someVar; //easy to understand, this is an integer variable
  2. int someVar2[10]; //also straightforward, 10 integers in an array
  3. int *someVar3; //it points to an integer...like
  4. someVar3 = &someVar; //straightforward
  5. someVar3 = someVar2; //huh?
  6.  
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:

Expand|Select|Wrap|Line Numbers
  1. //correct:
  2. someVar3 = new int[10];
  3. delete someVar3;
  4. //But this is not a smart idea:
  5. delete [] someVar2;
  6.  
Once you've read the C FAQ, post here, if you understand what's going on with arrays and pointers.
May 22 '08 #6
beacon
579 Contributor
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...
May 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3769
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
6
1737
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
14
3920
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
3
837
by: michi | last post by:
Hello, I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can do one-dimensional array, but I don't know how to specify the size of array when I want to do it 2D. I want to do it using 'new' operator. Thanks, michi.
9
4813
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
7
3661
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
4
1530
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those who experience. (For some reason I have to implement Array not ArrayLists) Below are the simple following code: Dim Array() As String Dim intCounter As Integer
23
7421
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
17
7256
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
6
2946
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on there position in the array. This should be possible but I can't figure out exactly how to accomplish this. The below code should better illustrate what I'm trying to do: template <int I> class Item
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.