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