This is a homework problem. I am trying to pass an object through a constructor to create a new object with double the values. The object is an int array with upper and lower indices and a size. I'm not sure if I just change the values of the private variables to * 2
or if I'm missing something completely.
Here's 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 I have for the constructor so far:
intArray::intArray(intArray& input){
input.iArray[size] *= 2;
input.size *= 2;
input.arrUpper *= 2;
input.arrLower *= 2;
}
which I am not even sure is correct.
intArray::intArray(intArray& input){
input.iArray[size] *= 2;
input.size *= 2;
input.arrUpper *= 2;
input.arrLower *= 2;
}
This is incorrect.
Since you prefix everything with input.
, you are modifying the original which is not what you want. Just copy all the old values, but multiplied 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;
}
While this will sort of work, the array is the same size, but size
is multiplied by two, which is just wrong. I'm not sure exactly what your class is actually supposed to be doing, but I can tell you it doesn't really make sense. What are arrUpper
, and arrLower
? If you answer, I can help you some more.
Here is one that makes a little more sense, but I still don't know what some of your variables mean.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With