Hi All!
I am a beginner to programming.
I learn C++ from site cplusplus.com.
I wanted to try operator overloading.
I wrote a C++ program using arrays.
I have now an instable code.
I don't know what's happening
but sometimes the code does what I want,
at other runtime it behaves strangely.
My C++ code was designed for this purpose:
1. Ask Length of Array A and B and C
2. Ask the Values of Array A and B
3. Length of C = Length of A + Length of B
4. Create a class CArray
5. Objects of this class get their values at initialization through constructors:
5.a. CArray obj(Array,Length) pass the Array and Length to the object
5.a. CArray obj(Length) pass the Length to the object
and create a Length length array filled with zeros.
6. Overload the operator ^
The operator function is a member of class CArray.
New meaning of operator ^
C=A^B concatenate the two arrays (A,B) into one (C).
Tutorials I used:
for Constructors and destructors
http://www.cplusplus.com/doc/tutorial/classes/
and
for Overloading operators
http://www.cplusplus.com/doc/tutorial/classes2/
INSATBLE RESULTS - WHY? I DON"T KNOW.
Any suggestion?
My Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// concatenate two arrays
#include <iostream>
using namespace std;
class CArray {
public:
int * cAry; int cLen;
CArray(int conAry[], int conLen); CArray(int conLen); ~CArray();
CArray operator ^ (CArray ritAry);
};
CArray::CArray(int conAry[], int conLen) {
cLen = conLen; cAry = new int [conLen];
for(int i=0;i<conLen;i++) cAry[i] = conAry[i];
}
CArray::~CArray() {
delete [] cAry;
}
CArray::CArray(int conLen) {
cLen = conLen; cAry = new int [conLen];
for(int i=0;i<conLen;i++) cAry[i] = 0;
}
CArray CArray::operator^ (CArray ritAry) {
int resLen=cLen+ritAry.cLen;
CArray resAry(resLen);
for(int i=0;i<cLen;i++)
resAry.cAry[i] = cAry[i];
for(int i=cLen,j=0;i<resLen;i++,j++)
resAry.cAry[i] = ritAry.cAry[j];
return (resAry);
}
void input4ary (int ary[], int lgth) {
for(int i=0;i<lgth;i++) {
cout << "ary["<<i<<"]= "; cin >> ary[i]; cout << "\n";
}
cout << "\n";
}
int main ()
{
// input for array A
int length_A; cout << "How many numbers do you want to type? ";
cin >> length_A; cout << "\n";
int * ary_A = new int [length_A]; input4ary(ary_A,length_A);
// input for array B
int length_B; cout << "How many numbers do you want to type? ";
cin >> length_B; cout << "\n";
int * ary_B = new int [length_B]; input4ary(ary_B,length_B);
// length for array C
int length_C; length_C = length_A + length_B;
cout << "\n";
CArray a(ary_A,length_A);
CArray b(ary_B,length_B);
CArray c(length_C);
c = a ^ b;
// Checking results
cout << "Length of array of object a: " << (a.cLen) << "\n\n";
cout << "Array of object a: \n\n";
for(int i=0;i<(a.cLen);i++)
cout << "a.cAry["<<i<<"]= " << (a.cAry[i]) << "\n";
cout << "\n";
cout << "Length of array of object b: " << (b.cLen) << "\n\n";
cout << "Array of object b: \n\n";
for(int i=0;i<(b.cLen);i++)
cout << "b.cAry["<<i<<"]= " << (b.cAry[i]) << "\n";
cout << "\n";
cout << "Length of array of object c: " << (c.cLen) << "\n\n";
cout << "Array of object c: \n\n";
for(int i=0;i<(c.cLen);i++)
cout << "c.cAry["<<i<<"]= " << (c.cAry[i]) << "\n";
cout << "\n";
return 0;
}
| |
Tests on different IDEs:
Orwell Dev-C++ - Dev-Cpp 5.4.0 TDM-GCC x64 4.7.1 Setup.EXE
TDM-GCC 4.7.1 32bit
Sometimes it works as designed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
How many numbers do you want to type? 3
ary[0]= 1
ary[1]= 2
ary[2]= 3
How many numbers do you want to type? 2
ary[0]= 1
ary[1]= 4
Length of array of object a: 3
Array of object a:
a.cAry[0]= 1
a.cAry[1]= 2
a.cAry[2]= 3
Length of array of object b: 2
Array of object b:
b.cAry[0]= 1
b.cAry[1]= 4
Length of array of object c: 5
Array of object c:
c.cAry[0]= 1
c.cAry[1]= 2
c.cAry[2]= 3
c.cAry[3]= 1
c.cAry[4]= 4
Process exited with return value 0
Press any key to continue . . .
| |
Sometimes it crashes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
How many numbers do you want to type? 5
ary[0]= 1
ary[1]= 2
ary[2]= 3
ary[3]= 4
ary[4]= 5
How many numbers do you want to type? 3
ary[0]= 6
ary[1]=
AND THAT'S IT. HERE THE CODE CRASHES.
| |
Code::Blocks 12.11 - GNU GCC Compiler:
Sometimes it works as if it read wrong data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
How many numbers do you want to type? 3
ary[0]= 1
ary[1]= 2
ary[2]= 3
How many numbers do you want to type? 2
ary[0]= 1
ary[1]= 4
Length of array of object a: 3
Array of object a:
a.cAry[0]= 1
a.cAry[1]= 2
a.cAry[2]= 3
Length of array of object b: 2
Array of object b:
b.cAry[0]= 8654776
b.cAry[1]= 8650948
Length of array of object c: 5
Array of object c:
c.cAry[0]= 8655112
c.cAry[1]= 8654728
c.cAry[2]= 3
c.cAry[3]= 1
c.cAry[4]= 4
Process returned 0 (0x0) execution time : 36.516 s
Press any key to continue.
| |
Other test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
How many numbers do you want to type? 3
ary[0]= 1
ary[1]= 2
ary[2]= 3
How many numbers do you want to type? 2
ary[0]= 1
ary[1]= 4
Length of array of object a: 3
Array of object a:
a.cAry[0]= 1
a.cAry[1]= 2
a.cAry[2]= 3
Length of array of object b: 2
Array of object b:
b.cAry[0]= 8654776
b.cAry[1]= 8650948
Length of array of object c: 5
Array of object c:
c.cAry[0]= 8655112
c.cAry[1]= 8654728
c.cAry[2]= 3
c.cAry[3]= 1
c.cAry[4]= 4
Process returned 0 (0x0) execution time : 36.516 s
Press any key to continue.
| |