views:

172

answers:

4

Arrays in C/C++ of N elements have subscripts from 0 as the lower bound to N-1 as the upper bound. Other languages like Pascal allow the user to specify any lower bound and upper bound subscript values to create an array of (upper bound - lower bound + 1) elements.

Question: Create a class "Array" in C++ that allows a user to specify both forms of arrays. Each element in the array will be a floating point value. The class is to maximize the amount of information hiding. Clearly show the information that goes into the header file array.h and into the source file array.cpp. The header file must guarantee that multiple copies of the header file information must not exist in any source code file.

Can anybody help me with this? Thanks.

A: 

Sounds like a homework question to me. Why don't you give a stub at it, and if you hit a specific roadblock, tell us about it.

+1  A: 

it's not a homework question. it's a question on a practice test. i'm just studying.

Then it serves the same purpose as homework. You will only learn if you solve problems *yourself*.
dmckee
+1  A: 

Pushing all your questions in a stack.

Popping them ...

Can anybody help me with this?

Let's see.

The header file must guarantee that multiple copies of the header file information must not exist in any source code file.

Ever heard of a header guard? Or, #pragma? That's two solutions, but you'd have to figure the pros and cons of each.

Clearly show the information that goes into the header file array.h and into the source file array.cpp.

Are you familiar with programming using C or C++?

The class is to maximize the amount of information hiding.

Encapsulation. Any standard text on C++/OOD will help you. The idea is to have interfaces and build your code around them.

Each element in the array will be a floating point value

Sure, why not?

Create a class "Array" in C++ that allows a user to specify both forms of arrays.

Sure. Do you need to have multi-dimensional arrays as well? All you need to do is translate between these forms. What syntax do you expect your users to use when using your array class? E.g: the popular form is to access array elements using the operator[] as in float_array[ i ] where i = 0, 1, ... N-1.

class Array {
   public:
     explicit Array(size_t n); // default array, C-style indexing
     Array(size_t begin, size_t end); // Pascal-style indexing
   private:
     float *_a;
};

That'll get you started. Let us know if you have more questions.

dirkgently
A: 

risking going over the top with this, I would go with a templated solution. It's much more elegant. Something like this maybe:

template<int START, int END>
class Array
{
   float operator[](int i) { ... }
...
private:
   float m_data[END - START];

};
shoosh