1 00:00:01,200 --> 00:00:07,860 As you saw in the previous lecture, the compiler will compile your source file as long as individual 2 00:00:07,860 --> 00:00:10,920 items called translation units. 3 00:00:10,920 --> 00:00:16,620 So the compiler will determine the objects and variables you declare and the types and functions you 4 00:00:16,620 --> 00:00:17,340 define. 5 00:00:17,760 --> 00:00:24,540 So and once declared, you can use any of these in the subsequent code with the scope of the declaration. 6 00:00:24,540 --> 00:00:32,270 So at its very broad set, you can declare an item at the global scope by declaring it in a header file. 7 00:00:32,280 --> 00:00:36,750 So that will be used by all of the source files in your project. 8 00:00:36,780 --> 00:00:44,250 If you do not use a namespace, it is often wise when you use such global variables to name them as 9 00:00:44,250 --> 00:00:46,230 being part of the global namespace. 10 00:00:46,230 --> 00:00:54,480 So now I want to create a new header file here in C plus plus which I will explain later what this header 11 00:00:54,480 --> 00:00:55,410 file is. 12 00:00:55,410 --> 00:00:58,860 So version here, let's create a version header file. 13 00:01:01,970 --> 00:01:09,590 And as you can see here, the Sea Lion compiler automatically created the version that Cplusplus file 14 00:01:09,590 --> 00:01:10,280 as well. 15 00:01:10,280 --> 00:01:16,130 So let's change my username to our online computer science academy. 16 00:01:16,710 --> 00:01:18,890 Oxley Name. 17 00:01:19,850 --> 00:01:24,500 And here we have a main dot C plus plus file and version file. 18 00:01:24,500 --> 00:01:28,690 And also we have a header file of this version cpp file. 19 00:01:28,700 --> 00:01:37,340 So in version in version header file, let's add the extern integer version here. 20 00:01:38,450 --> 00:01:46,640 Here and I will explain all of this code later as I do and version in versions dot cpp file. 21 00:01:47,000 --> 00:01:49,360 Include the version file. 22 00:01:49,370 --> 00:01:52,490 As you can see here, it's actually included by automatically. 23 00:01:52,490 --> 00:01:57,620 So we don't need to write here and version here. 24 00:01:58,520 --> 00:02:00,260 Equals 17. 25 00:02:01,450 --> 00:02:07,380 As you can see, we got an problem here because we have the insert class here. 26 00:02:07,390 --> 00:02:09,340 So, um. 27 00:02:11,900 --> 00:02:14,690 Oh integer version of. 28 00:02:18,060 --> 00:02:20,460 Integer version. 29 00:02:24,080 --> 00:02:25,460 Return zero. 30 00:02:27,850 --> 00:02:37,600 Here UPS Actually, we need to main here and then we will assign version as version. 31 00:02:38,790 --> 00:02:40,620 As seven. 32 00:02:42,570 --> 00:02:43,650 17 year. 33 00:02:44,760 --> 00:02:45,270 So. 34 00:02:46,580 --> 00:02:50,660 Now let's use the main main.cpp here as well. 35 00:02:51,230 --> 00:03:00,500 Here we're going to add the print where we're going to add print function in this main dot CPP file. 36 00:03:00,500 --> 00:03:06,260 So we have to include the firstly, we need to include the version here. 37 00:03:06,560 --> 00:03:08,060 Version dot header file. 38 00:03:08,060 --> 00:03:11,240 As you can see, it comes automatically. 39 00:03:11,240 --> 00:03:17,190 And then we let's define the print print function here. 40 00:03:17,210 --> 00:03:20,150 Void print version. 41 00:03:20,850 --> 00:03:21,330 Here. 42 00:03:24,350 --> 00:03:27,710 And let's use the braces here. 43 00:03:28,630 --> 00:03:29,120 STD. 44 00:03:30,320 --> 00:03:34,070 Or actually, let's use the print function here instead of STD. 45 00:03:34,670 --> 00:03:35,870 Printf. 46 00:03:35,900 --> 00:03:37,010 Printf. 47 00:03:37,910 --> 00:03:41,030 Okay, so version equals. 48 00:03:42,180 --> 00:03:43,700 Is equals to. 49 00:03:45,710 --> 00:03:49,860 Uh, the decimal, because we will get decimal here. 50 00:03:50,260 --> 00:03:52,650 And then here version. 51 00:03:53,620 --> 00:04:00,050 As you can see, we use this double dots here. 52 00:04:00,070 --> 00:04:02,740 As you can see here, because. 53 00:04:03,440 --> 00:04:10,910 We included the version that H mentioned, that header file and in version that header file we have 54 00:04:10,910 --> 00:04:17,150 an external integer version variable which I will explain what these are. 55 00:04:17,180 --> 00:04:29,450 So the code here as as you see here, has two source files, the version CPP and main dot CPP here. 56 00:04:29,450 --> 00:04:36,080 And we have also we also we have a header file as you know, also we have a header file included by 57 00:04:36,080 --> 00:04:38,120 both source files here. 58 00:04:38,150 --> 00:04:46,700 As you can see here in version of CPP, it's included version header file and in also in Maine dot CPP, 59 00:04:46,850 --> 00:04:50,870 we have the included version dot h file here. 60 00:04:50,870 --> 00:04:51,860 So. 61 00:04:53,190 --> 00:04:53,700 Um. 62 00:04:56,310 --> 00:04:58,350 So here the header file. 63 00:04:58,620 --> 00:05:06,810 A header file declares the global variable version, which can be used by both source files so it declares 64 00:05:06,810 --> 00:05:09,640 the variable but does not define it here. 65 00:05:09,660 --> 00:05:12,870 As you can see, we defined it here. 66 00:05:12,870 --> 00:05:21,360 We defined version as 17, so the actual variable is defined and initialized as you can see here here. 67 00:05:21,390 --> 00:05:26,730 So it is here that the compiler will allocate memory for the variable. 68 00:05:26,730 --> 00:05:34,770 So the extend keyword here used on the declaration in the header in the case to the compiler that the 69 00:05:34,770 --> 00:05:38,280 version has external linkage. 70 00:05:38,280 --> 00:05:39,750 So let's let me write it. 71 00:05:39,750 --> 00:05:48,990 So when you download this code you will see and read this and remind us the extern here means external 72 00:05:48,990 --> 00:05:51,240 external linkage. 73 00:05:52,730 --> 00:06:00,830 Linkage here, and that is the name is visible in files other than where the variables is defined. 74 00:06:00,830 --> 00:06:06,860 So the version variable is used in the main.cpp source file. 75 00:06:07,040 --> 00:06:11,030 So in this file, the scope resolution operator here, this is the scope resolution operator. 76 00:06:11,030 --> 00:06:16,670 As you can see, this is the two double dots here is used without a namespace name. 77 00:06:16,670 --> 00:06:20,150 As you can see in previous lecture, we actually used some namespace. 78 00:06:20,150 --> 00:06:29,840 For example, we did, for example, Revolver, and then we use this scope resolution operator here. 79 00:06:30,050 --> 00:06:32,390 So actually let me write the scope resolution. 80 00:06:32,390 --> 00:06:32,810 Operator. 81 00:06:32,810 --> 00:06:36,800 This means the scope resolution. 82 00:06:37,550 --> 00:06:45,980 Operator And I want to also note this extern terminology. 83 00:06:45,980 --> 00:06:54,300 So actually this terminology is matters because you will use them a lot if you do the recording programming 84 00:06:54,300 --> 00:07:00,900 or just any C plus plus programming, you will use this external linkage and scope resolution. 85 00:07:00,900 --> 00:07:06,990 Operator So of course there is a programs, the simple programs that don't use these operators here 86 00:07:06,990 --> 00:07:07,950 or keywords. 87 00:07:07,950 --> 00:07:16,290 So you can also declare items that will only be used within the current translation unit by declaring 88 00:07:16,290 --> 00:07:19,110 them within the source file before they are used. 89 00:07:19,110 --> 00:07:21,570 So usually here top on the file. 90 00:07:22,080 --> 00:07:30,180 So this produce a level of modularity and allows you to hide implementation details details from code 91 00:07:30,180 --> 00:07:31,170 in other source files. 92 00:07:31,170 --> 00:07:38,670 So for example, let's create an, let's write our um, header file here. 93 00:07:40,150 --> 00:07:41,380 Actually, let's create a. 94 00:07:44,750 --> 00:07:45,080 Um. 95 00:07:46,940 --> 00:07:48,650 Main here. 96 00:07:49,580 --> 00:07:50,840 So. 97 00:07:52,090 --> 00:07:56,540 Oxley and lets in main dot header file. 98 00:07:56,560 --> 00:08:00,310 Here we will declare the void usage. 99 00:08:00,640 --> 00:08:03,430 Here usage function just a function. 100 00:08:03,430 --> 00:08:06,400 We will not write a code in it yet. 101 00:08:06,940 --> 00:08:08,190 And in print. 102 00:08:08,260 --> 00:08:08,980 In here. 103 00:08:08,980 --> 00:08:10,120 Main.cpp. 104 00:08:10,960 --> 00:08:18,550 We're going to include the version age as we did earlier, and then we will use this standard. 105 00:08:18,730 --> 00:08:21,340 We will use string from the standard library. 106 00:08:23,020 --> 00:08:24,070 String. 107 00:08:25,670 --> 00:08:26,860 Application name here. 108 00:08:26,870 --> 00:08:30,710 For example, application name, for example. 109 00:08:30,710 --> 00:08:31,700 Port scanner. 110 00:08:34,730 --> 00:08:41,870 And here we have an app name variable string called app name here, which is this is a string. 111 00:08:41,870 --> 00:08:46,100 And inside this string we have a port scanner string here. 112 00:08:46,100 --> 00:08:54,200 So as you know, in C plus plus string in actually in a lot of language strings is just of arrays of 113 00:08:54,470 --> 00:08:59,780 arrays of characters here, which you will learn about these strings later also. 114 00:09:00,620 --> 00:09:05,420 So then let's use our as you can see, we have a print function here. 115 00:09:06,830 --> 00:09:08,030 Uh, and. 116 00:09:09,160 --> 00:09:12,400 We will use we will define usage function here also. 117 00:09:12,700 --> 00:09:22,450 Actually, let's include our main that include our include main header file here and define usage. 118 00:09:24,560 --> 00:09:25,640 Avoid usage. 119 00:09:27,120 --> 00:09:29,130 Here and. 120 00:09:30,600 --> 00:09:31,050 STD. 121 00:09:33,090 --> 00:09:34,110 See out. 122 00:09:35,540 --> 00:09:37,370 Application name. 123 00:09:38,310 --> 00:09:38,730 Here. 124 00:09:39,520 --> 00:09:47,410 Actually, we have to define out of this main function here, application name and then here. 125 00:09:48,990 --> 00:09:52,130 And then let's we will call the print function here. 126 00:09:52,140 --> 00:09:54,360 Print function. 127 00:09:54,360 --> 00:09:55,650 Print version. 128 00:09:56,550 --> 00:09:57,150 Here. 129 00:10:00,730 --> 00:10:01,510 And. 130 00:10:02,960 --> 00:10:10,720 And so the print that the main dot header file here contains the interface for the code in the main 131 00:10:10,720 --> 00:10:11,680 dot CPP file. 132 00:10:11,680 --> 00:10:13,810 As you can see, we use this usage. 133 00:10:13,810 --> 00:10:19,720 So only those functions declared in the header file will be callable by the other source file. 134 00:10:19,720 --> 00:10:24,070 So the color does not need to know about the implementation of the usage function. 135 00:10:24,160 --> 00:10:30,820 So and as you can see here, it's implemented using a call to a function called print version. 136 00:10:30,820 --> 00:10:36,040 So that is only available to main dot cplusplus file. 137 00:10:36,040 --> 00:10:45,460 So the variable application name is declared at a file scope, so it will only accessible to code in 138 00:10:45,460 --> 00:10:46,870 main.cpp. 139 00:10:47,080 --> 00:10:55,540 So if another source file declares a variable at file scope that is called the application name and 140 00:10:55,540 --> 00:10:57,670 is also a standard string here. 141 00:10:57,670 --> 00:10:59,320 So this is the string here. 142 00:10:59,380 --> 00:11:00,670 As you can see here. 143 00:11:01,750 --> 00:11:08,840 Um, the file will compile, but the linker will complain when it tries to link the object file. 144 00:11:08,840 --> 00:11:15,290 So the reason is that linker will see the same variable defined in two places and it will not know which 145 00:11:15,290 --> 00:11:16,610 one you to use. 146 00:11:16,610 --> 00:11:19,310 So a function also defines a scope. 147 00:11:19,310 --> 00:11:25,910 Variables defined within a function can only be accessed through the name, so the parameters of the 148 00:11:25,910 --> 00:11:29,870 function are also included as a variable within the function. 149 00:11:29,870 --> 00:11:35,180 So when you use when you declare the variables, you have to use different names. 150 00:11:35,180 --> 00:11:42,860 So if a parameter is not marked as a constant here const keyword as you remember, if your parameter 151 00:11:42,860 --> 00:11:49,400 is not marked as constant, then you can alter the value of of the parameter in your function. 152 00:11:49,400 --> 00:11:56,360 So you can declare the variables anywhere within a function as long as you declare them before you use 153 00:11:56,360 --> 00:11:56,870 them. 154 00:11:56,870 --> 00:12:03,200 Here, curly braces are used to define the code blocks and they also define local scopes. 155 00:12:03,200 --> 00:12:09,290 So if you declare a variable within the code block, then you can only use it there. 156 00:12:09,290 --> 00:12:16,520 So this means that you can declare variables within the same name outside of the code blocks, and the 157 00:12:16,520 --> 00:12:21,890 compiler will use the variable clauses to the scope it is accessed. 158 00:12:21,890 --> 00:12:30,620 So before finishing this lecture, it's important to mention one aspect of the C plus plus storage class 159 00:12:30,620 --> 00:12:31,310 here. 160 00:12:31,340 --> 00:12:38,600 A variable declared in function means that the compiler will allocate memory for the variable on the 161 00:12:38,600 --> 00:12:40,490 stack frame created for the function. 162 00:12:40,490 --> 00:12:47,030 So when the function finishes, the stack frame is torn down and memory is recycled here. 163 00:12:47,030 --> 00:12:55,370 So this means that after a function returns, the values in any local variables are lost completely. 164 00:12:55,370 --> 00:13:02,390 So when the function is called again, the variable is created a new and initialized again. 165 00:13:05,260 --> 00:13:11,890 Also in this lecture I want to talk about actually, yeah, in this lecture I want to talk about the 166 00:13:11,890 --> 00:13:15,010 C plus plus static keyword. 167 00:13:15,460 --> 00:13:22,180 So but in C plus plus here, static keyword change this behavior. 168 00:13:22,180 --> 00:13:29,800 So the static keyword means that the variables is allocated when the program starts, just like variables 169 00:13:29,800 --> 00:13:31,930 declared at a global scope. 170 00:13:31,930 --> 00:13:37,720 So applying static to a variable declared in a function means that the variable has internal linkage. 171 00:13:37,720 --> 00:13:42,640 So that is, the compiler restricts access to the variable to that function. 172 00:13:42,640 --> 00:13:46,870 So now I want to create an example code here, for example. 173 00:13:47,290 --> 00:13:47,620 Um. 174 00:13:49,340 --> 00:13:55,790 Know before this main function integer increment here in increment and. 175 00:13:56,730 --> 00:14:01,500 And then let's create a static static integer value. 176 00:14:02,460 --> 00:14:05,250 Value increment by one. 177 00:14:06,460 --> 00:14:08,260 Increment by one 178 00:14:10,540 --> 00:14:15,280 by one here and return a value. 179 00:14:17,000 --> 00:14:17,990 Here. 180 00:14:17,990 --> 00:14:23,330 Let's use the now let's print our function here. 181 00:14:24,230 --> 00:14:27,340 Increment here and. 182 00:14:29,250 --> 00:14:31,620 So increment here we will use. 183 00:14:31,740 --> 00:14:33,210 As you can see, this is the increment. 184 00:14:33,210 --> 00:14:35,700 So we're going to pass one number as a parameter. 185 00:14:35,700 --> 00:14:44,010 For example, five here, or let's create an this function type lots of types, for example. 186 00:14:44,010 --> 00:14:45,810 Five nine. 187 00:14:47,690 --> 00:14:48,950 19. 188 00:14:50,520 --> 00:14:51,840 999. 189 00:14:53,310 --> 00:14:54,120 So. 190 00:14:55,470 --> 00:14:57,810 Uh, let's compile this application. 191 00:14:59,290 --> 00:15:06,400 Actually let's create this application here project as a new project. 192 00:15:07,530 --> 00:15:13,620 Because I don't want to with these codes, because I want to share it with you as a file. 193 00:15:16,520 --> 00:15:21,080 Here and we're going to write the increment function here. 194 00:15:23,280 --> 00:15:23,790 Integer. 195 00:15:23,790 --> 00:15:25,320 Increment integer. 196 00:15:25,560 --> 00:15:26,340 Here. 197 00:15:29,880 --> 00:15:32,550 And static integer value. 198 00:15:35,350 --> 00:15:38,950 Value plus equals. 199 00:15:40,400 --> 00:15:43,880 E here and we will return the value. 200 00:15:46,330 --> 00:15:47,020 Here. 201 00:15:48,730 --> 00:15:52,660 So why we have a yeah, we have to add here. 202 00:15:53,110 --> 00:15:55,720 So then let's compile our code. 203 00:15:56,740 --> 00:15:58,870 Actually, let's add a new lines here. 204 00:16:00,830 --> 00:16:06,800 Because the cars here new lines so. 205 00:16:08,530 --> 00:16:09,370 By default. 206 00:16:09,370 --> 00:16:14,890 Here the compiler will initialize a static variable. 207 00:16:17,080 --> 00:16:17,950 Two zero. 208 00:16:18,750 --> 00:16:26,700 So but you can provide an initialization value and this will be used when the variable at first allocated. 209 00:16:26,700 --> 00:16:34,710 So when this program starts, the value variable will be initialized to zero before the main function 210 00:16:34,710 --> 00:16:35,070 called. 211 00:16:35,070 --> 00:16:42,680 So the first time the increment function is called the value variable incremented to ten, you know, 212 00:16:42,690 --> 00:16:47,550 to five here, which is returned by the function and printed to the console. 213 00:16:47,550 --> 00:16:54,990 And when the increment function returns, the value value variable is retained so that when the increment 214 00:16:54,990 --> 00:16:58,290 function is called again, the value variable incremented by. 215 00:17:00,770 --> 00:17:01,460 Nine here. 216 00:17:01,460 --> 00:17:05,360 As you can see, five plus nine equals 14. 217 00:17:05,390 --> 00:17:07,250 Actually, let's create a calculator. 218 00:17:07,250 --> 00:17:07,790 So. 219 00:17:09,740 --> 00:17:13,010 Let's create an example without static. 220 00:17:15,830 --> 00:17:16,160 Here. 221 00:17:16,190 --> 00:17:18,990 This is a static and this is not static here. 222 00:17:19,010 --> 00:17:21,500 So and now I want to. 223 00:17:23,320 --> 00:17:24,310 Notepad. 224 00:17:24,490 --> 00:17:25,630 Notepad. 225 00:17:26,490 --> 00:17:26,820 Here. 226 00:17:28,310 --> 00:17:30,440 With static. 227 00:17:32,340 --> 00:17:32,760 Here. 228 00:17:33,770 --> 00:17:35,360 Or without. 229 00:17:35,570 --> 00:17:38,090 Without static. 230 00:17:39,520 --> 00:17:40,930 And with static. 231 00:17:42,900 --> 00:17:45,090 With static here. 232 00:17:45,900 --> 00:17:51,960 And let's create, let's add static keyword and start it again. 233 00:17:55,910 --> 00:17:58,430 As you can see here, we got the different result. 234 00:18:01,310 --> 00:18:02,060 So. 235 00:18:07,370 --> 00:18:07,760 Here. 236 00:18:10,570 --> 00:18:14,890 So without static, our value here. 237 00:18:17,160 --> 00:18:21,690 Ursus itself and just does nothing. 238 00:18:21,960 --> 00:18:29,970 But here, by default, the compiler will initialize the static variable to zero, but you can provide 239 00:18:29,970 --> 00:18:31,350 an initialization value here. 240 00:18:31,350 --> 00:18:35,190 So and this will be used when the variable is first located. 241 00:18:35,190 --> 00:18:38,460 So when this program starts the value. 242 00:18:39,540 --> 00:18:46,080 A variable we will initialize to zero and is incremented. 243 00:18:46,710 --> 00:18:53,940 To increment the to five which is returned by the function and printed to the console. 244 00:18:55,200 --> 00:18:55,770 Here. 245 00:18:55,770 --> 00:18:56,670 So. 246 00:18:58,430 --> 00:19:01,910 When the INC function returns the value. 247 00:19:03,990 --> 00:19:08,580 Variable is returned so that the vendor Inc function is called again. 248 00:19:08,610 --> 00:19:19,920 The value variable is incremented by five by nine and total number is going to be five plus nine equals 249 00:19:19,920 --> 00:19:21,740 four teen. 250 00:19:21,930 --> 00:19:30,480 So it might be confusing at first, but I promise you will learn this constant and static variables 251 00:19:30,480 --> 00:19:32,300 and keywords in later lectures. 252 00:19:32,310 --> 00:19:37,050 This is just the beginning of the C plus plus files, but no. 253 00:19:37,050 --> 00:19:38,360 And you can do it. 254 00:19:38,370 --> 00:19:43,740 So in next lecture we will talk about the operators and I'm waiting you in the next lecture. 255 00:19:43,740 --> 00:19:51,090 So I know this, this lecture is actually almost 20 minutes, so I hope you watched all of this and 256 00:19:51,090 --> 00:19:52,800 I'm waiting you in the next lecture. 257 00:19:53,040 --> 00:19:56,970 This is just the beginning or intermediate C plus plus programming. 258 00:19:56,970 --> 00:19:58,680 So after. 259 00:19:59,630 --> 00:20:08,120 Watching and including your my lectures in your practically you will learn about the cplusplus a lot. 260 00:20:08,150 --> 00:20:10,220 So I'm waiting you in the next lecture.