But what happens if I want to create another box and then combine the dimensions from both boxes? The way the Box class is defined would require me to do something like this:

box1.height+box2.height;box1.width+box2.width;box1.depth+box2.depth;

This is quite annoying as I'd have to type out those three lines every time I want to combine box dimensions. if I end up having to do this multiple times, it could get old fast. It's also more code to read and good programmers care about having fewer lines of code to read and the DRY (Don't Repeat Yourself) principle.

So how can we do this better? Wrapping this stuff in a method is one way:

While this will create another box with the dimensions of the first two combined, there's an even better way we can do this:

// Add dimensions from another boxBoxadd(constBox&box2){Boxbox3;box3.height=self->height+box2.height;box3.width=self->width+box2.width;box3.depth=self->depth+box2.depth;returnbox3;}

to this:

// Add dimensions from another boxBoxoperator+(constBox&box2){Boxbox3;box3.height=self->height+box2.height;box3.width=self->width+box2.width;box3.depth=self->depth+box2.depth;returnbox3;}

Now we can add two boxes together like so:

box3=box1+box2;

This does exactly the same thing as before but using the + operator. This is far more semantic and less clutter than calling a function. It more clearly relates to how we tend to think about the action of adding two boxes. Rather than creating an add function, we overload the + operator instead, and just think of the action as "adding" two boxes the same as we'd normally add two numbers.