1 00:00:00,700 --> 00:00:03,880 In this lecture, you will learn about the using namespaces. 2 00:00:04,270 --> 00:00:09,730 So namespaces gives you a mechanism to modularized code. 3 00:00:09,820 --> 00:00:18,360 So a namespace allows you to label your types, functions and variables with unique name so that using 4 00:00:18,360 --> 00:00:23,740 the scope resolution operator, you can give a fully qualified name. 5 00:00:23,830 --> 00:00:27,940 So the advantage is that you know exactly which item will be called. 6 00:00:28,000 --> 00:00:36,550 And the disadvantage is that using a fully qualified name, you are in effect switching of C++ arguments, 7 00:00:36,970 --> 00:00:43,270 dependent lookup mechanism for overloaded functions where the compiler will choose the function that 8 00:00:43,270 --> 00:00:47,990 has the best fit according to the arguments priced to function. 9 00:00:48,010 --> 00:00:50,290 So defining namespace is simple. 10 00:00:50,890 --> 00:00:53,170 For example, let's define a namespace here. 11 00:00:54,310 --> 00:00:57,310 Namespace you use the namespace keyword. 12 00:00:57,430 --> 00:01:05,410 You declare the types, functions and global variables with the namespace keyword here and the name 13 00:01:05,410 --> 00:01:06,140 you give it to it. 14 00:01:06,160 --> 00:01:09,370 So let's create the utilities namespace. 15 00:01:12,050 --> 00:01:12,590 Here. 16 00:01:13,430 --> 00:01:14,270 And. 17 00:01:14,950 --> 00:01:15,490 Here. 18 00:01:15,490 --> 00:01:20,770 So let's create a function in this namespace pool pool data. 19 00:01:21,690 --> 00:01:23,160 Pull data. 20 00:01:24,160 --> 00:01:24,730 Here. 21 00:01:26,080 --> 00:01:31,450 Let's make this, for example, just the comment, the quote here, and then let's add another function 22 00:01:31,450 --> 00:01:32,680 here, integer. 23 00:01:32,710 --> 00:01:33,670 Get data. 24 00:01:34,330 --> 00:01:36,070 Get data. 25 00:01:37,080 --> 00:01:44,250 Let's add another comment here, because we will fill these courts in later lectures code. 26 00:01:45,380 --> 00:01:45,920 Here. 27 00:01:49,170 --> 00:01:52,800 So as you can see here, we create a code, for example. 28 00:01:53,250 --> 00:02:00,660 Example code two and example for example, code one. 29 00:02:01,530 --> 00:02:08,780 And then you don't use this semicolon after declaring and link namespaces here. 30 00:02:08,790 --> 00:02:11,890 So now you use these symbols here. 31 00:02:11,910 --> 00:02:25,560 For example, if if utilities namespace pool data returns true, then you will get the. 32 00:02:26,730 --> 00:02:28,930 Then you create the integer number. 33 00:02:28,950 --> 00:02:31,950 Utilities get data. 34 00:02:32,640 --> 00:02:35,560 And here, let's add an example. 35 00:02:35,580 --> 00:02:38,070 We will use e. 36 00:02:39,130 --> 00:02:40,690 Variable here. 37 00:02:41,380 --> 00:02:47,440 So then the namespace declaration may just declare the functions, so in which case the actual functions 38 00:02:47,440 --> 00:02:49,000 will have to be defined elsewhere. 39 00:02:49,000 --> 00:02:52,600 So and you will need to use the qualified name here. 40 00:02:54,010 --> 00:02:54,730 Let's. 41 00:02:56,460 --> 00:02:57,630 Make it here. 42 00:02:59,040 --> 00:03:00,600 You can do this like this. 43 00:03:00,780 --> 00:03:02,340 For example, namespace. 44 00:03:02,670 --> 00:03:05,520 My professional. 45 00:03:07,300 --> 00:03:08,350 Utilities. 46 00:03:08,770 --> 00:03:14,980 Usually it's not best practice to make your variable names that long, but it's okay. 47 00:03:15,330 --> 00:03:17,950 Is just an explanatory code here. 48 00:03:19,800 --> 00:03:20,370 Then. 49 00:03:21,770 --> 00:03:24,020 And then let's go declare the functions. 50 00:03:24,020 --> 00:03:26,420 Declaring the functions. 51 00:03:27,430 --> 00:03:29,320 Here, Bull. 52 00:03:31,280 --> 00:03:32,150 Pull data. 53 00:03:33,850 --> 00:03:35,110 For data. 54 00:03:39,050 --> 00:03:40,460 And integer. 55 00:03:40,790 --> 00:03:41,540 Get data. 56 00:03:42,110 --> 00:03:45,380 As you can see, we can declare the same function names. 57 00:03:45,380 --> 00:03:52,820 Because of that, these two utilities doesn't have to do with each other, so they are in the different 58 00:03:52,820 --> 00:03:57,620 namespace so you can declare the same named functions here. 59 00:03:57,620 --> 00:04:01,610 Because they are they they don't have to interact with each other. 60 00:04:02,540 --> 00:04:03,260 So. 61 00:04:04,270 --> 00:04:07,480 Then we can create a pool here. 62 00:04:07,510 --> 00:04:10,630 For example, let's define the pool. 63 00:04:11,340 --> 00:04:13,080 But here. 64 00:04:15,790 --> 00:04:18,520 Bull utilities or not your. 65 00:04:19,650 --> 00:04:21,660 You can define this called letters. 66 00:04:24,680 --> 00:04:31,220 Defining the functions later here, defining the functions here. 67 00:04:31,760 --> 00:04:40,160 So pool utilities, as you can see, we are here to double dots here and pool data. 68 00:04:41,830 --> 00:04:45,010 And then you can write the. 69 00:04:46,000 --> 00:04:53,320 Court that the person who returns a bull from this function. 70 00:04:54,010 --> 00:04:57,900 And we can create the data after that. 71 00:04:58,210 --> 00:05:00,100 As you can see here, this is the integer. 72 00:05:00,220 --> 00:05:01,630 So integer. 73 00:05:01,660 --> 00:05:03,520 Oops, We have a problem here. 74 00:05:03,550 --> 00:05:04,110 Let's see. 75 00:05:04,120 --> 00:05:04,990 What is this? 76 00:05:05,660 --> 00:05:11,620 Cannot define or declare pull data here because the name does not enclose namespace utilities. 77 00:05:11,650 --> 00:05:14,140 As you can see, we can now use the. 78 00:05:15,340 --> 00:05:15,930 Another. 79 00:05:16,570 --> 00:05:22,600 As you can see, there is a different namespace, so we need to add here my professional utilities. 80 00:05:22,600 --> 00:05:25,470 And as you can see here, error is gone. 81 00:05:25,480 --> 00:05:27,970 So let's add another bool here. 82 00:05:28,300 --> 00:05:33,080 Not this is an integer sorry integer OC. 83 00:05:33,190 --> 00:05:36,610 My professional utilities get data. 84 00:05:37,660 --> 00:05:46,360 And code that returns an integer or in this function. 85 00:05:47,440 --> 00:05:52,480 So one use of a namespace is to version your code. 86 00:05:52,480 --> 00:05:58,030 So the first version of your code may have a side effect that is not in your functional specification 87 00:05:58,030 --> 00:05:59,710 and is technically back. 88 00:05:59,710 --> 00:06:02,950 So about some colors will use it and depend on it. 89 00:06:03,250 --> 00:06:13,570 When you update your code to fix the bug, you may decide to love to allow your colors the option to 90 00:06:13,570 --> 00:06:17,980 use the old version so that their code does not break. 91 00:06:17,980 --> 00:06:20,890 So you can do this with the namespace. 92 00:06:20,890 --> 00:06:22,060 But before that. 93 00:06:23,100 --> 00:06:24,600 I want to hear. 94 00:06:24,630 --> 00:06:28,290 Let's use only my professional code. 95 00:06:28,710 --> 00:06:35,190 So I just commented this code and you can use later here. 96 00:06:36,060 --> 00:06:36,810 And. 97 00:06:37,620 --> 00:06:43,500 Let's go within our namespace version two here. 98 00:06:45,480 --> 00:06:49,310 OC namespace My profession Utilities. 99 00:06:50,990 --> 00:06:56,020 And then let's create a version to in this namespace. 100 00:06:56,030 --> 00:06:56,600 So. 101 00:06:58,700 --> 00:06:59,450 Namespace. 102 00:06:59,500 --> 00:07:00,400 Namespace. 103 00:07:00,770 --> 00:07:01,610 V two. 104 00:07:02,330 --> 00:07:05,900 And then you can declare your function again. 105 00:07:06,770 --> 00:07:07,910 And here. 106 00:07:09,390 --> 00:07:11,580 Pull data. 107 00:07:13,540 --> 00:07:17,080 And the integer get data. 108 00:07:20,000 --> 00:07:20,540 Here. 109 00:07:21,830 --> 00:07:24,620 Integer Neave Future new feature. 110 00:07:26,040 --> 00:07:26,520 Yes. 111 00:07:27,490 --> 00:07:33,820 So now here, now colors we want to specify. 112 00:07:34,680 --> 00:07:41,520 It's actually make our new weird aversion to program. 113 00:07:42,810 --> 00:07:52,980 So this callers who want to specify aversion can call the fully qualified names for example colors could 114 00:07:52,980 --> 00:07:53,640 use. 115 00:07:53,880 --> 00:07:56,280 Here my professional functions. 116 00:07:57,380 --> 00:08:01,670 We two and here pull data. 117 00:08:02,030 --> 00:08:03,830 As you can see here, there's no problem. 118 00:08:03,830 --> 00:08:08,030 So we can use this namespace inside the namespace. 119 00:08:08,030 --> 00:08:16,130 So to use the so we can use this and newer version and utilities pull data. 120 00:08:16,160 --> 00:08:24,650 My professional utilities pull data to use the older version so we can use this is the new version version 121 00:08:24,650 --> 00:08:29,620 of our namespace and we can use the professional my. 122 00:08:30,690 --> 00:08:33,930 Officer, we have to end quote here. 123 00:08:33,960 --> 00:08:39,150 My professional utility is just the first version. 124 00:08:39,150 --> 00:08:44,640 So as you can see, we are using pull data now and let's use the pool data. 125 00:08:44,970 --> 00:08:50,480 So there are different functions in the same namespace. 126 00:08:50,490 --> 00:08:56,700 So when an item in a specific namespace calls an item in the same namespace, it does not have to use 127 00:08:56,700 --> 00:08:58,110 qualified name. 128 00:08:58,980 --> 00:09:08,310 So if the new feature here we which are here functions calls get data, it will be here. 129 00:09:08,340 --> 00:09:09,690 My professional. 130 00:09:10,020 --> 00:09:11,310 My professional. 131 00:09:13,250 --> 00:09:15,740 Utilities here. 132 00:09:17,530 --> 00:09:18,310 V to. 133 00:09:21,200 --> 00:09:23,150 Need to get data. 134 00:09:25,110 --> 00:09:26,010 Get data. 135 00:09:28,450 --> 00:09:28,690 Yes. 136 00:09:29,680 --> 00:09:31,450 And that is the cult. 137 00:09:33,350 --> 00:09:34,210 So. 138 00:09:36,190 --> 00:09:43,420 So it's important to note that to declare a namespace, you have to do the nesting manually as shown 139 00:09:43,510 --> 00:09:44,200 here. 140 00:09:45,300 --> 00:09:52,470 So let's create that an example to write the first version of the code that will call it using the namespace 141 00:09:52,470 --> 00:09:53,670 utilities. 142 00:09:53,970 --> 00:10:00,990 So C++ 11 provides a facility called an inline namespace that allows you to define a nested namespace, 143 00:10:00,990 --> 00:10:08,610 but lost the compiler to treat the items as being in the parent namespace when it performs an argument 144 00:10:08,610 --> 00:10:10,300 dependent lookup. 145 00:10:10,320 --> 00:10:13,500 So let's create an inline namespace here. 146 00:10:15,670 --> 00:10:18,790 K inline namespace. 147 00:10:20,250 --> 00:10:21,240 We won. 148 00:10:21,270 --> 00:10:22,590 We Yes, we won. 149 00:10:22,590 --> 00:10:23,310 Verizon won. 150 00:10:23,790 --> 00:10:24,450 And. 151 00:10:25,340 --> 00:10:26,000 Here. 152 00:10:26,030 --> 00:10:26,810 Pull. 153 00:10:27,500 --> 00:10:28,610 Pull data. 154 00:10:29,360 --> 00:10:33,020 Pull data and integer. 155 00:10:33,050 --> 00:10:35,810 Get data here. 156 00:10:37,670 --> 00:10:39,620 And as you can see, we have namespace V. 157 00:10:40,520 --> 00:10:44,810 Also, let's actually add before we v two. 158 00:10:44,840 --> 00:10:45,680 So we can. 159 00:10:46,190 --> 00:10:47,240 It's more readable. 160 00:10:47,480 --> 00:10:53,090 So now to call the version of data here, as you can see, we got an error here. 161 00:10:53,860 --> 00:10:54,970 Is ambiguous. 162 00:10:54,970 --> 00:11:02,560 So not to call the first version of data you can use the utilities, get data or utilities. 163 00:11:02,680 --> 00:11:04,420 We want. 164 00:11:05,710 --> 00:11:06,430 Full data. 165 00:11:06,430 --> 00:11:08,260 As you can see here, it's fixed. 166 00:11:08,620 --> 00:11:10,450 We won. 167 00:11:11,080 --> 00:11:11,890 We want. 168 00:11:12,760 --> 00:11:13,660 Get data. 169 00:11:14,540 --> 00:11:16,280 And utilities. 170 00:11:16,280 --> 00:11:23,960 We want pool data so fully qualified names can make the record difficult to read, especially if your 171 00:11:23,960 --> 00:11:25,550 code will only use one namespace. 172 00:11:25,580 --> 00:11:29,180 So to help you here you have several options. 173 00:11:29,180 --> 00:11:38,670 You can place a using statement using statement to indicate the symbols declared in the specified namespace. 174 00:11:38,690 --> 00:11:41,840 Same space can be used without a fully qualified name. 175 00:11:42,290 --> 00:11:48,710 So for example, you can use the using a namespace utilities. 176 00:11:48,890 --> 00:11:49,730 My professional. 177 00:11:49,730 --> 00:11:53,120 My professional utilities and like that.