Storing Integer Arrays safely

Ok so I have two problems and I am wondering if anybody here can help me on this. I have an assignment where I have to design, implement, and test a class for storing integer arrays "safely". My first problem is that I am having a hard time exactly understanding the directions and I was hoping someone could tell me if I am doing this right so far. My second problem is that I do not know how to set up the destructor, so I would greatly appreciate it if someone could help me with that as well. The directions for this assignment are in bold text, along with the code I have written so far.

The goal of this programming assignment is to give students practice defining and using classes. In particular, students are required to design, implement, and test a class for storing integer arrays "safely". The array should be able to hold any number of integers up to 100.

In the class header file "SafeArray.h" students must define the class and specify the constructor/destructor functions, the public methods and private variables. In the class implementation file "SafeArray.cpp" students must implement the following operations:

constructor - to initialize the object.
copy constructor - to copy an object.
destructor - to delete the object.
set - allow the user to set a value of the array at a particular location.
get - allow the user to get a value of the array at a particular location.
print - print out the array.
add - add the elements of one array to another.
subtract - subtract the elements of one array from another.

The purpose of your main program "main.cpp" is to demonstrate that all of the methods above work properly. You should have at least one call to each of the methods, and print out the array as needed to show that the operations are performing correctly. Keep your main program as simple as possible. A well designed sequence of method calls is all you need. You do NOT need a fancy interactive program.


"SafeArray.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef SAFEARRAY_H
#define	SAFEARRAY_H

class Safe

{

private:

   // Declare variables to store A, B and C
    int A;
    int B;
    int C;


public:

   // Declare Default Constructor
    Safe();
    // Copy Constructor:
    Safe(Safe & orig);


   // Declare Print Function
    void print();

    // Add and Subtract:
    void add ( Safe & one );
    void subtract(Safe & one) ;


   // Declare Setter
    void set ( int a, int b, int c);

    int getA ()
    {
    return A;
}

    int getB ()
{
    return B;
}

    int getC ()
{
    return C;
}
};

#endif	/* SAFEARRAY_H */ 


"SafeArray.cpp":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "SafeArray.h"
#include <iostream>

using namespace std;

void Safe::print() {
    cout << A << "x^2 + " << B << "x + " << C << endl;
}
//Constructor
Safe::Safe() {
    A = 0;
    B = 0;
    C = 0;
}

// Copy constructor:
Safe::Safe(Safe & orig ) {
    A = orig.A;
    B = orig.B;
    C = orig.C;
}

void Safe::set ( int a, int b, int c)
{
    A = a;
    B = b;
    C = c;
}

    void Safe::add ( Safe & one )
    {
        A = A + one.A;
        B = B + one.B;
        C = C + one.C;

    }

    void Safe::subtract (Safe & one )
    {
        A = A - one.A;
        B = B - one.B;
        C = C - one.C;
    }



main.cpp project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

#include "SafeArray.h"

using namespace std;



int main()

{

   // Declare a SafeArray object
    Safe obj;
    int x;

   // Set the values A, B and C to 1, 2 and 3
    obj.set(2,3,4);
    Safe obj2(obj);

    obj.set(1,3,-2);
    cout << "Set q1: " << endl;
    obj.print();

    cout << "Set q2: " << endl;
    obj2.print();

    cout << "Add q2 to q1: " << endl;
    obj.add(obj2);

    cout << " Answer =" << endl;
    obj.print();

    cout << "Subtract q2 from q1: " << endl;
    obj.subtract(obj2);

    cout << " Answer  =" << endl;
    obj.print();


   return 0;

}
I do not know what means "storing integer arrays safely".:)
a. Your class must store an array of integers (max 100). You need to keep track of how many numbers are actually present; this may be less than 100.

1
2
3
4
5
6
struct safe_array
{
       // ...
       int a[100] ; // can store upto a maximum of 100 elements
       int size ; // number of actual elements ( 0 < size < 101 )
};


b. "safely" in this context would mean that access to the array elements must not be out of range. ie. the position of the element must be validated prior to access. For example in the member function to allow the user to set a value of the array at a particular location:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct safe_array
{
       int set( int value, int pos )
       {
             if( pos < 0 || pos >= size )
             {
                    // error, out of range access
             }
             else a[pos] = value ;
       }

       // ...
       int a[100] ; // can store upto a maximum of 100 elements
       int size ; // number of actual elements ( 0 < size < 101 )
};

Topic archived. No new replies allowed.