Unless your code is truly enormous, it's better if you paste it directly into your post (between [code][/code] tags):
Code:
#include <iostream>
using namespace std;
class VectorOfInt
{
public:
VectorOfInt();
VectorOfInt(int user_array_size);
~VectorOfInt();
int get(int index, int *result);
int set_value(int index, int value);
int pushback(int value);
int pushfront(int value);
int fill_array(int number_of_elements);
int auto_fill_array();
int grow_array();
int print_array();
int print_array_constructor();
VectorOfInt& operator=(const VectorOfInt& other);
VectorOfInt (const VectorOfInt& other);
private:
int _array_size;
int *_internal_array;
int _number_of_elements;
};
VectorOfInt :: VectorOfInt()
{
_array_size = 32;
_number_of_elements = 0;
_internal_array = new int [_array_size];
for(int i = 0; i< _array_size; i++)
{
_internal_array[i] = 0;
}
cout<<"\nThis is the state of the array after the default constructor is run";
print_array_constructor();
}
VectorOfInt :: VectorOfInt(int user_array_size)
{
if(user_array_size <0)
{
cout<<"VectorOfInt :: VectorOfInt(int user_array_size):The array size requested is less than 0, which is not allowed so defaulting to an array size of 0";
_array_size = 0;
}
else
{
_array_size = user_array_size;
}
_number_of_elements = 0;
_internal_array = new int [_array_size];
for(int i = 0; i< _array_size; i++)
{
_internal_array[i] = 0;
}
cout<<"\nThis is the state of the array after the size constructor is run";
print_array_constructor();
}
VectorOfInt :: ~VectorOfInt()
{
delete [] _internal_array;
}
int VectorOfInt :: get(int index, int * result)
{
if((index <0) || index >= _array_size)
{
cout<<"\nint VectorOfInt :: get(int index, int * result):Invalid array index "<<index;
*result = 0;
return(-1);
}
*result = _internal_array[index];
/*cout<<"\nThis is the state of the array after the get method is run with index value "<<index;
print_array();*/
return(0);
}
int VectorOfInt :: set_value(int index, int value)
{
if((index <0) || index >= _array_size)
{
cout<<"\nint VectorOfInt :: set_value(int index, int value):Invalid array index "<<index;
return(-1);
}
_internal_array[index]=value;
/* cout<<"\nThis is the state of the array after the set method is run with index "<<index<<" and value at index "<<value;
print_array(); */
return(0);
}
int VectorOfInt:: fill_array (int number_of_elements)
{
if((number_of_elements <0) || number_of_elements > _array_size)
{
return(-1);
}
for(int i= 0; i<number_of_elements;i++)
{
int value;
cout<<"Enter the "<<i<<" value"<<endl;
cin>>value;
set_value(i,value);
}
_number_of_elements = number_of_elements;
cout<<"\nThis is the state of the array after the fill array is run";
print_array();
return(0);
}
int VectorOfInt :: auto_fill_array ()
{
if(_array_size >=2)
{
for(int i=0; i<_array_size -2; i++)
{
set_value(i,i);
}
_number_of_elements = _array_size-2;
}
cout<<"\nThis is the state of the array after the auto fill array is run";
print_array();
return(0);
}
int VectorOfInt :: pushback(int value)
{
if(_number_of_elements ==_array_size)
grow_array();
cout<<"\nPush back was called with value "<<value;
set_value(_number_of_elements++,value);
cout<<"\nThis is the state of the array after the pushback "<<value<<" is run";
print_array();
return(0);
}
int VectorOfInt :: pushfront(int value)
{
if(_number_of_elements ==_array_size)
grow_array();
for(int i = _number_of_elements++;i>=1;i--)
{
set_value(i,_internal_array[i-1]);
}
set_value(0,value);
cout<<"\nThis is the state of the array after the pushfront "<<value<<" is run";
print_array();
return(0);
}
int VectorOfInt ::grow_array()
{
cout<<"\nThis is the state of the array before the grow array is run ";
print_array();
if(_array_size==0)_array_size++;
int *p_new_array = new int[_array_size*2 ];
for ( int i = 0; i < _array_size; ++i )
{
p_new_array[ i ] = _internal_array[ i ];
}
delete [] _internal_array;
_internal_array=p_new_array;
_array_size *=2;
cout<<"\nThis is the state of the array after the grow array is run ";
print_array();
return(0);
}
int VectorOfInt::print_array()
{
cout<<"\narray size is "<<_array_size;
cout<<"\nnumber of elements in the array is "<<_number_of_elements;
cout<<"\nindex";
for(int i=0;i<_number_of_elements;i++)
{
cout<<"\t"<<i;
}
cout<<"\nvalue";
for(int i=0;i<_number_of_elements;i++)
{
cout<<"\t"<<_internal_array[i];
}
return(0);
}
int VectorOfInt::print_array_constructor()
{
cout<<"\narray size is "<<_array_size;
cout<<"\nnumber of elements in the array is "<<_number_of_elements;
cout<<"\nindex";
for(int i=0;i<_array_size;i++)
{
cout<<"\t"<<i;
}
cout<<"\nvalue";
for(int i=0;i<_array_size;i++)
{
cout<<"\t"<<_internal_array[i];
}
return(0);
}
VectorOfInt& VectorOfInt::operator=(const VectorOfInt& other)
{
if (this == & other)
return *this;
delete [] _internal_array;
_array_size=other._array_size;
_number_of_elements=other._number_of_elements;
_internal_array = new int [_array_size];
for(int i=0;i<_array_size;i++)
{
_internal_array[i]=other._internal_array[i];
}
return *this;
}
VectorOfInt::VectorOfInt (const VectorOfInt& other)
{
_array_size=other._array_size;
_number_of_elements=other._number_of_elements;
_internal_array = new int [_array_size];
for(int i=0;i<_number_of_elements;i++)
{
this->_internal_array[i]=other._internal_array[i];
}
}
int main()
{
cout << "Hello world!" << endl;
VectorOfInt VectorOfInt1;
VectorOfInt1.auto_fill_array();
int value;
VectorOfInt1.get(5,&value);
cout<<"\nThe value at index 5 is "<<value;
VectorOfInt1.set_value(6,60);
cout<<"\nThis is the state of the array after the set method is run with index "<<6<<" and value at index "<<value;
VectorOfInt1.print_array();
VectorOfInt1.pushback(70);
VectorOfInt1.pushfront(70);
VectorOfInt1.pushfront(80);
VectorOfInt1.pushback(80);
cout<<"\nThis is for VectorOfInt2\n";
VectorOfInt VectorOfInt2(45);
VectorOfInt2.auto_fill_array();
VectorOfInt2.get(7,&value);
cout<<"\nThe value at index 7 is "<<value;
VectorOfInt2.set_value(8,60);
cout<<"\nThis is the state of the array after the set method is run with index "<<8<<" and value at index "<<60;
VectorOfInt2.print_array();
VectorOfInt2.pushback(70);
VectorOfInt2.pushfront(70);
VectorOfInt2.pushfront(80);
VectorOfInt2.pushback(80);
cout<<"\nThis is for VectorOfInt3\n";
VectorOfInt VectorOfInt3(-45);
VectorOfInt3.auto_fill_array();
VectorOfInt3.get(9,&value);
cout<<"\nThe value at index 9 is "<<value;
VectorOfInt3.set_value(10,60);
cout<<"\nThis is the state of the array after the set method is run with index "<<10<<" and value at index "<<60;
VectorOfInt3.print_array();
VectorOfInt3.pushback(70);
VectorOfInt3.pushfront(70);
VectorOfInt3.pushfront(80);
VectorOfInt3.pushback(80);
cout<<"\nThis is for VectorOfInt4\n";
VectorOfInt VectorOfInt4(0);
VectorOfInt4.auto_fill_array();
VectorOfInt4.get(0,&value);
cout<<"\nThe value at index 0 is "<<value;
VectorOfInt4.set_value(0,60);
VectorOfInt4.pushback(70);
VectorOfInt4.pushfront(70);
VectorOfInt4.pushfront(80);
VectorOfInt4.pushback(80);
cout<<"\nThis is for VectorOfInt5\n";
VectorOfInt VectorOfInt5(15);
VectorOfInt5.auto_fill_array();
VectorOfInt5.get(9,&value);
cout<<"\nThe value at index 5 is "<<value;
VectorOfInt5.set_value(6,60);
VectorOfInt5.pushback(70);
VectorOfInt5.pushfront(70);
VectorOfInt5.pushfront(80);
VectorOfInt5.pushback(80);
VectorOfInt5 = VectorOfInt5 ;
cout<<"\n The state of the VectorInt5 after assigning to itself is ";
VectorOfInt5.print_array();
VectorOfInt5 = VectorOfInt1 ;
cout<<"\n The state of the VectorInt5 after assigning to VectorInt1 is ";
VectorOfInt5.print_array();
VectorOfInt VectorOfInt6(VectorOfInt5);
cout<<"\n The state of the VectorInt6 after running copy constructor on it using VectorOfInt5 is ";
VectorOfInt6.print_array();
cout<<"\n The state of the VectorInt5 after running copy constructor on it is ";
VectorOfInt5.print_array();
return 0;
}
My compiler immediately warned me about the misleading indentation following the if-statement on line #152:
Code:
if(_number_of_elements ==_array_size)
grow_array(); // only this line is executed conditionally
for(int i = _number_of_elements++;i>=1;i--)
{
...
Haven't examined the code itself.