Passing an object through a constructor?

This is a home problem. I am trying to pass an object through a constructor to create a new object with double values. The object is an int array with upper and lower indices and size. I'm not sure if I just changed the values ​​of the private variables to * 2, or if I missed something completely.

Here is my class header:

const int SIZE = 100;

class intArray{
private:
    int iArray[SIZE];
    int arrLower, arrUpper;
    int size;

public:
    intArray();
    intArray(int size);
    intArray(int lower, int upper);
    intArray(intArray& input);
    int high();
    int low();
    int& operator[] (int size)             {  return iArray[size];  }

};

and all that I have for the constructor:

intArray::intArray(intArray& input){

    input.iArray[size] *= 2;
    input.size *= 2;
    input.arrUpper *= 2;
    input.arrLower *= 2;

}

which I’m not even sure is true.

+4
source share
3 answers
intArray::intArray(intArray& input){

    input.iArray[size] *= 2;
    input.size *= 2;
    input.arrUpper *= 2;
    input.arrLower *= 2;

}

This is not true.

Since you are all a prefix input., you change the original that you do not need. Just copy all the old values, but multiply by two

intArray::intArray(intArray& input){

    for(int i = 0; i < size; i++) {
        iArray[i] = input.iArray[i] * 2; //set this array to 2x the input array
    }
    size = input.size * 2;
    arrUpper = input.arrUpper * 2;
    arrLower = input.arrLower * 2;

}

, , size , . , , , . arrUpper arrLower? , .

, , , .

intArray::intArray(intArray& input){
    size = input.size; //keep this the same
    for(int i = 0; i < size; i++) {
        iArray[i] = input.iArray[i] * 2; //set this array to 2x the input array
    }
    arrUpper = input.arrUpper; //what is this even?
    arrLower = input.arrLower;

}
+5

, const&. , ( , ).

intArray::intArray(intArray const& input) {
    for (int i = 0; i < size; i++) {
        iArray[i] = 2 * input.iArray[i];
    }

    size = 2 * input.size;
    arrUpper = 2 * input.arrUpper;
    input.arrLower = 2 * input.arrLower;
}
+2

, (intArray:: intArray (intArray &)). .

intArray::intArray(intArray& input)
{

    for(int i=input.low(); i<=input.hig(); ++i)
    {
        iArray[i] = input.iArray[i]*2;
    }
}
+1
source

All Articles