(upbeat music)
- [Bill] Constructors and destructors
are special member functions that serve
a particular purpose.
There are several types of constructors in C++.
A constructor or a destructor has no name of its own,
they use the class name.
And if we come down here to the class,
you'll notice there are member functions declared here
with the name of the class
so the first one is the default constructor,
second one is a constructor with operands,
and the third one is called a copy constructor,
and then there is a destructor.
You'll notice here we have three private data members.
These are strings from the STL string class
and they're all initialized to this unknown constant.
There's another constant called clone_prefix,
it says clone_.
And we'll be using that when we make copies of the object.
And you'll see also here
there's another function member called print.
And all of these are implemented down here.
We have the default constructor starting on line 24,
we have the constructor with parameters,
and we have the copy constructor,
the constructor that takes another object of the same type.
And then we have the destructor,
and the print function that simply says,
name the type, says sound.
Now let's take a look at each of these constructors
in detail.
The default constructor is called a default constructor
because it has no arguments.
And see when you look down here in main,
you see Animal a is constructed with the default constructor
because it has no parameters.
And this particular one doesn't really do anything,
you can do things in a default constructor.
All this one does is it puts the string default constructor
in the console so that you can see that it was called,
and each of these does that.
The constructor with arguments takes three string arguments,
each of them is a constant reference to a string
and they're named type, name, and sound.
These use these special class initializers,
a constructor may initialize members before
or even without executing a body of a constructor function.
And these initializations are very common
and they're especially convenient and they look like this.
So for each of these,
it's the name of a private data member.
And so in this case, we have type, name, and sound,
each with an underscore before them,
you see them here on lines 12 through 14.
And then in parentheses is the value that is being used
to initialize that private data member.
And so in this case, it's type, name, and sound,
these strings that are being passed as parameters
or as arguments to the constructor.
And so there's a colon that introduces
this initialization section,
and it happens between that colon,
and the opening curly brace of the block.
And in this case, the block all it has in it
is the puts for constructor with arguments.
So again, this is something new,
you haven't seen this before,
it's not common for functions,
but it is very common for constructors
in a class definition.
And then finally, there's what's called a copy constructor.
And the copy constructor, you see it takes one argument.
And that argument is actually on
the other side of an equal sign.
So if we see this down here, we're defining const,
and the animal type, and the name for the object which is c,
and an equal sign and b.
And so b is being used to initialize c
by copying over its data.
And so here we put copy constructor,
and then we have these three lines of code,
which basically copy over the data from the right-hand side
of the equal sign from the argument
that's passed to this constructor.
And you'll notice that we prepend the clone_prefix
to the name so that the name will say clone-,
and then the name,
and we'll see that when we run this.
The destructor doesn't really do anything except say
that the destructor is being called and tells you which one.
And then the print function simply says,
name the type of animal, says the sound.
And so we call each of these three constructors
and we call print on each of these objects.
And then we say, end of main, and we end.
So I'm going to go ahead and I'm going to build and run this,
and you can see what it does here.
Our first one is the default constructor,
and that's this one here.
And you'll notice that when we print,
it says, unknown the unknown says unknown.
And that's because the default constructor
doesn't do anything,
and that unknown string is the initialization
for each of those data members.
And then we call the constructor with arguments,
and you see it says, constructor with arguments,
and bob the goat says baah.
And then we call the copy constructor and you see it says,
copy constructor,
it says, clone-bob the goat says baah.
And then we put the string end of main.
And then you notice the destructors
for each of these objects is called,
and that's because each of these is an automatic data,
it's data stored on the stack,
and when this block ends, all of those objects are released.
And as they're released, the destructors are called so
that you can clean up memory,
you can release resources and do whatever you need to do
when your object is destroyed.
So 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.
(upbeat music)