MadSci Network: Computer Science

Re: C++ Classes & Multidimensional Arrays

Date: Thu Jan 4 11:27:26 2001
Posted By: Joe McLeod, Group Engineer, Advanced Services, Charter Communications
Area of science: Computer Science
ID: 977962687.Cs

Hello, Jim

You are quite correct that any programming task using classes can also be accomplished using procedural or unstructured methods.

The importance of classes is really one of design methodology and abstraction. C++ classes are the set of procedures through which the object-oriented programming(OOP) methodology is accomplished. OOP allows a developer to concentrate more on the programming task and less on the housekeeping details associated with coding an application. Having said that, it is also important to note that concentrating less on the housekeeping details is also a good way to introduce security holes into an application since those details have been, to a degree, automated.

There are some tasks for which OOP and the class structure are not well suited. If I were developing firmware for a device in C++(not my first choice), or an embedded application, I would want to optimize my binary code for speed of execution and minimal footprint; these are not things OOP does well, in this context, due to the complexity of the object code libraries.

The following website can provide you with a very good comparison of design methodolgies (and less duplication of effort for me!):

For the "brief" version, see:

Q6: What is OOP? What is C++? from the "official" comp.lang.c++ USENET FAQ.

Chapter 2: A Survey of Programming Techniques by Peter Müller

For the complete version of both:

Introduction to Object-Oriented Programming Using C++ by Peter Müller

comp.lang.c++ FAQ

Both of these are excellent primers for OOP and C++.

On multidimensional arrays beyond the 3rd...I would say this is probably one of the least directly used features in the C/C++ family. That is not to say that there are not uses for nth-dimension arrays. Multidimensional arrays are most typically used to model objects. Regardless of the number of dimensions, how those arrays are built is the critical issue.

It is important to understand how C++ implements any multidimensional array, and the short answer is that it treats them as "arrays of arrays".

Let's say we have a real-world object which possesses 2 traits, and each trait has 2 characteristics. Further, we assume that there are 2 of these objects in existence.

To model this object, I create an array of 3 dimensions such as My3DArray[2][2][2] where I have 2 Rows(objects) X 2 Columns(traits) X 2 Elements(characteristics). I would, in reality, be creating ordered sets that appeared as:

{ { {R0C0E0,R0C0E1},{R0C1E0,R0C1E1} }
  { {R1C0E0,R1C0E1},{R1C1E0,R1C1E1} } }

In English, from the C perspective, I have an array (or set) of 2 elements(R0 and R1), each element of which is an array of 2 elements(C0 and C1), of which each element is a one-dimensional array of values(E0 and E1).

In practice, to model the real-world object, I would create a class that defined the traits of the real-world object, then instantiate that class as an array. Similarly, from a functional approach, I might use a C "struct" to define the traits of my real-world object, then create an array of that sruct. In the C paradigm, this array would still be a 3D array. If my class, or struct, happened to define an array, I have still created an n-dimension array, but I have done so at an abstracted level such that I did not have to explicitly design or specify the n-dimension array.

What follows is mostly an academic exercise, with little real value, to demonstrate how a 4D array might be directly executed...

Returning to our 2 real-world objects, possessing 2 traits, and each trait 2 characteristics, let's assume that each characteristic is static and possesses 2 concurrent "states".

I would create My4DArray[2][2][2][2] with: 2 Rows(objects) X 2 Columns (traits) X 2 Elements(characteristics) x 2 Values(states). I would then have the following sets:

{ { {R0C0E0V0,R0C0E0V1},{R0C0E1V0,R0C0E1V1} }
  { {R0C1E0V0,R0C1E0V1},{R0C1E1V0,R0C1E1V1} }
  { {R1C0E0V0,R1C0E0V1},{R1C0E1V0,R1C0E1V1} }
  { {R1C1E0V0,R1C1E0V1},{R1C1E1V0,R1C1E1V1} } }

And, in English, I have an array (or set) of 2 elements(R0 and R1), each element of which is an array of 2 elements(C0 and C1), of which each element is an array of 2 element(E0 and E1), where each element has a one-dimensional array of values(V0 and V1).

As I said, academic. Anything you could represent in the 4D array could more accurately be modeled using the other tools available in C/C++.

I hope this answers your questions. If you have any follow-up questions, please feel free to post to MAD Scientist or contact me directly.


Current Queue | Current Queue for Computer Science | Computer Science archives

Try the links in the MadSci Library for more information on Computer Science.

MadSci Home | Information | Search | Random Knowledge Generator | MadSci Archives | Mad Library | MAD Labs | MAD FAQs | Ask a ? | Join Us! | Help Support MadSci

MadSci Network,
© 1995-2000. All rights reserved.