In this episode, as well as the upcoming ones, we will focus on operator overloading. This is a rather technical subject which will make us able to define our very own operators. Let us begin with an example so that we may see what purpose it serves. For example, let us say we have defined a "Complexe" (= "Complex") class in order to add complex numbers. It does not matter what it is exactly. What matters, is that we wish to add complex numbers. Let us suppose we have already defined a few complex numbers : z1, z2 and so on. How should we proceed in order to add these complex numbers? For example, we may wish to store inside z3, the result of " z1 + z2 ". Of course, we would very much like to write something like : " z3 = z1 + z2 " If we write this in our usual classes we will get a compiling error since the " + " is indeed not defined for z1, z2, it is not defined for complex numbers. Until now, we would rather have defined a function in charge of the addition. For example, we could define a function called "add" which would work like this : add(z1, z2) and " z3 = add(z1, z2) " in order to store the result of the addition of z1 and z2 into z3. And if we wanted to store, into z4, the result of the addition of z1, z2 and z3, we would have to write it like this. As the first argument, here, we add up z1 and z2 and then, as the second argument, we will call the function "add" again and add z3 here. Unfortunatley, this way to write it is rather unnatural. It would be much more pleasant to write " z4 = z1 + z2 + z3 "; it is much clearer than something like this with an "add" function. Indeed, the goal of operator overloading is to let us write something like this, using the " + " operator with a class belonging to us, that we have defined, called the "Complexe" class. Similarly, we could wish to print complex numbers in an homogenous way like this. For example, printing the message " z3 = " followed by the value of the complex number z3 so that we may print z3. Why, writing this is only possible if we do not overload the printing operator. We daresay it is better to write this rather than having to cut out here a printing of the message "z3 = " and then calling a printing function which we have defined for the complex class. Finally printing the line break like this. This here is the purpose of operator overloading. This will let us expand the use of the usual operators (the ones we deem most useful) to the classes where we wish to add these different operators (addition, printing, multiplication) according to our needs. That said, operator overloading is still quite a technical subject; it can be rather complicated since we are in the core of the language. We will redefine elementary operations : the operators. Therefore, according to your level or to the restrains you find yourselves in, you have several ways to tackle the operator overloading. You can decide not to use them at all and continue using functions as you were doing up until now. Also, you may decide to introduce only the operators relatively simple : additions, multiplications without worrying about the so-called auto-assignment (that is, += , -=, x= ). At this level, you may also decide to overload the printing operator if you find it useful. At a higher level, you may start to tackle the overloading of auto-assignment operators : += , -= and so on but in a slightly simplified version with the return type "void". We will go back to this in detail in the different following episodes. Here, we are giving the plan of the videos we will be proposing. Finally, at the highest level, you may also redefine these operators according to the standard, at a much higher level. The level targeted by this course is the level 2. We will thus do a relatively simple operator overloading. However, in the upcoming videos, we will still present the level 3 and the level 4 so that you know they exist. If you interested, you may freely expand on the subject. Now, let us dive into the heart of the matter : this famous operator overloading. First of all, what is an operator? Operators are signs used to represent the operations such as arithmetic operators. For example, when you write " z = a + b ", you are using the addition operator. You have logical operators. For example, if we write " (a == b) and (x == y) " here, "and" is an operator. You also have comparison operators. For example, if you write : " x <= y " you are still using an operator. You also have the auto-increment operators. For example, when we write " ++i " in "for loops". You also have the assignment operator of which we have not yet spoken, but which is written here. If you have : " z = something ", it is indeed an operator. This operator is overloadable aswell. We will talk about it in the very last video. What you need to know is that when you write an expression containing an operator (the operator "==" here, the operator "and"...) it corresponds to the call to a function or to a call to a method (class member function). Every time you write "a Op b ", either you have the function "operatorOp (a, b) ". For example, when we write " a + b ", it means we have either a call to the function " Operator+ (a, b) or (we will see the distrinctions later) the class method of which a is an instance : a.operator+ This means the method "operator+ " where b is the argument. Similarly, if we have unary operators (operators with only one operand), for example, when we write " -a " to take the opposite of a, or when we write " ++a " to increment a, it is either a call to a function or a call to a method. In the case of "-a", it is either the function "operator-" with a as argument, or the method of a class instance written " a.operator- " here without argument. Similarly, " ++a" will either call a function according to what has been defined ( "operator++ (a)) or the method "operator++ " of the class where a is an instance (a.operator++()). To give more examples, when you write "cout << a ", you are either using the function "operator<< " taking cout and a as argument or (since cout is an instance of class called "iostream") the method "operator<< " of the class where "cout" is an instance, that is, the "ostream" class with a as parameter. Finally, you need to know that when you call " a = b ", you indeed in this case here. However, there is no function " = " with two arguments. Therefore, we are perforce using the method " operator= " of the class where a is an instance called with b as parameter. Those were a handful of examples. The set of the operators you can overload is given as a pdf complement on the course website. This is it for the operators. Now what about the overloading? The overloading is when a function or a method has the same name as another function/method, both being distinguished by their arguments For example, we can imagine a function "max" taking two integers as parameters and another function "max" taking two doubles as parameters. At this point, we say that the two "max" functions are overloaded. There are several functions with the same name "max" but distinguished by the types of their parameters. Regarding operators, we will do the same thing : we will overload operators. For example, we will overload the operator " + " for the "Complexe" class. Here, it takes two complex numbers and the addition of two complex numbers returns here a complex number. For example, if we are working with matrices we wish to add, we may overload the operator " + " and take two matrices here. This is the operator overloading. It is the possibility to redefine operators having the same name : "operator something" for different classes. In C++, pretty much all operators are overloadable. Once again, the complete list is given on the course website. Therefore, you can freely overload the usual operators for your classes. As we said, there are two ways to interpret an operator : either as a function or as a method. Indeed, there are two types of overloading. The so-called external overloading or internal overloading. The external overloading is the one using usual non-member functions; for example, "operator+ " taking two complex numbers in order to do, for example "z3 = z1 + z2" which will be called "operator+ " : " z3 = operator+(z1, z2) ". This overloading is external to the class; we call it an external overloading by opposition to the internal overloading which is done inside the class and is the alternative. This time, we would have " z3 = z1.operator+ (z2) " where "operator+ " is a method of the class of z1. Therefore, in an internal overloading, the operator corresponds to a method (a class member function). On the other hand, in the external overloading, the operator corresponds to a usual non-member function. We will begin by presenting the external overloading. Then, in the next episode, we will present the internal overloading and decide which one is more appropriate and choose one of these overloading options.