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