# Need help w/simple program

• 02-10-2003
LouB
Need help w/simple program
Could you please tell me what I am doing wrong? I am trying to overload the + operator, and output the sum of the 5 numbers (a, b, c, d, e).

Code:

``` #include <iostream.h> #include <math.h> class BigInteger{ private:         public:         double a, b, c, d, e;         void initial();         //sum(BigInteger &one, BigInteger &two);         friend BigInteger operator+(BigInteger); }; void BigInteger::initial() {         cout << "Please enter 5 integers: " ;         cin >> a >> b >> c >> d >> e; } BigInteger operator+(BigInteger &num) {         //BigInteger num;         num = ;        // what do I put here???         return num; } int main() {         BigInteger one, two;         one.initial();                 cout << one.a << one.b << one.c << one.d << one.e <<endl;                 return 0; }```
• 02-10-2003
ripper079
Code:

```BigInteger operator+(BigInteger &num) {         //BigInteger num;         num = ;        // what do I put here???         return num; }```
Well itīs up to you to deside! What du you want it do when you add???

I would guess that you want to add all data-members in the class and return the result. One solution may be

Code:

```BigInteger operator+(BigInteger &num) {                 BigInteger result_of_addition;                 result_of_addition.a = a + num.a;                 result_of_addition.b = b + num.b;                 result_of_addition.c = c + num.c;                 result_of_addition.d = d + num.d;                 result_of_addition.e = e + num.e;         return result_of_addition; }```
Your question isnīt easy to answer, but if you describe it more in detail it certainly would be easier for me to help you.
• 02-10-2003
LouB
Thanks Ripper....I want to enter 5 integers (a thru e), then add them together and print the result.
• 02-10-2003
Panopticon
Code:

```//... BigInteger BigInteger :: operator++() { //changed from friend function to member function return a+b+c+d+e; } //... int main() { BigInteger ob1; ob1.initial(); cout<<++ob1; return 0; }```
The above may be wrong cos im incompetent :p
Well I tried.
• 02-11-2003
ripper079
Code:

```... BigInteger BigInteger :: operator++() { //changed from friend function to member function return a+b+c+d+e; } ...```
hmmm well it depends again how you want it to behave. For instance if I would overload the ++ operator I would try it to mimic the built in types. My version would go something like this

Code:

```const BigInteger & BigInteger :: operator++() { a++; b++; c++; d++; e++; return *this; }```
This would increment the data-members by one and return a const refernce (more efficient then returing a copy of it). Now it is possible to e.i
Code:

```... BigInteger one, two; one.initial(); two = ++one; ...```
Also I would encourage you to use the constructor insteed of your initial member-function. The constructor is there so you can initialize the data-members.