Initializers.
Initializers were touched upon in the last lectures, but we will go into more depth here.
So for built in types you must initialize a variable before you use it.
So for custom types it is possible for the type to define a default value.
But there are some uses in doing this which will be covered in next lecture, which in next chapters.
Which in which.
Yeah.
In classes chapter of this Oxley Udemy course.
So in all versions of Cplusplus there are three ways to initialize a built in type.
The first here three ways to initialize.
Boiled.
Built in type.
The first is assignment, which I will explain all of this.
Assignment function.
Syntax.
Thanks for calling.
A constructor.
Stop.
Here.
Now let's try.
Firstly, try with assignment, then function, then syntax, and then call.
A Actually, it's a four ways, so I'm sorry for it.
It's a four ways for calling INITIALIZERS.
So the C plus 11.
Another way to initialize variables was introduced construction through a list initializer.
So there are four types here, as I said.
So let's firstly create our in our code and write our codes and use for Initializers here includes standard
steel here just for.
Putting it here.
So integer Actually we didn't need here.
I just wrote it.
Integer.
Key here, Integer G equals integer Q which yeah, yeah.
It's it's, it shows like pretty strange.
But I will explain all of this don't worry.
Integer key here.
Actually, let's make this A, B, C, integer C three or like this three and then integer M four.
Integer D four.
So as you can see here, we did we initialized for four variables to four integers.
So the first of these three is the clearest type.
So it shows using an easy to understand syntax that the variable is being initialized to a value.
So the second example here initialize a variable by calling the type as if it is a function.
So as you can see, the second is a function calling it by function.
And the third example calls the constructor of the integer type.
So this is a typical way to initialize custom types, so it's best to reserve the syntax just for custom
types.
So and the fourth here, as you can see here, syntax is new here.
Actually, let me write it here.
A new new to C plus plus.
So this is the kind of new to C plus plus.
It actually came with C plus plus 11.
And initialize initializes the variable using an initialized list between the curly braces here, as
you can see here.
And just to confuse things slightly, you can also initialize a built in type using the same syntax
as an assignment to a single to a single item list.
For example, integer E here equals equal to parameter here and five like this.
So you can assign this operator like this to.
So now I want to print these variables just in case to show that these variables works and you can print
these values to screen.
So let's print, print f.
Uh.
Okay.
Here.
Some decimal new line.
Decimal.
New line.
Decimal and new line.
New line.
Lastly.
Actually, let's add five decimal here.
Um, actually.
Same with the set here.
And new variable.
One, two, three, four.
A fight.
Sure.
And then add these variables.
E equals.
Because.
C equals.
Equality.
The equality.
Uh, and e equal.
Yeah.
And then he picked.
And actually I forgot to write main function.
The integer main here.
And then.
So actually it's the line.
The writing these things is pretty easy.
So as you can see here, it added one tab between the this last line automatically.
So now let's print our function actually.
Take this variables to.
Main in main function here.
Oops.
By the doggo.
Okay.
Take this to main function and compile and run our application.
So as you can see here, we initialize with the first type, assign assignment type and print it to.
Uh, printed to console here command line.
And then we did the function B2, C3, D4.
And these are the same actually with syntax calling, calling a constructor type, which is a kind of
new to C plus plus language.
And I want to show you another thing.
So just initializing the arrays here.
So actually.
Initializing a race.
Initializing.
Actually, let's print f.
Initializing types.
Initializing?
Yeah.
Types.
And then.
And just print.
Print f.
Initialize.
Race and let's create our array.
So this is a really confusing things.
So the type here, the type E here is an integer but not an array.
So as you know, we can write arrays like this, for example.
Integer.
Integer birth year, for example, birth years, and then for example.
11.
For example, like this, we can with this curly braces.
We always did just creating great things, as you can see.
But here we're assigning something just a one variable to.
Integers.
So this is kind of confusing.
I know.
So just to confuse things slightly, you can also initialize a built in type using the same syntax here
as an assignment to a single item list.
So recall that in the last lecture we created an array with the birthdates.
And I did this.
I wrote this code again.
So this creates an array of four integers.
So the type of each item is an integer, as you can see here.
But the type of the array variable is integer pointer here, integer asterisk.
So the variable points to memory that holds for integers.
So similarly you can initialize also initialize a variable to be an array of one of them, for example.
Integer John.
Uh, and.
For example, John.
For example, let's write a Jones birthday.
1988.
And as you can see here, we did.
Now create an just a single array so we can use it as the just usual integer here.
So this is exactly the same initialize initialization code that C plus plus.
C plus plus 11.
C plus plus 11 allows to initialize a single integer.
In addition, the same syntax is used to initialize instances of record types as you know, structs
here.
So adding another level of potential confusion about the what syntax means.
So it's best to avoid using the curly braces for variable initialization and use it exclusively for
initializing lists.
So just avoid this.
I just I just wanted to show you that C plus plus 11 has this feature.
So however, there are advantages of this syntax for casting as explained shortly.
So the curly brace syntax can use to provide the initial values for any of the collection classes in
C plus plus standard library.