issues in designing a class of array objects

Here is what my program is supposed to do:

Create an array and allow the user to enter values in sequential fashion as well as allow random read/write. I would want to do this using classes and objects so that the code is more dynamic and re-usable.

What I have written so far:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class array{
    private:
        int size,user_array[100];
    public:
        array() {
            for(int i=0;i<2;i++)
             user_array[i]=0;
            
        }
      /*  array(int size_n) {  //should i include this function to dynamically allocate the array??
            size=size_n;
            user_array=new int[size];
        }*/
            
    
        array(int size, int input){
            for(int i=2;i<size;i++)
            user_array[i]=input;
        }
  /*    ~array()
       {delete [] user_array;
       user_array=0;}*/
        int read(int i)//allow user to read any element of his/her choice
        {   if(i<0||i>size)
            cout<<"There is no array element with that subscript"<<endl;
            return user_array[i];
            }
       
 //       int get_element(int value)
  //      { return value;}
        
        array write(int i,int value)//allow user to write/modify any element
        {user_array[i]=value;}
        
        int &operator[](int sub)
        { if(sub<0||sub>size) //overload []
        cout<<"Terminating program, wrong choice"<<endl;
        return user_array[sub];
    }
//        friend ostream &operator << (ostream &os,array &a)//overload <<
 //       {   cout<<a.get_element(value);
 //           return os;
  //  }
};


int main() {
     
     array obj1,obj2;
     int *ptr,j,k,n,enter;
     cout<<" Number of elements?"<<endl;
     cin>>n;
    
     ptr = new int[n];
     
    int new_array[n];
        cout<<"Enter the values for this sparse array of "<<n<<" elements"<<endl;
        for(int i=0; i<n; i++)
       cin>>new_array[i];
    cout<<"Here is the array that you entered"<<endl;

    for(int i=0;i<n;i++)
    cout<<new_array[i];
    cout<<"\nWhat element do you want to view?" <<endl;
    cin>>j;
    cout<<obj1.read(j);   
   
  
//    cout<<"If you want to change an ellment, enter the subscript and the value, else program terminates?" <<endl; //this write function does not work as expected.
//    cin>>k>>enter;
  //  cout<<obj2.write(k);
     cout<<endl;
   
     return 0;
    }


Issues:I'm not able todefine a function(write_element) that allows the user to enter a value for any element in an array.

What can I do here? also, I'm unable to overload << operator to work with the class.
any other issues that you can see?

Please let me know. Thank You.
Please indent your code.

1
2
        array write(int i,int value)//allow user to write/modify any element
        {user_array[i]=value;}
¿return?
And you seem to use it with just 1 argument.

Also you've got issues with const correctness.
friend ostream &operator << (ostream &os, const array &a) in order to work with temporaries.

And finally, ¿where do you relate obj1 with new_array?
Hi

The easiest way to learn how to develop a matrix or array C++ class is to get some already well done matrix or array class or library, and see how they are coded and orginaized.

I would go to sourceforge and download me some C++ linear algebra library and learn from them.

One point that you should take into consideration when downloading a C++ library/class is the documentation, so first have a look at the documentation and then go forward....


Hope it helps
ne555,
Thank You.
I am aware that I need to indent/format the code. Sorry about that.

How do I use the second argument with the array write() function?

well, that ostram operator << function is giving me issues. I constantly get an error that says "must take only one argument".
What does this mean?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
using namespace std;

class array{
    private: int size,*storage;
    public:
        
    array() { //default constructor to initialize first two elements of array to zero
            for(int i=0;i<2;i++)
             storage[i]=0;
            
        }
    array(int size) //allows user to input the array elelments
{
	storage = new int [size];
	this -> size = size;
}


    ~array()
{
	delete [] storage;
}

    int read_element(int index)
{
	return storage[index]; 
}


    void write_element(int index, int value)
{
	storage[index] = value;
}


   int & array::operator[] (int index)
{
	if(index < 0 || index >size)
	cout<<"Error in accessing array subscript\n";
	else
    return storage[index];
 
}


     ostream & operator <<(ostream &os,array &a)
{
	int i;
	os << "[ ";
	for(i=0; i<a.size; i++)
		os << a[i] << " ";
	os << "]" << endl;
	return os;
}
}};

    int main()
{
  
int i,size;
array obj1(size),obj2; //2 objects
cout<<"Size of array?? ";
cin>>size;
cout<<"Enter elelments of array" \n ";
for(i=0;i<size;i++)

cin>>obj1[i];

cout<<"Here is what you entered :"<<endl
//for(i=0;i<3;i++)
cout<<obj2[i];
for(i=0;i<size;i++)
cout<<obj1[i];
return 0;
} 


I have modified my code as above.
My question is : how do I call the default constructor so that I can initialize the first two elements of my array to zero? I created one object here that calls the default constructor(obj2). is this correct?
when you create a new object of any class and provide no parameter, you create the object by calling the default constructor.
In your main you created the obj2 by calling the default constructor , it is ok, however your default constructor itself is not alright, it has some errors and bugs.

hope it helps
Last edited on
Topic archived. No new replies allowed.