OOP is killing me

Pages: 12
Okay, so I have to create a lab that adds and multiplies giant integers that are 40 digits long. I have to do this by essentially creating an array of 40 digits and writing code that imitates multiplying and adding.

I had everything working, with classes and everything.

Except, apparently, I'm supposed to be passing OBJECTS through my functions as parameters, not arrays. In addition, I'm supposed to implement operator overloading. I tried doing this and I can't even come remotely close to compiling.

I just don't understand the purpose of objects and how to use them.

Here's my current (and absolutely horrible) .cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
using namespace std;

#include "HugeInteger.h"

HugeInteger::HugeInteger()
{
        op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        op2[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        result[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

        //for (int i = 0; i <= MAX2; i++)
        //{
        //      op1[i] = 0;
        //}
}

//HugeInteger HugeInteger::operator+(const HugeInteger& c) const
//{
//      HugeInteger Result;
//      Result.op1 = Result.add(this->op1, c.op2, c.result);
//      return Result;
//}

HugeInteger HugeInteger::add(const int HugeInteger &)
{
        HugeInteger object;

        int carry;

        for (int i = MAX2; i >= 0; i--)
        {
                object.result[i] = object.op1[i] + object.op2[i] + carry;

                carry = 0;

                while (result[i] >= 10)
              {
                        object.op1[i] -= 10;
                        carry++;
                }

        }
}

HugeInteger HugeInteger:: mult(const int HugeInteger &)
{
        int mult_array[MAX] = {0};
        int addzero_ctr;
        int lastDigit;
        int firstDigit;
        const int num = 10;
        int product_holder;
        for (int m = (MAX2); m >= 0; m--) // This 'for' loop essentially sums together the results of the below 'for' loop to carry out the multiplication
{
                for (int i = (MAX2); i >= 0; i--) // This 'for' loop multiplies all of op1 by the last digit of op2 and puts the value into result[i]
                {
                        product_holder = op1[i] * op2[m];
                        object.op1[i] = product_holder;
                        lastDigit = product_holder % num;

                        object.result[i] = firstDigit;

                        product_holder -= lastDigit;
                        firstDigit = product_holder / num;

                        object.result[i] += lastDigit;
                }

                if (addzero_ctr != 0)                                   // solution to below
                {
                        for (int i = 0; i <= (MAX2 - addzero_ctr); i++)//This is a lot of overhead when addzero_ctr = 0
                        {
                                object.op1[i] = object.result[i+addzero_ctr];
                        }
                }


                for (int i = addzero_ctr; i > 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes
                {
                        object.result[MAX-i] = 0;
                }

//              cout << "------------------\n";                 //      Use this comment section to check which two numbers the system adds
//              cout << "These are the two numbers being added: \n";
//              for (int i = 0; i <= MAX2; i++)
//              {
//                      cout << mult_array[i];
//              }
//              cout << " + ";
//              for (int i = 0; i <= MAX2; i++)
//              {
//                      cout << result[i];
//              }
//              cout << endl;

                add(object);

                addzero_ctr++;
                for (int i = 0; i <= MAX2; i++)
                {
                        mult_array[i] = object.op1[i];
//              cout << "mult[i] = " << mult_array[i] << endl;  //   Use this comment section to check what result[i] is immediately before m is decremented
                }

        }
}

void HugeInteger::display() // Displays result of array as a single integer
{
        bool eraseZeroes = true;
        cout << "Answer is: ";
 for (int i = 0; i <= MAX2; i++)
        {
                if (result[i] != 0 || eraseZeroes == false)
                {
                        cout << result[i];
                        eraseZeroes = false;    // The boolean variable eraseZeroes is used to erase superflous zeroes (like 0009 instead of 9)
                }
        }
        if (eraseZeroes) // If the user multiplies by zero (or adds zero and zero), we need to cout a zero.
        {
                cout << 0;
        }

        cout << endl;
}

//void HugeInteger::create_array(int num1, int num2, int op1[], int op2[])      // Converts int into an array of 40 digits
//{
//      for (int i = MAX2; i >= 0; i--)
//       {
//                lastDigit = (num1 % num);
//                op1[i] = lastDigit;
//                num1 -= lastDigit;
//                num1 = (num1 / num);
//        }

//        lastDigit = 0;

//        for (int i = MAX2; i >= 0; i--)
//        {
//                lastDigit = (num2 % num);
//                op2[i] = lastDigit;
//                num2 -= lastDigit;
//                num2 = (num2 / num);
//        }
//}



And here's my .h


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
//prevent multiple inclusions of header file
#ifndef HugeInteger_h
#define HugeInteger_h

#include <string>

//Course class definition
class HugeInteger
{
        public:
                HugeInteger();
        //      HugeInteger(int[], int);

        //      bool operator==(HugeInteger);
        //      HugeInteger operator+(const HugeInteger&) const;
        //      friend HugeInteger operator*(const HugeInteger*);

                void add(const HugeInteger&);
                void mult(const HugeInteger&);


                void display();




         private:

                static const int MAX = 40;
                static const int MAX2 = 39; // Typing (MAX - 1) is tiresome

                int op1[MAX];
                int op2[MAX];
                int result[MAX];

#endif 


Obviously, I don't expect any of you to correct even a tenth of the slew of problems here. But can you at least show me the correct way to use an object? I mean, I had the ENTIRE THING working without using objects (but with a .h and a .cpp - that is, with classes), so I can't imagine that objects make it that much more complicated, you know?

This is my first time posting here, so I apologize in advance if I have made any errors in posintg.

Thank you all for your time and help.
Just a very quick glance at your HugeInteger class shows it contains no less than three arrays of integers.

That seems like probably the wrong starting point. I would expect there to be just a single array, which represents the value of the huge integer. (There might also be a separate sign, if you need to represent positive and negative values).

So I would start by trimming down the class, and then reconsidering the rest of the code.
To add on to that, you want to think of each array as just one part of an expression (like x = y + z). x, y, and z would all be separate HugeInteger objects, each with only one array (not 3).

In your constructor, you cannot initialize the array like that. Just use the for loop you have commented out, but remember that there will be only ONE array.

For operator +, you already have the declaration for it. You just need to basically take your code in add(...), put it in operator+, and make it work for your object with one array. You should be declaring a new HugeInteger to store the result, as you are doing. Let's assume the paramater is called "right" (because, in y + z, y is the "invoking object" and z is the parameter passed to y's operator+ function). Finally lets assume that the array in each HugeInteger is called "arr". Taking as an example line 33, it would now look like:
 
result.arr[i] = arr[i] + right.arr[i] + carry;

For operator *, assuming it all works correctly as you say, you can just take the mult function and modify it as I described for operator+.
Last edited on
Yes, my professor recommended that as well. And I changed it to that, but then I got confused...

You see, if I'm adding two numbers together, and getting a result, that's three different numbers, each represented by an array, and I don't see how it's possible to do that while working with just a single array.

I can guess that I will do this be creating three objects?

Well, let's say I alter my .h code into this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef HugeInteger_h
#define HugeInteger_h
#include <string>

class HugeInteger
{
        public:
                HugeInteger();
                HugeInteger(int[], int);
        
                void add(const HugeInteger&);
                void mult(const HugeInteger&);

                void display();

         private:

                static const int MAX = 40;
                static const int MAX2 = 39; // Typing (MAX - 1) is tiresome

                int op1[MAX];
}
#endif 



But how, then, does my .cpp account for this? How does it use the HugeInteger object? To keep things simple, let's ignore the complicated multiplication function and just focus solely on the simple additon function and the constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "HugeInteger.h"

HugeInteger::HugeInteger()
{
        op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
}


HugeInteger HugeInteger::add(const int HugeInteger &)
{
        HugeInteger object;
        int carry;

        for (int i = MAX2; i >= 0; i--)
        {
                object.result[i] = object.op1[i] + object.op2[i] + carry; ]
                carry = 0;
                while (result[i] >= 10)
               {
                        object.op1[i] -= 10;
                        carry++;
                }
        }
}


You see that bolded line "object.result[i] = object.op1[i] + object.op2[i] + carry;"??

What is that supposed to look like when there is only one array being used?
Read my reply above your new post.
Thank you for all the replies!

I read your post carefully, and I assume you meant for me to banish the add(...) function altogether and just copy paste into the operator+ function (which seems to make sense to me).

Howeve, when I did that, along with the other changes you recommended, the compiler still gave errors. The compiler said some of the problems were in the .h file, which I did modify slightly, so I'll repost it:

Here's my slightly modified .h file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef HugeInteger_h
#define HugeInteger_h

#include <string>

//Course class definition
class HugeInteger
{
        public:
                HugeInteger();
                HugeInteger(int[], int);
                HugeInteger operator+(const HugeInteger&) const;

                void add(const HugeInteger&);
                void mult(const HugeInteger&);
                void display();

         private:
                static const int MAX = 40;
                static const int MAX2 = 39; // Typing (MAX - 1) is tiresome

                int arr[MAX];
}
#endif 


And here's my constructor and add function:

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
HugeInteger::HugeInteger()
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = 0;
}

//HugeInteger::HugeInteger(int[] arr, int MAX2)
//{
//      for (int i = 0; i <= MAX2; i++)
//              arr[i] = 0;
//}

HugeInteger HugeInteger::operator+(const HugeInteger& right) const
{
        HugeInteger result;
        int carry;

        for (int i = result.MAX2; i >= 0; i--)
        {
                result.arr[i] = arr[i] + right.arr[i] + carry;
                //object.result[i] = object.op1[i] + object.op2[i] + carry;

                carry = 0;

                while (result.arr[i] >= 10)
                {
                        result.arr[i] -= 10;
                        carry++;
                }

        }
}


I tried pretty hard to find out what was wrong here but I couldn't see anything. The compiler is talking about an invalid attempt to attach a return value to the constructor function....but I'm not returning anything.

I'm at a loss here.
Well, you should be returning result at the end of operator+. Otherwise, what's the point of the function? Also it's more helpful if you just copy paste the entire error it gives you.
Ah, of course. I added that in, thank you.

And here's the error:

HugeInteger.h:15:1: error: new types may not be defined in a return type
HugeInteger.h:15:1: note: (perhaps a semicolon is missing after the definition of âHugeIntegerâ)
HugeInteger.cpp:11:26: error: return type specification for constructor invalid
The note it gives you is exactly right; you don't have a semicolon after the definition of your class. Add one after the '}' on line 23. As for the other error, please just copy paste the whole .cpp file. The error could be a result of something before the line it claims the error is on. Don't be afraid of it being too long.
Last edited on
Haha, I feel silly. I thought too deeply into it, I suppose.

And to avoid compilation errors, I actually turned the rest of my .cpp into a comment. I plan to undo it when the I nail down the add function.

Regardless, I was able to compile after putting in that semicolon. Thank you so much!

After fixing some more errors, I was also to make my program compile with the tester.cpp.

Now I think there's (hopefully) only one last loose end to tie up. I don't understand how to use a constructor with parameters in this kind of situation.

Here's my HugeIntMain.cpp file (the one that I actually run the program with):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

const int MAX = 40;

#include "HugeInteger.h"
int main()
{
        int op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,7};
        int op2[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1,8};

        HugeInteger arr;
        char op;
        op = '+';
        HugeInteger object;

        if (op == '+')
                arr = arr + object;
//      else
//              object.mult(op1, op2, result);

        object.display();
        return (0);
}


The idea is for me to add these two arrays together - the ones defined here have real numbers in them. I just don't know how to get them into my functions correctly, you see what I mean?

And here's the entire .cpp file, minus just the commented sections:

You'll see that the bolded section is not correct (that is, I'm pretty sure it isn't).

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
HugeInteger::HugeInteger()
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = 0;
}

HugeInteger::HugeInteger(int[] arr, int MAX2)
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = arr[i];
}

HugeInteger HugeInteger::operator+(const HugeInteger& right) const
{
        HugeInteger result;

        int carry;

        for (int i = result.MAX2; i >= 0; i--)
        {
                result.arr[i] = arr[i] + right.arr[i] + carry;
                //object.result[i] = object.op1[i] + object.op2[i] + carry;

                carry = 0;

                while (result.arr[i] >= 10)
                {
                        result.arr[i] -= 10;
                        carry++;
                }

        }
        return (result);
}

void HugeInteger::display() // Displays result of array as a single integer
{
        HugeInteger result;

        bool eraseZeroes = true;
        cout << "Answer is: ";
        for (int i = 0; i <= result.MAX2; i++)
        {
                if (result.arr[i] != 0 || eraseZeroes == false)
                {
                        cout << result.arr[i];
                        eraseZeroes = false;    // The boolean variable eraseZeroes is used to erase superflous zeroes (like 0009 instead of 9)
              }
        }
        if (eraseZeroes) // If the user multiplies by zero (or adds zero and zero), we need to cout a zero.
        {
                cout << 0;
        }

        cout << endl;
}
Here's what your constructor should be:

1
2
3
4
5
HugeInteger::HugeInteger(int[] op)
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = op[i]; //don't use the same name as your class variables
}


Here's how you would use it:
1
2
int op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,7};
HugeInteger myInt(op1);


You could add a function that lets you set a HugeInteger object equal to a new array, instead of making a new one every time.
I made the changes you recommended, and it wouldn't compile - it seems that I had to type the parameter as "HugeInteger::HugeInteger(int op[])", that is, with the bracket at the end of the variable name. After that it compiled - thank you!

And I considered adding the object-to-array function as you recommended, but I think that ties in with the fact that I will ultimately be receiving the arrays as integers from the user. So I'll cross that bridge when I get to it. For now, I'm just trying to successfully implement the operator overloading.

Based on what you said, I rewrote my code like this:

Main function
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
#include <iostream>
using namespace std;

const int MAX = 40;

#include "HugeInteger.h"
int main()
{
        int op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,7};
        int op2[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1,7};

        char op;
        op = '+';

        HugeInteger object(op1);
        HugeInteger arr(op2);

        if (op == '+')
                arr = arr + object;
//      else
//              object.mult(op1, op2, result);

        object.display();

        return (0);
}


This all compiles, but it claims the sum is zero when I run the program!
I examined the code carefully, but I can't see why it isn't adding the two arrays together. At the very least, it should be displaying a value of 4027 (op1 without being added to op2), but it is doing nothing at all!

I have two constructors (default and one with parameters) and they look like this:
1
2
3
4
5
6
7
8
9
10
11
12
HugeInteger::HugeInteger()
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = 0;
}

HugeInteger::HugeInteger(int op1[])
{
        for (int i = 0; i <= MAX2; i++)
                arr[i] = op1[i];
}


I made no other changes to my program.

Any ideas on what's wrong here?
I get a warning in your + operator that carry is uninitialized.
Reference lines 17 and 21 in your post (3 back). This will cause unpredictable results.

Also at line 25 of your main, object is being displayed. That's not the result. With those changes, I get the correct answer.

object: 4027
arr: 917
The answer is: 4944


I added an overload of the << operator so HugeInteger acts like any standard c++ type:

1
2
3
4
5
6
7
8
9
10
11
12
/* friend */
ostream & operator << (ostream & os, const HugeInteger & hi)
{    bool eraseZeroes = true;

   for (int i = 0; i <= hi.MAX2; i++)
   {   if (hi.arr[i] != 0 || eraseZeroes == false)
        {   os << hi.arr[i];
             eraseZeroes = false;    // The boolean variable eraseZeroes is used to erase superflous zeroes (like 0009 instead of 9)
        }
    }
    return os;
}


Corrected + opertator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
HugeInteger HugeInteger::operator+(const HugeInteger& right) const
{	HugeInteger result;

    int carry = 0;

    for (int i = result.MAX2; i >= 0; i--)
    {	result.arr[i] = arr[i] + right.arr[i] + carry;
        carry = 0;

        while (result.arr[i] >= 10)
        {	result.arr[i] -= 10;
            carry++;
        }

    }
    return (result);
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

const int MAX = 40;

#include "HugeInteger.h"
int main()
{	int op1[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,7};
    int op2[MAX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1,7};

    char op;
    op = '+';
				
    HugeInteger object(op1);
    HugeInteger arr(op2);
	cout << "object: " << object << endl;
	cout << "arr: " << arr << endl;
    if (op == '+')
		arr = arr + object;
	cout << "The answer is: " << arr << endl;
    return 0;
}


edit: to clarify reference to previous post.





Last edited on
Thanks! I did what you said and after playing around a little I got it working.

I also tried using the same logic to alter my multiplication function, and I'm pretty sure I got everything converted correctly, except there's one little hiccup that I'm not sure how to handle.

You see, as a part of the multiplication function I must add two numbers together (when you do the multiplication longhand you see what I mean). Obviously, I need to call the add function for that. I'm not sure how I would go about that, since I can only add two objects together and in the multiplication function itself I'm really dealing only with arrays.

The really confusing part is that my array "mult_array[]" is what I need to add...but I can't add with it because it's not an object. Do you see what I'm saying?

Here's my newly altered multiplication file and the addition function I am trying to use:

(look at the bolded section to see where the problem is - line 66)

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
//result.arr[i] = result || arr[i] = op1 || right.arr[i] = op2
HugeInteger HugeInteger::operator+(const HugeInteger& right) const
{
        HugeInteger result;

        int carry = 0;

        for (int i = result.MAX2; i >= 0; i--)
        {
                result.arr[i] = arr[i] + right.arr[i] + carry;
              
                carry = 0;

                while (result.arr[i] >= 10)
                {
                        result.arr[i] -= 10;
                        carry++;
                }

        }
        return (result);
}

HugeInteger HugeInteger:: operator*(const HugeInteger& rightside)
{
        HugeInteger resultmult;

        int mult_array[MAX] = {0};
        int addzero_ctr;
        int lastDigit;
        int firstDigit;

        const int num = 10;
        int product_holder;
        for (int m = (resultmult.MAX2); m >= 0; m--) // This 'for' loop essentially sums together the results of the below 'for' loop to carry out the multiplication
{
                for (int i = (resultmult.MAX2); i >= 0; i--) // This 'for' loop multiplies all of op1 by the last digit of op2 and puts the value into result[i]
                {
                        product_holder = arr[i] * right.arr[m];
                        arr[i] = product_holder;
                        lastDigit = product_holder % num;

                        resultmult.arr[i] = firstDigit;

                        product_holder -= lastDigit;
                        firstDigit = product_holder / num;

                        resultmult.arr[i] += lastDigit;
                }

                if (addzero_ctr != 0)                                   // solution to below
                {
                        for (int i = 0; i <= (resultmult.MAX2 - addzero_ctr); i++)//This is a lot of overhead when addzero_ctr = 0
                        {
                                arr[i] = resultmult.arr[i+addzero_ctr];
                        }
                }


                for (int i = addzero_ctr; i > 0; i--) // Since we need to carry a zero during multiplication, the contents of the array must shift to the left to make room for the zeroes
                {
                        resultmult.arr[MAX-i] = 0;
                }


                arr = arr + mult_array[i];

                addzero_ctr++;
                for (int i = 0; i <= resultmult.MAX2; i++)
                {
                        mult_array[i] = arr[i];
                }

        }
}

Last edited on
Anyone?

as a part of the multiplication function I must add two numbers together (when you do the multiplication longhand you see what I mean). Obviously, I need to call the add function for that. I'm not sure how I would go about that, since I can only add two objects together and in the multiplication function itself I'm really dealing only with arrays.

So convert the array to an object. :)

Actually, what I would do would be to create a private helper function that adds two arrays. The helper can then be called by both the + and * operators. You want the helper to be private since the internal representation of the array is private.

I also just noticed one thing about both your + and * operators. Both return a HugeInteger by value. When one defines an arithmetic operator, it is normal to both modify the instance and to return the instance by reference.

Your declarations should look like this:
1
2
const HugeInteger & operator + (const HugeInteger&);
const HugeInteger & operator * (const HugeInteger&);

This is more efficient that returning by value. Returning by value requires creating a temporay instance and copying the contents to the temporary instance. Returning by reference simply returns a pointer.

You'll need to change both functions to return a self-reference:
 
  return *this;


You also have a couple of errors in the code in your most recent post.
1) Line 39:
 
product_holder = arr[i] * right.arr[m];

You're referencing right.arr, while the argument passed in at line 24 is rightside.

2) Line 66:
 
arr = arr + mult_array[i];

i is undefined. i is only defined within the scope of the preceding for loop.

Thank you for the observations.

So, if I understand you correctly, you're saying I should move the contents of operator+ into its own add(...) function, and then merely have operator+ call that add(...) function, and also have operator* call that function when necessary?

And would I be correct to deduce that the operator+ function deals with objects, whereas the add(...) function deals purely with arrays?
Yes on both counts.

Ok, what you said made a lot of sense. And then I tried doing it and got extremely confused haha.

I'm not sure how to pass the array, seeing as how my variable arr is private and thus cannot be accessed by objects with the dot operator. I doubt that the solution would be to make it public.

Here's my modified .h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class HugeInteger
{
        public:
                HugeInteger();
                HugeInteger(int[]);

                const HugeInteger & operator + (const HugeInteger&);
                const HugeInteger & operator * (const HugeInteger&);

                friend ostream & operator << (ostream&, const HugeInteger&);
                friend istream & operator >> (istream&, const HugeInteger&);

                int atoi(char);
                
         private:
                void add(const HugeInteger&);
                void mult(const HugeInteger&);

                static const int MAX = 40;
                static const int MAX2 = 39; // Typing (MAX - 1) is tiresome

                int arr[MAX];
};
#endif   


And here's my operator+ along with my add(...) -- this is where I'm having trouble.
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
 
HugeInteger & operator + (HugeInteger & hi, const HugeInteger& right)
{
        add(hi.arr, right.arr);
        return (*this);
}

void HugeInteger::add(int op1[], int op2[])
{
        int carry;

        for (int i = MAX2; i >= 0; i--)
        {
                op1[i] = op1[i] + op2[i] + carry;

                carry = 0;

                while (op1[i] >= 10)
                {
                        op1[i] -= 10;
                        carry++;
                }

        }
} 
A few things:
1) The declaration for your + operator doesn't match your implementation. The declaration is correct. You need to fix the implementation to match.
hint:
 
    add(arr, right.arr);


2) Again for your add function, your declaration and implementation don't match.
This time, it's your implementation that is correct. You might think about whether you want add to use and return the result in op1 or operate on the instance's arr[] (i.e. take only one argument).

3) Line 11 in your header. You can't input to a const instance.
Pages: 12