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 declared, 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 plus plus which I will explain later what this header
file is.
So version here, let's create a version header file.
And as you can see here, the Sea Lion compiler automatically created the version that Cplusplus file
as well.
So let's change my username to our online computer science academy.
Oxley Name.
And here we have a main dot C plus plus file and version file.
And also we have a header file of this version cpp file.
So in version in version header file, let's add the extern integer version here.
Here and I will explain all of this code later as I do and version in versions dot cpp file.
Include the version file.
As you can see here, it's actually included by automatically.
So we don't need to write here and version here.
Equals 17.
As you can see, we got an problem here because we have the insert class here.
So, um.
Oh integer version of.
Integer version.
Return zero.
Here UPS Actually, we need to main here and then we will assign version as version.
As seven.
17 year.
So.
Now let's use the main main.cpp here as well.
Here we're going to add the print where we're going to add print function in this main dot CPP file.
So we have to include the firstly, we need to include the version here.
Version dot header file.
As you can see, it comes automatically.
And then we let's define the print print function here.
Void print version.
Here.
And let's use the braces here.
STD.
Or actually, let's use the print function here instead of STD.
Printf.
Okay, so version equals.
Is equals to.
Uh, the decimal, because we will get decimal here.
And then here version.
As you can see, we use this double dots here.
As you can see here, because.
We included the version that H mentioned, that header file and in version that header 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 CPP and main dot CPP here.
And we have also we 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 version of CPP, it's included version header file and in also in Maine dot CPP,
we have the included version dot h file here.
So.
Um.
So here the header file.
A header 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 extend keyword here used on the declaration in the header in the case to the compiler that the
version has external linkage.
So let's let me write it.
So when you download this code you will see and read this and remind us the extern here means external
external linkage.
Linkage here, and that is the name is visible in files other than where the variables is defined.
So the version variable is used in the main.cpp source file.
So in this file, the scope resolution operator here, this is the scope resolution operator.
As you can see, this is the two double dots here is used without a namespace name.
As you can see in previous lecture, we actually used some namespace.
For example, we did, for example, Revolver, and then we use this scope resolution operator here.
So actually let me write the scope resolution.
Operator.
This means the scope resolution.
Operator And I want to also note this extern terminology.
So actually this terminology is matters because you will use them a lot if you do the recording programming
or just any C plus plus programming, you will use this external linkage and scope resolution.
Operator So of course there is a programs, the simple programs that don't use these operators here
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 top on the file.
So this produce 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 um, header file here.
Actually, let's create a.
Um.
Main here.
So.
Oxley and lets in main dot header 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.
Main.cpp.
We're going to include the version age as we did earlier, and then we will use this standard.
We will use string from the standard library.
String.
Application name here.
For example, application name, for example.
Port scanner.
And here we have an app name variable string called app name here, which is this is a string.
And inside this string we have a port scanner string here.
So as you know, in C plus plus string in actually in a lot of language strings is just of arrays of
arrays of characters here, which you will learn about these strings later also.
So then let's use our as you can see, we have a print function here.
Uh, 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.
Avoid 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 we will call the print function here.
Print function.
Print version.
Here.
And.
And so the print that the main dot header file here contains the interface for the code in the main
dot CPP 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 file.
So the color does not need to know about the implementation of the usage 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 main dot cplusplus file.
So the variable application name is declared at a file scope, so it will only accessible to code in
main.cpp.
So if another source file declares a variable at file scope that is called the application name and
is also a standard string here.
So this is the string here.
As you can see here.
Um, 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 you to use.
So a function also defines a scope.
Variables defined within a function can only be accessed 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 const keyword as you remember, if your 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 scopes.
So if you declare a variable within the code block, then you can only use it there.
So this means that you can declare variables within the same name outside of the code blocks, and the
compiler will use the variable clauses to the scope it is accessed.
So before finishing this lecture, it's important to mention one aspect of the C plus plus 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, yeah, in this lecture I want to talk about the
C plus plus static keyword.
So but in C plus plus here, static keyword change this behavior.
So the static keyword means that the variables is allocated when the program starts, just like variables
declared at a global scope.
So applying static to a variable declared in a 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.
Um.
Know before this main function integer increment here in increment and.
And then let's create a static static integer value.
Value increment by one.
Increment by one
by one here and return a value.
Here.
Let's use the now let's print our function here.
Increment here and.
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, or 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 here project as a new project.
Because I don't want to with these codes, because I want to share it with you as a file.
Here and we're going to write 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 here.
So then let's compile our code.
Actually, let's add a new lines here.
Because the cars here new lines 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, you know,
to five here, which is returned by the function and printed to the console.
And when the increment function returns, the value value variable is retained so that when the increment
function is called again, the value variable incremented by.
Nine here.
As you can see, five plus nine equals 14.
Actually, let's create a 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.
Notepad.
Here.
With static.
Here.
Or without.
Without 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.
Here.
So without static, 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.
A variable we will initialize to zero and is incremented.
To increment the to five which is returned by the function and printed to the console.
Here.
So.
When the INC function returns the value.
Variable is returned so that the vendor Inc function is called again.
The value variable is incremented by five by nine and total number is going to be five plus nine equals
four teen.
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 plus plus files, but no.
And you can do it.
So in next lecture we will talk about the operators and I'm waiting you 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 plus plus programming.
So after.
Watching and including your my lectures in your practically you will learn about the cplusplus a lot.
So I'm waiting you in the next lecture.