1 00:00:01,430 --> 00:00:02,600 Initializers. 2 00:00:02,630 --> 00:00:09,260 Initializers were touched upon in the last lectures, but we will go into more depth here. 3 00:00:09,260 --> 00:00:14,720 So for built in types you must initialize a variable before you use it. 4 00:00:14,720 --> 00:00:20,960 So for custom types it is possible for the type to define a default value. 5 00:00:20,960 --> 00:00:28,340 But there are some uses in doing this which will be covered in next lecture, which in next chapters. 6 00:00:28,640 --> 00:00:30,140 Which in which. 7 00:00:30,470 --> 00:00:30,950 Yeah. 8 00:00:30,950 --> 00:00:36,590 In classes chapter of this Oxley Udemy course. 9 00:00:36,590 --> 00:00:42,560 So in all versions of Cplusplus there are three ways to initialize a built in type. 10 00:00:42,710 --> 00:00:48,800 The first here three ways to initialize. 11 00:00:51,220 --> 00:00:52,210 Boiled. 12 00:00:54,400 --> 00:00:55,360 Built in type. 13 00:00:55,600 --> 00:01:00,580 The first is assignment, which I will explain all of this. 14 00:01:00,730 --> 00:01:04,060 Assignment function. 15 00:01:04,950 --> 00:01:06,300 Syntax. 16 00:01:09,140 --> 00:01:12,290 Thanks for calling. 17 00:01:12,530 --> 00:01:14,390 A constructor. 18 00:01:16,460 --> 00:01:16,850 Stop. 19 00:01:19,530 --> 00:01:20,220 Here. 20 00:01:20,250 --> 00:01:23,100 Now let's try. 21 00:01:24,030 --> 00:01:29,430 Firstly, try with assignment, then function, then syntax, and then call. 22 00:01:29,540 --> 00:01:33,180 A Actually, it's a four ways, so I'm sorry for it. 23 00:01:33,210 --> 00:01:37,560 It's a four ways for calling INITIALIZERS. 24 00:01:37,560 --> 00:01:41,060 So the C plus 11. 25 00:01:41,070 --> 00:01:46,800 Another way to initialize variables was introduced construction through a list initializer. 26 00:01:46,800 --> 00:01:50,090 So there are four types here, as I said. 27 00:01:50,100 --> 00:02:04,260 So let's firstly create our in our code and write our codes and use for Initializers here includes standard 28 00:02:04,530 --> 00:02:06,360 steel here just for. 29 00:02:07,520 --> 00:02:08,330 Putting it here. 30 00:02:08,330 --> 00:02:10,700 So integer Actually we didn't need here. 31 00:02:10,700 --> 00:02:11,990 I just wrote it. 32 00:02:13,310 --> 00:02:14,210 Integer. 33 00:02:14,980 --> 00:02:22,090 Key here, Integer G equals integer Q which yeah, yeah. 34 00:02:22,090 --> 00:02:26,530 It's it's, it shows like pretty strange. 35 00:02:26,530 --> 00:02:29,890 But I will explain all of this don't worry. 36 00:02:30,250 --> 00:02:32,200 Integer key here. 37 00:02:32,800 --> 00:02:47,410 Actually, let's make this A, B, C, integer C three or like this three and then integer M four. 38 00:02:48,400 --> 00:02:50,780 Integer D four. 39 00:02:51,550 --> 00:03:00,580 So as you can see here, we did we initialized for four variables to four integers. 40 00:03:00,580 --> 00:03:05,260 So the first of these three is the clearest type. 41 00:03:05,260 --> 00:03:13,180 So it shows using an easy to understand syntax that the variable is being initialized to a value. 42 00:03:13,690 --> 00:03:22,630 So the second example here initialize a variable by calling the type as if it is a function. 43 00:03:22,780 --> 00:03:27,760 So as you can see, the second is a function calling it by function. 44 00:03:27,760 --> 00:03:33,070 And the third example calls the constructor of the integer type. 45 00:03:33,070 --> 00:03:38,020 So this is a typical way to initialize custom types, so it's best to reserve the syntax just for custom 46 00:03:38,050 --> 00:03:38,560 types. 47 00:03:38,560 --> 00:03:44,590 So and the fourth here, as you can see here, syntax is new here. 48 00:03:44,600 --> 00:03:46,900 Actually, let me write it here. 49 00:03:47,520 --> 00:03:52,440 A new new to C plus plus. 50 00:03:52,450 --> 00:03:56,650 So this is the kind of new to C plus plus. 51 00:03:56,650 --> 00:03:59,830 It actually came with C plus plus 11. 52 00:03:59,830 --> 00:04:08,740 And initialize initializes the variable using an initialized list between the curly braces here, as 53 00:04:08,740 --> 00:04:10,000 you can see here. 54 00:04:11,140 --> 00:04:18,130 And just to confuse things slightly, you can also initialize a built in type using the same syntax 55 00:04:18,130 --> 00:04:22,030 as an assignment to a single to a single item list. 56 00:04:22,030 --> 00:04:29,620 For example, integer E here equals equal to parameter here and five like this. 57 00:04:29,620 --> 00:04:33,160 So you can assign this operator like this to. 58 00:04:33,550 --> 00:04:40,930 So now I want to print these variables just in case to show that these variables works and you can print 59 00:04:40,930 --> 00:04:42,580 these values to screen. 60 00:04:42,580 --> 00:04:46,300 So let's print, print f. 61 00:04:47,740 --> 00:04:48,010 Uh. 62 00:04:48,010 --> 00:04:48,400 Okay. 63 00:04:48,400 --> 00:04:49,060 Here. 64 00:04:50,240 --> 00:04:53,330 Some decimal new line. 65 00:04:53,360 --> 00:04:54,590 Decimal. 66 00:04:54,860 --> 00:04:55,940 New line. 67 00:04:56,090 --> 00:04:59,060 Decimal and new line. 68 00:04:59,960 --> 00:05:00,740 New line. 69 00:05:00,740 --> 00:05:01,550 Lastly. 70 00:05:01,580 --> 00:05:04,250 Actually, let's add five decimal here. 71 00:05:05,530 --> 00:05:07,000 Um, actually. 72 00:05:08,540 --> 00:05:13,100 Same with the set here. 73 00:05:14,270 --> 00:05:16,160 And new variable. 74 00:05:16,160 --> 00:05:17,180 One, two, three, four. 75 00:05:18,040 --> 00:05:18,360 A fight. 76 00:05:18,550 --> 00:05:18,770 Sure. 77 00:05:19,120 --> 00:05:21,670 And then add these variables. 78 00:05:22,550 --> 00:05:23,990 E equals. 79 00:05:25,190 --> 00:05:26,510 Because. 80 00:05:27,700 --> 00:05:29,320 C equals. 81 00:05:30,150 --> 00:05:31,230 Equality. 82 00:05:31,980 --> 00:05:33,390 The equality. 83 00:05:35,040 --> 00:05:37,260 Uh, and e equal. 84 00:05:40,030 --> 00:05:40,260 Yeah. 85 00:05:41,090 --> 00:05:44,830 And then he picked. 86 00:05:46,900 --> 00:05:51,580 And actually I forgot to write main function. 87 00:05:52,030 --> 00:05:55,120 The integer main here. 88 00:05:55,420 --> 00:05:56,440 And then. 89 00:05:57,840 --> 00:06:00,840 So actually it's the line. 90 00:06:01,200 --> 00:06:04,560 The writing these things is pretty easy. 91 00:06:04,560 --> 00:06:13,110 So as you can see here, it added one tab between the this last line automatically. 92 00:06:13,110 --> 00:06:16,890 So now let's print our function actually. 93 00:06:18,590 --> 00:06:21,950 Take this variables to. 94 00:06:23,630 --> 00:06:25,460 Main in main function here. 95 00:06:25,490 --> 00:06:26,060 Oops. 96 00:06:26,690 --> 00:06:27,560 By the doggo. 97 00:06:28,260 --> 00:06:28,760 Okay. 98 00:06:30,160 --> 00:06:34,360 Take this to main function and compile and run our application. 99 00:06:34,360 --> 00:06:42,010 So as you can see here, we initialize with the first type, assign assignment type and print it to. 100 00:06:43,530 --> 00:06:47,970 Uh, printed to console here command line. 101 00:06:47,970 --> 00:06:54,570 And then we did the function B2, C3, D4. 102 00:06:54,570 --> 00:07:01,650 And these are the same actually with syntax calling, calling a constructor type, which is a kind of 103 00:07:01,650 --> 00:07:05,280 new to C plus plus language. 104 00:07:05,670 --> 00:07:08,310 And I want to show you another thing. 105 00:07:08,310 --> 00:07:13,830 So just initializing the arrays here. 106 00:07:14,130 --> 00:07:16,110 So actually. 107 00:07:16,930 --> 00:07:18,700 Initializing a race. 108 00:07:20,690 --> 00:07:21,710 Initializing. 109 00:07:24,970 --> 00:07:27,000 Actually, let's print f. 110 00:07:29,340 --> 00:07:31,650 Initializing types. 111 00:07:32,190 --> 00:07:33,150 Initializing? 112 00:07:33,180 --> 00:07:33,750 Yeah. 113 00:07:33,780 --> 00:07:34,530 Types. 114 00:07:34,860 --> 00:07:35,850 And then. 115 00:07:37,330 --> 00:07:39,480 And just print. 116 00:07:39,490 --> 00:07:40,590 Print f. 117 00:07:42,270 --> 00:07:43,950 Initialize. 118 00:07:48,540 --> 00:07:51,830 Race and let's create our array. 119 00:07:51,830 --> 00:07:54,860 So this is a really confusing things. 120 00:07:54,860 --> 00:08:01,910 So the type here, the type E here is an integer but not an array. 121 00:08:01,910 --> 00:08:06,200 So as you know, we can write arrays like this, for example. 122 00:08:06,200 --> 00:08:07,010 Integer. 123 00:08:07,040 --> 00:08:15,020 Integer birth year, for example, birth years, and then for example. 124 00:08:20,110 --> 00:08:21,190 11. 125 00:08:24,250 --> 00:08:27,400 For example, like this, we can with this curly braces. 126 00:08:28,060 --> 00:08:32,950 We always did just creating great things, as you can see. 127 00:08:32,950 --> 00:08:37,120 But here we're assigning something just a one variable to. 128 00:08:39,560 --> 00:08:40,190 Integers. 129 00:08:40,190 --> 00:08:41,990 So this is kind of confusing. 130 00:08:41,990 --> 00:08:42,700 I know. 131 00:08:42,710 --> 00:08:50,240 So just to confuse things slightly, you can also initialize a built in type using the same syntax here 132 00:08:50,240 --> 00:08:53,450 as an assignment to a single item list. 133 00:08:53,900 --> 00:09:00,800 So recall that in the last lecture we created an array with the birthdates. 134 00:09:00,800 --> 00:09:02,600 And I did this. 135 00:09:02,690 --> 00:09:05,000 I wrote this code again. 136 00:09:05,000 --> 00:09:10,400 So this creates an array of four integers. 137 00:09:11,570 --> 00:09:17,540 So the type of each item is an integer, as you can see here. 138 00:09:17,540 --> 00:09:24,710 But the type of the array variable is integer pointer here, integer asterisk. 139 00:09:24,710 --> 00:09:29,180 So the variable points to memory that holds for integers. 140 00:09:29,180 --> 00:09:37,550 So similarly you can initialize also initialize a variable to be an array of one of them, for example. 141 00:09:37,550 --> 00:09:38,630 Integer John. 142 00:09:39,890 --> 00:09:41,540 Uh, and. 143 00:09:43,340 --> 00:09:44,720 For example, John. 144 00:09:45,080 --> 00:09:47,420 For example, let's write a Jones birthday. 145 00:09:48,890 --> 00:09:50,660 1988. 146 00:09:50,660 --> 00:09:54,350 And as you can see here, we did. 147 00:09:55,890 --> 00:10:03,990 Now create an just a single array so we can use it as the just usual integer here. 148 00:10:03,990 --> 00:10:08,970 So this is exactly the same initialize initialization code that C plus plus. 149 00:10:09,240 --> 00:10:10,860 C plus plus 11. 150 00:10:11,550 --> 00:10:15,540 C plus plus 11 allows to initialize a single integer. 151 00:10:15,540 --> 00:10:23,280 In addition, the same syntax is used to initialize instances of record types as you know, structs 152 00:10:23,820 --> 00:10:24,530 here. 153 00:10:24,540 --> 00:10:30,180 So adding another level of potential confusion about the what syntax means. 154 00:10:30,180 --> 00:10:42,000 So it's best to avoid using the curly braces for variable initialization and use it exclusively for 155 00:10:42,000 --> 00:10:44,190 initializing lists. 156 00:10:44,190 --> 00:10:46,740 So just avoid this. 157 00:10:46,740 --> 00:10:51,360 I just I just wanted to show you that C plus plus 11 has this feature. 158 00:10:51,360 --> 00:10:57,730 So however, there are advantages of this syntax for casting as explained shortly. 159 00:10:57,730 --> 00:11:05,380 So the curly brace syntax can use to provide the initial values for any of the collection classes in 160 00:11:05,410 --> 00:11:07,780 C plus plus standard library.