Is this overloaded class working correctly?

Im trying to understand operator overloading so ive written a box class that takes the nescassary dimensions and multiplies them to get the volume then added two objects together to get the third objects values. The only thing i dont understand is the volume of the third box which doesnt equal the volume of the 1st two boxes added together.
Is it working right and if not how should i code the getvolume function to make it do so. Please explain. Thanks.

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
#include<iostream>

using namespace std;

class Box
{
	public:
	int SetitsHeight(int height) {itsHeight = height;}
	int SetitsLength(int length){itsLength= length;}
	int SetitsWidth(int width){itsWidth = width;}
	int GetitsHeight(){return itsHeight;}
	int GetitsLength(){return itsLength;}
int GetitsVolume(){return (itsWidth*itsLength)*itsHeight;}

	
	Box operator+(Box & spare)
	{
		Box box;
		box.itsWidth = this->itsWidth + spare.itsWidth;
		box.itsLength = this->itsLength + spare.itsLength;
		box.itsHeight = this->itsHeight + spare.itsHeight;
		return box;
	}
	
	private:
	int itsWidth;
	int itsLength;
	int itsHeight;
	
};

int main()
{
	Box Box1;
	Box1.SetitsHeight(5);
	Box1.SetitsWidth(5);
	Box1.SetitsLength(5);
	
	cout<<"Box1s volume is " << Box1.GetitsVolume()<< endl;
	
	Box Box2;
	Box2.SetitsHeight(7);
	Box2.SetitsWidth(7);
	Box2.SetitsLength(7);
	
	cout << "Box2s volume is " << Box2.GetitsVolume()<< endl;
	
	Box Box3;
	Box3 = Box1 + Box2;
	
	cout << "Box3s volume is " << Box3.GetitsVolume()<< endl;
Last edited on
Pretty good, but a few issues.

1.
1
2
3
	int SetitsHeight(int height) {itsHeight = height;}
	int SetitsLength(int length){itsLength= length;}
	int SetitsWidth(int width){itsWidth = width;}

None of these functions return a value. Make them void instead of int.

2.
53 = 125. Good.
73 = 343. Good.
You're last number is 1728. This is 123. That is the correct behavior as you have your program now.

Of course it doesn't equal 125 + 343 = 468, because you're adding lengths, not volumes.
(2 + 3)3 does not equal 23 + 33.
The end result of your addition is doing (5+7)*(5+7)*(5+7) for height * width * length. Not to mention, a 468-volume cube can't have integer side lengths, because 468 isn't a cube number.

The problem with operation overloading is it can be abused. What does it mean to add two Boxes together? This is a question you need to ask yourself, and provide clear test cases for.

If you want the volumes to be preserved (3 volume + 4 volume = 7 volume), then it's ambiguous as to how a 3*4*5 box should be added to a 2 * 3 * 4 box. The combined volume mathematically should be 60 + 24 = 84, but what should the new length, width, and heights be? It's ambiguous; there could be multiple answers.

You need to think about what adding boxes should actually mean. If there isn't a way to intuitively use addition, don't operator overload it.
Last edited on
Topic archived. No new replies allowed.