1 00:00:00,630 --> 00:00:07,080 In some cases you will want to provide constant values that can be used throughout your code. 2 00:00:07,110 --> 00:00:10,890 For example, you may decide to declare a constant for P. 3 00:00:10,920 --> 00:00:17,200 You shall not allow this value to be changed because it will change the underlying logic code. 4 00:00:17,220 --> 00:00:22,290 So this means that you should mark the variable as being constant. 5 00:00:22,320 --> 00:00:29,970 So when you do this, the compiler will check the use of the variable and if it is used in the code, 6 00:00:29,970 --> 00:00:32,010 that changes the value of the variable. 7 00:00:32,040 --> 00:00:35,690 The compiler will usually an error. 8 00:00:35,700 --> 00:00:37,950 So let's create a constant variable here. 9 00:00:38,790 --> 00:00:40,250 Constant double. 10 00:00:41,310 --> 00:00:41,910 Yes. 11 00:00:42,120 --> 00:00:45,120 3.14 and 15. 12 00:00:45,240 --> 00:00:47,940 And then double radius. 13 00:00:48,090 --> 00:00:51,780 Double radius equals 5.0. 14 00:00:53,540 --> 00:00:58,520 He has signed a 5.0 and double circumvents. 15 00:01:01,370 --> 00:01:08,420 And to multiply VIP android is here. 16 00:01:08,420 --> 00:01:14,740 So in this case the symbol P here is declared as being constant. 17 00:01:14,750 --> 00:01:19,470 So it cannot change if you subsequently decide to change the constant. 18 00:01:19,490 --> 00:01:23,020 The compiler will usually an error. 19 00:01:23,030 --> 00:01:28,480 So once you declare the constant, you can assure that the compiler will make sure it remains true. 20 00:01:28,490 --> 00:01:34,840 So you can assign the constant with expression as well. 21 00:01:34,850 --> 00:01:46,880 So for example, let's include include C might include k, C, math, C math error, and then let's 22 00:01:46,940 --> 00:01:50,240 make a constant. 23 00:01:51,660 --> 00:01:52,470 Double. 24 00:01:54,220 --> 00:01:56,500 Of two equals. 25 00:01:57,500 --> 00:02:07,270 Constant double score of two equals, STB, score T and two here. 26 00:02:07,280 --> 00:02:09,110 So now I want to change. 27 00:02:09,170 --> 00:02:11,510 For example, as you can see, this is the constant. 28 00:02:11,510 --> 00:02:18,470 We cannot change it, but let's try if we can change our constant value. 29 00:02:18,470 --> 00:02:22,820 So let's assign P to, for example, 99. 30 00:02:23,920 --> 00:02:32,700 As you can see, we got an error cannot assign to variable P with the course qualified type constable. 31 00:02:32,710 --> 00:02:40,720 So in order to work this code, we can as you can see here const we have to delete the cost and now 32 00:02:40,720 --> 00:02:44,620 we can assign P on some variable here. 33 00:02:44,620 --> 00:02:48,580 So let's run our code and see what error we will get. 34 00:02:48,610 --> 00:02:52,510 Error assignment of read only variable p. 35 00:02:52,510 --> 00:03:00,010 As you can see here, we got an error in 12 line P 99. 36 00:03:00,970 --> 00:03:01,380 Here. 37 00:03:01,390 --> 00:03:02,290 So. 38 00:03:03,880 --> 00:03:06,310 Now, as I can show you. 39 00:03:06,310 --> 00:03:07,630 Or we can delete this. 40 00:03:07,720 --> 00:03:11,720 Clear this and with the latest line of code as well. 41 00:03:11,740 --> 00:03:19,030 So in this code, as you can see, we got a constant we created a new double constant skirt of two and 42 00:03:19,030 --> 00:03:21,340 we use the this seam at. 43 00:03:22,130 --> 00:03:25,700 The library, which you will learn more next lectures. 44 00:03:25,880 --> 00:03:32,980 So you can also use constant to declare a constant that will be used as a constant expression for. 45 00:03:32,990 --> 00:03:38,810 So for example, you can declare an array using the square breaks Brexit syntax. 46 00:03:39,470 --> 00:03:41,810 For example, let's create an array here. 47 00:03:45,450 --> 00:03:47,370 Integer values. 48 00:03:47,670 --> 00:03:57,070 Five This is an array, so this declares an array of five integers and the stack and its items are accessed 49 00:03:57,070 --> 00:03:59,040 through the values of a variable. 50 00:03:59,040 --> 00:04:02,340 So the five is here is constant expression. 51 00:04:02,340 --> 00:04:08,430 So when you declare an array on the stack, you have to provide the compiler with a constant expression 52 00:04:08,430 --> 00:04:12,000 so it knows how much memory to allocate. 53 00:04:12,000 --> 00:04:17,820 And this means that the size of the array must be known at compile time so you can allocate an array 54 00:04:17,820 --> 00:04:19,830 with this size known only at runtime. 55 00:04:19,830 --> 00:04:26,310 But this requires dynamic memory allocation, which we will explain in next lecture. 56 00:04:26,910 --> 00:04:34,620 Lex The sections in section named working with memory pointers and arrays. 57 00:04:34,620 --> 00:04:39,530 So in C++ you can declare the constant like this too. 58 00:04:39,540 --> 00:04:51,990 For example, constant integer size equals five integer my new values and pass this as parameter size 59 00:04:51,990 --> 00:04:52,440 here. 60 00:04:52,440 --> 00:04:54,990 So we pass this size. 61 00:04:55,080 --> 00:04:58,260 And as you can see, we all size equals to five. 62 00:04:58,260 --> 00:05:03,630 So these and this line of code is the same, just a different variable names. 63 00:05:03,780 --> 00:05:09,810 So elsewhere in your code, when you access the values array, you can use the size constant to make 64 00:05:09,810 --> 00:05:18,360 sure that you do not don't access items past end of the day, so synthesised variable is declared in 65 00:05:18,360 --> 00:05:19,530 just one place. 66 00:05:19,530 --> 00:05:25,800 You will need to change the size of the array at the later stage, so you have just one place to make 67 00:05:25,800 --> 00:05:26,790 this change. 68 00:05:27,120 --> 00:05:36,180 Also, the keyword here can also be used two pointers and references which you will learn in the next 69 00:05:36,180 --> 00:05:39,420 sections and also on the objects. 70 00:05:39,750 --> 00:05:44,040 Often you will see used on the parameters to functions. 71 00:05:44,040 --> 00:05:53,280 So this is used to get the compiler to help ensure that pointers, references and objects are used appropriately 72 00:05:53,280 --> 00:05:54,750 as you intended.