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
Registered users can post here. Sign in or register to post.