- [Instructor] Constructors and destructors
are special member functions
that serve a particular purpose.
Constructors are used to initialize and set up an object,
and destructors are used to de-allocate and reset resources
when an object is destroyed.
There are several types of constructors in C++.
This is constructors.cpp from chapter six
of the exercise files.
And you notice we have a class here called Animal.
It has three data members, animal type, animal name,
animal sound, and there's three constructors
and one destructor.
The constructors are named with the name of the class.
They do not have their own name.
And this is how the compiler knows
that these are constructors.
And you can have as many constructors as you want,
as long as they have different function signatures.
And then a destructor is named with the name of the class
with a tilde added at the beginning of the name,
and this makes it a destructor.
So you'll notice that we have
three different constructors here.
The first one is what's called a default constructor
because it has no arguments.
And so if no arguments are specified,
then it's a default constructor.
And if no constructors are specified at all,
if we didn't have any of these,
then a default constructor would be provided,
an implicit default constructor would be provided
by the compiler.
Our second constructor takes three arguments,
type, name, and sound,
and our third constructor takes a reference
to another animal object.
And this makes it a copy constructor
because it will take that other animal object
and copy its data over into this new object.
And then there's our destructor
and a couple of other function members.
And so let's look down here at the implementation
of these constructors.
First, we have our default constructor
because it has no arguments in its parentheses.
And you'll notice after a colon
that there's this list of initializers.
And so each of the data members is initialized
with this string object.
You'll remember up here at the top of the code,
we have a string object called unk for unknown
and another one called clone prefix,
with the string clone- as its string.
And so when we come back down here
to our constructor implementation,
you notice that we're initializing
each of our data members, which are STL strings,
with this unknown string.
And so we're initializing the values
of type, name, and sound as unknown.
And then we simply put a string on the console
that says we've called the default constructor,
and we're just doing this for the purpose of demonstration,
so that when we run this,
we can see the construction and destruction
of these objects.
Now we have a constructor with three strings
as its parameters.
And so we're actually initializing each of these strings.
And again, after the colon,
we have these initializers.
This syntax of having the initializers after a colon
and before the beginning of the code
is only used in function members in class definitions.
And it's just a simple way of constructing constructors.
And so here we have constructor with arguments
for the string that's printed out
when we call this constructor.
And then we have our copy constructor,
which initializes the data members like this,
like in normal code.
And then we have our destructor,
which resets each of our strings.
And it prints out this destructor
so we know which object is being destructed,
or rather destroyed.
We print out the name and the type of the animal.
Then we have an assignment operator
and a simple print function,
and we'll demonstrate these.
These are pretty standard,
and we'll talk in more detail about them later.
And now here's our main.
We construct an animal with the default constructor.
We construct one with the explicit strings,
and we use the copy constructor,
and we use the assignment operator.
And then we end,
and then each of our objects will be destroyed.
So we really have three objects here,
A, which is constructed with the default constructor,
B, which has the three strings,
C, which is constructed with the copy constructor from B.
And then we use an assignment operator
to assign C back to A.
So let's go ahead and run this,
and we can watch what happens.
So first we have our default constructor,
and you see the string default constructor.
And then after that, we call the print function,
which says unknown, the unknown says unknown.
So it's name, type, and sound.
And we remember those from up here in our interface.
And then we have our constructor with arguments.
And so here we have constructor with arguments,
bob the goat says baah,
'cause we've given it a type, a name, and a sound,
and then we have an assignment constructor,
or rather a copy constructor.
It says copy constructor,
and it says, clone-bob the goat says baah.
And if we look up here at our copy constructor,
we see that we append the clone prefix,
plus the name in our initialization.
And our clone prefix, as you remember says, clone-.
And so we get that clone-bob,
just to show that it's a copy.
And we do the same thing with the assignment operator.
So we assign it to A, with the assignment operator,
and then we have clone-clone-Bob,
because we're adding another clone at the beginning.
And then end of main,
and then the three destructors clone-bob, bob the goat,
and clone-clone-bob the goat.
And so we can watch each of these objects get destroyed.
So it's worth noting,
and I'm just going to show you this one more time,
because this is unique to constructors
that you can use this initializer list
to initialize members before
or even without executing the body
of a constructor function.
And these initializations are very common
and very convenient, so you'll see them quite often.
And this is after a colon,
and before the beginning of the code block.
Constructors and destructors are important parts
of any C++ class.
It's always worth thinking carefully about
how your objects are constructed
and crafting constructors and destructors
that handle all the necessary use cases.
For more detail about constructors,
please see the companion course C++ advanced topics.