When an instance of a class is created in C++, the class constructor lets us program exactly the initial state of the object that's created. Let's see how this works. In C++, an automatic default constructor is provided for us if we haven't defined any constructor whatsoever. Every class we've seen so far has a constructor. It has the automatic default constructor. All that this automatic default constructor does is it's going to initialize all of the member variables to their default values. Let's look at some code. Here's the Cube class that we saw last week. This class has three member functions, and a member variable length. There's nothing in this class that's a constructor. Because no constructor is present, we have an automatic default constructor. The automatic default constructor as we mentioned initializes all of the variables to their default values. What that means is length will take on its default value. So, the default value for a primitive type is largely undefined. So, we're not sure what the initial length is going to be. If the private member variable was a class, then the constructor is going to define what its default value is. Let's see how we might define a constructor if we wanted to just set up the default values instead of accepting the system default values. To explain what's going on, we want to explain exactly a terminology we're talking about. Earlier we talked about an automatic default constructor. Now, we're going to talk about a custom default constructor. The term default constructor it defines a constructor that takes in no arguments. A custom default constructor is overriding that automatic default constructor that C++ provides for us. So, the simplest constructor we can make is this custom default constructor and it's going to specify the state of the object when it's being constructed. Because it's the default constructor, it's specifying the default state of the object when no arguments are given. We define a custom default constructor by having three conditions being true. We have to have a member function that has the same name as the class itself. So, in our Cube class, the name of the function is going to be Cube, the function needs to take zero parameters, and the function does not have a return type. So, the constructor builds the object, it doesn't return anything back to the programmer. If we look at the name of this function, we know the name of the function is Cube, it's part of the Cube class, and it takes zero arguments. If we look at how this looks in code, in the header file for Cube.h, we see Cube is the custom default constructor. Notice that it has no return type. It's not a double, it's not a void. There's absolutely no return type as part of Cubed.h. On the other side, Cubed.cpp is going to define the implementation of the Cube constructor. Here we have the Cube class defining the Cube member function because it has no return value and has the name of Cube, we know it's a custom default constructor, and here we have the customer default constructor initialize Cube to have an initial length of one. So effectively, when we create a cube, our cube is going to be a unit cube, where the length of every side of this cube is exactly one. For our first example here, we can see that we create a cube using the cube class we just defined. Looking at our code, here on line 11 we have our main function, on line 12, we create a cube in stack memory, and on line 13, we're just going to go ahead and see out the volume of the cube. We know that our custom default constructor created a cube that has a side of length one. So, we expect the volume of the cube to be one. So, our expected output is going to be Volume One, when we run this code. Let's check out what happens. Here in the console, I'm going to go into the CPP constructor folder, and then go into example one. Running make to build the program and then running./main to run our program. The output of this program says volume one. This exactly what we expected from the code we saw. Awesome. Let's look at doing a little more fancy stuff with constructors. We just talked about a custom default constructor which had zero parameters. However, we can also define custom non-default constructors or just custom constructors that allow user code to specify arguments to our constructor. For example, we might want the user to specify the length of our cube when they create the cube itself. So, here we're going to use the exact same syntax as our custom default constructor, but have additional parameters based on how many arguments we want the user code to pass in. For example, we might have the user passed in its length. Here we have double links being passed as argument. Constructors are not unique. Here in this code example we see that we have a custom default constructor, as well as the one argument constructor. So, a user using our Cube class is able to create a cube that has a default value, and is also able to create a cube that has a specific length. In our implementation, we see the exact same thing, we see two implementations of constructors, a custom default constructor, sets it's length to be equal to one, our one parameter constructor sets the length equal to the length passed in as a parameter. Looking at a second code example, here's another main in example two. The key difference between the example one we saw earlier and in example two here is that online five when we declared uiuc::Cube to be c2, we're calling a custom constructor. This customer constructor is going to be the one parameter constructor. So, C++ language is going to go search through all the constructors and find the constructor that has one parameter and takes in a numeric value. We see the numeric value is two. Because we're specifying the initial length of the side of the cube is two, we expect the volume to output to be two cubed or eight. So, volume eight is our expected output. Let's see what happens when we run this code. Navigating back to the base directory and moving into example two, I'm going to run make and run./main. What we see as the output is the volume is eight, exactly what we expected. Awesome. Let's talk about one important detail before we wrap this up. We started off by talking about the automatic default constructor. The idea that every class in C++ has a constructor of some sort. If any constructor is defined, we don't get an automatic default constructor provided for us by C++. So, if we define even a single constructor, the automatic constructor is not provided for us. Even if we define a custom three-parameter constructor, the automatic default constructor is not going to be provided. So, any constructor at all being defined prevents the automatic default constructor from being defined. We're going to see this trend happen throughout all of the different pieces of the constructor that we're going to see throughout the videos this week. Let's look at one example of where this comes into play. So, thinking about a combination of our two different programs we've seen so far, here's in example three, we have a uiuc :: Cube c being defined on line 12. Let's look at the Cube.h class that's here in the example three folder. Here we see that on line 13, the only constructor we have is a one parameter constructor. Because we only have our one parameter constructor, we know that there is no automatic default constructor. In fact there's no default constructor whatsoever. There's no zero parameter constructor. So, in line 12, when we're trying to call this zero parameter constructor, it's not going to find anything to call it. Because C++ is strongly typed language, it's going to find this error at compile time. So, let's see what the compiler says when we try and run a program where we don't have a constructor that's valid for it to run. Going back directory, going to example three, and running make, we see that we get in a compiler exactly what we expect. In main.cpp, on line 12, we have a fatal error where there's no matching constructor for the initialization of the Cube. We know there's no matching instructor because there is no automatic default constructor. The compiler helps us out a little bit. It did find a constructor, it said candidate constructors not viable. So, it looked at our one parameter constructor and it said the Cube with double length doesn't work because it requires a single argument length but no arguments were specified here in uiuc::Cube c. The implicit copy constructor, and the implicit move constructor are automatic constructors that were built for us, but they also don't fit the bill. So, because we define a constructor and the constructor was not a zero parameter constructor, there was no default constructor. So, there was no way for us to construct this Cube. C++ gives us an error when we try and compile this. So, you may see this error when you program, it's quite lengthy and length, but it tells us really useful information. So, that C++ constructors and now you know how to set up the initial value of a class. In almost every class we build, we're going to define a constructor so that we can have control of exactly what the initial state of the class is. So, we'll see a lot of these. In the next video, we're going to talk about another form of constructor that helps us copying an instance of the object to another instance. I'll see you then.