1 00:00:00,820 --> 00:00:02,740 Using types and variables. 2 00:00:02,740 --> 00:00:08,440 The types were covered in more detail in the next lecture, but it's useful to give a basic information 3 00:00:08,440 --> 00:00:08,770 here. 4 00:00:08,770 --> 00:00:13,690 So C++ is a strong type language as we mentioned earlier. 5 00:00:13,690 --> 00:00:18,550 So which means that you have to declare the type of the variable that you use. 6 00:00:18,910 --> 00:00:26,080 So the reason for this is that the compiler needs to know how much memory to allocate for the variable, 7 00:00:26,140 --> 00:00:29,920 and it can determine this by the type of the variable. 8 00:00:29,920 --> 00:00:38,650 So in addition to the compiler needs to know how to initialize a variable if it has not been explicitly 9 00:00:38,650 --> 00:00:39,550 initialized. 10 00:00:39,550 --> 00:00:46,300 And to perform this initialization, the compiler needs to know the type of the variable. 11 00:00:46,300 --> 00:00:52,660 So the C++ 11 provides with the auto keyword which you will learn. 12 00:00:52,660 --> 00:00:59,950 So which relaxes this concept of strong typing and it will be covered in the next chapters. 13 00:00:59,950 --> 00:01:06,760 So however, the type checking for the compiler is so important that you should use type checking as 14 00:01:06,760 --> 00:01:08,500 much as possible. 15 00:01:08,500 --> 00:01:15,610 So C++ variables can be declared anywhere in your code as long as they are declared before they are 16 00:01:15,610 --> 00:01:15,940 used. 17 00:01:15,940 --> 00:01:22,180 So where you declare a variable determines how you use it. 18 00:01:22,180 --> 00:01:25,660 So this is called the scope of variable. 19 00:01:25,750 --> 00:01:32,560 So in general it is best to declare the variable as close as possible to where you will use it. 20 00:01:32,560 --> 00:01:39,760 And within the most restrictive scope, this prevents name clashes where you will have to add additional 21 00:01:39,760 --> 00:01:44,560 information to disambiguate two or more variables. 22 00:01:44,560 --> 00:01:50,080 So you may and should give your variables descriptive names. 23 00:01:50,080 --> 00:01:55,000 So this makes your code much more readable and easier to understand. 24 00:01:55,000 --> 00:02:03,490 So C++ must start with a alphabetic character or an underscore so you can start. 25 00:02:03,580 --> 00:02:09,460 So for example, C++ start to have to start with alphabetic character or an underscore. 26 00:02:09,460 --> 00:02:12,820 So you can write variables like this in, for example, integer. 27 00:02:12,820 --> 00:02:14,770 My variable. 28 00:02:14,770 --> 00:02:18,580 And here, as you can see, there is no error at all. 29 00:02:18,580 --> 00:02:23,050 And you can write your variables by. 30 00:02:26,060 --> 00:02:28,360 Upper score variable. 31 00:02:28,370 --> 00:02:29,480 My variable. 32 00:02:30,420 --> 00:02:36,270 To underscore my very well. 33 00:02:38,850 --> 00:02:45,720 Come tool and into my. 34 00:02:46,940 --> 00:02:48,710 My variable. 35 00:02:50,280 --> 00:02:51,450 You're here. 36 00:02:52,020 --> 00:02:57,150 So as you can see here, these are names are the almost same, except there's an underscore here. 37 00:02:57,150 --> 00:02:58,910 But these three names are same. 38 00:02:58,920 --> 00:03:01,280 But as you can see, we didn't get an error. 39 00:03:01,290 --> 00:03:06,480 If you write this exactly like this underscores and upper scores like this, for example, you will 40 00:03:06,480 --> 00:03:09,300 get an error because of this redefinition. 41 00:03:09,300 --> 00:03:14,790 So we have to remove the previous declaration of a local variable, as C++ suggests. 42 00:03:14,790 --> 00:03:17,160 So let's compile it and see what happens. 43 00:03:17,160 --> 00:03:22,350 So radical version of an integer variable note here. 44 00:03:22,350 --> 00:03:25,050 Integer variable previously declared here. 45 00:03:25,080 --> 00:03:31,620 When we click on this here, as you can see, we can not declare the same names, but if we change this 46 00:03:32,490 --> 00:03:40,380 here, this alphabet here, we make it and make it underscore they are no longer the same variable here 47 00:03:40,380 --> 00:03:41,820 as let's try it. 48 00:03:41,820 --> 00:03:42,480 So. 49 00:03:44,690 --> 00:03:55,190 Here and the C++ names are case sensitive, as I said earlier, and the first 2080 50 00:03:56,300 --> 00:03:59,390 2048 characters are significant. 51 00:03:59,390 --> 00:04:05,240 So you can start variable name with an underscore, but you cannot use to underscore series. 52 00:04:05,270 --> 00:04:08,480 You can see here variable here. 53 00:04:11,050 --> 00:04:14,830 So no, you can use an underscore followed by a capital letter here. 54 00:04:18,450 --> 00:04:23,220 As you can see, no convertible is all assigned, but never accessed here. 55 00:04:24,390 --> 00:04:25,830 We got no error here. 56 00:04:26,010 --> 00:04:33,750 So and clearly you cannot use the type name as variable names, neither built in type names, for example, 57 00:04:34,140 --> 00:04:34,650 integer. 58 00:04:34,650 --> 00:04:43,770 So we can use the long as a variable name because it's a key word and it's reserved for its reserved 59 00:04:43,770 --> 00:04:44,300 keywords. 60 00:04:44,310 --> 00:04:50,730 So or we cannot use wild or other keywords in our language here, as you can see here. 61 00:04:51,510 --> 00:04:57,390 So you declare a variable in a statement and think with a semicolon here as you can see here. 62 00:04:57,390 --> 00:05:07,500 So the basic syntax of declaring variable is that you specify the type, then the name and optionally 63 00:05:07,500 --> 00:05:09,810 an initialization variable. 64 00:05:09,810 --> 00:05:16,170 Here this is a type name and initialization variable. 65 00:05:16,170 --> 00:05:20,640 So builtin types must be initialize before you use them. 66 00:05:20,640 --> 00:05:24,060 As you can see here integer e here. 67 00:05:24,870 --> 00:05:33,030 Let's increment e by one and here std C out and e here. 68 00:05:33,750 --> 00:05:40,350 And let's actually let's make a return statement here and let's run this program here. 69 00:05:40,350 --> 00:05:41,940 As you can see, we got an error. 70 00:05:41,940 --> 00:05:43,530 We got not error. 71 00:05:43,680 --> 00:05:44,430 This is incorrect. 72 00:05:45,870 --> 00:05:48,750 This has this program compiled successfully. 73 00:05:48,750 --> 00:05:51,150 And as you can see here, there is an output one. 74 00:05:51,150 --> 00:05:53,220 So this gave us output fun. 75 00:05:53,580 --> 00:06:02,070 This is the zero by default as you know earlier and we incremented increment by one here as you can 76 00:06:02,070 --> 00:06:02,340 see here. 77 00:06:02,340 --> 00:06:10,410 And we and so zero plus one equals one and we got this one here. 78 00:06:10,800 --> 00:06:16,200 So there are essentially three ways to initialize the variables. 79 00:06:17,010 --> 00:06:25,500 You can assign a value, you can call a, you can call the type constructor or you can initialize a 80 00:06:25,500 --> 00:06:27,540 variable using function syntax. 81 00:06:27,540 --> 00:06:33,750 By the way, you will learn constructors for classes in next lecture. 82 00:06:33,760 --> 00:06:42,780 So let's create an some program here, some syntax here, the code here A equals one. 83 00:06:42,910 --> 00:06:53,340 Assign it one integer, P integer two and integer C here three. 84 00:06:53,790 --> 00:06:57,300 So now I want to explain what these are. 85 00:06:57,330 --> 00:07:03,470 So obviously they are almost they they we can compile our program without a problem. 86 00:07:03,510 --> 00:07:10,380 Problem and actually E equals to one B equals to two and C equals to three. 87 00:07:10,380 --> 00:07:13,230 I will show you what these are. 88 00:07:13,260 --> 00:07:14,880 Here's a. 89 00:07:17,770 --> 00:07:19,810 So firstly a. 90 00:07:20,840 --> 00:07:22,730 Then the a. 91 00:07:22,790 --> 00:07:23,450 B. 92 00:07:25,650 --> 00:07:27,860 Okay, then. 93 00:07:28,950 --> 00:07:29,870 See? 94 00:07:33,720 --> 00:07:42,450 And yeah that's it and new line here so let's ABC and let's compile our program. 95 00:07:44,410 --> 00:07:45,040 UPS. 96 00:07:52,750 --> 00:07:53,260 Yeah. 97 00:07:54,410 --> 00:07:55,920 Let's delete this. 98 00:07:56,270 --> 00:07:56,510 A. 99 00:07:56,570 --> 00:07:56,850 B. 100 00:07:56,870 --> 00:07:57,620 C. 101 00:08:00,790 --> 00:08:01,240 There. 102 00:08:03,050 --> 00:08:11,990 As you can see here, we got this and there are three legal C++, but stylistically the first is the 103 00:08:11,990 --> 00:08:14,990 better because it is more obvious. 104 00:08:14,990 --> 00:08:16,850 So the variable is an integer. 105 00:08:16,850 --> 00:08:21,260 It's called a, and it's assigned to value one. 106 00:08:21,590 --> 00:08:23,540 The third looks confusing. 107 00:08:23,540 --> 00:08:30,830 As you can see here, it looks like the declaration of function when it is actually declaring variable. 108 00:08:32,240 --> 00:08:39,220 The next lecture will have a show of a variation of assigning a value using the initialization list 109 00:08:39,290 --> 00:08:39,920 syntax. 110 00:08:39,920 --> 00:08:48,440 So there is the reason why you will want to do this will be you will learn that in the next sections. 111 00:08:48,440 --> 00:08:49,130 So. 112 00:08:51,290 --> 00:08:57,350 And also you will learn classes, will cover classes, your own custom types. 113 00:08:57,350 --> 00:09:03,500 So a custom type may be defined to have a default value, which means that you will have you may decide 114 00:09:03,500 --> 00:09:07,430 not to initialize a variable of a custom type before using it. 115 00:09:07,430 --> 00:09:13,250 So however, this will result in a poorer performance because the compiler will initialize the variable 116 00:09:13,250 --> 00:09:20,420 with a default value and subsequently your code will assign a value, so resulting in a assign being 117 00:09:20,420 --> 00:09:23,060 performed twice here. 118 00:09:23,090 --> 00:09:28,820 So let's let's print this with here. 119 00:09:29,240 --> 00:09:30,170 Let's print this. 120 00:09:32,730 --> 00:09:33,930 Let's print this with. 121 00:09:35,640 --> 00:09:37,100 See our function here. 122 00:09:37,110 --> 00:09:38,130 See out. 123 00:09:40,100 --> 00:09:42,220 Single STDs out STDs. 124 00:09:42,230 --> 00:09:47,480 See out here and see here. 125 00:09:51,830 --> 00:09:59,500 As you can see here, if you delete this, see, as you can see, we got an error from a specified W 126 00:09:59,530 --> 00:09:59,980 chart. 127 00:10:01,520 --> 00:10:04,070 The argument has a typo in it here. 128 00:10:04,070 --> 00:10:08,150 As you can see here, our program is. 129 00:10:10,340 --> 00:10:14,970 Fakes steer and let's The lid is also here. 130 00:10:14,990 --> 00:10:15,980 Actually, no. 131 00:10:17,480 --> 00:10:17,960 Here. 132 00:10:18,830 --> 00:10:20,300 As you can see, we got. 133 00:10:20,660 --> 00:10:28,790 So I want to say why we got this hurt and this strange number. 134 00:10:28,820 --> 00:10:32,390 It's because of we need to. 135 00:10:33,860 --> 00:10:37,690 And as you can see there, the see here, that's the latest. 136 00:10:37,700 --> 00:10:42,260 And you will see our program has fixed the error here. 137 00:10:42,260 --> 00:10:44,780 We used see format specified. 138 00:10:44,810 --> 00:10:57,020 So if you put this operator before in print a function before any alphabet here, you this C will turn 139 00:10:57,020 --> 00:10:59,530 into just the sound format specified here. 140 00:10:59,540 --> 00:11:08,900 That's because you have to do the space here and our code will fix here, as you can see here, let's 141 00:11:09,050 --> 00:11:09,830 run it again. 142 00:11:09,830 --> 00:11:18,620 And as you can see, we got an A one, two, three and there is a three times three assignment ways 143 00:11:18,620 --> 00:11:19,820 in C++. 144 00:11:19,820 --> 00:11:22,100 So I'm waiting you in next lecture.