[MUSIC] Up until now, we've talked about classes having constructors, or ways to initialize the value of the class when it's being created. Now we want to pivot slightly, and talk about a copy assignment operator. Which defines a behavior of using the assignment operator or the equals sign in code. To assign one cube to another cube, or one object to another object, let's see how this works. One thing to remember as we are talking about the assignment operator is that it's very similar to the copy constructor. But it differs in that a copy constructor creates a new object. It's a constructor while an assignment operator replaces the value of an existing object. So every object in C++ has to be constructed, it has to be built by some constructor. Once it's been constructed, it can never be constructed again. To change the value of an existing class, a class that's already been constructed, it must be changed through an assignment operation. If the assignment operator is not provided, C++ provides a default automatic assignment operator for us. The assignment operator wil try and copy the contents of the values, simple as that. In many cases, the automatic assignment operator will do everything we need it to do. Only when we have externally allocated resources, such as memory. Or we want multiple objects to point to the same thing, do we need to define a custom assignment operator, let's see how one works. A custom assignment operator is going to require four different properties to be true. The custom assignment operator must be a public member function of the class, so it needs to be in the public section. The custom assignment operator must have the function name, operator=. So it's a very specific name and it has to be exactly operator followed by an equals sign. It has to have a return value of the reference of the class type. So the return value must be a cube by reference if our class said cube. If our class says sphere, it must be a sphere by reference. And it must have exactly one argument and the one argument must be a const reference to the class' type. Here we see a constant cube by reference object. And the goal of a custom assignment operator is to assign the contents in object to the instance of the class that's being called upon. Looking at our cube class, we now have several different functions. We have our custom default constructor, which we're going to print out, Default constructor invoked! We have our custom copy constructor, where we're going to print out, Copy constructor invoked! And now we have our custom assignment operator, here we see that it returns a cube by reference, its function name is operator=. And the one parameter it has is a constant cube by reference. On line 22, you see that we simply copied the value, this is exactly the same as line 17. And then we're going to go ahead and print out, Assignment operator invoked! So now we know that the assignment operator is invoked, as opposed to the copy constructor. So this is the exact same thing we did in the previous video. And we're going to go through a few examples and see what's invoked where. And any time you have a custom assignment operator, you'll always return a dereference value of this. This is the instance of the class itself, so you're simply returning an instance of the class. Looking at the assignment operator folder, we're going to see the main.cpp is the exact same code we saw in example four of the previous lecture. Here we have a cube, we expect that line of code to invoke a default constructor. On line 13, we see myCube is also going to invoke a default constructor. And finally, on line 15, we see that myCube, which has already been constructed, and c, which has already been constructed are in an assignment operation. So myCube is going to take on the value of c, so we have an assignment operation happening here, let's see if this works. Moving into the cpp assignment operator directory, running make to compile the code, and running ./main. We see two default constructors invoked, and an assignment operator, awesome, this is exactly what we expected to see. So there is a small nuanced difference between a copy constructor and assignment operator. Their functionality is largely the same, that their job is to copy the contents of one instance of a class to another. But the times invoked are slightly different. The invocation of an assignment operator means the object already exists and doesn't need constructed. We're going to explore a lot of these operators as we build data structures later in this class. For right now, we're going to move on and explore more about the construction of a C++ class. I'll see you in the next video.