Overloading Operator+ won't take both sides

So I'm trying to add two objects of a class i made called NaturalNumber, however when i try to add two NaturalNumber's together, it will replace one of the NatrualNumber's with zero and then add the second one. Any help will be appreciated! Also I know i shouldn't be using the std namespace but just humor me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int main()
{
	
	cout << "Please enter the desired number between 0 and 19000" << endl;
	int input, input2, input3;
	cin >> input;
	NaturalNumber example(input);
	example.displayNumber();
	cout << endl << "Now input a second number to add to the first, \nremembering the upper limit is 19000" << endl;
	cin >> input2;
	NaturalNumber example2(input2);
	example = example + example2;
	example.displayNumber();
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

class NaturalNumber
{
protected:
	int wholeNumber;
public:
	NaturalNumber();
	NaturalNumber(int num);
	void displayNumber();
	friend NaturalNumber NaturalNumber::operator+(const NaturalNumber& lhs, const NaturalNumber& rhs);
	
};


1
2
3
4
NaturalNumber operator+(const NaturalNumber& lhs, const NaturalNumber& rhs)
{
	NaturalNumber sum;
	sum.wholeNumber = lhs.wholeNumber + rhs.wholeNumber;



Like i said when i run the second instance of example.displayNumber() instead of getting the sum of input and input2, i just get input2. I omitted the rest of my class because there are other operators that I am using as well and they aren't important
My first suggestion would be to remove the scope operator NaturalNumber:: from line 13 of your code since you're declaring the function as a friend rather than a member of the class.
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include "Classes.h"
using namespace std;

int main()
{
	
	cout << "Please enter the desired number between 0 and 19000" << endl;
	int input, input2, input3;
	cin >> input;
	NaturalNumber example(input);
	example.displayNumber();
	cout << endl << "Now input a second number to add to the first, \nremembering the upper limit is 19000" << endl;
	cin >> input2;
	NaturalNumber example2(input2);
	example = example + example2;
	example.displayNumber();
	system("pause");
	return 0;
}

Classes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

class NaturalNumber
{
protected:
	int wholeNumber;
	const static string lessThan20[];
	const static string tens[];
public:
	NaturalNumber();
	NaturalNumber(int num);
	void displayNumber();
	friend NaturalNumber operator+(const NaturalNumber& lhs, const NaturalNumber& rhs);

};

NaturalNumber.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
#include <iostream>
#include "Classes.h"
#include <string>
using namespace std;

NaturalNumber::NaturalNumber()
{
	wholeNumber = 0;
}

NaturalNumber::NaturalNumber(int num)
{
	wholeNumber = num;
}

const string NaturalNumber::lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string NaturalNumber::tens[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };

void NaturalNumber::displayNumber()
{

	int a, b, c, d, e;
	a = wholeNumber % 10;
	wholeNumber = wholeNumber / 10;
	b = wholeNumber % 10;
	wholeNumber = wholeNumber / 10;
	c = wholeNumber % 10;
	wholeNumber = wholeNumber / 10;
	d = wholeNumber % 10;
	wholeNumber = wholeNumber / 10;
	e = wholeNumber % 10;
	cout << "The number is ";
	if (e == 1)
		cout << lessThan20[d + 10]  << " " << "thousand ";
	else if (d != 0)
		cout << lessThan20[d] << " " << "thousand ";
	if (c != 0)
		cout << lessThan20[c] << " " << "hundred ";
	if (b == 1)
		cout << lessThan20[a + 10] << " ";
	if (b > 1)
	{
		cout << tens[b - 2] << " ";
		if (a != 0)
			cout << lessThan20[a]<< " ";
	}
	cout << "." << endl;
}
NaturalNumber operator+(const NaturalNumber& lhs, const NaturalNumber& rhs)
{
	NaturalNumber sum;
	sum.wholeNumber = lhs.wholeNumber + rhs.wholeNumber;
	return sum;
}


Okay since I tried inputing 35 and 20 and it still said 20, here's the entire code, and hopefully that helps you out.
I appreciate the help, by the way, and I'm using VS 2013 Incase that matters

UPDATE:: After executing the ".displayNumber()" function, my variable "wholeNumber" is always at zero because I'm taking it apart piece by piece. In order to fix this I just made a temp variable to take the place of wholeNumber that way it stayed intact.
Thanks for everyone's help!
Last edited on
Topic archived. No new replies allowed.