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::operator[](int); //Overload [] for
Assigning Values
int& IntArray::operator[](int) const;
void operator=(const IntArray&); //Overload = for
Assigning two IntArray Objects
int IntArray::operator==(IntArray); //Overload == for
Comparing Arrays
int IntArray::operator!=(IntArray); //Overload != for
Comparing Arrays
IntArray IntArray::operator+(IntArray); //Overload + for Adding
Arrays
void IntArray::operator+=(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[i]=copyFrom.array[i];
}
int& IntArray::operator[] (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::operator[](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::operator=(const IntArray& copyFrom)
{
for(int i=arrayLow;i<arrayHigh;++i)
array[i]=copyFrom.array[copyFrom.arrayLow+i];
}
int IntArray::operator== (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[i]!=a[a.low()+i])
return 0; //Two elements do not match
}
return 1; //All elements matched, return TRUE
}
int IntArray::operator!= (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[i]!=a[a.low()+i])
return 1; //Two elements do not match
}
return 0; //All elements matched, return FALSE
}
IntArray IntArray::operator+ (IntArray b)
{
IntArray sum(arrayHigh-arrayLow);
for(int i=0;i<(arrayHigh-arrayLow);i++)
{
// sum[i]=array[arrayLow+i] + b[b.arrayLow()+i];
}
return sum;
}
void IntArray::operator+= (IntArray b)
{
for(int i=arrayLow;i<arrayHigh;i++)
array[i]=array[i]+b[b.arrayLow+i];
}
ostream& operator<<(ostream& theStream, IntArray toPrint)
{
for(int i=toPrint.arrayLow;i<=toPrint.arrayHigh;++i)
theStream << "Index: " << i << "\tValue: "
<< toPrint[i] << 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] = 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] = 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] = 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] = 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] = 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] = 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 2 1903
-Steve- wrote: 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.
-Steve-
Between the excess vertical whitespace and the lack of indentation, your
code is almost impossible to read. In order to get an appropriate
response, may I suggest you fix that and resubmit (hint: use spaces
instead of tabs; tabs often don't translate well across email).
Sorry,
--ag *********************
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::operator[](int); //Overload [] for
Assigning Values
int& IntArray::operator[](int) const;
void operator=(const IntArray&); //Overload = for
Assigning two IntArray Objects
int IntArray::operator==(IntArray); //Overload == for
Comparing Arrays
int IntArray::operator!=(IntArray); //Overload != for
Comparing Arrays
IntArray IntArray::operator+(IntArray); //Overload + for Adding
Arrays
void IntArray::operator+=(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[i]=copyFrom.array[i];
}
int& IntArray::operator[] (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::operator[](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::operator=(const IntArray& copyFrom)
{
for(int i=arrayLow;i<arrayHigh;++i)
array[i]=copyFrom.array[copyFrom.arrayLow+i];
}
int IntArray::operator== (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[i]!=a[a.low()+i])
return 0; //Two elements do not match
}
return 1; //All elements matched, return TRUE
}
int IntArray::operator!= (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[i]!=a[a.low()+i])
return 1; //Two elements do not match
}
return 0; //All elements matched, return FALSE
}
IntArray IntArray::operator+ (IntArray b)
{
IntArray sum(arrayHigh-arrayLow);
for(int i=0;i<(arrayHigh-arrayLow);i++)
{
// sum[i]=array[arrayLow+i] + b[b.arrayLow()+i];
}
return sum;
}
void IntArray::operator+= (IntArray b)
{
for(int i=arrayLow;i<arrayHigh;i++)
array[i]=array[i]+b[b.arrayLow+i];
}
ostream& operator<<(ostream& theStream, IntArray toPrint)
{
for(int i=toPrint.arrayLow;i<=toPrint.arrayHigh;++i)
theStream << "Index: " << i << "\tValue: "
<< toPrint[i] << 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] = 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] = 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] = 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] = 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] = 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] = 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
--
Artie Gold -- Austin, Texas
I've posted it on the web: http://planetevans.com/operators.htm
However I'm going to break down the problem more and post the seperate
problems. Thanks.
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:3F**********@austin.rr.com... -Steve- wrote: 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.
-Steve-
Between the excess vertical whitespace and the lack of indentation, your code is almost impossible to read. In order to get an appropriate response, may I suggest you fix that and resubmit (hint: use spaces instead of tabs; tabs often don't translate well across email).
Sorry, --ag
*********************
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::operator[](int); //Overload [] for
Assigning Values
int& IntArray::operator[](int) const;
void operator=(const IntArray&); //Overload = for
Assigning two IntArray Objects
int IntArray::operator==(IntArray); //Overload == for
Comparing Arrays
int IntArray::operator!=(IntArray); //Overload != for
Comparing Arrays
IntArray IntArray::operator+(IntArray); //Overload + for Adding
Arrays
void IntArray::operator+=(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[i]=copyFrom.array[i];
}
int& IntArray::operator[] (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::operator[](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::operator=(const IntArray& copyFrom)
{
for(int i=arrayLow;i<arrayHigh;++i)
array[i]=copyFrom.array[copyFrom.arrayLow+i];
}
int IntArray::operator== (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[i]!=a[a.low()+i])
return 0; //Two elements do not match
}
return 1; //All elements matched, return TRUE
}
int IntArray::operator!= (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[i]!=a[a.low()+i])
return 1; //Two elements do not match
}
return 0; //All elements matched, return FALSE
}
IntArray IntArray::operator+ (IntArray b)
{
IntArray sum(arrayHigh-arrayLow);
for(int i=0;i<(arrayHigh-arrayLow);i++)
{
// sum[i]=array[arrayLow+i] + b[b.arrayLow()+i];
}
return sum;
}
void IntArray::operator+= (IntArray b)
{
for(int i=arrayLow;i<arrayHigh;i++)
array[i]=array[i]+b[b.arrayLow+i];
}
ostream& operator<<(ostream& theStream, IntArray toPrint)
{
for(int i=toPrint.arrayLow;i<=toPrint.arrayHigh;++i)
theStream << "Index: " << i << "\tValue: "
<< toPrint[i] << 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] = 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] = 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] = 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] = 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] = 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] = 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
-- Artie Gold -- Austin, Texas This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by Andy Jarrell |
last post: by
|
3 posts
views
Thread by N4M |
last post: by
|
20 posts
views
Thread by Brad Eck |
last post: by
|
1 post
views
Thread by masood.iqbal |
last post: by
|
4 posts
views
Thread by masood.iqbal |
last post: by
|
10 posts
views
Thread by maadhuu |
last post: by
|
14 posts
views
Thread by ambar.shome |
last post: by
|
1 post
views
Thread by Alex Zhitlenok |
last post: by
|
5 posts
views
Thread by raylopez99 |
last post: by
|
3 posts
views
Thread by iluvatar |
last post: by
| | | | | | | | | | |