In this video, we'll talk more about the default constructor.
A default constructor is a constructor that doesn't expect any arguments.
It's also called the no args constructor.
C++ has to have a way to construct and initialize objects
if you don't provide any initialization information at all.
So if you don't provide any constructors at all,
c++ will generate a default constructor automatically.
This default constructor does nothing, but it's still generated by c++.
When you create objects with no initialization information,
this is the constructor that's called. Let's take a look at the account class again.
In this account class, we've defined no constructors at all.
So c++ will automatically generate a default constructor
that allows us to create objects with no initialization information.
Let's see a few examples.
Here we're creating two local objects Frank account and Jim account.
The compiler provided default constructor will be used since we didn't provide
since we didn't provide any constructors at all.
In the case of a pointer to an object,
the same thing happens except that the object is created on the heap.
Since the compiler generated default constructor does nothing,
the class member attributes of the account class could contain garbage since they haven't been initialized.
Of course, we're free to provide our own user-defined no args constructor.
In fact it's best practice to do so.
In this case, we're defining our own user-defined no argus constructor.
This constructor will be called when no initialization information is provided.
And in this constructor, we set the account name to none and the account balance to 0.
So we're initializing the account member attributes to values we know about, not garbage data.
Now let's see what happens when we define a constructor that expects arguments.
In this case, we're defining a constructor that expects a string and a double,
and we initialize our member attributes to the values passed into the constructor.
This is very commonly done.
However, once we define a constructor for our class
c++ will now not generate the no args default constructor automatically.
If we still need it, then we must explicitly define it ourselves.
If you have code that creates objects with no initialization information,
then that code will no longer compile since the default constructor
is no longer generated by c++.
So in this example, we can no longer create account objects with no arguments,
again, since the default constructor is no longer automatically generated by the compiler.
So we'll get three compiler errors when we try to create Frank, Jim and Mary's account.
However, we can still create account objects,
but we have to use the constructor we provided that expects a string and a double.
So we can create Bill's account since it provides the initialization information
required by the constructor.
Let's go through some of these examples in the IDE.
Okay. So I'm in the IDE. I'm in the section 13 workspace
in the default constructor project. And what I've got here is
the same player class that we've been using where we've got the name the health and the xp for a player
which are private instance variables,
and we've also got a couple of member methods here:
set name and get name that's just the setter and the getter for that name attribute right there.
What's important to understand here, and of course, I would do the same thing for health or xp,
but i don't want to clutter up the code, I just want to talk about the default constructors here.
You'll notice that this player class has no constructor declared.
C++ has to have a way to create objects.
So if you don't provide any constructors,
it will provide a system generated
no args constructor that will allow you to create objects.
So in this case, I don't have any constructors declared,
but yet, I'm able to create
player objects with no problem,
I could say Frank.set name to let's say Frank.
And I could output the
value of Frank's name by using get name.
Okay. Something real simple like that. And what we'll do is we'll walk through this,
we'll step through it using the debugger. So I'll put a break point right here,
and we can debug this real quickly.
And we really don't care about the output. We just want to see that object.
So you can see here that I'm on line 24,
and I'm going to instantiate that Frank object, which is a player.
There is no constructor.
So behind the scenes, a default no args constructor is
generated that does nothing,
but it does allow c++ to create that object.
So I'm going to go next here.
At this point, Frank is created.
You can see the object right up here.
And I executed the set name, which set the attribute name to Frank.
That's it.
The next statement would obviously print it to the console.
So you can see c++ has to have a way to create these objects.
If you don't provide a way to initialize this
using any kind of no args constructor,
you get one automatically by the compiler.
That's great. Now of course -- I'm going to stop this. Of course, we can create our own
no args constructor. So let's do that. I'll do it down here so it's closer to the main.
So what we can do is we can create our constructor. There it is. It's called player,
same name as the class, no args,
and we'll implement it right here.
Now what's the default behavior I want when I don't provide any initialization information,
anything reasonable whatever makes sense for your application.
In this case, let's just say that the name is none.
We'll say that the health is, let's say, a 100.
And let's say the xp is 3.
That's it. That's my initializer.
That's also my default initializer. That's what's important.
This will now be called right here. And I'll put a break
point right in there so we can see it being called, and we'll debug this again.
And here we go. What you can see happening is
we're right here on line 30.
And now when i step through this,
notice how it transfers control to line 22,
that's exactly what we expected.
We didn't see that before because we didn't provide a default constructor,
but now we're providing one. So ours will be called rather than the compiler created one.
And this will just step through and it'll set the properties to name
100 and 3.
Now I'm back here on line 31. And you can see right here
that we've got none, 100 and 3.
And now I can change the nun to Frank right here on this line.
And I'll refresh this, and you can see here it's Frank
right there, the string is Frank.
Okay. So there you go.
If you don't provide any constructors, you get a default constructor for free if you will.
And if you provide your own default constructor that's the one that will be used.
Now let's do one more thing here.
What happens if you create -- let's let me comment this out right here.
And suppose that I want to create
a different constructor, an overloaded constructor. So I'm going to say player
and I want to provide the name.
I want to provide all of those values. So this is my name val. I want health
and I want the xp as well. I want to provide all three of these values.
And in the body of this constructor, I'm just going to say name equals name val,
health equals health val
and xp equals xp val.
Okay. So that's where we're at. Now we have a bit of a problem now.
In c++ -- and this is true of many object-
oriented languages, including java and others.
You get that default constructor as long as you don't provide
any other constructor. Well, what we just did here is we provided a constructor.
In this case, this constructor expects three arguments.
Now that compiler generated
no args, default constructor will no longer be generated.
So this line of code right here on line 35 won't compile anymore.
Let me run the debugger here. You'll see the error right here,
it says no matching function call for the no args constructor.
This is perfectly normal. This is exactly the way it's supposed to work.
The idea being that now you've provided a way to construct your object,
so c++ note will no longer provide a default way,
right,maybe you don't want a default way. So this won't compile.
I'll comment that out,
and I'll move the breakpoint down to here.
So now the way we need to construct this object is to explicitly call that other constructor.
So I'm going to say player Frank, and we'll initialize that to Frank
100 and let's say 13 or something.
Now what's going to happen is when this line executes let me get rid of fearless break points,
when this line executes on 36, what's going to happen is
this constructor is going to be called, the one that i just created.
So I'll put a break point there so you can see that happening.
Okay. So let's debug this again. We'll walk through it one more time,
and here we are. We're on line 36.
When I step through it, you'll see control will be transferred up to line 27 up here.
So there we are. We're in the constructor now
so we're explicitly calling that 3 arg constructor that we just created.
Remember, once I create any kind of constructor, the default constructor
that's automatically generated won't be generated anymore.
We'll walk through this.
And when we're done, we'll refresh this. And you can see Frank 113 has
been created just as we expected.
Now of course, we can
create both of these, right. So now I can create,
let's say, a Hero object.
So now what we've got is we've got
two constructors that we provided. We provided a no argus constructor,and we provided
this overloaded constructor that expects the three arguments.
In this case, Hero, we're not providing any initialization information.
So the no args constructor, this guy right here on line 21 will be called.
And in the case of Frank, it'll be just like before this one would be called.
So let's step through that,
and let me do that again, I'll put a break point on Hero.
Let's try that one more time, and you can see what's happening here is. I'm right here on line 35,
so I'm going to construct that Hero object.
And you can see transfers control right up to here.
So there's no args constructor is being called, that's what we expect.
That's our constructor, right. That's not compiler generated because we wrote it ourselves.
So that's going to execute.
Now I come back to Frank, and this constructor will be called, so you'll see
control will be transferred over here to 27.
And we'll run through that, you can see it happening right here on line 29 that's where I'm at now.
And at this point, we've got both those objects constructed
and we can see them right here.
There's Frank and there's Hero. Hero uses none 103,
and Frank is using Frank 113, you see them right here.
Okay. So that's it. That's the behavior for default constructors in c++.