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