S
-Steve-
Okay I have a bunch of code below. Hope it comes across readable.
The problem I'm having is that in the lines under main():
cout << a << endl;
Is going into the code for IntArray(const IntArray&);. Without that function
these first 5 tests work fine. Of course I need that come test6().
Also something must be wrong with the deconstructor. If I comment it out my
program goes much further without crashing out.
And in case your wondering I can't modify any of the test#() functions.
Those came from the instructor.
Please rip it apart. Thanks.
*********************
My Class:
*********************
#include <iostream>
using namespace std;
class IntArray
{
private:
int arrayLow, arrayHigh; //low & high index of array
char name; //name of object
int *array; //pointer to
beggining of array
public:
IntArray(); //Constructor - create
array index 0-9
IntArray(int); //Constructor for arrays
starting at 0
IntArray(int, int); //Constructor for arrays not
starting at 0
IntArray(const IntArray&); //Constructor for making copy of
another IntArray object
~IntArray() {delete[] array;} //Default Deconstructor
int& IntArray:perator[](int); //Overload [] for
Assigning Values
int& IntArray:perator[](int) const;
void operator=(const IntArray&); //Overload = for
Assigning two IntArray Objects
int IntArray:perator==(IntArray); //Overload == for
Comparing Arrays
int IntArray:perator!=(IntArray); //Overload != for
Comparing Arrays
IntArray IntArray:perator+(IntArray); //Overload + for Adding
Arrays
void IntArray:perator+=(IntArray); //Overload +=
friend ostream& operator<<( ostream& theStream,IntArray
toPrint); //Overload <<
int low() const {return arrayLow;} //Returns lowest
index of array
int high() const {return arrayHigh;} //Returns
highest index of array
void setName(char myName) {name=myName;} //Sets name of
object
};
*********************
My Code for the Class:
*********************
#include "intarray.h"
using namespace std;
IntArray::IntArray()
{
arrayLow=0;
arrayHigh=9;
array=new int[arrayHigh-arrayLow+1];
}
IntArray::IntArray(int size)
{
arrayLow=0; //array starts at 0
arrayHigh=size-1; //highest index of array based on given
size
array=new int[size]; //create array memory locations
}
IntArray::IntArray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayHigh>=arrayLow)
array=new int[arrayHigh-arrayLow+1]; //create array
memory locations
else
cout << "Low Array Boundry Higher than High Array
Boundry" << endl;
}
IntArray::IntArray(const IntArray& copyFrom)
{
arrayLow=copyFrom.arrayLow;
arrayHigh=copyFrom.arrayHigh;
array=new int[arrayHigh-arrayLow+1];
for(int i=arrayLow;i<arrayHigh;++i)
array=copyFrom.array;
}
int& IntArray:perator[] (int forLocation)
{
if(forLocation>=arrayHigh || forLocation<arrayLow)
//Change to halt program - Array out of bounds
return array[arrayLow+forLocation];
else
return array[arrayLow+forLocation];
}
int& IntArray:perator[](int forLocation) const
{
if(forLocation>=arrayHigh || forLocation<arrayLow)
//Change to halt program - Array out of bounds
return array[arrayLow+forLocation];
else
return array[arrayLow+forLocation];
}
void IntArray:perator=(const IntArray& copyFrom)
{
for(int i=arrayLow;i<arrayHigh;++i)
array=copyFrom.array[copyFrom.arrayLow+i];
}
int IntArray:perator== (IntArray a)
{
if((arrayHigh-arrayLow)!=(a.high()-a.low()))
return 0; //Arrays not of same length
for(int i=arrayLow;i<arrayHigh;i++)
{
if(array!=a[a.low()+i])
return 0; //Two elements do not match
}
return 1; //All elements matched, return TRUE
}
int IntArray:perator!= (IntArray a)
{
if((arrayHigh-arrayLow)!=(a.high()-a.low()))
return 1; //Arrays not of same length
for(int i=arrayLow;i<arrayHigh;i++)
{
if(array!=a[a.low()+i])
return 1; //Two elements do not match
}
return 0; //All elements matched, return FALSE
}
IntArray IntArray:perator+ (IntArray b)
{
IntArray sum(arrayHigh-arrayLow);
for(int i=0;i<(arrayHigh-arrayLow);i++)
{
// sum=array[arrayLow+i] + b[b.arrayLow()+i];
}
return sum;
}
void IntArray:perator+= (IntArray b)
{
for(int i=arrayLow;i<arrayHigh;i++)
array=array+b[b.arrayLow+i];
}
ostream& operator<<(ostream& theStream, IntArray toPrint)
{
for(int i=toPrint.arrayLow;i<=toPrint.arrayHigh;++i)
theStream << "Index: " << i << "\tValue: "
<< toPrint << endl;
return theStream;
}
*********************
What main() runs
*********************
void test1()
{
system("cls");
cout << "1. Array declared with single integer: IntArray a(10);" << endl <<
endl;
csis << "1. Array declared with single integer: IntArray a(10);" << endl <<
endl;
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a = i * 10;
a.setName('a');
cout << a << endl;
csis << a << endl;
wait();
}
void test2()
{
system("cls");
cout << "2. Array declared with two integers: IntArray b(-3, 6);" << endl <<
endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" << endl <<
endl;
IntArray b(-3, 6);
for(int i = b.low(); i <= b.high(); i++)
b = i * 10;
b.setName('b');
cout << b << endl;
csis << b << endl;
wait();
}
void test3()
{
system("cls");
cout << "3. Array declared with two integers: IntArray c(6, 8);" << endl <<
endl;
csis << "3. Array declared with two integers: IntArray c(6, 8);" << endl <<
endl;
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c = i * 10;
c.setName('c');
cout << c << endl;
csis << c << endl;
wait();
}
void test4()
{
system("cls");
cout << "4. Array declared with two identical integers: IntArray d(5, 5);"
<< endl << endl;
csis << "4. Array declared with two identical integers: IntArray d(5, 5);"
<< endl << endl;
IntArray d(5, 5);
for(int i = d.low(); i <= d.high(); i++)
d = i * 10;
d.setName('d');
cout << d << endl;
csis << d << endl;
wait();
}
void test5()
{
system("cls");
cout << "5. Array declared with no integers: IntArray z;" << endl << endl;
csis << "5. Array declared with no integers: IntArray z;" << endl << endl;
IntArray z;
for(int i = z.low(); i <= z.high(); i++)
z = i * 10;
z.setName('z');
cout << z << endl;
csis << z << endl;
wait();
}
void test6()
{
system("cls");
cout << "6. Array declared with another object of type IntArray: IntArray
c(6, 8);" << endl;
cout << "
Intarray e(c);" << endl << endl;
csis << "6. Array declared with another object of type IntArray: IntArray
c(6, 8);" << endl;
csis << "
Intarray e(c);" << endl << endl;
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c = i * 10;
c.setName('c');
cout << c << endl;
csis << c << endl;
IntArray e(c);
e.setName('e');
cout << e << endl;
csis << e << endl;
wait();
}
Steve Evans
SDSU Foundation
The problem I'm having is that in the lines under main():
cout << a << endl;
Is going into the code for IntArray(const IntArray&);. Without that function
these first 5 tests work fine. Of course I need that come test6().
Also something must be wrong with the deconstructor. If I comment it out my
program goes much further without crashing out.
And in case your wondering I can't modify any of the test#() functions.
Those came from the instructor.
Please rip it apart. Thanks.
*********************
My Class:
*********************
#include <iostream>
using namespace std;
class IntArray
{
private:
int arrayLow, arrayHigh; //low & high index of array
char name; //name of object
int *array; //pointer to
beggining of array
public:
IntArray(); //Constructor - create
array index 0-9
IntArray(int); //Constructor for arrays
starting at 0
IntArray(int, int); //Constructor for arrays not
starting at 0
IntArray(const IntArray&); //Constructor for making copy of
another IntArray object
~IntArray() {delete[] array;} //Default Deconstructor
int& IntArray:perator[](int); //Overload [] for
Assigning Values
int& IntArray:perator[](int) const;
void operator=(const IntArray&); //Overload = for
Assigning two IntArray Objects
int IntArray:perator==(IntArray); //Overload == for
Comparing Arrays
int IntArray:perator!=(IntArray); //Overload != for
Comparing Arrays
IntArray IntArray:perator+(IntArray); //Overload + for Adding
Arrays
void IntArray:perator+=(IntArray); //Overload +=
friend ostream& operator<<( ostream& theStream,IntArray
toPrint); //Overload <<
int low() const {return arrayLow;} //Returns lowest
index of array
int high() const {return arrayHigh;} //Returns
highest index of array
void setName(char myName) {name=myName;} //Sets name of
object
};
*********************
My Code for the Class:
*********************
#include "intarray.h"
using namespace std;
IntArray::IntArray()
{
arrayLow=0;
arrayHigh=9;
array=new int[arrayHigh-arrayLow+1];
}
IntArray::IntArray(int size)
{
arrayLow=0; //array starts at 0
arrayHigh=size-1; //highest index of array based on given
size
array=new int[size]; //create array memory locations
}
IntArray::IntArray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayHigh>=arrayLow)
array=new int[arrayHigh-arrayLow+1]; //create array
memory locations
else
cout << "Low Array Boundry Higher than High Array
Boundry" << endl;
}
IntArray::IntArray(const IntArray& copyFrom)
{
arrayLow=copyFrom.arrayLow;
arrayHigh=copyFrom.arrayHigh;
array=new int[arrayHigh-arrayLow+1];
for(int i=arrayLow;i<arrayHigh;++i)
array=copyFrom.array;
}
int& IntArray:perator[] (int forLocation)
{
if(forLocation>=arrayHigh || forLocation<arrayLow)
//Change to halt program - Array out of bounds
return array[arrayLow+forLocation];
else
return array[arrayLow+forLocation];
}
int& IntArray:perator[](int forLocation) const
{
if(forLocation>=arrayHigh || forLocation<arrayLow)
//Change to halt program - Array out of bounds
return array[arrayLow+forLocation];
else
return array[arrayLow+forLocation];
}
void IntArray:perator=(const IntArray& copyFrom)
{
for(int i=arrayLow;i<arrayHigh;++i)
array=copyFrom.array[copyFrom.arrayLow+i];
}
int IntArray:perator== (IntArray a)
{
if((arrayHigh-arrayLow)!=(a.high()-a.low()))
return 0; //Arrays not of same length
for(int i=arrayLow;i<arrayHigh;i++)
{
if(array!=a[a.low()+i])
return 0; //Two elements do not match
}
return 1; //All elements matched, return TRUE
}
int IntArray:perator!= (IntArray a)
{
if((arrayHigh-arrayLow)!=(a.high()-a.low()))
return 1; //Arrays not of same length
for(int i=arrayLow;i<arrayHigh;i++)
{
if(array!=a[a.low()+i])
return 1; //Two elements do not match
}
return 0; //All elements matched, return FALSE
}
IntArray IntArray:perator+ (IntArray b)
{
IntArray sum(arrayHigh-arrayLow);
for(int i=0;i<(arrayHigh-arrayLow);i++)
{
// sum=array[arrayLow+i] + b[b.arrayLow()+i];
}
return sum;
}
void IntArray:perator+= (IntArray b)
{
for(int i=arrayLow;i<arrayHigh;i++)
array=array+b[b.arrayLow+i];
}
ostream& operator<<(ostream& theStream, IntArray toPrint)
{
for(int i=toPrint.arrayLow;i<=toPrint.arrayHigh;++i)
theStream << "Index: " << i << "\tValue: "
<< toPrint << endl;
return theStream;
}
*********************
What main() runs
*********************
void test1()
{
system("cls");
cout << "1. Array declared with single integer: IntArray a(10);" << endl <<
endl;
csis << "1. Array declared with single integer: IntArray a(10);" << endl <<
endl;
IntArray a(10);
for(int i = a.low(); i <= a.high(); i++)
a = i * 10;
a.setName('a');
cout << a << endl;
csis << a << endl;
wait();
}
void test2()
{
system("cls");
cout << "2. Array declared with two integers: IntArray b(-3, 6);" << endl <<
endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" << endl <<
endl;
IntArray b(-3, 6);
for(int i = b.low(); i <= b.high(); i++)
b = i * 10;
b.setName('b');
cout << b << endl;
csis << b << endl;
wait();
}
void test3()
{
system("cls");
cout << "3. Array declared with two integers: IntArray c(6, 8);" << endl <<
endl;
csis << "3. Array declared with two integers: IntArray c(6, 8);" << endl <<
endl;
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c = i * 10;
c.setName('c');
cout << c << endl;
csis << c << endl;
wait();
}
void test4()
{
system("cls");
cout << "4. Array declared with two identical integers: IntArray d(5, 5);"
<< endl << endl;
csis << "4. Array declared with two identical integers: IntArray d(5, 5);"
<< endl << endl;
IntArray d(5, 5);
for(int i = d.low(); i <= d.high(); i++)
d = i * 10;
d.setName('d');
cout << d << endl;
csis << d << endl;
wait();
}
void test5()
{
system("cls");
cout << "5. Array declared with no integers: IntArray z;" << endl << endl;
csis << "5. Array declared with no integers: IntArray z;" << endl << endl;
IntArray z;
for(int i = z.low(); i <= z.high(); i++)
z = i * 10;
z.setName('z');
cout << z << endl;
csis << z << endl;
wait();
}
void test6()
{
system("cls");
cout << "6. Array declared with another object of type IntArray: IntArray
c(6, 8);" << endl;
cout << "
Intarray e(c);" << endl << endl;
csis << "6. Array declared with another object of type IntArray: IntArray
c(6, 8);" << endl;
csis << "
Intarray e(c);" << endl << endl;
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c = i * 10;
c.setName('c');
cout << c << endl;
csis << c << endl;
IntArray e(c);
e.setName('e');
cout << e << endl;
csis << e << endl;
wait();
}
Steve Evans
SDSU Foundation