Using types and variables.
The types were covered in more detail in the next lecture, but it's useful to give a basic information
here.
So C++ is a strong type language as we mentioned earlier.
So which means that you have to declare the type of the variable that you use.
So the reason for this is that the compiler needs to know how much memory to allocate for the variable,
and it can determine this by the type of the variable.
So in addition to the compiler needs to know how to initialize a variable if it has not been explicitly
initialized.
And to perform this initialization, the compiler needs to know the type of the variable.
So the C++ 11 provides with the auto keyword which you will learn.
So which relaxes this concept of strong typing and it will be covered in the next chapters.
So however, the type checking for the compiler is so important that you should use type checking as
much as possible.
So C++ variables can be declared anywhere in your code as long as they are declared before they are
used.
So where you declare a variable determines how you use it.
So this is called the scope of variable.
So in general it is best to declare the variable as close as possible to where you will use it.
And within the most restrictive scope, this prevents name clashes where you will have to add additional
information to disambiguate two or more variables.
So you may and should give your variables descriptive names.
So this makes your code much more readable and easier to understand.
So C++ must start with a alphabetic character or an underscore so you can start.
So for example, C++ start to have to start with alphabetic character or an underscore.
So you can write variables like this in, for example, integer.
My variable.
And here, as you can see, there is no error at all.
And you can write your variables by.
Upper score variable.
My variable.
To underscore my very well.
Come tool and into my.
My variable.
You're here.
So as you can see here, these are names are the almost same, except there's an underscore here.
But these three names are same.
But as you can see, we didn't get an error.
If you write this exactly like this underscores and upper scores like this, for example, you will
get an error because of this redefinition.
So we have to remove the previous declaration of a local variable, as C++ suggests.
So let's compile it and see what happens.
So radical version of an integer variable note here.
Integer variable previously declared here.
When we click on this here, as you can see, we can not declare the same names, but if we change this
here, this alphabet here, we make it and make it underscore they are no longer the same variable here
as let's try it.
So.
Here and the C++ names are case sensitive, as I said earlier, and the first 2080
2048 characters are significant.
So you can start variable name with an underscore, but you cannot use to underscore series.
You can see here variable here.
So no, you can use an underscore followed by a capital letter here.
As you can see, no convertible is all assigned, but never accessed here.
We got no error here.
So and clearly you cannot use the type name as variable names, neither built in type names, for example,
integer.
So we can use the long as a variable name because it's a key word and it's reserved for its reserved
keywords.
So or we cannot use wild or other keywords in our language here, as you can see here.
So you declare a variable in a statement and think with a semicolon here as you can see here.
So the basic syntax of declaring variable is that you specify the type, then the name and optionally
an initialization variable.
Here this is a type name and initialization variable.
So builtin types must be initialize before you use them.
As you can see here integer e here.
Let's increment e by one and here std C out and e here.
And let's actually let's make a return statement here and let's run this program here.
As you can see, we got an error.
We got not error.
This is incorrect.
This has this program compiled successfully.
And as you can see here, there is an output one.
So this gave us output fun.
This is the zero by default as you know earlier and we incremented increment by one here as you can
see here.
And we and so zero plus one equals one and we got this one here.
So there are essentially three ways to initialize the variables.
You can assign a value, you can call a, you can call the type constructor or you can initialize a
variable using function syntax.
By the way, you will learn constructors for classes in next lecture.
So let's create an some program here, some syntax here, the code here A equals one.
Assign it one integer, P integer two and integer C here three.
So now I want to explain what these are.
So obviously they are almost they they we can compile our program without a problem.
Problem and actually E equals to one B equals to two and C equals to three.
I will show you what these are.
Here's a.
So firstly a.
Then the a.
B.
Okay, then.
See?
And yeah that's it and new line here so let's ABC and let's compile our program.
UPS.
Yeah.
Let's delete this.
A.
B.
C.
There.
As you can see here, we got this and there are three legal C++, but stylistically the first is the
better because it is more obvious.
So the variable is an integer.
It's called a, and it's assigned to value one.
The third looks confusing.
As you can see here, it looks like the declaration of function when it is actually declaring variable.
The next lecture will have a show of a variation of assigning a value using the initialization list
syntax.
So there is the reason why you will want to do this will be you will learn that in the next sections.
So.
And also you will learn classes, will cover classes, your own custom types.
So a custom type may be defined to have a default value, which means that you will have you may decide
not to initialize a variable of a custom type before using it.
So however, this will result in a poorer performance because the compiler will initialize the variable
with a default value and subsequently your code will assign a value, so resulting in a assign being
performed twice here.
So let's let's print this with here.
Let's print this.
Let's print this with.
See our function here.
See out.
Single STDs out STDs.
See out here and see here.
As you can see here, if you delete this, see, as you can see, we got an error from a specified W
chart.
The argument has a typo in it here.
As you can see here, our program is.
Fakes steer and let's The lid is also here.
Actually, no.
Here.
As you can see, we got.
So I want to say why we got this hurt and this strange number.
It's because of we need to.
And as you can see there, the see here, that's the latest.
And you will see our program has fixed the error here.
We used see format specified.
So if you put this operator before in print a function before any alphabet here, you this C will turn
into just the sound format specified here.
That's because you have to do the space here and our code will fix here, as you can see here, let's
run it again.
And as you can see, we got an A one, two, three and there is a three times three assignment ways
in C++.
So I'm waiting you in next lecture.