Learn to Code via Tutorials on Repl.it!

← Back to all posts
Overloading Operators in C++!
h
DynamicSquid

Hey guys! It’s the end of the week. Which means, another tutorial! Last couple weeks, we explored the Monte Carlos method, but this time, let’s change it up a little bit. Instead, we’ll be looking at some C++ operators, and more specifically, overloading them.

Prequisites:

  • OOP
  • 'this' pointer
  • operators

Introduction

What are overloading operators? Well take this example:

We get an error! Why? Well adding 5 to an object? What does that mean? But how do we add a number to value? Well the attribute is private, so our only choose are methods.

Well, that's good an all, but this is C++! There has to be a better way. Overloading operators.

Don't worry about the syntax for now. Just know that overloading operators change the behavior of normal operators. Now, the compiler doesn't see it as adding 5 to the object, but rather adding 5 to value.

Ready yourself to trash all your getters and setters.


The Basics

Let's start off by taking a look at the previous example.

Notice how the method and overloaded operator are very similar. An overloaded operator starts off with it's return type (it's usually void, itself, or bool), then followed by the keyword operator, then the actual operator itself, and a parameter if required. Then inside the overloaded operator function, just do what you would normally do.

Here's another example.

This time we overloaded the prefix increment. This means we can do stuff like this:

Notice how the return type is the object. That's because we use the increment like this:

So in the first example, we incremented the object. The object returned itself, but it wasn't being used by anything, so the return type isn't needed there.

But, the second example saw us printing the object, which means we needed a return type there. That's why the increment operator needs a return type.

Let's look at another example.

As you can see, this operator overload returns type bool, because it's making a comparison. Also, I added the const there because I'm trying to get into the habit of using const cause... idk, better programming practice?

Now hopefully you guys get the jist of things. Now were going to take a look at what we're going to take a look at :)

Unary Operator - operates on one value

++ (pre- and postfix)
-- (pre- and postfix)
conversion types

Binary Operators - operates on two values

==, !=, <, >, <=, >=
+, -, /, *, %
+=, -=, /=, *=, %=
[] (subscript operator)
() (function operator)


Unary Operators

Increments and Decrements

Notice how the return type is the object. This is because we use increments like this: cout << ++value;. If it didn't return anything, then cout would be printing a void value.

Conversion Types

Notice how the coversion operator just turns the object into an int. It's kinda special since it doesn't have a return type.

Oh, and just a side note, the conversion operator is very powerful. In some cases, that's the only overloaded operator you'll need! Because remeber, it takes an object, and turns it into an 'int'. Very useful.

Binary Operators

Addition Operator

Returns an object for the same reason that the increments return an object.

Assignment Operators

Notice how the return type is void. This is because we don't need to return a value when working with assignment operators.

Comparison Operators

The return type is expected.

Subscript Operator

The return type is expected.

Function Operator

Again, doesn't return a type because it doesn't need too.


Real Example

Okay, now you know what overloading operators are, let's take a look at a real example.

Oh, and what's up with all the squids? Well I'm making my own data type actually called "squid" (coming out soon), so yeah...


And that's it! Most of the operators you'll ever need. If there's one that I missed, please let me know so I can add it in.

And don't forget to upvote :)

Voters
nirmala01
hexOmicron
xxpertHacker
Wumi4
DynamicSquid
Comments
hotnewtop
Wumi4

Nice! :)

DynamicSquid

@DangHoang2 thanks!

[deleted]

watcha gonna do now for 5000th cycle mark eh?

DynamicSquid

@Aphmeta nothing.

firefish
DynamicSquid

@firefish with what?

firefish

@DynamicSquid ive been stressing for the past 20 minutes because I have a 5MB file (the linux binary for a fixed version of dusk i need to roll out) that won't appear in the downloaded zip, and my linux dualboot has been borked, so what do I do? (yes, I know, I'll reported this to @amasad but really?)

DynamicSquid

@firefish gitignore

firefish

@DynamicSquid I have le nil of zem

DynamicSquid
firefish

@DynamicSquid i guess dumb speak doesn't work I have no .gitignore files

HahaYes

Hello, DynamicSquid (I took down my post)
Anyway we can collab for a C++ course?

DynamicSquid

@HahaYes sorry, I'm really busy. I barely have enough time to upload this. maybe another time?

HahaYes

@DynamicSquid Yes, of course!

firefish

@HahaYes HahaNo he will not

DynamicSquid

@firefish Check the latest comment on the Dusk issue

firefish

@DynamicSquid Well, to start with we are going to be middlemen (you did a wonderful dusk cdn, could I borrow THAT instead?)

DynamicSquid

@firefish oh, that was a test I deleted that

firefish

@DynamicSquid well you are better at js than i am

DynamicSquid
DynamicSquid

@firefish The windows thing works now! https://night.dynamicsquid.repl.co/getting-started.html

Now I have to do linux

firefish

@DynamicSquid what the hell use the links on github, they are already stored there

DynamicSquid

@firefish I need the zip

firefish
DynamicSquid

@firefish ziiiiiiiiiiiiiiiiiiiiiiiiiiiiip is samller

firefish

@DynamicSquid oh ye le zip tiny, oh wait i can create le deb and le rpm (basically upload it to aptitude and yum)

DynamicSquid

@firefish Wow, the Package Manger section of my website is really looking professional

firefish

@DynamicSquid suggest adding dem newlines in the using dusk part

xxpertHacker

If there's one that I missed, please let me know so I can add it in.

All logical and bit-wise operators.

I'm trying to get into the habit of using const.

Nice! I make sure to use immutable data wherever possible.

using namespace std;

And... I just lost all respect for you!

Also, this reminds me of D, which made operator overloading 100x easier, wanna see an example? Oh yeah, I forgot how to code in D... lol :(
D > C++ > C

I honestly think if the tutorial is too long and not interactive enough, people won't care. I'ma make an interactive C++ tutorial, how about that!?

DynamicSquid

@StudentFires Oh thanks for letting me know, I'll try to add those in soon. Also, I used the standard namespace for readability. I don't use it when I'm actually coding.

Also and interactive tutorial? Yeah that'd be great!

xxpertHacker
firefish

@xxpertHacker

I'ma

Close: just missed a hyphen. I'm-a

xxpertHacker