1
00:00:00,380 --> 00:00:06,920
In some cases, you will want to provide constant values that can be used throughout your code.

2
00:00:07,010 --> 00:00:10,550
For example, you may decide to declare a constant for P.

3
00:00:10,760 --> 00:00:17,160
You should not allow this value to be changed because it will change the underlying logic in your code.

4
00:00:17,180 --> 00:00:22,220
So this means that you should mark the variable as being constant.

5
00:00:22,220 --> 00:00:29,900
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,900 --> 00:00:35,590
that changes the value of the variable, the compiler will usually an error.

7
00:00:35,600 --> 00:00:37,910
So let's create a constant variable here.

8
00:00:38,540 --> 00:00:47,870
Constant double p here 3.14 and 15 and then double radius.

9
00:00:47,900 --> 00:00:51,680
Double radius equals 5.0.

10
00:00:53,470 --> 00:00:53,950
Here.

11
00:00:53,950 --> 00:00:58,300
Sign the 5.0 and double circumference.

12
00:01:01,300 --> 00:01:04,210
Learns to multiply by P.

13
00:01:05,800 --> 00:01:08,350
Android is here.

14
00:01:08,350 --> 00:01:16,570
So in this case the symbol P here is declared as being constant, so it cannot change.

15
00:01:16,810 --> 00:01:22,990
If you subsequently decide to change the constant, the compiler will usually an error.

16
00:01:22,990 --> 00:01:28,410
So once you declare the constant, you can be sure that the compiler will make sure it remains true.

17
00:01:28,420 --> 00:01:34,740
So you can assign the constant with expression as well.

18
00:01:34,750 --> 00:01:44,980
So for example, this could include cmath, include k, C math C math here.

19
00:01:44,980 --> 00:01:50,200
And then let's make a constant.

20
00:01:51,620 --> 00:01:52,250
Double.

21
00:01:53,670 --> 00:01:56,430
Set of two equals.

22
00:01:57,400 --> 00:02:04,090
Constant double square of two equals STD sqrt.

23
00:02:04,490 --> 00:02:07,210
And two here.

24
00:02:07,210 --> 00:02:09,070
So now I want to change.

25
00:02:09,070 --> 00:02:11,410
For example, as you can see, this is the constant.

26
00:02:11,440 --> 00:02:13,060
We cannot change it.

27
00:02:13,420 --> 00:02:18,430
But let's try if we can change our constant value.

28
00:02:18,430 --> 00:02:22,780
So let's assign P to, for example, 99.

29
00:02:23,820 --> 00:02:32,600
As you can see, we got an error cannot assign to variable P with the const qualified type constable.

30
00:02:32,610 --> 00:02:35,850
So in order to work this code, we can.

31
00:02:36,360 --> 00:02:44,580
As you can see here, const, we have to delete the const and now we can assign P on some variable here.

32
00:02:44,580 --> 00:02:48,480
So let's run our code and see what error we will get.

33
00:02:48,570 --> 00:02:52,410
Error assignment of read only variable P.

34
00:02:52,410 --> 00:03:00,000
As you can see here, we got an error in 12 line p 99.

35
00:03:00,810 --> 00:03:01,320
Here.

36
00:03:01,320 --> 00:03:02,190
So.

37
00:03:03,780 --> 00:03:11,630
Now, as I can show you, we can delete this, clear this, and let's delete this line of code as well.

38
00:03:11,640 --> 00:03:15,750
So in this code, as you can see, we got a constant.

39
00:03:15,750 --> 00:03:21,270
We created a new double constant square root of two and we used this cmath.

40
00:03:22,060 --> 00:03:25,690
The library, which you will learn more in next lectures.

41
00:03:25,690 --> 00:03:32,940
So you can also use Constant to declare a constant that will be used as a constant expression for.

42
00:03:32,950 --> 00:03:38,830
So for example, you can declare an array using the square bracket braces syntax.

43
00:03:39,310 --> 00:03:40,060
For example.

44
00:03:40,060 --> 00:03:41,770
Let's create an array here.

45
00:03:42,220 --> 00:03:42,650
Um.

46
00:03:43,150 --> 00:03:43,780
Okay.

47
00:03:45,170 --> 00:03:48,260
Integer values five.

48
00:03:48,290 --> 00:03:49,520
This is an array.

49
00:03:49,550 --> 00:03:58,010
So this declares an array of five integers and the stack and the items are accessed through the values

50
00:03:58,010 --> 00:03:58,970
array variable.

51
00:03:58,970 --> 00:04:02,270
So the five is here is constant expression.

52
00:04:02,270 --> 00:04:08,390
So when you declare an array on the stack, you have to provide the compiler with a constant expression

53
00:04:08,390 --> 00:04:11,900
so it knows how much memory to allocate.

54
00:04:11,900 --> 00:04:17,720
And this means that the size of the array must be known at compile time so you can allocate an array

55
00:04:17,720 --> 00:04:19,730
with a size known only at runtime.

56
00:04:19,730 --> 00:04:26,510
But this requires dynamic memory allocation, which we will explain in next lesson.

57
00:04:26,810 --> 00:04:34,520
Next sections in section named working with memory pointers and arrays.

58
00:04:34,520 --> 00:04:43,530
So in C plus plus you can declare the constant like this two for example, constant integer size equals

59
00:04:43,530 --> 00:04:52,500
five integer my new values and pass this as parameter size here.

60
00:04:52,500 --> 00:04:58,290
So we pass this size and as you can see, we our size equals to five.

61
00:04:58,290 --> 00:05:03,690
So this and this line of code is the same, just a different variable names.

62
00:05:03,690 --> 00:05:09,840
So elsewhere in your code, when you access the values array, you can use the size constant to make

63
00:05:09,840 --> 00:05:14,940
sure that you do not don't access items past end of the day.

64
00:05:15,660 --> 00:05:22,110
So since the size variable is declared in just one place, you need to change the size of the array

65
00:05:22,110 --> 00:05:26,910
at a later stage so you have just one place to make this change.

66
00:05:27,210 --> 00:05:36,210
Also, the const keyword here can also be used on pointers and references which you will learn in next

67
00:05:36,210 --> 00:05:39,540
sections and also on the objects.

68
00:05:39,720 --> 00:05:44,070
Often you will see it used on a parameters to functions.

69
00:05:44,070 --> 00:05:53,280
So this is used to get the compiler to help ensure that pointers, references and objects are used appropriately

70
00:05:53,280 --> 00:05:54,840
as you intended.
