Array Class

Hello, I am doing an Array of object type, but I need some help because I want to create a constructor that can be something like:

Array(int A[], int size);

but I dont know how to do it, because the problem is that it is creating 100 elments instead of 10 because of the first constructor. I just help me with thgis part not with the other that are incomplete. Thanks you.

Here is the code

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
77
78
79
80
81
82
#include <iostream>

using namespace std;


class Array {
      private:
              int size;
              int counter;
              int *ptr;
              int *ptr2;

      public:
             Array(int = 100);
             Array(const Array &, int);
             void print();
             void capacity();
             void insert(int, int);
             void insert(int);
             
      

};

Array::Array(int Arrsize){
                 if (Arrsize <=0){
                             size = 100;
                             }
                 else{
                      size = Arrsize;
                      }
                 
                 ptr = new int[size];
                 for(int i=0; i<size; i++){
                         ptr[i]= 0;
                         }
}
Array::Array(const Array &nuevo, int siize){
                   if (siize <=0){
                             size = 100;
                             cout << " >>>Hereee 1<<< ";
                             }
                 else{
                      size = siize;
                      cout << " >>>Hereee 2<<< ";
                      }
                 
                 ptr = new int[size];
                 for(int i=0; i<size; i++){
                         ptr[i]= 0;
                         }
                 cout << " >>>Hereee 3<<< ";

}


void Array::print(){
               for(int i=0; i<size; i++){
               cout << ptr[i];
            }
}

void Array::capacity(){
        cout << "HERE NOTHING YET!";               
     
     }

void Array::insert(int val, int p){
     
     
     }

int main (){
    Array arr;
    Array(arr, 10);
    arr.print();
    arr.capacity();
    
    system("pause");
    return 0;

}


the problem is here :
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
Array::Array(int Arrsize){
                 if (Arrsize <=0){
                             size = 100;
                             }
                 else{
                      size = Arrsize;
                      }
                 
                 ptr = new int[size];
                 for(int i=0; i<size; i++){
                         ptr[i]= 0;
                         }
}
Array::Array(const Array &nuevo, int siize){
                   if (siize <=0){
                             size = 100;
                             cout << " >>>Hereee 1<<< ";
                             }
                 else{
                      size = siize;
                      cout << " >>>Hereee 2<<< ";
                      }
                 
                 ptr = new int[size];
                 for(int i=0; i<size; i++){
                         ptr[i]= 0;
                         }
                 cout << " >>>Hereee 3<<< ";

}

int main(){
Array(arr, 10);
}
Last edited on
What is line 14 all about ?
It looks like a default constructor but you're trying to store a value into an int data type itself.
Last edited on
Sorry I got the answer, I was not calling the function correctly, because instead doing like this:

Array (arr, 10);

should be like this:

Array (New Object)(arr, 10);

Array B(arr, 10);

That copies and create a new Object of an Array with 10 elements.

Thanks you Soranz anyway.

Here is the complete code if someone wants to see it:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>

using namespace std;

template <class T>
class Array {
      private:
              T size;
              T counter;
              T *ptr;
              T cap;
              void counterr();
              void counterPlus();

      public:
             Array(T = 100);
             Array(const Array &, T);
             void print();
             void capacity();
             void insert(T, T);
             void insert(T);
             void length();
             T remove(T);
             T search(T);
             
             ~Array();
      

};
template <class T>
void Array<T>::length(){
     cout << "The current length of the Array is: " << counter << endl; 
     
     }
template <class T>
void Array<T>::counterr(){
               counter = 0;
               }
template <class T>
void Array<T>::counterPlus(){
               counter = counter + 1;
               }
template <class T>
Array<T>::Array(T Arrsize){
                 if (Arrsize <=0){
                             size = 100;
                             }
                 else{
                      size = Arrsize;
                      }
                 
                 ptr = new int[size];
                 for(int i=0; i<size; i++){
                         ptr[i]= 0;
                         }
                 counterr();
}
template <class T>
Array<T>::Array(const Array &nuevo, T siize){
                   if (siize <=0){
                             size = 100;
                             }
                 else{
                      size = siize;
                      }
                 
                 ptr = new int[size];
                 for(int i=0; i<size; i++){
                         ptr[i]= 0;
                         }
                 counterr();

}

template <class T>
void Array<T>::print(){
     cout << endl << "Your array is: ";
     for(int i=0; i<size; i++){
                cout << ptr[i] << " ";
            }
     cout << endl;
}

template <class T>
void Array<T>::capacity(){
        cap = size - counter;
        cout << "Your capacity right now is: " << cap << endl;
        cout << "The size of your array is: " << size << endl;
            
     }
     
template <class T>
void Array<T>::insert(T val, T p){
     if(p > counter){
          ptr[counter] = val;
          counterPlus();
          }
     else{
         for(int i=counter + 1; i >= p-1 ; i--){
                 ptr[i]=ptr[i-1];
                 }
         ptr[p - 1] = val;
         counterPlus();
         }
}

template <class T>
void Array<T>::insert(T value){
          ptr[counter] = value;
          counterPlus();
}


template <class T>
T Array<T>::remove(T val){
     T j = 0;
     T p = 0;
     while(j == 0){
             for(int i = 0 ; i< counter; i++){
                     if(ptr[i] == val){
                               j = 1;
                               p = i;
                               break;
                               }
             }
             if(j == 0){
                  break;
                  }
             if(j == 1){
                  for(int i = p+1; i <counter+1 ; i++){
                          ptr[i-1] = ptr[i];
                          }
                  counterPlus();
                  break;
                  
                  }
     }
     if(j == 0){
          return -1;
          }
     else
         cout << "The value removed was in position: " << p << endl;
}

template <class T>
T Array<T>::search(T val){
     T j = 0;
     T p = 0;
     while(j == 0){
             for(int i = 0 ; i< counter; i++){
                     if(ptr[i] == val){
                               j = 1;
                               p = i;
                               break;
                               }
             }    
     if(j == 0){
          return -1;
          }
     else
         cout << "The value searched is in position: " << p << endl;
}
}    

template <class T>
Array<T>::~Array(){
                delete []ptr;
                cout << "Destructor called" << endl;
                }




int main (){
    
    Array<int> arr;
    Array<int> B(arr, 10);
    
    arr.insert(3, 5);
    arr.insert(4, 5);
    arr.insert(7, 5);
    arr.insert(2, 5);
    arr.insert(1, 5);
    arr.insert(5, 5);
    arr.insert(9, 5);
    arr.insert(6);
    arr.print();
    B.insert(1, 5);
    B.insert(2, 5);
    B.insert(3, 5);
    B.insert(4, 5);
    B.insert(5, 5);
    B.insert(6, 5);
    B.insert(7, 5);
    B.print();
    arr.capacity();
    B.capacity();
    arr.insert(22);
    arr.print();
    arr.length();
    B.length();
    B.remove(5); 
    B.print();  
    B.search(3);
    
    system("pause");
    return 0;

}




Topic archived. No new replies allowed.