# Advanced C++ Overloading the Arithmetic Operator

**URL:** <https://forum.codewithmosh.com/t/advanced-c-overloading-the-arithmetic-operator/24677>\
**Category:** C++\
**Created:** [January 20, 2024, 11:33pm UTC](https://forum.codewithmosh.com/t/advanced-c-overloading-the-arithmetic-operator/24677 "2024-01-20T23:33:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpandexMan](https://avatars.discourse-cdn.com/v4/letter/s/858c86/32.png) [@SpandexMan](https://forum.codewithmosh.com/u/SpandexMan)\
**Post date:** [January 20, 2024, 11:33pm UTC](https://forum.codewithmosh.com/t/advanced-c-overloading-the-arithmetic-operator/24677/1 "2024-01-20T23:33:24Z")

</div>

Hey all,

I’m just wondering if anyone managed to solve the topic in chapter 8 on the advanced course. After some work, I managed to get the, ahem, ‘basic’ one to work

The basic example is to be able to add an integer to a custom class (in this video, length) such as;

```auto
Length third = first + 4;

```

Mosh says to create a new overload taking an integer as the parameter. I found this really difficult  
I tried declaration;

```auto
	Length operator+(const int other) const;

```

with a definition of

```auto
Length Length::operator+(const int other) const
{
	return int(value);
}

```

but of course, we can’t do that as 'no suitable constructor exists to convert in to Length. So then I tried

```auto
Length Length::operator+(const int other) const
{
	return Length((int (value)));
}

```

but this doesn’t reference the other value so for

```auto
Length first{ 10 };
Length second{ 20 };
int four = 4;

Length third = first + 4;
cout << third;

```

the answer is 10.  
I finally tried

```auto
Length Length::operator+(const int other) const
{
	return Length((int (value))+ other);
}

```

and this works!! 😁

But I really can’t get past the excercise and I dn’t really understand the question. I think we have to add Point(x,y) to a second Point(x,y) as well as the integers? (I think?)

Anyway, I tried, but could not get my head around this, with using 2 parameters. All of my trials were a disaster, so there’s no point in me even posting them. I was hoping someone who completed this exercise would be able to share the solution?

![Screen Shot 2024-01-21 at 7.32.32 am](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/2/29fd37941854c5e1855333e8f9d60b18e4738bb7.png)
