Class defining and storying integer arrays

Ok so I am currently stuck on what I should do next in a program I am working on. These are my instructions:

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 output of my program is suppose to look like this:


Set q1: 2, 3, 4
Print q1: 2, 3, 4

Set q2: 1, 4, -2
Print q2: 1, 4, -2

Add q2 to q1

Print q1: 3, 7, 2
Get q1 at 1: 7



If I could get some help on this I would greatly appreciate it. Here is the code I have so far.

*main.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 <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;

}


*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
#ifndef SAFEARRAY_H
#define	SAFEARRAY_H

class Safe

{

private:

   // Declare variables to store A, B and C
    int A[100];
    int size;


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
    int set( int value, int pos)
    {
        if (pos < 0 || pos >= size)
        {
            //error, out of range access
        }
        else A[pos] = value;
    }
};
    
#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
#include "SafeArray.h"
#include <iostream>

using namespace std;

void Safe::print() {
    cout << A << endl;
}
//Constructor
Safe::Safe() {
    
}

// Copy constructor:
Safe::Safe(Safe & orig ) {

    
}

void Safe :: set (int a)
{
    
}

    void Safe::add ( Safe & one )
    {

    }

    void Safe::subtract (Safe & one )
    {
        
    }
1
2
3
4
5
6
7
8
9
10
11
12
void Safe::add ( Safe & one )
{
	for(int i = 0; i < 100; i++)
	{
		A[i] = A[i] + one.get(i);	
	}
}

void Safe::get(int pos)
{
	return A[i];
}


I wrote two functions for you. see how you can do the rest.
Last edited on
Topic archived. No new replies allowed.