Hello again! In this video, we are going to look at operators and overloading. C++ defines internal operators
for built-in types.
We have the plus operator for adding two ints, for example. So we can say c equals a plus b.
This will actually call a function called operator plus, which takes a and b as arguments.
This returns an int, and then the return value is stored in s.
So there is an internal function inside C++,
operator plus, which takes two ints and returns another int.
As another example, C++ has the equality operator for comparing two ints.
If we have a equals equals b. Built-in types are not really objects, but you can think of this
as if it is a call to a member function.
So the equality operator is called on a, with the argument b.
And there are examples in the Standard Library as well. For example, with the string. The plus operator
will add two strings and it is exactly the same except for the type, so it takes two strings and returns
another string.
The equality operator, that really is a member function this time, so it is called on the first string
and it takes the second string as argument.
So really, you can see that there are two types of operators; ones which take two arguments and are
called as non-member functions, and ones which take a single argument and are called as member functions.
And there are actually special names for this, or special terms. An operator which takes a single argument
is unary and an operator which takes two arguments is binary. And there is actually one example of
an operator which can be both, or it has two different versions. One for each.
There is an operator minus.
So that does the obvious thing of subtracting two numbers, which is a binary operation.
It takes two arguments, a and b.
But there is also a unary operator, which just changes the sign of the argument.
So that will take b, and it will return b, but with the opposite sign.
In fact, C++ has a ternary operator, which takes three arguments.
This is a shorthand way of watching an if statement. So "a query b colon c" is equivalent to "if a b, else c" and
that is implemented as an operator, which has three arguments.
So, ignoring the ternary operator for the moment, opeartors take either one or two arguments.
Operators can be member functions or non-member functions.
Normally, if you have a unary operator, it is implemented as a mamber function on an object. And binary operators
are usually non-member functions, so they take both objects as arguments.
The classes which are defined in the Standard Library, usually have operators which will act on objects
of that class.
So as we saw with the string, with the plus operator and the equality operator, these have the same
syntax as operators for the built-in types, and they have similar behavior.
So the plus sign will add two strings.
It is not going to reverse the string or remove alternate elements. That would be very confusing!
And C++ allows us to define our own operators. If we write a class in C++, it does not have any operators,
except for the assignment operator, if the compiler synthesizes that. But we can define other operators
if we want to, which will act on objects of our class.
And this is known as "overloading" the operators. "Overloading" here
means giving a different meaning to an existing symbol.
So if we have overloaded functions, then we have different meanings for different arguments. And we
have overloaded operators.
We have different meanings for different types that they operate upon.
We can only use the symbols which are supplied for built-in operators, and we have to use the same
syntax. So we are not allowed to invent our own operators or our own syntax.
Thank goodness!
Life is confusing enough in C++ as it is, without this!
And when we write our operators, they should have similar semantics to the built-in ones.
So that is just to avoid confusing people who are going to use your class and these operators.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!