As you saw in the previous lecture, the compiler will compile your source file.
As long as individual items called translation units.
So the compiler will determine the objects and variables you declare and the types and functions you
define.
So.
And once declare, you can use any of these in the subsequent code with the scope of the declaration.
So at its very broad set, you can declare an item at the global scope by declaring it in a header file.
So that will be used by all of the source files in your project.
If you do not use a namespace, it is often wise when you use such global variables to name them as
being part of the global namespace.
So now I want to create a new header file here in C++, which I will explain later.
What this header file he is.
So version here, let's create a version header file.
And as you can see here, the C line compiler automatically created the version that C++ file as well.
So let's change my username to our online science academy Oxley name.
And here we have a main C++ file and random file.
And also we have a header file of this version, CPP file.
So in the inversion header file, let's add the external integer version here.
Here and I will explain all of this called later as I do and visual inspection start up file include
the version H file.
As you can see here, it's actually included by automatically.
So we don't need to write a here and version here.
Equals 17.
As you can see, we got an problem here because we have the insert class here, so.
Well integer version ops.
Integer version.
The term zero.
Here.
Actually, we need to main here.
And then we will assign Virgin as virgin.
As seven.
17 year.
So.
Now let's use the main main step here as well.
Here we're going to add the print.
We're going to add print function in this main zip file.
So we have to include the.
Firstly, we need to include the version here version that either file, as you can see, or it comes
automatically.
And then we let's define the print, the print function here.
Void print version.
Here.
And let's use the braces here instead.
Or actually, let's use the print function here instead of STD.
Print f print f k.
So version equals.
Is it close to?
A decimal because I will get decimal here.
And then here.
Where?
As you can see, we use this double dots here, as you can see here, because.
We included the version that he mentioned that either file and in version that either file, we have
an external integer version variable, which I will explain what these are.
So the code here, as as you see here, has two source files, the version CP and main that C p p here.
And we have also we have also we have a header file as you know.
Also we have a header file included by both source files here.
As you can see here in the version that CPP, it's included version header file and also in main that
CPP, we have the included version of the file here.
So.
Um.
To hear Heather file.
Heather File declares the global variable version, which can be used by both source files, so it declares
the variable but does not define it here.
As you can see, we defined it here.
We defined version as 17.
So the actual variable is defined and initialized, as you can see here.
Here.
So it is here that the compiler will allocate memory for the variable.
So the extent keyword here used on the declaration in the header in the case to the compiler that the
version has external linkage.
So let's write it.
So when you don't want this code, you will see and read this and remind us the external here means
external external linkage.
Being Lincolnshire and that is the name is visible in files.
Other than the variables is defined.
So the virgin variable is used in the main CP source file.
So in this file, the scope resolution operator here, this is the scope resolution Operator.
As you can see, this is a two double dots here is used without the namespace name.
As you can see in previous lecture we actually used some names.
For example, we did a, for example, Revolver, and then we use this scope resolution operator here.
So actual limit variety, scope resolution Operator.
This means scope resolution.
Operator.
And I want to also note this extent terminology.
So actually this terminology is matters because you will use them a lot if you do the object auditing
programming or just any C++ programming, you will use this external linkage and scope resolution operator.
So of course there is a programs that simple programs that don't use the operators or keywords.
So you can also declare items that will only be used within the current translation unit by declaring
them within the source file before they are used.
So usually here are top on the file.
So this produces a level of modularity and allows you to hide implementation details details from code
in other source files.
So for example, let's create an let's write our other file here.
Actually, let's create the.
Um.
Mean here.
So.
Oxley.
And let's say in Maine that either file here we will declare the void.
Usage here, usage function just a function.
We will not write a code in it yet.
And in print in here, Maine, the CVP, we're going to include the version H as we did earlier, and
then we will use this standard.
We will use string from the standard library string.
Application name, for example.
Application name.
For example, Port scanner.
And here we have an app name, a variable string called app name here, which is this is a string.
And inside the string, we have a port scanner string here.
So as you know, in C++ string and actually in a lot of language strings is adjustable arrays of arrays
of characters here, which you will learn about these strings later also.
So then let's use our print.
As you can see, we have a print function here.
And.
We will use we will define usage function here also.
Actually, let's include our main that include our include main header file here and define usage.
Void usage.
Here and.
STD.
See out.
Application name.
Here.
Actually, we have to define out of this main function here application name and then here.
And then let's call the print function here.
Print function, print version.
Here.
And.
And so the print that the main that either file here contains the interface for the code in the main
zip file.
As you can see, we use this usage.
So only those functions declared in the header file will be callable by the other source files.
So the color does not need to know about the implementation of the user function.
So, and as you can see here, it's implemented using a call to a function called print version.
So that is only available to mean that C++ file.
So the variable application name is declared at the file scope, so it will only accessible to code
in main dot CP.
So if another source file declares a variable at file scope, that is called the application name.
And this is also standard string here.
So this is the string here.
As you can see here, the file will compile.
But the linker will complain when it tries to link the object file.
So the reason is that linker will see the same variable defined in two places and it will not know which
one to use.
So a function also defines a scope.
Variables defined within a function can only be access through the name, so the parameters of the function
are also included as a variable within the function.
So when you use, when you declare the variables, you have to use different names.
So if a parameter is not marked as a constant here keyword, as you remember, if you parameter is not
marked as constant, then you can alter the value of of the parameter in your function.
So you can declare the variables anywhere within a function.
As long as you declare them before you use them.
Here, curly braces are used to define the code blocks and they also define local scope.
So if you declare a variable within the code block, then you can only use a term.
So this means that you can declare variables within the same name outside of the code blocks, and the
compiler will use the variable closest to the scope it is accessed.
So before finishing this lecture, it's important to mention one aspect of the C++ storage class here.
A variable declared in function means that the compiler will allocate memory for the variable on the
stack frame created for the function.
So when the function finishes, the stack frame is torn down and memory is recycled here.
So this means that after a function returns, the values in any local variables are lost completely.
So when the function is called again, the variable is created a new and initialized again.
Also in this lecture I want to talk about actually in this lecture, I want to talk about the C++ static
keyword.
So but in C++ here, static, the keyword change behavior.
So the static keyword means that the variables is allocated when the program starts, just like variables
declared at the global scope.
So applying static to a variable declared and function means that the variable has internal linkage.
So that is the compiler restricts access to the variable to that function.
So now I want to create an example code here, for example.
Before this my entered the increment here in increment and.
And then let's create create a static, static integer value.
Value.
Increment by one.
Increment by one
by one year and return value.
Here.
Let's use the.
Now, let's print our function here.
Increment here.
And the.
So increment here we will use.
As you can see, this is the increment.
So we're going to pass one number as a parameter.
For example, five here Ora, let's create an this function type.
Lots of types for example.
Five, nine.
19.
999.
So.
Uh, let's compile this application.
Actually, let's create this application year project as a new project.
Because I don't want to waste these quotes because I want to share it with you as a file.
Yes.
And we're going to overwrite the increment function here.
Integer increment Integer here.
And static integer value.
Value plus equals.
E here and we will return the value.
Here.
So why we have a Yeah, we have to add the here.
So then let's compile our code.
Actually, let's add new lines here.
Because.
Because your new line's so.
By default Here the compiler will initialize a static variable.
Two zero.
So, but you can provide an initialization value and this will be used when the variable at first allocated.
So when this program starts, the value variable will be initialized to zero before the main function
called.
So the first time the increment function is called the value variable incremented to ten no to five
year, which is returned by the function and printed to the console.
And when the increment function returns, the valid value variable is retained so that when the increment
function is called again, the value variable incremented by.
Nine here.
As you can see, a five plus nine equals 14.
Actually, that's a great calculator.
So.
Let's create an example without static.
Here.
This is a static and this is not static here.
So.
And now I want to.
Not Pat.
Yeah.
With static.
Here.
Or without with how static.
And with static.
With static here.
And let's create let's add static keyword and start it again.
As you can see here, we got the different result.
So.
Yes.
So without stating our value here.
Ursus itself and just does nothing.
But here, by default, the compiler will initialize the static variable to zero, but you can provide
an initialization value here.
So and this will be used when the variable is first located.
So when this program starts the value.
Variable.
Initialized to zero and is incremented.
To increment the to five, which is returned by the function and printed to the console.
Piers.
So.
When the INC function returns the value.
Variable, there's tint so that the when the function is called again, the value variable is incremented
by five by nine, and total number is going to be five plus nine equals 410.
So it might be confusing at first, but I promise you will learn this constant and static variables
and keywords in later lectures.
This is just the beginning of the C++ pass, but I know and you can do it.
So in the next lecture we will talk about the operators and I'm waiting in the next lecture.
So I know this.
This lecture is actually almost 20 minutes.
So I hope you watched all of this and I'm waiting you in the next lecture.
This is just the beginning or intermediate C++ programming.
So after.
Watching and including your my lectures in your practically you will learn about C++ a lot.
So I'm waiting in the next lecture.