All language subtitles for C-Programming-Course-Beginner-to-Advanced_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,160 --> 00:00:05,278 this complete c plus plus course will 2 00:00:03,040 --> 00:00:08,160 take you from being an absolute beginner 3 00:00:05,278 --> 00:00:10,480 to using advanced c plus plus features 4 00:00:08,160 --> 00:00:12,719 danielle gaguaya teaches this course he 5 00:00:10,480 --> 00:00:15,440 is an experienced software developer and 6 00:00:12,718 --> 00:00:18,399 has created many popular courses 7 00:00:15,439 --> 00:00:20,879 hello guys and welcome to this modern c 8 00:00:18,399 --> 00:00:22,559 plus plus 20 course this is a course 9 00:00:20,879 --> 00:00:24,399 that is going to take you from the 10 00:00:22,559 --> 00:00:26,079 absolute beginning where you know 11 00:00:24,399 --> 00:00:28,799 nothing about c plus plus and you're 12 00:00:26,079 --> 00:00:30,879 going to go all the way to a point where 13 00:00:28,800 --> 00:00:33,200 you can use some advanced features in 14 00:00:30,879 --> 00:00:34,600 the c plus plus programming language 15 00:00:36,558 --> 00:00:41,199 inheritance and polymorphism with the c 16 00:00:38,960 --> 00:00:42,960 plus plus programming language now i'm 17 00:00:42,960 --> 00:00:47,840 convincing you on why you should use c 18 00:00:45,359 --> 00:00:49,920 plus plus but c plus plus is a very 19 00:00:47,840 --> 00:00:52,960 powerful programming language you can 20 00:00:49,920 --> 00:00:55,039 use to do all kinds of crazy things you 21 00:00:52,960 --> 00:00:57,359 can use it to do systems programming you 22 00:00:55,039 --> 00:01:00,079 can build games with it and it is used 23 00:00:57,359 --> 00:01:03,039 in a wide range of areas so you can do a 24 00:01:00,079 --> 00:01:05,439 lot with c plus plus now who is this 25 00:01:03,039 --> 00:01:07,680 course for this course is built for 26 00:01:05,438 --> 00:01:09,599 complete beginners if you already have 27 00:01:07,680 --> 00:01:11,840 some experience with c plus plus you 28 00:01:09,599 --> 00:01:14,478 will still find some good things you can 29 00:01:11,840 --> 00:01:16,719 learn from the course but i am going to 30 00:01:14,478 --> 00:01:19,118 go from the absolute beginning showing 31 00:01:16,719 --> 00:01:21,438 you every little thing showing you the 32 00:01:19,118 --> 00:01:23,438 tools showing you the compilers and the 33 00:01:21,438 --> 00:01:25,279 editor we're going to be using here so 34 00:01:23,438 --> 00:01:26,559 we're going to spend a lot of time 35 00:01:26,560 --> 00:01:30,640 fundamentals by the time you are done 36 00:01:28,719 --> 00:01:33,840 with this course you will understand the 37 00:01:30,640 --> 00:01:36,159 bare fundamentals about c plus we will 38 00:01:33,840 --> 00:01:38,478 spend a lot of time exploring the 39 00:01:36,159 --> 00:01:40,079 procedure programming side of c plus 40 00:01:38,478 --> 00:01:42,719 plus so you're going to be writing 41 00:01:40,078 --> 00:01:44,239 programs that run from top to bottom and 42 00:01:42,719 --> 00:01:46,319 we're going to spend a lot of time in 43 00:01:44,239 --> 00:01:48,399 the editor writing code making sure you 44 00:01:46,319 --> 00:01:49,839 really understand what is going on here 45 00:01:48,399 --> 00:01:51,920 towards the end we're going to learn 46 00:01:49,840 --> 00:01:53,520 about object oriented programming and 47 00:01:53,519 --> 00:01:57,679 inheritance and polymorphism and these 48 00:01:55,920 --> 00:02:00,000 are the tools you're going to be using a 49 00:01:57,680 --> 00:02:02,240 lot in your career as a c plus plus 50 00:02:00,000 --> 00:02:04,319 developer another great feature of this 51 00:02:02,239 --> 00:02:06,640 course is that we spend a lot of time 52 00:02:04,319 --> 00:02:08,560 making sure you understand the tools so 53 00:02:06,640 --> 00:02:10,878 if you don't know what a compiler is if 54 00:02:08,560 --> 00:02:12,560 you don't know what an ide is we're 55 00:02:10,878 --> 00:02:14,318 going to make sure you understand these 56 00:02:12,560 --> 00:02:16,560 things and we're going to spend a lot of 57 00:02:14,318 --> 00:02:17,759 time using these things and by the time 58 00:02:16,560 --> 00:02:20,479 you're done with the course you're going 59 00:02:17,759 --> 00:02:22,318 to be very comfortable using the tools 60 00:02:20,479 --> 00:02:25,039 the editor of choice for the course is 61 00:02:22,318 --> 00:02:26,719 going to be visual studio code so that's 62 00:02:25,039 --> 00:02:29,199 an editor that is going to allow us to 63 00:02:26,719 --> 00:02:31,598 type in the code and give us some things 64 00:02:29,199 --> 00:02:34,238 like a syntax highlighting but we're 65 00:02:31,598 --> 00:02:36,479 going to be connecting our editor to a 66 00:02:34,239 --> 00:02:39,200 background compiler which is going to 67 00:02:36,479 --> 00:02:41,759 make sure our code is transformed 68 00:02:39,199 --> 00:02:43,679 to run on our computer but don't worry 69 00:02:41,759 --> 00:02:46,399 about all that craziness right now we're 70 00:02:43,680 --> 00:02:48,800 going to go in detail about these things 71 00:02:46,400 --> 00:02:51,439 for now i just want you to have a bird's 72 00:02:48,800 --> 00:02:54,000 eye view on what you can expect from the 73 00:02:51,439 --> 00:02:57,120 course here now i hope you know what to 74 00:02:54,000 --> 00:03:00,158 expect from the course but i also expect 75 00:02:57,120 --> 00:03:02,800 you to be ready to take the course and 76 00:03:00,158 --> 00:03:04,560 the requirements are really not that bad 77 00:03:02,800 --> 00:03:06,560 the only requirement is that you have 78 00:03:04,560 --> 00:03:08,400 time set aside to really practice what 79 00:03:06,560 --> 00:03:10,318 you learn from the course here and you 80 00:03:08,400 --> 00:03:12,239 need to have a strong drive and 81 00:03:10,318 --> 00:03:14,560 willingness to learn now if you want to 82 00:03:12,239 --> 00:03:17,360 follow along i am going to share a link 83 00:03:14,560 --> 00:03:19,280 to the github repository for the code i 84 00:03:17,360 --> 00:03:21,920 use in the course so you can check that 85 00:03:19,280 --> 00:03:24,000 out and really follow along if you want 86 00:03:21,919 --> 00:03:26,318 if you want some support we have a 87 00:03:24,000 --> 00:03:28,639 discord server dedicated for our 88 00:03:26,318 --> 00:03:30,798 students so you can join and be part of 89 00:03:28,639 --> 00:03:32,639 the community if you have a question you 90 00:03:30,799 --> 00:03:35,040 can ask but another thing i would 91 00:03:32,639 --> 00:03:37,359 encourage you to do is to also help 92 00:03:35,039 --> 00:03:39,359 others so if you see somebody asking a 93 00:03:37,360 --> 00:03:41,680 question try to help them you're going 94 00:03:39,360 --> 00:03:43,360 to learn a lot that way okay now that 95 00:03:41,680 --> 00:03:45,760 you know a bit about the course let's 96 00:03:43,360 --> 00:03:48,000 talk about myself here my name is daniel 97 00:03:45,759 --> 00:03:50,719 cacoya i am a software engineer i have 98 00:03:51,759 --> 00:03:56,560 and i am a professional c plus plus 99 00:03:53,919 --> 00:03:58,639 developer i do a lot of freelance work i 100 00:03:56,560 --> 00:04:00,959 work for a few companies i do all kinds 101 00:03:58,639 --> 00:04:03,598 of crazy things i also like to teach 102 00:04:00,959 --> 00:04:05,280 others so i have a few courses on udemy 103 00:04:03,598 --> 00:04:07,438 if you are interested in cute you can 104 00:04:05,280 --> 00:04:09,680 check these things out i also have a 105 00:04:07,438 --> 00:04:12,560 trending c plus plus course that you can 106 00:04:09,680 --> 00:04:14,480 check out but don't feel the need to go 107 00:04:12,560 --> 00:04:17,280 and check this ziplesque course out 108 00:04:14,479 --> 00:04:19,759 because a lot is covered in this youtube 109 00:04:17,279 --> 00:04:22,399 course here so that's really all about 110 00:04:19,759 --> 00:04:24,160 me okay now that you have an idea about 111 00:04:22,399 --> 00:04:26,159 the course we're going to head over in 112 00:04:24,160 --> 00:04:28,240 the next lecture and learn about the 113 00:04:26,160 --> 00:04:30,240 tools we're going to be using throughout 114 00:04:28,240 --> 00:04:32,478 the course here go ahead and finish up 115 00:04:32,478 --> 00:04:37,199 welcome to this new chapter where we're 116 00:04:34,399 --> 00:04:39,839 going to be exploring the c plus plus 117 00:04:37,199 --> 00:04:41,919 development tools you need to start 118 00:04:41,918 --> 00:04:46,959 applications and you really need two 119 00:04:44,319 --> 00:04:49,599 kinds of tools the first one is going to 120 00:04:46,959 --> 00:04:51,758 be an editor or an ide through which 121 00:04:49,600 --> 00:04:53,919 you're going to be writing your c plus 122 00:04:51,759 --> 00:04:56,319 plus code we are going to be using 123 00:04:53,918 --> 00:04:58,560 visual studio code as an editor in this 124 00:04:56,319 --> 00:05:01,199 course because it is very easy to 125 00:04:58,560 --> 00:05:04,079 install it is cross-platform it is going 126 00:05:01,199 --> 00:05:07,038 to run well on windows mac and linux and 127 00:05:04,079 --> 00:05:08,959 it supports a host of tools that are 128 00:05:07,038 --> 00:05:11,599 going to make your job easier as a 129 00:05:08,959 --> 00:05:14,000 surplus plus developer but that's not to 130 00:05:11,600 --> 00:05:16,160 say you can't use your favorite ide if 131 00:05:14,000 --> 00:05:18,560 you can but visual studio code is going 132 00:05:16,160 --> 00:05:20,479 to be our main editor in this course so 133 00:05:18,560 --> 00:05:22,399 i would recommend installing it to be 134 00:05:20,478 --> 00:05:25,120 able to follow along with what we do 135 00:05:22,399 --> 00:05:28,000 here now after we have an ide or an 136 00:05:25,120 --> 00:05:31,120 editor in place we will need a compiler 137 00:05:28,000 --> 00:05:33,120 and the compiler is a piece of software 138 00:05:31,120 --> 00:05:37,038 that takes the code we write in the 139 00:05:33,120 --> 00:05:40,639 editor and compiles that into code that 140 00:05:37,038 --> 00:05:43,279 can directly run on the hardware or 141 00:05:40,639 --> 00:05:45,038 binary executable format so this is what 142 00:05:43,279 --> 00:05:48,000 we're going to be doing here we're going 143 00:05:45,038 --> 00:05:50,000 to be typing our code in an editor like 144 00:05:48,000 --> 00:05:52,000 visual studio code we're going to be 145 00:05:50,000 --> 00:05:54,000 kicking off a process to compile our 146 00:05:52,000 --> 00:05:56,720 program and this is going to turn our 147 00:05:54,000 --> 00:05:59,120 program into a format that can run it 148 00:05:56,720 --> 00:06:00,880 directly on the hardware so for example 149 00:05:59,120 --> 00:06:03,439 if you are on a windows machine you're 150 00:06:00,879 --> 00:06:05,918 going to generate a binary executable 151 00:06:03,439 --> 00:06:08,079 that can directly run on windows if you 152 00:06:05,918 --> 00:06:10,719 are on linux we will generate a binary 153 00:06:08,079 --> 00:06:13,680 that can run on linux and the same 154 00:06:10,720 --> 00:06:15,600 concepts can really extend on mac or any 155 00:06:13,680 --> 00:06:18,160 other kind of operating system you might 156 00:06:15,600 --> 00:06:20,319 be working on with the course here so we 157 00:06:18,160 --> 00:06:23,120 need two tools we need an ide or an 158 00:06:20,319 --> 00:06:26,080 editor and a compiler and the tools you 159 00:06:23,120 --> 00:06:28,160 install are going to be depending on the 160 00:06:26,079 --> 00:06:30,478 operating system you are using to watch 161 00:06:28,160 --> 00:06:33,439 the course if you are on windows you're 162 00:06:30,478 --> 00:06:36,318 going to install a set of tools that are 163 00:06:33,439 --> 00:06:38,079 specific to windows if you are on linux 164 00:06:36,319 --> 00:06:40,319 you're going to install a set of tools 165 00:06:38,079 --> 00:06:44,000 for linux if you are on a mac you're 166 00:06:40,319 --> 00:06:46,319 going to install a set of tools specific 167 00:06:44,000 --> 00:06:49,918 to the apple operating system for 168 00:06:46,319 --> 00:06:52,479 desktop now on windows in terms of ides 169 00:06:49,918 --> 00:06:55,758 or integrated development environment it 170 00:06:52,478 --> 00:06:58,318 is possible to use a host of ides for 171 00:06:55,759 --> 00:07:00,639 example you can use code lite it is an 172 00:06:58,319 --> 00:07:03,039 ide you can use you can use microsoft 173 00:07:00,639 --> 00:07:05,038 visual studio but in this course we're 174 00:07:03,038 --> 00:07:08,079 going to be using visual studio code 175 00:07:05,038 --> 00:07:10,399 because it is easy to install it has a 176 00:07:08,079 --> 00:07:12,719 set of features that are really good it 177 00:07:10,399 --> 00:07:14,799 is popular it has a great community 178 00:07:12,720 --> 00:07:17,280 around it i think you're going to have a 179 00:07:14,800 --> 00:07:19,918 great time as a beginner to start 180 00:07:17,279 --> 00:07:22,159 writing your c plus plus programs using 181 00:07:19,918 --> 00:07:24,399 visual studio code that's not to say 182 00:07:22,160 --> 00:07:27,120 that you can't use another ide you can 183 00:07:24,399 --> 00:07:29,439 if you want but i am going to be using 184 00:07:27,120 --> 00:07:31,439 visual studio code in this course here 185 00:07:29,439 --> 00:07:34,478 now on linux we are also going to be 186 00:07:31,439 --> 00:07:37,120 using visual studio code as our editor 187 00:07:34,478 --> 00:07:39,038 in the course but there are other ides 188 00:07:37,120 --> 00:07:40,478 you can use for example you can use cute 189 00:07:39,038 --> 00:07:43,120 creator if you want you can use 190 00:07:40,478 --> 00:07:45,918 codelight you can use a host of other 191 00:07:43,120 --> 00:07:47,519 ides on linux but again visual studio 192 00:07:45,918 --> 00:07:49,439 code is going to be our thing in the 193 00:07:47,519 --> 00:07:52,318 course here and i would recommend 194 00:07:49,439 --> 00:07:54,478 installing it on your linux box if you 195 00:07:52,319 --> 00:07:57,280 are watching this course on the linux 196 00:07:54,478 --> 00:07:59,439 machine so for osx we are also going to 197 00:07:57,279 --> 00:08:02,318 be using visual studio code as our 198 00:07:59,439 --> 00:08:04,319 editor as i said it is cross platforms 199 00:08:02,319 --> 00:08:06,240 it runs where regardless of the 200 00:08:04,319 --> 00:08:08,319 operating system you might be using to 201 00:08:06,240 --> 00:08:09,840 watch the course but if you want you can 202 00:08:09,839 --> 00:08:14,638 ide which is xcode or even code lite all 203 00:08:12,639 --> 00:08:16,960 these are going to work if you want but 204 00:08:14,639 --> 00:08:19,918 again i would recommend going with 205 00:08:16,959 --> 00:08:22,239 visual studio code.plus to start out so 206 00:08:19,918 --> 00:08:24,240 that we have a common ground and you 207 00:08:24,240 --> 00:08:28,639 especially if you are a beginner now 208 00:08:26,399 --> 00:08:30,399 after we have an ide rolling we will 209 00:08:30,399 --> 00:08:35,278 and as we say the compiler is a tool 210 00:08:32,958 --> 00:08:38,239 that is going to transform your c plus 211 00:08:35,278 --> 00:08:40,799 plus code into a format that can run 212 00:08:38,240 --> 00:08:42,479 directly on the hardware so the compiler 213 00:08:40,799 --> 00:08:44,240 you use again is going to depend on the 214 00:08:42,479 --> 00:08:46,480 operating system where you are watching 215 00:08:44,240 --> 00:08:49,600 the course i am going to show you a 216 00:08:46,480 --> 00:08:52,159 bunch of compilers you can use but gcc 217 00:08:49,600 --> 00:08:54,399 is going to be our common base so on 218 00:08:52,159 --> 00:08:56,799 windows i will be using what we call 219 00:08:54,399 --> 00:08:58,320 mingw don't worry if you don't what it 220 00:08:56,799 --> 00:09:01,039 is i am going to show you in a minute 221 00:08:58,320 --> 00:09:03,920 when we install it but this is a project 222 00:09:01,039 --> 00:09:06,319 that brings the gcc compiler on windows 223 00:09:03,919 --> 00:09:08,399 so that we can use it easily and i am 224 00:09:06,320 --> 00:09:09,839 going to show you how to install it but 225 00:09:08,399 --> 00:09:12,159 i am also going to show you how to 226 00:09:09,839 --> 00:09:15,360 install the compiler from microsoft and 227 00:09:12,159 --> 00:09:17,600 the clang llvm compiler these are all 228 00:09:15,360 --> 00:09:20,159 available to you on windows and it is 229 00:09:17,600 --> 00:09:22,879 really easy to install them these days 230 00:09:20,159 --> 00:09:24,958 and i will be using them all if you are 231 00:09:22,879 --> 00:09:27,278 on windows i would recommend installing 232 00:09:24,958 --> 00:09:30,239 them all so that you really have a 233 00:09:27,278 --> 00:09:31,838 chance to try your c plus plus code on 234 00:09:30,240 --> 00:09:34,320 the different compilers and that's a 235 00:09:31,839 --> 00:09:36,399 really good thing to do if you test your 236 00:09:34,320 --> 00:09:38,959 c plus plus code against multiple 237 00:09:36,399 --> 00:09:41,360 compilers you will be increasing your 238 00:09:38,958 --> 00:09:43,679 chances for your code to really be 239 00:09:41,360 --> 00:09:46,399 portable and work on multiple compilers 240 00:09:43,679 --> 00:09:48,159 and operating systems and that cannot be 241 00:09:46,399 --> 00:09:50,240 a bad thing so i would recommend 242 00:09:48,159 --> 00:09:52,319 installing all these compilers if you 243 00:09:50,240 --> 00:09:54,320 are on windows i am also going to show 244 00:09:52,320 --> 00:09:56,320 you how to install two compilers on 245 00:09:54,320 --> 00:09:58,879 linux i am going to show you gcc we're 246 00:09:56,320 --> 00:10:01,760 going to install the shiny new feature 247 00:09:58,879 --> 00:10:03,600 in gcc we're going to install clang lvm 248 00:10:01,759 --> 00:10:05,759 on linux and we will be able to use all 249 00:10:03,600 --> 00:10:07,920 these compilers again if you are on 250 00:10:05,759 --> 00:10:10,000 linux i would recommend installing all 251 00:10:07,919 --> 00:10:12,719 these compilers so that you have a 252 00:10:10,000 --> 00:10:14,480 chance to try your code against multiple 253 00:10:12,720 --> 00:10:18,000 compilers and that's going to make your 254 00:10:14,480 --> 00:10:21,120 code much portable and easier to use on 255 00:10:18,000 --> 00:10:22,958 multiple compilers and platforms on the 256 00:10:21,120 --> 00:10:25,200 mac i am going to show you how to 257 00:10:25,200 --> 00:10:29,759 and i am going to show you how to 258 00:10:27,120 --> 00:10:32,240 install clangle lvm but if you want you 259 00:10:29,759 --> 00:10:34,159 can also install xcode and the compiler 260 00:10:32,240 --> 00:10:37,039 that comes with that for c plus but 261 00:10:34,159 --> 00:10:38,879 development all these are options to you 262 00:10:37,039 --> 00:10:41,120 but again in this course we are mostly 263 00:10:38,879 --> 00:10:43,360 going to be using the gcc compiler so i 264 00:10:41,120 --> 00:10:45,120 would recommend getting a hold of this 265 00:10:43,360 --> 00:10:47,519 if you don't want to install other 266 00:10:45,120 --> 00:10:50,399 compilers if you only have to install 267 00:10:47,519 --> 00:10:52,399 one compiler please install gcc that's 268 00:10:50,399 --> 00:10:54,480 going to be our main compiler in the 269 00:10:52,399 --> 00:10:56,240 course here okay so these are the tools 270 00:10:54,480 --> 00:10:58,879 we need we are going to head over in the 271 00:10:56,240 --> 00:11:01,600 next lecture and show you how to install 272 00:10:58,879 --> 00:11:03,600 the visual studio code editor on windows 273 00:11:01,600 --> 00:11:05,680 and really get started with that 274 00:11:03,600 --> 00:11:07,440 go ahead and finish up here and meet me 275 00:11:05,679 --> 00:11:09,679 in the next lecture this video i am 276 00:11:07,440 --> 00:11:12,079 going to show you how to install a c 277 00:11:09,679 --> 00:11:14,958 plus plus compiler on your windows 278 00:11:12,078 --> 00:11:16,958 operating system so we're going to be 279 00:11:14,958 --> 00:11:18,799 looking at three kinds of compilers 280 00:11:16,958 --> 00:11:21,518 we're going to look at the mingw 281 00:11:18,799 --> 00:11:23,759 compiler which gives us access to a gcc 282 00:11:21,519 --> 00:11:26,000 like compiler on windows we're going to 283 00:11:23,759 --> 00:11:27,679 look at clan llvm which is another 284 00:11:26,000 --> 00:11:30,159 compiler we're going to look at the 285 00:11:30,159 --> 00:11:34,799 now before we talk about any compiler i 286 00:11:32,559 --> 00:11:37,199 would like you to come to google or your 287 00:11:37,200 --> 00:11:41,278 c plus plus compiler support okay so 288 00:11:39,759 --> 00:11:43,838 we're going to choose this little guy 289 00:11:41,278 --> 00:11:46,078 here and this is really going to give us 290 00:11:46,078 --> 00:11:51,838 on which feature is supported by which c 291 00:11:48,958 --> 00:11:54,319 plus plus compiler for example if we are 292 00:11:51,839 --> 00:11:56,079 interested in c plus plus 20 which is 293 00:11:54,320 --> 00:11:58,639 the lattice standard that we have in 294 00:11:56,078 --> 00:12:00,958 simplest plus at the time of recording 295 00:11:58,639 --> 00:12:01,959 this video i am recording of this in 296 00:12:01,958 --> 00:12:07,599 2021 but if we go here we're going to 297 00:12:04,879 --> 00:12:09,600 see c plus plus 20 core features and 298 00:12:07,600 --> 00:12:12,000 we're going to see that we have a list 299 00:12:09,600 --> 00:12:15,040 of what each compiler supports we have 300 00:12:12,000 --> 00:12:18,078 gcc we have clan we have msvc we have 301 00:12:15,039 --> 00:12:20,078 the clan version from the apple company 302 00:12:18,078 --> 00:12:22,479 and if we scroll down we can really see 303 00:12:20,078 --> 00:12:26,000 the features that are supported by each 304 00:12:22,480 --> 00:12:27,600 compiler again gcc is going to be our 305 00:12:26,000 --> 00:12:30,078 choice in this course this is going to 306 00:12:27,600 --> 00:12:32,320 be our main compiler but nothing really 307 00:12:30,078 --> 00:12:34,799 stops you from using the clan compiler 308 00:12:32,320 --> 00:12:36,720 or the msvc compiler and if you happen 309 00:12:34,799 --> 00:12:38,319 to see that your compiler of choice 310 00:12:36,720 --> 00:12:40,800 doesn't support what you need for 311 00:12:38,320 --> 00:12:44,320 example you can see that at the current 312 00:12:40,799 --> 00:12:46,958 time lambdas in an evaluated context are 313 00:12:46,958 --> 00:12:51,278 you can see that it is ready here but 314 00:12:49,278 --> 00:12:55,919 this is a feature you can use both in 315 00:12:51,278 --> 00:12:59,278 gcc and the msvc compiler from microsoft 316 00:12:55,919 --> 00:13:01,278 so keep an eye out on this page here to 317 00:12:59,278 --> 00:13:03,519 know which kind of things you can use in 318 00:13:01,278 --> 00:13:05,838 your compiler if you try something out 319 00:13:03,519 --> 00:13:07,919 and you see that it doesn't work this is 320 00:13:05,839 --> 00:13:10,240 a good place to come and figure out if 321 00:13:07,919 --> 00:13:12,399 your compiler supports the feature that 322 00:13:10,240 --> 00:13:14,079 you are using in this video i am going 323 00:13:12,399 --> 00:13:16,639 to show you how to install the three 324 00:13:14,078 --> 00:13:19,838 most common compilers and those are gcc 325 00:13:16,639 --> 00:13:22,560 clang msvc if you want you can come in 326 00:13:19,839 --> 00:13:24,800 your search engine and type gcc to see 327 00:13:22,559 --> 00:13:27,039 what this is it is a good thing to do to 328 00:13:24,799 --> 00:13:28,719 learn about your tools here you can see 329 00:13:27,039 --> 00:13:31,039 that it is a compiler that is mostly 330 00:13:28,720 --> 00:13:33,759 used on the lenox platform but we can 331 00:13:31,039 --> 00:13:34,958 also use it on windows the latest 332 00:13:34,958 --> 00:13:39,119 installing is eleven two zero i think it 333 00:13:39,120 --> 00:13:43,039 six days ago so it is really fresh and 334 00:13:41,440 --> 00:13:45,040 we're going to be taking advantage of 335 00:13:43,039 --> 00:13:47,519 this another thing i should say here is 336 00:13:45,039 --> 00:13:50,078 that you should install the latest tools 337 00:13:47,519 --> 00:13:52,320 you can get your hands upon this is the 338 00:13:50,078 --> 00:13:54,319 version we're going to be mostly using 339 00:13:52,320 --> 00:13:55,760 in this course but if you are watching 340 00:13:54,320 --> 00:13:57,760 this in the future you're going to have 341 00:13:55,759 --> 00:14:00,078 a newer version i would recommend 342 00:13:57,759 --> 00:14:02,319 installing the latest version or the 343 00:14:00,078 --> 00:14:04,879 newest version you can get your hands 344 00:14:02,320 --> 00:14:07,760 upon so the first we're going to do is 345 00:14:04,879 --> 00:14:10,000 install a gcc compiler on windows i am 346 00:14:07,759 --> 00:14:13,360 going to show you a way we can get both 347 00:14:10,000 --> 00:14:15,278 gcc and clang in one go and to get 348 00:14:13,360 --> 00:14:17,759 access to that please go in your search 349 00:14:15,278 --> 00:14:19,838 engine and type windlabs and this is 350 00:14:17,759 --> 00:14:22,319 going to bring us to this project here 351 00:14:19,839 --> 00:14:25,440 if you want you can click upon this 352 00:14:22,320 --> 00:14:28,320 and this is a cool project by a guy who 353 00:14:25,440 --> 00:14:30,399 is providing worlds for these compilers 354 00:14:30,399 --> 00:14:35,759 so if you want you can just go down here 355 00:14:32,958 --> 00:14:37,759 and look at the lettuce releases again i 356 00:14:37,759 --> 00:14:43,360 lettuce you can get your hands upon in 357 00:14:39,839 --> 00:14:45,199 this case the lettuce happens to be 1120 358 00:14:43,360 --> 00:14:46,800 but in the future this is going to 359 00:14:45,198 --> 00:14:50,159 change you're going to find a version 360 00:14:46,799 --> 00:14:52,479 for 12 or 13 so please grab the newest 361 00:14:50,159 --> 00:14:54,399 version you can get your hands up on now 362 00:14:52,480 --> 00:14:58,000 i want you to install the version that 363 00:14:54,399 --> 00:15:01,039 says that it has llvm and clang so if 364 00:14:58,000 --> 00:15:03,440 you are on a 32-bit version on windows i 365 00:15:01,039 --> 00:15:06,319 would recommend getting this 7-zip 366 00:15:03,440 --> 00:15:08,800 archive on this zip archive here if you 367 00:15:06,320 --> 00:15:11,199 are on 64 i would recommend getting a 368 00:15:08,799 --> 00:15:13,120 hold of this or this little thing here 369 00:15:11,198 --> 00:15:15,039 it doesn't really matter but make sure 370 00:15:13,120 --> 00:15:16,959 you get the latest version you can get 371 00:15:15,039 --> 00:15:19,759 your hands upon and you can see that 372 00:15:16,958 --> 00:15:22,479 this conveniently says latest here so i 373 00:15:19,759 --> 00:15:24,480 am going to download the 64-bit version 374 00:15:22,480 --> 00:15:27,440 on windows i am going to grab the zip 375 00:15:24,480 --> 00:15:30,159 archive here and let's click upon this 376 00:15:27,440 --> 00:15:32,240 this should kick off my download and 377 00:15:30,159 --> 00:15:34,399 this is going to download on my system i 378 00:15:32,240 --> 00:15:36,079 can download this if i want and you're 379 00:15:34,399 --> 00:15:38,480 going to see that it is going to start 380 00:15:36,078 --> 00:15:41,439 downloading here and i am going to wait 381 00:15:38,480 --> 00:15:44,320 for this to finish again this is going 382 00:15:41,440 --> 00:15:46,639 to give us both the gcc compiler and the 383 00:15:44,320 --> 00:15:49,360 clan compiler and this is really like 384 00:15:46,639 --> 00:15:51,519 shooting two birds with one stone so 385 00:15:49,360 --> 00:15:53,440 this is going to be really good i 386 00:15:51,519 --> 00:15:56,159 already have this downloaded so i am 387 00:15:53,440 --> 00:15:58,560 just going to head over to the location 388 00:15:56,159 --> 00:16:01,759 where i have this downloaded i think i 389 00:16:01,759 --> 00:16:06,079 so let's crack this open and go in my 390 00:16:06,078 --> 00:16:12,958 and if i go in my compressed folder i am 391 00:16:08,879 --> 00:16:15,360 going to find winlabs llvm and mingw 392 00:16:12,958 --> 00:16:17,039 this is what you really want but if you 393 00:16:15,360 --> 00:16:20,639 don't want the clan compiler you can 394 00:16:17,039 --> 00:16:22,159 grab the one that doesn't say llvm here 395 00:16:20,639 --> 00:16:24,320 because we really want both of these 396 00:16:22,159 --> 00:16:26,559 compilers we are going to grab the one 397 00:16:24,320 --> 00:16:28,959 that has both the gcc compiler and the 398 00:16:26,559 --> 00:16:30,719 clan compiler if we crack this open 399 00:16:28,958 --> 00:16:32,879 let's open this together so that you can 400 00:16:30,720 --> 00:16:35,120 see what is happening we are going to 401 00:16:32,879 --> 00:16:37,360 have this little folder here if we go in 402 00:16:35,120 --> 00:16:39,679 we're going to find a bin folder if we 403 00:16:37,360 --> 00:16:40,959 go in and crack that open we're going to 404 00:16:40,958 --> 00:16:46,638 that says clang you see we have a clang 405 00:16:44,159 --> 00:16:48,879 compiler we have our clan plus plus 406 00:16:46,639 --> 00:16:50,959 compiler for c plus plus and if we go 407 00:16:48,879 --> 00:16:53,679 down we're going to find our g plus plus 408 00:16:50,958 --> 00:16:55,838 compiler and the gcc compiler this is 409 00:16:53,679 --> 00:16:58,479 what we want so what we are going to do 410 00:16:55,839 --> 00:17:01,040 is extract this somewhere on our system 411 00:16:58,480 --> 00:17:03,759 i put this on my c drive so if you go 412 00:17:01,039 --> 00:17:06,159 here you're going to find mingw64 413 00:17:03,759 --> 00:17:08,720 so all you really have to do is go where 414 00:17:08,720 --> 00:17:12,078 and say extract we're going to right 415 00:17:12,078 --> 00:17:16,720 and say extract to a location and you're 416 00:17:14,959 --> 00:17:19,759 going to specify where you want this to 417 00:17:16,720 --> 00:17:21,759 go and this is going to be extracted or 418 00:17:19,759 --> 00:17:23,439 even if you want you can extract here 419 00:17:21,759 --> 00:17:25,519 this is something i like to do this is 420 00:17:23,439 --> 00:17:28,079 going to show up in the current folder 421 00:17:25,519 --> 00:17:30,319 and you can grab this and paste that in 422 00:17:28,078 --> 00:17:33,119 a location where you want this in my 423 00:17:30,319 --> 00:17:35,678 case i put this in my c drive as i said 424 00:17:33,119 --> 00:17:37,759 here so this is going to be my compiler 425 00:17:35,679 --> 00:17:39,519 now once you have these compilers 426 00:17:37,759 --> 00:17:42,480 installed again we have the clan 427 00:17:39,519 --> 00:17:44,079 compiler we have the gcc compiler if we 428 00:17:42,480 --> 00:17:46,240 go down we're going to find the g plus 429 00:17:44,079 --> 00:17:48,399 plus we even have gdb which is a 430 00:17:46,240 --> 00:17:50,720 debugger we can use to debug c plus plus 431 00:17:48,400 --> 00:17:52,798 applications but we will talk about this 432 00:17:50,720 --> 00:17:54,558 later once you have this i would 433 00:17:52,798 --> 00:17:57,119 recommend putting this in your 434 00:17:54,558 --> 00:17:59,359 environment variables on windows the way 435 00:17:57,119 --> 00:18:00,959 you do this you grab this path well we 436 00:17:59,359 --> 00:18:02,558 have our compilers here and we're going 437 00:18:00,960 --> 00:18:03,759 to copy this this is something i 438 00:18:03,759 --> 00:18:10,558 and on windows 10 you can go on the 439 00:18:10,558 --> 00:18:15,038 and this is going to give us a window in 440 00:18:12,798 --> 00:18:16,798 which we can edit our environment 441 00:18:15,038 --> 00:18:19,119 variables we're going to click on this 442 00:18:16,798 --> 00:18:20,558 little thing this is going to open up on 443 00:18:19,119 --> 00:18:22,639 our system we're going to wait for this 444 00:18:20,558 --> 00:18:25,038 to open up and we're going to have a 445 00:18:22,640 --> 00:18:26,960 window that says system properties we're 446 00:18:25,038 --> 00:18:30,079 going to click on environment variables 447 00:18:26,960 --> 00:18:32,880 here and we can put this in our user 448 00:18:30,079 --> 00:18:34,960 environment variables or on our system 449 00:18:32,880 --> 00:18:37,280 environment variables since i am the 450 00:18:34,960 --> 00:18:40,160 only one using this system i like to put 451 00:18:37,279 --> 00:18:42,480 this in my path for my system variables 452 00:18:40,160 --> 00:18:45,360 so i am going to click on path and click 453 00:18:42,480 --> 00:18:48,240 edit here and i am going to add a path 454 00:18:45,359 --> 00:18:50,558 to my gcc and cloud installation here 455 00:18:48,240 --> 00:18:52,880 you can see that right here if you don't 456 00:18:50,558 --> 00:18:54,399 have this end you can click on new 457 00:18:52,880 --> 00:18:56,880 you're going to have a window here and 458 00:18:54,400 --> 00:18:58,720 you can paste this right in and you can 459 00:18:56,880 --> 00:19:01,679 click ok but i'm not going to do that 460 00:18:58,720 --> 00:19:04,000 because i already have this in here so 461 00:19:01,679 --> 00:19:06,880 what i am going to do is just delete 462 00:19:04,000 --> 00:19:08,880 this let's see if i can delete i can 463 00:19:06,880 --> 00:19:11,039 delete this little guy and i have my 464 00:19:08,880 --> 00:19:12,960 path in here so if you don't have this 465 00:19:11,038 --> 00:19:15,440 in please make sure you have this in 466 00:19:12,960 --> 00:19:17,519 your environment variables so we're 467 00:19:15,440 --> 00:19:19,519 going to cancel out of this but if this 468 00:19:17,519 --> 00:19:22,480 is the first time you do this you should 469 00:19:19,519 --> 00:19:24,960 click ok and one way to test that your 470 00:19:22,480 --> 00:19:27,839 environment variables are working or 471 00:19:24,960 --> 00:19:30,880 even if your compilers are working 472 00:19:27,839 --> 00:19:32,879 is to crack up a command line terminal 473 00:19:30,880 --> 00:19:34,559 so we're going to say cmd on the start 474 00:19:34,558 --> 00:19:40,639 and all you need to do is say g plus 475 00:19:37,038 --> 00:19:41,919 plus just like this dash dash version 476 00:19:40,640 --> 00:19:43,840 if you do this you're going to see 477 00:19:41,919 --> 00:19:45,038 information about your c plus plus 478 00:19:45,038 --> 00:19:49,679 and this is coming from the installation 479 00:19:46,960 --> 00:19:53,120 we just did and for clan you can say 480 00:19:49,679 --> 00:19:55,120 clan plus plus version and this is going 481 00:19:53,119 --> 00:19:57,279 to tell you the information about your 482 00:19:55,119 --> 00:19:58,798 clan compiler installation let's wait 483 00:19:57,279 --> 00:20:01,038 for this to come up i don't know why 484 00:19:58,798 --> 00:20:03,038 it's slow but you can see that we have a 485 00:20:01,038 --> 00:20:06,240 clank compiler the lattice version is 486 00:20:03,038 --> 00:20:08,558 1201 for the gcc compiler the latest 487 00:20:08,558 --> 00:20:13,759 and these are the compilers we can use 488 00:20:10,798 --> 00:20:16,480 to write c plus plus 20 applications 489 00:20:13,759 --> 00:20:19,279 okay so by now we have a gcc compiler 490 00:20:16,480 --> 00:20:22,319 through the main gw project we have a 491 00:20:19,279 --> 00:20:24,399 client llcm compiler on our system we 492 00:20:22,319 --> 00:20:27,439 are also going to see how to get a hold 493 00:20:24,400 --> 00:20:29,600 of the compiler from microsoft and to 494 00:20:27,440 --> 00:20:32,000 get hold of this you need to install 495 00:20:32,000 --> 00:20:36,079 to install microsoft visual studio all 496 00:20:34,079 --> 00:20:38,319 you have to do is come to your favorite 497 00:20:36,079 --> 00:20:41,279 search engine and say microsoft visual 498 00:20:38,319 --> 00:20:43,439 studio we can look at the 2019 version 499 00:20:41,279 --> 00:20:45,839 which is the latest i think and we're 500 00:20:43,440 --> 00:20:48,480 going to click on the download link here 501 00:20:45,839 --> 00:20:50,399 we have as the first link and we're 502 00:20:48,480 --> 00:20:52,720 going to see ways we can download this 503 00:20:50,400 --> 00:20:55,038 little guy now microsoft visual studio 504 00:20:52,720 --> 00:20:57,440 has a community version you can download 505 00:20:55,038 --> 00:20:59,679 for free but we also have a professional 506 00:20:57,440 --> 00:21:02,080 and enterprise versions that we're not 507 00:20:59,679 --> 00:21:04,400 going to care about in this course here 508 00:21:04,400 --> 00:21:09,038 the community version and that's going 509 00:21:06,880 --> 00:21:11,600 to be enough for our purposes here so if 510 00:21:09,038 --> 00:21:14,400 you click on free download here this is 511 00:21:11,599 --> 00:21:16,639 going to download and install the visual 512 00:21:14,400 --> 00:21:18,320 studio compiler you can click on this 513 00:21:16,640 --> 00:21:20,000 little guy and it is going to download 514 00:21:18,319 --> 00:21:22,240 an installer you're going to crack this 515 00:21:20,000 --> 00:21:25,440 open on your system and it is going to 516 00:21:22,240 --> 00:21:27,359 install the visual studio installer so 517 00:21:25,440 --> 00:21:29,360 once you have it installed you can come 518 00:21:29,359 --> 00:21:33,918 and say visual studio installer i think 519 00:21:32,319 --> 00:21:35,519 if you say visual studio you're going to 520 00:21:33,919 --> 00:21:38,000 have this pop-up somewhere on your 521 00:21:35,519 --> 00:21:39,839 system we're going to click on this and 522 00:21:38,000 --> 00:21:41,599 if you crack the installer open you're 523 00:21:39,839 --> 00:21:43,359 going to see a window like this which is 524 00:21:41,599 --> 00:21:46,000 really going to give you a chance to 525 00:21:43,359 --> 00:21:48,719 install the tools you want to use with 526 00:21:48,720 --> 00:21:53,839 ide and in this case we are interested 527 00:21:51,679 --> 00:21:55,600 in desktop development with c plus plus 528 00:21:53,839 --> 00:21:57,678 so we're going to click this little guy 529 00:21:55,599 --> 00:22:00,558 here we're going to take it open 530 00:21:57,679 --> 00:22:02,400 and once you do this you going to click 531 00:22:00,558 --> 00:22:05,038 download and install and this is going 532 00:22:02,400 --> 00:22:07,038 to install the visual studio compiler 533 00:22:05,038 --> 00:22:08,879 with the underlying compiler you see 534 00:22:07,038 --> 00:22:11,599 that this is going to give us access to 535 00:22:08,880 --> 00:22:13,360 the msvc compiler and all kinds of crazy 536 00:22:11,599 --> 00:22:16,480 things we really needed to write c plus 537 00:22:13,359 --> 00:22:18,798 plus applications on windows using the 538 00:22:16,480 --> 00:22:20,319 compiler from microsoft now i have to 539 00:22:18,798 --> 00:22:22,480 say this is going to take some time to 540 00:22:20,319 --> 00:22:24,879 install so if you see that this takes up 541 00:22:22,480 --> 00:22:27,919 20 minutes of your time don't worry this 542 00:22:24,880 --> 00:22:30,640 is really normal because this id and the 543 00:22:27,919 --> 00:22:33,440 compiler behind it and really large 544 00:22:30,640 --> 00:22:35,840 they're not like the gcc or the clan 545 00:22:33,440 --> 00:22:38,320 llvm compilers that we just installed 546 00:22:35,839 --> 00:22:39,678 okay so once you have this ticked or 547 00:22:38,319 --> 00:22:41,839 checked you're going to click on 548 00:22:39,679 --> 00:22:44,159 download or install whatever this button 549 00:22:41,839 --> 00:22:47,279 is saying here this is going to install 550 00:22:44,159 --> 00:22:49,520 both the microsoft visual studio ide 551 00:22:47,279 --> 00:22:51,839 and the compiler from microsoft that you 552 00:22:49,519 --> 00:22:54,400 can use directly to write c plus plus 553 00:22:51,839 --> 00:22:56,319 applications so once we have this open i 554 00:22:54,400 --> 00:22:58,080 am going to suppose that you have this 555 00:22:56,319 --> 00:22:59,918 installed already i am going to close 556 00:22:58,079 --> 00:23:00,720 out of this because i don't want to do 557 00:23:00,720 --> 00:23:04,480 and you're going to have visual studio 558 00:23:02,240 --> 00:23:06,079 installed on your system once you have 559 00:23:04,480 --> 00:23:09,120 this installed you're going to come to 560 00:23:06,079 --> 00:23:10,879 the start menu and say developer 561 00:23:09,119 --> 00:23:12,319 and this is going to bring up two things 562 00:23:10,880 --> 00:23:15,520 one is going to be the developer 563 00:23:12,319 --> 00:23:16,798 powershell for visual studio 2019 and 564 00:23:15,519 --> 00:23:18,558 the other is going to be a command 565 00:23:16,798 --> 00:23:19,918 prompt it doesn't really matter what you 566 00:23:18,558 --> 00:23:21,599 choose i am going to click on the 567 00:23:21,599 --> 00:23:26,558 and once you do this this is going to 568 00:23:23,679 --> 00:23:27,440 give you a terminal or a window through 569 00:23:27,440 --> 00:23:31,679 the environment variables are already in 570 00:23:29,919 --> 00:23:34,159 to start using the compiler from 571 00:23:31,679 --> 00:23:36,640 microsoft and if we come here and type 572 00:23:36,640 --> 00:23:39,919 we're going to see that we're going to 573 00:23:37,839 --> 00:23:41,678 see the information here if this gives 574 00:23:39,919 --> 00:23:43,840 you an error or something you're going 575 00:23:41,679 --> 00:23:46,080 to know that you didn't really install 576 00:23:43,839 --> 00:23:48,240 this properly so please go back and make 577 00:23:46,079 --> 00:23:50,079 sure you have this installed if you have 578 00:23:48,240 --> 00:23:52,720 a problem you can ask me i can do the 579 00:23:50,079 --> 00:23:54,879 best i can to help you out but by this 580 00:23:52,720 --> 00:23:57,440 moment we have three compilers on 581 00:23:54,880 --> 00:24:00,880 windows that we can use for c plus plus 582 00:23:57,440 --> 00:24:03,200 development we have the mingw or the gcc 583 00:24:00,880 --> 00:24:06,080 compiler in our hands we have the klanga 584 00:24:03,200 --> 00:24:09,679 lvm compiler and we have the compiler 585 00:24:06,079 --> 00:24:11,918 from microsoft and now we can install an 586 00:24:09,679 --> 00:24:14,400 ide or an editor and that's going to be 587 00:24:11,919 --> 00:24:16,480 visual studio code and we can connect 588 00:24:14,400 --> 00:24:19,038 that to these compilers so that we can 589 00:24:16,480 --> 00:24:21,200 start writing our own z plus bus 20 590 00:24:19,038 --> 00:24:23,119 applications we are going to head over 591 00:24:21,200 --> 00:24:25,278 in the next lecture and install the 592 00:24:25,278 --> 00:24:29,839 go ahead and finish up here and meet me 593 00:24:27,519 --> 00:24:31,679 there in this lecture i am going to show 594 00:24:29,839 --> 00:24:34,399 you how to install the visual studio 595 00:24:31,679 --> 00:24:36,480 code editor on windows so if you are 596 00:24:34,400 --> 00:24:39,200 watching this course on windows this 597 00:24:36,480 --> 00:24:42,000 video here is going to be a must watch 598 00:24:39,200 --> 00:24:44,080 we are going to head over to our search 599 00:24:42,000 --> 00:24:46,079 engine and type visual studio code this 600 00:24:44,079 --> 00:24:48,720 is going to give you a link where we can 601 00:24:46,079 --> 00:24:50,558 download this cool editor if you want 602 00:24:48,720 --> 00:24:52,960 you can learn all you can about it it is 603 00:24:50,558 --> 00:24:55,200 a source code editor made by microsoft 604 00:24:52,960 --> 00:24:57,840 it supports multiple languages but we're 605 00:24:55,200 --> 00:25:00,640 going to be using that specifically for 606 00:24:57,839 --> 00:25:03,678 c plus plus in the course here here i am 607 00:25:00,640 --> 00:25:06,000 at the page for the editor and you can 608 00:25:03,679 --> 00:25:08,559 read all you want about it but we are 609 00:25:06,000 --> 00:25:10,720 interested in downloading this for 610 00:25:08,558 --> 00:25:12,879 windows if you want you can click on 611 00:25:10,720 --> 00:25:14,480 this download button here but i am just 612 00:25:14,480 --> 00:25:19,679 and have download links for windows if i 613 00:25:17,839 --> 00:25:21,439 want i can click on user installer 614 00:25:19,679 --> 00:25:24,159 system installer you can click on what 615 00:25:21,440 --> 00:25:25,919 you want if you are on a 32-bit system 616 00:25:24,159 --> 00:25:28,080 you're going to download a 32-bit 617 00:25:25,919 --> 00:25:31,840 version i am just going to click on 618 00:25:28,079 --> 00:25:33,918 64-bit here because i am on a 64-bit 619 00:25:33,919 --> 00:25:38,480 in this video we are doing this for 620 00:25:36,000 --> 00:25:40,960 windows so this is going to be our area 621 00:25:38,480 --> 00:25:43,200 of interest here i am going to click on 622 00:25:40,960 --> 00:25:45,519 this download link and i am going to 623 00:25:45,519 --> 00:25:49,519 and i will have a window to start 624 00:25:47,359 --> 00:25:51,599 downloading visual studio code setup 625 00:25:49,519 --> 00:25:54,000 here i can save this file and it is 626 00:25:51,599 --> 00:25:55,918 going to start downloading once it is 627 00:25:54,000 --> 00:25:58,240 downloaded i am going to double click 628 00:25:55,919 --> 00:26:00,000 and really click my way through this 629 00:25:58,240 --> 00:26:02,960 until i have a visual studio code 630 00:26:02,960 --> 00:26:07,600 okay the download is good i can crack 631 00:26:07,599 --> 00:26:11,199 and you're going to see that it is 632 00:26:08,960 --> 00:26:13,519 saying that it has detected that visual 633 00:26:11,200 --> 00:26:15,840 studio code is already running on my 634 00:26:13,519 --> 00:26:17,200 system because i have it installed but 635 00:26:15,839 --> 00:26:19,439 if you don't have it installed you're 636 00:26:17,200 --> 00:26:21,679 going to click your way through and 637 00:26:19,440 --> 00:26:23,440 install this on windows and after you 638 00:26:23,440 --> 00:26:26,880 okay once you start visual studio code 639 00:26:25,278 --> 00:26:28,798 you're going to have a window like this 640 00:26:26,880 --> 00:26:30,880 if you run it for the first time you are 641 00:26:28,798 --> 00:26:33,599 probably going to see a windows with 642 00:26:33,599 --> 00:26:36,879 let's look at the welcome screen you're 643 00:26:35,359 --> 00:26:38,639 going to probably see you're going to 644 00:26:36,880 --> 00:26:40,960 probably see something like this but i 645 00:26:38,640 --> 00:26:42,640 am not sure but the most important thing 646 00:26:40,960 --> 00:26:44,880 is that you're going to have a window 647 00:26:42,640 --> 00:26:46,480 like this with visual studio code the 648 00:26:44,880 --> 00:26:49,440 first thing we want to do for c plus 649 00:26:46,480 --> 00:26:51,679 plus development is going to scroll down 650 00:26:49,440 --> 00:26:54,880 and click on this little thing that says 651 00:26:51,679 --> 00:26:57,278 extensions i am going to click on this 652 00:26:54,880 --> 00:27:00,720 and i am going to go in my search box 653 00:26:57,278 --> 00:27:02,400 here and type c and c plus i can even 654 00:27:00,720 --> 00:27:05,360 type c plus plus it doesn't really 655 00:27:02,400 --> 00:27:08,798 matter and this is an area where we can 656 00:27:05,359 --> 00:27:11,199 install extensions to make visual studio 657 00:27:08,798 --> 00:27:13,759 code do all kinds of crazy things in 658 00:27:11,200 --> 00:27:16,480 this case we are interested in making it 659 00:27:13,759 --> 00:27:18,319 support c and c plus plus so we are 660 00:27:16,480 --> 00:27:21,120 going to click on this extension that 661 00:27:18,319 --> 00:27:23,439 says microsoft here cnc plus plus i am 662 00:27:21,119 --> 00:27:25,199 going to click on this and if you don't 663 00:27:23,440 --> 00:27:27,120 have it installed you can click on the 664 00:27:25,200 --> 00:27:29,120 install button and it is going to 665 00:27:27,119 --> 00:27:32,079 install this little thing and make your 666 00:27:29,119 --> 00:27:34,398 computer ready to use visual studio code 667 00:27:32,079 --> 00:27:37,038 for c plus plus so make sure you install 668 00:27:34,398 --> 00:27:39,519 this and at the end it is going to say 669 00:27:37,038 --> 00:27:43,038 disable or uninstall because it will be 670 00:27:39,519 --> 00:27:45,599 already installed and enabled on your 671 00:27:43,038 --> 00:27:47,839 computer and this is really the end of 672 00:27:45,599 --> 00:27:49,678 our installation video for visual studio 673 00:27:47,839 --> 00:27:51,918 code we are going to head over in the 674 00:27:49,679 --> 00:27:54,880 next video and show you how you can make 675 00:27:51,919 --> 00:27:57,360 a visual studio code use the compilers 676 00:27:54,880 --> 00:27:59,440 that we installed in the previous video 677 00:27:57,359 --> 00:28:01,439 so go ahead and finish up here and meet 678 00:28:01,440 --> 00:28:06,320 in this video we're going to see how to 679 00:28:03,119 --> 00:28:09,439 connect our visual studio code editor to 680 00:28:06,319 --> 00:28:11,839 a compiler so that we can compile our 681 00:28:09,440 --> 00:28:14,240 projects easily through the visual 682 00:28:11,839 --> 00:28:17,038 studio code editor we already have the 683 00:28:14,240 --> 00:28:18,960 ide installed from the last two previous 684 00:28:17,038 --> 00:28:20,480 lectures when we have a compiler 685 00:28:18,960 --> 00:28:22,399 installed in the last lecture we 686 00:28:20,480 --> 00:28:25,839 installed the gcc compiler through the 687 00:28:22,398 --> 00:28:27,678 mingw project we installed the clangela 688 00:28:25,839 --> 00:28:30,079 vm compiler we even installed the 689 00:28:27,679 --> 00:28:32,640 compiler from microsoft now we're going 690 00:28:30,079 --> 00:28:35,199 to connect the ide to the compiler to 691 00:28:32,640 --> 00:28:38,399 make it really easy to compile c plus 692 00:28:35,200 --> 00:28:40,319 plus applications using these tools here 693 00:28:38,398 --> 00:28:42,239 what i am going to do is really set up a 694 00:28:40,319 --> 00:28:45,359 template project we're going to be 695 00:28:42,240 --> 00:28:48,079 reusing throughout this course here so i 696 00:28:45,359 --> 00:28:50,240 am going to go on a folder in my system 697 00:28:48,079 --> 00:28:52,240 and i am going to right click on the 698 00:28:55,278 --> 00:28:59,519 let's do this this is going to open this 699 00:28:57,359 --> 00:29:02,079 folder in visual studio code and i am 700 00:28:59,519 --> 00:29:04,240 going to trust this folder here be 701 00:29:02,079 --> 00:29:06,000 careful what you trust because some 702 00:29:04,240 --> 00:29:08,159 things are going to do some malicious 703 00:29:06,000 --> 00:29:11,038 things on your computer make sure you 704 00:29:08,159 --> 00:29:12,960 trust the folders that you open up so i 705 00:29:11,038 --> 00:29:15,759 am going to trust myself because i am 706 00:29:12,960 --> 00:29:18,159 the creator of this folder here and this 707 00:29:15,759 --> 00:29:20,558 is going to open visual studio code 708 00:29:18,159 --> 00:29:22,640 now in our install video we installed 709 00:29:20,558 --> 00:29:24,839 the c plus plus extension for visual 710 00:29:22,640 --> 00:29:27,120 studio code again if you don't have this 711 00:29:24,839 --> 00:29:29,199 installed please make sure you go back 712 00:29:27,119 --> 00:29:32,719 and install and make sure this is saying 713 00:29:29,200 --> 00:29:34,558 that this is enabled and now we want to 714 00:29:32,720 --> 00:29:36,798 use this to write c plus plus 715 00:29:34,558 --> 00:29:39,200 applications here we're going to write a 716 00:29:36,798 --> 00:29:42,240 simple toy c plus plus application to 717 00:29:39,200 --> 00:29:44,798 just test that our compilers support c 718 00:29:42,240 --> 00:29:46,798 plus plus 20 and then we're going to use 719 00:29:44,798 --> 00:29:49,599 this as a starting point or as a 720 00:29:46,798 --> 00:29:52,079 template project so i want you to click 721 00:29:52,079 --> 00:29:56,158 and we're going to create a file called 722 00:29:56,159 --> 00:30:01,520 and once you do that you will be able to 723 00:29:58,880 --> 00:30:03,120 type c plus plus code in this year so we 724 00:30:01,519 --> 00:30:05,519 are just starting out here i would 725 00:30:03,119 --> 00:30:08,639 recommend to type this exactly as i do 726 00:30:05,519 --> 00:30:11,519 here so we're going to say include and 727 00:30:08,640 --> 00:30:12,720 say opening angle bracket and say io 728 00:30:12,720 --> 00:30:15,839 and we're going to go down here and say 729 00:30:15,839 --> 00:30:23,519 and we're going to go inside these curly 730 00:30:18,960 --> 00:30:26,240 braces and say auto result and you are 731 00:30:23,519 --> 00:30:27,759 going to exactly type what i tap here 732 00:30:26,240 --> 00:30:30,159 don't worry we're going to have a chance 733 00:30:27,759 --> 00:30:33,440 to learn about all these things here so 734 00:30:30,159 --> 00:30:36,960 i am going to put a pair of parentheses 735 00:30:33,440 --> 00:30:39,600 and i am going to say 10. and i am going 736 00:30:36,960 --> 00:30:42,159 to put a weird operator here this is 737 00:30:39,599 --> 00:30:44,079 called the spaceship operator it is just 738 00:30:42,159 --> 00:30:46,559 something we can use to test that our 739 00:30:44,079 --> 00:30:48,240 compiler supports it plus plus 20 and 740 00:30:46,558 --> 00:30:50,879 we're going to put a 20 here and then 741 00:30:48,240 --> 00:30:53,200 we're going to try and see if this is 742 00:30:50,880 --> 00:30:55,200 greater than zero and we're going to 743 00:30:53,200 --> 00:30:57,360 print this out we're going to say stdc 744 00:30:55,200 --> 00:30:59,919 out again don't worry if you don't know 745 00:30:57,359 --> 00:31:02,240 what this is this is just a way for us 746 00:30:59,919 --> 00:31:05,200 to test and see if our compiler is going 747 00:31:02,240 --> 00:31:07,278 to be able to use c plus plus 20. we're 748 00:31:07,278 --> 00:31:12,558 and this is going to be our program now 749 00:31:10,319 --> 00:31:14,720 we want to make sure we compile this 750 00:31:12,558 --> 00:31:17,119 program so to bring in your compilers in 751 00:31:14,720 --> 00:31:19,519 visual studio code all you have to do is 752 00:31:17,119 --> 00:31:20,719 come to the terminal menu here so please 753 00:31:22,558 --> 00:31:27,200 an option to configure tasks i want you 754 00:31:27,200 --> 00:31:30,720 and once you do this you're going to see 755 00:31:28,798 --> 00:31:33,918 that visual studio code is going to find 756 00:31:30,720 --> 00:31:35,919 the compilers that we just installed you 757 00:31:36,798 --> 00:31:42,960 gcc compiler we installed in mingw you 758 00:31:40,000 --> 00:31:45,200 see that it has found the clan compiler 759 00:31:42,960 --> 00:31:46,720 i know about the g plus plus compiler 760 00:31:45,200 --> 00:31:50,080 because you can see the path here you 761 00:31:46,720 --> 00:31:52,798 can see c major w ben g plus plus dot 762 00:31:50,079 --> 00:31:55,278 exe this is where we installed our g 763 00:31:52,798 --> 00:31:56,960 plus plus compiler and i know about the 764 00:31:55,278 --> 00:31:59,119 clank compiler because that's what's 765 00:31:56,960 --> 00:32:02,480 being found here for example on this 766 00:31:59,119 --> 00:32:05,278 entry here you see it says cmajw bin 767 00:32:02,480 --> 00:32:07,679 clan plus plus so we wanted to configure 768 00:32:05,278 --> 00:32:10,079 these two compilers here and later 769 00:32:07,679 --> 00:32:12,480 visual studio could use them to build 770 00:32:12,480 --> 00:32:17,038 now what we're going to do is click on 771 00:32:14,240 --> 00:32:18,880 the g plus plus compiler first 772 00:32:17,038 --> 00:32:22,158 and when we do that this is going to 773 00:32:18,880 --> 00:32:24,640 create a tasks that json file in our 774 00:32:22,159 --> 00:32:27,679 project and you see that it has created 775 00:32:24,640 --> 00:32:30,960 a folder called dot vs code if i click 776 00:32:27,679 --> 00:32:33,440 on my main cpp file here and do reveal 777 00:32:33,440 --> 00:32:37,600 this is going to open up on my system 778 00:32:35,359 --> 00:32:40,240 and you see that this has created a dot 779 00:32:37,599 --> 00:32:43,359 vs code folder and if we go and we're 780 00:32:40,240 --> 00:32:45,120 going to find this tasks.json file this 781 00:32:43,359 --> 00:32:47,839 is going to be our configuration to 782 00:32:45,119 --> 00:32:50,079 configure which compilers that 783 00:32:47,839 --> 00:32:53,678 visual studio code is using to compile 784 00:32:50,079 --> 00:32:55,519 our main.cpp file here so if we go in 785 00:32:53,679 --> 00:32:57,600 it's going to really have everything 786 00:32:55,519 --> 00:32:59,839 ready for us so you're going to see that 787 00:32:57,599 --> 00:33:03,038 it's going to be using our g plus plus 788 00:32:59,839 --> 00:33:05,918 compiler this is the path to it and in 789 00:33:03,038 --> 00:33:08,640 our args options here we have the 790 00:33:05,919 --> 00:33:11,759 options that we pass to the compiler to 791 00:33:08,640 --> 00:33:13,519 compile our program here now i want to 792 00:33:11,759 --> 00:33:15,599 change this to say which kind of 793 00:33:15,599 --> 00:33:19,839 so we're going to change the level here 794 00:33:19,839 --> 00:33:24,398 g plus plus and say the version i think 795 00:33:22,079 --> 00:33:26,960 that's going to be more descriptive here 796 00:33:24,398 --> 00:33:28,959 so we're going to save world with g plus 797 00:33:31,159 --> 00:33:36,240 11.2.0 because that's our version here 798 00:33:34,480 --> 00:33:38,399 and the rest is really going to be 799 00:33:36,240 --> 00:33:40,720 enough so let's try and see if we can 800 00:33:38,398 --> 00:33:42,639 compile our program here and see the 801 00:33:43,519 --> 00:33:48,798 the other thing we need to do because 802 00:33:45,519 --> 00:33:51,679 this program is using c plus plus 20 we 803 00:33:48,798 --> 00:33:54,480 need to configure our compiler to use c 804 00:33:51,679 --> 00:33:56,559 plus plus 20. and one way to do that in 805 00:33:54,480 --> 00:33:58,000 gcc and you can really search these 806 00:33:58,000 --> 00:34:04,000 if we come to our search engine here and 807 00:34:00,480 --> 00:34:06,240 say configure gcc to use c plus plus 20 808 00:34:06,240 --> 00:34:11,039 and say c plus plus 20. you can search 809 00:34:08,719 --> 00:34:13,358 these things and find information and it 810 00:34:11,039 --> 00:34:14,159 is a really good way to test your things 811 00:34:14,159 --> 00:34:18,878 so let's see if we can find a link to 812 00:34:16,639 --> 00:34:22,000 help out we have a stack overflow link 813 00:34:18,878 --> 00:34:23,519 here so we can crack this open and if we 814 00:34:22,000 --> 00:34:25,918 go and we're going to find a batch of 815 00:34:23,519 --> 00:34:28,079 things for example we can say std c plus 816 00:34:25,918 --> 00:34:31,199 plus 20 here as an option to our 817 00:34:28,079 --> 00:34:33,919 compiler and this is going to bring 818 00:34:31,199 --> 00:34:36,239 c plus plus 20 support in our gcc 819 00:34:33,918 --> 00:34:38,559 compiler so i am going to copy this 820 00:34:36,239 --> 00:34:40,479 and i am going to come back in my visual 821 00:34:40,480 --> 00:34:45,760 and i am going to pass a flag to boiled 822 00:34:43,280 --> 00:34:48,480 in c plus 20 mode and it is something i 823 00:34:45,760 --> 00:34:50,399 just got from stack overflow here and we 824 00:34:48,480 --> 00:34:52,719 are going to pass this to our compiler 825 00:34:50,398 --> 00:34:54,559 and put a comma to make this a valid 826 00:34:52,719 --> 00:34:55,759 configuration file and i am going to 827 00:34:55,760 --> 00:35:00,000 now once we have this open and we want 828 00:34:58,400 --> 00:35:01,680 to build this we need to bring a 829 00:35:00,000 --> 00:35:03,358 terminal window so we we're going to 830 00:35:01,679 --> 00:35:05,679 bring the terminal window so that we can 831 00:35:03,358 --> 00:35:07,440 see things happening and we do that by 832 00:35:05,679 --> 00:35:09,039 coming to terminal and saying the new 833 00:35:09,039 --> 00:35:14,639 and we will come back to terminal again 834 00:35:11,760 --> 00:35:16,560 and say that we want to run a task in 835 00:35:14,639 --> 00:35:20,400 this case we're going to be running our 836 00:35:16,559 --> 00:35:22,078 world with gcc 1120 again if you are 837 00:35:20,400 --> 00:35:25,280 using a newer version that's what you 838 00:35:22,079 --> 00:35:27,359 should use and if we click on this thing 839 00:35:25,280 --> 00:35:29,599 please make sure you have the main cpp 840 00:35:27,358 --> 00:35:31,598 file selected before you do this so i 841 00:35:29,599 --> 00:35:34,079 have this selected and i'm going to go 842 00:35:31,599 --> 00:35:36,079 to run task and i am going to click on 843 00:35:34,079 --> 00:35:37,760 this little thing you're going to see 844 00:35:36,079 --> 00:35:40,640 that this is going to kick off the 845 00:35:40,639 --> 00:35:43,358 and we're going to wait for this to 846 00:35:41,920 --> 00:35:46,159 finish and you're going to see that it 847 00:35:43,358 --> 00:35:47,440 is using c plus plus 20 here we're going 848 00:35:46,159 --> 00:35:51,199 to see that the world finished 849 00:35:47,440 --> 00:35:55,358 successfully and we have a main.exe file 850 00:35:51,199 --> 00:35:58,480 created here so if we go in our terminal 851 00:35:55,358 --> 00:35:59,358 in visual studio code and click enter 852 00:35:59,358 --> 00:36:03,358 type clear and hit enter this is going 853 00:36:01,599 --> 00:36:05,920 to clear the screen this is something 854 00:36:03,358 --> 00:36:09,759 you can do to clear things out we can 855 00:36:05,920 --> 00:36:12,320 run this main.exe binary directly here 856 00:36:09,760 --> 00:36:14,880 so we're going to run exe here and if 857 00:36:12,320 --> 00:36:17,200 you run it it's going to say zero and 858 00:36:14,880 --> 00:36:19,838 this is an indication that we have our 859 00:36:17,199 --> 00:36:22,319 visual studio code editor connected up 860 00:36:19,838 --> 00:36:24,960 to our gcc compiler and we can use that 861 00:36:22,320 --> 00:36:27,039 to compile c plus plus applications and 862 00:36:24,960 --> 00:36:30,079 this is really the configuration with it 863 00:36:27,039 --> 00:36:32,079 now you may ask how do i know this how 864 00:36:30,079 --> 00:36:33,760 do i did i know the steps to follow to 865 00:36:33,760 --> 00:36:37,920 well if you come to extension tab here 866 00:36:36,079 --> 00:36:39,599 and before we even do that i want to 867 00:36:37,920 --> 00:36:42,159 bring to your attention that we have a 868 00:36:39,599 --> 00:36:44,960 bunch of tabs here in visual studio code 869 00:36:42,159 --> 00:36:47,679 we have a tab that says file i think the 870 00:36:44,960 --> 00:36:50,400 one here saying explorer we have a one 871 00:36:47,679 --> 00:36:52,480 we can use for version control if you 872 00:36:50,400 --> 00:36:54,400 use something like git or whatever you 873 00:36:52,480 --> 00:36:56,400 can use this but in this course we're 874 00:36:54,400 --> 00:36:58,800 mostly going to be using the explorer 875 00:36:58,800 --> 00:37:03,280 extensions tab to install things and at 876 00:37:01,358 --> 00:37:06,000 a later time i will show you how to use 877 00:37:03,280 --> 00:37:07,519 the debug tab here or run a debug tab 878 00:37:06,000 --> 00:37:10,320 but in most cases we're going to be 879 00:37:07,519 --> 00:37:12,239 using the explorer tab or the extensions 880 00:37:10,320 --> 00:37:14,320 tab here if you want you can click on 881 00:37:12,239 --> 00:37:16,479 this to close this so that you have some 882 00:37:14,320 --> 00:37:18,000 more space you can click again to bring 883 00:37:16,480 --> 00:37:20,240 this back these are some things you can 884 00:37:18,000 --> 00:37:22,880 do in visual studio code but i am going 885 00:37:20,239 --> 00:37:24,879 to show you how to configure your 886 00:37:22,880 --> 00:37:27,200 compilers where i got the information so 887 00:37:27,199 --> 00:37:30,639 and we're going to find our cnc plus 888 00:37:30,639 --> 00:37:35,759 if we scroll down we're going to find 889 00:37:32,960 --> 00:37:37,838 tutorials on how to set up this on 890 00:37:35,760 --> 00:37:39,440 different compilers for example you can 891 00:37:39,440 --> 00:37:44,240 to see how to set up a visual studio 892 00:37:41,358 --> 00:37:46,639 code to use your compiler from microsoft 893 00:37:44,239 --> 00:37:49,439 if we come back we can see a link to see 894 00:37:46,639 --> 00:37:51,679 how to use this with mingw on windows 895 00:37:51,679 --> 00:37:56,239 and let's see what else we have we can 896 00:37:53,920 --> 00:37:58,639 come back to here and we can see how to 897 00:37:56,239 --> 00:38:00,559 use this with gcc on linux or clango on 898 00:37:58,639 --> 00:38:02,960 a mac you can really choose whatever is 899 00:38:00,559 --> 00:38:05,838 relevant for your operating system but 900 00:38:02,960 --> 00:38:07,519 in this case here we are just using the 901 00:38:05,838 --> 00:38:10,719 windows operating system so we're going 902 00:38:07,519 --> 00:38:13,440 to see how to use this with the mingw 903 00:38:10,719 --> 00:38:16,480 compiler that we just installed now we 904 00:38:13,440 --> 00:38:18,639 can use the mingw compiler and i would 905 00:38:16,480 --> 00:38:21,039 recommend reading up on this entire 906 00:38:18,639 --> 00:38:24,159 thing to have an idea on what is 907 00:38:21,039 --> 00:38:26,239 happening we already have our g plus 908 00:38:24,159 --> 00:38:28,159 plus compiler so this is this command is 909 00:38:26,239 --> 00:38:30,719 going to work again if we come back to 910 00:38:28,159 --> 00:38:32,480 terminal here and run g plus plus 911 00:38:30,719 --> 00:38:34,159 version i think we can see that we run 912 00:38:32,480 --> 00:38:37,280 these commands here to see these things 913 00:38:34,159 --> 00:38:39,358 working so we have this step here and if 914 00:38:37,280 --> 00:38:41,040 you wonder what gdb is it is just a 915 00:38:39,358 --> 00:38:43,519 debugger you can use to debug your 916 00:38:41,039 --> 00:38:47,199 programs and again if we come to our 917 00:38:47,199 --> 00:38:51,118 version we're going to have this 918 00:38:48,719 --> 00:38:53,358 available from our mingw installation 919 00:38:51,119 --> 00:38:55,680 you can see that we have this here so 920 00:38:53,358 --> 00:38:58,159 this is working just fine you can really 921 00:38:55,679 --> 00:39:00,639 follow this and configure your visual 922 00:38:58,159 --> 00:39:02,480 studio code installation but i am just 923 00:39:00,639 --> 00:39:04,799 showing you how to do this pretty fast 924 00:39:02,480 --> 00:39:07,358 you can see that this is what we set up 925 00:39:04,800 --> 00:39:10,720 in our configuration file which is our 926 00:39:07,358 --> 00:39:14,078 tasks dot json file now one thing you 927 00:39:10,719 --> 00:39:17,039 can do is modify the task.json file to 928 00:39:14,079 --> 00:39:18,480 build every single file in your project 929 00:39:17,039 --> 00:39:20,960 and this is something that is going to 930 00:39:18,480 --> 00:39:23,440 make things easier what i'm going to do 931 00:39:20,960 --> 00:39:25,760 is go back to our visual studio code 932 00:39:23,440 --> 00:39:28,559 instance and find our project we're 933 00:39:25,760 --> 00:39:31,200 going to crack open our tasks that json 934 00:39:28,559 --> 00:39:32,320 file which is living in our dot vs code 935 00:39:32,320 --> 00:39:37,200 and i am going to take out the craziness 936 00:39:34,639 --> 00:39:39,838 i have in my arguments here i am going 937 00:39:37,199 --> 00:39:41,199 to select everything i am going to 938 00:39:41,199 --> 00:39:46,000 and i am going to put in a configuration 939 00:39:43,760 --> 00:39:48,560 that i am using for my projects here to 940 00:39:46,000 --> 00:39:50,639 make this super easy what this is doing 941 00:39:48,559 --> 00:39:53,519 it is telling the compiler to build in c 942 00:39:50,639 --> 00:39:55,039 plus plus 20 mode it is going to tell 943 00:39:53,519 --> 00:39:57,838 visual studio it is going to tell the 944 00:39:55,039 --> 00:40:00,239 compiler to build every single cpp file 945 00:39:57,838 --> 00:40:01,679 in our project so if we have multiple 946 00:40:00,239 --> 00:40:04,078 files they are going to be built 947 00:40:01,679 --> 00:40:06,879 together and we're going to specify the 948 00:40:04,079 --> 00:40:09,039 name for our output file for example if 949 00:40:06,880 --> 00:40:11,920 we don't want our binary executable to 950 00:40:09,039 --> 00:40:15,199 be named mainly.exe we can give it a 951 00:40:11,920 --> 00:40:17,599 name and i am going to be naming mine 952 00:40:15,199 --> 00:40:19,358 rooster.exe in the course here i would 953 00:40:17,599 --> 00:40:20,720 recommend doing the same so that we can 954 00:40:20,719 --> 00:40:24,879 the same habits as we go through the 955 00:40:23,039 --> 00:40:26,880 course here but of course you are free 956 00:40:24,880 --> 00:40:29,119 to use whatever you want in your c plus 957 00:40:26,880 --> 00:40:31,760 plus programs here so once you have this 958 00:40:29,119 --> 00:40:33,838 you can click save here and this is 959 00:40:31,760 --> 00:40:35,359 going to save our json file here i can 960 00:40:35,358 --> 00:40:39,279 and i am going to bring my terminal by 961 00:40:39,280 --> 00:40:44,160 and the terminal here this is going to 962 00:40:41,280 --> 00:40:46,800 bring back my terminal that i had before 963 00:40:44,159 --> 00:40:48,480 and i am going to run the clear command 964 00:40:48,480 --> 00:40:52,400 and i am going to do dir to show the 965 00:40:52,400 --> 00:40:58,160 of my folder on windows here i wanted to 966 00:40:55,440 --> 00:41:00,639 remove the main.exe file from the 967 00:40:58,159 --> 00:41:03,039 terminal here so what i am going to do 968 00:41:03,039 --> 00:41:07,358 and i am going to say the name of the 969 00:41:05,519 --> 00:41:08,239 file i want to remove i am going to say 970 00:41:08,239 --> 00:41:12,959 dot exe and if i hit enter notice what 971 00:41:11,358 --> 00:41:15,440 is going to happen the file is going to 972 00:41:12,960 --> 00:41:17,440 go away i don't see it anymore here i 973 00:41:15,440 --> 00:41:21,039 want to build again using my new 974 00:41:17,440 --> 00:41:23,920 configurations in my tasks the json file 975 00:41:21,039 --> 00:41:26,400 i am going to run the task to build with 976 00:41:26,400 --> 00:41:30,480 this is going to build again and you see 977 00:41:28,318 --> 00:41:32,880 that it is using the configurations that 978 00:41:30,480 --> 00:41:35,760 i passed you see that it is building 979 00:41:32,880 --> 00:41:38,000 every single cpp file in the directory 980 00:41:35,760 --> 00:41:40,800 here and it is going to generate a 981 00:41:38,000 --> 00:41:42,880 program called rooster.exe and we if we 982 00:41:40,800 --> 00:41:45,280 look here we're going to find it we can 983 00:41:45,280 --> 00:41:50,640 and we can type clear to clear things 984 00:41:47,599 --> 00:41:53,200 out and if we do the ir again we're 985 00:41:50,639 --> 00:41:55,719 going to see that we have rooster.exe 986 00:41:55,719 --> 00:42:00,078 rooster.exe here and this is going to do 987 00:41:58,079 --> 00:42:02,160 what we want this is really all you need 988 00:42:00,079 --> 00:42:05,119 to do to connect visual studio code to 989 00:42:02,159 --> 00:42:07,199 your gcc compiler now we're going to 990 00:42:05,119 --> 00:42:09,200 connect visual studio code to our clan 991 00:42:07,199 --> 00:42:10,799 compiler that we have installed we 992 00:42:09,199 --> 00:42:12,318 really want to be able to use it with 993 00:42:10,800 --> 00:42:14,240 our project here and we're going to go 994 00:42:12,318 --> 00:42:15,759 through the seven steps again so we're 995 00:42:14,239 --> 00:42:18,159 going to go to terminal and say 996 00:42:18,159 --> 00:42:22,960 and we're going to choose something that 997 00:42:19,838 --> 00:42:26,639 says the clanger compiler and we're 998 00:42:22,960 --> 00:42:29,679 going to find clang plus plus so let's 999 00:42:26,639 --> 00:42:32,358 hunt for that i have something that says 1000 00:42:32,358 --> 00:42:36,719 cloneplusplus.exe i am going to click on 1001 00:42:34,480 --> 00:42:39,079 that and if i do this this is going to 1002 00:42:39,079 --> 00:42:43,280 tasks.json file so watch out for this i 1003 00:42:43,280 --> 00:42:48,880 and let's crack this test that json file 1004 00:42:46,960 --> 00:42:50,480 open and you're going to see that this 1005 00:42:48,880 --> 00:42:54,000 is going to also set up another 1006 00:42:50,480 --> 00:42:56,159 configuration to world using my clang 1007 00:42:56,159 --> 00:43:00,799 and again i can edit this out a little 1008 00:42:58,159 --> 00:43:02,639 bit to say what we want let's look at 1009 00:43:00,800 --> 00:43:06,720 the version of cloud we have installed 1010 00:43:02,639 --> 00:43:07,838 to say that out this is going to be 12 1011 00:43:07,838 --> 00:43:11,759 1 so we're going to say world with clank 1012 00:43:11,760 --> 00:43:19,119 12 0 1 let's say that here in our level 1013 00:43:16,119 --> 00:43:20,960 12.0.1 we can say that right here and 1014 00:43:19,119 --> 00:43:23,599 you see that it is really doing the same 1015 00:43:20,960 --> 00:43:26,240 thing we did with our gcc compiler so 1016 00:43:23,599 --> 00:43:29,680 what i can really do is still everything 1017 00:43:26,239 --> 00:43:30,959 i did in my gcc compiler i can 1018 00:43:30,960 --> 00:43:36,800 and i am going to take out the arguments 1019 00:43:33,358 --> 00:43:39,358 i have in my args here and i use what i 1020 00:43:36,800 --> 00:43:41,359 have in my gcc compiler you can see that 1021 00:43:39,358 --> 00:43:44,000 you can use the client compiler and the 1022 00:43:41,358 --> 00:43:46,559 gcc compiler in the same way and this is 1023 00:43:44,000 --> 00:43:48,559 something really cool so now we have the 1024 00:43:46,559 --> 00:43:50,719 option to use two compilers and this is 1025 00:43:48,559 --> 00:43:52,480 really cool so we're going to close the 1026 00:43:50,719 --> 00:43:54,480 json file and it's going to be saved 1027 00:43:54,480 --> 00:43:58,000 and we're going to bring up our terminal 1028 00:43:58,000 --> 00:44:03,358 and let's bring this up a little bit and 1029 00:44:00,159 --> 00:44:05,598 we're going to do dir on windows and 1030 00:44:03,358 --> 00:44:08,480 we're going to remove the binary again 1031 00:44:05,599 --> 00:44:10,400 we're going to remove roster.exe 1032 00:44:08,480 --> 00:44:12,240 and again what you can do you can start 1033 00:44:10,400 --> 00:44:15,119 typing the name of the file you want to 1034 00:44:12,239 --> 00:44:16,799 remove and hit tab if you do that the 1035 00:44:15,119 --> 00:44:18,960 terminal is going to autocomplete this 1036 00:44:16,800 --> 00:44:20,880 for you and this is really cool so if i 1037 00:44:18,960 --> 00:44:23,440 do this the file is going to go away you 1038 00:44:20,880 --> 00:44:25,200 see that it is going right here 1039 00:44:25,199 --> 00:44:29,358 and what i am going to do is world with 1040 00:44:27,039 --> 00:44:31,838 the clan compiler can do that by coming 1041 00:44:29,358 --> 00:44:34,719 to terminal i am going to say that i 1042 00:44:31,838 --> 00:44:37,119 want to run a task and i am going to be 1043 00:44:34,719 --> 00:44:39,358 given an option i can either build with 1044 00:44:37,119 --> 00:44:42,640 gcc so if i choose gcc here it's going 1045 00:44:39,358 --> 00:44:44,639 to be used but i can also use clan let's 1046 00:44:42,639 --> 00:44:46,159 use clang here so i'm going to click on 1047 00:44:46,159 --> 00:44:49,279 and you're going to see that visual 1048 00:44:47,599 --> 00:44:50,400 studio code is going to kick off the 1049 00:44:50,400 --> 00:44:54,720 this is going to build my binary 1050 00:44:52,159 --> 00:44:56,960 executable and notice that it is using c 1051 00:44:54,719 --> 00:44:58,799 plus plus 20 again the build is going to 1052 00:44:56,960 --> 00:45:00,639 finish successfully and i will have a 1053 00:44:58,800 --> 00:45:04,000 binary here and this is going to be all 1054 00:45:00,639 --> 00:45:07,920 i need to do so if i hit enter and run 1055 00:45:07,920 --> 00:45:12,079 and hitting tab this is going to 1056 00:45:10,000 --> 00:45:14,639 auto-complete if i run it it's going to 1057 00:45:12,079 --> 00:45:16,400 say zero and this is what we expect here 1058 00:45:14,639 --> 00:45:18,480 if you have some kind of error please 1059 00:45:16,400 --> 00:45:20,639 try and fix it if you have a problem you 1060 00:45:18,480 --> 00:45:22,240 can ask me and i will do the best i can 1061 00:45:22,239 --> 00:45:26,239 okay now we have our two compilers 1062 00:45:24,000 --> 00:45:29,358 working we need to configure the 1063 00:45:26,239 --> 00:45:31,358 compiler from microsoft now the compiler 1064 00:45:29,358 --> 00:45:33,358 from microsoft is a little bit weird 1065 00:45:31,358 --> 00:45:35,598 again you can read up on this by coming 1066 00:45:33,358 --> 00:45:37,759 to your extension documentation 1067 00:45:35,599 --> 00:45:39,920 and going to using the compiler from 1068 00:45:37,760 --> 00:45:41,680 microsoft with visual studio code on 1069 00:45:39,920 --> 00:45:43,838 windows you can read up on all this if 1070 00:45:41,679 --> 00:45:46,159 you want but i am just going to show you 1071 00:45:43,838 --> 00:45:48,559 a quick way to get this running so the 1072 00:45:46,159 --> 00:45:51,358 compiler from microsoft is special in 1073 00:45:48,559 --> 00:45:53,440 that you have to specify the path to it 1074 00:45:51,358 --> 00:45:55,159 for example if we open a regular 1075 00:45:58,318 --> 00:46:01,480 we're going to see that the terminal is 1076 00:46:01,480 --> 00:46:07,119 cl.exe compiler is not recognized and 1077 00:46:04,880 --> 00:46:09,519 the reason is we don't have the proper 1078 00:46:07,119 --> 00:46:11,838 environment variable settings for the 1079 00:46:09,519 --> 00:46:14,159 operating system to find this compiler 1080 00:46:11,838 --> 00:46:16,559 when we call it right here we are able 1081 00:46:14,159 --> 00:46:19,039 to use g plus plus and clan because we 1082 00:46:16,559 --> 00:46:20,719 put the environment variables in place 1083 00:46:19,039 --> 00:46:22,400 if you remember when we installed them 1084 00:46:20,719 --> 00:46:25,439 we did something like this we came to 1085 00:46:22,400 --> 00:46:28,000 the start menu and did env we came to 1086 00:46:25,440 --> 00:46:29,200 environment variables here and we put 1087 00:46:29,199 --> 00:46:33,919 to our compilers in our environment 1088 00:46:32,159 --> 00:46:36,159 variables here you can see them right 1089 00:46:33,920 --> 00:46:37,920 here so we don't have the same thing for 1090 00:46:36,159 --> 00:46:40,078 the compiler from microsoft and that's 1091 00:46:37,920 --> 00:46:42,000 for a reason because we may have 1092 00:46:40,079 --> 00:46:43,359 multiple compilers installed from 1093 00:46:43,358 --> 00:46:47,199 then if you have multiple compilers it 1094 00:46:45,440 --> 00:46:49,838 would be really cumbersome to choose 1095 00:46:47,199 --> 00:46:52,480 which compiler you want the guys at 1096 00:46:49,838 --> 00:46:54,799 microsoft opted for the option for you 1097 00:46:52,480 --> 00:46:57,679 the developer to specify which compiler 1098 00:46:54,800 --> 00:47:00,480 you want to run by coming to the start 1099 00:46:57,679 --> 00:47:02,559 menu here and typing developer 1100 00:47:00,480 --> 00:47:04,960 and choose which powershell for the 1101 00:47:02,559 --> 00:47:07,358 compiler you want to use for example if 1102 00:47:04,960 --> 00:47:09,599 we want to use the visual studio 2019 1103 00:47:07,358 --> 00:47:11,519 compiler we can click on this and then 1104 00:47:13,119 --> 00:47:17,440 the c plus plus compiler from microsoft 1105 00:47:15,358 --> 00:47:20,880 for example if we wait for this to be 1106 00:47:20,880 --> 00:47:23,920 we're going to see that we're going to 1107 00:47:22,159 --> 00:47:26,239 have a message saying that this is our 1108 00:47:23,920 --> 00:47:28,079 compiler we can use it however we want 1109 00:47:26,239 --> 00:47:30,879 so what we're going to do is set up a 1110 00:47:28,079 --> 00:47:33,039 terminal like this and use this terminal 1111 00:47:30,880 --> 00:47:34,880 to start visual studio code and the 1112 00:47:33,039 --> 00:47:36,079 visual studio code is going to start in 1113 00:47:36,079 --> 00:47:40,000 where it can find the compiler from 1114 00:47:38,000 --> 00:47:41,838 microsoft i know this is confusing but 1115 00:47:40,000 --> 00:47:44,559 please bear with me this is what we need 1116 00:47:41,838 --> 00:47:46,558 to do to get this to work if we start 1117 00:47:44,559 --> 00:47:48,720 visual studio code in a mode where it 1118 00:47:46,559 --> 00:47:51,920 knows about the environment variables 1119 00:47:48,719 --> 00:47:54,799 for the compiler from microsoft we will 1120 00:47:51,920 --> 00:47:56,880 have the ability to say cl.exe 1121 00:47:54,800 --> 00:47:58,880 and we will be able to use that right 1122 00:47:56,880 --> 00:48:01,358 here but you see that it is not 1123 00:47:58,880 --> 00:48:03,358 recognized now but we can use the g plus 1124 00:48:04,480 --> 00:48:08,480 we're going to see that it's available 1125 00:48:05,838 --> 00:48:10,960 we can use it we can use clan plus plus 1126 00:48:08,480 --> 00:48:12,719 and if we do version as well we're going 1127 00:48:10,960 --> 00:48:15,119 to see that we can use this little guy 1128 00:48:12,719 --> 00:48:17,759 here we also want the ability to use the 1129 00:48:15,119 --> 00:48:20,079 compiler from microsoft but to have 1130 00:48:17,760 --> 00:48:22,640 access to that we will have to close the 1131 00:48:20,079 --> 00:48:24,160 visual studio code instance we have here 1132 00:48:22,639 --> 00:48:27,039 and we're going to come back to our 1133 00:48:24,159 --> 00:48:28,558 project let's see if i can find it here 1134 00:48:27,039 --> 00:48:30,719 so we're going to go to the location 1135 00:48:28,559 --> 00:48:32,319 where we have our project and we're 1136 00:48:30,719 --> 00:48:34,719 going to change to the location of the 1137 00:48:32,318 --> 00:48:37,440 project here so i can grab the location 1138 00:48:34,719 --> 00:48:39,838 from my visual studio code project that 1139 00:48:37,440 --> 00:48:41,920 i just set up i am going to come back to 1140 00:48:39,838 --> 00:48:44,078 my powershell window that knows about 1141 00:48:41,920 --> 00:48:46,800 the path to the c plus plus compiler 1142 00:48:44,079 --> 00:48:49,200 from microsoft and i am going to change 1143 00:48:46,800 --> 00:48:51,280 it to the location of my project here so 1144 00:48:49,199 --> 00:48:53,919 i am going to say cd and paste in the 1145 00:48:51,280 --> 00:48:56,000 path i just copied and if i hit enter 1146 00:48:53,920 --> 00:48:58,240 this is going to change to my location 1147 00:48:56,000 --> 00:49:00,239 and i really recommend using powershell 1148 00:48:58,239 --> 00:49:02,399 if you have access to this because it is 1149 00:49:00,239 --> 00:49:05,199 a little more convenient to use it 1150 00:49:02,400 --> 00:49:07,599 supports more commands that you can use 1151 00:49:05,199 --> 00:49:10,239 in the command prompt but again that's 1152 00:49:07,599 --> 00:49:13,280 going to be your choice now i am in the 1153 00:49:10,239 --> 00:49:14,959 location for my project if i do ls or 1154 00:49:14,960 --> 00:49:19,039 you can see that i can see my main cpp 1155 00:49:17,039 --> 00:49:22,079 file can see the binary we generated 1156 00:49:19,039 --> 00:49:24,800 earlier but this is not problem so what 1157 00:49:24,800 --> 00:49:29,680 and i say a dot and this is going to 1158 00:49:27,039 --> 00:49:32,480 start visual studio code in the current 1159 00:49:29,679 --> 00:49:34,159 folder of our project year so if you hit 1160 00:49:32,480 --> 00:49:36,079 enter we're going to wait for visual 1161 00:49:34,159 --> 00:49:38,239 studio code to come up it is going to 1162 00:49:36,079 --> 00:49:39,280 come up with the project we had in that 1163 00:49:41,358 --> 00:49:45,039 starting visual studio code this way is 1164 00:49:43,679 --> 00:49:47,519 that it's going to know about the 1165 00:49:45,039 --> 00:49:49,358 compiler from microsoft if we come back 1166 00:49:52,159 --> 00:49:56,078 you're going to see that now it knows 1167 00:49:53,838 --> 00:49:58,558 about the compiler from microsoft and we 1168 00:49:56,079 --> 00:50:00,400 can use it to do things so let's set 1169 00:49:58,559 --> 00:50:02,720 this up again if you want you can read 1170 00:50:02,719 --> 00:50:07,279 on the documentation from visual studio 1171 00:50:05,199 --> 00:50:08,879 code itself but again i am going to show 1172 00:50:07,280 --> 00:50:11,200 you how to do this you can see that it 1173 00:50:08,880 --> 00:50:13,358 is really the same things they are doing 1174 00:50:11,199 --> 00:50:15,519 here so let's come back to visual studio 1175 00:50:13,358 --> 00:50:18,400 code and we're going to go to terminal 1176 00:50:15,519 --> 00:50:20,719 and we want to configure a task 1177 00:50:18,400 --> 00:50:22,559 now you see that we have the compiler 1178 00:50:20,719 --> 00:50:25,519 from microsoft available you can see 1179 00:50:22,559 --> 00:50:28,599 that we can use that right here so what 1180 00:50:31,358 --> 00:50:35,759 and this is going to add a new entry in 1181 00:50:33,358 --> 00:50:37,920 our configuration file and it is going 1182 00:50:35,760 --> 00:50:40,800 to open up if we go here we are going to 1183 00:50:37,920 --> 00:50:43,200 find that we have a command to build 1184 00:50:40,800 --> 00:50:45,359 using the compiler from microsoft so 1185 00:50:43,199 --> 00:50:48,318 let's change the level here and i say 1186 00:50:45,358 --> 00:50:50,719 world with msvc i think this is going to 1187 00:50:48,318 --> 00:50:52,880 be more descriptive and this is really 1188 00:50:50,719 --> 00:50:55,199 going to build your thing now i am going 1189 00:50:52,880 --> 00:50:57,599 to change this up so that it works 1190 00:50:55,199 --> 00:50:59,919 everything in the current folder 1191 00:50:57,599 --> 00:51:01,359 and again i am going to remove 1192 00:51:01,358 --> 00:51:05,759 configuration that i have ready on my 1193 00:51:03,599 --> 00:51:08,079 system here this is what i use for all 1194 00:51:05,760 --> 00:51:10,319 my projects again this is just calling 1195 00:51:08,079 --> 00:51:12,720 the compiler and giving it options to do 1196 00:51:10,318 --> 00:51:14,719 things we just need to specify that we 1197 00:51:12,719 --> 00:51:17,279 want to use the lattice standard of the 1198 00:51:14,719 --> 00:51:18,639 c plus plus standard and it is going to 1199 00:51:18,639 --> 00:51:22,000 the input files we're going to be 1200 00:51:20,239 --> 00:51:24,399 building everything in the current 1201 00:51:22,000 --> 00:51:26,159 folder this is what we are saying here 1202 00:51:24,400 --> 00:51:28,480 and the output file is going to be 1203 00:51:28,480 --> 00:51:33,119 if we save this by saying ctrl s or 1204 00:51:33,119 --> 00:51:38,720 and close our tasks dot json file we can 1205 00:51:36,559 --> 00:51:41,200 bring up our terminal window by going to 1206 00:51:38,719 --> 00:51:44,318 view and saying the terminal here 1207 00:51:41,199 --> 00:51:46,318 and we can go down and clear we can say 1208 00:51:46,318 --> 00:51:52,800 and if we do rm and say rooster and hit 1209 00:51:50,960 --> 00:51:55,760 tab this is going to remove the binary 1210 00:51:52,800 --> 00:51:58,559 we generated with the clang llvm 1211 00:51:55,760 --> 00:52:01,040 compiler earlier now we want to build 1212 00:51:58,559 --> 00:52:02,559 with the compiler from microsoft so 1213 00:52:01,039 --> 00:52:04,639 let's do this we're going to say 1214 00:52:02,559 --> 00:52:07,680 terminal and we're going to say that we 1215 00:52:04,639 --> 00:52:10,400 want to run a task but this time we want 1216 00:52:07,679 --> 00:52:12,639 to build with msvc this is what we want 1217 00:52:10,400 --> 00:52:14,160 to do here if we click on this little 1218 00:52:12,639 --> 00:52:16,679 thing we're going to kick off the 1219 00:52:14,159 --> 00:52:18,879 process to build with the compiler from 1220 00:52:16,679 --> 00:52:21,759 microsoft and we're going to wait for 1221 00:52:21,760 --> 00:52:25,680 and if we wait we're going to see that 1222 00:52:23,440 --> 00:52:27,119 the world finished successfully and if 1223 00:52:25,679 --> 00:52:30,000 we look in our folder we're going to see 1224 00:52:27,119 --> 00:52:32,318 that we have our output file and we can 1225 00:52:30,000 --> 00:52:34,960 come in our terminal and hit to enter 1226 00:52:32,318 --> 00:52:38,318 and do clear to clear things up and if 1227 00:52:38,318 --> 00:52:43,119 and hit enter you're going to see that 1228 00:52:39,920 --> 00:52:46,400 it's going to say zero and this is 1229 00:52:43,119 --> 00:52:49,119 confirmation that we are able to use our 1230 00:52:46,400 --> 00:52:51,519 visual studio code editor and be able to 1231 00:52:49,119 --> 00:52:53,838 use it with all kinds of crazy compilers 1232 00:52:51,519 --> 00:52:56,239 and this is really cool if you are on 1233 00:52:53,838 --> 00:52:57,759 windows i recommend setting this up 1234 00:52:56,239 --> 00:53:00,399 because it's it's going to make it 1235 00:52:57,760 --> 00:53:02,079 easier to run a c plus plus applications 1236 00:53:02,079 --> 00:53:07,280 state-of-the-art compilers like msvc 1237 00:53:04,880 --> 00:53:09,838 clone and gcc here and this is really 1238 00:53:07,280 --> 00:53:12,240 cool now we have everything we need to 1239 00:53:09,838 --> 00:53:14,318 start running c plus plus applications 1240 00:53:12,239 --> 00:53:16,159 but before i let you go let's show you 1241 00:53:14,318 --> 00:53:18,960 how you can set this up so that you have 1242 00:53:16,159 --> 00:53:21,118 an easier time with regards to 1243 00:53:18,960 --> 00:53:22,720 intellisense and whatever so what we're 1244 00:53:21,119 --> 00:53:24,400 going to do we're going to go to our 1245 00:53:22,719 --> 00:53:25,919 view menu and we're going to choose 1246 00:53:25,920 --> 00:53:30,318 and in this we're going to say c or c 1247 00:53:30,318 --> 00:53:35,039 we can say something like this and we're 1248 00:53:32,480 --> 00:53:36,719 going to choose edit configuration ui 1249 00:53:35,039 --> 00:53:38,079 this is going to give us an option to 1250 00:53:38,079 --> 00:53:41,359 and i want you to configure things your 1251 00:53:43,920 --> 00:53:49,280 let's see the options we have here i am 1252 00:53:46,480 --> 00:53:52,880 only going to have the option for win32 1253 00:53:49,280 --> 00:53:54,640 but if we go down we can specify 1254 00:53:54,639 --> 00:53:57,838 visual studio code configured for for 1255 00:53:56,559 --> 00:54:00,079 example it's going to give us 1256 00:53:57,838 --> 00:54:02,000 intellisense or autocomplete and all 1257 00:54:00,079 --> 00:54:04,240 kinds of greater things using the 1258 00:54:02,000 --> 00:54:06,960 setting we choose here if we want we can 1259 00:54:04,239 --> 00:54:08,959 choose the gcc compiler or whatever i am 1260 00:54:06,960 --> 00:54:11,358 just going to leave in the windows msvc 1261 00:54:08,960 --> 00:54:13,358 compiler because i am on windows here 1262 00:54:11,358 --> 00:54:15,199 but know that you can configure these 1263 00:54:13,358 --> 00:54:17,358 things now if we go down we're going to 1264 00:54:15,199 --> 00:54:20,000 see that we have the option to configure 1265 00:54:17,358 --> 00:54:22,078 the c plus plus standard and the 1266 00:54:20,000 --> 00:54:23,599 compiler path that is going to be used 1267 00:54:22,079 --> 00:54:26,079 to figure things out we're going to be 1268 00:54:23,599 --> 00:54:27,760 using the compiler from microsoft here 1269 00:54:26,079 --> 00:54:29,839 but know that you have the option to 1270 00:54:27,760 --> 00:54:31,520 choose any other compiler for example we 1271 00:54:29,838 --> 00:54:33,599 can choose the g plus plus compiler we 1272 00:54:31,519 --> 00:54:35,599 can choose the clank plus class compiler 1273 00:54:33,599 --> 00:54:37,359 you can configure these things and if 1274 00:54:35,599 --> 00:54:40,079 you need to change this please come back 1275 00:54:37,358 --> 00:54:41,440 here and do these configurations here 1276 00:54:40,079 --> 00:54:43,839 we're going to leave in this whole 1277 00:54:41,440 --> 00:54:46,720 intellisense and what we will do 1278 00:54:43,838 --> 00:54:49,039 is configure visual studio code to use 1279 00:54:46,719 --> 00:54:50,838 the c plus plus 20 standard so we're 1280 00:54:50,838 --> 00:54:55,358 here and if you set this up you're going 1281 00:54:53,519 --> 00:54:58,159 to see that visual studio code is going 1282 00:54:58,159 --> 00:55:02,480 file which is going to store all these 1283 00:55:00,559 --> 00:55:03,760 configurations here so it is really 1284 00:55:03,760 --> 00:55:08,559 a configuration for what we choose in 1285 00:55:06,318 --> 00:55:10,318 our configuration here so make sure you 1286 00:55:08,559 --> 00:55:12,720 choose what makes sense again i would 1287 00:55:10,318 --> 00:55:14,400 recommend using c plus plus 20 but if 1288 00:55:12,719 --> 00:55:16,159 you go on top you can really use any 1289 00:55:14,400 --> 00:55:18,559 compiler you want i am going to use the 1290 00:55:16,159 --> 00:55:20,239 compiler from microsoft this is okay i 1291 00:55:20,239 --> 00:55:24,558 configuration for intellisense here and 1292 00:55:22,559 --> 00:55:26,400 i am going to close this and we are 1293 00:55:24,559 --> 00:55:28,880 really ready to start using visual 1294 00:55:26,400 --> 00:55:31,039 studio code to ride c plus plus 1295 00:55:28,880 --> 00:55:32,798 application so let's clean this up a 1296 00:55:31,039 --> 00:55:35,119 little bit we're going to view our 1297 00:55:32,798 --> 00:55:37,920 terminal and we're going to remove the 1298 00:55:35,119 --> 00:55:40,720 junk that was generated by the compiler 1299 00:55:37,920 --> 00:55:42,559 from microsoft i am going to say rm and 1300 00:55:42,559 --> 00:55:48,559 and i can remove the binary i can say rm 1301 00:55:46,400 --> 00:55:51,039 and say rooster and say tab this is 1302 00:55:48,559 --> 00:55:53,119 going to autocomplete and really remove 1303 00:55:51,039 --> 00:55:56,000 things i don't need i am going to remove 1304 00:55:53,119 --> 00:55:57,680 the pdb file i am going to remove 1305 00:55:57,679 --> 00:56:04,639 you know i am going to remove the main 1306 00:56:00,318 --> 00:56:07,599 object file so let's say main dot obj 1307 00:56:04,639 --> 00:56:10,639 and i am going to remove main dot 1308 00:56:07,599 --> 00:56:13,599 or vc we don't want to remove our cpd 1309 00:56:10,639 --> 00:56:16,239 file so now we have a project we can use 1310 00:56:13,599 --> 00:56:19,039 as a starting point to write c and c 1311 00:56:16,239 --> 00:56:21,519 plus plus applications here so what we 1312 00:56:19,039 --> 00:56:24,719 will be doing in the next few lectures 1313 00:56:21,519 --> 00:56:28,480 or the next few chapters is reusing 1314 00:56:24,719 --> 00:56:31,439 this thing here as a template project so 1315 00:56:31,440 --> 00:56:36,159 c plus plus 20 template project and 1316 00:56:34,719 --> 00:56:37,838 let's do this here i think i need to 1317 00:56:36,159 --> 00:56:40,078 close video studio code otherwise it's 1318 00:56:37,838 --> 00:56:43,039 going to complain so i am going to call 1319 00:56:40,079 --> 00:56:44,798 this little guy c plus plus 20 template 1320 00:56:44,798 --> 00:56:48,559 later when we start learning about c 1321 00:56:46,559 --> 00:56:50,640 plus plus proper we're going to be using 1322 00:56:48,559 --> 00:56:52,960 this as a starting point and we want to 1323 00:56:50,639 --> 00:56:55,598 have to reconfigure the compilers our 1324 00:56:55,599 --> 00:57:00,798 the compilers that we set up here namely 1325 00:56:58,079 --> 00:57:03,039 the compiler from microsoft the g plus 1326 00:57:00,798 --> 00:57:05,199 plus compiler and the clang plus plus 1327 00:57:03,039 --> 00:57:07,440 compiler and that's going to be really 1328 00:57:05,199 --> 00:57:09,199 cool this is really all i have to share 1329 00:57:07,440 --> 00:57:10,960 in this lecture i hope you have visual 1330 00:57:09,199 --> 00:57:13,199 studio code set up to use all these 1331 00:57:10,960 --> 00:57:15,119 compilers we are going to stop here in 1332 00:57:13,199 --> 00:57:16,879 this lecture the next one we're going to 1333 00:57:15,119 --> 00:57:19,280 start and see how to set up your 1334 00:57:16,880 --> 00:57:20,960 development environment on linux and 1335 00:57:19,280 --> 00:57:24,880 we're going to start by installing 1336 00:57:24,880 --> 00:57:29,119 go ahead and finish up here and meet me 1337 00:57:27,039 --> 00:57:31,920 there in this lecture we're going to see 1338 00:57:29,119 --> 00:57:34,640 how to install c plus plus compilers on 1339 00:57:31,920 --> 00:57:38,798 linux we are going to be working on an 1340 00:57:34,639 --> 00:57:42,078 ubuntu 2004 system and we will install a 1341 00:57:38,798 --> 00:57:44,559 new version of gcc and the latest 1342 00:57:42,079 --> 00:57:47,839 version i can get my hands on for clang 1343 00:57:44,559 --> 00:57:52,240 llvm so let's do this okay so here i am 1344 00:57:47,838 --> 00:57:55,440 on my linux box this is ubuntu 2004 if i 1345 00:57:55,440 --> 00:57:57,679 we're going to see the information on 1346 00:57:57,679 --> 00:58:02,960 and you will have to adapt this for 1347 00:57:59,838 --> 00:58:05,679 whatever linux distribution you are on 1348 00:58:02,960 --> 00:58:08,159 and to install gcc it's really simple 1349 00:58:05,679 --> 00:58:10,558 but the problem is the latest version of 1350 00:58:08,159 --> 00:58:13,759 gcc at the time of recording this video 1351 00:58:10,559 --> 00:58:16,319 is gcc 11 and it's not available in the 1352 00:58:13,760 --> 00:58:17,920 ubuntu packages now for example if you 1353 00:58:26,880 --> 00:58:30,240 you're going to put in your password 1354 00:58:28,318 --> 00:58:33,119 you're going to see that this is really 1355 00:58:30,239 --> 00:58:36,159 not available yet right we cannot find 1356 00:58:33,119 --> 00:58:38,318 this so the best version we can install 1357 00:58:36,159 --> 00:58:40,480 is gcc 10 which i happen to have 1358 00:58:38,318 --> 00:58:43,039 installed already on this system so 1359 00:58:40,480 --> 00:58:44,880 we're going to do gcc 10 that's what 1360 00:58:44,880 --> 00:58:48,079 and if we do this we're going to see 1361 00:58:46,400 --> 00:58:50,559 that this is already available you can 1362 00:58:48,079 --> 00:58:52,160 also install g plus plus done i think 1363 00:58:52,159 --> 00:58:56,480 okay so we're going to run the command 1364 00:58:53,679 --> 00:58:58,480 here and we're also going to see that 1365 00:58:56,480 --> 00:59:00,240 it's already available here but if you 1366 00:59:00,239 --> 00:59:04,479 they are going to be installed and you 1367 00:59:02,079 --> 00:59:07,039 can use them on your linux system 1368 00:59:04,480 --> 00:59:09,519 another tool that is necessary on linux 1369 00:59:07,039 --> 00:59:12,079 is the gdb debugger so we're going to do 1370 00:59:15,440 --> 00:59:19,200 right if you install it it's going to be 1371 00:59:17,358 --> 00:59:23,039 installed but i already have it 1372 00:59:19,199 --> 00:59:25,838 installed already so now if i do gcc 10 1373 00:59:23,039 --> 00:59:27,519 on my system here and say version we're 1374 00:59:25,838 --> 00:59:29,599 going to see the version printed out we 1375 00:59:27,519 --> 00:59:32,400 can do g plus plus stand we can see the 1376 00:59:29,599 --> 00:59:34,480 version here let's do that for c plus 1377 00:59:34,480 --> 00:59:40,000 so we're going to see the version here 1378 00:59:40,639 --> 00:59:44,318 and it is going to show up let's wait 1379 00:59:42,400 --> 00:59:46,880 for this we're going to see my gdp 1380 00:59:44,318 --> 00:59:50,000 version here and now i can use these 1381 00:59:46,880 --> 00:59:53,119 compilers to compile c plus plus code on 1382 00:59:50,000 --> 00:59:56,318 my linux box here so this completes our 1383 00:59:53,119 --> 00:59:57,920 steps to install a gcc compiler on linux 1384 00:59:56,318 --> 01:00:00,558 we're going to look at how we can get a 1385 01:00:00,559 --> 01:00:05,359 on our linux box here and it is really 1386 01:00:03,039 --> 01:00:07,599 simple all you have to do is to do 1387 01:00:07,599 --> 01:00:12,160 and we're going to install clang 12 1388 01:00:10,159 --> 01:00:14,480 because that's the latest version at the 1389 01:00:12,159 --> 01:00:15,920 time of recording this video if we do 1390 01:00:14,480 --> 01:00:17,920 this we're going to see that it's going 1391 01:00:15,920 --> 01:00:20,079 to install i already have it installed 1392 01:00:17,920 --> 01:00:22,318 here but if you don't have it installed 1393 01:00:20,079 --> 01:00:24,160 it's going to install on your system and 1394 01:00:22,318 --> 01:00:26,000 this is going to give you all the tools 1395 01:00:24,159 --> 01:00:28,558 you need to compile with the clan 1396 01:00:26,000 --> 01:00:30,559 compiler on this particular ubuntu 1397 01:00:28,559 --> 01:00:33,440 system these installs are going to be 1398 01:00:30,559 --> 01:00:37,440 installed in my user bin directory so if 1399 01:00:33,440 --> 01:00:39,039 i see the there and do analysis to show 1400 01:00:39,039 --> 01:00:42,960 and scroll up we're going to find all 1401 01:00:41,119 --> 01:00:45,838 the versions for clan you see i have 1402 01:00:42,960 --> 01:00:48,798 multiple versions clang 10 11 12 1403 01:00:45,838 --> 01:00:51,279 and we have clan plus plus variance of 1404 01:00:48,798 --> 01:00:53,920 this i think we do okay we have clank 1405 01:00:51,280 --> 01:00:55,760 plus plus and clan here and this is 1406 01:00:53,920 --> 01:00:58,639 going to do what we want we have the 1407 01:00:55,760 --> 01:01:01,359 clan combiner and the gcc compiler 1408 01:00:58,639 --> 01:01:03,598 installed on our system here now if you 1409 01:01:01,358 --> 01:01:05,279 want to get the latest version of gcc 1410 01:01:05,280 --> 01:01:11,280 one easy way i found is to go through a 1411 01:01:08,880 --> 01:01:12,880 package manager called homebrew let's 1412 01:01:12,880 --> 01:01:16,000 right if you search for this you're 1413 01:01:14,318 --> 01:01:18,239 going to find it and if you want you can 1414 01:01:18,239 --> 01:01:23,679 and when you install it it's going to to 1415 01:01:20,400 --> 01:01:25,358 be basically there ready to use 1416 01:01:23,679 --> 01:01:28,000 and to install it i think there's a 1417 01:01:25,358 --> 01:01:30,719 command there install home brew 1418 01:01:28,000 --> 01:01:32,159 let's do this install here and if we do 1419 01:01:32,159 --> 01:01:37,358 we should find a link to install this 1420 01:01:34,798 --> 01:01:39,358 let's find a link i think they have a 1421 01:01:37,358 --> 01:01:42,239 link you can copy aha it is this little 1422 01:01:39,358 --> 01:01:44,000 thing here you can copy it paste it in 1423 01:01:42,239 --> 01:01:45,838 your terminal and it is going to install 1424 01:01:45,838 --> 01:01:50,239 and when you have home brew installed 1425 01:01:48,480 --> 01:01:52,480 you can go through it to install the 1426 01:01:50,239 --> 01:01:54,399 latest version of gcc for example if we 1427 01:01:57,920 --> 01:02:03,039 we're going to find all the information 1428 01:02:00,400 --> 01:02:05,599 to install this through home brew and 1429 01:02:03,039 --> 01:02:07,359 you just have to do bro install gcc and 1430 01:02:05,599 --> 01:02:09,039 this is going to install this on your 1431 01:02:07,358 --> 01:02:11,279 system and you're going to see that it 1432 01:02:09,039 --> 01:02:13,520 is going to install the latest version 1433 01:02:11,280 --> 01:02:15,519 at the time of recording this video so 1434 01:02:13,519 --> 01:02:19,280 this is what you should do for example 1435 01:02:15,519 --> 01:02:21,759 if we go back to our linux box we can do 1436 01:02:19,280 --> 01:02:23,599 let's cd into our home directory and 1437 01:02:26,159 --> 01:02:30,318 and it's going to tell me that it is 1438 01:02:28,000 --> 01:02:31,280 already installed because i installed 1439 01:02:31,280 --> 01:02:34,960 but if you don't have it installed it's 1440 01:02:33,039 --> 01:02:37,839 going to install it and you do this 1441 01:02:34,960 --> 01:02:39,760 after you have gone through the steps 1442 01:02:39,760 --> 01:02:44,079 the home brew package manager on your 1443 01:02:41,920 --> 01:02:45,838 system and you do that by just copying 1444 01:02:44,079 --> 01:02:48,000 this link and pasting that in your 1445 01:02:45,838 --> 01:02:49,838 terminal and hitting enter and it's 1446 01:02:48,000 --> 01:02:52,559 going to give you directions on what you 1447 01:02:49,838 --> 01:02:54,798 can do to get this properly installed on 1448 01:02:52,559 --> 01:02:56,880 your system here so this is really cool 1449 01:02:54,798 --> 01:02:59,838 so if you want the absolute latest 1450 01:02:56,880 --> 01:03:01,760 version in gcc this is one way you have 1451 01:02:59,838 --> 01:03:04,159 to get a hold of it without really going 1452 01:03:01,760 --> 01:03:07,359 through the complex steps to build the 1453 01:03:04,159 --> 01:03:09,598 compiler yourself on linux okay it is 1454 01:03:07,358 --> 01:03:11,358 taking some more time here i am just 1455 01:03:09,599 --> 01:03:14,480 going to kill this because i already 1456 01:03:11,358 --> 01:03:19,358 have the latest version of gcc here if i 1457 01:03:14,480 --> 01:03:21,760 do jcc 11 on linux and the do version 1458 01:03:19,358 --> 01:03:24,159 i am going to see that i have 11 2 and 1459 01:03:21,760 --> 01:03:26,079 it was installed through homebrew and if 1460 01:03:24,159 --> 01:03:28,078 you let this run to completion it's 1461 01:03:26,079 --> 01:03:30,559 going to install this on your computer 1462 01:03:28,079 --> 01:03:33,599 we don't have to wait for this so now we 1463 01:03:30,559 --> 01:03:35,839 have the latest version of gcc on an 1464 01:03:33,599 --> 01:03:38,000 ubuntu system we also have the latest 1465 01:03:35,838 --> 01:03:40,480 version of clan this completes our 1466 01:03:38,000 --> 01:03:42,798 tutorial on how to set up compilers on 1467 01:03:40,480 --> 01:03:45,119 linux we are going to stop here in this 1468 01:03:42,798 --> 01:03:47,599 lecture this one we're going to install 1469 01:03:45,119 --> 01:03:49,440 visual studio code on the linux system 1470 01:03:47,599 --> 01:03:51,838 and then in the next one we're going to 1471 01:03:49,440 --> 01:03:54,079 link visual studio code to these 1472 01:03:51,838 --> 01:03:56,798 compilers and be able to use visual 1473 01:03:54,079 --> 01:03:59,760 studio code type code and compile our 1474 01:03:56,798 --> 01:04:01,519 programs using these two compilers here 1475 01:03:59,760 --> 01:04:03,599 so go ahead and finish up here and meet 1476 01:04:03,599 --> 01:04:08,318 in this lecture we're going to see how 1477 01:04:05,358 --> 01:04:11,038 to install the visual studio code editor 1478 01:04:08,318 --> 01:04:15,279 on a linux system and again we will be 1479 01:04:11,039 --> 01:04:17,119 working on our ubuntu 2004 system here 1480 01:04:15,280 --> 01:04:18,960 so let's head over to the website for 1481 01:04:17,119 --> 01:04:22,000 visual studio code and download our 1482 01:04:18,960 --> 01:04:24,318 binary and install it on our linux 1483 01:04:22,000 --> 01:04:26,960 system here okay so here we are at the 1484 01:04:24,318 --> 01:04:29,038 website for visual studio code you can 1485 01:04:26,960 --> 01:04:31,119 get there by searching for visual studio 1486 01:04:29,039 --> 01:04:33,359 code in your search engine and you're 1487 01:04:31,119 --> 01:04:34,720 going to land here and all we really 1488 01:04:34,719 --> 01:04:38,639 is go down to find our download links 1489 01:04:36,880 --> 01:04:41,440 we're going to see that for debian and 1490 01:04:38,639 --> 01:04:43,440 ubuntu we can download here but you can 1491 01:04:41,440 --> 01:04:45,920 also find something specific to other 1492 01:04:43,440 --> 01:04:49,280 distribution okay so we are going to 1493 01:04:45,920 --> 01:04:51,838 grab the 64-bit version of debian ubuntu 1494 01:04:49,280 --> 01:04:53,440 here because that's my system and you're 1495 01:04:51,838 --> 01:04:56,078 going to find whatever makes sense for 1496 01:04:53,440 --> 01:04:58,318 your distribution so let's grab this 1497 01:04:56,079 --> 01:05:00,000 here we're going to grab the link so if 1498 01:05:00,000 --> 01:05:04,480 this is going to kick off the download 1499 01:05:01,920 --> 01:05:07,039 process and you will see this downloaded 1500 01:05:04,480 --> 01:05:09,199 on your system and once you have this 1501 01:05:07,039 --> 01:05:11,520 downloaded you're going to install it 1502 01:05:09,199 --> 01:05:13,919 and in ubuntu you can just double click 1503 01:05:11,519 --> 01:05:16,400 on this to install it or you can install 1504 01:05:13,920 --> 01:05:19,519 it like you install any debian package 1505 01:05:16,400 --> 01:05:22,318 on a linux system for example if you are 1506 01:05:19,519 --> 01:05:24,960 unsure on how to install debian packages 1507 01:05:22,318 --> 01:05:28,079 all you have to do is do a simple search 1508 01:05:24,960 --> 01:05:30,318 so let's do google.com and go there and 1509 01:05:28,079 --> 01:05:32,880 we're going to see how to install 1510 01:05:35,679 --> 01:05:40,000 2004 so this is our version here and 1511 01:05:40,000 --> 01:05:44,798 instructions on how to do this it's 1512 01:05:42,239 --> 01:05:47,919 going to be really simple so we can go 1513 01:05:44,798 --> 01:05:51,358 down and find a way to install so all 1514 01:05:47,920 --> 01:05:53,119 you have to do is says you do dpkga 1515 01:05:51,358 --> 01:05:54,639 say i and you pass the name of the 1516 01:05:53,119 --> 01:05:56,798 package and this is going to install 1517 01:05:54,639 --> 01:05:58,719 visual studio code on your system once 1518 01:05:56,798 --> 01:06:01,119 you do this you will have visual studio 1519 01:05:58,719 --> 01:06:03,838 code installed and you will be ready to 1520 01:06:01,119 --> 01:06:05,519 start it and use it on your system here 1521 01:06:03,838 --> 01:06:08,159 once it is installed you will have the 1522 01:06:05,519 --> 01:06:11,440 ability to come in your system here for 1523 01:06:08,159 --> 01:06:13,358 example and say code dot and this is 1524 01:06:11,440 --> 01:06:15,519 going to open a visual studio code 1525 01:06:13,358 --> 01:06:17,440 instance on your system let's wait for 1526 01:06:15,519 --> 01:06:18,559 this to come up so that you really see 1527 01:06:18,559 --> 01:06:22,240 okay so this is going to open visual 1528 01:06:20,159 --> 01:06:24,879 studio coordinate system and you can 1529 01:06:22,239 --> 01:06:27,038 start using it like we will do in the 1530 01:06:24,880 --> 01:06:29,440 next lecture okay once you have a visual 1531 01:06:27,039 --> 01:06:31,440 studio code installed and started up on 1532 01:06:29,440 --> 01:06:32,798 your linux system the very first thing 1533 01:06:32,798 --> 01:06:37,519 is to install the extension that is 1534 01:06:35,199 --> 01:06:40,000 going to allow you to do some c and c 1535 01:06:37,519 --> 01:06:43,280 plus plus and to do that you will come 1536 01:06:40,000 --> 01:06:45,760 to this tab here that says extension and 1537 01:06:43,280 --> 01:06:47,519 in our search box you can start typing c 1538 01:06:47,519 --> 01:06:53,280 and you are going to have an entry that 1539 01:06:50,000 --> 01:06:55,119 says microsoft here so please select 1540 01:06:53,280 --> 01:06:56,640 this and this is going to be the 1541 01:06:55,119 --> 01:06:58,160 extension you need to install you're 1542 01:06:56,639 --> 01:06:59,519 going to click on install and it is 1543 01:06:59,519 --> 01:07:05,119 and this is what enables a visual studio 1544 01:07:02,400 --> 01:07:07,200 code to do some c plus plus programming 1545 01:07:05,119 --> 01:07:09,838 and this is really cool if you want you 1546 01:07:07,199 --> 01:07:11,759 can check out how to use it on linux and 1547 01:07:09,838 --> 01:07:13,519 you're going to have instructions on how 1548 01:07:11,760 --> 01:07:15,440 to do this but we will do this in the 1549 01:07:13,519 --> 01:07:18,239 next lecture and show you how to 1550 01:07:15,440 --> 01:07:20,798 configure visual studio code to use the 1551 01:07:18,239 --> 01:07:22,798 compilers that we have installed in a 1552 01:07:20,798 --> 01:07:25,038 previous lecture so we're going to tell 1553 01:07:22,798 --> 01:07:26,719 visual studio code to use our client 1554 01:07:25,039 --> 01:07:29,039 compiler we're going to tell it to use 1555 01:07:26,719 --> 01:07:31,038 our gcc compiler and we will have the 1556 01:07:29,039 --> 01:07:33,760 flexibility to come to terminal and 1557 01:07:31,039 --> 01:07:35,920 choose which compiler we want to use and 1558 01:07:33,760 --> 01:07:38,640 we're going to do all this in the next 1559 01:07:35,920 --> 01:07:40,798 lecture so go ahead and finish up here 1560 01:07:40,798 --> 01:07:45,920 in this lecture we're going to configure 1561 01:07:42,880 --> 01:07:49,039 our visual studio code editor to use the 1562 01:07:45,920 --> 01:07:51,039 compilers that we just installed in a 1563 01:07:49,039 --> 01:07:53,119 few previous lectures so we will be able 1564 01:07:51,039 --> 01:07:56,400 to type our code in our visual studio 1565 01:07:53,119 --> 01:07:58,640 code editor just like we do here and we 1566 01:07:56,400 --> 01:08:01,838 will pass the code through the compiler 1567 01:07:58,639 --> 01:08:04,400 to generate a binary that can in turn 1568 01:08:01,838 --> 01:08:06,318 run on our linux system here this is 1569 01:08:04,400 --> 01:08:08,400 what we want to do so we want visual 1570 01:08:06,318 --> 01:08:10,639 studio code to use the compilers that we 1571 01:08:08,400 --> 01:08:13,680 installed so let's head over to our 1572 01:08:10,639 --> 01:08:15,920 linux box and do this okay here i am on 1573 01:08:13,679 --> 01:08:18,559 my linux box this is the current 1574 01:08:15,920 --> 01:08:21,039 directory where i am at this is where i 1575 01:08:18,560 --> 01:08:24,319 am going to do my things here so i am 1576 01:08:21,039 --> 01:08:26,798 going to create a folder and call it a 1577 01:08:24,319 --> 01:08:27,960 template project let's do this we're 1578 01:08:27,960 --> 01:08:33,279 mkdir and we're going to say c plus plus 1579 01:08:31,039 --> 01:08:34,640 20 template project i think this is 1580 01:08:34,640 --> 01:08:39,039 or we can even say cpp because i'm not 1581 01:08:37,198 --> 01:08:41,678 sure the plus signs here are not going 1582 01:08:39,039 --> 01:08:44,319 to give us a problem let's play it on 1583 01:08:41,679 --> 01:08:46,960 the safe side and we are going to create 1584 01:08:44,319 --> 01:08:48,319 this folder and we're going to cd into 1585 01:08:48,319 --> 01:08:52,880 okay once we are in this folder we're 1586 01:08:50,560 --> 01:08:55,440 going to open this folder in visual 1587 01:08:52,880 --> 01:08:56,239 studio code and the easiest way to do 1588 01:08:56,238 --> 01:09:00,399 once you have visual studio code 1589 01:08:57,679 --> 01:09:01,359 installed is just to type code and type 1590 01:09:01,359 --> 01:09:05,440 and you will basically be telling the 1591 01:09:03,198 --> 01:09:08,238 operating system to open visual studio 1592 01:09:05,439 --> 01:09:10,000 code in the current folder here so i am 1593 01:09:10,000 --> 01:09:15,520 and this is going to kick it open and i 1594 01:09:13,359 --> 01:09:18,079 trust this folder because i just created 1595 01:09:15,520 --> 01:09:20,080 it and we are in our project here the 1596 01:09:18,079 --> 01:09:23,359 very first thing we're going to do is to 1597 01:09:20,079 --> 01:09:25,519 create a main cpp file so let's do that 1598 01:09:23,359 --> 01:09:26,798 and i did that by clicking on the plus 1599 01:09:26,798 --> 01:09:32,479 and i typed in the name of the file and 1600 01:09:29,920 --> 01:09:34,640 i am going to put in some dummy code 1601 01:09:32,479 --> 01:09:36,639 here that we want to use our starter 1602 01:09:36,640 --> 01:09:41,359 iostream and we're going to put in our 1603 01:09:39,520 --> 01:09:43,199 code here we're going to say main and 1604 01:09:41,359 --> 01:09:45,120 again you don't have to type all this if 1605 01:09:43,198 --> 01:09:47,278 you want you can get the code from the 1606 01:09:45,119 --> 01:09:49,278 resource section on the lecture so you 1607 01:09:47,279 --> 01:09:51,600 can copy the code paste that in here and 1608 01:09:49,279 --> 01:09:53,359 be on your merry way because we haven't 1609 01:09:51,600 --> 01:09:55,440 really started learning about c plus 1610 01:09:53,359 --> 01:09:57,760 plus we are setting up the environment 1611 01:09:55,439 --> 01:09:59,678 so don't stress yourself here 1612 01:09:57,760 --> 01:10:02,480 this is just some simple code to test 1613 01:09:59,679 --> 01:10:04,800 our compilers to see if they support c 1614 01:10:02,479 --> 01:10:07,359 plus plus 20 because we want to use the 1615 01:10:04,800 --> 01:10:10,400 letter standard we can get our hands on 1616 01:10:10,399 --> 01:10:14,238 type exactly what i typed here so we're 1617 01:10:14,238 --> 01:10:18,639 and we're going to put this weird 1618 01:10:15,920 --> 01:10:19,760 operator and say 20 and we want to see 1619 01:10:19,760 --> 01:10:24,239 madness here is greater than zero this 1620 01:10:22,238 --> 01:10:27,919 is what we want to do and we're going to 1621 01:10:24,238 --> 01:10:30,479 say stdc out and the same result okay so 1622 01:10:27,920 --> 01:10:31,920 type exactly what i type here and as we 1623 01:10:30,479 --> 01:10:34,000 progress in the course we're going to 1624 01:10:31,920 --> 01:10:38,319 make sense of all these madness we see 1625 01:10:34,000 --> 01:10:40,880 here so now that we have a cpp file 1626 01:10:38,319 --> 01:10:44,319 containing the code for our c plus plus 1627 01:10:40,880 --> 01:10:46,239 program we want to compile this and we 1628 01:10:44,319 --> 01:10:48,158 need to tell visual studio code which 1629 01:10:46,238 --> 01:10:50,639 compiler it's going to use to compile 1630 01:10:48,158 --> 01:10:52,559 this program here to give us a binary 1631 01:10:50,640 --> 01:10:55,520 that we can run so what we're going to 1632 01:10:55,520 --> 01:10:58,960 and we're going to say that we want to 1633 01:10:58,960 --> 01:11:02,880 and the task is really a set of 1634 01:11:00,960 --> 01:11:05,920 instructions you give to visual studio 1635 01:11:02,880 --> 01:11:08,960 code on how to take your c plus plus 1636 01:11:05,920 --> 01:11:11,199 project and turn that into a binary that 1637 01:11:08,960 --> 01:11:12,800 can run on that system here so that's 1638 01:11:11,198 --> 01:11:15,279 what we want to do we want to tell 1639 01:11:12,800 --> 01:11:16,719 visual studio code which compiler it's 1640 01:11:15,279 --> 01:11:18,800 going to use we're going to configure 1641 01:11:16,719 --> 01:11:21,279 task here and you're going to see that 1642 01:11:18,800 --> 01:11:24,079 it's going to discover many compilers on 1643 01:11:21,279 --> 01:11:27,599 my system here you see we have clan 10 1644 01:11:24,079 --> 01:11:30,719 clang 11 clan 12 and we should also have 1645 01:11:27,600 --> 01:11:33,679 gcc 11 if we keep scrolling down we're 1646 01:11:30,719 --> 01:11:36,399 going to find these little guys here i 1647 01:11:33,679 --> 01:11:38,079 have many compilers on this system so i 1648 01:11:40,399 --> 01:11:44,319 which is installed through home brew 1649 01:11:42,479 --> 01:11:46,879 this is what i'm going to choose here 1650 01:11:44,319 --> 01:11:48,960 you say it's saying g plus plus 11 but 1651 01:11:46,880 --> 01:11:51,359 if you have access to g plus plus 10 you 1652 01:11:48,960 --> 01:11:53,198 can also use that it's going to work 1653 01:11:51,359 --> 01:11:55,759 with what we want to do in this course 1654 01:11:53,198 --> 01:11:58,559 here mostly so i am going to choose the 1655 01:11:58,560 --> 01:12:04,719 and this is going to set up an entry in 1656 01:12:01,198 --> 01:12:06,719 our tasks dot json file to build and run 1657 01:12:04,719 --> 01:12:09,119 this program here and that's what we're 1658 01:12:06,719 --> 01:12:12,480 going to do okay you see it is using 1659 01:12:09,119 --> 01:12:14,559 this particular compiler and one thing 1660 01:12:12,479 --> 01:12:16,559 we can do before we try and build this 1661 01:12:14,560 --> 01:12:17,760 we can change the level here to make it 1662 01:12:17,760 --> 01:12:21,360 more descriptive so we're going to save 1663 01:12:27,679 --> 01:12:33,199 and this is all we can save this 1664 01:12:30,079 --> 01:12:35,198 and we can close this json file and if 1665 01:12:33,198 --> 01:12:37,678 we bring up a terminal window we can do 1666 01:12:35,198 --> 01:12:40,639 that by hitting terminal here and 1667 01:12:40,640 --> 01:12:46,000 this is going to open up the current 1668 01:12:42,319 --> 01:12:49,039 location for example if we do pwd in 1669 01:12:46,000 --> 01:12:50,399 this terminal here let's use lowercase i 1670 01:12:49,039 --> 01:12:52,479 think that's what it wants we're going 1671 01:12:50,399 --> 01:12:55,119 to see the location here this is my 1672 01:12:52,479 --> 01:12:56,559 project so to build i can come to 1673 01:12:56,560 --> 01:13:02,400 and i choose run task and i am going to 1674 01:12:59,359 --> 01:13:05,439 choose the task to compile with my gcc 1675 01:13:02,399 --> 01:13:08,399 installation here if i choose this 1676 01:13:05,439 --> 01:13:09,599 it is going to kick off the weld process 1677 01:13:08,399 --> 01:13:11,920 and you're going to see that the 1678 01:13:09,600 --> 01:13:13,280 gibberish i have here is not supported 1679 01:13:13,279 --> 01:13:18,079 compiler is not configured to use the c 1680 01:13:15,760 --> 01:13:22,400 plus plus 20. so all we really need to 1681 01:13:18,079 --> 01:13:24,559 do is go back to our command and tell it 1682 01:13:22,399 --> 01:13:26,960 to use c plus plus 20. and we can do 1683 01:13:24,560 --> 01:13:29,840 that by passing a specific instruction 1684 01:13:26,960 --> 01:13:32,000 to the compiler to use c plus plus 20 1685 01:13:29,840 --> 01:13:35,039 like we do on line 10 here so we're 1686 01:13:32,000 --> 01:13:36,640 going to say std equals c plus plus 20. 1687 01:13:35,039 --> 01:13:38,158 we are going to save this a little bit 1688 01:13:36,640 --> 01:13:39,679 and i really wanted you to see this 1689 01:13:38,158 --> 01:13:41,839 problem here because many people are 1690 01:13:39,679 --> 01:13:43,679 going to forget this and we're going to 1691 01:13:41,840 --> 01:13:46,239 try and build again making sure we are 1692 01:13:43,679 --> 01:13:49,119 selecting the main cpp file here so 1693 01:13:46,238 --> 01:13:51,359 let's go to terminal run task and use 1694 01:13:51,359 --> 01:13:55,839 and wait for it to do its thing you're 1695 01:13:53,198 --> 01:13:58,000 going to see that the world now finished 1696 01:13:58,000 --> 01:14:02,960 and if we come back to our terminal here 1697 01:14:00,640 --> 01:14:05,280 and do ls we're going to see that we 1698 01:14:05,279 --> 01:14:10,319 and if we run it we should see a zero so 1699 01:14:10,319 --> 01:14:14,238 and if we run it we're going to see a 1700 01:14:11,760 --> 01:14:16,480 zero meaning that our program is running 1701 01:14:14,238 --> 01:14:19,599 and working correctly we are able to 1702 01:14:16,479 --> 01:14:22,959 compile a c plus plus project on a linux 1703 01:14:19,600 --> 01:14:24,960 system here now if you want to see how i 1704 01:14:22,960 --> 01:14:27,920 came up with the information to use the 1705 01:14:24,960 --> 01:14:30,319 tax json file please come back to the 1706 01:14:30,319 --> 01:14:35,439 and choose the c and c plus plus 1707 01:14:33,198 --> 01:14:36,559 extension we're going to click on this 1708 01:14:35,439 --> 01:14:38,960 and we're going to see all the 1709 01:14:36,560 --> 01:14:41,760 instructions on how to use this guy on 1710 01:14:38,960 --> 01:14:43,119 the linux system here so gcc on linux we 1711 01:14:44,158 --> 01:14:48,399 information here now you can read up on 1712 01:14:46,319 --> 01:14:51,039 all this if you want but what i am going 1713 01:14:48,399 --> 01:14:52,879 to do is go to visual studio code and 1714 01:14:51,039 --> 01:14:55,359 show you exactly what you need to do to 1715 01:14:52,880 --> 01:14:57,920 get this to work what i want to do in 1716 01:14:55,359 --> 01:15:00,719 there is set this up to compile 1717 01:14:57,920 --> 01:15:03,440 everything in my what i want to do is to 1718 01:15:00,719 --> 01:15:06,640 set what i really want to do is to what 1719 01:15:03,439 --> 01:15:09,198 i really wanted to do is to set this up 1720 01:15:06,640 --> 01:15:11,760 so that it compiles every single cpp 1721 01:15:09,198 --> 01:15:13,678 file in my directory here and i can do 1722 01:15:13,679 --> 01:15:17,199 a set of instructions i had prepared 1723 01:15:17,198 --> 01:15:21,119 so we're going to tell it to compile in 1724 01:15:18,960 --> 01:15:24,158 c plus plus 20 mode we're going to tell 1725 01:15:21,119 --> 01:15:26,158 it to compile every single cpp file in 1726 01:15:24,158 --> 01:15:28,719 this directory and then we're going to 1727 01:15:26,158 --> 01:15:31,119 specify the output file name for our 1728 01:15:28,719 --> 01:15:33,439 binary now this is code i used on 1729 01:15:31,119 --> 01:15:35,279 windows i need to adapt it to be 1730 01:15:37,279 --> 01:15:42,399 documentation there and we can 1731 01:15:40,479 --> 01:15:44,238 we can scroll down a little bit and 1732 01:15:42,399 --> 01:15:46,000 we're going to see that they use 1733 01:15:44,238 --> 01:15:47,678 backslashes here so that's what we're 1734 01:15:46,000 --> 01:15:50,719 going to use in visual video code to 1735 01:15:47,679 --> 01:15:53,760 play it safe so we can say this 1736 01:15:50,719 --> 01:15:56,158 a backslash and this is a backslash 1737 01:15:53,760 --> 01:15:57,840 and we will try and build this and we're 1738 01:15:56,158 --> 01:16:00,079 going to see what happens here this is 1739 01:15:57,840 --> 01:16:02,239 going to build our project in c plus 1740 01:16:00,079 --> 01:16:04,719 plus 20 mode we're going to be building 1741 01:16:02,238 --> 01:16:07,279 every single cpp file in the project and 1742 01:16:04,719 --> 01:16:09,439 the output will be called rooster this 1743 01:16:07,279 --> 01:16:10,559 is what we want to do here let's save 1744 01:16:10,560 --> 01:16:15,840 and i am going to close this json file 1745 01:16:13,600 --> 01:16:19,679 and i will bring up my terminal by 1746 01:16:15,840 --> 01:16:23,119 coming to view and save a terminal here 1747 01:16:19,679 --> 01:16:25,359 and i can remove the main.exe file or 1748 01:16:23,119 --> 01:16:28,880 the main executable i had in there if i 1749 01:16:25,359 --> 01:16:31,839 do lsuc it's gone but now if we try and 1750 01:16:28,880 --> 01:16:33,199 compile with the gcc compiler 1751 01:16:31,840 --> 01:16:35,279 we're going to see what happens you see 1752 01:16:33,198 --> 01:16:37,678 the world is good and we have a binary 1753 01:16:35,279 --> 01:16:39,920 called rooster and we can run it here if 1754 01:16:37,679 --> 01:16:41,760 we do rooster we're going to see it run 1755 01:16:39,920 --> 01:16:43,760 and give us a zero and this is really 1756 01:16:41,760 --> 01:16:45,679 all we need to do now we have this 1757 01:16:45,679 --> 01:16:50,640 our gcc compiler but we wanted the same 1758 01:16:48,238 --> 01:16:52,238 for a clanger compiler and we want the 1759 01:16:50,640 --> 01:16:54,880 flexibility to be able to switch 1760 01:16:52,238 --> 01:16:57,439 compilers by coming here and just 1761 01:16:54,880 --> 01:16:59,119 selecting the right task so let's do 1762 01:16:57,439 --> 01:17:01,039 this we're going to configure clan we're 1763 01:16:59,119 --> 01:17:03,198 going to come to terminal we want to 1764 01:17:01,039 --> 01:17:04,800 configure a task and we're going to 1765 01:17:03,198 --> 01:17:07,599 choose the best we have in terms of 1766 01:17:04,800 --> 01:17:09,679 clang that happens to be clang 1767 01:17:07,600 --> 01:17:11,840 plus plus 12 here so we're going to 1768 01:17:09,679 --> 01:17:14,119 choose this and this is going to add an 1769 01:17:14,119 --> 01:17:18,960 tasks.json file we're going to change 1770 01:17:16,640 --> 01:17:21,199 this to say that we want to build with 1771 01:17:18,960 --> 01:17:23,840 clan let's do that we're going to say 1772 01:17:26,359 --> 01:17:30,479 12.0.1 if i remember correctly but we 1773 01:17:30,479 --> 01:17:36,000 our terminal here let's go to terminal 1774 01:17:33,039 --> 01:17:37,840 and we're going to say clang plus plus 1775 01:17:36,000 --> 01:17:39,920 and 12 that's the version and we're 1776 01:17:37,840 --> 01:17:42,319 going to ask it what's your version it's 1777 01:17:39,920 --> 01:17:44,560 going to tell us in detail and if we 1778 01:17:42,319 --> 01:17:47,599 wait we're going to see this speed out 1779 01:17:44,560 --> 01:17:50,480 on the terminal here and this happens to 1780 01:17:50,479 --> 01:17:53,519 so that's what we're going to say in our 1781 01:18:00,319 --> 01:18:05,599 and uh this is really all all we need to 1782 01:18:05,600 --> 01:18:10,560 to build in c plus plus 20 mod exactly 1783 01:18:07,920 --> 01:18:14,079 what we did for gcc here so i am going 1784 01:18:10,560 --> 01:18:16,239 to copy this and put that in my 1785 01:18:14,079 --> 01:18:18,158 arguments here i'm i am going to take 1786 01:18:16,238 --> 01:18:20,879 out whatever i had in there i am going 1787 01:18:18,158 --> 01:18:23,039 to put in my new things and let's see if 1788 01:18:20,880 --> 01:18:26,159 this compiler works now we're going to 1789 01:18:23,039 --> 01:18:28,640 save our tasks.json file and come back 1790 01:18:26,158 --> 01:18:30,719 to the cpp file we're going to bring up 1791 01:18:33,198 --> 01:18:38,238 and remove rooster because we wanted to 1792 01:18:35,600 --> 01:18:42,079 build a new one with our clank compiler 1793 01:18:38,238 --> 01:18:43,759 and we will come to terminal run task 1794 01:18:42,079 --> 01:18:46,640 now we're going to choose to build with 1795 01:18:43,760 --> 01:18:48,719 clang 12.0 we're going to fix this in a 1796 01:18:46,640 --> 01:18:51,039 minute but if we do this we're going to 1797 01:18:48,719 --> 01:18:53,359 see that our weld process is going to 1798 01:18:51,039 --> 01:18:55,519 start it's going to be building with our 1799 01:18:53,359 --> 01:18:57,839 clan compiler it's going to be using c 1800 01:18:55,520 --> 01:19:00,480 plus plus 20. it's going to be building 1801 01:18:57,840 --> 01:19:03,279 every single cpp file in this location 1802 01:19:00,479 --> 01:19:05,359 here and the output of the world will be 1803 01:19:03,279 --> 01:19:07,920 a binary called rooster that we have 1804 01:19:05,359 --> 01:19:09,759 right here so the build is good if we do 1805 01:19:07,920 --> 01:19:12,239 ls we're going to find rooster and if we 1806 01:19:09,760 --> 01:19:15,119 run it it's going to say zero exactly as 1807 01:19:12,238 --> 01:19:17,919 we expect here and we have visual studio 1808 01:19:15,119 --> 01:19:20,158 code properly configured to use our two 1809 01:19:17,920 --> 01:19:22,719 compilers the client compiler and the 1810 01:19:20,158 --> 01:19:24,719 gcc compiler let's come back to our 1811 01:19:22,719 --> 01:19:26,880 configuration and change this to say 1812 01:19:26,880 --> 01:19:32,719 to be super clear on the version we have 1813 01:19:29,600 --> 01:19:35,280 we can close our json file here 1814 01:19:32,719 --> 01:19:38,640 and come and try to build again if we do 1815 01:19:35,279 --> 01:19:40,639 terminal and run task we will have our 1816 01:19:38,640 --> 01:19:42,159 compiler here ignore all these things i 1817 01:19:40,640 --> 01:19:44,880 think visual video code is going to give 1818 01:19:42,158 --> 01:19:47,439 you duplicates but you can choose 1819 01:19:44,880 --> 01:19:48,880 the version that says what you want and 1820 01:19:47,439 --> 01:19:52,079 it is going to work so we're going to 1821 01:19:48,880 --> 01:19:54,480 choose gcc oh we are saying gcc but we 1822 01:19:52,079 --> 01:19:56,559 want clang here so let's say that we 1823 01:19:54,479 --> 01:19:58,079 want to build with clan we're going to 1824 01:20:02,198 --> 01:20:06,638 12.0.0 this is right here so we're going 1825 01:20:06,639 --> 01:20:11,039 and it's going to build successfully and 1826 01:20:08,399 --> 01:20:11,839 we will have our binary here if we run 1827 01:20:11,840 --> 01:20:16,079 it is going to say zero and this is 1828 01:20:13,439 --> 01:20:17,599 really what we need to do here so this 1829 01:20:16,079 --> 01:20:19,039 is really all i had to share in this 1830 01:20:17,600 --> 01:20:20,320 lecture showing you how you can 1831 01:20:20,319 --> 01:20:24,719 visual studio code to compile using the 1832 01:20:22,399 --> 01:20:26,799 compilers that we have installed another 1833 01:20:24,719 --> 01:20:30,480 thing you can do to make your experience 1834 01:20:26,800 --> 01:20:34,079 better is come back to view and select 1835 01:20:30,479 --> 01:20:36,238 command palette here and you can do c 1836 01:20:36,238 --> 01:20:40,638 configurations through a user interface 1837 01:20:39,279 --> 01:20:42,479 and if we do this you're going to see 1838 01:20:40,639 --> 01:20:45,359 that this is going to create a cpp 1839 01:20:42,479 --> 01:20:46,479 properties json file so this is where we 1840 01:20:46,479 --> 01:20:50,238 what kind of intelligence what kind of 1841 01:20:48,560 --> 01:20:53,600 what kind of compiler is going to be 1842 01:20:50,238 --> 01:20:55,119 used we can do all these things here so 1843 01:20:53,600 --> 01:20:57,360 we're going to leave it to the default 1844 01:20:55,119 --> 01:20:59,840 it's on our linux system it's going to 1845 01:20:57,359 --> 01:21:00,719 be using gcc 11 which is the best we 1846 01:21:02,719 --> 01:21:08,800 but uh one thing you should note in this 1847 01:21:06,800 --> 01:21:10,800 course here is the c plus plus standard 1848 01:21:08,800 --> 01:21:13,600 we wanted to use so we wanted to use z 1849 01:21:10,800 --> 01:21:16,000 plus plus 20 and we want visual studio 1850 01:21:16,719 --> 01:21:21,920 format our main cpp file based on the c 1851 01:21:19,600 --> 01:21:23,440 plus plus 20 standard year that's what 1852 01:21:21,920 --> 01:21:24,880 we can achieve with this so once you 1853 01:21:27,520 --> 01:21:30,800 and if we go back to the main cpp file 1854 01:21:30,800 --> 01:21:35,360 that this is no longer flagged as a 1855 01:21:33,520 --> 01:21:37,280 problem with squiggly lines visual 1856 01:21:35,359 --> 01:21:39,279 studio code is properly understanding 1857 01:21:37,279 --> 01:21:40,880 what is happening here and this is 1858 01:21:39,279 --> 01:21:42,880 something you can do to make your 1859 01:21:40,880 --> 01:21:45,119 experience a little bit better 1860 01:21:42,880 --> 01:21:47,840 okay now that we have this project set 1861 01:21:45,119 --> 01:21:50,639 up and ready on linux we're going to be 1862 01:21:47,840 --> 01:21:53,600 reusing this as a template project every 1863 01:21:50,639 --> 01:21:54,960 time we start something new on linux so 1864 01:21:53,600 --> 01:21:57,600 every time we want to create a new 1865 01:21:54,960 --> 01:21:59,840 project we will grab this entire folder 1866 01:21:57,600 --> 01:22:03,280 so for example we will come to our 1867 01:21:59,840 --> 01:22:06,400 terminal here and go up a little bit 1868 01:22:03,279 --> 01:22:08,319 so cd and go up one level and if we do 1869 01:22:06,399 --> 01:22:11,519 ls we're going to have our c plus plus 1870 01:22:08,319 --> 01:22:14,158 20 template project we will copy it and 1871 01:22:11,520 --> 01:22:15,520 rename it to start a new project and 1872 01:22:14,158 --> 01:22:17,519 that's going to give us all the 1873 01:22:15,520 --> 01:22:19,920 configuration that we did we don't need 1874 01:22:17,520 --> 01:22:22,800 to reconfigure the compilers or whatever 1875 01:22:19,920 --> 01:22:25,039 it is going to work right out of the box 1876 01:22:22,800 --> 01:22:27,600 and that's really what we did here we 1877 01:22:25,039 --> 01:22:30,158 set up a template project that we will 1878 01:22:27,600 --> 01:22:32,800 be reusing all over again in the course 1879 01:22:30,158 --> 01:22:35,439 as we progress learning about c plus 1880 01:22:32,800 --> 01:22:37,520 plus 20. i am going to stop here in this 1881 01:22:35,439 --> 01:22:39,519 lecture the next one i am going to show 1882 01:22:37,520 --> 01:22:42,800 you how you can set up a development 1883 01:22:39,520 --> 01:22:45,040 environment on the mac os system 1884 01:22:42,800 --> 01:22:46,320 go ahead and finish up here and meet me 1885 01:22:46,319 --> 01:22:50,799 in this video i am going to show you how 1886 01:22:48,079 --> 01:22:54,000 to install c plus plus compiler on your 1887 01:22:50,800 --> 01:22:56,639 mac os system and we have a bunch of 1888 01:22:54,000 --> 01:22:59,198 options on the mac we can install the 1889 01:22:56,639 --> 01:23:00,319 gcc compiler we can install the clang 1890 01:23:00,319 --> 01:23:05,519 of the apple company or we can install 1891 01:23:03,119 --> 01:23:07,519 the clangle vm version all these are 1892 01:23:05,520 --> 01:23:09,679 options and i will show you how to 1893 01:23:07,520 --> 01:23:12,239 install these i don't have access to a 1894 01:23:09,679 --> 01:23:14,719 mac device so i won't really be able to 1895 01:23:12,238 --> 01:23:17,039 install xcode and show you but i think 1896 01:23:14,719 --> 01:23:19,119 you can manage if you are using a mac 1897 01:23:17,039 --> 01:23:21,359 system it is nothing complicated you 1898 01:23:19,119 --> 01:23:23,039 just have to go to the apple store and 1899 01:23:21,359 --> 01:23:27,039 click on xcode and it is going to 1900 01:23:23,039 --> 01:23:28,960 install for a gcc and clang llvm i will 1901 01:23:27,039 --> 01:23:31,439 show you how to do this using the 1902 01:23:28,960 --> 01:23:34,399 homebrew package manager which is also 1903 01:23:31,439 --> 01:23:36,719 going to make this super easy okay so 1904 01:23:34,399 --> 01:23:39,519 let's head over to our browser and we 1905 01:23:36,719 --> 01:23:42,800 have a good link here from digitalocean 1906 01:23:39,520 --> 01:23:45,360 on how to install and use homobrew on a 1907 01:23:42,800 --> 01:23:47,679 mac os system this is a really validated 1908 01:23:45,359 --> 01:23:50,158 link you can see that right here and you 1909 01:23:47,679 --> 01:23:52,158 can read up on this if you are using a 1910 01:23:50,158 --> 01:23:54,879 mac system i am going to share the link 1911 01:23:52,158 --> 01:23:57,599 here in the resources section of the 1912 01:23:54,880 --> 01:24:00,000 course and to use this we will go 1913 01:23:57,600 --> 01:24:01,760 through the mac os terminal and it is 1914 01:24:00,000 --> 01:24:04,960 nothing complicated you can search for 1915 01:24:01,760 --> 01:24:07,520 it and open it up and after you do this 1916 01:24:04,960 --> 01:24:10,079 you will have to install the tools 1917 01:24:07,520 --> 01:24:12,639 needed to build things on the mac os 1918 01:24:10,079 --> 01:24:15,679 system and that's going to install xcode 1919 01:24:12,639 --> 01:24:18,239 here so grab the command here and run it 1920 01:24:15,679 --> 01:24:20,560 in your terminal and this is going to 1921 01:24:18,238 --> 01:24:22,158 install everything you need and after 1922 01:24:20,560 --> 01:24:24,800 you have this installed you're going to 1923 01:24:22,158 --> 01:24:29,198 install home brew i do recommend coming 1924 01:24:24,800 --> 01:24:32,320 up to the website here for homebrew and 1925 01:24:29,198 --> 01:24:34,960 clicking on this copy icon here this is 1926 01:24:32,319 --> 01:24:36,559 going to copy the entire thing and you 1927 01:24:36,560 --> 01:24:40,239 and once you copy it it is going to give 1928 01:24:40,238 --> 01:24:44,799 and you can go in the terminal on your 1929 01:24:42,158 --> 01:24:46,399 mac os system i am using a linux 1930 01:24:44,800 --> 01:24:49,199 terminal here and then you're going to 1931 01:24:46,399 --> 01:24:51,039 paste this command in here and hit enter 1932 01:24:49,198 --> 01:24:52,719 once you do that you will be prompted to 1933 01:24:51,039 --> 01:24:54,880 put in the password it's going to tell 1934 01:24:52,719 --> 01:24:56,560 you that it needs to install homebrew 1935 01:24:54,880 --> 01:24:58,880 and you are basically going to wait for 1936 01:24:56,560 --> 01:25:00,719 this to finish i have to warn you this 1937 01:24:58,880 --> 01:25:02,880 is going to take some time even if you 1938 01:25:00,719 --> 01:25:04,719 have a good internet connection this is 1939 01:25:02,880 --> 01:25:06,480 going to take some time so be patient 1940 01:25:04,719 --> 01:25:08,639 here when it's done it's going to tell 1941 01:25:06,479 --> 01:25:11,119 you that it is finished and you will be 1942 01:25:08,639 --> 01:25:13,920 ready to install the gcc compiler and 1943 01:25:11,119 --> 01:25:16,000 the client llvm compiler through your 1944 01:25:13,920 --> 01:25:18,158 homebrew installation here i am not 1945 01:25:16,000 --> 01:25:20,158 going to click on this because 1946 01:25:18,158 --> 01:25:22,399 i already have this installed on my 1947 01:25:20,158 --> 01:25:24,719 linux system here let's go back to the 1948 01:25:22,399 --> 01:25:27,279 link and see what they say next so after 1949 01:25:24,719 --> 01:25:30,880 you paste that in it's going to install 1950 01:25:27,279 --> 01:25:32,479 and after that you will see that 1951 01:25:30,880 --> 01:25:33,679 homebrew is going to be installed on 1952 01:25:35,039 --> 01:25:38,800 you can use home brew to start 1953 01:25:36,880 --> 01:25:41,119 installing and upgrading packages you 1954 01:25:38,800 --> 01:25:42,639 can really read up on this but what i 1955 01:25:42,639 --> 01:25:47,600 is that you can use home brew to install 1956 01:25:44,880 --> 01:25:49,840 the gcc compiler and the clang ll vm 1957 01:25:47,600 --> 01:25:52,840 compiler so if we come to the search 1958 01:25:52,840 --> 01:25:57,840 broom we're going to find instructions 1959 01:25:55,279 --> 01:26:00,639 on how to install this so all you need 1960 01:25:57,840 --> 01:26:02,159 to do really is to say brew install gcc 1961 01:26:00,639 --> 01:26:04,480 this is going to install the latest 1962 01:26:04,479 --> 01:26:09,919 if we come here and say llvm or clank 1963 01:26:11,600 --> 01:26:17,360 we can find that right here llvm 1964 01:26:15,679 --> 01:26:19,359 and it is going to give us instructions 1965 01:26:17,359 --> 01:26:20,238 on how to install this and all you have 1966 01:26:20,238 --> 01:26:25,439 is brew install llvm and this is going 1967 01:26:22,719 --> 01:26:28,158 to install these two compilers on your 1968 01:26:25,439 --> 01:26:30,799 mac system so once you do that you will 1969 01:26:28,158 --> 01:26:33,198 have them stored somewhere let's go to 1970 01:26:30,800 --> 01:26:35,920 the install link and we're going to see 1971 01:26:33,198 --> 01:26:38,879 that homebrew is basically going to be 1972 01:26:35,920 --> 01:26:41,520 installed into things in your user local 1973 01:26:38,880 --> 01:26:43,760 directory on a mac device please tell me 1974 01:26:41,520 --> 01:26:46,320 if this is true for your case and if you 1975 01:26:43,760 --> 01:26:49,440 have a problem we'll try and go through 1976 01:26:46,319 --> 01:26:51,119 that and fix that up so if you are on a 1977 01:26:49,439 --> 01:26:53,439 mac i would really recommend going 1978 01:26:51,119 --> 01:26:55,198 through this to install homebrew once 1979 01:26:53,439 --> 01:26:58,638 you have that installed you will go 1980 01:26:55,198 --> 01:27:01,519 through that to install both of the llvm 1981 01:26:58,639 --> 01:27:03,359 compiler and the gcc compiler and that's 1982 01:27:01,520 --> 01:27:05,920 going to give you the latest versions 1983 01:27:03,359 --> 01:27:08,559 for these compilers to find out if they 1984 01:27:05,920 --> 01:27:11,039 will installed you can go in your user 1985 01:27:08,560 --> 01:27:12,800 local directory and you can hunt through 1986 01:27:11,039 --> 01:27:15,439 these compilers and you're going to find 1987 01:27:12,800 --> 01:27:17,440 binaries for them and start using them 1988 01:27:15,439 --> 01:27:19,198 to build your c plus plus applications 1989 01:27:17,439 --> 01:27:21,839 you okay so by now i am going to 1990 01:27:19,198 --> 01:27:24,399 consider that you have both gcc and a 1991 01:27:21,840 --> 01:27:27,039 clang installed on your mac system the 1992 01:27:24,399 --> 01:27:29,279 next logical thing to do is to want the 1993 01:27:27,039 --> 01:27:31,679 apple version of clang and i think 1994 01:27:29,279 --> 01:27:32,479 that's going to come as part of xcode 1995 01:27:32,479 --> 01:27:38,399 and it is going to come as a result of 1996 01:27:35,760 --> 01:27:40,239 running the command that we did before i 1997 01:27:38,399 --> 01:27:41,920 think it was this little command here 1998 01:27:40,238 --> 01:27:43,519 which is going to install everything you 1999 01:27:44,840 --> 01:27:49,920 ide so this is all i have to share in 2000 01:27:47,359 --> 01:27:52,079 this lecture again i apologize i don't 2001 01:27:49,920 --> 01:27:53,600 have access to a mac device but if you 2002 01:27:52,079 --> 01:27:56,238 have a problem you can tell me and i 2003 01:27:53,600 --> 01:27:57,440 will try to help you out and get this 2004 01:27:57,439 --> 01:28:00,799 we are going to stop here in this 2005 01:27:59,039 --> 01:28:03,279 lecture the next one we're going to see 2006 01:28:00,800 --> 01:28:06,000 how we can install xcode on your mac 2007 01:28:03,279 --> 01:28:07,439 device go ahead and finish up here 2008 01:28:07,439 --> 01:28:12,479 in this video i am going to show you how 2009 01:28:09,279 --> 01:28:15,119 to install the visual studio code editor 2010 01:28:12,479 --> 01:28:17,839 on your mac os system please know that 2011 01:28:15,119 --> 01:28:20,479 you can also use other ides like x code 2012 01:28:17,840 --> 01:28:22,560 or code lite do c plus plus development 2013 01:28:20,479 --> 01:28:25,519 but visual studio code is going to be 2014 01:28:22,560 --> 01:28:27,600 our main editor in this course here and 2015 01:28:25,520 --> 01:28:29,840 i would recommend to install it so that 2016 01:28:27,600 --> 01:28:31,920 we are doing consistent things that's 2017 01:28:29,840 --> 01:28:34,560 going to reduce chances for you to get 2018 01:28:31,920 --> 01:28:37,119 confused so we are going to head over to 2019 01:28:34,560 --> 01:28:40,159 the website for visual studio code 2020 01:28:37,119 --> 01:28:43,198 and to get there we can just type visual 2021 01:28:40,158 --> 01:28:45,599 studio code in here on in our search 2022 01:28:43,198 --> 01:28:47,919 engine and we will have a link here that 2023 01:28:45,600 --> 01:28:50,960 we can use to download our fans 2024 01:28:47,920 --> 01:28:54,800 so we are going to go down and find 2025 01:28:50,960 --> 01:28:57,279 download links for windows linux and mac 2026 01:28:54,800 --> 01:28:58,880 please use the mac download button here 2027 01:28:57,279 --> 01:29:01,198 and you're going to download the binary 2028 01:28:58,880 --> 01:29:03,840 and install it i think it is going to be 2029 01:29:01,198 --> 01:29:05,039 in the download folder by default so all 2030 01:29:05,039 --> 01:29:10,479 is drag that up in your installation 2031 01:29:07,760 --> 01:29:12,239 directory so that it becomes a runnable 2032 01:29:10,479 --> 01:29:14,479 application after you do that you're 2033 01:29:12,238 --> 01:29:17,039 going to run it and it is going to run 2034 01:29:14,479 --> 01:29:19,439 and you will have a user interface like 2035 01:29:17,039 --> 01:29:21,600 this with visual studio code opened up 2036 01:29:19,439 --> 01:29:24,000 once you have that up you will need to 2037 01:29:21,600 --> 01:29:26,239 install the c plus plus extension and 2038 01:29:24,000 --> 01:29:28,639 what you need to do is come to the 2039 01:29:26,238 --> 01:29:30,638 extensions tab here and type c and c 2040 01:29:28,639 --> 01:29:33,039 plus plus in here and you're going to 2041 01:29:30,639 --> 01:29:35,920 have an entry for microsoft click on 2042 01:29:33,039 --> 01:29:38,238 that and install this extension on your 2043 01:29:35,920 --> 01:29:40,800 system and your visual studio code 2044 01:29:38,238 --> 01:29:43,519 instance is going to be ready to handle 2045 01:29:40,800 --> 01:29:44,960 some c plus plus development and this is 2046 01:29:43,520 --> 01:29:46,800 really all i had to share in this 2047 01:29:44,960 --> 01:29:49,119 lecture showing you how you can get this 2048 01:29:46,800 --> 01:29:51,600 installed again i apologize i don't have 2049 01:29:49,119 --> 01:29:54,559 access to a mac device so i used my 2050 01:29:51,600 --> 01:29:56,639 linux box running inside window 2051 01:29:54,560 --> 01:29:58,719 subsystem for linux to show you this but 2052 01:29:56,639 --> 01:30:00,800 this is good enough i think if you have 2053 01:29:58,719 --> 01:30:03,119 a problem you can tell me and i will do 2054 01:30:00,800 --> 01:30:05,279 the best i can to help you out for now 2055 01:30:03,119 --> 01:30:07,439 we're going to stop here in this lecture 2056 01:30:05,279 --> 01:30:09,920 in the next one i will try and show you 2057 01:30:07,439 --> 01:30:12,079 how to set up a visual studio code to 2058 01:30:09,920 --> 01:30:13,440 use the compilers that we installed in 2059 01:30:13,439 --> 01:30:17,039 go ahead and finish up here and meet me 2060 01:30:17,039 --> 01:30:21,920 in this lecture i am going to try and 2061 01:30:19,039 --> 01:30:24,238 show you how to configure visual studio 2062 01:30:21,920 --> 01:30:26,639 code to use the compilers that we 2063 01:30:24,238 --> 01:30:28,638 installed on our mac system in a 2064 01:30:26,639 --> 01:30:30,639 previous lecture so we want visual 2065 01:30:28,639 --> 01:30:33,039 studio code to be able to pick up our 2066 01:30:30,639 --> 01:30:35,679 gcc compiler and use it we want it to 2067 01:30:33,039 --> 01:30:37,760 pick up our client compiler and use it 2068 01:30:35,679 --> 01:30:40,719 we also wanted to pick up our clanger 2069 01:30:37,760 --> 01:30:43,600 compiler from apple and use it to build 2070 01:30:40,719 --> 01:30:46,560 binaries that we can then run on our 2071 01:30:43,600 --> 01:30:48,880 system here so let's see how to do this 2072 01:30:46,560 --> 01:30:51,360 i have to say i don't own a mac device 2073 01:30:48,880 --> 01:30:53,279 so the best i can do here is describe 2074 01:30:51,359 --> 01:30:54,960 the process to you and if you have a 2075 01:30:53,279 --> 01:30:57,198 problem you can tell me i will do the 2076 01:30:54,960 --> 01:30:59,679 best i can to help you out okay so once 2077 01:30:57,198 --> 01:31:01,039 you have visual studio code installed 2078 01:30:59,679 --> 01:31:03,359 you're going to create a folder 2079 01:31:01,039 --> 01:31:06,079 somewhere and open visual studio code 2080 01:31:03,359 --> 01:31:08,639 from that folder and what you will 2081 01:31:06,079 --> 01:31:10,158 basically do is use the terminal to 2082 01:31:08,639 --> 01:31:12,560 navigate to a location you're going to 2083 01:31:10,158 --> 01:31:14,559 create a folder you can even call it cpp 2084 01:31:12,560 --> 01:31:17,280 template project you're going to hop 2085 01:31:14,560 --> 01:31:19,920 over into that folder and then open 2086 01:31:17,279 --> 01:31:23,840 visual studio code from that folder here 2087 01:31:19,920 --> 01:31:26,800 so for example we can hop over into cpp 2088 01:31:23,840 --> 01:31:29,600 and run code and say dot that's going to 2089 01:31:26,800 --> 01:31:31,840 open visual studio code in that folder 2090 01:31:29,600 --> 01:31:34,159 and from that folder you will create a 2091 01:31:31,840 --> 01:31:36,639 main cpp file by clicking on the plus 2092 01:31:34,158 --> 01:31:39,279 button here and you will create the cpp 2093 01:31:36,639 --> 01:31:42,639 file call it main cpp and you can put in 2094 01:31:39,279 --> 01:31:45,198 the content here and once you have this 2095 01:31:42,639 --> 01:31:48,560 the next task is going to be to set up a 2096 01:31:45,198 --> 01:31:50,719 compiler to build this little thing so 2097 01:31:48,560 --> 01:31:52,480 once you have this project you will come 2098 01:31:50,719 --> 01:31:55,198 to terminal i think there's going to be 2099 01:31:52,479 --> 01:31:58,079 a terminal menu somewhere and you will 2100 01:31:55,198 --> 01:32:00,158 choose configure tasks once you do that 2101 01:31:58,079 --> 01:32:03,279 visual studio code is smart enough to 2102 01:32:00,158 --> 01:32:05,519 scan your system and find any compiler 2103 01:32:03,279 --> 01:32:07,679 you have installed the first thing that 2104 01:32:05,520 --> 01:32:09,600 is likely to be found is the apple 2105 01:32:07,679 --> 01:32:10,880 client compiler so you're going to find 2106 01:32:10,880 --> 01:32:15,600 plus plus compiler so you can choose 2107 01:32:12,880 --> 01:32:18,000 that and you can use that for the apple 2108 01:32:15,600 --> 01:32:20,639 client compiler we are lucky we even 2109 01:32:18,000 --> 01:32:23,119 have a tutorial on how to do that so if 2110 01:32:20,639 --> 01:32:25,760 we click on our extensions tab here and 2111 01:32:26,960 --> 01:32:32,079 if we scroll down we're going to see a 2112 01:32:28,880 --> 01:32:33,600 link on using the client on mac os here 2113 01:32:32,079 --> 01:32:36,158 this is going to give us all the 2114 01:32:33,600 --> 01:32:38,719 instructions we need to use visual 2115 01:32:36,158 --> 01:32:41,599 studio code with the client compiler 2116 01:32:38,719 --> 01:32:43,119 from the apple company so if you scroll 2117 01:32:41,600 --> 01:32:45,600 down you're going to be able to read up 2118 01:32:43,119 --> 01:32:47,760 on all this but what i really want you 2119 01:32:45,600 --> 01:32:49,280 to see is the configuration file you're 2120 01:32:47,760 --> 01:32:51,679 going to see that it's going to be using 2121 01:32:49,279 --> 01:32:54,639 the clang compiler from the user bin 2122 01:32:51,679 --> 01:32:57,600 directory and this is mostly going to be 2123 01:32:54,639 --> 01:33:00,400 the clang version from the apple company 2124 01:32:57,600 --> 01:33:02,560 and sometimes you don't want this so if 2125 01:33:00,399 --> 01:33:04,960 you want to use the clang version that 2126 01:33:02,560 --> 01:33:07,199 we installed through home brew i think 2127 01:33:04,960 --> 01:33:08,800 you will need to go through the user 2128 01:33:07,198 --> 01:33:10,719 local directory because that's where 2129 01:33:08,800 --> 01:33:12,000 homebrew is going to put it you're going 2130 01:33:10,719 --> 01:33:14,158 to see that that's going to be the 2131 01:33:12,000 --> 01:33:16,479 location here from our tutorial and 2132 01:33:14,158 --> 01:33:19,679 you're going to hunt down for the clan 2133 01:33:16,479 --> 01:33:21,759 compiler and find your binary and that's 2134 01:33:19,679 --> 01:33:23,440 what we're going to be using next but 2135 01:33:21,760 --> 01:33:26,079 for the first time if you want to use 2136 01:33:23,439 --> 01:33:28,079 the clan compiler from apple this is the 2137 01:33:26,079 --> 01:33:30,639 configuration you need and all you need 2138 01:33:28,079 --> 01:33:32,158 to do in this case is change this to c 2139 01:33:32,158 --> 01:33:35,839 now once you have this configured you 2140 01:33:34,238 --> 01:33:38,959 will have the ability to come to 2141 01:33:35,840 --> 01:33:40,319 terminal let's show our files here you 2142 01:33:38,960 --> 01:33:42,158 will have the ability to come to 2143 01:33:40,319 --> 01:33:44,479 terminal and run task and choose the 2144 01:33:42,158 --> 01:33:46,719 compiler that's going to be the clang 2145 01:33:44,479 --> 01:33:48,638 compiler from apple but you can also 2146 01:33:46,719 --> 01:33:50,639 configure other compilers and all you 2147 01:33:48,639 --> 01:33:52,800 really have to do is choose them in the 2148 01:33:50,639 --> 01:33:55,760 list here and you're going to have a 2149 01:33:55,760 --> 01:34:00,079 json file to be exact let's show that up 2150 01:34:00,079 --> 01:34:03,600 and that's going to give you the 2151 01:34:01,359 --> 01:34:06,079 instructions you need to build this 2152 01:34:03,600 --> 01:34:07,679 and in most cases you will just need to 2153 01:34:06,079 --> 01:34:10,158 be careful about the path to the 2154 01:34:07,679 --> 01:34:12,239 compiler so for example if you are using 2155 01:34:10,158 --> 01:34:14,719 the gcc version you installed through 2156 01:34:12,238 --> 01:34:17,119 homo brew you're going to find that in 2157 01:34:14,719 --> 01:34:19,520 your location you're going to make sure 2158 01:34:17,119 --> 01:34:22,559 that's the correct one and you will put 2159 01:34:19,520 --> 01:34:25,520 in these instructions here to tell your 2160 01:34:22,560 --> 01:34:28,080 compiler to build every single cpp file 2161 01:34:25,520 --> 01:34:29,600 in the directory here this is what we do 2162 01:34:28,079 --> 01:34:32,079 if you use clan you're going to have 2163 01:34:29,600 --> 01:34:34,079 something like this and again i am 2164 01:34:32,079 --> 01:34:36,639 describing this to you because i don't 2165 01:34:34,079 --> 01:34:38,238 have a mac available to me right now 2166 01:34:36,639 --> 01:34:41,039 and if you have a problem you can tell 2167 01:34:38,238 --> 01:34:44,079 me we can try and sort this out okay and 2168 01:34:41,039 --> 01:34:46,399 by this time you will have the ability 2169 01:34:44,079 --> 01:34:47,760 to use your visual studio code instance 2170 01:34:47,760 --> 01:34:53,039 c plus plus programs using either the 2171 01:34:50,560 --> 01:34:55,520 apple client compiler the llvm glango 2172 01:34:53,039 --> 01:34:57,359 compiler or the gcc compiler and all 2173 01:34:55,520 --> 01:34:59,920 these are going to be compiling in c 2174 01:34:57,359 --> 01:35:02,000 plus plus 20 mode again to both you need 2175 01:34:59,920 --> 01:35:03,760 to make sure you have the file selected 2176 01:35:02,000 --> 01:35:05,920 and choose whatever compiler you want to 2177 01:35:03,760 --> 01:35:08,400 use for example if you want to use clone 2178 01:35:05,920 --> 01:35:09,920 12 from homebrew you're going to you're 2179 01:35:08,399 --> 01:35:12,000 going to select that and it is going to 2180 01:35:09,920 --> 01:35:13,600 be used to build your project and you 2181 01:35:12,000 --> 01:35:16,000 will have the ability to run your 2182 01:35:13,600 --> 01:35:18,079 projects just like we do here and it is 2183 01:35:16,000 --> 01:35:20,800 going to work let's make sure we are 2184 01:35:18,079 --> 01:35:23,118 running rooster here our binary 2185 01:35:20,800 --> 01:35:25,520 and it is going to run and printer 0 and 2186 01:35:23,118 --> 01:35:27,198 this is going to work again i am really 2187 01:35:25,520 --> 01:35:28,400 sorry i don't have a mac device 2188 01:35:28,399 --> 01:35:33,118 but this is what i have to work with now 2189 01:35:31,039 --> 01:35:35,118 if you have a problem on a mac device 2190 01:35:33,118 --> 01:35:37,920 please tell me i will do the best i can 2191 01:35:38,319 --> 01:35:43,679 in this lecture we're going to explore 2192 01:35:40,238 --> 01:35:46,158 the option to use online compilers if 2193 01:35:43,679 --> 01:35:49,520 for whatever reason we can't install a 2194 01:35:46,158 --> 01:35:51,599 radio compiler on our system and again 2195 01:35:49,520 --> 01:35:54,800 the goal is to be able to turn the code 2196 01:35:51,600 --> 01:35:57,679 we type into what looks like an ide 2197 01:35:54,800 --> 01:36:00,880 into a compiler and get a binary that we 2198 01:35:57,679 --> 01:36:03,039 can run and see the output of there are 2199 01:36:00,880 --> 01:36:05,920 a few online compilers that are very 2200 01:36:03,039 --> 01:36:08,000 popular within the c plus plus community 2201 01:36:05,920 --> 01:36:11,520 one box is one of them there is also 2202 01:36:08,000 --> 01:36:14,158 compiler explorer and kodiro but there 2203 01:36:11,520 --> 01:36:16,480 may be other ones out there if you do 2204 01:36:14,158 --> 01:36:19,198 research on your own we're going to go 2205 01:36:16,479 --> 01:36:22,000 to the browser and actually check each 2206 01:36:19,198 --> 01:36:25,839 of these out okay here we are in our 2207 01:36:22,000 --> 01:36:27,920 browser here i am on onebox.org 2208 01:36:25,840 --> 01:36:30,159 and you see that it really looks like an 2209 01:36:27,920 --> 01:36:32,960 ide inside we have the code we've been 2210 01:36:30,158 --> 01:36:36,079 running all along to make sure that it 2211 01:36:32,960 --> 01:36:38,000 compiles with c plus plus 20. on the 2212 01:36:36,079 --> 01:36:40,399 left here we have a few options we can 2213 01:36:38,000 --> 01:36:42,719 select which compiler we want to use you 2214 01:36:40,399 --> 01:36:43,839 see that there is a couple of gcc 2215 01:36:43,840 --> 01:36:48,159 you can see that this is a really nice 2216 01:36:48,158 --> 01:36:54,479 gcc versions you see we have clan 12 2217 01:36:51,920 --> 01:36:57,679 so if you want to test clang out you can 2218 01:36:54,479 --> 01:37:00,079 select it let's actually try this 2219 01:36:57,679 --> 01:37:02,880 so we're going to use clang head 2220 01:37:02,880 --> 01:37:09,199 so let's hit enter and see if clank 12 2221 01:37:09,198 --> 01:37:12,638 and you see that it really works clan 2222 01:37:12,639 --> 01:37:17,039 should support c plus plus 20 like this 2223 01:37:17,039 --> 01:37:22,399 gcc because that's what i like and you 2224 01:37:20,399 --> 01:37:25,279 can try putting different things in your 2225 01:37:22,399 --> 01:37:28,399 c plus plus code to see if the compiler 2226 01:37:25,279 --> 01:37:29,920 actually responds for example we can say 2227 01:37:37,279 --> 01:37:42,639 the message output here another good 2228 01:37:40,000 --> 01:37:45,920 thing with these online compilers is 2229 01:37:42,639 --> 01:37:48,000 that you can share this code and anybody 2230 01:37:45,920 --> 01:37:50,639 will be able to look at the code here 2231 01:37:48,000 --> 01:37:51,920 and run it so this is a really good way 2232 01:37:51,920 --> 01:37:57,679 get help from your instructor so i do 2233 01:37:55,198 --> 01:37:59,678 encourage you if you run into a problem 2234 01:37:57,679 --> 01:38:01,679 come and use one of these compilers and 2235 01:37:59,679 --> 01:38:04,480 send me the link the way you can send 2236 01:38:01,679 --> 01:38:07,039 the link you can hit on share here this 2237 01:38:04,479 --> 01:38:09,678 button here you're going to get the url 2238 01:38:07,039 --> 01:38:12,079 that you can copy and send to anybody 2239 01:38:09,679 --> 01:38:14,000 and they will be able to see your code 2240 01:38:12,079 --> 01:38:16,079 just like we're going to see here in a 2241 01:38:14,000 --> 01:38:18,399 minute okay you see that it isn't the 2242 01:38:16,079 --> 01:38:20,319 exact same thing and anybody on the 2243 01:38:18,399 --> 01:38:22,559 internet will be able to see your code 2244 01:38:20,319 --> 01:38:24,158 and try to find whatever the problem is 2245 01:38:24,158 --> 01:38:29,039 very fast one box is one compiler you 2246 01:38:27,039 --> 01:38:31,359 can play with the options that we have 2247 01:38:31,359 --> 01:38:35,679 load different libraries you see they 2248 01:38:33,520 --> 01:38:38,400 have a lot of boost libraries but this 2249 01:38:35,679 --> 01:38:40,079 is of no interest to us right now you 2250 01:38:38,399 --> 01:38:42,799 can play with it and see what you like 2251 01:38:40,079 --> 01:38:44,559 about it another one is kaliru which is 2252 01:38:42,800 --> 01:38:47,119 another online compiler you see it 2253 01:38:44,560 --> 01:38:49,280 doesn't have a lot of options like we 2254 01:38:47,118 --> 01:38:51,279 had in one box for example you cannot 2255 01:38:49,279 --> 01:38:54,719 change the compiler it's going to be 2256 01:38:51,279 --> 01:38:56,639 using g plus plus but if we hit compile 2257 01:38:54,719 --> 01:38:58,800 link and run it's going to run our code 2258 01:38:56,639 --> 01:39:00,960 and we're going to get the output here 2259 01:38:58,800 --> 01:39:02,960 again we can try and change this so that 2260 01:39:00,960 --> 01:39:04,960 you know i'm not lying to you so we can 2261 01:39:09,118 --> 01:39:15,359 std endl if you want you can hit control 2262 01:39:12,560 --> 01:39:17,600 plus on your browser and it's going to 2263 01:39:17,600 --> 01:39:24,159 and if we compile link and run 2264 01:39:21,600 --> 01:39:26,159 we should see the sky is blue in the 2265 01:39:24,158 --> 01:39:28,719 output window here this is really all 2266 01:39:26,158 --> 01:39:30,719 you can do with kolero and if you like 2267 01:39:28,719 --> 01:39:32,960 it you can donate to the developers i 2268 01:39:30,719 --> 01:39:36,399 think we have the same thing 2269 01:39:32,960 --> 01:39:39,520 on one box but it is in japanese so i 2270 01:39:39,520 --> 01:39:44,320 okay another one is compiler explorer 2271 01:39:42,238 --> 01:39:46,559 which is a really good one the main 2272 01:39:44,319 --> 01:39:49,279 purpose of this is really not giving you 2273 01:39:46,560 --> 01:39:52,239 the output it is actually seeing 2274 01:39:49,279 --> 01:39:54,399 assembly code that is generated for your 2275 01:39:52,238 --> 01:39:56,638 c plus plus code this is something a 2276 01:39:54,399 --> 01:39:58,719 little bit advanced for our purposes 2277 01:39:56,639 --> 01:40:01,679 here so we're going to ignore this we're 2278 01:39:58,719 --> 01:40:04,158 just going to be using it to test our 2279 01:40:01,679 --> 01:40:07,039 code with some of the popular compilers 2280 01:40:04,158 --> 01:40:08,879 if you look here on this drop box you 2281 01:40:07,039 --> 01:40:11,039 see that they also have a lot of 2282 01:40:08,880 --> 01:40:13,600 compilers you can use different gcc 2283 01:40:11,039 --> 01:40:16,800 compilers i think they have clan they 2284 01:40:13,600 --> 01:40:20,079 even have msvc how cool is this so you 2285 01:40:16,800 --> 01:40:22,079 can use a lot of compilers gcc clang you 2286 01:40:20,079 --> 01:40:23,760 see we have a lot of clan compilers we 2287 01:40:23,760 --> 01:40:28,960 11 here so this is a really good way to 2288 01:40:26,479 --> 01:40:30,638 try things out we're going to come back 2289 01:40:32,399 --> 01:40:38,719 and i noticed that i passed the flag of 2290 01:40:35,359 --> 01:40:41,519 c plus 20 to be able to support c plus 2291 01:40:38,719 --> 01:40:44,158 plus 20. again if i come here in my code 2292 01:40:46,639 --> 01:40:51,440 don't worry about these errors they're 2293 01:40:48,238 --> 01:40:53,439 going to go away in a minute 2294 01:40:51,439 --> 01:40:55,839 let's wait for the program to compile 2295 01:40:53,439 --> 01:40:58,799 you see that it is compiling let's wait 2296 01:40:55,840 --> 01:40:59,920 a minute the sky is white and everything 2297 01:40:59,920 --> 01:41:04,000 if you don't see this output here it is 2298 01:41:01,920 --> 01:41:06,800 possible that you didn't check this 2299 01:41:04,000 --> 01:41:08,319 checkbox run the compiled output 2300 01:41:08,319 --> 01:41:13,039 dropbox and make sure this is checked 2301 01:41:10,719 --> 01:41:16,239 and you should have a window like i have 2302 01:41:13,039 --> 01:41:18,880 here compiler explorer also allows you 2303 01:41:16,238 --> 01:41:20,000 to share your code with people on the 2304 01:41:20,000 --> 01:41:24,000 let me see if i can find the option to 2305 01:41:24,000 --> 01:41:30,399 you come to this drop box and you get a 2306 01:41:33,118 --> 01:41:38,479 and open a private window in my browser 2307 01:41:36,000 --> 01:41:39,439 here so that we know that it is some 2308 01:41:39,439 --> 01:41:44,719 browser that is going to be opening this 2309 01:41:42,399 --> 01:41:47,198 link and getting access to the same code 2310 01:41:44,719 --> 01:41:48,719 we have in compiler explorer and this is 2311 01:41:48,719 --> 01:41:52,319 and you see that we get compiler 2312 01:41:50,479 --> 01:41:53,118 explorer privacy we're going to close 2313 01:41:53,118 --> 01:41:57,198 and you see that we have the same code 2314 01:41:55,039 --> 01:41:59,198 we can try modifying this code for 2315 01:41:57,198 --> 01:42:01,439 example we can take out this third line 2316 01:41:59,198 --> 01:42:03,759 here it's going to start compiling you 2317 01:42:01,439 --> 01:42:06,000 see that it is compiling and it is going 2318 01:42:03,760 --> 01:42:08,639 to give us the output that we expect 2319 01:42:06,000 --> 01:42:11,760 from this so this is a really good tool 2320 01:42:08,639 --> 01:42:14,319 to try out if you want to share your 2321 01:42:11,760 --> 01:42:16,000 code with some people that are not close 2322 01:42:14,319 --> 01:42:18,719 to you this is a really good way to do 2323 01:42:16,000 --> 01:42:21,198 this it is also good to try out 2324 01:42:18,719 --> 01:42:24,319 different compilers for example if you 2325 01:42:21,198 --> 01:42:26,479 don't have a gcc compiler you can try it 2326 01:42:24,319 --> 01:42:29,439 out here and see if it does things for 2327 01:42:26,479 --> 01:42:32,000 example we didn't install clang on our 2328 01:42:29,439 --> 01:42:34,879 system but we can try it out and see if 2329 01:42:32,000 --> 01:42:37,359 it runs or could but make sure you pass 2330 01:42:34,880 --> 01:42:39,600 the correct flats here because you are 2331 01:42:37,359 --> 01:42:41,599 responsible for this if you pass the 2332 01:42:41,600 --> 01:42:46,639 you're going to get arrows for example 2333 01:42:49,840 --> 01:42:57,279 and oh and recognize command line option 2334 01:42:52,880 --> 01:42:59,118 std hello but if we say c plus plus 20 2335 01:42:57,279 --> 01:43:01,519 it is going to work so make sure you 2336 01:43:01,520 --> 01:43:06,320 in this lecture you're going to write 2337 01:43:03,198 --> 01:43:08,638 your first c plus 20 program and we're 2338 01:43:06,319 --> 01:43:10,880 going to start and attempt to understand 2339 01:43:08,639 --> 01:43:13,440 our program let's hop over to visual 2340 01:43:10,880 --> 01:43:15,679 studio code and actually do that the 2341 01:43:13,439 --> 01:43:18,479 first thing we want to do is to set up 2342 01:43:15,679 --> 01:43:20,880 our code so that we can open that in 2343 01:43:18,479 --> 01:43:24,399 visual studio code we're going to start 2344 01:43:20,880 --> 01:43:26,400 from our c plus 20 template project that 2345 01:43:26,399 --> 01:43:31,279 and i'm going to be doing the projects 2346 01:43:28,479 --> 01:43:33,439 for this chapter in this folder here so 2347 01:43:31,279 --> 01:43:35,679 we're going to do our very first c plus 2348 01:43:33,439 --> 01:43:39,279 plus program so what we're going to do 2349 01:43:35,679 --> 01:43:40,560 is go inside our c plus plus 20 template 2350 01:43:40,560 --> 01:43:45,520 and i noticed that we actually left in 2351 01:43:42,719 --> 01:43:47,600 the boiled files we don't want so 2352 01:43:45,520 --> 01:43:49,199 what we're going to do is remove this 2353 01:43:49,198 --> 01:43:54,079 and that this vc 140 fan we don't want 2354 01:43:55,920 --> 01:44:02,079 the main cpp file and the dot vs code 2355 01:43:59,760 --> 01:44:04,800 folder we're going to copy these things 2356 01:44:02,079 --> 01:44:07,198 and we're going to put them in our first 2357 01:44:07,198 --> 01:44:11,759 and we're going to open this folder in 2358 01:44:08,960 --> 01:44:14,399 visual studio code visual studio code is 2359 01:44:11,760 --> 01:44:17,280 not opened up you see that i have my 2360 01:44:14,399 --> 01:44:19,599 developer powershell opened if you want 2361 01:44:17,279 --> 01:44:21,519 you can open visual studio code directly 2362 01:44:19,600 --> 01:44:23,119 without going through this if you're not 2363 01:44:21,520 --> 01:44:26,000 going to be using the visual studio 2364 01:44:23,118 --> 01:44:28,319 compiler but i want to have the option 2365 01:44:26,000 --> 01:44:30,319 to use that compiler so this is how i am 2366 01:44:28,319 --> 01:44:32,840 going to open my visual studio code 2367 01:44:34,719 --> 01:44:39,679 to open this up i'm going to hit enter 2368 01:44:37,279 --> 01:44:41,679 and visual studio code is going to open 2369 01:44:39,679 --> 01:44:44,079 and it is going to open whatever we did 2370 01:44:41,679 --> 01:44:46,158 in the last lecture we don't want this 2371 01:44:44,079 --> 01:44:48,158 project because this is a template 2372 01:44:46,158 --> 01:44:51,279 project from the last chapter we're 2373 01:44:48,158 --> 01:44:52,638 going to close this folder go to file 2374 01:44:52,639 --> 01:44:57,679 close folder and we're going to open the 2375 01:44:54,960 --> 01:45:00,560 new folder we want to work on we're 2376 01:44:57,679 --> 01:45:02,239 going to go to file and hit open folder 2377 01:45:00,560 --> 01:45:04,239 we're going to go to the location where 2378 01:45:02,238 --> 01:45:06,079 our project is so you're going to go 2379 01:45:04,238 --> 01:45:07,039 where yours is i'm going to browse to 2380 01:45:07,039 --> 01:45:12,960 and i'm going to go in the current 2381 01:45:08,960 --> 01:45:14,000 chapter and choose 3 2 first c plus plus 2382 01:45:16,639 --> 01:45:20,800 it's going to open up and you're going 2383 01:45:20,800 --> 01:45:27,039 the last template project are still 2384 01:45:24,399 --> 01:45:29,039 valid here they were copied over if we 2385 01:45:27,039 --> 01:45:30,639 open the main cpp file it's going to be 2386 01:45:29,039 --> 01:45:31,679 the main thing we had in the last 2387 01:45:32,399 --> 01:45:37,118 we have the option to run task you're 2388 01:45:34,960 --> 01:45:39,760 going to see that we have two compilers 2389 01:45:37,118 --> 01:45:42,399 available and this is really good we are 2390 01:45:39,760 --> 01:45:45,119 ready to start using this let's try and 2391 01:45:45,118 --> 01:45:48,479 and we're going to have our binary 2392 01:45:48,479 --> 01:45:53,359 we can click in the terminal here 2393 01:45:51,439 --> 01:45:54,879 and if we want to open the terminal 2394 01:45:53,359 --> 01:45:57,839 again we have the option to come in 2395 01:45:54,880 --> 01:46:00,480 terminal and new terminal this is going 2396 01:45:57,840 --> 01:46:01,679 to open this up if we do dir in the 2397 01:46:01,679 --> 01:46:07,679 we're going to see our program generated 2398 01:46:04,560 --> 01:46:08,719 we can remove this thing here and say rm 2399 01:46:08,719 --> 01:46:15,679 dot exe and remove this we can clear 2400 01:46:13,520 --> 01:46:17,840 and we have a clean project we can start 2401 01:46:15,679 --> 01:46:20,158 learning from okay now we have the 2402 01:46:17,840 --> 01:46:22,639 project and it is building successfully 2403 01:46:20,158 --> 01:46:25,279 let's try to understand the code we have 2404 01:46:22,639 --> 01:46:28,000 in here the first statement we have it 2405 01:46:28,000 --> 01:46:34,000 this is a feature c plus plus offers to 2406 01:46:31,039 --> 01:46:37,039 load the pro built libraries that we can 2407 01:46:34,000 --> 01:46:39,600 use iostream is specifically going to 2408 01:46:37,039 --> 01:46:42,000 help us print thanks to the console 2409 01:46:39,600 --> 01:46:43,280 we've seen that we can for example print 2410 01:46:44,479 --> 01:46:51,678 let's take out all these things in here 2411 01:46:47,600 --> 01:46:54,639 and say std column column c out 2412 01:46:51,679 --> 01:46:57,440 this is coming from i iostream and we're 2413 01:46:54,639 --> 01:46:59,920 going to put two less than symbols we're 2414 01:46:57,439 --> 01:47:02,079 going to open two quotes and inside 2415 01:46:59,920 --> 01:47:05,039 we're going to type hello world 2416 01:47:02,079 --> 01:47:09,519 and we're going to go outside the quotes 2417 01:47:05,039 --> 01:47:12,319 and do two less than signs and say std 2418 01:47:12,319 --> 01:47:16,960 and put a semicolon this is going to 2419 01:47:14,800 --> 01:47:18,400 print things on the console if we run 2420 01:47:18,399 --> 01:47:23,599 so if we run this program the first 2421 01:47:21,520 --> 01:47:26,719 thing we need to do is to build this 2422 01:47:23,600 --> 01:47:29,760 program so we can come to terminal task 2423 01:47:26,719 --> 01:47:32,480 and we're going to build it with gcc and 2424 01:47:29,760 --> 01:47:34,239 after we've built it we can hit enter to 2425 01:47:34,238 --> 01:47:38,799 and we can do dirt to see the content of 2426 01:47:36,880 --> 01:47:41,600 the folder you see that a program has 2427 01:47:38,800 --> 01:47:43,440 been generated we can run it and if we 2428 01:47:41,600 --> 01:47:46,320 run it it's going to say hello world 2429 01:47:43,439 --> 01:47:48,559 this message is coming from this 2430 01:47:46,319 --> 01:47:51,679 statement here and we have this 2431 01:47:48,560 --> 01:47:54,400 statement because we have included io 2432 01:47:51,679 --> 01:47:57,039 stream to really make this super clear 2433 01:47:54,399 --> 01:47:59,039 we can try to remove this line that says 2434 01:47:59,039 --> 01:48:03,279 and the moment we do that you're going 2435 01:48:00,960 --> 01:48:04,480 to see that this tab says we have 2436 01:48:07,039 --> 01:48:11,600 you're going to see the message 2437 01:48:08,639 --> 01:48:14,639 namespace sdd has no member c out name 2438 01:48:11,600 --> 01:48:16,079 space std has no member a and dl 2439 01:48:16,079 --> 01:48:21,439 the z plus plus program doesn't know 2440 01:48:18,479 --> 01:48:23,839 what sddc out means because we don't 2441 01:48:21,439 --> 01:48:25,919 have the library included and you also 2442 01:48:23,840 --> 01:48:27,920 see that we have these squiggly lines to 2443 01:48:25,920 --> 01:48:30,639 make it really clear that we have a 2444 01:48:27,920 --> 01:48:34,158 problem so to really be able to use this 2445 01:48:30,639 --> 01:48:36,400 we have to include a third party library 2446 01:48:34,158 --> 01:48:38,799 that brings these features into our 2447 01:48:36,399 --> 01:48:41,118 program and that's what the include 2448 01:48:38,800 --> 01:48:42,800 statement here is really doing so let's 2449 01:48:42,800 --> 01:48:48,880 and we're going to say i o stream again 2450 01:48:46,800 --> 01:48:50,159 and let's wait and see that the problem 2451 01:48:50,158 --> 01:48:54,319 and the one thing you should really know 2452 01:48:51,920 --> 01:48:57,440 is that we don't have a semicolon at the 2453 01:48:54,319 --> 01:49:00,158 end of the io stream statement we don't 2454 01:48:57,439 --> 01:49:03,039 need to put it there but many c 2455 01:49:00,158 --> 01:49:04,079 plus plus statements need a semicolon at 2456 01:49:04,079 --> 01:49:09,679 okay i think this really makes it clear 2457 01:49:06,319 --> 01:49:11,920 what the include statement here is doing 2458 01:49:09,679 --> 01:49:15,359 the other thing we have in our program 2459 01:49:17,520 --> 01:49:20,800 this thing here is called the main 2460 01:49:20,800 --> 01:49:26,079 a function in c plus plus is a block of 2461 01:49:23,520 --> 01:49:28,880 code that does a lot of things 2462 01:49:26,079 --> 01:49:31,359 but the main function here is special 2463 01:49:28,880 --> 01:49:33,440 because it is the starting point of your 2464 01:49:33,439 --> 01:49:38,719 we also call the main function the entry 2465 01:49:36,000 --> 01:49:41,039 point of the c plus plus program and 2466 01:49:38,719 --> 01:49:43,760 what that means is that if you open the 2467 01:49:41,039 --> 01:49:46,238 program the main function is going to be 2468 01:49:43,760 --> 01:49:48,079 the very first thing that is going to 2469 01:49:46,238 --> 01:49:50,879 run and the things are going to be 2470 01:49:48,079 --> 01:49:52,880 executed in the main function in order 2471 01:49:50,880 --> 01:49:55,279 so the statement on the top is going to 2472 01:49:52,880 --> 01:49:57,359 run first and the statement after that 2473 01:49:55,279 --> 01:49:59,359 until we hit the end of the function so 2474 01:49:57,359 --> 01:50:02,158 this is what we mean here to really 2475 01:49:59,359 --> 01:50:05,519 drive this home we can come here in our 2476 01:50:06,880 --> 01:50:13,840 and say sdd engl and by the way this e n 2477 01:50:11,520 --> 01:50:14,960 d l statement is going to print a new 2478 01:50:16,079 --> 01:50:22,000 see it in effect in a moment let's 2479 01:50:19,520 --> 01:50:25,199 actually take it out so that we can see 2480 01:50:22,000 --> 01:50:28,000 its effect we're going to also do sddc 2481 01:50:30,158 --> 01:50:36,719 is to print number one and number two on 2482 01:50:33,279 --> 01:50:38,960 the terminal let's build this program 2483 01:50:36,719 --> 01:50:42,079 and if you want to build the default 2484 01:50:38,960 --> 01:50:44,719 task you can hit ctrl shift b for it to 2485 01:50:42,079 --> 01:50:47,519 happen very fast but i am always going 2486 01:50:44,719 --> 01:50:50,239 to come here so that it is really clear 2487 01:50:47,520 --> 01:50:53,599 what i am doing a program should be 2488 01:50:50,238 --> 01:50:55,000 generated now if we try to run it 2489 01:50:55,000 --> 01:51:01,118 rooster.exe and again we can hit 2490 01:50:58,079 --> 01:51:03,760 the app arrow button on our keyboard so 2491 01:51:01,118 --> 01:51:05,759 that's the last command that we run from 2492 01:51:03,760 --> 01:51:07,840 this terminal runs and this is going to 2493 01:51:05,760 --> 01:51:10,079 speed things up a little bit so if we 2494 01:51:07,840 --> 01:51:13,199 run this program it should say number 2495 01:51:10,079 --> 01:51:16,238 one and number two as you see here 2496 01:51:13,198 --> 01:51:19,039 but these two things are cramped on one 2497 01:51:16,238 --> 01:51:21,678 line and the reason is we are not 2498 01:51:19,039 --> 01:51:24,319 printing new line characters after we 2499 01:51:21,679 --> 01:51:28,000 print number one and number two and we 2500 01:51:24,319 --> 01:51:31,439 can solve this problem by putting in two 2501 01:51:28,000 --> 01:51:33,679 less than symbols and saying std column 2502 01:51:33,679 --> 01:51:38,000 this is going to put a new line 2503 01:51:35,439 --> 01:51:39,118 character after number one we can build 2504 01:51:39,118 --> 01:51:43,679 for our new changes to tech effect 2505 01:51:42,158 --> 01:51:46,799 after we do that we click on the 2506 01:51:43,679 --> 01:51:48,880 terminal hit enter we hit the app arrow 2507 01:51:46,800 --> 01:51:51,599 button if we run it's going to say 2508 01:51:48,880 --> 01:51:54,159 number one and number two on two 2509 01:51:51,599 --> 01:51:56,639 separate lines because we are printing a 2510 01:51:54,158 --> 01:51:58,719 new line character after number one i 2511 01:51:56,639 --> 01:52:00,960 really hope this makes sense 2512 01:51:58,719 --> 01:52:02,960 please do take some time to play with us 2513 01:52:00,960 --> 01:52:05,279 because if this is the first time you're 2514 01:52:02,960 --> 01:52:08,000 doing this some things might not be 2515 01:52:05,279 --> 01:52:10,880 clear and the best way to really make 2516 01:52:08,000 --> 01:52:13,118 sense of this is to type code run it and 2517 01:52:10,880 --> 01:52:15,118 see the effect of what you type by 2518 01:52:13,118 --> 01:52:17,279 running your program so let's do this a 2519 01:52:15,118 --> 01:52:19,118 couple of times we're going to do sdd 2520 01:52:20,399 --> 01:52:25,359 the statement for number two we're going 2521 01:52:28,158 --> 01:52:34,719 and we're going to do stdeandl 2522 01:52:32,079 --> 01:52:36,039 to really show the problem again we're 2523 01:52:36,039 --> 01:52:41,920 stdendl on the number two line 2524 01:52:39,279 --> 01:52:43,519 we are going to weld the build is going 2525 01:52:43,520 --> 01:52:48,480 we're going to click in the terminal hit 2526 01:52:45,599 --> 01:52:50,159 enter hit the app arrow button run the 2527 01:52:48,479 --> 01:52:52,238 program again and you're going to see 2528 01:52:50,158 --> 01:52:55,198 it's saying number one it's going on the 2529 01:52:52,238 --> 01:52:57,198 new line printing number two but we not 2530 01:52:55,198 --> 01:52:59,919 to jump into the new line from number 2531 01:52:57,198 --> 01:53:02,319 two so number two and number three are 2532 01:52:59,920 --> 01:53:04,800 going to be cramped on one line that's 2533 01:53:02,319 --> 01:53:07,439 what we see here we can solve this again 2534 01:53:04,800 --> 01:53:08,639 by putting in a new line after number 2535 01:53:12,479 --> 01:53:17,279 and uh this should solve the problem 2536 01:53:14,960 --> 01:53:19,760 again let's build we're going to run our 2537 01:53:17,279 --> 01:53:21,439 build task the build is going to be 2538 01:53:19,760 --> 01:53:23,599 successful we're going to hit the app 2539 01:53:21,439 --> 01:53:25,839 arrow to run the program if we run it 2540 01:53:23,599 --> 01:53:28,880 it's going to say number one number two 2541 01:53:25,840 --> 01:53:31,599 and number three and this is really good 2542 01:53:28,880 --> 01:53:34,400 now it should be clear that statements 2543 01:53:31,599 --> 01:53:36,719 in the main function are run in order 2544 01:53:34,399 --> 01:53:40,319 from top to bottom and that the program 2545 01:53:36,719 --> 01:53:43,520 is going to end when it hits the end of 2546 01:53:40,319 --> 01:53:45,599 this curly brace that includes our 2547 01:53:43,520 --> 01:53:47,679 function we're going to have a chance to 2548 01:53:47,679 --> 01:53:52,079 in detail later in the course but we are 2549 01:53:50,399 --> 01:53:54,079 just getting started and we need to 2550 01:53:52,079 --> 01:53:56,399 start somewhere so that's why we're 2551 01:53:54,079 --> 01:53:59,519 doing this main function thing here 2552 01:53:56,399 --> 01:54:02,559 another thing you can do is return from 2553 01:53:59,520 --> 01:54:05,199 the main function and in many examples 2554 01:54:02,560 --> 01:54:06,800 you will see people do return 0 2555 01:54:06,800 --> 01:54:12,239 and this is a way for the main function 2556 01:54:09,359 --> 01:54:15,279 to send the message to the operating 2557 01:54:12,238 --> 01:54:17,598 system if it's finished successfully or 2558 01:54:15,279 --> 01:54:19,519 if there was some kind of problem 2559 01:54:17,599 --> 01:54:21,360 we're not going to go into any more 2560 01:54:19,520 --> 01:54:22,560 details about the main function because 2561 01:54:22,560 --> 01:54:27,840 too much for now we are just getting 2562 01:54:25,118 --> 01:54:30,719 started we are trying to get a feel of 2563 01:54:27,840 --> 01:54:33,279 how c plus works the main message here 2564 01:54:30,719 --> 01:54:36,560 is really that you should be able to 2565 01:54:33,279 --> 01:54:38,880 print things to the console using sddc 2566 01:54:36,560 --> 01:54:40,960 out you should be able to print a new 2567 01:54:40,960 --> 01:54:49,599 using sdd ndl this stdc out and 2568 01:54:45,880 --> 01:54:52,239 stdendl thank come from the iostream 2569 01:54:49,599 --> 01:54:53,920 library that we are including here 2570 01:54:52,238 --> 01:54:56,158 if we don't include this library we're 2571 01:54:53,920 --> 01:54:58,480 going to have problems because 2572 01:54:56,158 --> 01:55:00,960 our surplus plus program is not going to 2573 01:54:58,479 --> 01:55:03,279 know where they are coming from so we 2574 01:55:00,960 --> 01:55:04,960 should really put this in if we want to 2575 01:55:04,960 --> 01:55:10,560 and we can return from a function to let 2576 01:55:07,679 --> 01:55:13,599 the system know the operating system 2577 01:55:10,560 --> 01:55:16,320 if the program finished up successfully 2578 01:55:13,599 --> 01:55:18,400 or if there was some kind of problem and 2579 01:55:16,319 --> 01:55:20,319 again this project is using the 2580 01:55:20,319 --> 01:55:25,198 and this is really all we set out to do 2581 01:55:22,479 --> 01:55:27,678 in this lecture trying to make sense of 2582 01:55:25,198 --> 01:55:29,919 the main function in c plus plus to 2583 01:55:27,679 --> 01:55:32,000 really practice these things i would 2584 01:55:29,920 --> 01:55:34,158 challenge you to write a c plus plus 2585 01:55:32,000 --> 01:55:37,198 program just like this that is going to 2586 01:55:34,158 --> 01:55:39,519 print your name 10 times try to do that 2587 01:55:37,198 --> 01:55:42,079 and if you have a problem please do ask 2588 01:55:39,520 --> 01:55:43,119 me and i will do the best i can to help 2589 01:55:43,118 --> 01:55:46,319 this is really all we set out to do in 2590 01:55:44,800 --> 01:55:48,159 this lecture i hope you found it 2591 01:55:46,319 --> 01:55:49,759 interesting we're going to stop here in 2592 01:55:48,158 --> 01:55:52,238 this lecture in the next one we're going 2593 01:55:49,760 --> 01:55:55,039 to start and learn about comments in c 2594 01:55:52,238 --> 01:55:56,559 plus plus go ahead and finish up here 2595 01:55:56,560 --> 01:56:00,560 in this lecture we're going to learn 2596 01:55:57,920 --> 01:56:02,800 about comments and c plus plus 2597 01:56:00,560 --> 01:56:05,920 comments are the way to put some 2598 01:56:02,800 --> 01:56:08,159 messages in your c plus plus chord that 2599 01:56:05,920 --> 01:56:10,319 are really meant to be read by humans 2600 01:56:08,158 --> 01:56:12,559 and one way we have to do comments in c 2601 01:56:10,319 --> 01:56:14,880 plus plus is through this backslash 2602 01:56:12,560 --> 01:56:17,039 backslash thing we have here this is 2603 01:56:14,880 --> 01:56:18,719 going to comment out the line so the 2604 01:56:17,039 --> 01:56:20,639 compiler is really not going to care 2605 01:56:18,719 --> 01:56:22,079 about it it's going to think that it is 2606 01:56:20,639 --> 01:56:24,880 something that is meant for the 2607 01:56:22,079 --> 01:56:26,880 developer to see so this is a one line 2608 01:56:24,880 --> 01:56:29,520 comment and this is how you do it in c 2609 01:56:26,880 --> 01:56:31,760 plus plus you can also comment out a 2610 01:56:29,520 --> 01:56:33,360 block of code in your c plus bus code 2611 01:56:31,760 --> 01:56:35,840 and this is something that comes in 2612 01:56:33,359 --> 01:56:39,039 quite handy many times you put your 2613 01:56:35,840 --> 01:56:41,440 comment in a backslash star and a star 2614 01:56:39,039 --> 01:56:43,679 backslash like this this is going to 2615 01:56:41,439 --> 01:56:45,519 comment things out in your code and the 2616 01:56:43,679 --> 01:56:47,760 compiler is really not going to care 2617 01:56:45,520 --> 01:56:50,239 about this let's hop over to visual 2618 01:56:47,760 --> 01:56:52,159 studio code and actually play with us so 2619 01:56:50,238 --> 01:56:54,238 here we are in the folder that is going 2620 01:56:52,158 --> 01:56:55,920 to contain our code the first thing i'm 2621 01:56:54,238 --> 01:56:58,638 going to do is go through the template 2622 01:56:55,920 --> 01:57:00,560 project i'm going to copy my files 2623 01:56:58,639 --> 01:57:02,480 and i'm going to go up again and we're 2624 01:57:00,560 --> 01:57:04,480 going to be learning about comments so 2625 01:57:02,479 --> 01:57:07,359 i'm going to put this in the folder on 2626 01:57:04,479 --> 01:57:09,759 comments i am going to close whatever 2627 01:57:07,359 --> 01:57:11,759 folder we have opened in visual studio 2628 01:57:09,760 --> 01:57:14,159 code and i'm going to browse to the 2629 01:57:11,760 --> 01:57:15,360 location where my project on comment 2630 01:57:15,359 --> 01:57:20,079 my project is in this folder that is 2631 01:57:17,920 --> 01:57:22,480 named three three comments i'm going to 2632 01:57:20,079 --> 01:57:25,198 open the folder and if i open the main 2633 01:57:22,479 --> 01:57:27,359 cpp file i'm going to find the starter 2634 01:57:25,198 --> 01:57:29,279 project we're going to remove this const 2635 01:57:27,359 --> 01:57:31,519 eval thank because we're really not 2636 01:57:29,279 --> 01:57:33,439 ready to understand this yet it was in 2637 01:57:33,439 --> 01:57:38,719 test that our compilers support c plus 2638 01:57:36,319 --> 01:57:40,479 plus 20. so we're going to remove this 2639 01:57:38,719 --> 01:57:42,960 and we're going to put in a message that 2640 01:57:40,479 --> 01:57:45,118 says hello world you already know how to 2641 01:57:42,960 --> 01:57:47,279 do that from the last lecture so we're 2642 01:57:47,279 --> 01:57:54,238 hello world and we're going to say 2643 01:57:50,599 --> 01:57:55,920 stdendl and don't forget this semicolon 2644 01:57:54,238 --> 01:57:58,319 at the end if you do you're going to 2645 01:57:55,920 --> 01:58:00,880 have problems so make sure your code 2646 01:57:58,319 --> 01:58:03,920 looks like this try to build it to make 2647 01:58:00,880 --> 01:58:07,118 sure it's working fine this is going to 2648 01:58:03,920 --> 01:58:09,279 run the gcc tasked world we can click on 2649 01:58:07,118 --> 01:58:10,799 the terminal here and it's going to 2650 01:58:10,800 --> 01:58:16,639 and we can open a new terminal here if 2651 01:58:13,599 --> 01:58:19,360 we want if we do dir we're going to find 2652 01:58:16,639 --> 01:58:21,679 our rooster program inside we can run it 2653 01:58:19,359 --> 01:58:23,359 and it's going to say hello world we 2654 01:58:21,679 --> 01:58:25,440 already know to do this from the last 2655 01:58:23,359 --> 01:58:28,158 lecture and in this one we are going to 2656 01:58:25,439 --> 01:58:31,118 be learning about comments the first 2657 01:58:28,158 --> 01:58:32,638 thing you can do is use slash slash so 2658 01:58:32,639 --> 01:58:40,079 and uh let's put some message here this 2659 01:58:36,158 --> 01:58:41,679 branch and the iostream library this is 2660 01:58:40,079 --> 01:58:44,000 a nice message that is going to give 2661 01:58:41,679 --> 01:58:46,158 some more information about this 2662 01:58:44,000 --> 01:58:48,158 statement to whoever might be reading 2663 01:58:46,158 --> 01:58:50,238 this program and this is called a one 2664 01:58:48,158 --> 01:58:52,319 line comment you can really put this 2665 01:58:50,238 --> 01:58:54,638 wherever you want in your c plus plus 2666 01:58:52,319 --> 01:58:56,799 file we can go in the main function for 2667 01:58:54,639 --> 01:58:59,118 example and say this is going to print 2668 01:58:56,800 --> 01:59:01,520 hello world to the console or the 2669 01:58:59,118 --> 01:59:03,839 terminal we can really put this anywhere 2670 01:59:01,520 --> 01:59:06,320 we can even put it after the return 2671 01:59:03,840 --> 01:59:08,480 statement here and really put in 2672 01:59:06,319 --> 01:59:10,799 whatever piece of information we think 2673 01:59:08,479 --> 01:59:13,598 is going to make sense to wherever it's 2674 01:59:10,800 --> 01:59:16,239 going to be reading our program okay 2675 01:59:13,599 --> 01:59:19,119 this is how you do a line comment you 2676 01:59:16,238 --> 01:59:21,359 can also do comments across different 2677 01:59:19,118 --> 01:59:23,039 lines and the way you do that you put 2678 01:59:23,039 --> 01:59:29,679 slash star and go down and say star 2679 01:59:27,439 --> 01:59:31,759 slash like this and whatever is going to 2680 01:59:29,679 --> 01:59:33,920 be in the middle here is going to be a 2681 01:59:31,760 --> 01:59:36,320 comment this is usually called a block 2682 01:59:33,920 --> 01:59:38,880 comment you can put in whatever you want 2683 01:59:36,319 --> 01:59:40,559 so if you have a piece of text that you 2684 01:59:38,880 --> 01:59:43,520 would like to use as a comment in your 2685 01:59:40,560 --> 01:59:46,159 program this is one way you can do this 2686 01:59:43,520 --> 01:59:48,880 one thing you should know though is that 2687 01:59:46,158 --> 01:59:50,479 you cannot nest these things but before 2688 01:59:48,880 --> 01:59:52,960 we do that let's try to build our 2689 01:59:50,479 --> 01:59:55,359 program to really show that there is no 2690 01:59:52,960 --> 01:59:58,319 problem if we have these comments in the 2691 01:59:55,359 --> 02:00:00,719 world is successful and if we click on 2692 02:00:00,719 --> 02:00:06,158 and hit clear to clear things out in the 2693 02:00:03,599 --> 02:00:08,079 terminal we can try to run our rooster 2694 02:00:06,158 --> 02:00:10,799 program and it's going to say hello 2695 02:00:08,079 --> 02:00:12,960 world you see that all these green lines 2696 02:00:10,800 --> 02:00:15,360 which are comments are really not 2697 02:00:12,960 --> 02:00:17,359 affecting the program all the compiler 2698 02:00:15,359 --> 02:00:19,439 is going to care about is this include 2699 02:00:17,359 --> 02:00:21,359 thing it's going to care about the main 2700 02:00:19,439 --> 02:00:23,598 function here it's going to ignore these 2701 02:00:21,359 --> 02:00:26,000 green things it's going to be like these 2702 02:00:23,599 --> 02:00:28,400 green lines that are our comments are 2703 02:00:26,000 --> 02:00:30,158 not in there this is the effect comments 2704 02:00:28,399 --> 02:00:32,879 have in your program and we're going to 2705 02:00:30,158 --> 02:00:34,638 be using this a lot in this course 2706 02:00:32,880 --> 02:00:38,079 one thing you should know though is that 2707 02:00:34,639 --> 02:00:40,480 you cannot nest these block comments so 2708 02:00:38,079 --> 02:00:44,000 if you try to do something like this 2709 02:00:40,479 --> 02:00:46,479 and uh say this is one comment and uh 2710 02:00:44,000 --> 02:00:48,319 come again and say this is another 2711 02:00:46,479 --> 02:00:50,399 comment you see that we have problems 2712 02:00:48,319 --> 02:00:52,238 already nesting is not allowed you're 2713 02:00:50,399 --> 02:00:54,079 going to get a compiler error and we're 2714 02:00:52,238 --> 02:00:56,319 going to learn more about these errors 2715 02:00:54,079 --> 02:00:58,639 in the next lecture but don't do this 2716 02:00:56,319 --> 02:01:00,799 don't nest these block comments if you 2717 02:00:58,639 --> 02:01:03,520 do that you're going to get some 2718 02:01:00,800 --> 02:01:06,239 confusing errors and this is going to be 2719 02:01:03,520 --> 02:01:08,560 the cause for them so don't nest your 2720 02:01:06,238 --> 02:01:10,879 blog comments it's going to give you 2721 02:01:08,560 --> 02:01:12,560 headaches okay this is really all we set 2722 02:01:10,880 --> 02:01:15,199 out to do in this lecture learning about 2723 02:01:12,560 --> 02:01:17,840 comments in c plus plus you can do one 2724 02:01:15,198 --> 02:01:20,479 line comments on a single line like this 2725 02:01:17,840 --> 02:01:23,679 you can do block comments to comment out 2726 02:01:20,479 --> 02:01:25,439 a block of text you can't nest your blog 2727 02:01:23,679 --> 02:01:27,520 comments because that's going to give 2728 02:01:25,439 --> 02:01:31,759 you a lot of problems you don't want 2729 02:01:27,520 --> 02:01:33,840 that and really use comments wisely and 2730 02:01:31,760 --> 02:01:36,079 don't overdo it because it's going to 2731 02:01:33,840 --> 02:01:38,319 turn your program into a novel and i 2732 02:01:36,079 --> 02:01:40,079 don't think people want that so use this 2733 02:01:38,319 --> 02:01:42,000 feature when you need it to give some 2734 02:01:40,079 --> 02:01:44,238 more information about your code and 2735 02:01:42,000 --> 02:01:46,000 don't overdo this we are going to stop 2736 02:01:44,238 --> 02:01:48,319 here in this lecture the next one we're 2737 02:01:46,000 --> 02:01:51,118 going to learn about different kinds of 2738 02:01:48,319 --> 02:01:53,039 errors and warnings you can see in your 2739 02:01:53,039 --> 02:01:57,439 go ahead and finish up here and meet me 2740 02:01:55,679 --> 02:02:00,158 there in this lecture we're going to 2741 02:01:57,439 --> 02:02:02,719 learn about errors and warnings in your 2742 02:02:00,158 --> 02:02:05,519 c plus plus programs there are three 2743 02:02:02,719 --> 02:02:08,079 kinds of errors or warnings you can get 2744 02:02:05,520 --> 02:02:10,560 you can get compile time errors you can 2745 02:02:08,079 --> 02:02:13,118 get runtime errors and you can get 2746 02:02:10,560 --> 02:02:15,920 warnings but what is really meant by 2747 02:02:13,118 --> 02:02:19,519 these things remember the ultimate goal 2748 02:02:15,920 --> 02:02:22,158 of every developer or every c plus plus 2749 02:02:19,520 --> 02:02:25,679 developer in our case here is to be able 2750 02:02:22,158 --> 02:02:28,079 to generate a binary executable file 2751 02:02:25,679 --> 02:02:30,319 from the c plus plus code and we have to 2752 02:02:28,079 --> 02:02:32,399 run the code through the compiler to be 2753 02:02:30,319 --> 02:02:34,639 able to do that and one thing you should 2754 02:02:32,399 --> 02:02:36,158 know is that there is a set of 2755 02:02:36,158 --> 02:02:40,879 that the compiler enforces on our code 2756 02:02:40,880 --> 02:02:45,520 successfully and those requirements are 2757 02:02:43,198 --> 02:02:47,598 the rules really that we have to follow 2758 02:02:45,520 --> 02:02:49,280 in our c plus plus program and if we 2759 02:02:47,599 --> 02:02:51,360 break those rules we're going to get 2760 02:02:49,279 --> 02:02:54,000 problems and these problems are going to 2761 02:02:51,359 --> 02:02:56,639 come in the form of compile time errors 2762 02:02:56,639 --> 02:03:00,480 let's look at compile time errors in 2763 02:03:00,479 --> 02:03:05,598 here we have a simple c plus plus 2764 02:03:02,399 --> 02:03:08,238 program that has a few problems we have 2765 02:03:05,599 --> 02:03:12,239 forgotten to put the semicolon at the 2766 02:03:08,238 --> 02:03:14,319 end of our stdc out statement here and 2767 02:03:12,238 --> 02:03:15,598 the compiler is going to freak out 2768 02:03:15,599 --> 02:03:20,239 if we compile this program we're going 2769 02:03:17,439 --> 02:03:22,719 to get a bunch of errors and the ide is 2770 02:03:20,238 --> 02:03:24,319 going to show us this problem some ides 2771 02:03:22,719 --> 02:03:27,118 are going to give you different errors 2772 02:03:24,319 --> 02:03:28,799 for example here i was using codelite 2773 02:03:28,800 --> 02:03:33,760 error expected semicolon before return 2774 02:03:31,840 --> 02:03:36,560 or something like this so this is a 2775 02:03:33,760 --> 02:03:38,560 compiler error and if you have this kind 2776 02:03:36,560 --> 02:03:41,199 of errors your program is not going to 2777 02:03:38,560 --> 02:03:42,960 compile compilation is going to fail 2778 02:03:41,198 --> 02:03:44,559 you're going to get this error and 2779 02:03:42,960 --> 02:03:46,719 you're not going to get your binary 2780 02:03:44,560 --> 02:03:48,719 executable and when you have this you 2781 02:03:46,719 --> 02:03:51,760 basically have to go back and fix this 2782 02:03:48,719 --> 02:03:53,760 problem compile again and when you get 2783 02:03:51,760 --> 02:03:56,079 rid of these problems the compilation is 2784 02:03:53,760 --> 02:03:58,800 going to succeed this is what a compile 2785 02:03:56,078 --> 02:04:01,279 time error is another thing you can have 2786 02:03:58,800 --> 02:04:03,679 is the run time error this is not going 2787 02:04:01,279 --> 02:04:06,078 to cause a compile time error the 2788 02:04:03,679 --> 02:04:08,480 compilation is going to succeed but when 2789 02:04:06,078 --> 02:04:11,198 you run the program it's not going to do 2790 02:04:08,479 --> 02:04:13,118 what you intended it to do in the first 2791 02:04:11,198 --> 02:04:16,158 place for example you may want it to 2792 02:04:13,118 --> 02:04:18,479 print green on some user interface it's 2793 02:04:16,158 --> 02:04:20,559 going to print gray or whatever it's 2794 02:04:18,479 --> 02:04:22,399 really not going to do what you want so 2795 02:04:20,560 --> 02:04:24,400 it's a logical error that you have in 2796 02:04:22,399 --> 02:04:26,879 your program and you have to fix it to 2797 02:04:24,399 --> 02:04:29,198 get rid of this and another thing you 2798 02:04:26,880 --> 02:04:32,480 should know is that sometimes these run 2799 02:04:29,198 --> 02:04:35,118 time errors can cause your program to 2800 02:04:32,479 --> 02:04:37,198 fail and terminate immediately and we 2801 02:04:35,118 --> 02:04:38,880 say that the program has crashed we're 2802 02:04:37,198 --> 02:04:41,118 going to see an example of this in a 2803 02:04:38,880 --> 02:04:44,480 minute the last kind of problem you can 2804 02:04:41,118 --> 02:04:46,880 have in your program is a warning 2805 02:04:44,479 --> 02:04:49,598 this is a problem that is not serious 2806 02:04:46,880 --> 02:04:51,840 enough for the compiler to halt your 2807 02:04:49,599 --> 02:04:53,840 compilation compilation is going to 2808 02:04:51,840 --> 02:04:55,840 succeed but this is basically the 2809 02:04:53,840 --> 02:04:58,480 compiler telling you you are doing 2810 02:04:55,840 --> 02:05:00,719 something that has some problems and you 2811 02:04:58,479 --> 02:05:02,718 should really fix it before it becomes a 2812 02:05:00,719 --> 02:05:05,039 serious problem so that's going to be a 2813 02:05:02,719 --> 02:05:07,840 warning for example here you see some 2814 02:05:05,039 --> 02:05:10,078 compilers can warn you when you divide 2815 02:05:07,840 --> 02:05:12,000 stuff by zero and we're going to see how 2816 02:05:10,078 --> 02:05:14,319 this works the main message in this 2817 02:05:12,000 --> 02:05:16,960 lecture is that you may have problems in 2818 02:05:14,319 --> 02:05:19,359 your program and they are going to come 2819 02:05:16,960 --> 02:05:22,000 in three forms some are going to be 2820 02:05:19,359 --> 02:05:24,479 compile time errors some are going to be 2821 02:05:22,000 --> 02:05:26,639 run time errors some are going to be 2822 02:05:24,479 --> 02:05:28,879 warnings we are going to hop over to 2823 02:05:26,639 --> 02:05:31,840 visual studio code and actually play 2824 02:05:28,880 --> 02:05:34,159 with this okay here i am in visual 2825 02:05:31,840 --> 02:05:36,000 studio code the first thing i'm going to 2826 02:05:36,000 --> 02:05:40,319 the template project we're going to be 2827 02:05:37,760 --> 02:05:42,480 using in our project here the project is 2828 02:05:40,319 --> 02:05:44,880 going to leave in this folder errors and 2829 02:05:42,479 --> 02:05:47,439 warnings i am going to copy over the 2830 02:05:44,880 --> 02:05:48,880 files and i am going to go in the 2831 02:05:47,439 --> 02:05:51,439 current project we're going to be 2832 02:05:48,880 --> 02:05:53,840 working on and i am going to open this 2833 02:05:51,439 --> 02:05:56,399 folder in visual studio code i am going 2834 02:05:53,840 --> 02:05:58,800 to go to file close folder i am going to 2835 02:05:56,399 --> 02:06:01,118 close this one and i am going to open 2836 02:05:58,800 --> 02:06:03,760 our new project i am going to browse to 2837 02:06:01,118 --> 02:06:06,238 it the project is this one i am going to 2838 02:06:03,760 --> 02:06:08,480 select it and select folder and it is 2839 02:06:06,238 --> 02:06:10,718 going to open up in visual studio code i 2840 02:06:08,479 --> 02:06:13,439 am going to close this welcome screen 2841 02:06:10,719 --> 02:06:15,840 and open our main cpp file we are going 2842 02:06:13,439 --> 02:06:18,158 to get rid of this constable fan this is 2843 02:06:18,158 --> 02:06:22,638 thing we had and we don't really need it 2844 02:06:20,238 --> 02:06:25,198 now we're going to get there in due time 2845 02:06:22,639 --> 02:06:28,000 what we want to see is compile time 2846 02:06:25,198 --> 02:06:31,039 errors we're going to put in our stdc 2847 02:06:33,198 --> 02:06:37,519 we're going to end this stdndm and we're 2848 02:06:37,520 --> 02:06:42,560 remove the semicolon on purpose and if 2849 02:06:40,399 --> 02:06:44,638 we try to compile our program like this 2850 02:06:42,560 --> 02:06:47,039 we're going to get a compile time error 2851 02:06:44,639 --> 02:06:49,440 this is what we want to see so let's try 2852 02:06:47,039 --> 02:06:52,880 and build our program we're going to run 2853 02:06:49,439 --> 02:06:55,279 the task to build with gcc and you see 2854 02:06:52,880 --> 02:06:57,760 that the world fails we're going to get 2855 02:06:55,279 --> 02:07:00,559 a bunch of errors in our terminal it's 2856 02:06:57,760 --> 02:07:03,840 going to say in function it main 2857 02:07:00,560 --> 02:07:05,920 expected a semicolon before return it's 2858 02:07:03,840 --> 02:07:08,000 going to try its best to tell you what 2859 02:07:05,920 --> 02:07:10,639 the problem is but sometimes these 2860 02:07:08,000 --> 02:07:12,719 errors can be really confusing in this 2861 02:07:10,639 --> 02:07:15,440 case it is really easy because we 2862 02:07:12,719 --> 02:07:17,359 introduced this error on purpose so 2863 02:07:15,439 --> 02:07:19,198 we're going to fix it in a minute but 2864 02:07:17,359 --> 02:07:21,519 before we do that i want you to see 2865 02:07:19,198 --> 02:07:23,439 these problems tab it is where the 2866 02:07:21,520 --> 02:07:25,440 problems are going to show up in visual 2867 02:07:23,439 --> 02:07:28,479 studio code so you should see your 2868 02:07:25,439 --> 02:07:30,638 problems in here or through the terminal 2869 02:07:28,479 --> 02:07:32,959 here okay one thing you should know is 2870 02:07:30,639 --> 02:07:34,960 that when you have a compiler arrow 2871 02:07:32,960 --> 02:07:36,719 compilation is not going to succeed and 2872 02:07:34,960 --> 02:07:39,039 you're not going to have your binary 2873 02:07:36,719 --> 02:07:41,599 file which is what you really want so to 2874 02:07:39,039 --> 02:07:43,840 fix this problem we're going to put in 2875 02:07:41,599 --> 02:07:46,078 our semicolon here we're going to build 2876 02:07:43,840 --> 02:07:49,119 again we're going to go to terminal run 2877 02:07:46,078 --> 02:07:50,559 task we're going to build with gcc and 2878 02:07:49,118 --> 02:07:53,039 this time you're going to see that 2879 02:07:50,560 --> 02:07:55,360 compilation is going to succeed and we 2880 02:07:53,039 --> 02:07:57,118 have our program here we can run it 2881 02:07:55,359 --> 02:07:58,799 through the terminal here so we're going 2882 02:07:57,118 --> 02:08:01,679 to click the terminal is going to go 2883 02:08:01,679 --> 02:08:06,800 and if we do dur we're going to see our 2884 02:08:04,399 --> 02:08:09,598 files in here and we can run our program 2885 02:08:06,800 --> 02:08:12,239 we can say rooster and if we run we're 2886 02:08:09,599 --> 02:08:14,960 going to have hello world so i hope this 2887 02:08:12,238 --> 02:08:18,479 really makes it very clear what a 2888 02:08:14,960 --> 02:08:20,800 compile time error as so this is one 2889 02:08:18,479 --> 02:08:23,759 kind of error you can get the other one 2890 02:08:20,800 --> 02:08:26,239 is around time error and this comes in 2891 02:08:23,760 --> 02:08:28,880 the form of things that don't work as 2892 02:08:26,238 --> 02:08:31,198 you really expected so let's say we are 2893 02:08:28,880 --> 02:08:32,319 trying to do some computation in our 2894 02:08:33,118 --> 02:08:39,839 try to take 7 and divide this by 0 for 2895 02:08:37,279 --> 02:08:42,319 example and this is a statement there is 2896 02:08:39,840 --> 02:08:44,719 a load in c plus plus so let's try and 2897 02:08:42,319 --> 02:08:47,519 build this and see what we get because 2898 02:08:44,719 --> 02:08:49,840 this is a common scenario where we have 2899 02:08:47,520 --> 02:08:53,280 run time errors we're going to run the 2900 02:08:49,840 --> 02:08:55,599 task to build with gcc okay if we do 2901 02:08:53,279 --> 02:08:58,399 this you see that world finished with 2902 02:08:55,599 --> 02:09:01,440 warnings we didn't get a compile time 2903 02:08:58,399 --> 02:09:03,679 arrow and our build was successful so to 2904 02:09:01,439 --> 02:09:06,399 really prove this i'm going to 2905 02:09:03,679 --> 02:09:08,480 go back in our directory i'm going to do 2906 02:09:06,399 --> 02:09:10,719 dur you're going to see that rooster is 2907 02:09:08,479 --> 02:09:12,118 there i'm going to remove it i'm going 2908 02:09:12,118 --> 02:09:17,519 rooster.exe this is how we remove stuff 2909 02:09:14,960 --> 02:09:19,520 from the command line when i do this and 2910 02:09:19,520 --> 02:09:24,480 executable file should be gone so let's 2911 02:09:21,840 --> 02:09:25,840 try and build with this statement and 2912 02:09:24,479 --> 02:09:28,959 and you're going to see that we get a 2913 02:09:25,840 --> 02:09:30,719 warning and not a compile time error 2914 02:09:28,960 --> 02:09:33,279 like we had here so we're going to go to 2915 02:09:30,719 --> 02:09:35,359 terminal run task we're going to build 2916 02:09:33,279 --> 02:09:37,679 with gcc and you're going to see that 2917 02:09:35,359 --> 02:09:39,839 we're going to have our executable here 2918 02:09:37,679 --> 02:09:42,960 but we had a warning and it's saying 2919 02:09:39,840 --> 02:09:45,920 that division by 0 was detected in our 2920 02:09:42,960 --> 02:09:48,719 program and it is a recipe for disaster 2921 02:09:45,920 --> 02:09:51,760 so why is it a disaster let's try and 2922 02:09:48,719 --> 02:09:54,480 run this program so we're going to 2923 02:09:51,760 --> 02:09:56,800 dirt to prove it's here and we're going 2924 02:09:54,479 --> 02:09:59,439 to run it if we run it it's going to say 2925 02:09:56,800 --> 02:10:02,400 hello world and it's going to end let's 2926 02:09:59,439 --> 02:10:04,719 try and actually use our own terminal to 2927 02:10:02,399 --> 02:10:06,960 see if we have a better chance of seeing 2928 02:10:04,719 --> 02:10:10,319 the problem here so i'm going to reveal 2929 02:10:06,960 --> 02:10:12,800 in explorer by clicking on main cpp and 2930 02:10:10,319 --> 02:10:15,198 i am going to try and open a terminal 2931 02:10:12,800 --> 02:10:17,360 window here powershell that we open 2932 02:10:15,198 --> 02:10:20,719 ourselves we've done this before 2933 02:10:17,359 --> 02:10:23,198 this shouldn't be new to you by now 2934 02:10:20,719 --> 02:10:24,560 so we're going to do dirt in here 2935 02:10:23,198 --> 02:10:27,519 and we're going to try and run the 2936 02:10:24,560 --> 02:10:30,480 program and it's going to run so so it's 2937 02:10:27,520 --> 02:10:33,360 not really giving us a run time error 2938 02:10:30,479 --> 02:10:35,118 here because we're not capturing this or 2939 02:10:33,359 --> 02:10:36,639 trying to use the result of this 2940 02:10:35,118 --> 02:10:38,960 division here we're just doing the 2941 02:10:36,639 --> 02:10:40,800 division which is a problem so let's try 2942 02:10:38,960 --> 02:10:43,118 and capture this we haven't learned 2943 02:10:40,800 --> 02:10:46,239 about variables yet but what we're going 2944 02:10:43,118 --> 02:10:48,799 to do now is do end value and we're 2945 02:10:46,238 --> 02:10:51,359 going to assign the result of this 2946 02:10:48,800 --> 02:10:54,079 division to this value and let's try 2947 02:10:51,359 --> 02:10:56,880 printing the result of dividing a number 2948 02:10:54,078 --> 02:10:59,118 by zero so we're going to do stdc out 2949 02:10:56,880 --> 02:11:02,078 and we're going to say value and this is 2950 02:10:59,118 --> 02:11:04,238 how you can print a value out on the 2951 02:11:02,078 --> 02:11:06,399 console okay now that we have this and 2952 02:11:04,238 --> 02:11:09,039 let's try and weld we're going to run 2953 02:11:06,399 --> 02:11:11,359 tasks and worldwide gcc we're going to 2954 02:11:09,039 --> 02:11:13,599 get the same warning but if we try to 2955 02:11:11,359 --> 02:11:17,039 run the program we're not going to get 2956 02:11:13,599 --> 02:11:19,440 what we expect let's run rooster exe we 2957 02:11:17,039 --> 02:11:21,439 expect it to say hello world and printed 2958 02:11:19,439 --> 02:11:23,359 the value but you see that it's really 2959 02:11:21,439 --> 02:11:26,238 struggling to print the value and it 2960 02:11:23,359 --> 02:11:29,679 gave up and ended immediately so this is 2961 02:11:26,238 --> 02:11:31,598 one example of a runtime error that you 2962 02:11:29,679 --> 02:11:33,279 can face in your program there are many 2963 02:11:31,599 --> 02:11:35,039 other kinds that we're going to see as 2964 02:11:35,039 --> 02:11:39,760 here we are just trying to raise your 2965 02:11:37,439 --> 02:11:41,759 awareness on these problems so that you 2966 02:11:39,760 --> 02:11:44,000 can know how to fix them when they come 2967 02:11:41,760 --> 02:11:46,880 up okay so this is all i can share with 2968 02:11:44,000 --> 02:11:48,560 you now about runtime errors we also had 2969 02:11:46,880 --> 02:11:50,800 a chance to see that this is also going 2970 02:11:48,560 --> 02:11:52,880 to give you a warning and a visual 2971 02:11:50,800 --> 02:11:55,520 studio code here is also helping out 2972 02:11:52,880 --> 02:11:58,319 telling us that we may have a problem 2973 02:11:55,520 --> 02:11:59,920 it's turning the main cpp file yellow 2974 02:11:58,319 --> 02:12:02,158 and it's saying that there is one 2975 02:11:59,920 --> 02:12:04,000 problem in this file and if you go here 2976 02:12:02,158 --> 02:12:06,319 you're going to see that we have a 2977 02:12:04,000 --> 02:12:08,158 warning here okay so this is one way you 2978 02:12:06,319 --> 02:12:09,599 have to see this so this is not 2979 02:12:08,158 --> 02:12:12,000 recommended to do so we're going to 2980 02:12:09,599 --> 02:12:14,719 comment this out okay this is really all 2981 02:12:12,000 --> 02:12:17,198 i had to share in this lecture we had a 2982 02:12:14,719 --> 02:12:19,039 chance to look at compile time errors we 2983 02:12:17,198 --> 02:12:21,919 also had a chance to look at warnings 2984 02:12:19,039 --> 02:12:24,078 and run time errors using this example 2985 02:12:21,920 --> 02:12:26,158 if you get a warning it is a message the 2986 02:12:24,078 --> 02:12:29,118 compiler is giving you that you should 2987 02:12:26,158 --> 02:12:32,000 really look it up and fix the problem 2988 02:12:29,118 --> 02:12:34,000 before it becomes more serious compile 2989 02:12:32,000 --> 02:12:36,238 time errors are going to cause your 2990 02:12:34,000 --> 02:12:38,078 compilation to fail and that's a really 2991 02:12:36,238 --> 02:12:41,279 good thing because the compiler is going 2992 02:12:38,078 --> 02:12:43,599 to stop you from generating a binary 2993 02:12:41,279 --> 02:12:45,759 that doesn't really do what you expect 2994 02:12:43,599 --> 02:12:47,840 it to do so that's a good thing but 2995 02:12:45,760 --> 02:12:50,239 sometimes the compiler want to give you 2996 02:12:47,840 --> 02:12:52,400 a compile arrow and you have a chance to 2997 02:12:50,238 --> 02:12:54,559 run into a runtime error and that's 2998 02:12:52,399 --> 02:12:57,118 really bad so be careful about these 2999 02:12:54,560 --> 02:12:58,800 things and when you have problems visual 3000 02:12:57,118 --> 02:13:01,519 studio code is going to show you the 3001 02:12:58,800 --> 02:13:03,360 problem in terminal or in this problems 3002 02:13:01,520 --> 02:13:05,199 tab and you're going to be able to fix 3003 02:13:03,359 --> 02:13:06,799 it up we're going to stop here in this 3004 02:13:05,198 --> 02:13:09,359 lecture in the next one we're going to 3005 02:13:06,800 --> 02:13:11,520 learn a little more about statements and 3006 02:13:09,359 --> 02:13:13,519 functions go ahead and finish up here 3007 02:13:11,520 --> 02:13:15,679 and meet me there in this lecture we're 3008 02:13:13,520 --> 02:13:18,560 going to learn about a little more about 3009 02:13:15,679 --> 02:13:21,118 statements and functions in c plus plus 3010 02:13:21,118 --> 02:13:26,880 computation in c plus plus you can say 3011 02:13:23,760 --> 02:13:30,000 that it is the smallest thing your cpu 3012 02:13:26,880 --> 02:13:32,000 can execute in your program every c plus 3013 02:13:30,000 --> 02:13:33,760 plus program is a collection of 3014 02:13:32,000 --> 02:13:37,039 statements so you're going to have a 3015 02:13:33,760 --> 02:13:39,360 bunch of statements that are organized 3016 02:13:37,039 --> 02:13:42,158 in a certain way to achieve whatever it 3017 02:13:39,359 --> 02:13:44,960 is you want and statements in c plus 3018 02:13:42,158 --> 02:13:47,439 plus must end with the semicolon we saw 3019 02:13:44,960 --> 02:13:49,840 in the last lecture that not putting 3020 02:13:47,439 --> 02:13:53,039 that semicolon is going to result in a 3021 02:13:49,840 --> 02:13:55,360 compile time error here is a basic 3022 02:13:53,039 --> 02:13:58,479 sample c plus plus program that we're 3023 02:13:55,359 --> 02:14:00,639 going to use to learn about statements 3024 02:13:58,479 --> 02:14:03,359 in the main function here we have two 3025 02:14:00,639 --> 02:14:05,359 variables declared we haven't really 3026 02:14:03,359 --> 02:14:08,238 learned about variables but they are a 3027 02:14:05,359 --> 02:14:11,359 way to store data in your program and 3028 02:14:08,238 --> 02:14:13,839 here we are storing two decimal numbers 3029 02:14:11,359 --> 02:14:16,078 one is called first number the second is 3030 02:14:13,840 --> 02:14:18,159 called second number we can do something 3031 02:14:16,078 --> 02:14:20,559 with these variables that we have in our 3032 02:14:18,158 --> 02:14:23,279 program for example we can add them to 3033 02:14:20,560 --> 02:14:25,599 have another variable in our program and 3034 02:14:23,279 --> 02:14:28,880 we can print that so you see here we are 3035 02:14:25,599 --> 02:14:31,920 saying stdc out the sum of two numbers 3036 02:14:28,880 --> 02:14:34,960 is sum so this is going to print 21 3037 02:14:31,920 --> 02:14:36,480 because it's going to add 12 and 9 and 3038 02:14:34,960 --> 02:14:39,118 the result of that is going to be 3039 02:14:36,479 --> 02:14:41,359 displayed on the terminal if we run this 3040 02:14:39,118 --> 02:14:44,319 program what i really want you to notice 3041 02:14:41,359 --> 02:14:47,839 is that this and first the number equals 3042 02:14:44,319 --> 02:14:50,479 12 thing is a statement in c plus plus 3043 02:14:47,840 --> 02:14:52,719 so it must end with a semicolon the line 3044 02:14:50,479 --> 02:14:55,279 here with second number is also a 3045 02:14:52,719 --> 02:14:58,319 statement so it must end with a 3046 02:14:55,279 --> 02:14:59,920 semicolon every single line we have here 3047 02:14:58,319 --> 02:15:02,639 in the main function is really a 3048 02:14:59,920 --> 02:15:05,039 statement and it must end with a 3049 02:15:02,639 --> 02:15:07,118 semicolon if we don't put the semicolon 3050 02:15:05,039 --> 02:15:09,039 in place we're going to get a compiler 3051 02:15:07,118 --> 02:15:11,519 error okay one other thing that is 3052 02:15:09,039 --> 02:15:14,238 really important to keep in mind is that 3053 02:15:11,520 --> 02:15:17,280 statements are executed in order in your 3054 02:15:14,238 --> 02:15:19,519 program and the order is from top to 3055 02:15:17,279 --> 02:15:21,840 bottom if you go back to our program 3056 02:15:19,520 --> 02:15:24,079 here so the first statement is the one 3057 02:15:21,840 --> 02:15:26,400 with first number here 12 is going to be 3058 02:15:24,078 --> 02:15:29,279 installed in memory when this statement 3059 02:15:26,399 --> 02:15:31,359 is executed by your computer and we're 3060 02:15:29,279 --> 02:15:33,279 going to go to the second statement with 3061 02:15:31,359 --> 02:15:35,759 second number and we're going to keep 3062 02:15:33,279 --> 02:15:38,078 going until we hit the end of the main 3063 02:15:35,760 --> 02:15:40,960 function and the return statement is 3064 02:15:38,078 --> 02:15:43,439 going to execute and when the main 3065 02:15:40,960 --> 02:15:45,920 function returns or terminates we're 3066 02:15:43,439 --> 02:15:47,919 going to get the return value sent to 3067 02:15:45,920 --> 02:15:50,800 your operating system and it's going to 3068 02:15:47,920 --> 02:15:52,800 know if your program ended successfully 3069 02:15:50,800 --> 02:15:54,880 or with a problem and the operating 3070 02:15:52,800 --> 02:15:57,199 system is going to know what to do with 3071 02:15:54,880 --> 02:15:59,920 that information execution keeps going 3072 02:15:57,198 --> 02:16:02,078 until we hit the end of the program or 3073 02:15:59,920 --> 02:16:04,000 if there is any other condition causing 3074 02:16:02,078 --> 02:16:05,920 for the program to terminate and we're 3075 02:16:04,000 --> 02:16:08,078 going to have more details about these 3076 02:16:05,920 --> 02:16:10,880 things later i just want you to know 3077 02:16:08,078 --> 02:16:12,960 that the statements are executed in 3078 02:16:10,880 --> 02:16:14,960 order in your main function another 3079 02:16:12,960 --> 02:16:18,239 concept i really want you to have an 3080 02:16:14,960 --> 02:16:20,239 idea about is the concept of functions a 3081 02:16:18,238 --> 02:16:22,319 function is really like a machine you 3082 02:16:20,238 --> 02:16:25,279 give it input and it's going to give you 3083 02:16:22,319 --> 02:16:27,599 output if we look at the operation here 3084 02:16:25,279 --> 02:16:30,479 to add first a number to second number 3085 02:16:27,599 --> 02:16:33,439 we can consider first number and second 3086 02:16:30,479 --> 02:16:35,359 number as input to the function and sum 3087 02:16:33,439 --> 02:16:37,599 is the output of the function we can 3088 02:16:35,359 --> 02:16:39,679 visualize that like this so we have a 3089 02:16:37,599 --> 02:16:42,079 first number and the second number we 3090 02:16:39,679 --> 02:16:44,000 pass that into our machine which is 3091 02:16:42,079 --> 02:16:46,879 really a function and we're going to get 3092 02:16:44,000 --> 02:16:49,519 the result of this function in a return 3093 02:16:46,879 --> 02:16:52,398 value or something that we get out of 3094 02:16:49,519 --> 02:16:55,280 the function as our result c plus plus 3095 02:16:52,398 --> 02:16:57,840 has a special syntax we use to define 3096 02:16:55,280 --> 02:17:00,559 functions this is our function to add 3097 02:16:57,840 --> 02:17:03,679 numbers on the left we have an integer 3098 02:17:00,558 --> 02:17:05,518 which is code name for a whole number in 3099 02:17:03,679 --> 02:17:07,840 c plus plus it's a number without 3100 02:17:05,519 --> 02:17:09,200 decimal points and it's a whole number 3101 02:17:07,840 --> 02:17:11,200 after that we have the name of the 3102 02:17:09,200 --> 02:17:13,599 function and we can name it whatever we 3103 02:17:11,200 --> 02:17:16,079 want and after that we're going to have 3104 02:17:13,599 --> 02:17:17,760 the list of parameters that we can pass 3105 02:17:16,079 --> 02:17:20,398 to the function and this is going to 3106 02:17:17,760 --> 02:17:22,719 work as the input to the function 3107 02:17:20,398 --> 02:17:26,239 after you do that you're going to have a 3108 02:17:22,718 --> 02:17:28,478 pair of curly braces the one is here the 3109 02:17:26,239 --> 02:17:30,799 other is here to the end and within 3110 02:17:28,478 --> 02:17:33,039 those curly braces it's going to be the 3111 02:17:30,799 --> 02:17:35,438 body of the function inside the function 3112 02:17:33,040 --> 02:17:37,120 you see that we are declaring a variable 3113 02:17:37,120 --> 02:17:41,519 and we are storing the sum of first 3114 02:17:39,679 --> 02:17:42,478 number and second number in this 3115 02:17:42,478 --> 02:17:47,679 and we are making this function return 3116 02:17:45,519 --> 02:17:50,000 the sum as the result and we're going to 3117 02:17:47,679 --> 02:17:52,000 be able to capture it if we call this 3118 02:17:50,000 --> 02:17:53,679 function in a function like me you're 3119 02:17:52,000 --> 02:17:56,079 going to see how this works in a minute 3120 02:17:53,679 --> 02:17:58,638 a function must be defined before it's 3121 02:17:56,079 --> 02:18:01,359 used so you can't really call a function 3122 02:17:58,638 --> 02:18:04,239 you haven't defined first in your c plus 3123 02:18:01,359 --> 02:18:06,639 plus program and here is a slightly 3124 02:18:04,239 --> 02:18:09,439 complete program to really put all these 3125 02:18:06,638 --> 02:18:11,920 ideas together we have our function to 3126 02:18:09,439 --> 02:18:14,079 add numbers it takes two numbers first 3127 02:18:11,920 --> 02:18:16,478 number and second number it's going to 3128 02:18:14,079 --> 02:18:19,280 add them up and store the results and 3129 02:18:16,478 --> 02:18:21,840 we're going to return some to be used by 3130 02:18:19,280 --> 02:18:24,719 whoever called this function and if you 3131 02:18:21,840 --> 02:18:27,280 look in our main program we have a 3132 02:18:24,718 --> 02:18:30,478 statement here we're going to store the 3133 02:18:27,280 --> 02:18:33,679 result of add numbers in the sum 3134 02:18:30,478 --> 02:18:35,519 variable and we pass first number and 3135 02:18:33,679 --> 02:18:37,599 second number in the function and we're 3136 02:18:35,519 --> 02:18:39,920 going to have that result stored in here 3137 02:18:37,599 --> 02:18:42,558 here we also call the function with 3138 02:18:39,920 --> 02:18:44,638 direct numbers you see that we pass in 3139 02:18:42,558 --> 02:18:46,718 34 and the seven and we're going to 3140 02:18:44,638 --> 02:18:48,718 print out the result here this is the 3141 02:18:46,718 --> 02:18:51,518 basic idea i want you to have about 3142 02:18:48,718 --> 02:18:53,919 functions they are reusable pieces of 3143 02:18:51,519 --> 02:18:55,840 code that group together a bunch of 3144 02:18:53,920 --> 02:18:58,159 statements to do whatever it is we 3145 02:18:55,840 --> 02:19:00,239 wanted to do in that function one 3146 02:18:58,159 --> 02:19:02,959 benefit about functions is that we can 3147 02:19:00,239 --> 02:19:05,519 reuse the code here if you look here we 3148 02:19:02,959 --> 02:19:07,678 are calling this function multiple times 3149 02:19:05,519 --> 02:19:10,159 without really rewriting these 3150 02:19:07,679 --> 02:19:12,000 statements that are inside the function 3151 02:19:10,159 --> 02:19:14,318 i know some of these concepts might be 3152 02:19:12,000 --> 02:19:17,200 cryptic to you right now we are going to 3153 02:19:14,318 --> 02:19:18,959 go to visual studio code and try this in 3154 02:19:17,200 --> 02:19:21,599 code and you're going to see exactly how 3155 02:19:18,959 --> 02:19:24,079 this works okay here we are in our 3156 02:19:21,599 --> 02:19:26,318 folder where we are storing our projects 3157 02:19:24,079 --> 02:19:28,718 i am going to grab the code from the 3158 02:19:26,318 --> 02:19:30,959 template project i am going to put that 3159 02:19:28,718 --> 02:19:33,438 in the current project which is 3160 02:19:30,959 --> 02:19:35,839 statements and functions i'm going to 3161 02:19:33,439 --> 02:19:38,479 paste that in here and i am going to 3162 02:19:35,840 --> 02:19:40,398 open this up in visual studio code i am 3163 02:19:38,478 --> 02:19:43,119 going to close the project we have right 3164 02:19:40,398 --> 02:19:46,079 now and i am going to open our new 3165 02:19:43,120 --> 02:19:48,160 project let's browse to its location i 3166 02:19:46,079 --> 02:19:50,719 am going to open the project on 3167 02:19:48,159 --> 02:19:52,879 statements and functions let's open this 3168 02:19:50,719 --> 02:19:55,358 folder in visual studio code and we're 3169 02:19:52,879 --> 02:19:57,039 going to have our starter project here i 3170 02:19:55,359 --> 02:19:59,040 am going to take out whatever we have in 3171 02:19:57,040 --> 02:20:01,120 the main function and take out this 3172 02:19:59,040 --> 02:20:03,359 contival thing we don't really need it 3173 02:20:01,120 --> 02:20:05,920 and we are going to try and define two 3174 02:20:03,359 --> 02:20:08,239 variables using two statements we are 3175 02:20:05,920 --> 02:20:10,960 going to say and first number and we're 3176 02:20:08,239 --> 02:20:13,199 going to put in a value let's say three 3177 02:20:10,959 --> 02:20:15,919 and we have to remember to end this with 3178 02:20:13,200 --> 02:20:18,240 a semicolon so this makes our statement 3179 02:20:15,920 --> 02:20:20,239 we can encode this in a comment to make 3180 02:20:18,239 --> 02:20:22,799 this pretty clear we're going to also 3181 02:20:20,239 --> 02:20:24,559 define a second variable so we're going 3182 02:20:24,559 --> 02:20:30,079 mean that it is a whole number it 3183 02:20:26,959 --> 02:20:32,318 doesn't have decimal points like 2.5 it 3184 02:20:30,079 --> 02:20:34,318 is a whole number like one or seven or 3185 02:20:32,318 --> 02:20:37,279 ten so we're going to say second number 3186 02:20:34,318 --> 02:20:39,439 we can give it a name and uh let's put 3187 02:20:37,280 --> 02:20:42,159 in a seven we're going to store these 3188 02:20:39,439 --> 02:20:44,239 values in our program and we can print 3189 02:20:42,159 --> 02:20:47,600 these values out let's print them out 3190 02:20:44,239 --> 02:20:49,840 first so we're going to say stdc out we 3191 02:20:47,600 --> 02:20:53,120 already know how to do that and we can 3192 02:20:49,840 --> 02:20:55,760 say first number and we can change these 3193 02:20:53,120 --> 02:20:58,800 output statements and say the first 3194 02:20:55,760 --> 02:21:02,239 number is first number you can do that 3195 02:20:58,799 --> 02:21:04,478 std endl and you can notice that visual 3196 02:21:02,239 --> 02:21:06,879 studio code is really helping out in 3197 02:21:04,478 --> 02:21:09,039 auto completing things we have in our 3198 02:21:06,879 --> 02:21:12,000 program let's print the second number to 3199 02:21:09,040 --> 02:21:13,520 really practice this so if we do std 3200 02:21:12,000 --> 02:21:15,439 you're going to see that it's going to 3201 02:21:13,520 --> 02:21:18,479 give us a bunch of options we can choose 3202 02:21:15,439 --> 02:21:20,800 from if you want you can scroll through 3203 02:21:18,478 --> 02:21:23,199 this with your mouse or you can just 3204 02:21:20,799 --> 02:21:25,840 type whatever you want to type so i am 3205 02:21:23,200 --> 02:21:28,000 going to do c out and it's going to try 3206 02:21:25,840 --> 02:21:30,398 and help out if i really want i can come 3207 02:21:28,000 --> 02:21:32,000 on c out here and click on it and it's 3208 02:21:30,398 --> 02:21:34,079 going to be auto completed which is 3209 02:21:32,000 --> 02:21:36,799 pretty cool so we're going to say second 3210 02:21:34,079 --> 02:21:38,879 number and we're going to say second 3211 02:21:36,799 --> 02:21:40,719 number here and it's going to auto 3212 02:21:40,719 --> 02:21:46,398 endl and we can try and compile this 3213 02:21:43,760 --> 02:21:49,359 program before we do i want you to guess 3214 02:21:46,398 --> 02:21:51,439 what we're going to get when we run this 3215 02:21:49,359 --> 02:21:53,840 program give it a guess we are then 3216 02:21:51,439 --> 02:21:56,318 going to go to terminal run task and 3217 02:21:53,840 --> 02:21:58,239 we're going to build this with gcc the 3218 02:21:56,318 --> 02:22:00,478 world is going to be successful we're 3219 02:21:58,239 --> 02:22:02,478 going to open this in our terminal the 3220 02:22:00,478 --> 02:22:04,639 terminal goes away no problem we're 3221 02:22:02,478 --> 02:22:08,159 going to open it again and we are going 3222 02:22:04,639 --> 02:22:09,959 to go in our folder and do the ir and 3223 02:22:08,159 --> 02:22:12,959 you're going to see that we have 3224 02:22:09,959 --> 02:22:15,599 rooster.exe our program we can run it 3225 02:22:12,959 --> 02:22:18,318 and it's going to say first number three 3226 02:22:15,600 --> 02:22:20,399 and second number s7 this is what we 3227 02:22:18,318 --> 02:22:22,478 have stored in these variables and the 3228 02:22:20,398 --> 02:22:25,039 benefit of storing your data in 3229 02:22:22,478 --> 02:22:27,199 variables is that you can change the 3230 02:22:25,040 --> 02:22:30,240 variables and let the rest of your 3231 02:22:27,200 --> 02:22:32,159 program really do the same things and 3232 02:22:30,239 --> 02:22:35,760 pick up the data that you have in those 3233 02:22:32,159 --> 02:22:37,200 variables for example we can put 13 in 3234 02:22:37,200 --> 02:22:41,439 and if we build again this number is 3235 02:22:39,200 --> 02:22:43,920 going to be picked up by this code and 3236 02:22:41,439 --> 02:22:46,960 we didn't need to go in and manually 3237 02:22:43,920 --> 02:22:49,600 change data in these sddc out statements 3238 02:22:46,959 --> 02:22:53,039 here let's weld so that we can see this 3239 02:22:49,600 --> 02:22:55,760 and we can close this by clicking and if 3240 02:22:53,040 --> 02:22:58,399 we run rooster again it's going to say 3241 02:22:55,760 --> 02:23:00,960 13 and 7. play with this a couple of 3242 02:22:58,398 --> 02:23:02,799 times until you really feel confident 3243 02:23:00,959 --> 02:23:04,799 and if you have any problem don't 3244 02:23:02,799 --> 02:23:06,799 hesitate to ask me i am going to do the 3245 02:23:06,799 --> 02:23:12,478 now that we have these numbers stored in 3246 02:23:09,120 --> 02:23:14,640 our program we can add them up let's say 3247 02:23:12,478 --> 02:23:16,959 and some we're going to declare another 3248 02:23:14,639 --> 02:23:20,000 variable and we're going to say it's 3249 02:23:16,959 --> 02:23:21,438 equal to first number plus second number 3250 02:23:20,000 --> 02:23:23,680 and we're going to end this with a 3251 02:23:21,439 --> 02:23:25,040 semicolon and we're going to print the 3252 02:23:25,040 --> 02:23:30,800 let's do sddc out and we're going to say 3253 02:23:28,239 --> 02:23:33,520 the sum is sum we're going to print our 3254 02:23:30,799 --> 02:23:37,199 variable here and we're going to print a 3255 02:23:33,520 --> 02:23:39,200 new line character with std endl we're 3256 02:23:37,200 --> 02:23:42,159 going to build our program so that our 3257 02:23:39,200 --> 02:23:44,399 changes are reflected in our binary here 3258 02:23:42,159 --> 02:23:47,359 so we're going to go to terminal 3259 02:23:44,398 --> 02:23:49,439 run task weld with gcc the build is 3260 02:23:47,359 --> 02:23:51,840 going to be successful we're going to 3261 02:23:49,439 --> 02:23:54,079 press any key we are going to click here 3262 02:23:51,840 --> 02:23:56,799 in the terminal and we're going to hit 3263 02:23:54,079 --> 02:23:58,959 enter and run rooster again and we 3264 02:23:56,799 --> 02:24:02,398 should see the sum and you see it says 3265 02:23:58,959 --> 02:24:05,199 the sum is 20 which is what we expect 3266 02:24:02,398 --> 02:24:07,519 now we are doing the sum here but it 3267 02:24:05,200 --> 02:24:10,479 would really be better if we're not 3268 02:24:07,520 --> 02:24:13,359 doing the sum in the main function here 3269 02:24:10,478 --> 02:24:15,679 what if we actually took this code and 3270 02:24:13,359 --> 02:24:18,239 wrapped it in a function that we could 3271 02:24:15,680 --> 02:24:20,398 reuse whatever we want we have seen how 3272 02:24:18,239 --> 02:24:22,079 we can declare a function so let's do 3273 02:24:20,398 --> 02:24:24,398 that we're going to follow the same 3274 02:24:22,079 --> 02:24:26,318 structure we saw in the slides earlier 3275 02:24:24,398 --> 02:24:29,279 so we're going to say and this is going 3276 02:24:26,318 --> 02:24:30,959 to be the return value or what we get 3277 02:24:29,280 --> 02:24:32,560 out of the function then we're going to 3278 02:24:30,959 --> 02:24:34,559 put in the name of the function we're 3279 02:24:32,559 --> 02:24:36,639 going to say add numbers and we're going 3280 02:24:34,559 --> 02:24:38,799 to pass in the parameters the parameters 3281 02:24:36,639 --> 02:24:40,639 can really have any name you want but 3282 02:24:40,639 --> 02:24:45,279 the type of the parameter first so the 3283 02:24:43,359 --> 02:24:48,960 parameters are also going to be whole 3284 02:24:45,280 --> 02:24:51,439 numbers like 1 5 7 or whatever you want 3285 02:24:48,959 --> 02:24:53,519 and we're going to say first program 3286 02:24:51,439 --> 02:24:55,600 and the second is going to also be a 3287 02:24:53,520 --> 02:24:56,960 whole number an integer and it's going 3288 02:24:56,959 --> 02:25:02,159 param okay after we do this we're going 3289 02:24:59,439 --> 02:25:04,960 to specify the body of the function 3290 02:25:02,159 --> 02:25:07,520 which is going to be between these curly 3291 02:25:04,959 --> 02:25:09,919 braces i am going to jump to the next 3292 02:25:07,520 --> 02:25:12,880 line so that this is really easy to see 3293 02:25:09,920 --> 02:25:14,879 so i am going to define our sum variable 3294 02:25:12,879 --> 02:25:17,679 inside the function it's going to be a 3295 02:25:14,879 --> 02:25:20,959 whole number and i can call it whatever 3296 02:25:17,680 --> 02:25:23,120 i want so let's call it result because 3297 02:25:20,959 --> 02:25:25,279 it's going to be the result of this 3298 02:25:23,120 --> 02:25:28,079 function and the result is going to be 3299 02:25:25,280 --> 02:25:30,239 the sum of first param and second param 3300 02:25:28,079 --> 02:25:32,398 okay i hope this makes sense and we're 3301 02:25:30,239 --> 02:25:35,199 going to end this with a semicolon 3302 02:25:32,398 --> 02:25:37,279 because every statement in c plus plus 3303 02:25:35,200 --> 02:25:38,399 has to end with the semicolon after we 3304 02:25:38,398 --> 02:25:43,039 we need to give it back to whoever 3305 02:25:40,879 --> 02:25:45,358 called this function and we're going to 3306 02:25:43,040 --> 02:25:47,600 do that with a return statement and 3307 02:25:45,359 --> 02:25:48,720 we're going to return the result to 3308 02:25:48,719 --> 02:25:52,318 might have called this function here now 3309 02:25:50,799 --> 02:25:54,478 that we have the function we can 3310 02:25:52,318 --> 02:25:56,879 actually use it we're going to go back 3311 02:25:54,478 --> 02:25:58,239 in our main function and what we're 3312 02:25:58,239 --> 02:26:04,239 is to reuse this sum variable and store 3313 02:26:01,680 --> 02:26:06,479 in whatever is returned from our 3314 02:26:04,239 --> 02:26:07,520 function the syntax for that is going to 3315 02:26:07,520 --> 02:26:12,079 and we're going to say equals and we're 3316 02:26:09,359 --> 02:26:14,318 going to say add numbers and we can pass 3317 02:26:12,079 --> 02:26:18,639 in whatever two numbers that we want to 3318 02:26:20,559 --> 02:26:27,199 we would expect this to give us 32 3319 02:26:23,760 --> 02:26:29,280 so if we print the sum again we can just 3320 02:26:29,280 --> 02:26:34,880 that prints the sum and reuse it here 3321 02:26:32,239 --> 02:26:37,199 let's align this a little bit and i want 3322 02:26:34,879 --> 02:26:39,438 you to take a guess at what we are going 3323 02:26:37,200 --> 02:26:41,359 to get from this program right now we 3324 02:26:39,439 --> 02:26:44,318 have these two numbers we are adding 3325 02:26:41,359 --> 02:26:46,239 them up if we get here this should print 3326 02:26:44,318 --> 02:26:50,239 the first number which is going to be a 3327 02:26:46,239 --> 02:26:52,398 13 so here we expect to get 13 and by 3328 02:26:50,239 --> 02:26:54,559 the time we hit this second number 3329 02:26:52,398 --> 02:26:57,279 statement we are going to print the 3330 02:26:54,559 --> 02:26:59,439 second number it's going to be 7. if we 3331 02:26:57,280 --> 02:27:02,479 get here we're going to print whatever 3332 02:26:59,439 --> 02:27:05,439 is stored in sum and in sum we have the 3333 02:27:02,478 --> 02:27:08,959 sum of first number and second number so 3334 02:27:05,439 --> 02:27:11,359 this should print 20 right and here we 3335 02:27:08,959 --> 02:27:14,478 are doing another sum of two numbers but 3336 02:27:11,359 --> 02:27:16,559 we are using a predefined function 3337 02:27:14,478 --> 02:27:19,119 that we have here before the main 3338 02:27:16,559 --> 02:27:21,199 function because you have to define the 3339 02:27:19,120 --> 02:27:24,319 function before you use it okay if we 3340 02:27:21,200 --> 02:27:27,200 add 25 and seven we are going to fall 3341 02:27:24,318 --> 02:27:28,959 into this function and it's going to add 3342 02:27:27,200 --> 02:27:30,399 those numbers up and we're going to get 3343 02:27:30,398 --> 02:27:34,318 and that's what we're going to return 3344 02:27:32,559 --> 02:27:36,478 and it is going to be saved in this 3345 02:27:34,318 --> 02:27:39,279 variable if we print it out it's going 3346 02:27:36,478 --> 02:27:42,799 to give us 32. i hope this makes sense 3347 02:27:39,280 --> 02:27:45,439 let's build and run our program run task 3348 02:27:42,799 --> 02:27:47,199 world with gcc the world is going to go 3349 02:27:45,439 --> 02:27:49,680 fine we're going to go back to our 3350 02:27:47,200 --> 02:27:52,800 terminal here that's clear so that 3351 02:27:49,680 --> 02:27:54,880 things don't disturb us and uh bring in 3352 02:27:52,799 --> 02:27:58,000 some confusion we're going to run our 3353 02:27:54,879 --> 02:28:00,079 rooster program and exactly what we have 3354 02:27:58,000 --> 02:28:03,120 here we have a 13 from the first 3355 02:28:00,079 --> 02:28:06,079 statement here we have a 7 from this 3356 02:28:03,120 --> 02:28:09,600 statement here we have a 20 from this 3357 02:28:06,079 --> 02:28:12,079 sum here which is here and we have a 32 3358 02:28:09,600 --> 02:28:13,760 that we got from this function the good 3359 02:28:12,079 --> 02:28:16,079 thing about functions is that we can 3360 02:28:13,760 --> 02:28:18,398 reuse them now that we know how to use 3361 02:28:16,079 --> 02:28:20,079 it we can use it again and we can say 3362 02:28:20,079 --> 02:28:26,478 add numbers and we can say 30 and 3363 02:28:24,159 --> 02:28:28,079 54 for example you add them up and 3364 02:28:26,478 --> 02:28:30,799 you're going to get the results so we're 3365 02:28:28,079 --> 02:28:33,520 going to print the result some and we're 3366 02:28:30,799 --> 02:28:35,519 going to put in the sum i really want 3367 02:28:33,520 --> 02:28:37,359 you to take some time and play with this 3368 02:28:35,520 --> 02:28:39,760 statement so that these things make 3369 02:28:37,359 --> 02:28:41,760 sense it's really not that difficult but 3370 02:28:39,760 --> 02:28:44,880 you just have to rock your brains around 3371 02:28:41,760 --> 02:28:47,200 how c plus plus defines these things so 3372 02:28:44,879 --> 02:28:51,759 we have this statement and we are adding 3373 02:28:47,200 --> 02:28:54,640 30 and 54 so it should get an 84 after 3374 02:28:51,760 --> 02:28:57,359 the 32 here let's weld we're going to 3375 02:28:54,639 --> 02:28:59,599 run the task to world with gcc we're 3376 02:28:57,359 --> 02:29:02,720 going to click on the terminal here and 3377 02:28:59,600 --> 02:29:04,399 hit enter let's clear so that we don't 3378 02:29:02,719 --> 02:29:07,358 have things disturbing us and we're 3379 02:29:04,398 --> 02:29:10,719 going to run our program and we have our 3380 02:29:07,359 --> 02:29:12,800 trusty 84 here i really hope this makes 3381 02:29:10,719 --> 02:29:15,679 sense and as a bonus i'm going to show 3382 02:29:12,799 --> 02:29:19,199 you that you can also call things 3383 02:29:15,680 --> 02:29:21,840 in the stdc out statement here without 3384 02:29:19,200 --> 02:29:24,079 having to store them in some predefined 3385 02:29:21,840 --> 02:29:27,920 variables and sometimes this comes in 3386 02:29:24,079 --> 02:29:31,200 handy so you can say std see out and 3387 02:29:27,920 --> 02:29:33,600 we're going to say sum and you might say 3388 02:29:31,200 --> 02:29:37,600 add numbers right in here and you're 3389 02:29:33,600 --> 02:29:40,800 going to say 3 for example and uh 42 and 3390 02:29:37,600 --> 02:29:43,200 this is going to give us a 45 that's 3391 02:29:40,799 --> 02:29:44,079 what we should expect to see after 84 3392 02:29:44,079 --> 02:29:49,039 so that's world and running the task to 3393 02:29:46,719 --> 02:29:51,760 build with gcc the world is going to be 3394 02:29:49,040 --> 02:29:54,080 successful we click on the terminal here 3395 02:29:51,760 --> 02:29:56,318 we hit enter so that we see our terminal 3396 02:29:54,079 --> 02:29:59,120 with output we're going to clear so that 3397 02:29:56,318 --> 02:30:01,600 we see our output without noise we're 3398 02:29:59,120 --> 02:30:04,240 going to run our program and we're going 3399 02:30:01,600 --> 02:30:06,000 to see a 45 here i really hope this 3400 02:30:04,239 --> 02:30:08,079 makes sense the whole point of this 3401 02:30:06,000 --> 02:30:10,559 lecture was to make you aware of 3402 02:30:08,079 --> 02:30:13,120 statements and function a statement is 3403 02:30:10,559 --> 02:30:16,079 the most basic unit in your c plus plus 3404 02:30:13,120 --> 02:30:19,200 program it basically lives on one line 3405 02:30:16,079 --> 02:30:20,959 but it can also spread across different 3406 02:30:19,200 --> 02:30:22,640 lines but we're not going to look at 3407 02:30:20,959 --> 02:30:25,119 that here maybe we're going to see that 3408 02:30:22,639 --> 02:30:27,760 later in the course statements are 3409 02:30:25,120 --> 02:30:30,240 executed in order so when our main 3410 02:30:27,760 --> 02:30:31,520 function gets to be run by the computer 3411 02:30:30,239 --> 02:30:33,439 it's going to go from the first 3412 02:30:31,520 --> 02:30:36,000 statement second statement third 3413 02:30:33,439 --> 02:30:39,040 statement and it's going to go until it 3414 02:30:36,000 --> 02:30:40,959 hits the end and we can see that here in 3415 02:30:39,040 --> 02:30:43,680 the order in which our numbers are 3416 02:30:40,959 --> 02:30:46,000 printed it is going from top to bottom 3417 02:30:43,680 --> 02:30:48,398 we have also seen that we can split our 3418 02:30:46,000 --> 02:30:50,159 code in different functions and wrap 3419 02:30:48,398 --> 02:30:52,799 whatever functionality we have in a 3420 02:30:50,159 --> 02:30:54,959 function we have seen the basic concepts 3421 02:30:52,799 --> 02:30:57,679 on functions a function has a return 3422 02:30:54,959 --> 02:31:00,079 value it has a name and you have to give 3423 02:30:57,680 --> 02:31:02,880 it the parameters and you put the body 3424 02:31:00,079 --> 02:31:05,760 of your function within these two curly 3425 02:31:02,879 --> 02:31:09,119 braces as an exercise i would challenge 3426 02:31:05,760 --> 02:31:11,680 you to do a function that multiplies two 3427 02:31:09,120 --> 02:31:14,000 numbers and call it in the main function 3428 02:31:11,680 --> 02:31:16,079 here to see the result if you have any 3429 02:31:14,000 --> 02:31:18,879 problem please do ask me i am going to 3430 02:31:16,079 --> 02:31:20,879 do the best i can to help you out this 3431 02:31:18,879 --> 02:31:22,559 covers all we set out to do in this 3432 02:31:20,879 --> 02:31:24,639 lecture and i hope you found it 3433 02:31:22,559 --> 02:31:26,318 interesting we're going to stop here in 3434 02:31:24,639 --> 02:31:29,279 this lecture in the next one we're going 3435 02:31:26,318 --> 02:31:32,239 to see how we can input and output 3436 02:31:29,280 --> 02:31:34,479 things from our c plus plus program go 3437 02:31:32,239 --> 02:31:35,920 ahead and finish up here and meet me 3438 02:31:34,478 --> 02:31:38,559 there in this lecture we're going to 3439 02:31:35,920 --> 02:31:41,120 learn about how we can get thanks in our 3440 02:31:38,559 --> 02:31:43,359 program and get things out of our 3441 02:31:41,120 --> 02:31:46,720 program and that's basically input and 3442 02:31:43,359 --> 02:31:49,040 output to and from our program we have 3443 02:31:46,719 --> 02:31:51,039 actually been doing this all along we 3444 02:31:49,040 --> 02:31:52,880 have been printing things from our 3445 02:31:52,879 --> 02:31:57,679 and with this technique you can really 3446 02:31:57,680 --> 02:32:02,960 as a highway that goes from your program 3447 02:32:01,040 --> 02:32:05,200 to the terminal you can basically think 3448 02:32:02,959 --> 02:32:08,239 of it like this data is going from your 3449 02:32:05,200 --> 02:32:10,560 program to sddc out and it's going from 3450 02:32:08,239 --> 02:32:13,280 here to the terminal and from this you 3451 02:32:13,280 --> 02:32:18,720 less than signs kind of point to the 3452 02:32:16,079 --> 02:32:22,318 left because data is going from your 3453 02:32:18,719 --> 02:32:25,279 program to htdc out and these two 3454 02:32:22,318 --> 02:32:29,840 symbols point to the left they point to 3455 02:32:25,280 --> 02:32:33,120 hddc out to signal to the fact that data 3456 02:32:29,840 --> 02:32:35,600 is going from here to here i really hope 3457 02:32:33,120 --> 02:32:37,920 this makes sense because sometimes these 3458 02:32:35,600 --> 02:32:41,040 things can confuse people this is the 3459 02:32:37,920 --> 02:32:43,520 reason why it's going from your data to 3460 02:32:41,040 --> 02:32:47,600 stdc out data is basically going from 3461 02:32:43,520 --> 02:32:50,079 your program into httc out and the sddc 3462 02:32:47,600 --> 02:32:52,000 out is one way we have to get data from 3463 02:32:50,079 --> 02:32:57,200 our program to the console there are 3464 02:32:52,000 --> 02:32:59,520 many others we have std c e r r or std 3465 02:32:57,200 --> 02:33:02,159 server this is what i'd like to call it 3466 02:32:59,520 --> 02:33:05,120 and what it does it prints errors to the 3467 02:33:02,159 --> 02:33:08,000 console we have stdc log which is going 3468 02:33:05,120 --> 02:33:09,760 to print log messages why do we have 3469 02:33:08,000 --> 02:33:12,478 these different things different 3470 02:33:09,760 --> 02:33:14,719 programs might decide to format these 3471 02:33:12,478 --> 02:33:17,199 messages differently because they are 3472 02:33:14,719 --> 02:33:19,519 different messages one is a simple 3473 02:33:17,200 --> 02:33:22,079 message from the program another is the 3474 02:33:19,520 --> 02:33:24,239 narrows you might want to turn this red 3475 02:33:22,079 --> 02:33:26,398 to really bring the attention 3476 02:33:24,239 --> 02:33:28,559 to this message because the user has to 3477 02:33:26,398 --> 02:33:30,719 solve that problem another might be a 3478 02:33:28,559 --> 02:33:33,039 log message that you want to store in 3479 02:33:33,040 --> 02:33:37,280 uses for these things differ and c plus 3480 02:33:35,439 --> 02:33:40,079 plus makes sure to give you different 3481 02:33:37,280 --> 02:33:42,720 ways to state your intent through the 3482 02:33:40,079 --> 02:33:47,600 tools that you use we have looked at 3483 02:33:42,719 --> 02:33:51,519 sddc out sddc error and stdc log what 3484 02:33:47,600 --> 02:33:53,439 does sd c i n do it does the reverse 3485 02:33:51,520 --> 02:33:56,479 it's going to take data out of the 3486 02:33:53,439 --> 02:33:58,880 program and bring that into the program 3487 02:33:56,478 --> 02:34:01,279 so let's look at a few examples using 3488 02:33:58,879 --> 02:34:04,478 these here we have an example of 3489 02:34:01,280 --> 02:34:06,800 printing data from the program to the 3490 02:34:08,719 --> 02:34:12,239 here we have a hello world message going 3491 02:34:12,239 --> 02:34:17,920 here we have a number going to c out 3492 02:34:15,680 --> 02:34:20,398 this is what we've been doing all along 3493 02:34:17,920 --> 02:34:22,318 we can also do a variable storing the 3494 02:34:20,398 --> 02:34:23,680 edge for example it's a whole number 3495 02:34:23,680 --> 02:34:30,000 so we print this out and the message is 3496 02:34:26,398 --> 02:34:32,079 going to go from the program to cl so 3497 02:34:30,000 --> 02:34:35,680 you can think that things are going from 3498 02:34:32,079 --> 02:34:37,760 the program to sddc out i really wanted 3499 02:34:35,680 --> 02:34:40,398 this to make sense okay if we go down 3500 02:34:37,760 --> 02:34:42,960 we're going to see that we can use cr to 3501 02:34:40,398 --> 02:34:45,358 print errors to the terminal and we 3502 02:34:42,959 --> 02:34:47,279 might say something went wrong or 3503 02:34:45,359 --> 02:34:50,159 something along these lines to bring the 3504 02:34:47,280 --> 02:34:53,280 attention to the problem and we can use 3505 02:34:50,159 --> 02:34:55,760 c log to print log messages and they 3506 02:34:53,280 --> 02:34:58,560 work the same way they all use the 3507 02:34:55,760 --> 02:35:01,280 stream output operator and it is going 3508 02:34:58,559 --> 02:35:03,519 to take data from the program to 3509 02:35:01,280 --> 02:35:05,760 the output stream really this is what we 3510 02:35:03,520 --> 02:35:08,239 mean here here is an example of how you 3511 02:35:05,760 --> 02:35:10,559 might read data and bring that into the 3512 02:35:08,239 --> 02:35:12,879 program here we have two variables one 3513 02:35:10,559 --> 02:35:15,600 is the age it's a whole number or an 3514 02:35:12,879 --> 02:35:18,000 integer and z plus plus and the other is 3515 02:35:15,600 --> 02:35:20,720 a string this is a type that allows you 3516 02:35:18,000 --> 02:35:22,559 to store strings in c plus plus we're 3517 02:35:20,719 --> 02:35:25,279 going to learn a little more about this 3518 02:35:22,559 --> 02:35:27,439 later but here i want to use it to 3519 02:35:25,280 --> 02:35:29,600 really make it clear that you can get 3520 02:35:27,439 --> 02:35:31,439 things from the outside and bring that 3521 02:35:29,600 --> 02:35:34,559 data in your program so we're going to 3522 02:35:31,439 --> 02:35:37,920 let the user type in the last name and 3523 02:35:37,920 --> 02:35:43,359 is getting data from the stream or from 3524 02:35:40,799 --> 02:35:45,920 the highway through which data comes 3525 02:35:43,359 --> 02:35:48,399 into our program and bringing that in 3526 02:35:45,920 --> 02:35:51,120 our program and here it should be clear 3527 02:35:48,398 --> 02:35:54,398 that the flow of data is in this 3528 02:35:51,120 --> 02:35:56,560 direction data is going from c i n and 3529 02:35:54,398 --> 02:35:59,119 we're storing that in a variable that we 3530 02:35:56,559 --> 02:36:01,519 have in our program that is called name 3531 02:35:59,120 --> 02:36:03,760 we do the same thing and ask the user to 3532 02:36:01,520 --> 02:36:06,159 print the age and they are going to 3533 02:36:03,760 --> 02:36:09,040 print that and the data is going to come 3534 02:36:06,159 --> 02:36:11,439 from cin and we're going to store that 3535 02:36:09,040 --> 02:36:13,120 in our edge variable here after we 3536 02:36:11,439 --> 02:36:14,800 collect the data from the user we're 3537 02:36:13,120 --> 02:36:16,960 going to print that and we're going to 3538 02:36:14,799 --> 02:36:18,799 say hello we're going to print the name 3539 02:36:16,959 --> 02:36:21,599 and we're going to say they are whatever 3540 02:36:18,799 --> 02:36:24,000 years old this is what this program does 3541 02:36:21,600 --> 02:36:27,200 and i really hope it makes it clear 3542 02:36:24,000 --> 02:36:29,200 how data input works through streams in 3543 02:36:27,200 --> 02:36:30,120 surplus plus another thing we can do 3544 02:36:30,120 --> 02:36:37,280 sddcin is chaining input and we can use 3545 02:36:34,639 --> 02:36:39,439 this technique to grab data in one 3546 02:36:37,280 --> 02:36:41,200 statement look at the program down here 3547 02:36:39,439 --> 02:36:43,680 to really understand this we have the 3548 02:36:41,200 --> 02:36:45,840 same variables age and name we let the 3549 02:36:43,680 --> 02:36:47,840 user type in the last name and age 3550 02:36:47,840 --> 02:36:53,120 and we grab the data like this we first 3551 02:36:50,318 --> 02:36:55,199 grab the name after that we grab the age 3552 02:36:53,120 --> 02:36:57,040 and we're going to print the same things 3553 02:36:57,040 --> 02:37:02,880 input stream operators like this to grab 3554 02:37:00,478 --> 02:37:04,478 the data in one go and this may come in 3555 02:37:04,478 --> 02:37:08,719 before we try these things in visual 3556 02:37:06,879 --> 02:37:10,799 studio code i want to bring your 3557 02:37:08,719 --> 02:37:13,760 attention to a problem that you are 3558 02:37:10,799 --> 02:37:16,879 likely to come in contact with and that 3559 02:37:13,760 --> 02:37:19,200 is reading data with spaces what if the 3560 02:37:19,200 --> 02:37:23,439 with spaces for example you asked me to 3561 02:37:21,359 --> 02:37:26,239 type my name i'm not going to type 3562 02:37:23,439 --> 02:37:28,000 daniel i'm going to type daniel cafwaya 3563 02:37:26,239 --> 02:37:30,559 and your program is going to run into 3564 02:37:28,000 --> 02:37:34,000 problems c plus plus allows to solve 3565 02:37:30,559 --> 02:37:35,119 this problem using std get line and you 3566 02:37:35,120 --> 02:37:39,840 using this syntax you're going to pass 3567 02:37:37,920 --> 02:37:41,760 in the stream where the data is going to 3568 02:37:39,840 --> 02:37:43,920 go and you're going to pass in the 3569 02:37:41,760 --> 02:37:45,040 variable that is going to read the data 3570 02:37:45,040 --> 02:37:50,000 information stored correctly in your 3571 02:37:47,200 --> 02:37:52,479 program this might be not making sense 3572 02:37:50,000 --> 02:37:55,040 yet to you we're going to go to visual 3573 02:37:52,478 --> 02:37:57,279 studio code and play with us and i am 3574 02:37:55,040 --> 02:37:59,840 sure it is going to make sense okay here 3575 02:37:57,280 --> 02:38:01,840 we are in visual studio code and we have 3576 02:37:59,840 --> 02:38:04,478 our project where we're going to store 3577 02:38:01,840 --> 02:38:06,639 our files data imported output we're 3578 02:38:04,478 --> 02:38:08,959 going to copy code from our template 3579 02:38:06,639 --> 02:38:11,119 project and we're going to put the data 3580 02:38:08,959 --> 02:38:13,438 in and i am going to open this folder in 3581 02:38:11,120 --> 02:38:15,920 visual studio code so let's close 3582 02:38:13,439 --> 02:38:18,960 whatever we have now and i am going to 3583 02:38:15,920 --> 02:38:21,520 open my folder let's browse there i am 3584 02:38:18,959 --> 02:38:24,000 on this folder data input and output i 3585 02:38:21,520 --> 02:38:26,560 am going to select that and the project 3586 02:38:24,000 --> 02:38:28,799 is going to open up in visual studio 3587 02:38:26,559 --> 02:38:30,719 code i am going to jump to my main 3588 02:38:28,799 --> 02:38:32,799 function i am going to take out things 3589 02:38:30,719 --> 02:38:34,799 we don't need and the first thing we're 3590 02:38:32,799 --> 02:38:36,079 going to do is printing data we have 3591 02:38:34,799 --> 02:38:38,239 really done this so we're going to 3592 02:38:36,079 --> 02:38:40,000 practice this a little bit we can say 3593 02:38:40,000 --> 02:38:47,359 and we can say hello c plus plus 20 why 3594 02:38:44,079 --> 02:38:50,079 not and we're going to put an std 3595 02:38:47,359 --> 02:38:53,680 endl at the end and remember our 3596 02:38:50,079 --> 02:38:56,959 semicolon and here i want you to really 3597 02:38:53,680 --> 02:39:01,040 put in your head that data is going from 3598 02:38:56,959 --> 02:39:02,879 our program here to hddc out and sddc 3599 02:39:01,040 --> 02:39:05,120 out is going to dump that on the 3600 02:39:02,879 --> 02:39:08,398 terminal that's how this works you can 3601 02:39:05,120 --> 02:39:11,760 also do a variable let's say ant 3602 02:39:08,398 --> 02:39:14,000 age for example let's say 21 and end 3603 02:39:11,760 --> 02:39:17,280 this with the semicolon and we can also 3604 02:39:14,000 --> 02:39:19,359 print the h we can say sddc out 3605 02:39:19,359 --> 02:39:23,600 age this is going to be sent to the 3606 02:39:23,600 --> 02:39:27,600 and we're going to do sdd and dl to add 3607 02:39:27,600 --> 02:39:32,239 new line character at the end and we can 3608 02:39:30,239 --> 02:39:34,159 try and build this before the program 3609 02:39:32,239 --> 02:39:36,239 gets more complicated let's go to 3610 02:39:34,159 --> 02:39:38,799 terminal we're going to run the test 3611 02:39:38,799 --> 02:39:44,318 and if we hit enter on the terminal here 3612 02:39:42,318 --> 02:39:47,039 the terminal is going to go away we're 3613 02:39:44,318 --> 02:39:49,600 going to create a new one no big deal 3614 02:39:47,040 --> 02:39:52,319 and uh if we do dir we're going to find 3615 02:39:49,600 --> 02:39:56,000 our rooster program if we run it it's 3616 02:39:52,318 --> 02:39:59,039 going to say hello c plus 20 and the age 3617 02:39:56,000 --> 02:40:01,600 is going to be 21. we are successfully 3618 02:39:59,040 --> 02:40:04,080 taking data from our program and 3619 02:40:01,600 --> 02:40:06,640 printing that to the terminal this is 3620 02:40:04,079 --> 02:40:09,359 pretty cool and you can think of sddc 3621 02:40:06,639 --> 02:40:12,079 out as the highway that is really going 3622 02:40:09,359 --> 02:40:16,079 to take your data from your program and 3623 02:40:12,079 --> 02:40:18,000 bring that to sddc out like this 3624 02:40:16,079 --> 02:40:20,398 we're also going to take the age it's 3625 02:40:18,000 --> 02:40:22,159 going to go through sddc out and it's 3626 02:40:20,398 --> 02:40:24,318 going to show up here you can think of 3627 02:40:22,159 --> 02:40:29,279 this like this okay now that we have 3628 02:40:24,318 --> 02:40:31,439 used sddc out we can use sddcr and sddc 3629 02:40:29,279 --> 02:40:34,000 log in the same way so we're going to 3630 02:40:34,000 --> 02:40:38,799 i'm going to say error message and the 3631 02:40:36,478 --> 02:40:41,358 message is going to be something is 3632 02:40:38,799 --> 02:40:43,840 wrong you may do whatever you want in 3633 02:40:41,359 --> 02:40:45,840 here and we have to remember to put a 3634 02:40:43,840 --> 02:40:48,239 new line character otherwise data is 3635 02:40:45,840 --> 02:40:51,359 going to be cramped on one line we can 3636 02:40:48,239 --> 02:40:54,959 also use sddc log it really works the 3637 02:40:51,359 --> 02:40:57,840 same way c log and we can say log 3638 02:40:54,959 --> 02:40:58,799 message and we're going to say something 3639 02:40:58,799 --> 02:41:05,920 and we remember to put std endl and our 3640 02:41:03,040 --> 02:41:07,520 semicolon if we build this program you 3641 02:41:05,920 --> 02:41:09,359 might guess what it's going to do it's 3642 02:41:07,520 --> 02:41:10,479 going to print these things on the 3643 02:41:13,439 --> 02:41:17,760 to terminal programs that support 3644 02:41:15,680 --> 02:41:20,079 features to format different messages 3645 02:41:17,760 --> 02:41:22,079 directly the program is going to know 3646 02:41:20,079 --> 02:41:24,159 that one message is a simple message the 3647 02:41:22,079 --> 02:41:26,799 other is an arrow the other is a log 3648 02:41:26,799 --> 02:41:30,559 but before we do that let's clear so 3649 02:41:28,639 --> 02:41:32,639 that we don't have noise here we're 3650 02:41:30,559 --> 02:41:34,959 going to run the program and it's going 3651 02:41:32,639 --> 02:41:37,278 to say the error message and the log 3652 02:41:34,959 --> 02:41:40,079 message here okay now what we're going 3653 02:41:37,279 --> 02:41:42,479 to look at is how we can get data into 3654 02:41:40,079 --> 02:41:44,799 our program and data is going to be 3655 02:41:42,478 --> 02:41:46,318 going in a different direction 3656 02:41:44,799 --> 02:41:49,358 it's going to be coming from the 3657 02:41:46,318 --> 02:41:51,519 terminal and flowing into our program 3658 02:41:49,359 --> 02:41:54,880 what we're going to do we're going to do 3659 02:41:51,520 --> 02:41:57,520 ant age one this is a new variable so 3660 02:41:54,879 --> 02:42:00,639 we're going to put in data and we're 3661 02:41:57,520 --> 02:42:04,079 going to bring in a string library to 3662 02:42:00,639 --> 02:42:05,439 allow our program to store strengths the 3663 02:42:04,079 --> 02:42:06,879 way we're going to do that we're going 3664 02:42:06,879 --> 02:42:11,278 strength this is how you do it in c plus 3665 02:42:09,279 --> 02:42:13,439 plus we haven't really learned about 3666 02:42:11,279 --> 02:42:15,600 this type it's a powerful type in c plus 3667 02:42:13,439 --> 02:42:17,600 plus but we will get it there when we 3668 02:42:15,600 --> 02:42:19,200 have more powerful tools to really 3669 02:42:17,600 --> 02:42:21,279 understand it we are just getting 3670 02:42:19,200 --> 02:42:24,159 started here so what we're going to do 3671 02:42:21,279 --> 02:42:25,680 we're going to say std strength and 3672 02:42:25,680 --> 02:42:30,960 and we're going to ask the user to print 3673 02:42:28,000 --> 02:42:33,520 their name and age you already know how 3674 02:42:30,959 --> 02:42:35,759 to do that so we're going to say please 3675 02:42:33,520 --> 02:42:38,800 type your name and age and we're going 3676 02:42:35,760 --> 02:42:41,520 to do stdndl on this and how we're going 3677 02:42:38,799 --> 02:42:43,318 to get data in the program we're going 3678 02:42:43,318 --> 02:42:50,398 stdcin and we're going to do std 3679 02:42:46,959 --> 02:42:53,199 cim and data is going to be going from 3680 02:42:50,398 --> 02:42:55,760 the stream to the program so the 3681 02:42:53,200 --> 02:42:56,560 stream input operator here points to the 3682 02:42:56,559 --> 02:43:00,959 and the data is going to be going in the 3683 02:42:59,279 --> 02:43:02,880 name variable because that's what 3684 02:43:00,959 --> 02:43:04,959 they're going to be typing in first 3685 02:43:02,879 --> 02:43:07,039 because that's what we told them to do 3686 02:43:04,959 --> 02:43:09,398 so we're going to store the name 3687 02:43:09,398 --> 02:43:17,439 stdcim data is going to be flowing from 3688 02:43:12,680 --> 02:43:19,120 sddcin into our h1 variable okay now 3689 02:43:17,439 --> 02:43:20,960 that the user has given us their 3690 02:43:19,120 --> 02:43:23,200 information we can greet them with a 3691 02:43:20,959 --> 02:43:26,799 personalized message we're going to say 3692 02:43:23,200 --> 02:43:29,279 stdc out hello and we're going to put 3693 02:43:26,799 --> 02:43:31,759 out their name and we're going to tell 3694 02:43:29,279 --> 02:43:32,640 them you are and we're going to put the 3695 02:43:32,639 --> 02:43:37,840 one because that's what they're going to 3696 02:43:34,559 --> 02:43:38,879 give us here and i'm going to say 3697 02:43:38,879 --> 02:43:44,639 and we're going to put an std endl here 3698 02:43:41,920 --> 02:43:47,600 i'm going to grab this and pull this to 3699 02:43:44,639 --> 02:43:50,559 the left so that we can see our entire 3700 02:43:47,600 --> 02:43:52,239 program here that's helpful so what we 3701 02:43:50,559 --> 02:43:54,398 are doing here we are getting data from 3702 02:43:52,239 --> 02:43:56,559 the user and we are storing that in our 3703 02:43:54,398 --> 02:43:58,639 variables and we are printing the 3704 02:43:56,559 --> 02:44:01,119 message so that the user can see it i 3705 02:43:58,639 --> 02:44:04,079 don't want the first things we did here 3706 02:44:01,120 --> 02:44:07,040 to disturb us when we world and try this 3707 02:44:04,079 --> 02:44:10,000 out so i am going to comment this out 3708 02:44:07,040 --> 02:44:12,000 and comment the first block of code out 3709 02:44:10,000 --> 02:44:14,398 using blog comments i think this is the 3710 02:44:12,000 --> 02:44:16,559 first time we use this and uh this is 3711 02:44:14,398 --> 02:44:18,799 going to be ignored by the compiler now 3712 02:44:16,559 --> 02:44:20,639 and it is only going to consider this 3713 02:44:18,799 --> 02:44:22,959 and this is going to be pretty cool 3714 02:44:20,639 --> 02:44:25,199 let's go down a little bit here and i 3715 02:44:22,959 --> 02:44:28,879 try to build the program we're going to 3716 02:44:25,200 --> 02:44:31,040 run the task that welds with gcc and uh 3717 02:44:28,879 --> 02:44:33,599 looks like the world is good we're going 3718 02:44:31,040 --> 02:44:36,560 to hit enter and we're going to clear so 3719 02:44:33,600 --> 02:44:38,318 that we are not disturbed by this we're 3720 02:44:36,559 --> 02:44:40,000 going to run the program let's say 3721 02:44:40,000 --> 02:44:44,559 it's going to ask me to type my name and 3722 02:44:42,559 --> 02:44:47,278 age my name is daniel i'm going to type 3723 02:44:44,559 --> 02:44:48,879 that hit enter it's going to want me to 3724 02:44:53,600 --> 02:44:59,840 and it's going to tell me hello daniel 3725 02:44:56,079 --> 02:45:02,000 you are 33 years old i put a slash here 3726 02:44:59,840 --> 02:45:04,159 we can try and run it again so that i 3727 02:45:02,000 --> 02:45:07,680 don't have to make that mistake again so 3728 02:45:04,159 --> 02:45:08,879 daniel that's my name i hit enter i say 3729 02:45:10,639 --> 02:45:16,318 it's going to say hello daniel you are 3730 02:45:13,040 --> 02:45:17,840 33 years old i really hope this brings 3731 02:45:21,680 --> 02:45:26,720 that takes data from the terminal into 3732 02:45:24,719 --> 02:45:28,559 your program so this is really what is 3733 02:45:26,719 --> 02:45:30,639 happening here when i type daniel it's 3734 02:45:28,559 --> 02:45:33,119 going to flow from the terminal and it's 3735 02:45:30,639 --> 02:45:35,840 going to go into my variable which is 3736 02:45:33,120 --> 02:45:38,720 name here and when i type the age it's 3737 02:45:35,840 --> 02:45:41,680 going to go in my edge variable here and 3738 02:45:41,680 --> 02:45:46,000 in here okay one other thing we can do 3739 02:45:46,000 --> 02:45:51,040 grab the data and put that in our 3740 02:45:48,318 --> 02:45:53,439 variables in one go so what we can do 3741 02:45:51,040 --> 02:45:54,359 here is comment this out and we're going 3742 02:45:54,359 --> 02:45:59,439 stdcin and we're going to put that in 3743 02:45:57,120 --> 02:46:02,720 name and we're going to change these two 3744 02:45:59,439 --> 02:46:05,200 things and put the other piece of data 3745 02:46:02,719 --> 02:46:07,679 in h1 if you run the program it's going 3746 02:46:05,200 --> 02:46:10,479 to work exactly the same way but we are 3747 02:46:07,680 --> 02:46:12,318 grabbing the data in one line so let's 3748 02:46:10,478 --> 02:46:15,119 weld to see this in action we're going 3749 02:46:12,318 --> 02:46:16,879 to run the task that worlds with gcc 3750 02:46:15,120 --> 02:46:18,960 we're going to click here and we're 3751 02:46:16,879 --> 02:46:21,039 going to clear if we run rooster it's 3752 02:46:18,959 --> 02:46:23,039 going to ask for the name and the age 3753 02:46:21,040 --> 02:46:26,160 i'm going to put in daniel and i'm going 3754 02:46:23,040 --> 02:46:28,319 to put my h and say 33 for example and 3755 02:46:26,159 --> 02:46:30,398 it's going to do exactly the same thing 3756 02:46:28,318 --> 02:46:32,159 but we have one compact line the last 3757 02:46:30,398 --> 02:46:34,879 thing i want to show you in this lecture 3758 02:46:32,159 --> 02:46:37,200 is how you can grab data with spaces 3759 02:46:34,879 --> 02:46:40,398 because if i try to put in data with 3760 02:46:37,200 --> 02:46:42,640 spaces here the program is going to fail 3761 02:46:40,398 --> 02:46:44,639 miserably let me show you this i'm going 3762 02:46:42,639 --> 02:46:47,439 to clear and i'm going to run rooster 3763 02:46:44,639 --> 02:46:50,318 again and i'm going to say daniel i'm 3764 02:46:47,439 --> 02:46:52,880 going to put in my full name and uh it's 3765 02:46:50,318 --> 02:46:55,519 really not grabbing the second name and 3766 02:46:52,879 --> 02:46:57,438 it didn't give me a chance to input my 3767 02:46:57,439 --> 02:47:03,040 so c plus plus provides a way to solve 3768 02:46:59,840 --> 02:47:04,880 this using std get line so let's see 3769 02:47:03,040 --> 02:47:06,240 what we are doing here and before we 3770 02:47:04,879 --> 02:47:08,719 play with this we're going to comment 3771 02:47:06,239 --> 02:47:11,279 out what we had here so that 3772 02:47:08,719 --> 02:47:14,159 it doesn't disturb us in what we are 3773 02:47:11,279 --> 02:47:16,560 about to do so let's come down here and 3774 02:47:14,159 --> 02:47:18,478 say that we were doing data input this 3775 02:47:16,559 --> 02:47:21,439 is helpful here and we're going to go 3776 02:47:18,478 --> 02:47:23,599 down and grab data with spaces so i am 3777 02:47:21,439 --> 02:47:25,840 going to set up another strength and 3778 02:47:23,600 --> 02:47:28,318 this is going to store my full name 3779 02:47:25,840 --> 02:47:30,318 that's what i'm going to say here and to 3780 02:47:28,318 --> 02:47:33,199 grab it i'm going to tell the user to 3781 02:47:30,318 --> 02:47:35,760 type in their full name and age so i am 3782 02:47:33,200 --> 02:47:38,880 going to sddc out so the way i'm going 3783 02:47:35,760 --> 02:47:40,880 to grab the data i'm going to do std get 3784 02:47:38,879 --> 02:47:42,799 line you see that we have this function 3785 02:47:40,879 --> 02:47:45,920 here and it's going to take an input 3786 02:47:42,799 --> 02:47:47,519 stream so asd dcin is going to work and 3787 02:47:45,920 --> 02:47:49,760 the second parameter is going to be the 3788 02:47:47,520 --> 02:47:51,680 variable where this string data is going 3789 02:47:49,760 --> 02:47:54,559 to be stored we're going to pass in our 3790 02:47:51,680 --> 02:47:57,920 full name and this is going to work just 3791 02:47:54,559 --> 02:47:59,680 fine and we're going to also grab the h 3792 02:47:57,920 --> 02:48:01,559 using the same technique we've used 3793 02:48:01,559 --> 02:48:07,680 stdcin and we're going to store that in 3794 02:48:04,559 --> 02:48:10,719 our h3 variable once we have the data we 3795 02:48:07,680 --> 02:48:12,720 can greet our user using the same 3796 02:48:10,719 --> 02:48:15,278 message we had before so i'm going to 3797 02:48:12,719 --> 02:48:17,599 copy that if i can do that and i'm going 3798 02:48:15,279 --> 02:48:19,439 to paste that here i'm going to bring 3799 02:48:17,600 --> 02:48:21,680 this to the same location it's going to 3800 02:48:19,439 --> 02:48:25,040 be full name here and it's going to say 3801 02:48:21,680 --> 02:48:28,479 h3 and let's try and weld we're going to 3802 02:48:25,040 --> 02:48:31,600 terminal run the task to bolt with gcc 3803 02:48:28,478 --> 02:48:33,760 and we have a problem because we forgot 3804 02:48:31,600 --> 02:48:35,680 our semicolon see how these problems 3805 02:48:33,760 --> 02:48:38,398 might come to you uh-huh so we're going 3806 02:48:35,680 --> 02:48:41,120 to put in our semicolon and another 3807 02:48:38,398 --> 02:48:42,398 thing i really want you to see 3808 02:48:42,398 --> 02:48:47,199 bring these things to the next line if 3809 02:48:44,639 --> 02:48:49,358 they turn out too long like we see here 3810 02:48:47,200 --> 02:48:51,200 so i can for example put this to the 3811 02:48:49,359 --> 02:48:53,120 next line and it is going to work just 3812 02:48:51,200 --> 02:48:56,159 fine so what i am going to do i'm going 3813 02:48:53,120 --> 02:48:59,279 to try and weld again run task to build 3814 02:48:56,159 --> 02:49:00,398 with gcc you see that the world is good 3815 02:49:00,398 --> 02:49:05,519 bring in our terminal i'm going to clear 3816 02:49:02,879 --> 02:49:07,438 so that we don't have noise here and 3817 02:49:05,520 --> 02:49:09,600 we're going to run our program it's 3818 02:49:07,439 --> 02:49:11,600 going to ask me to put in my full name 3819 02:49:11,600 --> 02:49:15,600 and it's going to want the age i'm going 3820 02:49:13,520 --> 02:49:18,640 to say 33 and it's going to reach me 3821 02:49:15,600 --> 02:49:21,600 hello daniel gaway you are 33 years old 3822 02:49:18,639 --> 02:49:24,478 we are able to bring data with spaces 3823 02:49:21,600 --> 02:49:26,800 into our program and this concludes what 3824 02:49:24,478 --> 02:49:30,639 we had to say in this lecture i hope it 3825 02:49:26,799 --> 02:49:33,519 made it very clear what these htd n and 3826 02:49:30,639 --> 02:49:36,000 stdc out things are they are basically 3827 02:49:33,520 --> 02:49:38,640 highways that take data out of your 3828 02:49:36,000 --> 02:49:40,639 program or bring data into your program 3829 02:49:38,639 --> 02:49:42,398 and the way you use them is what we've 3830 02:49:40,639 --> 02:49:44,159 done here in this course i would 3831 02:49:42,398 --> 02:49:45,840 encourage you to play with us a couple 3832 02:49:44,159 --> 02:49:47,680 of times if this is the first time 3833 02:49:45,840 --> 02:49:49,520 you're doing this we are going to stop 3834 02:49:47,680 --> 02:49:51,920 here in this lecture in the next one 3835 02:49:49,520 --> 02:49:54,560 we're going to try and recap on the c 3836 02:49:51,920 --> 02:49:57,120 plus plus development workflow go ahead 3837 02:49:54,559 --> 02:49:59,039 and finish up here and meet me there 3838 02:49:57,120 --> 02:50:02,160 in this lecture we're going to explore 3839 02:49:59,040 --> 02:50:04,800 the c plus plus execution model and show 3840 02:50:02,159 --> 02:50:08,079 you a simplified version of the memory 3841 02:50:04,799 --> 02:50:11,278 model of your c plus plus program here 3842 02:50:08,079 --> 02:50:13,680 is how our program looks it is a simple 3843 02:50:11,279 --> 02:50:15,840 program in our main function it is going 3844 02:50:13,680 --> 02:50:17,600 to call a simple function that is going 3845 02:50:15,840 --> 02:50:19,680 to add two numbers we have seen a 3846 02:50:17,600 --> 02:50:22,239 program like this before and when we 3847 02:50:19,680 --> 02:50:25,200 compile it it's going to be turned into 3848 02:50:22,239 --> 02:50:28,159 a binary this black icon here represents 3849 02:50:25,200 --> 02:50:29,439 the binary executable and it is not in 3850 02:50:29,439 --> 02:50:34,159 that you can read with your eyes if we 3851 02:50:31,680 --> 02:50:36,800 come back here to what we were doing in 3852 02:50:34,159 --> 02:50:39,600 the last lecture we can see our rooster 3853 02:50:36,799 --> 02:50:41,278 executable binary here if you try to 3854 02:50:39,600 --> 02:50:43,040 open it here you're going to see that 3855 02:50:41,279 --> 02:50:45,040 visual studio code is going to say the 3856 02:50:43,040 --> 02:50:48,240 file is not displayed in the editor 3857 02:50:45,040 --> 02:50:52,000 because it is either binary or uses an 3858 02:50:48,239 --> 02:50:53,520 unsupported text encoding so it says i 3859 02:50:52,000 --> 02:50:55,359 can't really say this because it's 3860 02:50:53,520 --> 02:50:57,760 binary and it says do you want to open 3861 02:50:55,359 --> 02:50:59,600 it anyway if i try to open it it's going 3862 02:50:57,760 --> 02:51:02,719 to be just some gibberish because this 3863 02:51:02,719 --> 02:51:09,039 read by programs it is not meant to be 3864 02:51:05,520 --> 02:51:11,760 by ides or text editors like visual 3865 02:51:09,040 --> 02:51:14,560 studio code so this is the binary file 3866 02:51:11,760 --> 02:51:16,478 we have here i tried to put a simplified 3867 02:51:14,559 --> 02:51:18,559 version of this program to represent 3868 02:51:16,478 --> 02:51:21,278 binary so that we can follow it up but 3869 02:51:18,559 --> 02:51:24,398 it is not in the form that is readable 3870 02:51:21,279 --> 02:51:26,720 by any human out there unless you are a 3871 02:51:24,398 --> 02:51:29,599 machine so why is it in binary because 3872 02:51:26,719 --> 02:51:31,920 binary format is easily understandable 3873 02:51:29,600 --> 02:51:34,079 by your cpu and we're going to see how 3874 02:51:31,920 --> 02:51:36,719 your program is loaded in memory and 3875 02:51:34,079 --> 02:51:39,200 executed statement by statement by the 3876 02:51:36,719 --> 02:51:41,679 cpu so that you can really understand 3877 02:51:39,200 --> 02:51:44,319 this so here is a representation of your 3878 02:51:41,680 --> 02:51:45,840 program here we have our how to drive 3879 02:51:44,318 --> 02:51:48,639 and we have the program sitting 3880 02:51:45,840 --> 02:51:52,000 somewhere on our hard drive in binary 3881 02:51:48,639 --> 02:51:54,639 format and it's waiting to be run by you 3882 02:51:52,000 --> 02:51:56,879 by double clicking on it or opening it 3883 02:51:54,639 --> 02:51:59,680 in the terminal and on the left here we 3884 02:51:56,879 --> 02:52:02,478 see a representation of the memory of 3885 02:51:59,680 --> 02:52:05,200 our computer and when i say memory i 3886 02:52:02,478 --> 02:52:07,920 mean random access memory i don't mean 3887 02:52:05,200 --> 02:52:09,920 memory on your hard drive this should be 3888 02:52:07,920 --> 02:52:12,318 really clear this is the random access 3889 02:52:09,920 --> 02:52:15,200 memory of your computer that is running 3890 02:52:12,318 --> 02:52:18,239 your programs very fast so when you 3891 02:52:15,200 --> 02:52:20,720 double click on our program or if we 3892 02:52:18,239 --> 02:52:23,279 open it in the terminal the binary file 3893 02:52:20,719 --> 02:52:25,119 is going to be loaded up in memory and 3894 02:52:23,279 --> 02:52:27,520 you see it in red here this is a 3895 02:52:25,120 --> 02:52:30,240 representation of that and it is ready 3896 02:52:27,520 --> 02:52:32,319 to be run by the cpu by the time the cpu 3897 02:52:30,239 --> 02:52:34,478 says in the memory location it's going 3898 02:52:32,318 --> 02:52:36,559 to know i have a program to run and the 3899 02:52:34,478 --> 02:52:38,398 cpu is going to start running it 3900 02:52:36,559 --> 02:52:40,639 statement by statement it is going to 3901 02:52:38,398 --> 02:52:43,760 start by the top so it's going to see 3902 02:52:40,639 --> 02:52:46,478 that it needs to allocate space to store 3903 02:52:43,760 --> 02:52:48,800 our integer a it's going to allocate to 3904 02:52:46,478 --> 02:52:50,879 that somewhere on the memory in yellow 3905 02:52:48,799 --> 02:52:53,920 here you see a and it's going to see 3906 02:52:50,879 --> 02:52:55,519 that it should store in a value of 10 3907 02:52:53,920 --> 02:52:57,760 and it's going to do that you can see 3908 02:52:55,520 --> 02:53:00,239 that here it's going to go to the next 3909 02:52:57,760 --> 02:53:02,398 statement and the next statement tells 3910 02:53:02,398 --> 02:53:08,079 a value b and it's going to store in f5 3911 02:53:06,000 --> 02:53:10,559 it's going to go to the next statement 3912 02:53:08,079 --> 02:53:12,559 it's a variable called c it's going to 3913 02:53:10,559 --> 02:53:15,439 allocate space for it but we didn't 3914 02:53:12,559 --> 02:53:17,840 specify a value for c so what it's going 3915 02:53:15,439 --> 02:53:20,639 to do is store in some junk value the 3916 02:53:17,840 --> 02:53:22,880 value in there is really unspecified at 3917 02:53:20,639 --> 02:53:25,119 the moment it's going to jump to the 3918 02:53:22,879 --> 02:53:27,599 next statement and what it's going to do 3919 02:53:25,120 --> 02:53:29,279 is print some things on the console and 3920 02:53:27,600 --> 02:53:31,439 it is going to do that it's going to 3921 02:53:29,279 --> 02:53:34,720 jump to the next statement and it is 3922 02:53:31,439 --> 02:53:36,559 going to print statement 2 and here it 3923 02:53:34,719 --> 02:53:38,398 is going to notice that it is going to 3924 02:53:36,559 --> 02:53:40,719 call a function i don't know if you have 3925 02:53:38,398 --> 02:53:44,079 noticed but the cpu has allocated a 3926 02:53:40,719 --> 02:53:46,318 special memory location for our function 3927 02:53:44,079 --> 02:53:49,039 the function doesn't leave with our code 3928 02:53:46,318 --> 02:53:52,719 here or anywhere near the variables that 3929 02:53:49,040 --> 02:53:55,040 we have here there might be a huge chunk 3930 02:53:52,719 --> 02:53:57,840 of memory between these two or three 3931 02:53:55,040 --> 02:54:00,080 things so the cpu knows that it is going 3932 02:53:57,840 --> 02:54:02,719 to jump to another location it's going 3933 02:54:00,079 --> 02:54:04,799 to ask itself if i jump to that location 3934 02:54:02,719 --> 02:54:07,599 to run this function how am i going to 3935 02:54:04,799 --> 02:54:10,239 come back and the cpu is smart enough to 3936 02:54:07,600 --> 02:54:12,159 store the return address and that's what 3937 02:54:10,239 --> 02:54:13,920 it's going to do it's going to say if i 3938 02:54:12,159 --> 02:54:16,478 go to that function i want to come back 3939 02:54:13,920 --> 02:54:18,079 to the address 6 and run whatever is 3940 02:54:16,478 --> 02:54:21,278 after this address so it's going to 3941 02:54:18,079 --> 02:54:23,039 store 6 and cpu memory and it's going to 3942 02:54:21,279 --> 02:54:25,600 jump to the function it's going to take 3943 02:54:23,040 --> 02:54:28,640 the first parameter as a and the second 3944 02:54:25,600 --> 02:54:30,800 parameter as b so a is done the 3945 02:54:28,639 --> 02:54:33,199 parameter 1 is going to be 10 it's going 3946 02:54:30,799 --> 02:54:35,840 to take parameter 2 it's going to be 5 3947 02:54:33,200 --> 02:54:38,079 because it is b here and in b we have a 3948 02:54:35,840 --> 02:54:40,239 5 and it is going to jump to the next 3949 02:54:38,079 --> 02:54:42,559 statement in the function and it is 3950 02:54:40,239 --> 02:54:45,680 going to add up parameter 1 and 3951 02:54:42,559 --> 02:54:48,478 parameter 2. after it adds those things 3952 02:54:45,680 --> 02:54:50,720 up it's going to return the result to c 3953 02:54:48,478 --> 02:54:52,879 we can see that here okay that's what 3954 02:54:50,719 --> 02:54:55,438 the cpu is going to do it's going to put 3955 02:54:52,879 --> 02:54:57,358 the results in c and the result is a 15 3956 02:54:55,439 --> 02:55:00,079 so we're going to write that in there 3957 02:54:57,359 --> 02:55:01,920 and now that the function is really done 3958 02:55:00,079 --> 02:55:03,920 it's going to come back to its address 3959 02:55:01,920 --> 02:55:06,559 and it is going to jump to the next 3960 02:55:03,920 --> 02:55:09,120 segment after that it is going to 3961 02:55:06,559 --> 02:55:11,439 print three statement three it is going 3962 02:55:09,120 --> 02:55:14,560 to jump through the next line and print 3963 02:55:11,439 --> 02:55:17,680 statement four it's going to do that and 3964 02:55:14,559 --> 02:55:20,239 by this time we have reached the end of 3965 02:55:17,680 --> 02:55:22,639 our program and it is going to be popped 3966 02:55:20,239 --> 02:55:24,959 off the memory and your program is 3967 02:55:22,639 --> 02:55:28,478 basically going to end this is how your 3968 02:55:24,959 --> 02:55:30,559 program is executed by your cpu and why 3969 02:55:28,478 --> 02:55:32,799 am i showing you this because it is 3970 02:55:30,559 --> 02:55:35,359 really good to have an idea of how 3971 02:55:32,799 --> 02:55:38,000 memory is allocated and used by your 3972 02:55:35,359 --> 02:55:40,318 program another good reason is that some 3973 02:55:38,000 --> 02:55:42,719 c plus plus feature require you to have 3974 02:55:40,318 --> 02:55:44,639 an understanding of a memory model like 3975 02:55:42,719 --> 02:55:46,879 this to understand how they work for 3976 02:55:44,639 --> 02:55:49,358 example you have seen that to call this 3977 02:55:46,879 --> 02:55:53,039 function the program had to jump from 3978 02:55:49,359 --> 02:55:55,040 address 6 to 30 and this is really heavy 3979 02:55:53,040 --> 02:55:57,120 especially if you are running a simple 3980 02:55:55,040 --> 02:55:59,920 function like we are doing here to add 3981 02:55:57,120 --> 02:56:02,880 two numbers so the cpu can realize that 3982 02:55:59,920 --> 02:56:06,079 and optimize this call to the function 3983 02:56:02,879 --> 02:56:08,799 to an inline function and add these two 3984 02:56:06,079 --> 02:56:10,799 numbers up right here at address six 3985 02:56:08,799 --> 02:56:12,879 without jumping to this address here and 3986 02:56:10,799 --> 02:56:14,959 this may come in handy and you need an 3987 02:56:12,879 --> 02:56:17,278 understanding of this memory model to 3988 02:56:14,959 --> 02:56:19,438 really understand these features this is 3989 02:56:17,279 --> 02:56:22,000 just one example i am giving you you are 3990 02:56:19,439 --> 02:56:24,559 going to reuse this model a lot in your 3991 02:56:22,000 --> 02:56:26,478 career as a class plus developer and i 3992 02:56:24,559 --> 02:56:29,119 thought it was a really good idea for 3993 02:56:26,478 --> 02:56:30,639 you to understand it firsthand this is 3994 02:56:29,120 --> 02:56:33,040 really all i had to share in this 3995 02:56:30,639 --> 02:56:34,719 lecture i hope you found it interesting 3996 02:56:33,040 --> 02:56:36,720 in the next one we're going to try and 3997 02:56:34,719 --> 02:56:38,959 understand the difference between three 3998 02:56:36,719 --> 02:56:41,039 key components of the c plus plus 3999 02:56:38,959 --> 02:56:43,199 programming language go ahead and finish 4000 02:56:41,040 --> 02:56:45,120 up here and meet me there in this 4001 02:56:43,200 --> 02:56:47,680 lecture i want to highlight the 4002 02:56:45,120 --> 02:56:50,640 differences between what we call a c 4003 02:56:47,680 --> 02:56:53,359 plus plus core language feature or a 4004 02:56:50,639 --> 02:56:55,278 standard library feature or an stl 4005 02:56:53,359 --> 02:56:57,920 feature because these are terms that 4006 02:56:55,279 --> 02:57:00,479 come up a lot if you happen to read some 4007 02:56:57,920 --> 02:57:03,920 documentation about c plus plus a core 4008 02:57:00,478 --> 02:57:05,840 feature is really a basic building block 4009 02:57:03,920 --> 02:57:08,239 that makes up the c plus plus 4010 02:57:05,840 --> 02:57:10,880 programming language you can think of it 4011 02:57:08,239 --> 02:57:13,760 for example how you define variables the 4012 02:57:10,879 --> 02:57:16,159 rules that govern how you can define and 4013 02:57:13,760 --> 02:57:18,800 use a function in c plus plus the rules 4014 02:57:16,159 --> 02:57:21,359 that say what you can do and not do with 4015 02:57:18,799 --> 02:57:24,239 the basic features of c plus plus that's 4016 02:57:21,359 --> 02:57:26,800 really what can be qualified as the core 4017 02:57:24,239 --> 02:57:29,039 feature of the c plus plus programming 4018 02:57:26,799 --> 02:57:31,438 language i don't know if that makes any 4019 02:57:29,040 --> 02:57:33,359 sense if we come back in visual studio 4020 02:57:31,439 --> 02:57:35,920 code we have a program we have used in 4021 02:57:33,359 --> 02:57:38,640 the last few lectures for example these 4022 02:57:35,920 --> 02:57:41,359 basic types that you use in c plus plus 4023 02:57:38,639 --> 02:57:45,039 the rules that define how you can define 4024 02:57:41,359 --> 02:57:47,439 a function for example if you put a 4025 02:57:45,040 --> 02:57:50,720 an angle bracket here and try to compile 4026 02:57:47,439 --> 02:57:53,840 this program it is not going to work 4027 02:57:50,719 --> 02:57:56,879 because we have violated the rules of 4028 02:57:53,840 --> 02:57:59,120 the c plus plus programs the basic rules 4029 02:57:56,879 --> 02:58:01,438 that define how the c plus plus 4030 02:57:59,120 --> 02:58:03,279 programming language works the basic 4031 02:58:01,439 --> 02:58:05,279 types that are defined in the c plus 4032 02:58:03,279 --> 02:58:08,079 plus programming language these little 4033 02:58:05,279 --> 02:58:10,960 things make up the raw ingredients you 4034 02:58:08,079 --> 02:58:14,079 use to make up a proper c plus plus 4035 02:58:10,959 --> 02:58:16,318 programs and these little row components 4036 02:58:14,079 --> 02:58:18,959 that you use to put together a c plus 4037 02:58:16,318 --> 02:58:19,920 plus program are called core features 4038 02:58:19,920 --> 02:58:25,840 think of them as the foundation on top 4039 02:58:22,799 --> 02:58:28,799 of which we really build daily use c 4040 02:58:25,840 --> 02:58:31,600 plus plus programs the standard library 4041 02:58:31,600 --> 02:58:36,079 highly specialized components that we 4042 02:58:34,000 --> 02:58:38,478 can easily use in our c plus plus 4043 02:58:36,079 --> 02:58:41,760 programs and again coming back to our 4044 02:58:38,478 --> 02:58:44,239 program in visual studio code iostream 4045 02:58:41,760 --> 02:58:46,880 here is a standard library feature 4046 02:58:44,239 --> 02:58:49,039 string here is a standard library thing 4047 02:58:46,879 --> 02:58:51,920 we have in the c plus plus programming 4048 02:58:49,040 --> 02:58:54,960 language and again the c plus plus 4049 02:58:51,920 --> 02:58:57,680 standard library tries to provide highly 4050 02:58:54,959 --> 02:59:00,799 specialized components that we can use 4051 02:58:57,680 --> 02:59:02,800 in regular c plus plus programs when you 4052 02:59:00,799 --> 02:59:05,519 use the c plus plus programming language 4053 02:59:02,799 --> 02:59:08,318 you don't really want to extend the c 4054 02:59:05,520 --> 02:59:10,720 plus plus programming language you want 4055 02:59:08,318 --> 02:59:13,519 to use it to build something of your own 4056 02:59:10,719 --> 02:59:16,000 and uh the standard library features are 4057 02:59:13,520 --> 02:59:18,880 things you use to work on your programs 4058 02:59:16,000 --> 02:59:20,799 very fast for example we have used sddc 4059 02:59:18,879 --> 02:59:23,119 out without really going into the 4060 02:59:20,799 --> 02:59:25,039 details of how the data is taken from 4061 02:59:23,120 --> 02:59:27,680 this program to the terminal that's 4062 02:59:25,040 --> 02:59:30,319 really advanced and probably complicated 4063 02:59:27,680 --> 02:59:33,120 but it is wrapped into this reusable 4064 02:59:30,318 --> 02:59:36,079 components and one of them is iostream 4065 02:59:33,120 --> 02:59:38,800 and we can use it relatively easily in 4066 02:59:36,079 --> 02:59:40,879 our c plus plus program okay i hope now 4067 02:59:38,799 --> 02:59:43,599 you have a better idea of what the c 4068 02:59:40,879 --> 02:59:47,358 plus plus standard library is now what 4069 02:59:43,600 --> 02:59:50,318 is the stl the stl is really part of the 4070 02:59:47,359 --> 02:59:53,279 c plus plus standard library but it is a 4071 02:59:50,318 --> 02:59:56,079 collection of container types think of 4072 02:59:53,279 --> 02:59:58,720 them as collections of things and we 4073 02:59:56,079 --> 03:00:02,239 have a set of types that allow us to 4074 02:59:58,719 --> 03:00:04,959 store collections we have algorithms or 4075 03:00:02,239 --> 03:00:08,079 functions that work on these collections 4076 03:00:04,959 --> 03:00:10,799 and we have specialized types that are 4077 03:00:08,079 --> 03:00:12,959 called iterators that allow us to 4078 03:00:10,799 --> 03:00:16,079 navigate through these containers 4079 03:00:12,959 --> 03:00:18,639 potentially running these algorithms on 4080 03:00:16,079 --> 03:00:21,039 each element in the collection this is 4081 03:00:18,639 --> 03:00:24,159 probably not making sense by now but you 4082 03:00:21,040 --> 03:00:26,560 can think of the stl as a highly 4083 03:00:24,159 --> 03:00:28,079 specialized part of the c plus plus 4084 03:00:26,559 --> 03:00:30,000 standard library and we're going to 4085 03:00:28,079 --> 03:00:32,239 learn about many features of these 4086 03:00:30,000 --> 03:00:35,120 things throughout the course but i want 4087 03:00:32,239 --> 03:00:37,680 you to have a basic understanding 4088 03:00:35,120 --> 03:00:40,079 between these three terms a core feature 4089 03:00:37,680 --> 03:00:43,279 of the c plus plus programming language 4090 03:00:40,079 --> 03:00:46,398 is standard library feature and the stl 4091 03:00:43,279 --> 03:00:48,560 and i really hope this is clear to you 4092 03:00:46,398 --> 03:00:51,439 by now in this chapter we're going to 4093 03:00:48,559 --> 03:00:53,519 learn about variables and data types 4094 03:00:51,439 --> 03:00:55,520 before we talk about variables and data 4095 03:00:53,520 --> 03:00:58,079 types i would like to take a moment and 4096 03:00:58,079 --> 03:01:01,920 and if you look at these cards many of 4097 03:01:00,239 --> 03:01:03,520 these things should make sense now you 4098 03:01:01,920 --> 03:01:05,200 know about the main function you know 4099 03:01:03,520 --> 03:01:07,439 that it is the entry point of your 4100 03:01:05,200 --> 03:01:09,600 program you know that your program is 4101 03:01:07,439 --> 03:01:12,000 going to be made up of statements and 4102 03:01:09,600 --> 03:01:13,040 statements and with a semicolon in c 4103 03:01:13,040 --> 03:01:18,160 you know about functions errors warnings 4104 03:01:15,680 --> 03:01:20,318 you now have an idea about how you can 4105 03:01:18,159 --> 03:01:22,879 handle input and output from your 4106 03:01:20,318 --> 03:01:25,359 program you can use comments you know 4107 03:01:22,879 --> 03:01:27,519 about the memory model and the execution 4108 03:01:25,359 --> 03:01:29,520 model that we talked about in the last 4109 03:01:27,520 --> 03:01:31,760 lecture you know quite a lot now we have 4110 03:01:29,520 --> 03:01:34,079 been using a program like this to do 4111 03:01:31,760 --> 03:01:36,478 things in the last chapter but there is 4112 03:01:34,079 --> 03:01:39,439 one thing that isn't really explained 4113 03:01:36,478 --> 03:01:42,879 yet and that is this ant this end is a 4114 03:01:39,439 --> 03:01:46,159 type that is predefined in c plus plus 4115 03:01:42,879 --> 03:01:48,959 and c plus plus predefines many types 4116 03:01:46,159 --> 03:01:51,760 here we have a couple of these types for 4117 03:01:48,959 --> 03:01:53,919 you to see as examples it is one of them 4118 03:01:51,760 --> 03:01:56,800 we have seen it a lot of times in the 4119 03:01:53,920 --> 03:01:59,520 last chapter it is used to represent 4120 03:01:56,799 --> 03:02:02,159 whole numbers like one two and three but 4121 03:01:59,520 --> 03:02:05,200 we have others for example double and 4122 03:02:02,159 --> 03:02:08,159 float are used to represent fractional 4123 03:02:05,200 --> 03:02:12,319 numbers numbers that have decimal points 4124 03:02:08,159 --> 03:02:13,840 for example 2.5 7.3 and numbers like 4125 03:02:12,318 --> 03:02:15,840 that and we're going to see the 4126 03:02:13,840 --> 03:02:19,359 difference between these two another one 4127 03:02:15,840 --> 03:02:22,000 that is commonly used is char or car and 4128 03:02:19,359 --> 03:02:25,279 it is used to represent characters in 4129 03:02:22,000 --> 03:02:28,079 memory and you can store in characters 4130 03:02:25,279 --> 03:02:30,000 like a b c and d and all the other 4131 03:02:28,079 --> 03:02:32,559 characters you can really put in we have 4132 03:02:30,000 --> 03:02:35,040 a bull which is a type that is going to 4133 03:02:32,559 --> 03:02:37,359 store two possible states the only 4134 03:02:35,040 --> 03:02:39,520 values you can really store in here are 4135 03:02:37,359 --> 03:02:43,279 true or false and we're going to see how 4136 03:02:39,520 --> 03:02:46,239 this works void is uh somewhat confusing 4137 03:02:43,279 --> 03:02:49,200 because it represents a typeless type 4138 03:02:46,239 --> 03:02:51,760 and it is really meaningful when we get 4139 03:02:49,200 --> 03:02:53,920 to talk about functions in detail so 4140 03:02:51,760 --> 03:02:56,318 we're going to learn more about the void 4141 03:02:53,920 --> 03:02:59,120 type a little later in the course we 4142 03:02:56,318 --> 03:03:02,159 also have the auto type which is really 4143 03:02:59,120 --> 03:03:04,720 not a type it is a keyword you use in c 4144 03:03:02,159 --> 03:03:06,959 plus plus to deduce other types and 4145 03:03:04,719 --> 03:03:09,199 we're going to see how this works later 4146 03:03:06,959 --> 03:03:12,159 in the course these are a few examples 4147 03:03:09,200 --> 03:03:14,479 of types we can use in c plus plus and 4148 03:03:12,159 --> 03:03:16,398 we are going to play with most of them 4149 03:03:14,478 --> 03:03:19,119 in this chapter another thing i really 4150 03:03:16,398 --> 03:03:21,840 want you to see is what does it mean to 4151 03:03:19,120 --> 03:03:23,680 really put a 4 in a variable in your 4152 03:03:21,840 --> 03:03:26,799 program everything you have in this 4153 03:03:23,680 --> 03:03:29,520 program here is represented by ones and 4154 03:03:26,799 --> 03:03:31,759 zeros in the computer's memory it really 4155 03:03:29,520 --> 03:03:34,800 looks like this it's a bunch of ones and 4156 03:03:31,760 --> 03:03:36,559 zeros in the ram of your computer and 4157 03:03:34,799 --> 03:03:39,358 there must be a technique to make sense 4158 03:03:36,559 --> 03:03:41,760 of this gibberish here and store data 4159 03:03:39,359 --> 03:03:44,559 that makes sense to humans and the 4160 03:03:41,760 --> 03:03:47,439 starting point is to group these ones 4161 03:03:44,559 --> 03:03:49,760 and zeros into little chunks that we can 4162 03:03:47,439 --> 03:03:52,639 manipulate each of these ones and zeros 4163 03:03:49,760 --> 03:03:55,040 is called a bet so this zero here is a 4164 03:03:52,639 --> 03:03:57,039 bit this one here is a bet and if we 4165 03:03:55,040 --> 03:03:59,840 group eight beats together they are 4166 03:03:57,040 --> 03:04:01,920 going to form what we call a byte okay 4167 03:03:59,840 --> 03:04:04,960 if it makes sense you can group your 4168 03:04:01,920 --> 03:04:07,439 bits in bytes or if it makes sense to 4169 03:04:04,959 --> 03:04:09,199 group them in groups of 16 you can do 4170 03:04:07,439 --> 03:04:11,200 that like we see here we're going to see 4171 03:04:09,200 --> 03:04:13,359 a little more details about this in the 4172 03:04:11,200 --> 03:04:16,159 next lecture okay here you see that if 4173 03:04:13,359 --> 03:04:19,520 you really want you can treat each bit 4174 03:04:16,159 --> 03:04:21,840 as a self-contained piece of data but 4175 03:04:19,520 --> 03:04:24,640 this is really not very useful but there 4176 03:04:21,840 --> 03:04:26,799 are some corner use cases for this okay 4177 03:04:24,639 --> 03:04:29,519 now we really have two sides on the 4178 03:04:26,799 --> 03:04:32,318 right we have data in the format that is 4179 03:04:29,520 --> 03:04:35,120 really convenient and understandable by 4180 03:04:32,318 --> 03:04:37,680 humans and on the left we have how data 4181 03:04:35,120 --> 03:04:40,319 is represented in memory so there must 4182 03:04:37,680 --> 03:04:42,720 be a way to transform between these two 4183 03:04:40,318 --> 03:04:44,959 forms of data because humans can't 4184 03:04:42,719 --> 03:04:47,119 really handle these ones and zeros well 4185 03:04:44,959 --> 03:04:50,398 and we do the transformation between 4186 03:04:47,120 --> 03:04:52,640 these two representations of data using 4187 03:04:50,398 --> 03:04:55,199 number systems we have a few number 4188 03:04:52,639 --> 03:04:58,079 systems available to us that we can use 4189 03:04:55,200 --> 03:05:00,240 binary octal hexadecimal are a few 4190 03:04:58,079 --> 03:05:02,719 examples and we're going to learn about 4191 03:05:00,239 --> 03:05:04,719 them starting in the next lecture so go 4192 03:05:02,719 --> 03:05:06,639 ahead and finish up here and meet me 4193 03:05:04,719 --> 03:05:09,599 there in this lecture we're going to 4194 03:05:06,639 --> 03:05:11,840 learn a little more about number systems 4195 03:05:09,600 --> 03:05:14,079 number systems allow us to transform 4196 03:05:11,840 --> 03:05:16,719 data from the form that is really 4197 03:05:14,079 --> 03:05:19,200 convenient for humans in a form that is 4198 03:05:16,719 --> 03:05:21,358 convenient for computers in these ones 4199 03:05:19,200 --> 03:05:23,680 and zeros that you see on the left we 4200 03:05:21,359 --> 03:05:26,159 can also use them to do the reverse go 4201 03:05:23,680 --> 03:05:28,559 from ones and zeros and go in the format 4202 03:05:26,159 --> 03:05:31,600 that is convenient to humans like this 4203 03:05:28,559 --> 03:05:33,920 22 or this steve name here to make sense 4204 03:05:31,600 --> 03:05:36,559 of how these number systems work let's 4205 03:05:33,920 --> 03:05:38,879 analyze the decimal system that we 4206 03:05:36,559 --> 03:05:41,278 really use in daily life and that is 4207 03:05:38,879 --> 03:05:43,079 usually called base 10. so if you look 4208 03:05:43,079 --> 03:05:48,719 2371 number here we can really split it 4209 03:05:46,478 --> 03:05:51,199 in the form that you see to the right 4210 03:05:48,719 --> 03:05:53,599 here if you try and take this expression 4211 03:05:51,200 --> 03:05:55,359 we have on the right here and evaluate 4212 03:05:53,600 --> 03:05:58,079 it you're going to get this number here 4213 03:05:55,359 --> 03:06:01,200 2 times 10 to the power of 3 this 4214 03:05:58,079 --> 03:06:03,760 evaluates to 2000 and this one here is 4215 03:06:01,200 --> 03:06:06,560 going to evaluate to 300 this one is 4216 03:06:03,760 --> 03:06:08,960 going to evaluate to 70 and the last one 4217 03:06:06,559 --> 03:06:11,199 is going to evaluate to one and if you 4218 03:06:08,959 --> 03:06:12,959 add these things up you're going to end 4219 03:06:11,200 --> 03:06:16,159 up with this number so this is how we 4220 03:06:12,959 --> 03:06:18,318 can take a number and uh make up a base 4221 03:06:16,159 --> 03:06:22,318 10 representation of it we can do the 4222 03:06:18,318 --> 03:06:25,119 same thing for 924 here or 47 here it 4223 03:06:22,318 --> 03:06:27,519 really is basic math you don't need to 4224 03:06:25,120 --> 03:06:30,079 have a lot of background in math to make 4225 03:06:27,520 --> 03:06:33,359 sense of this so we're going to use this 4226 03:06:30,079 --> 03:06:35,120 exact form and understand binary numbers 4227 03:06:33,359 --> 03:06:37,760 if you look at what we have here we have 4228 03:06:35,120 --> 03:06:40,240 a few binary numbers that are made up of 4229 03:06:37,760 --> 03:06:42,478 ones and zeros that's why it's called 4230 03:06:40,239 --> 03:06:46,079 binary because you can only have two 4231 03:06:42,478 --> 03:06:48,959 states or zero or one in your numbers so 4232 03:06:46,079 --> 03:06:51,760 we can really do the same thing we are 4233 03:06:48,959 --> 03:06:53,919 in base two so instead of using a ten 4234 03:06:51,760 --> 03:06:55,920 here we're going to be using a two but 4235 03:06:53,920 --> 03:06:58,639 it is the same operations we really did 4236 03:06:55,920 --> 03:07:00,478 if you take this and multiply and add 4237 03:06:58,639 --> 03:07:03,920 these things up what you're going to end 4238 03:07:00,478 --> 03:07:05,840 up is a base 10 representation of this 4239 03:07:03,920 --> 03:07:08,239 number if we go in the middle here we 4240 03:07:05,840 --> 03:07:10,799 have something we can easily do without 4241 03:07:08,239 --> 03:07:13,359 using a calculator the expression on the 4242 03:07:10,799 --> 03:07:15,358 left here is going to evaluate to 4243 03:07:13,359 --> 03:07:18,000 zero because we are multiplying with 4244 03:07:15,359 --> 03:07:20,159 zero the second one is going to be a two 4245 03:07:18,000 --> 03:07:22,478 because we are taking one and 4246 03:07:20,159 --> 03:07:24,478 multiplying this by two the others are 4247 03:07:22,478 --> 03:07:27,920 going to be zeros but the last one is 4248 03:07:24,478 --> 03:07:30,639 going to be 2 to the power of 4 which is 4249 03:07:27,920 --> 03:07:33,760 16 and we're going to add a 2 and this 4250 03:07:30,639 --> 03:07:36,159 is going to evaluate to 18. we can 4251 03:07:33,760 --> 03:07:38,800 actually bring up our calculator on 4252 03:07:36,159 --> 03:07:40,719 windows 10 i think it can help in this a 4253 03:07:38,799 --> 03:07:44,159 little bit we're going to bring it up 4254 03:07:40,719 --> 03:07:46,478 and we want to put it in a programmer 4255 03:07:44,159 --> 03:07:48,879 mode and we're going to go back on our 4256 03:07:46,478 --> 03:07:51,199 slide and find our number which is one 4257 03:07:48,879 --> 03:07:55,119 zero zero one zero so we're going to 4258 03:07:51,200 --> 03:07:58,079 type it out one zero zero one zero in 4259 03:07:55,120 --> 03:08:01,760 binary this is binary that we have typed 4260 03:07:58,079 --> 03:08:04,398 here so let's clear this and do one zero 4261 03:08:01,760 --> 03:08:06,960 zero one zero and you're going to see 4262 03:08:04,398 --> 03:08:10,000 that in the decimal it is 18 which is 4263 03:08:06,959 --> 03:08:12,239 what we came up by computing this in our 4264 03:08:10,000 --> 03:08:14,478 brain let's try the one we had before 4265 03:08:12,239 --> 03:08:16,398 that we couldn't really do in our brain 4266 03:08:14,478 --> 03:08:18,799 we're going to say one zero zero one 4267 03:08:16,398 --> 03:08:22,639 zero one let's do that we're going to 4268 03:08:18,799 --> 03:08:26,159 clear one zero zero one zero one this is 4269 03:08:22,639 --> 03:08:28,318 going to evaluate to 37 in decimal and 4270 03:08:26,159 --> 03:08:30,478 you can use calculators like this to 4271 03:08:28,318 --> 03:08:33,199 really do the transformations between 4272 03:08:30,478 --> 03:08:35,438 these different number representations 4273 03:08:33,200 --> 03:08:38,240 okay down below we have a one one one 4274 03:08:35,439 --> 03:08:40,880 which is going to evaluate to a seven in 4275 03:08:38,239 --> 03:08:43,119 base ten okay if we have three digits we 4276 03:08:40,879 --> 03:08:45,759 can really represent numbers between 4277 03:08:43,120 --> 03:08:48,000 zero and seven and you see a table here 4278 03:08:45,760 --> 03:08:50,000 that is going to prove that zero zero 4279 03:08:48,000 --> 03:08:52,398 zero is going to evaluate to zero in 4280 03:08:50,000 --> 03:08:55,279 decimal zero zero one is going to be a 4281 03:08:52,398 --> 03:08:57,920 one and if you go down to one one one 4282 03:08:55,279 --> 03:09:00,239 that is going to evaluate to a seven you 4283 03:08:57,920 --> 03:09:02,478 can try and use a calculator to prove 4284 03:09:00,239 --> 03:09:05,079 this and really understand how this 4285 03:09:02,478 --> 03:09:07,438 works if we use the three digits 4286 03:09:05,079 --> 03:09:09,279 representation our numbers are going to 4287 03:09:07,439 --> 03:09:11,520 look something like this in memory we're 4288 03:09:09,279 --> 03:09:14,399 going to take the bits in memory and 4289 03:09:11,520 --> 03:09:16,239 group them in groups of three and we are 4290 03:09:14,398 --> 03:09:18,318 going to use binary to actually make 4291 03:09:16,239 --> 03:09:20,559 sense of these numbers for example here 4292 03:09:18,318 --> 03:09:22,478 on the left this is going to be a one 4293 03:09:20,559 --> 03:09:25,119 the second one from the left is going to 4294 03:09:22,478 --> 03:09:27,840 be a six and this is going to be a six 4295 03:09:25,120 --> 03:09:30,640 as well this is going to be a zero this 4296 03:09:27,840 --> 03:09:33,359 is going to be a three because we have 4297 03:09:30,639 --> 03:09:35,199 one and two here we add those up we get 4298 03:09:33,359 --> 03:09:37,200 a three if this doesn't make sense you 4299 03:09:35,200 --> 03:09:39,680 can try and use a calculator you're 4300 03:09:37,200 --> 03:09:42,800 going to see that it makes sense okay so 4301 03:09:39,680 --> 03:09:45,200 what if we want to group our digits in 4302 03:09:42,799 --> 03:09:47,119 groups of four we can do that and we're 4303 03:09:45,200 --> 03:09:49,600 going to end up with something like this 4304 03:09:47,120 --> 03:09:52,720 the values we can represent are going to 4305 03:09:49,600 --> 03:09:55,760 go from 0 all the way to 15 and you can 4306 03:09:52,719 --> 03:09:58,318 try and prove this using the calculator 4307 03:09:55,760 --> 03:10:00,318 like we just did and it is going to look 4308 03:09:58,318 --> 03:10:03,359 like this in memory so what we really 4309 03:10:00,318 --> 03:10:06,639 are doing is grouping the bits in 4310 03:10:03,359 --> 03:10:09,120 different sizes of data and we have a 4311 03:10:06,639 --> 03:10:11,760 kind of formula that we can use to make 4312 03:10:09,120 --> 03:10:14,239 sense of the data in memory for example 4313 03:10:11,760 --> 03:10:16,478 on the left here we have zero one one 4314 03:10:14,238 --> 03:10:20,000 zero if we go back to our table and 4315 03:10:16,478 --> 03:10:21,840 defined zero one one zero this is a six 4316 03:10:20,000 --> 03:10:24,799 and we're going to know that these four 4317 03:10:21,840 --> 03:10:27,120 bits here are really storing s6 and 4318 03:10:24,799 --> 03:10:29,358 there are ways you can manipulate this 4319 03:10:27,120 --> 03:10:31,920 in your c plus plus program we're going 4320 03:10:29,359 --> 03:10:33,359 to see that you can also use five digits 4321 03:10:31,920 --> 03:10:35,439 and you're going to end up with 4322 03:10:33,359 --> 03:10:38,720 something like this and the numbers are 4323 03:10:35,439 --> 03:10:40,880 going to go from 0 all the way to 32 and 4324 03:10:38,719 --> 03:10:43,760 this is how it's going to look in memory 4325 03:10:40,879 --> 03:10:46,000 you can try and come up with what this 4326 03:10:43,760 --> 03:10:48,318 represents in decimal i'm not going to 4327 03:10:46,000 --> 03:10:51,200 tell you this but we can try and figure 4328 03:10:48,318 --> 03:10:54,799 out the one on the left here so it is 0 4329 03:10:51,200 --> 03:10:59,439 1 1 0 1. if we go back we're going to 4330 03:10:54,799 --> 03:11:02,799 come up to 0 1 1 0 1 which is a 13 here 4331 03:10:59,439 --> 03:11:06,079 so this memory location these five bits 4332 03:11:02,799 --> 03:11:08,879 represent a 17 number so if we want to 4333 03:11:06,079 --> 03:11:11,760 kind of generalize what we have talked 4334 03:11:08,879 --> 03:11:14,000 about in the last slides if you have one 4335 03:11:11,760 --> 03:11:16,398 digit you can really represent two 4336 03:11:14,000 --> 03:11:18,478 states zero and one if you have three 4337 03:11:16,398 --> 03:11:20,639 digits you can represent numbers between 4338 03:11:18,478 --> 03:11:24,079 zero and seven and you can go all the 4339 03:11:20,639 --> 03:11:27,039 way down and if we generalize this in a 4340 03:11:24,079 --> 03:11:29,120 mathematical formula we can say that if 4341 03:11:27,040 --> 03:11:31,760 we have n digits we're going to be able 4342 03:11:29,120 --> 03:11:34,720 to represent between 0 and 2 to the 4343 03:11:31,760 --> 03:11:36,880 power of n minus 1. and if you try to 4344 03:11:34,719 --> 03:11:39,199 plug in any number of digits you're 4345 03:11:36,879 --> 03:11:43,278 going to see that this formula works 4346 03:11:39,200 --> 03:11:45,680 okay so these are a few grouping of bits 4347 03:11:43,279 --> 03:11:47,840 we have in practice we can use eight 4348 03:11:45,680 --> 03:11:51,359 digits and we can represent numbers 4349 03:11:47,840 --> 03:11:54,159 between zero and two 255 if we have 16 4350 03:11:51,359 --> 03:11:56,000 bits we can represent this range if we 4351 03:11:56,000 --> 03:12:02,318 represent this range if we have 64 bets 4352 03:11:59,520 --> 03:12:04,800 we can really represent a really huge 4353 03:12:02,318 --> 03:12:06,719 range i can't even say this number it's 4354 03:12:04,799 --> 03:12:09,599 really bad okay now you should have a 4355 03:12:06,719 --> 03:12:12,719 pretty good idea of the binary system 4356 03:12:09,600 --> 03:12:16,079 and different representations we can use 4357 03:12:12,719 --> 03:12:18,159 to make sense of binary numbers in a 4358 03:12:16,079 --> 03:12:21,439 computer's memory the last thing i want 4359 03:12:18,159 --> 03:12:24,398 you to see is what we call hexadecimal 4360 03:12:21,439 --> 03:12:27,439 number system and it is a technique we 4361 03:12:24,398 --> 03:12:30,238 use to shorten the length of how we 4362 03:12:27,439 --> 03:12:32,960 represent a binary number in memory how 4363 03:12:30,238 --> 03:12:36,398 does it work well we take our number and 4364 03:12:32,959 --> 03:12:38,879 split it in groups of four bets and each 4365 03:12:36,398 --> 03:12:41,199 group is assigned a symbol we can use to 4366 03:12:38,879 --> 03:12:43,119 represent it if we have four zeros 4367 03:12:43,120 --> 03:12:48,560 hexadecimal if we have zero zero zero 4368 03:12:46,000 --> 03:12:51,279 one we're going to represent a one and 4369 03:12:48,559 --> 03:12:53,920 you can see that if we go up to nine 4370 03:12:51,279 --> 03:12:56,159 this is really similar to what we have 4371 03:12:53,920 --> 03:12:58,159 in the decimal number system but once we 4372 03:12:56,159 --> 03:13:00,959 hit 10 we're going to represent that 4373 03:12:58,159 --> 03:13:04,000 with an a in hexadecimal 11 is going to 4374 03:13:00,959 --> 03:13:07,039 be b 12 is going to be c 13 is going to 4375 03:13:04,000 --> 03:13:09,040 be d 14 is going to be e and 15 is going 4376 03:13:07,040 --> 03:13:11,680 to be f so how do we use this to 4377 03:13:09,040 --> 03:13:14,080 represent binary numbers let's see an 4378 03:13:11,680 --> 03:13:16,159 example here we have a huge number that 4379 03:13:14,079 --> 03:13:18,559 we represent in memory it starts with 4380 03:13:16,159 --> 03:13:20,159 zero and ends with the one here so the 4381 03:13:18,559 --> 03:13:23,278 first thing we're going to do is to 4382 03:13:20,159 --> 03:13:25,760 split this in groups of four and the one 4383 03:13:23,279 --> 03:13:28,000 on the left is going to be a one one one 4384 03:13:25,760 --> 03:13:30,478 one if we come back to our table here 4385 03:13:28,000 --> 03:13:32,879 one one one one means f so we're going 4386 03:13:30,478 --> 03:13:35,840 to represent this with an f if you go to 4387 03:13:32,879 --> 03:13:39,039 the second group of four bets we have 4388 03:13:35,840 --> 03:13:42,000 zero zero one one if we go back zero 4389 03:13:39,040 --> 03:13:44,319 zero one one means three so we're going 4390 03:13:42,000 --> 03:13:46,318 to have a three here i hope this makes 4391 03:13:44,318 --> 03:13:48,398 sense and we're going to keep using the 4392 03:13:46,318 --> 03:13:52,318 same transformations and we're going to 4393 03:13:48,398 --> 03:13:55,519 come up with one f 0 3 e 6 and we can 4394 03:13:52,318 --> 03:13:57,519 represent this binary number using these 4395 03:13:55,520 --> 03:14:01,359 symbols you can see that we go from 4396 03:13:57,520 --> 03:14:03,920 using 32 bits to using one two three 4397 03:14:01,359 --> 03:14:06,238 four five six seven eight symbols to 4398 03:14:03,920 --> 03:14:09,200 represent this and this is shorter and 4399 03:14:06,238 --> 03:14:13,039 easily manipulable by humans and we use 4400 03:14:09,200 --> 03:14:15,120 a zero x in front to mean that this is a 4401 03:14:13,040 --> 03:14:18,160 hexadecimal number i really hope this 4402 03:14:15,120 --> 03:14:20,880 makes sense so if we have numbers that 4403 03:14:18,159 --> 03:14:23,200 don't really fit in groups of four we're 4404 03:14:20,879 --> 03:14:25,438 going to go from the left and split this 4405 03:14:23,200 --> 03:14:28,640 in groups of four and we're going to end 4406 03:14:25,439 --> 03:14:30,960 up with a group on the left that doesn't 4407 03:14:28,639 --> 03:14:34,159 really meet our requirements and what we 4408 03:14:30,959 --> 03:14:36,478 can do is add the missing bits and make 4409 03:14:34,159 --> 03:14:38,639 them zeros and we call this operation 4410 03:14:36,478 --> 03:14:40,559 padding this is what you can do to 4411 03:14:38,639 --> 03:14:42,959 really represent any number in 4412 03:14:40,559 --> 03:14:45,278 hexadecimal if you don't have enough 4413 03:14:42,959 --> 03:14:47,839 bits on the left we also have another 4414 03:14:45,279 --> 03:14:51,040 system that is really not widely used 4415 03:14:47,840 --> 03:14:53,680 today but i am mentioning it here for 4416 03:14:51,040 --> 03:14:56,000 completeness it is called octal system 4417 03:14:53,680 --> 03:14:59,439 it goes from zero all the way to seven 4418 03:14:56,000 --> 03:15:02,398 and we can divide our binary number into 4419 03:14:59,439 --> 03:15:04,639 groups of three and represent using 4420 03:15:02,398 --> 03:15:06,318 these zeros and ones to shorten the 4421 03:15:04,639 --> 03:15:07,920 length of the binary number we can 4422 03:15:06,318 --> 03:15:10,318 really do the same thing we did with 4423 03:15:07,920 --> 03:15:13,200 hexadecimal so we're going to take our 4424 03:15:10,318 --> 03:15:15,439 binary number and divide this in groups 4425 03:15:13,200 --> 03:15:19,120 of three the one on the left is going to 4426 03:15:15,439 --> 03:15:22,079 evaluate to a 7 because it's 1 1 1 and 4427 03:15:19,120 --> 03:15:23,920 one one one evaluates to seven in octal 4428 03:15:22,079 --> 03:15:26,238 system the second one is also going to 4429 03:15:23,920 --> 03:15:29,279 be a one one one the third one is going 4430 03:15:26,238 --> 03:15:31,680 to be one zero zero if we go in our 4431 03:15:29,279 --> 03:15:33,439 table it's going to be one zero zero 4432 03:15:31,680 --> 03:15:35,680 it's going to be a four and you see we 4433 03:15:33,439 --> 03:15:37,840 have a 4 here and you can do the same 4434 03:15:35,680 --> 03:15:39,760 things until you have figured out the 4435 03:15:37,840 --> 03:15:42,238 entire number here and you see on the 4436 03:15:39,760 --> 03:15:44,880 left we are doing padding and adding a 4437 03:15:42,238 --> 03:15:48,079 missing bit and making it to 0 so we are 4438 03:15:44,879 --> 03:15:51,519 coming up with 0 0 1 and if we go back 4439 03:15:48,079 --> 03:15:53,120 to our table here 0 0 1 is a 1 and 4440 03:15:51,520 --> 03:15:55,120 that's what we put here so we're going 4441 03:15:53,120 --> 03:15:59,680 to put this number here it's going to be 4442 03:15:55,120 --> 03:16:03,040 15 61 41 70 47 7 and we're going to add 4443 03:15:59,680 --> 03:16:05,760 a zero in front to mean that this is a 4444 03:16:03,040 --> 03:16:07,920 number represented in the octal system i 4445 03:16:05,760 --> 03:16:10,559 really hope this makes sense okay so we 4446 03:16:07,920 --> 03:16:12,799 have seen all these binary numbers and 4447 03:16:10,559 --> 03:16:15,840 different number systems we can use to 4448 03:16:12,799 --> 03:16:18,478 represent them but how do we use them in 4449 03:16:15,840 --> 03:16:20,559 a c plus plus program well i'm glad you 4450 03:16:18,478 --> 03:16:23,519 asked here is a simple example of how 4451 03:16:20,559 --> 03:16:26,559 you can represent the number 15 through 4452 03:16:23,520 --> 03:16:28,640 different number systems in c plus plus 4453 03:16:26,559 --> 03:16:31,600 if you just say 15 like this it's going 4454 03:16:28,639 --> 03:16:34,398 to be in decimal if you add a zero in 4455 03:16:31,600 --> 03:16:37,040 front it's going to mean that the number 4456 03:16:34,398 --> 03:16:40,159 is in octal system so this can be 4457 03:16:37,040 --> 03:16:43,439 confusing sometimes if you do 0 17 it's 4458 03:16:40,159 --> 03:16:45,520 not going to be 17. so if you say 0 17 c 4459 03:16:43,439 --> 03:16:48,238 plus plus is going to interpret this 4460 03:16:45,520 --> 03:16:50,800 number as if it is in octal system and 4461 03:16:48,238 --> 03:16:53,520 it is going to turn this into 15. down 4462 03:16:50,799 --> 03:16:56,318 here you see the same representation in 4463 03:16:53,520 --> 03:16:58,319 hexadecimal we saw that you use a 0x in 4464 03:16:56,318 --> 03:17:01,439 front to represent that it is in 4465 03:16:58,318 --> 03:17:03,039 hexadecimal and this is going to be 15 4466 03:17:03,040 --> 03:17:09,040 representation in binary using zeros and 4467 03:17:06,318 --> 03:17:11,039 ones and you use zero b in front like 4468 03:17:09,040 --> 03:17:14,239 this i have to say that this was only 4469 03:17:11,040 --> 03:17:17,120 possible since c plus plus 14. so if you 4470 03:17:14,238 --> 03:17:18,879 are using an older standard of c plus 4471 03:17:17,120 --> 03:17:21,040 plus you want to be able to represent 4472 03:17:18,879 --> 03:17:22,959 binary like this and if we run this 4473 03:17:21,040 --> 03:17:25,279 program it is going to print the same 4474 03:17:22,959 --> 03:17:27,679 thing it is going to print 15 but we 4475 03:17:25,279 --> 03:17:30,000 stored the number using different number 4476 03:17:27,680 --> 03:17:32,880 systems let's go to visual studio code 4477 03:17:30,000 --> 03:17:35,200 and try this out okay here we are in a 4478 03:17:32,879 --> 03:17:37,358 folder i have on my system where i am 4479 03:17:35,200 --> 03:17:39,520 going to be storing my projects for this 4480 03:17:37,359 --> 03:17:42,800 chapter the first thing i am going to do 4481 03:17:39,520 --> 03:17:45,359 is copy code from a template project i 4482 03:17:42,799 --> 03:17:48,639 am going to go in and copy things over 4483 03:17:45,359 --> 03:17:51,040 and i am going to put these in 4 2 4484 03:17:48,639 --> 03:17:53,278 number systems i am going to open this 4485 03:17:51,040 --> 03:17:55,920 folder in visual studio code so i am 4486 03:17:53,279 --> 03:17:58,238 going to go to file close the folder we 4487 03:17:55,920 --> 03:18:01,040 have now and i am going to open up a new 4488 03:17:58,238 --> 03:18:03,680 one open folder and i am going to browse 4489 03:18:01,040 --> 03:18:05,920 to the location of my project i am going 4490 03:18:03,680 --> 03:18:08,318 to open this project here that says 4491 03:18:05,920 --> 03:18:11,040 number systems we are going to get rid 4492 03:18:08,318 --> 03:18:13,359 of the welcome screen and open our main 4493 03:18:11,040 --> 03:18:16,239 cpp file we're going to get rid of this 4494 03:18:13,359 --> 03:18:19,279 constival thing and clear everything we 4495 03:18:16,238 --> 03:18:21,439 have in the main function here the first 4496 03:18:19,279 --> 03:18:23,680 thing we want to do is to store an 4497 03:18:21,439 --> 03:18:26,479 integer that is 15 and we're going to 4498 03:18:23,680 --> 03:18:28,238 say and number one the exact same thing 4499 03:18:26,478 --> 03:18:31,039 we had in the slides and we're going to 4500 03:18:28,238 --> 03:18:33,680 store 15 in this variable here this is 4501 03:18:31,040 --> 03:18:36,319 going to be represented in decimal and 4502 03:18:33,680 --> 03:18:39,840 if we want to store the semi number in 4503 03:18:36,318 --> 03:18:42,959 octo we saw that in our slides we could 4504 03:18:39,840 --> 03:18:45,520 store this as 0 17. so let's do that 4505 03:18:42,959 --> 03:18:48,959 we're going to say it number 2 and we're 4506 03:18:45,520 --> 03:18:51,279 going to store in 0 17. this is not 4507 03:18:48,959 --> 03:18:54,238 going to be 17 this is going to be a 4508 03:18:51,279 --> 03:18:56,238 representation of a number in octo 4509 03:18:54,238 --> 03:18:58,879 system so we're going to say that this 4510 03:18:56,238 --> 03:19:01,600 is octo and we can represent the number 4511 03:18:58,879 --> 03:19:04,398 in hexadecimal as well so we're going to 4512 03:19:01,600 --> 03:19:05,120 say it number three and it is going to 4513 03:19:05,120 --> 03:19:10,640 0x because it is hexadecimal and we're 4514 03:19:08,159 --> 03:19:12,879 going to say zero f this is how we 4515 03:19:10,639 --> 03:19:15,039 represent it you can use lowercase or 4516 03:19:12,879 --> 03:19:17,519 uppercase it won't really matter let's 4517 03:19:15,040 --> 03:19:19,760 use uppercase here and we can use binary 4518 03:19:17,520 --> 03:19:22,000 because we are using c plus plus 20. so 4519 03:19:19,760 --> 03:19:24,398 we're going to say and number four and 4520 03:19:22,000 --> 03:19:28,159 we're going to store in a representation 4521 03:19:24,398 --> 03:19:31,840 of 15 in binary and it happens to be 4522 03:19:28,159 --> 03:19:35,279 four zeros and four ones and we have to 4523 03:19:31,840 --> 03:19:37,840 prefix this with a zero b this is how it 4524 03:19:35,279 --> 03:19:40,238 works so this is going to be in binary 4525 03:19:37,840 --> 03:19:42,639 and if we print all these numbers up 4526 03:19:40,238 --> 03:19:44,799 we're going to see let me not tell you 4527 03:19:42,639 --> 03:19:47,920 this what do you think we'll see okay so 4528 03:19:44,799 --> 03:19:50,398 i went ahead and typed this out what do 4529 03:19:47,920 --> 03:19:52,398 you think we will see when we build and 4530 03:19:50,398 --> 03:19:54,639 run this program before we do that we're 4531 03:19:52,398 --> 03:19:56,398 going to open a terminal so that we see 4532 03:19:54,639 --> 03:19:58,639 it here and we're going to go to 4533 03:19:56,398 --> 03:20:01,439 terminal run task we're going to build 4534 03:19:58,639 --> 03:20:03,599 with gcc let's wait and see if the world 4535 03:20:01,439 --> 03:20:05,840 is successful world finished 4536 03:20:03,600 --> 03:20:07,520 successfully we have a rooster program 4537 03:20:07,520 --> 03:20:12,079 click somewhere in this terminal and our 4538 03:20:10,238 --> 03:20:14,079 terminal is going to show up if we do 4539 03:20:12,079 --> 03:20:16,559 dur you're going to see that our rooster 4540 03:20:14,079 --> 03:20:18,318 program is here and if we run it i want 4541 03:20:16,559 --> 03:20:21,119 you to take a moment and guess what 4542 03:20:18,318 --> 03:20:24,159 we're going to see hmm number one 15 4543 03:20:21,120 --> 03:20:26,560 number 215 in number 315 and number four 4544 03:20:24,159 --> 03:20:28,799 15. this is really the same number we 4545 03:20:26,559 --> 03:20:31,680 are storing in these variables but we 4546 03:20:28,799 --> 03:20:33,840 are using a different representation 4547 03:20:31,680 --> 03:20:36,159 that is going to be convenient depending 4548 03:20:33,840 --> 03:20:37,920 on whatever it is we are doing if it is 4549 03:20:36,159 --> 03:20:39,920 more convenient to use binary we are 4550 03:20:37,920 --> 03:20:42,559 going to use binary if it is more 4551 03:20:39,920 --> 03:20:45,359 convenient use hexadecimal please do use 4552 03:20:42,559 --> 03:20:48,318 hexadecimal but the representation of 4553 03:20:45,359 --> 03:20:50,079 this number in memory is really going to 4554 03:20:48,318 --> 03:20:52,559 be the same i really want you to take 4555 03:20:50,079 --> 03:20:54,398 some time and let this sink in because 4556 03:20:52,559 --> 03:20:57,278 you're going to see these things a lot 4557 03:20:54,398 --> 03:21:00,159 of times in your career as a software 4558 03:20:57,279 --> 03:21:02,319 developer so please do yourself a favor 4559 03:21:00,159 --> 03:21:04,159 and make sure you really understand this 4560 03:21:02,318 --> 03:21:06,238 okay this is really all we set up to do 4561 03:21:04,159 --> 03:21:08,719 in this lecture trying to make sense of 4562 03:21:06,238 --> 03:21:10,959 the binary numbers we have in memory and 4563 03:21:08,719 --> 03:21:13,840 how we transform between them and 4564 03:21:10,959 --> 03:21:16,559 different forms of data that is easily 4565 03:21:13,840 --> 03:21:18,880 manipulable by humans all data is 4566 03:21:16,559 --> 03:21:21,359 represented by a bunch of grouped cells 4567 03:21:18,879 --> 03:21:23,438 of zeros and ones we have seen this as 4568 03:21:21,359 --> 03:21:25,040 the range of your data grows so will be 4569 03:21:23,439 --> 03:21:27,040 the number of digits you need to 4570 03:21:25,040 --> 03:21:29,520 represent the data in memory we have 4571 03:21:27,040 --> 03:21:31,760 seen that the hexadecimal system makes 4572 03:21:29,520 --> 03:21:34,238 it a little easier for humans to handle 4573 03:21:31,760 --> 03:21:36,800 streams of data in ones and zeros octo 4574 03:21:34,238 --> 03:21:39,199 has the same goal as hexadecimal but 4575 03:21:36,799 --> 03:21:41,759 it's almost no longer used in modern 4576 03:21:39,200 --> 03:21:44,000 times i just mentioned it here for 4577 03:21:41,760 --> 03:21:46,079 completeness we are going to stop here 4578 03:21:44,000 --> 03:21:48,159 in this lecture in the next one we're 4579 03:21:46,079 --> 03:21:50,559 going to look at integers in a little 4580 03:21:48,159 --> 03:21:53,200 more detail so go ahead and finish up 4581 03:21:50,559 --> 03:21:55,199 here and meet me there in this lecture 4582 03:21:53,200 --> 03:21:58,079 we're going to learn a little more about 4583 03:21:55,200 --> 03:22:00,960 integers in c plus plus integers are 4584 03:21:58,079 --> 03:22:03,600 represented by ant and z plus plus it's 4585 03:22:00,959 --> 03:22:06,238 what you see on the left here and they 4586 03:22:03,600 --> 03:22:09,680 are used to store whole numbers things 4587 03:22:06,238 --> 03:22:12,318 like 1 2 and 3. they typically occupy 4 4588 03:22:09,680 --> 03:22:14,800 bytes or more in memory but in most 4589 03:22:12,318 --> 03:22:17,039 times it's going to be 4 bytes so for 4590 03:22:14,799 --> 03:22:19,679 example if you have a variable called 4591 03:22:17,040 --> 03:22:21,439 age and it is an integer this is how 4592 03:22:19,680 --> 03:22:23,680 it's most probably going to look in 4593 03:22:21,439 --> 03:22:25,840 memory it's going to take four bytes and 4594 03:22:23,680 --> 03:22:28,559 i really want to make it clear what a 4595 03:22:25,840 --> 03:22:31,359 variable is it is a named piece of 4596 03:22:28,559 --> 03:22:34,318 memory that you use to store specific 4597 03:22:31,359 --> 03:22:37,520 types of data okay so here if we have a 4598 03:22:34,318 --> 03:22:39,840 variable named age it is a named piece 4599 03:22:37,520 --> 03:22:42,399 of memory edge is going to be the name 4600 03:22:39,840 --> 03:22:44,559 of that piece of memory and we can store 4601 03:22:42,398 --> 03:22:46,559 data in that memory it's going to look 4602 03:22:44,559 --> 03:22:48,879 like this in memory but the value is 4603 03:22:46,559 --> 03:22:51,039 going to be stored in here in binary 4604 03:22:48,879 --> 03:22:54,079 format i really hope this makes sense a 4605 03:22:51,040 --> 03:22:56,399 variable is a named piece of memory that 4606 03:22:54,079 --> 03:22:58,478 we can use to store different kinds of 4607 03:22:56,398 --> 03:23:01,199 data and okay now we're going to look at 4608 03:22:58,478 --> 03:23:04,318 a few examples of how you can work with 4609 03:23:01,200 --> 03:23:07,040 integers in your c plus program you can 4610 03:23:04,318 --> 03:23:09,119 define a few variables here we have one 4611 03:23:07,040 --> 03:23:11,840 that is going to store the number of 4612 03:23:09,120 --> 03:23:14,960 elephants we have you can declare it 4613 03:23:11,840 --> 03:23:17,279 like this and not put in a value and if 4614 03:23:14,959 --> 03:23:19,759 you do it like this the compiler is just 4615 03:23:17,279 --> 03:23:21,920 going to put in some garbage value that 4616 03:23:19,760 --> 03:23:24,800 is really not meaningful for your 4617 03:23:21,920 --> 03:23:27,040 program so you see the warning we have 4618 03:23:24,799 --> 03:23:29,599 here as a comment the variable may 4619 03:23:27,040 --> 03:23:31,359 contain random garbage value and this is 4620 03:23:29,600 --> 03:23:33,760 something you need to be aware of 4621 03:23:31,359 --> 03:23:36,159 another way we can define integers in 4622 03:23:33,760 --> 03:23:38,719 our c plus plus program is this 4623 03:23:36,159 --> 03:23:40,478 statement here that declares a variable 4624 03:23:38,719 --> 03:23:43,119 called lion count it's going to 4625 03:23:40,478 --> 03:23:44,799 initialize to zero if we use curly 4626 03:23:43,120 --> 03:23:47,200 braces like this it's going to 4627 03:23:44,799 --> 03:23:49,679 initialize to 0 by default we can also 4628 03:23:47,200 --> 03:23:52,560 explicitly put in a value we can do the 4629 03:23:49,680 --> 03:23:55,520 same with cat count and we can use these 4630 03:23:52,559 --> 03:23:57,760 variables to do operations for example 4631 03:23:55,520 --> 03:24:00,079 we can declare another variable and it 4632 03:23:57,760 --> 03:24:02,398 is going to store the number of 4633 03:24:00,079 --> 03:24:05,680 domesticated animals and we're going to 4634 03:24:02,398 --> 03:24:07,760 add up the numbers for dogs and cats and 4635 03:24:05,680 --> 03:24:09,920 we're going to store the result of that 4636 03:24:07,760 --> 03:24:11,760 in our variable that is called 4637 03:24:09,920 --> 03:24:14,000 domesticated animals this is something 4638 03:24:11,760 --> 03:24:16,800 you can do another thing that can be 4639 03:24:14,000 --> 03:24:19,680 problematic is trying to use variables 4640 03:24:16,799 --> 03:24:21,599 that you haven't defined or declared in 4641 03:24:19,680 --> 03:24:23,520 your program that is going to give you a 4642 03:24:21,600 --> 03:24:25,439 compiler error and the compiler is 4643 03:24:23,520 --> 03:24:27,680 basically going to say i don't know what 4644 03:24:25,439 --> 03:24:30,398 doesn't exist one here is i don't know 4645 03:24:27,680 --> 03:24:33,040 what this doesn't exist to is it is not 4646 03:24:30,398 --> 03:24:36,559 defined anywhere in the program another 4647 03:24:33,040 --> 03:24:39,120 thing you can do is try and store a 4648 03:24:36,559 --> 03:24:41,278 fractional number in a variable that you 4649 03:24:39,120 --> 03:24:43,840 declared as an integer and this is going 4650 03:24:41,279 --> 03:24:47,359 to give you a warning because fractional 4651 03:24:43,840 --> 03:24:49,760 numbers are usually bigger in size than 4652 03:24:47,359 --> 03:24:51,840 integers and this is going to lose some 4653 03:24:49,760 --> 03:24:54,079 of your data we don't really have enough 4654 03:24:51,840 --> 03:24:55,520 tools to understand this now but we are 4655 03:24:54,079 --> 03:24:57,039 going to understand this a little bit 4656 03:24:55,520 --> 03:24:58,880 later in the course but this is 4657 03:24:57,040 --> 03:25:01,600 something you can do you can take a 4658 03:24:58,879 --> 03:25:03,839 fractional number and store that in an 4659 03:25:01,600 --> 03:25:06,720 integer variable and we're going to do 4660 03:25:03,840 --> 03:25:10,000 an example and see how this behaves with 4661 03:25:06,719 --> 03:25:12,959 our compiler that is gcc10 okay here we 4662 03:25:10,000 --> 03:25:15,520 have mostly used curly braces to 4663 03:25:12,959 --> 03:25:18,079 initialize our variables but we can also 4664 03:25:15,520 --> 03:25:20,238 use parenthesis and this is going to be 4665 03:25:20,238 --> 03:25:25,279 as functional variable initialization 4666 03:25:23,279 --> 03:25:26,880 because we are using parenthesis and 4667 03:25:25,279 --> 03:25:29,520 this is mostly going to do the same 4668 03:25:26,879 --> 03:25:33,039 thing we saw in the last slide with 4669 03:25:29,520 --> 03:25:35,200 curly braces but when we come to the 4670 03:25:33,040 --> 03:25:38,000 example here where we are storing a 4671 03:25:35,200 --> 03:25:41,040 fractional number this is going to 4672 03:25:38,000 --> 03:25:43,680 silently chop off the data you have in a 4673 03:25:41,040 --> 03:25:45,680 fractional number and turn that into an 4674 03:25:43,680 --> 03:25:47,680 integer and this is called implicit 4675 03:25:45,680 --> 03:25:50,639 conversions but if we go to the last 4676 03:25:47,680 --> 03:25:52,639 example here where we are storing 2.9 4677 03:25:50,639 --> 03:25:54,799 into an integer this is going to take 4678 03:25:52,639 --> 03:25:57,680 this fractional number chop off the 4679 03:25:54,799 --> 03:25:59,519 decimal point and only store 2 in this 4680 03:25:57,680 --> 03:26:02,079 integer and this might not be what you 4681 03:25:59,520 --> 03:26:04,479 want and it is going to happen silently 4682 03:26:02,079 --> 03:26:06,559 so this is a really bad way to do this 4683 03:26:04,478 --> 03:26:09,840 if you want to do something like this it 4684 03:26:06,559 --> 03:26:12,159 is preferable to use curly braces 4685 03:26:09,840 --> 03:26:14,079 because you are either going to get a 4686 03:26:12,159 --> 03:26:16,318 warning that this is going to lose you 4687 03:26:14,079 --> 03:26:18,079 data or you're going to get a compiler 4688 03:26:16,318 --> 03:26:20,478 error and the compiler is going to 4689 03:26:18,079 --> 03:26:22,959 completely stop you from doing this and 4690 03:26:20,478 --> 03:26:25,199 again if any of this doesn't make any 4691 03:26:22,959 --> 03:26:27,278 sense we're going to go in visual studio 4692 03:26:25,200 --> 03:26:29,840 code play with us and see how the 4693 03:26:27,279 --> 03:26:32,479 compiler behaves with these things we 4694 03:26:29,840 --> 03:26:34,639 have another way we can initialize our 4695 03:26:32,478 --> 03:26:36,959 variables in surplus plus we can use 4696 03:26:34,639 --> 03:26:39,840 assignment and we have done this a 4697 03:26:36,959 --> 03:26:41,679 couple of times in the course but this 4698 03:26:39,840 --> 03:26:44,159 is something you can do and we are going 4699 03:26:41,680 --> 03:26:45,920 to play with this in visual studio code 4700 03:26:44,159 --> 03:26:48,478 in a minute and you're going to see how 4701 03:26:45,920 --> 03:26:50,799 it works okay by now we have seen three 4702 03:26:48,478 --> 03:26:53,039 ways we can initialize our integer 4703 03:26:53,040 --> 03:26:58,000 initialization which is basically using 4704 03:26:55,439 --> 03:27:01,279 curly braces we can use functional 4705 03:26:58,000 --> 03:27:03,200 notation which is using the parenthesis 4706 03:27:01,279 --> 03:27:05,520 and we can use the assignment 4707 03:27:03,200 --> 03:27:07,600 initialization which is what we have 4708 03:27:05,520 --> 03:27:09,920 here another thing i really want you to 4709 03:27:09,920 --> 03:27:16,159 to query for the size of your variable 4710 03:27:13,279 --> 03:27:17,920 here we are trying to print the size an 4711 03:27:16,159 --> 03:27:20,318 integer is going to take on our 4712 03:27:17,920 --> 03:27:22,799 operating system and compiler and if we 4713 03:27:20,318 --> 03:27:25,119 try to run this on our compiler it's 4714 03:27:22,799 --> 03:27:28,719 going to tell us exactly how much memory 4715 03:27:25,120 --> 03:27:31,840 an integer occupies in our program we 4716 03:27:28,719 --> 03:27:34,000 can either pass and explicitly like this 4717 03:27:31,840 --> 03:27:36,559 or we can pass a variable name and it is 4718 03:27:34,000 --> 03:27:38,559 going to work equally well okay ants are 4719 03:27:36,559 --> 03:27:40,600 mostly going to take up four bytes in 4720 03:27:40,600 --> 03:27:45,760 representation of three ants in memory 4721 03:27:43,680 --> 03:27:48,000 if we go from this zero to this zero 4722 03:27:45,760 --> 03:27:50,478 this is going to be exactly four bytes 4723 03:27:48,000 --> 03:27:52,478 and this is how it may look in memory 4724 03:27:50,478 --> 03:27:55,358 one other thing i haven't really talked 4725 03:27:52,478 --> 03:27:57,920 about on purpose is that there are a few 4726 03:27:55,359 --> 03:28:00,479 rules you have to follow to name your 4727 03:27:57,920 --> 03:28:03,359 variables in c plus plus one of those 4728 03:28:00,478 --> 03:28:06,799 rules is that a variable must start with 4729 03:28:03,359 --> 03:28:08,880 a letter like a b c d up to z or an 4730 03:28:06,799 --> 03:28:10,719 underscore you can't start a variable 4731 03:28:08,879 --> 03:28:13,119 for example with a number that's going 4732 03:28:10,719 --> 03:28:15,760 to be a problem after the first 4733 03:28:13,120 --> 03:28:17,840 character in your variable name you can 4734 03:28:15,760 --> 03:28:20,238 really put in anything you can put in 4735 03:28:17,840 --> 03:28:22,079 numbers you can put in letters you can 4736 03:28:20,238 --> 03:28:24,478 really put anything you want but the 4737 03:28:22,079 --> 03:28:27,600 first one can't be a number variable 4738 03:28:24,478 --> 03:28:30,159 names are also case sensitive so if you 4739 03:28:27,600 --> 03:28:31,920 have a variable called bike account with 4740 03:28:31,920 --> 03:28:36,478 and you have another one that starts 4741 03:28:36,478 --> 03:28:40,639 these are going to be two different 4742 03:28:38,079 --> 03:28:44,000 variables and uh you have to be aware of 4743 03:28:40,639 --> 03:28:46,559 this no spaces or spatial characters are 4744 03:28:44,000 --> 03:28:48,799 allowed in your variables you can use 4745 03:28:46,559 --> 03:28:50,639 underscores but you can't use something 4746 03:28:48,799 --> 03:28:52,959 like plus because that's going to be 4747 03:28:50,639 --> 03:28:54,959 confusing to your compiler and these are 4748 03:28:52,959 --> 03:28:57,278 a few rules you really need to be aware 4749 03:28:54,959 --> 03:29:00,000 of and you're mostly going to be fine 4750 03:28:57,279 --> 03:29:02,720 the general form for declaring and 4751 03:29:00,000 --> 03:29:04,959 initializing your variable is what you 4752 03:29:02,719 --> 03:29:07,358 see here you're going to pass the type 4753 03:29:04,959 --> 03:29:09,679 in this case we have used and because we 4754 03:29:07,359 --> 03:29:11,600 are learning about ants you're going to 4755 03:29:09,680 --> 03:29:13,359 pass the variable name and you're going 4756 03:29:13,359 --> 03:29:18,079 initializing your variable with of 4757 03:29:15,760 --> 03:29:21,439 course this is braced initialization 4758 03:29:18,079 --> 03:29:23,920 because it is using curly braces but you 4759 03:29:21,439 --> 03:29:26,639 can also use functional initialization 4760 03:29:23,920 --> 03:29:28,559 or assignment initialization and it is 4761 03:29:26,639 --> 03:29:31,039 going to work equally well okay now we 4762 03:29:28,559 --> 03:29:34,478 have enough information to go in visual 4763 03:29:31,040 --> 03:29:37,520 studio code and play with us okay here i 4764 03:29:34,478 --> 03:29:40,238 am in my folder for the projects of this 4765 03:29:37,520 --> 03:29:43,359 chapter i am going to hop over in my 4766 03:29:40,238 --> 03:29:46,079 template project and copy the files that 4767 03:29:43,359 --> 03:29:48,960 i need i am going to go up again 4768 03:29:46,079 --> 03:29:50,478 and go on the project that is called 4769 03:29:50,478 --> 03:29:55,278 i am going to go in and paste in my 4770 03:29:52,719 --> 03:29:58,079 files and i am going to open up this 4771 03:29:55,279 --> 03:30:00,159 folder in visual studio code i am going 4772 03:29:58,079 --> 03:30:02,398 to close the welcome screen here and 4773 03:30:02,398 --> 03:30:07,920 and i am going to do the usual take out 4774 03:30:05,040 --> 03:30:10,160 the things we don't need to save on time 4775 03:30:07,920 --> 03:30:13,520 i am going to put in a piece of code 4776 03:30:10,159 --> 03:30:14,639 that i have lying on my drive and we're 4777 03:30:14,639 --> 03:30:19,199 go through it to save on typing time i 4778 03:30:17,200 --> 03:30:21,439 don't want you to suffer watching me 4779 03:30:19,200 --> 03:30:23,439 type this the first thing we have is the 4780 03:30:21,439 --> 03:30:25,840 elephant count this is a variable that 4781 03:30:23,439 --> 03:30:28,398 is not initialized so this is going to 4782 03:30:25,840 --> 03:30:30,799 contain garbage data it's not going to 4783 03:30:28,398 --> 03:30:33,119 contain anything that is useful to us 4784 03:30:30,799 --> 03:30:35,599 but some compilers are going to usefully 4785 03:30:33,120 --> 03:30:37,760 put in a zero because that's a logical 4786 03:30:35,600 --> 03:30:40,079 number to put in here if you didn't 4787 03:30:37,760 --> 03:30:43,120 explicitly put a number in here the 4788 03:30:40,079 --> 03:30:45,200 second one is using curly braces and 4789 03:30:43,120 --> 03:30:47,439 this is going to automatically 4790 03:30:45,200 --> 03:30:50,399 initialize to zero the third one is 4791 03:30:47,439 --> 03:30:53,120 going to be explicitly initialized with 4792 03:30:50,398 --> 03:30:55,920 the 10 and the cat count is going to be 4793 03:30:53,120 --> 03:30:58,000 initialized with 15. you can try and 4794 03:30:55,920 --> 03:31:00,318 print this out and see what you come up 4795 03:30:58,000 --> 03:31:02,238 with after compiling this application 4796 03:31:00,318 --> 03:31:04,478 another thing we have seen is that we 4797 03:31:02,238 --> 03:31:09,520 can do something like this and put an 4798 03:31:04,478 --> 03:31:12,639 expression into our curly braces and the 4799 03:31:09,520 --> 03:31:15,359 value this evaluates to is going to 4800 03:31:12,639 --> 03:31:18,238 initialize this domesticated animals 4801 03:31:15,359 --> 03:31:20,238 variable and now you should know that 4802 03:31:18,238 --> 03:31:22,639 this is probably going to take 4 bytes 4803 03:31:20,238 --> 03:31:25,760 in memory because it is an integer 4804 03:31:22,639 --> 03:31:28,639 this is really how c plus plus works 4805 03:31:31,439 --> 03:31:36,159 most times so integers is mostly going 4806 03:31:34,238 --> 03:31:38,799 to be four bytes on your system and 4807 03:31:36,159 --> 03:31:41,520 we're going to query this a little later 4808 03:31:38,799 --> 03:31:44,478 in this video okay let's try and compile 4809 03:31:41,520 --> 03:31:46,479 this program and see if it actually 4810 03:31:44,478 --> 03:31:49,119 works we're going to start world and you 4811 03:31:46,478 --> 03:31:51,840 see that the world is successful another 4812 03:31:49,120 --> 03:31:53,520 thing we can do is try and use variables 4813 03:31:51,840 --> 03:31:55,359 we haven't really declared in our 4814 03:31:53,520 --> 03:31:57,840 program and you can guess what is going 4815 03:31:57,840 --> 03:32:03,120 and new number for example and putin 4816 03:32:01,040 --> 03:32:05,359 doesn't exist you know it's a number 4817 03:32:03,120 --> 03:32:07,359 that doesn't exist the compiler is not 4818 03:32:05,359 --> 03:32:10,399 going to know what this is because we 4819 03:32:07,359 --> 03:32:12,159 didn't declare a variable called doesn't 4820 03:32:10,398 --> 03:32:13,920 exist if you try to compile this you're 4821 03:32:12,159 --> 03:32:16,639 going to get a problem you see that we 4822 03:32:13,920 --> 03:32:19,359 have a problem before we even compile so 4823 03:32:16,639 --> 03:32:22,079 visual studio code is this smart it can 4824 03:32:19,359 --> 03:32:24,960 analyze our code before we compile it 4825 03:32:22,079 --> 03:32:27,120 and give us hints to problems that might 4826 03:32:24,959 --> 03:32:30,159 come up you see here it is saying 4827 03:32:27,120 --> 03:32:32,479 doesn't exist isn't defined so if we try 4828 03:32:30,159 --> 03:32:34,639 to compile this we're going to get an 4829 03:32:32,478 --> 03:32:36,719 error that we expect it's going to say 4830 03:32:34,639 --> 03:32:38,559 doesn't exist doesn't really exist i 4831 03:32:36,719 --> 03:32:40,478 don't know what that is so you shouldn't 4832 03:32:38,559 --> 03:32:43,600 really do that you shouldn't try to use 4833 03:32:40,478 --> 03:32:45,840 variables you haven't declared this is 4834 03:32:45,840 --> 03:32:50,559 we can also do functional initialization 4835 03:32:50,559 --> 03:32:56,079 okay another thing we can do is store a 4836 03:32:53,200 --> 03:32:57,840 fractional number in an empty variable 4837 03:32:56,079 --> 03:32:59,760 and we're going to see what this does 4838 03:32:57,840 --> 03:33:02,000 we're going to try and compile this code 4839 03:32:59,760 --> 03:33:04,318 you see we have a 2.9 we're trying to 4840 03:33:02,000 --> 03:33:07,760 store this in an integer if we try to 4841 03:33:04,318 --> 03:33:11,278 compile this let's try and use gcc let's 4842 03:33:07,760 --> 03:33:13,840 clear this let's bring up a terminal and 4843 03:33:11,279 --> 03:33:16,880 try to build this we're going to try and 4844 03:33:13,840 --> 03:33:19,359 run our gcc compiler on this program it 4845 03:33:16,879 --> 03:33:21,799 is going to say errol narrowing a 4846 03:33:21,799 --> 03:33:28,079 2.899 from double to integer c plus plus 4847 03:33:25,120 --> 03:33:30,880 20 isn't even going to allow this so 4848 03:33:28,079 --> 03:33:34,079 this is a good thing because you might 4849 03:33:30,879 --> 03:33:36,639 think that you have a 2.9 and some 4850 03:33:34,079 --> 03:33:39,920 compilers are going to chop this off and 4851 03:33:36,639 --> 03:33:42,159 store a 2 instead so this is one of the 4852 03:33:39,920 --> 03:33:44,559 safe things about this way of doing 4853 03:33:42,159 --> 03:33:46,879 thanks so i am going to comment this out 4854 03:33:44,559 --> 03:33:49,519 because otherwise our program wouldn't 4855 03:33:46,879 --> 03:33:51,839 compile now that we know how to use 4856 03:33:49,520 --> 03:33:54,399 these things we can try to print the 4857 03:33:51,840 --> 03:33:57,040 values in here okay i am going to put in 4858 03:33:54,398 --> 03:33:59,278 a piece of code i have lying around on 4859 03:33:57,040 --> 03:34:01,520 my drive i am going to remove these 4860 03:33:59,279 --> 03:34:04,159 spaces inside because it is annoying 4861 03:34:01,520 --> 03:34:06,159 let's do this and if we try and build 4862 03:34:04,159 --> 03:34:08,719 this program it should compile 4863 03:34:06,159 --> 03:34:11,119 successfully and if we run it we should 4864 03:34:08,719 --> 03:34:13,599 see our numbers we're going to go in our 4865 03:34:11,120 --> 03:34:15,760 terminal here and do der and we're going 4866 03:34:13,600 --> 03:34:19,279 to run rooster and we're going to see 4867 03:34:15,760 --> 03:34:21,279 elephant count is one because this is a 4868 03:34:19,279 --> 03:34:22,319 garbage value so it could really be 4869 03:34:26,879 --> 03:34:32,799 cat count should be 15 it is what we 4870 03:34:29,200 --> 03:34:35,840 have and a domesticated animal count is 4871 03:34:32,799 --> 03:34:38,238 25 because we have drug account 4872 03:34:35,840 --> 03:34:41,120 and we add that to cat count and we're 4873 03:34:38,238 --> 03:34:43,279 going to end up with 25. this is what we 4874 03:34:41,120 --> 03:34:45,600 expect now that we know how to work with 4875 03:34:43,279 --> 03:34:48,479 this we can also try and play with 4876 03:34:45,600 --> 03:34:50,720 functional initialization before i put 4877 03:34:48,478 --> 03:34:52,879 in my code i am going to comment all 4878 03:34:50,719 --> 03:34:55,199 this out because i don't want this to 4879 03:34:52,879 --> 03:34:57,679 disturb us as we work with our 4880 03:34:55,200 --> 03:35:00,000 functional initialization code so i am 4881 03:34:57,680 --> 03:35:02,479 going to comment all this out and go all 4882 03:35:00,000 --> 03:35:04,959 the way down and put in my code for 4883 03:35:04,959 --> 03:35:09,839 and you see here we have a variable 4884 03:35:06,879 --> 03:35:12,398 called ethel we initialize this to 5 we 4885 03:35:09,840 --> 03:35:13,920 have orange count which is 10 we have 4886 03:35:13,920 --> 03:35:18,238 and we have a bad initialization that is 4887 03:35:16,000 --> 03:35:21,600 not going to work it doesn't exist 3 and 4888 03:35:18,238 --> 03:35:23,359 doesn't exist 4 don't really exist so so 4889 03:35:21,600 --> 03:35:24,479 this is going to give us a compiler 4890 03:35:24,478 --> 03:35:30,398 and if we do something like we do in 4891 03:35:27,600 --> 03:35:32,960 narrowing the conversion function here 4892 03:35:30,398 --> 03:35:36,238 this is mostly going to compile and this 4893 03:35:32,959 --> 03:35:39,039 is going to chop off data and store a 2 4894 03:35:36,238 --> 03:35:41,520 instead of the 2.9 this is something you 4895 03:35:39,040 --> 03:35:43,920 should really be aware of and you see 4896 03:35:41,520 --> 03:35:46,238 the comment here says information lost 4897 03:35:43,920 --> 03:35:49,040 this is less safe than braced 4898 03:35:46,238 --> 03:35:50,478 initializer because breast initializer 4899 03:35:50,478 --> 03:35:54,799 give you a compiler error if you try to 4900 03:35:52,639 --> 03:35:57,519 do this and that's a good thing 4901 03:35:54,799 --> 03:36:00,238 so we also try and print these out so i 4902 03:35:57,520 --> 03:36:02,399 am going to tidy this up a little bit 4903 03:36:00,238 --> 03:36:05,039 and i am going to try and compile this 4904 03:36:02,398 --> 03:36:08,000 piece of code run task and world with 4905 03:36:05,040 --> 03:36:09,600 gcc the world is going to pass through 4906 03:36:09,600 --> 03:36:16,238 and if we do clear and run rooster we're 4907 03:36:13,120 --> 03:36:18,640 going to see apple count is 5 which is 4908 03:36:16,238 --> 03:36:21,600 what we have in here orange count is 4909 03:36:18,639 --> 03:36:24,000 going to be 10 so this is right fruit 4910 03:36:21,600 --> 03:36:25,279 count is going to be 15 because it is 5 4911 03:36:25,279 --> 03:36:31,680 and what is really worrying is that 4912 03:36:29,120 --> 03:36:34,880 narrowing conversion is 2 but we didn't 4913 03:36:31,680 --> 03:36:38,159 store in a 2 we started in a 2.9 4914 03:36:34,879 --> 03:36:43,119 and because an integer can really store 4915 03:36:38,159 --> 03:36:46,000 4 bytes 2.9 is more than 4 bytes and it 4916 03:36:43,120 --> 03:36:48,960 is going to store what it can and it is 4917 03:36:46,000 --> 03:36:51,439 going to chop off this 2.9 and only 4918 03:36:48,959 --> 03:36:54,238 store in a 2 so this is really something 4919 03:36:51,439 --> 03:36:56,960 you should be aware of sometimes this is 4920 03:36:54,238 --> 03:36:58,079 going to cause problems in your program 4921 03:36:58,079 --> 03:37:03,680 really really if you are trying to store 4922 03:37:00,799 --> 03:37:06,559 fractional numbers in an integer please 4923 03:37:06,559 --> 03:37:11,278 form because the compiler is going to 4924 03:37:08,398 --> 03:37:13,358 stop you from doing this if you try okay 4925 03:37:11,279 --> 03:37:15,279 so this is really the difference between 4926 03:37:13,359 --> 03:37:18,399 functional notation and breast 4927 03:37:15,279 --> 03:37:20,399 initializer notation functional notation 4928 03:37:18,398 --> 03:37:23,278 is not going to give you an arrow if you 4929 03:37:20,398 --> 03:37:25,840 try to store something that can't really 4930 03:37:23,279 --> 03:37:28,960 fit in a net it is going to silently 4931 03:37:25,840 --> 03:37:31,359 chop it off and put in whatever it can 4932 03:37:28,959 --> 03:37:33,278 and braced initializer is going to give 4933 03:37:31,359 --> 03:37:36,000 you a compiler error so this is the 4934 03:37:33,279 --> 03:37:39,040 difference between these two okay i am 4935 03:37:36,000 --> 03:37:41,359 going to comment this out as well and i 4936 03:37:39,040 --> 03:37:43,760 am going to put the starting comment 4937 03:37:41,359 --> 03:37:45,600 here and i am going to go down and show 4938 03:37:43,760 --> 03:37:48,318 you that you can do assignment 4939 03:37:45,600 --> 03:37:50,159 initialization as well i am going to put 4940 03:37:48,318 --> 03:37:52,398 in my code here because i don't want to 4941 03:37:50,159 --> 03:37:54,959 type it really and i am going to print 4942 03:37:52,398 --> 03:37:57,278 all this out and we have a bike account 4943 03:37:54,959 --> 03:37:59,839 variable that we initialize using the 4944 03:37:57,279 --> 03:38:02,319 assignment notation track count is going 4945 03:37:59,840 --> 03:38:04,799 to be seven fake account is going to be 4946 03:38:02,318 --> 03:38:06,639 bike account plus track count and we're 4947 03:38:04,799 --> 03:38:08,799 going to be narrowing conversion here 4948 03:38:06,639 --> 03:38:11,760 and we're going to see if this actually 4949 03:38:08,799 --> 03:38:13,599 works we're going to try and weld this 4950 03:38:11,760 --> 03:38:15,120 we're going to run the task to world 4951 03:38:15,120 --> 03:38:19,600 it is going to pass through you can 4952 03:38:17,120 --> 03:38:21,279 guess what we're going to see if we run 4953 03:38:21,279 --> 03:38:28,079 bike account is 2 track count is 7 which 4954 03:38:24,318 --> 03:38:31,039 is what we expect vehicle count is nine 4955 03:38:28,079 --> 03:38:33,840 and uh the narrowing conversion is also 4956 03:38:31,040 --> 03:38:35,600 chopping off what we have here so this 4957 03:38:33,840 --> 03:38:37,840 is something you need to be careful 4958 03:38:35,600 --> 03:38:39,680 about another thing i want you to see is 4959 03:38:37,840 --> 03:38:42,000 that you can check the size of your 4960 03:38:39,680 --> 03:38:45,120 variables in memory so we're going to 4961 03:38:48,639 --> 03:38:52,238 and i am going to put in a piece of code 4962 03:38:50,719 --> 03:38:55,079 to really play with this i don't want to 4963 03:38:52,238 --> 03:38:57,439 type all this because it's really 4964 03:38:55,079 --> 03:38:59,760 self-explanatory by now we're going to 4965 03:38:57,439 --> 03:39:02,559 print sites of ant and inside we're 4966 03:38:59,760 --> 03:39:04,478 going to say size of ant and this is 4967 03:39:02,559 --> 03:39:07,278 going to give us how much memory is 4968 03:39:04,478 --> 03:39:09,679 really occupied by an end and we can use 4969 03:39:07,279 --> 03:39:12,560 this form here by passing in a variable 4970 03:39:09,680 --> 03:39:15,359 name track count is a variable we have 4971 03:39:12,559 --> 03:39:18,079 declared and initialized here 4972 03:39:15,359 --> 03:39:20,880 so if we build this program 4973 03:39:18,079 --> 03:39:23,120 let's do that with gcc the build is 4974 03:39:20,879 --> 03:39:25,438 going to pass through and if we run 4975 03:39:23,120 --> 03:39:28,160 rooster we're going to see that the size 4976 03:39:25,439 --> 03:39:31,199 of int is four you're going to see that 4977 03:39:28,159 --> 03:39:34,398 this is in bytes and size of track count 4978 03:39:31,199 --> 03:39:37,439 is for as well because track account is 4979 03:39:34,398 --> 03:39:39,920 also an integer so these are all little 4980 03:39:37,439 --> 03:39:42,639 tools you can really use to 4981 03:39:39,920 --> 03:39:45,760 know how much memory is really being 4982 03:39:42,639 --> 03:39:47,278 used and occupied by your program and 4983 03:39:45,760 --> 03:39:49,760 this is really all we set up to do in 4984 03:39:47,279 --> 03:39:53,120 this lecture to learn as much as we can 4985 03:39:49,760 --> 03:39:56,238 about integers integers occupy 4 bytes 4986 03:39:53,120 --> 03:39:58,880 in memory for the most part and we have 4987 03:39:56,238 --> 03:40:01,920 seen different ways we can initialize 4988 03:39:58,879 --> 03:40:04,799 our integer variables we can use breast 4989 03:40:01,920 --> 03:40:07,120 initializers which is going to use curly 4990 03:40:07,120 --> 03:40:12,960 initialization using parentheses like 4991 03:40:09,439 --> 03:40:15,439 this we can also do assignment notation 4992 03:40:12,959 --> 03:40:18,559 using the from like this and we can use 4993 03:40:15,439 --> 03:40:21,359 size of to check the size of our integer 4994 03:40:18,559 --> 03:40:24,079 variables i have to say that these kinds 4995 03:40:21,359 --> 03:40:27,040 of initialization also work on other 4996 03:40:24,079 --> 03:40:29,520 variables but we took the chance to 4997 03:40:27,040 --> 03:40:31,439 introduce all these concepts using 4998 03:40:29,520 --> 03:40:33,120 integers this is really all we set out 4999 03:40:31,439 --> 03:40:35,840 to do in this lecture i hope you found 5000 03:40:33,120 --> 03:40:37,680 it interesting we are going to stop here 5001 03:40:35,840 --> 03:40:40,318 in this lecture the next one we're going 5002 03:40:37,680 --> 03:40:43,120 to start and learn about integer 5003 03:40:40,318 --> 03:40:44,559 modifiers go ahead and finish up here 5004 03:40:44,559 --> 03:40:49,359 in this lecture we're going to learn 5005 03:40:46,000 --> 03:40:51,439 about integer modifiers and these are 5006 03:40:49,359 --> 03:40:54,238 little things you can sprinkle around 5007 03:40:51,439 --> 03:40:56,880 your integer types to modify their 5008 03:40:54,238 --> 03:40:58,398 behavior a little bit but before we dive 5009 03:40:58,398 --> 03:41:03,920 let's talk about how we can store 5010 03:41:00,959 --> 03:41:06,799 positive or negative numbers in integers 5011 03:41:03,920 --> 03:41:10,398 and we can do something like this if we 5012 03:41:06,799 --> 03:41:12,079 put a tan inside our value 1 variable 5013 03:41:12,079 --> 03:41:18,000 it's going to work we can also put in a 5014 03:41:14,559 --> 03:41:20,159 negative number like minus 300 5015 03:41:18,000 --> 03:41:21,520 and it is going to work so what's the 5016 03:41:22,478 --> 03:41:28,398 this is signed by default and this is a 5017 03:41:24,879 --> 03:41:31,198 modifier we can apply to our variable 5018 03:41:28,398 --> 03:41:33,920 to signal that we have the possibility 5019 03:41:31,199 --> 03:41:36,319 to store in negative or positive numbers 5020 03:41:33,920 --> 03:41:38,559 and it is going to work if you only want 5021 03:41:36,318 --> 03:41:41,119 to store positive numbers in your end 5022 03:41:38,559 --> 03:41:44,478 variables you can use another 5023 03:41:41,120 --> 03:41:46,319 modifier called unsigned and if you put 5024 03:41:44,478 --> 03:41:48,000 in a negative number you're going to get 5025 03:41:46,318 --> 03:41:50,478 a compiler error we're going to see how 5026 03:41:48,000 --> 03:41:53,199 this works in a minute okay regardless 5027 03:41:50,478 --> 03:41:56,079 of whether your integer variables are 5028 03:41:53,199 --> 03:41:58,720 unsigned or signed they are going to 5029 03:41:56,079 --> 03:42:01,039 still occupy 4 bytes in memory so this 5030 03:41:58,719 --> 03:42:03,358 is how it's going to look in memory and 5031 03:42:01,040 --> 03:42:04,720 here is a simple formula we can use to 5032 03:42:04,719 --> 03:42:08,959 the range of values we can store in our 5033 03:42:08,959 --> 03:42:14,639 if they are unsigned or signed if the 5034 03:42:12,159 --> 03:42:17,279 number is inside you can use this 5035 03:42:14,639 --> 03:42:19,358 unsigned range formula and plug in the 5036 03:42:19,359 --> 03:42:24,399 in your data type if you are using a 5037 03:42:24,398 --> 03:42:28,719 this is what you're going to use and 5038 03:42:26,398 --> 03:42:31,278 here is a simple example for example if 5039 03:42:28,719 --> 03:42:33,519 we have an unsigned and this is the 5040 03:42:31,279 --> 03:42:36,079 range of values we can store we can 5041 03:42:33,520 --> 03:42:38,640 basically go from 0 all the way to 4 5042 03:42:36,079 --> 03:42:40,799 billion if the number is signed the 5043 03:42:38,639 --> 03:42:42,879 range is going to basically be divided 5044 03:42:40,799 --> 03:42:45,198 into half the half is going to go on the 5045 03:42:42,879 --> 03:42:47,278 left and store negative numbers and the 5046 03:42:45,199 --> 03:42:49,840 other half is going to go to the right 5047 03:42:47,279 --> 03:42:52,238 and store positive numbers and you can 5048 03:42:49,840 --> 03:42:55,680 see that we can basically go from minus 5049 03:42:52,238 --> 03:42:58,159 two billion to two billion if the number 5050 03:42:55,680 --> 03:43:01,520 is signed okay by now we have seen two 5051 03:42:58,159 --> 03:43:04,318 modifiers unsigned and designed but 5052 03:43:01,520 --> 03:43:07,920 there are others for example you can 5053 03:43:04,318 --> 03:43:11,278 flag your integer types as short or long 5054 03:43:07,920 --> 03:43:14,079 what does this do well this is a table 5055 03:43:11,279 --> 03:43:15,920 that i put together to try and come up 5056 03:43:14,079 --> 03:43:19,120 with all the combinations you can come 5057 03:43:15,920 --> 03:43:21,279 up with if you have a short variable it 5058 03:43:19,120 --> 03:43:23,600 is going to be two bytes in memory this 5059 03:43:21,279 --> 03:43:26,399 is the modification that this modifier 5060 03:43:23,600 --> 03:43:28,640 does to your type if you have short and 5061 03:43:26,398 --> 03:43:30,719 it's going to be two bytes if you have a 5062 03:43:28,639 --> 03:43:33,439 signed short and it's going to be two 5063 03:43:30,719 --> 03:43:35,920 bytes if you have unsigned shirt and 5064 03:43:33,439 --> 03:43:38,960 it's going to be two bites you can see 5065 03:43:35,920 --> 03:43:43,120 that the effect of short is really 5066 03:43:38,959 --> 03:43:46,318 shortening the original size in memory 5067 03:43:43,120 --> 03:43:48,640 for whatever type you applied this on 5068 03:43:46,318 --> 03:43:51,119 this is really working on an editor even 5069 03:43:48,639 --> 03:43:53,039 if we don't specify int here for example 5070 03:43:51,120 --> 03:43:55,600 short short here it's going to 5071 03:43:55,600 --> 03:44:01,359 we have two bytes because it's basically 5072 03:43:58,000 --> 03:44:04,398 an end that we have split into here in 5073 03:44:01,359 --> 03:44:07,199 the second column we have ant which is 5074 03:44:04,398 --> 03:44:09,519 going to be 4 bytes if it is assigned 5075 03:44:07,199 --> 03:44:11,279 and it's going to be 4 bytes this is 5076 03:44:09,520 --> 03:44:14,880 really the sizes you're going to come up 5077 03:44:11,279 --> 03:44:17,920 with if you use these combinations of 5078 03:44:14,879 --> 03:44:19,519 modifiers on your integer types one 5079 03:44:17,920 --> 03:44:22,559 thing i really want you to be careful 5080 03:44:19,520 --> 03:44:26,479 about is that these modifiers only work 5081 03:44:22,559 --> 03:44:28,799 for integral types so they work for data 5082 03:44:26,478 --> 03:44:31,438 types in which you can store 5083 03:44:28,799 --> 03:44:34,238 decimal or whole numbers you can't use 5084 03:44:31,439 --> 03:44:37,359 this on types in which you can store 5085 03:44:34,238 --> 03:44:39,520 fractional numbers like 2.9 that's going 5086 03:44:37,359 --> 03:44:42,079 to give you a compiler error now that 5087 03:44:39,520 --> 03:44:44,560 you have an idea about this why don't we 5088 03:44:42,079 --> 03:44:47,359 go in visual studio code and actually 5089 03:44:44,559 --> 03:44:50,159 play with us okay here we are in our 5090 03:44:47,359 --> 03:44:53,040 folder that contains our projects we're 5091 03:44:50,159 --> 03:44:55,760 going to do the usual and copy the files 5092 03:44:53,040 --> 03:44:58,960 from our template project and we're 5093 03:44:55,760 --> 03:45:02,159 going to put those files in our integer 5094 03:44:58,959 --> 03:45:04,238 modifiers project here let's open it up 5095 03:45:02,159 --> 03:45:06,478 and paste in the files we're going to go 5096 03:45:04,238 --> 03:45:08,639 up again and we're going to open this up 5097 03:45:06,478 --> 03:45:10,799 in visual studio code the first thing i 5098 03:45:08,639 --> 03:45:13,039 want to play with is storing positive 5099 03:45:10,799 --> 03:45:15,198 and negative numbers in our integer 5100 03:45:13,040 --> 03:45:17,920 types so we're going to say end value 5101 03:45:15,199 --> 03:45:20,479 one and we're going to store in a 10 for 5102 03:45:17,920 --> 03:45:21,439 example and we're going to do any value 5103 03:45:21,439 --> 03:45:25,760 and we're going to put in a minus 300. i 5104 03:45:24,238 --> 03:45:27,760 think this is what we had you know a 5105 03:45:25,760 --> 03:45:29,920 slide so we're going to keep this 5106 03:45:27,760 --> 03:45:31,920 if you try to print this out so we're 5107 03:45:29,920 --> 03:45:33,760 going to say value 1 and we're going to 5108 03:45:33,760 --> 03:45:38,398 and we're going to try and build this so 5109 03:45:36,639 --> 03:45:40,478 we're going to open our terminal so that 5110 03:45:38,398 --> 03:45:42,719 we can play with it right away 5111 03:45:40,478 --> 03:45:44,879 and we're going to run the task to world 5112 03:45:42,719 --> 03:45:48,159 with gcc the world is going to be 5113 03:45:44,879 --> 03:45:49,519 successful if we run our program we're 5114 03:45:48,159 --> 03:45:52,478 going to see that it's going to say 5115 03:45:52,478 --> 03:45:57,840 is -3 300 this is really cool we can 5116 03:45:55,760 --> 03:46:00,318 store positive numbers and negative 5117 03:45:57,840 --> 03:46:01,760 numbers in our end times and this is 5118 03:46:01,760 --> 03:46:06,159 4 bytes in memory we can prove this by 5119 03:46:04,000 --> 03:46:08,879 printing the size of value 1 and value 5120 03:46:06,159 --> 03:46:12,879 2. let's do that so we're going to say 5121 03:46:08,879 --> 03:46:16,719 size of value 1 is size of value 1. we 5122 03:46:12,879 --> 03:46:21,599 can also do it for value 2 size of value 5123 03:46:16,719 --> 03:46:24,318 2 and it's going to be size of value two 5124 03:46:21,600 --> 03:46:26,318 std and yeah if we build this again 5125 03:46:24,318 --> 03:46:30,079 we're going to run the test world with 5126 03:46:26,318 --> 03:46:31,920 gcc and uh we run our application it's 5127 03:46:30,079 --> 03:46:34,000 going to save four bytes so it doesn't 5128 03:46:31,920 --> 03:46:36,159 really matter if we store in a positive 5129 03:46:34,000 --> 03:46:38,799 number or a negative number it's going 5130 03:46:36,159 --> 03:46:41,600 to be 4 bytes this is the point i want 5131 03:46:38,799 --> 03:46:43,119 to make here another thing you can do is 5132 03:46:43,120 --> 03:46:49,359 very clear that you have signed numbers 5133 03:46:46,079 --> 03:46:52,159 in here and you can use the signed 5134 03:46:49,359 --> 03:46:55,199 integer modifier here so this is going 5135 03:46:52,159 --> 03:46:56,959 to give us the same result and if we try 5136 03:46:56,959 --> 03:47:02,719 run the task to build with gcc the world 5137 03:47:00,159 --> 03:47:04,799 is going to be successful if we run the 5138 03:47:02,719 --> 03:47:07,840 program we're going to get the same 5139 03:47:04,799 --> 03:47:09,920 result so if you don't put aside or if 5140 03:47:07,840 --> 03:47:12,000 you put it here it's going to be exactly 5141 03:47:09,920 --> 03:47:14,960 the same thing another thing you can do 5142 03:47:12,000 --> 03:47:16,159 is make it very clear that you want to 5143 03:47:16,159 --> 03:47:21,920 positive numbers in your variables we 5144 03:47:18,959 --> 03:47:24,000 can do that by using the unsigned 5145 03:47:21,920 --> 03:47:25,439 integer modifier so we're going to say 5146 03:47:25,439 --> 03:47:30,238 and value three for example and we're 5147 03:47:30,238 --> 03:47:37,119 this is going to be fine but if you do 5148 03:47:32,719 --> 03:47:38,719 unsigned and value 4 and put in a minus 5149 03:47:38,719 --> 03:47:43,198 this is going to give you a compiler 5150 03:47:40,478 --> 03:47:45,519 error because you save this is unsigned 5151 03:47:43,199 --> 03:47:48,560 and this really means that you want to 5152 03:47:45,520 --> 03:47:51,120 store in only positive numbers but you 5153 03:47:48,559 --> 03:47:53,519 are trying to put in a negative number 5154 03:47:51,120 --> 03:47:55,199 if you try to build this let's try this 5155 03:47:53,520 --> 03:47:58,079 we're going to run the task to build 5156 03:47:55,199 --> 03:48:02,000 with gcc we are going to get a compiler 5157 03:47:58,079 --> 03:48:03,600 arrow saying conversion from -5 to ant 5158 03:48:02,000 --> 03:48:05,760 this is not allowed you're going to get 5159 03:48:03,600 --> 03:48:08,238 a compiler arrow your program is not 5160 03:48:05,760 --> 03:48:10,880 going to compile so this is a compiler 5161 03:48:08,238 --> 03:48:13,119 error let's say this here and this is 5162 03:48:10,879 --> 03:48:15,839 really all i had to share about these 5163 03:48:15,840 --> 03:48:19,600 signed is going to mean that you can 5164 03:48:17,520 --> 03:48:22,159 store negative and positive numbers 5165 03:48:19,600 --> 03:48:25,040 unsigned is going to mean that you can 5166 03:48:22,159 --> 03:48:26,719 only put in positive numbers and if you 5167 03:48:25,040 --> 03:48:29,439 try to put in a negative number you're 5168 03:48:26,719 --> 03:48:32,159 going to get a compiler error now that 5169 03:48:29,439 --> 03:48:34,720 this is commented out if you try to run 5170 03:48:34,719 --> 03:48:39,198 the world is going to be good and we 5171 03:48:36,719 --> 03:48:40,799 will be able to run our program 5172 03:48:39,199 --> 03:48:44,079 and it's going to do whatever it was 5173 03:48:40,799 --> 03:48:46,719 doing before as we see here we also saw 5174 03:48:44,079 --> 03:48:49,199 that we could use the short and 5175 03:48:46,719 --> 03:48:51,039 long modifiers and we're going to put a 5176 03:48:49,199 --> 03:48:53,199 piece of code here that is going to 5177 03:48:51,040 --> 03:48:56,080 allow us to play with them so i don't 5178 03:48:53,199 --> 03:48:58,319 want to type this and if you don't want 5179 03:48:56,079 --> 03:49:00,559 to type this as well you can get this 5180 03:48:58,318 --> 03:49:03,039 code from the resource section it's 5181 03:49:00,559 --> 03:49:05,119 going to be left you can open the link 5182 03:49:03,040 --> 03:49:07,040 use the code copy the code and paste it 5183 03:49:05,120 --> 03:49:09,279 in here and really play with it because 5184 03:49:07,040 --> 03:49:11,439 the point is really not to type all this 5185 03:49:09,279 --> 03:49:13,279 the point is to understand what these 5186 03:49:13,279 --> 03:49:17,199 to your variables what we are going to 5187 03:49:15,279 --> 03:49:20,159 do we're going to try to print them out 5188 03:49:17,199 --> 03:49:22,319 and print the size they actually have in 5189 03:49:20,159 --> 03:49:24,398 memory we're going to do this in four 5190 03:49:22,318 --> 03:49:26,799 groups we're going to do one for short 5191 03:49:24,398 --> 03:49:29,519 and long we're going to do one for ant 5192 03:49:26,799 --> 03:49:31,358 here we're going to do one for long and 5193 03:49:29,520 --> 03:49:33,600 we're going to do one for long long 5194 03:49:31,359 --> 03:49:35,600 these are things you can do and we're 5195 03:49:33,600 --> 03:49:38,559 going to see how much memory is actually 5196 03:49:35,600 --> 03:49:41,520 being used by all these variables we are 5197 03:49:38,559 --> 03:49:43,840 going to start with the first group here 5198 03:49:41,520 --> 03:49:47,680 so we're going to go down and print the 5199 03:49:43,840 --> 03:49:50,238 size is out and this may be really hard 5200 03:49:47,680 --> 03:49:51,840 to wrap your brain around but they are 5201 03:49:51,840 --> 03:49:56,719 see out statements that we have been 5202 03:49:53,840 --> 03:49:59,040 doing all along i am going to split this 5203 03:49:56,719 --> 03:50:01,278 on different lines so that you can 5204 03:50:01,279 --> 03:50:07,279 we have an output statement for a short 5205 03:50:04,639 --> 03:50:09,519 var which is what we have here so we 5206 03:50:07,279 --> 03:50:11,279 expect this to take two bytes and all 5207 03:50:09,520 --> 03:50:13,760 these things are probably going to take 5208 03:50:11,279 --> 03:50:16,399 two bytes let's try and build the 5209 03:50:13,760 --> 03:50:18,639 program with gcc the bullet is going to 5210 03:50:16,398 --> 03:50:20,639 be successful we are going to run 5211 03:50:18,639 --> 03:50:23,840 rooster and you're going to see that 5212 03:50:20,639 --> 03:50:26,159 short end is two bytes signed short is 5213 03:50:23,840 --> 03:50:27,359 two bytes and everything is basically 5214 03:50:27,359 --> 03:50:32,159 in this group here and we did guess 5215 03:50:30,398 --> 03:50:35,198 right the next thing we want to try out 5216 03:50:35,199 --> 03:50:39,600 we are going to go down and give 5217 03:50:37,199 --> 03:50:41,680 ourselves some breathing room 5218 03:50:39,600 --> 03:50:43,680 and we're going to paste this in and 5219 03:50:41,680 --> 03:50:45,840 we're going to separate these again so 5220 03:50:43,680 --> 03:50:48,800 that we can see what is really happening 5221 03:50:45,840 --> 03:50:51,520 here and i think this is really enough 5222 03:50:48,799 --> 03:50:54,478 so we're going to see the sizes for the 5223 03:50:51,520 --> 03:50:57,520 thanks we have in this group here and we 5224 03:50:54,478 --> 03:51:00,559 expect this to take four bytes so let's 5225 03:50:57,520 --> 03:51:03,040 try and run the world task to build with 5226 03:51:00,559 --> 03:51:05,760 gcc the world is going to be good we can 5227 03:51:03,040 --> 03:51:08,239 run the program like we usually do and 5228 03:51:05,760 --> 03:51:11,600 you're going to see that ant variable is 5229 03:51:08,238 --> 03:51:15,439 4 bytes and everything in that group is 5230 03:51:11,600 --> 03:51:19,680 4 bytes and this tells you that 5231 03:51:15,439 --> 03:51:22,479 regardless of the side and unside 5232 03:51:19,680 --> 03:51:25,279 modifiers you put here it's going to 5233 03:51:22,478 --> 03:51:27,840 really use the size of it here okay it's 5234 03:51:25,279 --> 03:51:30,399 going to take four bytes in memory let's 5235 03:51:27,840 --> 03:51:33,040 try this group here we are going to go 5236 03:51:30,398 --> 03:51:34,639 down and put in the code to print the 5237 03:51:34,639 --> 03:51:40,398 and this is what we should use 5238 03:51:38,238 --> 03:51:43,439 let's split this on different lines so 5239 03:51:40,398 --> 03:51:44,959 that it is easier on the eyes and again 5240 03:51:43,439 --> 03:51:46,960 if you don't really want to type this 5241 03:51:44,959 --> 03:51:49,278 you can get the code from the length 5242 03:51:46,959 --> 03:51:51,519 resource section you're going to find 5243 03:51:49,279 --> 03:51:53,680 the code here and you can copy and paste 5244 03:51:51,520 --> 03:51:56,238 and really play with us but it is 5245 03:51:53,680 --> 03:51:58,960 helpful to really type these things out 5246 03:51:56,238 --> 03:52:00,959 and really bring these things in muscle 5247 03:51:58,959 --> 03:52:03,599 memory because it's going to help okay 5248 03:52:00,959 --> 03:52:06,959 so we have this and we're going to try 5249 03:52:03,600 --> 03:52:09,199 and run the world task with gcc the 5250 03:52:06,959 --> 03:52:10,719 build is good we're going to go in here 5251 03:52:10,719 --> 03:52:16,079 and it is going to say long variable is 5252 03:52:13,279 --> 03:52:19,279 four bytes and everything in this group 5253 03:52:16,079 --> 03:52:22,559 is basically going to be four bytes 5254 03:52:19,279 --> 03:52:23,439 in here the last one is this long long 5255 03:52:23,439 --> 03:52:26,398 it is going to really give you a huge 5256 03:52:26,398 --> 03:52:31,519 for your values you can store in the 5257 03:52:28,559 --> 03:52:33,359 variable but don't really trust my world 5258 03:52:31,520 --> 03:52:35,680 let's try this out so that you can see 5259 03:52:33,359 --> 03:52:38,000 for yourself we are going to go down and 5260 03:52:35,680 --> 03:52:40,720 really put this in so we're going to 5261 03:52:38,000 --> 03:52:42,959 give ourselves some breathing room 5262 03:52:47,439 --> 03:52:53,760 and what we are really doing is printing 5263 03:52:50,398 --> 03:52:55,519 the sizes for the variables that we have 5264 03:52:59,680 --> 03:53:04,000 the world is going to be good 5265 03:53:04,000 --> 03:53:09,199 and we're going to see that long long is 5266 03:53:06,159 --> 03:53:12,559 eight bytes eight bytes gives us a 5267 03:53:09,199 --> 03:53:14,159 really huge range of values okay this is 5268 03:53:12,559 --> 03:53:16,079 really all we set out to do in this 5269 03:53:14,159 --> 03:53:18,639 lecture to learn about these integer 5270 03:53:16,079 --> 03:53:20,879 modifiers they modify the way your 5271 03:53:18,639 --> 03:53:23,358 integer variables behave for example we 5272 03:53:20,879 --> 03:53:26,000 can specify that we only want positive 5273 03:53:23,359 --> 03:53:28,079 numbers all that we want to both store 5274 03:53:26,000 --> 03:53:30,719 positive and negative numbers for that 5275 03:53:28,079 --> 03:53:33,359 you're going to use designed or unsigned 5276 03:53:30,719 --> 03:53:35,920 modifiers you can also shorten or 5277 03:53:33,359 --> 03:53:38,318 lengthen the range of values you can 5278 03:53:35,920 --> 03:53:41,120 store in your variable by using the 5279 03:53:38,318 --> 03:53:43,920 modifiers short and long and from the 5280 03:53:41,120 --> 03:53:46,239 output of a program here you can really 5281 03:53:43,920 --> 03:53:48,318 see all these effects here this is 5282 03:53:46,238 --> 03:53:50,559 really all we set out to do in this 5283 03:53:48,318 --> 03:53:52,079 lecture i hope you found it interesting 5284 03:53:50,559 --> 03:53:53,760 we are going to stop here in this 5285 03:53:52,079 --> 03:53:56,959 lecture in the next one we're going to 5286 03:53:53,760 --> 03:53:59,600 start and learn about fractional numbers 5287 03:53:56,959 --> 03:54:01,198 go ahead and finish up here and meet me 5288 03:53:59,600 --> 03:54:04,399 there in this lecture we're going to 5289 03:54:01,199 --> 03:54:06,000 learn about fractional numbers and as we 5290 03:54:04,398 --> 03:54:08,238 work with these numbers i want you to 5291 03:54:06,000 --> 03:54:11,279 keep in mind that any piece of data you 5292 03:54:08,238 --> 03:54:13,760 work with in your c plus program 5293 03:54:11,279 --> 03:54:17,120 is ultimately going to be stored in the 5294 03:54:13,760 --> 03:54:18,880 form of ones and zeros in memory 5295 03:54:17,120 --> 03:54:22,079 functional number are also called 5296 03:54:18,879 --> 03:54:24,478 floating types in technical terms they 5297 03:54:22,079 --> 03:54:27,520 are used to represent numbers with 5298 03:54:24,478 --> 03:54:30,159 fractional parts in c plus plus and many 5299 03:54:27,520 --> 03:54:32,319 other languages and we have three types 5300 03:54:30,159 --> 03:54:33,279 that we can use in c plus plus we have 5301 03:54:33,279 --> 03:54:39,040 double and long double and the main 5302 03:54:35,920 --> 03:54:41,920 difference is in the size they occupy in 5303 03:54:39,040 --> 03:54:44,399 memory a float takes four bytes a double 5304 03:54:41,920 --> 03:54:47,760 takes eight bytes and the long double 5305 03:54:44,398 --> 03:54:50,000 takes 12 bytes typically so as the size 5306 03:54:47,760 --> 03:54:52,398 goes up you can also notice that the 5307 03:54:50,000 --> 03:54:54,879 precision goes up so what is the 5308 03:54:52,398 --> 03:54:58,238 precision the precision is basically the 5309 03:54:54,879 --> 03:55:01,039 number of bits you can represent with 5310 03:54:58,238 --> 03:55:03,039 that type starting from the number in 5311 03:55:01,040 --> 03:55:05,359 front of the decimal point so if we look 5312 03:55:03,040 --> 03:55:07,840 at this number here we can try and 5313 03:55:05,359 --> 03:55:13,920 figure out its precision so we're going 5314 03:55:07,840 --> 03:55:16,398 to count 1 2 3 4 5 6 7 8 9 10 11 12. the 5315 03:55:19,040 --> 03:55:24,319 we're really not able to represent a 5316 03:55:21,359 --> 03:55:26,399 number like this with a float 5317 03:55:24,318 --> 03:55:28,719 the smallest thing we can use to 5318 03:55:28,719 --> 03:55:34,318 is a double because the precision is 15 5319 03:55:32,079 --> 03:55:36,478 and it is more than what we need for 5320 03:55:34,318 --> 03:55:38,559 this number which is 12. one thing you 5321 03:55:36,478 --> 03:55:40,478 should remember is that the number in 5322 03:55:38,559 --> 03:55:42,639 front of the decimal point is also 5323 03:55:40,478 --> 03:55:44,639 counted then when you are trying to 5324 03:55:42,639 --> 03:55:48,000 figure out the precision for your 5325 03:55:44,639 --> 03:55:50,799 floating point numbers in c plus plus so 5326 03:55:48,000 --> 03:55:53,359 here we have an example of how we can 5327 03:55:50,799 --> 03:55:56,159 declare and initialize floating point 5328 03:55:53,359 --> 03:55:58,960 numbers in our c plus plus program 5329 03:55:56,159 --> 03:56:01,119 number one is a really huge number so 5330 03:55:58,959 --> 03:56:03,438 you can guess that this is more than we 5331 03:56:01,120 --> 03:56:06,640 can handle with the float we're going to 5332 03:56:03,439 --> 03:56:10,800 see how this is handled by our compiler 5333 03:56:06,639 --> 03:56:12,959 a double has a precision of 15 so we 5334 03:56:10,799 --> 03:56:15,599 should expect to store more numbers than 5335 03:56:12,959 --> 03:56:18,398 we did in a float a long double should 5336 03:56:15,600 --> 03:56:21,439 be able to handle this without a problem 5337 03:56:18,398 --> 03:56:23,680 because if we go back to our table here 5338 03:56:21,439 --> 03:56:26,960 our long double here should be able to 5339 03:56:23,680 --> 03:56:29,279 handle this better than double but it is 5340 03:56:26,959 --> 03:56:31,679 possible that it is the same thing as 5341 03:56:29,279 --> 03:56:33,920 double depending on the compiler 5342 03:56:31,680 --> 03:56:35,600 implementation and if we try to print 5343 03:56:33,920 --> 03:56:37,760 out the sizes here we're going to see 5344 03:56:35,600 --> 03:56:40,079 what the compiler gives us we're going 5345 03:56:37,760 --> 03:56:42,478 to do this when we try this in visual 5346 03:56:40,079 --> 03:56:44,879 studio code okay and here we are trying 5347 03:56:42,478 --> 03:56:49,840 to print out the precision of these 5348 03:56:44,879 --> 03:56:53,198 numbers we can use a special setting on 5349 03:56:49,840 --> 03:56:55,359 our stream to control the maximum 5350 03:56:53,199 --> 03:56:57,439 precision that we can see here you see 5351 03:56:55,359 --> 03:56:59,359 that we are trying to show the precision 5352 03:56:57,439 --> 03:57:02,720 for each number there is a special 5353 03:56:59,359 --> 03:57:04,880 setting we can put on stdc out to make 5354 03:57:02,719 --> 03:57:07,519 it control the precision to a given 5355 03:57:04,879 --> 03:57:09,278 number here we are using 20 and we're 5356 03:57:07,520 --> 03:57:12,079 going to see that for number one this is 5357 03:57:09,279 --> 03:57:14,479 going to be seven digits for number two 5358 03:57:12,079 --> 03:57:16,799 it should be 15 digits or something 5359 03:57:14,478 --> 03:57:19,438 close to that the specifics are really 5360 03:57:16,799 --> 03:57:22,238 dependent on the compiler implementation 5361 03:57:19,439 --> 03:57:24,960 and for long double it should at least 5362 03:57:22,238 --> 03:57:27,520 be the same as double or even more in 5363 03:57:24,959 --> 03:57:30,318 some cases okay here we want to see that 5364 03:57:27,520 --> 03:57:32,800 we can also have narrowing errors 5365 03:57:30,318 --> 03:57:35,760 if we try to store something back in a 5366 03:57:32,799 --> 03:57:38,398 float and a float can't really handle it 5367 03:57:35,760 --> 03:57:41,840 here we have a number with a lot of 5368 03:57:38,398 --> 03:57:44,639 digits one two three four five six seven 5369 03:57:41,840 --> 03:57:47,439 eight nine and we know that the float 5370 03:57:44,639 --> 03:57:48,959 can only handle seven digits so 5371 03:57:47,439 --> 03:57:51,880 this is probably going to give us a 5372 03:57:48,959 --> 03:57:54,799 compiler error as we've seen for breast 5373 03:57:51,879 --> 03:57:56,959 initialization if we use functional 5374 03:57:54,799 --> 03:58:00,079 initialization this problem is not going 5375 03:57:56,959 --> 03:58:02,398 to be code at compile time so we're 5376 03:58:00,079 --> 03:58:04,398 going to end up with a chunked piece of 5377 03:58:02,398 --> 03:58:06,879 data and we're going to see this when we 5378 03:58:04,398 --> 03:58:09,039 play with this with visual studio code 5379 03:58:06,879 --> 03:58:11,920 and another format we can use with 5380 03:58:09,040 --> 03:58:15,279 floating points is that we can use 5381 03:58:11,920 --> 03:58:18,478 scientific notation and it is basically 5382 03:58:15,279 --> 03:58:22,000 a way we can use a power of 10 5383 03:58:18,478 --> 03:58:24,478 multiplier to multiply with our floating 5384 03:58:22,000 --> 03:58:28,159 point number so for example here number 5385 03:58:24,478 --> 03:58:31,920 5 is going to be this number 1 9 2 4 0 0 5386 03:58:28,159 --> 03:58:34,879 0 23 we can use a floating point 5387 03:58:31,920 --> 03:58:36,559 scientific notation to represent this 5388 03:58:34,879 --> 03:58:40,318 so what we're going to do we're going to 5389 03:58:36,559 --> 03:58:42,959 say 1.9 and we're going to multiply this 5390 03:58:40,318 --> 03:58:47,760 with 10 to the power of 8. this is what 5391 03:58:42,959 --> 03:58:50,478 this means this 8 here means to multiply 5392 03:58:47,760 --> 03:58:52,880 with 10 to the power of 8. and if we 5393 03:58:50,478 --> 03:58:55,840 multiply with that that basically means 5394 03:58:52,879 --> 03:58:59,839 moving the decimal point 8 digits to the 5395 03:58:55,840 --> 03:59:02,639 right so we can go one two three four 5396 03:58:59,840 --> 03:59:05,439 five six seven eight and it is going to 5397 03:59:02,639 --> 03:59:07,439 end at these three here and it is going 5398 03:59:05,439 --> 03:59:09,920 to be basically the same number number 5399 03:59:07,439 --> 03:59:12,159 seven is also another one and if we do 5400 03:59:09,920 --> 03:59:14,799 this we're going to pad with zeros after 5401 03:59:12,159 --> 03:59:17,439 these four and we're going to end up at 5402 03:59:14,799 --> 03:59:18,959 this same location as the three but it 5403 03:59:17,439 --> 03:59:21,760 is going to be a zero we're going to see 5404 03:59:18,959 --> 03:59:24,159 about that in a minute and number eight 5405 03:59:21,760 --> 03:59:27,359 is another floating point number and we 5406 03:59:24,159 --> 03:59:30,559 can use scientific notation to represent 5407 03:59:27,359 --> 03:59:32,159 it just like you see with number nine 5408 03:59:30,559 --> 03:59:34,478 we're basically going to have the same 5409 03:59:32,159 --> 03:59:37,119 thing when we have a minus in here it 5410 03:59:34,478 --> 03:59:40,318 means we're going to multiply with 10 to 5411 03:59:37,120 --> 03:59:43,520 the power of minus 11 and this is going 5412 03:59:40,318 --> 03:59:45,840 to give us a number below one thing zero 5413 03:59:43,520 --> 03:59:48,479 point zero zero seven or something like 5414 03:59:45,840 --> 03:59:51,840 that so we're going to go 11 digits 5415 03:59:48,478 --> 03:59:57,599 after the zero here and if we count one 5416 03:59:51,840 --> 04:00:00,478 two three four 5 6 7 8 9 10 11 and the 5417 03:59:57,600 --> 04:00:02,318 decimal point is going to go after three 5418 04:00:00,478 --> 04:00:04,159 and we're going to basically end up with 5419 04:00:02,318 --> 04:00:05,519 the same thing here we're going to play 5420 04:00:04,159 --> 04:00:07,680 with this in a minute and you're going 5421 04:00:05,520 --> 04:00:09,920 to see that all this makes sense and 5422 04:00:07,680 --> 04:00:12,159 don't forget that any piece of data we 5423 04:00:09,920 --> 04:00:15,600 store in a computer is going to be 5424 04:00:12,159 --> 04:00:18,318 ultimately represented by ones and zeros 5425 04:00:15,600 --> 04:00:20,720 in memory and for floating points we 5426 04:00:18,318 --> 04:00:22,799 don't use the number systems like we've 5427 04:00:20,719 --> 04:00:25,679 done for integers there is a special 5428 04:00:22,799 --> 04:00:27,759 system that is used to do that but it is 5429 04:00:25,680 --> 04:00:29,920 a little bit complicated to cover in a 5430 04:00:27,760 --> 04:00:32,238 course like this i am just going to 5431 04:00:29,920 --> 04:00:34,000 point you to it if you are interested 5432 04:00:32,238 --> 04:00:36,318 and when you are done with this course 5433 04:00:34,000 --> 04:00:38,398 you can come up and read on this the 5434 04:00:36,318 --> 04:00:40,559 main point is that any piece of data 5435 04:00:38,398 --> 04:00:43,119 that you represent in your c plus plus 5436 04:00:40,559 --> 04:00:46,478 program is going to be represented in 5437 04:00:43,120 --> 04:00:48,399 terms of ones and zeros in the memory of 5438 04:00:46,478 --> 04:00:50,719 your computer okay there are a few 5439 04:00:48,398 --> 04:00:53,039 things you can do with floating points 5440 04:00:50,719 --> 04:00:56,079 that you can't really do with integers 5441 04:00:53,040 --> 04:00:58,239 for example you can divide with zero if 5442 04:00:56,079 --> 04:01:00,478 you take a floating point number and 5443 04:00:58,238 --> 04:01:03,279 divide that with zero you're going to 5444 04:01:00,478 --> 04:01:05,039 get what we call infinity if the number 5445 04:01:03,279 --> 04:01:06,800 is positive you're going to get positive 5446 04:01:05,040 --> 04:01:09,199 infinity if the number is negative 5447 04:01:06,799 --> 04:01:11,358 you're going to get negative infinity 5448 04:01:09,199 --> 04:01:14,239 and you can take two floating point 5449 04:01:11,359 --> 04:01:15,680 numbers that are zeros and divide them 5450 04:01:14,238 --> 04:01:18,159 and what you're going to get really is 5451 04:01:15,680 --> 04:01:20,398 not a number it's something called nan 5452 04:01:18,159 --> 04:01:23,119 which stands for naughty number 5453 04:01:20,398 --> 04:01:25,599 and uh your program is going to not 5454 04:01:23,120 --> 04:01:27,040 crash but very few things you can't do 5455 04:01:25,600 --> 04:01:29,040 with these things for example you can't 5456 04:01:27,040 --> 04:01:31,520 add them up you shouldn't really do this 5457 04:01:29,040 --> 04:01:34,640 it's probably going to end up bad for 5458 04:01:31,520 --> 04:01:37,040 your program here we have a few examples 5459 04:01:34,639 --> 04:01:39,358 we have three numbers declared they are 5460 04:01:37,040 --> 04:01:42,560 doubles they must be occupying eight 5461 04:01:39,359 --> 04:01:46,079 bytes in memory number 10 is initialized 5462 04:01:42,559 --> 04:01:48,879 to 5.6 11 is initialized to zero because 5463 04:01:46,079 --> 04:01:50,879 we are using the braced initialization 5464 04:01:50,879 --> 04:01:56,478 and infinity here is the result of 5465 04:01:53,680 --> 04:01:59,359 dividing a number by zero so it's going 5466 04:01:56,478 --> 04:02:01,920 to be positive infinity because number 5467 04:01:59,359 --> 04:02:04,159 10 is a positive number if you go down 5468 04:02:01,920 --> 04:02:06,478 we're going to print number 10 divided 5469 04:02:04,159 --> 04:02:08,639 by number 11 and we're going to say 5470 04:02:06,478 --> 04:02:10,879 yields we're going to put the result out 5471 04:02:08,639 --> 04:02:12,559 we're going to see that down here on the 5472 04:02:10,879 --> 04:02:14,719 second line you see that we are trying 5473 04:02:12,559 --> 04:02:17,600 to add something to infinity and this is 5474 04:02:14,719 --> 04:02:21,119 also going to yield infinity because 5475 04:02:17,600 --> 04:02:23,120 think of infinity as something super big 5476 04:02:21,120 --> 04:02:24,800 and if you add something small to 5477 04:02:23,120 --> 04:02:26,640 something big it's still going to be 5478 04:02:24,799 --> 04:02:29,198 something big down here you see that 5479 04:02:26,639 --> 04:02:31,760 result equals number 11 divided by 5480 04:02:29,199 --> 04:02:33,520 number 12 and these two are zeros so 5481 04:02:31,760 --> 04:02:35,520 we're going to end up with none and 5482 04:02:33,520 --> 04:02:37,120 that's what we're going to print in this 5483 04:02:37,120 --> 04:02:41,600 down to the bottom okay you should 5484 04:02:39,520 --> 04:02:44,000 really remember to put in the suffixes 5485 04:02:41,600 --> 04:02:46,880 when you are initializing your floating 5486 04:02:44,000 --> 04:02:49,040 point numbers otherwise the default is 5487 04:02:46,879 --> 04:02:51,599 going to be double this is what we mean 5488 04:02:49,040 --> 04:02:54,319 by this you see this floating number we 5489 04:02:51,600 --> 04:02:56,399 have a suffix that is f to mean that it 5490 04:02:54,318 --> 04:02:58,639 is a floating number if you don't put 5491 04:02:56,398 --> 04:03:00,879 that in this is going to be interpreted 5492 04:02:58,639 --> 04:03:03,278 as double and the compiler is going to 5493 04:03:00,879 --> 04:03:05,198 try and turn that into a float by 5494 04:03:03,279 --> 04:03:07,439 chopping off things that can't really 5495 04:03:05,199 --> 04:03:09,359 fit in a float so to really make it 5496 04:03:07,439 --> 04:03:12,238 clear that you are stirring in and 5497 04:03:09,359 --> 04:03:13,920 loading number please put this f suffix 5498 04:03:12,238 --> 04:03:16,879 here for double you don't need to put 5499 04:03:13,920 --> 04:03:19,520 anything because it is the default 5500 04:03:16,879 --> 04:03:22,159 assumed by the compiler but for a long 5501 04:03:19,520 --> 04:03:24,560 double you have to suffix this with l 5502 04:03:22,159 --> 04:03:26,318 just like this so that's what we really 5503 04:03:24,559 --> 04:03:29,119 mean here we have seen that the 5504 04:03:26,318 --> 04:03:31,600 precision for float isn't really enough 5505 04:03:29,120 --> 04:03:33,840 for many of the computations we might 5506 04:03:31,600 --> 04:03:36,479 want to do in our c plus plus 5507 04:03:33,840 --> 04:03:39,040 applications okay we have talked a lot 5508 04:03:36,478 --> 04:03:41,358 about floating point numbers let's head 5509 04:03:39,040 --> 04:03:44,319 to visual studio code and actually play 5510 04:03:41,359 --> 04:03:46,318 with them okay here we are in our folder 5511 04:03:44,318 --> 04:03:48,159 that is going to contain our project the 5512 04:03:46,318 --> 04:03:51,600 project is going to be called fractional 5513 04:03:48,159 --> 04:03:53,279 numbers so we're going to copy our files 5514 04:03:53,279 --> 04:03:58,159 and put those in and i'm going to go up 5515 04:03:55,760 --> 04:04:00,000 again and open this in visual studio 5516 04:03:58,159 --> 04:04:02,719 code we're going to close whatever it is 5517 04:04:00,000 --> 04:04:04,719 we have opened now we're going to close 5518 04:04:02,719 --> 04:04:07,278 folder and we're going to open our 5519 04:04:04,719 --> 04:04:09,198 project in visual studio code and we're 5520 04:04:07,279 --> 04:04:11,439 going to do the usual and remove things 5521 04:04:09,199 --> 04:04:14,159 we don't need in here the first thing we 5522 04:04:11,439 --> 04:04:16,639 want to do is to store a few floats in 5523 04:04:14,159 --> 04:04:18,879 our program we're going to declare and 5524 04:04:16,639 --> 04:04:20,959 initialize a few floating point numbers 5525 04:04:18,879 --> 04:04:23,679 we have number one number two and number 5526 04:04:20,959 --> 04:04:26,159 three and we want to make it clear that 5527 04:04:23,680 --> 04:04:28,479 the sizes are what we expect we are 5528 04:04:26,159 --> 04:04:31,119 going to open a terminal window here and 5529 04:04:28,478 --> 04:04:34,000 we're going to build with gcc run the 5530 04:04:31,120 --> 04:04:36,160 task to weld with gcc the world is going 5531 04:04:34,000 --> 04:04:37,920 to be successful we're going to come to 5532 04:04:36,159 --> 04:04:39,600 our terminal click a little bit and 5533 04:04:37,920 --> 04:04:42,238 we're going to type dir we're going to 5534 04:04:39,600 --> 04:04:44,479 see that a program is in place if we run 5535 04:04:42,238 --> 04:04:48,398 it we are going to see that size of 5536 04:04:44,478 --> 04:04:51,760 float is 4 size of double is eight and 5537 04:04:48,398 --> 04:04:54,159 size of long double is 16. this is what 5538 04:04:51,760 --> 04:04:57,040 we expected another thing we can do is 5539 04:04:54,159 --> 04:04:59,600 try and play with the precision of these 5540 04:04:57,040 --> 04:05:01,439 numbers and see what we can really go 5541 04:04:59,600 --> 04:05:04,079 away with okay so we're going to 5542 04:05:01,439 --> 04:05:06,238 actually put in a piece of code to play 5543 04:05:04,079 --> 04:05:09,279 with this we're going to add a setting 5544 04:05:06,238 --> 04:05:11,520 to our sddc out stream so that we can 5545 04:05:09,279 --> 04:05:14,000 control the precision but for this to 5546 04:05:11,520 --> 04:05:17,040 work we need to bring in a library that 5547 04:05:14,000 --> 04:05:20,398 is called i o manip so we're going to 5548 04:05:17,040 --> 04:05:22,640 include that i o manip and how do i know 5549 04:05:20,398 --> 04:05:25,599 this i usually consult the c plus plus 5550 04:05:22,639 --> 04:05:27,198 standard library documentation and you 5551 04:05:25,600 --> 04:05:29,600 are not at the point where you can 5552 04:05:27,199 --> 04:05:32,720 really understand it right now so we're 5553 04:05:29,600 --> 04:05:35,040 going to be putting in these things here 5554 04:05:32,719 --> 04:05:38,318 and explaining them as we go and i made 5555 04:05:35,040 --> 04:05:39,120 the type here it is io manip and you see 5556 04:05:39,120 --> 04:05:43,680 now this is recognized our program 5557 04:05:41,120 --> 04:05:45,520 should compile so we're going to try and 5558 04:05:43,680 --> 04:05:47,840 weld it so that we can try this out 5559 04:05:45,520 --> 04:05:49,920 we're going to build with gcc the world 5560 04:05:47,840 --> 04:05:52,478 is going to go through we can run our 5561 04:05:49,920 --> 04:05:55,040 program we're going to do dur and clear 5562 04:05:52,478 --> 04:05:57,840 and rooster to run you see that things 5563 04:05:55,040 --> 04:06:01,120 start to go off at sex and if we count 5564 04:05:57,840 --> 04:06:03,520 what we have here it is one two three 5565 04:06:01,120 --> 04:06:07,040 four five six seven we can only 5566 04:06:03,520 --> 04:06:09,120 represent seven numbers accurately with 5567 04:06:07,040 --> 04:06:11,120 a float and after that we're just going 5568 04:06:09,120 --> 04:06:13,199 to have garbage because the computer 5569 04:06:11,120 --> 04:06:15,120 can't really make sense of whatever we 5570 04:06:15,120 --> 04:06:19,199 seven digits that we can legally 5571 04:06:17,359 --> 04:06:21,359 represent with the flood so the 5572 04:06:19,199 --> 04:06:23,920 precision for our floating point number 5573 04:06:21,359 --> 04:06:26,479 here is seven i really hope this makes 5574 04:06:23,920 --> 04:06:29,279 sense let's check out number two 5575 04:06:26,478 --> 04:06:32,639 we are using double so precision should 5576 04:06:29,279 --> 04:06:35,279 be around something like 15. so we are 5577 04:06:32,639 --> 04:06:37,439 putting in a number one through nine and 5578 04:06:35,279 --> 04:06:40,479 we are starting over zero one two three 5579 04:06:37,439 --> 04:06:43,439 four five up to zero again if we look at 5580 04:06:40,478 --> 04:06:45,599 what we have in the output here we have 5581 04:06:43,439 --> 04:06:47,040 one two three four five six seven eight 5582 04:06:48,719 --> 04:06:54,879 four five six and here things start 5583 04:06:52,000 --> 04:06:59,760 falling off after our sixth year and if 5584 04:06:54,879 --> 04:07:07,039 we count we're going to have 1 2 3 4 5 6 5585 04:06:59,760 --> 04:07:09,120 7 8 9 10 11 12 13 14 15 16 17 digits 5586 04:07:07,040 --> 04:07:12,160 this is the precision we are having this 5587 04:07:09,120 --> 04:07:14,960 compiler for double 17 so it is 5588 04:07:12,159 --> 04:07:17,439 something around 15 and we should expect 5589 04:07:14,959 --> 04:07:20,318 to have something more than that or at 5590 04:07:17,439 --> 04:07:23,040 least the same thing as double with long 5591 04:07:20,318 --> 04:07:25,359 double so if we look at this you see 5592 04:07:23,040 --> 04:07:28,239 that it is one two three four five six 5593 04:07:25,359 --> 04:07:32,238 seven eight nine zero one two three four 5594 04:07:28,238 --> 04:07:36,000 five six seven eight nine we are able to 5595 04:07:32,238 --> 04:07:38,158 represent up to 20 and you can see that 5596 04:07:36,000 --> 04:07:41,680 long double leaves up to even the 5597 04:07:38,158 --> 04:07:44,238 precision of 20 that we have set with 5598 04:07:41,680 --> 04:07:46,479 std set precision here okay i really 5599 04:07:44,238 --> 04:07:48,639 hope now that you have an idea about 5600 04:07:46,478 --> 04:07:51,679 these precisions we have with our 5601 04:07:48,639 --> 04:07:54,559 floating point types in c plus plus 5602 04:07:51,680 --> 04:07:55,359 programs another thing i want you to see 5603 04:07:55,359 --> 04:08:01,199 what kind of narrowing errors we can get 5604 04:07:58,238 --> 04:08:03,439 if we try to initialize with something 5605 04:08:01,199 --> 04:08:05,760 that can't really be represented by a 5606 04:08:03,439 --> 04:08:08,318 floating point number and here we have a 5607 04:08:05,760 --> 04:08:10,158 float it is called number four and we 5608 04:08:08,318 --> 04:08:12,559 are trying to put in something that is 5609 04:08:10,158 --> 04:08:15,520 bigger than we can really fit in it has 5610 04:08:12,559 --> 04:08:18,799 more than seven digits so one two three 5611 04:08:15,520 --> 04:08:21,120 four five six seven eight nine it is 5612 04:08:18,799 --> 04:08:23,039 more than seven digits and we would 5613 04:08:21,120 --> 04:08:24,840 expect to have a problem with this 5614 04:08:24,840 --> 04:08:30,719 initialization let's try and build this 5615 04:08:27,920 --> 04:08:32,799 with gcc we're going to do that and this 5616 04:08:30,719 --> 04:08:35,760 is going to work successfully so this is 5617 04:08:32,799 --> 04:08:37,438 going to silently chop off our number if 5618 04:08:35,760 --> 04:08:40,960 we try to print it out we're going to 5619 04:08:37,439 --> 04:08:43,120 say sddc out number four and we're going 5620 04:08:40,959 --> 04:08:45,759 to world again run the test tube world 5621 04:08:43,120 --> 04:08:47,920 so that our changes are taken into 5622 04:08:45,760 --> 04:08:49,840 effect i'm going to do dur and do 5623 04:08:47,920 --> 04:08:52,000 rooster and this is going to print our 5624 04:08:49,840 --> 04:08:53,359 number you're going to see that after 5625 04:08:53,359 --> 04:08:57,760 we have junk our number has been 5626 04:08:55,199 --> 04:08:59,680 basically chopped off and this is junk 5627 04:08:57,760 --> 04:09:02,000 because we can't really represent this 5628 04:08:59,680 --> 04:09:03,760 with the double the 16 here has been 5629 04:09:02,000 --> 04:09:05,840 chopped off because we can't basically 5630 04:09:03,760 --> 04:09:08,000 represent this with the floating point 5631 04:09:05,840 --> 04:09:10,238 number if we change this to double this 5632 04:09:08,000 --> 04:09:13,279 is going to probably give us what we 5633 04:09:10,238 --> 04:09:15,198 expect so let's run again world with gcc 5634 04:09:13,279 --> 04:09:17,760 the world is going to be good if we run 5635 04:09:15,199 --> 04:09:19,840 the program it is going to show us the 5636 04:09:17,760 --> 04:09:22,079 same thing okay this is a really good 5637 04:09:19,840 --> 04:09:24,238 learning change why are we having the 5638 04:09:24,238 --> 04:09:29,920 before even if we changed the type to 5639 04:09:27,439 --> 04:09:34,000 double try to think about it the reason 5640 04:09:29,920 --> 04:09:36,238 is the suffix here is still saying f and 5641 04:09:34,000 --> 04:09:38,719 this is going to be stored as float in 5642 04:09:36,238 --> 04:09:41,198 memory so if we want this to really be 5643 04:09:38,719 --> 04:09:42,478 treated like a double we can take out 5644 04:09:42,478 --> 04:09:46,478 and if we build again and run the 5645 04:09:44,559 --> 04:09:49,519 application now you're going to see that 5646 04:09:46,478 --> 04:09:52,318 we see the 23 this is basically being 5647 04:09:49,520 --> 04:09:53,840 correctly stored as a double this is a 5648 04:09:52,318 --> 04:09:56,799 really good thing you should keep in 5649 04:09:53,840 --> 04:09:58,398 mind if you don't put in your suffixes 5650 04:09:56,799 --> 04:10:00,559 here the number is going to be 5651 04:10:00,559 --> 04:10:06,398 by now i hope you see that if you really 5652 04:10:03,439 --> 04:10:07,920 try to put more than your floating point 5653 04:10:07,920 --> 04:10:12,398 the number is going to be chopped off 5654 04:10:09,920 --> 04:10:14,639 and only the precision that it can 5655 04:10:12,398 --> 04:10:16,959 handle is going to be stored in i hope 5656 04:10:14,639 --> 04:10:19,278 this makes sense and you see that the 5657 04:10:16,959 --> 04:10:22,079 behavior for numbers is really different 5658 04:10:19,279 --> 04:10:24,720 we got the compiler error when we try to 5659 04:10:22,079 --> 04:10:27,199 do narrowing conversion with integers 5660 04:10:24,719 --> 04:10:29,679 but now it is silently chopping off our 5661 04:10:27,199 --> 04:10:31,520 number if we try and use functional 5662 04:10:29,680 --> 04:10:34,559 notation i think we're going to get the 5663 04:10:31,520 --> 04:10:37,520 same result and change this back to a 5664 04:10:34,559 --> 04:10:40,639 float and uh use float here and if we 5665 04:10:37,520 --> 04:10:43,920 both again let's go to terminal run task 5666 04:10:40,639 --> 04:10:46,238 use gcc we're going to run this 5667 04:10:43,920 --> 04:10:49,279 let's run rooster and you see that we 5668 04:10:46,238 --> 04:10:52,079 get the same results our 23 is chopped 5669 04:10:49,279 --> 04:10:54,079 off here we can also use the assignment 5670 04:10:52,079 --> 04:10:57,199 notation here just like we did with 5671 04:10:54,079 --> 04:10:59,039 integers and this is going to do exactly 5672 04:10:57,199 --> 04:11:01,520 the same thing it is going to chop off 5673 04:10:59,040 --> 04:11:04,479 the 23 here and we're going to have some 5674 04:11:01,520 --> 04:11:06,319 garbage which is 16 in this case if we 5675 04:11:04,478 --> 04:11:08,799 weld the gun the world is going to go 5676 04:11:06,318 --> 04:11:10,879 through and if we run we're going to see 5677 04:11:08,799 --> 04:11:12,879 as it's in here okay narrowing 5678 04:11:10,879 --> 04:11:15,438 conversions are something you should be 5679 04:11:12,879 --> 04:11:17,278 aware of when storing your floating 5680 04:11:15,439 --> 04:11:19,680 point numbers and you should really 5681 04:11:17,279 --> 04:11:21,920 remember to put in your suffixes here 5682 04:11:19,680 --> 04:11:24,158 and other thing we have seen that we 5683 04:11:21,920 --> 04:11:25,439 should really play with is scientific 5684 04:11:25,439 --> 04:11:31,199 as the comment here says what we have 5685 04:11:27,359 --> 04:11:33,920 seen so far in floating point numbers is 5686 04:11:31,199 --> 04:11:37,040 fixed notation it is what we have here 5687 04:11:33,920 --> 04:11:39,199 but we can also use scientific notation 5688 04:11:37,040 --> 04:11:41,680 which is exactly what we described in 5689 04:11:39,199 --> 04:11:45,600 the slides and what we have here for 5690 04:11:41,680 --> 04:11:48,398 example is e8 and it means that we are 5691 04:11:45,600 --> 04:11:51,279 going to multiply this number here 5692 04:11:48,398 --> 04:11:53,680 with 10 to the power of 8. if you go 5693 04:11:51,279 --> 04:11:56,479 down here we have number 9 so we have 5694 04:11:56,478 --> 04:12:02,879 and we have e minus 11 and what this is 5695 04:12:00,000 --> 04:12:05,840 going to do is multiply with 10 to the 5696 04:12:02,879 --> 04:12:08,079 power of minus 11. this is how i can 5697 04:12:05,840 --> 04:12:10,000 represent this here and we can print 5698 04:12:08,079 --> 04:12:12,238 these numbers out to really play with 5699 04:12:10,000 --> 04:12:14,559 them to make a difference with what we 5700 04:12:12,238 --> 04:12:18,318 have been doing before we're going to do 5701 04:12:14,559 --> 04:12:20,158 sddc out and put a dividing line here 5702 04:12:18,318 --> 04:12:22,559 this is one way we can do this so we're 5703 04:12:22,559 --> 04:12:27,519 we're going to weld this so we're going 5704 04:12:24,398 --> 04:12:29,599 to run the attached world with gcc 10 5705 04:12:27,520 --> 04:12:32,158 the world is going to go through if we 5706 04:12:29,600 --> 04:12:35,120 do rooster we're going to see that our 5707 04:12:32,158 --> 04:12:39,279 number here number five is what we 5708 04:12:35,120 --> 04:12:42,399 expect number six is the same thing but 5709 04:12:39,279 --> 04:12:44,079 we used scientific notation to do this 5710 04:12:42,398 --> 04:12:46,879 because we took this number and 5711 04:12:44,079 --> 04:12:48,799 multiplied by 10 to the power of eight 5712 04:12:46,879 --> 04:12:51,839 number seven should be almost the same 5713 04:12:48,799 --> 04:12:54,079 thing but the lower 23 here is going to 5714 04:12:51,840 --> 04:12:56,238 be chopped off because we haven't 5715 04:12:54,079 --> 04:12:58,559 specified it we're just multiplying with 5716 04:12:56,238 --> 04:13:01,920 10 to the power of 8 so that's why we 5717 04:12:58,559 --> 04:13:04,158 have 0 0 here i hope that makes sense 5718 04:13:01,920 --> 04:13:06,158 and if you look at number 8 you see that 5719 04:13:04,158 --> 04:13:08,559 we stored it like this without using 5720 04:13:06,158 --> 04:13:11,119 scientific notation we used fixed 5721 04:13:08,559 --> 04:13:13,920 notation but on the terminal it's going 5722 04:13:11,120 --> 04:13:15,920 to be printed out in scientific notation 5723 04:13:13,920 --> 04:13:19,520 and this may be pretty cool the main 5724 04:13:15,920 --> 04:13:22,478 usage for scientific notation is to make 5725 04:13:19,520 --> 04:13:24,238 sense of numbers that are really huge so 5726 04:13:22,478 --> 04:13:25,358 the way you interpret this you're going 5727 04:13:25,359 --> 04:13:30,238 move these numbers after the decimal 5728 04:13:27,840 --> 04:13:32,000 points with 0 in front and it's 5729 04:13:30,238 --> 04:13:33,920 basically going to be like this so if 5730 04:13:32,000 --> 04:13:36,398 you count the zeros you have here we're 5731 04:13:36,398 --> 04:13:44,318 3 4 5 6 7 8 9 10 and 11 is going to be 5732 04:13:42,238 --> 04:13:46,559 directly after three and you're going to 5733 04:13:44,318 --> 04:13:49,760 get the number here this is what we mean 5734 04:13:46,559 --> 04:13:52,079 and number nine was explicitly stored in 5735 04:13:49,760 --> 04:13:54,079 using scientific notation and you see 5736 04:13:52,079 --> 04:13:56,238 that we have the same thing you can play 5737 04:13:54,079 --> 04:13:59,520 with these things and really make sense 5738 04:13:56,238 --> 04:14:01,680 of them and again we didn't specify any 5739 04:13:59,520 --> 04:14:04,319 suffix here so these are going to be 5740 04:14:01,680 --> 04:14:06,639 interpreted as doubles but this is not a 5741 04:14:04,318 --> 04:14:08,639 problem because they are doubles here 5742 04:14:06,639 --> 04:14:11,119 okay we are making some progress with 5743 04:14:08,639 --> 04:14:13,039 this lecture which is a little lengthy 5744 04:14:11,120 --> 04:14:15,359 the last thing we want to do is to play 5745 04:14:15,359 --> 04:14:18,960 we're going to put a message here to say 5746 04:14:17,040 --> 04:14:20,479 what we want to do and we're going to 5747 04:14:18,959 --> 04:14:22,318 have number 10 which is going to be 5748 04:14:20,478 --> 04:14:24,799 positive we're going to have number 11 5749 04:14:22,318 --> 04:14:26,559 which is going to be zero number 12 is 5750 04:14:26,559 --> 04:14:32,238 if we take 10 and divide that with 11 5751 04:14:30,000 --> 04:14:35,359 we're going to get infinity because we 5752 04:14:32,238 --> 04:14:37,039 are dividing a floating number with zero 5753 04:14:35,359 --> 04:14:38,559 if we try to print that out we're going 5754 04:14:38,559 --> 04:14:42,478 and we're going to get infinity for this 5755 04:14:40,398 --> 04:14:44,879 result here because we are trying to add 5756 04:14:42,478 --> 04:14:47,198 something small to infinity which is 5757 04:14:44,879 --> 04:14:49,278 something really big and if we all 5758 04:14:47,199 --> 04:14:50,479 trying to divide zero with zero in 5759 04:14:50,478 --> 04:14:54,079 we're going to get what is called 5760 04:14:52,079 --> 04:14:55,920 naughty number and if we print that out 5761 04:14:54,079 --> 04:14:59,920 we're going to see this in a minute 5762 04:14:55,920 --> 04:15:02,318 let's build this with gcc we're going to 5763 04:14:59,920 --> 04:15:05,439 weld successfully let's clear this out 5764 04:15:02,318 --> 04:15:07,600 so that we don't have noise in here and 5765 04:15:05,439 --> 04:15:11,199 we're going to run rooster and you see 5766 04:15:07,600 --> 04:15:12,720 here it says infinity and none which is 5767 04:15:12,719 --> 04:15:17,920 and if we go down here it is saying 5768 04:15:14,719 --> 04:15:20,719 number 10 divided by number 11 and 5769 04:15:17,920 --> 04:15:23,760 number 11 is zero you see that here it's 5770 04:15:20,719 --> 04:15:26,639 going to yield infinity if we add five 5771 04:15:23,760 --> 04:15:29,120 to infinity we're going to get infinity 5772 04:15:26,639 --> 04:15:31,599 and if we try to divide a zero by a zero 5773 04:15:29,120 --> 04:15:33,199 we're going to get none which is really 5774 04:15:31,600 --> 04:15:34,960 a way to tell you that you're doing 5775 04:15:33,199 --> 04:15:35,920 something wrong you should really be 5776 04:15:35,920 --> 04:15:40,318 one other thing i want you to see is 5777 04:15:37,600 --> 04:15:42,640 that if you turn this five into a minus 5778 04:15:40,318 --> 04:15:45,278 five and build again we're going to 5779 04:15:42,639 --> 04:15:46,959 build with gcc and we're going to run 5780 04:15:46,959 --> 04:15:52,238 we're going to get minus infinity so if 5781 04:15:49,520 --> 04:15:54,319 you divide a positive number with zero 5782 04:15:52,238 --> 04:15:56,478 you're going to get positive infinity 5783 04:15:54,318 --> 04:15:58,318 and if you divide a negative number with 5784 04:15:56,478 --> 04:16:00,799 infinity you're going to get negative 5785 04:15:58,318 --> 04:16:02,398 infinity and this is what we see here 5786 04:16:00,799 --> 04:16:03,920 this is really all we set out to do in 5787 04:16:02,398 --> 04:16:06,398 this lecture i hope you found it 5788 04:16:03,920 --> 04:16:08,478 interesting and again the big message is 5789 04:16:06,398 --> 04:16:11,439 that any data type you're going to be 5790 04:16:08,478 --> 04:16:13,920 handling in your c plus plus program is 5791 04:16:11,439 --> 04:16:16,000 going to ultimately be stored as ones 5792 04:16:13,920 --> 04:16:17,920 and zeros in memory and we have learned 5793 04:16:16,000 --> 04:16:20,079 about floating point numbers float 5794 04:16:17,920 --> 04:16:22,079 double and long and they have these 5795 04:16:20,079 --> 04:16:24,478 properties you see here and you should 5796 04:16:22,079 --> 04:16:26,318 use them with this in mind otherwise 5797 04:16:24,478 --> 04:16:28,559 you're going to have problems with your 5798 04:16:26,318 --> 04:16:30,639 precision make sure you are using the 5799 04:16:28,559 --> 04:16:32,639 correct precision that you need in your 5800 04:16:30,639 --> 04:16:34,559 program we are going to stop here in 5801 04:16:32,639 --> 04:16:37,519 this lecture the next one we're going to 5802 04:16:34,559 --> 04:16:40,158 try and learn about booleans go ahead 5803 04:16:37,520 --> 04:16:41,840 and finish up here and meet me there in 5804 04:16:40,158 --> 04:16:44,879 this lecture we're going to learn about 5805 04:16:41,840 --> 04:16:47,600 booleans booleans are types that can 5806 04:16:44,879 --> 04:16:51,039 store two states in c plus plots it can 5807 04:16:47,600 --> 04:16:53,359 store either true or false and we can 5808 04:16:51,040 --> 04:16:55,279 use this to really do decisions in our 5809 04:16:53,359 --> 04:16:57,680 program if you look in the program we 5810 04:16:55,279 --> 04:16:59,840 have here we have two variables declared 5811 04:16:59,840 --> 04:17:03,760 and one is called red light the other is 5812 04:17:03,760 --> 04:17:09,120 and we have true and false inside one 5813 04:17:06,719 --> 04:17:12,238 thing we can do in c plus plus is make 5814 04:17:09,120 --> 04:17:13,439 decisions we can say if the red light is 5815 04:17:13,439 --> 04:17:19,199 print a message that says stop and if 5816 04:17:16,318 --> 04:17:21,600 it's not red we can print a message that 5817 04:17:19,199 --> 04:17:24,079 says go through we can do that and 5818 04:17:21,600 --> 04:17:26,399 booleans are really used in this way to 5819 04:17:24,079 --> 04:17:29,039 make decisions in your c plus plus 5820 04:17:26,398 --> 04:17:31,760 program we haven't really learned about 5821 04:17:29,040 --> 04:17:33,920 if statements in this course but i am 5822 04:17:31,760 --> 04:17:36,079 throwing this in here to really drive 5823 04:17:33,920 --> 04:17:38,639 the point home that we can use this to 5824 04:17:36,079 --> 04:17:40,639 make decisions in our program down here 5825 04:17:38,639 --> 04:17:42,879 we have another way we can do this we 5826 04:17:40,639 --> 04:17:46,238 can actually use the variable itself 5827 04:17:42,879 --> 04:17:48,478 without checking if it is equal to true 5828 04:17:46,238 --> 04:17:50,959 or false and we are going to get the 5829 04:17:48,478 --> 04:17:53,039 same message the point here is that if 5830 04:17:50,959 --> 04:17:55,358 green light is true we're going to say 5831 04:17:53,040 --> 04:17:57,279 the light is green if it's not we're 5832 04:17:55,359 --> 04:17:59,600 going to say the light is not green you 5833 04:17:57,279 --> 04:18:02,079 can do something like this and again the 5834 04:17:59,600 --> 04:18:05,199 big point here is that booleans data 5835 04:18:02,079 --> 04:18:07,760 types are used to store two states true 5836 04:18:05,199 --> 04:18:11,120 or false in your c plus plus program 5837 04:18:07,760 --> 04:18:13,520 okay by default if you print a boolean 5838 04:18:11,120 --> 04:18:15,680 that has two in it you're going to get 5839 04:18:13,520 --> 04:18:18,640 true and this is what you see here and 5840 04:18:15,680 --> 04:18:20,800 if you print one that has zero in it 5841 04:18:18,639 --> 04:18:22,719 you're going to get false we can change 5842 04:18:20,799 --> 04:18:25,840 that with a special setting that we can 5843 04:18:22,719 --> 04:18:28,719 apply to our std out stream and you do 5844 04:18:25,840 --> 04:18:29,840 that like this with std bull alpha and 5845 04:18:28,719 --> 04:18:31,278 we're going to play with this in a 5846 04:18:31,279 --> 04:18:35,920 if we try to print with this setting 5847 04:18:33,359 --> 04:18:38,880 applied we're going to see true on false 5848 04:18:35,920 --> 04:18:40,079 and not c 0 and 1 like we did before 5849 04:18:38,879 --> 04:18:42,158 we're going to play with this in a 5850 04:18:40,079 --> 04:18:44,318 minute and you're going to see another 5851 04:18:42,158 --> 04:18:45,680 thing you should know is that booleans 5852 04:18:45,680 --> 04:18:50,800 8 bits in memory they take up an entire 5853 04:18:48,719 --> 04:18:53,519 bite so you may think that this is 5854 04:18:50,799 --> 04:18:55,840 wasteful and it is but with the amount 5855 04:18:53,520 --> 04:18:58,479 of memory that we have today this is 5856 04:18:55,840 --> 04:19:00,960 really not a big problem but if you are 5857 04:18:58,478 --> 04:19:03,119 working on a device where memory is 5858 04:19:00,959 --> 04:19:05,438 really a problem there are techniques 5859 04:19:03,120 --> 04:19:07,600 you can use to pack even more data in a 5860 04:19:05,439 --> 04:19:10,079 byte and we're going to learn about that 5861 04:19:07,600 --> 04:19:12,880 later in the course okay so this is 5862 04:19:10,079 --> 04:19:14,959 really all i have to say about booleans 5863 04:19:12,879 --> 04:19:17,679 we're going to go to visual studio code 5864 04:19:14,959 --> 04:19:20,639 and play with them in code okay here i 5865 04:19:17,680 --> 04:19:22,559 am in the folder for my project here i 5866 04:19:20,639 --> 04:19:23,760 am going to do the usual and copy the 5867 04:19:23,760 --> 04:19:28,800 and i am going to go in my project on 5868 04:19:26,000 --> 04:19:31,040 booleans i am going to go up and open 5869 04:19:28,799 --> 04:19:33,519 this up in visual studio code i am going 5870 04:19:31,040 --> 04:19:35,920 to close the current folder open the new 5871 04:19:33,520 --> 04:19:38,319 one and browse to my project this is 5872 04:19:35,920 --> 04:19:40,238 going to open up in visual studio code 5873 04:19:38,318 --> 04:19:42,559 and i am going to close the welcome 5874 04:19:40,238 --> 04:19:45,279 screen here i don't need it anymore 5875 04:19:42,559 --> 04:19:48,000 so i'm going to remove what i don't need 5876 04:19:45,279 --> 04:19:50,720 and we are going to play with the exact 5877 04:19:48,000 --> 04:19:52,879 code that we had in the slides we're 5878 04:19:50,719 --> 04:19:54,879 going to declare two variables one is 5879 04:19:52,879 --> 04:19:56,799 going to be red light the other is going 5880 04:19:54,879 --> 04:19:59,198 to be green light they are going to be 5881 04:19:56,799 --> 04:20:02,158 booleans and in red light we're going to 5882 04:19:59,199 --> 04:20:04,239 store a true and story false in green 5883 04:20:02,158 --> 04:20:06,719 light and we can make a decision like 5884 04:20:04,238 --> 04:20:09,600 this so try to guess what we're going to 5885 04:20:06,719 --> 04:20:12,639 plant in this program we're going to try 5886 04:20:09,600 --> 04:20:14,800 and decide and say if the red light is 5887 04:20:12,639 --> 04:20:16,799 true we're going to print stop 5888 04:20:14,799 --> 04:20:19,119 if it's not true we're going to fall in 5889 04:20:19,120 --> 04:20:25,520 go through if we look at our program in 5890 04:20:22,318 --> 04:20:27,760 red light we have a true so this should 5891 04:20:25,520 --> 04:20:30,319 really win and we should get in this 5892 04:20:27,760 --> 04:20:32,478 block here i hope this makes sense so 5893 04:20:30,318 --> 04:20:34,799 let's try and build this program to see 5894 04:20:32,478 --> 04:20:36,959 that it actually works so we're going to 5895 04:20:34,799 --> 04:20:39,599 build with the gcc we're going to run 5896 04:20:36,959 --> 04:20:41,759 our program and our terminal just went 5897 04:20:39,600 --> 04:20:43,120 away we're going to create a new one no 5898 04:20:43,120 --> 04:20:48,560 and uh if we do dir we're going to see 5899 04:20:48,559 --> 04:20:54,799 we run it it's going to say stop okay so 5900 04:20:51,600 --> 04:20:56,640 in red light we have true the test is 5901 04:20:54,799 --> 04:20:59,198 going to succeed here and we're going to 5902 04:20:56,639 --> 04:21:01,519 fall in this block and say stop i hope 5903 04:20:59,199 --> 04:21:03,120 this makes sense you can try to store a 5904 04:21:03,120 --> 04:21:09,439 and both again going to world with gcc 5905 04:21:06,799 --> 04:21:12,158 if we run this program now it's going to 5906 04:21:09,439 --> 04:21:14,398 say go through because red light is not 5907 04:21:12,158 --> 04:21:16,879 true anymore this is really what we mean 5908 04:21:14,398 --> 04:21:19,840 when we say that we can make decisions 5909 04:21:16,879 --> 04:21:22,879 based on booleans in our c plus plus 5910 04:21:19,840 --> 04:21:26,000 code we can also try another example 5911 04:21:22,879 --> 04:21:26,879 we're going to say if green light is 5912 04:21:26,879 --> 04:21:30,559 we're going to say the light is green if 5913 04:21:28,879 --> 04:21:32,398 it's not we're going to say the light is 5914 04:21:30,559 --> 04:21:35,680 not green and this is another form we 5915 04:21:32,398 --> 04:21:36,559 can use instead of typing the entire 5916 04:21:36,559 --> 04:21:40,799 comparing red light to true here and 5917 04:21:39,359 --> 04:21:43,040 again we're going to learn about all 5918 04:21:43,040 --> 04:21:48,000 it is just because i thought that 5919 04:21:45,760 --> 04:21:50,559 talking about bulls without showing 5920 04:21:48,000 --> 04:21:53,439 tests like this is really not going to 5921 04:21:50,559 --> 04:21:55,519 drive the point home for you so try to 5922 04:21:53,439 --> 04:21:57,920 understand this as simple tests you can 5923 04:21:55,520 --> 04:21:59,920 do in math if one value is true i am 5924 04:21:57,920 --> 04:22:01,840 going to do something if it's not true 5925 04:21:59,920 --> 04:22:04,000 i'm going to do something else this is 5926 04:22:01,840 --> 04:22:06,158 what we are doing here okay this program 5927 04:22:04,000 --> 04:22:08,559 here is going to check if the green 5928 04:22:06,158 --> 04:22:11,119 light is true so we're going to look 5929 04:22:08,559 --> 04:22:14,158 here green light is not true so we 5930 04:22:11,120 --> 04:22:16,319 should fall here and print this message 5931 04:22:14,158 --> 04:22:18,559 that the light is not green 5932 04:22:16,318 --> 04:22:20,719 let's build the program the world is 5933 04:22:18,559 --> 04:22:21,519 going to go through we're going to run 5934 04:22:21,520 --> 04:22:25,760 and you're going to see go through and 5935 04:22:23,520 --> 04:22:27,359 the light is not green this is what we 5936 04:22:25,760 --> 04:22:30,318 have here another thing we can actually 5937 04:22:27,359 --> 04:22:33,439 try to see is the amount of memory that 5938 04:22:30,318 --> 04:22:36,238 a boolean takes up so we can use sizeof 5939 04:22:33,439 --> 04:22:39,040 to do that we have seen this before if 5940 04:22:36,238 --> 04:22:39,920 we build with gcc we're going to go 5941 04:22:39,920 --> 04:22:43,840 and we can run our program to actually 5942 04:22:42,000 --> 04:22:46,318 this message and you're going to see 5943 04:22:43,840 --> 04:22:48,639 that bull is actually going to take up 5944 04:22:46,318 --> 04:22:50,959 one byte in memory this is what we saw 5945 04:22:48,639 --> 04:22:53,760 in the slides another thing that can be 5946 04:22:50,959 --> 04:22:56,639 really confusing is printing out boolean 5947 04:22:53,760 --> 04:22:59,439 values for example here we are trying to 5948 04:22:56,639 --> 04:23:01,278 print great light and green light let's 5949 04:22:59,439 --> 04:23:04,398 go up and actually change them a little 5950 04:23:01,279 --> 04:23:06,079 bit for example take green light here 5951 04:23:06,079 --> 04:23:11,359 and we're going to go down again and 5952 04:23:08,559 --> 04:23:13,680 here we are trying to print them out red 5953 04:23:11,359 --> 04:23:15,920 light is going to print whatever value 5954 04:23:13,680 --> 04:23:17,520 we have stored then and green light is 5955 04:23:15,920 --> 04:23:19,920 going to store whatever value we have 5956 04:23:17,520 --> 04:23:21,760 stored in and again true is going to be 5957 04:23:21,760 --> 04:23:26,639 and false is going to be printed out as 5958 04:23:24,079 --> 04:23:29,840 a zero let's try and build this we're 5959 04:23:26,639 --> 04:23:31,599 going to build with our gcc compiler the 5960 04:23:29,840 --> 04:23:33,680 world is going to be good we're going to 5961 04:23:31,600 --> 04:23:36,640 run this and you're going to see that 5962 04:23:33,680 --> 04:23:38,318 red light is zero because we have a full 5963 04:23:38,318 --> 04:23:44,719 and green light is going to be one 5964 04:23:41,040 --> 04:23:47,199 because we haven't a true here if you 5965 04:23:44,719 --> 04:23:48,959 try to print out boolean variables 5966 04:23:47,199 --> 04:23:51,600 you're going to get one and zero by 5967 04:23:48,959 --> 04:23:53,438 default if you really want to see true 5968 04:23:51,600 --> 04:23:56,399 and false there is a special setting you 5969 04:23:53,439 --> 04:24:01,040 can apply to htdc out and you do that by 5970 04:24:01,040 --> 04:24:06,479 alpha and you do it like this 5971 04:24:03,600 --> 04:24:08,479 and if you try to print again we are 5972 04:24:06,478 --> 04:24:10,799 going to get true and false right now 5973 04:24:08,478 --> 04:24:13,840 okay let's try and print again and we're 5974 04:24:10,799 --> 04:24:15,278 going to work this with gcc and we're 5975 04:24:15,279 --> 04:24:20,399 and now you see that we see red light is 5976 04:24:20,398 --> 04:24:25,760 this is really all i had to share about 5977 04:24:22,639 --> 04:24:28,398 booleans in this lecture i really hope 5978 04:24:25,760 --> 04:24:30,880 it drives the point home that booleans 5979 04:24:28,398 --> 04:24:32,639 are mostly used in making decisions in 5980 04:24:30,879 --> 04:24:35,358 your program and that's what you're 5981 04:24:32,639 --> 04:24:37,519 going to be mostly using them for and 5982 04:24:35,359 --> 04:24:40,479 another thing is that they take up one 5983 04:24:37,520 --> 04:24:42,319 bite in your memory we are going to stop 5984 04:24:40,478 --> 04:24:44,879 here in this lecture the next one we're 5985 04:24:42,318 --> 04:24:46,318 going to try and learn about characters 5986 04:24:46,318 --> 04:24:50,478 go ahead and finish up here and meet me 5987 04:24:48,719 --> 04:24:53,278 there in this lecture we're going to 5988 04:24:50,478 --> 04:24:56,079 learn about characters and text and we 5989 04:24:53,279 --> 04:24:58,479 use a data type called car in c plus 5990 04:24:56,079 --> 04:25:00,238 plus to represent characters and this is 5991 04:24:58,478 --> 04:25:02,959 basically how you use it you store in 5992 04:25:00,238 --> 04:25:05,198 characters like abc and here you see 5993 04:25:05,199 --> 04:25:10,560 o w and you put your characters in 5994 04:25:08,318 --> 04:25:12,559 single quotes like this to mean that 5995 04:25:10,559 --> 04:25:14,238 they are characters if you don't do that 5996 04:25:12,559 --> 04:25:16,799 you're going to get a compiler error 5997 04:25:14,238 --> 04:25:19,760 after you have your characters declared 5998 04:25:16,799 --> 04:25:22,559 and defined like this you can print them 5999 04:25:19,760 --> 04:25:26,398 out with stdc out and we're going to see 6000 04:25:22,559 --> 04:25:28,478 them on the terminal car occupies one 6001 04:25:26,398 --> 04:25:31,278 byte in memory so it's going to take up 6002 04:25:28,478 --> 04:25:35,760 eight bits okay so one thing we can do 6003 04:25:31,279 --> 04:25:38,319 is map each value in these 256 possible 6004 04:25:35,760 --> 04:25:40,158 values to a character and we can do 6005 04:25:38,318 --> 04:25:41,519 something like this for example we can 6006 04:25:41,520 --> 04:25:47,840 33 represents the exclamation mark 6007 04:25:44,799 --> 04:25:50,559 48 represents the character 0 49 6008 04:25:47,840 --> 04:25:53,600 represents the character 1 50 represents 6009 04:25:50,559 --> 04:25:56,719 the character 2 and we can keep going up 6010 04:25:53,600 --> 04:25:58,318 and say for example 70 represents f 71 6011 04:25:59,000 --> 04:26:05,158 112 represents p you can really use a 6012 04:26:02,398 --> 04:26:07,840 mapping like this and come up with 6013 04:26:05,158 --> 04:26:10,000 representations of your characters in 6014 04:26:07,840 --> 04:26:12,559 memory and this is what we do you can 6015 04:26:10,000 --> 04:26:15,120 learn about this encoding and on the 6016 04:26:12,559 --> 04:26:18,719 link here this is a real thing and it is 6017 04:26:15,120 --> 04:26:21,600 called the ascii encoding that takes 6018 04:26:18,719 --> 04:26:23,519 characters and stores them in one byte 6019 04:26:21,600 --> 04:26:26,640 and the data type to store these 6020 04:26:23,520 --> 04:26:28,720 characters in c plus plus is car if this 6021 04:26:26,639 --> 04:26:30,559 doesn't make sense please bear with me 6022 04:26:28,719 --> 04:26:32,799 we're going to play with this in visual 6023 04:26:30,559 --> 04:26:35,278 studio code and it is most probably 6024 04:26:32,799 --> 04:26:38,959 going to make sense okay it is possible 6025 04:26:35,279 --> 04:26:41,680 to assign a valid ascii code to 6026 04:26:38,959 --> 04:26:44,000 a car variable and the corresponding 6027 04:26:41,680 --> 04:26:46,559 character will be stored in that piece 6028 04:26:44,000 --> 04:26:48,478 of memory so once you have that you can 6029 04:26:46,559 --> 04:26:51,119 choose to interpret that either as a 6030 04:26:48,478 --> 04:26:53,039 character or as a number that is 6031 04:26:51,120 --> 04:26:55,680 something like an integer this is what 6032 04:26:53,040 --> 04:26:59,199 we really mean if we declare a variable 6033 04:26:55,680 --> 04:27:02,238 called value which is of car type 65 is 6034 04:26:59,199 --> 04:27:04,800 going to be stored in memory but the c 6035 04:27:02,238 --> 04:27:07,840 plus plus program is going to interpret 6036 04:27:04,799 --> 04:27:10,879 this as a character if we go back to our 6037 04:27:07,840 --> 04:27:12,238 table and see what is represented by 65 6038 04:27:10,879 --> 04:27:15,839 this is what we're going to find we're 6039 04:27:12,238 --> 04:27:18,079 going to find that 65 represents a 6040 04:27:15,840 --> 04:27:20,158 and if we print this out by default it's 6041 04:27:18,079 --> 04:27:22,799 going to print a it's not going to print 6042 04:27:20,158 --> 04:27:26,000 65 but there is a way you can take this 6043 04:27:22,799 --> 04:27:28,879 character value and turn that into an 6044 04:27:26,000 --> 04:27:32,000 integer and print it out and you see 6045 04:27:28,879 --> 04:27:34,158 that here we are saying value and and we 6046 04:27:34,158 --> 04:27:38,959 and and in the parents we have a value 6047 04:27:36,879 --> 04:27:42,318 this is basically a way to say we wanted 6048 04:27:38,959 --> 04:27:44,079 to take the value and interpret that as 6049 04:27:42,318 --> 04:27:46,398 an integer and we're going to learn a 6050 04:27:44,079 --> 04:27:48,799 little more about static cast later in 6051 04:27:46,398 --> 04:27:51,198 the course but it is basically a way to 6052 04:27:48,799 --> 04:27:54,000 transform between data types and in this 6053 04:27:51,199 --> 04:27:56,399 case we are trying to transform from car 6054 04:27:54,000 --> 04:27:59,120 to end and we're going to print this out 6055 04:27:56,398 --> 04:28:01,680 okay what we just described is the ascii 6056 04:27:59,120 --> 04:28:05,279 encoding a technique by which we can map 6057 04:28:05,279 --> 04:28:10,000 a byte two characters and use that to 6058 04:28:08,000 --> 04:28:13,040 represent characters this technique 6059 04:28:10,000 --> 04:28:15,680 doesn't work well if you have languages 6060 04:28:13,040 --> 04:28:18,319 like arabic japanese and chinese because 6061 04:28:15,680 --> 04:28:19,680 they are completely different beasts 6062 04:28:18,318 --> 04:28:21,920 there are better ways to represent 6063 04:28:19,680 --> 04:28:24,800 characters in your c plus program and 6064 04:28:21,920 --> 04:28:27,120 that's usually unicode but that's really 6065 04:28:24,799 --> 04:28:29,198 out of scope for a course like this so 6066 04:28:27,120 --> 04:28:30,880 i'm not going to describe that anymore 6067 04:28:29,199 --> 04:28:32,800 okay this is really all we need to learn 6068 04:28:30,879 --> 04:28:35,198 about characters we're going to head to 6069 04:28:32,799 --> 04:28:38,559 visual studio code and play with them 6070 04:28:35,199 --> 04:28:40,640 okay here i am in my working folder i am 6071 04:28:38,559 --> 04:28:43,119 going to go in my template project i am 6072 04:28:40,639 --> 04:28:45,760 going to copy things over i am going to 6073 04:28:43,120 --> 04:28:48,560 put that in my characters and text 6074 04:28:45,760 --> 04:28:50,238 project and i am going to go up and open 6075 04:28:48,559 --> 04:28:52,079 this up in visual studio code we're 6076 04:28:50,238 --> 04:28:54,639 going to close the welcome screen here 6077 04:28:52,079 --> 04:28:57,279 we're going to open up main cpp and 6078 04:28:54,639 --> 04:28:59,599 we're going to put in the code to play 6079 04:28:59,600 --> 04:29:05,120 the first thing we can do is declare 6080 04:29:02,000 --> 04:29:08,158 characters like this we can put in a r 6081 04:29:05,120 --> 04:29:10,800 r o w here and we're going to see this 6082 04:29:08,158 --> 04:29:12,959 printed out if we run this program we 6083 04:29:10,799 --> 04:29:14,559 are going to put in some code to play 6084 04:29:14,559 --> 04:29:19,760 we have a few variables declared that 6085 04:29:19,760 --> 04:29:24,000 we go from character 1 through character 6086 04:29:22,079 --> 04:29:25,840 5 and these are the characters we are 6087 04:29:24,000 --> 04:29:27,760 storing and this is and this is the 6088 04:29:25,840 --> 04:29:30,398 format you're really going to use to 6089 04:29:27,760 --> 04:29:33,199 declare your characters after we have 6090 04:29:30,398 --> 04:29:36,238 them declared we can print them out like 6091 04:29:33,199 --> 04:29:38,239 we print any other variable and we can 6092 04:29:36,238 --> 04:29:40,398 try and compile this program and see 6093 04:29:38,238 --> 04:29:42,559 what we're going to get but before we do 6094 04:29:40,398 --> 04:29:44,959 that i would invite you to try and guess 6095 04:29:44,959 --> 04:29:50,398 and we're going to build with our gcc 6096 04:29:48,238 --> 04:29:52,639 task we're going to world the build is 6097 04:29:50,398 --> 04:29:54,719 going to go through and if we run 6098 04:29:52,639 --> 04:29:56,158 rooster we're going to see that we are 6099 04:29:56,158 --> 04:30:02,959 a r o w we are storing our characters 6100 04:30:00,238 --> 04:30:04,639 and and we are able to print them out 6101 04:30:02,959 --> 04:30:06,879 this is really how you work with these 6102 04:30:04,639 --> 04:30:10,639 guys another thing i want you to see is 6103 04:30:06,879 --> 04:30:13,759 the size that a character occupies 6104 04:30:10,639 --> 04:30:16,238 in memory by that i mean card type it's 6105 04:30:13,760 --> 04:30:19,559 going to occupy one byte in memory and 6106 04:30:16,238 --> 04:30:23,039 that means that we can only have 6107 04:30:19,559 --> 04:30:26,799 256 different values that go between 0 6108 04:30:23,040 --> 04:30:28,720 and 255 if we have decided to store in 6109 04:30:26,799 --> 04:30:30,879 positive numbers we are using the 6110 04:30:28,719 --> 04:30:33,278 knowledge we learned from the integers 6111 04:30:30,879 --> 04:30:35,679 lecture if these calculations we are 6112 04:30:33,279 --> 04:30:38,319 doing here don't make sense please go 6113 04:30:35,680 --> 04:30:41,040 back and check that out so down here we 6114 04:30:38,318 --> 04:30:44,158 are declaring a character type the name 6115 04:30:41,040 --> 04:30:46,720 is value and we are putting in a 65. the 6116 04:30:44,158 --> 04:30:48,959 thing is if you try to print this value 6117 04:30:46,719 --> 04:30:51,920 here it's not going to print 65 it's 6118 04:30:48,959 --> 04:30:54,959 going to print a which is a character 6119 04:30:51,920 --> 04:30:58,478 representation of 65. if you remember 6120 04:30:54,959 --> 04:31:01,519 from our ascii table we had these 6121 04:30:58,478 --> 04:31:05,599 mappings between characters and integers 6122 04:31:01,520 --> 04:31:08,479 and 65 mapped to the character a 6123 04:31:05,600 --> 04:31:10,800 upper case so if we print this out this 6124 04:31:08,478 --> 04:31:12,318 is going to give us a but we have the 6125 04:31:12,318 --> 04:31:18,000 interpret this as a number and we can 6126 04:31:14,959 --> 04:31:20,238 pass this through a static cast and it 6127 04:31:18,000 --> 04:31:22,079 is going to take a value turn this into 6128 04:31:20,238 --> 04:31:24,799 an integer and we're going to be 6129 04:31:22,079 --> 04:31:26,879 printing this integer on the console and 6130 04:31:24,799 --> 04:31:28,799 we're going to see it we're going to try 6131 04:31:26,879 --> 04:31:31,438 this out so we're going to run the task 6132 04:31:28,799 --> 04:31:34,398 to build with gcc the build is going to 6133 04:31:31,439 --> 04:31:36,880 go through we're going to run rooster 6134 04:31:34,398 --> 04:31:39,039 and you're going to see that value is a 6135 04:31:39,040 --> 04:31:46,000 and the value and is 65 because we 6136 04:31:42,799 --> 04:31:48,719 decided to interpret whatever is in this 6137 04:31:46,000 --> 04:31:51,359 value as an integer and not as a 6138 04:31:48,719 --> 04:31:52,799 character as it is done by default this 6139 04:31:51,359 --> 04:31:55,120 is really all we set out to do in this 6140 04:31:52,799 --> 04:31:56,799 lecture i hope you found it interesting 6141 04:31:55,120 --> 04:31:58,399 we are going to stop here in this 6142 04:31:56,799 --> 04:32:01,599 lecture in the next one we're going to 6143 04:31:58,398 --> 04:32:03,278 try and learn about the auto key world 6144 04:32:03,279 --> 04:32:07,199 go ahead and finish up here and meet me 6145 04:32:05,520 --> 04:32:10,238 there in this lecture we're going to 6146 04:32:07,199 --> 04:32:11,279 learn about the auto keyword in c plus 6147 04:32:11,279 --> 04:32:16,640 this is a keyword that is going to let 6148 04:32:14,158 --> 04:32:18,799 the compiler deduce the type for you and 6149 04:32:16,639 --> 04:32:22,238 this is going to come in handy when you 6150 04:32:18,799 --> 04:32:24,799 have longer type names that are really 6151 04:32:22,238 --> 04:32:27,359 hard to type we can't really give you an 6152 04:32:24,799 --> 04:32:31,358 example of that because that requires to 6153 04:32:27,359 --> 04:32:34,000 have a good number of concepts mastered 6154 04:32:31,359 --> 04:32:35,920 in clause plus but we are measuring this 6155 04:32:34,000 --> 04:32:38,398 here for completeness because we are 6156 04:32:35,920 --> 04:32:40,559 talking about variables and data types 6157 04:32:38,398 --> 04:32:42,799 so let's hear how we can use this here 6158 04:32:40,559 --> 04:32:46,238 we have an example where we are saying 6159 04:32:42,799 --> 04:32:49,198 autovar one and what we initialize this 6160 04:32:46,238 --> 04:32:51,920 with is an integer so the compiler is 6161 04:32:49,199 --> 04:32:54,079 really going to try and guess the type 6162 04:32:51,920 --> 04:32:56,559 of this thing and that's the type it's 6163 04:32:54,079 --> 04:32:59,840 going to assign to variable one this is 6164 04:32:56,559 --> 04:33:01,439 what ocho is really used for if we pass 6165 04:33:01,439 --> 04:33:04,958 inside and it is a double because we 6166 04:33:06,638 --> 04:33:12,159 so it is going to deduce a double if we 6167 04:33:09,520 --> 04:33:15,118 pass a suffix of f it is going to 6168 04:33:12,159 --> 04:33:17,359 interpret this as a fluid if we pass l 6169 04:33:15,118 --> 04:33:19,680 it is going to interpret this as long 6170 04:33:17,359 --> 04:33:21,920 double and if we pass in a character 6171 04:33:19,680 --> 04:33:24,159 it's going to interpret this as a 6172 04:33:21,919 --> 04:33:27,519 character and this is really the essence 6173 04:33:24,159 --> 04:33:30,879 of what auto is used for it is used when 6174 04:33:27,520 --> 04:33:33,439 you don't really want to explicitly type 6175 04:33:30,879 --> 04:33:36,959 the type of your variable and you want 6176 04:33:33,438 --> 04:33:38,798 the compiler to fill in that for you or 6177 04:33:36,958 --> 04:33:40,399 it gets it for you down here you see 6178 04:33:38,798 --> 04:33:43,118 that we have a few examples with 6179 04:33:40,400 --> 04:33:45,439 integers so we can make this unsigned 6180 04:33:43,118 --> 04:33:48,079 lung through this suffixes and the 6181 04:33:45,438 --> 04:33:51,118 compiler is going to deduce the type 6182 04:33:48,080 --> 04:33:52,879 based on what we pass in our initializer 6183 04:33:51,118 --> 04:33:55,520 here down here you see that we have a 6184 04:33:52,879 --> 04:33:57,920 few examples with integers if we pass a 6185 04:33:55,520 --> 04:34:01,039 suffix of u this means that we are 6186 04:33:57,919 --> 04:34:03,438 storing an an unsigned integer so that's 6187 04:34:01,039 --> 04:34:06,638 what the compiler is going to deduce if 6188 04:34:03,438 --> 04:34:09,358 we pass ul it is going to be unsigned 6189 04:34:06,638 --> 04:34:11,359 long if we pass in ll this is going to 6190 04:34:09,359 --> 04:34:13,600 be long long and that's what the 6191 04:34:11,359 --> 04:34:15,840 compiler is going to deduce here you see 6192 04:34:13,599 --> 04:34:19,198 that we are trying to print the sizes of 6193 04:34:15,840 --> 04:34:21,039 these variables and we trying to 6194 04:34:21,039 --> 04:34:26,080 that the compiler deduced the right type 6195 04:34:24,080 --> 04:34:28,639 and we are going to go to visual studio 6196 04:34:26,080 --> 04:34:32,480 code and actually try this out 6197 04:34:28,638 --> 04:34:34,719 okay here i am in my working folder i am 6198 04:34:32,479 --> 04:34:36,239 going to open my template project i'm 6199 04:34:37,840 --> 04:34:44,159 save my files in the auto project here 6200 04:34:41,919 --> 04:34:47,118 so i am going to go in and put in my 6201 04:34:44,159 --> 04:34:49,680 files i'm going to go up again and i am 6202 04:34:47,118 --> 04:34:51,278 going to close whatever it is i have in 6203 04:34:49,680 --> 04:34:53,599 visual studio code because we're going 6204 04:34:51,278 --> 04:34:55,680 to create a new project i am going to 6205 04:34:53,599 --> 04:34:57,599 open my new folder we're going to close 6206 04:34:55,680 --> 04:34:59,760 the welcome screen here and we're going 6207 04:34:57,599 --> 04:35:01,599 to remove whatever it is we don't need 6208 04:35:01,599 --> 04:35:07,198 and we are going to put in the code we 6209 04:35:03,919 --> 04:35:10,079 basically had in the slides i am pasting 6210 04:35:07,199 --> 04:35:11,760 this code in to save on some time 6211 04:35:10,080 --> 04:35:13,520 because i don't want you to see me type 6212 04:35:11,759 --> 04:35:14,798 these things we're going to save some 6213 04:35:14,798 --> 04:35:20,560 so the first statement here declares a 6214 04:35:17,520 --> 04:35:22,638 variable var1 the type we don't really 6215 04:35:20,561 --> 04:35:24,319 know but we're going to let the compiler 6216 04:35:24,319 --> 04:35:28,479 and we can try and actually hover over 6217 04:35:26,798 --> 04:35:30,958 this and we're going to see that the 6218 04:35:28,479 --> 04:35:33,919 compiler already deduced that this is an 6219 04:35:30,958 --> 04:35:36,399 integer if we go to vira 2 this is a 6220 04:35:33,919 --> 04:35:40,079 double if we go to var3 you see it's a 6221 04:35:36,400 --> 04:35:42,319 float if you go to var4 it's long double 6222 04:35:40,080 --> 04:35:45,119 if you go to wire 5 it's a character you 6223 04:35:42,319 --> 04:35:47,759 can see that visual studio code can even 6224 04:35:45,118 --> 04:35:50,319 see what the compiler is going to deduce 6225 04:35:47,759 --> 04:35:52,958 before we even compile the code this is 6226 04:35:50,319 --> 04:35:55,118 the beauty of visual studio code we're 6227 04:35:52,958 --> 04:35:57,599 going to look at virus 6 you're going to 6228 04:35:55,118 --> 04:35:59,680 see that it's going to be unsigned and 6229 04:35:57,599 --> 04:36:03,278 if we go to var 7 it's going to be 6230 04:35:59,680 --> 04:36:05,680 unsigned long if we go to var 8 it's 6231 04:36:03,278 --> 04:36:07,919 going to be long long this is really 6232 04:36:05,680 --> 04:36:10,719 cool and you can see that our types are 6233 04:36:07,919 --> 04:36:13,679 correctly deduced visuals video code was 6234 04:36:10,719 --> 04:36:16,561 really helpful in showing us these types 6235 04:36:13,680 --> 04:36:19,520 but we want to be sure by running this 6236 04:36:16,561 --> 04:36:22,879 piece of code and seeing whatever size 6237 04:36:19,520 --> 04:36:25,199 the deduced type is for example for var1 6238 04:36:22,879 --> 04:36:28,080 we guessed that it's going to deduce an 6239 04:36:28,080 --> 04:36:33,439 in size we're going to try and run this 6240 04:36:30,879 --> 04:36:35,359 we're going to open our terminal here 6241 04:36:33,438 --> 04:36:38,638 and we're going to go to terminal and 6242 04:36:35,359 --> 04:36:40,719 run task we're going to build with gcc 6243 04:36:38,638 --> 04:36:43,759 and by the way if you want you can build 6244 04:36:40,719 --> 04:36:46,000 with the compiler from microsoft by 6245 04:36:43,759 --> 04:36:49,519 coming to terminal here and choose it 6246 04:36:46,000 --> 04:36:51,840 with world with msvc but gcc is my 6247 04:36:49,520 --> 04:36:55,039 favorite compiler and i am going to use 6248 04:36:51,840 --> 04:36:58,359 it if i can so we're going to hit enter 6249 04:36:55,039 --> 04:37:01,680 and enter we're going to see that our 6250 04:36:58,359 --> 04:37:04,479 rooster.exe file is in place if we run 6251 04:37:01,680 --> 04:37:07,680 it we're going to see our sizes so var 6252 04:37:04,479 --> 04:37:11,199 one occupies four bytes in memory 6253 04:37:07,680 --> 04:37:13,039 var2 occupies for eight bytes in memory 6254 04:37:13,039 --> 04:37:18,879 double fire 3 occupies 3 bytes because 6255 04:37:15,840 --> 04:37:20,958 it is a float var4 occupies 16 bytes 6256 04:37:18,879 --> 04:37:24,080 because it is a long double 6257 04:37:20,958 --> 04:37:26,079 and var 5 better occupy 1 byte because 6258 04:37:24,080 --> 04:37:27,520 it is a character and we see the same 6259 04:37:27,520 --> 04:37:33,840 vera 6 virus 7 and var 8 and this proves 6260 04:37:30,958 --> 04:37:36,878 that the auto keyword is really doing 6261 04:37:33,840 --> 04:37:40,080 its job it is deducing the types for our 6262 04:37:36,879 --> 04:37:42,959 variables without us explicitly 6263 04:37:40,080 --> 04:37:45,279 specifying the variable type we didn't 6264 04:37:42,958 --> 04:37:47,680 have to do something like and or double 6265 04:37:45,278 --> 04:37:50,479 or whatever we can let the compiler do 6266 04:37:47,680 --> 04:37:53,039 that for us and this will come in handy 6267 04:37:50,479 --> 04:37:54,719 many times this is really all we set out 6268 04:37:53,039 --> 04:37:56,719 to do in this lecture and i hope you 6269 04:37:54,719 --> 04:37:58,719 found it interesting in the next one 6270 04:37:56,719 --> 04:38:01,359 we're going to look at how we can do 6271 04:37:58,719 --> 04:38:04,319 assignments and assign data to our 6272 04:38:01,359 --> 04:38:07,199 variables in a unified way go ahead and 6273 04:38:04,319 --> 04:38:08,879 finish up here and we'd be there in this 6274 04:38:07,199 --> 04:38:12,240 lecture we're going to learn about 6275 04:38:08,879 --> 04:38:15,600 assignments and the basic idea i really 6276 04:38:12,240 --> 04:38:18,320 want to drive home here is that after a 6277 04:38:15,599 --> 04:38:21,438 variable is initialized like we've been 6278 04:38:18,319 --> 04:38:24,561 doing all along you can later assign a 6279 04:38:21,438 --> 04:38:27,038 new value to it for example here we have 6280 04:38:24,561 --> 04:38:28,719 our one declared it is an integer we can 6281 04:38:27,039 --> 04:38:31,600 print it out and it is going to print 6282 04:38:28,719 --> 04:38:33,520 one two three but later in the program 6283 04:38:31,599 --> 04:38:36,079 we can put in another value for example 6284 04:38:33,520 --> 04:38:39,760 here we are putting in a 55 and if we 6285 04:38:36,080 --> 04:38:42,400 print var1 it's going to be 55 this time 6286 04:38:39,759 --> 04:38:44,798 the same idea can be extended to doubles 6287 04:38:42,400 --> 04:38:46,719 you see here we are doing the same thing 6288 04:38:44,798 --> 04:38:49,759 booleans we are doing the same thing 6289 04:38:46,719 --> 04:38:51,760 here the state is declared force and we 6290 04:38:49,759 --> 04:38:54,239 are printing that and after we are 6291 04:38:51,759 --> 04:38:56,878 changing this to a true value and if we 6292 04:38:54,240 --> 04:38:58,320 print that it's going to be true one 6293 04:38:56,879 --> 04:39:01,119 thing you really need to be careful 6294 04:38:58,319 --> 04:39:04,000 about is if you are doing auto type 6295 04:39:01,118 --> 04:39:06,319 deduction the type is not very apparent 6296 04:39:04,000 --> 04:39:08,479 by looking at the declaration of the 6297 04:39:06,319 --> 04:39:12,400 variable here for example here by 6298 04:39:08,479 --> 04:39:15,199 looking at o2 var 3 we don't really know 6299 04:39:12,400 --> 04:39:17,439 what that is and it is very easy to try 6300 04:39:15,199 --> 04:39:20,719 and put in a value that is illegal for 6301 04:39:17,438 --> 04:39:23,519 example here we have deduced an unsigned 6302 04:39:20,719 --> 04:39:26,000 integer which is only going to store 6303 04:39:23,520 --> 04:39:28,240 positive numbers but we are putting in a 6304 04:39:26,000 --> 04:39:30,080 minus 22 and this is going to be a 6305 04:39:28,240 --> 04:39:33,439 disaster we're going to get a compiler 6306 04:39:30,080 --> 04:39:35,520 error and this is going to be bad so be 6307 04:39:33,438 --> 04:39:38,239 careful when you are doing a type 6308 04:39:35,520 --> 04:39:40,637 deduction in your assignments that you 6309 04:39:38,240 --> 04:39:43,040 do to your variables this is really all 6310 04:39:40,637 --> 04:39:45,520 we have to share in this lecture let's 6311 04:39:43,040 --> 04:39:48,718 hop over to visual studio and show you 6312 04:39:45,520 --> 04:39:50,637 this in action okay here i am in my 6313 04:39:48,718 --> 04:39:52,718 working folder the project we're going 6314 04:39:50,637 --> 04:39:55,119 to be working on is four nine here 6315 04:39:52,718 --> 04:39:57,920 assignments i am going to grab my 6316 04:39:55,119 --> 04:40:00,399 template files and put that in this 6317 04:39:57,919 --> 04:40:02,877 folder here and i am going to open this 6318 04:40:00,400 --> 04:40:05,840 up in visual studio code so let's close 6319 04:40:05,840 --> 04:40:10,878 and open the folder and we're going to 6320 04:40:08,797 --> 04:40:13,279 close the welcome screen here we don't 6321 04:40:10,878 --> 04:40:16,000 want it and remove whatever we don't 6322 04:40:13,279 --> 04:40:18,637 want from this file here the first thing 6323 04:40:16,000 --> 04:40:21,840 we want to do is put in the code for our 6324 04:40:18,637 --> 04:40:22,957 integer variable var1 and show you that 6325 04:40:22,957 --> 04:40:27,919 assign a new value to it after it was 6326 04:40:25,599 --> 04:40:31,680 initialized and if we run this code 6327 04:40:27,919 --> 04:40:34,559 we're going to see var1 to b123 and the 6328 04:40:31,680 --> 04:40:35,920 second statement is going to say var1 6329 04:40:35,919 --> 04:40:40,957 55. that's what we're going to see let's 6330 04:40:38,400 --> 04:40:44,798 bring up our terminal and we are going 6331 04:40:40,957 --> 04:40:48,399 to run the task to build with gcc 6332 04:40:44,797 --> 04:40:49,919 and if we hop over here in the terminal 6333 04:40:49,919 --> 04:40:55,839 we're going to see that our rooster.exe 6334 04:40:52,637 --> 04:40:59,039 file is in here if we run it we're going 6335 04:40:55,840 --> 04:41:01,680 to see var one one two three var 155 6336 04:40:59,040 --> 04:41:05,280 exactly what we expect we have stored in 6337 04:41:01,680 --> 04:41:08,319 a one two three at initialization using 6338 04:41:05,279 --> 04:41:11,759 braced initialization here but after 6339 04:41:08,319 --> 04:41:14,159 later on in our program we have decided 6340 04:41:11,759 --> 04:41:15,519 to put a different value in this 6341 04:41:15,520 --> 04:41:20,080 essentially reusing it to store 6342 04:41:17,759 --> 04:41:22,239 something else this is the basic idea 6343 04:41:20,080 --> 04:41:24,480 here we can do the same things with 6344 04:41:22,240 --> 04:41:27,600 doubles but before we do that let's put 6345 04:41:24,479 --> 04:41:30,957 in a separation line so that we can see 6346 04:41:27,599 --> 04:41:32,717 these vents clearly in the terminal and 6347 04:41:30,957 --> 04:41:34,159 we're going to go down and put in our 6348 04:41:34,159 --> 04:41:40,560 so we have a double variable called var2 6349 04:41:40,560 --> 04:41:46,479 in and then we're going to assign a new 6350 04:41:43,040 --> 04:41:48,319 value to it and that's going to be 99.99 6351 04:41:46,479 --> 04:41:49,279 if we print that out we're going to see 6352 04:41:49,279 --> 04:41:54,000 let's work and see this in action we're 6353 04:41:56,000 --> 04:42:01,439 run the program now it's going to save 6354 04:41:58,560 --> 04:42:03,600 r1 is one two three four one fifty five 6355 04:42:01,439 --> 04:42:06,877 but we're also going to see that viral 6356 04:42:09,240 --> 04:42:15,120 99.99 it's going to show up here okay 6357 04:42:12,000 --> 04:42:16,878 let's put in another separation line 6358 04:42:15,119 --> 04:42:18,319 because we're going to need that in a 6359 04:42:18,319 --> 04:42:24,000 we can do the same things with booleans 6360 04:42:21,040 --> 04:42:26,560 here we are using this as just an 6361 04:42:26,560 --> 04:42:31,840 essentially extends to any other kind of 6362 04:42:29,520 --> 04:42:34,560 variable you're going to have in your c 6363 04:42:31,840 --> 04:42:37,200 plus plus code if you initialize it with 6364 04:42:34,560 --> 04:42:40,638 some value you have the option to change 6365 04:42:37,200 --> 04:42:42,958 that value later on in your program by 6366 04:42:40,637 --> 04:42:45,279 assigning that value to that variable 6367 04:42:42,957 --> 04:42:47,599 essentially what we are doing here the 6368 04:42:45,279 --> 04:42:50,239 variable state which is a bull 6369 04:42:47,599 --> 04:42:52,479 was initialized to false we're going to 6370 04:42:50,240 --> 04:42:54,878 print that out and notice that we have 6371 04:42:52,479 --> 04:42:57,200 this setting to allow us to see true or 6372 04:42:54,878 --> 04:43:00,240 false instead of ones and zeros we have 6373 04:42:57,200 --> 04:43:02,958 seen this in a few lectures back 6374 04:43:00,240 --> 04:43:05,520 and later on we are assigning true to 6375 04:43:02,957 --> 04:43:07,520 this state and we are printing that so 6376 04:43:05,520 --> 04:43:11,840 if we print these things out we're going 6377 04:43:07,520 --> 04:43:13,680 to see false and true for the state here 6378 04:43:16,000 --> 04:43:19,520 and the world is good if we run the 6379 04:43:19,520 --> 04:43:24,479 we're going to see false and true and it 6380 04:43:22,080 --> 04:43:26,400 is exactly what we expect again you 6381 04:43:24,479 --> 04:43:29,040 really need to be careful when you are 6382 04:43:26,400 --> 04:43:31,280 using auto type deduction because you 6383 04:43:29,040 --> 04:43:33,760 might have some serious problems if you 6384 04:43:31,279 --> 04:43:36,239 are not paying attention let's put in 6385 04:43:33,759 --> 04:43:39,119 the code so that we can play with it 6386 04:43:36,240 --> 04:43:41,920 here we have a variable called var 3 and 6387 04:43:39,119 --> 04:43:44,319 the type of it is not specified 6388 04:43:41,919 --> 04:43:47,519 explicitly we are letting the compiler 6389 04:43:44,319 --> 04:43:50,479 deduce this and because our initializer 6390 04:43:47,520 --> 04:43:53,040 here is an unsigned integer the compiler 6391 04:43:50,479 --> 04:43:55,759 is going to deduce that it is an 6392 04:43:53,040 --> 04:43:58,638 unsigned integer the problem will come 6393 04:43:55,759 --> 04:44:00,637 if you try to store in a negative number 6394 04:43:58,637 --> 04:44:03,200 the compiler is not going to give you a 6395 04:44:00,637 --> 04:44:05,439 compiler arrow it is going to silently 6396 04:44:05,439 --> 04:44:11,119 that we don't really want in there we 6397 04:44:07,599 --> 04:44:13,519 want a minus 22 and you can see that 6398 04:44:11,119 --> 04:44:16,878 this is what the compiler is turning our 6399 04:44:13,520 --> 04:44:19,520 value into we want a minus 22 in this 6400 04:44:16,878 --> 04:44:22,159 for three but if you hover over this 6401 04:44:19,520 --> 04:44:23,600 minus sign here in visual studio code 6402 04:44:22,159 --> 04:44:26,000 you're going to see that the value that 6403 04:44:23,599 --> 04:44:29,119 we have in is not what we put in we put 6404 04:44:26,000 --> 04:44:32,000 in a minus 22 and we are getting a 6405 04:44:29,119 --> 04:44:34,239 really large number close to 4 billion 6406 04:44:32,000 --> 04:44:36,637 so this is really something that can go 6407 04:44:34,240 --> 04:44:37,840 silently wrong in your code and you're 6408 04:44:37,840 --> 04:44:41,680 a hard time trying to figure out why 6409 04:44:39,919 --> 04:44:44,319 your code is not doing whatever it is 6410 04:44:41,680 --> 04:44:46,319 you want so be careful about this let's 6411 04:44:44,319 --> 04:44:48,000 try and weld this so we're going to 6412 04:44:48,000 --> 04:44:50,957 the world is going to be good you're 6413 04:44:49,360 --> 04:44:54,159 going to see that we're not even going 6414 04:44:50,957 --> 04:44:56,877 to get a compiler arrow if we run this 6415 04:44:54,159 --> 04:44:59,520 and we're going to see that viral 3 is 6416 04:44:56,878 --> 04:45:02,400 exactly what visual studio could guess 6417 04:44:59,520 --> 04:45:04,718 and it is not good it is this value here 6418 04:45:02,400 --> 04:45:07,520 and the problem is that the compiler is 6419 04:45:04,718 --> 04:45:10,637 silently putting in a garbage value and 6420 04:45:07,520 --> 04:45:12,637 not what we want here because virus 3 is 6421 04:45:10,637 --> 04:45:14,637 basically not capable of storing 6422 04:45:12,637 --> 04:45:16,637 negative numbers so the compiler is 6423 04:45:14,637 --> 04:45:18,957 going to do its best and put in a 6424 04:45:16,637 --> 04:45:21,680 garbage value and this is not what you 6425 04:45:18,957 --> 04:45:23,199 want and you need to be aware of this 6426 04:45:21,680 --> 04:45:25,119 this is really all we set out to do in 6427 04:45:23,200 --> 04:45:28,000 this lecture i hope you know about 6428 04:45:25,119 --> 04:45:30,399 assignments and be careful when you are 6429 04:45:28,000 --> 04:45:33,599 doing assignments on variables whose 6430 04:45:30,400 --> 04:45:35,200 types are deduced with auto and we're 6431 04:45:33,599 --> 04:45:36,637 going to stop here in this lecture in 6432 04:45:35,200 --> 04:45:39,520 the next one we're going to try and 6433 04:45:36,637 --> 04:45:41,439 recap what we saw in this chapter go 6434 04:45:41,439 --> 04:45:46,399 and meet me there in this lecture we're 6435 04:45:43,599 --> 04:45:48,877 going to recap what we saw in this 6436 04:45:46,400 --> 04:45:52,000 chapter and this chapter was really all 6437 04:45:48,878 --> 04:45:54,000 about variables and data types we had a 6438 04:45:52,000 --> 04:45:56,560 chance to look at different types that 6439 04:45:54,000 --> 04:45:58,797 we can use in c plus plus we saw that we 6440 04:45:56,560 --> 04:46:01,840 have fundamental types we can use to 6441 04:45:58,797 --> 04:46:05,199 represent integers floating points 6442 04:46:01,840 --> 04:46:08,159 booleans and we also saw that we can do 6443 04:46:05,200 --> 04:46:10,400 type deduction with auto we had a chance 6444 04:46:08,159 --> 04:46:13,439 to make it clear that data is 6445 04:46:10,400 --> 04:46:15,520 represented in memory as zeros and ones 6446 04:46:13,439 --> 04:46:18,159 and that there are some kinds of 6447 04:46:15,520 --> 04:46:21,279 transformations we do on these pieces of 6448 04:46:18,159 --> 04:46:24,637 data to turn them into a form that is 6449 04:46:21,279 --> 04:46:27,680 easily usable and manipulable by humans 6450 04:46:24,637 --> 04:46:30,319 and we saw that for integral types or 6451 04:46:27,680 --> 04:46:32,797 integers we could do that through number 6452 04:46:30,319 --> 04:46:35,759 systems and we had a chance to look at a 6453 04:46:32,797 --> 04:46:38,239 few number systems like binary octal and 6454 04:46:35,759 --> 04:46:41,359 hexadecimal and we had a chance to play 6455 04:46:38,240 --> 04:46:44,320 with all these variable types in visual 6456 04:46:41,360 --> 04:46:46,718 studio code and see the nuances of how 6457 04:46:44,319 --> 04:46:49,520 you really work with them i would like 6458 04:46:46,718 --> 04:46:51,440 to welcome you in this new chapter and 6459 04:46:49,520 --> 04:46:55,200 this one we're going to be looking at 6460 04:46:51,439 --> 04:46:57,119 operations we can do on our data and 6461 04:46:55,200 --> 04:46:59,280 there is a bunch of them we can add 6462 04:46:57,119 --> 04:47:01,360 things up we can subtract numbers we can 6463 04:46:59,279 --> 04:47:04,399 divide numbers we're going to see all 6464 04:47:01,360 --> 04:47:06,958 kinds of crazy operations we can do 6465 04:47:04,400 --> 04:47:10,319 on the data all the variables that we 6466 04:47:06,957 --> 04:47:11,919 have in our c plus plus program we are 6467 04:47:10,319 --> 04:47:14,239 also going to have a chance to look at 6468 04:47:11,919 --> 04:47:17,439 how we can format stuff when we print 6469 04:47:14,240 --> 04:47:20,000 them out to stdc out and we are going to 6470 04:47:17,439 --> 04:47:22,159 be using some utilities from the c plus 6471 04:47:20,000 --> 04:47:25,040 plus standard library to do some 6472 04:47:22,159 --> 04:47:27,200 operations on our variables this is 6473 04:47:25,040 --> 04:47:29,440 going to be a fun chapter i can't wait 6474 04:47:27,200 --> 04:47:31,920 to get started let's go in the next 6475 04:47:31,919 --> 04:47:35,839 in this lecture we're going to be 6476 04:47:33,279 --> 04:47:36,717 looking at some basic operations you can 6477 04:47:36,718 --> 04:47:41,520 on the data in your c plus plus program 6478 04:47:39,680 --> 04:47:44,240 and we are going to be specifically 6479 04:47:41,520 --> 04:47:46,957 looking at addition subtraction dividing 6480 04:47:44,240 --> 04:47:49,600 numbers the modulus operator and 6481 04:47:46,957 --> 04:47:50,559 multiplying stuff we have seen addition 6482 04:47:50,560 --> 04:47:55,840 here we have a simple example we have a 6483 04:47:53,279 --> 04:47:58,637 variable called sum and we are 6484 04:47:55,840 --> 04:48:00,878 initializing it with two numbers that we 6485 04:47:58,637 --> 04:48:02,797 are adding up number one and number two 6486 04:48:00,878 --> 04:48:05,760 this is something you can do in your c 6487 04:48:02,797 --> 04:48:07,599 plus bus program you can also do 6488 04:48:05,759 --> 04:48:10,239 some other things for example we have 6489 04:48:07,599 --> 04:48:12,877 another one called other sum and we are 6490 04:48:10,240 --> 04:48:15,600 adding up some other numbers and we are 6491 04:48:12,878 --> 04:48:16,958 going to collect the result into this 6492 04:48:16,957 --> 04:48:21,919 variable and we're going to print these 6493 04:48:19,200 --> 04:48:24,319 things out so that you can see them on 6494 04:48:21,919 --> 04:48:25,919 your command prompt this is how you can 6495 04:48:24,319 --> 04:48:27,599 do addition and we're going to have a 6496 04:48:25,919 --> 04:48:30,559 chance to play with this in visual 6497 04:48:27,599 --> 04:48:33,439 studio code in a minute we can also do 6498 04:48:30,560 --> 04:48:35,600 subtraction we can take a number and do 6499 04:48:33,439 --> 04:48:37,840 minus another number like we are doing 6500 04:48:35,599 --> 04:48:40,479 here we're going to store the results in 6501 04:48:37,840 --> 04:48:41,599 this div variable and we can do some 6502 04:48:41,599 --> 04:48:46,717 and down here you see another example in 6503 04:48:44,637 --> 04:48:49,599 other diff here we're going to subtract 6504 04:48:46,718 --> 04:48:52,080 numbers and store the result in other 6505 04:48:49,599 --> 04:48:54,399 def and we are also going to print this 6506 04:48:52,080 --> 04:48:56,240 out you can also multiply stuff this is 6507 04:48:54,400 --> 04:48:59,760 how you can do this you use this 6508 04:48:56,240 --> 04:49:01,840 operator here it is the star operator 6509 04:48:59,759 --> 04:49:03,759 and this is an operator that many 6510 04:49:01,840 --> 04:49:06,878 programming languages use to do 6511 04:49:03,759 --> 04:49:09,279 multiplication you should get familiar 6512 04:49:06,878 --> 04:49:11,440 with this and again we print out the 6513 04:49:09,279 --> 04:49:15,119 result so that you can see that this is 6514 04:49:11,439 --> 04:49:17,919 actually working we can also do division 6515 04:49:15,119 --> 04:49:19,840 division for integers is a little tricky 6516 04:49:17,919 --> 04:49:20,877 if you are seeing this for the first 6517 04:49:26,159 --> 04:49:30,240 we're not going to get fractional 6518 04:49:27,759 --> 04:49:33,840 numbers c plus plus is going to try and 6519 04:49:30,240 --> 04:49:37,440 figure out how many times it can fit 10 6520 04:49:33,840 --> 04:49:41,439 in 31 and we are visualizing this here 6521 04:49:37,439 --> 04:49:44,239 so in 31 we can only fit 10 three times 6522 04:49:41,439 --> 04:49:46,000 so the result of 31 is going to be three 6523 04:49:46,000 --> 04:49:50,080 okay with integer division you're not 6524 04:49:47,919 --> 04:49:51,679 going to be getting fractional numbers 6525 04:49:50,080 --> 04:49:54,798 this is something you should really 6526 04:49:51,680 --> 04:49:57,680 understand we have another operator that 6527 04:49:54,797 --> 04:49:59,680 is called modulus operator and this 6528 04:49:57,680 --> 04:50:02,319 operator is represented by this 6529 04:49:59,680 --> 04:50:04,479 percentage symbol that we are doing here 6530 04:50:02,319 --> 04:50:06,878 and the result of this operation here is 6531 04:50:06,878 --> 04:50:12,560 after you have done the division of 31 6532 04:50:10,000 --> 04:50:14,878 and 10. and here the result is going to 6533 04:50:12,560 --> 04:50:16,958 be one and you might be wondering why 6534 04:50:14,878 --> 04:50:18,479 this is useful it is going to be useful 6535 04:50:18,479 --> 04:50:22,000 and i think we're going to have a chance 6536 04:50:19,840 --> 04:50:24,080 to play with this later in the course 6537 04:50:22,000 --> 04:50:27,040 but right now you just need to be aware 6538 04:50:24,080 --> 04:50:28,560 of this operation and what it does okay 6539 04:50:27,040 --> 04:50:30,878 these are the operations we're going to 6540 04:50:28,560 --> 04:50:33,680 be learning about and playing with in 6541 04:50:30,878 --> 04:50:36,560 this lecture let's head to visual studio 6542 04:50:33,680 --> 04:50:39,599 code and actually play with this okay 6543 04:50:36,560 --> 04:50:41,280 here we are in our working folder for 6544 04:50:39,599 --> 04:50:43,119 this chapter you see that i have 6545 04:50:41,279 --> 04:50:46,399 prepared all the lectures we're going to 6546 04:50:43,119 --> 04:50:49,360 do in this chapter and i have my c plus 6547 04:50:46,400 --> 04:50:52,319 20 template project in place i am going 6548 04:50:49,360 --> 04:50:54,637 to do what we have been doing all along 6549 04:50:52,319 --> 04:50:55,759 so i am going to take these files and 6550 04:50:55,759 --> 04:50:59,279 in the current lecture which is basic 6551 04:50:59,279 --> 04:51:04,159 and i am going to paste those in and i 6552 04:51:02,240 --> 04:51:06,400 am going to open this folder in visual 6553 04:51:04,159 --> 04:51:08,637 studio code so let's do that pretty 6554 04:51:06,400 --> 04:51:10,560 quick we are going to close the welcome 6555 04:51:08,637 --> 04:51:14,239 screen here and we are going to open our 6556 04:51:10,560 --> 04:51:16,240 main cpp file and we are going to remove 6557 04:51:14,240 --> 04:51:18,240 things we don't need here the first 6558 04:51:16,240 --> 04:51:20,480 thing we want to play with is addition 6559 04:51:18,240 --> 04:51:23,280 so we're going to say that and we're 6560 04:51:20,479 --> 04:51:25,119 going to declare two end variables we're 6561 04:51:23,279 --> 04:51:27,680 going to call this number one and we're 6562 04:51:25,119 --> 04:51:30,718 going to put in a two for example 6563 04:51:27,680 --> 04:51:32,797 and we're going to do a number two 6564 04:51:30,718 --> 04:51:35,600 and we're going to put in a seven why 6565 04:51:32,797 --> 04:51:39,439 not and what we can do we can say 6566 04:51:35,599 --> 04:51:41,199 result equals number one plus number two 6567 04:51:39,439 --> 04:51:44,000 and we're going to store the result of 6568 04:51:41,200 --> 04:51:47,520 this operation and the result variable 6569 04:51:44,000 --> 04:51:50,000 here so what we can do is say sddcl and 6570 04:51:47,520 --> 04:51:53,040 we're going to print the result we can 6571 04:51:50,000 --> 04:51:55,439 try and compile this and see the result 6572 04:51:55,439 --> 04:51:59,840 and we're going to build with gcc as we 6573 04:51:59,840 --> 04:52:04,319 the world is going to go through and if 6574 04:52:02,000 --> 04:52:08,159 we run rooster here we're going to see 6575 04:52:04,319 --> 04:52:10,479 that the result of our operation is 9. 6576 04:52:08,159 --> 04:52:13,360 so this is how you can do addition 6577 04:52:10,479 --> 04:52:15,200 you can also do subtraction and here we 6578 04:52:13,360 --> 04:52:17,119 know to declare a new variable we're 6579 04:52:17,919 --> 04:52:23,279 our result variable here so what we're 6580 04:52:20,319 --> 04:52:25,279 going to do here is say subtraction and 6581 04:52:25,279 --> 04:52:31,599 equals for example let's take number two 6582 04:52:28,878 --> 04:52:34,319 minus number one this is the operation 6583 04:52:31,599 --> 04:52:36,559 we want to do this is number two what we 6584 04:52:34,319 --> 04:52:39,439 are going to be storing in this result 6585 04:52:36,560 --> 04:52:42,878 here is going to be a 5 because we are 6586 04:52:39,439 --> 04:52:46,479 taking 7 and subtracting 2. i hope this 6587 04:52:42,878 --> 04:52:48,958 makes sense so if we grab this line here 6588 04:52:46,479 --> 04:52:51,040 and paste it in here so that we save on 6589 04:52:57,599 --> 04:53:02,877 and we're going to run rooster again it 6590 04:53:00,000 --> 04:53:03,599 is going to say result is 5 because we 6591 04:53:03,599 --> 04:53:09,199 taking 7 minus two here the result is 6592 04:53:06,319 --> 04:53:11,840 going to be five we can also do a 6593 04:53:09,200 --> 04:53:14,319 subtraction operation that is going to 6594 04:53:11,840 --> 04:53:18,080 result in a negative number so for 6595 04:53:14,319 --> 04:53:20,878 example we can say result equals 6596 04:53:20,878 --> 04:53:24,637 and this is going to give us a negative 6597 04:53:22,637 --> 04:53:27,200 number because two minus seven is going 6598 04:53:27,200 --> 04:53:31,600 we're going to print that here i'm going 6599 04:53:29,680 --> 04:53:33,520 to paste this end because i still have 6600 04:53:33,520 --> 04:53:40,797 if i bolt with gcc and run my program 6601 04:53:37,279 --> 04:53:43,039 we're going to get -5 as a result of 6602 04:53:40,797 --> 04:53:45,039 this operation here this is how you can 6603 04:53:43,040 --> 04:53:47,520 do subtraction we can also do 6604 04:53:45,040 --> 04:53:50,479 multiplication so let's go down here and 6605 04:53:47,520 --> 04:53:53,760 say that and we're going to say result 6606 04:53:50,479 --> 04:53:56,319 equals number one multiplied by number 6607 04:53:53,759 --> 04:53:58,319 two and this is the operator we used to 6608 04:53:56,319 --> 04:54:00,560 do multiplication in programming 6609 04:53:58,319 --> 04:54:02,797 languages we're going to put in our 6610 04:54:02,797 --> 04:54:08,717 and we're going to world with gcc 6611 04:54:05,919 --> 04:54:12,000 and if we run rooster now we're going to 6612 04:54:08,718 --> 04:54:14,798 get 14 because we are taking 6613 04:54:12,000 --> 04:54:17,200 2 and multiplying that with 7. this is 6614 04:54:14,797 --> 04:54:20,000 how you can do multiplication 6615 04:54:20,000 --> 04:54:24,718 seven and divide that with two try to 6616 04:54:22,718 --> 04:54:26,637 give this a guess based on what we saw 6617 04:54:24,718 --> 04:54:28,878 in the slides we're going to go down 6618 04:54:26,637 --> 04:54:32,399 here and do division and we're going to 6619 04:54:28,878 --> 04:54:34,479 say result equals number two divided by 6620 04:54:34,479 --> 04:54:38,718 we're going to try and print this out if 6621 04:54:36,637 --> 04:54:41,039 we build this the world is going to go 6622 04:54:38,718 --> 04:54:43,920 through and if we run this we're going 6623 04:54:43,919 --> 04:54:47,519 so to really make sense of this let's 6624 04:54:45,919 --> 04:54:49,679 look at these numbers we are taking 6625 04:54:47,520 --> 04:54:51,760 number two and dividing that with number 6626 04:54:53,439 --> 04:54:58,877 and because we can't store fractional 6627 04:54:55,840 --> 04:55:01,040 numbers in an integer the fractional 6628 04:54:58,878 --> 04:55:03,600 part is going to be basically chopped 6629 04:55:01,040 --> 04:55:04,718 off and we're going to store a three 6630 04:55:06,637 --> 04:55:10,319 only three times i really hope this 6631 04:55:08,479 --> 04:55:12,399 makes sense the last operation we're 6632 04:55:12,400 --> 04:55:18,159 and it is a way to capture the reminder 6633 04:55:15,200 --> 04:55:20,718 of a division operation so we're going 6634 04:55:20,718 --> 04:55:25,600 and we're going to say number two and 6635 04:55:23,040 --> 04:55:27,920 we're going to say modulus using the 6636 04:55:25,599 --> 04:55:29,519 percentage operator here and we're going 6637 04:55:29,520 --> 04:55:33,680 and we're going to print the result of 6638 04:55:33,680 --> 04:55:37,200 take a look at this here and try to 6639 04:55:35,520 --> 04:55:39,520 guess what we're going to be doing what 6640 04:55:43,200 --> 04:55:46,878 and we're going to take seven divide 6641 04:55:46,878 --> 04:55:52,000 and the reminder of that is going to be 6642 04:55:49,360 --> 04:55:54,560 one and that's what we're going to store 6643 04:55:52,000 --> 04:55:57,520 in this results variable here and if we 6644 04:55:54,560 --> 04:56:00,479 print that we expect to get a one here 6645 04:55:57,520 --> 04:56:03,439 this is what modulus does let's build 6646 04:56:00,479 --> 04:56:06,079 and build with gcc the world is going to 6647 04:56:03,439 --> 04:56:08,717 go through if we run rooster here we're 6648 04:56:06,080 --> 04:56:10,878 going to get a one let's do what we used 6649 04:56:08,718 --> 04:56:13,520 in the slide so that this really makes 6650 04:56:10,878 --> 04:56:14,400 sense we're going to go down here and i 6651 04:56:14,400 --> 04:56:20,000 equals 31 modulus 10. this is what we 6652 04:56:20,000 --> 04:56:24,878 so we're going to print the result here 6653 04:56:22,319 --> 04:56:27,040 and here we are really asking 6654 04:56:24,878 --> 04:56:30,159 what is going to be the reminder if we 6655 04:56:27,040 --> 04:56:32,080 take 31 and divide that by 10 and that 6656 04:56:30,159 --> 04:56:34,319 reminder is going to be stored in this 6657 04:56:32,080 --> 04:56:37,600 research variable so here we're also 6658 04:56:34,319 --> 04:56:40,718 going to get a one so let's weld with 6659 04:56:37,599 --> 04:56:43,680 gcc and we're going to run the program 6660 04:56:40,718 --> 04:56:46,878 and we are also going to get a one down 6661 04:56:43,680 --> 04:56:49,520 here again the modulus operator can be 6662 04:56:46,878 --> 04:56:51,920 hard to understand for beginners but it 6663 04:56:49,520 --> 04:56:54,637 is really capturing the reminder of the 6664 04:56:51,919 --> 04:56:58,239 division again we can see that here we 6665 04:56:54,637 --> 04:57:01,039 are asking how many times can 10 fit in 6666 04:56:58,240 --> 04:57:04,159 31 and we visualize this here it can 6667 04:57:01,040 --> 04:57:06,080 only fit in three times and the reminder 6668 04:57:04,159 --> 04:57:08,400 is going to be this red thing you see 6669 04:57:06,080 --> 04:57:10,480 here it's going to be one and when we do 6670 04:57:08,400 --> 04:57:12,798 a modulus operation here we are 6671 04:57:10,479 --> 04:57:14,637 interested in that reminder and that's 6672 04:57:12,797 --> 04:57:16,637 what we're going to be storing in 6673 04:57:14,637 --> 04:57:20,239 whatever variable that is capturing the 6674 04:57:16,637 --> 04:57:22,878 result of this modules operation 6675 04:57:20,240 --> 04:57:24,480 and that's what we basically have here 6676 04:57:22,878 --> 04:57:26,718 and this is really all we set out to do 6677 04:57:24,479 --> 04:57:28,637 in this lecture now i hope you have a 6678 04:57:26,718 --> 04:57:31,520 good understanding of these operations 6679 04:57:28,637 --> 04:57:34,000 you can do on integers we have used 6680 04:57:31,520 --> 04:57:37,040 integers here but you can really do 6681 04:57:34,000 --> 04:57:39,360 these operations on numbers except for 6682 04:57:37,040 --> 04:57:42,319 the modulus operator which is really 6683 04:57:39,360 --> 04:57:43,040 going to make sense for integral types 6684 04:57:43,040 --> 04:57:48,240 you can't really do modulus on floating 6685 04:57:45,759 --> 04:57:50,159 points or fractional numbers that's not 6686 04:57:48,240 --> 04:57:52,080 going to make any sense at all we're 6687 04:57:50,159 --> 04:57:53,759 going to stop here in this lecture in 6688 04:57:52,080 --> 04:57:56,480 the next one we're going to learn about 6689 04:57:53,759 --> 04:57:59,119 precedence and associativity a very 6690 04:57:56,479 --> 04:58:01,439 important concept for your operations in 6691 04:58:01,439 --> 04:58:05,599 in this lecture we're going to learn 6692 04:58:02,878 --> 04:58:07,600 about precedence and associativity so 6693 04:58:05,599 --> 04:58:10,000 what do we mean by precedence and 6694 04:58:07,599 --> 04:58:12,479 associativity really what we mean by 6695 04:58:10,000 --> 04:58:13,520 this is a set of rules that we can 6696 04:58:15,680 --> 04:58:19,760 when we have multiple operators in the 6697 04:58:19,759 --> 04:58:24,797 like in the example we have here if they 6698 04:58:22,240 --> 04:58:26,080 tell you to evaluate the result of this 6699 04:58:26,080 --> 04:58:31,280 if we don't have clear rules to follow 6700 04:58:28,560 --> 04:58:33,600 we might have multiple answers and we 6701 04:58:31,279 --> 04:58:36,637 want to know which one is right or wrong 6702 04:58:33,599 --> 04:58:39,599 for example one might want to do a and b 6703 04:58:36,637 --> 04:58:43,360 first and then do c and then do d but 6704 04:58:39,599 --> 04:58:46,239 one might want to do e minus f 6705 04:58:43,360 --> 04:58:48,319 and do b multiply by c and then do 6706 04:58:46,240 --> 04:58:49,920 things in different ways and get 6707 04:58:49,919 --> 04:58:54,797 hopefully you are saying that this may 6708 04:58:52,080 --> 04:58:57,120 be a big problem what we get from 6709 04:58:54,797 --> 04:58:59,360 precedence and associativity is the 6710 04:58:57,119 --> 04:59:02,399 rules we can follow to know which 6711 04:58:59,360 --> 04:59:04,000 operator we have to do first for example 6712 04:59:04,000 --> 04:59:10,000 the multiplication and division operator 6713 04:59:07,439 --> 04:59:12,717 have to be done before you do addition 6714 04:59:10,000 --> 04:59:15,040 and subtraction and if you want to 6715 04:59:12,718 --> 04:59:18,000 evaluate this expression here you're 6716 04:59:15,040 --> 04:59:21,120 going to have to do b and c first and 6717 04:59:18,000 --> 04:59:23,919 then do d and e once you have those 6718 04:59:21,119 --> 04:59:26,559 results then you can do addition and 6719 04:59:23,919 --> 04:59:27,679 subtraction but the question remains 6720 04:59:27,680 --> 04:59:32,797 what do we do first if we have only 6721 04:59:30,797 --> 04:59:35,439 addition and subtraction in an 6722 04:59:32,797 --> 04:59:38,637 expiration and we solve that problem 6723 04:59:35,439 --> 04:59:41,119 with associativity associativity defines 6724 04:59:38,637 --> 04:59:42,637 if you do the operations from the left 6725 04:59:42,637 --> 04:59:47,039 and again these things might not make 6726 04:59:44,637 --> 04:59:49,599 sense yet we're going to do an example 6727 04:59:47,040 --> 04:59:50,958 and you're going to really see what is 6728 04:59:50,957 --> 04:59:56,399 to summarize precedence tells you which 6729 04:59:53,680 --> 04:59:58,560 operation you can do first and 6730 04:59:56,400 --> 05:00:00,400 associativity is going to tell you in 6731 04:59:58,560 --> 05:00:02,479 which direction you're going to go from 6732 05:00:00,400 --> 05:00:04,000 the left or from the right and some 6733 05:00:02,479 --> 05:00:06,479 operators are going to be left 6734 05:00:04,000 --> 05:00:08,797 associative and that means that you're 6735 05:00:06,479 --> 05:00:11,200 going to start evaluating them from the 6736 05:00:08,797 --> 05:00:13,360 left some operations are going to be 6737 05:00:11,200 --> 05:00:16,479 right associative and you're going to 6738 05:00:13,360 --> 05:00:18,637 start evaluating them from the right and 6739 05:00:16,479 --> 05:00:20,797 i am not making these things up there is 6740 05:00:18,637 --> 05:00:24,479 actually a c plus plus operator 6741 05:00:20,797 --> 05:00:26,399 precedence table that you can consult to 6742 05:00:26,400 --> 05:00:31,760 you should do first if you really are 6743 05:00:29,279 --> 05:00:33,680 evaluating this thing with your mind 6744 05:00:31,759 --> 05:00:35,919 okay for example if you look in this 6745 05:00:33,680 --> 05:00:37,840 table there are many operators we 6746 05:00:35,919 --> 05:00:40,399 haven't seen yet but that's not the 6747 05:00:37,840 --> 05:00:42,159 problem the most important thing here is 6748 05:00:42,159 --> 05:00:47,599 associativity are and when we get to 6749 05:00:45,040 --> 05:00:50,400 learn about these new operators you can 6750 05:00:47,599 --> 05:00:52,877 come here and know where they sit in 6751 05:00:50,400 --> 05:00:56,319 this precedence table for example we 6752 05:00:52,878 --> 05:00:57,920 know how to do addition multiplication 6753 05:00:57,919 --> 05:01:03,359 we see that the precedence for 6754 05:01:00,159 --> 05:01:06,240 multiplication division and modulus 6755 05:01:03,360 --> 05:01:07,520 is higher than for addition and 6756 05:01:07,520 --> 05:01:11,360 and we can use this in our operators to 6757 05:01:11,360 --> 05:01:16,797 which one is going to be evaluated by 6758 05:01:13,919 --> 05:01:18,797 our compiler first another thing i want 6759 05:01:16,797 --> 05:01:21,680 you to notice in this table is this 6760 05:01:18,797 --> 05:01:24,319 column here on associativity if you go 6761 05:01:21,680 --> 05:01:25,840 back to our familiar operators here 6762 05:01:25,840 --> 05:01:31,920 these operators are left to right so you 6763 05:01:28,957 --> 05:01:35,199 should start evaluating things from left 6764 05:01:31,919 --> 05:01:38,319 to right but some operators have right 6765 05:01:35,200 --> 05:01:40,319 to left associativity and when we get to 6766 05:01:38,319 --> 05:01:42,957 these operators you will start doing 6767 05:01:40,319 --> 05:01:45,040 things from right to the left okay i 6768 05:01:42,957 --> 05:01:47,119 think it is time we went to visual 6769 05:01:45,040 --> 05:01:49,920 studio code and actually played with 6770 05:01:47,119 --> 05:01:53,119 this thanks here we are in visual studio 6771 05:01:49,919 --> 05:01:55,199 code we are in our working folder we are 6772 05:01:53,119 --> 05:01:57,840 going to grab our template projects 6773 05:01:55,200 --> 05:02:00,000 we're going to copy this and put that in 6774 05:01:57,840 --> 05:02:02,479 our lecture on precedence and 6775 05:02:00,000 --> 05:02:05,279 associativity we're going to put that in 6776 05:02:02,479 --> 05:02:07,759 here we are going to open this project 6777 05:02:05,279 --> 05:02:09,919 in visual studio code we are going to 6778 05:02:07,759 --> 05:02:12,877 get rid of things we don't need and 6779 05:02:09,919 --> 05:02:15,919 remove this here in the body and we are 6780 05:02:12,878 --> 05:02:18,400 going to put in our test code so that we 6781 05:02:15,919 --> 05:02:21,599 don't have to type this and save on time 6782 05:02:18,400 --> 05:02:24,159 we have a few variables here a through g 6783 05:02:21,599 --> 05:02:25,519 and we have an expression here or a 6784 05:02:25,520 --> 05:02:31,760 that is combining multiplication 6785 05:02:28,319 --> 05:02:34,000 division addition and subtraction and we 6786 05:02:31,759 --> 05:02:35,679 have to figure out which operation we're 6787 05:02:35,680 --> 05:02:40,479 i would challenge you to do this in your 6788 05:02:38,319 --> 05:02:43,680 mind and try to guess what the result is 6789 05:02:40,479 --> 05:02:46,399 going to be before we run this in our 6790 05:02:43,680 --> 05:02:49,360 compiler we're going to compile this 6791 05:02:46,400 --> 05:02:50,718 with gcc and we are going to open our 6792 05:02:50,718 --> 05:02:55,840 and if we do durham we're going to see 6793 05:02:55,840 --> 05:03:01,119 if we run it we're going to see that the 6794 05:02:58,159 --> 05:03:03,759 result is 30. how did we come up with 6795 05:03:10,797 --> 05:03:16,559 before addition and subtraction we are 6796 05:03:14,000 --> 05:03:19,040 going to take b and c and multiply those 6797 05:03:16,560 --> 05:03:21,600 so b and c multiplied we're going to get 6798 05:03:19,040 --> 05:03:24,080 a 24 we're going to try and do this in 6799 05:03:21,599 --> 05:03:25,039 our brain d divided by a we're going to 6800 05:03:25,040 --> 05:03:31,040 so d divided by e we're going to get a 3 6801 05:03:28,319 --> 05:03:33,119 so we're going to have a 3 here and 6802 05:03:31,040 --> 05:03:36,400 after we have this we're going to do 6803 05:03:33,119 --> 05:03:39,680 addition and subtraction so we're going 6804 05:03:36,400 --> 05:03:42,480 to take a and add that to the result of 6805 05:03:39,680 --> 05:03:46,637 b and c so we're going to say 6806 05:03:42,479 --> 05:03:48,718 6 plus 24 which is the result of b and c 6807 05:03:46,637 --> 05:03:51,919 we're going to minus the results of d 6808 05:03:48,718 --> 05:03:53,760 and e and we're going to do minus here i 6809 05:03:51,919 --> 05:03:56,797 hope you are following and then we're 6810 05:03:53,759 --> 05:03:59,519 going to minus f and plus g we're going 6811 05:04:01,599 --> 05:04:07,599 which is 5. okay so this is our 6812 05:04:05,520 --> 05:04:09,439 expression here and now that we have 6813 05:04:07,599 --> 05:04:11,599 this we have to think about how we're 6814 05:04:09,439 --> 05:04:14,957 going to do this we can try to do this 6815 05:04:11,599 --> 05:04:18,399 from the right do two plus five and then 6816 05:04:14,957 --> 05:04:21,439 do three minus two plus five and go from 6817 05:04:18,400 --> 05:04:25,280 right to left but our associativity 6818 05:04:21,439 --> 05:04:28,000 table says that addition and subtraction 6819 05:04:25,279 --> 05:04:30,559 have left to right associativity so we 6820 05:04:28,000 --> 05:04:34,080 will start from the left so we're going 6821 05:04:30,560 --> 05:04:36,560 to do 6 plus 24 we're going to get 30 6822 05:04:34,080 --> 05:04:38,798 we're going to minus 3 and we're going 6823 05:04:36,560 --> 05:04:41,680 to get 27 we're going to minus 2 we're 6824 05:04:38,797 --> 05:04:43,439 going to get 25 we're going to add 5 6825 05:04:41,680 --> 05:04:45,760 we're going to get 30. i hope you are 6826 05:04:43,439 --> 05:04:48,479 following and we are really following 6827 05:04:45,759 --> 05:04:50,957 the rules from our c plus plus operator 6828 05:04:48,479 --> 05:04:53,919 precedence table here okay so this is 6829 05:04:50,957 --> 05:04:56,239 really how you would use this precedence 6830 05:04:53,919 --> 05:05:00,000 and the search activity rules to 6831 05:04:56,240 --> 05:05:03,600 evaluate your expressions okay here i 6832 05:05:00,000 --> 05:05:05,840 have another example for you so try to 6833 05:05:03,599 --> 05:05:07,519 evaluate what we're going to get from 6834 05:05:07,520 --> 05:05:13,439 we have a divided by b divided by c plus 6835 05:05:10,718 --> 05:05:15,680 d minus e plus f try to do this in your 6836 05:05:13,439 --> 05:05:18,559 brain and try to guess what we're going 6837 05:05:15,680 --> 05:05:20,000 to get from the compiler if we print 6838 05:05:20,000 --> 05:05:24,797 we're going to put in our htdc out 6839 05:05:22,797 --> 05:05:28,399 statement here and we're going to try 6840 05:05:24,797 --> 05:05:30,239 and build this with gcc of course we're 6841 05:05:28,400 --> 05:05:33,200 going to run the rooster and you're 6842 05:05:30,240 --> 05:05:35,600 going to get a 24. so how did we come up 6843 05:05:33,200 --> 05:05:39,600 with that we're going to start by 6844 05:05:35,599 --> 05:05:42,797 division and multiplication and again 6845 05:05:39,599 --> 05:05:46,159 where do we start do we do b and c first 6846 05:05:42,797 --> 05:05:48,957 or do we do a and b first well we can 6847 05:05:46,159 --> 05:05:50,079 check our precedence table here 6848 05:05:48,957 --> 05:05:53,359 and we're going to check the 6849 05:05:50,080 --> 05:05:56,080 associativity of multiplication and 6850 05:05:53,360 --> 05:05:59,279 division we're going to go to this entry 6851 05:05:56,080 --> 05:06:02,560 here 13 and we see that it is left to 6852 05:05:59,279 --> 05:06:05,439 right associative these operations here 6853 05:06:02,560 --> 05:06:06,878 are left to right associative so what 6854 05:06:05,439 --> 05:06:09,439 we're going to do we're going to start 6855 05:06:06,878 --> 05:06:12,319 from left and go to right so we are 6856 05:06:09,439 --> 05:06:14,559 going to take a and divide by b if we 6857 05:06:12,319 --> 05:06:17,680 take a divided by b we're going to get a 6858 05:06:14,560 --> 05:06:20,159 2 so let's type that here then the 6859 05:06:17,680 --> 05:06:22,319 result of that is going to be multiplied 6860 05:06:20,159 --> 05:06:24,560 by c and if we go up we're going to see 6861 05:06:22,319 --> 05:06:26,319 that c is eight so we're going to 6862 05:06:26,319 --> 05:06:30,957 and get six so we can replace this with 6863 05:06:32,718 --> 05:06:38,400 then we're going to add d d is nine then 6864 05:06:40,797 --> 05:06:47,279 three then we're going to add f which is 6865 05:06:44,479 --> 05:06:50,560 a five so we have 16 plus nine this is 6866 05:06:47,279 --> 05:06:52,717 going to give us 25 minus three 6867 05:06:50,560 --> 05:06:54,240 okay by checking this out i see that i 6868 05:06:55,680 --> 05:07:01,200 five it is a two we have here 6869 05:06:58,637 --> 05:07:03,360 and if we do this from left to right 6870 05:07:01,200 --> 05:07:05,920 because addition and subtraction are 6871 05:07:03,360 --> 05:07:08,400 left associative we have to do them from 6872 05:07:05,919 --> 05:07:11,439 left to the right we're going to take 16 6873 05:07:08,400 --> 05:07:14,718 and add a 9 we're going to get at 25 6874 05:07:11,439 --> 05:07:17,199 minus 3 we're going to get 22 plus 2 we 6875 05:07:14,718 --> 05:07:19,840 are going to get 24 and that's what we 6876 05:07:17,200 --> 05:07:22,319 have here i really hope this makes sense 6877 05:07:19,840 --> 05:07:24,000 okay so we have seen how we can use this 6878 05:07:24,000 --> 05:07:29,279 but sometimes you want to make it really 6879 05:07:26,560 --> 05:07:30,240 clear which operations should be done 6880 05:07:30,240 --> 05:07:36,000 and you can do that by using parentheses 6881 05:07:33,119 --> 05:07:38,559 around your expressions let's grab the 6882 05:07:36,000 --> 05:07:40,319 first expression here and do it again 6883 05:07:41,919 --> 05:07:48,637 and put that in place and we're going to 6884 05:07:44,400 --> 05:07:51,120 store the result in our result variable 6885 05:07:48,637 --> 05:07:53,680 and notice that we are reassigning these 6886 05:07:51,119 --> 05:07:55,680 things to the result variable so that we 6887 05:07:53,680 --> 05:07:56,957 don't have to declare another variable i 6888 05:07:56,957 --> 05:08:02,559 clean and that's what we're doing here 6889 05:07:59,680 --> 05:08:05,439 so what we can do here is make it clear 6890 05:08:02,560 --> 05:08:07,440 that we want multiplication done first 6891 05:08:08,319 --> 05:08:13,360 make it clear that we want the 6892 05:08:13,360 --> 05:08:16,878 and when you do this the compiler is 6893 05:08:15,360 --> 05:08:19,360 going to see that you want this 6894 05:08:16,878 --> 05:08:22,560 operation here done first and that's 6895 05:08:19,360 --> 05:08:25,360 what it is going to do and if we print 6896 05:08:22,560 --> 05:08:28,958 result again we're going to get exactly 6897 05:08:25,360 --> 05:08:30,479 the same thing we had before stdc out 6898 05:08:30,479 --> 05:08:35,040 and we're going to put result out and 6899 05:08:32,878 --> 05:08:37,119 let's make it clear that we are using 6900 05:08:37,119 --> 05:08:42,957 signal which operation we want to be 6901 05:08:39,439 --> 05:08:45,680 done first we can compile this with gcc 6902 05:08:42,957 --> 05:08:47,919 we're going to run the task to 6903 05:08:50,878 --> 05:08:56,878 we're going to see that the result is 30 6904 05:08:53,680 --> 05:08:59,200 exactly what we had before but now it is 6905 05:08:56,878 --> 05:09:02,560 very clear to the reader of this piece 6906 05:08:59,200 --> 05:09:04,400 of code that we want b and c done first 6907 05:09:02,560 --> 05:09:06,718 they don't have to go through the 6908 05:09:04,400 --> 05:09:09,040 precedence table to really try and 6909 05:09:06,718 --> 05:09:11,040 figure this out potentially wasting 6910 05:09:09,040 --> 05:09:14,319 precious time another thing you can do 6911 05:09:11,040 --> 05:09:16,479 with this is for example what if the 6912 05:09:14,319 --> 05:09:19,200 precedence table is not working 6913 05:09:16,479 --> 05:09:21,360 according to your needs for example you 6914 05:09:21,360 --> 05:09:26,319 done first you can specify this with 6915 05:09:23,840 --> 05:09:28,560 parents here and what this is basically 6916 05:09:26,319 --> 05:09:31,279 going to do is going to force the 6917 05:09:28,560 --> 05:09:33,920 compiler to do this first it's no longer 6918 05:09:31,279 --> 05:09:37,119 going to do b multiply by c and d 6919 05:09:33,919 --> 05:09:39,359 divided by e it's going to evaluate this 6920 05:09:37,119 --> 05:09:41,840 first then it's going to follow the 6921 05:09:39,360 --> 05:09:44,240 order in the precedence table that we 6922 05:09:41,840 --> 05:09:46,479 have seen before so try to guess what 6923 05:09:44,240 --> 05:09:48,718 we're going to get now and i am going to 6924 05:09:46,479 --> 05:09:51,360 run the code and we're going to 6925 05:09:48,718 --> 05:09:54,479 go through this together so let's run 6926 05:09:51,360 --> 05:09:57,520 task and we're going to run rooster now 6927 05:09:54,479 --> 05:10:00,159 you see that the result is 72. what is 6928 05:09:57,520 --> 05:10:03,520 happening here well let's go through 6929 05:10:00,159 --> 05:10:05,520 this line by line we are doing a plus b 6930 05:10:05,520 --> 05:10:09,840 9 so we're going to get a nine here 6931 05:10:08,000 --> 05:10:11,520 we're going to put that here as a 6932 05:10:09,840 --> 05:10:13,119 comment then we're going to multiply 6933 05:10:13,119 --> 05:10:20,159 so we're going to do 9 multiply by 8 and 6934 05:10:20,159 --> 05:10:26,957 and by that time we're going to be here 6935 05:10:23,439 --> 05:10:28,559 and we're going to do the division first 6936 05:10:28,560 --> 05:10:33,040 then we're going to take nine and divide 6937 05:10:30,878 --> 05:10:35,119 by three that's going to yield the three 6938 05:10:33,040 --> 05:10:36,718 so we're going to have a three here 6939 05:10:36,718 --> 05:10:42,798 we're going to minus f which is a 2 and 6940 05:10:39,759 --> 05:10:44,957 we're going to do plus g which is a 5. 6941 05:10:44,957 --> 05:10:50,877 we're going to get 72 minus 3 which is 6942 05:10:47,439 --> 05:10:54,957 going to be 69 minus 2 it's going to be 6943 05:10:50,878 --> 05:10:57,040 67 plus 2 it's going to be 72 and that's 6944 05:10:54,957 --> 05:11:00,079 what we have here hopefully you can see 6945 05:10:57,040 --> 05:11:02,479 that using the parents not only can you 6946 05:11:00,080 --> 05:11:04,400 make it very clear which operation you 6947 05:11:02,479 --> 05:11:07,680 want to be done first you can also 6948 05:11:04,400 --> 05:11:10,080 define the rules that are prescribed by 6949 05:11:07,680 --> 05:11:12,878 the precedence tables that are defined 6950 05:11:10,080 --> 05:11:14,400 in c plus plus i hope this makes sense 6951 05:11:12,878 --> 05:11:16,479 and this is really all we set up to do 6952 05:11:14,400 --> 05:11:19,200 in this lecture to learn a little more 6953 05:11:16,479 --> 05:11:22,000 about precedence and associativity 6954 05:11:19,200 --> 05:11:24,958 and my advice to beginning programmers 6955 05:11:22,000 --> 05:11:27,759 is not to rely on the president's table 6956 05:11:24,957 --> 05:11:30,957 too much it is to try and make your 6957 05:11:27,759 --> 05:11:32,559 intent very clear using parenthesis and 6958 05:11:30,957 --> 05:11:35,199 anyone reading your code you want to 6959 05:11:32,560 --> 05:11:37,680 have to go check these rules in 6960 05:11:35,200 --> 05:11:39,840 the reference c plus plus documentation 6961 05:11:37,680 --> 05:11:42,159 they are going to directly understand 6962 05:11:39,840 --> 05:11:44,718 what your expression is going to do and 6963 05:11:42,159 --> 05:11:46,718 that's a good thing readability of your 6964 05:11:44,718 --> 05:11:48,798 code is really something you should 6965 05:11:46,718 --> 05:11:50,878 strive for this is really all we set out 6966 05:11:48,797 --> 05:11:54,079 to do in this lecture i hope you have a 6967 05:11:50,878 --> 05:11:56,560 better idea about precedence 6968 05:11:54,080 --> 05:11:58,480 and associativity we are going to stop 6969 05:11:56,560 --> 05:12:00,000 here in this lecture and the next one 6970 05:11:58,479 --> 05:12:02,319 we're going to learn about some cool 6971 05:12:00,000 --> 05:12:03,840 things we can do with addition and 6972 05:12:03,840 --> 05:12:07,840 go ahead and finish up here and meet me 6973 05:12:06,080 --> 05:12:10,400 there in this lecture we're going to be 6974 05:12:07,840 --> 05:12:13,119 learning about prefix and postfix 6975 05:12:10,400 --> 05:12:15,600 addition and subtraction we have an 6976 05:12:13,119 --> 05:12:18,718 example here and it is a simple example 6977 05:12:15,599 --> 05:12:21,359 we've seen a lot of times by now we have 6978 05:12:18,718 --> 05:12:25,440 a variable value it is an integer and we 6979 05:12:21,360 --> 05:12:28,000 have a 5 inside we can add a 1 to it or 6980 05:12:25,439 --> 05:12:30,000 we can increment it by one this is 6981 05:12:28,000 --> 05:12:31,840 another term we use in programming 6982 05:12:31,840 --> 05:12:36,797 incrementing a value we are adding 6983 05:12:34,560 --> 05:12:40,159 something to it in this case we are 6984 05:12:36,797 --> 05:12:42,559 incrementing it by one and we are 6985 05:12:40,159 --> 05:12:45,040 basically going to store a six and this 6986 05:12:42,560 --> 05:12:47,440 value here so if we print this out we're 6987 05:12:47,439 --> 05:12:51,840 this is what we would expect we are 6988 05:12:49,599 --> 05:12:54,717 resetting the value to five and that we 6989 05:12:51,840 --> 05:12:57,119 are going to minus one or decrement by 6990 05:12:54,718 --> 05:13:00,400 one and the result is going to be saved 6991 05:12:57,119 --> 05:13:03,599 in value again so the second stdc out 6992 05:13:00,400 --> 05:13:06,080 statement here is going to print a 4. 6993 05:13:03,599 --> 05:13:08,559 this is something you should expect 6994 05:13:06,080 --> 05:13:11,680 now another thing we can do in c plus 6995 05:13:08,560 --> 05:13:13,040 plus is do something like value plus 6996 05:13:13,040 --> 05:13:17,200 and what do we mean by that when you use 6997 05:13:19,360 --> 05:13:23,279 the first thing you should know is that 6998 05:13:21,040 --> 05:13:25,120 this is going to increment by one it's 6999 05:13:23,279 --> 05:13:28,239 not going to increment by any other 7000 05:13:25,119 --> 05:13:30,718 value c plus plus doesn't provide a way 7001 05:13:28,240 --> 05:13:33,840 for you to specify another number to 7002 05:13:30,718 --> 05:13:36,159 increment by so value plus plus here is 7003 05:13:33,840 --> 05:13:37,599 going to increment by one but another 7004 05:13:37,599 --> 05:13:41,919 is that it's not going to increment 7005 05:13:39,840 --> 05:13:44,000 directly what's going to happen here if 7006 05:13:41,919 --> 05:13:47,919 you look to the top here we have a value 7007 05:13:44,000 --> 05:13:49,119 5 when execution hits this stdcl 7008 05:13:49,119 --> 05:13:54,797 we are going to grab the value here so 7009 05:13:52,159 --> 05:13:57,599 here we're going to really print a 5 but 7010 05:13:54,797 --> 05:14:00,239 as soon as this statement is done the 7011 05:13:57,599 --> 05:14:02,159 value is going to increment by one so 7012 05:14:00,240 --> 05:14:04,718 the next time somebody is going to use 7013 05:14:02,159 --> 05:14:07,439 this value here is he's going to find in 7014 05:14:04,718 --> 05:14:09,119 a 6 and that's what we have here so the 7015 05:14:07,439 --> 05:14:11,439 first one is going to print a five 7016 05:14:09,119 --> 05:14:14,878 because we're going to grab the value 7017 05:14:11,439 --> 05:14:17,359 in here as is but as soon as we have our 7018 05:14:14,878 --> 05:14:19,520 value this is going to increment by one 7019 05:14:17,360 --> 05:14:21,920 this is what we mean by this value plus 7020 05:14:19,520 --> 05:14:24,957 plus here and the second statement is 7021 05:14:21,919 --> 05:14:27,839 going to print sx because it is already 7022 05:14:24,957 --> 05:14:30,957 incremented and we call this postfix 7023 05:14:27,840 --> 05:14:33,680 increment because plus plus is after the 7024 05:14:30,957 --> 05:14:36,239 value here and this is really what this 7025 05:14:33,680 --> 05:14:38,319 operator does and sometimes it is going 7026 05:14:36,240 --> 05:14:40,718 to come in handy in your c plus plus 7027 05:14:38,319 --> 05:14:43,439 applications we can also do the same 7028 05:14:40,718 --> 05:14:45,600 thing with minus minus it is going to 7029 05:14:43,439 --> 05:14:47,840 grab whatever value we have in this 7030 05:14:45,599 --> 05:14:49,919 variable so we have in a five so that's 7031 05:14:47,840 --> 05:14:52,878 what we're going to print here 7032 05:14:49,919 --> 05:14:55,279 and the next statement is going to find 7033 05:14:52,878 --> 05:14:57,360 it already decremented by one because 7034 05:14:55,279 --> 05:14:59,279 after we have our five we're going to 7035 05:14:57,360 --> 05:15:01,440 decrement by one that's what we are 7036 05:14:59,279 --> 05:15:05,360 saying here and we're going to find the 7037 05:15:01,439 --> 05:15:08,399 nf4 because 5 decremented by 1 becomes a 7038 05:15:05,360 --> 05:15:11,279 4. i really hope this makes sense ok so 7039 05:15:08,400 --> 05:15:14,240 what we have seen here is postfix 7040 05:15:11,279 --> 05:15:17,199 increment and decrement but we can also 7041 05:15:14,240 --> 05:15:19,200 do a prefix version of this thing if you 7042 05:15:17,200 --> 05:15:20,319 look at the example we have here we have 7043 05:15:20,319 --> 05:15:25,759 and we are saying plus plus value so 7044 05:15:23,279 --> 05:15:28,957 this is going to increment the value 7045 05:15:25,759 --> 05:15:31,519 directly so as soon as this statement is 7046 05:15:28,957 --> 05:15:35,039 executed the plus plus value statement 7047 05:15:31,520 --> 05:15:37,119 here we're going to have a 6 inside and 7048 05:15:35,040 --> 05:15:39,040 if we print it we're going to get a 6 7049 05:15:37,119 --> 05:15:41,759 and this is what you see here if we go 7050 05:15:39,040 --> 05:15:42,958 down we are resetting our value and we 7051 05:15:45,759 --> 05:15:51,359 so in here we are going to increment the 7052 05:15:48,400 --> 05:15:54,080 value before we actually print it here 7053 05:15:51,360 --> 05:15:56,479 and we are also going to get a six year 7054 05:15:54,080 --> 05:15:58,878 i hope this makes sense and if you have 7055 05:15:56,479 --> 05:16:01,119 any problem with this i think if we go 7056 05:15:58,878 --> 05:16:02,718 in visual studio code you're going to 7057 05:16:01,119 --> 05:16:04,399 have a better way to say this you're 7058 05:16:02,718 --> 05:16:07,520 going to see the code running and make 7059 05:16:04,400 --> 05:16:09,360 sense of it okay so i think that 7060 05:16:07,520 --> 05:16:11,920 now you understand why we're going to 7061 05:16:09,360 --> 05:16:13,440 have these numbers printed if we go down 7062 05:16:11,919 --> 05:16:15,679 we can do the same thing with 7063 05:16:13,439 --> 05:16:16,559 decrementing we have a value here it's 7064 05:16:17,599 --> 05:16:22,559 prefix decrement it it's going to be a 7065 05:16:20,159 --> 05:16:23,919 four here and if we print it we're going 7066 05:16:23,919 --> 05:16:29,279 if you go down here we are resetting 7067 05:16:25,680 --> 05:16:32,000 that to five and we are going to prefix 7068 05:16:29,279 --> 05:16:34,319 decrement in place and this is also 7069 05:16:32,000 --> 05:16:37,599 going to print a four because we have a 7070 05:16:34,319 --> 05:16:38,957 five here and if we decrement it by one 7071 05:16:37,599 --> 05:16:41,199 we're going to get a four and that's 7072 05:16:38,957 --> 05:16:44,239 what we're going to get here okay this 7073 05:16:41,200 --> 05:16:46,637 is really all these prefix and postfix 7074 05:16:44,240 --> 05:16:49,040 increment and decrement operators are 7075 05:16:46,637 --> 05:16:51,279 about we are going to go in visual 7076 05:16:49,040 --> 05:16:54,159 studio code and actually see this 7077 05:16:51,279 --> 05:16:56,000 running okay here we are in our working 7078 05:16:54,159 --> 05:16:57,919 directory we're going to do the usual 7079 05:16:56,000 --> 05:17:00,240 we're going to copy code from our 7080 05:16:57,919 --> 05:17:02,559 template project we're going to put that 7081 05:17:00,240 --> 05:17:05,200 in here we're going to go up and open 7082 05:17:02,560 --> 05:17:08,080 this up in visual studio code so we're 7083 05:17:05,200 --> 05:17:09,680 going to remove things we don't need and 7084 05:17:09,680 --> 05:17:15,200 and we are going to put in the code we 7085 05:17:11,840 --> 05:17:18,637 exactly saw in the slides here we have a 7086 05:17:15,200 --> 05:17:20,798 value it is an integer and it is five 7087 05:17:18,637 --> 05:17:24,319 if we increment it by one we're going to 7088 05:17:20,797 --> 05:17:26,159 get a six here okay and if we print it 7089 05:17:24,319 --> 05:17:28,718 that's what we're going to get we are 7090 05:17:26,159 --> 05:17:30,957 resetting this to five and if we 7091 05:17:28,718 --> 05:17:32,400 decrement it by one we're going to get a 7092 05:17:32,400 --> 05:17:37,840 so if we run this piece of code we 7093 05:17:35,040 --> 05:17:39,680 should have a six here printed out 7094 05:17:40,957 --> 05:17:47,119 we're going to bring up our terminal 7095 05:17:43,919 --> 05:17:48,559 and build this with gcc the weld is 7096 05:17:48,560 --> 05:17:54,638 and if we run the program we're going to 7097 05:17:51,279 --> 05:17:57,360 get the value a6 and it is four and this 7098 05:17:54,637 --> 05:17:59,200 is really what we expect here okay now 7099 05:17:57,360 --> 05:18:02,159 that this is really clear we're going to 7100 05:17:59,200 --> 05:18:04,718 go down and play with post fix 7101 05:18:04,718 --> 05:18:10,637 i am going to put in a separator here so 7102 05:18:07,599 --> 05:18:13,759 that we can clearly see in our terminal 7103 05:18:10,637 --> 05:18:16,000 here and we are going to put in our test 7104 05:18:13,759 --> 05:18:19,039 code that we are going to use 7105 05:18:16,000 --> 05:18:20,797 to play with this let's bring this down 7106 05:18:19,040 --> 05:18:23,680 a little bit so that you can see 7107 05:18:20,797 --> 05:18:26,319 everything we can remove these things we 7108 05:18:23,680 --> 05:18:28,000 don't need them anymore okay we are 7109 05:18:26,319 --> 05:18:29,840 going to take the value we had we're 7110 05:18:32,000 --> 05:18:37,119 and if we value plus plus here 7111 05:18:34,957 --> 05:18:40,079 this is going to be a five because we're 7112 05:18:37,119 --> 05:18:41,919 going to capture the value as is so here 7113 05:18:41,919 --> 05:18:46,319 but next time we get this value it's 7114 05:18:44,159 --> 05:18:49,680 going to already be a sex because we 7115 05:18:46,319 --> 05:18:52,079 increment immediately after capturing 7116 05:18:49,680 --> 05:18:55,520 the value here so here we're going to 7117 05:18:52,080 --> 05:18:57,600 get a 5 and here we're going to get a 6. 7118 05:18:57,599 --> 05:19:03,199 and if we go down here we are resetting 7119 05:19:00,560 --> 05:19:04,798 our value to 5 before we do any 7120 05:19:04,797 --> 05:19:09,439 we are going to capture the value as is 7121 05:19:07,599 --> 05:19:10,717 because we are doing post fixed 7122 05:19:10,718 --> 05:19:15,840 and here we are going to get a 5 because 7123 05:19:13,040 --> 05:19:18,080 we are capturing the value as s 7124 05:19:15,840 --> 05:19:20,560 but as soon as we capture our value 7125 05:19:18,080 --> 05:19:22,958 we're going to decrement it and the 7126 05:19:20,560 --> 05:19:25,680 other statement here is going to get a 7127 05:19:22,957 --> 05:19:29,439 decremented value and that's going to be 7128 05:19:25,680 --> 05:19:30,560 a 4. so here we're going to print 5 6 5 7129 05:19:30,560 --> 05:19:36,638 let's try and build this with gcc 7130 05:19:34,718 --> 05:19:39,360 to see that we have no problems we have 7131 05:19:36,637 --> 05:19:41,200 no problems the world is good if we try 7132 05:19:47,439 --> 05:19:53,199 but uh for this code we just put in here 7133 05:19:51,040 --> 05:19:55,040 the value is going to be a 5 and that's 7134 05:19:55,040 --> 05:19:58,638 and we're going to get a 6 which is 7135 05:19:58,637 --> 05:20:03,759 after we are going to decrement and we 7136 05:20:01,279 --> 05:20:06,479 are going to capture the value as is 7137 05:20:03,759 --> 05:20:08,399 we're going to get this 5 here and for 7138 05:20:06,479 --> 05:20:11,520 the last statement we're going to get a 7139 05:20:08,400 --> 05:20:14,240 4 because the value is already 7140 05:20:11,520 --> 05:20:15,520 decremented and that's the for we are 7141 05:20:15,520 --> 05:20:19,600 i really hope this makes sense and this 7142 05:20:19,599 --> 05:20:24,797 the postfix increment and decrement 7143 05:20:22,319 --> 05:20:28,239 operators the last thing we want to see 7144 05:20:24,797 --> 05:20:31,680 in this lecture is that we can also do 7145 05:20:28,240 --> 05:20:34,159 prefix increment and decrementation 7146 05:20:31,680 --> 05:20:35,840 we're going to go down here and 7147 05:20:34,159 --> 05:20:37,680 set up a place where we're going to be 7148 05:20:35,840 --> 05:20:39,439 playing with this so we're going to say 7149 05:20:39,439 --> 05:20:43,439 and we are going to put in our code 7150 05:20:41,360 --> 05:20:46,319 because i don't want to type all this 7151 05:20:43,439 --> 05:20:48,239 that would waste your valuable time but 7152 05:20:46,319 --> 05:20:49,680 i do encourage you to type these things 7153 05:20:49,680 --> 05:20:53,760 drill them in your brain because that's 7154 05:20:51,919 --> 05:20:55,359 how you're going to learn okay let's 7155 05:20:53,759 --> 05:20:58,319 bring this down so that we can 7156 05:20:55,360 --> 05:21:01,600 understand this piece of code so we are 7157 05:20:58,319 --> 05:21:04,319 starting by resetting our value to 5 and 7158 05:21:01,599 --> 05:21:06,877 notice here that we do plus plus value 7159 05:21:04,319 --> 05:21:08,957 this is going to increment value right 7160 05:21:06,878 --> 05:21:12,240 away by this time the value is going to 7161 05:21:08,957 --> 05:21:14,557 be six and if we print it out here we're 7162 05:21:12,240 --> 05:21:16,718 going to have a six printed down we're 7163 05:21:14,558 --> 05:21:19,680 going to reset it to five and then we're 7164 05:21:16,718 --> 05:21:22,319 going to implement that in place this is 7165 05:21:19,680 --> 05:21:23,920 also going to be printing six and if you 7166 05:21:22,319 --> 05:21:25,840 go down we're going to do the same 7167 05:21:23,919 --> 05:21:27,679 version of the same thing but we are 7168 05:21:25,840 --> 05:21:30,000 going to be decrementing so we're going 7169 05:21:27,680 --> 05:21:31,279 to decrement the value that we just 7170 05:21:31,279 --> 05:21:35,599 here it's going to become a 4 and that's 7171 05:21:33,279 --> 05:21:38,239 what we're going to print and if we go 7172 05:21:35,599 --> 05:21:41,599 down here we reset it again to a 5 and 7173 05:21:38,240 --> 05:21:44,878 we prefix decrement it in place 7174 05:21:41,599 --> 05:21:46,637 so the 5 here is going to become a 4 7175 05:21:44,878 --> 05:21:48,958 in here and that's what we're going to 7176 05:21:46,637 --> 05:21:50,399 print out if we build and run that's 7177 05:21:48,957 --> 05:21:53,199 what we're going to get we're going to 7178 05:21:53,200 --> 05:22:00,000 the build is good and if we run our 7179 05:21:56,400 --> 05:22:01,920 rooster application the results for the 7180 05:22:00,000 --> 05:22:03,360 code we just put in here are just going 7181 05:22:03,360 --> 05:22:06,958 and you see that we get a six six this 7182 05:22:06,957 --> 05:22:11,279 the other six and for the last two 7183 05:22:09,599 --> 05:22:13,840 statements we're going to be getting 7184 05:22:11,279 --> 05:22:15,680 fours and that's what we have here i 7185 05:22:13,840 --> 05:22:18,637 really hope that now you have a better 7186 05:22:15,680 --> 05:22:21,520 idea of this prefix and postfix 7187 05:22:18,637 --> 05:22:24,079 increment and decrement operators and 7188 05:22:21,520 --> 05:22:26,159 you're going to make use of them when 7189 05:22:24,080 --> 05:22:28,480 they really helped you design your 7190 05:22:26,159 --> 05:22:31,279 applications better okay so these are 7191 05:22:28,479 --> 05:22:33,040 your tools use them when they make sense 7192 05:22:31,279 --> 05:22:35,199 another thing that i want to emphasize 7193 05:22:33,040 --> 05:22:38,558 here is that these operators only 7194 05:22:35,200 --> 05:22:40,400 increment or a decrement by one you 7195 05:22:40,400 --> 05:22:45,840 do things in increments of five or seven 7196 05:22:43,200 --> 05:22:48,159 or ten they are going to work in 7197 05:22:45,840 --> 05:22:50,000 increments or decrements of one we are 7198 05:22:48,159 --> 05:22:52,319 going to see a way we can do that with 7199 05:22:50,000 --> 05:22:55,119 other operators in the last lecture but 7200 05:22:52,319 --> 05:22:58,079 these prefix and postfix operators can't 7201 05:22:55,119 --> 05:23:00,797 do that and another thing that some of 7202 05:22:58,080 --> 05:23:03,440 you might be wondering about is if we 7203 05:23:00,797 --> 05:23:06,079 have the same kind of operators with 7204 05:23:03,439 --> 05:23:08,479 multiplication or division and we don't 7205 05:23:06,080 --> 05:23:10,000 have those operators in c plus plus 7206 05:23:08,479 --> 05:23:12,239 maybe we're going to have them in the 7207 05:23:10,000 --> 05:23:14,797 future when they make sense but in the 7208 05:23:12,240 --> 05:23:16,638 current version of c plus plus we don't 7209 05:23:14,797 --> 05:23:18,557 have them we are going to stop here in 7210 05:23:16,637 --> 05:23:21,200 this lecture in the next one we're going 7211 05:23:18,558 --> 05:23:23,520 to learn about compound assignment 7212 05:23:21,200 --> 05:23:26,000 operators in this lecture we're going to 7213 05:23:23,520 --> 05:23:27,360 learn about compound assignment 7214 05:23:27,360 --> 05:23:33,119 and these are operators that allow you 7215 05:23:30,080 --> 05:23:35,920 to do an arithmetic operation like 7216 05:23:33,119 --> 05:23:39,360 addition subtraction multiplication 7217 05:23:35,919 --> 05:23:43,039 division and modulus and assign the 7218 05:23:39,360 --> 05:23:45,360 result to a value in one go let's look 7219 05:23:43,040 --> 05:23:48,159 at an example here we have value here 7220 05:23:45,360 --> 05:23:49,920 which is 45 it is an integer and we're 7221 05:23:48,159 --> 05:23:54,079 going to print that out but if you go 7222 05:23:49,919 --> 05:23:56,399 down here we are doing value plus equal 7223 05:23:54,080 --> 05:23:59,440 five and if you look at the comment here 7224 05:23:56,400 --> 05:24:02,080 this is equivalent to if you did value 7225 05:23:59,439 --> 05:24:03,919 equals value plus five so what this is 7226 05:24:02,080 --> 05:24:06,320 going to do it's going to take whatever 7227 05:24:03,919 --> 05:24:08,479 value is in this value here it's going 7228 05:24:06,319 --> 05:24:10,797 to increment that by five so that's 7229 05:24:08,479 --> 05:24:14,159 going to become a 50 and we're going to 7230 05:24:10,797 --> 05:24:16,399 store that 50 results back in this 7231 05:24:14,159 --> 05:24:17,840 variable here so by this time if we 7232 05:24:16,400 --> 05:24:20,319 printed this out we're going to get a 7233 05:24:17,840 --> 05:24:23,200 50. again we're going to take the value 7234 05:24:20,319 --> 05:24:25,360 it is a 45 we're going to add a 5 it's 7235 05:24:23,200 --> 05:24:28,718 going to become a 50 and we're going to 7236 05:24:25,360 --> 05:24:31,600 store that result back in this value 7237 05:24:28,718 --> 05:24:33,440 variable here it's going to become 50 7238 05:24:31,599 --> 05:24:36,479 and if we print here we're going to get 7239 05:24:33,439 --> 05:24:39,520 a 50. this is the addition version of 7240 05:24:36,479 --> 05:24:42,239 the compound assignment operator we also 7241 05:24:39,520 --> 05:24:44,400 have a version for subtraction and we 7242 05:24:42,240 --> 05:24:46,638 have an example of that here so it's 7243 05:24:44,400 --> 05:24:48,798 going to take whatever value is in this 7244 05:24:46,637 --> 05:24:51,919 value variable it is going to find in a 7245 05:24:48,797 --> 05:24:54,479 50 we're going to subtract 5 we're going 7246 05:24:51,919 --> 05:24:56,877 to get a 45 and that's what we're going 7247 05:24:56,878 --> 05:25:03,200 and by the time we hit this stdc out 7248 05:25:00,558 --> 05:25:05,120 statement we're going to print a 45. 7249 05:25:03,200 --> 05:25:07,280 again we have another version for the 7250 05:25:05,119 --> 05:25:09,680 multiplication here so that we can 7251 05:25:07,279 --> 05:25:11,520 really play with all these things 7252 05:25:09,680 --> 05:25:14,000 we're going to take whatever value is in 7253 05:25:11,520 --> 05:25:16,878 here we're going to multiply that by 2 7254 05:25:14,000 --> 05:25:18,637 here we're going to get a 90 and that's 7255 05:25:16,878 --> 05:25:20,958 what we're going to store back in this 7256 05:25:18,637 --> 05:25:22,718 value and if we print here we're going 7257 05:25:22,718 --> 05:25:27,600 printed out we're going to go down and 7258 05:25:24,637 --> 05:25:30,557 do a version for a division the value is 7259 05:25:27,599 --> 05:25:32,957 90 we're going to divide that by 30 and 7260 05:25:30,558 --> 05:25:36,080 we're going to store the result back in 7261 05:25:32,957 --> 05:25:38,877 value here and we're going to get a 30 7262 05:25:36,080 --> 05:25:41,040 stored in here and if we print out 7263 05:25:38,878 --> 05:25:43,520 that's what we're going to get here 7264 05:25:41,040 --> 05:25:45,440 we also have a modulus version of this 7265 05:25:45,439 --> 05:25:51,199 here we have unit 30 we're going to take 7266 05:25:47,599 --> 05:25:54,239 it and do modulus 11 and what is the 7267 05:25:54,240 --> 05:25:58,240 by 11. the reminder is going to be 8 7268 05:25:58,240 --> 05:26:04,000 11 can only fit in 30 two times and 7269 05:26:02,319 --> 05:26:05,040 we're going to be left with the knight 7270 05:26:05,040 --> 05:26:10,400 30 minus 22 is eight and that's what 7271 05:26:08,240 --> 05:26:12,558 we're going to store in the value here 7272 05:26:10,400 --> 05:26:14,400 and if we print out that's what we're 7273 05:26:14,400 --> 05:26:19,040 if you have trouble understanding this 7274 05:26:16,319 --> 05:26:21,200 modulus operator here please go back in 7275 05:26:19,040 --> 05:26:23,440 a few lectures back we had a chance to 7276 05:26:21,200 --> 05:26:25,760 learn about it make sure you understand 7277 05:26:23,439 --> 05:26:27,439 it and then come back here and this is 7278 05:26:27,439 --> 05:26:32,877 this is really all compound assignment 7279 05:26:30,479 --> 05:26:35,200 is all about we're going to head to 7280 05:26:32,878 --> 05:26:39,040 visual studio code and actually play 7281 05:26:35,200 --> 05:26:41,520 with us okay here we are in our working 7282 05:26:39,040 --> 05:26:44,798 directory we're going to copy our 7283 05:26:41,520 --> 05:26:48,240 template project as we always do 7284 05:26:44,797 --> 05:26:50,797 and we're going to put that in our 7285 05:26:48,240 --> 05:26:52,320 compound assignment operators 7286 05:26:52,319 --> 05:26:56,797 we're going to come up and we are going 7287 05:26:54,558 --> 05:26:58,878 to open this folder in visual studio 7288 05:26:56,797 --> 05:27:01,279 code the project is going to show up in 7289 05:26:58,878 --> 05:27:04,718 here and we're going to open our main 7290 05:27:01,279 --> 05:27:06,557 cpp file and we are going to remove what 7291 05:27:06,558 --> 05:27:12,400 we are going to put in our code so that 7292 05:27:08,718 --> 05:27:14,400 we can start exploring this we have a 45 7293 05:27:12,400 --> 05:27:16,558 and this variable here which is an 7294 05:27:14,400 --> 05:27:18,159 integer if we print it out we're going 7295 05:27:19,919 --> 05:27:25,439 we are doing a compound addition 7296 05:27:23,279 --> 05:27:28,399 so what we are doing here we're going to 7297 05:27:25,439 --> 05:27:30,479 grab whatever value is in this variable 7298 05:27:28,400 --> 05:27:32,480 we're going to add a five we're going to 7299 05:27:30,479 --> 05:27:34,239 get a 50 and that's what is going to be 7300 05:27:34,240 --> 05:27:40,638 and if we print this out we're going to 7301 05:27:36,240 --> 05:27:42,878 get a 50. let's put in a terminal here 7302 05:27:40,637 --> 05:27:45,039 so that we can see this running and 7303 05:27:42,878 --> 05:27:48,400 we're going to run the task to build 7304 05:27:50,840 --> 05:27:56,797 executable we're going to get the value 7305 05:27:53,360 --> 05:27:58,718 is 50 and that's what we expect here 7306 05:27:58,718 --> 05:28:04,400 addition version of our compound 7307 05:28:01,520 --> 05:28:07,760 assignment operator and it is really 7308 05:28:04,400 --> 05:28:10,319 equivalent to doing this in two steps 7309 05:28:07,759 --> 05:28:12,079 saying value equals value plus five 7310 05:28:10,319 --> 05:28:14,400 you're going to get the same result if 7311 05:28:12,080 --> 05:28:17,360 you do that here actually let's do this 7312 05:28:14,400 --> 05:28:20,240 so that you don't think that i am making 7313 05:28:17,360 --> 05:28:22,159 this up so let's comment this out and we 7314 05:28:22,159 --> 05:28:29,040 equals value plus five and if we build 7315 05:28:31,279 --> 05:28:37,039 we're going to see the same thing we can 7316 05:28:33,599 --> 05:28:40,000 also go down and run the subtraction 7317 05:28:37,040 --> 05:28:42,000 version of this example here here we are 7318 05:28:40,000 --> 05:28:43,599 taking the value that we have in here 7319 05:28:43,599 --> 05:28:49,439 we are subtracting 5 we're going to get 7320 05:28:45,680 --> 05:28:51,200 a 45 and 45 is what we're going to store 7321 05:28:51,200 --> 05:28:54,718 that's what we're going to see if we 7322 05:28:54,718 --> 05:29:01,200 let's run the test world with gcc 7323 05:28:57,840 --> 05:29:03,360 so that our changes are reflected in our 7324 05:29:01,200 --> 05:29:06,558 output we're going to run this and we're 7325 05:29:03,360 --> 05:29:08,797 going to get a 45 this is what we expect 7326 05:29:06,558 --> 05:29:11,360 we are going to go down and do a 7327 05:29:08,797 --> 05:29:14,239 multiplication version of this 7328 05:29:11,360 --> 05:29:16,718 here we have our compound assignment 7329 05:29:14,240 --> 05:29:18,798 statement we are taking whatever value 7330 05:29:16,718 --> 05:29:21,520 we have in here which is a 45 we're 7331 05:29:18,797 --> 05:29:24,239 going to multiply that by 2 and the 7332 05:29:21,520 --> 05:29:26,479 result of that is going to be 90 and 7333 05:29:24,240 --> 05:29:28,320 we're going to store that back in value 7334 05:29:26,479 --> 05:29:31,040 here and if we print value here we're 7335 05:29:28,319 --> 05:29:33,840 going to get a 19. let's build this run 7336 05:29:31,040 --> 05:29:35,520 the gcc task and we're going to run our 7337 05:29:33,840 --> 05:29:37,759 program and we're going to see that 7338 05:29:35,520 --> 05:29:40,080 we're going to see this 90 here we can 7339 05:29:37,759 --> 05:29:42,797 go down by a few lines and put in the 7340 05:29:40,080 --> 05:29:43,600 code to play with a division version of 7341 05:29:43,599 --> 05:29:48,159 again this is going to do really the 7342 05:29:46,000 --> 05:29:50,240 same thing only it's going to be doing 7343 05:29:48,159 --> 05:29:52,159 this for division we're going to take 7344 05:29:50,240 --> 05:29:54,798 whatever value we have in here which is 7345 05:29:52,159 --> 05:29:57,840 going to be a 90 we're going to divide 7346 05:29:54,797 --> 05:30:01,039 that by 3 we're going to get a 30 and 7347 05:29:57,840 --> 05:30:04,000 that 30 results is going to be stored 7348 05:30:01,040 --> 05:30:06,479 back in value here and if we print this 7349 05:30:04,000 --> 05:30:08,797 out that's what we're going to get 7350 05:30:06,479 --> 05:30:11,040 let's build again and the build is going 7351 05:30:11,040 --> 05:30:15,840 our program we're going to get a 30. we 7352 05:30:13,680 --> 05:30:19,040 are going to go down by a few lines and 7353 05:30:15,840 --> 05:30:21,040 put in a modulus version of this we're 7354 05:30:19,040 --> 05:30:23,600 going to grab whatever value we have and 7355 05:30:21,040 --> 05:30:27,360 this variable here which is going to be 7356 05:30:23,599 --> 05:30:28,319 30 we're going to do modulus 11 on that 7357 05:30:28,319 --> 05:30:33,759 and that's going to be basically 30 7358 05:30:30,878 --> 05:30:35,200 modulus 11. the result of that is going 7359 05:30:35,200 --> 05:30:40,637 and we're going to store that result in 7360 05:30:37,759 --> 05:30:42,479 value here and if we print that out 7361 05:30:40,637 --> 05:30:45,599 that's what we're going to see in this 7362 05:30:42,479 --> 05:30:49,439 output statement here we can build again 7363 05:30:45,599 --> 05:30:52,479 run with gcc and if we run our rooster 7364 05:30:49,439 --> 05:30:55,039 program we're going to get an 8 printed 7365 05:30:55,040 --> 05:31:00,159 these compound assignments are about and 7366 05:31:00,159 --> 05:31:04,079 why go through the trouble to do 7367 05:31:01,919 --> 05:31:06,479 something like this if i can do this 7368 05:31:04,080 --> 05:31:08,718 like this well c plus plus gives you 7369 05:31:06,479 --> 05:31:11,119 different way to do things different 7370 05:31:08,718 --> 05:31:14,159 people have different preferences and 7371 05:31:11,119 --> 05:31:16,399 some people me included like to do 7372 05:31:14,159 --> 05:31:18,479 things this way because it is compact 7373 05:31:16,400 --> 05:31:20,159 and once you use this you're going to 7374 05:31:18,479 --> 05:31:22,637 get used to this you're not even going 7375 05:31:20,159 --> 05:31:25,279 to think about it in this lecture here i 7376 05:31:22,637 --> 05:31:27,759 just wanted you to be aware of this 7377 05:31:25,279 --> 05:31:30,797 possibility because you are definitely 7378 05:31:27,759 --> 05:31:31,679 going to see it in c plus plus code out 7379 05:31:31,680 --> 05:31:34,957 this is really all we set out to do in 7380 05:31:33,360 --> 05:31:37,279 this lecture i hope you found it 7381 05:31:34,957 --> 05:31:39,119 interesting we are going to stop here in 7382 05:31:37,279 --> 05:31:42,557 this lecture in the next one we're going 7383 05:31:39,119 --> 05:31:44,637 to learn about relational operators in c 7384 05:31:42,558 --> 05:31:47,760 plus plus in this lecture we're going to 7385 05:31:44,637 --> 05:31:49,919 learn about relational operators in c 7386 05:31:47,759 --> 05:31:53,279 plus plus and these are basically 7387 05:31:49,919 --> 05:31:56,159 operators you use to compare stuff let's 7388 05:31:53,279 --> 05:32:00,239 look at a piece of code here we have two 7389 05:31:56,159 --> 05:32:03,040 numbers one is 45 and another is 60 they 7390 05:32:00,240 --> 05:32:04,958 are stored in integer variables number 7391 05:32:04,957 --> 05:32:10,557 we can print them out and we're going to 7392 05:32:06,718 --> 05:32:13,200 see them on the console or our terminal 7393 05:32:10,558 --> 05:32:16,319 and if we go down we can really compare 7394 05:32:13,200 --> 05:32:19,119 them we can use the less than operator 7395 05:32:19,119 --> 05:32:24,957 greater than greater or equal operator 7396 05:32:22,080 --> 05:32:27,840 we can check and see if two numbers are 7397 05:32:24,957 --> 05:32:30,079 equal by using two equal signs here be 7398 05:32:27,840 --> 05:32:32,319 careful about these two equal signs 7399 05:32:30,080 --> 05:32:34,798 because if you use one you're not going 7400 05:32:32,319 --> 05:32:36,637 to be comparing numbers you're going to 7401 05:32:34,797 --> 05:32:39,360 be taking the thing on the right and 7402 05:32:36,637 --> 05:32:41,599 assigning that to the thing on the left 7403 05:32:39,360 --> 05:32:44,479 so if you want to compare you're going 7404 05:32:41,599 --> 05:32:47,279 to have to type two equal signs another 7405 05:32:44,479 --> 05:32:50,079 operator we can use to compare stuff is 7406 05:32:47,279 --> 05:32:52,797 exclamation mark and equal which is 7407 05:32:50,080 --> 05:32:54,000 going to basically test if two numbers 7408 05:32:54,000 --> 05:32:59,119 and we're going to get the result one 7409 05:32:56,400 --> 05:33:02,958 thing you should notice here is that we 7410 05:32:59,119 --> 05:33:05,919 are wrapping our comparison expression 7411 05:33:02,957 --> 05:33:07,680 into parenthesis that's because if we 7412 05:33:05,919 --> 05:33:10,159 don't we're going to get a weird 7413 05:33:07,680 --> 05:33:12,718 compiler error that i am going to show 7414 05:33:10,159 --> 05:33:15,040 you when we get in visual studio code 7415 05:33:12,718 --> 05:33:17,040 but we don't have enough tools to really 7416 05:33:17,040 --> 05:33:22,159 at this moment so what we are going to 7417 05:33:19,680 --> 05:33:24,878 do is avoid that problem by putting 7418 05:33:22,159 --> 05:33:26,957 parenthesis around our comparison 7419 05:33:24,878 --> 05:33:29,520 expression here we're going to go to 7420 05:33:26,957 --> 05:33:32,957 visual studio code and actually see this 7421 05:33:29,520 --> 05:33:35,040 in action here we are in our working 7422 05:33:32,957 --> 05:33:37,279 directory we're going to grab the code 7423 05:33:35,040 --> 05:33:39,840 from our template here and we're going 7424 05:33:37,279 --> 05:33:40,840 to put that in the current project which 7425 05:33:40,840 --> 05:33:46,000 relational operators we're going to go 7426 05:33:42,957 --> 05:33:48,159 in and put in our code and we are going 7427 05:33:46,000 --> 05:33:50,159 to open this up in visual studio code 7428 05:33:48,159 --> 05:33:52,797 we're going to get rid of things we 7429 05:33:50,159 --> 05:33:55,040 don't need here we are going to put in 7430 05:33:52,797 --> 05:33:57,759 our code so that we can explore these 7431 05:33:55,040 --> 05:34:00,878 things we have two variables in one we 7432 05:33:57,759 --> 05:34:02,877 have a 45 in another we have a 60 and 7433 05:34:00,878 --> 05:34:05,840 these are integers the name for these 7434 05:34:02,878 --> 05:34:07,600 variables are number one and number two 7435 05:34:05,840 --> 05:34:10,240 we're going to print them out and we're 7436 05:34:07,599 --> 05:34:11,919 going to see them on the console now we 7437 05:34:11,919 --> 05:34:18,797 one thing i haven't told you yet is that 7438 05:34:14,400 --> 05:34:21,600 these comparison operators evaluate to 7439 05:34:18,797 --> 05:34:24,479 booleans in other words they are going 7440 05:34:21,599 --> 05:34:25,759 to return true all false if you look 7441 05:34:25,759 --> 05:34:31,599 we want to know if number one is less 7442 05:34:28,957 --> 05:34:33,759 than number two and the result of that 7443 05:34:31,599 --> 05:34:35,279 is whether that's true or false and this 7444 05:34:35,279 --> 05:34:42,797 so because of that we are using std bull 7445 05:34:39,279 --> 05:34:45,840 alpha here to force sddc out to print 7446 05:34:42,797 --> 05:34:48,717 this boolean value here as a true or 7447 05:34:45,840 --> 05:34:51,599 false instead of a one or a zero and we 7448 05:34:48,718 --> 05:34:54,718 have seen this before so if we try to 7449 05:34:51,599 --> 05:34:56,637 build this and run this let's bring up a 7450 05:34:56,637 --> 05:35:02,159 and we're going to run the test to build 7451 05:34:58,558 --> 05:35:04,319 with gcc if we go down here and run our 7452 05:35:02,159 --> 05:35:07,759 rooster program we're going to get our 7453 05:35:04,319 --> 05:35:10,479 two numbers 45 and six and we want to 7454 05:35:07,759 --> 05:35:14,079 know if number one is less than number 7455 05:35:10,479 --> 05:35:16,239 two and that's true 45 is less than 60 7456 05:35:14,080 --> 05:35:18,638 so the result of this expression is 7457 05:35:16,240 --> 05:35:20,958 going to be true now you know how to use 7458 05:35:18,637 --> 05:35:23,439 this operator one thing i want you to 7459 05:35:24,718 --> 05:35:30,159 didn't use this parenthesis around this 7460 05:35:27,680 --> 05:35:32,159 expression here let's try this out we're 7461 05:35:30,159 --> 05:35:34,718 going to remove this parenthesis and 7462 05:35:32,159 --> 05:35:37,919 we're going to try and world we're going 7463 05:35:34,718 --> 05:35:40,319 to run the task to world with gcc and 7464 05:35:37,919 --> 05:35:41,759 we're going to get a really weird error 7465 05:35:41,759 --> 05:35:47,119 the compiler is going to try and 7466 05:35:43,840 --> 05:35:49,759 evaluate this operator first if we go 7467 05:35:47,119 --> 05:35:51,360 back to our c plus plus operator 7468 05:35:51,360 --> 05:35:57,200 and look at the operators we are trying 7469 05:35:53,919 --> 05:35:59,359 to use in the sddc out statement we're 7470 05:35:59,360 --> 05:36:05,680 operator to print fans on the console 7471 05:36:02,240 --> 05:36:08,718 has high precedence than our comparison 7472 05:36:05,680 --> 05:36:11,680 operators here so the precedence for our 7473 05:36:08,718 --> 05:36:15,200 operator to print things to sdd out is 7474 05:36:15,200 --> 05:36:21,360 precedence for our comparison operators 7475 05:36:18,240 --> 05:36:23,920 is nine and eight so the compiler is 7476 05:36:21,360 --> 05:36:26,000 going to try and do this operator first 7477 05:36:23,919 --> 05:36:27,759 if we come back in visual studio code 7478 05:36:26,000 --> 05:36:30,319 the compiler is going to try and think 7479 05:36:27,759 --> 05:36:32,239 that you are printing number one but 7480 05:36:30,319 --> 05:36:34,797 then again it's going to see this less 7481 05:36:32,240 --> 05:36:37,120 than sign and things are going to blow 7482 05:36:34,797 --> 05:36:39,199 up we don't really have enough tools to 7483 05:36:37,119 --> 05:36:41,680 understand what is going on right now 7484 05:36:39,200 --> 05:36:46,080 but i just don't want you to see this 7485 05:36:41,680 --> 05:36:48,400 arrow and show you a way to go around it 7486 05:36:46,080 --> 05:36:51,280 and move on with your life by comparing 7487 05:36:48,400 --> 05:36:53,360 stuff in your c plus plus programs if 7488 05:36:51,279 --> 05:36:55,199 you put parentheses around here the 7489 05:36:53,360 --> 05:36:56,718 problem is going to go away let's try 7490 05:36:56,718 --> 05:37:01,280 we're going to build with gcc 7491 05:36:59,360 --> 05:37:04,080 and everything is going to go back to 7492 05:37:01,279 --> 05:37:07,599 normal and we're going to see our output 7493 05:37:04,080 --> 05:37:10,000 if we run our rooster program here 7494 05:37:07,599 --> 05:37:11,759 this is how you can do this okay now 7495 05:37:10,000 --> 05:37:14,637 that we have a basic understanding of 7496 05:37:11,759 --> 05:37:17,759 this we can try the other operators and 7497 05:37:14,637 --> 05:37:20,479 it is going to be a walk in the park we 7498 05:37:17,759 --> 05:37:24,399 are going to go down here and put in our 7499 05:37:20,479 --> 05:37:28,079 remaining operators we can use less or 7500 05:37:24,400 --> 05:37:31,120 eco operator to test and see if number 7501 05:37:28,080 --> 05:37:33,440 one is less or equal to number two we 7502 05:37:31,119 --> 05:37:36,079 can use the greater than operator to 7503 05:37:33,439 --> 05:37:38,637 test and see if number one is greater 7504 05:37:36,080 --> 05:37:40,400 than number two we can use the greater 7505 05:37:40,400 --> 05:37:44,718 we can use the equal equal operator to 7506 05:37:42,479 --> 05:37:47,439 test that number one is equal to number 7507 05:37:44,718 --> 05:37:50,400 two and we can use this operator here 7508 05:37:47,439 --> 05:37:53,599 which is made up of the exclamation mark 7509 05:37:50,400 --> 05:37:56,000 and an equal sign to test and see 7510 05:37:56,000 --> 05:38:00,718 and all these operators are going to 7511 05:38:00,718 --> 05:38:06,637 basically saying if what you are testing 7512 05:38:03,520 --> 05:38:08,878 for is true or false and we can print 7513 05:38:06,637 --> 05:38:11,200 this out and notice that we have this 7514 05:38:08,878 --> 05:38:14,240 parenthesis around to avoid the problem 7515 05:38:11,200 --> 05:38:16,159 i just showed you a few minutes earlier 7516 05:38:14,240 --> 05:38:17,920 and if we run this we're going to see 7517 05:38:17,919 --> 05:38:24,957 let's weld this with gcc as usual 7518 05:38:22,637 --> 05:38:27,200 we're going to run our program and 7519 05:38:24,957 --> 05:38:30,159 you're going to see that number one is 7520 05:38:27,200 --> 05:38:32,637 less than number two this is true 7521 05:38:30,159 --> 05:38:34,240 number one less or equal to number two 7522 05:38:34,240 --> 05:38:40,558 number one greater than a number two 45 7523 05:38:37,599 --> 05:38:43,199 is not greater than 60 so this is false 7524 05:38:40,558 --> 05:38:44,718 it's not greater than equal so this is 7525 05:38:44,718 --> 05:38:49,200 number one equals to number two this is 7526 05:38:46,957 --> 05:38:51,680 going to be false and uh the last 7527 05:38:49,200 --> 05:38:54,878 statement that is going to test and see 7528 05:38:51,680 --> 05:38:57,680 if number one is not equal to number two 7529 05:38:54,878 --> 05:39:00,319 is going to evaluate to true because 7530 05:38:57,680 --> 05:39:02,637 number one is not equal to number two if 7531 05:39:00,319 --> 05:39:05,040 this is not making sense yet please go 7532 05:39:02,637 --> 05:39:07,919 through it a couple of times and if you 7533 05:39:05,040 --> 05:39:10,319 have a problem please do ask me i will 7534 05:39:07,919 --> 05:39:12,319 do the best i can to help you out 7535 05:39:10,319 --> 05:39:13,599 another thing i want you to see before i 7536 05:39:13,599 --> 05:39:20,319 is that you can store the results of 7537 05:39:16,718 --> 05:39:22,637 these operators into a variable and use 7538 05:39:20,319 --> 05:39:24,637 that later in your program for example 7539 05:39:22,637 --> 05:39:25,840 here we can take our expression to 7540 05:39:25,840 --> 05:39:31,200 number one and number two the result of 7541 05:39:29,200 --> 05:39:32,558 that is going to be installed in this 7542 05:39:32,558 --> 05:39:36,400 and we can print it out later 7543 05:39:37,759 --> 05:39:42,479 we're going to build with gcc and we're 7544 05:39:42,479 --> 05:39:49,520 we're going to see that 45 equals to 60 7545 05:39:46,400 --> 05:39:51,840 is false and it is coming from this 7546 05:39:49,520 --> 05:39:54,080 statement here you can try and change 7547 05:39:51,840 --> 05:39:57,920 these numbers for example change this to 7548 05:39:54,080 --> 05:40:00,400 a 20 and see the results that we get and 7549 05:39:57,919 --> 05:40:03,199 you can try and run these in your mind 7550 05:40:00,400 --> 05:40:05,520 and see that they really make sense if 7551 05:40:03,200 --> 05:40:07,520 we run rooster now we're going to get to 7552 05:40:05,520 --> 05:40:12,558 different results because we are now 7553 05:40:07,520 --> 05:40:14,400 comparing 45 and 20 and here at the last 7554 05:40:12,558 --> 05:40:16,558 statement you see that they are not 7555 05:40:14,400 --> 05:40:19,840 equal if we put into equal numbers we're 7556 05:40:16,558 --> 05:40:22,400 going to get a true there let's try this 7557 05:40:19,840 --> 05:40:25,920 out we're going to run the task to build 7558 05:40:22,400 --> 05:40:29,120 with gcc and if we clear so that we get 7559 05:40:25,919 --> 05:40:30,557 rid of all this noise and run rooster 7560 05:40:30,558 --> 05:40:35,680 20 is equal to 20 and this is true this 7561 05:40:34,080 --> 05:40:38,160 is really all we set out to do in this 7562 05:40:35,680 --> 05:40:40,558 lecture i hope you have a better idea of 7563 05:40:38,159 --> 05:40:41,439 these relational operators in z plus 7564 05:40:41,439 --> 05:40:45,359 we're going to stop here in this lecture 7565 05:40:43,759 --> 05:40:48,797 in the next lecture we're going to learn 7566 05:40:45,360 --> 05:40:51,200 about logical operators in c plus plus 7567 05:40:48,797 --> 05:40:53,119 go ahead and finish up here and meet me 7568 05:40:51,200 --> 05:40:56,000 there in this lecture we're going to 7569 05:40:53,119 --> 05:40:59,360 learn about logical operators these 7570 05:40:56,000 --> 05:41:01,279 operators will work on boolean operands 7571 05:40:59,360 --> 05:41:03,760 we're going to look at end first to 7572 05:41:01,279 --> 05:41:06,637 really make sense of how they work here 7573 05:41:03,759 --> 05:41:09,039 is a simple table we can use to try out 7574 05:41:06,637 --> 05:41:12,079 the end operation it is going to be 7575 05:41:09,040 --> 05:41:14,798 working on two operands a and b 7576 05:41:12,080 --> 05:41:18,000 and in the third column here we're going 7577 05:41:14,797 --> 05:41:21,599 to have the result so if a is false and 7578 05:41:21,599 --> 05:41:27,279 the result is going to be false if a is 7579 05:41:24,319 --> 05:41:31,119 false and b is true the result is going 7580 05:41:27,279 --> 05:41:34,159 to be false if a is true and b is false 7581 05:41:31,119 --> 05:41:37,119 the result is going to be false if a is 7582 05:41:34,159 --> 05:41:39,599 true and b is true the result is going 7583 05:41:37,119 --> 05:41:42,797 to be true the way you can think about 7584 05:41:42,797 --> 05:41:49,439 if any of the operands is false the 7585 05:41:47,040 --> 05:41:52,000 result is going to be false and if you 7586 05:41:49,439 --> 05:41:54,399 really use this way of understanding 7587 05:41:52,000 --> 05:41:56,400 this it is going to be really easy and 7588 05:41:54,400 --> 05:41:58,638 the first line here we have a false so 7589 05:41:56,400 --> 05:42:00,319 the result is going to be false and the 7590 05:41:58,637 --> 05:42:02,718 second line we have a force it's going 7591 05:42:00,319 --> 05:42:04,957 to be force in the third line we have a 7592 05:42:02,718 --> 05:42:08,000 false the result is going to be false 7593 05:42:04,957 --> 05:42:10,637 but in the line number four here we 7594 05:42:08,000 --> 05:42:13,200 don't have a false a is true and b is 7595 05:42:10,637 --> 05:42:16,159 true so the result is going to be true 7596 05:42:16,159 --> 05:42:24,079 and it is going to evaluate to true 7597 05:42:20,400 --> 05:42:26,400 if any of the operands is true let's 7598 05:42:24,080 --> 05:42:29,440 look at the first line here a is false 7599 05:42:26,400 --> 05:42:31,600 and b is false so we don't have a true 7600 05:42:29,439 --> 05:42:33,439 the result is going to be false on the 7601 05:42:31,599 --> 05:42:35,919 second line we have a true the research 7602 05:42:33,439 --> 05:42:37,840 is going to be true on the third line we 7603 05:42:35,919 --> 05:42:41,119 have a true the research is going to be 7604 05:42:37,840 --> 05:42:43,599 true on line number four we have a true 7605 05:42:41,119 --> 05:42:45,919 the result is going to be true and you 7606 05:42:43,599 --> 05:42:49,039 don't really have to memorize these 7607 05:42:45,919 --> 05:42:51,199 tables if you use these operators long 7608 05:42:49,040 --> 05:42:53,760 enough these things are going to be 7609 05:42:51,200 --> 05:42:56,479 drilled in your memory if you have any 7610 05:42:53,759 --> 05:42:58,637 problem with these operators just look 7611 05:42:56,479 --> 05:43:00,797 up tables like this and you can find 7612 05:42:58,637 --> 05:43:03,680 whatever it is you need without really 7613 05:43:00,797 --> 05:43:06,319 memorizing these things another operator 7614 05:43:03,680 --> 05:43:09,360 is the not operator which is going to 7615 05:43:06,319 --> 05:43:11,279 negate whatever you give it so if a is 7616 05:43:09,360 --> 05:43:14,319 true and we negate that we're going to 7617 05:43:11,279 --> 05:43:16,319 get a false if a is false and when you 7618 05:43:14,319 --> 05:43:19,439 get to that we're going to get it true 7619 05:43:16,319 --> 05:43:21,680 and we use this exfoliation mark to say 7620 05:43:19,439 --> 05:43:25,759 that we are negating something if you go 7621 05:43:21,680 --> 05:43:29,040 back to or it is denoted by these two 7622 05:43:25,759 --> 05:43:31,919 vertical bars and the end operator is 7623 05:43:29,040 --> 05:43:34,400 denoted by these two ampersand 7624 05:43:31,919 --> 05:43:36,957 symbols here okay now that we know about 7625 05:43:34,400 --> 05:43:39,440 these logical tables for these logical 7626 05:43:36,957 --> 05:43:41,359 operators we can actually try to use 7627 05:43:41,360 --> 05:43:45,520 here we have three boolean variables we 7628 05:43:45,520 --> 05:43:51,520 a is true b is false c is true we can 7629 05:43:49,599 --> 05:43:54,079 print them out and you see that we are 7630 05:43:51,520 --> 05:43:57,119 setting up sddc out to give us the 7631 05:43:54,080 --> 05:43:59,360 results in the format of true and false 7632 05:43:59,360 --> 05:44:04,878 and down here you see that we are trying 7633 05:44:01,680 --> 05:44:08,957 to use the and logical operator 7634 05:44:04,878 --> 05:44:11,440 down here you have a and b a and c and 7635 05:44:08,957 --> 05:44:13,680 we are doing this on three operators and 7636 05:44:11,439 --> 05:44:17,359 chaining these things up so we are 7637 05:44:13,680 --> 05:44:19,520 saying a and b and c and let's try and 7638 05:44:17,360 --> 05:44:22,080 do this in our mind to see that we 7639 05:44:19,520 --> 05:44:25,040 really understand this we have seen that 7640 05:44:22,080 --> 05:44:27,920 the end operator evaluates to true 7641 05:44:25,040 --> 05:44:30,718 when all operands are true another way 7642 05:44:27,919 --> 05:44:33,279 to say this is the end operator 7643 05:44:30,718 --> 05:44:36,000 evaluates to false if any of the 7644 05:44:33,279 --> 05:44:39,360 operands is false so we're going to end 7645 05:44:36,000 --> 05:44:41,599 a and b a is true b is false so the 7646 05:44:39,360 --> 05:44:44,558 result is going to be false because we 7647 05:44:41,599 --> 05:44:47,439 have a false in here a and c is going to 7648 05:44:44,558 --> 05:44:50,878 evaluate to true because a and c are 7649 05:44:47,439 --> 05:44:53,840 true a and b and c is going to evaluate 7650 05:44:50,878 --> 05:44:57,040 to false because we have a false in here 7651 05:44:53,840 --> 05:44:59,759 even if you have 1 000 tools and you end 7652 05:44:57,040 --> 05:45:03,040 that with one false that's going to be 7653 05:44:59,759 --> 05:45:05,679 dragged down to evaluate to false that's 7654 05:45:03,040 --> 05:45:08,878 what we are seeing here we can also do 7655 05:45:05,680 --> 05:45:11,840 all operations on these things and 7656 05:45:08,878 --> 05:45:13,520 this is going to give you a true if any 7657 05:45:13,520 --> 05:45:19,119 is true we know that a is true and b is 7658 05:45:16,957 --> 05:45:21,919 false so this is going to evaluate to 7659 05:45:19,119 --> 05:45:24,000 true a and c is also going to evaluate 7660 05:45:24,000 --> 05:45:29,759 a and b and c is going to evaluate the 7661 05:45:29,759 --> 05:45:34,000 so let's go back and see this these are 7662 05:45:31,840 --> 05:45:36,878 the values we're going to be using here 7663 05:45:34,000 --> 05:45:38,718 we can also try out the nut operator and 7664 05:45:36,878 --> 05:45:41,200 it is going to negate whatever we gave 7665 05:45:38,718 --> 05:45:43,760 it so if a is true we're going to get a 7666 05:45:41,200 --> 05:45:46,400 false b is false we're going to get it 7667 05:45:43,759 --> 05:45:48,239 true c is true we're going to get a 7668 05:45:46,400 --> 05:45:51,840 false another thing you can do is 7669 05:45:48,240 --> 05:45:54,400 combine this logical operators so we can 7670 05:45:51,840 --> 05:45:55,680 do something like a and b and all of 7671 05:45:55,680 --> 05:46:00,479 and you should notice that we are 7672 05:45:57,840 --> 05:46:02,718 wrapping our expressions here in 7673 05:46:00,479 --> 05:46:04,957 parenthesis so that we don't have the 7674 05:46:02,718 --> 05:46:07,360 same problem we experienced and 7675 05:46:04,957 --> 05:46:09,279 explained in the last lecture and one 7676 05:46:07,360 --> 05:46:12,718 thing you should know is that for the 7677 05:46:09,279 --> 05:46:15,199 compiler to evaluate these things we are 7678 05:46:12,718 --> 05:46:18,080 going to follow the rules in our table 7679 05:46:15,200 --> 05:46:20,080 for precedence and associativity but we 7680 05:46:18,080 --> 05:46:23,680 used parentheses to really make this 7681 05:46:20,080 --> 05:46:24,718 easy to follow so we are going to end a 7682 05:46:24,718 --> 05:46:29,760 and the result of that is going to be 7683 05:46:26,797 --> 05:46:31,279 ored with c and you can try and do this 7684 05:46:29,759 --> 05:46:33,359 in your mind and you're going to figure 7685 05:46:31,279 --> 05:46:35,119 this out but we're also going to go to 7686 05:46:33,360 --> 05:46:37,680 visual studio code in a minute and we're 7687 05:46:35,119 --> 05:46:40,000 going to see all these things in action 7688 05:46:37,680 --> 05:46:43,040 the last thing you can do is combine 7689 05:46:40,000 --> 05:46:46,080 logical and relational operators into 7690 05:46:43,040 --> 05:46:48,878 expressions for example here we have d e 7691 05:46:46,080 --> 05:46:51,440 and f they are integers and we can print 7692 05:46:48,878 --> 05:46:53,040 them out like we are doing here but we 7693 05:46:55,200 --> 05:47:00,958 and we're going to end the result of 7694 05:47:00,957 --> 05:47:07,840 and we know that these relational 7695 05:47:03,919 --> 05:47:10,000 operators evaluate to booleans so we can 7696 05:47:07,840 --> 05:47:12,718 use these booleans with logical 7697 05:47:10,000 --> 05:47:15,360 operators like we have been doing all 7698 05:47:12,718 --> 05:47:17,760 along in this lecture so for example 7699 05:47:15,360 --> 05:47:20,558 let's evaluate the first one here in 7700 05:47:17,759 --> 05:47:22,319 mind so that you really see what this is 7701 05:47:22,319 --> 05:47:29,599 d is 45 e is 20 so d greater than e is 7702 05:47:31,360 --> 05:47:35,760 that's also going to evaluate to true so 7703 05:47:35,759 --> 05:47:39,759 that's going to evaluate to true and 7704 05:47:41,439 --> 05:47:46,000 we are going to go to visual studio code 7705 05:47:43,599 --> 05:47:48,079 and actually play with this thanks 7706 05:47:46,000 --> 05:47:50,080 okay here we are in our working 7707 05:47:48,080 --> 05:47:52,638 directory we're going to jump into our 7708 05:47:50,080 --> 05:47:55,840 template project and copy the files we 7709 05:47:52,637 --> 05:47:58,557 need and we're going to put them in our 7710 05:47:55,840 --> 05:48:00,558 lecture on logical operators we're going 7711 05:48:00,558 --> 05:48:04,558 we're going to open up this folder in 7712 05:48:02,637 --> 05:48:06,797 visual studio code and it is going to 7713 05:48:04,558 --> 05:48:08,958 show up here in visual studio code we're 7714 05:48:06,797 --> 05:48:10,957 going to get rid of what we don't need 7715 05:48:08,957 --> 05:48:14,079 and we are going to put in the code to 7716 05:48:10,957 --> 05:48:18,797 play with these things we have three 7717 05:48:14,080 --> 05:48:20,080 boolean variables a is true b is false c 7718 05:48:20,080 --> 05:48:23,440 and we can print them out and we're 7719 05:48:21,680 --> 05:48:26,718 going to see their values and you see 7720 05:48:23,439 --> 05:48:29,599 the setup here so that sddc out is going 7721 05:48:26,718 --> 05:48:33,680 to show thanks in the format of true and 7722 05:48:29,599 --> 05:48:36,957 false and down here we are ending a few 7723 05:48:33,680 --> 05:48:39,920 values we are using the and operator and 7724 05:48:36,957 --> 05:48:41,039 it is denoted by these two ampersand 7725 05:48:41,759 --> 05:48:48,877 a is true and b is false and c is true 7726 05:48:48,878 --> 05:48:53,680 to false because we have a false in here 7727 05:48:51,599 --> 05:48:56,479 and again if this doesn't make sense 7728 05:48:53,680 --> 05:48:58,718 please do consult the tables for all 7729 05:48:56,479 --> 05:49:00,399 these logical operators and all these 7730 05:48:58,718 --> 05:49:02,718 things are going to make sense this is 7731 05:49:00,400 --> 05:49:06,718 going to evaluate to false because we 7732 05:49:02,718 --> 05:49:08,958 have a false in here and a and c we have 7733 05:49:06,718 --> 05:49:11,440 true and true so this is going to 7734 05:49:11,439 --> 05:49:16,797 a and b and c this is going to evaluate 7735 05:49:14,558 --> 05:49:19,760 to false because we have a false in here 7736 05:49:16,797 --> 05:49:23,199 remember as soon as you have a false in 7737 05:49:19,759 --> 05:49:26,239 your and logical operation you're going 7738 05:49:23,200 --> 05:49:28,637 to get a false in your result so we're 7739 05:49:26,240 --> 05:49:30,878 going to have a false here we can try 7740 05:49:28,637 --> 05:49:33,200 and compile this and see if this is 7741 05:49:30,878 --> 05:49:35,119 actually what we expect we're going to 7742 05:49:33,200 --> 05:49:37,680 bring up a terminal we're going to use 7743 05:49:35,119 --> 05:49:39,599 here and we're going to run the tasks to 7744 05:49:39,599 --> 05:49:46,557 and if we run our rooster program we're 7745 05:49:43,040 --> 05:49:48,479 going to see that a and b is false 7746 05:49:48,479 --> 05:49:54,159 a and c is true this is what we expect 7747 05:49:54,159 --> 05:49:58,878 is false and this is what we expect 7748 05:49:56,957 --> 05:50:02,000 this is how you can really play with 7749 05:49:58,878 --> 05:50:04,718 your and logical operator if you want 7750 05:50:02,000 --> 05:50:07,680 you can change things up to really 7751 05:50:04,718 --> 05:50:09,200 try and see if they live up to your 7752 05:50:09,200 --> 05:50:15,520 now we have changed a to false let's try 7753 05:50:12,240 --> 05:50:18,480 and build again we build with gcc 7754 05:50:15,520 --> 05:50:20,878 and we're going to run our program 7755 05:50:18,479 --> 05:50:23,599 and we're going to get a and b is false 7756 05:50:20,878 --> 05:50:27,119 because they are all false a and b are 7757 05:50:23,599 --> 05:50:28,079 false so false and false evaluates to 7758 05:50:28,080 --> 05:50:34,638 a and c is false because a is false so 7759 05:50:31,840 --> 05:50:37,200 we're going to get a false as the result 7760 05:50:34,637 --> 05:50:40,399 a and b and c is also going to evaluate 7761 05:50:37,200 --> 05:50:42,718 to false okay we have this tension here 7762 05:50:40,400 --> 05:50:45,440 let's bring this back to what it was 7763 05:50:42,718 --> 05:50:48,000 before so a is going to be true b is 7764 05:50:45,439 --> 05:50:50,479 going to be false c is going to be true 7765 05:50:48,000 --> 05:50:53,360 and we're going to try and play with the 7766 05:50:50,479 --> 05:50:57,200 or operator we are going to go down and 7767 05:50:53,360 --> 05:50:59,440 put in code to actually play with this 7768 05:50:57,200 --> 05:51:01,840 and it is really nothing special we are 7769 05:50:59,439 --> 05:51:04,319 just using the or operator here which is 7770 05:51:04,319 --> 05:51:08,239 symbols and or is going to evaluate to 7771 05:51:08,240 --> 05:51:14,159 when at least one of the operands is 7772 05:51:11,360 --> 05:51:17,760 true okay so we have our values here a 7773 05:51:14,159 --> 05:51:20,319 is true b is false c is true a or b is 7774 05:51:17,759 --> 05:51:21,279 going to evaluate to true because a is 7775 05:51:21,279 --> 05:51:25,520 a or c is going to evaluate to true 7776 05:51:25,520 --> 05:51:32,240 a or b or c is going to evaluate to true 7777 05:51:29,759 --> 05:51:34,319 because one of these things is true and 7778 05:51:34,319 --> 05:51:38,558 we can try and run this we're going to 7779 05:51:38,558 --> 05:51:46,159 with gcc and if we run this 7780 05:51:43,439 --> 05:51:48,079 we're going to go down here and run 7781 05:51:48,080 --> 05:51:52,400 and you're going to see that all these 7782 05:51:49,840 --> 05:51:55,520 things are true here because one of 7783 05:51:52,400 --> 05:51:59,280 these things is shown and as long as you 7784 05:51:55,520 --> 05:52:02,479 have one true operand your or operation 7785 05:51:59,279 --> 05:52:04,557 is going to evaluate to true and again 7786 05:52:02,479 --> 05:52:07,200 this is following the precedence and 7787 05:52:04,558 --> 05:52:10,159 associativity rules we have if we go 7788 05:52:07,200 --> 05:52:13,280 back to our table in our browser we're 7789 05:52:10,159 --> 05:52:14,319 going to try and look at logical or 7790 05:52:14,319 --> 05:52:19,439 left associative so we're going to try 7791 05:52:16,637 --> 05:52:22,957 and evaluate this from left to right so 7792 05:52:19,439 --> 05:52:26,159 we're going to or a and b and the result 7793 05:52:22,957 --> 05:52:29,279 of this is going to be ord with c we 7794 05:52:26,159 --> 05:52:32,479 have also seen the not operator which is 7795 05:52:29,279 --> 05:52:34,878 denoted by the exclamation mark here and 7796 05:52:32,479 --> 05:52:37,360 what it is going to do is basically 7797 05:52:34,878 --> 05:52:39,440 flip off or negate whatever it is you 7798 05:52:37,360 --> 05:52:42,240 have if you have it true in your 7799 05:52:39,439 --> 05:52:44,319 variable the result is going to be false 7800 05:52:42,240 --> 05:52:45,440 if you have uniforms the result is going 7801 05:52:45,439 --> 05:52:51,680 and not a is going to be false because a 7802 05:52:49,040 --> 05:52:53,440 is true not b is going to be true 7803 05:52:56,319 --> 05:53:01,919 is going to be false because c is true 7804 05:52:59,279 --> 05:53:04,159 as we can see here so let's run the 7805 05:53:01,919 --> 05:53:05,439 application and see if this is what we 7806 05:53:05,439 --> 05:53:11,359 we're going to run and build with gcc 7807 05:53:08,718 --> 05:53:14,878 and we're going to run rooster and we're 7808 05:53:11,360 --> 05:53:18,718 going to see that note a is false not b 7809 05:53:14,878 --> 05:53:21,119 is true and not c is false and this is 7810 05:53:18,718 --> 05:53:24,080 what we expect we have also seen that we 7811 05:53:21,119 --> 05:53:26,399 can do combinations of these operators 7812 05:53:24,080 --> 05:53:28,558 so for example we can look at the 7813 05:53:29,840 --> 05:53:36,159 take a and b this is going to evaluate 7814 05:53:36,159 --> 05:53:40,479 we're going to negate this and we're 7815 05:53:38,319 --> 05:53:42,159 going to get a true and we're going to 7816 05:53:44,000 --> 05:53:48,878 we know that the result of ordering this 7817 05:53:46,400 --> 05:53:52,000 with anything is going to be true so we 7818 05:53:48,878 --> 05:53:55,040 expect this expression here to evaluate 7819 05:53:55,040 --> 05:53:59,680 we're going to see that this is going to 7820 05:53:57,200 --> 05:54:00,958 evaluate to true and it is what we 7821 05:54:00,957 --> 05:54:05,199 the last thing we want to see in this 7822 05:54:02,558 --> 05:54:08,000 lecture is that we can combine 7823 05:54:05,200 --> 05:54:10,798 these logical operators with the 7824 05:54:08,000 --> 05:54:13,439 relational operators we have seen in the 7825 05:54:10,797 --> 05:54:16,878 last lecture we are going to put in a 7826 05:54:13,439 --> 05:54:19,279 piece of code to play with this this is 7827 05:54:16,878 --> 05:54:22,718 lengthy but you can play with this and 7828 05:54:19,279 --> 05:54:24,637 see what this is going to evaluate to to 7829 05:54:22,718 --> 05:54:28,558 start you off we are going to do the 7830 05:54:24,637 --> 05:54:31,200 first one together so it does test if d 7831 05:54:28,558 --> 05:54:33,920 is greater than e and if you look here 7832 05:54:31,200 --> 05:54:36,000 45 is greater than 20 so this is going 7833 05:54:36,000 --> 05:54:40,718 and we're going to enter that with d 7834 05:54:40,718 --> 05:54:46,558 the result of this is going to be true 7835 05:54:43,840 --> 05:54:48,957 so we're going to be ending a true and a 7836 05:54:46,558 --> 05:54:51,360 true and the result of this is going to 7837 05:54:48,957 --> 05:54:53,439 be a true and again you notice that we 7838 05:54:51,360 --> 05:54:55,119 are wrapping this in parenthesis to 7839 05:54:55,119 --> 05:55:00,479 with the compiler being confused with 7840 05:54:58,558 --> 05:55:03,040 what we are trying to print this is 7841 05:55:00,479 --> 05:55:05,439 going to get rid of the otherwise ugly 7842 05:55:03,040 --> 05:55:07,840 problems we might have to deal with if 7843 05:55:05,439 --> 05:55:10,319 we didn't wrap this into parentheses 7844 05:55:07,840 --> 05:55:13,040 like this so we expect to see a true 7845 05:55:13,040 --> 05:55:19,040 and run the task to build with gcc 7846 05:55:16,319 --> 05:55:21,680 and we're going to run rooster and if we 7847 05:55:19,040 --> 05:55:24,159 look at the first thing here this is 7848 05:55:21,680 --> 05:55:26,558 going to be true and you can try and do 7849 05:55:24,159 --> 05:55:29,119 the other ones in your mind as an 7850 05:55:26,558 --> 05:55:31,680 exercise and if you have a problem don't 7851 05:55:29,119 --> 05:55:34,079 hesitate to ask me i am going to do the 7852 05:55:34,080 --> 05:55:37,920 this is really all we set out to do in 7853 05:55:35,759 --> 05:55:41,039 this lecture i hope you have a better 7854 05:55:37,919 --> 05:55:43,359 understanding of these logical operators 7855 05:55:41,040 --> 05:55:46,319 again you don't have to really memorize 7856 05:55:43,360 --> 05:55:48,558 them all you have to do is consult the 7857 05:55:46,319 --> 05:55:51,279 tables and you're going to basically 7858 05:55:48,558 --> 05:55:53,920 know the result of your two operands 7859 05:55:51,279 --> 05:55:57,759 depending on the values you have inside 7860 05:55:53,919 --> 05:56:00,000 for and or and the not operator we are 7861 05:55:57,759 --> 05:56:01,519 going to stop here in this lecture in 7862 05:56:00,000 --> 05:56:05,200 the next one we're going to see how we 7863 05:56:01,520 --> 05:56:08,080 can format our output to make it really 7864 05:56:05,200 --> 05:56:10,319 look better go ahead and finish up here 7865 05:56:08,080 --> 05:56:13,360 and meet me there in this lecture we're 7866 05:56:10,319 --> 05:56:15,759 going to look at output formatting and c 7867 05:56:13,360 --> 05:56:18,080 plus and this is the way we can format 7868 05:56:15,759 --> 05:56:21,519 stuff we send it to the terminal through 7869 05:56:18,080 --> 05:56:24,080 sddc out and make them show up better as 7870 05:56:21,520 --> 05:56:27,439 an example let's say we have a table 7871 05:56:27,439 --> 05:56:32,637 and you see that they are cramped and 7872 05:56:30,240 --> 05:56:35,600 through output formatting we can take 7873 05:56:32,637 --> 05:56:38,000 this and turn this into this 7874 05:56:35,599 --> 05:56:38,877 and hopefully we agree that this is 7875 05:56:38,878 --> 05:56:43,680 there are a set of tools we can use in c 7876 05:56:41,360 --> 05:56:46,558 plus plus and those we're going to be 7877 05:56:43,680 --> 05:56:49,520 using in this lecture are going to be 7878 05:56:46,558 --> 05:56:53,040 coming from these two libraries ios and 7879 05:56:49,520 --> 05:56:56,637 io manip and there are a lot of things 7880 05:56:53,040 --> 05:57:00,400 in these two libraries here is a a set 7881 05:56:56,637 --> 05:57:01,840 of cards that show a lot of them we are 7882 05:57:00,400 --> 05:57:03,840 going to play with a lot of these in 7883 05:57:01,840 --> 05:57:06,400 this lecture so you don't have to worry 7884 05:57:06,400 --> 05:57:11,200 right now before we start looking at 7885 05:57:08,797 --> 05:57:13,439 them i want to bring to your attention 7886 05:57:11,200 --> 05:57:15,680 that there is a piece of documentation 7887 05:57:13,439 --> 05:57:17,479 on these things the link is here the 7888 05:57:19,040 --> 05:57:23,200 and you can open this up and look at 7889 05:57:21,599 --> 05:57:25,680 some of the things we're going to talk 7890 05:57:23,200 --> 05:57:29,440 about in this lecture okay the first one 7891 05:57:25,680 --> 05:57:31,840 we're going to look at is stdendl 7892 05:57:29,439 --> 05:57:34,399 this is not new we have been using it 7893 05:57:31,840 --> 05:57:38,000 all day long to print out a new line 7894 05:57:34,400 --> 05:57:40,718 character so that our htdcl statements 7895 05:57:38,000 --> 05:57:43,279 print things on different lines and if 7896 05:57:40,718 --> 05:57:46,240 we don't have this manipulator you see 7897 05:57:43,279 --> 05:57:48,079 here on the top we have hello and world 7898 05:57:46,240 --> 05:57:50,480 and if we print them out they are going 7899 05:57:48,080 --> 05:57:53,600 to show up on one line because we are 7900 05:57:50,479 --> 05:57:56,637 not separating the lines with std e and 7901 05:57:53,599 --> 05:57:59,759 dl down below here we have another way 7902 05:57:56,637 --> 05:58:02,718 we can do this we can post a backslash n 7903 05:57:59,759 --> 05:58:06,319 after our text and that's going to add a 7904 05:58:02,718 --> 05:58:10,958 new line character just like std endl 7905 05:58:06,319 --> 05:58:13,599 does okay stdndl and slash n in here 7906 05:58:10,957 --> 05:58:15,759 seemingly do the same thing 7907 05:58:13,599 --> 05:58:17,599 but they are different but we don't have 7908 05:58:15,759 --> 05:58:20,399 enough tools to really understand the 7909 05:58:17,599 --> 05:58:23,359 differences right now for now just think 7910 05:58:20,400 --> 05:58:26,319 of them as two things that do the same 7911 05:58:23,360 --> 05:58:28,400 thing they print out a new character 7912 05:58:26,319 --> 05:58:31,200 that allows your text to show up on 7913 05:58:28,400 --> 05:58:34,400 different lines like we have hello and 7914 05:58:31,200 --> 05:58:36,558 world here okay stdndl is the first 7915 05:58:34,400 --> 05:58:38,240 manipulator we're going to play with in 7916 05:58:36,558 --> 05:58:40,479 this lecture the next one we're going to 7917 05:58:40,479 --> 05:58:46,159 and this is going to send whatever is in 7918 05:58:43,040 --> 05:58:48,878 the output buffer to the terminal 7919 05:58:46,159 --> 05:58:51,439 and what the heck is an output buffer 7920 05:58:48,878 --> 05:58:54,240 let's look at an example suppose we have 7921 05:58:51,439 --> 05:58:56,797 sddc out here and we want to print on 7922 05:58:54,240 --> 05:59:00,400 this terminal and the message we want to 7923 05:58:56,797 --> 05:59:03,360 print is hello world how are you so what 7924 05:59:00,400 --> 05:59:06,000 is going to happen if the sddc out 7925 05:59:03,360 --> 05:59:07,760 statement is executed things are not 7926 05:59:06,000 --> 05:59:10,400 going to be directly sent to the 7927 05:59:07,759 --> 05:59:12,717 terminal they are going to go into an 7928 05:59:10,400 --> 05:59:15,120 intermediary buffer you can think of 7929 05:59:12,718 --> 05:59:18,319 this as some kind of storage in which 7930 05:59:15,119 --> 05:59:20,000 data goes before it goes to the terminal 7931 05:59:18,319 --> 05:59:21,919 and this is what's going to happen we 7932 05:59:20,000 --> 05:59:23,759 print out hello hello is not going to go 7933 05:59:25,759 --> 05:59:30,319 it's not going to go to the terminal we 7934 05:59:30,319 --> 05:59:34,558 it's not going to go to the terminal and 7935 05:59:34,558 --> 05:59:39,920 the data is going to be sent to the 7936 05:59:36,637 --> 05:59:42,878 terminal in one go and this may come in 7937 05:59:39,919 --> 05:59:45,599 handy in some situations so if you want 7938 05:59:42,878 --> 05:59:48,400 the data to go to the terminal directly 7939 05:59:45,599 --> 05:59:52,000 you're going to add an sdd flash 7940 05:59:48,400 --> 05:59:54,159 after your hddc out statement and we are 7941 05:59:52,000 --> 05:59:55,759 going to play with this in a minute in 7942 05:59:54,159 --> 05:59:58,000 visual studio code and you're going to 7943 05:59:55,759 --> 06:00:01,519 see how this works another manipulator 7944 05:59:58,000 --> 06:00:05,680 we have which is really cool is set with 7945 06:00:01,520 --> 06:00:09,600 and it is std set w here and what it 7946 06:00:05,680 --> 06:00:12,479 does it specifies a width for whatever 7947 06:00:09,599 --> 06:00:15,919 text you want to print here we have two 7948 06:00:12,479 --> 06:00:18,319 examples the first one is our table that 7949 06:00:15,919 --> 06:00:20,319 is not formatted well and you see we 7950 06:00:18,319 --> 06:00:23,680 just print out the data and it is going 7951 06:00:20,319 --> 06:00:27,840 to show up like we see here on the right 7952 06:00:23,680 --> 06:00:30,479 we can use std set with to set the width 7953 06:00:27,840 --> 06:00:32,957 of the text here and for example the 7954 06:00:30,479 --> 06:00:36,479 last name here is going to show up in a 7955 06:00:32,957 --> 06:00:38,717 field which is 10 characters white this 7956 06:00:36,479 --> 06:00:41,919 is what we mean here and we do the same 7957 06:00:38,718 --> 06:00:43,040 for the first name and the age and if we 7958 06:00:43,040 --> 06:00:48,558 data is going to show up nicely as we 7959 06:00:45,439 --> 06:00:51,119 see here and again i hope we agreed that 7960 06:00:48,558 --> 06:00:53,760 what we have down below here is much 7961 06:00:51,119 --> 06:00:56,878 better than what we had here 7962 06:00:53,759 --> 06:00:59,119 which was not formatted with set width 7963 06:00:56,878 --> 06:01:01,119 and this is how you use this manipulator 7964 06:00:59,119 --> 06:01:03,200 here we can also use what we call 7965 06:01:01,119 --> 06:01:06,079 justification to control whether our 7966 06:01:03,200 --> 06:01:06,958 data shows up on the right or on the 7967 06:01:06,957 --> 06:01:11,840 in the width that is allocated to it for 7968 06:01:11,840 --> 06:01:16,957 justification is sdd right so the data 7969 06:01:14,878 --> 06:01:19,840 is going to show up to the right in its 7970 06:01:16,957 --> 06:01:23,039 respective cells we can also use what we 7971 06:01:19,840 --> 06:01:25,759 call internal justification by which if 7972 06:01:23,040 --> 06:01:28,240 we are showing a negative number the 7973 06:01:25,759 --> 06:01:30,637 sign is going to be left justified so it 7974 06:01:28,240 --> 06:01:33,200 is going to show up on the left but the 7975 06:01:30,637 --> 06:01:35,200 data is going to be right justified and 7976 06:01:33,200 --> 06:01:37,600 this may come in handy if you want to 7977 06:01:37,599 --> 06:01:44,000 in your terminal we can also use std set 7978 06:01:41,439 --> 06:01:47,039 field to specify the field character in 7979 06:01:44,000 --> 06:01:49,680 our empty spaces for example here we set 7980 06:01:47,040 --> 06:01:52,878 the dash as a field character and the 7981 06:01:49,680 --> 06:01:56,240 result is what you see down below here 7982 06:01:52,878 --> 06:01:58,319 we have seen std ball alpha and its 7983 06:01:58,319 --> 06:02:05,919 stdc out to show boolean output in the 7984 06:02:02,479 --> 06:02:08,479 form of true and false if you don't put 7985 06:02:05,919 --> 06:02:11,439 this manipulator in you're going to see 7986 06:02:08,479 --> 06:02:12,878 those pieces of data as ones and zeros 7987 06:02:11,439 --> 06:02:15,439 so for example here we have two 7988 06:02:12,878 --> 06:02:17,200 variables condition and other condition 7989 06:02:15,439 --> 06:02:19,840 if we print them out they are going to 7990 06:02:17,200 --> 06:02:22,240 show up as one and zero we are going to 7991 06:02:19,840 --> 06:02:24,957 set our manipulator here so the data is 7992 06:02:22,240 --> 06:02:28,240 going to show up as true and false we 7993 06:02:24,957 --> 06:02:31,599 can disable this manipulator here with 7994 06:02:28,240 --> 06:02:34,638 std noble alpha and the data is going to 7995 06:02:31,599 --> 06:02:37,359 go back to be shown in the form of one 7996 06:02:34,637 --> 06:02:39,520 and zero this is what this manipulator 7997 06:02:37,360 --> 06:02:42,319 does we also have one to show the 7998 06:02:39,520 --> 06:02:43,200 positive sign for positive numbers 7999 06:02:43,200 --> 06:02:48,080 if we don't enable it the data here is 8000 06:02:45,279 --> 06:02:50,878 going to show up like this 34 and minus 8001 06:02:48,080 --> 06:02:53,760 45 you see that the minus sign is going 8002 06:02:50,878 --> 06:02:56,958 to be shown regardless of the setting 8003 06:02:53,759 --> 06:02:59,759 you have for this manipulator but if we 8004 06:02:59,759 --> 06:03:04,319 we're going to show the positive number 8005 06:03:02,159 --> 06:03:06,479 and this may make sense for whatever 8006 06:03:04,319 --> 06:03:09,520 application you might be doing 8007 06:03:06,479 --> 06:03:12,319 we can disable this with std no show 8008 06:03:09,520 --> 06:03:15,520 posts and after we do this the data is 8009 06:03:12,319 --> 06:03:17,840 going to show up in its default format 8010 06:03:15,520 --> 06:03:20,797 that we have seen before this is how you 8011 06:03:17,840 --> 06:03:23,119 use std show posts we also have 8012 06:03:23,119 --> 06:03:28,797 the base in which the data is shown and 8013 06:03:26,080 --> 06:03:31,760 we can show the data either in a decimal 8014 06:03:28,797 --> 06:03:34,079 in octo or in hexodecimal if you look at 8015 06:03:31,759 --> 06:03:35,919 the data here we have an integer which 8016 06:03:34,080 --> 06:03:38,718 is positive we have an integer which is 8017 06:03:35,919 --> 06:03:41,359 negative and we have a double variable 8018 06:03:38,718 --> 06:03:43,040 here and we have some data n for us to 8019 06:03:41,360 --> 06:03:45,840 play with we're going to show our 8020 06:03:43,040 --> 06:03:48,558 positive integer in these three bases 8021 06:03:48,558 --> 06:03:53,520 and the result is what you see here and 8022 06:03:51,200 --> 06:03:54,479 this should be no surprise you already 8023 06:03:54,479 --> 06:03:59,840 these number systems and how they work 8024 06:03:57,680 --> 06:04:02,159 we can take the negative number and also 8025 06:03:59,840 --> 06:04:05,279 show it in different bases decimal 8026 06:04:02,159 --> 06:04:08,400 hexadecimal and octo and it is what you 8027 06:04:05,279 --> 06:04:10,957 have on the right here in our terminal 8028 06:04:10,957 --> 06:04:17,039 a double or a floating point number in 8029 06:04:14,319 --> 06:04:20,159 these bases the result is not going to 8030 06:04:17,040 --> 06:04:22,958 change because these number systems have 8031 06:04:20,159 --> 06:04:25,360 no effect on floating point numbers if 8032 06:04:22,957 --> 06:04:26,877 you remember we have seen that floating 8033 06:04:26,878 --> 06:04:32,319 are represented in memory following 8034 06:04:29,520 --> 06:04:33,200 another protocol that is really advanced 8035 06:04:33,200 --> 06:04:37,600 a course like this so we are just going 8036 06:04:35,840 --> 06:04:39,840 to use this but we're not going to try 8037 06:04:37,599 --> 06:04:42,479 and understand it because it is out of 8038 06:04:39,840 --> 06:04:45,439 scope for what we are doing here we can 8039 06:04:45,439 --> 06:04:50,877 to show the base of our output 8040 06:04:48,718 --> 06:04:54,637 so here we have a positive number which 8041 06:04:50,878 --> 06:04:57,200 is an integer and we can show it in 8042 06:04:54,637 --> 06:05:00,079 decimal hex and act and we're going to 8043 06:04:57,200 --> 06:05:03,200 see what we see here to the right but we 8044 06:05:00,080 --> 06:05:05,440 can choose to also show the base of 8045 06:05:03,200 --> 06:05:08,479 whatever it is we output if we want to 8046 06:05:05,439 --> 06:05:11,039 specify that the output is in decimal 8047 06:05:08,479 --> 06:05:13,439 we're going to say that here but we can 8048 06:05:11,040 --> 06:05:18,240 also decide to show the base and we 8049 06:05:13,439 --> 06:05:20,239 enable that with std show base like this 8050 06:05:18,240 --> 06:05:22,159 and if we do this we're going to see 8051 06:05:20,240 --> 06:05:25,840 decimal is not going to have anything in 8052 06:05:22,159 --> 06:05:29,279 front hex is going to have a 0x in front 8053 06:05:25,840 --> 06:05:31,680 oct is going to have a zero in front and 8054 06:05:29,279 --> 06:05:32,557 by this it is going to be very easy to 8055 06:05:32,558 --> 06:05:38,159 the base in which your data is displayed 8056 06:05:35,759 --> 06:05:40,159 we can also use std upper case for the 8057 06:05:38,159 --> 06:05:43,119 data we print out to be shown in 8058 06:05:40,159 --> 06:05:44,878 uppercase where it makes sense for 8059 06:05:43,119 --> 06:05:47,520 example here we are trying to print 8060 06:05:44,878 --> 06:05:50,319 numbers in hexadecimal here in the 8061 06:05:47,520 --> 06:05:51,920 middle so if we don't use uppercase 8062 06:05:51,919 --> 06:05:57,759 the number is going to show up like this 8063 06:05:53,840 --> 06:06:00,080 you see a and f are lowercase but if we 8064 06:05:57,759 --> 06:06:02,717 enable uppercase we're going to see this 8065 06:06:00,080 --> 06:06:05,280 in uppercase and sometimes this is going 8066 06:06:02,718 --> 06:06:08,400 to be helpful for whatever application 8067 06:06:05,279 --> 06:06:10,637 you are using the sun we also have a few 8068 06:06:08,400 --> 06:06:13,440 manipulators to control how floating 8069 06:06:10,637 --> 06:06:16,159 point data is shown on the terminal and 8070 06:06:13,439 --> 06:06:19,439 the two of these are std scientific and 8071 06:06:16,159 --> 06:06:20,878 sdd fixed by default stdc out is going 8072 06:06:20,878 --> 06:06:26,558 in scientific format where necessary and 8073 06:06:23,680 --> 06:06:28,240 it is going to use fixed by default so 8074 06:06:31,119 --> 06:06:36,079 and if we just print them out they are 8075 06:06:33,360 --> 06:06:38,319 going to show up in this format here if 8076 06:06:36,080 --> 06:06:40,878 we use fixed we're going to force the 8077 06:06:38,319 --> 06:06:43,119 output to be in fixed format and you're 8078 06:06:40,878 --> 06:06:46,797 going to see that the third number in c 8079 06:06:43,119 --> 06:06:49,840 here is going to show up as 0.0 and a 8080 06:06:46,797 --> 06:06:52,079 few zeros after that because in the 8081 06:06:49,840 --> 06:06:55,439 precision that we have now we can't 8082 06:06:52,080 --> 06:06:57,840 really show a number that is as low as 8083 06:06:55,439 --> 06:07:00,319 we have here in c again we have seen 8084 06:06:57,840 --> 06:07:03,119 this scientific format this is 8085 06:07:03,119 --> 06:07:08,479 multiplied by 10 to the power of minus 8086 06:07:06,080 --> 06:07:10,840 10 and this is a really really low 8087 06:07:10,840 --> 06:07:16,797 0.000 up to 10 and that's when this 8088 06:07:14,479 --> 06:07:19,119 decimal point is going to show so it's a 8089 06:07:16,797 --> 06:07:21,759 number that is really close to zero 8090 06:07:19,119 --> 06:07:24,637 that's why we are showing it like 0.0 8091 06:07:21,759 --> 06:07:27,519 here i hope this makes sense we can also 8092 06:07:24,637 --> 06:07:30,319 use scientific notation if we want that 8093 06:07:27,520 --> 06:07:32,000 and if we force scientific notation 8094 06:07:30,319 --> 06:07:34,878 everything is going to be shown in 8095 06:07:32,000 --> 06:07:37,520 scientific notation another thing that i 8096 06:07:34,878 --> 06:07:40,958 really want you to be aware of is that 8097 06:07:37,520 --> 06:07:41,760 there is no official way to set 8098 06:07:41,759 --> 06:07:48,079 floating point output to its defaults 8099 06:07:45,599 --> 06:07:50,557 and here is a hack i actually learned 8100 06:07:48,080 --> 06:07:52,798 about when i was making this course the 8101 06:07:50,558 --> 06:07:55,680 explanation of this is really out of 8102 06:07:52,797 --> 06:07:57,360 scope for this course but just use this 8103 06:07:57,360 --> 06:08:02,159 the floating point output format to the 8104 06:08:00,479 --> 06:08:04,079 defaults and if you do this you're going 8105 06:08:04,080 --> 06:08:08,558 here with the defaults you're going to 8106 06:08:06,637 --> 06:08:10,957 see that it is the same things the next 8107 06:08:08,558 --> 06:08:13,360 thing we're going to look at is std set 8108 06:08:10,957 --> 06:08:16,159 precision which we have seen before and 8109 06:08:13,360 --> 06:08:18,558 this is going to set the precision 8110 06:08:16,159 --> 06:08:20,957 with which the data is going to show up 8111 06:08:18,558 --> 06:08:23,120 on the terminal if you use a precision 8112 06:08:20,957 --> 06:08:26,000 of 10 that's what you're going to get 8113 06:08:23,119 --> 06:08:29,439 here here you see that we have a double 8114 06:08:26,000 --> 06:08:32,000 number it is a number that has a lot of 8115 06:08:29,439 --> 06:08:33,759 numbers after the decimal point if we 8116 06:08:32,000 --> 06:08:35,520 show it by default it's going to show up 8117 06:08:33,759 --> 06:08:38,079 like this the precision is going to be 8118 06:08:35,520 --> 06:08:41,439 sex we can increase the precision to 10 8119 06:08:38,080 --> 06:08:43,680 to 20 and 50 and it is going to show up 8120 06:08:41,439 --> 06:08:44,557 at whatever precision that you specify 8121 06:08:44,558 --> 06:08:49,520 please know that this precision is going 8122 06:08:46,878 --> 06:08:50,558 to be limited by the type of this a 8123 06:08:50,558 --> 06:08:55,040 if it is a floating point the precision 8124 06:08:52,878 --> 06:08:57,040 is going to be something like 7 we have 8125 06:08:55,040 --> 06:08:59,440 seen that if it is a double the 8126 06:08:57,040 --> 06:09:02,479 precision is going to basically double 8127 06:08:59,439 --> 06:09:04,957 to something like 15 and if you have a 8128 06:09:02,479 --> 06:09:07,279 long double the precision is going to be 8129 06:09:04,957 --> 06:09:10,319 something bigger than double but it 8130 06:09:07,279 --> 06:09:12,957 can't go below that of a double we have 8131 06:09:10,319 --> 06:09:15,040 seen this before we also have show point 8132 06:09:15,040 --> 06:09:21,440 showing the decimal point for floating 8133 06:09:17,599 --> 06:09:23,279 point values you see here we have a 12.0 8134 06:09:21,439 --> 06:09:24,557 this is the most important piece of data 8135 06:09:24,558 --> 06:09:31,280 if we don't do show point this is going 8136 06:09:27,360 --> 06:09:34,558 to show up as 12 in the output here but 8137 06:09:31,279 --> 06:09:36,797 we can use show point to force for the 8138 06:09:34,558 --> 06:09:37,638 decimal point to be shown and you see 8139 06:09:37,637 --> 06:09:43,200 12.00 here is shown another side effect 8140 06:09:43,200 --> 06:09:48,159 show point is going to force the showing 8141 06:09:45,439 --> 06:09:51,359 of trailing zeros and it is what we see 8142 06:09:48,159 --> 06:09:53,520 here in d e and f we have seen a lot of 8143 06:09:51,360 --> 06:09:55,680 manipulators here is a table that is 8144 06:09:53,520 --> 06:09:58,878 going to show where many of these are 8145 06:09:55,680 --> 06:10:01,520 going to be living so if you need to use 8146 06:09:58,878 --> 06:10:05,200 set width this is going to be living 8147 06:10:01,520 --> 06:10:07,760 in the i o manip header and here we have 8148 06:10:05,200 --> 06:10:10,080 a few other ones so if you want to use a 8149 06:10:07,759 --> 06:10:12,319 few of these things these are the 8150 06:10:10,080 --> 06:10:14,878 libraries that you will need to include 8151 06:10:12,319 --> 06:10:17,520 with pound include as we have been 8152 06:10:14,878 --> 06:10:19,920 including i o stream and again the 8153 06:10:17,520 --> 06:10:20,600 documentation for these things is shown 8154 06:10:20,599 --> 06:10:26,000 cppreference.com this is the exact link 8155 06:10:23,520 --> 06:10:26,957 and you can check this out if it helps 8156 06:10:26,957 --> 06:10:30,637 now that we have an idea about these 8157 06:10:28,797 --> 06:10:33,439 things we're going to go in visual 8158 06:10:30,637 --> 06:10:36,399 studio code and actually see these in 8159 06:10:33,439 --> 06:10:37,919 action okay here we are in visual studio 8160 06:10:36,400 --> 06:10:41,120 code the project we're going to be 8161 06:10:37,919 --> 06:10:42,637 working on here is output formatting so 8162 06:10:45,919 --> 06:10:51,279 and put those in our project here 8163 06:10:49,119 --> 06:10:53,439 and we're going to go up again and open 8164 06:10:51,279 --> 06:10:55,199 up this folder in visual studio code 8165 06:10:53,439 --> 06:10:57,520 let's do that we're going to close the 8166 06:10:55,200 --> 06:10:59,680 welcome screen here and we're going to 8167 06:10:57,520 --> 06:11:01,520 remove whatever it is we don't need 8168 06:11:01,520 --> 06:11:05,920 and we are going to put in the first 8169 06:11:03,520 --> 06:11:07,680 code we want to play with and this is 8170 06:11:07,680 --> 06:11:11,360 you don't really need a lot of 8171 06:11:09,200 --> 06:11:13,119 explanations for this because we've used 8172 06:11:13,119 --> 06:11:17,039 if you don't put it out the data is 8173 06:11:14,718 --> 06:11:19,280 going to show up on one line and uh 8174 06:11:17,040 --> 06:11:20,159 you're going to have hello world on one 8175 06:11:20,159 --> 06:11:25,040 if we put stdndl like this we're going 8176 06:11:23,119 --> 06:11:27,039 to have the data show up on different 8177 06:11:25,040 --> 06:11:29,760 lines so we're going to have hello and 8178 06:11:27,040 --> 06:11:33,120 world on different lines and it is 8179 06:11:29,759 --> 06:11:35,919 possible to use a backslash n to achieve 8180 06:11:33,119 --> 06:11:38,557 the same thing but these work very 8181 06:11:35,919 --> 06:11:40,399 differently underground and we don't 8182 06:11:38,558 --> 06:11:43,440 have enough tools to really understand 8183 06:11:40,400 --> 06:11:45,920 the difference so for now just take this 8184 06:11:43,439 --> 06:11:48,479 as the truth and i know that there are 8185 06:11:45,919 --> 06:11:50,877 differences and the most important thing 8186 06:11:48,479 --> 06:11:53,759 is that you recognize these things if 8187 06:11:50,878 --> 06:11:56,718 you see them in code out there we are 8188 06:11:53,759 --> 06:11:59,279 going to bring up our terminum and build 8189 06:11:56,718 --> 06:12:01,040 this with our task that is going to 8190 06:12:01,040 --> 06:12:04,000 you're going to see that the world is 8191 06:12:04,000 --> 06:12:09,840 and that we can run our rooster 8192 06:12:06,637 --> 06:12:12,479 binary to see the output you see that we 8193 06:12:09,840 --> 06:12:14,957 have hello world on one line 8194 06:12:12,479 --> 06:12:17,119 we have hello and world on different 8195 06:12:14,957 --> 06:12:19,557 lines and this is a result of these 8196 06:12:17,119 --> 06:12:22,399 events we have here you see we have 8197 06:12:19,558 --> 06:12:24,638 stdndl this is going to force 8198 06:12:22,400 --> 06:12:26,878 the terminal to go to the next line and 8199 06:12:24,637 --> 06:12:29,200 print world and after that we're going 8200 06:12:26,878 --> 06:12:31,680 to force the terminal to go to the next 8201 06:12:29,200 --> 06:12:34,080 line and that's where the next text is 8202 06:12:31,680 --> 06:12:36,319 going to show up after that we have 8203 06:12:34,080 --> 06:12:39,760 hello and world down here in the 8204 06:12:36,319 --> 06:12:42,400 terminal and it is the output generated 8205 06:12:39,759 --> 06:12:44,479 by the statements that we have in here 8206 06:12:42,400 --> 06:12:47,680 and you can see that it is going to 8207 06:12:44,479 --> 06:12:50,239 basically do the same thing as stdendl 8208 06:12:47,680 --> 06:12:52,878 but again they work very differently 8209 06:12:50,240 --> 06:12:56,718 under the hood okay so this is really 8210 06:12:52,878 --> 06:12:59,520 how std endl works and you get access to 8211 06:12:56,718 --> 06:13:01,520 it by including the i o stream 8212 06:12:59,520 --> 06:13:04,319 library here the next thing we're going 8213 06:13:04,319 --> 06:13:09,759 this is going to flash the output buffer 8214 06:13:07,599 --> 06:13:10,877 to its final destination and that's 8215 06:13:10,878 --> 06:13:16,159 the terminal in this case and if we add 8216 06:13:18,797 --> 06:13:24,479 this is going to be directly sent to the 8217 06:13:22,080 --> 06:13:27,120 terminal and this may come in handy in 8218 06:13:24,479 --> 06:13:30,000 some applications if we run this we're 8219 06:13:27,119 --> 06:13:32,718 not going to really see a difference but 8220 06:13:30,000 --> 06:13:35,119 underground under the hood the data is 8221 06:13:32,718 --> 06:13:37,280 directly sent to the terminal and we had 8222 06:13:35,119 --> 06:13:40,637 the chance to explain this in the slides 8223 06:13:37,279 --> 06:13:43,119 earlier we can work this program and i 8224 06:13:40,637 --> 06:13:45,200 see it running the world is going to go 8225 06:13:43,119 --> 06:13:47,200 through if we run the rooster we're 8226 06:13:45,200 --> 06:13:48,479 going to see that this is a nice message 8227 06:13:48,479 --> 06:13:52,878 to the terminal the next thing we're 8228 06:13:50,637 --> 06:13:54,957 going to look at is set with i am going 8229 06:13:52,878 --> 06:13:57,840 to put in the code here because i don't 8230 06:13:54,957 --> 06:14:00,239 want to type all this and again you can 8231 06:14:00,240 --> 06:14:04,958 in the results section of this lecture 8232 06:14:03,279 --> 06:14:06,878 you don't really have to type all these 8233 06:14:04,957 --> 06:14:09,279 things the most important thing is that 8234 06:14:06,878 --> 06:14:11,840 you really understand how they work okay 8235 06:14:09,279 --> 06:14:14,479 you see here we are going to use set 8236 06:14:11,840 --> 06:14:16,957 with but we haven't really included the 8237 06:14:14,479 --> 06:14:18,637 library that is going to give us access 8238 06:14:16,957 --> 06:14:20,717 to this and if you don't know which 8239 06:14:18,637 --> 06:14:22,718 library you will need to include to get 8240 06:14:20,718 --> 06:14:25,680 access to this well you can check the 8241 06:14:22,718 --> 06:14:28,000 documentation and to get access to the 8242 06:14:25,680 --> 06:14:30,479 documentation you might want to consult 8243 06:14:28,000 --> 06:14:34,718 your favorite search engine i am going 8244 06:14:30,479 --> 06:14:38,558 to go in google here and type in std 8245 06:14:34,718 --> 06:14:40,479 set w and i am going to search and this 8246 06:14:38,558 --> 06:14:42,520 is going to come up with a few entries i 8247 06:14:40,479 --> 06:14:44,797 am going to click on the one from 8248 06:14:42,520 --> 06:14:47,920 cppreference.com because this is 8249 06:14:44,797 --> 06:14:49,840 reliable in my opinion and if we open 8250 06:14:47,919 --> 06:14:52,957 this up we're going to see that to work 8251 06:14:49,840 --> 06:14:55,360 with us you need to include i o my net 8252 06:14:52,957 --> 06:14:57,840 so this is what we need to put in visual 8253 06:14:55,360 --> 06:15:00,637 studio code to get rid of this problem 8254 06:14:57,840 --> 06:15:02,878 let's do that i really wanted you to see 8255 06:15:00,637 --> 06:15:05,520 this process you can go through 8256 06:15:02,878 --> 06:15:07,760 to know what you need to include we're 8257 06:15:05,520 --> 06:15:10,240 going to put this in and hopefully the 8258 06:15:07,759 --> 06:15:13,199 arrow is going to go away and we can 8259 06:15:10,240 --> 06:15:15,600 focus on studying the effects of these 8260 06:15:13,200 --> 06:15:17,680 things here this is going to print and 8261 06:15:15,599 --> 06:15:20,637 formatted so the data is going to be 8262 06:15:17,680 --> 06:15:23,840 cropped close to each other but we can 8263 06:15:20,637 --> 06:15:25,759 format this using set width and if we 8264 06:15:23,840 --> 06:15:28,957 pass in a 10 here we are basically 8265 06:15:25,759 --> 06:15:31,919 saying last name is going to take the 8266 06:15:28,957 --> 06:15:34,479 width of 10 characters even if we don't 8267 06:15:31,919 --> 06:15:36,797 really have 10 characters in this so so 8268 06:15:34,479 --> 06:15:39,759 you're going to have spaces fill the 8269 06:15:36,797 --> 06:15:42,557 remaining spaces so that the data shows 8270 06:15:39,759 --> 06:15:44,479 up nicely this is really hard to say in 8271 06:15:42,558 --> 06:15:46,400 world so i am going to run this in a 8272 06:15:44,479 --> 06:15:48,797 minute so that you can see this but 8273 06:15:46,400 --> 06:15:51,440 before we run i want you to see that you 8274 06:15:48,797 --> 06:15:53,919 can also store the width information in 8275 06:15:51,439 --> 06:15:56,637 a variable like this and i use that 8276 06:15:53,919 --> 06:15:59,359 instead in your std statements the 8277 06:15:56,637 --> 06:16:01,919 benefit with that is that it is really 8278 06:15:59,360 --> 06:16:04,479 easy to change this with if you happen 8279 06:16:01,919 --> 06:16:06,717 to change your mind otherwise you would 8280 06:16:04,479 --> 06:16:08,957 have to change a lot of things and this 8281 06:16:06,718 --> 06:16:11,360 is really bad design i did this to show 8282 06:16:08,957 --> 06:16:14,000 you how you can use this but in real 8283 06:16:11,360 --> 06:16:14,958 code you are most probably going to 8284 06:16:14,957 --> 06:16:20,079 your weft information in a variable and 8285 06:16:17,599 --> 06:16:22,717 that way it's going to be really easy to 8286 06:16:20,080 --> 06:16:26,638 change and update if you happen to need 8287 06:16:22,718 --> 06:16:28,958 that we can build this program with gcc 8288 06:16:26,637 --> 06:16:30,797 and if we run it let's bring this up a 8289 06:16:28,957 --> 06:16:32,717 little bit we're going to have our data 8290 06:16:30,797 --> 06:16:35,599 that is unformatted and you see that it 8291 06:16:32,718 --> 06:16:38,080 is really ugly but down below here we 8292 06:16:35,599 --> 06:16:40,319 have our formatted table and it is 8293 06:16:38,080 --> 06:16:43,200 really nice we have last name first name 8294 06:16:40,319 --> 06:16:45,360 and age and it is easier to see these 8295 06:16:43,200 --> 06:16:46,718 things we're going to see that it is 8296 06:16:46,718 --> 06:16:51,440 force the data to show on the left or to 8297 06:16:49,119 --> 06:16:53,919 the right with justification and we're 8298 06:16:51,439 --> 06:16:55,039 going to look at code that does that 8299 06:16:55,040 --> 06:16:59,360 if we go down here and put in the next 8300 06:16:57,200 --> 06:17:01,680 piece of code we want to play with we're 8301 06:16:59,360 --> 06:17:03,840 going to see that data is right 8302 06:17:01,680 --> 06:17:06,479 justified by default but we have seen 8303 06:17:03,840 --> 06:17:08,718 this also you see that gray is cramped 8304 06:17:06,479 --> 06:17:10,797 to the right of the width of the cell 8305 06:17:08,718 --> 06:17:13,200 for the first name so data is right 8306 06:17:10,797 --> 06:17:14,000 justified by default and it is the same 8307 06:17:18,080 --> 06:17:23,280 data here and trying to run we're going 8308 06:17:20,797 --> 06:17:25,199 to have a better visualization of that 8309 06:17:23,279 --> 06:17:27,199 so let's run a little bit we're going to 8310 06:17:25,200 --> 06:17:29,360 build the world is going to go through 8311 06:17:27,200 --> 06:17:31,440 and if we run this we're going to see 8312 06:17:29,360 --> 06:17:33,920 that data is going to show up to the 8313 06:17:31,439 --> 06:17:36,239 right gray is to the right woods is to 8314 06:17:33,919 --> 06:17:38,159 the right and this is the default 8315 06:17:38,159 --> 06:17:43,680 when you set with like this you can also 8316 06:17:40,878 --> 06:17:47,119 cause for the data to be left justified 8317 06:17:43,680 --> 06:17:49,840 and you do that by coming here and 8318 06:17:47,119 --> 06:17:52,878 changing this to left you can do that 8319 06:17:49,840 --> 06:17:55,119 but let's keep this in here instead 8320 06:17:52,878 --> 06:17:57,119 for your reference and let's put in 8321 06:17:55,119 --> 06:18:00,159 another piece of code to justify the 8322 06:17:57,119 --> 06:18:02,557 data to the left okay let's go down and 8323 06:18:00,159 --> 06:18:05,759 do that we're going to justify the data 8324 06:18:02,558 --> 06:18:08,319 to the left and if we print this out the 8325 06:18:05,759 --> 06:18:10,797 data here gray should come to the left 8326 06:18:08,319 --> 06:18:12,479 and be aligned with the first name here 8327 06:18:10,797 --> 06:18:16,159 i hope this makes sense we're going to 8328 06:18:12,479 --> 06:18:19,759 run we build with gcc and if we run 8329 06:18:16,159 --> 06:18:23,360 rooster gray is going to be justified to 8330 06:18:19,759 --> 06:18:26,637 the left this is the meaning of std left 8331 06:18:23,360 --> 06:18:29,279 and std right that we just saw and know 8332 06:18:26,637 --> 06:18:31,840 that data is justified to the right by 8333 06:18:29,279 --> 06:18:33,680 default and it is important to keep the 8334 06:18:33,680 --> 06:18:38,479 data is going to be justified to the 8335 06:18:35,919 --> 06:18:41,439 right by default the next thing we can 8336 06:18:38,479 --> 06:18:43,919 do is to make our data internal 8337 06:18:41,439 --> 06:18:47,119 justified and what this is going to do 8338 06:18:43,919 --> 06:18:50,000 is make the sign left justified and the 8339 06:18:47,119 --> 06:18:52,000 data is going to be right justified and 8340 06:18:50,000 --> 06:18:53,520 the best way to save this is really to 8341 06:18:53,520 --> 06:18:58,637 so here we have the data it's a negative 8342 06:18:55,919 --> 06:19:01,439 floating point number the width is set 8343 06:18:58,637 --> 06:19:03,759 to 10 it's going to be right justified 8344 06:19:01,439 --> 06:19:06,399 but we are also going to set the 8345 06:19:03,759 --> 06:19:07,439 justification to internal and see the 8346 06:19:07,439 --> 06:19:12,557 let's weld and run this we're going to 8347 06:19:09,840 --> 06:19:15,599 world with gcc and we are going to run 8348 06:19:12,558 --> 06:19:18,798 our rooster program and if we look at 8349 06:19:15,599 --> 06:19:20,797 the output here you see that if we have 8350 06:19:20,797 --> 06:19:27,279 the sign is going to go with the data 8351 06:19:23,680 --> 06:19:29,040 whereas if we do internal justification 8352 06:19:27,279 --> 06:19:31,680 the sign is going to go to the left and 8353 06:19:29,040 --> 06:19:34,240 the data is going to go to the right and 8354 06:19:31,680 --> 06:19:36,080 sometimes this is useful depending on 8355 06:19:34,240 --> 06:19:38,718 the application that you are welded 8356 06:19:36,080 --> 06:19:41,840 especially in applications where you 8357 06:19:38,718 --> 06:19:44,400 have to display tabular data on the 8358 06:19:41,840 --> 06:19:47,279 console or of the terminal we can also 8359 06:19:44,400 --> 06:19:48,319 specify the field character using set 8360 06:19:48,319 --> 06:19:53,360 and this is really how it works the data 8361 06:19:50,957 --> 06:19:55,039 is going to be left justified but we are 8362 06:19:55,040 --> 06:20:01,120 spaces with a dash here and we can bring 8363 06:19:58,878 --> 06:20:02,797 this up so that you see the entire thing 8364 06:20:01,119 --> 06:20:05,360 but you don't really have to type this 8365 06:20:02,797 --> 06:20:08,239 out you can use the code i have provided 8366 06:20:05,360 --> 06:20:10,637 in the resources section of this lecture 8367 06:20:08,240 --> 06:20:12,798 you can just copy it paste it in here 8368 06:20:10,637 --> 06:20:15,279 and use it to play with these things 8369 06:20:12,797 --> 06:20:18,079 okay with this our empty spaces are 8370 06:20:15,279 --> 06:20:19,199 going to be filled with this dash 8371 06:20:19,200 --> 06:20:24,878 let's world and build with gcc we are 8372 06:20:22,957 --> 06:20:27,359 going to run this and you're going to 8373 06:20:24,878 --> 06:20:29,200 see that we have a field character here 8374 06:20:27,360 --> 06:20:31,520 and you can change this basically to 8375 06:20:29,200 --> 06:20:34,798 whatever you want if i want i can change 8376 06:20:31,520 --> 06:20:37,279 this to a star let's do that and i can 8377 06:20:34,797 --> 06:20:39,360 build again if i run this it's going to 8378 06:20:37,279 --> 06:20:41,919 be filled with stars right now the next 8379 06:20:39,360 --> 06:20:44,958 thing we're going to look at is std bull 8380 06:20:41,919 --> 06:20:46,957 alpha we have seen this before but what 8381 06:20:44,957 --> 06:20:49,680 it's really going to do is force the 8382 06:20:46,957 --> 06:20:50,557 output to be in the format of true and 8383 06:20:50,558 --> 06:20:56,477 and the default is in the form of one 8384 06:20:52,957 --> 06:20:59,119 and zero here we have a few variables 8385 06:20:56,477 --> 06:21:02,477 and that they are of bull type 8386 06:20:59,119 --> 06:21:04,399 the first set of stdcl statements here 8387 06:21:02,477 --> 06:21:07,360 is going to print in the format of one 8388 06:21:04,400 --> 06:21:09,360 and zero we are going to set the output 8389 06:21:07,360 --> 06:21:12,319 to both alpha and it is going to be in 8390 06:21:09,360 --> 06:21:14,958 the format of true and false and then we 8391 06:21:12,319 --> 06:21:18,400 can disable the bull alpha with this 8392 06:21:14,957 --> 06:21:21,759 setting here which says hdd no pull 8393 06:21:18,400 --> 06:21:24,319 alpha we can vote this and see the 8394 06:21:21,759 --> 06:21:26,319 effect of this so we're going to run 8395 06:21:24,319 --> 06:21:29,439 rooster again and you're going to see 8396 06:21:26,319 --> 06:21:33,040 that condition 1 0 condition true false 8397 06:21:29,439 --> 06:21:36,079 condition 1 0 we are able to modify how 8398 06:21:33,040 --> 06:21:39,280 our boolean data is displayed on the 8399 06:21:36,080 --> 06:21:41,760 console using these two settings here 8400 06:21:39,279 --> 06:21:43,439 use them if they make sense for your 8401 06:21:41,759 --> 06:21:46,079 application the next thing we're going 8402 06:21:43,439 --> 06:21:48,079 to look at is show pose and we're going 8403 06:21:48,080 --> 06:21:52,718 we have two numbers positive number 8404 06:21:50,558 --> 06:21:56,240 which is 34 a negative number which is 8405 06:21:52,718 --> 06:21:57,280 minus 45 and we can decide if we want to 8406 06:21:57,279 --> 06:22:02,399 the plus sign for positive numbers by 8407 06:22:00,240 --> 06:22:04,718 default the plus sign is going to be 8408 06:22:02,400 --> 06:22:07,840 hidden it's not going to be shown if you 8409 06:22:04,718 --> 06:22:10,798 want it shown you can use the show pose 8410 06:22:07,840 --> 06:22:13,680 manipulator and if you want to disable 8411 06:22:10,797 --> 06:22:15,599 that when it was enabled you can use no 8412 06:22:13,680 --> 06:22:19,040 show pose and this is going to do what 8413 06:22:15,599 --> 06:22:20,957 you want we can build this again and if 8414 06:22:19,040 --> 06:22:23,360 we look at the output we're going to see 8415 06:22:20,957 --> 06:22:26,159 that post num doesn't have a plus sign 8416 06:22:23,360 --> 06:22:29,920 in front because by default the show 8417 06:22:26,159 --> 06:22:31,919 pause is disabled and we set the setting 8418 06:22:29,919 --> 06:22:34,797 here and we're going to see the plus 8419 06:22:31,919 --> 06:22:36,717 sign and if we disable that we're not 8420 06:22:34,797 --> 06:22:39,840 going to see the plus sign again this is 8421 06:22:36,718 --> 06:22:42,878 the effect of std show posts and std 8422 06:22:39,840 --> 06:22:45,520 neutral pose manipulators we can also 8423 06:22:42,878 --> 06:22:48,080 affect the base in which the data is 8424 06:22:45,520 --> 06:22:51,279 displayed on the terminal and we do this 8425 06:22:48,080 --> 06:22:55,440 by setting the output base system 8426 06:22:51,279 --> 06:22:56,878 we can set std deck to force the output 8427 06:22:56,878 --> 06:23:02,637 std hex to force the output to be in 8428 06:22:59,360 --> 06:23:06,797 hexadecimal and std act to force the 8429 06:23:02,637 --> 06:23:09,520 output in octal and this is only to take 8430 06:23:06,797 --> 06:23:12,239 effect for integral types this is not 8431 06:23:09,520 --> 06:23:14,159 going to have effect for floating point 8432 06:23:12,240 --> 06:23:16,240 numbers for example here we have a few 8433 06:23:14,159 --> 06:23:18,957 variables a positive integer and 8434 06:23:16,240 --> 06:23:20,878 negative integer and a floating point 8435 06:23:20,878 --> 06:23:26,319 and we're going to try and print these 8436 06:23:22,957 --> 06:23:27,359 things in all these base systems we're 8437 06:23:27,360 --> 06:23:31,279 print these things in the default format 8438 06:23:29,439 --> 06:23:33,039 we're going to see how they look we are 8439 06:23:31,279 --> 06:23:36,000 going to take the positive number and 8440 06:23:33,040 --> 06:23:37,680 show that in these different number 8441 06:23:36,000 --> 06:23:39,599 systems we're going to do the same for 8442 06:23:37,680 --> 06:23:40,797 the negative number and we're going to 8443 06:23:40,797 --> 06:23:46,717 this doesn't have effect on floating 8444 06:23:43,759 --> 06:23:48,877 point numbers because these number 8445 06:23:46,718 --> 06:23:52,240 systems don't really work for floating 8446 06:23:48,878 --> 06:23:54,797 points we can run this and see the 8447 06:23:52,240 --> 06:23:57,360 output we are going to run the task that 8448 06:23:54,797 --> 06:24:00,239 is going to build with gcc and we're 8449 06:23:57,360 --> 06:24:03,040 going to clear and run rooster and what 8450 06:24:00,240 --> 06:24:04,798 we're going to see is that by default 8451 06:24:03,040 --> 06:24:06,878 positive number is going to be this 8452 06:24:04,797 --> 06:24:09,199 negative number is going to be this and 8453 06:24:06,878 --> 06:24:10,958 our double var is going to be this and 8454 06:24:09,200 --> 06:24:13,119 we are going to take the positive number 8455 06:24:10,957 --> 06:24:15,199 show it in different base systems in 8456 06:24:13,119 --> 06:24:18,000 decimal it's going to be like this in 8457 06:24:15,200 --> 06:24:20,637 hex it's going to be like this in act 8458 06:24:18,000 --> 06:24:22,637 it's going to be like this the negative 8459 06:24:22,637 --> 06:24:27,680 in this format here so if we show it in 8460 06:24:25,119 --> 06:24:29,919 decimal it's going to be like this hex 8461 06:24:27,680 --> 06:24:32,637 it's going to be like this and octo it's 8462 06:24:29,919 --> 06:24:35,279 going to be like this you can see that 8463 06:24:32,637 --> 06:24:37,039 the double var which is a floating point 8464 06:24:35,279 --> 06:24:39,840 is not really affected it's going to be 8465 06:24:37,040 --> 06:24:42,159 showing in the same format regardless of 8466 06:24:39,840 --> 06:24:44,797 the base that we set here one thing you 8467 06:24:42,159 --> 06:24:47,279 should know is that this base setting 8468 06:24:44,797 --> 06:24:49,919 here is going to stick so here for 8469 06:24:47,279 --> 06:24:51,199 example you see that we have set output 8470 06:24:51,200 --> 06:24:56,637 so any other thing that we're going to 8471 06:24:53,279 --> 06:24:58,797 print out is going to be shown in octo 8472 06:24:56,637 --> 06:25:00,957 and we have to be sure that's what we 8473 06:24:58,797 --> 06:25:02,399 want if that's not what we want we will 8474 06:25:03,759 --> 06:25:08,000 number system and it's going to take 8475 06:25:06,000 --> 06:25:09,520 effect this is how you can use these 8476 06:25:09,520 --> 06:25:14,878 different number systems for output in 8477 06:25:12,159 --> 06:25:17,040 your terminal we have seen that you can 8478 06:25:14,878 --> 06:25:19,520 also force things to show up in 8479 06:25:17,040 --> 06:25:21,680 uppercase if that's important for your 8480 06:25:19,520 --> 06:25:24,477 application for example here we have a 8481 06:25:21,680 --> 06:25:26,000 positive number if we show that in hex 8482 06:25:24,477 --> 06:25:27,200 this is going to contain a few 8483 06:25:27,200 --> 06:25:32,718 and if we don't set uppercase 8484 06:25:30,477 --> 06:25:34,878 it is going to show up in lowercase and 8485 06:25:32,718 --> 06:25:37,040 if we want it in uppercase we can set 8486 06:25:34,878 --> 06:25:40,080 this setting and it is going to show up 8487 06:25:37,040 --> 06:25:42,319 in uppercase this may be helpful in some 8488 06:25:40,080 --> 06:25:44,718 applications and i just want you to be 8489 06:25:42,319 --> 06:25:48,637 aware of these things we're going to 8490 06:25:44,718 --> 06:25:50,240 world and run our application 8491 06:25:50,240 --> 06:25:55,520 let's bring this up a little bit so that 8492 06:25:52,080 --> 06:25:57,600 we have some breathing room our positive 8493 06:25:55,520 --> 06:26:00,718 integer here is going to show up in 8494 06:25:57,599 --> 06:26:03,359 lowercase here in hex because this is 8495 06:26:00,718 --> 06:26:05,360 going to contain characters and if we 8496 06:26:03,360 --> 06:26:08,159 set uppercase you're going to see that 8497 06:26:05,360 --> 06:26:11,200 the characters are going to show up in 8498 06:26:08,159 --> 06:26:13,680 uppercase and this is the effect of this 8499 06:26:11,200 --> 06:26:15,600 setting here we can also affect how 8500 06:26:19,040 --> 06:26:23,280 for example here we have a few variables 8501 06:26:23,279 --> 06:26:28,557 and by default they are going to show up 8502 06:26:25,759 --> 06:26:31,759 with decimal points and use scientific 8503 06:26:28,558 --> 06:26:33,440 notation where necessary but we can 8504 06:26:33,439 --> 06:26:39,039 fixed for example if that's what we need 8505 06:26:35,919 --> 06:26:41,679 in our application and the output is 8506 06:26:39,040 --> 06:26:44,080 going to be shown using fixed notation 8507 06:26:41,680 --> 06:26:46,477 with decimal points if we want we can 8508 06:26:44,080 --> 06:26:49,440 force the scientific format and we do it 8509 06:26:46,477 --> 06:26:52,319 like this and all output is going to be 8510 06:26:49,439 --> 06:26:54,797 in scientific format and we can disable 8511 06:26:52,319 --> 06:26:56,079 all these settings and reset this to the 8512 06:26:56,080 --> 06:27:01,440 with this piece of command here there is 8513 06:27:01,439 --> 06:27:07,599 set floating point output to the 8514 06:27:04,558 --> 06:27:09,760 defaults and this is a hack i actually 8515 06:27:07,599 --> 06:27:12,557 learned about when i was preparing the 8516 06:27:09,759 --> 06:27:15,279 code for this course and the proper 8517 06:27:12,558 --> 06:27:17,760 explanations for this are really out of 8518 06:27:15,279 --> 06:27:20,717 scope for this course so if you want to 8519 06:27:17,759 --> 06:27:23,599 reset the floating point output format 8520 06:27:20,718 --> 06:27:25,840 to the default you're going to use this 8521 06:27:23,599 --> 06:27:29,199 but we don't really have enough tools to 8522 06:27:25,840 --> 06:27:31,520 understand this yet so just take it by 8523 06:27:29,200 --> 06:27:33,600 faith and you're going to see that if we 8524 06:27:31,520 --> 06:27:36,319 run this it's going to work let's try 8525 06:27:33,599 --> 06:27:38,399 and run this through the compiler and 8526 06:27:36,319 --> 06:27:40,637 that the world is going to go through if 8527 06:27:38,400 --> 06:27:43,120 we run this and i bring this up a little 8528 06:27:40,637 --> 06:27:45,039 bit so that we have a little bit of 8529 06:27:43,119 --> 06:27:48,477 breathing room we're going to see that 8530 06:27:45,040 --> 06:27:51,440 our double values are a b and c and 8531 06:27:48,477 --> 06:27:53,200 that's what we have in our code here and 8532 06:27:51,439 --> 06:27:55,199 by default it's not going to be able to 8533 06:27:53,200 --> 06:27:57,920 show these things because of the 8534 06:27:55,200 --> 06:28:00,319 precision limits so we're going to have 8535 06:28:00,319 --> 06:28:04,878 it's going to go up to here and the rest 8536 06:28:02,477 --> 06:28:07,119 is going to be chopped off if we use 8537 06:28:04,878 --> 06:28:10,240 fixed formats you're going to see that 8538 06:28:07,119 --> 06:28:13,200 whatever we had in scientific format 8539 06:28:10,240 --> 06:28:15,600 it's going to show up in fixed format 8540 06:28:13,200 --> 06:28:18,477 and again this is a really low number 8541 06:28:19,439 --> 06:28:23,840 we can force the output to be scientific 8542 06:28:25,360 --> 06:28:29,440 and all output is going to be in 8543 06:28:29,439 --> 06:28:33,119 down below here we use our hack to set 8544 06:28:33,119 --> 06:28:39,039 the defaults and you see that it's going 8545 06:28:35,759 --> 06:28:42,557 to go back to the initial format that we 8546 06:28:39,040 --> 06:28:44,878 had before this is how you use std fixed 8547 06:28:45,680 --> 06:28:51,279 manipulate how your floating point 8548 06:28:48,080 --> 06:28:53,600 numbers are printed to the terminal we 8549 06:28:51,279 --> 06:28:57,680 have also seen that we can set the 8550 06:28:53,599 --> 06:29:00,399 precision of our output in the terminal 8551 06:28:57,680 --> 06:29:02,957 here we have a variable a it is of type 8552 06:29:00,400 --> 06:29:05,120 double as you can see in a visual studio 8553 06:29:02,957 --> 06:29:07,359 code here if you hover over a variable 8554 06:29:05,119 --> 06:29:09,680 you're going to be able to see its type 8555 06:29:07,360 --> 06:29:12,637 we can choose to see this in whatever 8556 06:29:09,680 --> 06:29:15,680 precision we want 6 is the default in 8557 06:29:12,637 --> 06:29:18,957 our case here but we can choose to set 8558 06:29:15,680 --> 06:29:20,797 the precision to 10 20 or whatever it is 8559 06:29:20,797 --> 06:29:24,878 please note that this is going to be 8560 06:29:22,637 --> 06:29:27,279 limited by the precision that your type 8561 06:29:24,878 --> 06:29:30,558 supports so for example we know that a 8562 06:29:27,279 --> 06:29:33,439 double has a precision of 15 and if we 8563 06:29:30,558 --> 06:29:35,760 have more than 15 digits in here the 8564 06:29:33,439 --> 06:29:38,000 rest is going to be garbage just be 8565 06:29:35,759 --> 06:29:40,877 aware of that we're going to try and 8566 06:29:38,000 --> 06:29:44,319 build this and we're going to use gcc as 8567 06:29:44,319 --> 06:29:48,319 we're going to get our numbers in 8568 06:29:46,159 --> 06:29:50,400 different precisions the first one is 8569 06:29:48,319 --> 06:29:51,360 using the default which is going to be 8570 06:29:51,360 --> 06:29:54,958 if we use 10 we're going to get 10 8571 06:29:53,119 --> 06:29:57,759 digits if we use 20 we're going to get 8572 06:29:54,957 --> 06:30:00,637 20 digits but if we go over the 8573 06:29:57,759 --> 06:30:02,957 precision that our type here supports 8574 06:30:00,637 --> 06:30:04,957 we're going to get garbage i hope this 8575 06:30:02,957 --> 06:30:07,840 makes sense the last thing we're going 8576 06:30:04,957 --> 06:30:08,717 to see is show point and we have seen 8577 06:30:11,200 --> 06:30:16,798 outputting the decimal point let's say 8578 06:30:13,680 --> 06:30:18,319 that here and a good example for that is 8579 06:30:19,840 --> 06:30:25,840 here we're not going to get the decimal 8580 06:30:22,797 --> 06:30:28,957 point by default it's going to print 12 8581 06:30:25,840 --> 06:30:31,200 but if we go down and force output of 8582 06:30:28,957 --> 06:30:34,717 the decimal point here we're going to 8583 06:30:34,718 --> 06:30:40,558 and another effect of this is that it's 8584 06:30:37,360 --> 06:30:43,119 going to add trailing zeros to fill 8585 06:30:40,558 --> 06:30:45,920 whatever space is allocated for your 8586 06:30:43,119 --> 06:30:48,159 output variable in the terminal 8587 06:30:45,919 --> 06:30:50,399 let's try and run this so that we can 8588 06:30:48,159 --> 06:30:54,240 actually see this running we're going to 8589 06:30:50,400 --> 06:30:56,958 run our rooster executable and in the 8590 06:30:54,240 --> 06:30:59,760 first group here we don't have the 8591 06:30:56,957 --> 06:31:02,477 setting for show point and if you look 8592 06:30:59,759 --> 06:31:05,359 at the numbers that we had in our code 8593 06:31:02,477 --> 06:31:07,439 the first number is 34.1 and you see 8594 06:31:05,360 --> 06:31:10,400 that we are padding a lot of numbers 8595 06:31:07,439 --> 06:31:13,039 because we had set our precision to a 8596 06:31:10,400 --> 06:31:15,440 really big number in the last example 8597 06:31:13,040 --> 06:31:16,798 that we have run let's actually go there 8598 06:31:15,439 --> 06:31:19,039 and show you that that's why we're 8599 06:31:19,040 --> 06:31:24,080 it is really huge but no big deal we can 8600 06:31:21,680 --> 06:31:26,240 reset this to whatever we want if this 8601 06:31:26,240 --> 06:31:32,958 the important thing here is that we have 8602 06:31:29,797 --> 06:31:36,520 34.1 and that's what we are seeing and 8603 06:31:32,957 --> 06:31:40,957 if you go to our 12 here you see we have 8604 06:31:36,520 --> 06:31:43,279 12.0 but our output is not showing 8605 06:31:40,957 --> 06:31:45,279 the decimal point here if it is 8606 06:31:43,279 --> 06:31:48,637 important to show the decimal point we 8607 06:31:45,279 --> 06:31:50,717 can do std show point here and it is 8608 06:31:48,637 --> 06:31:53,439 going to show up this is really what is 8609 06:31:50,718 --> 06:31:54,878 important in this piece of code here and 8610 06:31:53,439 --> 06:31:56,637 this is really all we set out to do in 8611 06:31:54,878 --> 06:31:59,520 this lecture we had a chance to play 8612 06:31:56,637 --> 06:32:01,279 with a lot of these manipulators and the 8613 06:31:59,520 --> 06:32:04,159 best way to learn about these things is 8614 06:32:01,279 --> 06:32:06,319 really to try them out in code 8615 06:32:04,159 --> 06:32:09,520 and try to change a few things and see 8616 06:32:06,319 --> 06:32:11,919 how that affects the output you get in 8617 06:32:09,520 --> 06:32:14,159 the terminal and we had a chance to play 8618 06:32:11,919 --> 06:32:17,039 with many of these things in this 8619 06:32:14,159 --> 06:32:19,919 lecture there is a lot of these guys i 8620 06:32:17,040 --> 06:32:22,878 tried to make this lecture as extensive 8621 06:32:19,919 --> 06:32:24,957 as i can that's why it turned out to be 8622 06:32:22,878 --> 06:32:27,119 pretty long but once you've learned 8623 06:32:24,957 --> 06:32:29,680 about these fans and played with them in 8624 06:32:27,119 --> 06:32:32,718 code you're never going to forget them 8625 06:32:29,680 --> 06:32:36,319 one last point i want to emphasize again 8626 06:32:32,718 --> 06:32:39,040 is that the documentation is of utmost 8627 06:32:36,319 --> 06:32:41,279 importance it is really good to come 8628 06:32:39,040 --> 06:32:43,760 here and check things out for example if 8629 06:32:41,279 --> 06:32:46,239 we go to show point here we are going to 8630 06:32:43,759 --> 06:32:48,957 get a nice explanation of what this 8631 06:32:46,240 --> 06:32:50,958 manipulator does enables all disables 8632 06:32:48,957 --> 06:32:53,279 the unconditional inclusion of the 8633 06:32:50,957 --> 06:32:54,319 decimal point character and you can read 8634 06:32:54,319 --> 06:32:58,637 and the nice thing is that if you go 8635 06:32:56,558 --> 06:32:59,520 down you're going to have a piece of 8636 06:32:59,520 --> 06:33:03,520 that you can use to really understand 8637 06:33:01,360 --> 06:33:05,760 whatever it is they are talking about 8638 06:33:03,520 --> 06:33:08,000 and this is priceless if you are 8639 06:33:05,759 --> 06:33:10,957 learning it is a good thing to be able 8640 06:33:08,000 --> 06:33:13,599 to use the documentation this is really 8641 06:33:10,957 --> 06:33:16,319 all we set out to do in this lecture it 8642 06:33:13,599 --> 06:33:18,399 turned out to be really long but i 8643 06:33:16,319 --> 06:33:20,319 really hope it was worth it for you 8644 06:33:18,400 --> 06:33:21,520 we're going to stop here in this lecture 8645 06:33:20,319 --> 06:33:24,718 in the next one we're going to learn 8646 06:33:21,520 --> 06:33:27,200 about numeric limits go ahead and finish 8647 06:33:24,718 --> 06:33:29,200 up here and meet me there in this 8648 06:33:27,200 --> 06:33:31,920 lecture we're going to be playing with a 8649 06:33:29,200 --> 06:33:33,680 few functions from this library 8650 06:33:31,919 --> 06:33:36,239 specifically we're going to be looking 8651 06:33:33,680 --> 06:33:38,000 at the minimum the maximum and the 8652 06:33:38,000 --> 06:33:43,520 and this is really how you use them you 8653 06:33:40,400 --> 06:33:46,080 can think of this t as a placeholder for 8654 06:33:43,520 --> 06:33:48,957 the type for which you want to know the 8655 06:33:46,080 --> 06:33:51,200 minimum the maximum and the lowest point 8656 06:33:48,957 --> 06:33:53,439 respectively i know this might be 8657 06:33:51,200 --> 06:33:56,319 cryptic to some of you so we're going to 8658 06:33:53,439 --> 06:33:58,877 look at a few specific examples here we 8659 06:33:56,319 --> 06:34:00,558 have a simple explanation of what these 8660 06:34:02,878 --> 06:34:07,760 the minimum is going to represent the 8661 06:34:07,759 --> 06:34:12,637 that is representable with that floating 8662 06:34:10,477 --> 06:34:14,477 point type the maximum is going to be 8663 06:34:12,637 --> 06:34:17,279 the maximum floating point number you 8664 06:34:14,477 --> 06:34:20,079 can represent and at the lowest number 8665 06:34:17,279 --> 06:34:22,717 is going to be the lowest negative 8666 06:34:20,080 --> 06:34:24,240 number you can represent with that type 8667 06:34:22,718 --> 06:34:27,200 things change a little bit when you 8668 06:34:24,240 --> 06:34:29,120 start dealing with integers and these 8669 06:34:27,200 --> 06:34:31,280 functions are going to look like this 8670 06:34:29,119 --> 06:34:32,878 the minimum is going to be the minimum 8671 06:34:31,279 --> 06:34:35,680 number you can represent with that 8672 06:34:32,878 --> 06:34:38,958 integer and lowest doesn't really mean 8673 06:34:35,680 --> 06:34:41,040 anything for integral types the maximum 8674 06:34:38,957 --> 06:34:42,957 is going to be the maximum number one 8675 06:34:41,040 --> 06:34:45,520 thing you have to keep in mind is that 8676 06:34:42,957 --> 06:34:49,199 the minimum may be negative 8677 06:34:45,520 --> 06:34:51,279 if you are using a signed integer if you 8678 06:34:49,200 --> 06:34:53,760 are using an unsigned integer for 8679 06:34:51,279 --> 06:34:56,159 example for short minimum is going to be 8680 06:34:53,759 --> 06:34:59,279 zero and the maximum is going to be the 8681 06:34:56,159 --> 06:35:01,840 maximum number representable with that 8682 06:34:59,279 --> 06:35:04,878 integer type here we are looking at 8683 06:35:01,840 --> 06:35:07,759 numbers for short and but the same story 8684 06:35:04,878 --> 06:35:10,000 applies to whatever integer type you are 8685 06:35:07,759 --> 06:35:13,599 using even if you happen to be using 8686 06:35:10,000 --> 06:35:15,680 long end or end without any modifier 8687 06:35:13,599 --> 06:35:19,439 remember that we had to do a lot of 8688 06:35:15,680 --> 06:35:22,080 things to get this range and it was 8689 06:35:19,439 --> 06:35:26,399 really hard to even do this for floating 8690 06:35:22,080 --> 06:35:28,240 point types so this library is helpful 8691 06:35:26,400 --> 06:35:30,718 in getting a sense of what we can 8692 06:35:28,240 --> 06:35:33,440 represent with the given type b8 8693 06:35:30,718 --> 06:35:35,840 floating point or integral let's head to 8694 06:35:33,439 --> 06:35:38,877 visual studio code and play with us here 8695 06:35:35,840 --> 06:35:40,797 we are in our working folder 8696 06:35:38,878 --> 06:35:42,637 we are going to copy code from our 8697 06:35:42,637 --> 06:35:46,718 and we are going to go in the current 8698 06:35:44,718 --> 06:35:49,520 project which is numeric limits we're 8699 06:35:46,718 --> 06:35:52,718 going to go and put in our files 8700 06:35:49,520 --> 06:35:54,558 go up again and open this file up in 8701 06:35:52,718 --> 06:35:57,440 visual studio code we're going to close 8702 06:35:54,558 --> 06:35:59,840 this up open the main cpp file remove 8703 06:35:57,439 --> 06:36:02,239 whatever it is we don't need we are 8704 06:35:59,840 --> 06:36:04,000 going to include the limits library 8705 06:36:02,240 --> 06:36:05,120 remember this is the first thing we need 8706 06:36:05,119 --> 06:36:11,439 if we want to play with these facilities 8707 06:36:09,040 --> 06:36:13,760 that we just looked at in the slides 8708 06:36:11,439 --> 06:36:16,557 and we are going to go down here and put 8709 06:36:13,759 --> 06:36:19,279 in the code we want to play with 8710 06:36:16,558 --> 06:36:21,440 and it is ridiculously simple 8711 06:36:19,279 --> 06:36:23,680 if you have watched the slides we just 8712 06:36:21,439 --> 06:36:25,359 did in a minute so what we're going to 8713 06:36:23,680 --> 06:36:28,000 be doing here we're going to be printing 8714 06:36:25,360 --> 06:36:30,159 out the range for each possible type we 8715 06:36:28,000 --> 06:36:33,520 want to play with here so we're going to 8716 06:36:30,159 --> 06:36:35,919 say the range for short and is from 8717 06:36:33,520 --> 06:36:37,040 and we're going to say std numeric 8718 06:36:37,040 --> 06:36:42,000 short that's what we want to deal with 8719 06:36:39,520 --> 06:36:44,319 now and we're going to call the minimum 8720 06:36:42,000 --> 06:36:46,718 function this is going to give us 8721 06:36:44,319 --> 06:36:49,520 the minimum value we can represent with 8722 06:36:46,718 --> 06:36:51,200 short and we're going to say std numeric 8723 06:36:49,520 --> 06:36:52,159 limits and we're going to call the max 8724 06:36:52,159 --> 06:36:56,797 passing in the type for which we want to 8725 06:36:56,797 --> 06:37:02,477 and the same story applies for 8726 06:37:03,840 --> 06:37:08,797 you see that's the type we are using 8727 06:37:05,680 --> 06:37:10,957 here we are also going to play with ant 8728 06:37:08,797 --> 06:37:12,477 getting the range we're going to use 8729 06:37:12,477 --> 06:37:16,399 to see how these things behave we're 8730 06:37:16,400 --> 06:37:21,840 and get to the range and we are also 8731 06:37:18,797 --> 06:37:24,079 going to be playing with floating point 8732 06:37:21,840 --> 06:37:26,878 types so we're going to compute the 8733 06:37:24,080 --> 06:37:29,440 range using the minimum function and the 8734 06:37:26,878 --> 06:37:32,477 maximum function and we are going to be 8735 06:37:29,439 --> 06:37:35,279 also using the lowest function to see 8736 06:37:32,477 --> 06:37:37,759 that this really is the same thing we 8737 06:37:35,279 --> 06:37:40,079 described in the slides we're going to 8738 06:37:37,759 --> 06:37:41,840 do this for double and long double and 8739 06:37:41,840 --> 06:37:47,040 come up in this program what we're going 8740 06:37:44,000 --> 06:37:50,240 to do is open up a terminal here we are 8741 06:37:47,040 --> 06:37:52,638 going to run the task to build with gcc 8742 06:37:50,240 --> 06:37:56,320 and the build is going to be good 8743 06:37:52,637 --> 06:37:57,919 and we can go down and run rooster and 8744 06:37:56,319 --> 06:38:02,637 we're going to get our values here the 8745 06:37:57,919 --> 06:38:04,119 range for short and is from minus 32 768 8746 06:38:04,119 --> 06:38:09,439 767 and you see that this is exactly the 8747 06:38:07,279 --> 06:38:11,759 same thing we saw in the lecture where 8748 06:38:09,439 --> 06:38:14,319 we talked about number systems we saw a 8749 06:38:11,759 --> 06:38:15,359 way you can do this manually by plugging 8750 06:38:15,360 --> 06:38:20,159 number of digits in some formula that we 8751 06:38:18,000 --> 06:38:22,319 came up you can go back and check that 8752 06:38:20,159 --> 06:38:25,040 out if you want we do the same for 8753 06:38:22,319 --> 06:38:27,279 unsigned short and and then we look at 8754 06:38:25,040 --> 06:38:29,760 floating point types we're going to 8755 06:38:32,000 --> 06:38:37,599 minimum function to get the start of the 8756 06:38:34,797 --> 06:38:40,477 range we're going to get the minimum 8757 06:38:37,599 --> 06:38:42,717 positive number we can represent with a 8758 06:38:40,477 --> 06:38:45,439 float if we use lowest we're going to 8759 06:38:42,718 --> 06:38:47,760 get the lowest minimum number and that 8760 06:38:45,439 --> 06:38:50,000 the maximum is going to be the same if 8761 06:38:47,759 --> 06:38:52,637 you think this is really small you have 8762 06:38:50,000 --> 06:38:56,477 the option to open up this folder in 8763 06:38:52,637 --> 06:38:58,957 file explorer and you can open a command 8764 06:38:56,477 --> 06:39:01,520 prompt or powershell window by hitting 8765 06:38:58,957 --> 06:39:04,319 shift and right clicking and choosing 8766 06:39:01,520 --> 06:39:05,119 this option here open powershell window 8767 06:39:05,119 --> 06:39:08,718 and you're going to get a bigger window 8768 06:39:07,040 --> 06:39:10,638 in which you can type things we're going 8769 06:39:08,718 --> 06:39:13,600 to run rooster and we're going to see 8770 06:39:10,637 --> 06:39:15,919 things in a much easier way here 8771 06:39:13,599 --> 06:39:19,439 this is an option you can use or you can 8772 06:39:15,919 --> 06:39:21,919 even use the integrated terminal inside 8773 06:39:19,439 --> 06:39:24,319 visual studio code here like we've been 8774 06:39:21,919 --> 06:39:27,359 doing all along there are a few other 8775 06:39:24,319 --> 06:39:32,239 facilities you can use in this library 8776 06:39:27,360 --> 06:39:34,797 of limits for example we can try and 8777 06:39:32,240 --> 06:39:37,680 print out if a number is signed or 8778 06:39:34,797 --> 06:39:40,637 unsigned or get the number of digits we 8779 06:39:37,680 --> 06:39:42,878 can represent in an integer type 8780 06:39:40,637 --> 06:39:44,878 these are a few examples i just chose 8781 06:39:42,878 --> 06:39:47,279 because they are easy to understand and 8782 06:39:44,878 --> 06:39:49,360 really helpful but you can find more 8783 06:39:51,119 --> 06:39:56,557 and if you come here at cppreference.com 8784 06:39:54,000 --> 06:39:59,040 the link is here you can really find all 8785 06:39:56,558 --> 06:40:01,200 about this library here this library 8786 06:39:59,040 --> 06:40:04,000 provides a standardized way to carry 8787 06:40:01,200 --> 06:40:06,159 various properties of arithmetic types 8788 06:40:04,000 --> 06:40:08,878 for example the largest possible value 8789 06:40:08,878 --> 06:40:13,680 and we have a bunch of things we can do 8790 06:40:10,637 --> 06:40:14,557 here don't be scared of by this template 8791 06:40:14,558 --> 06:40:19,920 just think of them as placeholders for 8792 06:40:18,000 --> 06:40:22,477 the type for which you are trying to 8793 06:40:19,919 --> 06:40:24,717 query information for there are a few 8794 06:40:22,477 --> 06:40:27,840 functions you can call on these things 8795 06:40:24,718 --> 06:40:29,840 for example there is mean lowest max 8796 06:40:27,840 --> 06:40:32,558 that we just played with in visual 8797 06:40:29,840 --> 06:40:34,637 studio code but uh you can see that 8798 06:40:32,558 --> 06:40:36,798 there are a lot of things you can play 8799 06:40:34,637 --> 06:40:38,957 with especially if you have some math 8800 06:40:36,797 --> 06:40:41,439 background to understand all these 8801 06:40:38,957 --> 06:40:44,159 concepts here you can play with us and 8802 06:40:41,439 --> 06:40:46,159 see if it is helpful for whatever 8803 06:40:44,159 --> 06:40:47,520 application you are doing okay this is 8804 06:40:46,159 --> 06:40:49,919 really all we set out to do in this 8805 06:40:47,520 --> 06:40:53,119 lecture to learn a little bit about the 8806 06:40:49,919 --> 06:40:55,359 limits library it is helpful in 8807 06:40:53,119 --> 06:40:57,279 trying to understand the ranges for your 8808 06:40:55,360 --> 06:40:59,840 types if you happen to need these kinds 8809 06:40:57,279 --> 06:41:01,680 of things this is going to come in handy 8810 06:40:59,840 --> 06:41:03,040 we're going to stop here in this lecture 8811 06:41:01,680 --> 06:41:06,240 in the next one we're going to learn 8812 06:41:03,040 --> 06:41:09,680 about some math functions we can use in 8813 06:41:06,240 --> 06:41:11,760 c plus plus go ahead and finish up here 8814 06:41:09,680 --> 06:41:14,400 and meet me there in this lecture we're 8815 06:41:11,759 --> 06:41:16,717 going to learn about math functions and 8816 06:41:14,400 --> 06:41:19,600 these are functions that are built into 8817 06:41:16,718 --> 06:41:22,159 the z plus plus standard library that we 8818 06:41:19,599 --> 06:41:24,159 can use to do some math if you are doing 8819 06:41:22,159 --> 06:41:25,759 some math operations you're going to 8820 06:41:25,759 --> 06:41:31,439 they live in the c math library and if 8821 06:41:30,000 --> 06:41:33,279 you want to use them you're just going 8822 06:41:31,439 --> 06:41:35,359 to include that and you're going to have 8823 06:41:33,279 --> 06:41:37,759 access to them what you see here is a 8824 06:41:35,360 --> 06:41:40,878 simple summary of some of the things you 8825 06:41:37,759 --> 06:41:45,359 can use in these math functions you can 8826 06:41:40,878 --> 06:41:46,080 use std flower to round down or hdd seal 8827 06:41:46,080 --> 06:41:51,840 round up you can use the abs function to 8828 06:41:49,439 --> 06:41:54,717 compute the absolute value of a number 8829 06:41:51,840 --> 06:41:57,920 you can do trigonometry you can do the 8830 06:41:54,718 --> 06:42:00,080 cosine sign and things like that you can 8831 06:41:57,919 --> 06:42:01,759 compute the exponential of a number 8832 06:42:00,080 --> 06:42:04,798 you're going to see what this means in a 8833 06:42:01,759 --> 06:42:07,199 few lectures you can use std log and 8834 06:42:04,797 --> 06:42:09,199 again if you have some math background 8835 06:42:07,200 --> 06:42:11,440 these concepts are not going to be 8836 06:42:09,200 --> 06:42:14,240 foreign to you many of these functions 8837 06:42:11,439 --> 06:42:17,039 are documented here at cpp reference so 8838 06:42:14,240 --> 06:42:19,440 it might be worthwhile to go there and 8839 06:42:17,040 --> 06:42:21,280 check what functions are available there 8840 06:42:19,439 --> 06:42:23,520 here is an example of something you 8841 06:42:21,279 --> 06:42:25,919 might want to do for example you have a 8842 06:42:23,520 --> 06:42:30,558 variable it's called weight inside we 8843 06:42:25,919 --> 06:42:32,239 have a 7.7 and if you do std floor of 8844 06:42:30,558 --> 06:42:35,200 this variable this is going to be 8845 06:42:32,240 --> 06:42:37,680 rounded down to a 7. if you want to 8846 06:42:35,200 --> 06:42:39,360 round up you're going to use std seal 8847 06:42:37,680 --> 06:42:41,279 and you're going to get that and we're 8848 06:42:39,360 --> 06:42:44,159 going to see what this is going to give 8849 06:42:41,279 --> 06:42:46,399 us in a minute when we hit visual studio 8850 06:42:44,159 --> 06:42:48,718 code here is how you would use the 8851 06:42:46,400 --> 06:42:52,080 absolute value function you have a few 8852 06:42:48,718 --> 06:42:54,798 variables here and if you try to plug 8853 06:42:52,080 --> 06:42:56,638 them in this absolute value function 8854 06:42:54,797 --> 06:43:00,319 you're basically going to get the weight 8855 06:42:56,637 --> 06:43:02,878 of the number without any sign in front 8856 06:43:00,319 --> 06:43:04,797 of the number positive or negative the 8857 06:43:02,878 --> 06:43:07,760 absolute value is the weight of the 8858 06:43:04,797 --> 06:43:10,319 number without caring about the sign if 8859 06:43:07,759 --> 06:43:12,717 i may say it like that okay we also have 8860 06:43:10,319 --> 06:43:15,520 the x function which is going to compute 8861 06:43:12,718 --> 06:43:17,520 the exponential of a number this is a 8862 06:43:15,520 --> 06:43:19,760 math concept so if you don't have a 8863 06:43:17,520 --> 06:43:22,000 background in math this is going to be a 8864 06:43:19,759 --> 06:43:24,239 little hard to wrap your brains around 8865 06:43:22,000 --> 06:43:25,520 but in math we have this number which is 8866 06:43:25,520 --> 06:43:32,558 and its value is approximately 2.7 18 28 8867 06:43:30,477 --> 06:43:35,439 like you see here the exponential 8868 06:43:32,558 --> 06:43:38,400 function is going to do what f x here is 8869 06:43:35,439 --> 06:43:41,759 doing it's going to take e and elevate 8870 06:43:38,400 --> 06:43:44,080 that to the power of x and for example 8871 06:43:41,759 --> 06:43:46,877 in our code here we're going to get the 8872 06:43:44,080 --> 06:43:49,680 exponential of 10 which is going to be e 8873 06:43:46,878 --> 06:43:51,680 elevated to the power of tan and we're 8874 06:43:49,680 --> 06:43:53,200 going to play with this in visual studio 8875 06:43:51,680 --> 06:43:55,680 code and you're going to see the value 8876 06:43:53,200 --> 06:43:57,360 of this we also have the power function 8877 06:43:55,680 --> 06:43:59,840 here which is going to take the first 8878 06:43:57,360 --> 06:44:01,680 parameter and elevate that to the power 8879 06:44:02,878 --> 06:44:08,080 this is going to be 3 to the power of 4 8880 06:44:05,439 --> 06:44:10,399 and 9 to the power of 3 and we're going 8881 06:44:08,080 --> 06:44:13,520 to get the answer if we plug this into 8882 06:44:10,400 --> 06:44:16,080 visual studio code and we have look 8883 06:44:13,520 --> 06:44:18,718 functions which is basically the reverse 8884 06:44:16,080 --> 06:44:20,080 of the power function that we just 8885 06:44:20,080 --> 06:44:27,360 if you do log 8 in base 2 for example 8886 06:44:24,477 --> 06:44:29,919 it is like you are asking to which power 8887 06:44:31,919 --> 06:44:36,477 and the answer here is going to be a 3 8888 06:44:33,840 --> 06:44:39,920 of course but the catch is that the log 8889 06:44:36,477 --> 06:44:42,477 function by default works in base e 8890 06:44:39,919 --> 06:44:46,199 so what we are asking for example if we 8891 06:44:46,400 --> 06:44:52,638 is to which power should we elevate e 8892 06:44:49,919 --> 06:44:54,239 to get this number in here and it is 8893 06:44:52,637 --> 06:44:56,878 going to be computed and you're going to 8894 06:44:54,240 --> 06:44:59,280 see this if you plug this into 8895 06:44:56,878 --> 06:45:01,760 visual studio code or you can try to use 8896 06:44:59,279 --> 06:45:03,360 one of the calculators available to you 8897 06:45:01,759 --> 06:45:06,239 and you're going to see that this makes 8898 06:45:03,360 --> 06:45:08,159 sense we also have a special function 8899 06:45:06,240 --> 06:45:09,680 that is going to do the same thing but 8900 06:45:09,680 --> 06:45:14,000 and this is going to be easier to 8901 06:45:11,599 --> 06:45:16,159 understand for example here we are 8902 06:45:17,200 --> 06:45:22,319 what we are really saying is to which 8903 06:45:22,319 --> 06:45:28,957 to get 10 000 and this is easy because 8904 06:45:25,279 --> 06:45:32,637 the answer is a 4 10 to the power of 4 8905 06:45:28,957 --> 06:45:35,439 is 10 000 i really hope this makes sense 8906 06:45:32,637 --> 06:45:38,239 we also have a square route function we 8907 06:45:35,439 --> 06:45:39,359 can use so square out of 81 is going to 8908 06:45:39,360 --> 06:45:43,840 we have a function called std round 8909 06:45:43,840 --> 06:45:49,200 and uh what it's going to do by default 8910 06:45:46,957 --> 06:45:52,319 halfway points are going to be rounded 8911 06:45:49,200 --> 06:45:55,200 away from zero so if you have a 2.5 it's 8912 06:45:52,319 --> 06:45:58,319 going to be rounded up to a 3 and if you 8913 06:45:55,200 --> 06:46:01,760 have 2.4 it's going to be rounded down 8914 06:45:58,319 --> 06:46:04,400 to a 2. we also have a host of functions 8915 06:46:01,759 --> 06:46:06,477 we can use in trigonometry we have a 8916 06:46:04,400 --> 06:46:08,878 sine function we have a cosine function 8917 06:46:06,477 --> 06:46:11,200 we have a tan function if you know these 8918 06:46:08,878 --> 06:46:14,400 concepts in math this is going to come 8919 06:46:11,200 --> 06:46:16,240 in really handy if you want to do this i 8920 06:46:14,400 --> 06:46:19,760 have to say that what we just talked 8921 06:46:16,240 --> 06:46:23,120 about is a small set of what we can do 8922 06:46:19,759 --> 06:46:26,797 with these functions here in c math and 8923 06:46:23,119 --> 06:46:29,360 uh we are here at cppreference.com 8924 06:46:26,797 --> 06:46:32,079 and we can see all these functions here 8925 06:46:29,360 --> 06:46:34,477 we can do reminder we can do f men and 8926 06:46:32,080 --> 06:46:37,200 if you want you can check this out in 8927 06:46:34,477 --> 06:46:40,399 the documentation for example if you go 8928 06:46:37,200 --> 06:46:42,477 to can here and click on it you are 8929 06:46:40,400 --> 06:46:44,878 going to get the description and 8930 06:46:42,477 --> 06:46:46,159 explanation of what this function does 8931 06:46:44,878 --> 06:46:49,119 and you're going to have a simple 8932 06:46:46,159 --> 06:46:51,439 example of how you can play with us okay 8933 06:46:49,119 --> 06:46:54,000 now i hope you have a better idea of 8934 06:46:51,439 --> 06:46:56,877 what we can do with these functions in 8935 06:46:54,000 --> 06:46:58,400 the cmat library we're going to head to 8936 06:46:58,400 --> 06:47:03,120 and play with some of these functions 8937 06:47:00,558 --> 06:47:05,040 here we are in visual studio code the 8938 06:47:03,119 --> 06:47:07,360 current project we're going to work on 8939 06:47:05,040 --> 06:47:10,080 is math functions we're going to copy 8940 06:47:10,080 --> 06:47:15,680 and we're going to put them in our 8941 06:47:12,957 --> 06:47:17,840 directory here math functions 8942 06:47:15,680 --> 06:47:21,279 and we are going to open this up in 8943 06:47:17,840 --> 06:47:23,759 visual studio code and we are going to 8944 06:47:21,279 --> 06:47:26,399 remove whatever it is we don't need 8945 06:47:23,759 --> 06:47:28,319 and we are going to include our library 8946 06:47:28,319 --> 06:47:32,797 and we are going to put in our code so 8947 06:47:32,797 --> 06:47:36,000 and here we have a variable called 8948 06:47:36,000 --> 06:47:41,040 and we're going to try and use a few 8949 06:47:38,159 --> 06:47:43,360 math functions on it so we can use sdd 8950 06:47:43,360 --> 06:47:49,360 and std seal to round up so weighted 8951 06:47:46,718 --> 06:47:51,920 rounded to floor is going to be printed 8952 06:47:49,360 --> 06:47:53,360 out and way to round it to seal is going 8953 06:47:53,360 --> 06:47:57,440 we're going to have a number here 8954 06:47:55,439 --> 06:47:59,840 in this savings variable it's going to 8955 06:47:57,439 --> 06:48:02,239 be a negative number and we're going to 8956 06:47:59,840 --> 06:48:04,319 try and compute the absolute value of 8957 06:48:02,240 --> 06:48:06,478 this we're going to see it printed out 8958 06:48:06,477 --> 06:48:12,797 compute is the exponential of the value 8959 06:48:12,797 --> 06:48:16,797 and what this is going to do is going to 8960 06:48:16,797 --> 06:48:21,199 and raise it to the power of 10 and 8961 06:48:19,599 --> 06:48:23,119 that's what we're going to get printed 8962 06:48:21,200 --> 06:48:25,040 here the other thing we want to do here 8963 06:48:23,119 --> 06:48:26,957 is compute the power of a number we're 8964 06:48:25,040 --> 06:48:30,400 going to take 3 and raise it to the 8965 06:48:26,957 --> 06:48:33,039 power of 4 and take 9 and raise it to 8966 06:48:30,400 --> 06:48:35,680 the power of 3 and we're going to see 8967 06:48:33,040 --> 06:48:38,638 what these things evaluate to we're 8968 06:48:35,680 --> 06:48:40,957 going to open up our terminal here and 8969 06:48:38,637 --> 06:48:43,919 we're going to run the task to build 8970 06:48:40,957 --> 06:48:46,239 with gcc the world is going to be good 8971 06:48:43,919 --> 06:48:49,599 and if we run rooster we're going to get 8972 06:48:46,240 --> 06:48:51,120 these things here so 7.7 rounded to 8973 06:48:49,599 --> 06:48:54,399 floor is going to be seven so we're 8974 06:48:51,119 --> 06:48:56,477 going to round down 7.7 rounded to sale 8975 06:48:54,400 --> 06:48:59,600 is going to be eight and that's what 8976 06:48:56,477 --> 06:49:02,360 we're going to get and the absolute 8977 06:49:02,360 --> 06:49:09,279 7.7 because it is a positive number and 8978 06:49:06,000 --> 06:49:11,759 the absolute value of minus 5000 is 8979 06:49:09,279 --> 06:49:14,878 going to be 5 000. and again when you 8980 06:49:11,759 --> 06:49:17,919 compute the absolute value of something 8981 06:49:14,878 --> 06:49:21,119 you are interested in the weight of that 8982 06:49:17,919 --> 06:49:23,759 thing without considering the sign 8983 06:49:21,119 --> 06:49:25,599 without really worrying if it is 8984 06:49:23,759 --> 06:49:27,919 positive or negative you just want to 8985 06:49:25,599 --> 06:49:30,000 get the magnitude of a number that's 8986 06:49:27,919 --> 06:49:32,477 what we use in math to mean that 8987 06:49:30,000 --> 06:49:35,200 magnitude okay so if we go down we're 8988 06:49:32,477 --> 06:49:36,477 going to get the exponential of 10 is 8989 06:49:36,477 --> 06:49:42,079 and you're going to get that here and 8990 06:49:38,718 --> 06:49:45,440 again what this function does is take e 8991 06:49:42,080 --> 06:49:47,680 and raise it to the power of 10 8992 06:49:45,439 --> 06:49:50,079 and that's the answer here we can try 8993 06:49:47,680 --> 06:49:52,159 and prove this with a calculator so i am 8994 06:49:50,080 --> 06:49:54,478 going to open my windows calculator you 8995 06:49:52,159 --> 06:49:55,840 can use whatever calculator you have 8996 06:49:55,840 --> 06:50:02,400 i am going to use a scientific 8997 06:49:58,718 --> 06:50:04,718 calculator here and you see that we can 8998 06:50:02,400 --> 06:50:07,360 hit e and we're going to get the value 8999 06:50:04,718 --> 06:50:11,200 of this e number in math here it is 9000 06:50:07,360 --> 06:50:14,797 simplified but if i take it and raise it 9001 06:50:11,200 --> 06:50:18,360 to the power of 10 the answer is going 9002 06:50:19,279 --> 06:50:23,637 and if we look in our program we have 22 9003 06:50:23,637 --> 06:50:29,840 26.5 okay it is rounded up a little bit 9004 06:50:26,957 --> 06:50:32,557 but it is almost the same thing okay we 9005 06:50:29,840 --> 06:50:33,680 also want to play with log functions a 9006 06:50:33,680 --> 06:50:38,400 log is basically the reverse of the 9007 06:50:38,400 --> 06:50:41,520 and we just talked about this in the 9008 06:50:42,558 --> 06:50:48,240 if we do log 18 base 2 we are basically 9009 06:50:45,360 --> 06:50:50,000 asking to which power should we raise 2 9010 06:50:50,000 --> 06:50:53,680 and the answer to that is going to be 9011 06:50:53,680 --> 06:50:58,637 number in base number is going to 9012 06:50:55,840 --> 06:51:02,080 evaluate to the catch is that the log 9013 06:50:58,637 --> 06:51:05,200 function if you use it row like this it 9014 06:51:02,080 --> 06:51:08,320 is going to be using base e and we just 9015 06:51:05,200 --> 06:51:09,680 saw that e is 2 to the power of 7 or 9016 06:51:08,319 --> 06:51:12,558 something let's go back to our 9017 06:51:09,680 --> 06:51:14,718 calculator and see if we can see that 9018 06:51:12,558 --> 06:51:17,280 why not why try to guess when we have a 9019 06:51:14,718 --> 06:51:20,080 good calculator in our hands we are 9020 06:51:17,279 --> 06:51:22,399 using the scientific calculator so if we 9021 06:51:27,040 --> 06:51:31,760 we are basically asking to which power 9022 06:51:33,878 --> 06:51:38,477 54.59 and the answer is going to be 9023 06:51:38,477 --> 06:51:44,319 we have a version of this that is going 9024 06:51:41,119 --> 06:51:46,399 to use base 10 so if we use this 9025 06:51:44,319 --> 06:51:49,279 we are basically saying to which power 9026 06:51:46,400 --> 06:51:51,360 should we raise 10 to get 10 000 and the 9027 06:51:49,279 --> 06:51:53,759 answer is going to be printed out here 9028 06:51:51,360 --> 06:51:57,040 and it is going to be a four because 9029 06:51:53,759 --> 06:52:00,319 this is easy to do in your mind so let's 9030 06:51:57,040 --> 06:52:02,718 run this we're going to build the task 9031 06:52:00,319 --> 06:52:05,680 we are going to run the task to build 9032 06:52:02,718 --> 06:52:08,240 with gcc and we're going to run our 9033 06:52:05,680 --> 06:52:10,840 program and uh we're going to get our 9034 06:52:10,840 --> 06:52:16,957 54.59 you would elevate e to the power 9035 06:52:14,240 --> 06:52:18,638 of this number here this is the answer 9036 06:52:16,957 --> 06:52:20,717 and if you want you can prove this with 9037 06:52:18,637 --> 06:52:22,878 your favorite calculator you're going to 9038 06:52:20,718 --> 06:52:25,040 get something close to this to get 10 9039 06:52:22,878 --> 06:52:27,360 000 you'd need to elevate 10 to the 9040 06:52:25,040 --> 06:52:30,000 power of 4. that's what we're going to 9041 06:52:27,360 --> 06:52:32,558 get in here let's print this out again 9042 06:52:30,000 --> 06:52:35,599 many mistakes sorry but i hope you get 9043 06:52:32,558 --> 06:52:38,638 the point if we build again so that we 9044 06:52:35,599 --> 06:52:40,957 can see things properly and run we're 9045 06:52:38,637 --> 06:52:42,957 going to get proper data and this is 9046 06:52:40,957 --> 06:52:44,957 really what we expected to get we have a 9047 06:52:42,957 --> 06:52:47,759 few more functions we can play with as 9048 06:52:47,759 --> 06:52:53,439 let's play with square root and the std 9049 06:52:51,360 --> 06:52:56,000 round function here we are going to get 9050 06:52:53,439 --> 06:52:58,079 the square root of 81 we're going to get 9051 06:52:56,000 --> 06:53:01,439 it printed down and we're going to see 9052 06:52:58,080 --> 06:53:05,200 what std round does it is going to 9053 06:53:01,439 --> 06:53:08,557 round away from zero if we are at half 9054 06:53:05,200 --> 06:53:10,240 point and if we are not at half point if 9055 06:53:08,558 --> 06:53:12,958 we are below that we're going to round 9056 06:53:10,240 --> 06:53:14,798 down if we are higher than that we're 9057 06:53:12,957 --> 06:53:16,957 going to round up we're going to run 9058 06:53:14,797 --> 06:53:19,439 this so that you can see this running 9059 06:53:16,957 --> 06:53:23,119 and we're going to call rooster and the 9060 06:53:19,439 --> 06:53:26,477 2.5 is rounded to 3. 2.4 is rounded to 9061 06:53:23,119 --> 06:53:30,159 2. 3.6 is rounded to 4. this is what we 9062 06:53:26,477 --> 06:53:32,878 expect and the square root of 81 9063 06:53:30,159 --> 06:53:35,680 is 9. this is really all we set out to 9064 06:53:32,878 --> 06:53:37,760 do in this lecture to play with a few of 9065 06:53:37,759 --> 06:53:43,679 they are not really hard to use once you 9066 06:53:40,477 --> 06:53:46,079 have the basics of math nailed down and 9067 06:53:43,680 --> 06:53:48,319 again if you want to check the full list 9068 06:53:46,080 --> 06:53:50,000 of these functions please come to our 9069 06:53:48,319 --> 06:53:52,239 reference documentation here you're 9070 06:53:52,240 --> 06:53:57,600 and if you want to see how to use one of 9071 06:53:54,797 --> 06:53:59,439 these for example if you click on log 10 9072 06:53:57,599 --> 06:54:00,957 here you're going to come here you're 9073 06:53:59,439 --> 06:54:03,119 going to get an explanation of the 9074 06:54:00,957 --> 06:54:05,359 function and even better you're going to 9075 06:54:03,119 --> 06:54:08,000 go down and get a nice piece of code you 9076 06:54:05,360 --> 06:54:09,760 can play with and really make sense of 9077 06:54:08,000 --> 06:54:11,680 what is happening in these functions 9078 06:54:09,759 --> 06:54:13,199 here this is really all we set out to do 9079 06:54:11,680 --> 06:54:15,360 in this lecture i hope you found it 9080 06:54:13,200 --> 06:54:17,280 interesting we are going to stop here in 9081 06:54:15,360 --> 06:54:21,040 this lecture the next one we're going to 9082 06:54:17,279 --> 06:54:23,439 learn about some weird integral types 9083 06:54:21,040 --> 06:54:25,200 go ahead and finish up here and meet me 9084 06:54:23,439 --> 06:54:28,159 there in this lecture we're going to 9085 06:54:25,200 --> 06:54:30,958 learn about some weird integral types 9086 06:54:28,159 --> 06:54:33,680 and what do i mean by this well if you 9087 06:54:30,957 --> 06:54:36,239 look here integral types less than 4 9088 06:54:33,680 --> 06:54:39,439 bytes in size don't support arithmetic 9089 06:54:36,240 --> 06:54:41,680 operations and these are operations like 9090 06:54:39,439 --> 06:54:44,399 addition subtraction multiplication 9091 06:54:41,680 --> 06:54:45,760 division you can do these operations on 9092 06:54:45,759 --> 06:54:51,840 and here i have an example of some of 9093 06:54:48,400 --> 06:54:54,000 those types we have a car it is one byte 9094 06:54:54,000 --> 06:55:00,400 is two bytes in size on most processors 9095 06:54:57,360 --> 06:55:03,360 so you can't do arithmetic operations if 9096 06:55:00,400 --> 06:55:04,638 your integer value is stored in these 9097 06:55:04,637 --> 06:55:08,477 why is that well this has to do with 9098 06:55:08,477 --> 06:55:12,797 and they decided to choose and as the 9099 06:55:12,797 --> 06:55:17,680 integral type for which they can do 9100 06:55:15,040 --> 06:55:19,760 these arithmetic operations but 9101 06:55:17,680 --> 06:55:22,080 compilers are really smart enough to 9102 06:55:19,759 --> 06:55:24,717 notice if you are trying to do 9103 06:55:22,080 --> 06:55:27,120 arithmetic operations on these types and 9104 06:55:24,718 --> 06:55:28,319 they are going to implicitly convert 9105 06:55:30,957 --> 06:55:35,759 and this is something you need to be 9106 06:55:32,400 --> 06:55:39,200 aware of if we look at this program here 9107 06:55:35,759 --> 06:55:42,239 for example we have var1 and var2 they 9108 06:55:39,200 --> 06:55:43,600 are stored in variables that are of type 9109 06:55:43,599 --> 06:55:50,000 and we have var 3 var 4 which are stored 9110 06:55:46,878 --> 06:55:52,319 in a variable of type car we can try and 9111 06:55:50,000 --> 06:55:54,878 print the resizes and we're going to get 9112 06:55:52,319 --> 06:55:57,200 two two one one for these variables 9113 06:55:57,200 --> 06:56:02,240 occupies two bytes in memory for my 9114 06:55:59,599 --> 06:56:05,919 computer here and the car occupies one 9115 06:56:02,240 --> 06:56:06,878 byte in memory but the catch is down 9116 06:56:08,878 --> 06:56:13,440 the result is not going to be the same 9117 06:56:10,637 --> 06:56:14,797 type as defense you added up and you 9118 06:56:15,840 --> 06:56:20,400 to deduce to the same type as var one 9119 06:56:20,400 --> 06:56:26,000 and it's not resort is going to be an 9120 06:56:22,797 --> 06:56:27,919 integer because and is the smallest type 9121 06:56:26,000 --> 06:56:30,637 for which we can support these 9122 06:56:27,919 --> 06:56:32,557 arithmetic operations so if we print the 9123 06:56:30,637 --> 06:56:34,957 size of result one and reserved two 9124 06:56:32,558 --> 06:56:37,200 we're going to get four but this is the 9125 06:56:34,957 --> 06:56:39,759 behavior you're going to get and we need 9126 06:56:37,200 --> 06:56:42,319 to be aware of it the same behavior can 9127 06:56:39,759 --> 06:56:44,797 be observed on other operators that we 9128 06:56:42,319 --> 06:56:47,200 will have a chance to look at in a few 9129 06:56:44,797 --> 06:56:49,360 chapters ahead and these are called 9130 06:56:47,200 --> 06:56:51,680 shift operators you use them to shift 9131 06:56:49,360 --> 06:56:53,200 bets but i don't want to talk anymore 9132 06:56:51,680 --> 06:56:55,760 about them because we're going to have a 9133 06:56:53,200 --> 06:56:58,400 chance to learn about them in detail 9134 06:56:55,759 --> 06:57:00,557 okay now is time to head to visual 9135 06:56:58,400 --> 06:57:03,600 studio code and actually see this in 9136 06:57:00,558 --> 06:57:05,680 action here we are in our working 9137 06:57:03,599 --> 06:57:07,680 directory we are going to copy our 9138 06:57:05,680 --> 06:57:08,558 template project we're going to copy 9139 06:57:08,558 --> 06:57:14,638 quickly put that in our folder on weird 9140 06:57:12,637 --> 06:57:16,637 integral types this is our current 9141 06:57:14,637 --> 06:57:18,637 project here and we're going to open 9142 06:57:16,637 --> 06:57:20,797 this up in visual studio code we're 9143 06:57:18,637 --> 06:57:23,520 going to open this up and we're going to 9144 06:57:20,797 --> 06:57:25,759 open our main cpp file remove whatever 9145 06:57:23,520 --> 06:57:28,319 it is we don't need and we are going to 9146 06:57:25,759 --> 06:57:30,557 put in our code this must be very 9147 06:57:28,319 --> 06:57:31,919 familiar because it is what we just saw 9148 06:57:31,919 --> 06:57:36,797 so what we are doing here we have four 9149 06:57:34,558 --> 06:57:39,520 variables var one and var two are short 9150 06:57:36,797 --> 06:57:43,039 and so these are going to take two bytes 9151 06:57:39,520 --> 06:57:45,200 in memory on my computer here and uh car 9152 06:57:43,040 --> 06:57:46,798 is going to take up one byte and we're 9153 06:57:45,200 --> 06:57:48,878 going to print their sizes we're going 9154 06:57:46,797 --> 06:57:51,360 to see them printed down and we are 9155 06:57:48,878 --> 06:57:54,000 adding things up here we are taking two 9156 06:57:51,360 --> 06:57:56,159 short end variables and adding them up 9157 06:57:54,000 --> 06:57:58,080 so what the compiler is going to do it's 9158 06:57:56,159 --> 06:58:00,319 going to take this turn it into an end 9159 06:57:58,080 --> 06:58:02,558 and then do addition and the result is 9160 06:58:00,319 --> 06:58:05,360 going to be an end the same story is 9161 06:58:02,558 --> 06:58:09,520 going to happen for reserved 2 here 9162 06:58:05,360 --> 06:58:12,159 var 3 is a car so it is one byte in 9163 06:58:09,520 --> 06:58:14,878 memory but the compiler can't really add 9164 06:58:12,159 --> 06:58:17,279 up integral types which are smaller than 9165 06:58:14,878 --> 06:58:18,797 four bytes in memory so what is going to 9166 06:58:17,279 --> 06:58:22,159 happen var three is going to be 9167 06:58:18,797 --> 06:58:24,878 transformed implicitly behind the scenes 9168 06:58:22,159 --> 06:58:26,637 to a net and var four is going to be 9169 06:58:24,878 --> 06:58:28,797 transformed to a net and then we're 9170 06:58:26,637 --> 06:58:31,039 going to add that up and we're going to 9171 06:58:28,797 --> 06:58:32,477 store that in a neat variable which is 9172 06:58:32,477 --> 06:58:37,279 if we print this up we're going to see 9173 06:58:34,080 --> 06:58:40,400 that size of result 1 and result 2 is a 9174 06:58:37,279 --> 06:58:42,557 4 and this is what we expect here and 9175 06:58:40,400 --> 06:58:44,878 this is a behavior you need to be aware 9176 06:58:42,558 --> 06:58:47,600 of because sometimes your application 9177 06:58:44,878 --> 06:58:50,080 logic is going to depend on the size of 9178 06:58:47,599 --> 06:58:51,119 data if that's the case you need to care 9179 06:58:51,119 --> 06:58:55,360 and if you don't know this you're going 9180 06:58:52,637 --> 06:58:58,718 to be beaten by this and it is really 9181 06:58:55,360 --> 06:59:01,040 hard to find problems like this 9182 06:58:58,718 --> 06:59:03,600 so what we can do is open up our 9183 06:59:01,040 --> 06:59:06,477 terminal and we're going to build this 9184 06:59:03,599 --> 06:59:09,039 with gcc as we usually do and that we 9185 06:59:06,477 --> 06:59:10,957 can run our program rooster and we're 9186 06:59:09,040 --> 06:59:14,477 going to see that this is what we expect 9187 06:59:10,957 --> 06:59:16,797 var1 is two bytes in size var2 is two 9188 06:59:14,477 --> 06:59:19,680 bytes in size wire three and waterfall 9189 06:59:16,797 --> 06:59:21,919 are one byte in size but our result is 9190 06:59:19,680 --> 06:59:24,080 four bytes because the compiler 9191 06:59:24,080 --> 06:59:30,160 our operands here to x to be able to 9192 06:59:27,279 --> 06:59:31,599 carry out this arithmetic operation this 9193 06:59:30,159 --> 06:59:34,000 is really all we setup to do in this 9194 06:59:31,599 --> 06:59:35,359 lecture i hope you found it interesting 9195 06:59:34,000 --> 06:59:37,520 we are going to stop here in this 9196 06:59:35,360 --> 06:59:41,240 lecture the next one we're going to try 9197 06:59:37,520 --> 06:59:43,520 and recap what we saw in the chapter 9198 06:59:41,240 --> 06:59:46,080 congratulations on hitting the end of 9199 06:59:43,520 --> 06:59:47,360 this chapter and this chapter was really 9200 06:59:47,360 --> 06:59:53,040 doing operations and manipulating the 9201 06:59:50,319 --> 06:59:55,279 data that you have stored in your 9202 06:59:53,040 --> 06:59:57,920 variables the first thing we saw was 9203 06:59:55,279 --> 06:59:59,919 that we could do arithmetic operations 9204 06:59:57,919 --> 07:00:02,877 on the data we can add things up 9205 06:59:59,919 --> 07:00:05,039 multiply divide we also saw the modulus 9206 07:00:02,878 --> 07:00:07,119 operator which may seem weird for 9207 07:00:05,040 --> 07:00:09,440 beginners but we had a chance to really 9208 07:00:07,119 --> 07:00:11,759 play with us after that we learned about 9209 07:00:09,439 --> 07:00:13,759 precedence and associativity we saw that 9210 07:00:13,759 --> 07:00:18,557 we have to follow to know which 9211 07:00:16,240 --> 07:00:22,240 operation to do first if we have 9212 07:00:18,558 --> 07:00:24,718 multiple operators in our expression we 9213 07:00:22,240 --> 07:00:26,958 also had a chance to learn about prefix 9214 07:00:24,718 --> 07:00:29,360 and postfix increment and decrement 9215 07:00:26,957 --> 07:00:32,477 operators we had a chance to do compound 9216 07:00:29,360 --> 07:00:35,600 assignments things like plus equal minus 9217 07:00:32,477 --> 07:00:37,840 equal multiply equal modulus equal now 9218 07:00:35,599 --> 07:00:39,919 these things must be making sense to you 9219 07:00:37,840 --> 07:00:42,159 we had a chance to look at how we could 9220 07:00:39,919 --> 07:00:45,119 compare things using relational 9221 07:00:42,159 --> 07:00:47,840 operators like greater than lesser than 9222 07:00:45,119 --> 07:00:50,239 and equal or not equal we also had a 9223 07:00:47,840 --> 07:00:53,680 chance to look at output formatting and 9224 07:00:50,240 --> 07:00:57,360 we saw a host of things we could do to 9225 07:00:53,680 --> 07:00:59,279 make data show up better with sddc out 9226 07:00:57,360 --> 07:01:02,080 after that we learned about numeric 9227 07:00:59,279 --> 07:01:04,477 limits and this is a cool way to know 9228 07:01:02,080 --> 07:01:06,878 the ranges for your data types but there 9229 07:01:04,477 --> 07:01:09,599 are a host of other properties you can 9230 07:01:06,878 --> 07:01:11,600 use in this limits library we learned a 9231 07:01:09,599 --> 07:01:13,840 little bit about math functions and we 9232 07:01:11,599 --> 07:01:16,797 played with things like the power of 9233 07:01:13,840 --> 07:01:18,797 something the exponential the few 9234 07:01:16,797 --> 07:01:21,360 rounding functions that we have in the 9235 07:01:18,797 --> 07:01:24,159 cmat library and now you must have a 9236 07:01:21,360 --> 07:01:26,400 good idea about these functions that you 9237 07:01:24,159 --> 07:01:29,040 can use to manipulate and do math on 9238 07:01:26,400 --> 07:01:32,240 your variables we ended the chapter by 9239 07:01:29,040 --> 07:01:34,718 looking at some weird integral types and 9240 07:01:32,240 --> 07:01:37,840 the main message in that lecture was 9241 07:01:34,718 --> 07:01:41,040 that you can't do arithmetic operations 9242 07:01:37,840 --> 07:01:43,439 on data types whose size is less than 4 9243 07:01:41,040 --> 07:01:45,760 bytes in memory if you try to do that 9244 07:01:43,439 --> 07:01:48,319 the compiler is going to insert 9245 07:01:45,759 --> 07:01:50,717 implicit conversions and if your 9246 07:01:48,319 --> 07:01:51,840 application depends on the size of 9247 07:01:51,840 --> 07:01:55,840 this is something you need to be careful 9248 07:01:53,599 --> 07:01:57,199 about otherwise you're going to have a 9249 07:01:57,200 --> 07:02:02,000 finding these problems i would like to 9250 07:01:59,439 --> 07:02:04,319 welcome you in this new chapter where 9251 07:02:02,000 --> 07:02:06,718 we're going to be learning about a new 9252 07:02:04,319 --> 07:02:09,200 way we can do things in c plus plus and 9253 07:02:06,718 --> 07:02:11,600 we're going to be doing conditional 9254 07:02:09,200 --> 07:02:14,637 programming in other words we're going 9255 07:02:11,599 --> 07:02:17,199 to be able to do different things 9256 07:02:14,637 --> 07:02:19,360 based on the conditions that we have set 9257 07:02:17,200 --> 07:02:21,360 up in our code just to give you an 9258 07:02:19,360 --> 07:02:23,680 example here we have a few variables 9259 07:02:21,360 --> 07:02:26,558 they are booleans and we have a few 9260 07:02:23,680 --> 07:02:29,040 initializers in there red is false green 9261 07:02:26,558 --> 07:02:31,040 is true yellow is false police stop is 9262 07:02:31,040 --> 07:02:35,600 different things based on discipline 9263 07:02:40,000 --> 07:02:44,957 if the light is yellow we might be told 9264 07:02:42,319 --> 07:02:47,439 to slow down if the light is green we 9265 07:02:44,957 --> 07:02:49,199 might be told to go and do whatever it 9266 07:02:47,439 --> 07:02:51,599 is we want to do so we're going to be 9267 07:02:49,200 --> 07:02:53,920 able to do these kinds of things and c 9268 07:02:51,599 --> 07:02:56,477 plus plus provides different constructs 9269 07:02:53,919 --> 07:02:57,599 to be able to do conditional programming 9270 07:02:57,599 --> 07:03:02,957 code we just have one of them in this 9271 07:03:00,080 --> 07:03:05,600 slide which is this if statement here 9272 07:03:02,957 --> 07:03:07,840 this if keyword allows you to do things 9273 07:03:05,599 --> 07:03:09,759 like this we have a few others we have 9274 07:03:07,840 --> 07:03:12,558 the else keyword we have the switch 9275 07:03:09,759 --> 07:03:15,599 keyword we also have a completely 9276 07:03:12,558 --> 07:03:18,080 separate operator that is called ternary 9277 07:03:15,599 --> 07:03:19,680 operator that allows you to do these 9278 07:03:18,080 --> 07:03:22,080 kinds of things and we're going to be 9279 07:03:19,680 --> 07:03:24,000 learning about all these things and more 9280 07:03:22,080 --> 07:03:26,240 in this chapter we are going to start 9281 07:03:24,000 --> 07:03:27,520 and learn about the if statement in the 9282 07:03:27,520 --> 07:03:30,637 go ahead and finish up here and meet me 9283 07:03:30,637 --> 07:03:34,797 in this lecture we're going to learn 9284 07:03:32,080 --> 07:03:36,878 about the if statement and this is a 9285 07:03:34,797 --> 07:03:39,680 statement that is going to allow you to 9286 07:03:36,878 --> 07:03:40,558 do things based on some condition being 9287 07:03:40,558 --> 07:03:45,440 or false here is a simple code example 9288 07:03:43,680 --> 07:03:46,558 we have two variables number one and 9289 07:03:46,558 --> 07:03:51,280 and we have a statement here that is 9290 07:03:48,797 --> 07:03:53,680 going to compare number one and number 9291 07:03:51,279 --> 07:03:56,477 two and we're going to store the result 9292 07:03:56,477 --> 07:04:01,040 that we have on the left then we're 9293 07:03:58,319 --> 07:04:04,159 going to do something based on the value 9294 07:04:01,040 --> 07:04:05,840 that we are storing in this result here 9295 07:04:04,159 --> 07:04:07,200 if the result in there is true we're 9296 07:04:07,200 --> 07:04:11,600 number one is less than number two if 9297 07:04:09,759 --> 07:04:14,477 the result is not true we're going to 9298 07:04:11,599 --> 07:04:17,519 say number one is not less than 9299 07:04:14,477 --> 07:04:20,239 number two and notice in the first if 9300 07:04:17,520 --> 07:04:23,279 close year we are testing for the case 9301 07:04:20,240 --> 07:04:25,520 where the result is true but sometimes 9302 07:04:23,279 --> 07:04:28,557 we need to do something when this 9303 07:04:25,520 --> 07:04:31,040 condition here is not true one way to do 9304 07:04:28,558 --> 07:04:34,159 that is to negate what we have here 9305 07:04:31,040 --> 07:04:37,280 inside the if parenthesis and test for 9306 07:04:34,159 --> 07:04:39,599 the reverse of whatever we have in here 9307 07:04:37,279 --> 07:04:42,319 if that is making any sense if you look 9308 07:04:39,599 --> 07:04:45,759 here we are testing for the case where 9309 07:04:42,319 --> 07:04:47,759 if not the result is a true and in other 9310 07:04:45,759 --> 07:04:50,000 words we are testing for the case where 9311 07:04:47,759 --> 07:04:52,877 the result here is false and we're going 9312 07:04:50,000 --> 07:04:54,718 to say number one is not less than 9313 07:04:52,878 --> 07:04:57,279 number two this is one way we can go 9314 07:04:54,718 --> 07:04:59,600 about this again the if statement is 9315 07:04:57,279 --> 07:05:01,840 used to do conditional programming this 9316 07:04:59,599 --> 07:05:04,159 is the syntax we use to do it in c plus 9317 07:05:01,840 --> 07:05:06,957 plus we say f we put a set of 9318 07:05:04,159 --> 07:05:10,000 parentheses and inside the parenthesis 9319 07:05:06,957 --> 07:05:11,759 we put the condition we want to test for 9320 07:05:10,000 --> 07:05:14,718 and after that we're going to have a 9321 07:05:11,759 --> 07:05:17,359 pair of curly braces and what we have 9322 07:05:14,718 --> 07:05:20,878 inside these curly braces is going to be 9323 07:05:17,360 --> 07:05:22,958 the body of our if statement and it is 9324 07:05:20,878 --> 07:05:26,558 basically going to be the code we want 9325 07:05:22,957 --> 07:05:28,877 to run if the test here is successful we 9326 07:05:26,558 --> 07:05:31,280 can also use an else clause to catch the 9327 07:05:28,878 --> 07:05:33,760 case where the test hasn't been 9328 07:05:31,279 --> 07:05:35,919 successful in this case we can say if 9329 07:05:33,759 --> 07:05:37,119 result equals true we're going to do 9330 07:05:37,119 --> 07:05:42,239 and else we're going to do something 9331 07:05:39,759 --> 07:05:44,797 else in other words if research is true 9332 07:05:42,240 --> 07:05:46,878 we're going to say is less than if it's 9333 07:05:44,797 --> 07:05:49,360 not true we're going to say is not less 9334 07:05:46,878 --> 07:05:52,637 than and this is a more compact way of 9335 07:05:49,360 --> 07:05:54,718 doing what we just did in the slide here 9336 07:05:52,637 --> 07:05:57,680 you can also directly use your 9337 07:05:54,718 --> 07:06:00,558 expression as a condition and the 9338 07:05:57,680 --> 07:06:03,360 requirement here is that the expression 9339 07:06:00,558 --> 07:06:05,680 evaluates to something that we can treat 9340 07:06:03,360 --> 07:06:08,319 like a boolean because the condition has 9341 07:06:05,680 --> 07:06:10,477 to be a boolean it has to be true or 9342 07:06:08,319 --> 07:06:12,319 false okay here is another simple 9343 07:06:10,477 --> 07:06:14,878 example we have seen this in the last 9344 07:06:12,319 --> 07:06:18,000 lecture we have a few variables 9345 07:06:14,878 --> 07:06:20,797 containing our conditions and we are 9346 07:06:18,000 --> 07:06:22,400 saying if the light is red for example 9347 07:06:20,797 --> 07:06:24,239 we're going to do something if it's 9348 07:06:22,400 --> 07:06:26,400 yellow we're going to do something if 9349 07:06:24,240 --> 07:06:29,440 it's green we're going to do something 9350 07:06:26,400 --> 07:06:32,319 we can also nest conditions for example 9351 07:06:32,319 --> 07:06:38,000 if statement and inside that if 9352 07:06:34,878 --> 07:06:40,718 statement set up other if statements and 9353 07:06:38,000 --> 07:06:42,319 if our logic needs something like this 9354 07:06:40,718 --> 07:06:44,400 you can do this and you can take 9355 07:06:42,319 --> 07:06:46,718 advantage of this to achieve whatever it 9356 07:06:44,400 --> 07:06:48,400 is you want to do in this case if the 9357 07:06:46,718 --> 07:06:50,159 light is green we're going to 9358 07:06:48,400 --> 07:06:52,000 fall in here and we're going to do 9359 07:06:50,159 --> 07:06:54,718 another condition so if the light is 9360 07:06:52,000 --> 07:06:57,040 green and if the police officer has 9361 07:06:54,718 --> 07:06:58,637 stopped you you're going to stop and if 9362 07:06:57,040 --> 07:07:00,718 they haven't stopped you you're going to 9363 07:06:58,637 --> 07:07:02,557 go because the light is green and you 9364 07:07:02,558 --> 07:07:06,958 we can also use logical operators to 9365 07:07:04,718 --> 07:07:09,920 kind of achieve the same thing and we 9366 07:07:06,957 --> 07:07:11,599 say if the light is green and the police 9367 07:07:09,919 --> 07:07:14,319 officer hasn't stopped you you're going 9368 07:07:11,599 --> 07:07:16,319 to go else you're going to stop and you 9369 07:07:14,319 --> 07:07:18,477 can do something like this and you see 9370 07:07:16,319 --> 07:07:21,360 that you can combine the logical 9371 07:07:18,477 --> 07:07:23,360 operators we've learned about with the 9372 07:07:21,360 --> 07:07:25,520 conditional programming techniques we 9373 07:07:23,360 --> 07:07:27,119 are learning about here to do really 9374 07:07:25,520 --> 07:07:29,520 powerful stuff and we're going to be 9375 07:07:27,119 --> 07:07:31,759 doing this all over the place in the 9376 07:07:29,520 --> 07:07:34,240 course so we're going to head over to 9377 07:07:31,759 --> 07:07:36,797 visual studio code and play with us here 9378 07:07:34,240 --> 07:07:39,600 we are in our working directory we're 9379 07:07:36,797 --> 07:07:42,637 going to be working on if statements and 9380 07:07:39,599 --> 07:07:45,119 we're going to grab our template project 9381 07:07:42,637 --> 07:07:47,360 the template files i should say and 9382 07:07:45,119 --> 07:07:49,119 we're going to put them in here 9383 07:07:47,360 --> 07:07:51,600 and we're going to open this up in 9384 07:07:49,119 --> 07:07:54,079 visual studio code as we always do so 9385 07:07:51,599 --> 07:07:57,199 let's do that open folder and we should 9386 07:07:54,080 --> 07:07:59,200 fall in here and open our main cpp file 9387 07:07:57,200 --> 07:08:00,477 let's do the usual and remove what we 9388 07:08:00,477 --> 07:08:04,797 and we are going to put in our code so 9389 07:08:02,797 --> 07:08:07,039 that we can really play with us we have 9390 07:08:04,797 --> 07:08:09,840 two variables in here number one and 9391 07:08:07,040 --> 07:08:14,240 number two they are integers and we have 9392 07:08:09,840 --> 07:08:16,957 the values 55 and 60 inside we can do a 9393 07:08:14,240 --> 07:08:18,958 comparison between these two variables 9394 07:08:16,957 --> 07:08:22,000 and what this comparison is going to 9395 07:08:18,957 --> 07:08:24,557 yield is a boolean value that we're 9396 07:08:22,000 --> 07:08:26,637 going to store in our boolean variable 9397 07:08:24,558 --> 07:08:29,200 here which is called result 9398 07:08:26,637 --> 07:08:31,360 and what we can do is use this to do 9399 07:08:29,200 --> 07:08:33,280 some conditional programming so we're 9400 07:08:31,360 --> 07:08:35,760 going to say if the result is true we're 9401 07:08:33,279 --> 07:08:38,079 going to do something and if the result 9402 07:08:35,759 --> 07:08:40,557 is not true we're going to do something 9403 07:08:38,080 --> 07:08:43,440 else and this is how we say it this 9404 07:08:40,558 --> 07:08:47,040 syntax here may be confusing to new 9405 07:08:43,439 --> 07:08:49,520 students but try to think of it if not 9406 07:08:47,040 --> 07:08:53,040 research is true and that's really the 9407 07:08:49,520 --> 07:08:55,040 same thing as saying if result is false 9408 07:08:53,040 --> 07:08:56,240 try to stream this in your mind a little 9409 07:08:55,040 --> 07:08:58,319 bit and you really are going to 9410 07:08:56,240 --> 07:09:00,320 understand okay so now that you have 9411 07:09:01,599 --> 07:09:05,279 guess what we're going to see if we run 9412 07:09:03,599 --> 07:09:07,919 this what are we going to see on the 9413 07:09:05,279 --> 07:09:10,000 console okay give it a try so we're 9414 07:09:07,919 --> 07:09:12,319 going to run this in visual studio code 9415 07:09:10,000 --> 07:09:14,957 let's open up a terminal first 9416 07:09:12,319 --> 07:09:16,319 and we're going to world with gcc as we 9417 07:09:16,319 --> 07:09:20,319 we're going to bring this up a little 9418 07:09:17,840 --> 07:09:22,477 bit so that we have some breathing room 9419 07:09:20,319 --> 07:09:24,319 and we're going to run rooster let's see 9420 07:09:22,477 --> 07:09:27,279 what we have here we have reserved 9421 07:09:24,319 --> 07:09:28,797 equals true and that's coming from this 9422 07:09:28,797 --> 07:09:34,079 and we have freestanding if statement 9423 07:09:34,080 --> 07:09:39,440 here and we have our meat of this 9424 07:09:36,878 --> 07:09:43,840 program what we really are interested in 9425 07:09:39,439 --> 07:09:47,119 we have 55 is less than 60. so our test 9426 07:09:43,840 --> 07:09:50,718 here succeeded and we fell in this body 9427 07:09:47,119 --> 07:09:52,957 and executed this sddc out statement 9428 07:09:50,718 --> 07:09:54,080 that's why we're saying number one is 9429 07:09:54,080 --> 07:10:00,240 number two and number one is 55 which is 9430 07:09:56,878 --> 07:10:02,477 what we see here and number two is 60 9431 07:10:00,240 --> 07:10:05,520 which is what we are seeing here 9432 07:10:05,520 --> 07:10:11,040 the second statement here execute you 9433 07:10:08,319 --> 07:10:13,520 know it is testing for the case where 9434 07:10:11,040 --> 07:10:16,159 not result is true so it's going to 9435 07:10:13,520 --> 07:10:17,520 exactly do the reverse of what this test 9436 07:10:17,520 --> 07:10:22,159 and if this fails this one is going to 9437 07:10:19,680 --> 07:10:23,760 succeed which is what we have now so 9438 07:10:22,159 --> 07:10:26,718 what we can do to really play with this 9439 07:10:23,759 --> 07:10:29,199 we can go up and change for example 9440 07:10:29,200 --> 07:10:34,159 this is going to flip the order of these 9441 07:10:31,599 --> 07:10:37,439 numbers if i may say it like that and if 9442 07:10:34,159 --> 07:10:39,040 we run it we're going to build with gcc 9443 07:10:39,040 --> 07:10:45,200 we're going to see result is false so 9444 07:10:42,240 --> 07:10:47,520 result became false because number one 9445 07:10:45,200 --> 07:10:50,878 is not less than number two so this is 9446 07:10:47,520 --> 07:10:53,119 going to be false and the test here is 9447 07:10:50,878 --> 07:10:56,000 going to fail result is not true so 9448 07:10:53,119 --> 07:10:59,039 we're not going to go in here and look 9449 07:10:56,000 --> 07:11:01,040 at here if not result is true so 9450 07:11:01,040 --> 07:11:06,080 if we negate this this is going to 9451 07:11:03,360 --> 07:11:08,477 become true and this test here is going 9452 07:11:06,080 --> 07:11:10,798 to succeed and we are going to fall in 9453 07:11:08,477 --> 07:11:13,040 here take some time to really understand 9454 07:11:10,797 --> 07:11:14,878 this and if you have a problem ask me i 9455 07:11:13,040 --> 07:11:17,200 am going to do the best i can to help 9456 07:11:14,878 --> 07:11:19,680 you out but make sure you understand 9457 07:11:17,200 --> 07:11:22,000 this because this is really fundamental 9458 07:11:19,680 --> 07:11:24,878 in your journey as a software developer 9459 07:11:22,000 --> 07:11:26,558 not just in c plus plus any career as a 9460 07:11:24,878 --> 07:11:28,718 software developer you're going to need 9461 07:11:26,558 --> 07:11:31,040 to understand this okay what we're going 9462 07:11:31,040 --> 07:11:34,638 what we just did here because we're 9463 07:11:32,718 --> 07:11:37,119 going to see another way to do this and 9464 07:11:34,637 --> 07:11:39,439 we don't want to have noise output in 9465 07:11:37,119 --> 07:11:42,079 our terminal this is going to just make 9466 07:11:39,439 --> 07:11:43,359 things hard for us to see so what we're 9467 07:11:43,360 --> 07:11:47,600 we're going to go down in here and put 9468 07:11:47,599 --> 07:11:51,359 the result was commented out we don't 9469 07:11:49,919 --> 07:11:53,199 want to comment that out so we're going 9470 07:11:51,360 --> 07:11:55,600 to take this out because this is our 9471 07:11:53,200 --> 07:11:58,400 condition we're going to grab it and put 9472 07:11:55,599 --> 07:12:00,717 that outside our comment section this is 9473 07:11:58,400 --> 07:12:02,958 going to do and if we go down again 9474 07:12:00,718 --> 07:12:05,040 we're going to click on this file icon 9475 07:12:02,957 --> 07:12:06,797 and uh give ourselves some more 9476 07:12:05,040 --> 07:12:09,360 breathing room here we don't really need 9477 07:12:06,797 --> 07:12:11,520 to see that main cpp file anyway 9478 07:12:09,360 --> 07:12:12,477 so if we come here we can use the else 9479 07:12:12,477 --> 07:12:15,919 to test for the case where our condition 9480 07:12:15,919 --> 07:12:20,159 and if you look here we are essentially 9481 07:12:18,159 --> 07:12:22,558 saying the same thing we are saying if 9482 07:12:20,159 --> 07:12:25,520 result equals true we're going to fall 9483 07:12:22,558 --> 07:12:27,840 in here and else we're going to fall in 9484 07:12:25,520 --> 07:12:30,240 here so we don't need to do the double 9485 07:12:27,840 --> 07:12:32,797 if statements that we just did 9486 07:12:30,240 --> 07:12:34,798 here and this is much more compact and 9487 07:12:32,797 --> 07:12:37,039 easy to read let's look again at the 9488 07:12:34,797 --> 07:12:39,759 numbers we have here number one is 65 9489 07:12:37,040 --> 07:12:43,360 number 2 is 60. this expression here is 9490 07:12:39,759 --> 07:12:45,840 going to be false because 65 is not less 9491 07:12:43,360 --> 07:12:48,400 than 60 we're going to have a false in 9492 07:12:48,400 --> 07:12:52,319 result is going to be false this is 9493 07:12:50,159 --> 07:12:54,477 going to fail and we are going to fall 9494 07:12:55,840 --> 07:13:02,637 or 65 is not less than 60 which is what 9495 07:12:59,840 --> 07:13:04,957 we have in our values so we're going to 9496 07:13:02,637 --> 07:13:06,878 build this again and we're going to run 9497 07:13:04,957 --> 07:13:08,477 rooster that's clear so that we have 9498 07:13:06,878 --> 07:13:11,520 some breathing room we're going to run 9499 07:13:08,477 --> 07:13:13,200 rooster and you're going to see that 65 9500 07:13:13,200 --> 07:13:17,680 60 which is what we expect again if you 9501 07:13:15,759 --> 07:13:19,919 go up and change these numbers to 9502 07:13:17,680 --> 07:13:22,637 whatever you want try to change these 9503 07:13:19,919 --> 07:13:25,599 numbers to different values to see what 9504 07:13:22,637 --> 07:13:27,279 you get as output if we do this we're 9505 07:13:25,599 --> 07:13:30,000 going to see that number one is less 9506 07:13:27,279 --> 07:13:31,840 than 60 so this is going to be true 9507 07:13:30,000 --> 07:13:34,558 and we're going to have a true in this 9508 07:13:31,840 --> 07:13:37,200 result variable here if we go down 9509 07:13:34,558 --> 07:13:39,360 result is true this test condition here 9510 07:13:37,200 --> 07:13:42,159 is going to succeed and we are going to 9511 07:13:39,360 --> 07:13:44,000 fall in here let's click in our terminal 9512 07:13:42,159 --> 07:13:46,797 and hit enter we're going to run rooster 9513 07:13:44,000 --> 07:13:49,119 we're going to see 55 is less than 60 9514 07:13:46,797 --> 07:13:51,199 which is what we have here okay now i 9515 07:13:49,119 --> 07:13:54,159 hope you have a better idea of how you 9516 07:13:51,200 --> 07:13:56,159 can use the else close to make your if 9517 07:13:54,159 --> 07:13:58,319 statements really compact and this is 9518 07:13:56,159 --> 07:14:00,400 really cool okay another thing i want 9519 07:14:00,400 --> 07:14:05,760 not go through a variable like this and 9520 07:14:03,200 --> 07:14:07,680 use an expression as a condition 9521 07:14:05,759 --> 07:14:09,279 directly c plus plus allows you to do 9522 07:14:07,680 --> 07:14:11,360 this so what we're going to do we're 9523 07:14:09,279 --> 07:14:13,599 going to comment out what we just did 9524 07:14:11,360 --> 07:14:15,600 let's do that i'm going to use a block 9525 07:14:13,599 --> 07:14:16,557 comment i hope you know how to use this 9526 07:14:16,558 --> 07:14:21,040 and we're going to go down and put in 9527 07:14:18,477 --> 07:14:23,520 our code it is exactly the same thing 9528 07:14:21,040 --> 07:14:25,920 but we took the expression and put that 9529 07:14:23,520 --> 07:14:28,718 in the place of our condition variable 9530 07:14:25,919 --> 07:14:31,119 here because this is going to evaluate 9531 07:14:28,718 --> 07:14:33,440 to a boolean anyway so we can use this 9532 07:14:31,119 --> 07:14:36,477 as a condition and this is something 9533 07:14:33,439 --> 07:14:38,159 very legal to do in c plus plus so if we 9534 07:14:36,477 --> 07:14:39,680 run this we're going to see let's see 9535 07:14:38,159 --> 07:14:40,878 what we have in the numbers i have 9536 07:14:40,878 --> 07:14:46,637 by now so 55 number one sixty number two 9537 07:14:44,797 --> 07:14:48,319 number one is less than number two this 9538 07:14:48,319 --> 07:14:52,558 so the test here is going to succeed and 9539 07:14:51,040 --> 07:14:55,120 we are going to fall in here that's what 9540 07:14:52,558 --> 07:14:57,520 we're going to say we're going to weld 9541 07:14:57,520 --> 07:15:02,797 and i'm going to clear and run rooster 9542 07:15:01,279 --> 07:15:05,119 and you're going to see that this is the 9543 07:15:02,797 --> 07:15:07,039 output we expect if we change up the 9544 07:15:05,119 --> 07:15:10,718 numbers again let's take this and make 9545 07:15:07,040 --> 07:15:12,638 it a 75 number one so number one 9546 07:15:10,718 --> 07:15:15,600 is less than number two is going to 9547 07:15:12,637 --> 07:15:18,477 evaluate to false the test here is going 9548 07:15:15,599 --> 07:15:20,159 to fail and we are going to fall in this 9549 07:15:18,477 --> 07:15:23,520 block here we're going to fall in the 9550 07:15:20,159 --> 07:15:24,477 else close of our test here we're going 9551 07:15:27,279 --> 07:15:32,079 55 is that what we have in there it's a 9552 07:15:29,599 --> 07:15:34,239 60 but this block here is going to 9553 07:15:32,080 --> 07:15:36,080 execute that's the most important thing 9554 07:15:34,240 --> 07:15:39,360 so we're going to run this and we're 9555 07:15:36,080 --> 07:15:41,520 going to try and clear and run rooster 9556 07:15:39,360 --> 07:15:43,600 we're going to get what we expect this 9557 07:15:41,520 --> 07:15:46,000 is one way we can do this and it is 9558 07:15:43,599 --> 07:15:48,877 really cool let's comment this out and 9559 07:15:46,000 --> 07:15:50,240 see another example and if we go down we 9560 07:15:50,240 --> 07:15:56,798 the example we saw in the slides which 9561 07:16:00,558 --> 07:16:05,200 so what we really want to see here is 9562 07:16:02,957 --> 07:16:06,717 that we can nest stuff but before we do 9563 07:16:06,718 --> 07:16:11,840 try and use these conditions because it 9564 07:16:08,878 --> 07:16:14,718 is just fun so if red is true meaning 9565 07:16:11,840 --> 07:16:16,718 that the red light is on we're going to 9566 07:16:14,718 --> 07:16:19,040 stop if yellow is true we're going to 9567 07:16:16,718 --> 07:16:20,159 slow down if green is true we're going 9568 07:16:20,159 --> 07:16:26,240 and if we look at what we have here 9569 07:16:23,040 --> 07:16:28,638 red is false green is true false is true 9570 07:16:26,240 --> 07:16:30,878 and a police stop is true police stop is 9571 07:16:28,637 --> 07:16:32,957 not really being used so green is true 9572 07:16:30,878 --> 07:16:35,200 we're going to execute the green 9573 07:16:32,957 --> 07:16:37,199 statement here and we're going to say go 9574 07:16:35,200 --> 07:16:38,798 that's what we're going to say let's try 9575 07:16:38,797 --> 07:16:43,039 we're going to build with gcc 9576 07:16:41,279 --> 07:16:45,199 bring this up a little bit so that we 9577 07:16:47,360 --> 07:16:50,878 and run rooster and you see that it is 9578 07:16:50,878 --> 07:16:56,240 go here you can try and change these 9579 07:16:53,599 --> 07:16:58,477 boolean variables to different things 9580 07:16:56,240 --> 07:17:00,958 and see what is printed out here this is 9581 07:16:58,477 --> 07:17:02,637 a great way to learn but what we really 9582 07:17:02,637 --> 07:17:07,759 is nested statements and let's go down 9583 07:17:05,200 --> 07:17:09,920 and do that okay again we don't want 9584 07:17:07,759 --> 07:17:11,599 what we have on top here to disturb us 9585 07:17:11,599 --> 07:17:17,039 comment this out and we are going to go 9586 07:17:22,398 --> 07:17:28,080 is if the light is green we should go 9587 07:17:25,520 --> 07:17:30,477 but the police officer has the right to 9588 07:17:28,080 --> 07:17:32,638 stop us even if the light is green at 9589 07:17:30,477 --> 07:17:34,878 least these are the laws in the country 9590 07:17:32,637 --> 07:17:37,840 where i leave so we have a variable 9591 07:17:34,878 --> 07:17:40,000 called police stop here so if they stop 9592 07:17:37,840 --> 07:17:41,840 you even if the light is green you're 9593 07:17:40,000 --> 07:17:43,919 going to stop and we're going to see 9594 07:17:41,840 --> 07:17:45,840 what we see here so if the light is 9595 07:17:43,919 --> 07:17:49,039 green we're going to fall in this body 9596 07:17:45,840 --> 07:17:51,920 again and we're going to run this inside 9597 07:17:49,040 --> 07:17:53,520 or nested if statement and if the 9598 07:17:51,919 --> 07:17:54,397 officer has stopped us we're going to 9599 07:17:54,398 --> 07:17:59,200 if they haven't stopped us we're going 9600 07:17:56,000 --> 07:18:01,200 to go and go on our merry way this is 9601 07:17:59,200 --> 07:18:03,840 what we want to see here and again i 9602 07:18:01,200 --> 07:18:06,637 want you to stop for a moment try to run 9603 07:18:03,840 --> 07:18:09,040 this through your brain and try to come 9604 07:18:06,637 --> 07:18:11,439 up with the value we're going to see 9605 07:18:09,040 --> 07:18:12,878 when we run this in visual studio code 9606 07:18:15,040 --> 07:18:20,000 we're going to build with gcc 9607 07:18:17,599 --> 07:18:22,717 and we're going to run a program and you 9608 07:18:25,520 --> 07:18:31,680 the light is green okay so we we should 9609 07:18:28,637 --> 07:18:34,878 usually go but the police stop variable 9610 07:18:31,680 --> 07:18:37,040 here is also true so if we fall in here 9611 07:18:34,878 --> 07:18:39,920 this is going to execute and it is going 9612 07:18:37,040 --> 07:18:42,558 to succeed and we are going to say stop 9613 07:18:39,919 --> 07:18:45,119 and once this statement executes the 9614 07:18:42,558 --> 07:18:48,240 else statement is not going to execute 9615 07:18:45,119 --> 07:18:50,079 control is going to fall outside here 9616 07:18:48,240 --> 07:18:53,200 and we're going to keep doing whatever 9617 07:18:50,080 --> 07:18:56,160 we have outside this if statement here 9618 07:18:53,200 --> 07:18:57,119 this is really critical to understand if 9619 07:18:57,119 --> 07:19:03,039 block of a statement succeeds all the 9620 07:19:00,319 --> 07:19:05,680 others are not going to execute and try 9621 07:19:03,040 --> 07:19:07,680 to really understand this okay we have 9622 07:19:05,680 --> 07:19:10,718 seen this we can now comment this out 9623 07:19:07,680 --> 07:19:11,760 because we have something else i want to 9624 07:19:11,759 --> 07:19:16,159 we're going to comment this out and 9625 07:19:13,680 --> 07:19:18,080 that's that you can combine the if 9626 07:19:16,159 --> 07:19:21,040 statements here or conditional 9627 07:19:18,080 --> 07:19:23,440 programming with the logical operators 9628 07:19:21,040 --> 07:19:25,920 we have learned about earlier in the 9629 07:19:23,439 --> 07:19:28,159 course and do really powerful stuff for 9630 07:19:28,159 --> 07:19:33,599 not use this nested statement and do 9631 07:19:31,200 --> 07:19:35,920 this in one go like this and i think 9632 07:19:33,599 --> 07:19:38,319 this is much cleaner so we're going to 9633 07:19:35,919 --> 07:19:40,957 say if the light is green and the police 9634 07:19:38,319 --> 07:19:43,439 officer hasn't stopped us we're going to 9635 07:19:40,957 --> 07:19:44,957 go and if they have stopped us we're 9636 07:19:43,439 --> 07:19:46,797 going to stop and you're going to see 9637 07:19:44,957 --> 07:19:49,119 that we're going to get exactly the same 9638 07:19:46,797 --> 07:19:51,199 result that we had before but this is 9639 07:19:49,119 --> 07:19:53,840 really much more compact so we're going 9640 07:19:51,200 --> 07:19:56,718 to run and see the result of this and 9641 07:19:56,718 --> 07:20:00,319 and run rooster you're going to see that 9642 07:19:58,637 --> 07:20:02,477 they're going to stop us and this is 9643 07:20:02,477 --> 07:20:07,200 up again and change up these variables 9644 07:20:04,477 --> 07:20:09,119 to really anything you want and see what 9645 07:20:07,200 --> 07:20:12,319 you get trying to really understand what 9646 07:20:09,119 --> 07:20:14,957 is going on here it is not difficult and 9647 07:20:12,319 --> 07:20:17,599 the if statement is really fundamental 9648 07:20:14,957 --> 07:20:19,119 to any programming you're going to do so 9649 07:20:17,599 --> 07:20:21,359 this is really all we set up to do in 9650 07:20:19,119 --> 07:20:24,159 this lecture to learn about conditional 9651 07:20:21,360 --> 07:20:26,240 programming using the if statement it is 9652 07:20:24,159 --> 07:20:29,360 a powerful thing and you're going to see 9653 07:20:26,240 --> 07:20:31,280 it all over your software development 9654 07:20:29,360 --> 07:20:33,200 career so make sure you really 9655 07:20:31,279 --> 07:20:35,039 understand this but even if it's not 9656 07:20:33,200 --> 07:20:37,200 clear now we're going to have a chance 9657 07:20:35,040 --> 07:20:40,000 to do many examples and you're going to 9658 07:20:37,200 --> 07:20:41,280 really understand what this is all about 9659 07:20:40,000 --> 07:20:43,279 we are going to stop here in this 9660 07:20:41,279 --> 07:20:46,797 lecture the next one we're going to see 9661 07:20:43,279 --> 07:20:49,119 how we can use the else if close 9662 07:20:46,797 --> 07:20:50,878 go ahead and finish up here and meet me 9663 07:20:49,119 --> 07:20:54,000 there in this lecture we're going to 9664 07:20:50,878 --> 07:20:57,440 learn about the else if close that you 9665 07:20:54,000 --> 07:20:59,439 can attach to your if statement 9666 07:20:57,439 --> 07:21:01,680 and that's going to allow you to test 9667 07:20:59,439 --> 07:21:02,797 for several conditions in your if 9668 07:21:03,599 --> 07:21:09,599 to really understand this suppose we are 9669 07:21:06,558 --> 07:21:12,240 building a drawing application and at 9670 07:21:09,599 --> 07:21:14,957 any given moment the user might be using 9671 07:21:12,240 --> 07:21:17,040 the pan or the marker or the eraser 9672 07:21:14,957 --> 07:21:19,919 already whatever tool that it is we 9673 07:21:22,319 --> 07:21:27,360 we need to know which tool is currently 9674 07:21:24,718 --> 07:21:29,920 selected by the user and we can do a 9675 07:21:27,360 --> 07:21:32,958 piece of logic like this we can have the 9676 07:21:29,919 --> 07:21:35,199 tool stored somewhere in our application 9677 07:21:32,957 --> 07:21:36,877 and when we get to the moment where we 9678 07:21:35,200 --> 07:21:38,878 want to do something for example when 9679 07:21:36,878 --> 07:21:41,440 the user clicks with the mouse and they 9680 07:21:38,878 --> 07:21:44,000 want to drag and start doing things then 9681 07:21:41,439 --> 07:21:45,520 we're going to test for the current tool 9682 07:21:44,000 --> 07:21:47,919 if the tool is the pan we're going to 9683 07:21:45,520 --> 07:21:49,279 use the settings for the pen if the tool 9684 07:21:47,919 --> 07:21:51,039 is the marker we're going to use the 9685 07:21:49,279 --> 07:21:53,279 settings of the marker and we're going 9686 07:21:53,279 --> 07:21:58,637 one tool that is actually selected as 9687 07:21:56,240 --> 07:22:00,558 the current tool by the user and when 9688 07:21:58,637 --> 07:22:03,680 you do something like this only one 9689 07:22:00,558 --> 07:22:06,638 block in this whole chain of statements 9690 07:22:03,680 --> 07:22:09,439 is going to execute so for example if we 9691 07:22:06,637 --> 07:22:11,520 have the marker as the current tool the 9692 07:22:09,439 --> 07:22:14,397 code inside this block is going to 9693 07:22:11,520 --> 07:22:17,279 execute and say active tool is marker 9694 07:22:14,398 --> 07:22:20,398 but nothing else in this block is going 9695 07:22:17,279 --> 07:22:22,878 to execute after we run this statement 9696 07:22:20,398 --> 07:22:25,680 control is going to jump at the end of 9697 07:22:22,878 --> 07:22:27,600 this whole chain of if and else if 9698 07:22:25,680 --> 07:22:29,920 statements here i really hope you 9699 07:22:27,599 --> 07:22:31,919 understand this and uh this is really 9700 07:22:31,919 --> 07:22:36,397 this is one of the applications i have 9701 07:22:34,000 --> 07:22:39,040 done in one of my advanced course about 9702 07:22:36,398 --> 07:22:40,478 qt and c plus qt is a framework that 9703 07:22:39,040 --> 07:22:42,477 allows you to do graphical user 9704 07:22:40,477 --> 07:22:44,718 interfaces like this and you can build 9705 07:22:42,477 --> 07:22:47,919 applications like this so what i did 9706 07:22:44,718 --> 07:22:50,398 here is i did the exact same thing that 9707 07:22:47,919 --> 07:22:53,599 we have here i had the chain of if and 9708 07:22:50,398 --> 07:22:56,319 else if statements and whatever tool 9709 07:22:53,599 --> 07:22:58,477 was selected as the current tool was 9710 07:22:56,319 --> 07:23:00,239 used to draw things on the canvas here 9711 07:22:58,477 --> 07:23:02,477 for example if the current tool was the 9712 07:23:00,240 --> 07:23:04,638 pen and the pen is this little pen tool 9713 07:23:02,477 --> 07:23:07,119 here if it is active we're going to draw 9714 07:23:04,637 --> 07:23:08,797 things like hello world here if we have 9715 07:23:07,119 --> 07:23:11,039 the star selected we're going to draw 9716 07:23:08,797 --> 07:23:12,878 stars if we have the rectangle selected 9717 07:23:11,040 --> 07:23:15,120 we're going to draw rectangles we could 9718 07:23:12,878 --> 07:23:17,520 do all kinds of crazy things using this 9719 07:23:15,119 --> 07:23:19,520 piece of logic here but it is powerful 9720 07:23:17,520 --> 07:23:21,680 stuff and you really have to make sure 9721 07:23:19,520 --> 07:23:24,080 you understand this so this is how the 9722 07:23:21,680 --> 07:23:26,240 else if clause can really help you the 9723 07:23:24,080 --> 07:23:28,398 main thing is to allow you to test for 9724 07:23:26,240 --> 07:23:30,718 several different conditions you could 9725 07:23:28,398 --> 07:23:32,398 do this with a lot of if statements but 9726 07:23:30,718 --> 07:23:34,878 that's not going to be really cool it's 9727 07:23:32,398 --> 07:23:36,398 going to be ugly and hard to read this 9728 07:23:36,398 --> 07:23:39,680 now that you have an idea about this 9729 07:23:38,159 --> 07:23:41,840 we're going to head to visual studio 9730 07:23:39,680 --> 07:23:44,718 code and actually see this in action 9731 07:23:41,840 --> 07:23:47,040 here we are in our working directory 9732 07:23:44,718 --> 07:23:49,200 we're going to grab our template project 9733 07:23:47,040 --> 07:23:51,600 pretty quick and we're going to use the 9734 07:23:49,200 --> 07:23:54,240 else if project here that's our current 9735 07:23:51,599 --> 07:23:56,717 project so we're going to do that and 9736 07:23:54,240 --> 07:23:58,638 open this up in visual studio code as we 9737 07:23:56,718 --> 07:24:01,280 always do we're going to open this up 9738 07:23:58,637 --> 07:24:03,520 we're going to open main cpp and we're 9739 07:24:01,279 --> 07:24:05,599 going to remove what we don't need 9740 07:24:03,520 --> 07:24:08,558 the first thing i am going to do is to 9741 07:24:05,599 --> 07:24:10,877 put in a set of variables that are going 9742 07:24:08,558 --> 07:24:12,477 to store our tools and we can do 9743 07:24:10,878 --> 07:24:15,440 something like this we have a variable 9744 07:24:12,477 --> 07:24:17,919 for pen marker eraser rectangle circle 9745 07:24:15,439 --> 07:24:19,599 and ellipse i have to say that this is 9746 07:24:17,919 --> 07:24:21,279 not the way to go about this we're going 9747 07:24:19,599 --> 07:24:23,359 to learn about another way we can do 9748 07:24:21,279 --> 07:24:25,039 this but that's going to be there in the 9749 07:24:23,360 --> 07:24:27,279 course when we have enough tools to 9750 07:24:25,040 --> 07:24:29,600 understand this for now we're just going 9751 07:24:29,599 --> 07:24:34,557 okay 10 is going to represent the pan 20 9752 07:24:32,558 --> 07:24:36,398 is going to represent the marker 30 is 9753 07:24:34,558 --> 07:24:38,240 going to represent the razor and we can 9754 07:24:36,398 --> 07:24:41,520 go on until we hit ellipse which is 9755 07:24:38,240 --> 07:24:43,760 represented by 60. now if we go in main 9756 07:24:41,520 --> 07:24:46,319 and set up a variable and call it tool 9757 07:24:43,759 --> 07:24:48,877 for example and initialize it with 9758 07:24:46,319 --> 07:24:50,797 whatever tool we think that the user is 9759 07:24:48,878 --> 07:24:53,760 currently using for example we can use 9760 07:24:50,797 --> 07:24:57,199 circle and initialize this we can set up 9761 07:24:53,759 --> 07:24:59,599 a chain of s and else if statements to 9762 07:24:57,200 --> 07:25:01,920 do whatever it is we want to do when 9763 07:24:59,599 --> 07:25:04,319 this tool here is selected okay we're 9764 07:25:01,919 --> 07:25:06,079 going to go down and put in our code i 9765 07:25:04,319 --> 07:25:08,319 don't want to type all this so i'm going 9766 07:25:06,080 --> 07:25:10,478 to put this up here and if you want you 9767 07:25:10,477 --> 07:25:14,159 resource section the code is going to be 9768 07:25:12,398 --> 07:25:16,159 there you can copy it and paste it in 9769 07:25:14,159 --> 07:25:19,119 here and do things with that okay we 9770 07:25:16,159 --> 07:25:21,520 have a chain of if and else if close is 9771 07:25:19,119 --> 07:25:23,919 in here and the first one is going to 9772 07:25:21,520 --> 07:25:26,080 test and see if the tool is the pan and 9773 07:25:23,919 --> 07:25:28,637 notice that we are using double equal 9774 07:25:26,080 --> 07:25:30,958 signs here this is what you use to test 9775 07:25:28,637 --> 07:25:33,279 for equality you don't use one equal 9776 07:25:30,957 --> 07:25:35,039 sign you have to use two equal signs if 9777 07:25:33,279 --> 07:25:36,878 you use one equal sign that's going to 9778 07:25:35,040 --> 07:25:38,878 be an assignment and this is going to 9779 07:25:36,878 --> 07:25:41,200 always evaluate to true so you don't 9780 07:25:38,878 --> 07:25:43,200 want to do that again to test for 9781 07:25:41,200 --> 07:25:45,760 equality you're going to use double 9782 07:25:43,200 --> 07:25:47,280 equal signs in c plus plus so if the 9783 07:25:45,759 --> 07:25:49,759 current tool is the pen we're going to 9784 07:25:47,279 --> 07:25:51,759 say active tool span if the current tool 9785 07:25:49,759 --> 07:25:54,079 is marker we're going to say that so we 9786 07:25:51,759 --> 07:25:57,119 are basically going to do this for all 9787 07:25:54,080 --> 07:25:59,600 the tools that our application supports 9788 07:25:57,119 --> 07:26:01,680 so what do you think we'll see 9789 07:25:59,599 --> 07:26:02,637 when we run this the active tool is 9790 07:26:02,637 --> 07:26:06,477 so we should print active tool is circle 9791 07:26:06,477 --> 07:26:10,159 let's try this out we're going to open 9792 07:26:08,558 --> 07:26:12,477 the terminal so that we can see the 9793 07:26:10,159 --> 07:26:14,957 output nicely and we're going to world 9794 07:26:12,477 --> 07:26:16,797 with gcc and the build is going to go 9795 07:26:14,957 --> 07:26:19,759 through if we run this we're going to 9796 07:26:16,797 --> 07:26:21,520 see active tool is circle you can go up 9797 07:26:19,759 --> 07:26:23,679 here and change the tool to rectangle 9798 07:26:21,520 --> 07:26:26,000 for example if you build again you're 9799 07:26:23,680 --> 07:26:27,840 going to see that the correct block of 9800 07:26:26,000 --> 07:26:30,718 code is going to be executed we're going 9801 07:26:27,840 --> 07:26:33,279 to clear and run rooster active tool is 9802 07:26:30,718 --> 07:26:35,360 rectangle if we try to put in something 9803 07:26:33,279 --> 07:26:36,477 that doesn't exist let's say let's put 9804 07:26:36,477 --> 07:26:40,637 just to mess with our compiler here 9805 07:26:39,279 --> 07:26:43,520 we're going to get a compiler error 9806 07:26:40,637 --> 07:26:45,840 because that type is not known by 9807 07:26:43,520 --> 07:26:47,760 our code here so we're going to get bird 9808 07:26:45,840 --> 07:26:49,759 was not declared in this scope we don't 9809 07:26:47,759 --> 07:26:52,477 know what it is you have to put in 9810 07:26:49,759 --> 07:26:55,679 whatever thing you have declared 9811 07:26:52,477 --> 07:26:58,558 beforehand here so if we put in a razor 9812 07:26:55,680 --> 07:27:00,159 for example we're going to weld with gcc 9813 07:26:58,558 --> 07:27:02,558 the world is going to go through going 9814 07:27:00,159 --> 07:27:04,957 to clear run rooster the tool is going 9815 07:27:02,558 --> 07:27:07,680 to be eraser and this is exactly what we 9816 07:27:04,957 --> 07:27:10,397 expect again the main message in this 9817 07:27:07,680 --> 07:27:12,558 lecture is that you can use this else if 9818 07:27:10,398 --> 07:27:15,040 close if you have several conditions 9819 07:27:12,558 --> 07:27:17,760 that you want to test for just like we 9820 07:27:15,040 --> 07:27:20,558 have here and again one of these blocks 9821 07:27:17,759 --> 07:27:23,119 is going to execute and once we hit the 9822 07:27:20,558 --> 07:27:26,000 end of this chain of if and else if 9823 07:27:23,119 --> 07:27:28,319 closes we're going to fall here and code 9824 07:27:26,000 --> 07:27:30,398 after that is going to execute so let's 9825 07:27:28,319 --> 07:27:33,759 say moving on so that you can really see 9826 07:27:30,398 --> 07:27:36,398 this here very clear moving on and if we 9827 07:27:33,759 --> 07:27:39,759 bolt this we're going to see only one 9828 07:27:36,398 --> 07:27:42,398 block from this whole chain and once one 9829 07:27:39,759 --> 07:27:45,119 block executes control is going to go 9830 07:27:42,398 --> 07:27:47,040 after all this chain and execute 9831 07:27:45,119 --> 07:27:50,239 whatever is after that so we're going to 9832 07:27:47,040 --> 07:27:52,878 say moving on after we print that the 9833 07:27:50,240 --> 07:27:55,440 active tool is eraser here i think we 9834 07:27:55,439 --> 07:28:01,680 try and run this and you see active tool 9835 07:27:58,159 --> 07:28:04,797 is razer and moving on so after we print 9836 07:28:01,680 --> 07:28:07,360 the code from the eraser block we're not 9837 07:28:04,797 --> 07:28:10,079 going to execute rectangle or a circle 9838 07:28:07,360 --> 07:28:12,398 or ellipse it's going to jump at the end 9839 07:28:10,080 --> 07:28:14,000 of this whole block this is really 9840 07:28:14,000 --> 07:28:18,080 super clear and again make sure you 9841 07:28:16,000 --> 07:28:20,477 understand the syntax here the else if 9842 07:28:18,080 --> 07:28:23,360 statement is followed by a set of 9843 07:28:20,477 --> 07:28:25,759 parentheses and inside the parenthesis 9844 07:28:23,360 --> 07:28:28,637 we have the condition we want to test 9845 07:28:25,759 --> 07:28:31,119 for the condition has to evaluate to 9846 07:28:28,637 --> 07:28:33,520 boolean and after that we have the block 9847 07:28:31,119 --> 07:28:35,840 of code that we want to execute and this 9848 07:28:33,520 --> 07:28:38,240 is really all you have to do to get this 9849 07:28:38,240 --> 07:28:41,280 we are going to stop here in this 9850 07:28:39,599 --> 07:28:43,439 lecture in the next one we're going to 9851 07:28:43,439 --> 07:28:46,797 go ahead and finish up here and meet me 9852 07:28:46,797 --> 07:28:51,520 in this lecture we're going to learn 9853 07:28:48,240 --> 07:28:54,398 about the switch statement and this is 9854 07:28:51,520 --> 07:28:56,957 an alternative way to do what we did 9855 07:28:54,398 --> 07:28:59,440 with these else if clauses in the last 9856 07:28:56,957 --> 07:29:01,279 lecture basically testing for several 9857 07:28:59,439 --> 07:29:03,919 different conditions but it's going to 9858 07:29:01,279 --> 07:29:06,000 be in a much more compact way than what 9859 07:29:03,919 --> 07:29:08,397 we did in the last lecture we are going 9860 07:29:06,000 --> 07:29:10,558 to be using the exact same example we 9861 07:29:08,398 --> 07:29:12,798 did in the last lecture and we're going 9862 07:29:10,558 --> 07:29:15,280 to look at the switch statement and how 9863 07:29:12,797 --> 07:29:17,759 it works in c plus plus and this is how 9864 07:29:15,279 --> 07:29:20,000 it works we have our variable which is 9865 07:29:17,759 --> 07:29:21,919 going to store the current tool and 9866 07:29:21,919 --> 07:29:24,557 inside the parenthesis here we're going 9867 07:29:24,558 --> 07:29:29,200 pass the variable that we're going to be 9868 07:29:26,477 --> 07:29:30,797 basically switching on or testing the 9869 07:29:30,797 --> 07:29:36,159 and inside the block we're going to be 9870 07:29:33,279 --> 07:29:38,159 basically testing for each case so we're 9871 07:29:36,159 --> 07:29:40,159 going to say if the current tool is the 9872 07:29:38,159 --> 07:29:42,477 pan we're going to say case pan we're 9873 07:29:40,159 --> 07:29:44,558 going to put a colon in here and we're 9874 07:29:42,477 --> 07:29:46,000 going to put the code we want to execute 9875 07:29:46,000 --> 07:29:51,439 where the current tool is the pan in 9876 07:29:48,398 --> 07:29:53,520 these curly braces here and one thing 9877 07:29:51,439 --> 07:29:55,439 you should notice from the start is this 9878 07:29:55,439 --> 07:29:59,359 this is a statement that tells c plus 9879 07:29:59,360 --> 07:30:05,279 that after we successfully execute a 9880 07:30:02,718 --> 07:30:07,760 block of code we're going to jump out of 9881 07:30:05,279 --> 07:30:09,360 this switch statement if you omit this 9882 07:30:09,360 --> 07:30:14,637 everything after the case that you just 9883 07:30:12,159 --> 07:30:17,119 executed is going to execute and that's 9884 07:30:14,637 --> 07:30:19,520 not probably going to be what you want 9885 07:30:17,119 --> 07:30:21,680 if this is not making sense yet please 9886 07:30:19,520 --> 07:30:23,600 bear with me you're going to see it run 9887 07:30:21,680 --> 07:30:25,920 in visual studio code and you are going 9888 07:30:23,599 --> 07:30:27,519 to understand so what you're going to do 9889 07:30:25,919 --> 07:30:30,477 you're going to put all the tools you 9890 07:30:27,520 --> 07:30:33,680 want to test for in separate cases you 9891 07:30:30,477 --> 07:30:36,558 see here we have marker and after that 9892 07:30:33,680 --> 07:30:39,200 you can put a default case that is going 9893 07:30:36,558 --> 07:30:40,878 to be matched when nothing has been 9894 07:30:39,200 --> 07:30:44,319 matched in whatever it is you have 9895 07:30:40,878 --> 07:30:45,920 tested for in this switch block here and 9896 07:30:44,319 --> 07:30:47,919 when we run this piece of code we're 9897 07:30:45,919 --> 07:30:50,239 going to get exactly the same results 9898 07:30:47,919 --> 07:30:52,159 that we had in the last lecture but 9899 07:30:50,240 --> 07:30:55,360 hopefully you can see that this is much 9900 07:30:52,159 --> 07:30:58,000 more compact than the series of else if 9901 07:30:55,360 --> 07:31:00,159 closes that we had in the last lecture 9902 07:30:58,000 --> 07:31:02,797 again the break statement is really 9903 07:31:00,159 --> 07:31:04,957 useful if you don't put it in c plus 9904 07:31:02,797 --> 07:31:07,279 plus is going to be confused and after 9905 07:31:04,957 --> 07:31:09,119 you hit your successful case 9906 07:31:07,279 --> 07:31:11,520 everything after that is going to be 9907 07:31:09,119 --> 07:31:14,319 executed so the break statement is 9908 07:31:11,520 --> 07:31:17,760 basically telling c plus plus if you hit 9909 07:31:14,319 --> 07:31:19,680 it after a successful case is 9910 07:31:17,759 --> 07:31:21,119 jump out of this switch block because 9911 07:31:19,680 --> 07:31:23,520 you are done you don't have to do 9912 07:31:21,119 --> 07:31:26,079 anything else in here and another thing 9913 07:31:23,520 --> 07:31:27,840 you should know is that the condition 9914 07:31:26,080 --> 07:31:31,040 which is what we have inside this 9915 07:31:27,840 --> 07:31:33,840 parenthesis here can only be an integer 9916 07:31:31,040 --> 07:31:36,000 or an enum so basically it can be ant 9917 07:31:36,000 --> 07:31:40,797 basically every integral type we have 9918 07:31:38,159 --> 07:31:42,957 seen before but it can't be another type 9919 07:31:40,797 --> 07:31:45,360 such as string because that doesn't 9920 07:31:42,957 --> 07:31:46,397 really make sense so be sure to keep 9921 07:31:46,398 --> 07:31:50,958 now that you have an idea about this 9922 07:31:48,477 --> 07:31:53,040 switch statement let's head to visual 9923 07:31:50,957 --> 07:31:56,319 video code and actually see this in 9924 07:31:53,040 --> 07:31:58,080 action here we are in our working folder 9925 07:31:56,319 --> 07:31:59,840 the current project is going to be 9926 07:31:58,080 --> 07:32:02,160 switch so we're going to grab our 9927 07:31:59,840 --> 07:32:04,398 template files and we're going to bring 9928 07:32:02,159 --> 07:32:07,119 that in here i'm going to go up a little 9929 07:32:04,398 --> 07:32:09,600 bit and open this up in visual studio 9930 07:32:07,119 --> 07:32:11,360 code as we always do and we're going to 9931 07:32:09,599 --> 07:32:13,840 open up our main file we're going to get 9932 07:32:11,360 --> 07:32:16,477 rid of what we don't need we are going 9933 07:32:13,840 --> 07:32:18,558 to go outside the main function here and 9934 07:32:16,477 --> 07:32:21,119 put in our tools just like in the last 9935 07:32:18,558 --> 07:32:24,638 lecture we are using integers each one 9936 07:32:21,119 --> 07:32:26,878 is represented by 10 20 30 up to 60 and 9937 07:32:24,637 --> 07:32:28,637 these are the tools we have and i don't 9938 07:32:26,878 --> 07:32:30,637 think i explained this but we are 9939 07:32:28,637 --> 07:32:33,039 flagging them const because we don't 9940 07:32:30,637 --> 07:32:36,319 want them to be modifiable we have seen 9941 07:32:33,040 --> 07:32:38,319 about const earlier in the course we are 9942 07:32:36,319 --> 07:32:40,477 going to jump into main and set up our 9943 07:32:38,319 --> 07:32:42,797 variable it's going to be ant and we're 9944 07:32:40,477 --> 07:32:44,797 going to call it tool and we're going to 9945 07:32:42,797 --> 07:32:47,439 initialize this whatever tool we want 9946 07:32:44,797 --> 07:32:49,360 let's use eraser why not and then we're 9947 07:32:47,439 --> 07:32:50,957 going to put in our switch block we're 9948 07:32:50,957 --> 07:32:55,279 and if i type this you see that visual 9949 07:32:53,439 --> 07:32:57,840 studio code is going to give me 9950 07:32:55,279 --> 07:33:00,000 intelligence or suggestions on what 9951 07:32:57,840 --> 07:33:01,840 thing i could do one cool thing that 9952 07:33:00,000 --> 07:33:03,840 visual studio code does is give you 9953 07:33:01,840 --> 07:33:06,319 snippets to really help you type things 9954 07:33:03,840 --> 07:33:09,360 out so we can click on this and it is 9955 07:33:06,319 --> 07:33:13,279 going to auto complete a starter version 9956 07:33:09,360 --> 07:33:15,840 of our case and we can type in our 9957 07:33:13,279 --> 07:33:18,079 expression here which is going to be 9958 07:33:15,840 --> 07:33:19,680 what we're going to be switching over so 9959 07:33:18,080 --> 07:33:22,398 we're going to switch over the variable 9960 07:33:19,680 --> 07:33:24,797 tool here and we can put in our cases so 9961 07:33:22,398 --> 07:33:26,638 in our case we're going to start with 9962 07:33:24,797 --> 07:33:28,477 pan which is what we have here so we're 9963 07:33:26,637 --> 07:33:30,637 going to say case pan we're going to 9964 07:33:30,637 --> 07:33:35,759 uh let's remove this and say pen 9965 07:33:37,599 --> 07:33:42,637 a block of code that is going to execute 9966 07:33:40,477 --> 07:33:44,319 when the current tool is the pen and 9967 07:33:42,637 --> 07:33:45,599 we're going to put that in here let's 9968 07:33:45,599 --> 07:33:51,759 to the right a little bit and inside the 9969 07:33:47,919 --> 07:33:54,239 block we can say sddc out active tool is 9970 07:33:51,759 --> 07:33:56,797 pan okay so after that we're going to 9971 07:33:54,240 --> 07:33:58,638 put this break statement if it's not 9972 07:33:56,797 --> 07:34:00,637 here we're going to have a problem 9973 07:33:58,637 --> 07:34:03,200 because when the current tool is the pan 9974 07:34:00,637 --> 07:34:05,200 and this code here executes every case 9975 07:34:03,200 --> 07:34:06,718 after that is going to execute and 9976 07:34:05,200 --> 07:34:08,798 that's not what we want we have to 9977 07:34:06,718 --> 07:34:10,798 remember to put this break statement 9978 07:34:08,797 --> 07:34:12,878 here okay so we're going to put in all 9979 07:34:10,797 --> 07:34:14,000 our other cases so we're going to copy 9980 07:34:14,000 --> 07:34:18,398 and look at the next one it's going to 9981 07:34:15,680 --> 07:34:20,319 be marker so we can go on the next line 9982 07:34:18,398 --> 07:34:22,718 and put in marker we're going to paste 9983 07:34:20,319 --> 07:34:25,040 this in we're going to say marker and 9984 07:34:22,718 --> 07:34:27,280 we're going to say active tool is marker 9985 07:34:25,040 --> 07:34:29,520 and we can keep doing the same thing 9986 07:34:27,279 --> 07:34:32,000 we're going to go download them and 9987 07:34:29,520 --> 07:34:34,159 paste this in the next one is going to 9988 07:34:32,000 --> 07:34:36,240 be eraser so let's do that we're going 9989 07:34:34,159 --> 07:34:38,718 to say eraser and we're going to say 9990 07:34:36,240 --> 07:34:40,478 current tool is razer and we're going to 9991 07:34:40,477 --> 07:34:46,000 i am doing this live because it really 9992 07:34:42,718 --> 07:34:47,840 is important for you to see me type this 9993 07:34:46,000 --> 07:34:50,558 otherwise you may be confused because 9994 07:34:47,840 --> 07:34:54,159 this is a slightly confusing construct 9995 07:34:50,558 --> 07:34:56,638 we have in c plus plus the next one is 9996 07:34:54,159 --> 07:34:58,159 rectangle so we're going to use that and 9997 07:34:56,637 --> 07:35:01,520 we're going to say current tool is 9998 07:34:58,159 --> 07:35:03,840 rectangle the next one is going to be 9999 07:35:01,520 --> 07:35:05,439 circle and ellipse so let's do that 10000 07:35:03,840 --> 07:35:08,319 we're going to put in circle we're going 10001 07:35:05,439 --> 07:35:10,000 to go down and paste this in and bring 10002 07:35:10,000 --> 07:35:15,439 and i'm going to say circle and we're 10003 07:35:12,159 --> 07:35:17,680 going to go down and put the next one in 10004 07:35:15,439 --> 07:35:20,637 which is going to be ellipse and after 10005 07:35:17,680 --> 07:35:23,279 that notice that video studio code also 10006 07:35:20,637 --> 07:35:26,079 inserted a default block and this is a 10007 07:35:23,279 --> 07:35:27,840 block that is going to execute when none 10008 07:35:29,200 --> 07:35:34,319 so this is really important so you can 10009 07:35:34,319 --> 07:35:39,360 and say no match found okay so we want 10010 07:35:37,520 --> 07:35:41,200 to see this print out and we're going to 10011 07:35:41,200 --> 07:35:46,477 okay after we do this we should get this 10012 07:35:43,840 --> 07:35:48,957 to work and do whatever we were doing in 10013 07:35:46,477 --> 07:35:50,718 the last lecture so the current tool is 10014 07:35:50,718 --> 07:35:54,798 the block here is going to be matched so 10015 07:35:52,637 --> 07:35:56,397 code here is going to execute 10016 07:35:54,797 --> 07:35:59,439 after that we're going to meet this 10017 07:35:56,398 --> 07:36:01,600 break and what it's really saying is 10018 07:35:59,439 --> 07:36:03,840 you have done what you have to do jump 10019 07:36:01,599 --> 07:36:05,359 out of this switch block here and we're 10020 07:36:03,840 --> 07:36:07,119 going to go to the end of this switch 10021 07:36:05,360 --> 07:36:09,520 block and we're going to execute 10022 07:36:07,119 --> 07:36:11,840 whatever is after that so just like in 10023 07:36:09,520 --> 07:36:14,637 the last lecture we can say moving on 10024 07:36:11,840 --> 07:36:15,920 and if we execute the program as we have 10025 07:36:15,919 --> 07:36:21,599 it's going to say active tool is razor 10026 07:36:18,957 --> 07:36:23,759 and it's going to print moving on 10027 07:36:21,599 --> 07:36:25,840 let's bring up the terminal so that we 10028 07:36:23,759 --> 07:36:28,397 can really see this and we're going to 10029 07:36:25,840 --> 07:36:31,520 build with gcc the world is going to be 10030 07:36:28,398 --> 07:36:33,600 good and if we run rooster let's clear a 10031 07:36:31,520 --> 07:36:36,240 bit we're going to see that active tool 10032 07:36:33,599 --> 07:36:38,159 is a razor and we are moving on this is 10033 07:36:36,240 --> 07:36:39,600 really cool and it is exactly the same 10034 07:36:39,599 --> 07:36:45,680 in the last lecture but now we are using 10035 07:36:42,797 --> 07:36:48,159 a much cleaner construct which switch 10036 07:36:45,680 --> 07:36:50,319 provides okay we can try to change the 10037 07:36:48,159 --> 07:36:52,319 tool and see that this really works we 10038 07:36:52,319 --> 07:36:57,200 why not i'm going to build with gcc the 10039 07:36:54,797 --> 07:36:59,840 world is going to go through as you see 10040 07:36:57,200 --> 07:37:01,760 here and if we run rooster we're going 10041 07:36:59,840 --> 07:37:03,599 to see active tool a circle we are 10042 07:37:01,759 --> 07:37:06,159 moving on you can try and change this 10043 07:37:03,599 --> 07:37:08,159 however you want if we put in ellipse 10044 07:37:06,159 --> 07:37:11,200 and world again we're going to build 10045 07:37:08,159 --> 07:37:14,718 with gcc and if we run we're going to 10046 07:37:11,200 --> 07:37:17,040 get active tool is pan and moving on why 10047 07:37:14,718 --> 07:37:19,600 is that ellipse we are saying active 10048 07:37:17,040 --> 07:37:22,878 tool is pen we didn't change that so we 10049 07:37:19,599 --> 07:37:25,279 just code a really bad bag here so let's 10050 07:37:22,878 --> 07:37:27,600 fix this and we're going to build again 10051 07:37:25,279 --> 07:37:29,840 i'm glad i found this so we're going to 10052 07:37:29,840 --> 07:37:33,840 and we're going to see active tool is 10053 07:37:33,840 --> 07:37:40,159 okay so let's see what could happen 10054 07:37:36,477 --> 07:37:43,119 if you forgot your break statement after 10055 07:37:40,159 --> 07:37:44,477 your case here so the current tool is 10056 07:37:44,477 --> 07:37:49,040 and it is the last thing we have in here 10057 07:37:46,797 --> 07:37:51,039 so it's not a good test case 10058 07:37:51,040 --> 07:37:55,600 is make the current tool the pen or the 10059 07:37:53,520 --> 07:37:57,040 marker let's use marker that's going to 10060 07:37:57,040 --> 07:38:01,360 and i am going to remove the break 10061 07:38:01,360 --> 07:38:07,119 okay so and i am going to remove all the 10062 07:38:03,759 --> 07:38:08,477 bread statements we have until we hit 10063 07:38:13,360 --> 07:38:18,718 and we're even going to remove that for 10064 07:38:18,718 --> 07:38:22,558 and if we both we're going to build fine 10065 07:38:20,957 --> 07:38:25,039 you're going to see that the world was 10066 07:38:22,558 --> 07:38:26,798 good but if we run this try to guess 10067 07:38:25,040 --> 07:38:29,680 what we're going to get the current 10068 07:38:26,797 --> 07:38:30,797 stool is the marker so we should print 10069 07:38:30,797 --> 07:38:37,279 and hit the end of the switch block and 10070 07:38:34,080 --> 07:38:39,600 do moving on but let's see what we get 10071 07:38:37,279 --> 07:38:42,557 did we build let's build again just to 10072 07:38:39,599 --> 07:38:44,397 be sure so we're going to run rooster 10073 07:38:44,398 --> 07:38:48,878 active tool is marker active thought is 10074 07:38:46,797 --> 07:38:50,000 eraser active tool is rectangle we 10075 07:38:50,000 --> 07:38:54,797 print every other case after the macro 10076 07:38:54,797 --> 07:39:00,000 and if we try to change this for example 10077 07:38:57,439 --> 07:39:01,759 not use marker and use eraser let's 10078 07:39:00,000 --> 07:39:02,957 change this you're going to see that 10079 07:39:05,520 --> 07:39:10,080 and everything after that is also going 10080 07:39:07,840 --> 07:39:12,398 to execute and it is not what you want 10081 07:39:10,080 --> 07:39:14,398 so let's clear and run rooster you're 10082 07:39:12,398 --> 07:39:16,798 going to see eraser rectangle circle 10083 07:39:14,398 --> 07:39:18,958 ellipse and no match found so you have 10084 07:39:16,797 --> 07:39:21,520 ready to remember to put in this brick 10085 07:39:18,957 --> 07:39:24,239 statement i am going to hit ctrl z a 10086 07:39:21,520 --> 07:39:26,558 couple of times and bring those in and i 10087 07:39:24,240 --> 07:39:29,440 think this is enough we have to hit 10088 07:39:26,558 --> 07:39:31,360 again so that marker also has it and if 10089 07:39:29,439 --> 07:39:33,840 we run now we're going to get what we 10090 07:39:31,360 --> 07:39:36,558 expect let's try to use an eraser again 10091 07:39:33,840 --> 07:39:38,718 and if we both again and clear and run 10092 07:39:36,558 --> 07:39:41,120 rooster we're going to see active tool 10093 07:39:38,718 --> 07:39:43,200 is razer and we are moving on 10094 07:39:41,119 --> 07:39:45,439 this is what we expect okay this is 10095 07:39:43,200 --> 07:39:47,200 really how you work with a switch 10096 07:39:45,439 --> 07:39:50,000 block you have to pass in your 10097 07:39:47,200 --> 07:39:52,718 expression or your test case and you 10098 07:39:50,000 --> 07:39:55,520 catch each case using this case syntax 10099 07:39:52,718 --> 07:39:57,200 here you say case you pass in whatever 10100 07:39:55,520 --> 07:39:59,680 it is you want to match and you put a 10101 07:39:57,200 --> 07:40:02,637 column and you put your code inside this 10102 07:39:59,680 --> 07:40:05,439 block here and remember after each case 10103 07:40:02,637 --> 07:40:07,119 you have to add this break otherwise 10104 07:40:05,439 --> 07:40:09,599 everything else after this is going to 10105 07:40:07,119 --> 07:40:12,477 execute and it is not going to be what 10106 07:40:09,599 --> 07:40:14,239 you want before i let you go i want to 10107 07:40:12,477 --> 07:40:16,878 show you that you can actually group 10108 07:40:14,240 --> 07:40:19,680 these case statements if this is what is 10109 07:40:16,878 --> 07:40:21,200 making sense for your application so for 10110 07:40:25,520 --> 07:40:28,797 this can be grouped into the group of 10111 07:40:33,439 --> 07:40:39,759 and uh put them on different lines 10112 07:40:36,477 --> 07:40:41,919 something like this so let's delete this 10113 07:40:41,919 --> 07:40:44,877 and we're going to delete the other 10114 07:40:46,080 --> 07:40:51,920 case circle just like this and we're 10115 07:40:48,878 --> 07:40:54,797 going to say drawing shape i think this 10116 07:40:51,919 --> 07:40:57,759 syntax is supported in c plus plus okay 10117 07:40:57,759 --> 07:41:03,919 and uh we're going to world with gcc 10118 07:41:01,520 --> 07:41:06,000 and the world is going to be good so if 10119 07:41:03,919 --> 07:41:08,397 we have a razor which is what we have in 10120 07:41:06,000 --> 07:41:10,240 here we're going to say drawing shape 10121 07:41:12,240 --> 07:41:17,040 you're going to see drawing shapes 10122 07:41:14,000 --> 07:41:21,040 moving on we have grouped different 10123 07:41:17,040 --> 07:41:23,280 cases and handles them with one block 10124 07:41:21,040 --> 07:41:24,878 and this may come in handy in some 10125 07:41:24,878 --> 07:41:29,119 one thing you should keep in mind is 10126 07:41:26,558 --> 07:41:32,319 that there are limitations on what you 10127 07:41:29,119 --> 07:41:34,557 can use as an expression or a condition 10128 07:41:32,319 --> 07:41:37,520 in your switch statement and that has to 10129 07:41:34,558 --> 07:41:39,520 be an integer something like ant car 10130 07:41:37,520 --> 07:41:41,920 unsigned shorter things like that we 10131 07:41:39,520 --> 07:41:44,718 have seen all of these things but it can 10132 07:41:41,919 --> 07:41:46,397 also be an enum but enam is something we 10133 07:41:44,718 --> 07:41:48,558 haven't learned about yet we will learn 10134 07:41:46,398 --> 07:41:50,878 about it later when we have enough tools 10135 07:41:48,558 --> 07:41:52,159 to understand that but know about this 10136 07:41:52,159 --> 07:41:58,000 so if we can in our project in visual 10137 07:41:55,200 --> 07:42:00,878 studio code for example and say 10138 07:42:00,878 --> 07:42:06,637 john for example and use this as a 10139 07:42:03,599 --> 07:42:08,557 switch condition or expression we have 10140 07:42:06,637 --> 07:42:10,079 to include string for this to work so 10141 07:42:10,080 --> 07:42:14,798 and include the string library we're 10142 07:42:12,477 --> 07:42:17,840 going to do string and if we go down 10143 07:42:14,797 --> 07:42:20,319 again and try to do switch and use name 10144 07:42:17,840 --> 07:42:22,637 as an expression and take out everything 10145 07:42:20,319 --> 07:42:25,200 we have in here and try to compile this 10146 07:42:22,637 --> 07:42:27,599 you see that the compiler is not happy 10147 07:42:25,200 --> 07:42:30,878 okay it's going to say expression must 10148 07:42:27,599 --> 07:42:32,477 have an integral or enum type so this is 10149 07:42:30,878 --> 07:42:34,637 not even going to compile this is a 10150 07:42:32,477 --> 07:42:37,599 limitation you have and you have to make 10151 07:42:34,637 --> 07:42:41,039 sure what you pass as a condition here 10152 07:42:37,599 --> 07:42:43,519 is an integer something like ant or car 10153 07:42:41,040 --> 07:42:46,638 or other integral types we have learnt 10154 07:42:46,878 --> 07:42:51,279 this lecture we're going to learn about 10155 07:42:48,558 --> 07:42:54,878 ternary operators and this is an 10156 07:42:51,279 --> 07:42:56,159 alternative way to do tests with the if 10157 07:42:56,159 --> 07:43:01,279 suppose here we have a variable called 10158 07:42:58,398 --> 07:43:03,200 max we have two variables called a and b 10159 07:43:01,279 --> 07:43:04,717 and we want to find the maximum between 10160 07:43:04,718 --> 07:43:09,600 one easy way we can do this is say if a 10161 07:43:07,840 --> 07:43:12,878 is greater than b we're going to store 10162 07:43:09,599 --> 07:43:14,477 the maximum in max else then b is going 10163 07:43:12,878 --> 07:43:17,200 to be the maximum and we're going to 10164 07:43:14,477 --> 07:43:18,878 store that in our maximum value and 10165 07:43:17,200 --> 07:43:20,798 we're going to print this out if we do 10166 07:43:18,878 --> 07:43:23,360 this we're going to get whatever is the 10167 07:43:20,797 --> 07:43:25,279 maximum between a and b to be printed 10168 07:43:23,360 --> 07:43:28,000 out on the console and this is going to 10169 07:43:25,279 --> 07:43:30,397 work really well but we can also use a 10170 07:43:28,000 --> 07:43:32,240 ternary expression and the ternary 10171 07:43:32,240 --> 07:43:36,398 and it starts with a pair of parentheses 10172 07:43:34,957 --> 07:43:38,319 you're going to put your condition 10173 07:43:36,398 --> 07:43:40,240 inside the parenthesis you're going to 10174 07:43:38,319 --> 07:43:42,477 follow that with the question mark 10175 07:43:40,240 --> 07:43:44,080 you're going to put your option one and 10176 07:43:42,477 --> 07:43:46,718 after that you're going to put a column 10177 07:43:44,080 --> 07:43:49,200 and option two and because this is a 10178 07:43:46,718 --> 07:43:52,558 statement in c plus plus we're going to 10179 07:43:49,200 --> 07:43:54,798 end this with a semicolon here is an 10180 07:43:54,797 --> 07:44:00,079 using f as we have been doing all along 10181 07:43:57,840 --> 07:44:03,040 so we say if condition results equals 10182 07:44:00,080 --> 07:44:05,040 option one if condition is false result 10183 07:44:03,040 --> 07:44:07,040 is going to be equal to option two and 10184 07:44:05,040 --> 07:44:09,520 this is exactly the same thing we are 10185 07:44:07,040 --> 07:44:11,280 doing with our ternary expression here 10186 07:44:09,520 --> 07:44:13,600 if the condition is true we're going to 10187 07:44:11,279 --> 07:44:15,759 take option one and assign that to 10188 07:44:13,599 --> 07:44:18,957 result if it's false we're going to take 10189 07:44:15,759 --> 07:44:21,359 option two and assign that to resort so 10190 07:44:18,957 --> 07:44:24,159 this is really a syntactic sugar or a 10191 07:44:21,360 --> 07:44:25,840 shorthand for this thing here and it's 10192 07:44:24,159 --> 07:44:27,360 going to make your code shorter and 10193 07:44:25,840 --> 07:44:28,240 you're going to see this all over the 10194 07:44:28,240 --> 07:44:32,320 in c plus plus code out there so you 10195 07:44:32,319 --> 07:44:38,319 familiar with this okay so here is our 10196 07:44:35,599 --> 07:44:40,637 example of computing the maximum you see 10197 07:44:38,319 --> 07:44:43,520 that we have the same variables max a 10198 07:44:40,637 --> 07:44:45,759 and b but we have a one liner that is 10199 07:44:43,520 --> 07:44:47,279 going to store the maximum in max and we 10200 07:44:47,279 --> 07:44:52,637 and hopefully you can see that this is 10201 07:44:49,680 --> 07:44:54,240 shorter and better than this okay one 10202 07:44:52,637 --> 07:44:57,520 other thing you should know is that 10203 07:44:54,240 --> 07:45:00,638 option one and option two should be of 10204 07:44:57,520 --> 07:45:02,718 the same type or at least the types must 10205 07:45:00,637 --> 07:45:04,477 be convertible and if that fails you're 10206 07:45:02,718 --> 07:45:06,798 going to get a compiler error we can 10207 07:45:04,477 --> 07:45:09,360 also do ternary initialization here we 10208 07:45:09,360 --> 07:45:14,319 we initialize this to false we can use 10209 07:45:14,319 --> 07:45:19,680 initialize our speed variable here we 10210 07:45:16,878 --> 07:45:23,040 are basically saying if fast initialize 10211 07:45:25,680 --> 07:45:29,439 and we're going to print the speed here 10212 07:45:27,360 --> 07:45:31,760 and this is going to work and here you 10213 07:45:29,439 --> 07:45:34,239 see that i lied a little bit in my 10214 07:45:31,759 --> 07:45:36,957 explanation to make it really obvious 10215 07:45:34,240 --> 07:45:39,200 what we had in our ternary expression 10216 07:45:36,957 --> 07:45:41,759 you don't have to always wrap your 10217 07:45:39,200 --> 07:45:44,159 condition in parenthesis but it makes 10218 07:45:41,759 --> 07:45:47,039 the code much cleaner okay now that you 10219 07:45:44,159 --> 07:45:49,279 have an idea about distance it's time we 10220 07:45:47,040 --> 07:45:53,040 headed to visual studio code and 10221 07:45:49,279 --> 07:45:56,000 actually tried this and so for ourselves 10222 07:45:53,040 --> 07:45:58,878 okay here we are in our working folder 10223 07:45:56,000 --> 07:46:02,159 the current project is ternary operators 10224 07:45:58,878 --> 07:46:05,200 we're going to grab our template files 10225 07:46:02,159 --> 07:46:08,079 and put those where they belong it is in 10226 07:46:05,200 --> 07:46:09,760 this lecture here on ternary operators 10227 07:46:08,080 --> 07:46:12,558 and we're going to open this up in 10228 07:46:09,759 --> 07:46:14,637 visual studio code as we always do we're 10229 07:46:12,558 --> 07:46:17,280 going to open this and we're going to 10230 07:46:14,637 --> 07:46:19,840 open main cpp and take out whatever we 10231 07:46:17,279 --> 07:46:22,717 don't need we are going to go down and 10232 07:46:19,840 --> 07:46:25,520 put in our test code we basically have a 10233 07:46:22,718 --> 07:46:28,477 few variables let's kill these spaces so 10234 07:46:25,520 --> 07:46:31,360 that our code is not cluttered here and 10235 07:46:28,477 --> 07:46:33,439 we're going to print our maximum 10236 07:46:31,360 --> 07:46:36,319 and we're going to be using a regular if 10237 07:46:33,439 --> 07:46:39,279 we have three variables max a and b we 10238 07:46:36,319 --> 07:46:43,200 are saying if a is greater than b 10239 07:46:39,279 --> 07:46:45,360 we're going to store the maximum and max 10240 07:46:43,200 --> 07:46:46,477 otherwise we're going to store b 10241 07:46:46,477 --> 07:46:50,797 and this is going to allow us to print 10242 07:46:48,398 --> 07:46:53,520 whatever is the maximum between 35 and 10243 07:46:50,797 --> 07:46:54,957 20. if we build this let's bring up a 10244 07:46:54,957 --> 07:47:00,079 and we are going to build with gcc as we 10245 07:46:57,759 --> 07:47:02,159 always do let's bring this up a little 10246 07:47:02,159 --> 07:47:07,279 and run rooster we're going to see that 10247 07:47:04,159 --> 07:47:08,477 the maximum is 35 if we put our maximum 10248 07:47:08,477 --> 07:47:12,239 we're going to get the maximum steel so 10249 07:47:10,637 --> 07:47:14,637 we're going to run this out to build 10250 07:47:14,637 --> 07:47:18,477 and we're going to run this clear 10251 07:47:18,477 --> 07:47:23,680 max is 200 so we're going to get the 10252 07:47:20,477 --> 07:47:27,119 maximum regardless of where it is stored 10253 07:47:23,680 --> 07:47:30,080 in a or b this is how our code works but 10254 07:47:27,119 --> 07:47:31,279 we can use ternary operators to do the 10255 07:47:31,279 --> 07:47:35,199 so what we're going to do here we're 10256 07:47:32,637 --> 07:47:36,319 going to comment out our if statement 10257 07:47:36,319 --> 07:47:42,159 a ternary version so what we're going to 10258 07:47:39,279 --> 07:47:44,957 do we're going to say result equals 10259 07:47:42,159 --> 07:47:47,200 and we're going to say a greater than b 10260 07:47:44,957 --> 07:47:48,319 we're going to put a question mark to 10261 07:47:49,520 --> 07:47:54,637 start of our two options option one is 10262 07:47:51,919 --> 07:47:57,599 going to be a because if a is greater 10263 07:47:54,637 --> 07:48:00,637 than b then a is going to be the maximum 10264 07:47:57,599 --> 07:48:02,557 so we're going to grab a here and if a 10265 07:48:02,558 --> 07:48:06,638 then b must be the maximum so we're 10266 07:48:04,558 --> 07:48:08,878 going to put that as a second option 10267 07:48:06,637 --> 07:48:10,637 here this is what we mean with this 10268 07:48:08,878 --> 07:48:12,637 statement and we don't have a variable 10269 07:48:10,637 --> 07:48:14,718 called results that's why we have a 10270 07:48:12,637 --> 07:48:17,360 problem here we're going to put our 10271 07:48:14,718 --> 07:48:19,040 result in max and this should 10272 07:48:19,040 --> 07:48:23,760 you see the squiggly lines go away and 10273 07:48:22,000 --> 07:48:26,477 we can build this and we're going to get 10274 07:48:23,759 --> 07:48:29,199 exactly the same result so this is our 10275 07:48:26,477 --> 07:48:31,919 ternary operator if we build this it is 10276 07:48:29,200 --> 07:48:33,520 going to go through and if we run this 10277 07:48:31,919 --> 07:48:36,797 we're going to get exactly the same 10278 07:48:33,520 --> 07:48:39,439 result max is 200 and it is what we 10279 07:48:36,797 --> 07:48:43,039 expect okay so take some time and really 10280 07:48:39,439 --> 07:48:44,877 make yourself familiar with this syntax 10281 07:48:43,040 --> 07:48:47,760 it is really nothing complicated you 10282 07:48:44,878 --> 07:48:49,840 just have to make sure your brain is 10283 07:48:47,759 --> 07:48:51,599 aware of something like this and you're 10284 07:48:49,840 --> 07:48:54,319 going to recognize that everywhere you 10285 07:48:51,599 --> 07:48:57,119 see it in code out there okay we have 10286 07:48:54,319 --> 07:48:59,680 mentioned that you can't pass types that 10287 07:48:57,119 --> 07:49:02,718 are not compatible and the requirement 10288 07:48:59,680 --> 07:49:05,680 is that a and b be of the same type 10289 07:49:02,718 --> 07:49:08,718 or at least types you can do conversions 10290 07:49:05,680 --> 07:49:10,558 between so for example if we try to do 10291 07:49:13,200 --> 07:49:19,360 we can't really turn a strength into a 10292 07:49:17,040 --> 07:49:21,760 integer and this is going to give us a 10293 07:49:19,360 --> 07:49:23,760 compiler error and let's look at the 10294 07:49:21,759 --> 07:49:26,557 error we see from visual studio code 10295 07:49:23,759 --> 07:49:29,199 operand types are incompatible and and 10296 07:49:26,558 --> 07:49:31,760 the const car are not compatible and 10297 07:49:29,200 --> 07:49:33,520 they have to be compatible if we ignore 10298 07:49:31,759 --> 07:49:36,477 this and go ahead and build we're going 10299 07:49:33,520 --> 07:49:39,840 to also see an error from the compiler 10300 07:49:36,477 --> 07:49:41,520 and it should be the same error so 10301 07:49:41,520 --> 07:49:46,000 question mark and column have different 10302 07:49:47,840 --> 07:49:52,878 stands you can really compare so for 10303 07:49:50,398 --> 07:49:55,680 example we can take out this string here 10304 07:49:52,878 --> 07:49:59,119 and put in a floating point let's put in 10305 07:49:55,680 --> 07:50:00,637 a 22.5 and say that this is a fluid 10306 07:49:59,119 --> 07:50:02,557 you're going to see that the problem is 10307 07:50:00,637 --> 07:50:04,637 going to go away here let's wait for a 10308 07:50:02,558 --> 07:50:06,638 minute it's not going away we're going 10309 07:50:04,637 --> 07:50:08,957 to build anyway you see the world is 10310 07:50:08,957 --> 07:50:13,439 so that we don't have all this clutter 10311 07:50:10,878 --> 07:50:15,840 you see problems by the way and if we 10312 07:50:13,439 --> 07:50:17,520 both again let's try to do that the 10313 07:50:15,840 --> 07:50:19,680 builder is going to go through and if we 10314 07:50:19,680 --> 07:50:26,240 max is 22 because we are comparing a and 10315 07:50:23,599 --> 07:50:28,239 22 here and the compiler inserted an 10316 07:50:26,240 --> 07:50:30,638 implicit conversion from this floating 10317 07:50:28,240 --> 07:50:32,159 point to integer that's why we are 10318 07:50:32,159 --> 07:50:37,119 so what is the type that's the compiler 10319 07:50:34,797 --> 07:50:39,759 really used here okay to make it really 10320 07:50:37,119 --> 07:50:40,878 clear what is going on we're not going 10321 07:50:40,878 --> 07:50:46,718 use max here let's say auto max one 10322 07:50:44,878 --> 07:50:48,319 and see what the compiler is really 10323 07:50:48,319 --> 07:50:53,200 so we're going to take this out 10324 07:50:50,878 --> 07:50:56,159 and by this we're going to see 10325 07:50:53,200 --> 07:50:58,637 what implicit conversion the compiler 10326 07:50:56,159 --> 07:51:00,718 really did here because what we had 10327 07:50:58,637 --> 07:51:03,039 before we were doing an assignment so 10328 07:51:00,718 --> 07:51:04,878 whatever was on the left was being 10329 07:51:03,040 --> 07:51:07,440 turned into an integer because we were 10330 07:51:04,878 --> 07:51:09,680 doing an implicit conversion from an 10331 07:51:07,439 --> 07:51:12,079 assignment so whatever is on the left is 10332 07:51:09,680 --> 07:51:14,159 turned into the type of whatever we have 10333 07:51:12,080 --> 07:51:16,398 on the left of the assignment i hope you 10334 07:51:14,159 --> 07:51:18,797 know this already but if we do things 10335 07:51:16,398 --> 07:51:21,360 like this we're going to deduce whatever 10336 07:51:18,797 --> 07:51:23,199 is on the left and assign that into max 10337 07:51:21,360 --> 07:51:24,958 one so we're going to do so we're going 10338 07:51:23,200 --> 07:51:27,360 to see the implicit conversion that the 10339 07:51:24,957 --> 07:51:29,680 compiler did in here and it is good to 10340 07:51:27,360 --> 07:51:31,520 be able to see things like that it is a 10341 07:51:31,520 --> 07:51:36,000 let's see what happens here we're going 10342 07:51:32,957 --> 07:51:39,359 to print max one and i'm going to world 10343 07:51:36,000 --> 07:51:41,360 with gcc this is going to go through 10344 07:51:39,360 --> 07:51:43,279 and we're going to see what is printed 10345 07:51:43,279 --> 07:51:47,360 and you're going to see that the 10346 07:51:44,477 --> 07:51:50,159 implicit conversion happened from end to 10347 07:51:47,360 --> 07:51:52,558 flood and we deduce the flood here in 10348 07:51:50,159 --> 07:51:54,878 max one so this is really interesting 10349 07:51:52,558 --> 07:51:56,798 one might have thought that we had a 10350 07:51:56,797 --> 07:52:01,439 to end but it was actually the reverse 10351 07:51:59,279 --> 07:52:02,797 and this is quite interesting this is 10352 07:52:01,439 --> 07:52:05,279 really all we set out to do in this 10353 07:52:02,797 --> 07:52:07,439 lecture i hope you found it interesting 10354 07:52:05,279 --> 07:52:09,520 again make sure you have this syntax 10355 07:52:07,439 --> 07:52:11,279 here drilled in your brain because 10356 07:52:09,520 --> 07:52:14,080 you're going to be seeing this a lot in 10357 07:52:11,279 --> 07:52:16,477 your career as a software developer not 10358 07:52:14,080 --> 07:52:18,558 only a c plus plus developer actually 10359 07:52:16,477 --> 07:52:20,119 and it is going to be helpful 10360 07:52:20,119 --> 07:52:24,797 congratulations on hitting the end of 10361 07:52:22,398 --> 07:52:26,878 this chapter this chapter was all about 10362 07:52:24,797 --> 07:52:29,199 flow control and we had the chance to 10363 07:52:26,878 --> 07:52:32,000 learn about many of the tools that c 10364 07:52:29,200 --> 07:52:34,000 plus plus provides to do conditional 10365 07:52:32,000 --> 07:52:35,919 programming and we were able to see all 10366 07:52:34,000 --> 07:52:38,240 kinds of crazy things we had the chance 10367 07:52:35,919 --> 07:52:40,637 to do things like if red is true we're 10368 07:52:38,240 --> 07:52:42,638 going to run the code inside if yellow 10369 07:52:40,637 --> 07:52:44,477 is true we're going to run code inside 10370 07:52:42,637 --> 07:52:46,319 if green is true we're going to run code 10371 07:52:44,477 --> 07:52:49,439 inside and you had a chance to play with 10372 07:52:46,319 --> 07:52:51,439 those things on many occasions we have 10373 07:52:49,439 --> 07:52:53,599 seen that the tools that c plus plus 10374 07:52:51,439 --> 07:52:56,239 provides really revolve around these 10375 07:52:53,599 --> 07:52:58,477 four things here we have if statements 10376 07:52:58,477 --> 07:53:05,119 on this if statements we can do else if 10377 07:53:02,319 --> 07:53:07,360 to really do long chains of things where 10378 07:53:05,119 --> 07:53:10,079 we do different decisions to do things 10379 07:53:07,360 --> 07:53:12,319 in our applications we also saw that 10380 07:53:10,080 --> 07:53:14,398 switch is an option if you have several 10381 07:53:12,319 --> 07:53:17,119 decisions you can make in your 10382 07:53:14,398 --> 07:53:20,080 application and we saw that a ternary 10383 07:53:17,119 --> 07:53:22,718 operator can really be used to do 10384 07:53:20,080 --> 07:53:25,120 two decisions if you have two decisions 10385 07:53:22,718 --> 07:53:27,920 you're going to be able to reduce the 10386 07:53:25,119 --> 07:53:30,477 number of lines you used to do these 10387 07:53:27,919 --> 07:53:32,637 decisions with to one line and we had a 10388 07:53:30,477 --> 07:53:35,520 chance to really look at that in detail 10389 07:53:32,637 --> 07:53:37,520 in the lecture on ternary operators this 10390 07:53:35,520 --> 07:53:40,000 is really all i had to share in this 10391 07:53:37,520 --> 07:53:41,439 chapter i hope you found it interesting 10392 07:53:40,000 --> 07:53:43,200 we are going to stop here in this 10393 07:53:41,439 --> 07:53:46,079 lecture in the next chapter we're going 10394 07:53:43,200 --> 07:53:48,398 to start and learn about loops 10395 07:53:46,080 --> 07:53:50,638 go ahead and finish up here and meet me 10396 07:53:48,398 --> 07:53:53,360 there in this chapter we're going to be 10397 07:53:50,637 --> 07:53:56,079 learning about loops and this is a 10398 07:53:53,360 --> 07:53:58,637 construct that is provided by the c plus 10399 07:53:56,080 --> 07:54:03,360 plus programming language to allow you 10400 07:53:58,637 --> 07:54:06,637 to do repetitive tasks relatively easily 10401 07:54:03,360 --> 07:54:08,319 suppose we have a task to print a 10402 07:54:08,319 --> 07:54:13,279 we can go down and do it like this and 10403 07:54:10,878 --> 07:54:14,398 it is going to work but what if they try 10404 07:54:14,398 --> 07:54:18,000 do this 100 times then you're going to 10405 07:54:16,477 --> 07:54:20,079 start scratching your head you're going 10406 07:54:18,000 --> 07:54:22,000 to type this 100 times and you're going 10407 07:54:20,080 --> 07:54:24,160 to run your program and it is going to 10408 07:54:22,000 --> 07:54:26,558 work then your manager comes up and they 10409 07:54:24,159 --> 07:54:28,797 say i want you to do this 10 000 times 10410 07:54:26,558 --> 07:54:30,558 or even 100 000 times and then you're 10411 07:54:28,797 --> 07:54:32,717 going to start pulling your hair or 10412 07:54:30,558 --> 07:54:34,878 running around breaking vents because 10413 07:54:32,718 --> 07:54:37,840 that's impossible to do not to worry 10414 07:54:34,878 --> 07:54:40,000 here c plus plus provides a way to do 10415 07:54:37,840 --> 07:54:42,319 this relatively easily and we're going 10416 07:54:40,000 --> 07:54:45,119 to be learning about ways we can do this 10417 07:54:42,319 --> 07:54:46,878 in our program in this chapter the 10418 07:54:45,119 --> 07:54:49,520 constructs we're going to be using are 10419 07:54:46,878 --> 07:54:51,760 these you see here there is a for loop a 10420 07:54:49,520 --> 07:54:54,398 range base for a loop a while loop and 10421 07:54:51,759 --> 07:54:57,840 do a while loop and we can make use of 10422 07:54:54,398 --> 07:55:00,798 these tools to not go crazy printing a 10423 07:54:57,840 --> 07:55:01,920 message one million times because that's 10424 07:55:01,919 --> 07:55:05,599 okay so that's what we are up against in 10425 07:55:04,159 --> 07:55:07,759 this chapter we're going to be learning 10426 07:55:05,599 --> 07:55:09,840 about these loop constructs and you're 10427 07:55:07,759 --> 07:55:12,159 going to find that they are really cool 10428 07:55:09,840 --> 07:55:14,319 for your application i am excited to be 10429 07:55:12,159 --> 07:55:16,637 teaching you this so let's get started 10430 07:55:14,319 --> 07:55:19,599 in the next lecture and learn about the 10431 07:55:16,637 --> 07:55:21,439 for loop go ahead and finish up here and 10432 07:55:19,599 --> 07:55:24,239 meet me there in this lecture we're 10433 07:55:21,439 --> 07:55:26,557 going to learn about four loops 10434 07:55:24,240 --> 07:55:29,120 for loops are one of the constructs we 10435 07:55:26,558 --> 07:55:31,440 have in c plus plus to do repetitive 10436 07:55:29,119 --> 07:55:33,520 tasks they allow us to do something like 10437 07:55:31,439 --> 07:55:36,717 this much more easily for example we 10438 07:55:33,520 --> 07:55:40,240 don't have to type stdc out of c plus 10439 07:55:36,718 --> 07:55:42,398 plus 10 times or 100 times we can do 10440 07:55:40,240 --> 07:55:44,558 this in less than five lines and you're 10441 07:55:42,398 --> 07:55:47,680 going to see how cool this is the syntax 10442 07:55:44,558 --> 07:55:50,398 for for loops looks like this and it may 10443 07:55:47,680 --> 07:55:53,200 look intimidating for first users but 10444 07:55:50,398 --> 07:55:56,159 we're going to break this down we have a 10445 07:55:53,200 --> 07:55:58,637 few parts in here the first part is the 10446 07:55:56,159 --> 07:56:01,439 iterator which is a variable that we're 10447 07:55:58,637 --> 07:56:03,520 going to be using to navigate through 10448 07:56:01,439 --> 07:56:06,557 the loop and that variable is this 10449 07:56:03,520 --> 07:56:09,439 unsigned int i here we are going to 10450 07:56:06,558 --> 07:56:12,477 initialize this to zero and the value we 10451 07:56:09,439 --> 07:56:14,797 initialize this with is going to be our 10452 07:56:12,477 --> 07:56:17,360 starting value and that's another key 10453 07:56:14,797 --> 07:56:19,599 point of any loop any loop is going to 10454 07:56:17,360 --> 07:56:22,159 have a starting point we are also going 10455 07:56:19,599 --> 07:56:25,199 to have a test which is going to control 10456 07:56:22,159 --> 07:56:27,680 when our loop ends for example this loop 10457 07:56:25,200 --> 07:56:30,398 is going to start with i equal to zero 10458 07:56:27,680 --> 07:56:32,398 it is going to keep doing stuff until 10459 07:56:32,398 --> 07:56:37,360 less than 10. as long as i is less than 10460 07:56:35,520 --> 07:56:40,000 10 it's going to keep doing whatever it 10461 07:56:37,360 --> 07:56:43,279 is we want to do and the first part of 10462 07:56:40,000 --> 07:56:45,279 the loop is this plus plus i thing here 10463 07:56:43,279 --> 07:56:46,797 and this is an incrementation part 10464 07:56:45,279 --> 07:56:49,199 that's how i'm going to call it for 10465 07:56:46,797 --> 07:56:52,477 simplicity but at least a part that 10466 07:56:49,200 --> 07:56:55,040 keeps changing our iterator to make our 10467 07:56:52,477 --> 07:56:57,520 loop move forward after these four 10468 07:56:55,040 --> 07:57:00,558 points we are going to have a pair of 10469 07:56:57,520 --> 07:57:03,119 curly braces and inside these curly 10470 07:57:00,558 --> 07:57:05,040 braces we're going to have the body of 10471 07:57:03,119 --> 07:57:08,239 our loop and this is the code we want to 10472 07:57:05,040 --> 07:57:10,638 execute multiple times and inside this 10473 07:57:08,240 --> 07:57:12,240 loop body we can really do all kinds of 10474 07:57:10,637 --> 07:57:14,397 crazy things you're going to see how 10475 07:57:12,240 --> 07:57:16,958 cool this is again we have a few parts 10476 07:57:14,398 --> 07:57:19,280 with our loop we have our iterator which 10477 07:57:16,957 --> 07:57:21,759 is this variable i here we're going to 10478 07:57:19,279 --> 07:57:24,397 be initializing this with a value that 10479 07:57:21,759 --> 07:57:26,557 denotes where we want our loop to start 10480 07:57:24,398 --> 07:57:28,958 we then have a test which is going to 10481 07:57:26,558 --> 07:57:30,638 control when the loop is going to end 10482 07:57:28,957 --> 07:57:33,199 we're going to have our incrementation 10483 07:57:30,637 --> 07:57:35,119 part which is going to allow our loop to 10484 07:57:33,200 --> 07:57:36,798 move forward and it's going to be doing 10485 07:57:35,119 --> 07:57:39,520 that through incrementation or 10486 07:57:36,797 --> 07:57:41,439 decrementation but to make explanation 10487 07:57:39,520 --> 07:57:44,637 easier here we're going to call this 10488 07:57:41,439 --> 07:57:46,319 part the incrementation part of the loop 10489 07:57:44,637 --> 07:57:48,397 after that we're going to have the body 10490 07:57:46,319 --> 07:57:50,878 of the loop and it's going to be within 10491 07:57:48,398 --> 07:57:53,440 this curly braces and it's going to be 10492 07:57:50,878 --> 07:57:55,840 whatever it is we want to execute inside 10493 07:57:53,439 --> 07:57:57,919 this loop okay these are the parts of a 10494 07:57:55,840 --> 07:58:00,240 loop and you need to keep this in mind 10495 07:57:57,919 --> 07:58:01,279 okay now that you know the main parts of 10496 07:58:01,279 --> 07:58:05,439 how does it really work let's go through 10497 07:58:03,279 --> 07:58:08,319 this and see how it's going to print i 10498 07:58:05,439 --> 07:58:10,477 love c plus plus 10 times so when this 10499 07:58:08,319 --> 07:58:12,957 code gets executed we're going to start 10500 07:58:10,477 --> 07:58:15,840 with i equal to 0 because that's what we 10501 07:58:12,957 --> 07:58:18,000 are initializing this value with 10502 07:58:15,840 --> 07:58:19,680 after the incrementation part we're 10503 07:58:19,680 --> 07:58:25,439 and the test is going to say is i is 0 10504 07:58:23,119 --> 07:58:28,477 less than 10 and that's going to be true 10505 07:58:25,439 --> 07:58:30,477 so if the test turns out to be true 10506 07:58:28,477 --> 07:58:32,797 we're going to jump into the loop body 10507 07:58:30,477 --> 07:58:36,319 and if it's false we're going to jump to 10508 07:58:32,797 --> 07:58:39,439 the end of the loop and execute whatever 10509 07:58:36,319 --> 07:58:41,599 is after our loop this is how it works 10510 07:58:39,439 --> 07:58:44,000 so if we jump in for the first time when 10511 07:58:41,599 --> 07:58:46,159 i is 0 we're going to print i love c 10512 07:58:44,000 --> 07:58:48,080 plus plus for the first time then after 10513 07:58:46,159 --> 07:58:50,477 the body is executed we're going to 10514 07:58:48,080 --> 07:58:53,200 execute the incrementation part 10515 07:58:50,477 --> 07:58:54,637 this is going to make i equal to 1 then 10516 07:58:53,200 --> 07:58:57,440 we're going to come back to the test 10517 07:58:54,637 --> 07:58:59,279 we're going to say is 1 less than 10 10518 07:58:57,439 --> 07:59:01,759 that's going to be true we're going to 10519 07:58:59,279 --> 07:59:04,079 come in the body and print i love c plus 10520 07:59:01,759 --> 07:59:05,679 plus for the second time after that the 10521 07:59:04,080 --> 07:59:08,878 incrementation part is going to run 10522 07:59:05,680 --> 07:59:11,360 we're going to have a 2 inside i here 2 10523 07:59:08,878 --> 07:59:12,878 is less than 10 this is going to be true 10524 07:59:11,360 --> 07:59:15,119 so we're going to follow him the body 10525 07:59:12,878 --> 07:59:17,520 again and print i love c plus plus for 10526 07:59:15,119 --> 07:59:20,718 the third time we're going to keep doing 10527 07:59:17,520 --> 07:59:22,797 this until we hit nine for example if we 10528 07:59:20,718 --> 07:59:25,920 increment and get a nine we're going to 10529 07:59:22,797 --> 07:59:27,520 form the test and say is nine less than 10530 07:59:25,919 --> 07:59:30,000 ten that's going to be true so we're 10531 07:59:27,520 --> 07:59:31,360 going to fall in here and print i love c 10532 07:59:31,360 --> 07:59:36,558 and after that we're going to run the 10533 07:59:33,520 --> 07:59:38,878 incrementation part again this time i is 10534 07:59:36,558 --> 07:59:42,240 going to become 10 we're going to do the 10535 07:59:38,878 --> 07:59:43,680 test 10 is not less than 10 so the test 10536 07:59:43,680 --> 07:59:48,878 and we're going to fall on the outside 10537 07:59:46,080 --> 07:59:52,080 of the loop here and by that time we'll 10538 07:59:48,878 --> 07:59:54,398 have printed i love c plus plus 10 times 10539 07:59:52,080 --> 07:59:56,320 and this is really how a loop works for 10540 07:59:54,398 --> 07:59:59,040 the first time we're going to run this 10541 07:59:56,319 --> 08:00:01,520 initializer part here to initialize our 10542 07:59:59,040 --> 08:00:03,040 iterator which is this value i 10543 08:00:01,520 --> 08:00:04,159 and then after that we're going to run 10544 08:00:04,159 --> 08:00:08,000 if the test is good if it's successful 10545 08:00:06,240 --> 08:00:09,840 we're going to fall in the body we're 10546 08:00:08,000 --> 08:00:11,759 going to execute whatever code is in the 10547 08:00:09,840 --> 08:00:14,240 body and after that we're going to keep 10548 08:00:11,759 --> 08:00:16,319 doing the cycle increment test the body 10549 08:00:19,439 --> 08:00:24,797 we hit a point where the test fails and 10550 08:00:22,718 --> 08:00:26,000 if the test fails we're going to fall 10551 08:00:26,000 --> 08:00:31,439 this for loop here we're going to 10552 08:00:28,398 --> 08:00:33,840 execute whatever is after this closing 10553 08:00:31,439 --> 08:00:35,919 curly brace and this is how a loop works 10554 08:00:33,840 --> 08:00:38,797 i want you to go through this one more 10555 08:00:35,919 --> 08:00:41,599 time by printing i love c plus plus and 10556 08:00:38,797 --> 08:00:42,477 making sure that this is printed 10 10557 08:00:42,477 --> 08:00:47,599 and one thing you should have noticed 10558 08:00:44,319 --> 08:00:49,279 now is that we use this test to control 10559 08:00:47,599 --> 08:00:51,919 how many times the loop is going to 10560 08:00:49,279 --> 08:00:54,797 start for example if we wanted it to run 10561 08:00:51,919 --> 08:00:57,759 20 times we would start from zero and 10562 08:00:54,797 --> 08:00:59,599 and at 20. and you don't always have to 10563 08:00:57,759 --> 08:01:01,519 start from zero you can really start 10564 08:00:59,599 --> 08:01:04,957 from anywhere for example you can 10565 08:01:01,520 --> 08:01:08,240 initialize i with a value like 30 and 10566 08:01:04,957 --> 08:01:10,159 end when i is equal to 55. let's say 10567 08:01:08,240 --> 08:01:13,120 that as an example you can really do 10568 08:01:10,159 --> 08:01:15,439 anything the main important thing is to 10569 08:01:13,119 --> 08:01:18,159 understand how a loop works one thing i 10570 08:01:15,439 --> 08:01:20,239 want you to notice is that we are saying 10571 08:01:23,279 --> 08:01:29,439 iterators are a common thing in c plus 10572 08:01:26,080 --> 08:01:31,760 plus code and they're not just used for 10573 08:01:29,439 --> 08:01:34,957 loops like we are doing it here there is 10574 08:01:31,759 --> 08:01:36,717 a type we use to do iterators and it 10575 08:01:34,957 --> 08:01:38,877 makes our code much more readable and 10576 08:01:38,878 --> 08:01:45,119 this event is a representation of some 10577 08:01:42,000 --> 08:01:46,797 unsigned and type in your c plus plus 10578 08:01:45,119 --> 08:01:48,878 compiler this is going to be different 10579 08:01:46,797 --> 08:01:52,319 from compiler to compiler but for 10580 08:01:48,878 --> 08:01:55,040 example on my system this is eight bytes 10581 08:01:52,319 --> 08:01:57,520 so it has quite a good range of values 10582 08:01:55,040 --> 08:02:00,477 you can put in there if we use size t 10583 08:01:57,520 --> 08:02:02,080 type to declare our i iterator here 10584 08:02:00,477 --> 08:02:04,159 we're going to say something like this 10585 08:02:04,159 --> 08:02:07,840 we're going to put our curly braces 10586 08:02:05,919 --> 08:02:09,839 we're going to put a semicolon we're 10587 08:02:07,840 --> 08:02:12,637 going to put our test and we're going to 10588 08:02:09,840 --> 08:02:15,040 put our incrementation part and i think 10589 08:02:12,637 --> 08:02:17,360 i really emphasize this but the 10590 08:02:15,040 --> 08:02:20,958 parts of our loop inside this 10591 08:02:17,360 --> 08:02:23,279 parenthesis are separated by semicolons 10592 08:02:20,957 --> 08:02:25,680 like this so we have the iterator 10593 08:02:23,279 --> 08:02:28,239 declaration we have a semicolon we have 10594 08:02:25,680 --> 08:02:30,477 the test we have a semicolon and we put 10595 08:02:28,240 --> 08:02:33,040 our incrementation part and then we go 10596 08:02:30,477 --> 08:02:35,360 on and put our curlies and inside we can 10597 08:02:33,040 --> 08:02:37,520 do whatever it is we want to do 10598 08:02:35,360 --> 08:02:39,520 inside the loop so size t is something 10599 08:02:37,520 --> 08:02:42,080 you're going to see a lot make sure you 10600 08:02:39,520 --> 08:02:45,840 know what it means remember that it is a 10601 08:02:42,080 --> 08:02:47,920 representation of an unsigned and type 10602 08:02:45,840 --> 08:02:50,558 so it can only represent positive 10603 08:02:47,919 --> 08:02:53,439 numbers and it is used to represent 10604 08:02:50,558 --> 08:02:56,159 things like sizes in c plus plus thank 10605 08:02:53,439 --> 08:02:58,477 the number of students in a classroom 10606 08:02:56,159 --> 08:03:00,957 for example that can never be negative 10607 08:02:58,477 --> 08:03:02,637 think about the number of seats in a car 10608 08:03:00,957 --> 08:03:05,520 that's something that is always going to 10609 08:03:02,637 --> 08:03:08,000 be positive and it won't make sense for 10610 08:03:05,520 --> 08:03:11,360 that to be negative so for those kinds 10611 08:03:08,000 --> 08:03:13,599 of things we can use size t to represent 10612 08:03:11,360 --> 08:03:15,279 the size of those things and you're 10613 08:03:13,599 --> 08:03:17,279 going to see how this works in a minute 10614 08:03:15,279 --> 08:03:20,079 okay now that you have a pretty good 10615 08:03:17,279 --> 08:03:22,239 idea about how a loop works and the main 10616 08:03:20,080 --> 08:03:24,478 parts that make it up i want you to know 10617 08:03:22,240 --> 08:03:27,360 that you can also do all kinds of crazy 10618 08:03:24,477 --> 08:03:30,000 operations inside the body of your loop 10619 08:03:27,360 --> 08:03:32,797 for example here we are printing out the 10620 08:03:30,000 --> 08:03:35,119 current value of our iterator and we're 10621 08:03:32,797 --> 08:03:37,840 going to print the double that we can do 10622 08:03:35,119 --> 08:03:40,637 this we can also leave out the curly 10623 08:03:37,840 --> 08:03:43,040 braces and this is only valid if we have 10624 08:03:40,637 --> 08:03:45,119 one statement we want to run in the loop 10625 08:03:43,040 --> 08:03:47,200 so this is going to print this message 10626 08:03:45,119 --> 08:03:49,840 here five times because this loop is 10627 08:03:47,200 --> 08:03:52,558 going from zero all the way to four it 10628 08:03:49,840 --> 08:03:54,878 is going to stop when i stops being less 10629 08:03:52,558 --> 08:03:56,638 than five and this is going to be five 10630 08:03:54,878 --> 08:03:58,240 times if you go through this you're 10631 08:03:56,637 --> 08:04:00,397 going to see that this is going to print 10632 08:03:58,240 --> 08:04:03,040 five times you should also know that 10633 08:04:00,398 --> 08:04:06,159 this iterator that we are declaring like 10634 08:04:03,040 --> 08:04:08,878 this is going to be scoped inside the 10635 08:04:06,159 --> 08:04:11,520 body of the loop so you can only use it 10636 08:04:08,878 --> 08:04:15,040 inside these curly braces and if you try 10637 08:04:11,520 --> 08:04:17,680 to use it outside for example here after 10638 08:04:15,040 --> 08:04:19,280 the closing curly braces of the body of 10639 08:04:17,680 --> 08:04:21,520 the loop you're going to get a compiler 10640 08:04:19,279 --> 08:04:24,239 error again you can only use this 10641 08:04:21,520 --> 08:04:27,600 variable i which is our iterator 10642 08:04:24,240 --> 08:04:30,478 inside the curlies that mark the 10643 08:04:27,599 --> 08:04:32,557 start and the end of our loop body if 10644 08:04:30,477 --> 08:04:35,759 you want the iterator to be usable 10645 08:04:32,558 --> 08:04:37,280 outside the scope of the for loop you 10646 08:04:35,759 --> 08:04:40,239 can do something like this you can 10647 08:04:37,279 --> 08:04:41,840 declare your iterator outside the loop 10648 08:04:40,240 --> 08:04:43,760 and you can initialize that with 10649 08:04:41,840 --> 08:04:46,477 whatever you want and then you can set 10650 08:04:43,759 --> 08:04:49,039 up your loop like this you can say for j 10651 08:04:46,477 --> 08:04:51,520 and put and your test and your 10652 08:04:49,040 --> 08:04:52,958 incrementation part and inside do 10653 08:04:51,520 --> 08:04:55,520 whatever it is you want to do in the 10654 08:04:52,957 --> 08:04:58,159 body of the look if we do it like this 10655 08:04:55,520 --> 08:05:00,319 then j is going to be usable outside of 10656 08:04:58,159 --> 08:05:02,797 the loop and we're going to see its 10657 08:05:00,319 --> 08:05:04,878 value if we print it out like this so 10658 08:05:02,797 --> 08:05:08,557 this is also something you can do 10659 08:05:04,878 --> 08:05:09,440 you can even go all crazy and leave out 10660 08:05:09,439 --> 08:05:13,279 declaration part if you happen to have 10661 08:05:13,279 --> 08:05:18,239 iterator declared outside so if you do 10662 08:05:16,477 --> 08:05:20,477 something like this it is also going to 10663 08:05:18,240 --> 08:05:22,798 work okay the last thing i want you to 10664 08:05:20,477 --> 08:05:25,119 see before we head over to visual studio 10665 08:05:22,797 --> 08:05:27,840 code and actually try this out 10666 08:05:25,119 --> 08:05:29,840 is that it is a bad thing to hard code 10667 08:05:27,840 --> 08:05:32,398 your values in for example you see here 10668 08:05:29,840 --> 08:05:34,957 we are saying j less than 10 10669 08:05:32,398 --> 08:05:37,280 this is really bad design because every 10670 08:05:34,957 --> 08:05:39,520 time you need to change the number of 10671 08:05:37,279 --> 08:05:41,199 times that your loop is going to run 10672 08:05:39,520 --> 08:05:43,360 you're going to have to hunt for these 10673 08:05:41,200 --> 08:05:45,920 loops and change these values and this 10674 08:05:43,360 --> 08:05:48,477 may be in a lot of places in your code 10675 08:05:45,919 --> 08:05:51,039 so a good thing to do is to put the 10676 08:05:48,477 --> 08:05:53,040 value that controls how many times your 10677 08:05:51,040 --> 08:05:55,120 loop is going to run in a variable like 10678 08:05:53,040 --> 08:05:56,958 this we're going to store that value in 10679 08:05:56,957 --> 08:06:01,279 notice that it is of size t because it 10680 08:05:59,360 --> 08:06:03,360 is a representation of the size of 10681 08:06:01,279 --> 08:06:05,759 things something we don't really want to 10682 08:06:03,360 --> 08:06:09,279 be negative and we're going to use this 10683 08:06:05,759 --> 08:06:11,359 in our test part of our for loop here 10684 08:06:09,279 --> 08:06:14,079 instead of saying j less than 10 we're 10685 08:06:11,360 --> 08:06:16,797 going to say j less than count and if we 10686 08:06:14,080 --> 08:06:19,280 want to change this we have one point of 10687 08:06:16,797 --> 08:06:20,878 control to go and change this value here 10688 08:06:19,279 --> 08:06:23,199 and this is really good design you 10689 08:06:20,878 --> 08:06:25,760 should do this okay we have talked a lot 10690 08:06:23,200 --> 08:06:28,319 about loops it is time we headed over to 10691 08:06:25,759 --> 08:06:30,797 visual studio code and actually tried 10692 08:06:28,319 --> 08:06:33,279 these things out here we are in visual 10693 08:06:30,797 --> 08:06:35,360 studio code the current project is for 10694 08:06:33,279 --> 08:06:37,919 loop here we're going to grab the code 10695 08:06:37,919 --> 08:06:41,759 and we're going to put that in place 10696 08:06:41,759 --> 08:06:46,957 and we're going to open this in visual 10697 08:06:43,840 --> 08:06:48,957 studio code so let's do that pretty fast 10698 08:06:46,957 --> 08:06:51,439 for loop that's the current project we 10699 08:06:48,957 --> 08:06:52,717 selected and we opened this in visual 10700 08:06:52,718 --> 08:06:58,159 we're going to open up our main cpp file 10701 08:06:55,680 --> 08:07:00,000 which is going to show up here and we're 10702 08:06:58,159 --> 08:07:00,878 going to remove things we don't need 10703 08:07:00,878 --> 08:07:05,360 so let's remove that okay the first 10704 08:07:03,040 --> 08:07:06,718 thing i want you to do is the bad way to 10705 08:07:06,718 --> 08:07:12,477 again if we want to print i love c plus 10706 08:07:09,119 --> 08:07:14,319 plus 10 times we can do it like this and 10707 08:07:12,477 --> 08:07:16,558 if we run the program it's going to work 10708 08:07:14,319 --> 08:07:18,797 but this is really bad design because 10709 08:07:16,558 --> 08:07:20,798 you would have to copy and paste this 10710 08:07:18,797 --> 08:07:22,717 for example a thousand times or even a 10711 08:07:20,797 --> 08:07:24,957 hundred thousand of times and that's 10712 08:07:22,718 --> 08:07:26,398 really bad so if we bring up our 10713 08:07:26,398 --> 08:07:31,040 and build with gcc like we always do 10714 08:07:31,919 --> 08:07:36,877 the build is going to go through you see 10715 08:07:34,398 --> 08:07:38,478 it's successful we're going to clear and 10716 08:07:36,878 --> 08:07:40,958 we're going to run rooster and it's 10717 08:07:38,477 --> 08:07:43,119 going to say lfc plus plus 10 times if 10718 08:07:40,957 --> 08:07:45,119 you count this it's going to be 10 times 10719 08:07:43,119 --> 08:07:46,878 because that's what we're doing here but 10720 08:07:45,119 --> 08:07:49,039 this is really bad design you don't want 10721 08:07:46,878 --> 08:07:50,958 to do something like this so we're going 10722 08:07:50,957 --> 08:07:55,680 and do this better using a for loop 10723 08:07:53,599 --> 08:07:57,439 let's say that this is the bad way and 10724 08:07:55,680 --> 08:07:59,360 we're going to use for loop which is the 10725 08:07:57,439 --> 08:08:01,840 good way sorry so we're going to start 10726 08:07:59,360 --> 08:08:04,797 by declaring our iterator so we're going 10727 08:08:01,840 --> 08:08:06,797 to say for we're going to say size t 10728 08:08:04,797 --> 08:08:08,477 let's use unsigned end to show you that 10729 08:08:06,797 --> 08:08:10,557 you can use that and we're going to say 10730 08:08:08,477 --> 08:08:12,239 i and we're going to initialize this 10731 08:08:12,240 --> 08:08:18,000 and this is going to be the declaration 10732 08:08:14,797 --> 08:08:20,319 of our iterator this is the first part 10733 08:08:18,000 --> 08:08:22,718 we have in this parenthesis here we're 10734 08:08:20,319 --> 08:08:25,119 going to put in a semicolon and put in 10735 08:08:25,119 --> 08:08:29,279 loop to run ten times so it's going to 10736 08:08:27,360 --> 08:08:31,840 run from zero to nine we're going to 10737 08:08:31,840 --> 08:08:36,637 less than ten this is one way you can 10738 08:08:34,159 --> 08:08:38,159 understand this after we have our test 10739 08:08:38,159 --> 08:08:42,957 incrementation part which is going to 10740 08:08:40,398 --> 08:08:45,040 change the iterator to make the loop 10741 08:08:42,957 --> 08:08:48,000 move forward and we're going to do that 10742 08:08:45,040 --> 08:08:50,477 by incrementing our iterator after that 10743 08:08:48,000 --> 08:08:52,558 we're going to put a pair of curly 10744 08:08:50,477 --> 08:08:55,119 braces not angle brackets and we're 10745 08:08:52,558 --> 08:08:57,840 going to fall inside the body of the 10746 08:08:55,119 --> 08:09:00,637 loop this block here delimitated by the 10747 08:08:57,840 --> 08:09:02,797 curly braces is the body of the loop and 10748 08:09:00,637 --> 08:09:04,797 inside here we're going to put whatever 10749 08:09:02,797 --> 08:09:07,520 we want the loop to run so we can go 10750 08:09:04,797 --> 08:09:10,079 down here and say i love c plus plus 10 10751 08:09:07,520 --> 08:09:12,477 times let's say that but before we do 10752 08:09:10,080 --> 08:09:15,040 that let's say i so that we know the 10753 08:09:12,477 --> 08:09:17,119 current iteration where we are at 10754 08:09:15,040 --> 08:09:20,000 and we're going to put a column and say 10755 08:09:17,119 --> 08:09:23,200 i love c plus plus this is going to be 10756 08:09:20,000 --> 08:09:25,680 better to follow in the terminal i guess 10757 08:09:30,398 --> 08:09:36,000 and we need to put our stream output 10758 08:09:33,439 --> 08:09:38,797 operator here for this to make sense to 10759 08:09:36,000 --> 08:09:40,718 the compiler and this is our loop now if 10760 08:09:38,797 --> 08:09:42,477 we run it we're going to see exactly the 10761 08:09:40,718 --> 08:09:45,360 same thing like this but we're going to 10762 08:09:42,477 --> 08:09:47,599 have these iterators prepended to the 10763 08:09:45,360 --> 08:09:50,319 message so that it becomes easy to 10764 08:09:47,599 --> 08:09:53,759 follow this and again how this is going 10765 08:09:50,319 --> 08:09:56,718 to work we are going to run our code to 10766 08:09:53,759 --> 08:09:58,957 declare and initialize the iterator 10767 08:09:56,718 --> 08:10:01,760 after that we're going to do the test we 10768 08:09:58,957 --> 08:10:03,759 are going to start with i zero so zero 10769 08:10:01,759 --> 08:10:05,759 is less than ten this is going to 10770 08:10:03,759 --> 08:10:06,557 succeed we are going to fall in the body 10771 08:10:06,558 --> 08:10:11,600 and after that we're going to 10772 08:10:08,797 --> 08:10:13,520 print sddc out i love c plus plus and 10773 08:10:11,599 --> 08:10:15,119 this is going to be zero we're going to 10774 08:10:15,119 --> 08:10:20,319 and we're going to do the test again 10775 08:10:17,759 --> 08:10:21,919 we're going to have to do is one less 10776 08:10:20,319 --> 08:10:23,599 than 10 the test is going to be 10777 08:10:21,919 --> 08:10:26,159 successful so we're going to fall in 10778 08:10:23,599 --> 08:10:27,119 here and we're going to print a one 10779 08:10:27,119 --> 08:10:31,119 incrementation part again we're going to 10780 08:10:29,360 --> 08:10:32,797 run the test and we're going to keep 10781 08:10:31,119 --> 08:10:35,439 doing this until we hit a point for 10782 08:10:32,797 --> 08:10:36,319 example when we increment this to be a 10783 08:10:36,319 --> 08:10:39,919 and after that we're going to run the 10784 08:10:37,919 --> 08:10:42,557 test we're going to say is 9 less than 10785 08:10:39,919 --> 08:10:43,919 10 the test is going to be successful so 10786 08:10:42,558 --> 08:10:46,398 we're going to follow in here and we're 10787 08:10:46,398 --> 08:10:49,200 and after that we're going to increment 10788 08:10:48,240 --> 08:10:50,878 again we're going to run the 10789 08:10:49,200 --> 08:10:53,600 incrementation part and we're going to 10790 08:10:50,878 --> 08:10:56,958 make i 10 if we run the test we're going 10791 08:10:53,599 --> 08:10:58,877 to say is 10 less than 10 the test here 10792 08:10:56,957 --> 08:11:00,637 is going to fail we're going to stop 10793 08:10:58,878 --> 08:11:02,878 running the loop all together and we're 10794 08:11:00,637 --> 08:11:05,919 going to fall on the outside of the loop 10795 08:11:02,878 --> 08:11:08,159 and say loop done let's say that so that 10796 08:11:05,919 --> 08:11:10,637 we can see this on the terminal and this 10797 08:11:08,159 --> 08:11:13,759 is going to run and print i love c plus 10798 08:11:10,637 --> 08:11:16,079 plus 10 times and we expect i to go from 10799 08:11:16,080 --> 08:11:20,558 try to run this in your mind 10800 08:11:18,240 --> 08:11:22,718 take a piece of paper try to 10801 08:11:20,558 --> 08:11:24,240 print this as you fall into the body of 10802 08:11:22,718 --> 08:11:26,878 the loop you're going to see that's 10803 08:11:24,240 --> 08:11:28,878 exactly what is happening here so what 10804 08:11:26,878 --> 08:11:31,920 we're going to do we're going to boil 10805 08:11:28,878 --> 08:11:33,360 this again so we're going to run the 10806 08:11:35,040 --> 08:11:40,718 the world is good we're going to go down 10807 08:11:37,360 --> 08:11:42,159 and run this we're going to clear 10808 08:11:42,159 --> 08:11:45,759 and you're going to see that we have 10809 08:11:43,520 --> 08:11:48,637 zero i love c plus plus all the way to 10810 08:11:45,759 --> 08:11:51,039 nine i love c plus plus our loop is 10811 08:11:48,637 --> 08:11:53,200 working exactly the way we want okay so 10812 08:11:51,040 --> 08:11:55,520 this is the first step we are able to 10813 08:11:53,200 --> 08:11:57,840 run our code a lot of times without 10814 08:11:55,520 --> 08:12:00,398 really having to manually do things like 10815 08:11:57,840 --> 08:12:02,637 this and the benefit of this is that if 10816 08:12:00,398 --> 08:12:05,600 we want this to run for example 100 10817 08:12:02,637 --> 08:12:06,477 times all we have to do is change our 10818 08:12:08,398 --> 08:12:13,040 is equal to 100 and we're going to stop 10819 08:12:13,040 --> 08:12:17,040 less than 100 and if we run this we're 10820 08:12:17,040 --> 08:12:22,398 the message from 0 all the way to 99 and 10821 08:12:20,477 --> 08:12:25,279 this is pretty cool the torso is going 10822 08:12:22,398 --> 08:12:27,440 to be 100 times so let's weld again to 10823 08:12:27,439 --> 08:12:31,199 we're going to run this task to weld 10824 08:12:33,119 --> 08:12:39,439 run rooster prepare to be amazed run and 10825 08:12:37,119 --> 08:12:42,637 it's going to run all the way to 99 we 10826 08:12:39,439 --> 08:12:45,840 can change this to even 1 000 or even 10 10827 08:12:42,637 --> 08:12:48,797 000 why not we're going to weld again 10828 08:12:45,840 --> 08:12:50,477 and we're going to be successful 10829 08:12:50,477 --> 08:12:54,718 and run rooster and this is going to go 10830 08:12:52,878 --> 08:12:56,477 all the way to 10 000 times we're going 10831 08:12:54,718 --> 08:13:03,520 to wait for this to get there 10832 08:12:56,477 --> 08:13:05,200 now 3 000 4 000 5000 6000 7000 8000 10833 08:13:03,520 --> 08:13:07,600 ten thousand it's going to go all the 10834 08:13:05,200 --> 08:13:09,760 way to nine thousand nine hundred ninety 10835 08:13:07,599 --> 08:13:12,239 nine and it is going to stop and the 10836 08:13:15,520 --> 08:13:20,080 this storping number here okay now we 10837 08:13:18,240 --> 08:13:22,798 can see that our loop is really working 10838 08:13:20,080 --> 08:13:25,680 fine we don't have to do these hddc out 10839 08:13:22,797 --> 08:13:28,079 statements a crazy amount of times okay 10840 08:13:25,680 --> 08:13:31,599 and the benefit again is that 10841 08:13:28,080 --> 08:13:33,920 regardless of the number of times the 10842 08:13:31,599 --> 08:13:36,000 code here is going to run our loop is 10843 08:13:33,919 --> 08:13:38,637 going to stay relatively the same it's 10844 08:13:36,000 --> 08:13:40,957 just going to be one two three four 10845 08:13:38,637 --> 08:13:43,279 lines of code and this is really cool 10846 08:13:40,957 --> 08:13:45,840 okay so i think this now makes it very 10847 08:13:43,279 --> 08:13:47,680 clear how loops work so we're going to 10848 08:13:45,840 --> 08:13:49,119 comment this out and show you another 10849 08:13:47,680 --> 08:13:51,599 thing and we're going to show you that 10850 08:13:49,119 --> 08:13:54,477 you can use size t to actually 10851 08:13:51,599 --> 08:13:57,039 denote the type of your iterator 10852 08:13:54,477 --> 08:14:00,079 and it is easy to do that so we're going 10853 08:13:57,040 --> 08:14:02,718 to say use size t and for us to be able 10854 08:14:00,080 --> 08:14:05,200 to see all this here let's hit this file 10855 08:14:02,718 --> 08:14:06,637 icon here to close this pane and we're 10856 08:14:05,200 --> 08:14:09,520 going to have a better chance of seeing 10857 08:14:06,637 --> 08:14:11,039 our code here so what we want to do 10858 08:14:09,520 --> 08:14:12,957 we're going to declare a group we're 10859 08:14:11,040 --> 08:14:15,760 going to save for we're not going to say 10860 08:14:12,957 --> 08:14:18,159 our design and we're going to say size t 10861 08:14:15,759 --> 08:14:20,000 and this is going to be the type of our 10862 08:14:18,159 --> 08:14:22,398 iterator we're going to say i we're 10863 08:14:20,000 --> 08:14:24,398 going to initialize this with a zero 10864 08:14:22,398 --> 08:14:26,319 and we're going to put a semicolon like 10865 08:14:24,398 --> 08:14:29,440 we just did we're going to put in the 10866 08:14:26,319 --> 08:14:32,477 end test we're going to put in the test 10867 08:14:29,439 --> 08:14:34,079 so we're going to say i less than 10 10868 08:14:34,080 --> 08:14:39,600 and we are going to increment to put in 10869 08:14:36,797 --> 08:14:41,039 place our incrementation part we're 10870 08:14:39,599 --> 08:14:43,599 going to fall in the loop we're going to 10871 08:14:41,040 --> 08:14:45,840 say stdc out this is the code we want to 10872 08:14:43,599 --> 08:14:48,159 run so we're going to put in our 10873 08:14:45,840 --> 08:14:50,319 coverage value for the iterator and 10874 08:14:48,159 --> 08:14:52,558 we're going to say i love c plus plus 10875 08:14:50,319 --> 08:14:55,279 again and we're going to put in our new 10876 08:14:52,558 --> 08:14:57,280 line character and if we build and run 10877 08:14:55,279 --> 08:14:59,759 this it's going to work and run the same 10878 08:14:59,759 --> 08:15:03,359 so that we know when it's done and we're 10879 08:15:01,520 --> 08:15:05,600 going to weld and run we're going to 10880 08:15:07,759 --> 08:15:10,957 and what we're going to do we're going 10881 08:15:10,957 --> 08:15:18,079 and run rooster and this is going to say 10882 08:15:13,360 --> 08:15:20,080 i love c plus plus 10 times from 0 to 9. 10883 08:15:18,080 --> 08:15:23,120 again the message here is that you can 10884 08:15:20,080 --> 08:15:25,360 use size t to represent the type of your 10885 08:15:23,119 --> 08:15:27,599 iterators and this is the type that is 10886 08:15:25,360 --> 08:15:30,240 even used in the c plus plus standard 10887 08:15:27,599 --> 08:15:32,637 library so this is going to be something 10888 08:15:30,240 --> 08:15:35,360 people expect from your code if you are 10889 08:15:32,637 --> 08:15:37,360 representing sizes in code that should 10890 08:15:35,360 --> 08:15:40,000 be compliant with the c plus plus 10891 08:15:37,360 --> 08:15:42,797 standard library nothing stops you from 10892 08:15:40,000 --> 08:15:43,919 using unsigned it like we do here but 10893 08:15:42,797 --> 08:15:46,079 we're just going to follow the 10894 08:15:43,919 --> 08:15:48,477 convention from the c plus plus standard 10895 08:15:46,080 --> 08:15:51,040 library and use this type here to 10896 08:15:51,040 --> 08:15:55,600 and some of you might be wondering how 10897 08:15:53,200 --> 08:15:56,477 big is this thing we have the tools to 10898 08:15:56,477 --> 08:16:01,200 how big it is so let's do that pretty 10899 08:15:58,558 --> 08:16:03,920 fast so we're going to go down here 10900 08:16:01,200 --> 08:16:05,520 and say sddc out and we're going to say 10901 08:16:05,520 --> 08:16:10,159 size t and we can print this out and 10902 08:16:08,159 --> 08:16:11,759 let's say what we are doing here so that 10903 08:16:11,759 --> 08:16:17,039 use this code as the reference and if we 10904 08:16:14,477 --> 08:16:19,279 print this out we're going to see 10905 08:16:17,040 --> 08:16:21,680 the size of this thing on the system 10906 08:16:19,279 --> 08:16:23,360 where this code here is going to run 10907 08:16:26,477 --> 08:16:31,599 the bolt is going to be good we're going 10908 08:16:28,558 --> 08:16:33,280 to go here and clear and run rooster 10909 08:16:31,599 --> 08:16:35,119 you're going to see that it is eight 10910 08:16:33,279 --> 08:16:37,520 bytes on my system so you're going to 10911 08:16:35,119 --> 08:16:39,840 have quite a huge range of numbers you 10912 08:16:37,520 --> 08:16:41,760 can put in here because this is going to 10913 08:16:41,759 --> 08:16:46,159 okay if you try to put in a negative 10914 08:16:43,919 --> 08:16:48,397 number you're going to get an underflow 10915 08:16:46,159 --> 08:16:51,680 and we have learned about overflow and 10916 08:16:48,398 --> 08:16:53,760 underfloor in the previous chapter 10917 08:16:51,680 --> 08:16:56,477 okay so let's comment this out and keep 10918 08:16:53,759 --> 08:16:58,397 learning more things about for loops 10919 08:16:56,477 --> 08:17:01,200 okay the next thing i really want you to 10920 08:16:58,398 --> 08:17:03,520 focus on and learn is the scope of the 10921 08:17:01,200 --> 08:17:05,440 iterator and we're going to grab our 10922 08:17:07,599 --> 08:17:12,717 as a learning tool so we're going to go 10923 08:17:12,718 --> 08:17:17,680 and what we're going to have let's move 10924 08:17:15,200 --> 08:17:19,760 this a little bit to the right i think 10925 08:17:17,680 --> 08:17:22,080 we can do something like this i am going 10926 08:17:19,759 --> 08:17:24,477 to select the entire thing here and hit 10927 08:17:22,080 --> 08:17:26,558 tab and it is going to be aligned with 10928 08:17:26,558 --> 08:17:30,638 okay so we have our loop it is going to 10929 08:17:28,637 --> 08:17:32,797 run 10 times it's going to print i love 10930 08:17:34,957 --> 08:17:40,957 what is the scope of this i think here 10931 08:17:37,680 --> 08:17:43,439 and the fact is it is scoped inside this 10932 08:17:40,957 --> 08:17:46,159 loop here you might have guessed that if 10933 08:17:43,439 --> 08:17:47,680 you try to use it outside the loop 10934 08:17:46,159 --> 08:17:50,240 you're going to get a compiler arrow 10935 08:17:47,680 --> 08:17:53,040 because it is only usable inside the 10936 08:17:50,240 --> 08:17:55,840 body of the you can also use it inside 10937 08:17:53,040 --> 08:17:58,398 the control part of the loop here but 10938 08:17:55,840 --> 08:18:00,957 you can't use it outside the loop so if 10939 08:17:58,398 --> 08:18:02,638 you try to print i here for example 10940 08:18:00,957 --> 08:18:04,797 you're going to get a compiler error so 10941 08:18:02,637 --> 08:18:06,397 let's try and do that and show you how 10942 08:18:06,398 --> 08:18:12,958 so if we try to print i here let's say i 10943 08:18:10,398 --> 08:18:14,478 to make it clear what we want to print 10944 08:18:12,957 --> 08:18:17,439 if we try to do this we're going to get 10945 08:18:14,477 --> 08:18:20,558 a compiler error because i is not in 10946 08:18:17,439 --> 08:18:23,599 scope it is only in scope inside these 10947 08:18:20,558 --> 08:18:25,680 curly braces that show the body part of 10948 08:18:23,599 --> 08:18:27,199 our loop okay visual studio code is 10949 08:18:25,680 --> 08:18:29,840 showing the problem here it's saying 10950 08:18:27,200 --> 08:18:32,240 eyes undefined here so you shouldn't 10951 08:18:29,840 --> 08:18:34,080 really do this but if we move on and 10952 08:18:32,240 --> 08:18:36,159 ignore the message from visual studio 10953 08:18:36,159 --> 08:18:39,040 we're going to get a compiler error and 10954 08:18:39,040 --> 08:18:44,240 i don't know what you're doing here i 10955 08:18:41,360 --> 08:18:46,477 was not declared in this scope where you 10956 08:18:44,240 --> 08:18:48,240 are using it and i think it's even going 10957 08:18:46,477 --> 08:18:51,439 to give me a line a number it's going to 10958 08:18:48,240 --> 08:18:53,680 tell me 46 that's where you are doing 10959 08:18:51,439 --> 08:18:55,840 something wrong and if we go in our code 10960 08:18:53,680 --> 08:18:58,477 we're going to see that this is the line 10961 08:18:55,840 --> 08:19:00,637 where we are making the mistake so i is 10962 08:18:58,477 --> 08:19:04,718 not in scope here we're going to comment 10963 08:19:00,637 --> 08:19:06,637 this out and say compiler error i is not 10964 08:19:04,718 --> 08:19:08,718 in scope that's what you're going to get 10965 08:19:06,637 --> 08:19:11,439 if you try to do this and the main 10966 08:19:08,718 --> 08:19:14,317 message is that if you declare your loop 10967 08:19:11,439 --> 08:19:16,079 like this ah is only going to be scoped 10968 08:19:14,317 --> 08:19:18,398 in the body of the loop and that's the 10969 08:19:16,080 --> 08:19:20,638 only place where you can mention its 10970 08:19:18,398 --> 08:19:23,520 name and use it if we comment this out 10971 08:19:20,637 --> 08:19:25,200 and world again the compiler error is 10972 08:19:23,520 --> 08:19:27,439 going to go away you're going to see 10973 08:19:25,200 --> 08:19:30,317 that the world is good let's clear so 10974 08:19:30,317 --> 08:19:34,000 we're going to build again and show you 10975 08:19:31,759 --> 08:19:36,477 that the build is good and uh this is 10976 08:19:34,000 --> 08:19:39,200 really cool both finished successfully 10977 08:19:36,477 --> 08:19:42,159 okay this is one way we can set up our 10978 08:19:39,200 --> 08:19:45,600 loop but there is a way we can make our 10979 08:19:42,159 --> 08:19:46,878 iterator usable outside the body of the 10980 08:19:45,599 --> 08:19:49,279 loop and we're going to show you how you 10981 08:19:46,878 --> 08:19:51,520 can do that next so let's comment this 10982 08:19:51,520 --> 08:19:55,760 and we're going to grab the code because 10983 08:19:53,279 --> 08:19:58,717 we don't want to type all this again and 10984 08:19:55,759 --> 08:20:01,679 we're going to say iterator declared 10985 08:19:58,718 --> 08:20:04,080 outside the loop we're going to go down 10986 08:20:04,080 --> 08:20:10,718 and we're going to align this nicely 10987 08:20:07,279 --> 08:20:13,279 and we're going to take the iterator and 10988 08:20:10,718 --> 08:20:15,520 declare it on the outside of the loop so 10989 08:20:15,520 --> 08:20:19,439 and we're going to say i and initialize 10990 08:20:19,439 --> 08:20:25,520 and we're going to say this very clearly 10991 08:20:30,240 --> 08:20:34,159 and we're going to say in our 10992 08:20:34,159 --> 08:20:38,797 declaration part of the iterator we're 10993 08:20:36,718 --> 08:20:41,520 going to just say i and this is going to 10994 08:20:38,797 --> 08:20:43,599 work pretty fine if we build this you're 10995 08:20:41,520 --> 08:20:45,600 going to see that the world is good and 10996 08:20:43,599 --> 08:20:47,279 if we run this it's going to do the same 10997 08:20:45,599 --> 08:20:50,159 thing it's been doing it's going to 10998 08:20:47,279 --> 08:20:52,797 print i love c plus plus 10 times and if 10999 08:20:50,159 --> 08:20:55,040 you want i usable on the outside of the 11000 08:20:52,797 --> 08:20:57,919 loop now you can use it we can grab the 11001 08:20:57,919 --> 08:21:03,359 in the last run of our code and put that 11002 08:21:01,279 --> 08:21:05,039 out here and we're going to align this a 11003 08:21:05,040 --> 08:21:08,878 and if we compile this the book is going 11004 08:21:08,878 --> 08:21:13,920 and if we try to run rooster we're going 11005 08:21:11,200 --> 08:21:15,600 to see i love c plus 10 times we're 11006 08:21:13,919 --> 08:21:18,239 going to say loop done and we're going 11007 08:21:15,599 --> 08:21:20,397 to print the value of i and you see that 11008 08:21:18,240 --> 08:21:23,040 if we do things like this i is going to 11009 08:21:20,398 --> 08:21:23,920 be usable on the outside of the loop 11010 08:21:23,919 --> 08:21:28,397 and sometimes these may come in handy 11011 08:21:26,718 --> 08:21:30,718 depending on the logic of the 11012 08:21:28,398 --> 08:21:33,040 application that you are designing just 11013 08:21:30,718 --> 08:21:34,878 know that this is something you can do 11014 08:21:34,878 --> 08:21:41,840 okay you can go even crazier and leave 11015 08:21:38,317 --> 08:21:44,000 out this iterator declaration part in 11016 08:21:41,840 --> 08:21:45,680 your loop all together so you can remove 11017 08:21:44,000 --> 08:21:47,520 that and we're going to show you how you 11018 08:21:45,680 --> 08:21:49,520 can do that in a minute so let's comment 11019 08:21:47,520 --> 08:21:51,200 this out i don't want to remove code 11020 08:21:49,520 --> 08:21:53,119 because i want you to have this as a 11021 08:21:51,200 --> 08:21:56,080 reference if you need to look at this 11022 08:21:53,119 --> 08:21:59,039 later so we're going to say leave out 11023 08:21:56,080 --> 08:22:02,798 and what you can do is grab the code we 11024 08:21:59,040 --> 08:22:05,040 had before i'm going to grab it 11025 08:22:02,797 --> 08:22:06,797 and we're going to paste that in here 11026 08:22:05,040 --> 08:22:08,958 and what we're going to do is take out 11027 08:22:06,797 --> 08:22:12,397 this eye this can work and you may see 11028 08:22:08,957 --> 08:22:14,079 code like this out there in the wild so 11029 08:22:12,398 --> 08:22:17,040 we're going to run this task to build 11030 08:22:14,080 --> 08:22:19,440 with gcc the world is going to be good 11031 08:22:17,040 --> 08:22:21,040 and we're going to clear and run rooster 11032 08:22:19,439 --> 08:22:23,279 and it's going to do exactly the same 11033 08:22:21,040 --> 08:22:25,680 thing so this is one way you can set up 11034 08:22:23,279 --> 08:22:28,317 your loops okay the last thing we need 11035 08:22:28,317 --> 08:22:33,279 this test here is using a hard coded 11036 08:22:30,957 --> 08:22:35,359 value and if we needed to change this 11037 08:22:33,279 --> 08:22:38,000 loop to run for example for a thousand 11038 08:22:35,360 --> 08:22:40,317 times we would need to manually hunt for 11039 08:22:38,000 --> 08:22:43,040 this line of code change this and this 11040 08:22:40,317 --> 08:22:45,360 is really not good design so what is 11041 08:22:43,040 --> 08:22:48,317 recommended is to store this value in a 11042 08:22:45,360 --> 08:22:50,080 variable and test against that variable 11043 08:22:50,080 --> 08:22:54,000 let's comment this out and show you that 11044 08:22:55,680 --> 08:23:00,718 and go down and copy this entire thing 11045 08:22:58,957 --> 08:23:02,797 i don't think i want to do my loops like 11046 08:23:00,718 --> 08:23:06,080 this because this is really unintuitive 11047 08:23:02,797 --> 08:23:08,477 most times so i am going to declare my 11048 08:23:06,080 --> 08:23:10,878 iterator inside and i'm going to copy 11049 08:23:08,477 --> 08:23:12,317 the code that does that here we're going 11050 08:23:13,680 --> 08:23:18,637 don't hardcord values okay we're going 11051 08:23:18,637 --> 08:23:22,718 and we're going to go down and show you 11052 08:23:20,317 --> 08:23:25,439 a better way to do things and again 11053 08:23:22,718 --> 08:23:28,080 right now we are hard coding in this 11054 08:23:30,317 --> 08:23:34,477 and we can call this value count for 11055 08:23:32,558 --> 08:23:36,638 example and we can initialize it with 11056 08:23:34,477 --> 08:23:39,759 whatever we want in this case we want to 11057 08:23:36,637 --> 08:23:42,159 control our loop to run 10 times so 11058 08:23:39,759 --> 08:23:44,477 we're going to put in a 10 and instead 11059 08:23:42,159 --> 08:23:46,477 of hard coding and this 10 we're going 11060 08:23:47,439 --> 08:23:51,039 and if we build and run it the loop is 11061 08:23:49,279 --> 08:23:53,279 going to do exactly the same thing it's 11062 08:23:51,040 --> 08:23:54,159 going to print i love c plus plus 10 11063 08:23:58,398 --> 08:24:02,798 it's going to do the same thing but now 11064 08:24:00,558 --> 08:24:06,240 we have one point of control if we want 11065 08:24:02,797 --> 08:24:09,199 this to run for let's say 100 times we 11066 08:24:06,240 --> 08:24:11,280 can change the count value to 100 times 11067 08:24:09,200 --> 08:24:13,040 and if we both again that's how many 11068 08:24:11,279 --> 08:24:15,520 times the loop is going to run and this 11069 08:24:13,040 --> 08:24:16,558 is much cleaner so i do recommend you 11070 08:24:16,558 --> 08:24:20,080 the values you use in your loops in 11071 08:24:18,317 --> 08:24:22,797 variables like this and it's going to 11072 08:24:22,797 --> 08:24:27,680 so it's going to run 100 times and this 11073 08:24:27,680 --> 08:24:30,718 this is really all we set out to do in 11074 08:24:29,200 --> 08:24:31,840 this lecture i hope you found it 11075 08:24:31,840 --> 08:24:36,240 i am sorry that this lecture turned out 11076 08:24:34,398 --> 08:24:39,360 lengthy because we had a lot of things 11077 08:24:36,240 --> 08:24:41,600 to talk about but now i hope you really 11078 08:24:39,360 --> 08:24:44,080 know a lot of things about ways you can 11079 08:24:41,599 --> 08:24:46,477 use your for loops to make them do 11080 08:24:44,080 --> 08:24:48,318 things and again many loops that we're 11081 08:24:46,477 --> 08:24:50,397 going to use in our c plus plus code are 11082 08:24:48,317 --> 08:24:52,317 going to have five parts we're going to 11083 08:24:50,398 --> 08:24:55,200 have an iterator which is going to be a 11084 08:24:52,317 --> 08:24:57,119 variable we use to manipulate how a loop 11085 08:24:55,200 --> 08:24:59,040 moves forward and does things the 11086 08:24:57,119 --> 08:25:00,797 starting point is going to be where the 11087 08:24:59,040 --> 08:25:02,398 loop is going to start we're going to 11088 08:25:00,797 --> 08:25:04,397 have a test in place which is going to 11089 08:25:02,398 --> 08:25:06,718 control when the loop ends and we're 11090 08:25:04,398 --> 08:25:08,240 going to have the end point encoded in 11091 08:25:06,718 --> 08:25:10,159 that test we're going to have the 11092 08:25:08,240 --> 08:25:12,878 incrementation part of the loop which is 11093 08:25:10,159 --> 08:25:14,797 going to control how our loop moves 11094 08:25:12,878 --> 08:25:18,000 forward and we are going to set up a 11095 08:25:14,797 --> 08:25:20,957 pair of curly braces that are going to 11096 08:25:18,000 --> 08:25:22,957 contain the body of our loop in this 11097 08:25:20,957 --> 08:25:25,199 lecture we're going to learn about while 11098 08:25:22,957 --> 08:25:27,599 loops and this is another construct we 11099 08:25:25,200 --> 08:25:29,680 have in the c plus plus programming 11100 08:25:27,599 --> 08:25:32,557 language to allow us to do repetitive 11101 08:25:29,680 --> 08:25:35,520 tasks the syntax for a while loop looks 11102 08:25:32,558 --> 08:25:37,600 like this we have five parts in our loop 11103 08:25:35,520 --> 08:25:40,000 we have the iterator the starting point 11104 08:25:37,599 --> 08:25:41,840 the test the incrementation and the loop 11105 08:25:40,000 --> 08:25:45,279 body it's just going to be set up 11106 08:25:41,840 --> 08:25:47,759 differently for while loops the iterator 11107 08:25:45,279 --> 08:25:49,759 is declared on the outside and you see 11108 08:25:47,759 --> 08:25:52,239 here we have unsigned in i which is 11109 08:25:49,759 --> 08:25:54,317 exactly the same type of iterator we've 11110 08:25:52,240 --> 08:25:56,798 used in the last lecture but know that 11111 08:25:54,317 --> 08:25:59,439 you can also use size t here i am just 11112 08:25:56,797 --> 08:26:02,717 using unsigned and for this case after 11113 08:25:59,439 --> 08:26:04,957 you have your iterator declared you're 11114 08:26:02,718 --> 08:26:08,080 going to fall to this part here and say 11115 08:26:04,957 --> 08:26:10,877 while open a set of parentheses and 11116 08:26:08,080 --> 08:26:12,798 inside you're going to put in your test 11117 08:26:10,878 --> 08:26:15,200 after that you're going to open a pair 11118 08:26:12,797 --> 08:26:17,919 of curly braces and design them you're 11119 08:26:15,200 --> 08:26:19,360 going to put the body of your loop 11120 08:26:17,919 --> 08:26:22,477 and you're going to do whatever it is 11121 08:26:19,360 --> 08:26:24,398 you want to do in the loop in there and 11122 08:26:22,477 --> 08:26:26,718 after your code to do whatever it is you 11123 08:26:24,398 --> 08:26:29,760 want to do is executed you're going to 11124 08:26:26,718 --> 08:26:32,000 put in your incrementation part after 11125 08:26:29,759 --> 08:26:33,679 you code and that's what we have here 11126 08:26:32,000 --> 08:26:37,040 and this is the setup we're going to be 11127 08:26:33,680 --> 08:26:38,477 using for our while loops again 11128 08:26:38,477 --> 08:26:43,520 iterator declared on the outside and we 11129 08:26:41,200 --> 08:26:44,798 can initialize it with whatever it is we 11130 08:26:46,159 --> 08:26:50,159 then we're going to put in our test it's 11131 08:26:48,080 --> 08:26:52,318 going to be within this parenthesis and 11132 08:26:50,159 --> 08:26:54,797 after that we're going to set up a pair 11133 08:26:52,317 --> 08:26:56,558 of curly braces and inside them we're 11134 08:26:54,797 --> 08:26:58,477 going to put whatever code we want to 11135 08:26:56,558 --> 08:27:01,200 run in the loop and then we're going to 11136 08:26:58,477 --> 08:27:03,040 follow that with our incrementation part 11137 08:27:01,200 --> 08:27:05,119 and if we try to run this we're going to 11138 08:27:03,040 --> 08:27:07,440 run this line we're going to declare the 11139 08:27:05,119 --> 08:27:10,797 iterator i and it's going to start with 11140 08:27:07,439 --> 08:27:12,557 zero so the test is going to run and 0 11141 08:27:12,558 --> 08:27:16,000 and we're going to succeed and we're 11142 08:27:14,398 --> 08:27:18,478 going to run the body here we're going 11143 08:27:16,000 --> 08:27:20,080 to say i love c plus plus and after that 11144 08:27:18,477 --> 08:27:22,797 we're going to run the incrementation 11145 08:27:20,080 --> 08:27:25,520 part i is going to become one we're 11146 08:27:22,797 --> 08:27:27,199 going to run the test again so one is 11147 08:27:25,520 --> 08:27:29,520 less than 10 we're going to succeed 11148 08:27:27,200 --> 08:27:31,680 we're going to fall in print i love c 11149 08:27:29,520 --> 08:27:34,477 plus plus we're going to increment again 11150 08:27:31,680 --> 08:27:36,317 then we're going to have a 2 in i here 11151 08:27:34,477 --> 08:27:38,637 we're going to run the test again we're 11152 08:27:36,317 --> 08:27:40,159 going to say is 2 less than 10 that's 11153 08:27:38,637 --> 08:27:42,477 going to be true so we're going to be 11154 08:27:40,159 --> 08:27:44,079 successful we're going to fall in the 11155 08:27:42,477 --> 08:27:46,079 body of the loop we're going to 11156 08:27:44,080 --> 08:27:48,878 increment and get a three we're going to 11157 08:27:46,080 --> 08:27:50,478 test against our three value three is 11158 08:27:48,878 --> 08:27:53,200 less than ten we're going to succeed 11159 08:27:50,477 --> 08:27:55,119 we're going to print i love c plus plus 11160 08:27:53,200 --> 08:27:57,280 we're going to increment again we're 11161 08:27:55,119 --> 08:28:01,039 going to test again and we're going to 11162 08:27:57,279 --> 08:28:02,557 say is for less than count or 10 we're 11163 08:28:01,040 --> 08:28:04,798 going to be successful and we're going 11164 08:28:02,558 --> 08:28:07,360 to keep running until we increment and 11165 08:28:04,797 --> 08:28:09,520 get a 9. once we get a 9 we're going to 11166 08:28:07,360 --> 08:28:12,000 run the test again and we're going to 11167 08:28:09,520 --> 08:28:14,957 say is 9 less than 10 that's going to be 11168 08:28:12,000 --> 08:28:16,957 successful so we're going to fall inside 11169 08:28:14,957 --> 08:28:18,957 print i love c plus plus we're going to 11170 08:28:18,957 --> 08:28:23,919 and if we run the test again i'm going 11171 08:28:20,957 --> 08:28:26,477 to say is 10 less than 10 that's going 11172 08:28:23,919 --> 08:28:27,919 to fail and we are going to fall on the 11173 08:28:26,477 --> 08:28:30,317 outside of the loop and we're going to 11174 08:28:27,919 --> 08:28:32,717 run whatever code is on the outside here 11175 08:28:30,317 --> 08:28:34,718 this is really how a while loop works 11176 08:28:32,718 --> 08:28:36,878 and i hope you can see the similarities 11177 08:28:34,718 --> 08:28:38,878 between the for loop we have seen it is 11178 08:28:36,878 --> 08:28:41,200 really the same thing we just have 11179 08:28:38,878 --> 08:28:42,878 different syntaxes to do these things 11180 08:28:41,200 --> 08:28:44,240 okay if you look at these things we 11181 08:28:42,878 --> 08:28:46,317 already have the same pieces of 11182 08:28:44,240 --> 08:28:48,878 information it is just different 11183 08:28:46,317 --> 08:28:51,040 syntaxes that c plus plus provides to do 11184 08:28:48,878 --> 08:28:53,279 the same things and sometimes you're 11185 08:28:51,040 --> 08:28:54,878 going to find it convenient to use while 11186 08:28:53,279 --> 08:28:57,439 loops and sometimes you're going to find 11187 08:28:54,878 --> 08:28:59,040 it convenient to use for loops and 11188 08:28:57,439 --> 08:29:01,520 you're going to have to decide what 11189 08:28:59,040 --> 08:29:03,600 works better for whatever it is you are 11190 08:29:01,520 --> 08:29:05,840 working on for now we're going to head 11191 08:29:03,599 --> 08:29:08,317 over to visual studio code and try this 11192 08:29:05,840 --> 08:29:10,957 out okay here we are in visual studio 11193 08:29:08,317 --> 08:29:13,520 code the current project is while loop 11194 08:29:10,957 --> 08:29:15,279 we're going to grab our template files 11195 08:29:13,520 --> 08:29:16,797 and we're going to go in our project and 11196 08:29:16,797 --> 08:29:21,680 and we're going to open this in visual 11197 08:29:18,718 --> 08:29:24,080 studio code so let's do that 11198 08:29:21,680 --> 08:29:26,000 we're going to open the folder 11199 08:29:24,080 --> 08:29:28,638 and we're going to work on while loop 11200 08:29:26,000 --> 08:29:30,637 here we are going to open our main cpp 11201 08:29:28,637 --> 08:29:33,200 file pretty quick and we're going to get 11202 08:29:30,637 --> 08:29:35,520 rid of this and what we're going to do 11203 08:29:33,200 --> 08:29:36,878 is remove what we don't need and again 11204 08:29:35,520 --> 08:29:38,477 we're going to show you the bad way to 11205 08:29:36,878 --> 08:29:40,477 do things you don't really want to do 11206 08:29:38,477 --> 08:29:42,317 this manually we want to print i love c 11207 08:29:43,119 --> 08:29:47,119 we're going to build let's bring up a 11208 08:29:47,119 --> 08:29:52,557 use this when we run thanks and we are 11209 08:29:49,919 --> 08:29:55,039 going to world with gcc let's do that 11210 08:29:52,558 --> 08:29:57,680 pretty quick the world is good 11211 08:29:55,040 --> 08:29:59,520 so we can clear and run rooster and 11212 08:29:57,680 --> 08:30:02,477 we're going to have i love c plus 11213 08:29:59,520 --> 08:30:05,840 printed out 10 times so what we can do 11214 08:30:02,477 --> 08:30:08,079 is comment this out and use a while loop 11215 08:30:05,840 --> 08:30:11,040 like we want to do in this lecture here 11216 08:30:08,080 --> 08:30:13,520 and as we have seen with the slides 11217 08:30:11,040 --> 08:30:15,520 the first thing we need to do is declare 11218 08:30:13,520 --> 08:30:17,279 our iterator on the outside so we're 11219 08:30:15,520 --> 08:30:19,279 going to say size ti we're going to 11220 08:30:19,279 --> 08:30:24,239 and that's going to be our iterator 11221 08:30:21,360 --> 08:30:26,558 declaration and we're going to say while 11222 08:30:24,240 --> 08:30:28,798 i is less than the value where we want 11223 08:30:26,558 --> 08:30:30,718 to stop and again we don't want to hard 11224 08:30:28,797 --> 08:30:32,397 code these values and so what we're 11225 08:30:32,398 --> 08:30:38,080 const size t and we're going to call 11226 08:30:35,200 --> 08:30:40,159 this count and make it uppercase this is 11227 08:30:38,080 --> 08:30:42,160 a convention we use to declare constants 11228 08:30:40,159 --> 08:30:43,840 in c plus plus but you don't have to 11229 08:30:42,159 --> 08:30:45,840 make them uppercase it is just a 11230 08:30:43,840 --> 08:30:48,477 convention that is going to make your 11231 08:30:45,840 --> 08:30:50,000 code easier to read for other people so 11232 08:30:50,000 --> 08:30:54,477 put a 10 in here because we want our 11233 08:30:56,797 --> 08:31:02,000 is less than count and we're going to 11234 08:30:58,797 --> 08:31:03,919 set up a pair of curly braces and they 11235 08:31:03,919 --> 08:31:09,199 the start and the end of the body of a 11236 08:31:07,200 --> 08:31:11,440 loop here and inside we're going to put 11237 08:31:09,200 --> 08:31:14,637 in whatever code we want the loop to run 11238 08:31:11,439 --> 08:31:17,919 multiple times and the code is now very 11239 08:31:14,637 --> 08:31:19,680 familiar we're going to say sddc out 11240 08:31:19,680 --> 08:31:25,920 current iteration and we're going to say 11241 08:31:22,159 --> 08:31:28,000 i love c plus plus sdd endl for our new 11242 08:31:25,919 --> 08:31:30,557 line character and after that we will 11243 08:31:28,000 --> 08:31:32,718 need to put in our own incrementation 11244 08:31:30,558 --> 08:31:34,798 part if you forget that you're going to 11245 08:31:32,718 --> 08:31:37,119 get something really bad happen we're 11246 08:31:34,797 --> 08:31:40,239 really going to look at that later in 11247 08:31:37,119 --> 08:31:42,718 the chapter but for now remember to put 11248 08:31:40,240 --> 08:31:45,440 the incrementation part and we're going 11249 08:31:42,718 --> 08:31:47,440 to say plus plus i this is really easy 11250 08:31:45,439 --> 08:31:49,199 and we're going to say that this is the 11251 08:31:49,200 --> 08:31:54,159 and this is going to do what we want but 11252 08:31:51,439 --> 08:31:56,317 let's go down here and say that loop is 11253 08:31:54,159 --> 08:31:59,759 done when it is done so we're going to 11254 08:31:59,759 --> 08:32:05,199 and we're going to say stdendya 11255 08:32:03,439 --> 08:32:07,439 and before we run this we're going to go 11256 08:32:05,200 --> 08:32:10,558 through it and see how it is going to 11257 08:32:07,439 --> 08:32:12,877 work so let's click on this file icon 11258 08:32:10,558 --> 08:32:15,280 here to close this pane so that we have 11259 08:32:12,878 --> 08:32:17,760 some breathing room i am going to open a 11260 08:32:15,279 --> 08:32:20,397 notepad file we're going to use to see 11261 08:32:20,398 --> 08:32:25,120 when this loop runs we're going to 11262 08:32:22,317 --> 08:32:27,200 initialize the iterator here what we're 11263 08:32:25,119 --> 08:32:29,840 going to do is put in a 0 and we are 11264 08:32:27,200 --> 08:32:31,840 going to run the test to see if 0 is 11265 08:32:29,840 --> 08:32:34,240 less than 10 and the test is going to be 11266 08:32:31,840 --> 08:32:35,920 successful so we are going to fall in 11267 08:32:35,919 --> 08:32:41,199 0 i love c plus plus so let's say that 11268 08:32:38,558 --> 08:32:42,798 here to make this super clear inside the 11269 08:32:41,200 --> 08:32:45,200 loop we're going to print this message 11270 08:32:42,797 --> 08:32:47,039 zero i love c plus plus and after we 11271 08:32:45,200 --> 08:32:49,840 execute this line we're going to 11272 08:32:47,040 --> 08:32:51,680 increment i is going to become one we're 11273 08:32:49,840 --> 08:32:54,080 going to go up again and run the test 11274 08:32:54,080 --> 08:32:59,520 less than count or is 1 less than 10. 11275 08:32:57,520 --> 08:33:01,360 the test is going to be successful we're 11276 08:32:59,520 --> 08:33:03,920 going to run inside the loop and we're 11277 08:33:01,360 --> 08:33:05,200 going to print 1 i love c plus plus 11278 08:33:03,919 --> 08:33:07,119 that's what we're going to do here so 11279 08:33:07,119 --> 08:33:11,200 and after we do that we're going to run 11280 08:33:08,718 --> 08:33:13,600 our incrementation part again 11281 08:33:11,200 --> 08:33:15,680 i is going to become two we are going to 11282 08:33:13,599 --> 08:33:17,919 run the test we're going to say is 2 11283 08:33:15,680 --> 08:33:19,599 less than 10 that's going to be true of 11284 08:33:17,919 --> 08:33:22,717 course and we're going to fall in the 11285 08:33:19,599 --> 08:33:23,919 body again and say two i love c plus 11286 08:33:25,840 --> 08:33:28,878 we're going to increment and we're going 11287 08:33:27,279 --> 08:33:30,397 to get a three we're going to run the 11288 08:33:28,878 --> 08:33:32,558 test again the test is going to be 11289 08:33:30,398 --> 08:33:35,200 successful because three is less than 11290 08:33:32,558 --> 08:33:38,000 ten we are going to fall in here and say 11291 08:33:35,200 --> 08:33:40,240 three i love c plus plus okay so we can 11292 08:33:40,240 --> 08:33:44,718 and we are going to increment to get a 11293 08:33:42,080 --> 08:33:47,120 four and the test is going to say is 11294 08:33:44,718 --> 08:33:48,637 four less than ten that's going to be 11295 08:33:47,119 --> 08:33:50,477 true so we're going to succeed and 11296 08:33:48,637 --> 08:33:53,759 following the body again and we're going 11297 08:33:50,477 --> 08:33:55,279 to say 4 i love z plus blocks 11298 08:33:53,759 --> 08:33:56,637 we're going to keep doing this and we're 11299 08:33:55,279 --> 08:33:59,119 going to increment again we're going to 11300 08:33:56,637 --> 08:34:01,200 get a 5. the test is going to run again 11301 08:33:59,119 --> 08:34:03,279 we're going to say is 5 less than 10 11302 08:34:01,200 --> 08:34:05,600 that's going to be true of course and 11303 08:34:03,279 --> 08:34:08,397 we're going to run the code to print 11304 08:34:08,398 --> 08:34:11,760 we're going to increment again to get a 11305 08:34:11,759 --> 08:34:16,000 we're going to run the test we're going 11306 08:34:13,520 --> 08:34:18,159 to say is 6 less than 10 that's going to 11307 08:34:16,000 --> 08:34:21,279 be true of course again and we're going 11308 08:34:18,159 --> 08:34:23,119 to print sets i love c plus plus and 11309 08:34:21,279 --> 08:34:25,759 we're going to increment to get a 7 11310 08:34:23,119 --> 08:34:28,797 we're going to run the test our iterator 11311 08:34:25,759 --> 08:34:30,877 we're going to say is 7 less than count 11312 08:34:28,797 --> 08:34:33,599 that's going to be true we're going to 11313 08:34:30,878 --> 08:34:35,680 print the message again and say seven i 11314 08:34:33,599 --> 08:34:37,680 love c plus plus and we're going to 11315 08:34:35,680 --> 08:34:39,360 increment again to get an eight we're 11316 08:34:37,680 --> 08:34:40,317 going to say is eight less than and 11317 08:34:40,317 --> 08:34:44,718 that's going to be true we're going to 11318 08:34:41,840 --> 08:34:47,200 fall in the body and say eight i love c 11319 08:34:44,718 --> 08:34:48,798 plus plus we're going to increment to 11320 08:34:48,797 --> 08:34:54,000 we're going to run the test against our 11321 08:34:50,718 --> 08:34:55,680 nine of course nine is less than ten so 11322 08:34:54,000 --> 08:34:58,878 that's going to be true we're going to 11323 08:34:55,680 --> 08:35:00,398 fall inside and we're going to say nine 11324 08:35:00,398 --> 08:35:04,240 and after that we're going to increment 11325 08:35:02,080 --> 08:35:06,160 again and we're going to get a 10. 11326 08:35:04,240 --> 08:35:09,360 now if we run the test we're going to 11327 08:35:06,159 --> 08:35:12,000 say is 10 less than 10 and that's going 11328 08:35:09,360 --> 08:35:15,360 to be false because 10 is not less than 11329 08:35:12,000 --> 08:35:17,279 10 and if the test here fails we're not 11330 08:35:15,360 --> 08:35:20,080 going to fall in the body of the loop 11331 08:35:17,279 --> 08:35:22,239 control is going to fall at the end of 11332 08:35:20,080 --> 08:35:24,160 the loop body and we're going to print 11333 08:35:22,240 --> 08:35:26,840 loop done that's what we're going to say 11334 08:35:26,840 --> 08:35:33,439 done and our loop will have run 10 times 11335 08:35:31,360 --> 08:35:35,200 by the time we print this loop down 11336 08:35:33,439 --> 08:35:37,439 message here if you count here you see 11337 08:35:35,200 --> 08:35:39,520 that the loop is going to run 10 times 11338 08:35:37,439 --> 08:35:41,680 and it is going to say i love c plus 11339 08:35:39,520 --> 08:35:43,840 plus 10 times which is really what we 11340 08:35:41,680 --> 08:35:46,000 want okay i really had to show you this 11341 08:35:43,840 --> 08:35:48,477 manually so that you can really see what 11342 08:35:46,000 --> 08:35:50,878 is happening but now we can weld and run 11343 08:35:48,477 --> 08:35:53,200 this and let the computer do the heavy 11344 08:35:50,878 --> 08:35:54,637 duty of running this and showing us all 11345 08:35:54,637 --> 08:35:57,840 so the build is going to be successful 11346 08:35:57,840 --> 08:36:01,520 we're going to run rooster and it's 11347 08:35:59,680 --> 08:36:05,680 going to exactly show the same thing we 11348 08:36:01,520 --> 08:36:08,159 just came up in our trusty notepad file 11349 08:36:05,680 --> 08:36:10,240 here you see it is the same thing but 11350 08:36:08,159 --> 08:36:12,878 the beauty is that we don't have to do 11351 08:36:10,240 --> 08:36:15,280 all these crazy computations again what 11352 08:36:12,878 --> 08:36:17,840 we can do is we can even change the 11353 08:36:15,279 --> 08:36:19,439 count to 100 so it is going to run for 11354 08:36:22,957 --> 08:36:28,717 clear and run rooster and is going to do 11355 08:36:25,840 --> 08:36:30,159 that 100 times and that's pretty fast 11356 08:36:30,159 --> 08:36:35,279 this is really all we had to share with 11357 08:36:32,637 --> 08:36:37,840 you in this lecture the main thing is to 11358 08:36:35,279 --> 08:36:40,159 learn about the syntax of a while loop 11359 08:36:37,840 --> 08:36:42,957 and again notice that we have these five 11360 08:36:40,159 --> 08:36:44,637 parts that make up our loop we have an 11361 08:36:42,957 --> 08:36:46,877 iterator we have the starting point we 11362 08:36:44,637 --> 08:36:49,520 have a test we have the incrementation 11363 08:36:46,878 --> 08:36:52,000 part and we have the loop body it is 11364 08:36:49,520 --> 08:36:54,637 just set up differently with a while 11365 08:36:52,000 --> 08:36:56,558 loop like we can see here in this 11366 08:36:54,637 --> 08:36:58,797 lecture we're going to learn about do 11367 08:36:56,558 --> 08:37:01,120 while loops and this is yet another 11368 08:36:58,797 --> 08:37:03,039 construct we have in c plus plus to do 11369 08:37:03,040 --> 08:37:08,558 let's look at the syntax here we have 11370 08:37:05,520 --> 08:37:10,637 our declaration of the iterator on the 11371 08:37:08,558 --> 08:37:13,440 outside and after that you're going to 11372 08:37:10,637 --> 08:37:15,759 say do you're going to put a pair of 11373 08:37:13,439 --> 08:37:18,397 curly braces like this you're going to 11374 08:37:15,759 --> 08:37:20,397 put whatever code it is you want to run 11375 08:37:18,398 --> 08:37:22,958 inside the loop and then you're going to 11376 08:37:20,398 --> 08:37:25,840 do your incrementation and after the 11377 08:37:22,957 --> 08:37:27,439 closing curly brace you're going to say 11378 08:37:25,840 --> 08:37:29,599 while and you're going to put in your 11379 08:37:27,439 --> 08:37:32,317 text this is really the structure again 11380 08:37:29,599 --> 08:37:33,599 notice that we have those five parts and 11381 08:37:33,599 --> 08:37:38,317 declaration of our iterator and we're 11382 08:37:36,240 --> 08:37:40,558 going to initialize this in place for 11383 08:37:38,317 --> 08:37:42,239 example if we want to do that 11384 08:37:42,240 --> 08:37:46,558 fall in the body of the loop 11385 08:37:44,718 --> 08:37:48,798 and after that we're going to do the 11386 08:37:46,558 --> 08:37:50,958 incrementation after we run the code it 11387 08:37:48,797 --> 08:37:53,439 is we want to run in the body of the 11388 08:37:50,957 --> 08:37:56,797 loop and then we're going to do the test 11389 08:37:53,439 --> 08:38:00,477 later and the special thing about a do 11390 08:37:56,797 --> 08:38:03,520 while loop is that it is going to run 11391 08:38:00,477 --> 08:38:05,200 first and then you do the test last 11392 08:38:03,520 --> 08:38:07,760 this is really something you need to be 11393 08:38:05,200 --> 08:38:09,680 careful about make sure that when it 11394 08:38:07,759 --> 08:38:12,159 runs it is doing whatever it is you want 11395 08:38:09,680 --> 08:38:15,200 to do because the test is going to run 11396 08:38:12,159 --> 08:38:17,119 after your code has actually run and you 11397 08:38:15,200 --> 08:38:19,680 can tweak this to do whatever it is we 11398 08:38:17,119 --> 08:38:21,840 want it to do as we are going to see 11399 08:38:19,680 --> 08:38:24,000 when we hit visual studio code in a 11400 08:38:21,840 --> 08:38:26,797 minute okay again one thing you need to 11401 08:38:24,000 --> 08:38:28,797 keep in mind is that the do while loop 11402 08:38:28,797 --> 08:38:34,239 of the loop and then do the check or the 11403 08:38:31,599 --> 08:38:36,557 test and that may come in handy in some 11404 08:38:34,240 --> 08:38:38,718 of the applications you'll be doing 11405 08:38:36,558 --> 08:38:40,798 within your career as a c plus plus 11406 08:38:38,718 --> 08:38:42,477 developer okay this is really the syntax 11407 08:38:40,797 --> 08:38:44,878 we're going to be using we're going to 11408 08:38:42,477 --> 08:38:48,317 head over to visual studio code and play 11409 08:38:44,878 --> 08:38:50,317 with us okay here we are in our working 11410 08:38:48,317 --> 08:38:52,079 folder the current project is going to 11411 08:38:52,080 --> 08:38:57,360 we're going to grab the template files 11412 08:38:54,718 --> 08:39:00,000 pretty fast and we are going to put them 11413 08:38:57,360 --> 08:39:02,159 in our current project let's open this 11414 08:39:02,159 --> 08:39:07,279 and we are going to open this in visual 11415 08:39:04,637 --> 08:39:09,520 studio code do while loop is our project 11416 08:39:07,279 --> 08:39:12,477 we're going to open this and the main 11417 08:39:09,520 --> 08:39:14,797 cpp is opened up so let's remove 11418 08:39:12,477 --> 08:39:16,957 whatever it is we don't need and we are 11419 08:39:14,797 --> 08:39:19,599 going to show you the bad way to do 11420 08:39:16,957 --> 08:39:22,000 things the bad way to do things is 11421 08:39:19,599 --> 08:39:24,079 typing your code to do things manually 11422 08:39:22,000 --> 08:39:26,317 if you wanted them to be done multiple 11423 08:39:24,080 --> 08:39:28,240 times and this is something bad if we 11424 08:39:26,317 --> 08:39:30,878 bought this and run we're going to go 11425 08:39:30,878 --> 08:39:34,000 that's g brush we're going to run 11426 08:39:34,000 --> 08:39:37,759 and we're going to get i love c plus 11427 08:39:35,840 --> 08:39:40,240 plus 10 times and you don't want to do 11428 08:39:37,759 --> 08:39:42,079 something like this what we want in this 11429 08:39:42,080 --> 08:39:46,958 do while loops to solve this problem 11430 08:39:45,119 --> 08:39:49,039 we're going to comment this out because 11431 08:39:49,040 --> 08:39:53,840 and we're going to go down and set up 11432 08:39:51,200 --> 08:39:56,080 our do while loop the first thing we 11433 08:39:53,840 --> 08:39:57,920 need to do is to set up the count 11434 08:39:56,080 --> 08:39:59,600 because we want to store this in some 11435 08:39:57,919 --> 08:40:00,477 variables so we're going to say const 11436 08:40:01,840 --> 08:40:05,279 and we're going to put in our value 11437 08:40:05,279 --> 08:40:11,360 not 190 we're going to say 10 times 11438 08:40:08,957 --> 08:40:14,557 and we're going to set up our iterator 11439 08:40:14,558 --> 08:40:18,958 and we're going to say i and initialize 11440 08:40:16,558 --> 08:40:20,638 this with our starting point we're going 11441 08:40:20,637 --> 08:40:27,360 and what we're going to do is say do 11442 08:40:24,080 --> 08:40:30,318 and put a pair of curly braces 11443 08:40:27,360 --> 08:40:31,840 and inside this pair of curly braces 11444 08:40:30,317 --> 08:40:33,119 we're going to put whatever it is we 11445 08:40:33,119 --> 08:40:37,360 multiple times so we want to print i 11446 08:40:35,599 --> 08:40:38,957 love c plus plus so that's what we're 11447 08:40:38,957 --> 08:40:43,199 put in the current iteration 11448 08:40:41,439 --> 08:40:46,159 and we're going to put a column and say 11449 08:40:46,159 --> 08:40:51,200 and then we're going to jump to the new 11450 08:40:51,200 --> 08:40:55,520 and after that we don't want to forget 11451 08:40:53,599 --> 08:40:57,680 our incrementation part so we're going 11452 08:40:59,680 --> 08:41:04,718 the part that moves our iterator to the 11453 08:41:02,637 --> 08:41:07,039 next step this is going to be the 11454 08:41:04,718 --> 08:41:08,398 incrementation and this is going to be 11455 08:41:11,040 --> 08:41:16,159 and after the closing brace we need to 11456 08:41:16,159 --> 08:41:19,919 so we're going to say while and we're 11457 08:41:18,398 --> 08:41:22,718 going to put in our test here and we 11458 08:41:19,919 --> 08:41:24,957 want to keep doing this as long as i is 11459 08:41:24,957 --> 08:41:28,717 and we're going to put in our closing 11460 08:41:28,718 --> 08:41:34,317 now this code is going to run 10 times 11461 08:41:31,919 --> 08:41:36,317 but let's go through how it's going to 11462 08:41:34,317 --> 08:41:37,520 run so that you can really see 11463 08:41:37,520 --> 08:41:42,439 that is happening here okay so when this 11464 08:41:40,240 --> 08:41:44,958 code here is run we're going to hit the 11465 08:41:42,439 --> 08:41:48,159 initialization line here we're going to 11466 08:41:44,957 --> 08:41:50,557 declare i and we're going to put in a 0. 11467 08:41:48,159 --> 08:41:52,878 we're going to say do and inside we're 11468 08:41:50,558 --> 08:41:55,520 going to print whatever we have in i and 11469 08:41:52,878 --> 08:41:58,558 say i love c plus plus so we're going to 11470 08:41:55,520 --> 08:42:02,398 say 0 because i is 0 at this point and 11471 08:41:58,558 --> 08:42:04,477 we're going to say i love c plus plus 11472 08:42:02,398 --> 08:42:07,680 and after that we're going to increment 11473 08:42:07,680 --> 08:42:10,477 and we are going to do the test at this 11474 08:42:10,477 --> 08:42:14,797 we're going to say is one less than ten 11475 08:42:12,878 --> 08:42:17,600 that's going to be successful so we're 11476 08:42:17,599 --> 08:42:22,397 and we're going to say 1 i love c plus 11477 08:42:20,317 --> 08:42:24,398 let's copy this and print this again so 11478 08:42:22,398 --> 08:42:26,159 that we don't have to type this all the 11479 08:42:26,159 --> 08:42:31,119 i love c plus and after that we're going 11480 08:42:28,637 --> 08:42:33,439 to run the incrementation part again so 11481 08:42:31,119 --> 08:42:36,397 we're going to get a 2 inside i we're 11482 08:42:33,439 --> 08:42:38,159 going to test and say is 2 less than 10 11483 08:42:36,398 --> 08:42:41,280 that's going to be true so we're going 11484 08:42:38,159 --> 08:42:43,439 to run the body again and print 2 i love 11485 08:42:43,439 --> 08:42:46,957 and we're going to increment to get a 11486 08:42:46,957 --> 08:42:51,199 we're going to run the test against our 11487 08:42:49,119 --> 08:42:52,797 three is three less than ten that's 11488 08:42:51,200 --> 08:42:55,520 going to be true so we're going to fall 11489 08:42:52,797 --> 08:42:58,000 in the body again and print three 11490 08:42:55,520 --> 08:42:59,680 i love c plus plus let's do that we're 11491 08:42:58,000 --> 08:43:02,240 going to do the incrementation we're 11492 08:42:59,680 --> 08:43:04,398 going to get a 4 inside this i variable 11493 08:43:02,240 --> 08:43:06,478 here we're going to run the test we're 11494 08:43:04,398 --> 08:43:08,398 going to say is 4 less than 10 that's 11495 08:43:06,477 --> 08:43:11,520 going to be true of course we're going 11496 08:43:08,398 --> 08:43:13,120 to say four i love c plus plus when we 11497 08:43:13,119 --> 08:43:17,200 we're going to do the implementation and 11498 08:43:14,718 --> 08:43:19,119 get a five we're going to do the test 11499 08:43:17,200 --> 08:43:21,280 it's five less than ten that's going to 11500 08:43:19,119 --> 08:43:24,159 be true so we're going to fall in the 11501 08:43:21,279 --> 08:43:25,919 body again and say five i love c plus 11502 08:43:27,279 --> 08:43:31,919 we're going to increment again 11503 08:43:30,000 --> 08:43:34,878 we're going to get a six the test is 11504 08:43:31,919 --> 08:43:36,797 going to run and we're going to say is 11505 08:43:36,797 --> 08:43:42,159 that's going to be true again then the 11506 08:43:38,878 --> 08:43:44,477 body is going to execute and say 11507 08:43:42,159 --> 08:43:47,119 6 i love c plus plus we're going to do 11508 08:43:44,477 --> 08:43:49,360 the incrementation and get a 7. we're 11509 08:43:47,119 --> 08:43:51,520 going to run the test against our 7 so 11510 08:43:49,360 --> 08:43:53,040 we're going to say is 7 less than 10 11511 08:43:53,040 --> 08:43:58,398 we are going to fall in the body again 11512 08:43:55,360 --> 08:44:00,477 and say 7 i love c plus plus 11513 08:43:58,398 --> 08:44:03,120 we are going to do the incrementation we 11514 08:44:00,477 --> 08:44:06,000 are going to get 8 we're going to say is 11515 08:44:03,119 --> 08:44:08,000 8 less than 10 that's going to be true 11516 08:44:06,000 --> 08:44:10,878 we are going to fall in the body again 11517 08:44:08,000 --> 08:44:12,398 and say 8 i love c plus plus we're going 11518 08:44:10,878 --> 08:44:14,878 to do the incrementation we're going to 11519 08:44:12,398 --> 08:44:16,958 get a 9 and we're going to run the test 11520 08:44:14,878 --> 08:44:18,477 we're going to say is 9 less than 10 11521 08:44:16,957 --> 08:44:21,759 that's going to be true so we're going 11522 08:44:18,477 --> 08:44:23,360 to go inside and say 9 i love c plus 11523 08:44:23,360 --> 08:44:27,680 and we're going to do the incrementation 11524 08:44:25,040 --> 08:44:30,958 again now we're going to get a 10 and 11525 08:44:27,680 --> 08:44:31,760 the test is going to say is 10 less than 11526 08:44:31,759 --> 08:44:35,519 and that's going to be false now the 11527 08:44:35,520 --> 08:44:40,878 and we're going to fall after the while 11528 08:44:38,398 --> 08:44:44,159 loop here so if we had a statement here 11529 08:44:40,878 --> 08:44:46,477 that said stdc out loop done we would 11530 08:44:44,159 --> 08:44:48,797 print that we would go down here and say 11531 08:44:48,797 --> 08:44:52,397 and this is what we're going to get if 11532 08:44:50,398 --> 08:44:54,240 we run this program and again i 11533 08:44:52,398 --> 08:44:56,798 encourage you to try this out in your 11534 08:44:54,240 --> 08:44:59,200 mind print this in some file you have on 11535 08:44:56,797 --> 08:45:01,680 your computer or you can even use a 11536 08:44:59,200 --> 08:45:04,000 regular piece of paper but make sure you 11537 08:45:01,680 --> 08:45:06,477 understand the flow we go through to get 11538 08:45:04,000 --> 08:45:08,718 this code to run multiple times okay now 11539 08:45:06,477 --> 08:45:10,477 that we have done this we can work this 11540 08:45:10,477 --> 08:45:14,957 do the heavy work for us and run this 11541 08:45:12,797 --> 08:45:16,957 fence multiple times the world is going 11542 08:45:14,957 --> 08:45:18,797 to be good so what we're going to do 11543 08:45:18,797 --> 08:45:23,360 and run rooster and it's going to say 11544 08:45:21,040 --> 08:45:25,760 exactly what we expect i'll have c plus 11545 08:45:23,360 --> 08:45:28,080 10 times and then it's going to say 11546 08:45:25,759 --> 08:45:29,039 loop done okay one thing i want you to 11547 08:45:29,040 --> 08:45:35,920 is that the do while loop runs before it 11548 08:45:32,957 --> 08:45:38,397 does the test and this may cause crazy 11549 08:45:35,919 --> 08:45:40,079 things happening in your program if 11550 08:45:40,080 --> 08:45:44,958 let's say that you wanted this program 11551 08:45:42,477 --> 08:45:47,200 not to run and initialize this thing 11552 08:45:44,957 --> 08:45:49,759 with a zero for the count so it's going 11553 08:45:47,200 --> 08:45:51,840 to run zero times but that's not going 11554 08:45:49,759 --> 08:45:53,359 to do what you expect with the do while 11555 08:45:53,360 --> 08:45:59,200 it runs things before it does the test 11556 08:45:57,279 --> 08:46:01,439 so if you run this program it's not 11557 08:45:59,200 --> 08:46:03,760 going to just say loop done it's going 11558 08:46:01,439 --> 08:46:06,079 to say i love c plus plus and have zero 11559 08:46:03,759 --> 08:46:07,599 value in here and it's going to say loop 11560 08:46:07,599 --> 08:46:12,557 that's not going to be probably what you 11561 08:46:10,000 --> 08:46:15,279 intended by putting a zero in here you 11562 08:46:12,558 --> 08:46:17,840 put a zero in here if you want this code 11563 08:46:15,279 --> 08:46:20,317 to run zero times or the code in the 11564 08:46:17,840 --> 08:46:22,878 body not to run that's another way to 11565 08:46:20,317 --> 08:46:25,119 say that but in this case a do while 11566 08:46:22,878 --> 08:46:28,878 loop is not going to do what you expect 11567 08:46:25,119 --> 08:46:29,840 because it runs before it does the test 11568 08:46:29,840 --> 08:46:35,520 let's build this and show you this 11569 08:46:33,360 --> 08:46:38,240 so we're going to go through the world 11570 08:46:40,797 --> 08:46:44,717 and run rooster you're going to see that 11571 08:46:42,558 --> 08:46:47,920 it's saying zero i love c plus plus loop 11572 08:46:44,718 --> 08:46:51,040 done it is running even if we set our 11573 08:46:47,919 --> 08:46:53,519 count to zero here so be sure the do 11574 08:46:51,040 --> 08:46:55,360 while loop is doing what you want but if 11575 08:46:55,360 --> 08:47:00,240 you're going to have no problems and if 11576 08:46:57,840 --> 08:47:01,360 you want the rope to actually not do 11577 08:47:01,360 --> 08:47:06,797 when you have zero counting here you can 11578 08:47:03,840 --> 08:47:08,398 use the while loop or the full look like 11579 08:47:08,398 --> 08:47:11,600 i would like to welcome you in this new 11580 08:47:11,599 --> 08:47:17,519 where we're going to be learning about 11581 08:47:13,680 --> 08:47:19,840 arrays and arrays are a way to 11582 08:47:17,520 --> 08:47:22,317 set up collections in our c plus plus 11583 08:47:19,840 --> 08:47:24,317 program so far we have been working with 11584 08:47:22,317 --> 08:47:26,878 single variables for example you have a 11585 08:47:24,317 --> 08:47:30,477 bunch of integer variables you can even 11586 08:47:26,878 --> 08:47:32,080 have 10 or 20 variables of integer type 11587 08:47:30,477 --> 08:47:34,239 and that's going to be fine but 11588 08:47:32,080 --> 08:47:37,680 sometimes you don't want to manage all 11589 08:47:34,240 --> 08:47:40,718 these variables it is really desirable 11590 08:47:37,680 --> 08:47:43,520 to group all these variables into one 11591 08:47:40,718 --> 08:47:45,920 single unity and manage them as one 11592 08:47:43,520 --> 08:47:48,477 single entity and we can do something 11593 08:47:45,919 --> 08:47:50,957 like this group them together and give 11594 08:47:48,477 --> 08:47:52,797 them a name so that we can refer to them 11595 08:47:52,797 --> 08:47:59,680 under this name here and c plus plus has 11596 08:47:55,840 --> 08:48:02,558 the array construct to help us do this 11597 08:47:59,680 --> 08:48:04,159 okay so again if you look in memory how 11598 08:48:02,558 --> 08:48:06,317 things are laid out you're going to have 11599 08:48:04,159 --> 08:48:09,840 a bunch of zeros and ones for example 11600 08:48:06,317 --> 08:48:12,398 here each red defend is a net it is four 11601 08:48:09,840 --> 08:48:15,119 bytes in size and it can be used to 11602 08:48:12,398 --> 08:48:17,280 represent a net so if we have a bunch of 11603 08:48:15,119 --> 08:48:19,680 events like this we might want to group 11604 08:48:17,279 --> 08:48:22,477 them together and give them a name and 11605 08:48:19,680 --> 08:48:24,159 manipulate them under this name this is 11606 08:48:22,477 --> 08:48:27,200 another way to look at what we just 11607 08:48:24,159 --> 08:48:29,520 talked about here and again arrays are a 11608 08:48:27,200 --> 08:48:31,920 way to do these kinds of things in c 11609 08:48:29,520 --> 08:48:34,398 plus plus and we're going to be learning 11610 08:48:31,919 --> 08:48:36,477 all about it in this chapter we are 11611 08:48:34,398 --> 08:48:38,240 going to stop here in this lecture and 11612 08:48:36,477 --> 08:48:40,878 we are going to start and look at how 11613 08:48:38,240 --> 08:48:42,318 you can declare and initialize an array 11614 08:48:44,240 --> 08:48:48,718 and meet me there in this lecture we're 11615 08:48:46,317 --> 08:48:52,159 going to see how we can declare and use 11616 08:48:48,718 --> 08:48:55,119 arrays in our c plus plus program and 11617 08:48:52,159 --> 08:48:57,680 again arrays are a facility we have in 11618 08:48:55,119 --> 08:49:00,557 the c plus plus programming language to 11619 08:48:57,680 --> 08:49:03,840 group many variables together and 11620 08:49:00,558 --> 08:49:06,317 manipulate them as a single unit for 11621 08:49:03,840 --> 08:49:08,637 example here we have a bunch of integers 11622 08:49:06,317 --> 08:49:11,119 in memory each of these red things is 11623 08:49:08,637 --> 08:49:13,360 going to be four bytes in size so we can 11624 08:49:11,119 --> 08:49:16,000 think of it as an integer and we can 11625 08:49:13,360 --> 08:49:18,398 group them together and give them an a 11626 08:49:16,000 --> 08:49:21,439 we call them scores here and they are 11627 08:49:18,398 --> 08:49:24,000 going to be living under that scores 11628 08:49:24,000 --> 08:49:29,439 refer to each of these things z plus 11629 08:49:26,558 --> 08:49:32,558 plus provides an indexing system so we 11630 08:49:29,439 --> 08:49:36,317 cannot use angle brackets like this and 11631 08:49:32,558 --> 08:49:39,040 refer to the first of these integers as 11632 08:49:36,317 --> 08:49:42,317 zero so we can go from zero all the way 11633 08:49:39,040 --> 08:49:44,558 to nine here but your array can even be 11634 08:49:42,317 --> 08:49:46,477 bigger than this many times bigger than 11635 08:49:44,558 --> 08:49:48,317 this and we're going to see examples of 11636 08:49:46,477 --> 08:49:51,040 how we can do that but this is an 11637 08:49:48,317 --> 08:49:54,637 example of how you can take many 11638 08:49:51,040 --> 08:49:56,718 variables group them under one entity 11639 08:49:54,637 --> 08:49:59,119 give them a name and be able to 11640 08:49:56,718 --> 08:50:01,760 reference each of these events that are 11641 08:49:59,119 --> 08:50:04,317 under that entity and now that the idea 11642 08:50:01,759 --> 08:50:06,717 is pretty clear let's see how we can do 11643 08:50:04,317 --> 08:50:09,200 this in c plus plus the way to do that 11644 08:50:06,718 --> 08:50:11,600 is to say and you have to specify the 11645 08:50:09,200 --> 08:50:13,520 type of your collection you have to 11646 08:50:11,599 --> 08:50:16,000 specify the name after that and then 11647 08:50:13,520 --> 08:50:18,240 you're going to put a pair of angle 11648 08:50:16,000 --> 08:50:21,119 brackets like this and inside those 11649 08:50:18,240 --> 08:50:23,520 angle brackets you can specify the size 11650 08:50:21,119 --> 08:50:26,718 you want that collection to have or that 11651 08:50:23,520 --> 08:50:29,119 array to have and after this statement 11652 08:50:26,718 --> 08:50:30,558 is run we are basically going to have 11653 08:50:29,119 --> 08:50:32,557 something like this in memory we're 11654 08:50:30,558 --> 08:50:35,280 going to have 10 integers so if you 11655 08:50:32,558 --> 08:50:37,360 count here 0 all the way through 9 11656 08:50:35,279 --> 08:50:39,840 that's 10 integers they're going to be 11657 08:50:37,360 --> 08:50:41,760 grouped together under one logic entity 11658 08:50:39,840 --> 08:50:44,637 that logic entity is going to be called 11659 08:50:41,759 --> 08:50:47,439 scores and we can go through that name 11660 08:50:44,637 --> 08:50:50,159 to manipulate all these things that are 11661 08:50:47,439 --> 08:50:52,397 in this entity okay after your array is 11662 08:50:50,159 --> 08:50:54,637 declared and you have space for it in 11663 08:50:52,398 --> 08:50:57,120 memory basically something like this we 11664 08:50:54,637 --> 08:50:59,119 need a way to read data from at and 11665 08:50:57,119 --> 08:51:01,360 we're going to see how we can do that 11666 08:50:59,119 --> 08:51:03,360 and it is what we have here for example 11667 08:51:01,360 --> 08:51:05,600 if we want to print the first of these 11668 08:51:03,360 --> 08:51:08,080 elements we're going to say scores 11669 08:51:05,599 --> 08:51:11,279 angle brackets and specify the index of 11670 08:51:08,080 --> 08:51:13,840 the first element in that array or that 11671 08:51:11,279 --> 08:51:16,159 collection of integers that is called 11672 08:51:13,840 --> 08:51:19,439 scores okay one thing you should really 11673 08:51:16,159 --> 08:51:21,840 keep in mind is that arrays are indexed 11674 08:51:19,439 --> 08:51:23,919 from zero they don't start from one so 11675 08:51:21,840 --> 08:51:26,080 the first element is going to live at 11676 08:51:23,919 --> 08:51:28,637 index zero it's not going to live at 11677 08:51:26,080 --> 08:51:30,958 index one so here scores zero is going 11678 08:51:28,637 --> 08:51:32,878 to print the first element and scores 11679 08:51:30,957 --> 08:51:35,119 nine is going to print the last element 11680 08:51:32,878 --> 08:51:38,000 which happens to be the last in our 11681 08:51:35,119 --> 08:51:40,797 collection here of integers okay once 11682 08:51:38,000 --> 08:51:42,957 our array is declared it's not going to 11683 08:51:40,797 --> 08:51:45,439 have data in it there is going to be 11684 08:51:42,957 --> 08:51:48,159 data in but it's not going to be data 11685 08:51:45,439 --> 08:51:51,119 that we put in so we can think of this 11686 08:51:48,159 --> 08:51:53,599 data basically as garbage another thing 11687 08:51:51,119 --> 08:51:54,477 i wanted to bring to your attention is 11688 08:51:54,477 --> 08:52:00,558 your array has clear boundaries when we 11689 08:51:58,398 --> 08:52:04,558 declare an array like this notice that 11690 08:52:00,558 --> 08:52:06,638 we specify 10 as the size of our array 11691 08:52:04,558 --> 08:52:08,798 it's only going to have 10 elements 11692 08:52:06,637 --> 08:52:11,520 inside and you can see that we have from 11693 08:52:08,797 --> 08:52:13,759 index 0 all the way to 9 and inside we 11694 08:52:11,520 --> 08:52:15,920 have garbage data but we're going to fix 11695 08:52:13,759 --> 08:52:18,317 that in a minute what i want you to see 11696 08:52:15,919 --> 08:52:20,957 here is that you can to try and read 11697 08:52:18,317 --> 08:52:23,279 outside the bounds of your array 11698 08:52:20,957 --> 08:52:25,359 c plus plus is funny in that it allows 11699 08:52:23,279 --> 08:52:27,279 you to do that but if you do that you're 11700 08:52:25,360 --> 08:52:29,760 going to get weird problems for example 11701 08:52:27,279 --> 08:52:31,680 your program would crash or do something 11702 08:52:29,759 --> 08:52:35,199 really bad so you shouldn't really do 11703 08:52:31,680 --> 08:52:37,760 this be aware of the boundaries of your 11704 08:52:35,200 --> 08:52:40,080 array if you try to read the data from 11705 08:52:37,759 --> 08:52:42,797 the outside of your balance you may read 11706 08:52:40,080 --> 08:52:44,718 garbage data you may even cause failure 11707 08:52:42,797 --> 08:52:46,957 of the operating system because you 11708 08:52:44,718 --> 08:52:49,200 don't really know whose memory you're 11709 08:52:46,957 --> 08:52:51,840 reading it's not going to be memory that 11710 08:52:49,200 --> 08:52:53,360 is allocated to your program so array 11711 08:52:51,840 --> 08:52:56,159 boundaries are something you need to 11712 08:52:53,360 --> 08:52:58,637 keep in mind when working with arrays 11713 08:52:56,159 --> 08:53:00,957 okay we just saw that we could read data 11714 08:52:58,637 --> 08:53:03,919 from the array using this angle bracket 11715 08:53:00,957 --> 08:53:05,359 syntax we can even do that through the 11716 08:53:05,360 --> 08:53:09,200 read all the elements in the array and 11717 08:53:07,200 --> 08:53:11,040 this is how we could do that and you can 11718 08:53:09,200 --> 08:53:12,398 see that the loops we learned about in 11719 08:53:12,398 --> 08:53:16,878 are starting to come in handy okay we 11720 08:53:14,317 --> 08:53:17,919 have seen how we can read data from the 11721 08:53:17,919 --> 08:53:22,557 but what if we want to put something 11722 08:53:20,159 --> 08:53:25,840 back in what if we want to write our own 11723 08:53:22,558 --> 08:53:28,000 data in an array well the syntax is 11724 08:53:25,840 --> 08:53:30,558 pretty much what you would expect we 11725 08:53:28,000 --> 08:53:33,680 also use this angle bracket syntax but 11726 08:53:30,558 --> 08:53:35,680 we're going to assign data to that 11727 08:53:33,680 --> 08:53:38,558 element in the array for example if we 11728 08:53:35,680 --> 08:53:41,279 wanted to assign data to the first 11729 08:53:38,558 --> 08:53:42,317 element in our array we're going to say 11730 08:53:42,317 --> 08:53:47,360 angle brackets 0 and we're going to 11731 08:53:44,398 --> 08:53:51,520 assign a 20 in it and we can assign a 21 11732 08:53:47,360 --> 08:53:54,797 into the element at index 1 and the 22 11733 08:53:51,520 --> 08:53:56,240 in the element at index 2 and if we do 11734 08:53:54,797 --> 08:53:59,039 that we're going to have something like 11735 08:53:56,240 --> 08:54:02,159 this in our array so index 0 we have a 11736 08:53:59,040 --> 08:54:05,520 20 index 1 we have a 21 index 2 we have 11737 08:54:02,159 --> 08:54:08,000 a 22 and the rest is still garbage 11738 08:54:05,520 --> 08:54:10,317 because we didn't put in our data we can 11739 08:54:08,000 --> 08:54:13,439 also write data in a loop for example 11740 08:54:10,317 --> 08:54:15,520 here we have our array declared 11741 08:54:13,439 --> 08:54:18,397 and we're going to loop around putting 11742 08:54:15,520 --> 08:54:20,477 data at different indexes notice that 11743 08:54:18,398 --> 08:54:23,280 the index is going to be the current 11744 08:54:20,477 --> 08:54:25,680 iteration we are at so this is a common 11745 08:54:23,279 --> 08:54:27,840 way to do things in c plus plus and you 11746 08:54:25,680 --> 08:54:30,398 need to be familiar with this and after 11747 08:54:27,840 --> 08:54:32,797 we put in the data we can read that back 11748 08:54:30,398 --> 08:54:34,718 and make sure that it is the actual data 11749 08:54:32,797 --> 08:54:36,239 that we put in and we're going to have a 11750 08:54:34,718 --> 08:54:38,637 chance to play with this in visual 11751 08:54:36,240 --> 08:54:40,798 studio code in a minute so don't worry 11752 08:54:38,637 --> 08:54:43,279 if some of these things don't make sense 11753 08:54:40,797 --> 08:54:45,680 yet but again the main thing here is 11754 08:54:43,279 --> 08:54:49,039 this angle bracket syntax we can use 11755 08:54:45,680 --> 08:54:51,599 that to either write data in the element 11756 08:54:49,040 --> 08:54:53,280 of the array or read data from the 11757 08:54:51,599 --> 08:54:56,557 element of the array and this is the 11758 08:54:53,279 --> 08:54:59,840 syntax here scores angle brackets i okay 11759 08:54:56,558 --> 08:55:01,920 so far our array has been declared and 11760 08:54:59,840 --> 08:55:04,398 we didn't really put in data and there 11761 08:55:01,919 --> 08:55:07,519 was junk data inside but there's a way 11762 08:55:04,398 --> 08:55:10,240 you can declare an array and put data in 11763 08:55:07,520 --> 08:55:12,159 in one single statement and this is how 11764 08:55:10,240 --> 08:55:14,240 you would do it here we have another 11765 08:55:12,159 --> 08:55:15,439 array it's going to be grouping double 11766 08:55:15,439 --> 08:55:19,840 we call the array salaries and we 11767 08:55:17,439 --> 08:55:23,279 specify that it's going to be 11768 08:55:19,840 --> 08:55:27,360 of size five and we can put these 11769 08:55:23,279 --> 08:55:29,520 elements in a comma separated list using 11770 08:55:27,360 --> 08:55:31,440 braced initialization like this and it 11771 08:55:29,520 --> 08:55:32,957 is going to work if we try to print it 11772 08:55:31,439 --> 08:55:35,119 out we're going to see these elements 11773 08:55:32,957 --> 08:55:36,797 here and you see that we can read this 11774 08:55:35,119 --> 08:55:40,239 using the same syntax we've learned 11775 08:55:36,797 --> 08:55:42,317 about salaries we say the array name we 11776 08:55:40,240 --> 08:55:44,878 do angle brackets and inside we put the 11777 08:55:42,317 --> 08:55:47,680 index of the element we are reading from 11778 08:55:44,878 --> 08:55:49,840 and this index is coming from our loop 11779 08:55:47,680 --> 08:55:51,840 here and it is going to work you're 11780 08:55:49,840 --> 08:55:54,080 going to get this element printed out 11781 08:55:51,840 --> 08:55:55,599 this is pretty cool when initializing 11782 08:55:55,599 --> 08:56:00,079 you can leave out elements for example 11783 08:55:57,360 --> 08:56:03,040 here we have a neat array it is called 11784 08:56:00,080 --> 08:56:05,440 families it's going to be of size 5 but 11785 08:56:03,040 --> 08:56:08,080 we are just putting in three elements 11786 08:56:05,439 --> 08:56:10,557 the rest are going to be initialized to 11787 08:56:08,080 --> 08:56:12,398 zero when we do things like this and 11788 08:56:10,558 --> 08:56:14,477 this is something you need to be aware 11789 08:56:12,398 --> 08:56:16,080 of and we are going to try this out in 11790 08:56:14,477 --> 08:56:18,239 visual studio code and you're going to 11791 08:56:16,080 --> 08:56:21,280 see that this is actually true it is 11792 08:56:18,240 --> 08:56:23,360 possible to also omit the size when you 11793 08:56:21,279 --> 08:56:25,599 are declaring your array and the 11794 08:56:23,360 --> 08:56:28,000 compiler is going to deduce the size 11795 08:56:25,599 --> 08:56:30,637 from the elements that you initialize 11796 08:56:28,000 --> 08:56:33,360 your array with for example here we have 11797 08:56:30,637 --> 08:56:35,919 in one two three four five six elements 11798 08:56:33,360 --> 08:56:38,000 this array is going to have a size of 11799 08:56:35,919 --> 08:56:40,079 six and if we print elements we're going 11800 08:56:38,000 --> 08:56:43,520 to get those printed out notice here 11801 08:56:40,080 --> 08:56:44,958 that we are using a ranged base for loop 11802 08:56:44,957 --> 08:56:49,279 hopefully you can see that it is easier 11803 08:56:46,797 --> 08:56:51,439 to read data from a collection like this 11804 08:56:49,279 --> 08:56:53,840 using a ranged base for loop because we 11805 08:56:51,439 --> 08:56:55,919 don't have to set up an iterator 11806 08:56:53,840 --> 08:56:58,000 initialize it to do the test do the 11807 08:56:55,919 --> 08:57:01,279 incrementation we can just read the 11808 08:56:58,000 --> 08:57:03,360 value and use it and be on our merry way 11809 08:57:01,279 --> 08:57:06,317 this is pretty cool it is possible to 11810 08:57:03,360 --> 08:57:08,080 also declare your array constant and 11811 08:57:06,317 --> 08:57:10,477 when you do that you won't be able to 11812 08:57:08,080 --> 08:57:12,478 modify elements of that array 11813 08:57:10,477 --> 08:57:14,878 that's what it means if you make it 11814 08:57:12,477 --> 08:57:17,279 constant you basically want it to stay 11815 08:57:14,878 --> 08:57:19,760 constant and you don't want to allow 11816 08:57:17,279 --> 08:57:21,439 modifications of your array and we're 11817 08:57:19,759 --> 08:57:23,759 going to see this in a minute when we 11818 08:57:21,439 --> 08:57:25,279 get in visual studio code the last thing 11819 08:57:23,759 --> 08:57:27,759 i want you to see is that you can 11820 08:57:25,279 --> 08:57:30,397 actually do operations on the data that 11821 08:57:27,759 --> 08:57:32,239 you have stored in an array for example 11822 08:57:30,398 --> 08:57:34,798 here we can sum the elements up and 11823 08:57:32,240 --> 08:57:36,798 store the result back in our variable 11824 08:57:34,797 --> 08:57:39,919 sum and this is something you would do 11825 08:57:36,797 --> 08:57:42,317 and it might come in handy in many times 11826 08:57:39,919 --> 08:57:44,397 okay one thing i want you to see before 11827 08:57:42,317 --> 08:57:47,599 we head over to visual studio code and 11828 08:57:44,398 --> 08:57:49,920 play with us is that arrays store 11829 08:57:49,919 --> 08:57:56,159 repeat after me arrays store elements of 11830 08:57:53,599 --> 08:57:58,477 the same type you can't try and store 11831 08:57:56,159 --> 08:58:00,079 variables of different types in an array 11832 08:57:58,477 --> 08:58:02,878 i don't even know how you would actually 11833 08:58:00,080 --> 08:58:04,318 declare that and if you try and store a 11834 08:58:04,317 --> 08:58:08,878 than the type that was declared for the 11835 08:58:06,797 --> 08:58:10,317 array you're going to get a compiler 11836 08:58:08,878 --> 08:58:13,680 error so this is something you should 11837 08:58:10,317 --> 08:58:15,439 keep in mind but it makes sense because 11838 08:58:13,680 --> 08:58:18,957 when you declare an array you're going 11839 08:58:15,439 --> 08:58:20,477 to be given a bunch of bytes in memory 11840 08:58:18,957 --> 08:58:23,840 and those bytes are going to be 11841 08:58:20,477 --> 08:58:26,159 collections of data that is of the same 11842 08:58:23,840 --> 08:58:28,637 type for example here we have a bunch of 11843 08:58:26,159 --> 08:58:31,360 integers again each of these red events 11844 08:58:28,637 --> 08:58:34,239 is an integer and if you try to put in 11845 08:58:31,360 --> 08:58:36,558 something that is smaller or bigger than 11846 08:58:34,240 --> 08:58:38,958 an edge then the compiler is going to be 11847 08:58:36,558 --> 08:58:42,240 confused that as to how to read these 11848 08:58:38,957 --> 08:58:44,797 text so for arrays to work the types 11849 08:58:42,240 --> 08:58:47,600 that you store in the array have to be 11850 08:58:44,797 --> 08:58:49,360 the same and they need to be consistent 11851 08:58:47,599 --> 08:58:51,599 this is something you should know and if 11852 08:58:49,360 --> 08:58:53,440 you try to break this rule you're going 11853 08:58:53,439 --> 08:58:57,840 now that you know about arrays we're 11854 08:58:55,599 --> 08:58:59,680 going to head over to visual studio code 11855 08:58:57,840 --> 08:59:02,718 and play with them a little bit okay 11856 08:58:59,680 --> 08:59:04,957 here we are in our working folder the 11857 08:59:02,718 --> 08:59:07,840 current chapter is raised the current 11858 08:59:04,957 --> 08:59:10,797 project is declaring and using arrays 11859 08:59:07,840 --> 08:59:12,957 we're going to grab our template code 11860 08:59:10,797 --> 08:59:14,477 and we're going to copy and move the 11861 08:59:14,477 --> 08:59:18,317 and we're going to open this thing up in 11862 08:59:16,558 --> 08:59:20,958 visual studio code pretty quick we're 11863 08:59:18,317 --> 08:59:23,520 going to open up the main cpp file and 11864 08:59:23,520 --> 08:59:28,398 and we're going to see how we can 11865 08:59:28,398 --> 08:59:33,520 okay the way you do that you say the 11866 08:59:30,477 --> 08:59:36,159 name of the arrays and you put these 11867 08:59:33,520 --> 08:59:38,637 angle brackets and specify the size of 11868 08:59:36,159 --> 08:59:40,240 your array if you want you can put a 11869 08:59:38,637 --> 08:59:42,000 space in here it's really not going to 11870 08:59:40,240 --> 08:59:44,878 make a difference and you're going to 11871 08:59:42,000 --> 08:59:47,599 put a semicolon at the end the moment 11872 08:59:44,878 --> 08:59:50,398 this line executes we're going to have 11873 08:59:47,599 --> 08:59:52,797 the space for 10 integers allocated to 11874 08:59:50,398 --> 08:59:54,638 us in memory and that space is going to 11875 08:59:52,797 --> 08:59:57,039 be called scores and we're going to be 11876 08:59:54,637 --> 08:59:59,840 able to read data from it or even write 11877 08:59:57,040 --> 09:00:01,920 data back into it for now let's see how 11878 08:59:59,840 --> 09:00:03,920 we can read the data and it is really 11879 09:00:01,919 --> 09:00:05,919 simple we're going to use this angle 11880 09:00:03,919 --> 09:00:08,317 bracket syntax that we just saw in the 11881 09:00:05,919 --> 09:00:10,079 slides so if we want to read the first 11882 09:00:10,080 --> 09:00:14,878 sddc out we want to print it out and 11883 09:00:14,878 --> 09:00:19,040 specify the index we want to read from 11884 09:00:19,040 --> 09:00:24,240 and we're going to say std endl to move 11885 09:00:22,080 --> 09:00:25,680 to the next line if you have this typed 11886 09:00:24,240 --> 09:00:27,920 out you're going to see that this is 11887 09:00:25,680 --> 09:00:31,119 going to work and it is going to print 11888 09:00:27,919 --> 09:00:33,679 whatever is stored in the first element 11889 09:00:31,119 --> 09:00:36,878 in this array here and again i want you 11890 09:00:33,680 --> 09:00:38,718 to keep in mind the memory structure of 11891 09:00:36,878 --> 09:00:40,718 your data when you are working with 11892 09:00:38,718 --> 09:00:44,558 arrays so what we're going to print 11893 09:00:40,718 --> 09:00:47,119 right now is whatever junk data is in 11894 09:00:44,558 --> 09:00:48,558 this first element here okay in here 11895 09:00:47,119 --> 09:00:50,797 we're going to say that we haven't 11896 09:00:50,797 --> 09:00:55,520 and let's bring up a terminal 11897 09:00:53,520 --> 09:00:58,159 so that we can play with us and we're 11898 09:00:55,520 --> 09:00:59,520 going to build this with gcc as we 11899 09:00:59,520 --> 09:01:03,680 the build is going to be good 11900 09:01:03,680 --> 09:01:08,718 and run rooster you see that this is the 11901 09:01:06,477 --> 09:01:09,599 junk value we have in here we don't have 11902 09:01:09,599 --> 09:01:15,039 useful data in here it's just junk data 11903 09:01:12,637 --> 09:01:17,599 because we didn't initialize this array 11904 09:01:15,040 --> 09:01:19,520 we can also read the element at location 11905 09:01:17,599 --> 09:01:22,317 two and see what we have in there when 11906 09:01:19,520 --> 09:01:23,840 you run the program i'm not even sure if 11907 09:01:22,317 --> 09:01:26,239 it is guaranteed that you're going to 11908 09:01:23,840 --> 09:01:28,558 get the same data if you run the program 11909 09:01:26,240 --> 09:01:31,360 multiple times but we don't really care 11910 09:01:28,558 --> 09:01:33,360 we know that it is junk data so we can't 11911 09:01:33,360 --> 09:01:38,317 what we're going to do is run this 11912 09:01:35,520 --> 09:01:39,840 and see the value for the element at 11913 09:01:42,957 --> 09:01:46,717 the world is going to be good we're 11914 09:01:46,718 --> 09:01:50,398 and we're going to run rooster and 11915 09:01:48,637 --> 09:01:51,840 you're going to see that at index 1 we 11916 09:01:52,878 --> 09:01:58,080 doing it manually like this is really 11917 09:01:55,119 --> 09:02:00,397 time consuming and it is possible to 11918 09:02:00,398 --> 09:02:06,478 we know the size of this array so it's a 11919 09:02:02,878 --> 09:02:09,600 10. and we can loop around 10 times 11920 09:02:06,477 --> 09:02:11,040 pretend each element at each iteration 11921 09:02:09,599 --> 09:02:13,199 and the way we do that we're going to 11922 09:02:11,040 --> 09:02:14,878 read with that loop and we're going to 11923 09:02:16,558 --> 09:02:20,878 and we're going to start from index to 11924 09:02:18,878 --> 09:02:23,760 zero this is really important because 11925 09:02:20,878 --> 09:02:25,680 arrays are indexed from zero they don't 11926 09:02:23,759 --> 09:02:27,840 start from one again this is something 11927 09:02:25,680 --> 09:02:29,680 you need to drill in your brain 11928 09:02:27,840 --> 09:02:32,240 and we're going to put in our test case 11929 09:02:29,680 --> 09:02:34,957 we're going to say i less than 10 11930 09:02:32,240 --> 09:02:37,440 because we want to go from 0 through 9 11931 09:02:34,957 --> 09:02:40,000 and we are going to increment our 11932 09:02:37,439 --> 09:02:41,199 iterator after we do that we're going to 11933 09:02:41,200 --> 09:02:46,159 jump into the body of the loop and we 11934 09:02:44,080 --> 09:02:48,160 didn't name our iterator that's why 11935 09:02:46,159 --> 09:02:50,079 we're having this squiggly line here 11936 09:02:48,159 --> 09:02:53,520 let's do that pretty quick we're going 11937 09:02:50,080 --> 09:02:55,840 to jump into the body of our loop and we 11938 09:02:55,840 --> 09:03:00,159 and we're going to say scores 11939 09:02:58,159 --> 09:03:02,477 and we need to be careful about how we 11940 09:03:00,159 --> 09:03:04,637 format this because i is not something 11941 09:03:02,477 --> 09:03:06,718 we have we're going to be getting that 11942 09:03:04,637 --> 09:03:08,557 from our iterator here so we're going to 11943 09:03:06,718 --> 09:03:10,718 print it like this and this is going to 11944 09:03:12,840 --> 09:03:18,477 squares and we're going to say i that's 11945 09:03:15,919 --> 09:03:20,159 going to be our index that we want to 11946 09:03:20,159 --> 09:03:25,840 and we're going to say stdendl 11947 09:03:23,520 --> 09:03:27,040 and uh because we don't want this to 11948 09:03:27,040 --> 09:03:32,718 we're going to comment that out 11949 09:03:29,439 --> 09:03:35,680 and we're going to go up and make sure 11950 09:03:32,718 --> 09:03:37,440 our comment is nice and neat 11951 09:03:35,680 --> 09:03:39,279 and we're going to try and run this and 11952 09:03:39,279 --> 09:03:44,239 well we don't have any real data in here 11953 09:03:42,317 --> 09:03:46,637 we're going to have some junk data so it 11954 09:03:44,240 --> 09:03:50,000 could really be anything we get printed 11955 09:03:46,637 --> 09:03:52,239 here so let's build with gcc and we're 11956 09:03:50,000 --> 09:03:54,398 going to give ourselves some breathing 11957 09:03:52,240 --> 09:03:57,360 room we're going to go down and clear 11958 09:03:54,398 --> 09:03:59,680 and run rooster and you see that um this 11959 09:03:57,360 --> 09:04:01,840 is really some junk data and if we run 11960 09:03:59,680 --> 09:04:03,040 it again let's see that we get the same 11961 09:04:03,040 --> 09:04:08,240 it is the same data in here if we want 11962 09:04:05,279 --> 09:04:10,477 to have in some reliable data we need to 11963 09:04:08,240 --> 09:04:12,080 write in our own data and we're going to 11964 09:04:10,477 --> 09:04:16,397 see how we can do that we're going to 11965 09:04:12,080 --> 09:04:19,280 see how we can write data into an array 11966 09:04:16,398 --> 09:04:20,798 and the syntax is really simple so the 11967 09:04:20,797 --> 09:04:26,317 you just say the index for which you 11968 09:04:22,637 --> 09:04:29,439 want to write data in and you say scores 11969 09:04:26,317 --> 09:04:31,840 and you specify the index let's say zero 11970 09:04:29,439 --> 09:04:33,759 and i'm going to put in a 20 for example 11971 09:04:36,240 --> 09:04:40,478 we're going to grab the index one the 11972 09:04:38,398 --> 09:04:42,798 first element and we're going to put in 11973 09:04:42,797 --> 09:04:47,520 and we're going to say scores we're 11974 09:04:44,878 --> 09:04:49,920 going to say 2 for index 2 and we're 11975 09:04:49,919 --> 09:04:55,679 why not and after we do this we can 11976 09:04:53,360 --> 09:04:57,920 print this out or read data with the 11977 09:04:55,680 --> 09:05:00,240 loop and make sure that the data we did 11978 09:05:02,000 --> 09:05:06,957 and comment this out because we don't 11979 09:05:04,000 --> 09:05:08,637 want this to confuse us on the console 11980 09:05:06,957 --> 09:05:12,159 and we're going to do that we're going 11981 09:05:08,637 --> 09:05:14,397 to come down here and print the data out 11982 09:05:12,159 --> 09:05:17,119 let's say what we're doing here and if 11983 09:05:14,398 --> 09:05:20,159 we build this we expect to see 11984 09:05:17,119 --> 09:05:23,439 that at index 0 1 and 2 we have the 11985 09:05:23,439 --> 09:05:27,919 let's build and see we're going to run 11986 09:05:27,919 --> 09:05:32,000 the build is going to go through we're 11987 09:05:29,360 --> 09:05:35,680 going to clear and run the rooster 11988 09:05:32,000 --> 09:05:38,878 and look at this score is a 0 20 score 11989 09:05:38,878 --> 09:05:43,279 and our data is actually getting gain 11990 09:05:41,520 --> 09:05:45,439 now we're going to see that we can also 11991 09:05:43,279 --> 09:05:48,477 write the data using the loop to make it 11992 09:05:45,439 --> 09:05:49,919 really easy without us manually doing 11993 09:05:49,919 --> 09:05:54,637 for that we're going to copy what we 11994 09:05:54,637 --> 09:05:57,599 and we're going to comment what we had 11995 09:05:57,599 --> 09:06:01,119 so let's comment this out and we're 11996 09:06:01,119 --> 09:06:06,239 and uh print the data again we're going 11997 09:06:03,520 --> 09:06:08,237 to put in the code here and before we 11998 09:06:06,240 --> 09:06:08,959 print the data we're going to put data 11999 09:06:08,959 --> 09:06:12,398 with the loop so the loop is basically 12000 09:06:11,122 --> 09:06:14,000 going to be the same thing we're going 12001 09:06:14,000 --> 09:06:18,639 ti we're going to initialize this we're 12002 09:06:16,398 --> 09:06:20,800 going to say i less than 10 because we 12003 09:06:18,639 --> 09:06:22,078 want to loop 10 times we're going to 12004 09:06:22,078 --> 09:06:26,078 and we're going to fall in the body of 12005 09:06:23,597 --> 09:06:28,000 the loop what we're going to do is write 12006 09:06:26,078 --> 09:06:30,319 data at the index of the current 12007 09:06:28,000 --> 09:06:32,480 iteration i should say so what we're 12008 09:06:30,319 --> 09:06:34,959 going to do we're going to say scores i 12009 09:06:32,480 --> 09:06:37,840 and we can really put in anything for 12010 09:06:34,959 --> 09:06:40,319 now let's say we want to put an i 12011 09:06:37,839 --> 09:06:43,597 multiplied by 10 so we're going to take 12012 09:06:40,319 --> 09:06:45,758 the current index multiply it by 10 and 12013 09:06:43,597 --> 09:06:47,839 we're going to store that in the index 12014 09:06:45,758 --> 09:06:49,122 at the current iteration this is what we 12015 09:06:49,122 --> 09:06:53,122 and if we print this out try to think 12016 09:06:51,122 --> 09:06:54,160 about what we're going to get here 12017 09:06:54,160 --> 09:06:59,040 at index 0 let's put in a 0 to be super 12018 09:06:56,877 --> 09:07:01,597 clear here that we are starting from 12019 09:06:59,040 --> 09:07:03,597 index 0. at index 0 we're going to take 12020 09:07:01,597 --> 09:07:07,121 0 and multiply that with the 10 so we're 12021 09:07:03,597 --> 09:07:08,877 going to store 0 at index 0. the loop is 12022 09:07:07,122 --> 09:07:10,960 going to run again at index 1 we're 12023 09:07:08,877 --> 09:07:13,277 going to take a 1 multiply with 10 we're 12024 09:07:10,959 --> 09:07:16,159 going to get it 10 at index 2 we're 12025 09:07:13,277 --> 09:07:19,040 going to multiply 2 with a 10 and store 12026 09:07:16,160 --> 09:07:21,438 a 20 at index 2 and we're going to keep 12027 09:07:21,438 --> 09:07:26,480 10 20 all the way to 90 stored in our 12028 09:07:24,639 --> 09:07:29,917 array here so if we print it out we 12029 09:07:26,480 --> 09:07:32,000 expect to see 0 10 20 all the way to 90. 12030 09:07:29,917 --> 09:07:34,877 let's weld and see that that's actually 12031 09:07:32,000 --> 09:07:37,199 what we have in our array we're going to 12032 09:07:34,877 --> 09:07:39,757 work successfully let's clear and run 12033 09:07:37,199 --> 09:07:42,557 rooster and you see that this is exactly 12034 09:07:39,758 --> 09:07:44,800 the same thing we have in our array how 12035 09:07:42,557 --> 09:07:47,437 cool is that okay another thing i want 12036 09:07:44,800 --> 09:07:49,680 you to see is that we can declare and 12037 09:07:49,680 --> 09:07:54,078 in place but before we do that let's say 12038 09:07:52,000 --> 09:07:56,078 what we were doing here we were trying 12039 09:07:54,078 --> 09:07:58,160 to write data in a loop and we're going 12040 09:07:56,078 --> 09:08:01,360 to comment this out because we don't 12041 09:07:58,160 --> 09:08:03,520 want a lot of confusing output 12042 09:08:01,360 --> 09:08:05,680 in our console and we're going to go 12043 09:08:03,519 --> 09:08:07,597 down and put in the data because i don't 12044 09:08:05,680 --> 09:08:09,840 want to type all these thanks 12045 09:08:07,597 --> 09:08:13,277 so we can declare and initialize an 12046 09:08:09,839 --> 09:08:15,757 array in place and the way we do that 12047 09:08:13,277 --> 09:08:17,680 the array is going to store doubles now 12048 09:08:15,758 --> 09:08:20,000 we can really declare an array of 12049 09:08:17,680 --> 09:08:22,319 anything but so far we have seen that we 12050 09:08:20,000 --> 09:08:23,917 can do that for ant now let's try a 12051 09:08:22,319 --> 09:08:26,319 little bit and show you that you can do 12052 09:08:23,917 --> 09:08:28,799 the same thing for double times so we 12053 09:08:26,319 --> 09:08:31,040 can declare an array of double types the 12054 09:08:28,800 --> 09:08:33,360 size is going to be five and we're going 12055 09:08:31,040 --> 09:08:35,277 to initialize that with this data here 12056 09:08:33,360 --> 09:08:41,758 so at index zero we're going to have a 12057 09:08:35,277 --> 09:08:43,121 12.7 index one a 7.5 index 2 a 13.2 12058 09:08:43,122 --> 09:08:49,520 an 8.1 and index 4 and 9.3 and that's 12059 09:08:47,597 --> 09:08:51,437 what we're going to have if we print 12060 09:08:49,519 --> 09:08:53,121 this out here we have a simple loop to 12061 09:08:51,438 --> 09:08:54,680 print it out and it's going to basically 12062 09:08:54,680 --> 09:09:01,360 12.7 and it's going to go all the way to 12063 09:08:57,360 --> 09:09:02,800 salary 4 and print a 9.3 and this is 12064 09:09:02,800 --> 09:09:07,840 if it makes sense for whatever you are 12065 09:09:04,877 --> 09:09:10,479 designing with c plus plus we're going 12066 09:09:10,480 --> 09:09:16,238 let's clear and run and you see that 12067 09:09:13,199 --> 09:09:18,639 this is exactly what we expect okay now 12068 09:09:16,237 --> 09:09:20,799 this is super clear the next thing we're 12069 09:09:18,639 --> 09:09:24,078 going to see is that if you don't 12070 09:09:20,800 --> 09:09:26,319 initialize all the elements in the array 12071 09:09:24,078 --> 09:09:28,558 those you leave out are going to be 12072 09:09:26,319 --> 09:09:31,438 initialized to zero and we are going to 12073 09:09:28,557 --> 09:09:33,360 put in a piece of code to play with that 12074 09:09:31,438 --> 09:09:35,520 so here we have an array it's a net 12075 09:09:33,360 --> 09:09:38,160 array it's going to be called families 12076 09:09:35,519 --> 09:09:41,359 and the size is going to be five but we 12077 09:09:38,160 --> 09:09:43,200 are only initializing three elements and 12078 09:09:41,360 --> 09:09:47,199 these elements are going to be elements 12079 09:09:43,199 --> 09:09:49,680 on the front so elements at index 3 and 12080 09:09:47,199 --> 09:09:52,479 4 are going to be left out and they are 12081 09:09:49,680 --> 09:09:54,800 going to be initialized to 0 by the 12082 09:09:52,480 --> 09:09:58,960 compiler so if we print this out we 12083 09:09:54,800 --> 09:10:01,520 expect to get 12 7 5 and 0 0 at 12084 09:09:58,959 --> 09:10:03,040 different indexes in this array here 12085 09:10:04,959 --> 09:10:08,799 we're going to clear because the build 12086 09:10:11,040 --> 09:10:16,398 this is exactly what we expect we have a 12087 09:10:13,040 --> 09:10:18,959 12 a 7 and 5 and the last two elements 12088 09:10:16,398 --> 09:10:21,438 are initialized to zero and this may 12089 09:10:18,959 --> 09:10:23,438 come in handy sometimes okay let's 12090 09:10:21,438 --> 09:10:26,238 comment this out another thing we want 12091 09:10:23,438 --> 09:10:29,277 to see is that we can actually 12092 09:10:26,237 --> 09:10:30,319 omit the size when we are declaring our 12093 09:10:30,319 --> 09:10:35,277 and initializing it so here is a simple 12094 09:10:33,040 --> 09:10:38,398 example we have an inter array called 12095 09:10:35,277 --> 09:10:41,360 class sizes we don't specify the size 12096 09:10:38,398 --> 09:10:44,000 but we initialize it and the compiler is 12097 09:10:41,360 --> 09:10:46,718 going to deduce the size of this array 12098 09:10:44,000 --> 09:10:49,122 from the elements we put in here so if 12099 09:10:46,718 --> 09:10:51,520 we put in two elements so it's going to 12100 09:10:49,122 --> 09:10:54,238 be of size 2 if we put in 10 elements 12101 09:10:51,519 --> 09:10:56,959 it's going to be of size 10 and this is 12102 09:10:54,237 --> 09:10:59,199 how this works but if you do something 12103 09:10:56,959 --> 09:11:02,398 like this and not to initialize the 12104 09:10:59,199 --> 09:11:03,917 array let's take this out for example 12105 09:11:02,398 --> 09:11:06,319 you're going to get a compiler error 12106 09:11:03,917 --> 09:11:08,319 because the compiler now doesn't know 12107 09:11:06,319 --> 09:11:10,959 which size it's going to give to this 12108 09:11:12,237 --> 09:11:17,360 put in the size or initialize your array 12109 09:11:15,122 --> 09:11:19,278 like this and it is going to work in 12110 09:11:17,360 --> 09:11:21,758 this case we're going to have six 12111 09:11:19,277 --> 09:11:23,917 elements and and you see that a range 12112 09:11:21,758 --> 09:11:26,319 based for loop is going to come in handy 12113 09:11:23,917 --> 09:11:28,557 here because we don't really know the 12114 09:11:26,319 --> 09:11:31,597 size of this array if somebody comes and 12115 09:11:28,557 --> 09:11:33,757 adds another element let's say 23 12116 09:11:31,597 --> 09:11:36,557 the size is going to change and if we 12117 09:11:33,758 --> 09:11:38,959 hard to code the size in our full loop 12118 09:11:36,557 --> 09:11:41,680 the code is going to break but a range 12119 09:11:38,959 --> 09:11:43,597 based for loop is going to use whatever 12120 09:11:41,680 --> 09:11:45,360 size this already has so we're going to 12121 09:11:43,597 --> 09:11:47,917 try and build this we're going to build 12122 09:11:47,917 --> 09:11:51,839 and the world is going to go through if 12123 09:11:49,680 --> 09:11:56,160 we run rooster we're going to get the 12124 09:11:51,839 --> 09:11:58,000 elements we have 10 12 15 11 18 17 23 if 12125 09:11:56,160 --> 09:11:58,960 we add a new element this is going to 12126 09:11:58,959 --> 09:12:02,319 you see that we don't need to modify the 12127 09:12:02,319 --> 09:12:06,959 put in the new size and this is really 12128 09:12:04,639 --> 09:12:09,040 cool i like this about range based for 12129 09:12:06,959 --> 09:12:11,040 loops we're going to build again 12130 09:12:11,040 --> 09:12:15,122 run the rooster and this is going to 12131 09:12:12,557 --> 09:12:17,519 give us exactly the data we have in the 12132 09:12:15,122 --> 09:12:19,840 array so this is one way you can do 12133 09:12:17,519 --> 09:12:22,557 things another thing we saw is that you 12134 09:12:22,557 --> 09:12:27,360 so let's do an example here for example 12135 09:12:25,040 --> 09:12:30,319 we're going to grab this thing and uh 12136 09:12:27,360 --> 09:12:32,718 put an array declared down here we're 12137 09:12:30,319 --> 09:12:34,800 going to copy that and use that here and 12138 09:12:32,718 --> 09:12:37,520 we're going to make this cost 12139 09:12:34,800 --> 09:12:39,680 let's give it a name let's say 12140 09:12:37,519 --> 09:12:40,557 birds for example for example we want to 12141 09:12:42,398 --> 09:12:46,639 lines of birds or whatever this is just 12142 09:12:44,959 --> 09:12:49,438 an example to show you that you can't 12143 09:12:46,639 --> 09:12:51,839 modify this array so if you try to do 12144 09:12:51,839 --> 09:12:57,519 and index 2 for example and you want to 12145 09:12:57,519 --> 09:13:01,757 the compiler is going to complain you 12146 09:12:59,758 --> 09:13:03,438 see that visual studio code is 12147 09:13:03,438 --> 09:13:09,360 expiration must be a modifiable value so 12148 09:13:06,160 --> 09:13:12,398 it is saying that the array stores const 12149 09:13:09,360 --> 09:13:14,877 elements so you can't modify elements of 12150 09:13:12,398 --> 09:13:16,800 this array okay so this is something you 12151 09:13:14,877 --> 09:13:20,397 can do if you don't want people to be 12152 09:13:16,800 --> 09:13:22,718 able to modify data in your array and 12153 09:13:20,398 --> 09:13:26,078 the last thing i'm going to show you is 12154 09:13:22,718 --> 09:13:27,597 that you can do operations on the data 12155 09:13:27,597 --> 09:13:31,917 so let's bring up our scores array again 12156 09:13:30,237 --> 09:13:34,077 so we're going to go up and copy it 12157 09:13:31,917 --> 09:13:37,519 because the code is using there so let's 12158 09:13:34,078 --> 09:13:39,520 put in our own scores array 12159 09:13:37,519 --> 09:13:41,199 it's going to be pretty fast and we're 12160 09:13:39,519 --> 09:13:42,717 going to say that it is an array with 12161 09:13:41,199 --> 09:13:44,799 this angle brackets we're going to 12162 09:13:42,718 --> 09:13:47,520 initialize this let's put in a bunch of 12163 09:13:44,800 --> 09:13:49,520 values and what we can do is sum these 12164 09:13:47,519 --> 09:13:52,077 things up we're going to store our 12165 09:13:49,519 --> 09:13:54,479 results in the sum variable and we're 12166 09:13:52,078 --> 09:13:56,800 going to loop around with a ranged based 12167 09:13:54,480 --> 09:13:58,718 for loop because we don't really know 12168 09:13:56,800 --> 09:14:02,238 the size of this array it can change any 12169 09:13:58,718 --> 09:14:04,557 time with however many elements and a 12170 09:14:02,237 --> 09:14:06,398 range based for loop is really cool in 12171 09:14:04,557 --> 09:14:07,917 that we don't have to keep track of the 12172 09:14:07,917 --> 09:14:12,639 so we're going to go in here 12173 09:14:14,718 --> 09:14:18,800 so for example for the first time sum is 12174 09:14:17,122 --> 09:14:20,639 going to be 0 because that's what we 12175 09:14:18,800 --> 09:14:23,598 initialize it with we're going to get 12176 09:14:20,639 --> 09:14:25,519 into this loop here the first element is 12177 09:14:23,597 --> 09:14:28,479 going to be 2 we're going to add that to 12178 09:14:25,519 --> 09:14:29,917 0 and get 2 we're going to add a 5 we're 12179 09:14:28,480 --> 09:14:33,122 going to get a 7 we're going to keep 12180 09:14:29,917 --> 09:14:35,757 adding this element and at the end sum 12181 09:14:33,122 --> 09:14:37,918 is going to contain the sum of all these 12182 09:14:35,758 --> 09:14:40,398 elements in the array here and this is 12183 09:14:37,917 --> 09:14:43,040 one cool way you can sum up the elements 12184 09:14:40,398 --> 09:14:44,800 in your array so if we run this we're 12185 09:14:43,040 --> 09:14:46,959 going to get the sun printed out let's 12186 09:14:44,800 --> 09:14:50,000 do that pretty quick we're going to run 12187 09:14:46,959 --> 09:14:52,319 this has to build with gcc and we have 12188 09:14:52,319 --> 09:14:58,237 what is happening here it's going to say 12189 09:14:54,959 --> 09:15:00,639 redeclaration of scores where did we 12190 09:14:58,237 --> 09:15:03,121 declare scores before let's go up and 12191 09:15:00,639 --> 09:15:05,040 make sure it is not declared somewhere 12192 09:15:05,040 --> 09:15:09,597 so we want to comment this out 12193 09:15:07,438 --> 09:15:10,319 come back to our code so that we can see 12194 09:15:10,319 --> 09:15:16,078 and we're going to build again with gcc 12195 09:15:14,160 --> 09:15:17,278 and now the world is going through we're 12196 09:15:20,160 --> 09:15:26,640 sum is 37 and if you sum these things up 12197 09:15:23,758 --> 09:15:28,398 i'm sure you get 37. this is really all 12198 09:15:26,639 --> 09:15:30,398 we set out to do in this lecture i hope 12199 09:15:28,398 --> 09:15:33,680 you have a better idea of how you can 12200 09:15:30,398 --> 09:15:36,319 declare your arrays put data in read 12201 09:15:33,680 --> 09:15:38,800 data out of those arrays make them const 12202 09:15:36,319 --> 09:15:40,718 or even do all kinds of operations on 12203 09:15:38,800 --> 09:15:42,718 them and this was really what this 12204 09:15:42,718 --> 09:15:46,000 we are going to stop here in this 12205 09:15:44,160 --> 09:15:48,398 lecture in the next one we're going to 12206 09:15:46,000 --> 09:15:50,639 see a cool way we can use to get the 12207 09:15:52,480 --> 09:15:56,718 and meet me there in this lecture we're 12208 09:15:54,718 --> 09:15:58,398 going to see a way we can query for the 12209 09:15:58,398 --> 09:16:02,800 at runtime if you remember in the last 12210 09:16:00,718 --> 09:16:05,277 lecture we had an example like this 12211 09:16:02,800 --> 09:16:08,000 showing how we can declare an array and 12212 09:16:05,277 --> 09:16:08,799 initialize it in a single statement like 12213 09:16:08,800 --> 09:16:14,800 but the problem we had with this is that 12214 09:16:11,680 --> 09:16:17,040 if we tried to print the array using a 12215 09:16:14,800 --> 09:16:19,680 for loop for example we would have to 12216 09:16:17,040 --> 09:16:22,000 use the size and the size of this array 12217 09:16:19,680 --> 09:16:24,078 may change if somebody decides to add 12218 09:16:22,000 --> 09:16:26,398 elements or remove elements then the 12219 09:16:24,078 --> 09:16:29,200 size is going to be irrelevant c plus 12220 09:16:26,398 --> 09:16:31,917 plus provides a way we can query for the 12221 09:16:29,199 --> 09:16:34,479 size of an array at runtime and we're 12222 09:16:31,917 --> 09:16:36,877 going to get the real size accounting 12223 09:16:34,480 --> 09:16:39,360 for the current number of elements that 12224 09:16:36,877 --> 09:16:42,319 we have in the array and for that we use 12225 09:16:42,319 --> 09:16:46,237 this is a function that comes with the c 12226 09:16:44,557 --> 09:16:48,479 plus plus standard library so you're 12227 09:16:46,237 --> 09:16:50,799 going to have it already if you have a 12228 09:16:48,480 --> 09:16:54,000 compliant compiler if you see here you 12229 09:16:50,800 --> 09:16:56,000 see that it was introduced in c plus 17 12230 09:16:54,000 --> 09:16:58,319 and it is a cool addition we can use it 12231 09:16:56,000 --> 09:16:59,839 to make our code easier to work with if 12232 09:16:58,319 --> 09:17:02,000 you look here we have a simple loop 12233 09:16:59,839 --> 09:17:04,319 which is going to print data out it is 12234 09:17:02,000 --> 09:17:06,877 going to loop starting from zero all the 12235 09:17:04,319 --> 09:17:09,597 way to the size of the array you see 12236 09:17:06,877 --> 09:17:12,000 that we use this to control when this 12237 09:17:09,597 --> 09:17:14,078 loop is going to end we then put in our 12238 09:17:12,000 --> 09:17:16,237 incrementation part and then we jump 12239 09:17:14,078 --> 09:17:18,319 into the body of the loop and print the 12240 09:17:16,237 --> 09:17:19,680 current element in the loop and if we do 12241 09:17:18,319 --> 09:17:22,237 things like this we're going to be able 12242 09:17:19,680 --> 09:17:24,959 to print all elements in the array 12243 09:17:22,237 --> 09:17:27,680 regardless of the number we initialize 12244 09:17:24,959 --> 09:17:30,557 it with okay so this is one way and it 12245 09:17:27,680 --> 09:17:32,718 was introduced in c plus plus 17. but 12246 09:17:32,718 --> 09:17:36,398 how did we do things before z plus plus 12247 09:17:36,398 --> 09:17:42,160 well we had to kind of do a hack to get 12248 09:17:39,597 --> 09:17:44,718 something like this we use the size of 12249 09:17:42,160 --> 09:17:45,680 operator we have seen a lot of times 12250 09:17:47,519 --> 09:17:52,959 if you do size of and passing the array 12251 09:17:50,959 --> 09:17:56,237 you're going to get the entire size of 12252 09:17:52,959 --> 09:17:58,959 the array so if we take that and divide 12253 09:17:56,237 --> 09:18:00,959 that with the size of a single element 12254 09:17:58,959 --> 09:18:03,199 in the array we're going to get the 12255 09:18:00,959 --> 09:18:05,277 number of elements of the array and this 12256 09:18:03,199 --> 09:18:07,680 is really what we are doing here we are 12257 09:18:05,277 --> 09:18:09,917 setting up a count variable and we are 12258 09:18:07,680 --> 09:18:12,480 taking the size of the array and 12259 09:18:09,917 --> 09:18:14,799 dividing that with the size of a single 12260 09:18:12,480 --> 09:18:17,040 element in the array and we are just 12261 09:18:14,800 --> 09:18:19,598 grabbing the first element because you 12262 09:18:17,040 --> 09:18:22,078 can really use any element here and once 12263 09:18:19,597 --> 09:18:24,078 we have this we can use this count to do 12264 09:18:22,078 --> 09:18:25,122 a loop like this and it is working 12265 09:18:25,122 --> 09:18:29,360 please remember that you don't have to 12266 09:18:27,597 --> 09:18:32,479 go through this craziness we're doing 12267 09:18:29,360 --> 09:18:35,520 here you don't even have to use std size 12268 09:18:32,480 --> 09:18:37,840 because you can use a ranged base for 12269 09:18:35,519 --> 09:18:39,437 loop and get access to the elements you 12270 09:18:37,839 --> 09:18:41,597 have in the array and it is going to 12271 09:18:39,438 --> 09:18:44,000 work pretty well now that you have an 12272 09:18:41,597 --> 09:18:46,718 idea about this let's head to visual 12273 09:18:44,000 --> 09:18:49,680 studio code and actually play with us 12274 09:18:46,718 --> 09:18:52,000 okay so here we are in our working 12275 09:18:49,680 --> 09:18:54,557 folder we are going to be working on 12276 09:18:52,000 --> 09:18:56,557 this project size of an array we're 12277 09:18:54,557 --> 09:18:59,121 going to grab our template project we're 12278 09:18:56,557 --> 09:19:00,237 going to copy the files and put them in 12279 09:19:00,237 --> 09:19:03,917 and we're going to open this up in 12280 09:19:01,758 --> 09:19:05,840 visual studio code let's do that it's 12281 09:19:03,917 --> 09:19:08,078 going to open up in visual studio code 12282 09:19:08,078 --> 09:19:12,800 clean it up a little bit and we are 12283 09:19:10,237 --> 09:19:14,479 going to declare an array if we want to 12284 09:19:14,480 --> 09:19:19,360 we need to know how many times we're 12285 09:19:16,639 --> 09:19:21,595 going to loop and print elements in here 12286 09:19:19,360 --> 09:19:24,319 so one thing you might want to do is to 12287 09:19:21,595 --> 09:19:26,639 say okay we have 10 elements in here so 12288 09:19:24,319 --> 09:19:28,720 i'm going to loop for 10 times you might 12289 09:19:26,639 --> 09:19:30,479 think this is going to work and it is 12290 09:19:32,400 --> 09:19:36,959 and build this with gcc like we usually 12291 09:19:39,595 --> 09:19:44,239 and run rooster we are going to get our 12292 09:19:42,160 --> 09:19:46,160 numbers this is what we have in the 12293 09:19:46,160 --> 09:19:51,200 but what if somebody comes and changes 12294 09:19:49,360 --> 09:19:54,080 the elements we have in here let's add 12295 09:19:54,959 --> 09:19:59,519 the moment we change this we will need 12296 09:19:57,040 --> 09:20:01,840 to come and change the size in here and 12297 09:19:59,519 --> 09:20:04,719 this loop here might be buried somewhere 12298 09:20:01,839 --> 09:20:08,319 deep in your project and it might not be 12299 09:20:04,720 --> 09:20:10,880 as easy as it is now to see that we also 12300 09:20:08,319 --> 09:20:14,400 need to change this this may be a really 12301 09:20:10,879 --> 09:20:17,680 big source of hard to debug problems so 12302 09:20:14,400 --> 09:20:21,275 what we really need is a way to get the 12303 09:20:17,680 --> 09:20:23,756 size of the array at runtime and be able 12304 09:20:21,275 --> 09:20:26,079 to dynamically query for that size and 12305 09:20:23,756 --> 09:20:30,080 use it in our program and that's exactly 12306 09:20:26,080 --> 09:20:32,080 what std size provides so we're going to 12307 09:20:32,080 --> 09:20:36,400 and we're going to put in a value let's 12308 09:20:34,400 --> 09:20:38,800 use a braced initializer so we're going 12309 09:20:38,800 --> 09:20:42,639 and we're going to pass the array we 12310 09:20:42,639 --> 09:20:46,000 and if we do this we're going to get 12311 09:20:46,000 --> 09:20:50,319 and we're going to be looping for this 12312 09:20:48,319 --> 09:20:53,839 number of times so we're going to test 12313 09:20:50,319 --> 09:20:56,159 for i is less than the count here 12314 09:20:53,839 --> 09:20:58,000 and now it doesn't matter how many 12315 09:20:56,160 --> 09:21:00,480 elements we have in here we're going to 12316 09:20:58,000 --> 09:21:03,120 get them at run time we need to only 12317 09:21:00,480 --> 09:21:04,880 update this initializer here so what we 12318 09:21:09,756 --> 09:21:14,160 and run rooster and we're going to get 12319 09:21:12,400 --> 09:21:17,275 the numbers here you see that we get all 12320 09:21:14,160 --> 09:21:19,596 the elements if we take out a few of 12321 09:21:17,275 --> 09:21:22,079 these elements let's take out a few of 12322 09:21:19,595 --> 09:21:22,959 these let's leave it up to seven or 12323 09:21:22,959 --> 09:21:28,000 let's build again and we are going to 12324 09:21:25,595 --> 09:21:30,399 clear and run rooster we're going to get 12325 09:21:28,000 --> 09:21:33,199 however many elements we have in there 12326 09:21:30,400 --> 09:21:35,840 and this is pretty cool again you might 12327 09:21:33,199 --> 09:21:39,915 ask how did we do things before c plus 12328 09:21:35,839 --> 09:21:42,959 plus 17 because std size was introduced 12329 09:21:39,915 --> 09:21:46,239 in c plus plus 17 let's say that here 12330 09:21:42,959 --> 09:21:49,756 before that we had to get the size of 12331 09:21:46,239 --> 09:21:52,479 the entire array and divide that with 12332 09:21:49,756 --> 09:21:55,120 the size of each element in the array 12333 09:21:52,480 --> 09:21:58,400 and we could use the size of operator to 12334 09:21:55,120 --> 09:22:00,959 do that so before we do any of that we 12335 09:21:58,400 --> 09:22:02,800 want to play with the size of method to 12336 09:22:02,800 --> 09:22:08,480 so what we're going to do is say sdd the 12337 09:22:08,480 --> 09:22:13,120 scores the entire array and we're going 12338 09:22:11,120 --> 09:22:15,436 to print that and we're going to say 12339 09:22:17,199 --> 09:22:24,479 you know one element let's say 12340 09:22:20,839 --> 09:22:27,199 scores and use the first element this is 12341 09:22:24,480 --> 09:22:29,040 the easiest thing we can find here but 12342 09:22:27,199 --> 09:22:31,360 you can use any element so we're going 12343 09:22:29,040 --> 09:22:34,559 to say size of here and we're going to 12344 09:22:31,360 --> 09:22:36,879 say scores and grab the first element 12345 09:22:34,559 --> 09:22:39,360 which is at index 0 and we're going to 12346 09:22:36,879 --> 09:22:40,879 print its size if we build and run this 12347 09:22:43,519 --> 09:22:48,559 and the world is good we can clear now 12348 09:22:46,400 --> 09:22:51,596 and run rooster you're going to see that 12349 09:22:48,559 --> 09:22:54,639 we're going to get size of scores is 32 12350 09:22:51,595 --> 09:22:56,559 the entire array the size of one element 12351 09:22:54,639 --> 09:23:00,080 is four because we are storing in 12352 09:22:56,559 --> 09:23:03,119 integers and i want you to see that 32 12353 09:23:00,080 --> 09:23:05,436 is four times eight because we have an 12354 09:23:03,120 --> 09:23:08,319 eight elements so it's going to give you 12355 09:23:05,436 --> 09:23:11,200 the combined size of all of the elements 12356 09:23:08,319 --> 09:23:12,559 that we have in here if you do size of 12357 09:23:12,559 --> 09:23:16,639 but if you do size of a single element 12358 09:23:14,879 --> 09:23:19,199 you're going to get the size of a single 12359 09:23:16,639 --> 09:23:21,595 element so c plus plus developers were 12360 09:23:21,595 --> 09:23:25,595 and see that we could use this 12361 09:23:23,756 --> 09:23:28,239 information and get the count of 12362 09:23:25,595 --> 09:23:30,239 elements inside so we can do something 12363 09:23:30,239 --> 09:23:33,756 and we're going to assign a value to 12364 09:23:33,756 --> 09:23:37,840 in our braced initializer so we're going 12365 09:23:41,040 --> 09:23:46,720 and we're going to divide that with size 12366 09:23:43,756 --> 09:23:49,120 of and i'm going to grab scores we can 12367 09:23:46,720 --> 09:23:51,680 really use any element in the array but 12368 09:23:49,120 --> 09:23:53,360 we're going to grab the one at index 0 12369 09:23:51,680 --> 09:23:55,840 and now we're going to get the count 12370 09:23:53,360 --> 09:23:59,520 stored in our account variable without 12371 09:23:55,839 --> 09:24:00,879 using std size and we can uncomment our 12372 09:24:00,879 --> 09:24:05,360 or the code to print the array and if we 12373 09:24:03,519 --> 09:24:07,680 run now it is going to work exactly the 12374 09:24:05,360 --> 09:24:10,319 same thing regardless of the amount of 12375 09:24:07,680 --> 09:24:13,915 elements you have in here let's put a 12376 09:24:10,319 --> 09:24:16,639 few numbers randomly and if we will then 12377 09:24:13,915 --> 09:24:18,719 run we are going to clear and run 12378 09:24:16,639 --> 09:24:20,639 rooster and we're going to get all the 12379 09:24:20,639 --> 09:24:24,720 you can really use any of these 12380 09:24:22,480 --> 09:24:27,840 techniques just choose what you feel 12381 09:24:24,720 --> 09:24:30,080 works better for you okay we are going 12382 09:24:27,839 --> 09:24:32,239 through all this trouble to set up an 12383 09:24:30,080 --> 09:24:34,160 iterator we're going to do the test 12384 09:24:32,239 --> 09:24:37,595 we're going to increment this is really 12385 09:24:34,160 --> 09:24:40,320 too much work to try and get data from a 12386 09:24:37,595 --> 09:24:43,519 collection like an array so what we can 12387 09:24:40,319 --> 09:24:44,800 do instead is comment out all this hard 12388 09:24:44,800 --> 09:24:49,200 and we can use a ranged based for loop 12389 09:24:47,436 --> 09:24:52,239 as we have seen before and it is 12390 09:24:49,199 --> 09:24:55,199 ridiculously easy and convenient to work 12391 09:24:52,239 --> 09:24:57,436 with so what you can do is say for 12392 09:24:57,436 --> 09:25:02,160 type deduction on whatever it is we have 12393 09:24:59,756 --> 09:25:04,400 stored in our array so we're going to 12394 09:25:04,400 --> 09:25:10,720 and we're going to print out the element 12395 09:25:07,120 --> 09:25:14,000 so let's do that we're going to print 12396 09:25:10,720 --> 09:25:16,480 stdcl and here we're going to say value 12397 09:25:14,000 --> 09:25:17,756 let's say value in the array and we're 12398 09:25:17,756 --> 09:25:22,559 i and this is going to print all the 12399 09:25:20,000 --> 09:25:25,120 elements and we can work with the values 12400 09:25:22,559 --> 09:25:27,519 inside if we want that but you see that 12401 09:25:27,519 --> 09:25:32,319 on the indexes so if you really need the 12402 09:25:29,839 --> 09:25:34,799 indexes you will need to go through 12403 09:25:32,319 --> 09:25:38,159 something like this and if you just need 12404 09:25:34,800 --> 09:25:40,080 the values in the array ranged based for 12405 09:25:38,160 --> 09:25:42,320 loops are going to be really cool let's 12406 09:25:40,080 --> 09:25:44,319 say range base for loop and if we boiled 12407 09:25:42,319 --> 09:25:45,199 and run this it's going to work pretty 12408 09:25:45,199 --> 09:25:50,479 so the world is going to go through 12409 09:25:47,839 --> 09:25:53,119 let's bring this up so that we have some 12410 09:25:50,480 --> 09:25:55,120 breathing room we're going to clear 12411 09:25:55,120 --> 09:25:58,720 and we're going to get our elements 12412 09:25:56,800 --> 09:26:01,120 printed out here and you see that it is 12413 09:25:58,720 --> 09:26:01,916 the exact same thing we have in scores 12414 09:26:01,915 --> 09:26:05,839 so this is really all we set out to do 12415 09:26:03,839 --> 09:26:08,799 in this lecture i just wanted you to be 12416 09:26:08,800 --> 09:26:13,360 facility that we have in the c plus plus 12417 09:26:11,275 --> 09:26:16,000 standard library if you don't want to 12418 09:26:13,360 --> 09:26:18,559 use it for any reason you can also go in 12419 09:26:16,000 --> 09:26:21,839 and do the division we did here to get 12420 09:26:18,559 --> 09:26:24,799 the count of elements in the array 12421 09:26:21,839 --> 09:26:27,119 and uh do your best to try and use 12422 09:26:24,800 --> 09:26:30,160 ranged basis for loops because they make 12423 09:26:27,120 --> 09:26:32,800 it easier and cleaner to get data from a 12424 09:26:30,160 --> 09:26:35,520 collection like we are doing here with 12425 09:26:32,800 --> 09:26:37,120 an array we are going to stop here in 12426 09:26:35,519 --> 09:26:39,519 this lecture and the next one we're 12427 09:26:37,120 --> 09:26:41,276 going to see how we can work with arrays 12428 09:26:41,275 --> 09:26:45,119 go ahead and finish up here and meet me 12429 09:26:45,120 --> 09:26:49,520 in this lecture we're going to work on 12430 09:26:47,120 --> 09:26:50,480 array of characters and show how special 12431 09:26:50,480 --> 09:26:55,276 you can declare an array of characters 12432 09:26:53,120 --> 09:26:56,400 just like any other array so here we 12433 09:26:56,400 --> 09:27:01,360 an array called message it stores 12434 09:26:58,639 --> 09:27:04,239 elements of type character the size is 12435 09:27:01,360 --> 09:27:05,520 five and we initialize it with a bunch 12436 09:27:05,519 --> 09:27:10,639 if we want to print it out we can look 12437 09:27:07,756 --> 09:27:12,480 like we loop through any other array in 12438 09:27:10,639 --> 09:27:15,519 this case we're going to be using a 12439 09:27:12,480 --> 09:27:17,276 ranged base for loop we say for auto c 12440 09:27:15,519 --> 09:27:19,595 we pass the message and then we're going 12441 09:27:17,275 --> 09:27:21,680 to be printing each character in the 12442 09:27:19,595 --> 09:27:24,000 message and if we do this we're going to 12443 09:27:21,680 --> 09:27:27,040 see hello printed out on the console we 12444 09:27:24,000 --> 09:27:29,199 can also modify elements in our array 12445 09:27:27,040 --> 09:27:31,595 for example we can say message1 and 12446 09:27:31,595 --> 09:27:36,000 and if we print out we should see hello 12447 09:27:33,839 --> 09:27:38,879 printed out on the console nothing 12448 09:27:36,000 --> 09:27:41,595 really special so far the cool thing you 12449 09:27:38,879 --> 09:27:43,275 can do with character arrays 12450 09:27:41,595 --> 09:27:45,595 is that you can print them to the 12451 09:27:43,275 --> 09:27:47,595 console directly without even doing the 12452 09:27:45,595 --> 09:27:50,159 loop you can do something like this you 12453 09:27:47,595 --> 09:27:52,239 can do sddc out you say whatever you 12454 09:27:50,160 --> 09:27:54,640 want to do in quotes here and you're 12455 09:27:52,239 --> 09:27:57,595 going to print the message but sometimes 12456 09:27:54,639 --> 09:28:01,275 this one to do what you expect because 12457 09:27:57,595 --> 09:28:04,559 only proper c strings can be printed on 12458 09:28:01,275 --> 09:28:07,839 the console and what makes up a proper c 12459 09:28:04,559 --> 09:28:10,479 string a proper c string has this thing 12460 09:28:07,839 --> 09:28:11,435 you see on the end here this backslash 12461 09:28:11,436 --> 09:28:16,480 is called a null termination string and 12462 09:28:14,400 --> 09:28:19,040 it is what is going to tell c plus plus 12463 09:28:16,480 --> 09:28:21,276 that we have hit the end of the string 12464 09:28:19,040 --> 09:28:24,239 and we are going to stop printing it if 12465 09:28:21,275 --> 09:28:26,799 we are printing with things like stdcl 12466 09:28:24,239 --> 09:28:29,360 so this is really important to be in 12467 09:28:26,800 --> 09:28:31,520 your strength if it's not there it may 12468 09:28:29,360 --> 09:28:34,239 work but sometimes it's also going to 12469 09:28:31,519 --> 09:28:37,595 print some garbage characters after this 12470 09:28:34,239 --> 09:28:39,839 because what tells sddc out that we have 12471 09:28:37,595 --> 09:28:42,319 reached the end of the string is this 12472 09:28:39,839 --> 09:28:44,239 backslash zero here if it's not there 12473 09:28:42,319 --> 09:28:46,480 it's going to keep printing until it 12474 09:28:44,239 --> 09:28:48,720 sees one and then you're going to 12475 09:28:46,480 --> 09:28:51,120 probably have some garbage characters 12476 09:28:48,720 --> 09:28:53,040 added to your string and that's not what 12477 09:28:51,120 --> 09:28:56,080 you want so to have your strengths 12478 09:28:53,040 --> 09:28:58,720 printed out correctly be sure to have in 12479 09:28:56,080 --> 09:29:00,400 this null termination character here and 12480 09:28:58,720 --> 09:29:02,000 if we print it out we're going to get it 12481 09:29:00,400 --> 09:29:05,120 and we're going to see the size printed 12482 09:29:02,000 --> 09:29:07,839 out properly if we do things like this 12483 09:29:05,120 --> 09:29:10,000 but it is possible to get to this auto 12484 09:29:07,839 --> 09:29:12,959 field for us if you look here we have a 12485 09:29:10,000 --> 09:29:15,680 character array called message2 inside 12486 09:29:12,959 --> 09:29:18,400 we have said that we need six elements 12487 09:29:15,680 --> 09:29:20,879 and we say hello so we have one two 12488 09:29:18,400 --> 09:29:24,000 three four five elements and the 12489 09:29:20,879 --> 09:29:26,639 elements we don't initialize properly is 12490 09:29:24,000 --> 09:29:28,959 going to be initialized with a zero 12491 09:29:26,639 --> 09:29:31,756 version of a character and that happens 12492 09:29:28,959 --> 09:29:34,319 to be the null terminator of the null 12493 09:29:31,756 --> 09:29:36,000 termination character so this is going 12494 09:29:34,319 --> 09:29:37,595 to print correctly and we're going to 12495 09:29:37,595 --> 09:29:42,959 backslash n appended to this for us by 12496 09:29:40,720 --> 09:29:45,756 the compiler but be sure you understand 12497 09:29:42,959 --> 09:29:48,239 the difference between this and this 12498 09:29:45,756 --> 09:29:50,959 here we have specified the size to be a 12499 09:29:48,239 --> 09:29:53,360 5 and we are putting in five elements so 12500 09:29:50,959 --> 09:29:55,595 we have no null character appended to 12501 09:29:53,360 --> 09:29:58,400 this string but here we have said that 12502 09:29:55,595 --> 09:30:01,680 we can put in six elements and we have 12503 09:29:58,400 --> 09:30:05,120 only put five and the compiler is going 12504 09:30:01,680 --> 09:30:07,199 to autofill then the null terminating 12505 09:30:05,120 --> 09:30:08,560 character these two things are different 12506 09:30:07,199 --> 09:30:11,039 and make sure you understand the 12507 09:30:08,559 --> 09:30:13,435 difference okay so now take a look at 12508 09:30:11,040 --> 09:30:15,915 what we have here we have a strand we 12509 09:30:13,436 --> 09:30:18,639 didn't specify the size and we just 12510 09:30:15,915 --> 09:30:20,639 initialize it with hello someone might 12511 09:30:18,639 --> 09:30:23,040 think ah this is also going to autofill 12512 09:30:20,639 --> 09:30:25,756 but it's not because the compiler is 12513 09:30:23,040 --> 09:30:28,239 going to deduce the size of this array 12514 09:30:25,756 --> 09:30:30,559 using what you initialize it with and 12515 09:30:28,239 --> 09:30:32,079 you didn't put in a null character so 12516 09:30:30,559 --> 09:30:34,239 it's not going to be accounted for 12517 09:30:32,080 --> 09:30:36,480 you're only going to have this here and 12518 09:30:34,239 --> 09:30:38,559 if you print this out you may get 12519 09:30:36,480 --> 09:30:40,800 garbage characters appended to your 12520 09:30:38,559 --> 09:30:44,000 string here so be careful but it is 12521 09:30:40,800 --> 09:30:47,040 possible to do literal c strings and 12522 09:30:44,000 --> 09:30:49,275 only say hello like this in quotes and 12523 09:30:47,040 --> 09:30:52,480 you're going to get the null terminating 12524 09:30:49,275 --> 09:30:54,319 character appended to your string here 12525 09:30:52,480 --> 09:30:56,720 so if we print this this is going to 12526 09:30:54,319 --> 09:30:58,959 pretty nicely and this is the 12527 09:30:56,720 --> 09:31:01,680 recommended way to initialize your 12528 09:30:58,959 --> 09:31:04,879 character arrays if you want to put in 12529 09:31:01,680 --> 09:31:06,959 string data like this you have to use 12530 09:31:04,879 --> 09:31:09,360 double quotes and the cool thing about 12531 09:31:06,959 --> 09:31:11,519 this is that you can even put in 12532 09:31:09,360 --> 09:31:13,915 spaces between words like this it is 12533 09:31:11,519 --> 09:31:16,639 going to work this really looks like how 12534 09:31:13,915 --> 09:31:19,595 you would like to type text and store 12535 09:31:16,639 --> 09:31:21,360 that in your character arrays and we're 12536 09:31:19,595 --> 09:31:23,119 going to be using this a lot in the 12537 09:31:21,360 --> 09:31:25,520 course again i want to bring to your 12538 09:31:23,120 --> 09:31:26,639 attention that character arrays are 12539 09:31:26,639 --> 09:31:30,479 in that you can directly print them on 12540 09:31:30,480 --> 09:31:35,436 like we are doing here you see this is a 12541 09:31:32,239 --> 09:31:38,079 character array and we are saying stdc 12542 09:31:35,436 --> 09:31:40,880 out message for you can't do this with 12543 09:31:38,080 --> 09:31:43,840 other arrays and this is really all i 12544 09:31:40,879 --> 09:31:45,839 had to share about character arrays so 12545 09:31:43,839 --> 09:31:48,159 what we're going to do next is head to 12546 09:31:45,839 --> 09:31:51,275 visual studio code and play with us okay 12547 09:31:48,160 --> 09:31:54,400 here we are in our working directory the 12548 09:31:51,275 --> 09:31:57,360 current project is arrays of characters 12549 09:31:54,400 --> 09:31:59,275 we're going to grab our template project 12550 09:31:57,360 --> 09:32:01,915 and we're going to copy the files 12551 09:32:01,915 --> 09:32:06,879 and we are going to open our folder 12552 09:32:04,720 --> 09:32:08,559 we're going to open the main cpp file 12553 09:32:06,879 --> 09:32:10,639 and we're going to get rid of what we 12554 09:32:08,559 --> 09:32:12,479 don't need here let's put in some 12555 09:32:10,639 --> 09:32:14,400 startup code so that we can start 12556 09:32:12,480 --> 09:32:16,720 playing with this without typing this 12557 09:32:14,400 --> 09:32:18,959 too much so we're going to declare an 12558 09:32:16,720 --> 09:32:20,880 array of characters it's going to be 12559 09:32:18,959 --> 09:32:22,799 called message the size is going to be 12560 09:32:20,879 --> 09:32:25,040 five and we're going to fill it with 12561 09:32:22,800 --> 09:32:27,756 characters like this so if we do this we 12562 09:32:25,040 --> 09:32:30,400 might want to print it like we print any 12563 09:32:27,756 --> 09:32:32,720 other array we're going to use loops and 12564 09:32:32,720 --> 09:32:37,120 a range base for loop so we take a toast 12565 09:32:37,120 --> 09:32:41,276 in message and we're going to print out 12566 09:32:39,275 --> 09:32:44,879 each character notice that we're not 12567 09:32:41,275 --> 09:32:47,360 adding std endl here because we want the 12568 09:32:44,879 --> 09:32:49,915 characters printed close to each other 12569 09:32:47,360 --> 09:32:51,840 so that we can read this easily and 12570 09:32:49,915 --> 09:32:53,519 after this we will need to go to the 12571 09:32:51,839 --> 09:32:55,839 next line so that the next thing we 12572 09:32:53,519 --> 09:32:59,915 print doesn't disturb us so we're going 12573 09:32:55,839 --> 09:33:01,915 to say sddc out std endl and if we do 12574 09:32:59,915 --> 09:33:04,399 this we're going to see hello printed 12575 09:33:01,915 --> 09:33:07,040 out on the console but we had to do too 12576 09:33:04,400 --> 09:33:09,360 much work to print something simple 12577 09:33:07,040 --> 09:33:10,319 like hello but let's do this we are 12578 09:33:10,319 --> 09:33:15,360 so we're going to bring up our terminal 12579 09:33:12,800 --> 09:33:16,960 and we're going to build with gcc let's 12580 09:33:16,959 --> 09:33:20,400 the world is going to go through and 12581 09:33:20,400 --> 09:33:24,239 and run rooster you're going to see that 12582 09:33:22,480 --> 09:33:26,080 it's going to say message is hello and 12583 09:33:24,239 --> 09:33:28,479 this is what we expect and this is 12584 09:33:26,080 --> 09:33:30,560 really how you would declare an array of 12585 09:33:28,480 --> 09:33:32,480 characters and initialize it with 12586 09:33:30,559 --> 09:33:34,720 whatever it is you want to initialize it 12587 09:33:32,480 --> 09:33:37,916 with but you notice that it's really not 12588 09:33:34,720 --> 09:33:40,959 convenient to put data in here if you 12589 09:33:37,915 --> 09:33:43,839 want to store a message for example for 12590 09:33:40,959 --> 09:33:45,519 someone you don't want to do this thing 12591 09:33:43,839 --> 09:33:48,000 you see here it's really ugly it's 12592 09:33:45,519 --> 09:33:50,879 convenient and nobody would definitely 12593 09:33:48,000 --> 09:33:53,199 use your program if you required your 12594 09:33:50,879 --> 09:33:55,595 users to type things like this so we 12595 09:33:53,199 --> 09:33:57,755 need to change this and we will but 12596 09:33:55,595 --> 09:34:00,639 before we get there let's also see that 12597 09:33:57,756 --> 09:34:03,520 we can change characters in our array so 12598 09:34:00,639 --> 09:34:07,120 if you look here this e here is at index 12599 09:34:03,519 --> 09:34:10,079 two so what we can say is a message and 12600 09:34:07,120 --> 09:34:11,040 uh we're going to target the element on 12601 09:34:13,680 --> 09:34:19,360 let's use that so this is index zero the 12602 09:34:16,239 --> 09:34:21,519 h and the e is at index one let's make 12603 09:34:19,360 --> 09:34:23,595 sure we get this straight and we're 12604 09:34:23,595 --> 09:34:29,839 in quotes remember characters are 12605 09:34:27,120 --> 09:34:32,720 surrounded by single quotes like this 12606 09:34:29,839 --> 09:34:35,679 once we do this and we try to print the 12607 09:34:35,680 --> 09:34:39,519 technique here we're going to copy the 12608 09:34:37,519 --> 09:34:40,319 code and we're going to paste it down 12609 09:34:42,800 --> 09:34:48,880 and if we do this we're going to see the 12610 09:34:45,436 --> 09:34:50,800 message printed out here to be hello 12611 09:34:48,879 --> 09:34:53,040 and the second message is going to be 12612 09:34:53,040 --> 09:34:58,800 because we changed the second element 12613 09:34:56,160 --> 09:35:00,960 to an a it's no longer an e so that's 12614 09:34:58,800 --> 09:35:03,840 what we expect we're going to run the 12615 09:35:00,959 --> 09:35:06,080 test to build with gcc and we're going 12616 09:35:03,839 --> 09:35:07,915 to bring this up and clear and we're 12617 09:35:06,080 --> 09:35:11,120 going to run rooster you see that we 12618 09:35:07,915 --> 09:35:13,595 have message hello and message hello and 12619 09:35:11,120 --> 09:35:16,000 we are able to print things successfully 12620 09:35:13,595 --> 09:35:18,799 this is really cool but we are doing too 12621 09:35:16,000 --> 09:35:20,639 much work really to print something on 12622 09:35:18,800 --> 09:35:23,276 the console and there should be an 12623 09:35:20,639 --> 09:35:25,519 easier way to do this and there is so 12624 09:35:23,275 --> 09:35:28,079 what we're going to do is comment this 12625 09:35:25,519 --> 09:35:30,239 out because we don't want noise output 12626 09:35:28,080 --> 09:35:31,756 in the console when we try to do 12627 09:35:31,756 --> 09:35:35,840 so what we're going to do is comment out 12628 09:35:34,160 --> 09:35:38,240 all we've done so far we're going to 12629 09:35:35,839 --> 09:35:40,559 leave in our message here and we're 12630 09:35:38,239 --> 09:35:44,000 going to try and print it directly on 12631 09:35:40,559 --> 09:35:46,159 the console so we can say sddcl 12632 09:35:46,160 --> 09:35:51,520 and we're going to print out the message 12633 09:35:48,720 --> 09:35:54,480 and take a moment and look at this 12634 09:35:51,519 --> 09:35:56,559 and try to compile this it is going to 12635 09:35:54,480 --> 09:35:58,720 go through it is going to compile 12636 09:35:58,720 --> 09:36:02,559 supports printing character arrays like 12637 09:36:03,915 --> 09:36:09,119 in most times this is not going to do 12638 09:36:05,756 --> 09:36:11,360 what you expect let's try and run this 12639 09:36:14,800 --> 09:36:19,120 we're lucky to get something that really 12640 09:36:19,120 --> 09:36:23,520 it's pretty hello and you see that we 12641 09:36:21,360 --> 09:36:25,840 have a bunch of other things and the 12642 09:36:23,519 --> 09:36:28,879 reason is what we have discussed in 12643 09:36:25,839 --> 09:36:31,435 these slides for character arrays like 12644 09:36:28,879 --> 09:36:34,399 this to be printed out properly 12645 09:36:31,436 --> 09:36:36,880 on the console they need to be null 12646 09:36:34,400 --> 09:36:39,360 terminated in other words they need to 12647 09:36:36,879 --> 09:36:42,239 have the null character which is a 12648 09:36:39,360 --> 09:36:45,680 backslash zero appended to the string 12649 09:36:42,239 --> 09:36:47,756 for sddc out to know that it has reached 12650 09:36:45,680 --> 09:36:51,275 the end of the string so this is what 12651 09:36:47,756 --> 09:36:52,720 makes your character strings legal 12652 09:36:51,275 --> 09:36:55,360 now you see that we are getting a 12653 09:36:52,720 --> 09:36:57,840 compiler error because we have overflown 12654 09:36:55,360 --> 09:36:59,840 the size of the array i think that's the 12655 09:36:57,839 --> 09:37:02,239 message too many initializer values and 12656 09:36:59,839 --> 09:37:05,119 this is exactly what we have done 12657 09:37:02,239 --> 09:37:07,839 now we can try and change this guy to a 12658 09:37:05,120 --> 09:37:08,639 six to say that we have six elements in 12659 09:37:08,639 --> 09:37:13,275 this wiggly line should go away and if 12660 09:37:13,275 --> 09:37:18,479 we're going to build with gcc 12661 09:37:15,839 --> 09:37:20,079 let's bring this up a little bit and 12662 09:37:22,400 --> 09:37:26,720 and you see that now we have hello 12663 09:37:24,000 --> 09:37:29,360 printed out correctly if we turn this 12664 09:37:29,360 --> 09:37:35,275 and take out the null terminator 12665 09:37:33,120 --> 09:37:37,840 we're going to try and build again and 12666 09:37:35,275 --> 09:37:39,119 we're going to clear and run rooster 12667 09:37:37,839 --> 09:37:41,755 you're going to see that we get the 12668 09:37:39,120 --> 09:37:44,560 garbage back so for your strings to be 12669 09:37:41,756 --> 09:37:48,000 printed out correctly they need to be 12670 09:37:44,559 --> 09:37:50,639 now terminated that's why it is not safe 12671 09:37:50,639 --> 09:37:55,275 when your character array is not now 12672 09:37:53,595 --> 09:37:58,000 terminated i really want you to 12673 09:37:55,275 --> 09:38:00,959 understand this so in c plus plus we 12674 09:37:58,000 --> 09:38:03,436 have a proper name for these strings 12675 09:38:00,959 --> 09:38:05,756 that are null terminated a null 12676 09:38:03,436 --> 09:38:07,916 terminated string is called the c string 12677 09:38:05,756 --> 09:38:09,756 because these things come from the c 12678 09:38:07,915 --> 09:38:11,915 programming language okay and we're 12679 09:38:09,756 --> 09:38:15,360 going to see a few ways we can set up c 12680 09:38:11,915 --> 09:38:18,479 strings now and the first one is to say 12681 09:38:15,360 --> 09:38:20,080 car we can call this message one 12682 09:38:18,480 --> 09:38:22,240 and it's going to be an array of 12683 09:38:20,080 --> 09:38:24,400 characters and we're going to initialize 12684 09:38:22,239 --> 09:38:26,720 this with hello let's copy this because 12685 09:38:24,400 --> 09:38:29,756 i don't want to type all these 12686 09:38:26,720 --> 09:38:31,916 things here so let's copy this 12687 09:38:29,756 --> 09:38:33,916 and initialize this properly notice that 12688 09:38:31,915 --> 09:38:36,159 we didn't specify the size so the 12689 09:38:33,915 --> 09:38:38,639 compiler is going to get the size from 12690 09:38:36,160 --> 09:38:41,120 however many elements we put in here and 12691 09:38:38,639 --> 09:38:43,360 we're going to put in our null 12692 09:38:41,120 --> 09:38:46,160 terminator here and if we print this 12693 09:38:43,360 --> 09:38:48,239 this is going to work and we are going 12694 09:38:46,160 --> 09:38:50,880 to comment this out and i'm going to say 12695 09:38:48,239 --> 09:38:52,159 that this will give you garbage okay if 12696 09:38:50,879 --> 09:38:55,040 we come down here we're going to see 12697 09:38:52,160 --> 09:38:57,916 that we have a car array it is called 12698 09:38:55,040 --> 09:39:00,720 message1 and we have initialized it with 12699 09:38:57,915 --> 09:39:03,119 data we have hello and we have a null 12700 09:39:00,720 --> 09:39:04,800 terminator here and the compiler is 12701 09:39:03,120 --> 09:39:07,200 going to deduce the size so this is 12702 09:39:04,800 --> 09:39:09,200 going to be a proper c string and we 12703 09:39:09,199 --> 09:39:14,159 let's try and weld this the board is 12704 09:39:11,756 --> 09:39:15,680 going to go through if we clear and run 12705 09:39:14,160 --> 09:39:18,640 rooster we're going to get our message 12706 09:39:15,680 --> 09:39:21,756 here and this is one way we can set up a 12707 09:39:18,639 --> 09:39:25,680 proper c string the second way is to 12708 09:39:21,756 --> 09:39:28,559 actually not putting the null terminator 12709 09:39:25,680 --> 09:39:30,160 but let the compiler add that in for you 12710 09:39:28,559 --> 09:39:33,199 and the way we can do that we can say 12711 09:39:30,160 --> 09:39:37,276 car message to and we're going to say 12712 09:39:33,199 --> 09:39:39,119 this array is of size 6. let's say that 12713 09:39:39,120 --> 09:39:43,276 and a fix how we end things here we're 12714 09:39:43,275 --> 09:39:48,000 and end this with a semicolon now what 12715 09:39:46,080 --> 09:39:50,160 is going to happen is the compiler is 12716 09:39:48,000 --> 09:39:53,199 going to see that this array is of size 12717 09:39:50,160 --> 09:39:55,520 6 but we are putting in five elements so 12718 09:39:53,199 --> 09:39:58,479 the remaining slots are going to be auto 12719 09:39:55,519 --> 09:40:01,275 filled and it is going to be using 12720 09:39:58,480 --> 09:40:03,520 null terminators to autofill this then 12721 09:40:01,275 --> 09:40:05,839 if we try to print the size here we're 12722 09:40:05,839 --> 09:40:12,239 because the null terminator is added in 12723 09:40:09,275 --> 09:40:14,959 by the compiler let's try and print the 12724 09:40:12,239 --> 09:40:17,519 sizes here actually so that you can see 12725 09:40:14,959 --> 09:40:19,436 these things so we're going to say size 12726 09:40:17,519 --> 09:40:21,755 and we're going to say that size of 12727 09:40:19,436 --> 09:40:24,720 message one and we're going to put in a 12728 09:40:21,756 --> 09:40:27,200 new line character and this should be 12729 09:40:24,720 --> 09:40:28,959 what we expect so what we're going to do 12730 09:40:28,959 --> 09:40:32,479 and i put that down here because we're 12731 09:40:30,959 --> 09:40:34,319 going to be doing the same thing for 12732 09:40:32,480 --> 09:40:36,319 message to so we're going to say message 12733 09:40:36,319 --> 09:40:40,639 and i say size of message to and do 12734 09:40:38,800 --> 09:40:43,200 signs of message too so what we're going 12735 09:40:40,639 --> 09:40:45,680 to see is a message 1 and message 2 12736 09:40:43,199 --> 09:40:48,399 printing out and we expect to see a size 12737 09:40:45,680 --> 09:40:51,199 of 6 because both of these guys have six 12738 09:40:48,400 --> 09:40:54,239 elements inside so we're going to run 12739 09:40:58,080 --> 09:41:03,436 and this is exactly what we expect 12740 09:41:00,720 --> 09:41:05,840 message one is hello the size is six 12741 09:41:03,436 --> 09:41:07,916 message two is hello and the size is six 12742 09:41:05,839 --> 09:41:10,159 and these are a few ways you can declare 12743 09:41:07,915 --> 09:41:12,959 your character arrays and initialize 12744 09:41:10,160 --> 09:41:16,400 them with proper data that is going to 12745 09:41:12,959 --> 09:41:18,959 print correctly with stdc out make sure 12746 09:41:16,400 --> 09:41:21,840 you don't do something like we did here 12747 09:41:18,959 --> 09:41:24,159 by printing message here because it is 12748 09:41:21,839 --> 09:41:27,119 not null terminated this is going to 12749 09:41:24,160 --> 09:41:29,200 give you a lot of problems again i want 12750 09:41:29,199 --> 09:41:35,595 that message three here is not a proper 12751 09:41:32,800 --> 09:41:37,756 c string because the compiler is going 12752 09:41:35,595 --> 09:41:40,399 to deduce however many elements we have 12753 09:41:37,756 --> 09:41:42,239 in here and this thing is going to be of 12754 09:41:42,239 --> 09:41:45,680 and we're not going to have a null 12755 09:41:43,839 --> 09:41:47,755 terminator and if we print it out we're 12756 09:41:45,680 --> 09:41:50,239 going to get some garbage so let's try 12757 09:41:47,756 --> 09:41:51,040 and copy this so that we are consistent 12758 09:41:51,040 --> 09:41:56,639 what we've done above here and if we 12759 09:41:56,639 --> 09:42:01,915 we are going to compile successfully but 12760 09:41:58,959 --> 09:42:04,319 we may get weird things if we print out 12761 09:42:01,915 --> 09:42:06,159 message three like we do here and you're 12762 09:42:04,319 --> 09:42:09,040 going to see that the size is five 12763 09:42:06,160 --> 09:42:13,200 because we only have five elements in 12764 09:42:09,040 --> 09:42:14,879 here so let's clear and run rooster and 12765 09:42:14,879 --> 09:42:19,519 we have hello for message one we have 12766 09:42:17,120 --> 09:42:22,560 hello for message two but for message 12767 09:42:19,519 --> 09:42:24,719 three we have things we don't expect 12768 09:42:22,559 --> 09:42:26,639 because it's going to keep printing and 12769 09:42:24,720 --> 09:42:29,520 it's going to go overbound and it's 12770 09:42:26,639 --> 09:42:32,319 going to stop when it hits the first 12771 09:42:32,319 --> 09:42:37,436 like what we have in here okay so this 12772 09:42:34,800 --> 09:42:39,276 is not safe don't do it make sure your 12773 09:42:39,275 --> 09:42:44,799 it if you intend to print them with sddc 12774 09:42:42,319 --> 09:42:46,800 out like this okay so working with 12775 09:42:44,800 --> 09:42:48,639 character arrays like this to print 12776 09:42:46,800 --> 09:42:51,120 messages and do all kinds of crazy 12777 09:42:48,639 --> 09:42:53,436 things is nice but it really isn't 12778 09:42:53,436 --> 09:42:58,720 you wouldn't expect your users to type 12779 09:42:55,839 --> 09:43:01,435 characters like this by surrounding them 12780 09:42:58,720 --> 09:43:04,480 with single quotes this is really ugly 12781 09:43:01,436 --> 09:43:07,436 and it is painful to do if you want to 12782 09:43:04,480 --> 09:43:10,240 do some productive work so c plus plus 12783 09:43:07,436 --> 09:43:11,916 allows you to use what we call string 12784 09:43:10,239 --> 09:43:13,436 literals and we're going to see an 12785 09:43:13,436 --> 09:43:18,400 down below we're going to say string 12786 09:43:15,040 --> 09:43:21,756 literals and uh what we can do is car 12787 09:43:18,400 --> 09:43:22,880 message for it's going to be an array as 12788 09:43:25,199 --> 09:43:30,399 with a string that is surrounded by 12789 09:43:27,915 --> 09:43:32,239 double quotes and what this is going to 12790 09:43:32,239 --> 09:43:38,400 is you know take these things break it 12791 09:43:35,120 --> 09:43:41,840 into characters and store this in this 12792 09:43:38,400 --> 09:43:44,880 array here and make sure you properly 12793 09:43:41,839 --> 09:43:47,519 now terminate this string here so if we 12794 09:43:44,879 --> 09:43:50,000 print the size of message four we're not 12795 09:43:47,519 --> 09:43:52,000 going to get five we're going to get six 12796 09:43:50,000 --> 09:43:54,319 because c plus plus is going to see 12797 09:43:52,000 --> 09:43:56,319 these double quotes and know that we 12798 09:43:54,319 --> 09:43:58,879 intend for these things to be properly 12799 09:43:56,319 --> 09:44:02,400 null terminated and it's going to add a 12800 09:43:58,879 --> 09:44:05,199 null terminator so let's try and print 12801 09:44:02,400 --> 09:44:08,160 this out so that we really are sure and 12802 09:44:05,199 --> 09:44:09,360 we're going to weld this with gcc as we 12803 09:44:09,360 --> 09:44:13,520 the world is going to go through let's 12804 09:44:11,360 --> 09:44:14,639 go up and clear and we're going to run 12805 09:44:14,639 --> 09:44:20,319 and you see that message 4 is hello 12806 09:44:20,319 --> 09:44:25,199 so this is one way we have to do things 12807 09:44:25,199 --> 09:44:30,559 and do all kinds of crazy things and 12808 09:44:27,915 --> 09:44:32,319 it's really cool and if we try to build 12809 09:44:30,559 --> 09:44:34,399 this we're going to get the proper 12810 09:44:32,319 --> 09:44:38,080 message printed out you see the world is 12811 09:44:34,400 --> 09:44:40,239 good if we clear and run rooster we're 12812 09:44:38,080 --> 09:44:42,720 going to see hello world how are you 12813 09:44:40,239 --> 09:44:45,680 doing and this is really cool so from 12814 09:44:42,720 --> 09:44:47,756 now on if we need to store a string in 12815 09:44:45,680 --> 09:44:49,519 our program this is how we're going to 12816 09:44:47,756 --> 09:44:52,160 do it because this is much more 12817 09:44:49,519 --> 09:44:54,399 civilized and it is how you would really 12818 09:44:52,160 --> 09:44:57,276 like to type characters in your program 12819 09:44:54,400 --> 09:44:59,596 i have said that character arrays are 12820 09:44:57,275 --> 09:45:03,680 really special because you can directly 12821 09:44:59,595 --> 09:45:05,360 print them out with stdc out like this i 12822 09:45:03,680 --> 09:45:07,436 want to prove this so we're going to try 12823 09:45:05,360 --> 09:45:09,756 and declare another array so we're going 12824 09:45:09,756 --> 09:45:14,080 and we're going to make this an array 12825 09:45:11,595 --> 09:45:17,595 and we're going to initialize with one 12826 09:45:14,080 --> 09:45:19,596 two three four five this is going to do 12827 09:45:17,595 --> 09:45:21,435 and we're going to put our closing 12828 09:45:19,595 --> 09:45:24,879 semicolon and we're going to try and 12829 09:45:21,436 --> 09:45:27,120 print this out i'm going to say std 12830 09:45:29,360 --> 09:45:33,680 let's try and compile this and see what 12831 09:45:31,275 --> 09:45:36,159 we get it is not actually going to give 12832 09:45:33,680 --> 09:45:38,080 us a compiler error but it's not going 12833 09:45:36,160 --> 09:45:39,840 to print what we expect and let's put a 12834 09:45:38,080 --> 09:45:42,400 message here so that we know that it is 12835 09:45:39,839 --> 09:45:44,559 this thing we are seeing here so we're 12836 09:45:42,400 --> 09:45:47,120 going to say numbers and we're going to 12837 09:45:54,879 --> 09:46:00,239 and if a run rooster look out for the 12838 09:45:57,915 --> 09:46:02,559 output that says numbers here so let's 12839 09:46:00,239 --> 09:46:05,040 run rooster it's not a one two three 12840 09:46:02,559 --> 09:46:05,839 four five it's something we don't really 12841 09:46:05,839 --> 09:46:09,435 and we will get to understand what is 12842 09:46:09,436 --> 09:46:14,959 in the next chapter for now note that 12843 09:46:12,160 --> 09:46:17,520 you can't directly print arrays other 12844 09:46:14,959 --> 09:46:19,120 than those of characters if you try to 12845 09:46:17,519 --> 09:46:21,119 do that you're going to get weird 12846 09:46:19,120 --> 09:46:23,276 numbers printed out and we're going to 12847 09:46:23,275 --> 09:46:27,595 the next chapter so let's say that here 12848 09:46:25,756 --> 09:46:29,360 okay this is really all we set out to do 12849 09:46:27,595 --> 09:46:32,239 in this lecture i hope you found it 12850 09:46:29,360 --> 09:46:34,480 interesting character arrays have a few 12851 09:46:32,239 --> 09:46:37,119 things you need to be careful about and 12852 09:46:34,480 --> 09:46:39,040 we try to cover those in this lecture we 12853 09:46:37,120 --> 09:46:40,880 are going to stop here in this lecture 12854 09:46:39,040 --> 09:46:43,680 and the next one we're going to try and 12855 09:46:43,680 --> 09:46:48,319 go ahead and finish up here and meet me 12856 09:46:46,000 --> 09:46:50,959 there in this lecture we're going to 12857 09:46:48,319 --> 09:46:53,680 look at what could happen if you try to 12858 09:46:50,959 --> 09:46:56,479 manipulate data out of the bounds of 12859 09:46:53,680 --> 09:46:59,680 your array we have seen that when you 12860 09:46:56,480 --> 09:47:01,520 allocate space for an array the computer 12861 09:46:59,680 --> 09:47:03,915 is going to give you a bunch of memory 12862 09:47:01,519 --> 09:47:06,239 locations you can use to store your data 12863 09:47:03,915 --> 09:47:08,639 and it's going to be of the size that 12864 09:47:06,239 --> 09:47:11,360 you have specified or that is deduced by 12865 09:47:08,639 --> 09:47:13,360 the compiler so here is an example of an 12866 09:47:11,360 --> 09:47:16,000 array that is going to store seven 12867 09:47:13,360 --> 09:47:18,000 elements and we have green slots for 12868 09:47:16,000 --> 09:47:21,199 these elements and these are the 12869 09:47:18,000 --> 09:47:23,915 locations we are allowed to manipulate 12870 09:47:21,199 --> 09:47:26,159 in our c plus plus program if we try to 12871 09:47:23,915 --> 09:47:29,519 go overbound for example if we try to 12872 09:47:26,160 --> 09:47:31,520 modify something before our array or 12873 09:47:31,519 --> 09:47:35,839 it might be a memory location that is 12874 09:47:33,360 --> 09:47:38,000 owned by another program it might be a 12875 09:47:35,839 --> 09:47:40,639 memory location that is even being used 12876 09:47:38,000 --> 09:47:44,000 by the operating system so bad things 12877 09:47:40,639 --> 09:47:47,199 can really happen if you try to use data 12878 09:47:44,000 --> 09:47:49,120 that is outside the bounds of your array 12879 09:47:47,199 --> 09:47:50,639 let's try to look at a simple example 12880 09:47:49,120 --> 09:47:54,000 here here we have an array called 12881 09:47:50,639 --> 09:47:57,519 numbers we have 10 slots inside so we 12882 09:47:54,000 --> 09:48:00,400 are only allowed to modify the memory 12883 09:47:57,519 --> 09:48:03,119 locations for these 10 numbers and if we 12884 09:48:00,400 --> 09:48:05,520 try to access something outside the 12885 09:48:03,120 --> 09:48:07,916 bounds of this array something might 12886 09:48:05,519 --> 09:48:10,479 happen but some people say that c plus 12887 09:48:07,915 --> 09:48:12,319 plus is really funny because it should 12888 09:48:10,480 --> 09:48:14,480 be stopping you from doing something 12889 09:48:12,319 --> 09:48:17,360 like this but c plus plus allows you to 12890 09:48:14,480 --> 09:48:20,240 do this if you look at code here we have 12891 09:48:17,360 --> 09:48:23,120 an array we have 10 elements n so the 12892 09:48:20,239 --> 09:48:26,319 valid indexes we can really use are from 12893 09:48:23,120 --> 09:48:29,200 0 to 9. but if we try and access 12894 09:48:26,319 --> 09:48:31,275 something at index 12 the code is going 12895 09:48:29,199 --> 09:48:33,199 to compile and if we run it we're going 12896 09:48:31,275 --> 09:48:35,360 to get something printed out but that's 12897 09:48:33,199 --> 09:48:37,915 something this is going to give you 12898 09:48:35,360 --> 09:48:40,639 garbage data or you might be even trying 12899 09:48:37,915 --> 09:48:42,239 to read some restricted memory and your 12900 09:48:40,639 --> 09:48:44,720 program is going to terminate 12901 09:48:42,239 --> 09:48:47,839 immediately and we're going to say that 12902 09:48:44,720 --> 09:48:50,080 it has crashed if you try to write over 12903 09:48:47,839 --> 09:48:52,959 bounce the compiler is also going to 12904 09:48:50,080 --> 09:48:55,040 allow it but you don't own the memory so 12905 09:48:52,959 --> 09:48:57,360 other programs might come and change 12906 09:48:55,040 --> 09:48:58,959 that memory location and if you try to 12907 09:48:57,360 --> 09:49:00,959 read from it again you're not going to 12908 09:48:58,959 --> 09:49:03,436 get what you expect another thing that 12909 09:49:00,959 --> 09:49:06,319 could happen is that you try and modify 12910 09:49:03,436 --> 09:49:09,596 data that is owned by other programs and 12911 09:49:06,319 --> 09:49:12,800 you might imagine how bad that really is 12912 09:49:09,595 --> 09:49:14,799 so the message here is don't go over 12913 09:49:12,800 --> 09:49:17,596 your array balance and you're going to 12914 09:49:14,800 --> 09:49:20,000 be playing in the safe zone let's head 12915 09:49:17,595 --> 09:49:22,720 over to visual studio code and play with 12916 09:49:20,000 --> 09:49:25,436 us a little bit here we are in our 12917 09:49:22,720 --> 09:49:27,520 working directory the current project is 12918 09:49:25,436 --> 09:49:29,436 array balance we're going to grab the 12919 09:49:33,275 --> 09:49:36,639 and we're going to open this up in 12920 09:49:34,639 --> 09:49:40,239 visual studio code we are going to 12921 09:49:36,639 --> 09:49:43,436 declare an array and try to read beyond 12922 09:49:40,239 --> 09:49:46,319 the legal bounds of our array so let's 12923 09:49:43,436 --> 09:49:48,800 try and bring up a terminal here and we 12924 09:49:46,319 --> 09:49:50,639 are going to build this with gcc the 12925 09:49:48,800 --> 09:49:53,436 world is going to go through you're not 12926 09:49:50,639 --> 09:49:55,519 going to get a compiler error but if we 12927 09:49:53,436 --> 09:49:58,080 run this we can really get anything and 12928 09:49:55,519 --> 09:50:01,039 you see we have a number we don't really 12929 09:49:58,080 --> 09:50:02,720 know what it means and this means that 12930 09:50:02,720 --> 09:50:07,275 out of the legal bounds of your array 12931 09:50:07,275 --> 09:50:13,119 allows you to do this and the reason 12932 09:50:09,756 --> 09:50:15,680 they do this is because they expect you 12933 09:50:13,120 --> 09:50:17,840 the developer to know what you're doing 12934 09:50:15,680 --> 09:50:20,639 when you try manipulating things like 12935 09:50:17,839 --> 09:50:22,559 ray and now you know this and be careful 12936 09:50:20,639 --> 09:50:25,040 when you are working with arrays like 12937 09:50:22,559 --> 09:50:28,479 this so don't go overbound that's the 12938 09:50:25,040 --> 09:50:31,680 message we may also try to modify data 12939 09:50:28,480 --> 09:50:34,000 beyond balance so let's go down and put 12940 09:50:31,680 --> 09:50:36,959 in the code to modify thanks and i have 12941 09:50:34,000 --> 09:50:38,559 put a clear message here as a comment to 12942 09:50:36,959 --> 09:50:40,799 let you know that this is really 12943 09:50:38,559 --> 09:50:42,639 something you should be very very 12944 09:50:40,800 --> 09:50:44,880 careful about so we're going to try and 12945 09:50:42,639 --> 09:50:46,400 change that number to 1000 and we're 12946 09:50:44,879 --> 09:50:49,519 going to try and print it out and see 12947 09:50:46,400 --> 09:50:51,040 what we get but this memory location at 12948 09:50:51,040 --> 09:50:55,915 is not something we own so other 12949 09:50:53,595 --> 09:50:58,399 programs might come and write to this 12950 09:50:55,915 --> 09:51:01,119 location and if we try to read from it 12951 09:50:58,400 --> 09:51:03,520 again we might not get what we put in 12952 09:51:01,120 --> 09:51:06,240 here even worse this memory location 12953 09:51:03,519 --> 09:51:08,079 might be owned by other programs and if 12954 09:51:06,239 --> 09:51:10,639 they read they're going to get what they 12955 09:51:08,080 --> 09:51:13,120 didn't put in they're going to get what 12956 09:51:10,639 --> 09:51:15,436 you put in and this is something bad we 12957 09:51:13,120 --> 09:51:17,520 might try and really go crazy with us 12958 09:51:15,436 --> 09:51:19,680 let's put in a really huge number and 12959 09:51:19,680 --> 09:51:23,040 this is a good way to learn by the way 12960 09:51:23,040 --> 09:51:28,480 and let's try and print what we have in 12961 09:51:25,595 --> 09:51:30,559 there and we're going to say 12962 09:51:30,559 --> 09:51:34,879 and that's what we're going to say here 12963 09:51:32,720 --> 09:51:36,480 and we're going to build again the world 12964 09:51:34,879 --> 09:51:39,040 is going to go through you see that we 12965 09:51:36,480 --> 09:51:41,916 are doing something really crazy our 12966 09:51:39,040 --> 09:51:44,559 array has 10 elements but we are trying 12967 09:51:44,559 --> 09:51:50,639 1 million memory locations away and uh 12968 09:51:48,480 --> 09:51:52,400 this is really crazy and c plus plus 12969 09:51:52,400 --> 09:51:58,239 so let's clear and run did we world 12970 09:51:54,720 --> 09:52:00,319 let's make sure we have world so let's 12971 09:52:02,160 --> 09:52:06,240 and run rooster our program is crashing 12972 09:52:04,720 --> 09:52:07,756 because we are trying to access 12973 09:52:07,756 --> 09:52:10,959 that isn't really ours and you see it 12974 09:52:10,959 --> 09:52:16,159 okay for example if we have something 12975 09:52:13,120 --> 09:52:18,160 printed out after this let's say std 12976 09:52:18,160 --> 09:52:21,680 and then so that you can see it is 12977 09:52:23,915 --> 09:52:29,040 this statement here is not going to 12978 09:52:26,400 --> 09:52:31,275 print because once we hit this location 12979 09:52:29,040 --> 09:52:33,120 we're going to try and modify this and 12980 09:52:31,275 --> 09:52:34,479 the program is not going to be able to 12981 09:52:33,120 --> 09:52:36,160 do that it's going to crash the 12982 09:52:34,480 --> 09:52:38,560 operating system is going to notice that 12983 09:52:36,160 --> 09:52:40,880 we are trying to do something crazy and 12984 09:52:38,559 --> 09:52:43,199 it's going to say this program is really 12985 09:52:40,879 --> 09:52:44,879 crazy it doesn't know what it's doing i 12986 09:52:43,199 --> 09:52:47,039 am going to kill it and it is going to 12987 09:52:44,879 --> 09:52:50,879 stop that's basically what is happening 12988 09:52:47,040 --> 09:52:53,200 here so let's weld again with gcc 12989 09:52:55,120 --> 09:52:59,520 and run rooster you see that it's going 12990 09:52:57,680 --> 09:53:01,756 to try and do this it's going to try and 12991 09:52:59,519 --> 09:53:04,399 put a value in here the program is going 12992 09:53:01,756 --> 09:53:06,319 to end here and this is really bad 12993 09:53:04,400 --> 09:53:09,040 because memory locations you have 12994 09:53:06,319 --> 09:53:11,360 allocated for before here are not 12995 09:53:09,040 --> 09:53:13,520 properly released and returned to the 12996 09:53:11,360 --> 09:53:16,400 operating system and this is something 12997 09:53:16,400 --> 09:53:22,560 don't go overbound make sure you are 12998 09:53:19,519 --> 09:53:25,519 working within the legal boundaries of 12999 09:53:22,559 --> 09:53:27,435 your array welcome to this new chapter 13000 09:53:25,519 --> 09:53:29,680 and we are going to be talking about 13001 09:53:27,436 --> 09:53:32,239 pointers pointers are one of the 13002 09:53:29,680 --> 09:53:34,480 important topics in c plus plus and they 13003 09:53:32,239 --> 09:53:36,720 allow you to do some of the things very 13004 09:53:34,480 --> 09:53:38,960 conveniently and we're going to be 13005 09:53:36,720 --> 09:53:41,200 learning about all those tricky things 13006 09:53:38,959 --> 09:53:43,199 you can do with them in this chapter but 13007 09:53:41,199 --> 09:53:45,435 now i want you to have a basic 13008 09:53:43,199 --> 09:53:46,239 understanding of what a pointer really 13009 09:53:46,239 --> 09:53:50,000 so far we have been working with 13010 09:53:47,915 --> 09:53:52,399 variables like this we set up a variable 13011 09:53:50,000 --> 09:53:54,480 in our c plus plus code we get to a 13012 09:53:52,400 --> 09:53:56,880 point where we put a value inside that 13013 09:53:54,480 --> 09:54:00,000 variable and we could use it for example 13014 09:53:56,879 --> 09:54:02,000 we could print it out on stdc out or we 13015 09:54:00,000 --> 09:54:04,239 could even store a value or assign 13016 09:54:02,000 --> 09:54:06,800 another value to the variable we've been 13017 09:54:04,239 --> 09:54:08,639 doing that quite a lot but there is a 13018 09:54:06,800 --> 09:54:11,276 point we haven't really been talking 13019 09:54:08,639 --> 09:54:13,839 about those variables have an address in 13020 09:54:11,275 --> 09:54:15,199 memory and here for example you see that 13021 09:54:15,199 --> 09:54:19,755 is stored at location one zero zero 13022 09:54:17,756 --> 09:54:21,120 eight of course this is a simple example 13023 09:54:21,120 --> 09:54:26,319 but i hope it drives the point home all 13024 09:54:24,080 --> 09:54:28,720 the variables you use in your code have 13025 09:54:26,319 --> 09:54:31,756 some kind of address in memory 13026 09:54:28,720 --> 09:54:34,400 and we can grab that address and store 13027 09:54:31,756 --> 09:54:36,800 that in a special kind of variable which 13028 09:54:34,400 --> 09:54:37,840 is really a pointer so we can say that a 13029 09:54:37,839 --> 09:54:43,435 is a special kind of variable 13030 09:54:40,239 --> 09:54:45,275 that stores addresses of other variables 13031 09:54:43,436 --> 09:54:47,680 make sure you really understand this 13032 09:54:45,275 --> 09:54:50,879 here we have p var so it is a pointer 13033 09:54:47,680 --> 09:54:54,400 its type is end pointer and we denote 13034 09:54:50,879 --> 09:54:56,479 pointer using this star symbol after our 13035 09:54:54,400 --> 09:54:58,160 type if you see something like this 13036 09:54:56,480 --> 09:55:00,720 you're going to know that this type is a 13037 09:54:58,160 --> 09:55:02,560 pointer to end its name is going to be p 13038 09:55:02,559 --> 09:55:06,639 and its value is going to be the address 13039 09:55:04,959 --> 09:55:10,239 of another variable you see it is 13040 09:55:06,639 --> 09:55:13,595 storing 1 0 0 8 which happens to be the 13041 09:55:10,239 --> 09:55:15,595 address of our var here in memory 13042 09:55:13,595 --> 09:55:17,595 pointers and variables we've been 13043 09:55:15,595 --> 09:55:19,595 talking about can work hand in hand and 13044 09:55:17,595 --> 09:55:21,435 we're going to be seeing how this is 13045 09:55:19,595 --> 09:55:24,399 done in the next few lectures in the 13046 09:55:21,436 --> 09:55:26,959 chapter but the basic idea is really the 13047 09:55:24,400 --> 09:55:29,120 same here you see that we have a pointer 13048 09:55:26,959 --> 09:55:30,959 to it but you can have a pointer ready 13049 09:55:29,120 --> 09:55:33,436 to anything in your code you can have a 13050 09:55:30,959 --> 09:55:35,839 pointer to car you can have a pointer to 13051 09:55:33,436 --> 09:55:39,120 double you can have a pointer to float 13052 09:55:35,839 --> 09:55:40,879 even pointers to custom types we haven't 13053 09:55:39,120 --> 09:55:42,880 really talked about now but we will 13054 09:55:40,879 --> 09:55:45,519 learn about them later in the course 13055 09:55:42,879 --> 09:55:48,639 again a pointer is a special type of 13056 09:55:45,519 --> 09:55:51,119 variable in your c plus program that is 13057 09:55:48,639 --> 09:55:54,000 going to store addresses to other 13058 09:55:51,120 --> 09:55:55,680 variables that's what it really is and 13059 09:55:54,000 --> 09:55:58,080 we're going to start and see how you can 13060 09:55:55,680 --> 09:56:00,800 declare initialize and use them in your 13061 09:55:58,080 --> 09:56:03,120 code starting in the next lecture 13062 09:56:00,800 --> 09:56:04,000 go ahead and finish up here and meet me 13063 09:56:04,000 --> 09:56:08,720 in this lecture we're going to see how 13064 09:56:05,595 --> 09:56:10,879 we can declare and use pointers in our c 13065 09:56:08,720 --> 09:56:13,436 plus plus code in the last lecture we 13066 09:56:10,879 --> 09:56:14,479 say that a pointer is a special kind of 13067 09:56:14,480 --> 09:56:20,000 that can store addresses to other 13068 09:56:17,120 --> 09:56:22,080 variables in our c plus plus code and we 13069 09:56:20,000 --> 09:56:24,160 have seen a slide like this to drive the 13070 09:56:22,080 --> 09:56:27,040 point home now we're going to see how we 13071 09:56:24,160 --> 09:56:29,596 can actually use pointers in our code 13072 09:56:27,040 --> 09:56:32,000 here is a simple example of how you can 13073 09:56:29,595 --> 09:56:34,559 declare a pointer variable in your c 13074 09:56:32,000 --> 09:56:36,959 plus plus code you say ent you put a 13075 09:56:36,959 --> 09:56:41,680 to mean that this variable is not a 13076 09:56:39,519 --> 09:56:45,359 regular variable like we have seen 13077 09:56:41,680 --> 09:56:47,120 before it is a special kind of variable 13078 09:56:47,120 --> 09:56:51,596 addresses to other variables and here i 13079 09:56:49,519 --> 09:56:54,959 have to say that you have to be really 13080 09:56:51,595 --> 09:56:57,595 careful and only store addresses to 13081 09:56:57,595 --> 09:57:02,639 in your pointers for example this is a 13082 09:57:00,000 --> 09:57:05,595 pointer to end it can only store an 13083 09:57:02,639 --> 09:57:08,239 address of a variable of type and for 13084 09:57:05,595 --> 09:57:10,319 other types if you try and put in their 13085 09:57:08,239 --> 09:57:12,559 addresses in here you're going to get a 13086 09:57:10,319 --> 09:57:14,959 compiler error down here you see we have 13087 09:57:12,559 --> 09:57:17,119 another example and it's called p 13088 09:57:14,959 --> 09:57:19,519 fractional number because it's a double 13089 09:57:17,120 --> 09:57:22,959 and you see that our pointers here are 13090 09:57:19,519 --> 09:57:24,639 initialized using braced initializers 13091 09:57:22,959 --> 09:57:28,000 and this is going to initialize the 13092 09:57:24,639 --> 09:57:30,080 pointers with a special address that 13093 09:57:28,000 --> 09:57:32,559 really denotes that these pointers are 13094 09:57:30,080 --> 09:57:35,916 not pointing to anything yet okay you 13095 09:57:32,559 --> 09:57:38,399 can also say that explicitly and put now 13096 09:57:35,915 --> 09:57:41,040 ptr inside and this is going to mean 13097 09:57:38,400 --> 09:57:43,275 that these pointers are not pointing 13098 09:57:41,040 --> 09:57:46,160 anywhere you can't really use the values 13099 09:57:43,275 --> 09:57:48,799 at the addresses where they point this 13100 09:57:46,160 --> 09:57:51,756 is the message here don't use pointers 13101 09:57:48,800 --> 09:57:54,240 that contain null pointer all pointers 13102 09:57:51,756 --> 09:57:56,800 on a given system are going to have the 13103 09:57:54,239 --> 09:57:59,436 same size for example if you have a 13104 09:57:56,800 --> 09:58:01,040 pointer to end and the pointer to double 13105 09:57:59,436 --> 09:58:03,596 they are going to be the same size 13106 09:58:01,040 --> 09:58:06,239 because they really store the same thing 13107 09:58:03,595 --> 09:58:08,879 they store addresses to variables and 13108 09:58:06,239 --> 09:58:10,799 addresses to different variables even if 13109 09:58:08,879 --> 09:58:13,519 they happen to be of the same type on 13110 09:58:10,800 --> 09:58:15,840 the same system are of the same size 13111 09:58:13,519 --> 09:58:17,519 that's why all the pointers you're going 13112 09:58:15,839 --> 09:58:19,275 to use in your code are going to have 13113 09:58:17,519 --> 09:58:21,519 the same size and this is something you 13114 09:58:19,275 --> 09:58:23,680 should keep in mind and here is a simple 13115 09:58:21,519 --> 09:58:26,799 example to really play with that we have 13116 09:58:23,680 --> 09:58:28,959 a size of it printed out we have a size 13117 09:58:26,800 --> 09:58:30,720 of our pointers printed out and we're 13118 09:58:28,959 --> 09:58:32,639 going to compare these things when we 13119 09:58:30,720 --> 09:58:33,756 get to visual studio code in a minute 13120 09:58:32,639 --> 09:58:36,479 and you're going to see that all 13121 09:58:33,756 --> 09:58:39,200 pointers are of the same size a question 13122 09:58:36,480 --> 09:58:42,080 i get from many people is if the 13123 09:58:42,080 --> 09:58:47,276 really matters and i have to break it to 13124 09:58:44,319 --> 09:58:49,275 you it doesn't but i prefer to put it on 13125 09:58:47,275 --> 09:58:51,680 the left some people prefer to put it on 13126 09:58:49,275 --> 09:58:53,680 the right but definitely don't put it in 13127 09:58:51,680 --> 09:58:55,756 the middle i've never seen anybody do 13128 09:58:53,680 --> 09:58:58,559 this but it's going to work it's going 13129 09:58:55,756 --> 09:59:00,239 to be legal c plus plus code another 13130 09:58:58,559 --> 09:59:03,119 thing i see people do which is really 13131 09:59:00,239 --> 09:59:05,680 confusing is to declare your pointers on 13132 09:59:03,120 --> 09:59:08,400 the same line and do a comma like this 13133 09:59:05,680 --> 09:59:11,595 this is really going to be confusing and 13134 09:59:08,400 --> 09:59:15,120 now i want you to guess and see what is 13135 09:59:11,595 --> 09:59:17,915 happening here is either it var a 13136 09:59:15,120 --> 09:59:19,520 pointer or it's a regular end i want you 13137 09:59:17,915 --> 09:59:21,519 to keep this in mind i'm not going to 13138 09:59:19,519 --> 09:59:23,595 tell you we're going to try this out 13139 09:59:21,519 --> 09:59:25,680 actually in visual studio code and we're 13140 09:59:23,595 --> 09:59:27,680 going to figure this out together but 13141 09:59:25,680 --> 09:59:29,275 now i want you to try and think about it 13142 09:59:27,680 --> 09:59:31,275 we're going to leave this question 13143 09:59:29,275 --> 09:59:33,680 unanswered and we're going to answer it 13144 09:59:31,275 --> 09:59:35,756 in a little while but for now 13145 09:59:33,680 --> 09:59:37,840 don't really do this because it's 13146 09:59:35,756 --> 09:59:40,160 confusing and it's going to make your 13147 09:59:37,839 --> 09:59:42,000 code less readable if you have to do 13148 09:59:40,160 --> 09:59:44,160 something like this make sure you just 13149 09:59:42,000 --> 09:59:46,480 put them on different lines and it's not 13150 09:59:44,160 --> 09:59:48,320 going to be confusing for example if you 13151 09:59:46,480 --> 09:59:50,480 look down here you're going to know that 13152 09:59:48,319 --> 09:59:52,959 p number seven is a pointer other 13153 09:59:50,480 --> 09:59:55,756 interval is a regular end and there is 13154 09:59:52,959 --> 09:59:57,839 no room for confusion this time use code 13155 09:59:55,756 --> 10:00:00,800 like this and don't put things on the 13156 09:59:57,839 --> 10:00:02,959 same line like we have on top here okay 13157 10:00:00,800 --> 10:00:06,319 now we have seen that we can declare and 13158 10:00:02,959 --> 10:00:09,275 initialize pointers with null pointer 13159 10:00:06,319 --> 10:00:12,000 now is the time to see how we can put 13160 10:00:09,275 --> 10:00:13,915 actual data in those pointer variables 13161 10:00:12,000 --> 10:00:17,120 here we have a variable which is called 13162 10:00:13,915 --> 10:00:18,559 into var it is of type and and its value 13163 10:00:18,559 --> 10:00:23,040 we can also set up another pointer 13164 10:00:25,275 --> 10:00:31,680 underscore int and we can initialize it 13165 10:00:28,559 --> 10:00:34,000 with the address of another variable and 13166 10:00:31,680 --> 10:00:37,120 here is a new operator we haven't seen 13167 10:00:34,000 --> 10:00:39,595 before this ampersand variable here 13168 10:00:37,120 --> 10:00:41,360 really means the address of this ant 13169 10:00:39,595 --> 10:00:43,595 variable that's what we mean here so 13170 10:00:41,360 --> 10:00:46,319 we're going to initialize this p end 13171 10:00:43,595 --> 10:00:48,559 variable with the address of enter 13172 10:00:46,319 --> 10:00:50,720 variable and this is how we do it 13173 10:00:48,559 --> 10:00:52,239 if we go down here we print antivirus 13174 10:00:50,720 --> 10:00:55,275 we're going to get the value printed 13175 10:00:52,239 --> 10:00:57,199 down if we print the pointer directly 13176 10:00:55,275 --> 10:00:59,519 we're going to get the address of this 13177 10:00:57,199 --> 10:01:01,595 little guy printed out on the console 13178 10:00:59,519 --> 10:01:03,680 and we're going to see it okay so this 13179 10:01:01,595 --> 10:01:05,519 is how you can store things in your 13180 10:01:03,680 --> 10:01:08,080 pointer variables and what you have to 13181 10:01:05,519 --> 10:01:09,435 store and has to be an address of 13182 10:01:09,436 --> 10:01:13,120 down here you see we have another 13183 10:01:10,800 --> 10:01:16,560 variable called into r1 inside we have a 13184 10:01:13,120 --> 10:01:19,200 65 and if you go down here you see that 13185 10:01:16,559 --> 10:01:21,915 we are storing something else 13186 10:01:19,199 --> 10:01:24,399 in our point of variable here we had in 13187 10:01:21,915 --> 10:01:27,435 the address of into var now we are 13188 10:01:24,400 --> 10:01:29,916 putting in the address of into var1 13189 10:01:27,436 --> 10:01:32,080 using an assignment operator and this is 13190 10:01:29,915 --> 10:01:34,639 something you can definitely do it's 13191 10:01:32,080 --> 10:01:36,400 legal c plus plus it's going to work if 13192 10:01:34,639 --> 10:01:38,639 we print it out we're going to get a 13193 10:01:36,400 --> 10:01:41,200 different address printed out this is 13194 10:01:38,639 --> 10:01:44,159 how you can store values in your pointer 13195 10:01:41,199 --> 10:01:46,799 variables again you can't cross a sign 13196 10:01:44,160 --> 10:01:48,840 before different pointer types here we 13197 10:01:46,800 --> 10:01:51,360 have an eight pointer which is called 13198 10:01:48,839 --> 10:01:53,755 pn1 and we have a double variable it's 13199 10:01:51,360 --> 10:01:56,879 called double var we can't take the 13200 10:01:53,756 --> 10:01:59,200 address of double var and store that in 13201 10:01:56,879 --> 10:02:01,915 pnt1 this is going to be a compiler 13202 10:01:59,199 --> 10:02:05,680 error the variables whose addresses you 13203 10:02:01,915 --> 10:02:07,839 store in payment 1 have to be of type 13204 10:02:05,680 --> 10:02:10,559 and again the basic idea is that a 13205 10:02:07,839 --> 10:02:12,959 pointer is really a special kind of 13206 10:02:10,559 --> 10:02:15,839 variable that is going to store 13207 10:02:12,959 --> 10:02:18,479 addresses to other variables and if your 13208 10:02:18,480 --> 10:02:23,756 the variables whose addresses you can 13209 10:02:20,720 --> 10:02:26,400 store in this pointer to end can only be 13210 10:02:23,756 --> 10:02:27,595 ends if you try and put in doubles 13211 10:02:26,400 --> 10:02:29,520 you're going to be in trouble you're 13212 10:02:27,595 --> 10:02:31,519 going to get a compiler error and it's 13213 10:02:29,519 --> 10:02:33,595 not going to work now we're going to 13214 10:02:31,519 --> 10:02:36,239 head over to visual studio code and 13215 10:02:33,595 --> 10:02:39,360 actually play with these things here we 13216 10:02:36,239 --> 10:02:41,756 are in our working directory the current 13217 10:02:39,360 --> 10:02:43,915 chapter is pointers the current lecture 13218 10:02:41,756 --> 10:02:46,160 is declaring and using pointers we're 13219 10:02:43,915 --> 10:02:48,000 going to grab our template files and put 13220 10:02:48,000 --> 10:02:52,720 so put them in our project here and 13221 10:02:51,120 --> 10:02:54,400 we're going to open this in visual 13222 10:02:52,720 --> 10:02:56,559 studio code the first thing we're going 13223 10:02:56,559 --> 10:03:00,799 and initialize pointers and we're going 13224 10:02:58,800 --> 10:03:02,720 to use a pointer to end as a first 13225 10:03:00,800 --> 10:03:05,200 example we're going to set up an inch 13226 10:03:02,720 --> 10:03:06,480 pointer and we say in store we're going 13227 10:03:05,199 --> 10:03:09,199 to give it a name we're going to call 13228 10:03:06,480 --> 10:03:11,840 this p number and you can leave it like 13229 10:03:09,199 --> 10:03:14,159 this uninitialized but it is really 13230 10:03:11,839 --> 10:03:16,559 advised to always initialize your 13231 10:03:14,160 --> 10:03:19,276 pointers so we're going to do that with 13232 10:03:16,559 --> 10:03:21,839 a brist initializer which is going to do 13233 10:03:19,275 --> 10:03:24,239 that for us this is going to implicitly 13234 10:03:21,839 --> 10:03:26,399 initialize it with zero equivalent of 13235 10:03:24,239 --> 10:03:29,680 pointers and that happens to be null 13236 10:03:26,400 --> 10:03:31,360 pointer so we'll initialize okay so this 13237 10:03:29,680 --> 10:03:32,080 is what we're going to initialize with 13238 10:03:34,720 --> 10:03:39,680 and this is how you should do things you 13239 10:03:37,436 --> 10:03:41,520 can also do a pointer to double why not 13240 10:03:39,680 --> 10:03:44,080 let's do double pointer and we're going 13241 10:03:41,519 --> 10:03:45,915 to say fractional number pointer to 13242 10:03:44,080 --> 10:03:48,959 fractional number and we can also 13243 10:03:45,915 --> 10:03:50,399 initialize this with null pointer 13244 10:03:51,275 --> 10:03:57,119 once you do this the program is going to 13245 10:03:53,199 --> 10:03:59,275 be legal if we try to world at with gcc 13246 10:03:57,120 --> 10:04:01,436 like we always do the build is going to 13247 10:03:59,275 --> 10:04:03,680 be good but we don't really have any 13248 10:04:03,680 --> 10:04:07,680 if we run this program we are just 13249 10:04:05,595 --> 10:04:10,720 getting started we will get to see 13250 10:04:07,680 --> 10:04:13,680 things later for now just understand 13251 10:04:10,720 --> 10:04:16,400 that p number is a variable we have in 13252 10:04:13,680 --> 10:04:19,360 our code this variable can only store 13253 10:04:16,400 --> 10:04:22,800 addresses to other variables that happen 13254 10:04:19,360 --> 10:04:25,436 to be of type and if you try and store 13255 10:04:22,800 --> 10:04:27,916 in an address of a variable which is of 13256 10:04:25,436 --> 10:04:29,200 type double for example or flute you're 13257 10:04:27,915 --> 10:04:31,435 going to get a compiler error you 13258 10:04:29,199 --> 10:04:34,079 shouldn't do that the same for p 13259 10:04:31,436 --> 10:04:36,880 fractional number it can only store 13260 10:04:34,080 --> 10:04:39,596 addresses to other variables which 13261 10:04:36,879 --> 10:04:41,435 happen to be of type double keep this in 13262 10:04:41,436 --> 10:04:47,120 explicitly initialize with no pointer so 13263 10:04:44,319 --> 10:04:49,199 we can say end pointer and say p number 13264 10:04:47,120 --> 10:04:51,520 one why not and we're going to 13265 10:04:51,519 --> 10:04:56,479 ptr like this and this is going to work 13266 10:04:59,756 --> 10:05:05,120 one why not and say now pqr this is 13267 10:05:03,436 --> 10:05:06,720 going to work this is something you can 13268 10:05:05,120 --> 10:05:08,720 do you're going to see that if we 13269 10:05:06,720 --> 10:05:11,756 compile this the compiler is going to be 13270 10:05:08,720 --> 10:05:13,360 happy with this the world is good okay 13271 10:05:11,756 --> 10:05:15,360 now you know how you can declare and 13272 10:05:13,360 --> 10:05:16,080 initialize your pointers now we want to 13273 10:05:16,080 --> 10:05:21,120 that pointers to different variables are 13274 10:05:21,120 --> 10:05:25,840 on whatever system you are working on 13275 10:05:23,595 --> 10:05:28,479 it's not consistent across operating 13276 10:05:25,839 --> 10:05:31,359 systems or riffing systems but it is 13277 10:05:28,480 --> 10:05:32,560 guaranteed to be of the same size on the 13278 10:05:32,559 --> 10:05:36,799 i am going to go down and put in a piece 13279 10:05:34,879 --> 10:05:39,040 of code that is going to show us all 13280 10:05:36,800 --> 10:05:41,276 these things on the console and i am 13281 10:05:39,040 --> 10:05:43,040 going to click on this file icon here 13282 10:05:43,040 --> 10:05:46,959 some breathing room so that we can see 13283 10:05:45,040 --> 10:05:48,959 the entire thing here if you look here 13284 10:05:46,959 --> 10:05:51,595 we are printing the size of ant we know 13285 10:05:48,959 --> 10:05:53,519 that this is going to be a four 13286 10:05:51,595 --> 10:05:55,915 size of double this is going to be an 13287 10:05:53,519 --> 10:05:58,239 eight on our system here but we don't 13288 10:05:55,915 --> 10:06:00,799 know what the size of a double pointer 13289 10:05:58,239 --> 10:06:03,119 is or the size of an end pointer and 13290 10:06:00,800 --> 10:06:04,960 we're going to see that in a minute here 13291 10:06:03,120 --> 10:06:06,880 you see that we are also using the 13292 10:06:04,959 --> 10:06:08,959 pointer variables immediately this is 13293 10:06:06,879 --> 10:06:11,275 something you can do you see that visual 13294 10:06:08,959 --> 10:06:14,799 studio code is not complaining and we 13295 10:06:11,275 --> 10:06:16,400 can go on top here and boulder with gcc 13296 10:06:16,400 --> 10:06:21,680 the world is going to be good now we can 13297 10:06:19,360 --> 10:06:23,520 open up a terminal and actually run this 13298 10:06:21,680 --> 10:06:24,800 program here we're going to clear if we 13299 10:06:27,756 --> 10:06:32,239 our directory here so you see that we 13300 10:06:32,239 --> 10:06:35,519 and we can run this and it's going to 13301 10:06:35,519 --> 10:06:39,680 sizeofant is four size of double is 13302 10:06:39,680 --> 10:06:47,120 this is expected but pointer types are 13303 10:06:43,360 --> 10:06:49,915 of size 8 on my system they occupy 8 13304 10:06:47,120 --> 10:06:52,400 bytes in memory so it doesn't matter the 13305 10:06:49,915 --> 10:06:55,435 type we are pointing to a pointer to it 13306 10:06:52,400 --> 10:06:57,756 is always going to be 8 bytes in memory 13307 10:06:55,436 --> 10:06:59,756 this is something you should really know 13308 10:06:57,756 --> 10:07:01,916 and keep in your mind another thing we 13309 10:06:59,756 --> 10:07:02,959 have said that i want to play with right 13310 10:07:02,959 --> 10:07:07,040 is the location of the star symbol some 13311 10:07:05,275 --> 10:07:08,639 people say you should put it on the left 13312 10:07:07,040 --> 10:07:11,360 some people say you should put it to the 13313 10:07:08,639 --> 10:07:13,519 right i don't really think any of these 13314 10:07:11,360 --> 10:07:15,360 dense matters i prefer to put it to the 13315 10:07:13,519 --> 10:07:17,839 left but if you put it to the right it's 13316 10:07:15,360 --> 10:07:19,436 also possible it's going to work you see 13317 10:07:17,839 --> 10:07:21,359 that even if you put it in the middle 13318 10:07:19,436 --> 10:07:23,756 here which is really crazy the compiler 13319 10:07:21,360 --> 10:07:25,520 is going to take this we can run the 13320 10:07:23,756 --> 10:07:27,120 task to build this to make sure the 13321 10:07:25,519 --> 10:07:29,039 compiler is really happy and you see 13322 10:07:27,120 --> 10:07:31,200 that there's no problem the compiler is 13323 10:07:29,040 --> 10:07:34,000 going to accept this and we are going to 13324 10:07:31,199 --> 10:07:36,479 build successfully if you put your 13325 10:07:34,000 --> 10:07:39,199 declaration on a single line like this 13326 10:07:36,480 --> 10:07:41,436 what is other ant var going to be is it 13327 10:07:39,199 --> 10:07:43,755 going to be a regular end variable is it 13328 10:07:41,436 --> 10:07:45,756 going to be a pointer to end 13329 10:07:43,756 --> 10:07:47,680 we don't really know now and i don't 13330 10:07:45,756 --> 10:07:49,436 want to tell you we're going to find out 13331 10:07:47,680 --> 10:07:52,080 together so what we're going to do we're 13332 10:07:49,436 --> 10:07:54,000 going to print the size of these things 13333 10:07:52,080 --> 10:07:56,319 i am going to go down here and put in 13334 10:07:54,000 --> 10:07:59,199 the code and come back in a minute here 13335 10:07:56,319 --> 10:08:01,839 is our code typed out and we're going to 13336 10:07:59,199 --> 10:08:04,239 see the size of these things what we're 13337 10:08:01,839 --> 10:08:07,519 going to do is run the task to build 13338 10:08:04,239 --> 10:08:08,799 with gcc as we always do let's clear 13339 10:08:10,720 --> 10:08:15,595 and you see size of p number five is 13340 10:08:15,595 --> 10:08:20,879 because it's a pointer size of either 13341 10:08:17,756 --> 10:08:22,639 end is four and the same applies to the 13342 10:08:22,639 --> 10:08:28,000 so from this we can tell that other 13343 10:08:24,639 --> 10:08:30,639 interval and other ant versus x are not 13344 10:08:28,000 --> 10:08:34,559 pointers they are regular integers and 13345 10:08:30,639 --> 10:08:37,519 this can be really confusing so to avoid 13346 10:08:34,559 --> 10:08:38,559 this confusion i would advise not to 13347 10:08:38,559 --> 10:08:42,239 your thanks when you have pointers 13348 10:08:40,480 --> 10:08:44,560 inside like this because that's going to 13349 10:08:42,239 --> 10:08:46,559 be really confusing what you can do is 13350 10:08:44,559 --> 10:08:49,040 put them on separate lines and do 13351 10:08:46,559 --> 10:08:51,595 something like this so here it is very 13352 10:08:49,040 --> 10:08:54,000 clear that p number seven is a pointer 13353 10:08:51,595 --> 10:08:55,915 other eight var seven is a regular it 13354 10:08:54,000 --> 10:08:58,959 variable and there is no room for 13355 10:08:55,915 --> 10:09:00,639 confusion with this way of doing that so 13356 10:08:58,959 --> 10:09:02,479 make sure you put things on separate 13357 10:09:00,639 --> 10:09:05,199 lines like this and you're going to be 13358 10:09:02,480 --> 10:09:07,436 fine now what i want you to see is that 13359 10:09:05,199 --> 10:09:10,239 we can actually store data in all 13360 10:09:07,436 --> 10:09:12,400 pointers i mean valid data that is not 13361 10:09:10,239 --> 10:09:14,720 null pointer in this case we're going to 13362 10:09:12,400 --> 10:09:17,275 set up a variable a regular end variable 13363 10:09:14,720 --> 10:09:19,200 it's going to store a 43 inside and 13364 10:09:17,275 --> 10:09:22,239 we're going to set up another pointer 13365 10:09:19,199 --> 10:09:24,639 and initialize that with the address of 13366 10:09:22,239 --> 10:09:27,360 this it variable and this is going to 13367 10:09:24,639 --> 10:09:29,199 work properly what we can do now 13368 10:09:27,360 --> 10:09:31,360 is print the interval we're going to 13369 10:09:29,199 --> 10:09:33,519 print it out if we try to print the 13370 10:09:31,360 --> 10:09:36,480 pointer we're going to get the address 13371 10:09:33,519 --> 10:09:38,559 of this into var in memory and this is 13372 10:09:36,480 --> 10:09:40,400 something you need to see with your eyes 13373 10:09:38,559 --> 10:09:42,639 this is the first time we are printing 13374 10:09:40,400 --> 10:09:44,720 out an address that is stored in a 13375 10:09:42,639 --> 10:09:46,879 pointer and we're going to see that on 13376 10:09:44,720 --> 10:09:49,200 the console here what i am going to do 13377 10:09:46,879 --> 10:09:50,879 before we build and run this i am going 13378 10:09:49,199 --> 10:09:53,119 to comment out everything we've done 13379 10:09:50,879 --> 10:09:54,319 before because our console is now 13380 10:09:54,319 --> 10:10:01,519 so let's come and comment out everything 13381 10:09:58,959 --> 10:10:03,360 and let's go down again now if we print 13382 10:10:01,519 --> 10:10:07,119 we're going to see these two statements 13383 10:10:03,360 --> 10:10:09,595 and that's going to be easier to follow 13384 10:10:07,120 --> 10:10:11,200 on the console here the build is good so 13385 10:10:13,275 --> 10:10:18,720 and the var is 43 and its address in 13386 10:10:16,080 --> 10:10:21,360 memory is this number here it is in 13387 10:10:18,720 --> 10:10:24,400 hexadecimal so this is the address in 13388 10:10:21,360 --> 10:10:26,639 memory and we can print that out and see 13389 10:10:24,400 --> 10:10:29,120 what it is okay so this is really cool 13390 10:10:26,639 --> 10:10:31,275 but we can also change what is stored in 13391 10:10:29,120 --> 10:10:34,240 our pointer here for example we can set 13392 10:10:31,275 --> 10:10:37,756 up another variable call it into our one 13393 10:10:34,239 --> 10:10:40,319 and uh change pnt which is a pointer to 13394 10:10:37,756 --> 10:10:42,880 contain the address of this new variable 13395 10:10:40,319 --> 10:10:44,879 and we can do that now if we print 13396 10:10:42,879 --> 10:10:46,799 payment we're going to see that it 13397 10:10:44,879 --> 10:10:48,239 contains a different address and we can 13398 10:10:48,239 --> 10:10:53,275 so let's build with gcc again 13399 10:10:51,275 --> 10:10:55,360 we're going to go down and clear and run 13400 10:10:53,275 --> 10:10:57,519 rooster and we're going to see that now 13401 10:10:55,360 --> 10:11:01,756 we have a different address the one we 13402 10:10:57,519 --> 10:11:04,239 had ended in d e4 now we end in de0 13403 10:11:01,756 --> 10:11:06,559 these are different addresses and we are 13404 10:11:04,239 --> 10:11:08,239 storing different things in our pointer 13405 10:11:06,559 --> 10:11:10,559 this is really cool you can do something 13406 10:11:08,239 --> 10:11:14,000 like this and the next thing we want to 13407 10:11:10,559 --> 10:11:16,959 see now is that we can't cross a sign 13408 10:11:14,000 --> 10:11:19,275 between pointers of different types and 13409 10:11:16,959 --> 10:11:21,839 what i mean here is that for example if 13410 10:11:19,275 --> 10:11:24,479 we have two variables here one is a 13411 10:11:21,839 --> 10:11:26,639 pointer to end it is initialized with no 13412 10:11:24,480 --> 10:11:28,560 pointer and we have another variable 13413 10:11:26,639 --> 10:11:30,000 which is a double var it's going to be 13414 10:11:30,000 --> 10:11:37,360 and we can't do something like payment 13415 10:11:33,915 --> 10:11:38,959 equals address of double bar you can't 13416 10:11:37,360 --> 10:11:40,879 do something like this if you do it 13417 10:11:38,959 --> 10:11:43,436 you're going to get a compiler error and 13418 10:11:40,879 --> 10:11:45,360 you see that we have squiggly lines 13419 10:11:43,436 --> 10:11:47,596 in visual studio code this is a good 13420 10:11:45,360 --> 10:11:49,915 indication that something might be wrong 13421 10:11:47,595 --> 10:11:52,559 if we open the problems tab we're going 13422 10:11:49,915 --> 10:11:54,799 to say a value of double pointer cannot 13423 10:11:52,559 --> 10:11:57,839 be assigned to an entity of type 8 13424 10:11:54,800 --> 10:11:59,436 pointer so this is not something you can 13425 10:11:57,839 --> 10:12:02,559 do if you do this you're going to get a 13426 10:11:59,436 --> 10:12:05,275 compiler error be aware of this visual 13427 10:12:02,559 --> 10:12:07,435 studio code has warned us enough but we 13428 10:12:07,436 --> 10:12:12,720 a clear compiler error by building this 13429 10:12:10,239 --> 10:12:14,879 with gcc and we're going to get 13430 10:12:12,720 --> 10:12:16,800 basically the same thing you can't 13431 10:12:14,879 --> 10:12:20,239 cross-assign between pointers of 13432 10:12:16,800 --> 10:12:22,000 different types we are trying to assign 13433 10:12:22,000 --> 10:12:26,639 into a pointer to enter and this is not 13434 10:12:24,239 --> 10:12:28,319 going to work so let's comment this out 13435 10:12:26,639 --> 10:12:31,199 because we don't want this compiler 13436 10:12:28,319 --> 10:12:33,436 error now you know this and before we 13437 10:12:31,199 --> 10:12:36,239 close this lecture i want to mention 13438 10:12:33,436 --> 10:12:39,520 what is called the referencing and that 13439 10:12:36,239 --> 10:12:41,680 is the act of reading something 13440 10:12:39,519 --> 10:12:43,680 through a pointer so if we have an 13441 10:12:41,680 --> 10:12:46,480 address in a pointer we can go through 13442 10:12:43,680 --> 10:12:48,800 that address and actually read the value 13443 10:12:46,480 --> 10:12:51,040 contained in that address and we can do 13444 10:12:48,800 --> 10:12:54,160 something like this so if we have for 13445 10:12:51,040 --> 10:12:57,275 example an end pointer let's call it p 13446 10:12:54,160 --> 10:12:59,436 and two do we have a variable like this 13447 10:12:57,275 --> 10:13:02,479 so let's say we initialize this with 13448 10:12:59,436 --> 10:13:06,400 nodepointer and we have an end 13449 10:13:02,480 --> 10:13:09,436 variable and this is a 56 for example 13450 10:13:06,400 --> 10:13:12,400 and we do something like we do say 13451 10:13:09,436 --> 10:13:14,560 pnt2 we're going to make it contain the 13452 10:13:12,400 --> 10:13:16,959 address of int data this is something 13453 10:13:14,559 --> 10:13:19,680 you know you can do now now we want to 13454 10:13:16,959 --> 10:13:23,199 go through pm to 2 to read what is 13455 10:13:19,680 --> 10:13:26,400 contained in interdata because pn 2 13456 10:13:23,199 --> 10:13:29,915 contains the address of our integer here 13457 10:13:26,400 --> 10:13:32,560 so what we can do we can do stdc out and 13458 10:13:29,915 --> 10:13:35,040 say value and the syntax we use in c 13459 10:13:32,559 --> 10:13:37,119 plus plus is like this we say store and 13460 10:13:35,040 --> 10:13:40,160 we say our pointer variable we're going 13461 10:13:37,120 --> 10:13:42,639 to say pnt2 and now if we run we're 13462 10:13:40,160 --> 10:13:44,560 going to get the value here printed out 13463 10:13:42,639 --> 10:13:48,319 okay now if we try to build we're going 13464 10:13:44,559 --> 10:13:49,839 to build fine let's try with gcc 13465 10:13:53,519 --> 10:13:59,680 a previous run are cleared out 13466 10:13:56,800 --> 10:14:01,200 you see the bullet is good we can now 13467 10:14:01,199 --> 10:14:05,119 and we're going to see that value is 56 13468 10:14:05,120 --> 10:14:10,240 reading the value stored in the address 13469 10:14:07,680 --> 10:14:12,959 pointed to by our pointer here and this 13470 10:14:10,239 --> 10:14:14,799 is the syntax used and this is called 13471 10:14:12,959 --> 10:14:17,436 the referencing a pointer we are 13472 10:14:14,800 --> 10:14:19,360 basically reading something stored in 13473 10:14:17,436 --> 10:14:21,520 the address of a pointer this is what we 13474 10:14:19,360 --> 10:14:23,275 mean here this is really all we set out 13475 10:14:21,519 --> 10:14:26,399 to do in this lecture the lecture turned 13476 10:14:23,275 --> 10:14:29,199 out to be lengthy but we had a lot of 13477 10:14:26,400 --> 10:14:31,680 ground to cover and now i hope you have 13478 10:14:29,199 --> 10:14:34,479 a good grasp on how to declare 13479 10:14:31,680 --> 10:14:36,559 initialize and use your pointers to do 13480 10:14:36,559 --> 10:14:40,159 we are going to stop here in this 13481 10:14:38,239 --> 10:14:42,479 lecture in the next one we're going to 13482 10:14:40,160 --> 10:14:45,040 learn a little more about pointer to 13483 10:14:42,480 --> 10:14:46,160 character types because they are also 13484 10:14:46,160 --> 10:14:50,560 go ahead and finish up here and meet me 13485 10:14:48,480 --> 10:14:53,596 there in this lecture we're going to 13486 10:14:50,559 --> 10:14:55,756 explore pointer to characters 13487 10:14:53,595 --> 10:14:58,159 and you can declare a pointer to a 13488 10:14:55,756 --> 10:14:59,200 character just like this we have car 13489 10:14:59,199 --> 10:15:04,239 and we say p carvar to give it a 13490 10:15:02,080 --> 10:15:06,880 variable name we initialize this with a 13491 10:15:04,239 --> 10:15:09,360 null pointer and after that we set up a 13492 10:15:06,879 --> 10:15:12,879 character variable and it is called 13493 10:15:09,360 --> 10:15:16,400 corvar and we store in an a in uppercase 13494 10:15:12,879 --> 10:15:19,275 a after that we take the address of car 13495 10:15:16,400 --> 10:15:21,680 of r and assign that to our pointer 13496 10:15:19,275 --> 10:15:23,915 variable this is how we store data in a 13497 10:15:21,680 --> 10:15:26,720 pointer and we can go through this 13498 10:15:23,915 --> 10:15:28,879 pointer to access data in this character 13499 10:15:26,720 --> 10:15:30,880 variable if we do something like this 13500 10:15:28,879 --> 10:15:33,360 we're going to print a down here we 13501 10:15:30,879 --> 10:15:34,879 declare another variable we store in a c 13502 10:15:34,879 --> 10:15:40,479 assign the address of this new character 13503 10:15:37,756 --> 10:15:42,959 variable to our pointer variable and if 13504 10:15:40,480 --> 10:15:45,040 we print this again we're going to get a 13505 10:15:42,959 --> 10:15:46,720 c printed out this is really what we 13506 10:15:45,040 --> 10:15:48,720 have done in the last lecture we are 13507 10:15:46,720 --> 10:15:51,120 just extending that to character 13508 10:15:48,720 --> 10:15:53,520 pointers but with character pointers you 13509 10:15:51,120 --> 10:15:55,200 can do something really special what you 13510 10:15:53,519 --> 10:15:58,000 can do that is special you can 13511 10:15:55,199 --> 10:16:00,159 initialize that with a string literal 13512 10:15:58,000 --> 10:16:03,040 something like hello world which is 13513 10:16:00,160 --> 10:16:05,040 wrapped in quotes like this when you do 13514 10:16:03,040 --> 10:16:07,840 something like this the string here is 13515 10:16:05,040 --> 10:16:11,040 going to be expanded into a character 13516 10:16:07,839 --> 10:16:14,079 array and the first character of our 13517 10:16:11,040 --> 10:16:16,800 array is going to be pointed to by p 13518 10:16:14,080 --> 10:16:18,880 message here this is what we mean i 13519 10:16:16,800 --> 10:16:21,436 realize i might be throwing a lot of 13520 10:16:18,879 --> 10:16:23,755 things at you right now i am mixing up 13521 10:16:21,436 --> 10:16:25,916 arrays and pointers but they have a 13522 10:16:23,756 --> 10:16:28,000 really strong relationship and we're 13523 10:16:25,915 --> 10:16:30,639 going to learn about that in detail in a 13524 10:16:30,639 --> 10:16:34,879 think of it like this this hello world 13525 10:16:33,199 --> 10:16:37,595 string is going to be turned into a 13526 10:16:34,879 --> 10:16:40,239 character array and the p message is 13527 10:16:37,595 --> 10:16:42,399 going to point to the first character 13528 10:16:40,239 --> 10:16:44,400 in this array here this is what is going 13529 10:16:42,400 --> 10:16:46,480 to happen and this is going to give us 13530 10:16:44,400 --> 10:16:49,840 the flexibility of treating our 13531 10:16:46,480 --> 10:16:51,756 strengths as character pointers and this 13532 10:16:49,839 --> 10:16:52,959 is going to come in handy at many 13533 10:16:52,959 --> 10:16:57,199 one thing i should tell you though is 13534 10:16:54,800 --> 10:17:00,080 that some compilers will refuse to 13535 10:16:57,199 --> 10:17:02,559 compile this code for example visual 13536 10:17:00,080 --> 10:17:04,720 studio 19 that i have installed on my 13537 10:17:02,559 --> 10:17:07,199 computer right now is going to refuse to 13538 10:17:04,720 --> 10:17:09,360 compile this code and the reason is the 13539 10:17:07,199 --> 10:17:11,119 compiler is going to expand this 13540 10:17:11,120 --> 10:17:17,756 a character array of const car 13541 10:17:14,959 --> 10:17:20,559 and here what we are using to point to 13542 10:17:17,756 --> 10:17:23,756 that is not a cost car pointer 13543 10:17:20,559 --> 10:17:27,040 so this pointer here can be used to try 13544 10:17:23,756 --> 10:17:29,120 and modify data into an array of cost 13545 10:17:27,040 --> 10:17:31,360 cars and that's going to be disastrous 13546 10:17:29,120 --> 10:17:33,200 because if you try to modify that 13547 10:17:31,360 --> 10:17:35,199 that's going to fail and that's not 13548 10:17:33,199 --> 10:17:37,755 going to be good so some compilers are 13549 10:17:35,199 --> 10:17:41,275 going to try and protect you from this 13550 10:17:37,756 --> 10:17:43,756 and force you to use pointers that are 13551 10:17:41,275 --> 10:17:46,319 qualified with cost to really mean that 13552 10:17:43,756 --> 10:17:48,239 they are closed car pointers and to get 13553 10:17:46,319 --> 10:17:49,839 the code to work we will have to do that 13554 10:17:48,239 --> 10:17:52,000 and i will show you how we do that in 13555 10:17:49,839 --> 10:17:54,319 visual studio code but i have to tell 13556 10:17:52,000 --> 10:17:56,639 you this in case you just want to jump 13557 10:17:54,319 --> 10:17:58,639 ahead and try this code out if you get a 13558 10:17:56,639 --> 10:18:00,479 compiler error that's going to be the 13559 10:17:58,639 --> 10:18:02,400 reason now that we have our strength 13560 10:18:00,480 --> 10:18:04,800 stored in memory we can really print it 13561 10:18:02,400 --> 10:18:06,400 out like this if you print p message 13562 10:18:04,800 --> 10:18:09,040 you're going to get this ring printed 13563 10:18:06,400 --> 10:18:09,916 out but i want to give you a question 13564 10:18:09,915 --> 10:18:14,959 what do you think we will get if we 13565 10:18:16,720 --> 10:18:21,840 i told you that the compiler is going to 13566 10:18:19,120 --> 10:18:25,120 expand this hello world string into an 13567 10:18:21,839 --> 10:18:27,435 array of characters or to be specific an 13568 10:18:27,436 --> 10:18:31,680 and p message is going to be pointing to 13569 10:18:33,360 --> 10:18:38,480 so if we dereference p message we're 13570 10:18:36,480 --> 10:18:40,960 going to get that character printed out 13571 10:18:38,480 --> 10:18:42,640 in this case we will get an h and that's 13572 10:18:40,959 --> 10:18:45,756 what is going to happen here but 13573 10:18:42,639 --> 10:18:48,400 character pointers are special in that 13574 10:18:45,756 --> 10:18:51,120 if you print them out directory the 13575 10:18:48,400 --> 10:18:53,840 compiler is going to do its magic and 13576 10:18:51,120 --> 10:18:56,639 make it possible to print out the entire 13577 10:18:53,839 --> 10:18:58,799 string that is stored in p message here 13578 10:18:56,639 --> 10:19:00,800 that's how character pointers are 13579 10:18:58,800 --> 10:19:02,639 special and you really need to be aware 13580 10:19:00,800 --> 10:19:06,400 of this because this is going to come in 13581 10:19:02,639 --> 10:19:08,639 handy many times just to emphasize that 13582 10:19:06,400 --> 10:19:11,520 this is going to be expanded into an 13583 10:19:08,639 --> 10:19:12,800 array of constant characters if we try 13584 10:19:12,800 --> 10:19:18,400 something inside p message here for 13585 10:19:15,199 --> 10:19:20,879 example we try to change the h into a b 13586 10:19:18,400 --> 10:19:23,436 we can try to do that because p message 13587 10:19:20,879 --> 10:19:26,319 is pointing to the first character so we 13588 10:19:23,436 --> 10:19:28,959 can try to dereference and assign a new 13589 10:19:26,319 --> 10:19:31,199 value and if we do that we're going to 13590 10:19:28,959 --> 10:19:34,080 get a compiler arrow or even worse we 13591 10:19:31,199 --> 10:19:36,399 might get a crash at run time so this is 13592 10:19:34,080 --> 10:19:38,160 something you shouldn't really do and 13593 10:19:38,160 --> 10:19:43,120 your p message here as the cost card 13594 10:19:41,040 --> 10:19:45,040 pointer and that's going to prevent you 13595 10:19:43,120 --> 10:19:47,520 from doing things like this which may 13596 10:19:45,040 --> 10:19:50,319 turn out to be very bad but most 13597 10:19:47,519 --> 10:19:52,959 compilers are going to even prevent you 13598 10:19:50,319 --> 10:19:55,360 from compiling this thing in modern 13599 10:19:52,959 --> 10:19:57,120 times for example if you are using a 13600 10:19:57,120 --> 10:20:02,639 of visual studio you're going to get a 13601 10:19:59,680 --> 10:20:04,959 compiler error but i don't believe gcc 13602 10:20:02,639 --> 10:20:07,275 is going to give us a compiler error so 13603 10:20:04,959 --> 10:20:10,239 we will have to try this out and see how 13604 10:20:07,275 --> 10:20:12,400 it behaves but the message is if you 13605 10:20:10,239 --> 10:20:15,040 have to do something like this make sure 13606 10:20:12,400 --> 10:20:17,200 you use the cost car pointer and you're 13607 10:20:15,040 --> 10:20:19,595 going to be playing on the safe side 13608 10:20:17,199 --> 10:20:21,915 okay now i think you know enough about 13609 10:20:19,595 --> 10:20:24,799 character pointers what if we want our 13610 10:20:21,915 --> 10:20:26,559 users to be able to change this string 13611 10:20:24,800 --> 10:20:28,880 and make it be something else for 13612 10:20:26,559 --> 10:20:31,680 example change the first character here 13613 10:20:28,879 --> 10:20:34,719 into a b well in that case don't use 13614 10:20:31,680 --> 10:20:37,436 character pointers use regular arrays 13615 10:20:34,720 --> 10:20:40,080 like we have seen before so you can set 13616 10:20:37,436 --> 10:20:43,360 up an array like this it's going to be a 13617 10:20:40,080 --> 10:20:45,916 character array and you can initialize 13618 10:20:43,360 --> 10:20:47,680 it with the string literal and this is 13619 10:20:45,915 --> 10:20:50,159 going to allow you to modify things 13620 10:20:47,680 --> 10:20:52,879 inside so if we do something like this 13621 10:20:50,160 --> 10:20:54,400 we will get the message printed out and 13622 10:20:52,879 --> 10:20:57,275 the message is going to be tailored 13623 10:20:54,400 --> 10:21:00,319 because we changed the edge to be 13624 10:20:57,275 --> 10:21:02,479 a t and this is going to work so this is 13625 10:21:00,319 --> 10:21:05,199 something you really need to be aware of 13626 10:21:02,480 --> 10:21:07,756 but character pointers are very helpful 13627 10:21:05,199 --> 10:21:09,839 in many cases and in most cases you just 13628 10:21:07,756 --> 10:21:12,000 want to store the string and print it 13629 10:21:09,839 --> 10:21:13,755 out you're not interested in changing it 13630 10:21:12,000 --> 10:21:17,040 so that's why this is going to come in 13631 10:21:13,756 --> 10:21:18,400 handy in a lot of occasions now that you 13632 10:21:17,040 --> 10:21:20,720 know this we're going to head over to 13633 10:21:18,400 --> 10:21:23,840 visual studio code and actually try this 13634 10:21:20,720 --> 10:21:26,319 out okay here we are in our working 13635 10:21:23,839 --> 10:21:29,519 folder the current project is pointer to 13636 10:21:26,319 --> 10:21:31,275 car that's what we are exploring here so 13637 10:21:29,519 --> 10:21:34,079 we're going to grab our template files 13638 10:21:31,275 --> 10:21:36,559 and put them in place and we are going 13639 10:21:34,080 --> 10:21:38,800 to open this thing into visual studio 13640 10:21:36,559 --> 10:21:42,319 code and we're going to jump into here 13641 10:21:38,800 --> 10:21:44,720 and set up our first const car pointer 13642 10:21:42,319 --> 10:21:46,800 let's do a car pointer to see how this 13643 10:21:44,720 --> 10:21:48,959 thing behaves we're going to call this 13644 10:21:46,800 --> 10:21:52,240 message and we're going to initialize 13645 10:21:48,959 --> 10:21:54,080 this with our trusty message here hello 13646 10:21:52,239 --> 10:21:56,559 world okay once you do something like 13647 10:21:54,080 --> 10:21:57,276 this your string is going to be referred 13648 10:21:57,275 --> 10:22:02,720 by message let's try and compile this 13649 10:21:59,680 --> 10:22:04,639 and see that gcc is happy we can compile 13650 10:22:04,639 --> 10:22:09,436 and uh it's going to tell us we can't 13651 10:22:06,879 --> 10:22:11,680 really do that and this is really good 13652 10:22:09,436 --> 10:22:14,400 for base converting a string to constant 13653 10:22:11,680 --> 10:22:19,040 card pointer so what we are doing really 13654 10:22:14,400 --> 10:22:22,560 is making an array of const characters 13655 10:22:19,040 --> 10:22:24,319 and refer to it as a car pointer and 13656 10:22:22,559 --> 10:22:26,639 this is not going to work this is a good 13657 10:22:24,319 --> 10:22:28,559 thing so you can't really do this and 13658 10:22:28,559 --> 10:22:34,319 and let's try and see if visual studio 13659 10:22:32,319 --> 10:22:37,275 can actually do this we're going to 13660 10:22:34,319 --> 10:22:40,080 build with msvc in this case let's do 13661 10:22:37,275 --> 10:22:43,040 read to rooster before we do that we can 13662 10:22:40,080 --> 10:22:45,436 right click to add and say delete 13663 10:22:45,436 --> 10:22:49,596 make sure it is selected and hit delete 13664 10:22:49,595 --> 10:22:53,839 move to recycle bin in this case 13665 10:22:52,000 --> 10:22:56,639 now we're going to clear here let's 13666 10:22:58,959 --> 10:23:05,199 and we're going to build with gcc 13667 10:23:02,319 --> 10:23:08,319 we're going to choose the msvc compiler 13668 10:23:05,199 --> 10:23:10,079 if you're trying to build this 13669 10:23:10,080 --> 10:23:15,360 with errors and the error is going to be 13670 10:23:12,959 --> 10:23:17,756 basically the same thing this line here 13671 10:23:15,360 --> 10:23:20,559 won't work what we can do is make this 13672 10:23:17,756 --> 10:23:23,595 cost and make it super clear 13673 10:23:20,559 --> 10:23:26,079 that message here is going to point to 13674 10:23:26,080 --> 10:23:30,639 if we try to build again i think we're 13675 10:23:27,680 --> 10:23:31,756 going to be good now so let's try and do 13676 10:23:31,756 --> 10:23:38,239 we're going to build with msvc this time 13677 10:23:35,360 --> 10:23:40,239 and world finished successfully 13678 10:23:38,239 --> 10:23:42,959 now this is really cool and we can print 13679 10:23:44,959 --> 10:23:48,879 and we're going to say message 13680 10:23:51,040 --> 10:23:55,120 let's go back to gcc because that's my 13681 10:23:53,199 --> 10:23:57,039 favorite compiler and you see that the 13682 10:23:55,120 --> 10:23:59,120 compiler from visual studio is going to 13683 10:23:57,040 --> 10:24:00,239 clutter our folder here with a lot of 13684 10:24:00,239 --> 10:24:04,319 we don't want that what i'm going to do 13685 10:24:02,480 --> 10:24:06,880 i'm going to select all this collector 13686 10:24:04,319 --> 10:24:09,360 here and remove that and i'm going to 13687 10:24:06,879 --> 10:24:11,119 hit delete with the right click 13688 10:24:11,120 --> 10:24:16,639 we're going to go back to gcc and use it 13689 10:24:16,639 --> 10:24:21,436 the build is going to go through because 13690 10:24:18,239 --> 10:24:24,079 we qualified our character pointer with 13691 10:24:21,436 --> 10:24:25,756 cost here and everything is going to be 13692 10:24:25,756 --> 10:24:31,040 you know what is happening here because 13693 10:24:28,480 --> 10:24:33,596 this kind of problems can be really hard 13694 10:24:31,040 --> 10:24:35,756 to solve okay now that we have this we 13695 10:24:33,595 --> 10:24:37,756 can try and change something inside but 13696 10:24:35,756 --> 10:24:40,080 the compiler is not going to allow it so 13697 10:24:37,756 --> 10:24:43,040 if we try and do something like this is 13698 10:24:40,080 --> 10:24:45,756 not going to work because message is a 13699 10:24:45,756 --> 10:24:50,480 and we can't go through this pointer to 13700 10:24:47,680 --> 10:24:52,160 modify something so we're going to get a 13701 10:24:50,480 --> 10:24:55,680 compiler arrow this is something you 13702 10:24:52,160 --> 10:24:58,000 should know but we can add the reference 13703 10:24:55,680 --> 10:25:00,319 this pointer and print something that is 13704 10:24:58,000 --> 10:25:02,080 inside and i want you to guess again and 13705 10:25:00,319 --> 10:25:03,680 see that you can come up with what is 13706 10:25:02,080 --> 10:25:06,639 going to be printed if with the 13707 10:25:03,680 --> 10:25:09,436 reference message and print out when we 13708 10:25:06,639 --> 10:25:11,275 run this program if we have this thing 13709 10:25:09,436 --> 10:25:13,275 printed out here again we are the 13710 10:25:13,275 --> 10:25:19,915 and printing whatever result we get from 13711 10:25:16,160 --> 10:25:23,040 that remember that this string literal 13712 10:25:19,915 --> 10:25:25,915 is going to be turned into an array of 13713 10:25:23,040 --> 10:25:28,319 const cars and the first element of that 13714 10:25:25,915 --> 10:25:32,239 array is going to be pointed to 13715 10:25:28,319 --> 10:25:35,436 by this message car pointer here so if 13716 10:25:32,239 --> 10:25:37,756 we dereference this thing we should get 13717 10:25:35,436 --> 10:25:39,436 the h here printed out that's what i 13718 10:25:43,436 --> 10:25:47,436 the bolt is going to be good let's clear 13719 10:25:47,436 --> 10:25:52,639 see that message is h this is exactly 13720 10:25:50,559 --> 10:25:54,079 what we expect here now that we have 13721 10:25:54,080 --> 10:25:59,756 what if we really want this thing here 13722 10:25:57,436 --> 10:26:01,680 to work what if we wanted to allow our 13723 10:26:01,680 --> 10:26:06,160 this message thing and modify what is 13724 10:26:04,319 --> 10:26:07,275 stored inside because sometimes that's 13725 10:26:07,275 --> 10:26:11,915 useful okay we wanted to allow our users 13726 10:26:10,000 --> 10:26:15,680 to modify this trend if you want to do 13727 10:26:11,915 --> 10:26:17,680 that you can use row character arrays 13728 10:26:15,680 --> 10:26:20,879 and the way we do that we can say car 13729 10:26:17,680 --> 10:26:23,360 message one and we can make it an array 13730 10:26:20,879 --> 10:26:25,275 and initialize that with hello world and 13731 10:26:23,360 --> 10:26:28,400 once we do this we can go through the 13732 10:26:25,275 --> 10:26:31,275 angle bracket syntax and modify the 13733 10:26:28,400 --> 10:26:33,200 first character or whatever character we 13734 10:26:31,275 --> 10:26:36,239 really want to modify in this string 13735 10:26:33,199 --> 10:26:38,239 here in this case we are going to target 13736 10:26:36,239 --> 10:26:42,239 the first so we're going to say message 13737 10:26:38,239 --> 10:26:43,915 1 and we're going to grab the index 0 13738 10:26:42,239 --> 10:26:46,239 because that's going to be the first 13739 10:26:43,915 --> 10:26:48,799 character here and we're going to assign 13740 10:26:48,800 --> 10:26:56,080 b like we just said and if we print this 13741 10:26:51,839 --> 10:26:58,000 out say message one and uh put that out 13742 10:26:56,080 --> 10:26:59,680 we're going to see what comes out if we 13743 10:26:58,000 --> 10:27:01,120 run this program we're going to build 13744 10:27:03,436 --> 10:27:08,639 the world is good so we're good here 13745 10:27:06,800 --> 10:27:11,040 if we're on we're going to get below 13746 10:27:08,639 --> 10:27:13,680 printed down and we have successfully 13747 10:27:11,040 --> 10:27:15,595 changed this thing here so if you don't 13748 10:27:13,680 --> 10:27:18,639 want to allow modifications to your 13749 10:27:15,595 --> 10:27:21,199 strength use this thing here if you want 13750 10:27:18,639 --> 10:27:23,595 to allow modification to your strength 13751 10:27:21,199 --> 10:27:25,519 use this syntax here and this is really 13752 10:27:25,519 --> 10:27:31,119 in this lecture in this lecture we're 13753 10:27:27,595 --> 10:27:32,559 going to revisit our idea of a c plus 13754 10:27:32,559 --> 10:27:37,119 memory map we have seen that the c plus 13755 10:27:34,959 --> 10:27:39,275 plus development workflow really goes 13756 10:27:37,120 --> 10:27:41,520 like this we have a program typed out in 13757 10:27:39,275 --> 10:27:44,319 our ide we pass that through the 13758 10:27:41,519 --> 10:27:47,275 compiler and we get a binary executable 13759 10:27:44,319 --> 10:27:50,319 which is a binary representation of the 13760 10:27:47,275 --> 10:27:52,959 code we have in our c plus plus program 13761 10:27:50,319 --> 10:27:55,199 you can then run that program on an 13762 10:27:52,959 --> 10:27:58,479 operating system for example here i am 13763 10:27:55,199 --> 10:28:01,360 on windows if i double click on my dot 13764 10:27:58,480 --> 10:28:02,596 exe file for example our applications 13765 10:28:02,595 --> 10:28:07,360 rooster.exe you've seen me run this a 13766 10:28:07,360 --> 10:28:12,800 if we double click this program is going 13767 10:28:09,595 --> 10:28:15,680 to be loaded in our computer memory and 13768 10:28:12,800 --> 10:28:17,756 by memory i mean random access memory 13769 10:28:15,680 --> 10:28:19,840 the memory that is going to be actively 13770 10:28:17,756 --> 10:28:22,239 running things when they happen in your 13771 10:28:19,839 --> 10:28:23,119 computer is different from the hard 13772 10:28:23,120 --> 10:28:27,276 storage that can also be called memory 13773 10:28:25,436 --> 10:28:30,080 and this can throw off a lot of 13774 10:28:27,275 --> 10:28:32,239 beginners we are talking about ram here 13775 10:28:30,080 --> 10:28:34,480 when we mean memory random access memory 13776 10:28:32,239 --> 10:28:36,879 to be exact if you double click on your 13777 10:28:34,480 --> 10:28:39,276 program or open it on the terminal like 13778 10:28:36,879 --> 10:28:41,680 we've been doing the program is going to 13779 10:28:39,275 --> 10:28:44,879 be loaded in memory and it's going to be 13780 10:28:41,680 --> 10:28:47,519 stored in a special section of memory 13781 10:28:44,879 --> 10:28:50,159 called program area and so far we have 13782 10:28:47,519 --> 10:28:53,435 been thinking that our program is really 13783 10:28:50,160 --> 10:28:57,276 loaded in real memory on our computer 13784 10:28:53,436 --> 10:28:59,840 but that's not the case it is an idea of 13785 10:28:57,275 --> 10:29:02,479 the memory that the program thinks it 13786 10:28:59,839 --> 10:29:04,879 has but it's not the real thing and i am 13787 10:29:02,480 --> 10:29:07,680 going to explain but before we explain 13788 10:29:04,879 --> 10:29:10,000 let me elaborate on the reason why this 13789 10:29:07,680 --> 10:29:12,319 is done if we have many programs and 13790 10:29:10,000 --> 10:29:15,436 each program is going to use real memory 13791 10:29:12,319 --> 10:29:17,040 on our computer we could quickly run out 13792 10:29:15,436 --> 10:29:19,275 of memory because there are many 13793 10:29:17,040 --> 10:29:20,959 programs you might even have hundreds or 13794 10:29:19,275 --> 10:29:22,959 thousands of programs running on your 13795 10:29:20,959 --> 10:29:26,080 computer you may never have enough 13796 10:29:22,959 --> 10:29:28,239 memory to run them all if real memory 13797 10:29:31,120 --> 10:29:36,639 create what is called virtual memory and 13798 10:29:34,400 --> 10:29:39,120 it is a trick that pulls your program 13799 10:29:36,639 --> 10:29:41,915 into thinking it is the only program 13800 10:29:39,120 --> 10:29:44,959 running on your operating systems and it 13801 10:29:41,915 --> 10:29:46,719 owns or the address space or all the 13802 10:29:44,959 --> 10:29:49,680 memory on your computer that's what your 13803 10:29:46,720 --> 10:29:52,880 program thanks and the view that your 13804 10:29:49,680 --> 10:29:55,840 program has of that memory is called a 13805 10:29:52,879 --> 10:29:58,000 memory map each program is abstracted 13806 10:29:55,839 --> 10:30:00,159 into what we call a process you can 13807 10:30:00,160 --> 10:30:05,596 that we have been welding as a process 13808 10:30:03,360 --> 10:30:08,639 on the system and each process has 13809 10:30:05,595 --> 10:30:11,435 access to the memory range between 0 and 13810 10:30:08,639 --> 10:30:14,559 2 to the power of n where n is the 13811 10:30:11,436 --> 10:30:17,239 number of bits that your operating 13812 10:30:14,559 --> 10:30:20,559 system supports for example i am on a 13813 10:30:17,239 --> 10:30:23,199 64-bit operating system so the range for 13814 10:30:20,559 --> 10:30:25,915 my memory is going to be between 0 and 2 13815 10:30:23,199 --> 10:30:28,319 to the power of 64. and that's a lot of 13816 10:30:25,915 --> 10:30:31,275 memory my program is going to think that 13817 10:30:28,319 --> 10:30:32,879 it owns all that all and it can never 13818 10:30:31,275 --> 10:30:35,519 run out it's going to do whatever it 13819 10:30:32,879 --> 10:30:37,435 wants if you are on a 32-bit system 13820 10:30:35,519 --> 10:30:39,915 that's going to be between 0 and 2 to 13821 10:30:37,436 --> 10:30:42,639 the power of 32 and that's going to be 13822 10:30:39,915 --> 10:30:45,595 your virtual memory the amount of memory 13823 10:30:42,639 --> 10:30:47,680 that your program fans that it owns and 13824 10:30:45,595 --> 10:30:50,079 it happens to be the entirety of your 13825 10:30:47,680 --> 10:30:52,720 ram that you have on your system each 13826 10:30:50,080 --> 10:30:55,276 program basically thinks that it owns 13827 10:30:52,720 --> 10:30:58,000 the entire memory and it can do whatever 13828 10:30:55,275 --> 10:31:00,159 it wants okay so we have the rio memory 13829 10:30:58,000 --> 10:31:02,720 which is random access memory and we 13830 10:31:00,160 --> 10:31:06,000 have virtual memory which is going to be 13831 10:31:02,720 --> 10:31:09,680 between 0 and 2 to the power of n okay 13832 10:31:06,000 --> 10:31:12,239 so now we're going to update on the idea 13833 10:31:09,680 --> 10:31:14,239 of the program execution model because 13834 10:31:12,239 --> 10:31:16,799 now we know more we know about virtual 13835 10:31:14,239 --> 10:31:18,799 memory here is our c plus program it's 13836 10:31:16,800 --> 10:31:21,200 going to go through the compiler and we 13837 10:31:18,800 --> 10:31:23,916 are going to generate a binary file 13838 10:31:21,199 --> 10:31:25,915 which is going to be a representation of 13839 10:31:23,915 --> 10:31:27,915 how things are going to be laid out in 13840 10:31:30,800 --> 10:31:36,720 it is going to go through a section of 13841 10:31:33,199 --> 10:31:40,799 the cpu called memory management unit or 13842 10:31:36,720 --> 10:31:43,275 mmu for short so that mmu is going to be 13843 10:31:40,800 --> 10:31:47,200 basically transforming between the 13844 10:31:43,275 --> 10:31:49,756 memory map of each process and the radio 13845 10:31:47,199 --> 10:31:51,839 memory layout that we have in ram what 13846 10:31:49,756 --> 10:31:54,319 the memory management unit does is 13847 10:31:54,319 --> 10:31:58,800 representation we have in the memory map 13848 10:31:56,639 --> 10:32:00,720 and the real thing we have in ram the 13849 10:31:58,800 --> 10:32:02,480 entire program is not loaded in real 13850 10:32:02,480 --> 10:32:07,436 only parts of the program that are 13851 10:32:05,120 --> 10:32:09,360 really going to be used soon unloaded in 13852 10:32:07,436 --> 10:32:12,239 memory and this is going to make 13853 10:32:09,360 --> 10:32:14,400 effective use of the memory parts that 13854 10:32:12,239 --> 10:32:17,040 are not likely to be used are going to 13855 10:32:14,400 --> 10:32:18,720 be discarded from memory and stored on 13856 10:32:17,040 --> 10:32:21,520 the hard drive instead and this is 13857 10:32:18,720 --> 10:32:23,120 really cool please note that the real 13858 10:32:21,519 --> 10:32:26,000 way these things are done is really 13859 10:32:23,120 --> 10:32:28,480 complicated but it is buried down into 13860 10:32:26,000 --> 10:32:30,080 the logic of the cpu and whatever you 13861 10:32:28,480 --> 10:32:32,480 don't really need to worry about this as 13862 10:32:30,080 --> 10:32:34,319 a super plus programmer but there are a 13863 10:32:32,480 --> 10:32:36,880 few things you need to know as we are 13864 10:32:34,319 --> 10:32:39,839 about to see in this lecture so memory 13865 10:32:36,879 --> 10:32:40,959 management unit helps out in mapping 13866 10:32:40,959 --> 10:32:46,159 the memory map you have in your program 13867 10:32:42,879 --> 10:32:49,119 basically the idea that your program has 13868 10:32:46,160 --> 10:32:51,916 of memory and the real thing 13869 10:32:49,120 --> 10:32:53,756 that we have in memory okay here is a 13870 10:32:51,915 --> 10:32:56,559 slide that is going to drive this home 13871 10:32:53,756 --> 10:32:59,120 we have a few programs with their own 13872 10:32:56,559 --> 10:33:00,319 separate memory maps we have program one 13873 10:33:00,319 --> 10:33:05,360 if we execute them they are going to go 13874 10:33:02,480 --> 10:33:07,276 through the memory management unit and 13875 10:33:05,360 --> 10:33:10,160 the memory management unit is going to 13876 10:33:07,275 --> 10:33:12,000 assign them sections on the real 13877 10:33:10,160 --> 10:33:14,800 ram that we have in our computer and 13878 10:33:12,000 --> 10:33:17,360 they are going to be using that section 13879 10:33:14,800 --> 10:33:19,436 on the real random access memory that we 13880 10:33:17,360 --> 10:33:22,319 have on our computer the thing is your 13881 10:33:19,436 --> 10:33:23,916 program is going to really think it owns 13882 10:33:23,915 --> 10:33:28,239 but it's not just going to be using a 13883 10:33:26,080 --> 10:33:30,800 part of it and the memory management 13884 10:33:28,239 --> 10:33:33,595 unit is going to be transforming between 13885 10:33:30,800 --> 10:33:36,319 the idea that your program has and the 13886 10:33:33,595 --> 10:33:39,040 real memory that we have on our computer 13887 10:33:36,319 --> 10:33:41,839 and the memory map is a standard format 13888 10:33:39,040 --> 10:33:43,915 that is defined by the operating system 13889 10:33:41,839 --> 10:33:46,959 this is why for example you can't take 13890 10:33:43,915 --> 10:33:49,435 an executable that was set up on windows 13891 10:33:46,959 --> 10:33:52,159 and directly run it on linux this is an 13892 10:33:49,436 --> 10:33:55,200 operating system thing the memory map or 13893 10:33:52,160 --> 10:33:57,520 the structure of your program is defined 13894 10:33:55,199 --> 10:34:01,199 by the operating system that is going to 13895 10:33:57,519 --> 10:34:03,359 be running that program so all program 13896 10:34:01,199 --> 10:34:05,915 written for an operating system must 13897 10:34:03,360 --> 10:34:08,639 conform to the memory map that is 13898 10:34:05,915 --> 10:34:11,040 defined by that operating system so all 13899 10:34:08,639 --> 10:34:13,436 programs on windows if you had a way to 13900 10:34:11,040 --> 10:34:15,360 go in and look at the memory map they 13901 10:34:13,436 --> 10:34:17,436 are going to be pretty similar so we 13902 10:34:15,360 --> 10:34:18,160 have been talking about the memory map 13903 10:34:20,080 --> 10:34:25,040 well here is a slide that is going to 13904 10:34:22,720 --> 10:34:27,275 try and clear that out so we're going to 13905 10:34:25,040 --> 10:34:29,915 have a bunch of memory locations from 0 13906 10:34:27,275 --> 10:34:32,079 all the way to 2 to the power of m 13907 10:34:29,915 --> 10:34:34,719 and the memory map is really divided 13908 10:34:32,080 --> 10:34:36,319 into a lot of parts for example we have 13909 10:34:34,720 --> 10:34:38,080 the text section we have the data 13910 10:34:36,319 --> 10:34:39,040 section we have the heap stack and 13911 10:34:39,040 --> 10:34:43,360 and this is one view you can have on 13912 10:34:41,120 --> 10:34:45,120 this there are a few different versions 13913 10:34:43,360 --> 10:34:47,120 of this slide that you're going to see 13914 10:34:45,120 --> 10:34:50,639 on the internet but what is really 13915 10:34:47,120 --> 10:34:53,276 important is stack and hip for our 13916 10:34:50,639 --> 10:34:56,479 purposes here stack is what is going to 13917 10:34:53,275 --> 10:34:59,360 store our local variables thank the a 13918 10:34:56,480 --> 10:35:01,680 that we have here in main bc thanks we 13919 10:34:59,360 --> 10:35:05,120 had inside functions those things are 13920 10:35:01,680 --> 10:35:06,400 stored inside the stack section of our 13921 10:35:06,400 --> 10:35:10,880 besides the stack we also have the hip 13922 10:35:08,879 --> 10:35:13,595 which is going to be the location where 13923 10:35:10,879 --> 10:35:16,079 we get additional memory we can use to 13924 10:35:13,595 --> 10:35:17,839 make things better for our program we 13925 10:35:16,080 --> 10:35:20,400 also have the text section which is 13926 10:35:17,839 --> 10:35:23,119 going to load the actual binary of our 13927 10:35:20,400 --> 10:35:24,880 program so that the cpu can execute it 13928 10:35:23,120 --> 10:35:26,560 but we're not going to worry too much 13929 10:35:24,879 --> 10:35:28,879 about this section in this course 13930 10:35:26,559 --> 10:35:31,040 because it is out of scope for 13931 10:35:28,879 --> 10:35:33,839 what we are trying to do here we are 13932 10:35:31,040 --> 10:35:35,520 going to focus on stack and hip because 13933 10:35:33,839 --> 10:35:38,479 hip is actually going to be very 13934 10:35:35,519 --> 10:35:41,199 important from the next lecture when we 13935 10:35:38,480 --> 10:35:43,120 start talking about dynamic memory hip 13936 10:35:41,199 --> 10:35:45,275 is going to be additional memory that 13937 10:35:43,120 --> 10:35:47,360 you can use in your program if you run 13938 10:35:45,275 --> 10:35:49,436 out of stack memory and we're going to 13939 10:35:47,360 --> 10:35:51,915 see a few other reasons why you might 13940 10:35:49,436 --> 10:35:54,239 want to use this as we progress in this 13941 10:35:51,915 --> 10:35:57,435 chapter so our memory map is going to 13942 10:35:54,239 --> 10:35:59,915 have a lot of parts but the parts that 13943 10:35:57,436 --> 10:36:02,480 we really care about in this course are 13944 10:35:59,915 --> 10:36:04,000 stack and heap this is really all we set 13945 10:36:02,480 --> 10:36:06,480 out to do in this lecture i hope you 13946 10:36:04,000 --> 10:36:09,199 have a better idea of the memory map and 13947 10:36:06,480 --> 10:36:11,596 how this really works we introduced 13948 10:36:09,199 --> 10:36:14,799 another view that you can have on how 13949 10:36:11,595 --> 10:36:16,720 your c plus plus program is executed and 13950 10:36:14,800 --> 10:36:18,800 i hope you have a better idea of the 13951 10:36:16,720 --> 10:36:21,436 memory system and we are going to start 13952 10:36:18,800 --> 10:36:23,840 and see how you can work with dynamic 13953 10:36:21,436 --> 10:36:25,520 memory allocation which is going to 13954 10:36:27,595 --> 10:36:30,879 go ahead and finish up here and meet me 13955 10:36:30,879 --> 10:36:36,079 in this lecture we're going to start and 13956 10:36:32,800 --> 10:36:38,480 learn about dynamic memory allocation 13957 10:36:36,080 --> 10:36:42,080 and this is a technique we can use to 13958 10:36:38,480 --> 10:36:44,400 start using hip storage that we have in 13959 10:36:42,080 --> 10:36:46,480 the memory map of our c plus plus 13960 10:36:44,400 --> 10:36:48,560 program we can use that to get 13961 10:36:46,480 --> 10:36:51,840 additional memory we can use and do 13962 10:36:48,559 --> 10:36:54,079 stuff if stack memory isn't enough for 13963 10:36:51,839 --> 10:36:56,079 our purposes this is how we have been 13964 10:36:54,080 --> 10:36:58,319 using pointers so far for example we 13965 10:36:56,080 --> 10:37:00,959 have a variable we set up a pointer and 13966 10:36:58,319 --> 10:37:03,839 we store the address of the variable n 13967 10:37:00,959 --> 10:37:06,479 so we can go ahead and use this pointer 13968 10:37:03,839 --> 10:37:08,879 to manipulate the variable or do all 13969 10:37:06,480 --> 10:37:11,756 kinds of crazy things if you go down 13970 10:37:08,879 --> 10:37:14,399 here we have a pointer p number one 13971 10:37:11,756 --> 10:37:16,639 and we have a number one variable and we 13972 10:37:14,400 --> 10:37:18,720 can store the address of number one into 13973 10:37:16,639 --> 10:37:21,519 this pointer it's really the same thing 13974 10:37:18,720 --> 10:37:24,800 like we have on top here but the pointer 13975 10:37:21,519 --> 10:37:26,399 is not initialized at declaration here 13976 10:37:24,800 --> 10:37:28,480 and you have to make sure you put 13977 10:37:26,400 --> 10:37:30,959 invalid data and one thing i haven't 13978 10:37:28,480 --> 10:37:32,319 really stressed enough is that it is 13979 10:37:32,319 --> 10:37:37,360 really bad to try and use a pointer that 13980 10:37:34,800 --> 10:37:39,916 is not initialized for example here we 13981 10:37:37,360 --> 10:37:43,199 have p number two which is a pointer it 13982 10:37:39,915 --> 10:37:47,199 is not initialized it contains junk 13983 10:37:43,199 --> 10:37:49,275 and we try and write into it okay so try 13984 10:37:47,199 --> 10:37:51,119 to think about what we really are doing 13985 10:37:49,275 --> 10:37:54,159 here this is a pointer that we have set 13986 10:37:51,120 --> 10:37:56,480 up we haven't really initialized that so 13987 10:37:54,160 --> 10:37:58,800 it's going to contain whatever address 13988 10:37:56,480 --> 10:38:00,800 is possible it can contain an address 13989 10:37:58,800 --> 10:38:02,639 that is used by another program it may 13990 10:38:00,800 --> 10:38:04,800 contain an address that is being used by 13991 10:38:02,639 --> 10:38:07,680 the operating system it's not your 13992 10:38:04,800 --> 10:38:09,520 address to mess with and here we are 13993 10:38:07,680 --> 10:38:13,275 actually trying to mess with that by 13994 10:38:09,519 --> 10:38:16,079 storing in a 55 so this is going to 13995 10:38:13,275 --> 10:38:18,559 cause the crash possibly or even 13996 10:38:16,080 --> 10:38:21,276 corrupt data that is being used by other 13997 10:38:18,559 --> 10:38:23,435 people so don't do this another thing is 13998 10:38:21,275 --> 10:38:25,915 to try and use a pointer that is 13999 10:38:23,436 --> 10:38:29,200 initialized to know for example here we 14000 10:38:25,915 --> 10:38:31,360 have a pointer p number three and it is 14001 10:38:29,199 --> 10:38:34,000 braced initialized this is going to 14002 10:38:31,360 --> 10:38:37,275 initialize it to null pointer but we are 14003 10:38:34,000 --> 10:38:39,360 trying and storing in a value here so 14004 10:38:37,275 --> 10:38:40,959 this is also going to be bad it's not 14005 10:38:39,360 --> 10:38:43,120 granted what you're going to get 14006 10:38:40,959 --> 10:38:45,120 actually c plus doesn't give you any 14007 10:38:43,120 --> 10:38:47,200 guarantees this is called undefined 14008 10:38:45,120 --> 10:38:50,160 behavior so you're going to get a crash 14009 10:38:47,199 --> 10:38:51,915 and don't do it as a good practice the 14010 10:38:50,160 --> 10:38:53,200 first thing you should do is initialize 14011 10:38:53,199 --> 10:38:58,799 before you use them you can initialize 14012 10:38:55,519 --> 10:39:00,000 them with no pointer and make sure that 14013 10:39:00,000 --> 10:39:04,400 modifying valid addresses into those 14014 10:39:02,720 --> 10:39:06,959 pointers if you don't know what is in a 14015 10:39:04,400 --> 10:39:09,520 pointer don't use that pointer because 14016 10:39:06,959 --> 10:39:12,080 bad things can happen okay rule number 14017 10:39:09,519 --> 10:39:13,680 one don't try to use a pointer that you 14018 10:39:13,680 --> 10:39:19,436 rule number two don't try to work with a 14019 10:39:16,720 --> 10:39:21,120 pointer that is pointing to no ptr 14020 10:39:19,436 --> 10:39:23,275 that's going to give you bad things you 14021 10:39:21,120 --> 10:39:26,080 don't want to really deal with okay so 14022 10:39:23,275 --> 10:39:28,879 far we have been using memory that lives 14023 10:39:26,080 --> 10:39:30,800 on the stack section of our memory map 14024 10:39:28,879 --> 10:39:32,959 we were using variables that we have 14025 10:39:30,800 --> 10:39:34,639 declared in the main function we were 14026 10:39:32,959 --> 10:39:37,199 using the variables that we have 14027 10:39:34,639 --> 10:39:39,756 declared inside some functions and 14028 10:39:37,199 --> 10:39:41,519 that's all stuff that live on the stack 14029 10:39:39,756 --> 10:39:44,239 now we want to see how we can use the 14030 10:39:41,519 --> 10:39:46,799 hip okay let's take a closer look at our 14031 10:39:44,239 --> 10:39:47,595 memory map we have the stack we have the 14032 10:39:47,595 --> 10:39:50,879 and the differences between these two 14033 10:39:52,480 --> 10:39:58,720 the stack memory is finite so you don't 14034 10:39:55,756 --> 10:40:00,959 have infinite memory but the same is 14035 10:39:58,720 --> 10:40:03,200 true for the heap but hip is additional 14036 10:40:00,959 --> 10:40:05,680 memory that you can use if you don't 14037 10:40:03,199 --> 10:40:08,399 want to run out of stock memory for a 14038 10:40:05,680 --> 10:40:11,680 stack memory the developer isn't in full 14039 10:40:08,400 --> 10:40:13,680 control of the memory lifetime so if you 14040 10:40:11,680 --> 10:40:16,480 declare something on the stack for 14041 10:40:13,680 --> 10:40:19,519 example if you say int var1 and you 14042 10:40:16,480 --> 10:40:22,000 initialize that to 23 you don't control 14043 10:40:19,519 --> 10:40:24,319 when that variable is wiped out of 14044 10:40:22,000 --> 10:40:26,800 memory the variable is going to be 14045 10:40:24,319 --> 10:40:29,839 killed when the scope in which it was 14046 10:40:29,839 --> 10:40:34,719 and you developer don't really say when 14047 10:40:32,559 --> 10:40:36,479 the variable dies and sometimes you 14048 10:40:36,480 --> 10:40:40,880 when your variable dies so this is a 14049 10:40:38,239 --> 10:40:42,959 limitation we have with the stack memory 14050 10:40:40,879 --> 10:40:44,719 okay so for stack memory lifetime is 14051 10:40:42,959 --> 10:40:47,915 controlled by the scope mechanism we 14052 10:40:44,720 --> 10:40:50,720 just say that for heap the developer is 14053 10:40:47,915 --> 10:40:53,519 in full control of when the variable 14054 10:40:50,720 --> 10:40:55,200 comes to life and when the variable dies 14055 10:40:53,519 --> 10:40:57,755 and this is a benefit you might have 14056 10:40:55,199 --> 10:40:59,275 with heap storage and you might want to 14057 10:40:57,756 --> 10:41:01,840 use this and we're going to see how we 14058 10:40:59,275 --> 10:41:03,436 can use that in the next few lectures 14059 10:41:01,839 --> 10:41:05,755 okay so make sure you understand the 14060 10:41:03,436 --> 10:41:07,436 difference between stack and hip here if 14061 10:41:05,756 --> 10:41:10,239 you look at the example here we have a 14062 10:41:07,436 --> 10:41:13,275 variable which is called locoscope var 14063 10:41:10,239 --> 10:41:16,000 it is stored within this block delimited 14064 10:41:13,275 --> 10:41:17,756 by the curly braces and the variable is 14065 10:41:16,000 --> 10:41:20,720 going to come to life when this 14066 10:41:17,756 --> 10:41:21,520 statement is executed but it is going to 14067 10:41:21,519 --> 10:41:26,639 when we hit the closing curly brace here 14068 10:41:24,639 --> 10:41:29,839 so the lifetime of this variable is 14069 10:41:26,639 --> 10:41:33,116 controlled by the scope mechanism but it 14070 10:41:29,839 --> 10:41:34,719 is possible to use dynamic memory or use 14071 10:41:33,116 --> 10:41:37,116 memory on the heap and we're going to 14072 10:41:34,720 --> 10:41:38,880 see a syntax you can use to do that the 14073 10:41:37,116 --> 10:41:41,116 first thing we want to do it to set up 14074 10:41:38,879 --> 10:41:44,239 employer that is going to be pointing to 14075 10:41:41,116 --> 10:41:45,840 that heap allocated memory the way you 14076 10:41:44,239 --> 10:41:48,639 do that you set up a pointer here we 14077 10:41:45,839 --> 10:41:50,159 have p number four we initialize it with 14078 10:41:50,160 --> 10:41:54,960 but what we do to allocate memory is the 14079 10:41:52,879 --> 10:41:58,239 second statement here we say p number 14080 10:41:58,239 --> 10:42:03,040 the moment this statement is going to be 14081 10:42:00,400 --> 10:42:06,080 executed the operating system is going 14082 10:42:03,040 --> 10:42:07,840 to allocate a piece of memory on the 14083 10:42:06,080 --> 10:42:10,000 heap the memory is going to be large 14084 10:42:07,839 --> 10:42:12,159 enough to accommodate for an integer so 14085 10:42:10,000 --> 10:42:14,319 it's going to be four bytes long 14086 10:42:12,160 --> 10:42:16,880 and it is your memory now your program 14087 10:42:14,319 --> 10:42:19,116 can use it to do whatever it wants and 14088 10:42:16,879 --> 10:42:21,519 no other program on the system can use 14089 10:42:19,116 --> 10:42:25,276 this piece of memory so it is going to 14090 10:42:21,519 --> 10:42:27,519 be yours until you explicitly return 14091 10:42:25,275 --> 10:42:29,040 this piece of memory to the system and 14092 10:42:27,519 --> 10:42:31,435 we're going to see how you can do that 14093 10:42:29,040 --> 10:42:33,436 in a minute once this memory is yours 14094 10:42:31,436 --> 10:42:35,200 you can use it you see here we already 14095 10:42:33,436 --> 10:42:37,680 referencing the memory and the story 14096 10:42:35,199 --> 10:42:39,680 again is 77 and if we're trying to print 14097 10:42:37,680 --> 10:42:41,915 this 77 we're going to get it printed 14098 10:42:39,680 --> 10:42:44,480 out okay this is how you can use dynamic 14099 10:42:44,480 --> 10:42:49,756 things are not being stored on the stack 14100 10:42:47,595 --> 10:42:51,360 things are being stored on the heap i 14101 10:42:49,756 --> 10:42:54,639 really hope you understand this because 14102 10:42:51,360 --> 10:42:58,000 it is very important okay so here is our 14103 10:42:54,639 --> 10:43:00,559 view of stack and heap so if you use new 14104 10:42:58,000 --> 10:43:03,839 to allocate for memory stuff are going 14105 10:43:00,559 --> 10:43:06,159 to be stored in this hip section here of 14106 10:43:03,839 --> 10:43:08,879 your memory map here is another example 14107 10:43:06,160 --> 10:43:11,840 to try and understand the lifetime of 14108 10:43:08,879 --> 10:43:13,755 our variables docovar again is limited 14109 10:43:11,839 --> 10:43:15,755 to this scope it is going to come into 14110 10:43:13,756 --> 10:43:19,040 life when this statement here is 14111 10:43:15,756 --> 10:43:21,520 executed and it is going to die when we 14112 10:43:19,040 --> 10:43:24,319 run out of this scope here so if you try 14113 10:43:21,519 --> 10:43:26,479 executing code out of this scope locovar 14114 10:43:24,319 --> 10:43:29,360 is no longer going to be available you 14115 10:43:26,480 --> 10:43:33,116 can't use it but that's not the case for 14116 10:43:29,360 --> 10:43:35,040 locro ptr var because this is a pointer 14117 10:43:35,040 --> 10:43:40,720 and if you go out of this scope the 14118 10:43:38,160 --> 10:43:42,800 memory is still going to be yours if you 14119 10:43:40,720 --> 10:43:44,959 have a way you can access it and use it 14120 10:43:42,800 --> 10:43:48,319 you're going to be able to use it and it 14121 10:43:44,959 --> 10:43:50,959 is going to be yours until you decide to 14122 10:43:48,319 --> 10:43:52,879 return it to the operating system so we 14123 10:43:50,959 --> 10:43:54,559 have been talking about returning the 14124 10:43:54,559 --> 10:44:00,399 how do we do it well you use the delete 14125 10:43:57,756 --> 10:44:03,200 keyword in c plus plus here we have our 14126 10:44:00,400 --> 10:44:05,520 memory which is allocated using new and 14127 10:44:03,199 --> 10:44:07,839 so the operating system is going to give 14128 10:44:05,519 --> 10:44:09,435 us a new piece of memory it is going to 14129 10:44:07,839 --> 10:44:11,595 be four bytes because we're going to 14130 10:44:09,436 --> 10:44:14,000 store in an integer and we are going to 14131 10:44:11,595 --> 10:44:16,319 be pointing to that memory through p 14132 10:44:14,000 --> 10:44:18,160 number four here we can use this memory 14133 10:44:16,319 --> 10:44:20,319 however we want through the referencing 14134 10:44:18,160 --> 10:44:22,480 and do all kinds of crazy things and 14135 10:44:20,319 --> 10:44:24,639 when we are done with this memory we are 14136 10:44:22,480 --> 10:44:26,400 going to say delete p number four and 14137 10:44:24,639 --> 10:44:28,080 this statement is going to return the 14138 10:44:26,400 --> 10:44:30,720 memory to the operating system the 14139 10:44:28,080 --> 10:44:33,276 memory is no longer going to be hours 14140 10:44:30,720 --> 10:44:35,916 after this statement and a good thing to 14141 10:44:33,275 --> 10:44:37,680 do after you release your memory is to 14142 10:44:37,680 --> 10:44:43,360 no ptr so that other people know that 14143 10:44:40,639 --> 10:44:46,559 you don't really have any valid data in 14144 10:44:43,360 --> 10:44:47,840 that memory because if you try to use a 14145 10:44:47,839 --> 10:44:51,595 memory section bad things are going to 14146 10:44:50,080 --> 10:44:53,680 happen that's not going to be your 14147 10:44:51,595 --> 10:44:55,519 memory we're going to fall into the same 14148 10:44:55,519 --> 10:45:01,199 uninitialized piece of memory don't do 14149 10:44:58,160 --> 10:45:04,240 that so whenever you release memory also 14150 10:45:01,199 --> 10:45:06,639 reset it to no ptr to play it safe we 14151 10:45:04,239 --> 10:45:09,360 can also initialize our pointers with 14152 10:45:06,639 --> 10:45:11,275 dynamic memory when we declare them and 14153 10:45:09,360 --> 10:45:14,080 this is how you can do it we say p 14154 10:45:11,275 --> 10:45:15,915 number five and we initialize that with 14155 10:45:14,080 --> 10:45:18,639 new and this is going to give us a new 14156 10:45:15,915 --> 10:45:21,040 piece of memory on the heap and it is 14157 10:45:18,639 --> 10:45:23,436 going to be pointed to by p number five 14158 10:45:21,040 --> 10:45:25,840 we get the same thing here but uh here 14159 10:45:23,436 --> 10:45:27,275 we are just pointing in new values and p 14160 10:45:27,275 --> 10:45:32,319 number seven the memory locations on the 14161 10:45:29,839 --> 10:45:35,755 heap are going to be containing 22 and 14162 10:45:32,319 --> 10:45:38,000 23 for p number six and p number seven 14163 10:45:35,756 --> 10:45:40,400 and here we have a few examples of how 14164 10:45:38,000 --> 10:45:43,436 we can print the contents of these 14165 10:45:43,436 --> 10:45:49,200 and after you are done using your hip 14166 10:45:46,480 --> 10:45:51,360 memory you're going to release it using 14167 10:45:49,199 --> 10:45:54,319 the delete keyword and you are going to 14168 10:45:51,360 --> 10:45:57,040 reset that to no ptr to really play it 14169 10:45:54,319 --> 10:46:00,080 safe this is how you should use dynamic 14170 10:45:57,040 --> 10:46:02,559 memory allocated on the heap another 14171 10:46:00,080 --> 10:46:05,916 thing you should know is that it is 14172 10:46:02,559 --> 10:46:08,079 really bad to call delete twice on a 14173 10:46:05,915 --> 10:46:10,159 pointer so here we have a memory 14174 10:46:08,080 --> 10:46:12,560 location that we allocate on the heap we 14175 10:46:12,559 --> 10:46:18,159 we are trying to use it here in sddc 14176 10:46:18,160 --> 10:46:21,756 and if we delete it for the first time 14177 10:46:20,239 --> 10:46:23,915 that's going to be cool it is going to 14178 10:46:21,756 --> 10:46:26,319 be returned to the system but if you try 14179 10:46:23,915 --> 10:46:28,639 to delete it for the second time you're 14180 10:46:26,319 --> 10:46:31,199 going to get a crash don't do this this 14181 10:46:28,639 --> 10:46:34,319 is really bad okay now that we have a 14182 10:46:31,199 --> 10:46:36,959 slightly clearer idea about pointers and 14183 10:46:34,319 --> 10:46:38,959 dynamic memory allocation i think it is 14184 10:46:36,959 --> 10:46:40,959 time we went to visual studio code and 14185 10:46:38,959 --> 10:46:44,000 actually played with a few of these 14186 10:46:40,959 --> 10:46:46,400 concepts here we are in our working 14187 10:46:44,000 --> 10:46:48,480 directory the current project is dynamic 14188 10:46:46,400 --> 10:46:50,639 memory allocation we're going to grab 14189 10:46:48,480 --> 10:46:52,960 our template files and we're going to 14190 10:46:50,639 --> 10:46:55,275 put them in place dynamic memory 14191 10:46:52,959 --> 10:46:57,519 allocation let's do that and we're going 14192 10:46:55,275 --> 10:46:59,360 to open this in visual studio code the 14193 10:46:57,519 --> 10:47:02,079 first thing we're going to do is review 14194 10:46:59,360 --> 10:47:04,080 how we have been using players so far 14195 10:47:02,080 --> 10:47:06,560 we have been using them like this we had 14196 10:47:04,080 --> 10:47:09,200 a variable stored somewhere on the stack 14197 10:47:06,559 --> 10:47:11,839 again this is the stack memory that we 14198 10:47:11,839 --> 10:47:16,079 stored addresses of some variables you 14199 10:47:14,080 --> 10:47:17,360 know pointers something like this and we 14200 10:47:16,080 --> 10:47:19,360 could go through the pointer to 14201 10:47:17,360 --> 10:47:20,319 manipulate that memory location for 14202 10:47:20,319 --> 10:47:24,400 we can print the address of that memory 14203 10:47:22,239 --> 10:47:26,000 we can even the reference and print the 14204 10:47:26,000 --> 10:47:31,360 by doing something like we do here and 14205 10:47:29,360 --> 10:47:33,595 we are not printing here so let's say 14206 10:47:31,360 --> 10:47:35,360 that and say p number we can do 14207 10:47:33,595 --> 10:47:38,879 something like this and we have been 14208 10:47:35,360 --> 10:47:40,720 doing this quite a lot so far we can 14209 10:47:38,879 --> 10:47:43,519 also have a piece of memory that is 14210 10:47:40,720 --> 10:47:46,080 uninitialized and we store the address 14211 10:47:43,519 --> 10:47:47,915 of some variable in that piece of memory 14212 10:47:46,080 --> 10:47:49,520 and we can do the same thing for example 14213 10:47:47,915 --> 10:47:52,479 if we print p number one we're going to 14214 10:47:49,519 --> 10:47:54,399 get the value 12 printed out here and we 14215 10:47:52,480 --> 10:47:56,640 could run this so let's try and do that 14216 10:47:54,400 --> 10:47:58,720 we're going to bring up a terminal first 14217 10:47:56,639 --> 10:48:01,275 and close this pane so that we have some 14218 10:47:58,720 --> 10:48:04,319 breathing room and we are going to build 14219 10:48:04,319 --> 10:48:08,720 we're going to build successfully going 14220 10:48:06,639 --> 10:48:10,639 to clear and run rooster and we're going 14221 10:48:08,720 --> 10:48:13,040 to see that we have these things printed 14222 10:48:10,639 --> 10:48:14,959 out here the variable we have in here is 14223 10:48:13,040 --> 10:48:17,040 22 so we're going to get that if we 14224 10:48:17,040 --> 10:48:20,879 if we print pointer we're going to get 14225 10:48:18,720 --> 10:48:23,275 the address printed out we can also go 14226 10:48:20,879 --> 10:48:26,319 through the address of operator to print 14227 10:48:23,275 --> 10:48:28,479 the address of this variable here but we 14228 10:48:26,319 --> 10:48:31,116 can also print our value through the 14229 10:48:28,480 --> 10:48:33,200 referencing our pointer and we can get 14230 10:48:31,116 --> 10:48:35,520 access to it here down here we have a 14231 10:48:33,199 --> 10:48:37,519 pointer that is not initialized so it 14232 10:48:35,519 --> 10:48:39,755 will contain a junk address that you 14233 10:48:37,519 --> 10:48:41,595 shouldn't really use but we're not going 14234 10:48:39,756 --> 10:48:44,480 to do that because we're going to store 14235 10:48:41,595 --> 10:48:47,199 in the address of number one here and we 14236 10:48:44,480 --> 10:48:50,080 can manipulate the value here through 14237 10:48:47,199 --> 10:48:52,319 the pointer using the the reference 14238 10:48:50,080 --> 10:48:54,800 operator here and if we print this p 14239 10:48:52,319 --> 10:48:57,199 number one we're going to get 12 printed 14240 10:48:54,800 --> 10:49:00,639 out this is pretty cool okay so this is 14241 10:48:57,199 --> 10:49:02,319 how we have been using pointers so far 14242 10:49:00,639 --> 10:49:04,239 let's comment this out because we're 14243 10:49:02,319 --> 10:49:06,639 going to see some other things okay 14244 10:49:04,239 --> 10:49:08,799 before we really look at how dynamic 14245 10:49:06,639 --> 10:49:11,680 memory works we are going to show you a 14246 10:49:08,800 --> 10:49:13,916 few bad things you need to be aware of 14247 10:49:11,680 --> 10:49:15,275 so let's put in a piece of code for us 14248 10:49:15,275 --> 10:49:21,519 and here we are saying that writing into 14249 10:49:18,360 --> 10:49:23,275 uninitialized memory is really bad so 14250 10:49:23,275 --> 10:49:28,239 uninitialized and we are trying and 14251 10:49:25,839 --> 10:49:30,559 directly right into it through the 14252 10:49:28,239 --> 10:49:31,756 reference and this is going to be bad 14253 10:49:31,756 --> 10:49:36,480 visual studio code is not complaining it 14254 10:49:34,239 --> 10:49:39,115 thinks that we know what we are doing 14255 10:49:36,480 --> 10:49:41,200 here and it's not going to warn us but 14256 10:49:39,116 --> 10:49:43,840 this pointer could really be pointing 14257 10:49:41,199 --> 10:49:46,079 anywhere including operating system 14258 10:49:43,839 --> 10:49:47,359 specific memory and here you may be 14259 10:49:47,360 --> 10:49:53,040 modify things that your operating system 14260 10:49:50,080 --> 10:49:54,720 is going to need so clearly the 14261 10:49:53,040 --> 10:49:57,275 operating system is not going to be 14262 10:49:54,720 --> 10:49:59,040 happy about this and the bad things can 14263 10:49:59,040 --> 10:50:03,116 so now we have this memory location 14264 10:50:01,040 --> 10:50:04,720 we're going to try and write into it and 14265 10:50:03,116 --> 10:50:07,040 we're going to see what happens if we 14266 10:50:04,720 --> 10:50:08,639 run this program for now let's try and 14267 10:50:07,040 --> 10:50:10,559 take this little section here and 14268 10:50:10,559 --> 10:50:16,159 and we want to see what happens on the 14269 10:50:13,756 --> 10:50:17,756 program here the code here that we might 14270 10:50:17,756 --> 10:50:23,200 let's try and build it with gcc we're 14271 10:50:20,800 --> 10:50:24,720 going to run the task to do that 14272 10:50:23,199 --> 10:50:26,719 the build is going to be good you see 14273 10:50:24,720 --> 10:50:28,959 that we don't even get a compiler error 14274 10:50:26,720 --> 10:50:30,639 and if we're trying to run rooster you 14275 10:50:28,959 --> 10:50:32,559 see that the program is going to try and 14276 10:50:30,639 --> 10:50:34,639 run you know the operating system is 14277 10:50:32,559 --> 10:50:37,115 going to say this guy doesn't know what 14278 10:50:34,639 --> 10:50:39,595 they are doing and i am going to close 14279 10:50:37,116 --> 10:50:40,639 them you see that it's not even 14280 10:50:40,639 --> 10:50:46,080 this statement here let's go on top here 14281 10:50:43,519 --> 10:50:47,680 and put in an sddc out statement so that 14282 10:50:47,680 --> 10:50:52,800 where this program is failing 14283 10:50:54,959 --> 10:50:58,799 okay so this is what we have we have a 14284 10:50:56,639 --> 10:51:01,360 few statements other than and we want to 14285 10:50:58,800 --> 10:51:03,116 see if we actually get to run these two 14286 10:51:03,116 --> 10:51:09,596 where the program is actually failing 14287 10:51:06,000 --> 10:51:11,595 we are going to build it again with gcc 14288 10:51:11,595 --> 10:51:17,040 and run rooster you see that it's going 14289 10:51:13,680 --> 10:51:18,959 to say writing in a 55 but this 14290 10:51:17,040 --> 10:51:22,160 statement here is going to fail because 14291 10:51:18,959 --> 10:51:24,559 we are trying to ride into memory that 14292 10:51:22,160 --> 10:51:27,040 is not ours and the operating system is 14293 10:51:24,559 --> 10:51:30,000 going to stop us so our program is 14294 10:51:27,040 --> 10:51:32,559 crashing and it is terminating before it 14295 10:51:30,000 --> 10:51:34,879 even runs the things that are down here 14296 10:51:32,559 --> 10:51:36,799 hopefully you can see how bad this is 14297 10:51:34,879 --> 10:51:39,360 you have designed your program to do 14298 10:51:36,800 --> 10:51:41,756 things but it is going to stop 14299 10:51:39,360 --> 10:51:44,080 without having done any of the other 14300 10:51:41,756 --> 10:51:46,720 things that you have designed below so 14301 10:51:44,080 --> 10:51:47,916 this is really bad and another bad thing 14302 10:51:47,915 --> 10:51:53,435 you might have allocated some dynamic 14303 10:51:50,480 --> 10:51:56,640 memory beforehand and your program is 14304 10:51:53,436 --> 10:51:58,880 going to end before it even releases 14305 10:51:56,639 --> 10:52:01,436 that memory properly and returns that to 14306 10:51:58,879 --> 10:52:04,000 the operating system so this is bad you 14307 10:52:01,436 --> 10:52:07,200 don't want crashes in your programs and 14308 10:52:04,000 --> 10:52:10,400 don't ever try and use and reference 14309 10:52:07,199 --> 10:52:12,319 memory that you haven't initialized this 14310 10:52:10,400 --> 10:52:15,360 is one bad thing we're going to comment 14311 10:52:12,319 --> 10:52:18,080 this out i hope you really know how bad 14312 10:52:15,360 --> 10:52:21,915 this is and we're going to go down again 14313 10:52:18,080 --> 10:52:23,840 and uncomment what we have done here 14314 10:52:21,915 --> 10:52:26,079 and uh it's going to be another bad 14315 10:52:23,839 --> 10:52:27,755 thing because we are trying to write 14316 10:52:27,756 --> 10:52:32,239 initialized to null pqr okay here we 14317 10:52:30,400 --> 10:52:36,160 have a pointer this is called p number 14318 10:52:32,239 --> 10:52:38,720 three and it is initialized with no ptr 14319 10:52:36,160 --> 10:52:42,400 you can think of ptr as an address that 14320 10:52:38,720 --> 10:52:44,480 says i don't have any valid thing in me 14321 10:52:42,400 --> 10:52:46,160 don't use me that's what no ptr is 14322 10:52:46,160 --> 10:52:51,596 initialized to ptr but we are trying to 14323 10:52:48,879 --> 10:52:54,879 store something in here and you can 14324 10:52:51,595 --> 10:52:56,959 imagine how bad this is this is memory 14325 10:52:54,879 --> 10:52:58,719 that isn't really valid the memory 14326 10:52:56,959 --> 10:53:00,959 itself is saying i don't contain 14327 10:52:58,720 --> 10:53:02,720 anything valid don't try to use me and 14328 10:53:00,959 --> 10:53:05,040 if you try to use it bad things are 14329 10:53:02,720 --> 10:53:07,040 going to happen so if we try and compile 14330 10:53:05,040 --> 10:53:08,879 this program it is going to compile the 14331 10:53:07,040 --> 10:53:10,480 compiler is not going to stop us to do 14332 10:53:10,480 --> 10:53:14,720 and these are some things you need to be 14333 10:53:12,319 --> 10:53:16,480 aware of in c plus plus but if we try to 14334 10:53:14,720 --> 10:53:18,880 run it you're going to see that we are 14335 10:53:16,480 --> 10:53:21,040 also going to get a crash again let's 14336 10:53:18,879 --> 10:53:24,000 try and put a statement in front of this 14337 10:53:21,040 --> 10:53:25,275 failing line and uh below this line i 14338 10:53:30,319 --> 10:53:35,680 and both of the programs so that we can 14339 10:53:32,239 --> 10:53:39,040 see which std statements run and which 14340 10:53:35,680 --> 10:53:40,959 dot after our program crashes so we're 14341 10:53:39,040 --> 10:53:42,239 going to clear the world is good and run 14342 10:53:40,959 --> 10:53:44,639 rooster you're going to see it's going 14343 10:53:42,239 --> 10:53:46,720 to say what it's doing it's going to try 14344 10:53:44,639 --> 10:53:48,639 and fail and the operating system is 14345 10:53:46,720 --> 10:53:50,639 going to kill this program because it is 14346 10:53:48,639 --> 10:53:53,199 crazy it is doing what it shouldn't 14347 10:53:50,639 --> 10:53:55,756 really be doing so this is something bad 14348 10:53:53,199 --> 10:53:57,039 don't do it be aware of this thing okay 14349 10:53:55,756 --> 10:54:00,000 we're going to comment this out and 14350 10:53:57,040 --> 10:54:02,559 actually look at how we can dynamically 14351 10:54:00,000 --> 10:54:04,239 allocate memory from the heap and we're 14352 10:54:02,559 --> 10:54:05,360 going to put in a piece of code to play 14353 10:54:05,360 --> 10:54:09,840 it's not really complicated we have a 14354 10:54:07,040 --> 10:54:12,160 pointer we initialize it with no pointer 14355 10:54:09,839 --> 10:54:15,199 to say that this pointer doesn't point 14356 10:54:12,160 --> 10:54:16,080 to anywhere valid don't use it and we 14357 10:54:16,080 --> 10:54:21,520 calling new and when we do this the 14358 10:54:19,519 --> 10:54:23,839 operating system is going to give us a 14359 10:54:21,519 --> 10:54:25,519 new piece of memory on the heap 14360 10:54:23,839 --> 10:54:27,519 this piece of memory is going to have 14361 10:54:25,519 --> 10:54:29,680 the size enough to contain it so it's 14362 10:54:27,519 --> 10:54:31,680 going to be four bytes in memory and we 14363 10:54:29,680 --> 10:54:34,319 are going to be pointing to that piece 14364 10:54:31,680 --> 10:54:36,160 of memory using p number four here this 14365 10:54:34,319 --> 10:54:38,319 is what we are trying to say here the 14366 10:54:36,160 --> 10:54:40,640 moment we do this we own this piece of 14367 10:54:38,319 --> 10:54:42,480 memory and nobody else on the operating 14368 10:54:40,639 --> 10:54:44,720 system can use it so that's something 14369 10:54:42,480 --> 10:54:46,880 you should know once you have it you can 14370 10:54:44,720 --> 10:54:48,480 manipulate it through the pointer here 14371 10:54:46,879 --> 10:54:51,915 so here we can go through the the 14372 10:54:48,480 --> 10:54:53,596 reference operator and store in 77 and 14373 10:54:51,915 --> 10:54:55,915 if we print it out we're going to get 14374 10:54:53,595 --> 10:54:57,680 that printed out here okay this is 14375 10:54:55,915 --> 10:55:00,479 really cool this is the first time we 14376 10:54:57,680 --> 10:55:02,319 use hip memory and i am excited about 14377 10:55:00,480 --> 10:55:04,800 showing you this thanks we're going to 14378 10:55:02,319 --> 10:55:06,879 try and build this program and the build 14379 10:55:04,800 --> 10:55:08,080 is going to be good and we are going to 14380 10:55:08,080 --> 10:55:13,520 and if we run rooster we should see a 77 14381 10:55:13,519 --> 10:55:19,755 i'm going to get our 77 but this 77 is 14382 10:55:16,959 --> 10:55:21,839 stored on the heap now it is not stored 14383 10:55:19,756 --> 10:55:24,800 on the stack and this is really 14384 10:55:21,839 --> 10:55:27,115 really cool we can control when this 14385 10:55:24,800 --> 10:55:29,756 variable comes alive by allocating 14386 10:55:27,116 --> 10:55:32,639 memory on the heap and we exactly 14387 10:55:29,756 --> 10:55:34,319 control when this variable dies 14388 10:55:32,639 --> 10:55:36,800 if we want and we're going to see how we 14389 10:55:34,319 --> 10:55:39,040 can kill this variable from memory but 14390 10:55:36,800 --> 10:55:41,436 for now just take a moment and 14391 10:55:39,040 --> 10:55:43,595 appreciate how good it is to be able to 14392 10:55:41,436 --> 10:55:46,080 allocate stuff on the heap it is really 14393 10:55:43,595 --> 10:55:48,639 cool now that you have used the memory 14394 10:55:46,080 --> 10:55:50,800 it is really time to return it to the 14395 10:55:48,639 --> 10:55:52,639 system because we have no more use for 14396 10:55:50,800 --> 10:55:56,720 it the way you release memory to the 14397 10:55:52,639 --> 10:55:58,879 system is to call the delete keyword 14398 10:55:56,720 --> 10:56:01,436 and say the pointer pointing to the 14399 10:55:58,879 --> 10:56:03,199 memory that you are trying to release to 14400 10:56:01,436 --> 10:56:05,680 the system so we're going to say p 14401 10:56:03,199 --> 10:56:07,275 number four here let's bring this down a 14402 10:56:05,680 --> 10:56:09,840 little bit so that you can see the 14403 10:56:07,275 --> 10:56:11,915 entire story so we have a pointer here 14404 10:56:09,839 --> 10:56:13,915 and it is pointing to dynamically 14405 10:56:11,915 --> 10:56:15,915 allocated memory from the heap this 14406 10:56:15,915 --> 10:56:21,839 large because it stores an integer 14407 10:56:18,559 --> 10:56:24,399 we have it dynamically allocated we use 14408 10:56:21,839 --> 10:56:26,719 it here and it is time to return it to 14409 10:56:24,400 --> 10:56:28,880 the operating system okay i hope this is 14410 10:56:26,720 --> 10:56:30,319 clear the moment you call the delete 14411 10:56:30,319 --> 10:56:34,879 now the memory is going to be containing 14412 10:56:32,720 --> 10:56:37,200 some junk and you don't want to really 14413 10:56:34,879 --> 10:56:39,115 use that anymore if you try to use it 14414 10:56:37,199 --> 10:56:41,115 it's not your memory you're going to run 14415 10:56:39,116 --> 10:56:43,040 into trouble so for example after we 14416 10:56:41,116 --> 10:56:45,360 delete this memory if we try to use it 14417 10:56:43,040 --> 10:56:48,720 i'm going to show you here i think i can 14418 10:56:45,360 --> 10:56:50,959 let's say p number four and um storing 14419 10:56:48,720 --> 10:56:53,200 something notice that we are using the 14420 10:56:50,959 --> 10:56:55,519 the reference operator and let's try and 14421 10:56:55,519 --> 10:57:00,399 so let's build this with gcc as we 14422 10:56:58,080 --> 10:57:02,000 always do it is going to world compiler 14423 10:57:00,400 --> 10:57:03,916 is not going to stop you from doing 14424 10:57:03,915 --> 10:57:06,399 writing into memory that you have 14425 10:57:06,400 --> 10:57:12,000 released to the system but we're going 14426 10:57:08,800 --> 10:57:14,080 to see what happens so run rooster and 14427 10:57:12,000 --> 10:57:17,275 it is going to say done writing but you 14428 10:57:14,080 --> 10:57:20,400 see that the program has really crashed 14429 10:57:17,275 --> 10:57:22,559 because here we are trying to write into 14430 10:57:20,400 --> 10:57:25,520 memory that we don't really own and this 14431 10:57:22,559 --> 10:57:27,915 is bad don't do this so the moment you 14432 10:57:25,519 --> 10:57:29,595 delete the memory or the moment you 14433 10:57:27,915 --> 10:57:32,399 return the memory to the operating 14434 10:57:29,595 --> 10:57:35,040 system it is good practice to reset that 14435 10:57:32,400 --> 10:57:36,880 memory to no ptr so we're going to say p 14436 10:57:36,879 --> 10:57:42,799 make sure it contains no ptor 14437 10:57:40,639 --> 10:57:45,199 at least when we do this other people 14438 10:57:42,800 --> 10:57:47,436 have a chance to check and see that 14439 10:57:45,199 --> 10:57:49,755 that's valid memory before they use it 14440 10:57:47,436 --> 10:57:51,916 that's the use of resetting your memory 14441 10:57:49,756 --> 10:57:54,400 here so this is something you should do 14442 10:57:51,915 --> 10:57:56,399 again the flow to use dynamic memory is 14443 10:57:54,400 --> 10:57:58,720 really simple you set up a pointer that 14444 10:57:56,400 --> 10:57:59,916 is going to point to your memory 14445 10:57:59,915 --> 10:58:04,799 allocate the memory from the heap using 14446 10:58:02,400 --> 10:58:07,436 the new operator you're going to specify 14447 10:58:04,800 --> 10:58:08,960 the type you want to allocate memory for 14448 10:58:07,436 --> 10:58:10,400 the operating system is going to give 14449 10:58:08,959 --> 10:58:12,000 you that memory and you're going to be 14450 10:58:10,400 --> 10:58:14,319 pointing to that memory with your 14451 10:58:12,000 --> 10:58:16,720 pointer you are going to manipulate that 14452 10:58:14,319 --> 10:58:19,116 memory using your pointer and doing all 14453 10:58:16,720 --> 10:58:20,639 kinds of crazy things and after you are 14454 10:58:19,116 --> 10:58:23,596 done with that memory you're going to 14455 10:58:20,639 --> 10:58:24,639 release it to the operating system and 14456 10:58:24,639 --> 10:58:29,915 please remember to reset your pointers 14457 10:58:27,040 --> 10:58:31,840 after you have deleted them like this 14458 10:58:29,915 --> 10:58:34,559 that's going to make your life a whole 14459 10:58:31,839 --> 10:58:36,639 lot easier and other developers are 14460 10:58:34,559 --> 10:58:38,639 going to thank you for doing this 14461 10:58:36,639 --> 10:58:40,959 when they get to use your code because 14462 10:58:38,639 --> 10:58:43,040 your code is going to be much safer to 14463 10:58:40,959 --> 10:58:45,519 work with okay let's comment this out 14464 10:58:43,040 --> 10:58:47,840 and show you another example of what 14465 10:58:45,519 --> 10:58:49,915 cool things you can do with dynamic heap 14466 10:58:47,839 --> 10:58:51,519 memory we're going to go down and put in 14467 10:58:49,915 --> 10:58:52,879 a piece of code and show you that you 14468 10:58:52,879 --> 10:58:57,915 dynamically initialize your pointers at 14469 10:58:55,519 --> 10:59:00,000 declaration this is something you can do 14470 10:58:57,915 --> 10:59:02,079 the first one is not initialized so it's 14471 10:59:00,000 --> 10:59:03,756 going to contain junk volume p number 14472 10:59:02,080 --> 10:59:06,319 five if we try to print it we're going 14473 10:59:03,756 --> 10:59:08,400 to get some junk address print it out p 14474 10:59:06,319 --> 10:59:10,319 number six we're going to dereference it 14475 10:59:08,400 --> 10:59:12,239 and print the value inside so we're 14476 10:59:10,319 --> 10:59:14,159 going to see that and we can do the same 14477 10:59:12,239 --> 10:59:16,159 thing for p number six and p number 14478 10:59:14,160 --> 10:59:18,400 seven after we are done using the 14479 10:59:16,160 --> 10:59:20,320 pointers we are going to release the 14480 10:59:18,400 --> 10:59:22,480 memory here and notice that we are 14481 10:59:20,319 --> 10:59:24,239 resetting the pointers to no pointer to 14482 10:59:22,480 --> 10:59:27,040 say that they don't really contain 14483 10:59:24,239 --> 10:59:29,040 anything useful don't use them make sure 14484 10:59:27,040 --> 10:59:31,040 you have something valid and before you 14485 10:59:29,040 --> 10:59:32,720 use them this is the meaning here if 14486 10:59:31,040 --> 10:59:34,639 you're trying to run the program you can 14487 10:59:32,720 --> 10:59:36,800 guess what we're going to see so try and 14488 10:59:34,639 --> 10:59:38,879 guess and we're going to build we're 14489 10:59:36,800 --> 10:59:39,680 going to weld successfully going to 14490 10:59:39,680 --> 10:59:44,000 clear not clear and we're going to run 14491 10:59:42,080 --> 10:59:46,639 rooster and we're going to see that p 14492 10:59:44,000 --> 10:59:49,040 number five contains some junk address 14493 10:59:46,639 --> 10:59:51,519 okay and if we try to dereference that 14494 10:59:49,040 --> 10:59:54,000 we're going to get a junk value this is 14495 10:59:51,519 --> 10:59:56,479 really not useful don't use this value 14496 10:59:54,000 --> 10:59:58,400 because you didn't put this value in in 14497 10:59:56,480 --> 11:00:00,400 the first place you're going to see that 14498 10:59:58,400 --> 11:00:02,400 p number 6 is going to point to some 14499 11:00:00,400 --> 11:00:05,680 other address but we're going to have a 14500 11:00:02,400 --> 11:00:09,040 valid value inside because we have put 14501 11:00:05,680 --> 11:00:10,959 that n at initialization here the same 14502 11:00:09,040 --> 11:00:13,116 is true for p number seven it's going to 14503 11:00:10,959 --> 11:00:17,040 point to some address and we're going to 14504 11:00:13,116 --> 11:00:18,480 have 23 and because we put in our 23. 14505 11:00:17,040 --> 11:00:20,319 this is how you really use these 14506 11:00:18,480 --> 11:00:23,360 pointers another thing i want you to 14507 11:00:20,319 --> 11:00:26,480 know is that even if we have called 14508 11:00:23,360 --> 11:00:28,879 delete and reset these pointers they are 14509 11:00:26,480 --> 11:00:30,800 still available for use in our program 14510 11:00:28,879 --> 11:00:32,959 so if we want we can reuse these 14511 11:00:30,800 --> 11:00:34,800 pointers so for example p number five we 14512 11:00:34,800 --> 11:00:38,720 and use it to allocate new memory so we 14513 11:00:42,000 --> 11:00:46,400 okay so we can go down here and use that 14514 11:00:44,400 --> 11:00:47,680 this is going to be valid c plus plus 14515 11:00:47,680 --> 11:00:52,879 okay take a moment and look at it it is 14516 11:00:52,879 --> 11:00:56,399 and the visual studio code is going to 14517 11:00:54,559 --> 11:00:59,275 take this because it is valid c plus 14518 11:00:56,400 --> 11:01:01,756 plus code the memory has been returned 14519 11:00:59,275 --> 11:01:04,559 to the operating system and the pointer 14520 11:01:01,756 --> 11:01:06,800 has been reset to no ptr but we can 14521 11:01:04,559 --> 11:01:09,040 reuse this end pointer to allocate new 14522 11:01:06,800 --> 11:01:11,520 memory so we are allocating new memory 14523 11:01:09,040 --> 11:01:14,319 on the heap the memory is going to be 14524 11:01:14,319 --> 11:01:18,400 going to contain an integer and we are 14525 11:01:18,400 --> 11:01:22,880 to point to that new memory so this 14526 11:01:20,559 --> 11:01:23,595 pointer is being reused it was pointing 14527 11:01:23,595 --> 11:01:28,559 the memory that we allocated here which 14528 11:01:25,839 --> 11:01:30,239 was uninitialized and we are using that 14529 11:01:30,239 --> 11:01:35,199 memory location on the hip i really want 14530 11:01:32,239 --> 11:01:37,915 you to understand these things because 14531 11:01:35,199 --> 11:01:40,159 many beginners get them wrong and it is 14532 11:01:37,915 --> 11:01:43,115 hard to correct these things in your 14533 11:01:40,160 --> 11:01:45,276 mind once you have a wrong idea about 14534 11:01:43,116 --> 11:01:47,276 them in the first place so make sure you 14535 11:01:45,275 --> 11:01:50,239 understand this so if we print this 14536 11:01:47,275 --> 11:01:51,275 we're going to get the 81 printed out 14537 11:01:51,275 --> 11:01:55,756 we have allocated new memory we have to 14538 11:01:53,680 --> 11:01:58,160 remember to release it again so we have 14539 11:01:58,160 --> 11:02:03,040 p number five and we need to reset it to 14540 11:02:05,915 --> 11:02:09,680 once we do this we're going to be 14541 11:02:07,839 --> 11:02:12,239 playing it safe let's try and run and 14542 11:02:09,680 --> 11:02:14,160 see that we see the at1 printed out 14543 11:02:12,239 --> 11:02:16,000 we're going to build with gcc the world 14544 11:02:16,000 --> 11:02:21,199 and we are going to clear and run 14545 11:02:18,319 --> 11:02:23,839 rooster and you see that we see our 81 14546 11:02:21,199 --> 11:02:25,680 and we don't have any crazy crash for 14547 11:02:23,839 --> 11:02:28,559 example to prove that we don't have a 14548 11:02:25,680 --> 11:02:30,639 crash we can say that our program is 14549 11:02:34,959 --> 11:02:40,879 if the program has actually reached the 14550 11:02:37,839 --> 11:02:42,479 end before it dies we're going to build 14551 11:02:46,959 --> 11:02:51,595 and run rooster and you're going to see 14552 11:02:49,436 --> 11:02:53,360 that the program is ending well 14553 11:02:51,595 --> 11:02:57,435 the other thing i want you to see is 14554 11:02:53,360 --> 11:03:01,360 that it is really bad to call delete 14555 11:02:57,436 --> 11:03:04,800 twice on a pointer again it is really 14556 11:03:01,360 --> 11:03:06,559 bad to call delete twice on a pointer 14557 11:03:04,800 --> 11:03:08,560 don't do that okay we're going to see an 14558 11:03:08,559 --> 11:03:12,239 so we're going to allocate new memory 14559 11:03:10,160 --> 11:03:14,560 using p number five why not so we're 14560 11:03:12,239 --> 11:03:17,275 going to say p number five and we're 14561 11:03:14,559 --> 11:03:18,799 going to say new and going to allocate 14562 11:03:17,275 --> 11:03:22,239 new memory on the heap and we're going 14563 11:03:18,800 --> 11:03:24,720 to store in a 99 in that memory we can 14564 11:03:22,239 --> 11:03:26,959 print out the 99 so we're going to say 14565 11:03:24,720 --> 11:03:28,959 and after we are done using this memory 14566 11:03:26,959 --> 11:03:31,519 we are a good c plus plus citizen so 14567 11:03:33,756 --> 11:03:38,239 okay this is good it is going to work if 14568 11:03:35,839 --> 11:03:42,159 we will then run we're going to get what 14569 11:03:38,239 --> 11:03:45,680 we expect we should get a 99 printed out 14570 11:03:42,160 --> 11:03:47,916 last in our program before it ends and 14571 11:03:45,680 --> 11:03:49,915 we see that printed out here program 14572 11:03:47,915 --> 11:03:53,595 ending well this is really cool 14573 11:03:49,915 --> 11:03:56,079 but if we try to call delete twice 14574 11:03:56,080 --> 11:03:59,520 and try to boil you see that the 14575 11:03:57,595 --> 11:04:00,799 compiler is not going to stop us because 14576 11:04:02,720 --> 11:04:06,720 so this is something you need to be 14577 11:04:04,239 --> 11:04:07,839 aware of in c plus plus if we run this 14578 11:04:07,839 --> 11:04:11,839 you're going to see ah the program is 14579 11:04:09,756 --> 11:04:14,319 not going to end well the c plus plus 14580 11:04:11,839 --> 11:04:16,159 runtime or the operating system is going 14581 11:04:14,319 --> 11:04:18,400 to get here and see uh this guy is 14582 11:04:16,160 --> 11:04:20,560 trying to release memory twice and i 14583 11:04:18,400 --> 11:04:22,160 don't do that this is a bad program i am 14584 11:04:20,559 --> 11:04:24,639 going to kill it and it is going to be 14585 11:04:22,160 --> 11:04:27,116 killed the bad thing again is that 14586 11:04:24,639 --> 11:04:29,680 anything you have in your program after 14587 11:04:27,116 --> 11:04:31,840 these lines is not going to run because 14588 11:04:29,680 --> 11:04:34,639 the operating system is going to kill 14589 11:04:31,839 --> 11:04:37,199 your program at this line here so if you 14590 11:04:34,639 --> 11:04:39,839 had memory allocated that you planned to 14591 11:04:37,199 --> 11:04:41,435 release later on in your program your 14592 11:04:39,839 --> 11:04:43,435 memory is not going to be released 14593 11:04:41,436 --> 11:04:45,916 because those statements to release the 14594 11:04:43,436 --> 11:04:47,916 memory will never be run this is where 14595 11:04:45,915 --> 11:04:49,595 the program is going to be killed the 14596 11:04:47,915 --> 11:04:52,159 operating system is going to see that 14597 11:04:49,595 --> 11:04:55,199 you are a rug program and it is going to 14598 11:04:52,160 --> 11:04:57,276 kill the program so don't delete your 14599 11:04:55,199 --> 11:04:59,115 memory twice this is something you 14600 11:04:57,275 --> 11:05:01,115 should really watch out for and you 14601 11:04:59,116 --> 11:05:03,596 might think uh-huh i know this why would 14602 11:05:01,116 --> 11:05:06,080 i call delete twice in a single line 14603 11:05:03,595 --> 11:05:08,639 here well you may not do it like this 14604 11:05:06,080 --> 11:05:09,756 but your program might have multiple 14605 11:05:09,756 --> 11:05:13,680 where you have pointers pointing to the 14606 11:05:11,595 --> 11:05:15,839 same memory and if you try to delete 14607 11:05:13,680 --> 11:05:17,840 that memory twice you're going to get 14608 11:05:15,839 --> 11:05:19,679 this error and this is really good so 14609 11:05:17,839 --> 11:05:21,115 watch out for these kinds of things and 14610 11:05:19,680 --> 11:05:22,080 we're going to see different ways you 14611 11:05:22,080 --> 11:05:26,959 use to really make sure you are doing 14612 11:05:24,959 --> 11:05:28,400 things right in c plus plus this is 14613 11:05:26,959 --> 11:05:31,275 really all we set out to do in this 14614 11:05:28,400 --> 11:05:34,800 lecture giving you first-hand practice 14615 11:05:31,275 --> 11:05:37,040 on how to do dynamic memory allocation i 14616 11:05:34,800 --> 11:05:38,800 hope you found this useful we are going 14617 11:05:37,040 --> 11:05:40,559 to stop here in this lecture and the 14618 11:05:38,800 --> 11:05:42,240 next one we're going to learn about 14619 11:05:42,239 --> 11:05:46,799 go ahead and finish up here and we'd be 14620 11:05:44,720 --> 11:05:49,040 there in this lecture we're going to 14621 11:05:46,800 --> 11:05:51,360 focus on dangling pointers and show you 14622 11:05:49,040 --> 11:05:54,160 how they can be bad a dangling pointer 14623 11:05:51,360 --> 11:05:56,160 is a pointer that is pointing to a valid 14624 11:05:54,160 --> 11:05:58,160 address and we have seen a few examples 14625 11:05:56,160 --> 11:06:00,160 of this already but in this lecture 14626 11:05:58,160 --> 11:06:02,320 we're going to focus on that and show 14627 11:06:00,160 --> 11:06:04,800 you common solutions we can apply to 14628 11:06:02,319 --> 11:06:06,959 these problems here and download 14629 11:06:04,800 --> 11:06:09,040 pointers are really bad they are going 14630 11:06:06,959 --> 11:06:11,680 to lead you programs to undefined 14631 11:06:09,040 --> 11:06:13,275 behavior and we have seen that that can 14632 11:06:13,275 --> 11:06:17,915 many times there are three kinds of 14633 11:06:16,239 --> 11:06:19,915 dangling pointers we're going to look at 14634 11:06:17,915 --> 11:06:22,399 in this lecture the first one is a 14635 11:06:19,915 --> 11:06:25,360 pointer that is not initialized the 14636 11:06:22,400 --> 11:06:28,000 second one is a deleted pointer and the 14637 11:06:25,360 --> 11:06:29,915 third case may come if you have multiple 14638 11:06:28,000 --> 11:06:32,879 pointers pointing to the same memory 14639 11:06:29,915 --> 11:06:35,199 location and let's look at these cases 14640 11:06:32,879 --> 11:06:37,839 one by one here we have an example of a 14641 11:06:35,199 --> 11:06:40,159 pointer that is not properly initialized 14642 11:06:37,839 --> 11:06:40,959 this is p number five it is a pointer to 14643 11:06:40,959 --> 11:06:44,879 and it is not initialized it doesn't 14644 11:06:42,720 --> 11:06:47,275 really point anywhere and it is going to 14645 11:06:44,879 --> 11:06:49,040 contain junk address if we try to 14646 11:06:47,275 --> 11:06:51,360 dereference it we're going to get bad 14647 11:06:49,040 --> 11:06:54,239 things to happen because we don't really 14648 11:06:51,360 --> 11:06:56,160 know which address is stored in there 14649 11:06:54,239 --> 11:06:58,879 and putting something in there might 14650 11:06:56,160 --> 11:07:01,116 lead to a crash so this is something you 14651 11:06:58,879 --> 11:07:03,275 need to watch out for the second kind of 14652 11:07:01,116 --> 11:07:06,240 dangling pointer that can really lead 14653 11:07:03,275 --> 11:07:09,275 you to bad places is a deleted pointer 14654 11:07:06,239 --> 11:07:12,079 here is a pointer p number one it is 14655 11:07:09,275 --> 11:07:14,079 initialized with dynamic memory and we 14656 11:07:12,080 --> 11:07:16,240 can use it for example here we are 14657 11:07:14,080 --> 11:07:18,720 dereferencing the pointer and printing 14658 11:07:16,239 --> 11:07:21,275 the value inside and deleting the 14659 11:07:18,720 --> 11:07:23,840 pointer after we delete the pointer 14660 11:07:21,275 --> 11:07:26,319 don't really use it before you properly 14661 11:07:23,839 --> 11:07:28,799 initialize it with some kind of memory 14662 11:07:26,319 --> 11:07:31,275 if we try to do that this is going to be 14663 11:07:28,800 --> 11:07:33,756 undefined behavior and your program 14664 11:07:31,275 --> 11:07:36,079 might crash so make sure you don't do 14665 11:07:33,756 --> 11:07:38,080 something like this another case is if 14666 11:07:36,080 --> 11:07:40,880 you have multiple pointers pointing to 14667 11:07:38,080 --> 11:07:43,520 the same address and here is a simple 14668 11:07:40,879 --> 11:07:46,159 example to drive the point home we have 14669 11:07:43,519 --> 11:07:49,199 pointer three which is pointing to 14670 11:07:46,160 --> 11:07:52,560 dynamic memory here we are using new ant 14671 11:07:49,199 --> 11:07:55,360 in our braced initializer and we are 14672 11:07:52,559 --> 11:07:56,479 putting value 83 in that pointer 14673 11:07:56,480 --> 11:08:01,436 and here we set up another pointer p 14674 11:07:58,639 --> 11:08:03,680 number four and we initialize that with 14675 11:08:01,436 --> 11:08:05,200 another pointer and if we print these 14676 11:08:03,680 --> 11:08:07,275 things out we're going to get that they 14677 11:08:05,199 --> 11:08:08,799 contain the same address and they 14678 11:08:07,275 --> 11:08:09,839 contain the same value we're going to 14679 11:08:12,559 --> 11:08:16,959 and try to use p number four later we're 14680 11:08:15,040 --> 11:08:19,360 going to get problems because this 14681 11:08:16,959 --> 11:08:20,319 memory has been deleted by p number 14682 11:08:20,319 --> 11:08:24,800 but p number four is still pointing to 14683 11:08:22,800 --> 11:08:27,360 that and if it tries to use that it's 14684 11:08:24,800 --> 11:08:29,200 going to be deleted memory basically and 14685 11:08:27,360 --> 11:08:32,480 we're going to get undefined behavior so 14686 11:08:29,199 --> 11:08:34,879 be sure you know these problems and try 14687 11:08:32,480 --> 11:08:37,360 to avoid them in your programs okay 14688 11:08:34,879 --> 11:08:40,239 these are problems but how do we solve 14689 11:08:37,360 --> 11:08:42,400 them well there are a few solutions one 14690 11:08:40,239 --> 11:08:44,639 is to always initialize your pointers 14691 11:08:42,400 --> 11:08:46,959 the moment you declare a pointer make 14692 11:08:44,639 --> 11:08:48,800 sure it is initialized if you don't know 14693 11:08:46,959 --> 11:08:51,360 what you can initialize that pointer 14694 11:08:48,800 --> 11:08:53,200 with put in null pointer first and then 14695 11:08:51,360 --> 11:08:56,000 you're going to put in an address later 14696 11:08:53,199 --> 11:08:57,915 when you have a clear idea of which 14697 11:08:56,000 --> 11:09:00,720 address you can store in the second 14698 11:08:57,915 --> 11:09:04,239 solution is to reset your pointers to 14699 11:09:00,720 --> 11:09:06,000 now ptr after you delete the memory 14700 11:09:04,239 --> 11:09:08,319 that's the second thing you should do 14701 11:09:06,000 --> 11:09:10,400 every time you call delete that's going 14702 11:09:08,319 --> 11:09:12,879 to give other developers or even 14703 11:09:10,400 --> 11:09:14,480 yourself a chance to check and see if 14704 11:09:14,480 --> 11:09:19,436 contains a valid address before you do 14705 11:09:16,959 --> 11:09:22,080 that and for multiple pointers pointing 14706 11:09:19,436 --> 11:09:24,959 to the same address you have to choose 14707 11:09:22,080 --> 11:09:26,959 and make one pointer the master of the 14708 11:09:26,959 --> 11:09:32,080 and all pointers are basically going to 14709 11:09:29,116 --> 11:09:34,400 be slaves which can't release the memory 14710 11:09:32,080 --> 11:09:36,240 but they can read and do something with 14711 11:09:34,400 --> 11:09:39,275 that memory that they don't have the 14712 11:09:36,239 --> 11:09:41,199 task of releasing that memory and here 14713 11:09:39,275 --> 11:09:43,756 is how you can apply solution one for 14714 11:09:41,199 --> 11:09:45,839 example we have pointer number five and 14715 11:09:43,756 --> 11:09:48,639 we're going to brace initialize it this 14716 11:09:48,639 --> 11:09:53,275 null pointer the other thing you can do 14717 11:09:50,800 --> 11:09:55,276 is initialize the pointer with dynamic 14718 11:09:53,275 --> 11:09:57,436 memory like we are doing here with p 14719 11:09:55,275 --> 11:09:59,360 number six and this is going to put in 14720 11:09:57,436 --> 11:10:00,880 the value and we are going to be playing 14721 11:10:00,879 --> 11:10:05,435 now if your pointers are initialized 14722 11:10:03,519 --> 11:10:08,559 developers have a chance to check your 14723 11:10:05,436 --> 11:10:11,596 pointers against no ptr to see that they 14724 11:10:08,559 --> 11:10:13,915 contain something valid before they use 14725 11:10:11,595 --> 11:10:16,399 it and if they see that it contains no 14726 11:10:13,915 --> 11:10:19,040 ptr they are not going to try and use it 14727 11:10:16,400 --> 11:10:21,916 and this is going to be very safe for 14728 11:10:19,040 --> 11:10:24,400 the second problem with deleted pointers 14729 11:10:21,915 --> 11:10:26,159 we are going to reset our pointers after 14730 11:10:24,400 --> 11:10:28,480 we call delete for example here we have 14731 11:10:26,160 --> 11:10:31,756 pin number seven initialized with 14732 11:10:28,480 --> 11:10:33,916 dynamic memory and 82 is stored in we 14733 11:10:31,756 --> 11:10:35,840 can print it out all do things with it 14734 11:10:33,915 --> 11:10:38,479 when we delete the memory we're going to 14735 11:10:35,839 --> 11:10:40,799 reset it to null p2r so that other 14736 11:10:38,480 --> 11:10:43,596 developers or even ourselves can check 14737 11:10:40,800 --> 11:10:46,800 this memory against no ptr and if it's 14738 11:10:43,595 --> 11:10:48,799 different from no ptr we can use it if 14739 11:10:46,800 --> 11:10:50,560 it's equal to no ptr we're going to 14740 11:10:48,800 --> 11:10:53,116 leave it alone and do something else 14741 11:10:50,559 --> 11:10:55,040 this is going to be very safe if you 14742 11:10:53,116 --> 11:10:56,880 have multiple pointers pointing to the 14743 11:10:55,040 --> 11:10:58,720 same address like we have here then 14744 11:10:56,879 --> 11:11:01,115 you're going to have to decide which 14745 11:10:58,720 --> 11:11:03,200 pointer is the master pointer and other 14746 11:11:06,480 --> 11:11:10,800 all the other pointers are going to 14747 11:11:08,160 --> 11:11:13,116 check against the master pointer and see 14748 11:11:10,800 --> 11:11:15,276 that it contains valid memory because 14749 11:11:13,116 --> 11:11:17,436 the master pointer has the task of 14750 11:11:15,275 --> 11:11:19,275 deleting this memory let's go through 14751 11:11:17,436 --> 11:11:20,720 this and see how this could work in 14752 11:11:20,720 --> 11:11:26,239 so p number five is our master pointer 14753 11:11:23,680 --> 11:11:29,360 we are setting p number five and 14754 11:11:26,239 --> 11:11:31,360 initializing it with p number eight we 14755 11:11:29,360 --> 11:11:33,360 are creating things using p number eight 14756 11:11:31,360 --> 11:11:35,360 we can either use p number eight or pin 14757 11:11:33,360 --> 11:11:37,915 urban eye this is going to work but 14758 11:11:35,360 --> 11:11:40,080 before we use the memory location we're 14759 11:11:37,915 --> 11:11:42,319 going to check against the master 14760 11:11:40,080 --> 11:11:44,240 pointer if the master pointer contains 14761 11:11:42,319 --> 11:11:46,720 no ptr we're not going to use this 14762 11:11:44,239 --> 11:11:49,275 memory if the master pointer doesn't 14763 11:11:46,720 --> 11:11:51,116 contain now ptr we're going to use this 14764 11:11:49,275 --> 11:11:53,275 memory here okay if you go down you see 14765 11:11:51,116 --> 11:11:55,200 that the master pointer is going to be 14766 11:11:53,275 --> 11:11:58,159 releasing the memory and if we try to 14767 11:11:55,199 --> 11:11:59,839 use that memory after it was deleted 14768 11:11:58,160 --> 11:12:02,160 we're going to see that it contains now 14769 11:11:59,839 --> 11:12:04,319 ptr and we're not going to be using it 14770 11:12:02,160 --> 11:12:06,800 this is something cool and it is really 14771 11:12:04,319 --> 11:12:08,959 easy to implement this safety guidelines 14772 11:12:06,800 --> 11:12:10,639 in your programs now that you know about 14773 11:12:08,959 --> 11:12:13,116 this we're going to go to visual studio 14774 11:12:10,639 --> 11:12:16,159 code and show you this in action okay 14775 11:12:13,116 --> 11:12:18,880 here we are in our working directory the 14776 11:12:16,160 --> 11:12:21,116 current project is dangling pointers so 14777 11:12:18,879 --> 11:12:24,719 we're going to grab our template files 14778 11:12:21,116 --> 11:12:26,240 pretty quick and put that in our project 14779 11:12:24,720 --> 11:12:29,200 and we're going to open this in visual 14780 11:12:26,239 --> 11:12:31,519 studio code we are going to look at the 14781 11:12:29,199 --> 11:12:33,115 first case of a dangling pointer and 14782 11:12:31,519 --> 11:12:36,399 this is a pointer that is not 14783 11:12:33,116 --> 11:12:38,240 initialized properly like p number here 14784 11:12:36,400 --> 11:12:40,560 if we try to reference it we're going to 14785 11:12:40,559 --> 11:12:45,756 so let's see that and let's go down and 14786 11:12:43,040 --> 11:12:47,680 say that program is ending well just to 14787 11:12:45,756 --> 11:12:49,595 know that we didn't get a crash because 14788 11:12:47,680 --> 11:12:52,319 a crash is basically going to terminate 14789 11:12:49,595 --> 11:12:54,399 your program on the offending line 14790 11:12:52,319 --> 11:12:55,519 that's what it's going to do so if the 14791 11:12:55,519 --> 11:12:59,359 works well and ends well we're going to 14792 11:13:01,519 --> 11:13:06,479 gcc to build this program and let's open 14793 11:13:06,480 --> 11:13:09,436 run it we're going to clear the world 14794 11:13:09,436 --> 11:13:14,800 and we're going to run rooster 14795 11:13:11,519 --> 11:13:17,519 you see that the program didn't end well 14796 11:13:14,800 --> 11:13:18,960 it crashed and it crashed because we are 14797 11:13:18,959 --> 11:13:23,275 uninitialized memory when the operating 14798 11:13:21,519 --> 11:13:25,199 system is going to try and run this 14799 11:13:23,275 --> 11:13:27,756 thing it's going to see ah this guy is 14800 11:13:25,199 --> 11:13:29,755 trying to modify memory that doesn't 14801 11:13:27,756 --> 11:13:31,840 really belong to him it is a road 14802 11:13:29,756 --> 11:13:34,400 program i am going to kill it and it is 14803 11:13:31,839 --> 11:13:35,839 going to be terminated on this line here 14804 11:13:35,839 --> 11:13:40,079 you should avoid these kinds of 14805 11:13:37,199 --> 11:13:41,839 situations so don't do this we are going 14806 11:13:40,080 --> 11:13:44,400 to comment this out and show you another 14807 11:13:41,839 --> 11:13:47,595 offending event we can do in our program 14808 11:13:44,400 --> 11:13:48,800 and that is using a deleted pointer if 14809 11:13:47,595 --> 11:13:51,040 you do that you're going to get 14810 11:13:48,800 --> 11:13:53,916 something bad in your programs 14811 11:13:51,040 --> 11:13:57,275 so here we have a small example we have 14812 11:13:53,915 --> 11:13:59,360 a pointer to end we initialize this with 14813 11:13:57,275 --> 11:14:01,519 a dynamic piece of memory and we're 14814 11:14:01,519 --> 11:14:04,879 and we can print this out this is going 14815 11:14:05,680 --> 11:14:10,319 we delete the memory down here and we 14816 11:14:07,680 --> 11:14:12,639 try to use it after we have deleted the 14817 11:14:10,319 --> 11:14:14,480 memory so the operating system is not 14818 11:14:12,639 --> 11:14:17,199 going to be happy about this because 14819 11:14:14,480 --> 11:14:20,319 once the memory is deleted it is no 14820 11:14:17,199 --> 11:14:22,319 longer our memory so we have no business 14821 11:14:20,319 --> 11:14:24,720 the referencing pointer one and trying 14822 11:14:22,319 --> 11:14:26,000 to use that here and we're going to get 14823 11:14:26,000 --> 11:14:30,400 and this is something you should avoid 14824 11:14:27,680 --> 11:14:33,275 so let's try and build this and show you 14825 11:14:30,400 --> 11:14:35,275 this in action we are going to clear 14826 11:14:35,275 --> 11:14:39,756 and you're going to see that program is 14827 11:14:37,116 --> 11:14:42,639 ending well but in some cases it might 14828 11:14:39,756 --> 11:14:43,840 crash and hopefully this shows you the 14829 11:14:43,839 --> 11:14:49,039 that these things might put you into 14830 11:14:46,720 --> 11:14:51,756 sometimes your program is going to seem 14831 11:14:49,040 --> 11:14:54,160 working fine but sometimes when this 14832 11:14:51,756 --> 11:14:56,480 memory that we are pointing to in p 14833 11:14:54,160 --> 11:14:58,000 number one here is being used to do 14834 11:14:56,480 --> 11:15:00,400 something really important by the 14835 11:14:58,000 --> 11:15:02,720 operating system you are going to get a 14836 11:15:00,400 --> 11:15:05,275 crash so you're not guaranteed to get 14837 11:15:02,720 --> 11:15:07,360 the same behavior every time you run 14838 11:15:05,275 --> 11:15:09,915 your program some of you might be saying 14839 11:15:07,360 --> 11:15:13,275 aha the program is working here daniel 14840 11:15:09,915 --> 11:15:15,915 why are you making this such a big deal 14841 11:15:13,275 --> 11:15:18,239 well it is a big deal because you are 14842 11:15:15,915 --> 11:15:21,435 not guaranteed to get the same results 14843 11:15:18,239 --> 11:15:24,000 every time you run this program so be 14844 11:15:21,436 --> 11:15:26,000 sure you really avoid these kinds of 14845 11:15:24,000 --> 11:15:27,595 situations let's try and clear and run 14846 11:15:26,000 --> 11:15:30,639 the program again hopefully we're going 14847 11:15:27,595 --> 11:15:33,199 to see it crash but we're not okay so 14848 11:15:30,639 --> 11:15:35,116 let's clear and rebuild again 14849 11:15:33,199 --> 11:15:36,719 and see that we have a better luck that 14850 11:15:38,400 --> 11:15:43,116 and run rooster it is going to end well 14851 11:15:41,040 --> 11:15:45,595 but you notice that we are trying to use 14852 11:15:43,116 --> 11:15:48,480 a piece of memory that has already been 14853 11:15:45,595 --> 11:15:51,040 deleted and this is bad don't do this 14854 11:15:48,480 --> 11:15:52,640 make sure you don't do something like 14855 11:15:51,040 --> 11:15:55,200 this we're going to comment this out 14856 11:15:52,639 --> 11:15:57,915 again and show you another offending 14857 11:15:55,199 --> 11:16:00,159 thing we can do with our programs and 14858 11:15:57,915 --> 11:16:02,879 bad things might happen if you try to 14859 11:16:00,160 --> 11:16:04,640 have multiple pointers played into the 14860 11:16:02,879 --> 11:16:07,040 same address and that's what we have 14861 11:16:04,639 --> 11:16:09,595 here we have p number three and p number 14862 11:16:07,040 --> 11:16:12,080 four here p number three is allocating 14863 11:16:09,595 --> 11:16:15,680 new space and it is pointing to it we 14864 11:16:12,080 --> 11:16:17,840 have an 83 inside and a p number four is 14865 11:16:15,680 --> 11:16:20,319 initialized with p number three we can 14866 11:16:17,839 --> 11:16:23,435 do that so these two pointers are 14867 11:16:20,319 --> 11:16:24,720 basically pointing to the same memory 14868 11:16:24,720 --> 11:16:28,959 let's run this and show you that 14869 11:16:27,040 --> 11:16:32,080 we're going to comment out this delete 14870 11:16:28,959 --> 11:16:34,479 statement here and run the program first 14871 11:16:32,080 --> 11:16:36,720 to really prove that they are pointing 14872 11:16:34,480 --> 11:16:40,080 to the same addresses and they contain 14873 11:16:36,720 --> 11:16:43,200 the same values let's build and run 14874 11:16:40,080 --> 11:16:45,200 we're going to use gcc for that 14875 11:16:43,199 --> 11:16:46,719 and we're going to clear and run rooster 14876 11:16:46,720 --> 11:16:51,275 p number three the address is this and 14877 11:16:48,959 --> 11:16:52,799 it contains an 83 p number four contains 14878 11:16:52,800 --> 11:16:58,480 so let's see what happens if we try to 14879 11:16:58,480 --> 11:17:03,360 and using p number four later to try and 14880 11:17:01,519 --> 11:17:04,159 read stuff this is what we are doing 14881 11:17:04,160 --> 11:17:08,800 and we're going to see if the program 14882 11:17:08,800 --> 11:17:12,560 we're going to build again the world is 14883 11:17:10,480 --> 11:17:14,880 going to be good let's bring this up and 14884 11:17:14,879 --> 11:17:18,479 and you're going to see that the program 14885 11:17:18,480 --> 11:17:23,596 but we are using a deleted piece of 14886 11:17:21,116 --> 11:17:26,240 memory here and anything can really 14887 11:17:23,595 --> 11:17:28,479 happen okay so you might get garbage you 14888 11:17:26,239 --> 11:17:32,159 might have really any piece of junk 14889 11:17:28,480 --> 11:17:34,720 inside or you might get a crush in some 14890 11:17:32,160 --> 11:17:37,436 extreme cases so this is really bad 14891 11:17:34,720 --> 11:17:40,080 don't do it don't try to use a piece of 14892 11:17:37,436 --> 11:17:42,319 memory that you have already deleted 14893 11:17:40,080 --> 11:17:44,319 through a pointer and these are problems 14894 11:17:42,319 --> 11:17:47,116 you might run into in your programs if 14895 11:17:44,319 --> 11:17:49,756 you try to use dynamic memory allocation 14896 11:17:47,116 --> 11:17:51,040 let's look at how we can solve them 14897 11:17:53,680 --> 11:17:58,959 make sure you put a little ptr in a 14898 11:17:56,080 --> 11:18:01,116 pointer if you don't know what that 14899 11:17:58,959 --> 11:18:03,519 pointer is actually stored at okay let's 14900 11:18:01,116 --> 11:18:05,360 go down and look at solution one 14901 11:18:03,519 --> 11:18:08,239 solution one is to initialize your 14902 11:18:05,360 --> 11:18:10,160 pointer to no pointer every time if you 14903 11:18:08,239 --> 11:18:12,720 don't have a value you can put in use 14904 11:18:10,160 --> 11:18:15,360 null ptr that's what we are using here 14905 11:18:12,720 --> 11:18:18,080 implicitly but we can explicitly put 14906 11:18:15,360 --> 11:18:20,639 that end because it is good and before 14907 11:18:18,080 --> 11:18:23,436 you use a pointer you have to check and 14908 11:18:20,639 --> 11:18:26,000 see if it doesn't contain null pointer 14909 11:18:23,436 --> 11:18:28,160 this is how we do it if pointer six is 14910 11:18:26,000 --> 11:18:30,639 different from null pointer we're going 14911 11:18:28,160 --> 11:18:31,916 to use it if it is equal to no pointer 14912 11:18:30,639 --> 11:18:34,159 we're not going to use it and we're 14913 11:18:31,915 --> 11:18:35,680 going to be playing on the safe side and 14914 11:18:34,160 --> 11:18:38,160 you're going to see that the program is 14915 11:18:35,680 --> 11:18:41,360 going to end well here if we have a null 14916 11:18:44,400 --> 11:18:49,756 invalid address okay and that the user 14917 11:18:47,360 --> 11:18:52,319 is going to know that they are trying to 14918 11:18:49,756 --> 11:18:54,800 do something really bad so let's try and 14919 11:18:54,800 --> 11:19:00,800 so let's go and store a null pointer in 14920 11:18:57,756 --> 11:19:02,880 p number six to really try this out and 14921 11:19:00,800 --> 11:19:05,200 show you that we're going to get this 14922 11:19:02,879 --> 11:19:07,755 warning if we're trying to use it 14923 11:19:05,199 --> 11:19:10,239 and we're going to build with gcc and 14924 11:19:07,756 --> 11:19:12,319 we're going to clear and run rooster 14925 11:19:10,239 --> 11:19:14,639 you're going to see that invalid address 14926 11:19:12,319 --> 11:19:17,680 and the program is going to end well 14927 11:19:14,639 --> 11:19:19,680 and if we try to put this statement 14928 11:19:17,680 --> 11:19:21,519 somewhere where we're not shaking 14929 11:19:21,519 --> 11:19:26,079 no pointer we are going to get a crash 14930 11:19:23,915 --> 11:19:29,115 so let's comment this out so that it's 14931 11:19:26,080 --> 11:19:30,639 clear what it is we are running here 14932 11:19:29,116 --> 11:19:32,720 so if we run this we're going to get a 14933 11:19:30,639 --> 11:19:34,239 crash we have seen that but let's show 14934 11:19:34,239 --> 11:19:39,595 and really highlight that we are solving 14935 11:19:37,199 --> 11:19:41,519 the problem here if we run rooster the 14936 11:19:39,595 --> 11:19:43,159 program is going to crash because we are 14937 11:19:44,800 --> 11:19:50,880 memory but if we try and check against 14938 11:19:48,000 --> 11:19:52,800 no ptr like we are doing here let's do 14939 11:19:50,879 --> 11:19:54,799 that we're going to be playing on the 14940 11:19:52,800 --> 11:19:56,960 safe side because it's going to tell us 14941 11:19:54,800 --> 11:19:59,360 invalid address and we're not going to 14942 11:19:56,959 --> 11:20:01,199 get a crush and the program is going to 14943 11:19:59,360 --> 11:20:02,720 end well this is really cool so let's 14944 11:20:01,199 --> 11:20:05,115 try and do that again we're going to 14945 11:20:05,436 --> 11:20:10,000 the world is going to be good we can 14946 11:20:07,199 --> 11:20:13,519 clear and run rooster and the program is 14947 11:20:10,000 --> 11:20:15,839 going to end well okay so if we have a 14948 11:20:13,519 --> 11:20:18,319 value inside we're going to get that so 14949 11:20:15,839 --> 11:20:21,115 let's put that n and say new ant and put 14950 11:20:18,319 --> 11:20:23,040 in an 87 for example it doesn't really 14951 11:20:23,040 --> 11:20:26,879 so we're going to build again the work 14952 11:20:24,800 --> 11:20:29,200 is going to be good and we can run 14953 11:20:26,879 --> 11:20:31,915 rooster and you see that we are getting 14954 11:20:29,199 --> 11:20:34,639 a value here if we are not storing no 14955 11:20:31,915 --> 11:20:37,360 ptlr in p number six and you can do the 14956 11:20:34,639 --> 11:20:40,239 same checks against p number five but i 14957 11:20:37,360 --> 11:20:43,840 think this is really enough so this is 14958 11:20:40,239 --> 11:20:45,595 one way you can avoid dangling pointers 14959 11:20:43,839 --> 11:20:48,239 the next thing we're going to look at is 14960 11:20:45,595 --> 11:20:50,479 if we are using a deleted pointer and 14961 11:20:48,239 --> 11:20:52,879 again the solution is to set a pointer 14962 11:20:52,879 --> 11:20:59,040 after we delete it here we have p number 14963 11:20:55,595 --> 11:21:01,115 seven it has 82 inside we are using it 14964 11:20:59,040 --> 11:21:03,360 here and printing stuff out we're going 14965 11:21:01,116 --> 11:21:05,276 to delete it after we are done using it 14966 11:21:05,275 --> 11:21:11,115 the first thing we do is to reset it to 14967 11:21:07,839 --> 11:21:13,359 no ptr when we do that other people or 14968 11:21:11,116 --> 11:21:15,916 even ourselves have a chance to check it 14969 11:21:13,360 --> 11:21:18,319 against no ptr and we're going to know 14970 11:21:15,915 --> 11:21:19,360 that it doesn't contain anything 14971 11:21:19,360 --> 11:21:23,116 for our program and if we do something 14972 11:21:23,116 --> 11:21:27,276 we're not going to run into all these 14973 11:21:27,275 --> 11:21:30,799 and our program is going to have 14974 11:21:30,800 --> 11:21:34,639 across different runs and this is really 14975 11:21:32,959 --> 11:21:36,879 cool so we're going to run this we're 14976 11:21:34,639 --> 11:21:39,756 going to get things printed out and the 14977 11:21:36,879 --> 11:21:43,680 program is going to end well because 14978 11:21:39,756 --> 11:21:45,916 right now we are checking against no ptr 14979 11:21:43,680 --> 11:21:48,720 we can even be explicit in showing the 14980 11:21:45,915 --> 11:21:51,755 warning here we can say invalid memory 14981 11:21:48,720 --> 11:21:53,275 access and if you run this program 14982 11:21:51,756 --> 11:21:55,275 you're going to really know this so 14983 11:21:53,275 --> 11:21:57,839 let's finish this up and we're going to 14984 11:21:55,275 --> 11:21:59,839 bolt and the bulk is going to be good if 14985 11:21:57,839 --> 11:22:02,000 we run this and run rooster we're going 14986 11:21:59,839 --> 11:22:04,799 to know that we are doing any valid 14987 11:22:02,000 --> 11:22:06,480 memory access after we have deleted the 14988 11:22:04,800 --> 11:22:09,276 pointer and this is going to save you 14989 11:22:06,480 --> 11:22:11,916 from possible crashes that you might 14990 11:22:09,275 --> 11:22:14,400 experience if you don't do this so this 14991 11:22:11,915 --> 11:22:16,399 is a second solution you might want to 14992 11:22:16,400 --> 11:22:20,800 and i'm going to comment it out and show 14993 11:22:18,400 --> 11:22:23,436 you how you can get away from the 14994 11:22:23,436 --> 11:22:27,840 multiple pointers pointing to the same 14995 11:22:25,680 --> 11:22:29,915 address we are going to go down and put 14996 11:22:27,839 --> 11:22:32,559 in the code it is what you have seen in 14997 11:22:29,915 --> 11:22:34,959 the slides and it is really nothing new 14998 11:22:32,559 --> 11:22:37,275 so we have pointer eight and pointer 14999 11:22:34,959 --> 11:22:38,720 nine here and p number eight here is 15000 11:22:43,839 --> 11:22:48,079 p number nine is initialized with 15001 11:22:45,839 --> 11:22:49,359 whatever we have in p number eight so 15002 11:22:48,080 --> 11:22:52,639 they are going to be pointing to the 15003 11:22:49,360 --> 11:22:55,116 same addresses here but we have made p 15004 11:22:52,639 --> 11:22:57,040 number eight the master pointer so it is 15005 11:22:55,116 --> 11:22:59,680 the only pointer that is going to be 15006 11:22:57,040 --> 11:23:01,520 able to delete this memory location even 15007 11:22:59,680 --> 11:23:02,639 if we have multiple pointers pointing to 15008 11:23:02,639 --> 11:23:07,360 okay so every time we want to use this 15009 11:23:04,639 --> 11:23:09,595 memory we have the option to check 15010 11:23:07,360 --> 11:23:12,639 against the master pointer and see that 15011 11:23:09,595 --> 11:23:14,720 we have some valid data inside if it is 15012 11:23:12,639 --> 11:23:16,639 no pointer we're not going to use that 15013 11:23:14,720 --> 11:23:18,720 and we're going to be playing on the 15014 11:23:16,639 --> 11:23:20,559 safe side this is really what we are 15015 11:23:18,720 --> 11:23:22,239 doing here if we run this we're going to 15016 11:23:20,559 --> 11:23:23,360 see that the program is going to end 15017 11:23:23,360 --> 11:23:26,720 the world is going to be good and if we 15018 11:23:25,275 --> 11:23:28,479 run rooster we're going to see that we 15019 11:23:26,720 --> 11:23:29,916 get things printed out we're going to go 15020 11:23:28,480 --> 11:23:31,680 through p number eight we're going to 15021 11:23:29,915 --> 11:23:33,595 print stuff we're going to go through p 15022 11:23:31,680 --> 11:23:36,400 number nine we're going to print stuff 15023 11:23:33,595 --> 11:23:38,319 and if we try to use the pointer 15024 11:23:38,319 --> 11:23:42,879 memory has been deleted we're going to 15025 11:23:40,480 --> 11:23:45,116 get a clear warning that we are trying 15026 11:23:42,879 --> 11:23:48,159 to do something really bad and this is 15027 11:23:45,116 --> 11:23:50,880 really what you should aim for in your c 15028 11:23:48,160 --> 11:23:53,436 plus plus programs that use dynamic 15029 11:23:50,879 --> 11:23:55,040 memory allocation and pointers 15030 11:23:53,436 --> 11:23:56,959 this is really all we set out to do in 15031 11:23:55,040 --> 11:23:58,879 this lecture i hope you know how you can 15032 11:23:56,959 --> 11:24:00,959 avoid these problems with dangling 15033 11:23:58,879 --> 11:24:02,719 pointers we are going to stop here in 15034 11:24:00,959 --> 11:24:05,040 this lecture the next lecture we're 15035 11:24:07,360 --> 11:24:11,595 fails to give you the memory that you 15036 11:24:09,436 --> 11:24:14,239 want to allocate on the heap and that's 15037 11:24:11,595 --> 11:24:16,399 possible go ahead and finish up here 15038 11:24:14,239 --> 11:24:20,000 and meet me there in this lecture we're 15039 11:24:16,400 --> 11:24:22,400 going to explore what happens when new 15040 11:24:20,000 --> 11:24:25,040 fails in the last lecture we saw that we 15041 11:24:22,400 --> 11:24:28,000 could use the new operator to allocate 15042 11:24:25,040 --> 11:24:30,879 for new memory on the heap and use that 15043 11:24:28,000 --> 11:24:33,275 to do stuff and in most cases the new 15044 11:24:30,879 --> 11:24:36,399 operator is going to succeed but in some 15045 11:24:33,275 --> 11:24:38,400 rare cases it is going to fail and when 15046 11:24:36,400 --> 11:24:40,639 you don't do anything about that failure 15047 11:24:38,400 --> 11:24:42,800 your program is just going to crash and 15048 11:24:40,639 --> 11:24:45,756 you might want to do something about 15049 11:24:42,800 --> 11:24:48,080 that i have to say that it is very rare 15050 11:24:45,756 --> 11:24:50,880 for new to fail in practice so you're 15051 11:24:48,080 --> 11:24:53,360 going to see a lot of code out there 15052 11:24:50,879 --> 11:24:55,755 that just uses the new operator it 15053 11:24:55,756 --> 11:25:01,200 checking if the operation actually 15054 11:24:58,480 --> 11:25:03,520 failed or succeeded so this is a very 15055 11:25:01,199 --> 11:25:05,199 rare case but if you need to handle it 15056 11:25:03,519 --> 11:25:08,000 we're going to see how you can do that 15057 11:25:05,199 --> 11:25:10,879 in this lecture here is a simple example 15058 11:25:08,000 --> 11:25:12,559 that is going to try and force the new 15059 11:25:12,559 --> 11:25:17,360 in the first segment here we are using 15060 11:25:14,639 --> 11:25:20,639 the new operator but we are using a 15061 11:25:17,360 --> 11:25:23,756 syntax that is going to try and allocate 15062 11:25:20,639 --> 11:25:25,756 a lot of integers in one go you see that 15063 11:25:23,756 --> 11:25:27,680 it is an array but we haven't really 15064 11:25:25,756 --> 11:25:30,639 seen how we can use an array like this 15065 11:25:27,680 --> 11:25:33,199 so just think of this code as a piece of 15066 11:25:30,639 --> 11:25:35,595 code that is going to allocate a lot of 15067 11:25:33,199 --> 11:25:38,239 integers in one go and we're going to 15068 11:25:38,239 --> 11:25:42,720 i have to say that this may give you an 15069 11:25:40,400 --> 11:25:45,040 error on some compilers saying that you 15070 11:25:42,720 --> 11:25:46,800 are exceeding the array size if that's 15071 11:25:45,040 --> 11:25:49,040 the case no problem we're going to find 15072 11:25:46,800 --> 11:25:51,116 another way to get this new operator to 15073 11:25:49,040 --> 11:25:52,959 fail we are going to put a thing like 15074 11:25:51,116 --> 11:25:56,240 this in a loop and we're going to loop a 15075 11:25:52,959 --> 11:25:59,116 lot of times enough to exhaust the heap 15076 11:25:56,239 --> 11:26:00,479 that is allocated to our program here 15077 11:25:59,116 --> 11:26:02,400 and when that happens you're going to 15078 11:26:00,480 --> 11:26:05,840 see that the program is going to crash 15079 11:26:02,400 --> 11:26:08,720 and it is bad so new can fail and if you 15080 11:26:05,839 --> 11:26:09,679 need to play on the server side you're 15081 11:26:09,680 --> 11:26:15,040 handle that problem and we have two ways 15082 11:26:12,480 --> 11:26:17,596 we can handle this problem we can go 15083 11:26:15,040 --> 11:26:20,720 through the exception mechanism this is 15084 11:26:17,595 --> 11:26:23,115 a built-in way we have in c plus plus to 15085 11:26:20,720 --> 11:26:26,480 check for arrows and handle them we can 15086 11:26:23,116 --> 11:26:29,520 also use the std no throw setting 15087 11:26:26,480 --> 11:26:31,680 in our new operator and force it not to 15088 11:26:29,519 --> 11:26:33,915 throw an exception and it's going to 15089 11:26:31,680 --> 11:26:36,319 give us no pointer if the allocation 15090 11:26:33,915 --> 11:26:37,915 fails i know this might sound cryptic 15091 11:26:36,319 --> 11:26:39,680 right now but we're going to see an 15092 11:26:37,915 --> 11:26:41,915 example shortly and you're going to 15093 11:26:39,680 --> 11:26:43,756 understand this let's look at how we can 15094 11:26:41,915 --> 11:26:46,399 use the exception mechanism the 15095 11:26:43,756 --> 11:26:49,040 exception mechanism is there in c plus 15096 11:26:46,400 --> 11:26:51,840 plus to let you handle problems when 15097 11:26:49,040 --> 11:26:54,480 they arise and the way you use that you 15098 11:26:51,839 --> 11:26:56,159 wrap code that is potentially going to 15099 11:26:56,160 --> 11:27:01,916 and try and catch blocks you see here 15100 11:26:59,275 --> 11:27:04,400 inside the loop we are saying try 15101 11:27:01,915 --> 11:27:06,799 and inside the tri blocks we have the 15102 11:27:04,400 --> 11:27:07,916 code that could fail we have our new 15103 11:27:07,915 --> 11:27:13,360 and in the catch block you see we have a 15104 11:27:10,879 --> 11:27:15,915 catch block here we're going to 15105 11:27:15,915 --> 11:27:19,519 and we have a way of catching the 15106 11:27:17,519 --> 11:27:22,879 problem and if you look at this thing we 15107 11:27:19,519 --> 11:27:26,799 have in this parenthesis after catch we 15108 11:27:22,879 --> 11:27:29,275 have an ex variable and we can ask it 15109 11:27:29,275 --> 11:27:33,595 leading to this exception happening in 15110 11:27:31,519 --> 11:27:35,839 our code you can think of this like this 15111 11:27:33,595 --> 11:27:38,159 i realize we haven't really talked about 15112 11:27:35,839 --> 11:27:39,915 exceptions we are going to talk about 15113 11:27:38,160 --> 11:27:41,680 them later in the course when we have 15114 11:27:39,915 --> 11:27:43,755 enough tools to really understand what 15115 11:27:43,756 --> 11:27:49,275 think of them as a tool to handle 15116 11:27:46,720 --> 11:27:51,200 when the new operator fails and you're 15117 11:27:49,275 --> 11:27:53,360 not going to crash your program your 15118 11:27:51,199 --> 11:27:55,839 program will keep running it will go 15119 11:27:53,360 --> 11:27:58,400 through the end but the new operation 15120 11:27:55,839 --> 11:28:00,159 that you wanted to do here will fail and 15121 11:27:58,400 --> 11:28:02,239 you will be able to catch that and this 15122 11:28:00,160 --> 11:28:04,800 is a good thing if you don't want to use 15123 11:28:02,239 --> 11:28:06,159 exceptions you can use the std no throw 15124 11:28:06,160 --> 11:28:11,436 and this is a new version of the new 15125 11:28:08,239 --> 11:28:13,839 operator you can use to tell the c plus 15126 11:28:11,436 --> 11:28:16,160 plus system to not throw an exception 15127 11:28:13,839 --> 11:28:18,079 when the operation fails it is just 15128 11:28:16,160 --> 11:28:19,756 going to return no pointer and you can 15129 11:28:18,080 --> 11:28:22,560 check for that and know that memory 15130 11:28:19,756 --> 11:28:23,840 allocation actually failed this is what 15131 11:28:22,559 --> 11:28:25,680 we're going to talk about in this 15132 11:28:23,839 --> 11:28:27,519 lecture i hope you have an idea about 15133 11:28:25,680 --> 11:28:29,915 this we're going to head over to visual 15134 11:28:27,519 --> 11:28:32,959 studio code and actually try this in 15135 11:28:29,915 --> 11:28:35,199 action okay here we are in our working 15136 11:28:32,959 --> 11:28:36,400 directory the current project is when 15137 11:28:36,400 --> 11:28:41,436 we are going to grab our template files 15138 11:28:38,879 --> 11:28:44,559 like we always do and we're going to put 15139 11:28:41,436 --> 11:28:46,720 them in place so when new fails that's 15140 11:28:44,559 --> 11:28:49,199 the lecture so we're going to do that 15141 11:28:46,720 --> 11:28:51,436 and open this in visual studio code 15142 11:28:49,199 --> 11:28:54,239 pretty quick and we are going to try and 15143 11:28:51,436 --> 11:28:56,319 allocate a large piece of memory in one 15144 11:28:54,239 --> 11:28:57,595 go so the way you do that we can do say 15145 11:28:57,595 --> 11:29:02,479 let's call this data it really doesn't 15146 11:28:59,519 --> 11:29:04,239 matter and we're going to say new and 15147 11:29:02,480 --> 11:29:05,916 and we're going to say that we want a 15148 11:29:05,915 --> 11:29:11,115 let's try and do that and we're going to 15149 11:29:13,199 --> 11:29:19,519 we know when we are ending this program 15150 11:29:16,959 --> 11:29:21,756 properly so let's try and compile this 15151 11:29:19,519 --> 11:29:24,799 we're going to bring up the terminal and 15152 11:29:21,756 --> 11:29:26,800 we are going to weld with gcc as we 15153 11:29:24,800 --> 11:29:29,040 always do and the build is going to be 15154 11:29:26,800 --> 11:29:31,680 good let's try and increase this number 15155 11:29:29,040 --> 11:29:33,116 and push it to a really big number and 15156 11:29:33,116 --> 11:29:38,720 have made visual studio code mad here 15157 11:29:36,319 --> 11:29:39,839 array is too large so they can't really 15158 11:29:40,800 --> 11:29:45,520 a really large array in here okay so 15159 11:29:43,199 --> 11:29:47,519 let's bring this down a little bit 15160 11:29:45,519 --> 11:29:49,595 and if we're trying to compile the 15161 11:29:47,519 --> 11:29:53,115 bullet is going to be good and if we run 15162 11:29:49,595 --> 11:29:56,799 rooster you see that the program 15163 11:29:53,116 --> 11:29:59,116 is going to not end well okay so this is 15164 11:29:56,800 --> 11:30:01,520 really a case where new fails we can't 15165 11:30:01,519 --> 11:30:06,719 on the heap storage that is allocated to 15166 11:30:04,160 --> 11:30:09,596 this program here remember when we 15167 11:30:06,720 --> 11:30:11,436 revisited the memory map idea we saw 15168 11:30:09,595 --> 11:30:13,595 that each program is going to have its 15169 11:30:11,436 --> 11:30:15,436 own memory map the memory map is going 15170 11:30:13,595 --> 11:30:17,756 to have a lot of sections inside we have 15171 11:30:15,436 --> 11:30:20,239 the stack we have the heap our hip is 15172 11:30:17,756 --> 11:30:22,239 not infinite so we can run out and we 15173 11:30:20,239 --> 11:30:25,040 just ran out with this allocation that 15174 11:30:22,239 --> 11:30:26,959 we did here and our program is going to 15175 11:30:25,040 --> 11:30:28,480 terminate immediately you see that it is 15176 11:30:28,480 --> 11:30:33,436 if this is not working for whatever 15177 11:30:30,400 --> 11:30:35,680 compiler you are using we can go and do 15178 11:30:33,436 --> 11:30:36,400 this through a loop so what we can do we 15179 11:30:36,400 --> 11:30:40,959 comment this out and we can really loop 15180 11:30:38,639 --> 11:30:44,239 many many times enough to exhaust the 15181 11:30:40,959 --> 11:30:46,959 memory that is allocated for this 15182 11:30:46,959 --> 11:30:52,319 in the memory map in the hip section 15183 11:30:49,040 --> 11:30:54,639 that's what i mean and if we do this at 15184 11:30:52,319 --> 11:30:56,400 some point the program is going to fail 15185 11:30:54,639 --> 11:30:58,720 and we're going to get the same problem 15186 11:30:58,720 --> 11:31:04,000 we're going to build with gcc 15187 11:31:01,756 --> 11:31:05,520 and we are going to clear and run 15188 11:31:04,000 --> 11:31:07,116 rooster and we're going to see it's 15189 11:31:05,519 --> 11:31:09,275 going to loop loop loop and it's going 15190 11:31:09,275 --> 11:31:13,680 the failure in a minute and we need to 15191 11:31:11,275 --> 11:31:15,839 find a way to solve this problem we have 15192 11:31:13,680 --> 11:31:17,360 seen that we have two options let's 15193 11:31:17,360 --> 11:31:21,756 and uh look at those solutions here the 15194 11:31:19,839 --> 11:31:24,159 first option is to go through the 15195 11:31:21,756 --> 11:31:27,040 exception mechanism we can also use the 15196 11:31:24,160 --> 11:31:29,756 std no throw option we're going to try 15197 11:31:27,040 --> 11:31:31,360 the exception mechanism first and uh 15198 11:31:31,360 --> 11:31:38,080 is wrap possibly offending code in try 15199 11:31:34,879 --> 11:31:40,479 and catch blocks and we're going to say 15200 11:31:38,080 --> 11:31:41,520 try here and put a block and we're going 15201 11:31:41,519 --> 11:31:46,559 catch and we're going to say std 15202 11:31:44,160 --> 11:31:49,276 exception and we're going to put an 15203 11:31:46,559 --> 11:31:51,199 ampersand symbol here just use this like 15204 11:31:49,275 --> 11:31:53,595 this and we're going to get a chance to 15205 11:31:51,199 --> 11:31:56,000 really explain these things when we have 15206 11:31:53,595 --> 11:31:58,799 enough tools to understand this for now 15207 11:31:56,000 --> 11:32:01,040 think of this as if i have code that is 15208 11:32:01,040 --> 11:32:05,520 like the new operator here i am going to 15209 11:32:05,519 --> 11:32:10,799 and if something goes wrong we're going 15210 11:32:08,000 --> 11:32:12,959 to get the error in the catch block this 15211 11:32:10,800 --> 11:32:16,000 is the way we do these things here so 15212 11:32:12,959 --> 11:32:17,519 for example i can take my loop here 15213 11:32:21,519 --> 11:32:26,159 and i know that this line of code could 15214 11:32:26,160 --> 11:32:31,116 so i am going to wrap that inside try 15215 11:32:28,959 --> 11:32:32,159 and catch blocks so i am going to grab 15216 11:32:35,116 --> 11:32:38,639 let's do that and align this a little 15217 11:32:38,639 --> 11:32:43,436 and we are going to take the potentially 15218 11:32:45,595 --> 11:32:50,239 in the try block and when the operator 15219 11:32:50,239 --> 11:32:53,756 something goes wrong and we're going to 15220 11:32:51,839 --> 11:32:56,399 catch the problem in the catch block 15221 11:32:53,756 --> 11:32:58,720 this is what we mean here again we don't 15222 11:32:56,400 --> 11:33:00,319 have enough tools to understand why we 15223 11:32:58,720 --> 11:33:02,639 are doing this thing here what is the 15224 11:33:00,319 --> 11:33:04,239 meaning of this ampersand symbol we're 15225 11:33:02,639 --> 11:33:06,479 going to learn about that later in the 15226 11:33:04,239 --> 11:33:09,519 course but for now we want to put 15227 11:33:06,480 --> 11:33:13,200 together something good enough for this 15228 11:33:09,519 --> 11:33:14,799 program not to crash like it was doing 15229 11:33:13,199 --> 11:33:17,839 right here okay we're going to catch the 15230 11:33:14,800 --> 11:33:19,360 arrow and say something went wrong 15231 11:33:17,839 --> 11:33:21,199 and we're going to print out what 15232 11:33:19,360 --> 11:33:23,520 actually went wrong we're going to say 15233 11:33:23,519 --> 11:33:28,839 a function called what on this 15234 11:33:30,639 --> 11:33:36,080 let's hide this pen here because we want 15235 11:33:33,360 --> 11:33:38,720 to see clearly what is happening here 15236 11:33:36,080 --> 11:33:40,800 this is how you can prevent your program 15237 11:33:38,720 --> 11:33:42,840 from crashing and it is going to end 15238 11:33:40,800 --> 11:33:45,916 well because now we have handled the 15239 11:33:42,839 --> 11:33:46,959 problem and that we haven't caused a 15240 11:33:46,959 --> 11:33:51,436 like we were doing down here now we're 15241 11:33:49,360 --> 11:33:53,520 going to try and build this code let's 15242 11:33:51,436 --> 11:33:55,596 bring it up a little bit so that it is 15243 11:33:55,595 --> 11:33:59,756 and we're going to weld with gcc the 15244 11:33:57,756 --> 11:34:02,559 bolt is going to be good and we're going 15245 11:33:59,756 --> 11:34:04,160 to clear and run rooster and it is going 15246 11:34:02,559 --> 11:34:06,319 to do its thing it's going to keep 15247 11:34:06,319 --> 11:34:09,519 and it's going to say something went bad 15248 11:34:08,160 --> 11:34:11,756 it's going to hit a point where all 15249 11:34:09,519 --> 11:34:13,755 these allocations are going to fail and 15250 11:34:11,756 --> 11:34:15,595 if we really wait for it it is going to 15251 11:34:13,756 --> 11:34:16,959 end and we're going to see that it's 15252 11:34:16,959 --> 11:34:21,199 let's try and hit ctrl c and terminate 15253 11:34:19,680 --> 11:34:22,800 it because we don't want to wait for 15254 11:34:24,480 --> 11:34:27,916 down to 100 and increase this number 15255 11:34:27,915 --> 11:34:32,399 let's see if that actually works so 15256 11:34:30,480 --> 11:34:34,319 we're going to boil for now we just want 15257 11:34:32,400 --> 11:34:35,680 to see that the program is not really 15258 11:34:35,680 --> 11:34:38,959 so we're going to run rooster and you 15259 11:34:38,959 --> 11:34:42,319 it's going to keep running and at some 15260 11:34:42,319 --> 11:34:45,756 it's going to fail to allocate memory 15261 11:34:44,160 --> 11:34:48,000 and it's going to say something went 15262 11:34:45,756 --> 11:34:50,239 wrong but if you keep going down you're 15263 11:34:48,000 --> 11:34:51,116 going to see that the program is ending 15264 11:34:51,116 --> 11:34:56,160 so what is happening here is that 15265 11:34:53,680 --> 11:34:59,040 the new operator actually failed 15266 11:34:56,160 --> 11:35:02,160 but we code the problem and could handle 15267 11:34:59,040 --> 11:35:03,360 it in here so what is called handling 15268 11:35:03,360 --> 11:35:07,199 you're going to handle it in whatever 15269 11:35:04,879 --> 11:35:09,680 makes sense in your program if we were 15270 11:35:07,199 --> 11:35:10,639 for example trying to set up a color in 15271 11:35:10,639 --> 11:35:15,360 widgets in your application and the 15272 11:35:13,116 --> 11:35:17,360 color fails and you fail to set up the 15273 11:35:15,360 --> 11:35:19,680 color you may show your user interface 15274 11:35:17,360 --> 11:35:21,840 in black and white and keep running your 15275 11:35:19,680 --> 11:35:24,080 program instead of getting it crashed 15276 11:35:21,839 --> 11:35:26,000 this is just a way to show you 15277 11:35:26,000 --> 11:35:30,239 when new fails this is one way we can 15278 11:35:28,800 --> 11:35:31,916 solve the problem so we're going to look 15279 11:35:31,915 --> 11:35:36,239 the exception mechanism is the first 15280 11:35:34,639 --> 11:35:38,559 so we're going to comment this out 15281 11:35:36,239 --> 11:35:40,959 because we're going to see another way 15282 11:35:38,559 --> 11:35:44,639 and let's do that we can also use the 15283 11:35:40,959 --> 11:35:46,159 std no throw setting on our new operator 15284 11:35:46,160 --> 11:35:51,276 allocating for new memory so we're going 15285 11:35:48,639 --> 11:35:52,639 to do that and we're going to bring up 15286 11:35:52,639 --> 11:35:56,239 we're going to reuse that for this here 15287 11:35:54,639 --> 11:35:58,159 we're not going to be using the 15288 11:35:56,239 --> 11:36:00,400 exception mechanism so we're going to do 15289 11:35:58,160 --> 11:36:02,720 something like this get rid of the try 15290 11:36:00,400 --> 11:36:04,480 and catch block but we're going to still 15291 11:36:04,480 --> 11:36:10,400 so what we can do is pass the new 15292 11:36:07,040 --> 11:36:14,080 operator a parameter and say std no 15293 11:36:10,400 --> 11:36:16,639 throw and when we do this if new fails 15294 11:36:14,080 --> 11:36:18,639 we are going to get no pointer stored in 15295 11:36:16,639 --> 11:36:20,559 data here so we can check this against 15296 11:36:21,839 --> 11:36:27,839 data is different from null pointer 15297 11:36:25,519 --> 11:36:29,680 and we can do whatever we want to do we 15298 11:36:32,400 --> 11:36:35,916 get it allocated and we can try and 15299 11:36:35,915 --> 11:36:42,159 so what we're going to do is run this 15300 11:36:38,080 --> 11:36:43,520 through our compiler through gcc 15301 11:36:42,160 --> 11:36:45,756 and you're going to see that the world 15302 11:36:43,519 --> 11:36:47,595 is going to be good and you see that 15303 11:36:45,756 --> 11:36:49,436 visual studio code was giving us a 15304 11:36:47,595 --> 11:36:51,435 squiggly line because it can't really 15305 11:36:49,436 --> 11:36:53,596 understand this but this is going to go 15306 11:36:51,436 --> 11:36:56,319 through the compiler and it is going to 15307 11:36:56,319 --> 11:37:00,720 so let's try and run this program 15308 11:36:59,040 --> 11:37:02,480 we're going to ignore visual studio code 15309 11:37:00,720 --> 11:37:05,436 for now because the world was good we 15310 11:37:02,480 --> 11:37:08,640 could build with gcc and we saw that 15311 11:37:05,436 --> 11:37:10,080 this was welding successfully so we can 15312 11:37:10,080 --> 11:37:14,720 let's clear properly and run rooster and 15313 11:37:12,559 --> 11:37:16,559 it's going to say data allocated a few 15314 11:37:14,720 --> 11:37:18,880 other times and the program is going to 15315 11:37:16,559 --> 11:37:21,040 end you see that the program is not 15316 11:37:18,879 --> 11:37:23,360 crashing because we are also handling 15317 11:37:21,040 --> 11:37:25,520 the problem but we are going through a 15318 11:37:23,360 --> 11:37:28,639 new mechanism to do that and that's 15319 11:37:25,519 --> 11:37:31,115 using the std no throw option with our 15320 11:37:28,639 --> 11:37:32,959 new operator here this is really all we 15321 11:37:31,116 --> 11:37:35,840 set out to do in this lecture i hope it 15322 11:37:32,959 --> 11:37:38,559 is very clear that the new operator can 15323 11:37:35,839 --> 11:37:41,115 fail and you have a couple of options at 15324 11:37:38,559 --> 11:37:43,199 your hands to handle things when new 15325 11:37:41,116 --> 11:37:45,200 fails you can either go through the 15326 11:37:43,199 --> 11:37:47,755 exception mechanism by using try and 15327 11:37:45,199 --> 11:37:51,435 catch blocks like we did here or you can 15328 11:37:47,756 --> 11:37:53,595 use the std no throw option we are going 15329 11:37:51,436 --> 11:37:55,680 to stop here in this lecture and the 15330 11:37:53,595 --> 11:37:57,839 next one we are going to learn about no 15331 11:37:57,839 --> 11:38:02,319 go ahead and finish up here and meet me 15332 11:38:00,239 --> 11:38:05,519 there in this lecture we're going to 15333 11:38:02,319 --> 11:38:07,915 explore the concept called null pointer 15334 11:38:05,519 --> 11:38:10,399 safety and this is a series of measures 15335 11:38:07,915 --> 11:38:13,199 you take to make sure that when you are 15336 11:38:10,400 --> 11:38:15,040 using a pointer it contains a valid 15337 11:38:13,199 --> 11:38:17,039 address if you don't have a valid 15338 11:38:15,040 --> 11:38:19,436 address inside you don't use that 15339 11:38:17,040 --> 11:38:21,680 pointer and we do that through a series 15340 11:38:19,436 --> 11:38:23,360 of checks we can do against the null 15341 11:38:21,680 --> 11:38:25,915 pointer for example here we have a 15342 11:38:23,360 --> 11:38:26,879 pointer that is initialized to null 15343 11:38:26,879 --> 11:38:33,915 implicitly through braced initializers 15344 11:38:30,400 --> 11:38:35,436 and what we can do is check if p number 15345 11:38:35,436 --> 11:38:39,916 null pointer and this is how we say it 15346 11:38:36,959 --> 11:38:41,360 in c plus and if it contains no pointer 15347 11:38:39,915 --> 11:38:44,159 we're not going to use it we're going to 15348 11:38:41,360 --> 11:38:46,400 say it points to an invalid address this 15349 11:38:44,160 --> 11:38:49,200 is something you can do it is simple in 15350 11:38:46,400 --> 11:38:51,596 nature but it is going to save you lots 15351 11:38:49,199 --> 11:38:54,239 of trouble this is one version we can do 15352 11:38:51,595 --> 11:38:57,275 this on but we can also directly plug in 15353 11:38:54,239 --> 11:38:58,959 p number and use it like this if it 15354 11:38:57,275 --> 11:39:00,879 contains no pointer we're going to get 15355 11:38:58,959 --> 11:39:04,000 the message printed out that we have an 15356 11:39:00,879 --> 11:39:05,595 invalid address if we have something 15357 11:39:04,000 --> 11:39:07,519 other than no pointer we're going to 15358 11:39:05,595 --> 11:39:09,040 have a valid address and we can do 15359 11:39:07,519 --> 11:39:11,595 something with that this is going to 15360 11:39:09,040 --> 11:39:14,080 work because a pointer can also be 15361 11:39:11,595 --> 11:39:17,199 implicitly converted into a boolean 15362 11:39:14,080 --> 11:39:19,436 expression that our if statement expects 15363 11:39:17,199 --> 11:39:21,360 here and it is going to work exactly how 15364 11:39:19,436 --> 11:39:23,436 we want here so we're going to see this 15365 11:39:21,360 --> 11:39:25,680 in a minute when we hit visual studio 15366 11:39:23,436 --> 11:39:28,959 code before we do that though i want you 15367 11:39:25,680 --> 11:39:31,360 to be aware that you can call 15368 11:39:28,959 --> 11:39:33,595 delete on a pointer containing no 15369 11:39:31,360 --> 11:39:35,520 pointer without doing the check here it 15370 11:39:33,595 --> 11:39:37,519 is going to be okay we have seen many 15371 11:39:35,519 --> 11:39:40,000 rules about pointers and many students 15372 11:39:37,519 --> 11:39:42,399 will develop an intuition that you have 15373 11:39:40,000 --> 11:39:44,559 to check for no pointer before you do 15374 11:39:42,400 --> 11:39:46,880 anything on your pointer and they even 15375 11:39:44,559 --> 11:39:48,959 do that before they call delete i have 15376 11:39:46,879 --> 11:39:51,115 to break it to you that it is safe to 15377 11:39:48,959 --> 11:39:53,756 call delete on a pointer that contains 15378 11:39:51,116 --> 11:39:55,680 null pointer so you don't need to do 15379 11:39:53,756 --> 11:39:57,756 something like we have here say and 15380 11:39:55,680 --> 11:40:00,000 check if p number one and then delete p 15381 11:39:57,756 --> 11:40:02,160 number one and reset that to low pointer 15382 11:40:00,000 --> 11:40:04,160 this is really overkill you can call 15383 11:40:02,160 --> 11:40:06,720 delete on the pointer and it is going to 15384 11:40:04,160 --> 11:40:08,480 be fine even if it contains no pointer 15385 11:40:06,720 --> 11:40:10,880 now we're going to go to visual studio 15386 11:40:08,480 --> 11:40:13,756 code and see these things in action here 15387 11:40:10,879 --> 11:40:16,559 we are in our working directory the 15388 11:40:13,756 --> 11:40:18,880 current project is no pointer safety 15389 11:40:16,559 --> 11:40:20,559 we're going to grab our template project 15390 11:40:18,879 --> 11:40:23,435 and we're going to put that in the 15391 11:40:20,559 --> 11:40:24,159 current project that we are interested 15392 11:40:24,160 --> 11:40:28,800 and we are going to open this in visual 15393 11:40:26,319 --> 11:40:31,436 studio code open folder and we are going 15394 11:40:28,800 --> 11:40:33,680 to put in a verbose no pointer check 15395 11:40:31,436 --> 11:40:35,840 what we are doing here is nothing new we 15396 11:40:33,680 --> 11:40:39,116 have a pointer called p number it is a 15397 11:40:35,839 --> 11:40:41,839 pointer to ant and it is initialized to 15398 11:40:39,116 --> 11:40:44,360 null pointer through implicit 15399 11:40:41,839 --> 11:40:47,039 initialization here using braced 15400 11:40:44,360 --> 11:40:49,275 initialization and we are checking to 15401 11:40:47,040 --> 11:40:50,080 see if it contains null pointer if it 15402 11:40:50,080 --> 11:40:53,360 we're going to say it contains an 15403 11:40:51,519 --> 11:40:55,595 invalid address if it doesn't we're 15404 11:40:53,360 --> 11:40:57,520 going to say that it has a valid address 15405 11:40:55,595 --> 11:40:59,756 and you see that we are using a negation 15406 11:40:57,519 --> 11:41:01,435 here you can do this however you want if 15407 11:41:01,436 --> 11:41:07,040 with gcc let's see what we get 15408 11:41:04,559 --> 11:41:09,435 the vote is going to go through 15409 11:41:07,040 --> 11:41:12,080 and if we run rooster we're going to see 15410 11:41:09,436 --> 11:41:14,400 that p number points to an invalid 15411 11:41:14,400 --> 11:41:19,680 null pointer but if we try and set up a 15412 11:41:17,275 --> 11:41:21,436 new variable for example and say p 15413 11:41:21,436 --> 11:41:26,319 and let's use dynamic memory we have 15414 11:41:23,680 --> 11:41:28,720 learnt about that and initialize that 15415 11:41:26,319 --> 11:41:30,400 with a 7 for example and when it points 15416 11:41:28,720 --> 11:41:32,400 to a valid address we're going to print 15417 11:41:30,400 --> 11:41:35,520 whatever is in there we know how to do 15418 11:41:32,400 --> 11:41:37,520 that so we can say p number and we're 15419 11:41:35,519 --> 11:41:40,479 going to print whatever is inside and 15420 11:41:37,519 --> 11:41:43,199 say p number through the reference end 15421 11:41:40,480 --> 11:41:45,436 and we're going to say stdndl now the 15422 11:41:43,199 --> 11:41:47,915 pointer is not going to contain 15423 11:41:45,436 --> 11:41:48,959 no pointer because it contains the 15424 11:41:48,959 --> 11:41:52,639 of the memory that was allocated on the 15425 11:41:52,639 --> 11:41:57,839 but once we do that we have to remember 15426 11:41:55,275 --> 11:41:59,756 to release this memory so please 15427 11:41:57,839 --> 11:42:01,839 remember to release your memory after 15428 11:41:59,756 --> 11:42:04,959 you are done using it so we're going to 15429 11:42:01,839 --> 11:42:07,359 say delete p number after that and we 15430 11:42:04,959 --> 11:42:10,000 need to reset it to no pointer in this 15431 11:42:07,360 --> 11:42:11,520 case it really is useless because nobody 15432 11:42:10,000 --> 11:42:14,800 is going to be using this pointer 15433 11:42:11,519 --> 11:42:17,519 anymore the program is about to end but 15434 11:42:14,800 --> 11:42:20,400 it is a good practice to do this even in 15435 11:42:17,519 --> 11:42:23,680 this case because once this way of doing 15436 11:42:20,400 --> 11:42:25,596 events is burnt into your memory it has 15437 11:42:23,680 --> 11:42:28,400 become your habit you're going to be 15438 11:42:25,595 --> 11:42:30,079 writing cipher c plus plus code now 15439 11:42:28,400 --> 11:42:31,756 we're going to try and run this so we're 15440 11:42:30,080 --> 11:42:34,400 going to build again the bold is going 15441 11:42:31,756 --> 11:42:35,436 to go through and it is going to say 15442 11:42:35,436 --> 11:42:40,160 we have a valid address in memory which 15443 11:42:37,360 --> 11:42:42,959 is this guy and what is inside is a 15444 11:42:40,160 --> 11:42:45,756 seven and now we are writing cipher c 15445 11:42:42,959 --> 11:42:48,159 plus plus code through null pointer 15446 11:42:45,756 --> 11:42:51,040 safety here we can also do a direct 15447 11:42:48,160 --> 11:42:53,116 check using p number without checking if 15448 11:42:51,040 --> 11:42:55,915 it is equal to null pointer because this 15449 11:42:53,116 --> 11:42:57,916 is verbose but it is something you will 15450 11:42:55,915 --> 11:42:59,915 see in practice you can also do 15451 11:42:57,915 --> 11:43:01,595 something like this and say if p number 15452 11:42:59,915 --> 11:43:04,399 and you're going to get exactly the same 15453 11:43:01,595 --> 11:43:06,799 thing so if we have some valid 15454 11:43:04,400 --> 11:43:09,436 address inside we're going to print 15455 11:43:06,800 --> 11:43:10,400 that let's print the same thing as 15456 11:43:12,160 --> 11:43:16,480 and if we build and run we're going to 15457 11:43:18,160 --> 11:43:22,240 clear because the world is good let's 15458 11:43:20,319 --> 11:43:25,275 clear and run rooster you see that we 15459 11:43:22,239 --> 11:43:27,915 get exactly the same thing and if we 15460 11:43:25,275 --> 11:43:30,239 didn't initialize this guy with the new 15461 11:43:27,915 --> 11:43:32,719 dynamic piece of memory we're going to 15462 11:43:30,239 --> 11:43:34,239 get that the memory is invalid the 15463 11:43:32,720 --> 11:43:36,080 address we have in the pointer is going 15464 11:43:34,239 --> 11:43:38,239 to be invalid and we're going to print 15465 11:43:36,080 --> 11:43:41,116 that out through our code here 15466 11:43:41,116 --> 11:43:46,720 okay the last thing i want you to see is 15467 11:43:43,275 --> 11:43:48,799 that it is okay to call delete 15468 11:43:46,720 --> 11:43:50,800 on a null pointer and we're going to say 15469 11:43:48,800 --> 11:43:52,960 that here so we're going to go down here 15470 11:43:50,800 --> 11:43:55,360 and put in a piece of code we are 15471 11:43:52,959 --> 11:43:58,159 setting up a new pointer p number one 15472 11:43:55,360 --> 11:44:00,080 initialized with no pointer if we call 15473 11:43:58,160 --> 11:44:01,916 delete on it this is going to work 15474 11:44:00,080 --> 11:44:04,240 really well and you're not going to get 15475 11:44:01,915 --> 11:44:06,559 a crush or anything this is valid c plus 15476 11:44:04,239 --> 11:44:09,519 plus code so if we're trying to compile 15477 11:44:06,559 --> 11:44:11,915 this it is going to work fine and you 15478 11:44:11,915 --> 11:44:16,959 do a no pointer check before you delete 15479 11:44:14,639 --> 11:44:19,595 like this because that's overkill you 15480 11:44:16,959 --> 11:44:21,360 see that the program both successfully 15481 11:44:19,595 --> 11:44:23,040 and we can clear and run it and we're 15482 11:44:21,360 --> 11:44:25,520 going to get exactly the same things we 15483 11:44:23,040 --> 11:44:27,680 had before so some people are going to 15484 11:44:27,680 --> 11:44:31,756 they're going to check for no pointer 15485 11:44:29,595 --> 11:44:34,479 and they're going to say if p number one 15486 11:44:31,756 --> 11:44:36,160 for example is not equal to no pointer 15487 11:44:34,480 --> 11:44:38,080 and they're going to say something like 15488 11:44:36,160 --> 11:44:39,436 delete here and they're going to do 15489 11:44:39,436 --> 11:44:43,116 and this is going to work but it is 15490 11:44:41,436 --> 11:44:46,239 really overkill you don't need to do 15491 11:44:43,116 --> 11:44:49,276 this test because it is safe to call 15492 11:44:46,239 --> 11:44:51,756 delete on a pointer even if it contains 15493 11:44:49,275 --> 11:44:53,915 no pointer in the first place so you 15494 11:44:51,756 --> 11:44:56,400 don't need to do things like this this 15495 11:44:53,915 --> 11:44:59,275 is going to work just fine don't be 15496 11:44:56,400 --> 11:45:02,480 afraid to call delete on a pointer 15497 11:44:59,275 --> 11:45:04,879 because it might contain null pointer 15498 11:45:02,480 --> 11:45:06,960 this is really all we mean by null 15499 11:45:04,879 --> 11:45:09,519 pointer safety and i hope i gave you a 15500 11:45:06,959 --> 11:45:11,040 few examples to make this pretty clear 15501 11:45:09,519 --> 11:45:12,559 we are going to stop here in this 15502 11:45:11,040 --> 11:45:15,275 lecture the next one we're going to 15503 11:45:15,275 --> 11:45:19,595 go ahead and finish up here and meet me 15504 11:45:17,756 --> 11:45:21,840 there in this lecture we're going to 15505 11:45:21,839 --> 11:45:28,000 and a memory leak is when you have lost 15506 11:45:24,879 --> 11:45:30,639 access to a piece of memory that was 15507 11:45:28,000 --> 11:45:32,959 dynamically allocated to your program 15508 11:45:30,639 --> 11:45:35,116 you basically lose the pointer that was 15509 11:45:32,959 --> 11:45:37,199 pointing to that dynamically allocated 15510 11:45:35,116 --> 11:45:39,040 piece of memory here we have a few 15511 11:45:37,199 --> 11:45:41,275 examples of that for example here we 15512 11:45:39,040 --> 11:45:43,520 have p number which contains a 15513 11:45:41,275 --> 11:45:44,959 dynamically allocated piece of memory 15514 11:45:43,519 --> 11:45:47,359 which is going to contain an integer 15515 11:45:44,959 --> 11:45:50,639 it's going to be 4 byte the value inside 15516 11:45:47,360 --> 11:45:53,520 is going to be a 67. this p number is 15517 11:45:50,639 --> 11:45:55,756 our only key to manipulate that memory 15518 11:45:53,519 --> 11:45:58,639 and it is the key through which we even 15519 11:45:55,756 --> 11:46:01,360 go to release that memory and return 15520 11:45:58,639 --> 11:46:02,959 that to the operating system but notice 15521 11:46:02,959 --> 11:46:08,400 we are setting up a new number variable 15522 11:46:06,000 --> 11:46:09,915 it is an integer and it is allocated on 15523 11:46:08,400 --> 11:46:11,916 the stack because this is a local 15524 11:46:09,915 --> 11:46:15,115 variable it's not a pointer it's not 15525 11:46:11,915 --> 11:46:17,915 dynamically allocated and we take the 15526 11:46:15,116 --> 11:46:20,400 address of number and assign that to our 15527 11:46:17,915 --> 11:46:23,595 pointer the moment we do that 15528 11:46:20,400 --> 11:46:26,080 we have no way to release the memory 15529 11:46:23,595 --> 11:46:27,275 that was allocated to p number in the 15530 11:46:27,275 --> 11:46:31,756 and if we do that the operating system 15531 11:46:29,680 --> 11:46:34,879 is going to think that this memory is 15532 11:46:31,756 --> 11:46:37,116 ours but we don't have a way to release 15533 11:46:34,879 --> 11:46:38,239 this memory anymore because the pointer 15534 11:46:38,239 --> 11:46:43,519 helping us access this memory has been 15535 11:46:41,116 --> 11:46:45,840 repurposed to point to this number 15536 11:46:43,519 --> 11:46:47,755 variable here i hope you see that 15537 11:46:45,839 --> 11:46:49,839 and the memory leak is something like 15538 11:46:47,756 --> 11:46:52,400 this when you lose the pointer that 15539 11:46:49,839 --> 11:46:54,479 would otherwise be used to release the 15540 11:46:52,400 --> 11:46:56,639 memory and your program is basically 15541 11:46:54,480 --> 11:46:58,720 going to keep using this memory and the 15542 11:46:56,639 --> 11:47:01,199 operating system is not going to have a 15543 11:46:58,720 --> 11:47:03,756 way to reclaim this memory so it is 15544 11:47:01,199 --> 11:47:05,435 useless so you are no longer using this 15545 11:47:03,756 --> 11:47:07,436 memory because you can't access it and 15546 11:47:05,436 --> 11:47:09,596 do things with it and the operating 15547 11:47:07,436 --> 11:47:11,840 system can't use it either because it 15548 11:47:09,595 --> 11:47:14,319 fences your memory hopefully you can see 15549 11:47:11,839 --> 11:47:16,719 that this is a really bad situation and 15550 11:47:14,319 --> 11:47:19,839 you should avoid these things in your 15551 11:47:16,720 --> 11:47:21,200 application so do avoid memory leaks 15552 11:47:21,199 --> 11:47:25,755 this is one situation in which we can 15553 11:47:23,199 --> 11:47:28,399 have a memory leak here is another 15554 11:47:25,756 --> 11:47:30,639 example where we have double allocation 15555 11:47:28,400 --> 11:47:33,200 we have a pointer p number one it is 15556 11:47:30,639 --> 11:47:37,040 pointing to dynamically allocated memory 15557 11:47:33,199 --> 11:47:39,275 again and the value inside is a 55. 15558 11:47:37,040 --> 11:47:41,595 if we use this pointer to allocate new 15559 11:47:39,275 --> 11:47:43,839 memory the first memory that we were 15560 11:47:41,595 --> 11:47:46,720 pointing to is going to be leaked 15561 11:47:43,839 --> 11:47:49,755 again it is the same thing we have lost 15562 11:47:46,720 --> 11:47:52,559 access to the first memory now we are 15563 11:47:49,756 --> 11:47:54,880 pointing to the second dynamic 15564 11:47:52,559 --> 11:47:58,000 memory that was allocated to contain the 15565 11:47:54,879 --> 11:47:59,915 44 here and again this is a memory leak 15566 11:47:58,000 --> 11:48:03,199 your program has lost access to the 15567 11:47:59,915 --> 11:48:05,519 memory containing 55 here and the 15568 11:48:03,199 --> 11:48:08,479 operating system also thinks that you 15569 11:48:05,519 --> 11:48:11,199 are using this memory because you didn't 15570 11:48:08,480 --> 11:48:14,880 explicitly return it to the operating 15571 11:48:11,199 --> 11:48:17,755 system through the delete operator again 15572 11:48:14,879 --> 11:48:20,159 memory leaks are bad do avoid them in 15573 11:48:17,756 --> 11:48:22,160 your programs here is another simple 15574 11:48:20,160 --> 11:48:24,800 example of a case where you might have a 15575 11:48:22,160 --> 11:48:27,200 memory leak you go in a nested scope 15576 11:48:24,800 --> 11:48:29,840 like this and allocate a new piece of 15577 11:48:27,199 --> 11:48:32,719 memory dynamically of course and point 15578 11:48:29,839 --> 11:48:34,000 to that through p number two here but p 15579 11:48:34,000 --> 11:48:39,519 itself is a pointer and it is a local 15580 11:48:36,800 --> 11:48:41,756 variable nested in this scope 15581 11:48:39,519 --> 11:48:44,319 when this scope ends p number two is 15582 11:48:41,756 --> 11:48:46,639 going to die but the dynamic memory 15583 11:48:44,319 --> 11:48:48,720 itself is not going to die you just lost 15584 11:48:46,639 --> 11:48:50,959 access to it and you can't access it 15585 11:48:48,720 --> 11:48:53,275 again this is a memory leak and you 15586 11:48:50,959 --> 11:48:56,080 should avoid this in your programs 15587 11:48:53,275 --> 11:48:58,559 memory leaks are really bad and in 15588 11:48:56,080 --> 11:49:01,200 extreme cases they might even lead to 15589 11:48:58,559 --> 11:49:03,595 your program crashing if your program 15590 11:49:01,199 --> 11:49:06,399 keeps running for an extended period of 15591 11:49:03,595 --> 11:49:08,799 time think of programs like those 15592 11:49:06,400 --> 11:49:10,880 running in servers for example they can 15593 11:49:08,800 --> 11:49:14,000 even run for three months without 15594 11:49:10,879 --> 11:49:16,399 stopping and if your program is 15595 11:49:14,000 --> 11:49:18,160 increasingly leaking memory the 15596 11:49:16,400 --> 11:49:20,400 operating system is going to think that 15597 11:49:18,160 --> 11:49:22,640 you are taking too much hip memory it's 15598 11:49:20,400 --> 11:49:23,520 going to terminate your program like we 15599 11:49:23,519 --> 11:49:28,479 in the lecture where we learned on 15600 11:49:26,000 --> 11:49:31,436 conditions where the new operator failed 15601 11:49:28,480 --> 11:49:33,840 so if you allocate too much memory then 15602 11:49:31,436 --> 11:49:35,436 your program can really take the next 15603 11:49:33,839 --> 11:49:37,199 operations you're going to do with new 15604 11:49:35,436 --> 11:49:39,756 are going to fail and your program is 15605 11:49:37,199 --> 11:49:41,839 going to terminate the message here is 15606 11:49:39,756 --> 11:49:44,239 memory leaks are bad you should avoid 15607 11:49:41,839 --> 11:49:46,000 them in your programs and whenever you 15608 11:49:44,239 --> 11:49:48,559 think that you're no longer using a 15609 11:49:46,000 --> 11:49:50,720 piece of memory make sure you delete it 15610 11:49:48,559 --> 11:49:53,275 and return it to the operating system 15611 11:49:50,720 --> 11:49:55,200 you're going to be writing much safer c 15612 11:49:53,275 --> 11:49:57,519 plus plus code we're going to head over 15613 11:49:55,199 --> 11:50:00,559 to visual studio code and actually try 15614 11:49:57,519 --> 11:50:02,879 this here we are in our working 15615 11:50:00,559 --> 11:50:05,040 directory the current project is memory 15616 11:50:02,879 --> 11:50:06,879 leaks we're going to bring in our 15617 11:50:06,879 --> 11:50:12,079 and we're going to put them in place in 15618 11:50:08,959 --> 11:50:14,239 the lecture on memory leaks and we are 15619 11:50:12,080 --> 11:50:15,916 going to open this in visual studio code 15620 11:50:14,239 --> 11:50:18,319 we are going to clean this up a little 15621 11:50:15,915 --> 11:50:20,239 bit bring this up here we have a 15622 11:50:18,319 --> 11:50:22,879 variable called p number it is a pointer 15623 11:50:20,239 --> 11:50:25,756 to end and we initialize that with the 15624 11:50:22,879 --> 11:50:28,479 address of a piece of memory that is 15625 11:50:25,756 --> 11:50:31,916 allocated dynamically on the heap we 15626 11:50:28,480 --> 11:50:34,160 store a 67 inside and here down you see 15627 11:50:31,915 --> 11:50:37,115 we set up another variable on the stack 15628 11:50:34,160 --> 11:50:39,116 again this says a stack variable 15629 11:50:37,116 --> 11:50:41,116 it lives on the stack it's not on the 15630 11:50:39,116 --> 11:50:43,116 heap and we text the address of this 15631 11:50:41,116 --> 11:50:45,436 thing and assign that to our pointer 15632 11:50:43,116 --> 11:50:47,756 here the moment we do that we have no 15633 11:50:45,436 --> 11:50:50,319 more way we can access this dynamic 15634 11:50:47,756 --> 11:50:52,639 memory and we have leaked it and this is 15635 11:50:50,319 --> 11:50:54,000 really bad the thing is this program is 15636 11:50:54,000 --> 11:50:58,400 i don't think compilers have a way to 15637 11:50:56,160 --> 11:51:00,160 stop you from making mistakes like this 15638 11:50:58,400 --> 11:51:02,080 so you really have to be careful and 15639 11:51:00,160 --> 11:51:04,880 make sure that you are not leaking 15640 11:51:02,080 --> 11:51:06,639 memory in your program so if we run this 15641 11:51:04,879 --> 11:51:08,559 program it's going to run it's going to 15642 11:51:06,639 --> 11:51:11,680 run and finish let's say that it is 15643 11:51:08,559 --> 11:51:14,559 ending well okay so we're going to build 15644 11:51:11,680 --> 11:51:16,559 and see this run the build is good so we 15645 11:51:14,559 --> 11:51:18,559 can clear and run rooster and we're 15646 11:51:16,559 --> 11:51:20,720 going to see that the program is ending 15647 11:51:20,720 --> 11:51:24,880 leaking memory here because the 15648 11:51:22,879 --> 11:51:26,559 dynamically piece of memory that we 15649 11:51:26,559 --> 11:51:32,159 wasn't released to the system and when 15650 11:51:29,275 --> 11:51:34,559 we did this assignment here we just lost 15651 11:51:32,160 --> 11:51:36,400 access to it so the operating system is 15652 11:51:34,559 --> 11:51:39,435 going to thank this memory belongs to 15653 11:51:36,400 --> 11:51:41,756 this program that is ours here but your 15654 11:51:39,436 --> 11:51:44,239 program is either not going to be using 15655 11:51:41,756 --> 11:51:46,319 this piece of memory because we have 15656 11:51:46,319 --> 11:51:50,800 that would allow us to go and do 15657 11:51:48,160 --> 11:51:52,480 something useful with this memory here 15658 11:51:50,800 --> 11:51:55,200 so this is something bad and you should 15659 11:51:52,480 --> 11:51:57,680 avoid it the second example of memory 15660 11:51:55,199 --> 11:51:59,915 leaks is when you do double allocation 15661 11:51:57,680 --> 11:52:02,160 on a pointer for example here we have 15662 11:51:59,915 --> 11:52:04,079 pin number one again and we are going to 15663 11:52:05,595 --> 11:52:12,159 of a piece of memory that was allocated 15664 11:52:09,116 --> 11:52:15,116 dynamically on the heap the value inside 15665 11:52:12,160 --> 11:52:18,960 is going to be 55 but we are using this 15666 11:52:15,116 --> 11:52:22,000 pointer again to make it point to a new 15667 11:52:18,959 --> 11:52:25,116 piece of memory containing the address 15668 11:52:22,000 --> 11:52:27,680 of the newly allocated piece of memory 15669 11:52:25,116 --> 11:52:29,680 on the stack on the heap i mean and this 15670 11:52:27,680 --> 11:52:32,319 is really bad because again once we do 15671 11:52:29,680 --> 11:52:33,915 this we lose access to this memory here 15672 11:52:33,915 --> 11:52:37,595 the program here is not going to have 15673 11:52:35,595 --> 11:52:40,319 access to it and the operating system is 15674 11:52:37,595 --> 11:52:43,275 going to thank we own this memory and it 15675 11:52:40,319 --> 11:52:45,436 is going to be bad and uh down here 15676 11:52:43,275 --> 11:52:48,000 maybe you can go and say delete number 15677 11:52:45,436 --> 11:52:50,480 one p number one but this is not going 15678 11:52:48,000 --> 11:52:53,116 to release the memory here it is going 15679 11:52:50,480 --> 11:52:55,436 to release the memory here with the 44 15680 11:52:53,116 --> 11:52:58,160 and the memory containing 55 here is 15681 11:52:55,436 --> 11:53:00,720 going to be leaked let's be good c plus 15682 11:52:58,160 --> 11:53:02,880 plus citizens and reset our pointers 15683 11:53:00,720 --> 11:53:05,756 after we delete so we're going to say p 15684 11:53:02,879 --> 11:53:08,159 number one equals no pointer and we are 15685 11:53:05,756 --> 11:53:10,959 going to be playing it safe here and 15686 11:53:08,160 --> 11:53:12,400 again we just lift memory here and it is 15687 11:53:10,959 --> 11:53:15,199 something you should avoid in your 15688 11:53:12,400 --> 11:53:18,400 programs if you run this program it is 15689 11:53:15,199 --> 11:53:20,159 going to run just fine so let's see that 15690 11:53:18,400 --> 11:53:22,959 because i don't want to leave you with 15691 11:53:22,959 --> 11:53:27,680 if i run rooster again it's going to end 15692 11:53:25,040 --> 11:53:28,959 well but it is going to be leaking 15693 11:53:28,959 --> 11:53:33,915 so we don't want to leak memory avoid 15694 11:53:31,756 --> 11:53:35,520 this like a plague we're going to 15695 11:53:35,519 --> 11:53:40,000 and show you that you can also leak 15696 11:53:37,199 --> 11:53:42,319 memory when you have some nested scopes 15697 11:53:40,000 --> 11:53:44,800 in your programs okay let's go down here 15698 11:53:42,319 --> 11:53:47,360 and put that in and here is a simple 15699 11:53:44,800 --> 11:53:49,840 example we have a nested scope nested 15700 11:53:47,360 --> 11:53:52,160 inside the main function and inside the 15701 11:53:49,839 --> 11:53:53,275 scope here we are setting up a new 15702 11:53:53,275 --> 11:53:57,839 and it is going to be pointing to 15703 11:53:55,275 --> 11:54:00,319 dynamically allocated memory and that 15704 11:53:57,839 --> 11:54:02,399 memory is going to contain a 57. 15705 11:54:00,319 --> 11:54:05,595 the only key we have to this dynamic 15706 11:54:02,400 --> 11:54:08,239 memory is this p number two pointer here 15707 11:54:05,595 --> 11:54:11,275 but this p number two pointer lives on 15708 11:54:08,239 --> 11:54:13,199 the stack itself so when the scope here 15709 11:54:11,275 --> 11:54:15,756 is going to end p number two is no 15710 11:54:13,199 --> 11:54:17,755 longer going to be accessible and the 15711 11:54:15,756 --> 11:54:20,000 memory is going to be linked if you 15712 11:54:17,756 --> 11:54:22,080 really want to play it safe you can go 15713 11:54:20,000 --> 11:54:24,480 do whatever you want with the dynamic 15714 11:54:22,080 --> 11:54:26,800 memory and when you are done with that 15715 11:54:24,480 --> 11:54:28,800 you can delete that piece of memory here 15716 11:54:26,800 --> 11:54:30,400 while you still have access to p number 15717 11:54:31,756 --> 11:54:35,595 in the scope where this thing was 15718 11:54:35,595 --> 11:54:40,319 if you hit the end of this scope you're 15719 11:54:38,080 --> 11:54:42,480 going to lose access to this pointer and 15720 11:54:40,319 --> 11:54:44,400 you will be losing access to this 15721 11:54:44,400 --> 11:54:48,560 and you're going to be leaking memory 15722 11:54:46,080 --> 11:54:50,319 because you're not deleting it even if 15723 11:54:48,559 --> 11:54:52,720 you want to delete it you won't be able 15724 11:54:50,319 --> 11:54:54,159 to do that because you don't have a key 15725 11:54:52,720 --> 11:54:56,480 that is going to lead you to that 15726 11:54:54,160 --> 11:54:59,040 dynamic memory so if we try and do 15727 11:54:56,480 --> 11:55:01,040 delete p number two here this is not 15728 11:54:59,040 --> 11:55:03,520 going to compile because we don't have 15729 11:55:01,040 --> 11:55:06,639 access to p number two p number two is 15730 11:55:03,519 --> 11:55:09,755 scoped inside this nested scope here 15731 11:55:06,639 --> 11:55:12,959 this is what i mean so be sure you are 15732 11:55:09,756 --> 11:55:15,916 aware of this memory leak problem the 15733 11:55:12,959 --> 11:55:17,595 cases we have seen here are really 15734 11:55:15,915 --> 11:55:20,879 extensive enough to give you an 15735 11:55:17,595 --> 11:55:23,199 intuition on how this is bad and you're 15736 11:55:20,879 --> 11:55:24,959 going to be avoiding this new programs 15737 11:55:23,199 --> 11:55:28,000 and you're going to be on your way to 15738 11:55:24,959 --> 11:55:29,519 become a better c plus plus developer 15739 11:55:28,000 --> 11:55:31,436 this is really all we set out to do in 15740 11:55:29,519 --> 11:55:33,755 this lecture to learn about memory leads 15741 11:55:31,436 --> 11:55:36,000 show you a few examples of them i hope 15742 11:55:33,756 --> 11:55:37,680 now you understand them we are going to 15743 11:55:36,000 --> 11:55:38,959 stop here in this lecture and the next 15744 11:55:37,680 --> 11:55:42,080 one we're going to learn about 15745 11:55:42,080 --> 11:55:45,360 go ahead and finish up here and meet me 15746 11:55:45,360 --> 11:55:51,040 in this lecture we're going to look at 15747 11:55:47,360 --> 11:55:53,840 dynamic arrays and these are arrays you 15748 11:55:51,040 --> 11:55:55,840 can allocate on the heap and they're not 15749 11:55:53,839 --> 11:55:57,435 going to be stored on the stack like we 15750 11:55:55,839 --> 11:56:00,319 have seen before the arrays we have 15751 11:55:57,436 --> 11:56:03,040 learned about so far have been living on 15752 11:56:00,319 --> 11:56:05,436 the stack we want to see how we can 15753 11:56:03,040 --> 11:56:08,400 allocate arrays on the heap if we happen 15754 11:56:05,436 --> 11:56:10,800 to need that in our applications arrays 15755 11:56:08,400 --> 11:56:13,116 are allocated on the hip with the new 15756 11:56:10,800 --> 11:56:16,720 operator we can also use the sdd no 15757 11:56:13,116 --> 11:56:19,116 throw version of the new operator if we 15758 11:56:16,720 --> 11:56:21,916 don't want exceptions in our c plus plus 15759 11:56:19,116 --> 11:56:23,840 programs here is a simple example of how 15760 11:56:21,915 --> 11:56:25,595 you can do that we have a pointer called 15761 11:56:23,839 --> 11:56:28,319 p salaries it's going to be a pointer to 15762 11:56:25,595 --> 11:56:31,360 double and it is going to contain the 15763 11:56:28,319 --> 11:56:33,040 address to the dynamic memory that we 15764 11:56:31,360 --> 11:56:35,040 allocate on the heap which is going to 15765 11:56:35,040 --> 11:56:40,160 of doubles of this size here okay this 15766 11:56:38,400 --> 11:56:42,720 is an array that is going to be 15767 11:56:40,160 --> 11:56:45,040 initialized with junk values because we 15768 11:56:45,040 --> 11:56:49,360 explicitly so this is something you need 15769 11:56:47,116 --> 11:56:52,720 to be aware of okay p students is also 15770 11:56:49,360 --> 11:56:54,959 going to be containing the address of an 15771 11:56:52,720 --> 11:56:57,916 array that is going to be allocated on 15772 11:56:54,959 --> 11:57:00,400 the heap we are using the sdd nothro 15773 11:56:57,915 --> 11:57:02,399 version of the new operator because in 15774 11:57:00,400 --> 11:57:04,480 this case for example we might not want 15775 11:57:02,400 --> 11:57:06,000 exceptions and we're going to say that 15776 11:57:04,480 --> 11:57:09,116 it is an array of events it's going to 15777 11:57:06,000 --> 11:57:11,680 be this size but we are using breast 15778 11:57:09,116 --> 11:57:13,680 initializer so this is going to be 15779 11:57:13,680 --> 11:57:17,360 all the values inside are going to be 15780 11:57:15,436 --> 11:57:20,400 initialized to zero this is what we mean 15781 11:57:17,360 --> 11:57:22,800 here we also have a p scores array we 15782 11:57:20,400 --> 11:57:25,436 are using the std no throw version of 15783 11:57:22,800 --> 11:57:28,319 new and it is going to be this size and 15784 11:57:25,436 --> 11:57:29,436 initialized with one two three four five 15785 11:57:29,436 --> 11:57:33,200 the array is bigger than five the 15786 11:57:31,595 --> 11:57:35,915 remaining slots are going to be 15787 11:57:33,199 --> 11:57:37,839 initialized with zeros just like we saw 15788 11:57:35,915 --> 11:57:40,159 with the arrays we have learned about 15789 11:57:37,839 --> 11:57:41,839 previously that we're stored on the 15790 11:57:40,160 --> 11:57:44,000 stack this is really how you work with 15791 11:57:41,839 --> 11:57:46,799 these things and i hope it is super 15792 11:57:44,000 --> 11:57:47,915 clear again we are calling the new 15793 11:57:47,915 --> 11:57:53,360 this is going to allocate however memory 15794 11:57:50,720 --> 11:57:55,680 space is needed to store these things on 15795 11:57:53,360 --> 11:57:57,915 the heap so if this array is going to 15796 11:57:55,680 --> 11:58:00,239 contain 10 elements we're going to 15797 11:57:57,915 --> 11:58:02,399 allocate enough space to contain 10 15798 11:58:00,239 --> 11:58:04,879 double elements on the heap and we're 15799 11:58:02,400 --> 11:58:06,480 going to be pointing to that with the p 15800 11:58:04,879 --> 11:58:09,275 salaries pointer that's what we mean 15801 11:58:06,480 --> 11:58:11,916 here once you have your dynamic array 15802 11:58:09,275 --> 11:58:13,595 allocated on the hip you can try to do 15803 11:58:11,915 --> 11:58:15,275 things with it for example we can try 15804 11:58:13,595 --> 11:58:18,000 and loop through the squirrels and print 15805 11:58:15,275 --> 11:58:21,360 them out here you see that we are using 15806 11:58:18,000 --> 11:58:23,756 the angle bracket syntax and the 15807 11:58:21,360 --> 11:58:25,360 pointer arithmetic syntax it is going to 15808 11:58:23,756 --> 11:58:27,595 work equally well you can choose 15809 11:58:25,360 --> 11:58:29,680 whatever you want but i just wanted you 15810 11:58:27,595 --> 11:58:31,680 to see that you can use both versions 15811 11:58:29,680 --> 11:58:34,000 here the next logical thing to think 15812 11:58:31,680 --> 11:58:36,639 about when you talk about dynamic memory 15813 11:58:34,000 --> 11:58:38,800 allocation is how you release the memory 15814 11:58:36,639 --> 11:58:41,040 to the operating system when you're no 15815 11:58:38,800 --> 11:58:43,596 longer using it and this is the syntax 15816 11:58:41,040 --> 11:58:45,436 to do it with arrays you say delete you 15817 11:58:43,595 --> 11:58:48,239 put angle brackets and you say the 15818 11:58:45,436 --> 11:58:50,639 pointer that was used to allocate that 15819 11:58:48,239 --> 11:58:52,639 memory in the first place and after you 15820 11:58:50,639 --> 11:58:54,639 do that you have to remember to reset 15821 11:58:52,639 --> 11:58:57,595 the pointer to null pointer because you 15822 11:58:54,639 --> 11:58:59,436 are a good c plus plus citizen and this 15823 11:58:57,595 --> 11:59:01,199 is really all we had to say in this 15824 11:58:59,436 --> 11:59:04,480 lecture this is how you dynamically 15825 11:59:01,199 --> 11:59:06,639 allocate a raise on the hip before we 15826 11:59:04,480 --> 11:59:08,960 head over to visual studio code and play 15827 11:59:06,639 --> 11:59:11,839 with this though i want you to keep in 15828 11:59:08,959 --> 11:59:14,159 your mind that arrays you allocate 15829 11:59:11,839 --> 11:59:16,559 dynamically on the heap are very 15830 11:59:14,160 --> 11:59:18,480 different from the arrays that we have 15831 11:59:16,559 --> 11:59:21,360 learned about before and that's because 15832 11:59:18,480 --> 11:59:24,640 they don't work with std size and they 15833 11:59:21,360 --> 11:59:26,400 don't work for range based for loops 15834 11:59:24,639 --> 11:59:28,479 and that the reason for all these 15835 11:59:26,400 --> 11:59:30,639 limitations is that when you do 15836 11:59:28,480 --> 11:59:34,000 something like this the array really 15837 11:59:30,639 --> 11:59:36,800 decays into a pointer and it loses 15838 11:59:34,000 --> 11:59:39,275 information that would otherwise be used 15839 11:59:36,800 --> 11:59:41,840 to deduce the size that is used by sdd 15840 11:59:39,275 --> 11:59:44,639 size and there are other things that are 15841 11:59:41,839 --> 11:59:46,959 used by the range base for loop that 15842 11:59:44,639 --> 11:59:48,959 they lose when you store and refer to 15843 11:59:46,959 --> 11:59:51,519 them using a pointer like this that's 15844 11:59:48,959 --> 11:59:53,839 why we have this limitation keep this in 15845 11:59:51,519 --> 11:59:56,239 mind if you are using a dynamically 15846 11:59:53,839 --> 11:59:58,479 allocated array on the heap you're going 15847 11:59:56,239 --> 12:00:01,040 to be using other techniques to deduce 15848 11:59:58,480 --> 12:00:03,200 its size and it is not going to work 15849 12:00:01,040 --> 12:00:05,915 with a range base for loop so we will 15850 12:00:03,199 --> 12:00:08,000 have to use other kinds of loops that we 15851 12:00:05,915 --> 12:00:09,839 have learned about in this course and it 15852 12:00:09,839 --> 12:00:14,239 this is really all i had to share in 15853 12:00:11,915 --> 12:00:16,719 this lecture now i think it's time we 15854 12:00:14,239 --> 12:00:18,639 went to visual studio code and actually 15855 12:00:18,639 --> 12:00:24,319 here we are in our working folder the 15856 12:00:21,519 --> 12:00:25,435 current project is dynamically allocated 15857 12:00:25,436 --> 12:00:29,680 we are going to grab our template files 15858 12:00:29,680 --> 12:00:34,239 and we're going to put that in the 15859 12:00:31,436 --> 12:00:36,959 current project and open this in visual 15860 12:00:34,239 --> 12:00:39,360 studio code like we always do and we are 15861 12:00:36,959 --> 12:00:41,595 going to go down and put in some starter 15862 12:00:39,360 --> 12:00:43,756 code we can play with here we're going 15863 12:00:43,756 --> 12:00:48,880 file icon to remove this pane 15864 12:00:46,480 --> 12:00:50,560 and we're going to try and show the code 15865 12:00:48,879 --> 12:00:53,115 here and again you see that visual 15866 12:00:50,559 --> 12:00:55,360 studio code is freaking out when it sees 15867 12:00:53,116 --> 12:00:57,520 the std no throw version of the new 15868 12:00:55,360 --> 12:00:59,915 operator here but it is going to work 15869 12:00:57,519 --> 12:01:02,799 when we hit the compiler okay so we are 15870 12:00:59,915 --> 12:01:04,879 allocating three arrays on the heap 15871 12:01:02,800 --> 12:01:07,756 dynamically and we are going to be 15872 12:01:04,879 --> 12:01:10,239 storing the addresses to these arrays 15873 12:01:07,756 --> 12:01:12,639 and these pointers for example p 15874 12:01:10,239 --> 12:01:14,559 salaries is going to store the address 15875 12:01:12,639 --> 12:01:16,400 to this dynamic array which is going to 15876 12:01:14,559 --> 12:01:18,559 be leaving on the heap and it is going 15877 12:01:16,400 --> 12:01:20,880 to be the same case for p students here 15878 12:01:18,559 --> 12:01:23,360 and p scores i don't think there is much 15879 12:01:20,879 --> 12:01:25,040 to explain here because we have already 15880 12:01:23,360 --> 12:01:27,680 learned about arrays we know about the 15881 12:01:25,040 --> 12:01:29,915 size of an array we know that it is a 15882 12:01:27,680 --> 12:01:32,000 piece of contiguous memory location that 15883 12:01:29,915 --> 12:01:33,519 is going to be storing variables of the 15884 12:01:32,000 --> 12:01:34,720 same type this is really what we're 15885 12:01:34,720 --> 12:01:39,520 so let's try and compile this and see 15886 12:01:36,720 --> 12:01:42,239 that it actually goes through our gcc 15887 12:01:39,519 --> 12:01:44,079 compiler and it is going to work fine 15888 12:01:42,239 --> 12:01:46,720 and another thing you should notice is 15889 12:01:44,080 --> 12:01:49,360 that this is really compiling even if 15890 12:01:46,720 --> 12:01:52,639 what we are using to store the size of 15891 12:01:49,360 --> 12:01:55,040 the array is not const remember for 15892 12:01:52,639 --> 12:01:56,559 stack arrays or static arrays that we 15893 12:01:56,559 --> 12:01:59,756 we had to make sure this guy here is 15894 12:02:02,000 --> 12:02:07,275 a requirement and if you didn't fulfill 15895 12:02:04,480 --> 12:02:09,916 that your code wouldn't compile now you 15896 12:02:07,275 --> 12:02:12,479 see that with dynamic arrays the code is 15897 12:02:09,915 --> 12:02:14,159 actually compiling even if it's not cost 15898 12:02:12,480 --> 12:02:16,960 and this is something you should be 15899 12:02:14,160 --> 12:02:19,436 aware of let's build this again i just 15900 12:02:19,436 --> 12:02:23,756 and the world is good if we go down and 15901 12:02:22,319 --> 12:02:26,400 run it we're not going to see anything 15902 12:02:23,756 --> 12:02:28,639 really because we're not having any 15903 12:02:26,400 --> 12:02:30,160 output in this program but this is how 15904 12:02:30,160 --> 12:02:36,320 space for a dynamic array on the heap 15905 12:02:33,680 --> 12:02:38,559 now let's try and print some of the data 15906 12:02:36,319 --> 12:02:41,360 in this course here let's go down here 15907 12:02:38,559 --> 12:02:43,519 and actually do that we are going to do 15908 12:02:41,360 --> 12:02:45,275 a no pointer check because we want to 15909 12:02:43,519 --> 12:02:48,000 make sure that memory was actually 15910 12:02:45,275 --> 12:02:50,159 allocated for the p scores array 15911 12:02:48,000 --> 12:02:52,480 and after we do that we're going to loop 15912 12:02:52,480 --> 12:02:56,880 and we're going to be printing whatever 15913 12:02:54,080 --> 12:02:59,040 element is at each iteration the index 15914 12:02:56,879 --> 12:03:01,435 at each iteration and you see that we 15915 12:02:59,040 --> 12:03:04,639 can either use the angle brackets 15916 12:03:01,436 --> 12:03:06,560 notation or the pointer arithmetic 15917 12:03:04,639 --> 12:03:07,360 notation it is going to work equally 15918 12:03:08,959 --> 12:03:13,595 let's bring up our terminal we haven't 15919 12:03:10,800 --> 12:03:15,360 opened one and let's go down and clear 15920 12:03:20,639 --> 12:03:25,756 and we're going to be printing the data 15921 12:03:22,879 --> 12:03:28,000 first using the angle brackets notation 15922 12:03:25,756 --> 12:03:30,800 second using pointer arithmetic and you 15923 12:03:28,000 --> 12:03:32,879 see that it is working pretty well 15924 12:03:30,800 --> 12:03:34,480 one thing you should have noticed now is 15925 12:03:34,480 --> 12:03:37,840 released the memory that we have 15926 12:03:36,160 --> 12:03:39,756 allocated here and this is something 15927 12:03:37,839 --> 12:03:42,239 really bad so we're going to see how we 15928 12:03:39,756 --> 12:03:45,436 can do that we have allocated space for 15929 12:03:42,239 --> 12:03:47,275 salaries p students and scores so let's 15930 12:03:45,436 --> 12:03:48,959 go down and do that and we're going to 15931 12:03:48,959 --> 12:03:53,519 and we're going to have angular brackets 15932 12:03:50,720 --> 12:03:56,480 because this is an array we are 15933 12:03:53,519 --> 12:03:59,359 releasing memory for or to be exact a 15934 12:03:56,480 --> 12:04:01,916 dynamic array which was allocated on the 15935 12:04:03,680 --> 12:04:09,360 and we're going to reset that pointer 15936 12:04:06,639 --> 12:04:11,839 to no pointer because we are good c plus 15937 12:04:09,360 --> 12:04:13,915 plus citizens so we're going to do that 15938 12:04:11,839 --> 12:04:15,915 no pointer and we're going to do the 15939 12:04:17,680 --> 12:04:21,519 space for a dynamic array so the angle 15940 12:04:21,519 --> 12:04:27,915 the square brackets i should say and we 15941 12:04:24,400 --> 12:04:30,800 are going to delete p students i think 15942 12:04:27,915 --> 12:04:32,959 yes so we're going to say p student 15943 12:04:30,800 --> 12:04:35,520 and we're going to say p students and 15944 12:04:32,959 --> 12:04:37,116 we're going to reset this to no pointer 15945 12:04:35,519 --> 12:04:38,879 and we're going to do the same for p 15946 12:04:38,879 --> 12:04:44,079 let's grab this here that's what we want 15947 12:04:42,160 --> 12:04:46,480 no that's not what we want we're going 15948 12:04:44,080 --> 12:04:48,000 to say delete and say square brackets 15949 12:04:46,480 --> 12:04:50,720 ourselves and we're going to say p 15950 12:04:48,000 --> 12:04:52,959 scores and we're going to release that 15951 12:04:52,959 --> 12:04:57,915 and reset this to null pointer now we 15952 12:04:56,080 --> 12:05:00,319 are really playing it safe and our 15953 12:04:57,915 --> 12:05:02,479 program is going to release the memory 15954 12:05:00,319 --> 12:05:03,839 that we allocated in these three 15955 12:05:03,839 --> 12:05:07,435 if we build the program it is going to 15956 12:05:05,680 --> 12:05:10,239 go through well so we're going to build 15957 12:05:13,040 --> 12:05:17,680 and run rooster and it is going to run 15958 12:05:15,915 --> 12:05:18,719 pretty well you see that for peace 15959 12:05:18,720 --> 12:05:24,160 we initialized the first five elements 15960 12:05:22,160 --> 12:05:25,520 and the remaining elements were 15961 12:05:25,519 --> 12:05:29,755 implicitly by the compiler to zero and 15962 12:05:28,080 --> 12:05:32,000 you can see that here i don't think i 15963 12:05:29,756 --> 12:05:33,680 explained this but this is my chance to 15964 12:05:32,000 --> 12:05:35,915 explain this i hope you understand the 15965 12:05:33,680 --> 12:05:37,595 next thing i wanted to explore before we 15966 12:05:35,915 --> 12:05:40,079 close this video is the difference 15967 12:05:37,595 --> 12:05:42,479 between static arrays and dynamic arrays 15968 12:05:40,080 --> 12:05:44,319 a static array is an array you allocate 15969 12:05:42,480 --> 12:05:47,276 on the stack like we have been doing 15970 12:05:44,319 --> 12:05:49,839 before and you do it using a syntax like 15971 12:05:47,275 --> 12:05:51,839 let's say scores why not and it is going 15972 12:05:49,839 --> 12:05:54,079 to be an array that says it is going to 15973 12:05:54,080 --> 12:05:57,840 and we are going to put in our values in 15974 12:05:57,839 --> 12:06:01,839 and we're going to go all the way to 10 15975 12:06:00,080 --> 12:06:04,400 let's put in a 10 here and we're going 15976 12:06:04,400 --> 12:06:08,400 this array is cool and you can loop 15977 12:06:06,639 --> 12:06:10,479 through it you can do all kinds of crazy 15978 12:06:08,400 --> 12:06:13,200 things with it but i am going to 15979 12:06:10,480 --> 12:06:15,680 emphasize two differences we have with 15980 12:06:13,199 --> 12:06:17,519 dynamic arrays one is that this guy is 15981 12:06:15,680 --> 12:06:19,436 going to work with sdd size so we're 15982 12:06:19,436 --> 12:06:22,720 okay this is going to work if we compile 15983 12:06:22,720 --> 12:06:27,680 we're going to get a good world at least 15984 12:06:25,436 --> 12:06:30,239 i hope yes the world is good and if we 15985 12:06:30,239 --> 12:06:33,436 we're going to get scores the size is 15986 12:06:33,436 --> 12:06:37,520 the other thing is that you can use a 15987 12:06:35,436 --> 12:06:40,239 range based for loop to loop through 15988 12:06:37,519 --> 12:06:41,359 this guy here so for example we can say 15989 12:06:41,360 --> 12:06:47,915 auto score s for score in scores 15990 12:06:45,116 --> 12:06:50,000 and it is going to work so let's go down 15991 12:06:47,915 --> 12:06:52,959 and say what we are doing here or we can 15992 12:06:50,000 --> 12:06:55,360 even say a single line to separate fence 15993 12:06:52,959 --> 12:06:57,436 here i don't want to be confused so i am 15994 12:06:55,360 --> 12:07:00,000 going to do something like this so that 15995 12:06:57,436 --> 12:07:01,275 i know where my output here is going to 15996 12:07:01,275 --> 12:07:07,199 so i am going to build this 15997 12:07:03,839 --> 12:07:08,879 with gcc of course the world is going to 15998 12:07:12,319 --> 12:07:17,360 i am going to see that the size is 10 15999 12:07:14,480 --> 12:07:21,040 and i am going to print my elements here 16000 12:07:21,040 --> 12:07:25,436 okay let's see how these things behave 16001 12:07:23,199 --> 12:07:28,719 when we use a dynamic array we are going 16002 12:07:25,436 --> 12:07:31,436 to set up a similar array but on the hip 16003 12:07:28,720 --> 12:07:33,520 which is going to leave on the hip this 16004 12:07:31,436 --> 12:07:35,360 array here is the static array in that 16005 12:07:33,519 --> 12:07:37,359 it is going to live on the stack you 16006 12:07:35,360 --> 12:07:38,959 have to be aware of these differences 16007 12:07:37,360 --> 12:07:40,639 okay we're going to do another one which 16008 12:07:38,959 --> 12:07:43,040 is going to leave on the heap and the 16009 12:07:40,639 --> 12:07:45,360 way we do that we say end pointer let's 16010 12:07:43,040 --> 12:07:47,436 say p scores one to make sure this is 16011 12:07:45,360 --> 12:07:49,116 different from p squares that we have on 16012 12:07:47,436 --> 12:07:51,275 top here and we're going to have 10 16013 12:07:49,116 --> 12:07:53,840 elements inside and we can even 16014 12:07:51,275 --> 12:07:56,559 initialize this guy exactly like we are 16015 12:07:53,839 --> 12:07:58,639 doing here but the data is not going to 16016 12:07:56,559 --> 12:08:00,159 leave on the stack the data is going to 16017 12:08:00,160 --> 12:08:04,720 if we do something like this first let's 16018 12:08:02,959 --> 12:08:06,799 make sure that the code compiles we're 16019 12:08:08,319 --> 12:08:12,959 and watch the compiler output here the 16020 12:08:10,639 --> 12:08:14,400 bolt is good so this is valid c plus 16021 12:08:14,400 --> 12:08:21,040 we can try and use hdd size on that and 16022 12:08:17,595 --> 12:08:21,040 we're going to go down and do that 16023 12:08:21,360 --> 12:08:26,559 and if we do that we're going to say p 16024 12:08:26,559 --> 12:08:31,435 one size and say p scores if we try to 16025 12:08:29,595 --> 12:08:33,519 pass this pointer here you're going to 16026 12:08:31,436 --> 12:08:36,239 see that visual studio code is going to 16027 12:08:33,519 --> 12:08:37,435 freak out because this won't work 16028 12:08:37,436 --> 12:08:43,680 array here has decayed into a pointer 16029 12:08:40,879 --> 12:08:46,000 when we refer to it using a row pointer 16030 12:08:43,680 --> 12:08:48,800 like this so we can't really use it 16031 12:08:48,800 --> 12:08:53,040 so if we look at this we're going to get 16032 12:08:50,480 --> 12:08:54,880 a weird compiler error but this is going 16033 12:08:54,879 --> 12:09:00,159 okay so you can't do this with dynamic 16034 12:08:57,199 --> 12:09:03,039 arrays this is one limitation the other 16035 12:09:00,160 --> 12:09:06,160 is that you can't use dynamic arrays 16036 12:09:03,040 --> 12:09:09,680 like we are using a range based for loop 16037 12:09:06,160 --> 12:09:12,720 here so if we grab this loop and try to 16038 12:09:09,680 --> 12:09:14,959 work with it on p scores for example 16039 12:09:12,720 --> 12:09:15,680 this is something somebody might want to 16040 12:09:16,680 --> 12:09:21,040 pscores1 okay and try to print something 16041 12:09:19,595 --> 12:09:23,040 inside you're going to see that this is 16042 12:09:21,040 --> 12:09:25,595 not going to work the compiler visuals 16043 12:09:23,040 --> 12:09:29,840 video code is not going to like it first 16044 12:09:25,595 --> 12:09:31,839 and if we try and run this through gcc 16045 12:09:29,839 --> 12:09:34,000 we're also going to get a weird compiler 16046 12:09:31,839 --> 12:09:37,039 arrow and the problem is going to be 16047 12:09:34,000 --> 12:09:39,275 that p scores is really not an array it 16048 12:09:37,040 --> 12:09:42,000 is just a pointer that is pointing to 16049 12:09:39,275 --> 12:09:44,479 some location in memory that's all it is 16050 12:09:42,000 --> 12:09:46,720 and it doesn't have information that 16051 12:09:44,480 --> 12:09:47,756 could be used otherwise to loop through 16052 12:09:47,756 --> 12:09:54,800 like we can do for a real static array 16053 12:09:52,000 --> 12:09:57,360 that is leaving on the stack again let's 16054 12:09:54,800 --> 12:09:59,916 be super clear here and say that this 16055 12:09:59,915 --> 12:10:04,079 on the heap you have to understand these 16056 12:10:02,000 --> 12:10:06,879 things but this is really not a big 16057 12:10:04,080 --> 12:10:09,116 limitation because you can store the 16058 12:10:06,879 --> 12:10:11,360 size of this guy in some variable and 16059 12:10:09,116 --> 12:10:13,436 refer to it if you need that and you can 16060 12:10:11,360 --> 12:10:15,840 loop through the array exactly like we 16061 12:10:13,436 --> 12:10:18,480 did here using size and it is going to 16062 12:10:15,839 --> 12:10:20,879 work pretty well so don't be afraid to 16063 12:10:18,480 --> 12:10:23,200 store your arrays on the heap if that's 16064 12:10:20,879 --> 12:10:25,115 what you need in your application it is 16065 12:10:23,199 --> 12:10:28,079 going to work pretty well you just have 16066 12:10:25,116 --> 12:10:31,840 to be aware of some limitations that 16067 12:10:28,080 --> 12:10:33,596 come with doing things this way here 16068 12:10:31,839 --> 12:10:35,359 this is really all we set out to do in 16069 12:10:33,595 --> 12:10:38,559 this lecture i hope you have a better 16070 12:10:35,360 --> 12:10:40,959 idea about dynamic arrays you set them 16071 12:10:38,559 --> 12:10:43,360 up like this the size here doesn't have 16072 12:10:40,959 --> 12:10:45,839 to be cost like we have with static 16073 12:10:43,360 --> 12:10:48,480 arrays and they have a few differences 16074 12:10:45,839 --> 12:10:51,755 compared to static arrays that were 16075 12:10:48,480 --> 12:10:53,756 stored on the stack and that std size 16076 12:10:51,756 --> 12:10:57,200 doesn't work for them and they don't 16077 12:10:53,756 --> 12:10:59,916 work with range based for loops other 16078 12:10:57,199 --> 12:11:02,479 than that dynamic arrays are ridiculed 16079 12:10:59,915 --> 12:11:04,639 and you should use them when that helps 16080 12:11:02,480 --> 12:11:07,276 whatever it is you are designing 16081 12:11:04,639 --> 12:11:10,319 welcome to this new chapter where we are 16082 12:11:07,275 --> 12:11:12,479 going to be talking about references 16083 12:11:10,319 --> 12:11:15,680 references are a way you can set up 16084 12:11:12,480 --> 12:11:18,240 alliances for your variables and have 16085 12:11:15,680 --> 12:11:21,116 other variable names you can go through 16086 12:11:18,239 --> 12:11:23,436 to modify that same variable for example 16087 12:11:21,116 --> 12:11:25,840 here we have a variable called var it is 16088 12:11:23,436 --> 12:11:29,436 an integer it leaves at this address 16089 12:11:25,839 --> 12:11:32,559 here and the value inside is 33. 16090 12:11:29,436 --> 12:11:35,596 if we set up another alliance to it as 16091 12:11:32,559 --> 12:11:38,879 var elias we can go through this var 16092 12:11:35,595 --> 12:11:40,639 elias variable and use it in pretty much 16093 12:11:38,879 --> 12:11:42,559 the same way we use the original 16094 12:11:40,639 --> 12:11:45,839 valuable and this is going to come in 16095 12:11:42,559 --> 12:11:48,319 handy in many situations as we are about 16096 12:11:45,839 --> 12:11:50,319 to see in this chapter we are going to 16097 12:11:48,319 --> 12:11:52,400 start in the next lecture and see how we 16098 12:11:50,319 --> 12:11:54,400 can declare initialize and use 16099 12:11:55,680 --> 12:11:59,915 go ahead and finish up here and meet me 16100 12:11:58,000 --> 12:12:02,720 there in this lecture we're going to see 16101 12:11:59,915 --> 12:12:05,915 how we can declare and use references in 16102 12:12:02,720 --> 12:12:09,040 our c plus application again a reference 16103 12:12:05,915 --> 12:12:11,519 is an elias variable that you can use to 16104 12:12:09,040 --> 12:12:13,680 reference an original variable and use 16105 12:12:11,519 --> 12:12:16,799 that reference in pretty much the same 16106 12:12:13,680 --> 12:12:19,275 way you use the original variable let's 16107 12:12:16,800 --> 12:12:21,520 see how that can work in c plus plus 16108 12:12:19,275 --> 12:12:24,720 here is a simple example we have two 16109 12:12:21,519 --> 12:12:28,159 variables one is an integer we have 45 n 16110 12:12:24,720 --> 12:12:28,959 the other is a double we have 33.65 16111 12:12:28,959 --> 12:12:33,756 we want to set up references to this 16112 12:12:31,199 --> 12:12:34,719 variable and the syntax is ridiculously 16113 12:12:34,720 --> 12:12:39,840 for the edge value its reference is 16114 12:12:37,040 --> 12:12:42,639 going to be something like ant ampersand 16115 12:12:39,839 --> 12:12:45,039 you put the ampersand symbol you say the 16116 12:12:42,639 --> 12:12:48,080 name of the reference and you initialize 16117 12:12:45,040 --> 12:12:50,239 this with the value in the variable that 16118 12:12:48,080 --> 12:12:52,400 you want this to reference so reference 16119 12:12:50,239 --> 12:12:54,959 to h value is going to be referencing 16120 12:12:52,400 --> 12:12:58,880 our integer here and we can use this 16121 12:12:54,959 --> 12:13:00,799 name to reference this variable here and 16122 12:12:58,879 --> 12:13:03,275 it is going to be like we are using the 16123 12:13:00,800 --> 12:13:04,639 original variable which is interval here 16124 12:13:03,275 --> 12:13:06,639 we're going to see how this works in a 16125 12:13:04,639 --> 12:13:09,199 minute we can do the same using 16126 12:13:06,639 --> 12:13:11,116 assignment initialization and down here 16127 12:13:09,199 --> 12:13:13,435 you see that we have an example for the 16128 12:13:11,116 --> 12:13:15,680 double value and we have a reference to 16129 12:13:13,436 --> 12:13:17,680 it once we have these references we can 16130 12:13:17,680 --> 12:13:20,959 the original variables for example if 16131 12:13:19,436 --> 12:13:23,116 you come here you see we are printing 16132 12:13:20,959 --> 12:13:25,275 stuff out if we print any value we're 16133 12:13:23,116 --> 12:13:26,720 going to get the value printed out if we 16134 12:13:25,275 --> 12:13:28,879 print double value we're going to get 16135 12:13:26,720 --> 12:13:30,239 the value printed out we can also print 16136 12:13:28,879 --> 12:13:32,079 through references you're going to see 16137 12:13:30,239 --> 12:13:34,720 that they are going to print exactly the 16138 12:13:32,080 --> 12:13:36,000 same value as we have in the original 16139 12:13:36,000 --> 12:13:39,360 but the interesting thing is going to be 16140 12:13:39,360 --> 12:13:44,639 if you print the addresses through the 16141 12:13:41,519 --> 12:13:46,559 original values and print the addresses 16142 12:13:44,639 --> 12:13:49,040 through the references you're going to 16143 12:13:46,559 --> 12:13:51,680 get the same addresses printed out which 16144 12:13:49,040 --> 12:13:55,116 is really going to say this to variable 16145 12:13:51,680 --> 12:13:57,360 names are almost exactly the same thing 16146 12:13:55,116 --> 12:14:00,000 in c plus plus and this is really cool 16147 12:13:57,360 --> 12:14:02,639 you can use the reference as if you are 16148 12:14:00,000 --> 12:14:05,116 using the original variable name in your 16149 12:14:02,639 --> 12:14:06,879 c plus plus code we're even going to try 16150 12:14:05,116 --> 12:14:08,080 and go through the sizes and print them 16151 12:14:06,879 --> 12:14:10,399 out and you're going to see that they 16152 12:14:08,080 --> 12:14:12,240 are going to be exactly the same thing 16153 12:14:10,400 --> 12:14:15,040 and we are going to see how important 16154 12:14:12,239 --> 12:14:16,959 this can become in c plus plus code as 16155 12:14:15,040 --> 12:14:19,436 we go through this chapter okay now that 16156 12:14:16,959 --> 12:14:21,915 we have the reference declared we can go 16157 12:14:19,436 --> 12:14:24,160 through that and modify the value and if 16158 12:14:21,915 --> 12:14:27,115 we do that the changes are going to be 16159 12:14:24,160 --> 12:14:29,116 reflected in the original variables and 16160 12:14:27,116 --> 12:14:31,040 if we print stuff like this again we're 16161 12:14:29,116 --> 12:14:33,360 going to see that the changes are going 16162 12:14:31,040 --> 12:14:36,080 to be reflected in the original 16163 12:14:33,360 --> 12:14:38,639 variables okay if you modify the 16164 12:14:36,080 --> 12:14:41,116 original variable the changes are also 16165 12:14:38,639 --> 12:14:43,275 going to be reflected in our references 16166 12:14:41,116 --> 12:14:45,360 and this is exactly what we would expect 16167 12:14:43,275 --> 12:14:47,275 from our references okay so this is 16168 12:14:45,360 --> 12:14:49,840 really all there is about references 16169 12:14:47,275 --> 12:14:51,756 they are alliances to your variables and 16170 12:14:49,839 --> 12:14:54,159 you can use them pretty much the same 16171 12:14:51,756 --> 12:14:56,639 way you use your variables let's head 16172 12:14:54,160 --> 12:14:59,520 over to visual studio code and see some 16173 12:14:56,639 --> 12:15:02,080 of these things in action here we are in 16174 12:14:59,519 --> 12:15:04,639 our working directory the chapter is 16175 12:15:02,080 --> 12:15:06,880 references and the lecture is declaring 16176 12:15:04,639 --> 12:15:09,595 and using references we are going to 16177 12:15:09,595 --> 12:15:14,239 and we're going to put them in place and 16178 12:15:12,080 --> 12:15:16,400 we are going to open this in visual 16179 12:15:14,239 --> 12:15:19,275 studio code we are going to set up a new 16180 12:15:16,400 --> 12:15:21,520 variable let's call this data or it's 16181 12:15:19,275 --> 12:15:23,275 data this is going to be much more 16182 12:15:21,519 --> 12:15:26,559 descriptive and we're going to 16183 12:15:23,275 --> 12:15:28,559 initialize this with a 33 why not and 16184 12:15:26,559 --> 12:15:31,360 we're going to do a double and say 16185 12:15:28,559 --> 12:15:32,959 double data and we're going to put in a 16186 12:15:32,959 --> 12:15:36,799 and we're going to set up references to 16187 12:15:36,800 --> 12:15:41,840 and again a reference is an elias that 16188 12:15:39,680 --> 12:15:44,000 you can use in pretty much the same way 16189 12:15:41,839 --> 12:15:46,239 you use the original variable so we're 16190 12:15:44,000 --> 12:15:47,595 going to set up a reference to int data 16191 12:15:49,199 --> 12:15:54,559 reference mean reference by putting the 16192 12:15:51,595 --> 12:15:55,756 ampersand symbol here and we're going to 16193 12:15:56,879 --> 12:16:03,435 yeah and you initialize that 16194 12:16:00,239 --> 12:16:05,680 with the variable name you want this 16195 12:16:05,680 --> 12:16:10,480 and uh here we want to reference into 16196 12:16:07,839 --> 12:16:12,799 data so we're going to pass in into data 16197 12:16:10,480 --> 12:16:14,560 and this is going to be it our reference 16198 12:16:12,800 --> 12:16:16,800 is declared we can do the same for 16199 12:16:16,800 --> 12:16:20,480 double and we're going to say it's a 16200 12:16:18,400 --> 12:16:22,720 reference we're going to say rough 16201 12:16:20,480 --> 12:16:25,596 double data and we're going to reference 16202 12:16:22,720 --> 12:16:28,000 devo data here so let's do that okay 16203 12:16:25,595 --> 12:16:29,680 let's bring up our terminal and see if 16204 12:16:28,000 --> 12:16:32,480 we can compile this code here we're 16205 12:16:29,680 --> 12:16:35,360 going to compile with gcc like we always 16206 12:16:32,480 --> 12:16:37,756 do and let's close this then on the left 16207 12:16:37,756 --> 12:16:43,200 some of the space here the world is good 16208 12:16:40,800 --> 12:16:44,400 so this syntax is valid c plus plus 16209 12:16:44,400 --> 12:16:49,680 so what we can do is read values from 16210 12:16:47,756 --> 12:16:51,756 all these things and show you that they 16211 12:16:49,680 --> 12:16:54,000 really are the same things so we're 16212 12:16:51,756 --> 12:16:56,239 going to print stuff out and i am going 16213 12:16:54,000 --> 12:16:58,559 to put here some code and i am going to 16214 12:16:56,239 --> 12:17:01,115 come back when i have typed this i don't 16215 12:16:58,559 --> 12:17:03,680 want you to suffer watching me type 16216 12:17:01,116 --> 12:17:05,756 these things okay here i have the code 16217 12:17:03,680 --> 12:17:08,400 type down and you see that i am just 16218 12:17:05,756 --> 12:17:10,400 going to print dana through the original 16219 12:17:08,400 --> 12:17:12,480 variables i am going to print the actual 16220 12:17:10,400 --> 12:17:15,116 data and print the address of the 16221 12:17:12,480 --> 12:17:17,680 variable and do the same for the debo 16222 12:17:15,116 --> 12:17:20,000 data which is of type double 16223 12:17:17,680 --> 12:17:21,360 how convenient and we're going to print 16224 12:17:20,000 --> 12:17:23,199 references we're going to print the 16225 12:17:21,360 --> 12:17:25,199 reference and the address of the 16226 12:17:23,199 --> 12:17:28,559 reference and we're going to do the same 16227 12:17:25,199 --> 12:17:31,915 thing for our ref devil data here let's 16228 12:17:31,915 --> 12:17:35,275 so we're going to run the task to do 16229 12:17:35,275 --> 12:17:40,479 and we are going to clear the book is 16230 12:17:37,595 --> 12:17:43,040 good and run rooster if we run rooster 16231 12:17:40,480 --> 12:17:44,800 you see that let's put a separate end 16232 12:17:43,040 --> 12:17:45,915 line here so that we see the entire 16233 12:17:45,915 --> 12:17:51,275 i really like to see my fans easily 16234 12:17:49,040 --> 12:17:53,275 so let's do that and build again okay 16235 12:17:51,275 --> 12:17:55,519 the build is good we can clear and run 16236 12:17:53,275 --> 12:17:59,360 rooster now and we're going to see the 16237 12:17:55,519 --> 12:18:02,399 original data and data is 33 the address 16238 12:17:59,360 --> 12:18:05,275 is this guy here we have demo data the 16239 12:18:02,400 --> 12:18:07,040 value is 55 and this is the address if 16240 12:18:05,275 --> 12:18:09,115 we look at our references we have 16241 12:18:07,040 --> 12:18:11,595 exactly the same thing the addresses are 16242 12:18:09,116 --> 12:18:13,360 even the same it really is like we are 16243 12:18:11,595 --> 12:18:16,079 using the original variable and this is 16244 12:18:13,360 --> 12:18:18,720 really cool it can allow you to pass 16245 12:18:18,720 --> 12:18:23,360 using references and whatever you are 16246 12:18:21,040 --> 12:18:25,275 using when you have sent this data for 16247 12:18:23,360 --> 12:18:27,360 example to some other file or some other 16248 12:18:25,275 --> 12:18:29,680 function you're going to be 16249 12:18:27,360 --> 12:18:31,756 affecting the same data okay so you're 16250 12:18:29,680 --> 12:18:33,915 not going to be making copies and we're 16251 12:18:31,756 --> 12:18:36,000 going to see how cold this can be 16252 12:18:33,915 --> 12:18:38,639 especially when we get to working with 16253 12:18:36,000 --> 12:18:40,800 functions this is going to be super cool 16254 12:18:38,639 --> 12:18:43,519 okay now we have seen that the 16255 12:18:40,800 --> 12:18:46,400 references are really referencing the 16256 12:18:43,519 --> 12:18:48,079 original values let's try and modify the 16257 12:18:48,080 --> 12:18:51,436 the original values and see that the 16258 12:18:50,080 --> 12:18:53,436 references are going to pick up the 16259 12:18:51,436 --> 12:18:56,436 changes let's do that we're going to 16260 12:18:53,436 --> 12:18:58,959 take 80 data and change that to 16261 12:18:56,436 --> 12:19:03,360 111 and we're going to take the apple 16262 12:19:03,360 --> 12:19:07,436 why not and after we do this we're going 16263 12:19:05,199 --> 12:19:10,159 to print data again so we're going to 16264 12:19:11,519 --> 12:19:15,915 use that again here and we should see 16265 12:19:15,915 --> 12:19:20,319 so we're going to weld with dcc the bolt 16266 12:19:20,319 --> 12:19:24,639 as you see here and if we clear and run 16267 12:19:26,879 --> 12:19:33,199 the value is 55 let's put a separation 16268 12:19:30,319 --> 12:19:35,275 line in here this is cramped and it is 16269 12:19:33,199 --> 12:19:39,915 really hard to see what is happening 16270 12:19:35,275 --> 12:19:43,595 here so i am going to put an std endl in 16271 12:19:39,915 --> 12:19:46,000 here and weld again so let's do that and 16272 12:19:43,595 --> 12:19:48,399 the book is good so we can clear and run 16273 12:19:46,000 --> 12:19:50,800 rooster again and now we see things 16274 12:19:48,400 --> 12:19:53,596 separated out and it is easier on the 16275 12:19:50,800 --> 12:19:56,319 eye here we have empty data to b33 and 16276 12:19:53,595 --> 12:19:58,079 the depot data is 55 and we are going to 16277 12:19:56,319 --> 12:20:00,080 print our references which are going to 16278 12:19:58,080 --> 12:20:02,880 pick up the same things because a 16279 12:20:00,080 --> 12:20:05,116 reference is really analyzed to 16280 12:20:02,879 --> 12:20:06,639 the original variables but if you go 16281 12:20:05,116 --> 12:20:08,720 down you're going to see that the data 16282 12:20:06,639 --> 12:20:10,800 has actually changed and together has 16283 12:20:10,800 --> 12:20:16,560 and nabodana has changed to 67.2 16284 12:20:14,559 --> 12:20:18,799 and the interesting thing is that the 16285 12:20:16,559 --> 12:20:20,720 references have also picked up those 16286 12:20:18,800 --> 12:20:22,639 changes because they really are 16287 12:20:20,720 --> 12:20:24,239 referencing the same things in memory 16288 12:20:22,639 --> 12:20:26,800 it's like we are using the original 16289 12:20:26,800 --> 12:20:31,116 read these things like we are doing with 16290 12:20:29,199 --> 12:20:33,039 references here and this is really cool 16291 12:20:31,116 --> 12:20:35,360 here we have used the original variables 16292 12:20:33,040 --> 12:20:37,116 to modify data but we can also modify 16293 12:20:35,360 --> 12:20:38,959 the data through references and the 16294 12:20:37,116 --> 12:20:40,959 original variables are going to pick up 16295 12:20:38,959 --> 12:20:43,360 those changes so let's do that we're 16296 12:20:40,959 --> 12:20:45,040 going to say ref data and we're going to 16297 12:20:50,839 --> 12:20:57,039 1014 or 12. this is going to do we're 16298 12:20:53,680 --> 12:21:00,959 going to do ref double data and put in 16299 12:20:57,040 --> 12:21:02,879 another large number let's say 1000.45 16300 12:21:00,959 --> 12:21:04,839 why not and we're going to print the 16301 12:21:02,879 --> 12:21:08,559 data again so we're going to grab 16302 12:21:04,839 --> 12:21:10,719 this and print stuff out again so we're 16303 12:21:08,559 --> 12:21:12,319 going to go down and put in the code to 16304 12:21:10,720 --> 12:21:14,080 print stuff and you see it's going to 16305 12:21:12,319 --> 12:21:16,800 use the original variables and the 16306 12:21:14,080 --> 12:21:19,116 references and we should see the changes 16307 12:21:16,800 --> 12:21:20,560 reflected in our terminal here 16308 12:21:20,559 --> 12:21:24,639 and we're going to weld with gcc as we 16309 12:21:24,639 --> 12:21:28,639 the bullet is going to be cool we're 16310 12:21:26,319 --> 12:21:31,915 going to clear and run rooster we're 16311 12:21:28,639 --> 12:21:33,680 going to see that we have 33 and 55 the 16312 12:21:31,915 --> 12:21:35,595 references are going to pick that up 16313 12:21:33,680 --> 12:21:36,680 that's what we have here we're going to 16314 12:21:36,680 --> 12:21:41,519 111 and 67 through the original 16315 12:21:39,595 --> 12:21:43,595 variables and the references are going 16316 12:21:41,519 --> 12:21:46,319 to be picking that up and we're going to 16317 12:21:43,595 --> 12:21:48,799 use references and modify data you see 16318 12:21:46,319 --> 12:21:51,116 that the original variables are also 16319 12:21:48,800 --> 12:21:53,436 picking that up because we are really 16320 12:21:51,116 --> 12:21:55,680 modifying data in the same memory 16321 12:21:53,436 --> 12:21:57,840 location that's why these changes are 16322 12:21:55,680 --> 12:22:00,000 being shared between references and the 16323 12:21:57,839 --> 12:22:01,595 original variables and this is really 16324 12:22:00,000 --> 12:22:03,680 all we set out to do in this lecture to 16325 12:22:01,595 --> 12:22:06,399 show you how you can set up a reference 16326 12:22:03,680 --> 12:22:09,040 and prove that the changes we do either 16327 12:22:06,400 --> 12:22:11,275 through original data or references are 16328 12:22:09,040 --> 12:22:13,595 reflected between these two guys meaning 16329 12:22:11,275 --> 12:22:14,879 references and the original 16330 12:22:14,879 --> 12:22:18,000 we are going to stop here in this 16331 12:22:16,480 --> 12:22:20,560 lecture in the next one we're going to 16332 12:22:18,000 --> 12:22:23,360 do a comparison between pointers and 16333 12:22:20,559 --> 12:22:25,115 references because they seem to be doing 16334 12:22:25,116 --> 12:22:29,360 go ahead and finish up here 16335 12:22:27,040 --> 12:22:32,080 and meet me there and this lecture we're 16336 12:22:29,360 --> 12:22:34,239 going to be looking at the similarities 16337 12:22:32,080 --> 12:22:36,639 and the differences between references 16338 12:22:34,239 --> 12:22:38,479 and pointers okay we know that a pointer 16339 12:22:36,639 --> 12:22:40,879 is really something that is going to 16340 12:22:38,480 --> 12:22:43,276 store the address of the variable and we 16341 12:22:40,879 --> 12:22:45,595 can go through that pointer and modify 16342 12:22:43,275 --> 12:22:47,595 the original variable or really do 16343 12:22:45,595 --> 12:22:49,680 things with that but in the last lecture 16344 12:22:47,595 --> 12:22:52,559 we saw that we could also do the same 16345 12:22:49,680 --> 12:22:54,879 thing with references so why have these 16346 12:22:54,879 --> 12:22:59,839 and what are the differences between 16347 12:22:56,720 --> 12:23:02,160 them here is a simple slide that i put 16348 12:22:59,839 --> 12:23:04,879 together to put the important points 16349 12:23:02,160 --> 12:23:06,560 about references and pointers together 16350 12:23:04,879 --> 12:23:08,879 the first thing is that if you use 16351 12:23:06,559 --> 12:23:11,519 references you are not going to go 16352 12:23:08,879 --> 12:23:12,959 through the star symbol to the reference 16353 12:23:11,519 --> 12:23:15,435 and get to the value you are 16354 12:23:12,959 --> 12:23:17,436 manipulating so that's going to really 16355 12:23:15,436 --> 12:23:19,840 feel like you are using the original 16356 12:23:17,436 --> 12:23:21,596 variable but with pointers you are going 16357 12:23:19,839 --> 12:23:23,115 to go through the referencing so that's 16358 12:23:23,116 --> 12:23:27,916 another thing about references is that 16359 12:23:25,199 --> 12:23:30,399 you can't change them to reference 16360 12:23:27,915 --> 12:23:31,915 something else later but with pointers 16361 12:23:30,400 --> 12:23:34,639 we saw that we could do that we can 16362 12:23:31,915 --> 12:23:37,275 change a pointer to point somewhere else 16363 12:23:34,639 --> 12:23:39,519 if it happens to be a non-const pointer 16364 12:23:37,275 --> 12:23:41,756 the other thing is that for references 16365 12:23:39,519 --> 12:23:43,680 you have to initialize them at 16366 12:23:41,756 --> 12:23:46,160 declaration you can't declare a 16367 12:23:43,680 --> 12:23:47,595 reference and not initialize that if you 16368 12:23:46,160 --> 12:23:49,040 do that you're going to get a compiler 16369 12:23:47,595 --> 12:23:52,399 error and we're going to see that in a 16370 12:23:49,040 --> 12:23:54,559 minute but for pointers you can declare 16371 12:23:52,400 --> 12:23:57,040 a pointer and not initialize it and it 16372 12:23:54,559 --> 12:23:59,595 is going to just contain a junk address 16373 12:23:57,040 --> 12:24:01,840 and if you try to use that uninitialized 16374 12:23:59,595 --> 12:24:04,000 you are going to be setting yourself up 16375 12:24:01,839 --> 12:24:06,079 for trouble so we saw that so these are 16376 12:24:04,000 --> 12:24:08,480 the main differences between pointers 16377 12:24:06,080 --> 12:24:11,520 and references and now we are just going 16378 12:24:08,480 --> 12:24:14,000 to be looking at how these translate in 16379 12:24:11,519 --> 12:24:15,755 code here is a simple example we have a 16380 12:24:14,000 --> 12:24:17,915 variable called double value which 16381 12:24:15,756 --> 12:24:20,639 happens to be double and we have a 16382 12:24:17,915 --> 12:24:22,879 reference to it and a pointer to it so 16383 12:24:20,639 --> 12:24:25,680 you already know these concepts 16384 12:24:22,879 --> 12:24:27,915 now if we want to read the value in here 16385 12:24:25,680 --> 12:24:30,559 through our pointer and references we're 16386 12:24:27,915 --> 12:24:32,799 going to be using two different syntaxes 16387 12:24:30,559 --> 12:24:35,115 if we go through the reference we can 16388 12:24:32,800 --> 12:24:38,080 use this reference variable name 16389 12:24:35,116 --> 12:24:39,916 directly and read the value or change it 16390 12:24:39,915 --> 12:24:44,639 ref double value to print that out this 16391 12:24:42,400 --> 12:24:47,200 is going to work but if we want to go 16392 12:24:44,639 --> 12:24:49,275 through the pointer we will have to go 16393 12:24:47,199 --> 12:24:50,399 through the referencing and this is 16394 12:24:50,400 --> 12:24:55,116 you know sometimes people don't like 16395 12:24:52,400 --> 12:24:57,840 this syntax they think that it is not 16396 12:24:55,116 --> 12:25:00,160 direct enough so they prefer to use 16397 12:24:57,839 --> 12:25:02,319 references to do these kinds of things 16398 12:25:00,160 --> 12:25:04,240 but pointers are still important as we 16399 12:25:02,319 --> 12:25:06,720 will see later as we progress in the 16400 12:25:04,239 --> 12:25:08,559 course so this is one difference with 16401 12:25:06,720 --> 12:25:11,040 references you don't have to go through 16402 12:25:08,559 --> 12:25:13,360 the dereferencing mechanism to use this 16403 12:25:11,040 --> 12:25:15,840 star here to print stuff or even 16404 12:25:13,360 --> 12:25:17,360 manipulate the data inside you just use 16405 12:25:15,839 --> 12:25:19,915 the variable name and it is going to 16406 12:25:17,360 --> 12:25:21,520 work with pointers you have to reference 16407 12:25:19,915 --> 12:25:23,115 because if you don't the reference 16408 12:25:21,519 --> 12:25:25,755 you're going to be referring to the 16409 12:25:23,116 --> 12:25:28,080 address stored in this pointer this is 16410 12:25:25,756 --> 12:25:30,880 the first difference and we see that the 16411 12:25:28,080 --> 12:25:33,916 same applies if we are trying to 16412 12:25:30,879 --> 12:25:35,595 write data into our variable if we go 16413 12:25:33,915 --> 12:25:37,839 through the pointer we will have to 16414 12:25:35,595 --> 12:25:39,115 dereference and use this dollar symbol 16415 12:25:39,116 --> 12:25:44,080 if we are using a reference we can do 16416 12:25:41,116 --> 12:25:46,800 that directly as if we are modifying the 16417 12:25:44,080 --> 12:25:48,639 original variable so this syntax is much 16418 12:25:46,800 --> 12:25:51,200 more readable the other difference is 16419 12:25:48,639 --> 12:25:53,275 that we can't make a reference 16420 12:25:51,199 --> 12:25:55,039 reference something else but we can 16421 12:25:53,275 --> 12:25:57,680 change a pointer to point to something 16422 12:25:55,040 --> 12:25:59,595 else let's look at an example here we 16423 12:25:57,680 --> 12:26:01,680 have ref double value which is a 16424 12:26:02,959 --> 12:26:07,839 we are doing an assignment to this thing 16425 12:26:05,519 --> 12:26:09,519 here we are saying either double value 16426 12:26:09,519 --> 12:26:15,039 to our reference but what we are doing 16427 12:26:12,559 --> 12:26:17,595 here is not making ref double value 16428 12:26:15,040 --> 12:26:19,680 reference this other variable we are 16429 12:26:17,595 --> 12:26:21,595 just grabbing the value in other double 16430 12:26:19,680 --> 12:26:24,319 value and assigning that to our 16431 12:26:21,595 --> 12:26:26,720 reference and the value in either double 16432 12:26:24,319 --> 12:26:29,040 value is going to be reflected in the 16433 12:26:26,720 --> 12:26:31,916 original value here so this is what we 16434 12:26:29,040 --> 12:26:33,915 are doing we are not reassigning to our 16435 12:26:31,915 --> 12:26:36,239 reference to make it reference something 16436 12:26:33,915 --> 12:26:38,799 else we are assigning the value to the 16437 12:26:36,239 --> 12:26:40,639 reference as it is now and it is going 16438 12:26:38,800 --> 12:26:43,040 to be still referencing the same 16439 12:26:40,639 --> 12:26:44,400 variable in memory if you print out the 16440 12:26:43,040 --> 12:26:46,879 address in memory you're going to see 16441 12:26:44,400 --> 12:26:48,880 that the address of the reference here 16442 12:26:46,879 --> 12:26:50,799 hasn't changed so this is something you 16443 12:26:48,879 --> 12:26:52,879 should keep in mind but we know that a 16444 12:26:50,800 --> 12:26:54,720 pointer can point somewhere else if we 16445 12:26:52,879 --> 12:26:57,755 have a pointer here for example p double 16446 12:26:54,720 --> 12:27:00,559 value we can reassign it another address 16447 12:26:57,756 --> 12:27:02,239 and make it point to that new address so 16448 12:27:00,559 --> 12:27:05,040 in this regard we can think of 16449 12:27:02,239 --> 12:27:07,839 references as cost pointers because cost 16450 12:27:05,040 --> 12:27:09,595 pointers can't also be made to point 16451 12:27:07,839 --> 12:27:11,915 somewhere else and if you're trying to 16452 12:27:09,595 --> 12:27:15,756 do that for example here we have a cost 16453 12:27:11,915 --> 12:27:17,519 pointer it is called cost p double value 16454 12:27:15,756 --> 12:27:19,520 and if we try and make it point 16455 12:27:17,519 --> 12:27:21,595 somewhere else through this assignment 16456 12:27:19,519 --> 12:27:24,159 here we're going to get a compiler error 16457 12:27:21,595 --> 12:27:26,239 okay so this is really all in terms of 16458 12:27:24,160 --> 12:27:28,400 the comparisons that we had to make 16459 12:27:26,239 --> 12:27:30,000 between references and pointers now 16460 12:27:28,400 --> 12:27:32,160 we're going to head over to visual 16461 12:27:30,000 --> 12:27:35,360 studio code and see how these things 16462 12:27:32,160 --> 12:27:37,840 work in real code so here we are in our 16463 12:27:35,360 --> 12:27:40,400 working folder the current project is 16464 12:27:37,839 --> 12:27:43,199 comparing pointers and references we are 16465 12:27:40,400 --> 12:27:44,800 going to grab our template files and put 16466 12:27:44,800 --> 12:27:50,400 and we are going to open this project in 16467 12:27:47,519 --> 12:27:52,159 visual studio code like we always do we 16468 12:27:50,400 --> 12:27:54,720 are going to go down and put in some 16469 12:27:52,160 --> 12:27:56,080 starter code and let's align this a 16470 12:27:56,080 --> 12:28:00,720 so and we can close this pane so that we 16471 12:27:58,480 --> 12:28:03,116 have some breathing room here we have a 16472 12:28:00,720 --> 12:28:05,360 variable of double type and we set up a 16473 12:28:03,116 --> 12:28:07,116 reference to it and a pointer to it this 16474 12:28:05,360 --> 12:28:08,160 is how we do this thanks you already 16475 12:28:08,160 --> 12:28:12,880 and we can try and read through 16476 12:28:11,199 --> 12:28:15,595 the original variable through the 16477 12:28:12,879 --> 12:28:18,959 reference and through the pointer and 16478 12:28:15,595 --> 12:28:21,519 see how these syntaxes differ and how 16479 12:28:18,959 --> 12:28:24,479 one might be better than the other for 16480 12:28:21,519 --> 12:28:26,319 example if we look at how we are using 16481 12:28:24,480 --> 12:28:28,400 the reference it's really like we are 16482 12:28:26,319 --> 12:28:30,800 using the original value here we don't 16483 12:28:28,400 --> 12:28:32,560 have to do reference if we try to print 16484 12:28:30,800 --> 12:28:34,800 the pointer directly like this without 16485 12:28:32,559 --> 12:28:37,839 the referencing this is going to print 16486 12:28:34,800 --> 12:28:40,160 the actual address stored in this 16487 12:28:37,839 --> 12:28:43,435 pointer it's not going to print the 16488 12:28:40,160 --> 12:28:45,360 actual value stored in the address 16489 12:28:43,436 --> 12:28:47,840 pointed to by this pointer i really want 16490 12:28:45,360 --> 12:28:50,080 this to be clear so this is the syntax 16491 12:28:47,839 --> 12:28:52,319 we can use for reading stuff either 16492 12:28:50,080 --> 12:28:54,639 through references or pointers we can 16493 12:28:52,319 --> 12:28:56,959 also use these syntaxes to write stuff 16494 12:28:54,639 --> 12:28:58,800 into our variables for example here we 16495 12:28:56,959 --> 12:29:01,116 can go through the pointer and we have 16496 12:28:58,800 --> 12:29:03,200 to the reference and we can go through 16497 12:29:01,116 --> 12:29:04,800 the reference to really do the same 16498 12:29:03,199 --> 12:29:07,039 thing and we're going to see that our 16499 12:29:04,800 --> 12:29:08,080 syntaxes here are going to pick up the 16500 12:29:08,080 --> 12:29:13,756 regardless of whether we write through 16501 12:29:10,720 --> 12:29:15,040 pointer or a reference because all these 16502 12:29:13,756 --> 12:29:18,480 things really are going to be 16503 12:29:15,040 --> 12:29:21,756 referencing the same location in memory 16504 12:29:18,480 --> 12:29:24,560 let's try and bring up a terminal here 16505 12:29:21,756 --> 12:29:26,800 and we are going to world with gcc let's 16506 12:29:24,559 --> 12:29:29,435 do that the build is going to be good 16507 12:29:26,800 --> 12:29:31,200 and we're going to clear and run rooster 16508 12:29:29,436 --> 12:29:33,436 and you're going to see that at first 16509 12:29:35,199 --> 12:29:39,595 that's going to be the value we can get 16510 12:29:37,040 --> 12:29:42,160 it through original value the reference 16511 12:29:39,595 --> 12:29:44,799 or the pointer through the referencing 16512 12:29:42,160 --> 12:29:46,320 and the address is this one here 16513 12:29:44,800 --> 12:29:48,240 if we change the value through the 16514 12:29:46,319 --> 12:29:50,400 pointer we're going to see the changes 16515 12:29:48,239 --> 12:29:52,159 reflected here and if we change the 16516 12:29:50,400 --> 12:29:54,239 value through the reference we're going 16517 12:29:52,160 --> 12:29:56,560 to see the changes reflected in here so 16518 12:29:54,239 --> 12:29:59,040 this is how you can use pointers and 16519 12:29:56,559 --> 12:30:00,720 references to do these things here and 16520 12:29:59,040 --> 12:30:02,559 you can really choose what makes sense 16521 12:30:00,720 --> 12:30:04,880 for your application they mostly do the 16522 12:30:02,559 --> 12:30:07,360 same things but some people say that the 16523 12:30:04,879 --> 12:30:09,595 syntax to the reference the pointer here 16524 12:30:07,360 --> 12:30:11,840 is really ugly that's why most people 16525 12:30:09,595 --> 12:30:14,319 prefer to use references like this but 16526 12:30:11,839 --> 12:30:16,639 references have other benefits as well 16527 12:30:14,319 --> 12:30:18,319 as we're going to see as we progress in 16528 12:30:16,639 --> 12:30:20,239 the course another thing i want you to 16529 12:30:18,319 --> 12:30:22,959 see is that you can't change a reference 16530 12:30:20,239 --> 12:30:24,720 to reference something else in memory 16531 12:30:22,959 --> 12:30:27,756 for example here we have our reference 16532 12:30:24,720 --> 12:30:30,160 which is ref double value if we try and 16533 12:30:27,756 --> 12:30:31,916 make it reference something else 16534 12:30:30,160 --> 12:30:33,840 we're not going to get that result let's 16535 12:30:33,839 --> 12:30:38,799 so we're going to set up another double 16536 12:30:38,800 --> 12:30:42,880 make our ref double value reference this 16537 12:30:42,879 --> 12:30:46,879 the syntax that comes to mind is saying 16538 12:30:46,879 --> 12:30:51,360 double value and assign that the some 16539 12:30:51,360 --> 12:30:55,680 double value here now let's see what 16540 12:30:53,519 --> 12:30:58,479 happens if we do that we're going to try 16541 12:30:55,680 --> 12:31:00,319 and print whatever we have in memory 16542 12:31:00,319 --> 12:31:06,480 ref double value here let's grab this 16543 12:31:04,000 --> 12:31:08,800 and go down here and say that we are 16544 12:31:06,480 --> 12:31:11,040 changing the reference making the 16545 12:31:08,800 --> 12:31:13,436 reference reference something else let's 16546 12:31:11,040 --> 12:31:15,756 say that so if we try to build this 16547 12:31:13,436 --> 12:31:18,239 let's see what we get we're going to run 16548 12:31:18,239 --> 12:31:22,799 and we're going to try and run this 16549 12:31:20,720 --> 12:31:24,080 clear not clear and we're going to run 16550 12:31:25,680 --> 12:31:31,639 the last line of output here say 78.45 16551 12:31:31,756 --> 12:31:38,000 but the address is still the same so we 16552 12:31:35,116 --> 12:31:40,480 are still referencing the same memory 16553 12:31:38,000 --> 12:31:43,360 with our reference and what we just did 16554 12:31:40,480 --> 12:31:45,116 here was just grab the value in some 16555 12:31:45,116 --> 12:31:49,916 and assign that in here but the 16556 12:31:46,959 --> 12:31:52,239 reference is referencing the same memory 16557 12:31:49,915 --> 12:31:55,199 location it's been referencing so in c 16558 12:31:52,239 --> 12:31:56,319 plus plus you don't have a way you can 16559 12:31:56,319 --> 12:32:01,360 to make a reference reference something 16560 12:31:58,800 --> 12:32:03,360 else the only chance you have to make it 16561 12:32:01,360 --> 12:32:06,720 reference something is through 16562 12:32:03,360 --> 12:32:09,436 declaration here and that's the reason 16563 12:32:06,720 --> 12:32:10,959 you can't declare a reference and not 16564 12:32:09,436 --> 12:32:13,040 initialize it if you do that you're 16565 12:32:10,959 --> 12:32:14,400 going to get a compiler error so let's 16566 12:32:13,040 --> 12:32:16,879 try and do something like this we're 16567 12:32:14,400 --> 12:32:18,239 going to take out this double value and 16568 12:32:16,879 --> 12:32:20,079 the moment we do this you're going to 16569 12:32:18,239 --> 12:32:22,639 see that visual studio code is going to 16570 12:32:20,080 --> 12:32:24,880 be unhappy you see the squiggly line and 16571 12:32:22,639 --> 12:32:27,436 it is going to say initial value of 16572 12:32:24,879 --> 12:32:29,115 reference to non-cos must be an l value 16573 12:32:29,116 --> 12:32:33,680 okay if we take out the initialization 16574 12:32:31,595 --> 12:32:35,839 altogether it's going to give us a 16575 12:32:33,680 --> 12:32:38,800 better error i think reference variable 16576 12:32:35,839 --> 12:32:41,435 requires an initializer so every time 16577 12:32:38,800 --> 12:32:44,639 you declare a reference you have to 16578 12:32:41,436 --> 12:32:46,959 initialize it and we should do like we 16579 12:32:44,639 --> 12:32:49,116 just did before here and we're going to 16580 12:32:46,959 --> 12:32:51,756 be fine again you can't declare a 16581 12:32:49,116 --> 12:32:53,276 reference and not initialize it if you 16582 12:32:51,756 --> 12:32:56,080 do that you're going to get a compiler 16583 12:32:53,275 --> 12:32:58,239 error and the reason is that 16584 12:32:56,080 --> 12:33:00,880 this is the only shot you get to make 16585 12:32:58,239 --> 12:33:02,559 your reference reference something else 16586 12:33:00,879 --> 12:33:05,839 okay if there was a way for you to 16587 12:33:02,559 --> 12:33:08,000 bypass this and let the compiler compile 16588 12:33:05,839 --> 12:33:09,915 this you would never get another chance 16589 12:33:08,000 --> 12:33:12,080 to make this guy actually reference 16590 12:33:09,915 --> 12:33:14,319 something and it would really be a 16591 12:33:12,080 --> 12:33:16,319 useless reference so make sure you 16592 12:33:14,319 --> 12:33:19,116 understand this you can't make a 16593 12:33:16,319 --> 12:33:21,275 reference reference something else and 16594 12:33:19,116 --> 12:33:23,116 if you try to do something through a 16595 12:33:23,116 --> 12:33:27,756 you're going to be assigning the value 16596 12:33:24,959 --> 12:33:29,436 of whatever variable you have to the 16597 12:33:27,756 --> 12:33:31,916 reference you're not going to be making 16598 12:33:29,436 --> 12:33:33,360 the reference referencing something else 16599 12:33:31,915 --> 12:33:35,595 the reference is going to keep 16600 12:33:33,360 --> 12:33:37,915 referencing the same variable in memory 16601 12:33:35,595 --> 12:33:39,915 as we have seen here so let's say what 16602 12:33:37,915 --> 12:33:42,399 we were trying to do here but we know 16603 12:33:39,915 --> 12:33:44,719 that we can make pointers reference 16604 12:33:42,400 --> 12:33:45,596 something else so here we have a pointer 16605 12:33:45,595 --> 12:33:50,000 this pointer called the p double value 16606 12:33:47,756 --> 12:33:51,040 and it has the address of double value 16607 12:33:51,040 --> 12:33:57,275 but we can set up another variable 16608 12:33:53,915 --> 12:33:58,959 okay so we can make that reference this 16609 12:33:57,275 --> 12:34:00,479 other variable here and let's do that 16610 12:33:58,959 --> 12:34:02,319 you're going to see that our pointer now 16611 12:34:00,480 --> 12:34:04,960 is going to be containing another 16612 12:34:07,436 --> 12:34:10,880 which is our new pointer and we're going 16613 12:34:10,879 --> 12:34:19,595 the address of some other double value 16614 12:34:15,040 --> 12:34:22,239 and if we print the data now and go down 16615 12:34:19,595 --> 12:34:24,239 we are going to say make the pointer 16616 12:34:22,239 --> 12:34:26,479 point to something else that's what we 16617 12:34:24,239 --> 12:34:29,115 are doing here and we're going to say 16618 12:34:26,480 --> 12:34:31,040 the same thing here through a comma and 16619 12:34:29,116 --> 12:34:32,400 if we print this try to guess what we're 16620 12:34:32,400 --> 12:34:36,720 the pointer is going to contain a 16621 12:34:34,239 --> 12:34:38,400 completely new address because we can do 16622 12:34:42,480 --> 12:34:48,560 and we're going to see our data here 16623 12:34:44,959 --> 12:34:51,519 let's make sure we see the entire fan 16624 12:34:48,559 --> 12:34:53,435 okay i think this is the data we have 16625 12:34:53,436 --> 12:34:58,639 there's no harm in that so we have our 16626 12:34:56,000 --> 12:35:01,839 fence that we have seen before but what 16627 12:34:58,639 --> 12:35:05,040 is going to be important here is that pw 16628 12:35:01,839 --> 12:35:07,915 value now contains a new address 16629 12:35:05,040 --> 12:35:10,239 okay so this is really important and you 16630 12:35:07,915 --> 12:35:12,159 have to understand that so we made the 16631 12:35:10,239 --> 12:35:14,959 pointer successfully point to something 16632 12:35:12,160 --> 12:35:16,160 else and if we change the data in the 16633 12:35:16,160 --> 12:35:20,880 we're not going to be affecting the data 16634 12:35:18,559 --> 12:35:23,360 we have in here in double value we're 16635 12:35:20,879 --> 12:35:25,755 going to be affecting the data we have 16636 12:35:23,360 --> 12:35:27,436 and some other double value here let's 16637 12:35:25,756 --> 12:35:28,959 do that we're going to change the data 16638 12:35:27,436 --> 12:35:31,200 through the pointer so we're going to 16639 12:35:28,959 --> 12:35:32,559 say p double value and we're going to 16640 12:35:35,199 --> 12:35:39,915 and now we can print the data and see 16641 12:35:37,519 --> 12:35:42,000 that things have changed in memory and 16642 12:35:39,915 --> 12:35:44,719 we can print this to see the differences 16643 12:35:42,000 --> 12:35:46,879 here but before we do that let's also 16644 12:35:44,720 --> 12:35:49,116 print the address of the original 16645 12:35:46,879 --> 12:35:52,239 variable and the address because for now 16646 12:35:49,116 --> 12:35:54,160 we are just printing only the address 16647 12:35:52,239 --> 12:35:58,079 through the pointer so what we can do 16648 12:35:54,160 --> 12:36:00,320 here is grab htdc out and say 16649 12:35:58,080 --> 12:36:03,116 address of double volume and we're going 16650 12:36:00,319 --> 12:36:04,879 to put that out through htdc out here 16651 12:36:03,116 --> 12:36:07,276 we're going to do that and we're going 16652 12:36:04,879 --> 12:36:08,959 to print the address of the reference 16653 12:36:08,959 --> 12:36:13,519 ref double value and we're going to put 16654 12:36:11,680 --> 12:36:15,275 that out as well i'm going to say 16655 12:36:16,480 --> 12:36:21,360 and we're going to say std endn and 16656 12:36:19,199 --> 12:36:24,159 after that we are going to grab the 16657 12:36:21,360 --> 12:36:27,436 entire thing here and print it out after 16658 12:36:24,160 --> 12:36:29,360 we modify the value in the pointer 16659 12:36:27,436 --> 12:36:30,639 i hope this is going to show thanks in a 16660 12:36:30,639 --> 12:36:36,479 clear way so we're going to weld this 16661 12:36:33,040 --> 12:36:39,275 with gcc let's do that the build is good 16662 12:36:36,480 --> 12:36:41,360 so let's clear and run rooster and what 16663 12:36:39,275 --> 12:36:42,400 you're going to see is that 16664 12:36:42,400 --> 12:36:47,520 the original variable is living at 16665 12:36:47,519 --> 12:36:51,435 which we can see in our reference but 16666 12:36:49,436 --> 12:36:52,319 the pointer now is pointing to something 16667 12:36:52,319 --> 12:36:56,080 no wonder that the changes we did 16668 12:36:54,480 --> 12:36:58,160 through the pointer are not being 16669 12:36:56,080 --> 12:37:00,240 reflected in our double value because 16670 12:37:00,239 --> 12:37:05,519 are living in different memory locations 16671 12:37:03,436 --> 12:37:08,239 i really hope you see that and this is 16672 12:37:05,519 --> 12:37:10,239 really all we had to share in terms 16673 12:37:08,239 --> 12:37:12,639 of the differences between pointers and 16674 12:37:10,239 --> 12:37:15,519 references i hope you have a clear 16675 12:37:12,639 --> 12:37:16,720 picture of what these two things are and 16676 12:37:15,519 --> 12:37:18,879 we are going to stop here in this 16677 12:37:16,720 --> 12:37:21,520 lecture this one we're going to see how 16678 12:37:18,879 --> 12:37:22,955 the const key world works with 16679 12:37:22,955 --> 12:37:27,116 go ahead and finish up here and meet me 16680 12:37:25,199 --> 12:37:30,159 there in this lecture we're going to see 16681 12:37:27,116 --> 12:37:31,436 how the const key world can be used with 16682 12:37:31,436 --> 12:37:37,520 and again the idea is to set up an alias 16683 12:37:34,000 --> 12:37:40,080 to a variable and make it const so that 16684 12:37:37,519 --> 12:37:42,479 we can't go through it to modify the 16685 12:37:40,080 --> 12:37:44,800 original variable this is the syntax we 16686 12:37:42,480 --> 12:37:47,040 used to set up a non-const reference 16687 12:37:44,800 --> 12:37:50,080 that we have seen so far so for example 16688 12:37:47,040 --> 12:37:52,955 we can say int age put in a value 27 in 16689 12:37:50,080 --> 12:37:54,880 this case and set up a reference to age 16690 12:37:54,879 --> 12:37:59,915 with the age variable once we do this 16691 12:37:57,360 --> 12:38:02,239 riff edge can be used to modify 16692 12:37:59,915 --> 12:38:05,115 this edge variable here and whatever is 16693 12:38:02,239 --> 12:38:08,159 inside for example we can go through ref 16694 12:38:05,116 --> 12:38:10,400 age to increment the age and if we print 16695 12:38:08,160 --> 12:38:12,956 the edge now we should get a 28 printed 16696 12:38:10,400 --> 12:38:15,680 out because this reference is going to 16697 12:38:12,955 --> 12:38:18,159 change these two things at the same time 16698 12:38:15,680 --> 12:38:20,879 or another way to say this these two 16699 12:38:18,160 --> 12:38:22,720 things age and ref age are referencing 16700 12:38:22,720 --> 12:38:27,040 in your computer so if you change one 16701 12:38:25,116 --> 12:38:28,880 the other is going to pick up the change 16702 12:38:27,040 --> 12:38:31,436 we can also set up accounts reference 16703 12:38:28,879 --> 12:38:34,079 and the syntax is ridiculously easy you 16704 12:38:31,436 --> 12:38:36,955 say constant reference like this and 16705 12:38:34,080 --> 12:38:39,436 your reference is suddenly caused and 16706 12:38:36,955 --> 12:38:40,879 you can't go through it to modify the 16707 12:38:40,879 --> 12:38:44,559 if you need something like this in your 16708 12:38:42,639 --> 12:38:46,479 application this is how you do it you 16709 12:38:44,559 --> 12:38:48,559 can also duplicate the behavior with 16710 12:38:46,480 --> 12:38:51,200 count references with pointers and for 16711 12:38:48,559 --> 12:38:53,680 that we can use a construct like const 16712 12:38:51,199 --> 12:38:56,399 end pointer cost this is going to set up 16713 12:38:53,680 --> 12:38:58,319 a cost pointer to cost which we can't 16714 12:38:56,400 --> 12:38:59,520 use to modify the value that we are 16715 12:38:59,519 --> 12:39:04,239 but it can't also be used to point to 16716 12:39:02,160 --> 12:39:06,000 something else so this is going to 16717 12:39:04,239 --> 12:39:08,159 simulate the behavior we have with 16718 12:39:06,000 --> 12:39:10,400 references and you can do that if you 16719 12:39:08,160 --> 12:39:13,436 want and please know that we have no 16720 12:39:10,400 --> 12:39:15,360 such thing as constant reference cost 16721 12:39:13,436 --> 12:39:17,756 this is going to give you a compiler 16722 12:39:15,360 --> 12:39:20,800 error because we have no such concept in 16723 12:39:17,756 --> 12:39:23,200 c plus plus and my head is hurting right 16724 12:39:20,800 --> 12:39:25,520 now trying to think about what this 16725 12:39:23,199 --> 12:39:27,680 would even mean and another thing i want 16726 12:39:25,519 --> 12:39:30,319 to make clear before we head over to 16727 12:39:27,680 --> 12:39:32,879 vg's video code and play with us is that 16728 12:39:30,319 --> 12:39:34,559 the const key world is going to apply to 16729 12:39:34,559 --> 12:39:39,199 that you apply it to for example here we 16730 12:39:37,275 --> 12:39:42,400 have cost reference age which is going 16731 12:39:39,199 --> 12:39:44,879 to be a const reference but this doesn't 16732 12:39:42,400 --> 12:39:46,239 mean that the edge variable here has to 16733 12:39:46,239 --> 12:39:51,115 okay it can be cost or it may not be 16734 12:39:48,559 --> 12:39:53,839 cost but the cost here applies to this 16735 12:39:51,116 --> 12:39:55,916 variable name constraint age and we 16736 12:39:53,839 --> 12:39:58,079 can't go through this variable name and 16737 12:39:55,915 --> 12:40:00,319 modify the age if you're trying to do 16738 12:39:58,080 --> 12:40:02,400 that we're going to get a compiler error 16739 12:40:00,319 --> 12:40:05,839 now we can head over to visual studio 16740 12:40:02,400 --> 12:40:08,639 code and play with us here we are in our 16741 12:40:05,839 --> 12:40:11,359 working directory the current project is 16742 12:40:08,639 --> 12:40:14,080 reference and cost we're going to grab 16743 12:40:11,360 --> 12:40:14,879 our template files pretty quick let's do 16744 12:40:14,879 --> 12:40:20,000 and we're going to put them in place 16745 12:40:17,756 --> 12:40:22,559 and we are going to open this in visual 16746 12:40:20,000 --> 12:40:24,955 studio code we are going to review the 16747 12:40:22,559 --> 12:40:27,275 references we have seen so far in this 16748 12:40:24,955 --> 12:40:30,319 little example here we have the edge 16749 12:40:27,275 --> 12:40:33,040 variable it has a 27 inside and it is a 16750 12:40:30,319 --> 12:40:35,680 net and we have a reference to it 16751 12:40:33,040 --> 12:40:37,756 and this reference is not const so we 16752 12:40:35,680 --> 12:40:40,400 can go through this reference modify the 16753 12:40:37,756 --> 12:40:42,160 age and if we do that this is going to 16754 12:40:40,400 --> 12:40:45,116 work and if we print the edge here we're 16755 12:40:42,160 --> 12:40:48,480 going to see that it has turned into a 16756 12:40:45,116 --> 12:40:50,720 28 and we can try and print this through 16757 12:40:48,480 --> 12:40:53,116 the terminal so that you can see this 16758 12:40:50,720 --> 12:40:55,116 for yourself so let's run the task to 16759 12:40:59,756 --> 12:41:04,400 and if we do that you're going to see 16760 12:41:01,199 --> 12:41:06,955 edge is 27 now it is 28 and we have 16761 12:41:04,400 --> 12:41:08,319 successfully modified data through our 16762 12:41:08,319 --> 12:41:12,480 but if we change this reference to be 16763 12:41:10,559 --> 12:41:14,639 const we won't be able to do that 16764 12:41:12,480 --> 12:41:15,680 anymore so let's try and make it a const 16765 12:41:15,680 --> 12:41:19,756 if we do that we're going to get a 16766 12:41:17,680 --> 12:41:22,239 compiler error if we try to do something 16767 12:41:19,756 --> 12:41:24,239 like this because this is now a const 16768 12:41:22,239 --> 12:41:26,239 reference we can't go through it to 16769 12:41:24,239 --> 12:41:28,159 modify our data here and we're going to 16770 12:41:26,239 --> 12:41:30,955 get a compiler error here we have visual 16771 12:41:28,160 --> 12:41:34,480 studio called complaining here but if we 16772 12:41:30,955 --> 12:41:36,000 ignore that and go through the compiler 16773 12:41:34,480 --> 12:41:38,560 we're going to see that we get the same 16774 12:41:36,000 --> 12:41:41,040 compiler error we can't implement a read 16775 12:41:38,559 --> 12:41:43,435 only reference which is this ref age 16776 12:41:41,040 --> 12:41:45,436 here so this is what a cost reference 16777 12:41:43,436 --> 12:41:48,319 really is you can't go through it to 16778 12:41:45,436 --> 12:41:49,916 modify the data that is being referenced 16779 12:41:48,319 --> 12:41:52,239 and another thing i want you to really 16780 12:41:49,915 --> 12:41:55,040 understand is that this const keyword 16781 12:41:52,239 --> 12:41:57,275 here has nothing to do with the original 16782 12:41:55,040 --> 12:41:59,360 variable this is a constant reference we 16783 12:41:57,275 --> 12:42:02,720 can't go through it to modify the edge 16784 12:41:59,360 --> 12:42:04,879 variable but this doesn't matter if age 16785 12:42:02,720 --> 12:42:08,319 is either const or not const the 16786 12:42:04,879 --> 12:42:11,435 constants here applies to this variable 16787 12:42:08,319 --> 12:42:13,199 name here so this cost h may be cost if 16788 12:42:11,436 --> 12:42:14,955 it's cost it's going to work equally 16789 12:42:13,199 --> 12:42:17,360 well and we're going to still get a 16790 12:42:14,955 --> 12:42:19,436 compiler error here if it's not cost 16791 12:42:17,360 --> 12:42:22,480 it's going to be the same the cost we 16792 12:42:19,436 --> 12:42:24,400 have on our reference applies to this 16793 12:42:22,480 --> 12:42:26,956 reference variable name here this is 16794 12:42:24,400 --> 12:42:29,840 what i want you to understand okay this 16795 12:42:26,955 --> 12:42:32,479 is what a cost reference really is 16796 12:42:29,839 --> 12:42:34,159 another thing we want you to see 16797 12:42:32,480 --> 12:42:35,276 is that we can simulate the same 16798 12:42:35,275 --> 12:42:40,400 with pointers so let's set up a const 16799 12:42:37,839 --> 12:42:41,755 pointer to const on this edge here and 16800 12:42:40,400 --> 12:42:43,680 we're going to see that we can have the 16801 12:42:41,756 --> 12:42:45,756 same behavior but for that we're going 16802 12:42:43,680 --> 12:42:48,080 to have to comment this out because we 16803 12:42:45,756 --> 12:42:50,880 don't want noise output and we're going 16804 12:42:48,080 --> 12:42:53,360 to go on top here and comment this out 16805 12:42:50,879 --> 12:42:56,639 and say what we are intending to do we 16806 12:42:53,360 --> 12:42:58,639 can say that we are simulating reference 16807 12:42:56,639 --> 12:42:59,915 behavior with pointers and the way we're 16808 12:42:58,639 --> 12:43:02,879 going to do that we're going to say 16809 12:42:59,915 --> 12:43:04,479 const and pointer cost and we're going 16810 12:43:04,480 --> 12:43:09,116 and we're going to initialize this with 16811 12:43:06,480 --> 12:43:12,480 our age variable or the address of our 16812 12:43:09,116 --> 12:43:15,200 age to be super clear okay so we have 16813 12:43:12,480 --> 12:43:17,436 our pointer set up let's try and go 16814 12:43:15,199 --> 12:43:19,755 through it and modify the age let's see 16815 12:43:17,436 --> 12:43:21,756 what happens we the reference because 16816 12:43:19,756 --> 12:43:25,520 this is a pointer and we're going to say 16817 12:43:21,756 --> 12:43:28,559 ph and say plus plus or even change this 16818 12:43:25,519 --> 12:43:29,680 to another age let's say 45 and if we do 16819 12:43:28,559 --> 12:43:33,360 this we're going to get the same 16820 12:43:29,680 --> 12:43:36,879 compiler error so if we build this 16821 12:43:33,360 --> 12:43:38,559 let's clear here clear not clear 16822 12:43:36,879 --> 12:43:40,879 and above again we're going to get 16823 12:43:38,559 --> 12:43:43,360 another compiler error saying that we 16824 12:43:40,879 --> 12:43:46,159 can't modify something that is read only 16825 12:43:43,360 --> 12:43:50,400 assignment of read only location this 16826 12:43:46,160 --> 12:43:53,840 pointer can't be used to modify the data 16827 12:43:50,400 --> 12:43:56,560 in this age location here so this is the 16828 12:43:53,839 --> 12:43:58,955 same behavior with references because 16829 12:43:56,559 --> 12:44:01,756 the reference by default can't be made 16830 12:43:58,955 --> 12:44:04,639 to point somewhere else we had to make 16831 12:44:01,756 --> 12:44:07,040 this pointer also a const pointer 16832 12:44:04,639 --> 12:44:08,955 preventing us from making it point to 16833 12:44:07,040 --> 12:44:12,400 other locations so this is how you can 16834 12:44:08,955 --> 12:44:14,239 simulate references with pointers and 16835 12:44:12,400 --> 12:44:15,680 this is really all we set out to do in 16836 12:44:14,239 --> 12:44:16,879 this lecture i hope you found it 16837 12:44:16,879 --> 12:44:21,839 the main idea was to let you know how 16838 12:44:19,519 --> 12:44:23,359 you can work with cost references and 16839 12:44:21,839 --> 12:44:25,519 once you have your const reference you 16840 12:44:23,360 --> 12:44:27,756 can't go through it to modify the 16841 12:44:25,519 --> 12:44:29,595 original data if you do that you're 16842 12:44:27,756 --> 12:44:31,360 going to get a compiler error 16843 12:44:29,595 --> 12:44:34,879 welcome to this new chapter where we 16844 12:44:31,360 --> 12:44:36,720 will be doing cool stuff with text data 16845 12:44:34,879 --> 12:44:39,275 we have seen that we can model a 16846 12:44:36,720 --> 12:44:41,595 character array and store in a bunch of 16847 12:44:39,275 --> 12:44:43,436 characters and we could for example call 16848 12:44:41,595 --> 12:44:45,756 this character array message and 16849 12:44:43,436 --> 12:44:47,840 manipulate it in memory in this 16850 12:44:45,756 --> 12:44:50,559 structure we will see that this is 16851 12:44:47,839 --> 12:44:52,879 actually the way text data is stored in 16852 12:44:50,559 --> 12:44:55,115 c plus plus and we have a lot of 16853 12:44:52,879 --> 12:44:57,839 facilities built on top of this 16854 12:44:55,116 --> 12:45:01,040 character already construct to make it 16855 12:44:57,839 --> 12:45:03,199 easy to work with text data we will see 16856 12:45:01,040 --> 12:45:05,840 that we can use character arrays to 16857 12:45:03,199 --> 12:45:09,680 model c strings which are a facility we 16858 12:45:05,839 --> 12:45:12,079 use in c plus plus to handle text data 16859 12:45:09,680 --> 12:45:13,915 and we will see that there are a few 16860 12:45:12,080 --> 12:45:16,080 built-in facilities we can use to 16861 12:45:13,915 --> 12:45:18,079 manipulate that we can get the length of 16862 12:45:16,080 --> 12:45:20,480 a string we can concatenate strings and 16863 12:45:18,080 --> 12:45:22,955 put them together we can copy strings we 16864 12:45:20,480 --> 12:45:25,200 can search for data inside strings we 16865 12:45:22,955 --> 12:45:27,680 can do all kinds of crazy things but we 16866 12:45:25,199 --> 12:45:30,319 will see that c strings are not that 16867 12:45:27,680 --> 12:45:32,800 easy or convenient to work with for 16868 12:45:30,319 --> 12:45:35,519 example you need to keep track of how 16869 12:45:32,800 --> 12:45:38,160 big the string is in memory you need to 16870 12:45:35,519 --> 12:45:39,915 also remember to make sure it ends 16871 12:45:38,160 --> 12:45:42,320 correctly and we will see the details 16872 12:45:39,915 --> 12:45:44,799 about that in this chapter long story 16873 12:45:42,319 --> 12:45:47,360 short c strings are not safe or 16874 12:45:44,800 --> 12:45:50,000 convenient to work with in c plus plus 16875 12:45:47,360 --> 12:45:52,639 and c plus plus provide another type 16876 12:45:50,000 --> 12:45:55,519 that really builds on top of character 16877 12:45:52,639 --> 12:45:58,159 arrays but it gives us an easier 16878 12:45:55,519 --> 12:46:00,479 interface to work with and we can just 16879 12:45:58,160 --> 12:46:03,520 focus on storing in the data and doing 16880 12:46:00,480 --> 12:46:05,200 stuff with it in our c plus programs and 16881 12:46:03,519 --> 12:46:08,000 the big idea about what we're going to 16882 12:46:05,199 --> 12:46:10,719 be doing in this chapter is learn about 16883 12:46:08,000 --> 12:46:13,436 the facilities we have in c plus plus to 16884 12:46:10,720 --> 12:46:15,520 handle text data in our c plus plus 16885 12:46:13,436 --> 12:46:17,360 programs in the next lecture we're going 16886 12:46:15,519 --> 12:46:18,399 to start and show you how you can 16887 12:46:18,400 --> 12:46:21,520 individual characters in a character 16888 12:46:21,519 --> 12:46:24,719 go ahead and finish up here and meet me 16889 12:46:24,720 --> 12:46:28,239 in this lecture we're going to be 16890 12:46:25,915 --> 12:46:30,559 learning about some facilities in the c 16891 12:46:30,559 --> 12:46:35,915 that can help us manipulate characters 16892 12:46:33,915 --> 12:46:38,319 and here are a few things you might want 16893 12:46:35,915 --> 12:46:40,879 to do with those facilities for example 16894 12:46:38,319 --> 12:46:43,595 you might want to know if a character is 16895 12:46:40,879 --> 12:46:45,915 alphanumeric or if it's a number or a 16896 12:46:43,595 --> 12:46:48,159 character you might want to know if it 16897 12:46:45,915 --> 12:46:50,559 is alphabetic you might want to know if 16898 12:46:48,160 --> 12:46:52,640 it's a blank character you might want to 16899 12:46:50,559 --> 12:46:54,399 know if it's a lowercase or uppercase 16900 12:46:52,639 --> 12:46:56,800 character depending on what you want to 16901 12:46:54,400 --> 12:46:58,800 do in your application of course there 16902 12:46:56,800 --> 12:47:01,840 are facilities to check if characters 16903 12:46:58,800 --> 12:47:04,160 are digits there are even those that 16904 12:47:01,839 --> 12:47:06,479 help us you know transform characters 16905 12:47:04,160 --> 12:47:07,840 make them lowercase or uppercase and 16906 12:47:07,839 --> 12:47:11,879 these examples here all of these are 16907 12:47:11,879 --> 12:47:17,115 cppreference.com so you can go there and 16908 12:47:14,239 --> 12:47:19,839 find the documentation here is the same 16909 12:47:17,116 --> 12:47:23,360 link opened in my browser you see that 16910 12:47:19,839 --> 12:47:25,275 they leave in the cc type library so you 16911 12:47:23,360 --> 12:47:27,756 need this included if you want to use 16912 12:47:25,275 --> 12:47:30,159 them and you see that there is quite a 16913 12:47:27,756 --> 12:47:32,559 bunch of them we are going to cover just 16914 12:47:30,160 --> 12:47:34,880 a section of them and i will leave you 16915 12:47:32,559 --> 12:47:37,275 to use the documentation if you need to 16916 12:47:34,879 --> 12:47:39,360 use any more of these guys our first 16917 12:47:37,275 --> 12:47:42,400 example here is going to help us check 16918 12:47:39,360 --> 12:47:44,080 if a character is alpha numeric and the 16919 12:47:42,400 --> 12:47:48,160 function we're going to use from the c 16920 12:47:44,080 --> 12:47:48,955 plus plus standard library is std is l 16921 12:47:48,955 --> 12:47:54,400 and you just pass it a parameter of a 16922 12:47:51,839 --> 12:47:57,275 character and it's going to tell you if 16923 12:47:54,400 --> 12:47:59,200 a character is either alphanumeric or if 16924 12:47:57,275 --> 12:48:00,879 it's not if it is alphanumeric you're 16925 12:47:59,199 --> 12:48:02,955 going to get a value that is different 16926 12:48:00,879 --> 12:48:04,955 from zero which can be treated like a 16927 12:48:02,955 --> 12:48:07,275 true if it's not you're going to get a 16928 12:48:04,955 --> 12:48:09,756 value that you can treat like a zero and 16929 12:48:07,275 --> 12:48:12,559 how do i know this well we can check the 16930 12:48:09,756 --> 12:48:13,840 documentation so if i come back to our 16931 12:48:13,839 --> 12:48:19,679 and there is a function here is i'll num 16932 12:48:16,639 --> 12:48:21,519 i can open it in another tab and it is 16933 12:48:19,680 --> 12:48:23,595 going to tell me what this function does 16934 12:48:21,519 --> 12:48:26,275 checks if the given character that you 16935 12:48:23,595 --> 12:48:29,040 pass as a parameter here is an 16936 12:48:26,275 --> 12:48:30,879 alphanumeric character as classified by 16937 12:48:31,756 --> 12:48:34,955 it's going to tell us if the character 16938 12:48:34,955 --> 12:48:40,159 alphanumeric or not you give it a 16939 12:48:37,360 --> 12:48:42,160 character and what you get from it is 16940 12:48:40,160 --> 12:48:44,800 the return value here it's going to 16941 12:48:42,160 --> 12:48:47,916 return a non-zero value if the character 16942 12:48:44,800 --> 12:48:49,756 is alpha numeric so this non-zero value 16943 12:48:47,915 --> 12:48:51,839 which is an integer because that's what 16944 12:48:49,756 --> 12:48:53,840 we get out of this function is going to 16945 12:48:53,839 --> 12:48:58,639 and if this character here is not alpha 16946 12:48:56,160 --> 12:49:00,480 numeric we're going to get zero that we 16947 12:48:58,639 --> 12:49:01,915 can treat like a false this is really 16948 12:49:00,480 --> 12:49:03,840 cool and if you go down here you're 16949 12:49:01,915 --> 12:49:06,000 going to find a few examples i don't 16950 12:49:03,839 --> 12:49:06,879 want you to worry about this c local 16951 12:49:10,955 --> 12:49:14,479 say more about the meaning of the 16952 12:49:12,800 --> 12:49:16,240 characters that you are storing in 16953 12:49:14,480 --> 12:49:18,956 memory but for now we're just going to 16954 12:49:16,239 --> 12:49:21,199 be happy with the defaults and use this 16955 12:49:18,955 --> 12:49:23,595 function as is it is going to work 16956 12:49:21,199 --> 12:49:26,639 enough well okay here is our function 16957 12:49:23,595 --> 12:49:28,720 again so here c is alphanumeric we 16958 12:49:28,720 --> 12:49:33,116 different from zero because c is 16959 12:49:30,720 --> 12:49:35,756 alphanumeric and the second example here 16960 12:49:33,116 --> 12:49:38,400 is not alphanumeric so we expect to get 16961 12:49:35,756 --> 12:49:40,720 a value that is equal to zero and we can 16962 12:49:38,400 --> 12:49:42,880 use the return of this function 16963 12:49:40,720 --> 12:49:44,720 in a test for example we can say a 16964 12:49:42,879 --> 12:49:48,719 character we put in a star here it is 16965 12:49:44,720 --> 12:49:50,800 called input car and we say f is al num 16966 12:49:48,720 --> 12:49:52,800 and we pass this character we're going 16967 12:49:50,800 --> 12:49:54,956 to print out this character is alpha 16968 12:49:52,800 --> 12:49:57,200 numeric if it's not we're going to print 16969 12:49:54,955 --> 12:49:59,680 is not alpha numeric you can do that and 16970 12:49:57,199 --> 12:50:02,399 you can do all kinds of crazy tests in 16971 12:49:59,680 --> 12:50:04,720 your c plus plus applications here is 16972 12:50:02,400 --> 12:50:08,400 another example that we can use to know 16973 12:50:04,720 --> 12:50:11,040 if the character is alphabetic so it's 16974 12:50:08,400 --> 12:50:14,160 going to return a value different from 16975 12:50:11,040 --> 12:50:16,955 zero if the character is a an alphabet 16976 12:50:14,160 --> 12:50:19,520 character a through z that's going to 16977 12:50:16,955 --> 12:50:21,519 return something different from zero and 16978 12:50:19,519 --> 12:50:22,719 if we pass something that is not for 16979 12:50:22,720 --> 12:50:27,595 this second line here or the seven at 16980 12:50:25,436 --> 12:50:29,916 the third line here we're going to get a 16981 12:50:27,595 --> 12:50:32,159 value equal to zero because that's not 16982 12:50:29,915 --> 12:50:34,719 alphabetic we have another facility to 16983 12:50:32,160 --> 12:50:37,040 help check if a character is a black 16984 12:50:34,720 --> 12:50:39,040 character and we see an example that 16985 12:50:37,040 --> 12:50:41,116 here we have a character array called 16986 12:50:39,040 --> 12:50:44,559 message we have a couple of blanks 16987 12:50:41,116 --> 12:50:47,596 inside and we can hunt for them using 16988 12:50:44,559 --> 12:50:49,680 the ease blank facility from the c plus 16989 12:50:47,595 --> 12:50:51,756 plus standard library here we have a 16990 12:50:49,680 --> 12:50:54,639 variable that is going to keep track of 16991 12:50:51,756 --> 12:50:56,239 how many black characters are in here it 16992 12:50:54,639 --> 12:50:58,639 is initialized to zero and we're going 16993 12:50:56,239 --> 12:51:02,400 to loop around okay and we're going to 16994 12:50:58,639 --> 12:51:04,720 look by the amount equal to the size of 16995 12:51:02,400 --> 12:51:07,200 the message array here we can use std 16996 12:51:04,720 --> 12:51:09,916 size again because this is a row array 16997 12:51:07,199 --> 12:51:11,755 it is a static array and we can use sdd 16998 12:51:09,915 --> 12:51:13,915 size it is going to give us how many 16999 12:51:11,756 --> 12:51:16,319 characters we have in this array you 17000 12:51:13,915 --> 12:51:19,040 already know this and if we get inside 17001 12:51:16,319 --> 12:51:20,800 we are going to try and check and see if 17002 12:51:20,800 --> 12:51:26,319 is a blank if it is we're going to print 17003 12:51:23,519 --> 12:51:29,839 that out and we're going to increment 17004 12:51:26,319 --> 12:51:32,239 our blank account by the time this loop 17005 12:51:29,839 --> 12:51:34,879 is done we will have printed out a lot 17006 12:51:32,239 --> 12:51:37,595 of blank characters if we happen to 17007 12:51:34,879 --> 12:51:39,435 have them in our message and at the end 17008 12:51:37,595 --> 12:51:41,680 we're going to say in total we found 17009 12:51:39,436 --> 12:51:43,040 however many blank characters this is 17010 12:51:43,040 --> 12:51:47,116 if we run this program and hopefully you 17011 12:51:47,116 --> 12:51:52,240 this can be in text processing 17012 12:51:49,275 --> 12:51:54,319 applications if that happens to be what 17013 12:51:52,239 --> 12:51:56,479 you are working on we're going to run 17014 12:51:54,319 --> 12:51:58,480 this example in visual studio code in a 17015 12:51:56,480 --> 12:52:00,400 minute here is another example that we 17016 12:51:58,480 --> 12:52:02,319 can use to check if a character is 17017 12:52:02,319 --> 12:52:07,595 lowercase we have an array of characters 17018 12:52:04,955 --> 12:52:10,239 here inside we have some text 17019 12:52:07,595 --> 12:52:12,955 and we have a few variables to keep 17020 12:52:10,239 --> 12:52:15,680 track of how many characters are 17021 12:52:12,955 --> 12:52:17,595 lowercase or uppercase we're going to 17022 12:52:15,680 --> 12:52:20,400 set up a loop which is going to be a 17023 12:52:17,595 --> 12:52:22,955 range based for loop and we know that 17024 12:52:22,955 --> 12:52:27,275 we're going to increment the lowercase 17025 12:52:25,275 --> 12:52:29,040 count if it is uppercase we're going to 17026 12:52:27,275 --> 12:52:30,639 increment the uppercase count this is 17027 12:52:30,639 --> 12:52:35,116 and we are going to print that 17028 12:52:32,879 --> 12:52:37,435 information when the loop here is done 17029 12:52:35,116 --> 12:52:38,955 okay we still have a few examples ahead 17030 12:52:37,436 --> 12:52:40,800 the current one is going to check if a 17031 12:52:38,955 --> 12:52:43,275 character is a digit if you want to do 17032 12:52:40,800 --> 12:52:45,520 that you can use this and we have a 17033 12:52:43,275 --> 12:52:49,519 character array here and we're going to 17034 12:52:45,519 --> 12:52:51,595 have for digits inside this message here 17035 12:52:49,519 --> 12:52:53,519 so we're going to do basically the same 17036 12:52:51,595 --> 12:52:56,399 thing we're going to set up a variable 17037 12:52:53,519 --> 12:52:59,115 that is going to keep track of how many 17038 12:52:56,400 --> 12:53:01,275 digits we have in this message we are 17039 12:52:59,116 --> 12:53:03,596 going to loop around using a range based 17040 12:53:01,275 --> 12:53:05,680 for loop we're going to check if the 17041 12:53:03,595 --> 12:53:06,879 current character is a digit and we're 17042 12:53:08,480 --> 12:53:12,880 our variable here if it happens to be 17043 12:53:10,639 --> 12:53:16,080 the case and at the end we're going to 17044 12:53:12,879 --> 12:53:18,639 say how many digits we found in our 17045 12:53:16,080 --> 12:53:20,639 message here it is this simple the last 17046 12:53:18,639 --> 12:53:23,839 example we're going to look at is to 17047 12:53:20,639 --> 12:53:26,319 help us turn a character into lowercase 17048 12:53:23,839 --> 12:53:28,879 or uppercase and here is an example that 17049 12:53:26,319 --> 12:53:30,480 is going to help us play with us we're 17050 12:53:30,480 --> 12:53:35,276 character array called home the feeling 17051 12:53:33,040 --> 12:53:38,080 of belonging and we will set up a 17052 12:53:35,275 --> 12:53:40,159 destination character array 17053 12:53:38,080 --> 12:53:41,916 that we're going to store whatever we 17054 12:53:41,915 --> 12:53:47,115 here you see a loop that is going to 17055 12:53:43,436 --> 12:53:49,596 turn our message here into uppercase 17056 12:53:47,116 --> 12:53:52,000 and you see we're going to loop around 17057 12:53:49,595 --> 12:53:52,955 by this amount the size of original 17058 12:53:52,955 --> 12:53:57,915 and we're going to be grabbing each 17059 12:53:57,915 --> 12:54:04,079 and turning that to uppercase this is 17060 12:54:00,720 --> 12:54:06,639 what std to upper function does the 17061 12:54:04,080 --> 12:54:09,200 result of that is going to be stored in 17062 12:54:06,639 --> 12:54:11,519 our destination string and by the time 17063 12:54:09,199 --> 12:54:14,399 we are done here original string is 17064 12:54:11,519 --> 12:54:16,955 going to be however it was but uppercase 17065 12:54:14,400 --> 12:54:19,360 string is going to be stored in our 17066 12:54:16,955 --> 12:54:21,839 destination array and if we print it out 17067 12:54:19,360 --> 12:54:24,239 we should see this thing printed in 17068 12:54:21,839 --> 12:54:27,519 uppercase we can basically do the same 17069 12:54:24,239 --> 12:54:29,680 thing and turn the string into lowercase 17070 12:54:27,519 --> 12:54:31,595 and our destination string is going to 17071 12:54:29,680 --> 12:54:34,000 be now lowercase and you're going to see 17072 12:54:31,595 --> 12:54:36,955 that this works out pretty well 17073 12:54:34,000 --> 12:54:39,116 now that you have a basic understanding 17074 12:54:36,955 --> 12:54:41,519 about how these things work we're going 17075 12:54:39,116 --> 12:54:44,480 to head over to vg studio code and play 17076 12:54:41,519 --> 12:54:46,239 with us okay here we are in our working 17077 12:54:44,480 --> 12:54:48,319 directory the current project is 17078 12:54:48,319 --> 12:54:53,040 we are going to grab our template files 17079 12:54:50,800 --> 12:54:55,200 like we always do and we're going to put 17080 12:54:53,040 --> 12:54:57,840 this in place and we're going to open 17081 12:54:55,199 --> 12:54:59,839 this in visual studio code pretty quick 17082 12:54:57,839 --> 12:55:02,159 and we are going to go down and put in 17083 12:54:59,839 --> 12:55:06,239 our first example this example is going 17084 12:55:02,160 --> 12:55:08,160 to be using the std is al num facility 17085 12:55:06,239 --> 12:55:10,000 and this is going to be checking if the 17086 12:55:08,160 --> 12:55:12,240 character is alpha numeric we're going 17087 12:55:10,000 --> 12:55:14,800 to see what we get from this if we print 17088 12:55:12,239 --> 12:55:17,040 this out we're going to check if c the 17089 12:55:14,800 --> 12:55:18,880 character c is alphanumeric we're going 17090 12:55:17,040 --> 12:55:21,275 to see what we get and we're going to 17091 12:55:18,879 --> 12:55:23,755 check if this character here i don't 17092 12:55:21,275 --> 12:55:25,595 know how to pronounce it i'm sorry 17093 12:55:23,756 --> 12:55:27,595 we're going to check if it is 17094 12:55:25,595 --> 12:55:29,680 alpha numeric we're going to see that 17095 12:55:27,595 --> 12:55:31,680 and we're going to use this in at 17096 12:55:29,680 --> 12:55:34,000 condition we're going to check and see 17097 12:55:31,680 --> 12:55:35,756 if our star character here is 17098 12:55:34,000 --> 12:55:37,436 alphanumeric and we're going to print 17099 12:55:35,756 --> 12:55:39,520 that out we're going to say it is alpha 17100 12:55:37,436 --> 12:55:41,756 numeric or it is not from the 17101 12:55:39,519 --> 12:55:44,239 documentation we know that this thing 17102 12:55:44,239 --> 12:55:49,115 library so we may want to include that 17103 12:55:46,720 --> 12:55:51,520 but it is possible that it is coming 17104 12:55:49,116 --> 12:55:52,639 from iostream here that's why we don't 17105 12:55:52,639 --> 12:55:56,879 visual studio code complaining here as 17106 12:55:54,639 --> 12:55:58,479 an example if we try and comment out 17107 12:56:00,559 --> 12:56:06,000 this is not going to make sense anymore 17108 12:56:02,879 --> 12:56:09,360 okay so the include needed to use these 17109 12:56:06,000 --> 12:56:11,839 guys here is coming from io stream okay 17110 12:56:09,360 --> 12:56:15,756 so we're not going to include that here 17111 12:56:11,839 --> 12:56:18,799 we are going to open up a terminal 17112 12:56:15,756 --> 12:56:20,720 and build this with gcc and you're going 17113 12:56:18,800 --> 12:56:23,756 to see that the block is good if we go 17114 12:56:20,720 --> 12:56:26,239 down and clear let's clear properly and 17115 12:56:23,756 --> 12:56:28,639 run rooster we're going to see that c is 17116 12:56:26,239 --> 12:56:30,955 alphanumeric because we have a one the 17117 12:56:28,639 --> 12:56:33,040 one can be treated like a true and the 17118 12:56:30,955 --> 12:56:35,680 character here is not alphanumeric 17119 12:56:35,680 --> 12:56:40,480 and you see that the star here is also 17120 12:56:37,839 --> 12:56:42,799 not alpha numeric and we know this 17121 12:56:40,480 --> 12:56:44,956 through our test here and you see that 17122 12:56:44,955 --> 12:56:50,639 with this facility here if you want to 17123 12:56:47,756 --> 12:56:53,680 get true or false from this function you 17124 12:56:50,639 --> 12:56:55,756 can turn this into booleans using static 17125 12:56:53,680 --> 12:56:58,239 cast but we're not going to do that here 17126 12:56:55,756 --> 12:57:00,319 you already know that we can comment 17127 12:56:58,239 --> 12:57:02,559 this out for now and look at another 17128 12:57:02,559 --> 12:57:07,115 cctype library there are many of these 17129 12:57:05,436 --> 12:57:09,520 we're going to look at quite a few of 17130 12:57:07,116 --> 12:57:12,400 them we're going to comment this out and 17131 12:57:09,519 --> 12:57:14,559 go down and put in a code here and this 17132 12:57:12,400 --> 12:57:17,040 is going to check if a character is 17133 12:57:14,559 --> 12:57:18,079 alphabetic so if it is an alphabet 17134 12:57:18,080 --> 12:57:23,680 a from z lowercase and uppercase 17135 12:57:21,116 --> 12:57:26,400 c is alphabetic the character here is 17136 12:57:23,680 --> 12:57:29,436 not alphabetic and seven here is not 17137 12:57:26,400 --> 12:57:31,680 alphabetic so for the first one here 17138 12:57:29,436 --> 12:57:34,720 we're going to get something 17139 12:57:31,680 --> 12:57:37,840 not equal to zero we should get a one 17140 12:57:34,720 --> 12:57:39,360 that's what they use in most cases for 17141 12:57:37,839 --> 12:57:41,275 this we're going to get a zero the 17142 12:57:39,360 --> 12:57:43,595 second one and for the third one we're 17143 12:57:41,275 --> 12:57:44,639 going to also get a zero this is what we 17144 12:57:44,639 --> 12:57:50,239 we are going to build with gcc of course 17145 12:57:47,756 --> 12:57:51,436 and we are going to clear and run 17146 12:57:51,436 --> 12:57:56,720 and we're going to get that c is 17147 12:57:53,595 --> 12:57:59,595 alphabetic this guy is not alphabetic 17148 12:57:56,720 --> 12:58:01,680 and seven is not alphabetic if we change 17149 12:58:03,839 --> 12:58:07,115 and both again you're going to see that 17150 12:58:05,519 --> 12:58:09,680 the results are going to be what we 17151 12:58:09,680 --> 12:58:14,400 let's clear and run rooster this is 17152 12:58:12,239 --> 12:58:16,799 going to be exactly what we expect but 17153 12:58:14,400 --> 12:58:19,596 you see that it is not a one and this is 17154 12:58:16,800 --> 12:58:21,756 not guaranteed to always give you a one 17155 12:58:19,595 --> 12:58:24,399 it is guaranteed to give you something 17156 12:58:21,756 --> 12:58:27,916 different from zero if the character is 17157 12:58:24,400 --> 12:58:30,160 alphabetic and zero if not and we can 17158 12:58:31,595 --> 12:58:37,839 we can say f sdd is alpha and if we're 17159 12:58:35,595 --> 12:58:40,079 boiled the both is going to be good and 17160 12:58:37,839 --> 12:58:42,879 we're going to clear and run rooster and 17161 12:58:40,080 --> 12:58:45,276 you see e is alphabetic i should put a 17162 12:58:42,879 --> 12:58:47,360 space here i do encourage you to change 17163 12:58:45,275 --> 12:58:49,436 these characters up and see what you get 17164 12:58:47,360 --> 12:58:52,319 here and see how you can use this that's 17165 12:58:49,436 --> 12:58:54,239 in tests that's the best way to learn 17166 12:58:52,319 --> 12:58:56,720 okay before we continue i do encourage 17167 12:58:54,239 --> 12:58:58,955 you to try and run these methods through 17168 12:58:56,720 --> 12:59:01,916 different compilers for example here we 17169 12:58:58,955 --> 12:59:04,239 also have an msvc compiler from 17170 12:59:01,915 --> 12:59:06,799 microsoft setup so we can use that and 17171 12:59:04,239 --> 12:59:09,915 try to build this thing here but before 17172 12:59:06,800 --> 12:59:11,400 we do that let's bring up our pen here 17173 12:59:14,239 --> 12:59:19,756 we don't want to override it we're going 17174 12:59:19,756 --> 12:59:24,480 and i am going to remove this and move 17175 12:59:24,480 --> 12:59:28,560 and i am going to clear whatever i have 17176 12:59:28,559 --> 12:59:32,639 and i am going to go there i am going to 17177 12:59:32,639 --> 12:59:37,040 and i am going to choose world with msvc 17178 12:59:37,040 --> 12:59:41,520 i am going to use that it is going to 17179 12:59:39,199 --> 12:59:44,479 build my thang you see the output here 17180 12:59:44,480 --> 12:59:49,680 and it should say that the world is good 17181 12:59:46,800 --> 12:59:51,520 in a minute world finished successfully 17182 12:59:49,680 --> 12:59:54,400 microsoft is going to give us a lot of 17183 12:59:51,519 --> 12:59:55,199 output here but no problem the build is 17184 13:00:01,199 --> 13:00:05,199 in our folder here you're going to see 17185 13:00:02,639 --> 13:00:07,436 that we have these files one of them is 17186 13:00:07,436 --> 13:00:11,840 if we run it we are going to get the 17187 13:00:10,000 --> 13:00:14,400 outputs that we expect and you see that 17188 13:00:11,839 --> 13:00:16,399 it is exactly the same thing we got 17189 13:00:16,400 --> 13:00:19,756 i am telling you to try and use 17190 13:00:17,839 --> 13:00:23,115 different compilers because some 17191 13:00:19,756 --> 13:00:25,756 compilers are going to flat some of the 17192 13:00:25,756 --> 13:00:31,200 or those that start with c as unsafe 17193 13:00:29,275 --> 13:00:33,199 because there are some tanks that can 17194 13:00:31,199 --> 13:00:34,799 really throw you off about them and 17195 13:00:33,199 --> 13:00:37,039 we're going to learn about them in this 17196 13:00:34,800 --> 13:00:38,800 chapter as we move forward just know 17197 13:00:37,040 --> 13:00:41,275 that it is a good idea to try these 17198 13:00:38,800 --> 13:00:43,200 things on different compilers and see 17199 13:00:41,275 --> 13:00:45,595 the errors you get and make yourself 17200 13:00:45,595 --> 13:00:48,879 for now we're going to move on and we 17201 13:00:48,879 --> 13:00:53,680 gcc because that's my favorite compiler 17202 13:00:51,839 --> 13:00:55,435 you can use whatever compiler you want 17203 13:00:53,680 --> 13:00:58,000 the next example is going to check if 17204 13:00:55,436 --> 13:00:58,880 the character is blank let's close this 17205 13:01:00,720 --> 13:01:05,275 and give ourselves some breathing room 17206 13:01:03,756 --> 13:01:06,800 we're going to bring this down a little 17207 13:01:05,275 --> 13:01:09,115 bit and we're going to check if the 17208 13:01:06,800 --> 13:01:11,596 character is blank the facility for that 17209 13:01:11,595 --> 13:01:15,040 and if it's a blank character we're 17210 13:01:13,360 --> 13:01:17,275 going to get something different than 17211 13:01:15,040 --> 13:01:19,436 zero and we can use that in a test like 17212 13:01:17,275 --> 13:01:21,839 this and if it's not a black we're going 17213 13:01:19,436 --> 13:01:24,000 to get something equal to zero and this 17214 13:01:21,839 --> 13:01:25,359 test here is going to fail so what this 17215 13:01:24,000 --> 13:01:27,436 example here is going to do it's going 17216 13:01:25,360 --> 13:01:28,879 to count how many blank characters we 17217 13:01:28,879 --> 13:01:33,040 message here and we're going to print 17218 13:01:31,199 --> 13:01:34,955 the counter of that on the console using 17219 13:01:33,040 --> 13:01:36,400 this line here so let's go through this 17220 13:01:34,955 --> 13:01:39,436 loop and see what it does the first 17221 13:01:36,400 --> 13:01:41,756 thing we do is initialize blank count to 17222 13:01:39,436 --> 13:01:44,560 0 and we are using an integer and 17223 13:01:41,756 --> 13:01:47,200 because this is a count i think blank 17224 13:01:44,559 --> 13:01:50,319 count is of wrong type because it can be 17225 13:01:47,199 --> 13:01:52,000 negative so to really be safe in our 17226 13:01:50,319 --> 13:01:54,800 program here we should make this 17227 13:01:54,800 --> 13:01:59,040 this is better okay so let's use size 17228 13:01:59,040 --> 13:02:04,955 because this is a size and size t is a 17229 13:02:01,915 --> 13:02:07,519 good type to keep track of sizes in your 17230 13:02:04,955 --> 13:02:09,360 c plus plus programs so make sure you 17231 13:02:11,436 --> 13:02:15,756 is go in the loop at each iteration 17232 13:02:13,519 --> 13:02:17,680 we're going to check if each character 17233 13:02:15,756 --> 13:02:19,756 at each iteration is a blank if it is 17234 13:02:17,680 --> 13:02:22,955 we're going to fall in here and say that 17235 13:02:19,756 --> 13:02:24,880 we found a blank character at this index 17236 13:02:24,879 --> 13:02:28,955 and we're going to increment our blank 17237 13:02:26,800 --> 13:02:30,720 account and by the time this loop is 17238 13:02:28,955 --> 13:02:32,799 done we're going to have the blank 17239 13:02:30,720 --> 13:02:34,639 account stored in this blank account 17240 13:02:32,800 --> 13:02:36,400 variable and we're going to print that 17241 13:02:36,400 --> 13:02:42,400 now we can build with gcc let's do that 17242 13:02:39,839 --> 13:02:43,839 we're going to world with our gcc 17243 13:02:43,839 --> 13:02:47,199 and the world is going to be good can 17244 13:02:50,879 --> 13:02:54,955 it's going to say we found a blank 17245 13:02:59,595 --> 13:03:04,399 and in total we have found nine blank 17246 13:03:01,839 --> 13:03:08,239 characters do we have that many let's 17247 13:03:04,400 --> 13:03:11,040 count so one one blank character 17248 13:03:08,239 --> 13:03:12,000 we have two here we have three we have 17249 13:03:17,199 --> 13:03:21,039 eight and nine if you count you're going 17250 13:03:18,955 --> 13:03:25,040 to find that we have them and the first 17251 13:03:21,040 --> 13:03:27,595 one is at index five so index zero is h 17252 13:03:25,040 --> 13:03:30,400 one two three four the first one is at 17253 13:03:27,595 --> 13:03:33,040 index five and this is doing exactly 17254 13:03:30,400 --> 13:03:36,319 what we want we can go down and check if 17255 13:03:33,040 --> 13:03:39,436 a character is uppercase or lowercase we 17256 13:03:36,319 --> 13:03:41,756 can do that through an example like this 17257 13:03:39,436 --> 13:03:44,880 and the facility is going to be std 17258 13:03:41,756 --> 13:03:46,160 lower and std is upper we have a message 17259 13:03:46,160 --> 13:03:50,000 we're going to loop around using a range 17260 13:03:48,080 --> 13:03:52,720 based for loop and we're going to check 17261 13:03:50,000 --> 13:03:53,915 if each character is either lowercase or 17262 13:03:53,915 --> 13:03:57,595 and if the test is good we're going to 17263 13:03:55,915 --> 13:04:00,079 jump in the loop and we're going to 17264 13:03:57,595 --> 13:04:03,115 increment lowercase count which are a 17265 13:04:00,080 --> 13:04:05,040 few variables to keep track of how many 17266 13:04:03,116 --> 13:04:07,436 uppercase and lowercase characters we 17267 13:04:05,040 --> 13:04:09,436 have in here and again these variables 17268 13:04:07,436 --> 13:04:10,560 are going to store counts so it is 17269 13:04:10,559 --> 13:04:16,079 to use a size t or some unsigned integer 17270 13:04:13,839 --> 13:04:18,479 type but now we're going to use size t 17271 13:04:16,080 --> 13:04:20,319 because it is good so we're going to 17272 13:04:18,480 --> 13:04:22,480 keep this stored in here and we're going 17273 13:04:20,319 --> 13:04:26,000 to build this and see what we get 17274 13:04:22,480 --> 13:04:29,680 before we run we want to clear 17275 13:04:26,000 --> 13:04:31,839 and we are going to build with gcc 17276 13:04:29,680 --> 13:04:34,080 the bolt is going to be good we're going 17277 13:04:34,080 --> 13:04:38,720 and you see that the original strand is 17278 13:04:36,800 --> 13:04:40,400 this guy here what we're going to do 17279 13:04:38,720 --> 13:04:42,639 we're going to print whatever character 17280 13:04:40,400 --> 13:04:45,360 we are at i'm going to put a space in 17281 13:04:42,639 --> 13:04:48,400 front of that and we're going to see 17282 13:04:45,360 --> 13:04:50,639 increment this is the output why do we 17283 13:04:48,400 --> 13:04:52,400 have this method here because we are 17284 13:04:52,400 --> 13:04:57,596 each character and we are not printing 17285 13:04:55,116 --> 13:05:00,400 the upper case ones we are printing 17286 13:04:57,595 --> 13:05:02,399 lowercase ones that's why you see 17287 13:05:00,400 --> 13:05:04,639 this guy here this is really interesting 17288 13:05:02,400 --> 13:05:06,800 you can keep this in if you want i am 17289 13:05:04,639 --> 13:05:09,436 going to keep this on the important 17290 13:05:06,800 --> 13:05:11,756 message is that we found lowercase 17291 13:05:09,436 --> 13:05:14,239 characters and uppercase characters and 17292 13:05:11,756 --> 13:05:16,080 we have five uppercase characters if we 17293 13:05:14,239 --> 13:05:18,239 go in and count we're going to find that 17294 13:05:18,239 --> 13:05:24,319 two and three and four and five five 17295 13:05:22,160 --> 13:05:26,720 uppercase characters and if you count 17296 13:05:24,319 --> 13:05:29,360 the lowercase characters one by one i am 17297 13:05:26,720 --> 13:05:30,800 sure that you're going to get a 47 like 17298 13:05:29,360 --> 13:05:33,360 we did here we're going to comment this 17299 13:05:30,800 --> 13:05:35,596 out because this was just an example 17300 13:05:33,360 --> 13:05:37,756 i am leaving this in here so that you 17301 13:05:35,595 --> 13:05:40,239 can use this as a reference if you want 17302 13:05:37,756 --> 13:05:41,916 we can also check if a character is a 17303 13:05:41,915 --> 13:05:46,719 and we're going to use std is digit to 17304 13:05:44,955 --> 13:05:48,559 check that the return type is going to 17305 13:05:48,559 --> 13:05:52,879 if the test succeeds if it fails we're 17306 13:05:51,116 --> 13:05:55,916 going to get a zero and we can use that 17307 13:05:52,879 --> 13:05:58,799 like this so we have a message here 17308 13:05:55,915 --> 13:06:00,000 stored in the character array called 17309 13:06:00,000 --> 13:06:04,800 and we're going to loop around checking 17310 13:06:02,160 --> 13:06:06,720 if each character we are at is actually 17311 13:06:04,800 --> 13:06:08,319 a digit or not if it's a digit we're 17312 13:06:06,720 --> 13:06:10,160 going to say found a digit i'm going to 17313 13:06:08,319 --> 13:06:12,400 say whatever digit we found and we're 17314 13:06:10,160 --> 13:06:14,320 going to increment the digits count and 17315 13:06:12,400 --> 13:06:17,275 we're going to store that information in 17316 13:06:14,319 --> 13:06:19,199 this digit count variable here we don't 17317 13:06:17,275 --> 13:06:21,436 want to make it an end so we're going to 17318 13:06:21,436 --> 13:06:24,080 and we're going to build and run this 17319 13:06:24,080 --> 13:06:28,800 and see how many digits we found you 17320 13:06:26,955 --> 13:06:30,159 know it is this number here i'm not 17321 13:06:30,160 --> 13:06:34,840 we're going to let our program figure 17322 13:06:32,400 --> 13:06:38,560 this out so rooster and we're going to 17323 13:06:34,839 --> 13:06:40,879 find two two and one so we found three 17324 13:06:38,559 --> 13:06:41,915 digits and it is exactly what we expect 17325 13:06:41,915 --> 13:06:46,319 we're going to comment this out and show 17326 13:06:43,839 --> 13:06:48,000 you another example and in the last 17327 13:06:46,319 --> 13:06:49,756 example we're going to be showing you 17328 13:06:48,000 --> 13:06:52,319 that you can change the case of a 17329 13:06:49,756 --> 13:06:54,400 character if it's lowercase make it 17330 13:06:52,319 --> 13:06:56,480 uppercase if it's uppercase make it 17331 13:06:54,400 --> 13:06:59,520 lowercase and you can do things like 17332 13:06:56,480 --> 13:07:02,720 that we have a message here stored in 17333 13:06:59,519 --> 13:07:04,639 our character array called original str 17334 13:07:02,720 --> 13:07:08,160 we set up another array which is going 17335 13:07:04,639 --> 13:07:10,000 to store our message that is transformed 17336 13:07:08,160 --> 13:07:12,800 and we're going to loop around turning 17337 13:07:10,000 --> 13:07:14,879 everything we come across uppercase 17338 13:07:12,800 --> 13:07:17,116 the way we do that we're going to say 17339 13:07:17,116 --> 13:07:21,200 and we're going to pass in the character 17340 13:07:19,116 --> 13:07:23,520 that we want to turn to uppercase if we 17341 13:07:21,199 --> 13:07:26,000 do that we're going to get an uppercase 17342 13:07:23,519 --> 13:07:28,799 character out of that and we're going to 17343 13:07:28,800 --> 13:07:33,040 array at this index here it is this 17344 13:07:31,199 --> 13:07:35,519 simple after that we're going to print 17345 13:07:33,040 --> 13:07:37,680 the original strand and the uppercase 17346 13:07:35,519 --> 13:07:39,519 string we should get that printed out 17347 13:07:42,160 --> 13:07:46,560 and we're going to do the reverse and 17348 13:07:46,559 --> 13:07:50,559 we're going to grab the original 17349 13:07:47,839 --> 13:07:52,479 strength loop over it and at each 17350 13:07:50,559 --> 13:07:53,435 iteration we're going to get whatever we 17351 13:07:53,436 --> 13:07:57,436 and turn that to uppercase to lowercase 17352 13:07:57,436 --> 13:08:02,880 and the result of that is going to be 17353 13:07:59,756 --> 13:08:04,639 stored back in our destination strand so 17354 13:08:02,879 --> 13:08:07,435 we're going to overwrite whatever we 17355 13:08:04,639 --> 13:08:08,955 have in our destination string here and 17356 13:08:07,436 --> 13:08:11,040 after that we're going to branch the 17357 13:08:08,955 --> 13:08:12,720 lowercase string and we should get that 17358 13:08:11,040 --> 13:08:14,879 printed out this is how the example 17359 13:08:12,720 --> 13:08:17,360 works and if you have any problem 17360 13:08:14,879 --> 13:08:20,159 understanding this i am always around 17361 13:08:17,360 --> 13:08:22,800 please do ask i will do the best i can 17362 13:08:20,160 --> 13:08:24,240 to help you out for now we are going to 17363 13:08:24,239 --> 13:08:28,319 with gcc the builder is going to be good 17364 13:08:26,800 --> 13:08:31,116 we're going to clear and we're going to 17365 13:08:28,319 --> 13:08:33,275 run booster and we're going to see that 17366 13:08:31,116 --> 13:08:35,520 the message is here it is turned 17367 13:08:33,275 --> 13:08:38,079 uppercase it is turned lower case and 17368 13:08:35,519 --> 13:08:41,199 this is how you can do these things here 17369 13:08:38,080 --> 13:08:43,520 so we have looked at a few of this 17370 13:08:41,199 --> 13:08:45,755 facilities from cctime i don't really 17371 13:08:43,519 --> 13:08:48,799 want to look at all of them because that 17372 13:08:45,756 --> 13:08:50,955 would be really boring i hope you have 17373 13:08:48,800 --> 13:08:52,560 an idea about how they work and if you 17374 13:08:50,955 --> 13:08:55,199 want to look at all of them all you have 17375 13:08:52,559 --> 13:08:56,799 to do is come to the documentation here 17376 13:08:56,800 --> 13:09:01,360 open it up and look at the documentation 17377 13:08:59,360 --> 13:09:04,000 and in most cases they are going to have 17378 13:09:01,360 --> 13:09:05,360 examples down here that you can look at 17379 13:09:05,360 --> 13:09:08,639 for now we are going to stop here in 17380 13:09:06,879 --> 13:09:11,275 this lecture this is really all we set 17381 13:09:08,639 --> 13:09:14,080 out to do to play with many of these 17382 13:09:11,275 --> 13:09:15,680 facilities from the cctype library in 17383 13:09:14,080 --> 13:09:18,400 the next lecture we're going to look at 17384 13:09:15,680 --> 13:09:20,720 how we can manipulate c strings and by 17385 13:09:18,400 --> 13:09:23,116 that we mean concatenating them or 17386 13:09:20,720 --> 13:09:25,840 putting them together copying them out 17387 13:09:23,116 --> 13:09:28,160 and doing all kinds of crazy things 17388 13:09:25,839 --> 13:09:29,039 go ahead and finish up here and meet me 17389 13:09:29,040 --> 13:09:33,116 in this lecture we're going to look at 17390 13:09:30,559 --> 13:09:36,079 the facilities from the c plus plus 17391 13:09:33,116 --> 13:09:38,160 standard library to play with c strings 17392 13:09:36,080 --> 13:09:40,560 all of these are going to be leaving in 17393 13:09:38,160 --> 13:09:42,956 the c string library you can go and 17394 13:09:40,559 --> 13:09:44,399 check it out at cpp reference and we're 17395 13:09:42,955 --> 13:09:47,116 going to start and look at a few 17396 13:09:44,400 --> 13:09:49,596 examples of some of the things we can do 17397 13:09:47,116 --> 13:09:51,436 with this library i mean c string the 17398 13:09:49,595 --> 13:09:53,756 first thing we're going to look at is an 17399 13:09:51,436 --> 13:09:56,560 example of how we can check the length 17400 13:09:53,756 --> 13:09:58,720 of a strength here we have a character 17401 13:09:56,559 --> 13:10:01,040 array called message one it is a 17402 13:09:58,720 --> 13:10:04,800 character array and you see that we made 17403 13:10:01,040 --> 13:10:07,360 it const we also have another array that 17404 13:10:04,800 --> 13:10:09,200 is pointed to by pointer you know that 17405 13:10:07,360 --> 13:10:11,520 if we do things like this this array is 17406 13:10:09,199 --> 13:10:13,360 going to decay into a pointer and there 17407 13:10:11,519 --> 13:10:16,479 are a few things we can do with that for 17408 13:10:13,360 --> 13:10:19,275 example we can't use it with std size 17409 13:10:16,480 --> 13:10:21,916 and we can't use a range based for loop 17410 13:10:19,275 --> 13:10:22,720 to loop through this message to thank 17411 13:10:23,519 --> 13:10:28,955 sdr then is going to help us check the 17412 13:10:26,480 --> 13:10:33,436 length of this thing here so we can use 17413 13:10:28,955 --> 13:10:34,799 it for example if we do hdd sdr lam 17414 13:10:33,436 --> 13:10:36,720 message one we're going to get the 17415 13:10:38,000 --> 13:10:42,559 string here this is not going to count 17416 13:10:40,400 --> 13:10:44,880 the null character though so the null 17417 13:10:42,559 --> 13:10:46,879 character that is implicitly added in by 17418 13:10:44,879 --> 13:10:48,079 the compiler is not going to be counted 17419 13:10:46,879 --> 13:10:50,479 in here you're going to see this in a 17420 13:10:48,080 --> 13:10:52,955 minute when we hit visual studio code to 17421 13:10:50,480 --> 13:10:55,840 play with this thanks but this facility 17422 13:10:52,955 --> 13:10:58,319 can work regardless of whether you are 17423 13:10:55,839 --> 13:11:00,799 using a row array like this or even for 17424 13:10:58,319 --> 13:11:02,720 pointers like this and it is really cool 17425 13:11:00,800 --> 13:11:05,116 so we can use it like this we're going 17426 13:11:02,720 --> 13:11:08,319 to get it printed out please note that 17427 13:11:05,116 --> 13:11:10,400 we can also use the size of operator to 17428 13:11:08,319 --> 13:11:13,436 get the same thing so it is going to 17429 13:11:10,400 --> 13:11:15,360 work almost the same way but the size of 17430 13:11:13,436 --> 13:11:17,840 operator is going to include the null 17431 13:11:15,360 --> 13:11:20,639 character you need to be aware of this 17432 13:11:17,839 --> 13:11:23,039 the good thing about str lan here is 17433 13:11:20,639 --> 13:11:25,595 that it still works for a decade arrays 17434 13:11:23,040 --> 13:11:26,480 so if we use it on message 2 we're going 17435 13:11:26,480 --> 13:11:30,480 the correct output we're going to get 17436 13:11:28,400 --> 13:11:31,680 the number of characters in here which 17437 13:11:31,680 --> 13:11:35,680 and we can also try and print out the 17438 13:11:35,680 --> 13:11:39,915 message two but here we're going to get 17439 13:11:37,519 --> 13:11:43,359 the size of a pointer printed out okay 17440 13:11:39,915 --> 13:11:45,755 that's how hdr lan works it lets you 17441 13:11:43,360 --> 13:11:49,360 evaluate the length of a strand here is 17442 13:11:45,756 --> 13:11:51,595 another example and it is str cmp or 17443 13:11:49,360 --> 13:11:53,275 string compare it is used to compare 17444 13:11:51,595 --> 13:11:56,159 strings what you're going to do with 17445 13:11:53,275 --> 13:11:58,479 this facility is give it two strands c 17446 13:11:58,480 --> 13:12:04,240 and one is this guy here l h ace or left 17447 13:12:02,239 --> 13:12:06,639 hand string i think that's what this 17448 13:12:04,239 --> 13:12:08,799 stands for and we're going to give it a 17449 13:12:06,639 --> 13:12:11,275 right hand strand and by the way these 17450 13:12:08,800 --> 13:12:13,436 signatures are copied straight from the 17451 13:12:11,275 --> 13:12:15,839 documentation so that we can look at 17452 13:12:13,436 --> 13:12:17,916 this in one slide to make it really easy 17453 13:12:15,839 --> 13:12:20,879 to follow this is going to return a 17454 13:12:17,915 --> 13:12:22,639 negative value if the first guy happens 17455 13:12:22,639 --> 13:12:28,159 the second guy in lexicographical order 17456 13:12:26,000 --> 13:12:30,720 so it is something like a comes in front 17457 13:12:28,160 --> 13:12:33,200 of b c comes in front of d or something 17458 13:12:30,720 --> 13:12:34,955 like that if the two strands are equal 17459 13:12:34,955 --> 13:12:38,000 zero and it's going to return a positive 17460 13:12:38,000 --> 13:12:41,839 if the first guy appears after the 17461 13:12:41,839 --> 13:12:46,239 okay and we're going to try it out we 17462 13:12:43,680 --> 13:12:48,559 have two strings alabama and la bama 17463 13:12:46,239 --> 13:12:50,879 here and they are stored in character 17464 13:12:48,559 --> 13:12:52,955 arrays that are pointed to by these two 17465 13:12:50,879 --> 13:12:55,199 pointers string data one and string data 17466 13:12:52,955 --> 13:12:56,879 two and we can do something like this 17467 13:12:55,199 --> 13:12:58,079 below here so we're going to print 17468 13:12:58,080 --> 13:13:02,080 and we're going to say std string 17469 13:13:00,239 --> 13:13:04,319 compare and we're going to pass in the 17470 13:13:02,080 --> 13:13:06,319 first string and the second string for 17471 13:13:04,319 --> 13:13:07,756 now i want you to pause a little bit and 17472 13:13:06,319 --> 13:13:09,199 try to guess what we're going to get 17473 13:13:09,199 --> 13:13:14,239 the first string is alabama the second 17474 13:13:11,275 --> 13:13:16,159 one is blah blah so the first one comes 17475 13:13:16,160 --> 13:13:21,116 and we see that we're going to get a 17476 13:13:18,080 --> 13:13:23,756 negative value if the first guy comes in 17477 13:13:21,116 --> 13:13:25,916 front of the second guy so alabama comes 17478 13:13:25,915 --> 13:13:30,159 and we would expect to get something 17479 13:13:27,839 --> 13:13:31,519 negative from this thing we are doing 17480 13:13:31,519 --> 13:13:35,275 and again if we try and change them up a 17481 13:13:33,360 --> 13:13:38,239 little bit you see that alabama still 17482 13:13:38,239 --> 13:13:41,595 so we're going to get a negative value 17483 13:13:39,915 --> 13:13:43,519 pointed out and we can change these 17484 13:13:41,595 --> 13:13:46,000 things up to see what we get it is 17485 13:13:43,519 --> 13:13:48,000 really simple if you know how to use 17486 13:13:46,000 --> 13:13:50,000 them we can also use another version of 17487 13:13:48,000 --> 13:13:52,239 string compare which is going to let us 17488 13:13:50,000 --> 13:13:54,160 specify the number of characters that we 17489 13:13:54,160 --> 13:13:58,956 we can specify that we want to compare n 17490 13:13:57,116 --> 13:14:01,436 characters in this case it is three 17491 13:13:58,955 --> 13:14:04,319 characters and it is going to compare a 17492 13:14:01,436 --> 13:14:07,040 section of the strings that you specify 17493 13:14:04,319 --> 13:14:09,116 as a first and second parameter here so 17494 13:14:07,040 --> 13:14:12,319 for example if we change string that i 17495 13:14:09,116 --> 13:14:16,000 want to be a a i a and string data two 17496 13:14:12,319 --> 13:14:17,680 to b a a a n c a and we are comparing 17497 13:14:16,000 --> 13:14:20,559 three characters in here so we are 17498 13:14:17,680 --> 13:14:22,239 comparing aaa to aaa we're going to see 17499 13:14:20,559 --> 13:14:24,639 that they are equal and we're going to 17500 13:14:22,239 --> 13:14:26,720 get a 0 printed out here you're going to 17501 13:14:24,639 --> 13:14:28,800 see that this is exactly the case when 17502 13:14:26,720 --> 13:14:30,319 we get to play with us in visual studio 17503 13:14:30,319 --> 13:14:34,720 we have a facility that we can use to 17504 13:14:32,800 --> 13:14:37,840 search in a string and search for a 17505 13:14:34,720 --> 13:14:40,955 character the facility for that is sdr 17506 13:14:37,839 --> 13:14:43,519 chr and it is going to find the first 17507 13:14:40,955 --> 13:14:45,595 occurrence of a character in a string 17508 13:14:43,519 --> 13:14:46,479 and the documentation for this is right 17509 13:14:46,480 --> 13:14:50,560 and please note that this example is 17510 13:14:50,559 --> 13:14:55,360 straight from the documentation 17511 13:14:52,879 --> 13:14:57,275 okay so we have a message here 17512 13:14:55,360 --> 13:15:00,160 it is a character array and it is 17513 13:14:57,275 --> 13:15:02,639 pointed to by a const car pointer called 17514 13:15:00,160 --> 13:15:04,240 hdr and we have a target character that 17515 13:15:04,239 --> 13:15:09,915 and the result is going to be stored in 17516 13:15:06,639 --> 13:15:12,159 the result character array here and we 17517 13:15:09,915 --> 13:15:14,079 have a variable called iterations that 17518 13:15:12,160 --> 13:15:16,720 is going to keep track of how many 17519 13:15:14,080 --> 13:15:19,276 iterations we did to find our target 17520 13:15:16,720 --> 13:15:22,720 here the way this guy works it is going 17521 13:15:19,275 --> 13:15:24,879 to return a pointer to what you find 17522 13:15:22,720 --> 13:15:28,080 if it is found for example in here if we 17523 13:15:24,879 --> 13:15:29,680 find this first t this function here or 17524 13:15:28,080 --> 13:15:32,240 this facility is going to return a 17525 13:15:32,239 --> 13:15:36,400 if it doesn't find what we want it is 17526 13:15:34,480 --> 13:15:38,480 going to return a null pointer that's 17527 13:15:36,400 --> 13:15:40,720 how it works and you can see that in the 17528 13:15:38,480 --> 13:15:42,956 documentation here we are going to take 17529 13:15:40,720 --> 13:15:44,955 advantage of how it works in this while 17530 13:15:42,955 --> 13:15:46,879 loop here and the way you use this 17531 13:15:44,955 --> 13:15:49,040 facility you give it the strength you 17532 13:15:46,879 --> 13:15:51,680 want to search in and you give it the 17533 13:15:49,040 --> 13:15:53,680 character that you are looking for so if 17534 13:15:51,680 --> 13:15:55,595 we look at how it works in the while 17535 13:15:53,680 --> 13:15:56,400 loop here we are going to call the 17536 13:15:56,400 --> 13:16:01,596 and we are going to store the result in 17537 13:15:58,720 --> 13:16:03,916 our character pointer here and if we go 17538 13:16:01,595 --> 13:16:06,239 inside we're going to hit the first 17539 13:16:03,915 --> 13:16:08,479 character here at the first iteration so 17540 13:16:06,239 --> 13:16:10,319 we're going to find this t here and when 17541 13:16:08,480 --> 13:16:12,880 we find it the result of this is going 17542 13:16:10,319 --> 13:16:15,116 to be stored back in result here 17543 13:16:12,879 --> 13:16:17,435 i hope this makes sense so we're going 17544 13:16:15,116 --> 13:16:19,436 to find it we're going to print it and 17545 13:16:17,436 --> 13:16:21,596 we're going to increment result to make 17546 13:16:19,436 --> 13:16:24,880 it point to the next character this is 17547 13:16:21,595 --> 13:16:26,559 pointer arithmetic in action here 17548 13:16:24,879 --> 13:16:28,399 and after we do that we're going to 17549 13:16:26,559 --> 13:16:30,720 increment the iterations so we should 17550 13:16:28,400 --> 13:16:33,116 have one iteration by now and we're 17551 13:16:30,720 --> 13:16:34,880 going to jump into the loop again so the 17552 13:16:33,116 --> 13:16:37,276 string we search in is going to be 17553 13:16:37,275 --> 13:16:42,239 so we're going to try and find 17554 13:16:39,436 --> 13:16:45,360 an uppercase tn and it is going to find 17555 13:16:42,239 --> 13:16:48,319 this uppercase t here so it is going to 17556 13:16:45,360 --> 13:16:50,239 point to it and the pointer to this t 17557 13:16:48,319 --> 13:16:52,559 is going to be stored in this result 17558 13:16:50,239 --> 13:16:54,559 here okay and we're going to jump into 17559 13:16:52,559 --> 13:16:57,275 our body here we're going to say that we 17560 13:16:54,559 --> 13:16:59,360 found this t we're going to print it out 17561 13:16:57,275 --> 13:17:01,756 and we're going to increment results if 17562 13:16:59,360 --> 13:17:04,720 we increment reserved now research is 17563 13:17:01,756 --> 13:17:06,480 going to be pointing to the h here 17564 13:17:06,480 --> 13:17:11,276 say how many iterations we did to find 17565 13:17:08,800 --> 13:17:13,276 our thing we will have printed out the t 17566 13:17:11,275 --> 13:17:15,040 that we have found and we're going to 17567 13:17:13,275 --> 13:17:17,199 jump to the next iteration which is 17568 13:17:17,199 --> 13:17:20,639 in the reminder of the string here it's 17569 13:17:19,116 --> 13:17:22,955 not going to find it of course it's 17570 13:17:20,639 --> 13:17:24,879 going to return no pointer and the test 17571 13:17:22,955 --> 13:17:27,040 here is going to fail and we're going to 17572 13:17:24,879 --> 13:17:29,435 fall down here and say how many 17573 13:17:27,040 --> 13:17:31,840 iterations we found i realize this 17574 13:17:29,436 --> 13:17:34,080 example can be really confusing i am 17575 13:17:31,839 --> 13:17:36,719 going to try and explain it again when 17576 13:17:34,080 --> 13:17:39,520 we hit visual studio code for now note 17577 13:17:36,720 --> 13:17:41,360 that this facility can help you find 17578 13:17:39,519 --> 13:17:43,199 characters in a string that's the 17579 13:17:41,360 --> 13:17:45,680 message here there is also another 17580 13:17:43,199 --> 13:17:48,559 function that helps you find the last 17581 13:17:45,680 --> 13:17:50,239 occurrence of a character in a string 17582 13:17:48,559 --> 13:17:51,199 for example here we have a character 17583 13:17:51,199 --> 13:17:56,639 it is a path to a file on some limit 17584 13:17:56,639 --> 13:18:01,756 and we have an output character pointer 17585 13:17:59,839 --> 13:18:04,559 which is going to store the pointer to 17586 13:18:01,756 --> 13:18:06,080 the character that we found okay if we 17587 13:18:04,559 --> 13:18:08,239 find the character we're going to store 17588 13:18:06,080 --> 13:18:10,240 in the pointer to that character if we 17589 13:18:08,239 --> 13:18:12,319 don't find it we're going to return no 17590 13:18:10,239 --> 13:18:13,275 pointer i think that's how this thing 17591 13:18:13,275 --> 13:18:18,639 so what we really want here 17592 13:18:15,519 --> 13:18:20,955 is to hunt for the file name in this 17593 13:18:18,639 --> 13:18:23,436 path here and this is really interesting 17594 13:18:20,955 --> 13:18:26,080 so we're going to search for the 17595 13:18:23,436 --> 13:18:28,560 backslash the last one and this string 17596 13:18:26,080 --> 13:18:30,639 here and it is going to find this guy 17597 13:18:28,559 --> 13:18:32,319 here because that's the last occurrence 17598 13:18:32,319 --> 13:18:37,275 in our input string here once we find it 17599 13:18:34,879 --> 13:18:39,839 we're going to get a pointer to this guy 17600 13:18:37,275 --> 13:18:41,756 stored in the output character pointer 17601 13:18:39,839 --> 13:18:43,115 here and we're going to print it out 17602 13:18:41,756 --> 13:18:45,436 what we're going to do we're going to 17603 13:18:43,116 --> 13:18:47,276 use pointer arithmetic to move this 17604 13:18:45,436 --> 13:18:49,360 pointer to the next guy because that's 17605 13:18:47,275 --> 13:18:51,360 what we really want we want to print 17606 13:18:49,360 --> 13:18:52,800 starting from the edge here and that's 17607 13:18:51,360 --> 13:18:53,915 what we're going to get when we print 17608 13:18:53,915 --> 13:18:58,319 now that we have looked at all these 17609 13:18:56,080 --> 13:19:00,480 examples i think it is time we headed to 17610 13:18:58,319 --> 13:19:02,955 visual studio code and actually played 17611 13:19:00,480 --> 13:19:05,680 with them okay here we are in our 17612 13:19:02,955 --> 13:19:07,915 working folder the current project is c 17613 13:19:05,680 --> 13:19:10,000 string manipulation that's what we're 17614 13:19:07,915 --> 13:19:12,079 going to work on we're going to copy our 17615 13:19:10,000 --> 13:19:14,480 template files and we're going to put 17616 13:19:14,480 --> 13:19:19,116 and we're going to open this in visual 17617 13:19:16,400 --> 13:19:20,720 studio code pretty quick let's include c 17618 13:19:19,116 --> 13:19:22,800 string because that's what we're going 17619 13:19:20,720 --> 13:19:24,639 to be using many of the things we're 17620 13:19:22,800 --> 13:19:26,800 going to be playing with in this lecture 17621 13:19:24,639 --> 13:19:29,040 are documented in this piece of 17622 13:19:26,800 --> 13:19:31,360 documentation here so you can come here 17623 13:19:29,040 --> 13:19:33,595 and get more details than we could even 17624 13:19:31,360 --> 13:19:35,199 cover in a course like this let's bring 17625 13:19:33,595 --> 13:19:37,519 this up a little bit and we're going to 17626 13:19:35,199 --> 13:19:40,319 go in our main function and put in the 17627 13:19:37,519 --> 13:19:43,115 first piece of code we can play with the 17628 13:19:40,319 --> 13:19:45,519 str land facility is going to find the 17629 13:19:43,116 --> 13:19:48,080 length of the strength and it's going to 17630 13:19:45,519 --> 13:19:50,879 work regardless of whether you are using 17631 13:19:48,080 --> 13:19:52,800 a row array like this or even if you are 17632 13:19:50,879 --> 13:19:55,115 using the character pointer to point to 17633 13:19:52,800 --> 13:19:57,436 your array and this is pretty cool so 17634 13:19:55,116 --> 13:19:58,639 here we have strlen message one we're 17635 13:19:57,436 --> 13:20:00,319 going to print that out we're going to 17636 13:19:58,639 --> 13:20:01,680 print how many characters we have in 17637 13:20:01,680 --> 13:20:06,160 we're going to also use the size of 17638 13:20:03,915 --> 13:20:08,559 operator to try and compare these things 17639 13:20:06,160 --> 13:20:10,240 but please know that the null character 17640 13:20:08,559 --> 13:20:13,115 is going to be included if you use 17641 13:20:13,116 --> 13:20:19,360 this is very important to know 17642 13:20:16,319 --> 13:20:21,595 but one interesting thing about hdr lan 17643 13:20:19,360 --> 13:20:23,680 is that it is also going to work if you 17644 13:20:23,680 --> 13:20:28,879 and something that is pointed to by 17645 13:20:25,680 --> 13:20:30,639 pointer so even decayed pointers 17646 13:20:28,879 --> 13:20:33,435 are going to be usable here and this is 17647 13:20:30,639 --> 13:20:35,360 pretty cool so we can also try and print 17648 13:20:33,436 --> 13:20:37,840 the size of message 2 which is pointed 17649 13:20:35,360 --> 13:20:39,520 to by a row pointer and we're going to 17650 13:20:37,839 --> 13:20:41,199 see what we get but we know that we're 17651 13:20:39,519 --> 13:20:43,680 going to get the size of a pointer 17652 13:20:41,199 --> 13:20:46,000 because that's what it really is message 17653 13:20:43,680 --> 13:20:48,160 two here is just a pointer okay let's 17654 13:20:48,160 --> 13:20:51,520 and we are going to bring up our 17655 13:20:51,519 --> 13:20:55,915 and i try and build this with gcc we're 17656 13:20:54,160 --> 13:20:57,596 going to do that so we're going to use 17657 13:20:57,595 --> 13:21:01,595 the world is going to go through 17658 13:20:59,519 --> 13:21:02,559 and we're going to clear and run rooster 17659 13:21:02,559 --> 13:21:07,756 and this is going to give us what we 17660 13:21:07,756 --> 13:21:12,319 size of message 1 is 17 because sizeof 17661 13:21:12,319 --> 13:21:18,400 the null character htrlen message2 you 17662 13:21:21,040 --> 13:21:26,160 that is pointed to by a pointer and this 17663 13:21:23,915 --> 13:21:28,639 is really useful if you happen to need 17664 13:21:26,160 --> 13:21:30,800 the size of an array like this you can 17665 13:21:28,639 --> 13:21:33,915 use hdr then and you're going to get the 17666 13:21:30,800 --> 13:21:36,160 size of data in there but know that the 17667 13:21:33,915 --> 13:21:39,680 null terminating character is not 17668 13:21:36,160 --> 13:21:42,160 counted if you use hdr ln here and size 17669 13:21:39,680 --> 13:21:44,400 of message 2 is going to be 8 because 17670 13:21:42,160 --> 13:21:47,276 that's the size of a pointer on our 17671 13:21:44,400 --> 13:21:48,639 system here so know that this guy is 17672 13:21:51,199 --> 13:21:55,079 we are going to comment this out the 17673 13:21:52,879 --> 13:21:57,755 next thing we're going to look at is 17674 13:21:55,080 --> 13:21:59,040 strcmp which can be used to compare 17675 13:21:59,040 --> 13:22:04,000 so it works exactly like we say it's 17676 13:22:01,839 --> 13:22:06,719 going to return something negative if 17677 13:22:04,000 --> 13:22:08,955 the first string happens to come in 17678 13:22:06,720 --> 13:22:10,480 front of the second string in 17679 13:22:25,360 --> 13:22:29,595 for example here we have a few variables 17680 13:22:26,955 --> 13:22:31,680 we have alabama and blabama there are 17681 13:22:34,639 --> 13:22:40,239 and we have a couple of other strings 17682 13:22:37,436 --> 13:22:42,080 stored in row arrays like this and by 17683 13:22:40,239 --> 13:22:44,479 doing this i want to prove that this 17684 13:22:42,080 --> 13:22:46,880 facility is going to work regardless of 17685 13:22:44,480 --> 13:22:50,160 how your array or your c string is 17686 13:22:46,879 --> 13:22:51,839 stored so we're going to use that here 17687 13:22:51,839 --> 13:22:56,239 c string pointer 2 by pointers 17688 13:22:54,480 --> 13:22:58,319 we're going to try and compare whatever 17689 13:22:58,319 --> 13:23:04,319 with string data two string data one has 17690 13:23:01,199 --> 13:23:06,399 alabama string data two has blah blah so 17691 13:23:04,319 --> 13:23:09,436 if we compare these guys we expect to 17692 13:23:06,400 --> 13:23:10,720 get a negative value out of this thing 17693 13:23:12,480 --> 13:23:19,520 because alabama comes in front of 17694 13:23:16,160 --> 13:23:21,596 blabama in our alphabet so a is in front 17695 13:23:19,519 --> 13:23:23,915 of b so we're going to get a negative 17696 13:23:21,595 --> 13:23:25,680 value out of this and we expect to get 17697 13:23:23,915 --> 13:23:27,839 the same thing in the second example 17698 13:23:25,680 --> 13:23:30,400 here which is going to be using what we 17699 13:23:30,400 --> 13:23:34,560 so make sure you understand this and 17700 13:23:32,639 --> 13:23:38,239 we're going to try and build this to see 17701 13:23:34,559 --> 13:23:39,915 that it passes through our gcc compiler 17702 13:23:42,000 --> 13:23:46,000 and run rooster and you're going to see 17703 13:23:46,000 --> 13:23:50,720 i want you to play with this thanks and 17704 13:23:47,756 --> 13:23:52,639 try and change for example the 8 with c 17705 13:23:52,639 --> 13:23:56,879 okay we're going to try and build but 17706 13:23:54,559 --> 13:23:59,595 before we do try to expect to see what 17707 13:23:56,879 --> 13:24:01,360 you get c comes after blah blah 17708 13:24:01,360 --> 13:24:06,480 if we look at this description here we 17709 13:24:03,915 --> 13:24:08,799 should get a positive value because 17710 13:24:08,800 --> 13:24:13,840 the second guy in lexicographical order 17711 13:24:11,595 --> 13:24:17,040 so we should get a positive value 17712 13:24:13,839 --> 13:24:19,039 for the first thing that we print here 17713 13:24:17,040 --> 13:24:21,520 okay let's weld and see if that's the 17714 13:24:19,040 --> 13:24:24,080 case we're going to build successfully 17715 13:24:21,519 --> 13:24:26,399 we're going to clear and run rooster and 17716 13:24:24,080 --> 13:24:27,520 you see that the first guy prints a one 17717 13:24:27,519 --> 13:24:32,479 cla gamma comes before blah blah 17718 13:24:30,559 --> 13:24:35,275 and the second one is going to 17719 13:24:32,480 --> 13:24:36,080 still be a negative one because we still 17720 13:24:36,080 --> 13:24:40,240 whatever we had before in there okay 17721 13:24:38,080 --> 13:24:42,800 this is how you work with this thanks 17722 13:24:40,239 --> 13:24:45,360 let's turn this back to alabama 17723 13:24:47,116 --> 13:24:51,916 working on this and the next piece of 17724 13:24:50,160 --> 13:24:54,000 code we are going to try and modify 17725 13:24:51,915 --> 13:24:56,955 whatever is stored in string data 1 and 17726 13:24:54,000 --> 13:24:58,000 string data 2 and print the comparisons 17727 13:24:58,000 --> 13:25:02,800 for example we have alabama and album 17728 13:25:00,639 --> 13:25:04,159 and by the way we are able to modify 17729 13:25:04,160 --> 13:25:09,200 because it is a pointer to a strength 17730 13:25:07,360 --> 13:25:11,360 and notice that this is not the 17731 13:25:09,199 --> 13:25:13,199 conspirator so we can really change 17732 13:25:11,360 --> 13:25:15,436 where this guy is pointing and that's 17733 13:25:13,199 --> 13:25:17,915 what we are doing here but we can't do 17734 13:25:15,436 --> 13:25:20,880 that with string data 3 and string data 17735 13:25:17,915 --> 13:25:23,360 4 because these are arrays and remember 17736 13:25:20,879 --> 13:25:25,915 you can't change where an array points 17737 13:25:23,360 --> 13:25:28,239 so this is going to compile nice but if 17738 13:25:25,915 --> 13:25:30,719 we try and do the same thing with string 17739 13:25:30,720 --> 13:25:33,436 let's do that you're going to get a 17740 13:25:33,436 --> 13:25:37,360 so we're going to put this guy here and 17741 13:25:39,116 --> 13:25:43,436 and we should see visual studio code 17742 13:25:41,199 --> 13:25:45,360 complaining here because this is not 17743 13:25:43,436 --> 13:25:46,319 allowed you're not allowed to make an 17744 13:25:46,319 --> 13:25:50,000 point to another location and this is 17745 13:25:50,000 --> 13:25:55,436 character array or cost character array 17746 13:25:52,639 --> 13:25:57,199 in memory that we want this pointers to 17747 13:25:55,436 --> 13:25:59,040 point to or this array to point to and 17748 13:25:57,199 --> 13:26:00,879 this is not possible so we're going to 17749 13:25:59,040 --> 13:26:03,116 get a compiler error if we do this this 17750 13:26:00,879 --> 13:26:05,680 is something i wanted you to know that's 17751 13:26:03,116 --> 13:26:07,756 why i set up this example here so if we 17752 13:26:05,680 --> 13:26:10,559 run this we're going to see the first 17753 13:26:10,559 --> 13:26:16,720 or i think i should comment that out 17754 13:26:14,639 --> 13:26:18,319 or let's not comment it out now if we 17755 13:26:16,720 --> 13:26:21,360 run this program we're going to get the 17756 13:26:18,319 --> 13:26:24,639 output for obama and alabama printed out 17757 13:26:21,360 --> 13:26:27,116 and alabama comes after alabama so we 17758 13:26:24,639 --> 13:26:28,955 expect to get a negative one because the 17759 13:26:27,116 --> 13:26:30,400 first guy here comes in front of the 17760 13:26:28,955 --> 13:26:31,680 second guy so we're going to get a 17761 13:26:34,239 --> 13:26:39,199 thing we are doing here let's try in 17762 13:26:39,199 --> 13:26:43,595 the builder is going to be good so we're 17763 13:26:41,199 --> 13:26:45,275 going to clear and run rooster and we're 17764 13:26:43,595 --> 13:26:49,275 going to see that we have a negative one 17765 13:26:45,275 --> 13:26:51,519 here alabama comes in front of a lab 17766 13:26:49,275 --> 13:26:53,436 so this is cool so we can try and put in 17767 13:26:51,519 --> 13:26:55,039 many other examples for you to play with 17768 13:26:53,436 --> 13:26:56,880 i am going to do that because i don't 17769 13:26:55,040 --> 13:26:58,400 want to type all this thanks and we're 17770 13:26:56,879 --> 13:27:00,719 going to print the results out for 17771 13:26:58,400 --> 13:27:02,800 example we put in india and france we 17772 13:27:00,720 --> 13:27:04,955 put in kigali and kigali and we're going 17773 13:27:02,800 --> 13:27:08,160 to get things printed out here so let's 17774 13:27:04,955 --> 13:27:10,000 build this so that we don't waste time 17775 13:27:08,160 --> 13:27:11,436 we're going to clear and we're going to 17776 13:27:11,436 --> 13:27:15,680 and we're going to see that we get these 17777 13:27:13,275 --> 13:27:17,519 things here alakama comes after alabama 17778 13:27:17,519 --> 13:27:23,199 india comes after france we get a one 17779 13:27:20,720 --> 13:27:24,880 shigari comes together with kigali and 17780 13:27:23,199 --> 13:27:27,275 we get a zero you can try and put in 17781 13:27:24,879 --> 13:27:30,079 whatever you want these are just a few 17782 13:27:27,275 --> 13:27:31,839 examples for you to play with and by now 17783 13:27:30,080 --> 13:27:34,400 i hope you really understand how this 17784 13:27:31,839 --> 13:27:37,039 function works or this facility works so 17785 13:27:34,400 --> 13:27:38,319 we're going to comment it out whatever 17786 13:27:38,319 --> 13:27:42,639 so we're going to comment all this out 17787 13:27:40,400 --> 13:27:44,955 so that we don't have noise output in 17788 13:27:42,639 --> 13:27:47,116 whatever we want to do next and what we 17789 13:27:44,955 --> 13:27:49,116 want to look at next is this function 17790 13:27:47,116 --> 13:27:51,596 here which is going to compare 17791 13:27:49,116 --> 13:27:54,480 strings in the same ways but we're going 17792 13:27:51,595 --> 13:27:56,079 to tell it to compare however many 17793 13:27:54,480 --> 13:27:58,080 characters in the string and you see 17794 13:27:56,080 --> 13:28:01,276 that my string here has been commented 17795 13:27:58,080 --> 13:28:03,520 up i can quickly take it and bring it 17796 13:28:01,275 --> 13:28:04,639 down here let's do that i can't find 17797 13:28:04,639 --> 13:28:10,400 let's find that and copy it down 17798 13:28:08,400 --> 13:28:12,080 and we're going to find them here so i'm 17799 13:28:12,080 --> 13:28:16,319 and bring that down here so that i can 17800 13:28:13,915 --> 13:28:19,115 compare these things so let's come down 17801 13:28:16,319 --> 13:28:20,955 here and print and put in our 17802 13:28:19,116 --> 13:28:23,520 declarations for this strengths and you 17803 13:28:20,955 --> 13:28:25,756 see that visual studio code is happy now 17804 13:28:23,519 --> 13:28:28,955 so what we're going to do is compare a 17805 13:28:25,756 --> 13:28:31,200 set of characters in these strings here 17806 13:28:28,955 --> 13:28:32,879 okay so we're going to compare any 17807 13:28:31,199 --> 13:28:36,479 characters in our strengths so we're 17808 13:28:32,879 --> 13:28:38,319 going to compare allah to blah here and 17809 13:28:36,480 --> 13:28:39,756 allah is going to come in front of black 17810 13:28:38,319 --> 13:28:42,239 so we're going to have a negative one 17811 13:28:39,756 --> 13:28:46,080 printed out here but if we change the 17812 13:28:42,239 --> 13:28:48,639 strengths to aaa and aaa here and 17813 13:28:46,080 --> 13:28:50,560 compare the first three characters we're 17814 13:28:48,639 --> 13:28:53,275 going to get a zero printed out because 17815 13:28:50,559 --> 13:28:55,115 the first three characters are equal and 17816 13:28:53,275 --> 13:28:56,955 again the special thing about this 17817 13:28:55,116 --> 13:28:58,160 function is that it allows you to 17818 13:28:58,160 --> 13:29:03,040 a number of characters you want compared 17819 13:29:00,720 --> 13:29:04,880 in these two strings so you can say i 17820 13:29:03,040 --> 13:29:06,720 want to compare three strands you can 17821 13:29:04,879 --> 13:29:07,755 say i want to compare four strings or 17822 13:29:07,756 --> 13:29:12,000 just make sure that you don't go over 17823 13:29:09,680 --> 13:29:13,595 the size of the string and if you do 17824 13:29:12,000 --> 13:29:15,839 that it's not going to make sense what 17825 13:29:13,595 --> 13:29:17,519 you are trying to do here if we go down 17826 13:29:15,839 --> 13:29:19,519 here we're going to change n to 5 and 17827 13:29:17,519 --> 13:29:21,359 we're going to be comparing all these 17828 13:29:19,519 --> 13:29:22,319 things in here and we're going to get 17829 13:29:22,319 --> 13:29:28,559 the first one comes in front of the 17830 13:29:24,639 --> 13:29:32,000 second one because i comes in front of n 17831 13:29:28,559 --> 13:29:33,680 in our alphabet if we change the data to 17832 13:29:33,680 --> 13:29:38,480 o is going to come after n and we're 17833 13:29:35,915 --> 13:29:40,955 going to get a positive number printed 17834 13:29:38,480 --> 13:29:45,116 out here we're going to try and run this 17835 13:29:40,955 --> 13:29:47,199 so let's run the task to build with jcc 17836 13:29:45,116 --> 13:29:49,840 the bullet is going to be good and we're 17837 13:29:47,199 --> 13:29:51,680 going to clear and run rooster 17838 13:29:49,839 --> 13:29:55,275 and we're going to see that alabama 17839 13:29:51,680 --> 13:29:58,480 comes in front of blabama because we are 17840 13:29:55,275 --> 13:30:01,275 comparing the first three characters 17841 13:29:58,480 --> 13:30:02,720 if we have aaa in here and we compare 17842 13:30:01,275 --> 13:30:04,879 the three characters you're going to see 17843 13:30:02,720 --> 13:30:06,559 that they are equal we get a zero 17844 13:30:06,559 --> 13:30:11,915 and n here we're going to get a negative 17845 13:30:09,040 --> 13:30:14,720 one because i comes in front of n 17846 13:30:11,915 --> 13:30:17,595 if we use o here and n we're going to 17847 13:30:14,720 --> 13:30:19,916 get a one because o comes after n this 17848 13:30:17,595 --> 13:30:22,159 is really simple to understand now that 17849 13:30:19,915 --> 13:30:24,639 we know how to use the first version of 17850 13:30:22,160 --> 13:30:27,436 string compare this version is just 17851 13:30:24,639 --> 13:30:29,360 going to allow us to specify how many 17852 13:30:27,436 --> 13:30:31,756 characters in the string we want 17853 13:30:29,360 --> 13:30:34,720 compared so we're going to comment this 17854 13:30:31,756 --> 13:30:37,840 out and we are going to go down and do 17855 13:30:34,720 --> 13:30:40,239 another example that allows us to search 17856 13:30:40,239 --> 13:30:45,040 and the method we use to do that is std 17857 13:30:46,559 --> 13:30:50,955 so this guy is going to find whatever 17858 13:30:48,480 --> 13:30:52,880 target you want to find and if it finds 17859 13:30:50,955 --> 13:30:55,595 that it's going to return you a pointer 17860 13:30:52,879 --> 13:30:58,079 to dot's target if it doesn't find it 17861 13:30:55,595 --> 13:31:00,559 it's going to return you no pointer so 17862 13:30:58,080 --> 13:31:03,756 we set up an array here and we store 17863 13:31:00,559 --> 13:31:06,479 that in a location that is pointed to by 17864 13:31:03,756 --> 13:31:08,319 the pointer to character here and we say 17865 13:31:06,480 --> 13:31:11,360 whatever target we are looking for in 17866 13:31:08,319 --> 13:31:13,595 this case we want to find this t here 17867 13:31:11,360 --> 13:31:16,080 we are going to set up another character 17868 13:31:13,595 --> 13:31:17,756 array that is going to store our results 17869 13:31:16,080 --> 13:31:19,520 the result is really going to be a 17870 13:31:17,756 --> 13:31:22,239 pointer which is going to be pointing to 17871 13:31:19,519 --> 13:31:24,079 something valid if we find the target 17872 13:31:22,239 --> 13:31:26,159 and it's going to be no equator if we 17873 13:31:24,080 --> 13:31:28,000 didn't find the target and we set up 17874 13:31:26,160 --> 13:31:30,160 another variable which is going to store 17875 13:31:28,000 --> 13:31:32,480 how many iterations we did we should 17876 13:31:30,160 --> 13:31:35,116 really make this an unsigned integer or 17877 13:31:32,480 --> 13:31:37,200 size t so let's make that here and we're 17878 13:31:35,116 --> 13:31:38,800 going to loop here so the first time 17879 13:31:37,199 --> 13:31:40,955 we'll open here we're going to call this 17880 13:31:38,800 --> 13:31:41,916 guy we're going to say i want to find a 17881 13:31:41,915 --> 13:31:45,519 and result here and the result is going 17882 13:31:43,680 --> 13:31:47,915 to be pointing to this line here because 17883 13:31:45,519 --> 13:31:49,839 we did the assignment here so it's going 17884 13:31:47,915 --> 13:31:52,879 to go and try to find the t it's going 17885 13:31:49,839 --> 13:31:55,679 to find the guy here at the first index 17886 13:31:52,879 --> 13:31:57,680 and the test here is going to succeed 17887 13:31:55,680 --> 13:31:59,519 when it succeeds it's not going to be 17888 13:31:57,680 --> 13:32:01,595 equal to no pointer it's going to be 17889 13:32:01,595 --> 13:32:07,756 and it's going to succeed and return the 17890 13:32:04,879 --> 13:32:09,435 pointer to be stored in resort after 17891 13:32:07,756 --> 13:32:11,520 this guide returns the pointer is going 17892 13:32:09,436 --> 13:32:13,040 to be stored in the result here and 17893 13:32:11,519 --> 13:32:15,039 that's going to be a pointer to this 17894 13:32:13,040 --> 13:32:16,879 team and we're going to jump in the body 17895 13:32:15,040 --> 13:32:18,639 here i really want you to understand how 17896 13:32:18,639 --> 13:32:22,239 once we get to the body we're going to 17897 13:32:20,080 --> 13:32:24,880 say uh-huh we found our target that's 17898 13:32:22,239 --> 13:32:26,955 going to be our t print it out and it's 17899 13:32:24,879 --> 13:32:29,115 going to start at this location 17900 13:32:26,955 --> 13:32:32,080 okay and we're going to print starting 17901 13:32:29,116 --> 13:32:34,880 from the t here because that's what our 17902 13:32:32,080 --> 13:32:36,720 result here is pointing to after we do 17903 13:32:34,879 --> 13:32:39,199 that we're going to increment the result 17904 13:32:36,720 --> 13:32:41,360 to make it point to the next character 17905 13:32:39,199 --> 13:32:43,435 if we do that with pointer arithmetic 17906 13:32:41,360 --> 13:32:44,400 now we're going to be pointing to the r 17907 13:32:44,400 --> 13:32:48,480 i hope this makes sense and we're going 17908 13:32:46,720 --> 13:32:51,200 to increment our iterations so we're 17909 13:32:48,480 --> 13:32:52,640 going to say we have done one iteration 17910 13:32:51,199 --> 13:32:55,115 and we're going to try and run this 17911 13:32:52,639 --> 13:32:58,319 again and by the time we get in here 17912 13:32:55,116 --> 13:33:00,955 we're going to have this string here in 17913 13:32:58,319 --> 13:33:03,436 which we are searching for a t 17914 13:33:00,955 --> 13:33:06,319 and it is going to be found starting at 17915 13:33:03,436 --> 13:33:08,480 this second t here there here 17916 13:33:08,480 --> 13:33:14,560 it's going to store the pointer to it to 17917 13:33:11,756 --> 13:33:16,160 resort here or i should say the search 17918 13:33:14,559 --> 13:33:18,720 is going to be successful so we're going 17919 13:33:16,160 --> 13:33:21,436 to find the pointer to this t 17920 13:33:18,720 --> 13:33:23,275 and return it from this function here 17921 13:33:21,436 --> 13:33:24,639 and the pointer is going to be stored in 17922 13:33:24,639 --> 13:33:29,199 pointer here we're going to jump in the 17923 13:33:26,800 --> 13:33:31,040 body and we're going to print we found 17924 13:33:29,199 --> 13:33:33,680 the target starting at this t we're 17925 13:33:31,040 --> 13:33:35,756 going to print that out and stdc out is 17926 13:33:33,680 --> 13:33:37,360 going to print until it meets an old 17927 13:33:35,756 --> 13:33:39,275 character so we're going to print this 17928 13:33:37,360 --> 13:33:42,080 thing through the end and we're going to 17929 13:33:39,275 --> 13:33:43,680 put a slash n which is going to move us 17930 13:33:42,080 --> 13:33:45,916 to the next line after we do that we're 17931 13:33:45,915 --> 13:33:50,399 and uh result now is going to point to 17932 13:33:50,400 --> 13:33:54,480 and we're going to increment iterations 17933 13:33:52,480 --> 13:33:57,040 now we're going to have done two 17934 13:33:54,480 --> 13:33:59,200 iterations by now and we're going to run 17935 13:33:57,040 --> 13:34:01,520 the loop again this time the loop is 17936 13:33:59,199 --> 13:34:03,199 going to fail because starting from here 17937 13:34:03,199 --> 13:34:07,755 and that this function is going to 17938 13:34:04,720 --> 13:34:09,840 return no pointer and this test here is 17939 13:34:07,756 --> 13:34:12,239 going to fail okay we are going to 17940 13:34:09,839 --> 13:34:14,239 return no pointer no pointer is not 17941 13:34:12,239 --> 13:34:16,319 going to be different from no pointer so 17942 13:34:14,239 --> 13:34:18,079 this is going to fail and we're going to 17943 13:34:16,319 --> 13:34:20,000 fall down here and say how many 17944 13:34:18,080 --> 13:34:22,240 iterations we're going to find so in 17945 13:34:20,000 --> 13:34:24,559 total we're going to do two iterations 17946 13:34:22,239 --> 13:34:26,079 and we are going to find two t's and 17947 13:34:24,559 --> 13:34:28,079 we're going to print whatever we find 17948 13:34:26,080 --> 13:34:28,880 here make sure you really understand 17949 13:34:28,879 --> 13:34:34,399 and i put in this example on purpose 17950 13:34:31,519 --> 13:34:36,879 because it is a little complicated but 17951 13:34:34,400 --> 13:34:39,520 it is going to really bring your game up 17952 13:34:36,879 --> 13:34:41,680 in how you use these functions from the 17953 13:34:39,519 --> 13:34:43,915 c plus plus standard library and you 17954 13:34:41,680 --> 13:34:45,436 really need to know this if you have any 17955 13:34:45,436 --> 13:34:49,116 be sure to ask me i will do the best i 17956 13:34:47,519 --> 13:34:51,115 can to help you out so we're going to 17957 13:34:49,116 --> 13:34:53,916 build this and run this we're going to 17958 13:34:51,116 --> 13:34:56,160 build with gcc as always and we are 17959 13:34:53,915 --> 13:34:57,680 going to clear and run rooster and 17960 13:34:56,160 --> 13:34:58,720 you're going to see that we are looking 17961 13:34:58,720 --> 13:35:04,319 we are going to say we found t starting 17962 13:35:01,116 --> 13:35:06,480 at the t here so we're going to say try 17963 13:35:04,319 --> 13:35:08,480 okay we're going to print all the 17964 13:35:06,480 --> 13:35:10,080 message out and the second iteration is 17965 13:35:10,080 --> 13:35:14,880 starting at this location here and we're 17966 13:35:12,559 --> 13:35:16,399 going to do two iterations in total 17967 13:35:14,879 --> 13:35:18,639 we're going to comment this out and 17968 13:35:16,400 --> 13:35:20,800 again if you don't understand this 17969 13:35:18,639 --> 13:35:23,199 please go through it again and if you 17970 13:35:20,800 --> 13:35:25,040 still don't understand be sure to ask me 17971 13:35:23,199 --> 13:35:28,719 i am going to do the best i can to help 17972 13:35:25,040 --> 13:35:30,955 you out and some of you might be asking 17973 13:35:28,720 --> 13:35:34,080 why did we go through the trouble to 17974 13:35:38,559 --> 13:35:43,680 and the explanation i can come up with 17975 13:35:40,879 --> 13:35:45,360 is that you don't really want to modify 17976 13:35:43,680 --> 13:35:47,199 this guy here because somebody is going 17977 13:35:45,360 --> 13:35:50,239 to look at this thing and say 17978 13:35:47,199 --> 13:35:52,319 this is the message i have in sdr here 17979 13:35:50,239 --> 13:35:53,915 but if you try and modify this in the 17980 13:35:52,319 --> 13:35:55,519 loop it's going to be messed up because 17981 13:35:53,915 --> 13:35:57,435 you're going to be incrementing it and 17982 13:35:55,519 --> 13:36:00,079 doing all kinds of crazy things on it so 17983 13:35:57,436 --> 13:36:02,955 you don't really want to modify this so 17984 13:36:00,080 --> 13:36:05,436 a better way to even do this is to mark 17985 13:36:02,955 --> 13:36:08,000 this as a const pointer and we know how 17986 13:36:05,436 --> 13:36:10,560 to do that so we can say cost here and 17987 13:36:08,000 --> 13:36:12,720 if you even try to increment htr 17988 13:36:10,559 --> 13:36:13,680 somewhere you're going to get a compiler 17989 13:36:13,680 --> 13:36:19,199 so let's make sure we see that for 17990 13:36:16,080 --> 13:36:20,560 example we try and do plus plus sdr 17991 13:36:19,199 --> 13:36:22,955 we're going to get a compiler error 17992 13:36:20,559 --> 13:36:25,199 because this is a const pointer by now 17993 13:36:26,400 --> 13:36:31,200 we're going to comment this out and show 17994 13:36:28,080 --> 13:36:32,880 you something that might throw off many 17995 13:36:31,199 --> 13:36:35,755 of you guys so we're going to comment 17996 13:36:32,879 --> 13:36:38,000 this out and i am going to bring that in 17997 13:36:38,000 --> 13:36:41,116 and we are going to do basically the 17998 13:36:43,199 --> 13:36:48,239 and use str in the first place here 17999 13:36:46,720 --> 13:36:50,239 okay this is how we're going to do this 18000 13:36:48,239 --> 13:36:51,680 we're going to have our string we're 18001 13:36:50,239 --> 13:36:53,595 going to have our target we're going to 18002 13:36:51,680 --> 13:36:56,319 have our result here which is equal to 18003 13:36:53,595 --> 13:36:58,159 null pointer and we have our iterations 18004 13:36:56,319 --> 13:37:00,239 which is an integer let's make this a 18005 13:36:58,160 --> 13:37:03,916 size t and what we're going to do in 18006 13:37:00,239 --> 13:37:06,239 here we're going to use htr to search 18007 13:37:03,915 --> 13:37:08,319 okay and we're going to basically do the 18008 13:37:06,239 --> 13:37:10,799 same checks we're going to jump in here 18009 13:37:08,319 --> 13:37:13,756 and say we found the target starting at 18010 13:37:10,800 --> 13:37:16,800 result and we're going to increment hdr 18011 13:37:13,756 --> 13:37:18,800 here because it is not a constrainer now 18012 13:37:16,800 --> 13:37:21,596 try to go through this thing here and 18013 13:37:18,800 --> 13:37:24,956 see how many times you're going to loop 18014 13:37:21,595 --> 13:37:26,955 okay go through it and when you are done 18015 13:37:24,955 --> 13:37:28,879 we're going to try and run this we're 18016 13:37:26,955 --> 13:37:31,839 going to build with gcc the build is 18017 13:37:28,879 --> 13:37:34,719 going to be good we are going to clear 18018 13:37:31,839 --> 13:37:37,199 and run rooster and you see that this 18019 13:37:34,720 --> 13:37:39,595 guy is going to look 25 times 18020 13:37:37,199 --> 13:37:41,435 and this is crazy why is this happening 18021 13:37:41,436 --> 13:37:48,239 what we really are doing here we are not 18022 13:37:44,639 --> 13:37:49,915 assigning the result in this trend that 18023 13:37:49,915 --> 13:37:53,755 okay let's try and go through this a 18024 13:37:51,595 --> 13:37:56,239 couple of times so the first time we're 18025 13:37:53,756 --> 13:37:58,480 going to do our search okay so we're 18026 13:37:58,480 --> 13:38:03,916 and if we find our target we're going to 18027 13:38:01,275 --> 13:38:05,115 return the pointer to the target 18028 13:38:03,915 --> 13:38:07,040 if we don't find it we're going to 18029 13:38:05,116 --> 13:38:09,200 return a null pointer the first time 18030 13:38:07,040 --> 13:38:10,639 we're going to find this t here okay and 18031 13:38:09,199 --> 13:38:13,039 we're going to return a pointer to it 18032 13:38:13,040 --> 13:38:17,360 okay so after we do this we're going to 18033 13:38:17,360 --> 13:38:21,275 and we're going to say we found t 18034 13:38:19,040 --> 13:38:23,116 starting at this location here that's 18035 13:38:21,275 --> 13:38:27,040 going to work and we're going to 18036 13:38:23,116 --> 13:38:29,200 implement str okay notice that we're not 18037 13:38:29,199 --> 13:38:34,479 if we increment sdr it's going to point 18038 13:38:31,915 --> 13:38:35,595 to the next character which is r here 18039 13:38:35,595 --> 13:38:38,319 and we're going to increment our 18040 13:38:36,879 --> 13:38:39,435 iterations and we're going to start in 18041 13:38:39,436 --> 13:38:43,116 we're going to search in here 18042 13:38:41,436 --> 13:38:44,800 and we're going to be searching from 18043 13:38:44,800 --> 13:38:48,480 and we're going to find the t starting 18044 13:38:46,559 --> 13:38:50,639 at this location we are going to find 18045 13:38:48,480 --> 13:38:53,200 this t here and we're going to 18046 13:38:50,639 --> 13:38:55,360 return that and store that in reserved 18047 13:38:53,199 --> 13:38:58,159 but the problem is that what we are 18048 13:38:55,360 --> 13:39:00,559 incrementing here but the problem here 18049 13:38:58,160 --> 13:39:03,680 is that we are incrementing one by one 18050 13:39:00,559 --> 13:39:06,000 we are not jumping to this t here and we 18051 13:39:06,000 --> 13:39:11,040 by assigning the result into the same 18052 13:39:09,199 --> 13:39:13,680 variable that we are searching in okay 18053 13:39:11,040 --> 13:39:16,400 so this is going to loop 25 times and it 18054 13:39:13,680 --> 13:39:18,319 is not really good and some of you might 18055 13:39:16,400 --> 13:39:20,720 try to set up something like this and it 18056 13:39:18,319 --> 13:39:22,000 is really not going to be good try to go 18057 13:39:20,720 --> 13:39:25,520 through this and you're going to see the 18058 13:39:22,000 --> 13:39:27,275 reason why it is looping 25 times okay i 18059 13:39:25,519 --> 13:39:28,955 wanted you to see this don't do 18060 13:39:27,275 --> 13:39:30,479 something like this and we are going to 18061 13:39:28,955 --> 13:39:32,879 comment this out the next thing we're 18062 13:39:30,480 --> 13:39:35,040 going to see is how we can find the last 18063 13:39:32,879 --> 13:39:37,040 occurrence of the character and this is 18064 13:39:35,040 --> 13:39:39,756 a really cool example i like it it is 18065 13:39:37,040 --> 13:39:41,680 going to do something very practical we 18066 13:39:39,756 --> 13:39:43,756 are going to take a path which is going 18067 13:39:41,680 --> 13:39:46,239 to be an array basically stored in this 18068 13:39:43,756 --> 13:39:47,756 input array and we're going to strip off 18069 13:39:46,239 --> 13:39:50,400 all the things we don't need and we're 18070 13:39:47,756 --> 13:39:53,040 going to hunt down for the file name 18071 13:39:50,400 --> 13:39:55,520 from a full path and this can be useful 18072 13:39:53,040 --> 13:39:57,436 in whatever application you are doing so 18073 13:39:55,519 --> 13:40:00,159 we are going to hunt for the last 18074 13:39:57,436 --> 13:40:03,116 occurrence of a backslash character 18075 13:40:03,116 --> 13:40:08,080 chr facility in the ziploc squad 18076 13:40:06,080 --> 13:40:09,916 standard library and you can find the 18077 13:40:08,080 --> 13:40:11,436 documentation on this guy here i'm not 18078 13:40:09,915 --> 13:40:13,755 going to go there you should really 18079 13:40:11,436 --> 13:40:16,160 practice to go there and read what these 18080 13:40:13,756 --> 13:40:18,000 things do that's why i am living in the 18081 13:40:16,160 --> 13:40:20,000 lens to the documentation you should go 18082 13:40:18,000 --> 13:40:22,319 there and read and if you have a problem 18083 13:40:20,000 --> 13:40:24,000 you can ask me i will do the best i can 18084 13:40:24,000 --> 13:40:27,275 so this line here is going to hunt for 18085 13:40:27,275 --> 13:40:31,115 backslash it's going to find this guy 18086 13:40:29,275 --> 13:40:33,680 here and we're going to store a pointer 18087 13:40:31,116 --> 13:40:35,756 to that in our output character pointer 18088 13:40:33,680 --> 13:40:38,400 here after we do that we're going to say 18089 13:40:35,756 --> 13:40:40,559 if we found something useful if this is 18090 13:40:38,400 --> 13:40:42,880 not no pointer we're going to print that 18091 13:40:40,559 --> 13:40:44,879 out but we don't want to print starting 18092 13:40:42,879 --> 13:40:47,040 from the backslash we are going to use 18093 13:40:44,879 --> 13:40:49,199 pointer arithmetic and move to the next 18094 13:40:47,040 --> 13:40:51,360 character which is going to be this h 18095 13:40:49,199 --> 13:40:54,079 here so if we print this out we're going 18096 13:40:51,360 --> 13:40:56,800 to have hello cpp printed out because 18097 13:40:54,080 --> 13:40:57,680 sddc out is going to print until it 18098 13:40:57,680 --> 13:41:03,040 the last now character and that's going 18099 13:41:00,319 --> 13:41:05,680 to be after the last p here because this 18100 13:41:03,040 --> 13:41:07,275 is a c string let's roll here okay we're 18101 13:41:05,680 --> 13:41:09,756 going to build this the build is going 18102 13:41:07,275 --> 13:41:11,436 to be good we're going to clear 18103 13:41:09,756 --> 13:41:13,756 and run rooster and you're going to see 18104 13:41:13,756 --> 13:41:17,595 printed out here okay this is really all 18105 13:41:15,915 --> 13:41:19,595 we set out to do in this lecture i 18106 13:41:17,595 --> 13:41:22,000 apologize that it turned out to be 18107 13:41:19,595 --> 13:41:24,159 lengthy these things can take long to 18108 13:41:22,000 --> 13:41:27,519 explain and i really wanted you guys to 18109 13:41:24,160 --> 13:41:28,720 have as many explanations as i could 18110 13:41:28,720 --> 13:41:31,916 we are going to stop here in this 18111 13:41:30,239 --> 13:41:34,879 lecture the next one we're going to see 18112 13:41:31,915 --> 13:41:38,079 how we can copy and concatenate 18113 13:41:34,879 --> 13:41:41,040 c strings using the facilities from c 18114 13:41:38,080 --> 13:41:42,560 string here go ahead and finish up here 18115 13:41:42,559 --> 13:41:47,435 in this lecture we're going to look at 18116 13:41:44,720 --> 13:41:50,239 a few more facilities from the c string 18117 13:41:47,436 --> 13:41:53,116 library and those are going to allow us 18118 13:41:50,239 --> 13:41:54,955 to concatenate and copy strings over the 18119 13:41:53,116 --> 13:41:57,116 documentation for what we're going to be 18120 13:41:54,955 --> 13:41:58,879 talking about can be found here if you 18121 13:41:57,116 --> 13:42:00,639 go there you're going to fall on this 18122 13:41:58,879 --> 13:42:03,435 link here and you're going to 18123 13:42:00,639 --> 13:42:07,040 find that c string was originally in the 18124 13:42:03,436 --> 13:42:10,160 c standard library as string dot h but 18125 13:42:07,040 --> 13:42:11,756 in c plus plus we can use it like this 18126 13:42:10,160 --> 13:42:14,720 we have a couple of functions we can 18127 13:42:11,756 --> 13:42:17,040 copy stuff we can concatenate we can get 18128 13:42:14,720 --> 13:42:18,955 the string length we have seen this but 18129 13:42:17,040 --> 13:42:21,756 now we're going to look at things that 18130 13:42:18,955 --> 13:42:22,639 allow us to copy and concatenate stuff 18131 13:42:22,639 --> 13:42:26,239 that's something you may want to do a 18132 13:42:24,720 --> 13:42:27,559 lot the first example we're going to 18133 13:42:27,559 --> 13:42:32,399 stdstr cat which is going to allow you 18134 13:42:34,160 --> 13:42:38,480 documentation here and see more about 18135 13:42:36,080 --> 13:42:40,639 this facility here and you're going to 18136 13:42:38,480 --> 13:42:42,000 see all about it for now we're just 18137 13:42:40,639 --> 13:42:44,559 going to play with it we're going to set 18138 13:42:42,000 --> 13:42:45,519 up two arrays one is going to be called 18139 13:42:45,519 --> 13:42:49,275 it's going to contain hello the other is 18140 13:42:47,839 --> 13:42:51,595 going to be source here and it's going 18141 13:42:49,275 --> 13:42:54,479 to contain worlds notice that these are 18142 13:42:51,595 --> 13:42:55,435 row static arrays that are stored on the 18143 13:42:55,436 --> 13:42:59,916 and uh that's how we set them up here 18144 13:42:57,756 --> 13:43:02,160 okay so we can concatenate strengths 18145 13:42:59,915 --> 13:43:04,955 using the syntax like this we're going 18146 13:43:02,160 --> 13:43:07,116 to say hdd hdr cache we're going to say 18147 13:43:04,955 --> 13:43:09,275 the destination string and we're going 18148 13:43:07,116 --> 13:43:11,360 to say the source string we want to copy 18149 13:43:09,275 --> 13:43:13,680 from okay so after we do this the 18150 13:43:11,360 --> 13:43:16,955 destination is going to contain 18151 13:43:13,680 --> 13:43:19,595 these two strings joined together and 18152 13:43:16,955 --> 13:43:22,000 before you do this you have to be sure 18153 13:43:19,595 --> 13:43:24,559 that your destination strength is big 18154 13:43:22,000 --> 13:43:26,480 enough and this is a problem because you 18155 13:43:24,559 --> 13:43:29,115 have to really keep track of your 18156 13:43:26,480 --> 13:43:31,680 destination strength and it is easy to 18157 13:43:29,116 --> 13:43:34,160 put in many more characters that can 18158 13:43:31,680 --> 13:43:36,000 really fit in the destinations rank 18159 13:43:34,160 --> 13:43:38,480 that's the reason many of these 18160 13:43:36,000 --> 13:43:40,559 functions here are considered unsafe by 18161 13:43:38,480 --> 13:43:42,560 many compilers i don't think you can 18162 13:43:40,559 --> 13:43:45,040 compile this on visual studio it's going 18163 13:43:42,559 --> 13:43:46,159 to stop you from doing this because of 18164 13:43:47,116 --> 13:43:51,276 you have to keep track of things 18165 13:43:48,879 --> 13:43:53,680 yourself but i am just showing you so 18166 13:43:51,275 --> 13:43:55,436 that you know how to avoid these 18167 13:43:53,680 --> 13:43:57,595 problems if you happen to be on a 18168 13:43:55,436 --> 13:43:59,596 compiler that doesn't protect you from 18169 13:43:57,595 --> 13:44:02,479 this so we're going to append goodbye 18170 13:43:59,595 --> 13:44:04,239 world to whatever is in dust and down 18171 13:44:02,480 --> 13:44:06,720 here if we print this we're going to 18172 13:44:04,239 --> 13:44:08,479 have hello world goodbye world printed 18173 13:44:06,720 --> 13:44:10,239 out we are going to play with this in a 18174 13:44:08,480 --> 13:44:13,116 visual studio code in a minute don't 18175 13:44:10,239 --> 13:44:15,115 worry if any of this doesn't make sense 18176 13:44:13,116 --> 13:44:18,639 yet here is another example of how we 18177 13:44:15,116 --> 13:44:20,955 can join strengths through strcat 18178 13:44:18,639 --> 13:44:22,955 we have two strengths and we set them up 18179 13:44:20,955 --> 13:44:25,756 with a simple characters like this you 18180 13:44:22,955 --> 13:44:28,000 can do this and put in your own now 18181 13:44:25,756 --> 13:44:30,319 terminating character this is a legal c 18182 13:44:28,000 --> 13:44:32,559 plus plus could and notice that this 18183 13:44:30,319 --> 13:44:34,559 array is a dynamic array it's going to 18184 13:44:32,559 --> 13:44:37,199 leave on the hip because of this new 18185 13:44:34,559 --> 13:44:39,115 operator here we also have a source 18186 13:44:37,199 --> 13:44:42,000 array which is also going to leave on 18187 13:44:39,116 --> 13:44:44,400 the hip it is 30 characters long so it 18188 13:44:42,000 --> 13:44:46,639 is really long and we're going to try 18189 13:44:44,400 --> 13:44:48,080 and concatenate stance here but before 18190 13:44:46,639 --> 13:44:50,080 we do that we're going to check their 18191 13:44:48,080 --> 13:44:52,240 length we're going to print them out and 18192 13:44:50,080 --> 13:44:55,040 we are going to concatenate the syntax 18193 13:44:52,239 --> 13:44:57,436 is the same you are going to say hdr cat 18194 13:44:55,040 --> 13:44:58,800 you're going to specify your destination 18195 13:44:57,436 --> 13:45:01,436 and your source you're going to be 18196 13:44:58,800 --> 13:45:03,840 copying stuff from and you are going to 18197 13:45:01,436 --> 13:45:05,360 store the result in destination here 18198 13:45:03,839 --> 13:45:08,159 what this is going to do it's going to 18199 13:45:05,360 --> 13:45:09,840 grab whatever is in source and copy it 18200 13:45:08,160 --> 13:45:11,916 so it's going to copy the entire thing 18201 13:45:09,839 --> 13:45:13,679 here from source and it's going to 18202 13:45:11,915 --> 13:45:17,275 append that to whatever we have in 18203 13:45:13,680 --> 13:45:19,116 destination starting from the last null 18204 13:45:17,275 --> 13:45:21,519 character here so it's going to 18205 13:45:19,116 --> 13:45:23,916 overwrite from this null character and 18206 13:45:21,519 --> 13:45:26,399 it's going to basically copy everything 18207 13:45:23,915 --> 13:45:28,079 and the destination here and if we print 18208 13:45:26,400 --> 13:45:30,239 whatever we have in destination here 18209 13:45:30,239 --> 13:45:33,519 the phoenix kind of printed out and 18210 13:45:32,160 --> 13:45:36,080 you're going to see that this is going 18211 13:45:33,519 --> 13:45:37,839 to be the case when we hit visual studio 18212 13:45:36,080 --> 13:45:40,720 code in a minute you can also use a 18213 13:45:37,839 --> 13:45:42,799 version of the sdr cat version that is 18214 13:45:40,720 --> 13:45:45,756 going to allow you to specify 18215 13:45:42,800 --> 13:45:48,240 how many characters you want to 18216 13:45:45,756 --> 13:45:50,000 concatenate from the source string in 18217 13:45:48,239 --> 13:45:51,839 this case we have destination which is 18218 13:45:50,000 --> 13:45:53,595 hello and we have source which is there 18219 13:45:55,040 --> 13:46:00,400 and down here you see that we do hdr and 18220 13:45:58,319 --> 13:46:03,040 cat we save the destination we save the 18221 13:46:00,400 --> 13:46:06,319 source and we say how many characters we 18222 13:46:03,040 --> 13:46:08,480 want copied from the source here and we 18223 13:46:06,319 --> 13:46:10,080 say six characters so we are going to 18224 13:46:08,480 --> 13:46:12,560 count from the source we're going to say 18225 13:46:10,080 --> 13:46:14,880 one two three four five six we're going 18226 13:46:12,559 --> 13:46:17,115 to grab this their string with this 18227 13:46:14,879 --> 13:46:18,955 space here and we're going to append 18228 13:46:17,116 --> 13:46:21,520 that to the source and that's what we're 18229 13:46:18,955 --> 13:46:24,080 going to get in here we're going to say 18230 13:46:21,519 --> 13:46:26,239 hello space and there and that's what 18231 13:46:24,080 --> 13:46:29,276 we're going to have printed out here if 18232 13:46:26,239 --> 13:46:32,239 we print dust we can also copy stuff and 18233 13:46:29,275 --> 13:46:34,159 the str cpy function here is going to do 18234 13:46:32,239 --> 13:46:37,680 that and here we have an example that is 18235 13:46:34,160 --> 13:46:39,520 going to try and drive this home for you 18236 13:46:37,680 --> 13:46:41,756 we have a source strength which is c 18237 13:46:39,519 --> 13:46:44,000 plus plus is a multi-purpose programming 18238 13:46:41,756 --> 13:46:46,080 language okay guest three here is going 18239 13:46:44,000 --> 13:46:48,720 to be the same length as the source we 18240 13:46:46,080 --> 13:46:50,880 made sure of that in here because we're 18241 13:46:48,720 --> 13:46:53,436 going to grab the length of the source 18242 13:46:50,879 --> 13:46:54,879 strength please remember that sdrlam is 18243 13:46:54,879 --> 13:46:59,680 the null character that's why we are 18244 13:46:57,436 --> 13:47:01,916 adding a one here to account for the 18245 13:46:59,680 --> 13:47:03,680 noun terminating character here okay 18246 13:47:01,915 --> 13:47:07,915 after that we're going to jump down here 18247 13:47:03,680 --> 13:47:10,319 and say htr cpy dest 3 and source 3 we 18248 13:47:07,915 --> 13:47:12,399 are basically going to be copying from 18249 13:47:12,400 --> 13:47:16,480 in destination here that's what we are 18250 13:47:14,160 --> 13:47:17,916 doing if we print destination we're 18251 13:47:16,480 --> 13:47:20,800 going to see that now it's going to 18252 13:47:17,915 --> 13:47:22,719 contain whatever we had in source and 18253 13:47:20,800 --> 13:47:25,040 this is how you can use these methods 18254 13:47:25,040 --> 13:47:29,116 with which we can specify how many 18255 13:47:27,275 --> 13:47:31,519 characters we want copied from the 18256 13:47:29,116 --> 13:47:33,756 source here we have a source string 18257 13:47:31,519 --> 13:47:36,479 which is hello we have a destination 18258 13:47:33,756 --> 13:47:38,319 string which is zest 4 and we are going 18259 13:47:36,480 --> 13:47:40,480 to copy stuff we're going to copy from 18260 13:47:38,319 --> 13:47:42,559 the source we're going to paste in the 18261 13:47:40,480 --> 13:47:45,520 destination and we are just going to 18262 13:47:42,559 --> 13:47:48,079 copy five characters what this is going 18263 13:47:45,519 --> 13:47:51,275 to do is copy everything from the source 18264 13:47:48,080 --> 13:47:54,160 and paste in our destination and it is 18265 13:47:51,275 --> 13:47:57,040 going to override whatever we have in 18266 13:47:54,160 --> 13:47:59,840 the starting indexes in destination here 18267 13:47:57,040 --> 13:48:02,955 so we're going to have a b c d e here 18268 13:47:59,839 --> 13:48:05,679 replaced by hello and if we print this 18269 13:48:02,955 --> 13:48:08,080 here we're going to have hello f printed 18270 13:48:05,680 --> 13:48:10,319 out this is how these things work and if 18271 13:48:08,080 --> 13:48:12,639 you wonder how i know how these things 18272 13:48:10,319 --> 13:48:15,360 work i just come and read the 18273 13:48:12,639 --> 13:48:17,680 documentation here for example htr and 18274 13:48:15,360 --> 13:48:19,520 cpy here if we click on it we are going 18275 13:48:17,680 --> 13:48:22,239 to get the documentation this is going 18276 13:48:19,519 --> 13:48:23,915 to say copies at most count characters 18277 13:48:23,915 --> 13:48:28,799 source including the null terminating 18278 13:48:26,480 --> 13:48:30,880 character to the character array pointed 18279 13:48:28,800 --> 13:48:33,040 to by desk that's what it does we have 18280 13:48:30,879 --> 13:48:34,719 the destination we have the source and 18281 13:48:33,040 --> 13:48:36,720 we have how many characters we want 18282 13:48:34,720 --> 13:48:38,800 copied and they say a few things you 18283 13:48:36,720 --> 13:48:41,595 need to be careful about this guy here 18284 13:48:38,800 --> 13:48:43,436 one thing that you usually care about is 18285 13:48:41,595 --> 13:48:45,595 the return value you see that the return 18286 13:48:43,436 --> 13:48:47,756 value is going to be the destination 18287 13:48:45,595 --> 13:48:50,079 strength so if you want this pointer you 18288 13:48:47,756 --> 13:48:52,800 can use it and you can go down and look 18289 13:48:50,080 --> 13:48:55,276 at a few examples here and play with 18290 13:48:52,800 --> 13:48:57,276 them to really drive the point home okay 18291 13:48:55,275 --> 13:48:59,436 now that you have an idea about how 18292 13:48:57,275 --> 13:49:02,000 these guys work we are going to head 18293 13:48:59,436 --> 13:49:02,880 over to visual studio code and play with 18294 13:49:02,879 --> 13:49:08,319 right away okay here we are in our 18295 13:49:05,275 --> 13:49:10,559 working folder the current project is c 18296 13:49:08,319 --> 13:49:12,400 string concatenation and copy that's 18297 13:49:10,559 --> 13:49:15,360 what we are doing here we're going to 18298 13:49:12,400 --> 13:49:18,160 copy our template files and uh jump to 18299 13:49:15,360 --> 13:49:20,800 our current project and put them in 18300 13:49:18,160 --> 13:49:22,880 place we're going to open this in visual 18301 13:49:20,800 --> 13:49:24,639 studio code pretty quick we are going to 18302 13:49:22,879 --> 13:49:26,399 include c string because that's what 18303 13:49:24,639 --> 13:49:28,400 we're going to be using here we are 18304 13:49:26,400 --> 13:49:30,560 going to bring this up a little bit and 18305 13:49:28,400 --> 13:49:32,800 we are going to put in our first example 18306 13:49:32,800 --> 13:49:37,520 destination and source and you see that 18307 13:49:35,199 --> 13:49:39,915 we tried and made sure that they are 18308 13:49:37,519 --> 13:49:42,079 large enough to contain however many 18309 13:49:39,915 --> 13:49:44,639 characters we want to put in 18310 13:49:42,080 --> 13:49:47,756 and they are row arrays stored on the 18311 13:49:44,639 --> 13:49:49,199 stack because they are set up like this 18312 13:49:47,756 --> 13:49:51,200 and again you can check out the 18313 13:49:49,199 --> 13:49:53,360 documentation here if you want to know 18314 13:49:51,199 --> 13:49:57,755 how this function works and we are going 18315 13:49:53,360 --> 13:50:00,559 to call strcat here to concatenate the 18316 13:49:57,756 --> 13:50:02,756 source to our destination here so if we 18317 13:50:00,559 --> 13:50:05,756 do this we're going to have world here 18318 13:50:02,756 --> 13:50:07,916 concatenated to what we have in hello 18319 13:50:05,756 --> 13:50:10,480 here and we're going to have hello world 18320 13:50:07,915 --> 13:50:13,755 in place for example if we try and print 18321 13:50:10,480 --> 13:50:16,160 this out i'm going to try and do that 18322 13:50:13,756 --> 13:50:18,319 after we do the first concatenation here 18323 13:50:16,160 --> 13:50:20,880 let's do that that's going to be better 18324 13:50:18,319 --> 13:50:23,116 to show you how these things work okay 18325 13:50:20,879 --> 13:50:26,399 after the first function here runs we're 18326 13:50:23,116 --> 13:50:28,639 going to have source concatenated to 18327 13:50:26,400 --> 13:50:30,880 destination here so what we're going to 18328 13:50:28,639 --> 13:50:32,559 have is hello world stored in 18329 13:50:32,559 --> 13:50:37,360 concatenation here is going to append 18330 13:50:34,720 --> 13:50:39,436 goodbye world to whatever we have in 18331 13:50:37,360 --> 13:50:42,559 destination here so what we're going to 18332 13:50:39,436 --> 13:50:44,955 have printed down is if we print that of 18333 13:50:42,559 --> 13:50:46,399 course is hello world and goodbye world 18334 13:50:44,955 --> 13:50:49,040 okay we're going to have this written 18335 13:50:46,400 --> 13:50:51,520 down let's try and run this and actually 18336 13:50:49,040 --> 13:50:53,520 make sure that that's the case and then 18337 13:50:51,519 --> 13:50:56,159 we're going to see how this function can 18338 13:50:53,519 --> 13:50:58,239 be unsafe for some compilers we're going 18339 13:50:56,160 --> 13:51:00,240 to build this with gcc you see that it's 18340 13:51:00,239 --> 13:51:04,319 and if we run this we're going to have 18341 13:51:01,839 --> 13:51:06,879 hello world hello world goodbye world 18342 13:51:04,319 --> 13:51:07,756 printed out this is what we expect and 18343 13:51:07,756 --> 13:51:12,480 this function here sdr cat is going to 18344 13:51:10,239 --> 13:51:14,639 have whatever you have in source and it 18345 13:51:12,480 --> 13:51:17,200 is going to append that to whatever you 18346 13:51:14,639 --> 13:51:19,360 have in destination and for this to work 18347 13:51:17,199 --> 13:51:22,639 you have to be sure that the destination 18348 13:51:19,360 --> 13:51:25,275 is modifiable for example if it was an 18349 13:51:22,639 --> 13:51:27,040 array of cost core let's do that and 18350 13:51:25,275 --> 13:51:29,680 show you the compiler error we should 18351 13:51:27,040 --> 13:51:32,319 get a compiler error because now 18352 13:51:29,680 --> 13:51:34,955 destination is not modifiable it is an 18353 13:51:32,319 --> 13:51:37,595 array of cost characters and we can't 18354 13:51:37,595 --> 13:51:42,720 the destination array is modifiable and 18355 13:51:40,559 --> 13:51:44,479 in this case it is okay please note that 18356 13:51:42,720 --> 13:51:47,680 some of these functions can be treated 18357 13:51:44,480 --> 13:51:50,720 as unsafe by some compilers but i tried 18358 13:51:47,680 --> 13:51:52,955 this out on both on gcc and the compiler 18359 13:51:50,720 --> 13:51:54,955 from microsoft and they work pretty well 18360 13:51:52,955 --> 13:51:57,360 so you can try them out here to prove 18361 13:51:54,955 --> 13:51:59,915 that let's go and try and build with the 18362 13:51:57,360 --> 13:52:01,275 compiler from microsoft we have it 18363 13:52:01,275 --> 13:52:05,595 so we're going to run the task to build 18364 13:52:05,595 --> 13:52:08,879 and you're going to see that the world 18365 13:52:06,879 --> 13:52:11,040 is going to be good the world finished 18366 13:52:08,879 --> 13:52:12,559 successfully and if we go and we're 18367 13:52:12,559 --> 13:52:16,399 and run rooster you're going to see that 18368 13:52:14,160 --> 13:52:18,160 it is going to do exactly the same thing 18369 13:52:16,400 --> 13:52:20,080 but some compilers might give you 18370 13:52:18,160 --> 13:52:22,480 warnings or errors if you try to do 18371 13:52:20,080 --> 13:52:24,160 something like this and the reason is 18372 13:52:22,480 --> 13:52:26,720 you really have to be careful using 18373 13:52:24,160 --> 13:52:29,276 these functions because if for example 18374 13:52:26,720 --> 13:52:32,000 destination array is not large enough 18375 13:52:29,275 --> 13:52:33,839 you may do some crazy things running 18376 13:52:32,000 --> 13:52:36,480 over the balance of your array and it 18377 13:52:33,839 --> 13:52:38,479 can be really bad for example if we try 18378 13:52:36,480 --> 13:52:41,040 and make destination array here for 18379 13:52:38,480 --> 13:52:43,680 example a size of three it is possible 18380 13:52:41,040 --> 13:52:46,800 to do that and you see that we get a 18381 13:52:43,680 --> 13:52:49,436 warning from visual studio code and if 18382 13:52:46,800 --> 13:52:51,520 we try and build this with gcc let's try 18383 13:52:49,436 --> 13:52:54,000 and remove the garbage we have from 18384 13:52:51,519 --> 13:52:58,079 visual studio first now that this is 18385 13:52:54,000 --> 13:53:00,000 clean we can uh try and build with gcc 18386 13:52:58,080 --> 13:53:02,560 let's do that our compiler we're going 18387 13:53:00,000 --> 13:53:03,839 to choose gcc and you're going to see 18388 13:53:02,559 --> 13:53:05,756 that it's going to give you an error 18389 13:53:03,839 --> 13:53:08,159 it's going to actually protect us from 18390 13:53:05,756 --> 13:53:10,160 doing something like this but in some 18391 13:53:08,160 --> 13:53:12,400 cases some compilers are not going to 18392 13:53:10,160 --> 13:53:15,276 stop you from doing this but any 18393 13:53:12,400 --> 13:53:17,680 compiler that really respects itself 18394 13:53:15,275 --> 13:53:19,360 it's going to have these safety checks 18395 13:53:17,680 --> 13:53:21,360 in and it's going to protect you from 18396 13:53:19,360 --> 13:53:23,680 doing something crazy like this okay 18397 13:53:21,360 --> 13:53:27,840 this is how you can do concatenation 18398 13:53:23,680 --> 13:53:29,436 using hdr cat like we did here let's try 18399 13:53:27,839 --> 13:53:31,275 and weld and see that now we are 18400 13:53:29,436 --> 13:53:33,756 building fine because i don't like these 18401 13:53:31,275 --> 13:53:36,000 red lines here the world is good and we 18402 13:53:33,756 --> 13:53:37,275 are all fine now i am going to comment 18403 13:53:37,275 --> 13:53:41,436 and we're going to see another example 18404 13:53:38,800 --> 13:53:43,276 of how we can concatenate stuff we are 18405 13:53:41,436 --> 13:53:45,596 going to go down here let's bring this 18406 13:53:45,595 --> 13:53:49,915 and we are going to put in our simple 18407 13:53:47,839 --> 13:53:52,239 example exactly the same thing we saw in 18408 13:53:52,239 --> 13:53:57,756 character arrays they are dynamic arrays 18409 13:53:55,116 --> 13:54:00,000 but we initialize them with characters 18410 13:53:57,756 --> 13:54:00,880 like this and we make sure to put in our 18411 13:54:00,879 --> 13:54:05,275 terminator we have our source strength 18412 13:54:05,275 --> 13:54:08,955 femix king and it's going to have a 18413 13:54:08,955 --> 13:54:13,275 and what we're going to do we're going 18414 13:54:10,319 --> 13:54:15,519 to take soros and concatenate that to 18415 13:54:13,275 --> 13:54:17,680 our destination so what is really going 18416 13:54:15,519 --> 13:54:19,680 to happen we are going to overwrite the 18417 13:54:17,680 --> 13:54:22,639 null terminator here and we're going to 18418 13:54:19,680 --> 13:54:24,955 start pasting in things from source here 18419 13:54:22,639 --> 13:54:27,360 so after the concatenation is done what 18420 13:54:24,955 --> 13:54:29,595 we are doing here we are concatenating 18421 13:54:29,595 --> 13:54:35,360 we are going to have fire lord the 18422 13:54:32,400 --> 13:54:36,480 phoenix kang stored in our destination 18423 13:54:36,480 --> 13:54:42,319 and notice that we made sure that 18424 13:54:39,756 --> 13:54:44,239 large enough to contain our characters 18425 13:54:42,319 --> 13:54:46,879 here and if we print it out we're going 18426 13:54:44,239 --> 13:54:49,595 to have the thing printed out we can try 18427 13:54:46,879 --> 13:54:51,755 and weld with gcc that's going to go 18428 13:54:49,595 --> 13:54:52,639 through let's bring this up the bulb is 18429 13:54:52,639 --> 13:54:57,595 if you go down here the build is good 18430 13:54:54,800 --> 13:54:59,276 we're going to clear and run rooster 18431 13:54:57,595 --> 13:55:01,839 okay this is the output here you see 18432 13:54:59,275 --> 13:55:05,275 that the destination is eight characters 18433 13:55:01,839 --> 13:55:08,079 long if we count one two three four five 18434 13:55:05,275 --> 13:55:10,319 six seven eight remember sdrl lan 18435 13:55:08,080 --> 13:55:12,400 doesn't count the null character so this 18436 13:55:10,319 --> 13:55:14,559 is what we expect eight characters the 18437 13:55:12,400 --> 13:55:16,000 source is going to be 17 characters if 18438 13:55:14,559 --> 13:55:17,199 you count all this that's what you're 18439 13:55:17,199 --> 13:55:21,595 and if we concatenate them now this 18440 13:55:24,000 --> 13:55:28,879 in the eyes of hdr land and you see that 18441 13:55:26,955 --> 13:55:31,595 we print the destination and it is 18442 13:55:28,879 --> 13:55:34,000 exactly what we expect fire lord the 18443 13:55:31,595 --> 13:55:35,595 phoenix gang okay we're going to comment 18444 13:55:34,000 --> 13:55:37,595 this out i just wanted you to see an 18445 13:55:35,595 --> 13:55:40,399 example of this and the one thing i 18446 13:55:37,595 --> 13:55:42,799 really want to stress in this example 18447 13:55:40,400 --> 13:55:45,916 is that strcat is going to start 18448 13:55:42,800 --> 13:55:49,756 overriding whatever is in dust starting 18449 13:55:45,915 --> 13:55:52,239 at the last null character okay so this 18450 13:55:49,756 --> 13:55:54,160 guy is going to be overridden by the 18451 13:55:52,239 --> 13:55:55,839 comma here and we're going to start and 18452 13:55:54,160 --> 13:55:58,080 putting stuff and that's what we're 18453 13:55:55,839 --> 13:56:00,159 going to see here let's comment this out 18454 13:55:58,080 --> 13:56:02,240 because we want to move on and show you 18455 13:56:00,160 --> 13:56:04,640 a few other examples okay the next thing 18456 13:56:02,239 --> 13:56:08,400 we're going to look at is a new version 18457 13:56:04,639 --> 13:56:09,915 of str cat that allows you to specify 18458 13:56:11,436 --> 13:56:16,639 from source for example here we have two 18459 13:56:13,756 --> 13:56:19,756 strengths destination and source and 18460 13:56:16,639 --> 13:56:22,879 here we are basically saying i want to 18461 13:56:19,756 --> 13:56:24,639 concatenate six characters from source 18462 13:56:22,879 --> 13:56:26,799 and put them at the end of the 18463 13:56:24,639 --> 13:56:30,800 destination here and what it's going to 18464 13:56:26,800 --> 13:56:33,040 do it's going to grab one two three four 18465 13:56:30,800 --> 13:56:35,040 five six it's going to grab this entire 18466 13:56:33,040 --> 13:56:37,200 thing and it's going to append that to 18467 13:56:35,040 --> 13:56:40,160 hello and we're going to have hello 18468 13:56:37,199 --> 13:56:42,479 there stored in the destination and the 18469 13:56:40,160 --> 13:56:44,956 return value is going to be a pointer to 18470 13:56:42,480 --> 13:56:47,360 destination 2. and how do i know the 18471 13:56:44,955 --> 13:56:50,080 return value well i know that from the 18472 13:56:47,360 --> 13:56:51,879 documentation if you go in your favorite 18473 13:56:51,879 --> 13:56:57,115 stdstr and cat and you're going to see a 18474 13:56:54,955 --> 13:56:58,639 link here if you click on it from cpp 18475 13:56:57,116 --> 13:57:00,160 reference it's going to give you 18476 13:56:58,639 --> 13:57:02,639 everything you need to know about this 18477 13:57:00,160 --> 13:57:05,916 function here it's going to append 18478 13:57:02,639 --> 13:57:07,756 source to destination and and count 18479 13:57:05,915 --> 13:57:09,519 characters are going to be copied so the 18480 13:57:07,756 --> 13:57:11,916 third parameter here is going to be how 18481 13:57:09,519 --> 13:57:14,559 many characters you want copied and 18482 13:57:11,915 --> 13:57:16,639 pasted into our destination 18483 13:57:14,559 --> 13:57:18,319 and the return value is going to be dest 18484 13:57:16,639 --> 13:57:20,479 okay if we try this out we're going to 18485 13:57:18,319 --> 13:57:22,159 see hello there printed out we're going 18486 13:57:20,480 --> 13:57:24,240 to print it out directly and we're going 18487 13:57:24,239 --> 13:57:29,040 using the destination pointer that is 18488 13:57:26,955 --> 13:57:30,955 going to be returned from this guy here 18489 13:57:29,040 --> 13:57:33,436 so if we try and build this we're going 18490 13:57:30,955 --> 13:57:35,756 to build with gcc the bullet is going to 18491 13:57:33,436 --> 13:57:37,040 be good we're going to clear and run 18492 13:57:35,756 --> 13:57:39,436 rooster and you're going to see that we 18493 13:57:37,040 --> 13:57:41,436 have hello there twice here and this is 18494 13:57:41,436 --> 13:57:48,000 we are going to comment this out and we 18495 13:57:43,436 --> 13:57:51,040 are going to go down and look at sdr cpi 18496 13:57:48,000 --> 13:57:53,595 i should say sdr cpy which is going to 18497 13:57:51,040 --> 13:57:57,040 copy stuff from the source and paste 18498 13:57:53,595 --> 13:57:59,199 those in the destination and one thing 18499 13:57:57,040 --> 13:58:01,275 you should know is that the copy is 18500 13:57:59,199 --> 13:58:03,595 going to happen starting from the start 18501 13:58:01,275 --> 13:58:06,000 of the destination array here we have 18502 13:58:03,595 --> 13:58:08,559 two arrays one is called source three 18503 13:58:06,000 --> 13:58:11,116 the other is called destination three 18504 13:58:08,559 --> 13:58:12,879 destination 3 is set up on the heap 18505 13:58:11,116 --> 13:58:15,276 using the new operator so it is a 18506 13:58:12,879 --> 13:58:17,519 dynamic array and you see that we make 18507 13:58:15,275 --> 13:58:18,639 sure that it is the same size as the 18508 13:58:18,639 --> 13:58:23,436 we are using sdr lan to figure out how 18509 13:58:21,275 --> 13:58:26,159 many characters we have in here 18510 13:58:23,436 --> 13:58:28,239 and remember that sdr land doesn't 18511 13:58:26,160 --> 13:58:30,720 account for the null character so we are 18512 13:58:28,239 --> 13:58:32,639 adding one to account for the null 18513 13:58:30,720 --> 13:58:34,559 character that's what we are doing here 18514 13:58:32,639 --> 13:58:38,159 and after that we are going to do std 18515 13:58:34,559 --> 13:58:41,275 str cpy and what that is going to do is 18516 13:58:38,160 --> 13:58:42,956 copy stuff from source to destination 18517 13:58:41,275 --> 13:58:46,159 character by character so we are 18518 13:58:42,955 --> 13:58:48,639 basically going to have this guy pasted 18519 13:58:46,160 --> 13:58:50,000 into destination here and if we print 18520 13:58:48,639 --> 13:58:52,720 things out we're going to see that 18521 13:58:50,000 --> 13:58:55,595 destination contains the same thing as 18522 13:58:52,720 --> 13:58:57,200 source here if we built this with gcc 18523 13:58:55,595 --> 13:58:58,799 let's do that the build is going to be 18524 13:58:58,800 --> 13:59:03,200 and run rooster and you're going to see 18525 13:59:00,879 --> 13:59:05,275 that the destination has exactly the 18526 13:59:03,199 --> 13:59:08,159 same thing we had in the source and if 18527 13:59:05,275 --> 13:59:11,199 you need to copy stuff from arrays sdr 18528 13:59:08,160 --> 13:59:13,040 cpui is going to be very helpful 18529 13:59:11,199 --> 13:59:15,115 we are going to comment this out because 18530 13:59:13,040 --> 13:59:15,915 we still have a few things we want to 18531 13:59:15,915 --> 13:59:20,955 so let's comment this out we are going 18532 13:59:18,879 --> 13:59:23,595 to go down and see that we can also use 18533 13:59:20,955 --> 13:59:25,915 a another version that allows us to 18534 13:59:23,595 --> 13:59:28,239 specify how many characters we want 18535 13:59:25,915 --> 13:59:31,680 copied from the source okay in this 18536 13:59:28,239 --> 13:59:34,559 example here we are using std str and 18537 13:59:31,680 --> 13:59:38,080 cpy and this n here means that we can 18538 13:59:34,559 --> 13:59:39,115 specify the count of characters we do 18539 13:59:39,116 --> 13:59:43,916 the current function is doing here okay 18540 13:59:41,360 --> 13:59:46,800 so we are specifying i want five 18541 13:59:43,915 --> 13:59:49,275 characters copied from source and i want 18542 13:59:46,800 --> 13:59:51,436 those pasted into destination here 18543 13:59:49,275 --> 13:59:53,275 that's what we are saying here and the 18544 13:59:51,436 --> 13:59:54,800 source is going to contain hello 18545 13:59:53,275 --> 13:59:57,275 destination is going to contain the 18546 13:59:54,800 --> 14:00:00,240 world and the copy is going to happen 18547 13:59:57,275 --> 14:00:02,079 starting at the first index here so 18548 14:00:00,239 --> 14:00:05,436 we're going to have hello override 18549 14:00:02,080 --> 14:00:07,916 whatever we have in a b c d e because 18550 14:00:05,436 --> 14:00:10,639 that's five characters here and the dust 18551 14:00:07,915 --> 14:00:12,955 is going to contain hello f if we print 18552 14:00:10,639 --> 14:00:14,479 it out let's build this and see if 18553 14:00:12,955 --> 14:00:16,400 that's what we're going to get we're 18554 14:00:14,480 --> 14:00:17,840 going to build with gcc the build is 18555 14:00:17,839 --> 14:00:22,000 we are going to clear and run rooster 18556 14:00:20,080 --> 14:00:23,116 and you see that that's exactly what we 18557 14:00:23,116 --> 14:00:27,040 okay this is how these methods work 18558 14:00:25,360 --> 14:00:27,915 there are many more functions you can 18559 14:00:27,915 --> 14:00:32,079 from the c string library and again if 18560 14:00:32,080 --> 14:00:36,319 a list of them all you can go through 18561 14:00:36,319 --> 14:00:40,080 favorite search engine and search for c 18562 14:00:40,080 --> 14:00:45,436 and we can specify cpp reference here 18563 14:00:43,040 --> 14:00:47,840 let's try and do that okay we can find a 18564 14:00:45,436 --> 14:00:49,680 link to that we can click on that and 18565 14:00:47,839 --> 14:00:52,000 it's going to open up and show us many 18566 14:00:49,680 --> 14:00:53,840 more functions we can use and if you 18567 14:00:52,000 --> 14:00:55,915 need to know about a special function 18568 14:00:53,839 --> 14:00:57,519 you can click on it and see what it does 18569 14:00:55,915 --> 14:00:59,839 and if you go down you're going to find 18570 14:00:57,519 --> 14:01:01,435 even an example of how you can use it 18571 14:00:59,839 --> 14:01:03,039 for now we are going to stop here in 18572 14:01:01,436 --> 14:01:05,360 this lecture and this is actually going 18573 14:01:03,040 --> 14:01:07,116 to be the last lecture we see on c 18574 14:01:05,360 --> 14:01:10,160 strings the next one we're going to 18575 14:01:07,116 --> 14:01:12,639 start and look at std strength which is 18576 14:01:10,160 --> 14:01:13,680 a compound type that makes working with 18577 14:01:16,319 --> 14:01:20,559 go ahead and finish up here and meet me 18578 14:01:18,720 --> 14:01:23,360 there in this lecture we're going to 18579 14:01:20,559 --> 14:01:25,435 start and learn about hdd strength which 18580 14:01:23,360 --> 14:01:28,239 is a type that is going to make it super 18581 14:01:25,436 --> 14:01:30,080 easy to work with string data we have 18582 14:01:28,239 --> 14:01:32,639 seen many ways we can work with 18583 14:01:30,080 --> 14:01:35,200 character arrays by now we can use row 18584 14:01:32,639 --> 14:01:37,595 rays we can point to them with pointers 18585 14:01:35,199 --> 14:01:40,639 we can even allocate arrays on the hip 18586 14:01:37,595 --> 14:01:43,756 but all these character arrays had a few 18587 14:01:40,639 --> 14:01:46,400 problems that are really a pain in the 18588 14:01:43,756 --> 14:01:48,480 neck to deal with on a daily basis 18589 14:01:46,400 --> 14:01:50,319 one of those problems was that you had 18590 14:01:48,480 --> 14:01:51,756 to keep the size in check you have to 18591 14:01:51,756 --> 14:01:57,680 elements you have in the character array 18592 14:01:54,639 --> 14:02:00,319 and that was defining the bounds that 18593 14:01:57,680 --> 14:02:02,720 you can work in if you go over bounds we 18594 14:02:00,319 --> 14:02:04,955 have seen that that can cause crashes of 18595 14:02:02,720 --> 14:02:07,275 your program and you don't want that 18596 14:02:04,955 --> 14:02:09,436 another problem was that we had 3d to 18597 14:02:07,275 --> 14:02:11,915 remember to keep track of the null 18598 14:02:09,436 --> 14:02:13,680 character if we don't have it in place 18599 14:02:11,915 --> 14:02:16,079 we're going to have problems when our 18600 14:02:13,680 --> 14:02:18,400 string is printed out and it's really 18601 14:02:16,080 --> 14:02:21,040 annoying to always have to think about 18602 14:02:18,400 --> 14:02:24,400 these little details when all you really 18603 14:02:21,040 --> 14:02:27,520 want is to store a strand so c plus plus 18604 14:02:24,400 --> 14:02:29,756 offers a high level type that we can use 18605 14:02:27,519 --> 14:02:32,799 and it is going to hide all these little 18606 14:02:29,756 --> 14:02:35,275 details underground and all we have to 18607 14:02:32,800 --> 14:02:37,276 do is put in our string and use it and 18608 14:02:35,275 --> 14:02:39,436 we can be on our merry way and do 18609 14:02:37,275 --> 14:02:41,519 whatever we want to do with string data 18610 14:02:39,436 --> 14:02:42,639 in our c plus plus application so that's 18611 14:02:41,519 --> 14:02:44,955 what we're going to do we're going to 18612 14:02:42,639 --> 14:02:47,680 have a type that is going to wrap on top 18613 14:02:44,955 --> 14:02:50,400 of the ugly array computations we have 18614 14:02:47,680 --> 14:02:52,639 been doing on character arrays we're 18615 14:02:50,400 --> 14:02:54,955 going to call that std string and we're 18616 14:02:52,639 --> 14:02:57,756 going to be able to do many of the 18617 14:02:54,955 --> 14:02:59,915 things we were doing with c strings but 18618 14:02:57,756 --> 14:03:01,916 in a much better way we can concatenate 18619 14:02:59,915 --> 14:03:04,000 we can search we can copy we can do all 18620 14:03:01,915 --> 14:03:06,239 kinds of crazy things really but it's 18621 14:03:04,000 --> 14:03:08,080 going to be in a way that you don't have 18622 14:03:06,239 --> 14:03:10,479 to worry about these little details 18623 14:03:08,080 --> 14:03:12,955 about array bounds the null character 18624 14:03:10,480 --> 14:03:16,000 you're going to just use it for what you 18625 14:03:12,955 --> 14:03:17,595 want which is storing character or text 18626 14:03:16,000 --> 14:03:19,275 data okay so that's what we're going to 18627 14:03:17,595 --> 14:03:21,275 be doing in the next few lectures we're 18628 14:03:19,275 --> 14:03:22,955 going to be learning about std string 18629 14:03:21,275 --> 14:03:25,040 and how you can use it in your c plus 18630 14:03:22,955 --> 14:03:27,595 plus applications and it's going to make 18631 14:03:25,040 --> 14:03:29,436 your life a whole lot easier we are 18632 14:03:27,595 --> 14:03:30,879 going to stop here in this lecture the 18633 14:03:29,436 --> 14:03:34,160 next one we're going to see how you can 18634 14:03:30,879 --> 14:03:36,319 declare initialize and use std strength 18635 14:03:34,160 --> 14:03:39,276 in your c plus plus application go ahead 18636 14:03:36,319 --> 14:03:40,639 and finish up here and meet me there and 18637 14:03:39,275 --> 14:03:43,436 this lecture we're going to see how you 18638 14:03:40,639 --> 14:03:45,519 can declare and use hdd strength in your 18639 14:03:45,519 --> 14:03:49,519 std strength is a type that is going to 18640 14:03:47,436 --> 14:03:51,360 wrap on top of arrays and give us a 18641 14:03:53,116 --> 14:03:58,080 store string data without having to deal 18642 14:03:55,839 --> 14:04:00,479 with the ugly little details like 18643 14:03:58,080 --> 14:04:02,480 keeping track of array bound remembering 18644 14:04:00,480 --> 14:04:04,720 to keep in check the null character and 18645 14:04:02,480 --> 14:04:07,040 things like that to use std strength you 18646 14:04:04,720 --> 14:04:09,040 will need to include the string library 18647 14:04:07,040 --> 14:04:11,915 and after you do this you can declare 18648 14:04:09,040 --> 14:04:14,480 and use your strings just like this here 18649 14:04:11,915 --> 14:04:16,639 we have a series of examples just to get 18650 14:04:14,480 --> 14:04:18,319 you started on this you declare your 18651 14:04:16,639 --> 14:04:20,400 strengths just like you declare any 18652 14:04:18,319 --> 14:04:22,400 other time you specify the type on the 18653 14:04:20,400 --> 14:04:24,720 left the type is going to be std 18654 14:04:22,400 --> 14:04:26,955 strength by the way you'll have to 18655 14:04:24,720 --> 14:04:28,800 remember to use std column column like 18656 14:04:26,955 --> 14:04:32,080 this and we will have a chance to 18657 14:04:28,800 --> 14:04:34,319 explain why in a few chapters ahead but 18658 14:04:32,080 --> 14:04:37,116 for now we're just going to think of std 18659 14:04:34,319 --> 14:04:39,680 strength as a type so we declare a 18660 14:04:37,116 --> 14:04:41,840 string type we call it full name and we 18661 14:04:39,680 --> 14:04:43,915 can leave it like this uninitialized and 18662 14:04:41,839 --> 14:04:46,319 it is going to be an empty strand we can 18663 14:04:43,915 --> 14:04:48,719 also put in our string and initialize 18664 14:04:46,319 --> 14:04:51,199 that with a string literal and it is 18665 14:04:48,720 --> 14:04:53,360 going to work another way of doing this 18666 14:04:51,199 --> 14:04:55,360 is initializing a string with another 18667 14:04:53,360 --> 14:04:57,199 string which we are doing down here you 18668 14:04:55,360 --> 14:04:59,680 can also set up another string and 18669 14:04:57,199 --> 14:05:02,000 initialize it with part of another 18670 14:04:59,680 --> 14:05:04,160 string literal so for example message 18671 14:05:02,000 --> 14:05:06,480 here is going to be hello and it's only 18672 14:05:04,160 --> 14:05:08,000 going to grab five characters from this 18673 14:05:06,480 --> 14:05:09,436 string literal this is something you 18674 14:05:09,436 --> 14:05:12,955 if it makes sense for whatever 18675 14:05:10,955 --> 14:05:15,436 application you are doing here we can 18676 14:05:12,955 --> 14:05:18,159 also initialize our strength with 18677 14:05:15,436 --> 14:05:20,639 multiple copies of one character and 18678 14:05:18,160 --> 14:05:22,240 this is the syntax to do it you say 18679 14:05:20,639 --> 14:05:23,839 the name of your variable you say 18680 14:05:22,239 --> 14:05:25,915 however many times you want the 18681 14:05:23,839 --> 14:05:28,559 character repeated and you say which 18682 14:05:25,915 --> 14:05:31,595 character it is just like this we can 18683 14:05:28,559 --> 14:05:34,159 also initialize a string with part of 18684 14:05:31,595 --> 14:05:36,720 another existing string so for example 18685 14:05:34,160 --> 14:05:39,200 here we have saying hello and it is 18686 14:05:36,720 --> 14:05:41,520 initialized with part of what we have in 18687 14:05:39,199 --> 14:05:44,000 greeting here so we're going to start at 18688 14:05:44,000 --> 14:05:51,199 0 1 2 3 4 5 6 on the w here and we're 18689 14:05:48,639 --> 14:05:53,116 going to grab five characters so one two 18690 14:05:51,199 --> 14:05:54,955 three four five and that's what we're 18691 14:05:53,116 --> 14:05:56,955 going to use to initialize this saying 18692 14:05:54,955 --> 14:05:59,436 hello here and if we print it out we're 18693 14:05:56,955 --> 14:06:01,519 going to see that this is going to be 18694 14:05:59,436 --> 14:06:03,436 exactly the case so hopefully you can 18695 14:06:01,519 --> 14:06:05,039 see that it is easier you don't have to 18696 14:06:03,436 --> 14:06:08,000 use pointers you don't have to use 18697 14:06:05,040 --> 14:06:10,559 arrays and the best part is std strength 18698 14:06:08,000 --> 14:06:12,879 can grow automatically if you happen to 18699 14:06:10,559 --> 14:06:15,275 store in more characters than you 18700 14:06:12,879 --> 14:06:16,955 initialized it with in the first place 18701 14:06:15,275 --> 14:06:19,040 and that's pretty cool so now we're 18702 14:06:16,955 --> 14:06:20,955 going to head over to visual studio code 18703 14:06:19,040 --> 14:06:23,756 and play with us so that you can see for 18704 14:06:20,955 --> 14:06:26,319 yourself okay here we are in our working 18705 14:06:23,756 --> 14:06:28,559 folder the current project is declaring 18706 14:06:26,319 --> 14:06:30,400 and using std strength which is what we 18707 14:06:28,559 --> 14:06:32,319 are doing here we're going to grab our 18708 14:06:30,400 --> 14:06:34,239 template files pretty quick and we're 18709 14:06:32,319 --> 14:06:36,480 going to go and store in the files and 18710 14:06:34,239 --> 14:06:38,879 we're going to open the folder and we're 18711 14:06:36,480 --> 14:06:41,596 going to include the strength library 18712 14:06:38,879 --> 14:06:44,399 let's do that okay this is how we do it 18713 14:06:41,595 --> 14:06:47,199 and now that we have it included in our 18714 14:06:44,400 --> 14:06:48,720 project we can start declaring and using 18715 14:06:48,720 --> 14:06:53,040 types in our c plus plus program we are 18716 14:06:51,275 --> 14:06:55,115 going to go down and put in the code to 18717 14:06:53,040 --> 14:06:57,520 play with this a little bit so you can 18718 14:06:55,116 --> 14:06:59,680 declare an empty string like this and 18719 14:06:57,519 --> 14:07:02,000 it's not going to have anything inside 18720 14:06:59,680 --> 14:07:04,800 you can declare a string and put in a 18721 14:07:02,000 --> 14:07:07,595 string literal like we are doing here 18722 14:07:04,800 --> 14:07:09,520 and std string knows how to handle this 18723 14:07:07,595 --> 14:07:12,079 so it's going to store it inside using a 18724 14:07:09,519 --> 14:07:14,079 character array but we don't have to 18725 14:07:12,080 --> 14:07:16,639 really worry about that character array 18726 14:07:14,080 --> 14:07:20,800 is going to work we're just going to use 18727 14:07:16,639 --> 14:07:23,436 std string here we can set up a string 18728 14:07:20,800 --> 14:07:26,160 using another string in our initializer 18729 14:07:26,160 --> 14:07:29,756 part of a literal to initialize the 18730 14:07:29,756 --> 14:07:33,520 this string literal here we're going to 18731 14:07:31,519 --> 14:07:35,519 grab part of it and we're going to grab 18732 14:07:33,519 --> 14:07:38,479 five characters and use those to 18733 14:07:35,519 --> 14:07:40,799 initialize our message std string here 18734 14:07:38,480 --> 14:07:42,560 and this is pretty cool we can also use 18735 14:07:40,800 --> 14:07:44,560 multiple characters to initialize a 18736 14:07:42,559 --> 14:07:46,479 string like this we say however many 18737 14:07:44,559 --> 14:07:48,159 copies we want of this character in this 18738 14:07:46,480 --> 14:07:50,400 string here and it is going to be 18739 14:07:48,160 --> 14:07:51,680 exactly what we want so it's going to be 18740 14:07:54,239 --> 14:07:58,479 and we can initialize the string with 18741 14:07:56,319 --> 14:08:01,040 part of another string and this is 18742 14:07:58,480 --> 14:08:03,520 really cool and it may come in handy in 18743 14:08:01,040 --> 14:08:06,319 many applications so for example here 18744 14:08:03,519 --> 14:08:08,879 saying hello is going to contain 18745 14:08:06,319 --> 14:08:10,800 world here and we are saying that this 18746 14:08:08,879 --> 14:08:13,680 is what we want by saying we want to 18747 14:08:10,800 --> 14:08:17,200 start at index six in this string here 18748 14:08:13,680 --> 14:08:20,400 so if we go zero one two three four five 18749 14:08:17,199 --> 14:08:22,719 six this is going to be our character at 18750 14:08:20,400 --> 14:08:24,319 index six and from this index we're 18751 14:08:22,720 --> 14:08:27,116 going to grab five characters so we're 18752 14:08:24,319 --> 14:08:29,360 going to count one two three four five 18753 14:08:27,116 --> 14:08:31,520 and that's going to be what's stored in 18754 14:08:29,360 --> 14:08:33,360 this string here and if you don't 18755 14:08:31,519 --> 14:08:35,039 believe me we can print all these things 18756 14:08:33,360 --> 14:08:37,360 out and they are going to be exactly 18757 14:08:35,040 --> 14:08:39,595 what we expect and by this you can even 18758 14:08:37,360 --> 14:08:41,840 see that we can print std strengths 18759 14:08:41,839 --> 14:08:46,239 very conveniently like this and this is 18760 14:08:44,080 --> 14:08:49,200 very cool okay so we can try and weld 18761 14:08:46,239 --> 14:08:50,559 this with gcc the build is going to go 18762 14:08:49,199 --> 14:08:52,719 through so we're going to bring up a 18763 14:08:50,559 --> 14:08:55,839 terminal so that we can run this and 18764 14:08:52,720 --> 14:08:57,275 we're going to clear and let's see what 18765 14:08:57,275 --> 14:09:02,559 run rooster here so we're going to print 18766 14:09:00,080 --> 14:09:04,400 the full name the full name is empty 18767 14:09:02,559 --> 14:09:06,239 okay so we're not going to see anything 18768 14:09:04,400 --> 14:09:08,800 because it is empty we're going to see 18769 14:09:06,239 --> 14:09:11,680 planet planet is this thing we have in 18770 14:09:08,800 --> 14:09:14,480 here planet where the sky is blue so 18771 14:09:11,680 --> 14:09:17,199 this is what we expect we can say 18772 14:09:14,480 --> 14:09:19,200 preferred planet and put in the text for 18773 14:09:17,199 --> 14:09:20,799 the planet here so we're going to get 18774 14:09:20,800 --> 14:09:24,956 and if we have a message and say hello 18775 14:09:24,955 --> 14:09:29,275 first characters in this string literal 18776 14:09:27,436 --> 14:09:31,680 message is going to say hello which is 18777 14:09:29,275 --> 14:09:33,199 what we are saying here let's go down 18778 14:09:31,680 --> 14:09:35,519 and look at where the message is going 18779 14:09:33,199 --> 14:09:39,039 to be for ease which is what we have 18780 14:09:35,519 --> 14:09:41,519 here and we can initialize saying hello 18781 14:09:39,040 --> 14:09:44,559 with part of what we have in greeting 18782 14:09:41,519 --> 14:09:46,719 and we have targeted this word string 18783 14:09:44,559 --> 14:09:49,199 here and it is what we see here and this 18784 14:09:46,720 --> 14:09:51,520 is pretty cool the good thing about hdd 18785 14:09:49,199 --> 14:09:53,595 string however is that you can 18786 14:09:51,519 --> 14:09:57,199 store in more characters than we 18787 14:09:53,595 --> 14:09:59,519 initialize this guy with so for example 18788 14:09:57,199 --> 14:10:00,639 here we have this message here but we 18789 14:10:00,639 --> 14:10:05,116 planet let's go down here and say it 18790 14:10:02,800 --> 14:10:07,360 we're going to change std string at run 18791 14:10:05,116 --> 14:10:08,880 time so we're going to say planet and 18792 14:10:07,360 --> 14:10:10,720 we're going to assign something to it 18793 14:10:08,879 --> 14:10:12,799 we're going to put in whatever was in 18794 14:10:10,720 --> 14:10:14,880 there first and we're going to paste 18795 14:10:12,800 --> 14:10:16,956 that in multiple times let's do three 18796 14:10:14,879 --> 14:10:19,595 times i think that's going to be enough 18797 14:10:16,955 --> 14:10:21,839 or we can even bring this back so that 18798 14:10:19,595 --> 14:10:24,159 we see the entire thing here doesn't 18799 14:10:21,839 --> 14:10:27,275 really matter all we want is for this 18800 14:10:24,160 --> 14:10:28,560 thing to be larger than it was initially 18801 14:10:28,559 --> 14:10:33,040 in a planet here so we're going to 18802 14:10:31,040 --> 14:10:35,520 remove these quotes because they are 18803 14:10:33,040 --> 14:10:37,436 making things ugly for us and now it's 18804 14:10:35,519 --> 14:10:40,879 going to be a valid strength let's wait 18805 14:10:37,436 --> 14:10:42,800 for visual studio code to realize that 18806 14:10:40,879 --> 14:10:45,199 and it's going to remove these squiggly 18807 14:10:42,800 --> 14:10:47,756 lines now we are good std string is 18808 14:10:45,199 --> 14:10:48,719 automatically going to take care of the 18809 14:10:48,720 --> 14:10:53,436 to make this string grow we don't have 18810 14:10:51,360 --> 14:10:55,116 to worry about that and if we print 18811 14:10:53,436 --> 14:10:57,596 planet now we're going to see the same 18812 14:10:55,116 --> 14:11:00,319 details and taken care of okay this is 18813 14:10:57,595 --> 14:11:01,756 really cool and it makes it super easy 18814 14:11:01,756 --> 14:11:07,200 text and string data like you see here 18815 14:11:04,559 --> 14:11:08,319 so let's build again so that our changes 18816 14:11:08,319 --> 14:11:13,199 reflected in our output we're going to 18817 14:11:10,639 --> 14:11:15,839 clear and run rooster and we're going to 18818 14:11:13,199 --> 14:11:17,115 see that planet is now really large okay 18819 14:11:17,116 --> 14:11:21,360 this thing the size of this thing at 18820 14:11:19,199 --> 14:11:23,519 runtime and this is something we 18821 14:11:21,360 --> 14:11:25,840 couldn't do with arrays so for example 18822 14:11:23,519 --> 14:11:28,559 if we go down here and use a row array 18823 14:11:25,839 --> 14:11:30,639 to do something like this this code is 18824 14:11:28,559 --> 14:11:32,479 going to run but try to think about what 18825 14:11:30,639 --> 14:11:35,360 is really happening here we have an 18826 14:11:32,480 --> 14:11:38,160 array which contains this text here 18827 14:11:35,360 --> 14:11:40,559 and it is pointed to by this cost card 18828 14:11:38,160 --> 14:11:42,800 pointer that we have if we try and do an 18829 14:11:40,559 --> 14:11:46,079 assignment like this this is really 18830 14:11:42,800 --> 14:11:49,040 going to be to make it point to another 18831 14:11:46,080 --> 14:11:50,880 character array in memory and this piece 18832 14:11:49,040 --> 14:11:54,559 of memory here is basically going to be 18833 14:11:50,879 --> 14:11:57,360 wasted because we think we have our 18834 14:11:57,360 --> 14:12:01,840 this is not the case with std strength 18835 14:11:59,915 --> 14:12:04,239 because std strength when we do 18836 14:12:01,839 --> 14:12:06,559 something like this the initial memory 18837 14:12:06,559 --> 14:12:11,595 planet here is going to be returned to 18838 14:12:08,720 --> 14:12:13,756 the operand system and a new memory is 18839 14:12:11,595 --> 14:12:16,319 going to be allocated to contain this 18840 14:12:13,756 --> 14:12:19,360 new thing so the waste we're doing here 18841 14:12:16,319 --> 14:12:21,756 by just leaving this in memory and not 18842 14:12:19,360 --> 14:12:23,360 using it is not being done here so if we 18843 14:12:21,756 --> 14:12:26,239 try to print we're going to get the same 18844 14:12:23,360 --> 14:12:29,840 output but inside and how our memory is 18845 14:12:26,239 --> 14:12:32,159 managed hdd string is better in that the 18846 14:12:29,839 --> 14:12:34,879 memory that was used to initialize this 18847 14:12:34,879 --> 14:12:39,199 here with array what we really are doing 18848 14:12:37,040 --> 14:12:42,080 is just make this planet pointer point 18849 14:12:39,199 --> 14:12:43,755 to another string and leaving this to be 18850 14:12:42,080 --> 14:12:46,080 wasted in memory this is what we are 18851 14:12:43,756 --> 14:12:47,756 doing here and we can avoid that with 18852 14:12:47,756 --> 14:12:52,400 in this lecture we're going to learn 18853 14:12:49,275 --> 14:12:55,519 about the one definition rule and this 18854 14:12:52,400 --> 14:12:58,800 is a rule that says definitions can't 18855 14:12:55,519 --> 14:13:01,839 show up more than once in your entire c 18856 14:12:58,800 --> 14:13:04,240 plus plus program or in some cases a 18857 14:13:01,839 --> 14:13:06,559 definition can show up more than once in 18858 14:13:04,239 --> 14:13:08,720 a translation unit we are going to look 18859 14:13:06,559 --> 14:13:10,955 at the one definition rule in the 18860 14:13:08,720 --> 14:13:13,916 context of freestanding variables 18861 14:13:10,955 --> 14:13:17,116 functions classes member functions and 18862 14:13:13,915 --> 14:13:19,595 static members in our classes in most 18863 14:13:17,116 --> 14:13:22,240 cases a definition can't really show up 18864 14:13:19,595 --> 14:13:24,955 more than once in your entire c-plus 18865 14:13:22,239 --> 14:13:27,680 plus program but we have exceptions to 18866 14:13:24,955 --> 14:13:30,319 this rule and the exception is for 18867 14:13:27,680 --> 14:13:34,000 classes a definition for a class can 18868 14:13:30,319 --> 14:13:35,360 show up in multiple translation unit and 18869 14:13:35,360 --> 14:13:39,915 we need to create objects of those 18870 14:13:37,360 --> 14:13:42,239 classes so each translation unit really 18871 14:13:39,915 --> 14:13:45,115 needs to see the definition for the 18872 14:13:42,239 --> 14:13:47,839 class and we are going to try and make 18873 14:13:45,116 --> 14:13:50,400 this super clear with examples once we 18874 14:13:47,839 --> 14:13:52,639 hit visual studio code okay so it is 18875 14:13:50,400 --> 14:13:55,275 time to head over to visual studio code 18876 14:13:52,639 --> 14:13:57,839 and shed some more light on the one 18877 14:13:55,275 --> 14:13:59,839 definition rule here we are in our 18878 14:13:57,839 --> 14:14:01,915 working folder the current project is 18879 14:13:59,839 --> 14:14:04,879 one definition rule we're going to grab 18880 14:14:01,915 --> 14:14:06,639 our template files like we always do 18881 14:14:04,879 --> 14:14:08,479 we're going to put them in place and 18882 14:14:06,639 --> 14:14:10,400 we're going to open this little guy in 18883 14:14:08,480 --> 14:14:12,000 visual studio code let's do that i'm 18884 14:14:10,400 --> 14:14:13,756 going to drag and drop and we're going 18885 14:14:12,000 --> 14:14:15,595 to have our main cpp file here we're 18886 14:14:13,756 --> 14:14:17,520 going to clean it up that's what we 18887 14:14:17,519 --> 14:14:22,079 and now we want to explore the one 18888 14:14:20,239 --> 14:14:24,639 definition rule in the context of 18889 14:14:22,080 --> 14:14:27,040 variables so we have a freestanding 18890 14:14:27,040 --> 14:14:30,720 and that's going to be the same variable 18891 14:14:28,800 --> 14:14:33,116 we had in the last lecture we're going 18892 14:14:30,720 --> 14:14:36,080 to initialize this and this is really 18893 14:14:33,116 --> 14:14:38,000 good we can use it we can go in main and 18894 14:14:36,080 --> 14:14:39,116 use it however we want we can print the 18895 14:14:39,116 --> 14:14:43,916 and if we won't run this program it is 18896 14:14:41,360 --> 14:14:46,955 going to work but the one definition 18897 14:14:43,915 --> 14:14:50,159 rule says we can't have more than one 18898 14:14:46,955 --> 14:14:52,080 definition for anything and for this 18899 14:14:50,160 --> 14:14:54,320 variable here we have seen that this is 18900 14:14:54,319 --> 14:14:59,595 and a definition so we can't have more 18901 14:14:57,040 --> 14:15:02,160 than one definition for the weight name 18902 14:14:59,595 --> 14:15:04,479 here for example if we try and grab it 18903 14:15:02,160 --> 14:15:06,240 and copy and paste it here so we're 18904 14:15:04,480 --> 14:15:08,560 going to try and build this program 18905 14:15:08,559 --> 14:15:12,115 we're going to see that we have a 18906 14:15:09,756 --> 14:15:13,756 problem and the problem is going to say 18907 14:15:13,756 --> 14:15:18,400 of double weight and it is not going to 18908 14:15:16,319 --> 14:15:20,639 help even if you take this and put that 18909 14:15:18,400 --> 14:15:22,319 in another file so let's create another 18910 14:15:20,639 --> 14:15:26,159 file to really show you that we're going 18911 14:15:22,319 --> 14:15:29,199 to call it some other file that cpp and 18912 14:15:26,160 --> 14:15:31,436 we're going to grab our redefinition 18913 14:15:31,436 --> 14:15:35,756 and you're going to see that if we try 18914 14:15:35,756 --> 14:15:39,200 we're going to get the same problem 18915 14:15:37,040 --> 14:15:40,879 we're going to have redefinition so 18916 14:15:40,879 --> 14:15:44,639 what is the problem here the compiler 18917 14:15:42,639 --> 14:15:47,595 error is a little bit different but it 18918 14:15:44,639 --> 14:15:48,400 is going to say multiple definitions of 18919 14:15:48,400 --> 14:15:53,916 and it's going to say one is defined in 18920 14:15:50,639 --> 14:15:56,400 the main cpp file so this is a linker 18921 14:15:53,915 --> 14:15:58,879 error this was caught by the laker 18922 14:15:56,400 --> 14:16:01,520 because the redefinition is showing up 18923 14:15:58,879 --> 14:16:03,755 in multiple files so the linker was 18924 14:16:01,519 --> 14:16:06,000 linking this program and it needed to 18925 14:16:03,756 --> 14:16:09,040 see a definition for weight and it had 18926 14:16:06,000 --> 14:16:10,955 seen one definition in the main cpp file 18927 14:16:09,040 --> 14:16:13,275 and it has seen another definition and 18928 14:16:10,955 --> 14:16:15,040 some other file and this is a problem 18929 14:16:13,275 --> 14:16:17,275 the linker doesn't know which one to 18930 14:16:15,040 --> 14:16:19,200 choose so it's going to throw an error 18931 14:16:17,275 --> 14:16:21,595 and you can't really have multiple 18932 14:16:19,199 --> 14:16:23,839 definitions for a variable in your c 18933 14:16:21,595 --> 14:16:26,559 plus plus program and this is the one 18934 14:16:23,839 --> 14:16:28,559 definition rule and the context of 18935 14:16:26,559 --> 14:16:31,435 freestanding variables like the weight 18936 14:16:28,559 --> 14:16:34,319 here but this is the same in the context 18937 14:16:31,436 --> 14:16:36,639 of functions if we go down and put up 18938 14:16:34,319 --> 14:16:37,595 our add function here we're going to say 18939 14:16:38,400 --> 14:16:43,756 and double b here and we're going to put 18940 14:16:40,955 --> 14:16:45,275 the definition below the main cpp file 18941 14:16:45,275 --> 14:16:49,915 we can copy this and put this in the 18942 14:16:48,160 --> 14:16:51,436 code here i'm going to remove this 18943 14:16:49,915 --> 14:16:53,435 little thing we don't want it we're 18944 14:16:51,436 --> 14:16:56,160 going to put in our body and we're going 18945 14:16:53,436 --> 14:16:58,720 to return a plus b here okay so this is 18946 14:16:56,160 --> 14:17:00,956 our function it is working we can take 18947 14:16:58,720 --> 14:17:03,275 out what we had here because it has 18948 14:17:00,955 --> 14:17:05,756 served its purpose you can say double 18949 14:17:03,275 --> 14:17:08,239 result and we're going to add up 10 and 18950 14:17:05,756 --> 14:17:10,955 20 like we did in the last lecture 18951 14:17:08,239 --> 14:17:12,720 no problem and we can try and print out 18952 14:17:12,720 --> 14:17:16,639 if we build and run this program it is 18953 14:17:14,559 --> 14:17:19,756 going to work without a problem because 18954 14:17:16,639 --> 14:17:20,720 we only have one definition for the add 18955 14:17:20,720 --> 14:17:25,756 what is the problem here we have weight 18956 14:17:23,519 --> 14:17:27,519 defined multiple times we're going to 18957 14:17:25,756 --> 14:17:28,955 comment out one the definition for 18958 14:17:28,955 --> 14:17:33,116 and we're going to build again 18959 14:17:31,360 --> 14:17:35,360 now the build is good and we can run 18960 14:17:35,360 --> 14:17:39,520 kill our terminal here and bring up 18961 14:17:37,519 --> 14:17:42,399 another one and run rooster you're going 18962 14:17:39,519 --> 14:17:45,199 to see that the result is 30 and the 18963 14:17:42,400 --> 14:17:47,360 program is working just fine but if we 18964 14:17:45,199 --> 14:17:50,799 set up another definition for add even 18965 14:17:47,360 --> 14:17:52,800 if we put this in the same file 18966 14:17:50,800 --> 14:17:55,596 we're going to get a problem because we 18967 14:17:52,800 --> 14:17:57,116 are violating the one definition rule 18968 14:17:57,116 --> 14:18:00,319 you're going to see the problem here 18969 14:17:58,480 --> 14:18:02,720 we're going to see redefinition 18970 14:18:00,319 --> 14:18:04,559 redefinition of the add function this is 18971 14:18:02,720 --> 14:18:07,200 not allowed your program is not going to 18972 14:18:04,559 --> 14:18:08,955 compile we see the compiler error here 18973 14:18:07,199 --> 14:18:11,199 even if you put the definition in 18974 14:18:08,955 --> 14:18:13,360 another file that's not going to help 18975 14:18:11,199 --> 14:18:15,755 we're going to take this and put this in 18976 14:18:15,756 --> 14:18:19,436 and try to compile the program 18977 14:18:20,400 --> 14:18:24,319 we're going to get a linker error and it 18978 14:18:22,319 --> 14:18:27,116 is going to say multiple definition of 18979 14:18:24,319 --> 14:18:29,436 the add function and this is not allowed 18980 14:18:27,116 --> 14:18:31,680 this is a linker error in this time if 18981 14:18:29,436 --> 14:18:33,596 we comment out one definition let's do 18982 14:18:31,680 --> 14:18:35,680 that we're going to comment out the one 18983 14:18:33,595 --> 14:18:38,799 in the main function this is going to 18984 14:18:35,680 --> 14:18:41,360 work and we want to be violating the one 18985 14:18:38,800 --> 14:18:43,436 definition rule so this is good the 18986 14:18:43,436 --> 14:18:48,000 and run rooster we're going to see our 18987 14:18:45,360 --> 14:18:50,559 result here so the one definition rule 18988 14:18:48,000 --> 14:18:51,360 says you can't have multiple definitions 18989 14:18:51,360 --> 14:18:55,840 freestanding variables or even functions 18990 14:18:54,239 --> 14:18:58,079 and reserve that if you do that you're 18991 14:18:55,839 --> 14:19:00,399 going to get a compiler error let's look 18992 14:18:58,080 --> 14:19:01,840 at this in the context of classes we're 18993 14:19:00,400 --> 14:19:04,080 going to do a class we're going to say 18994 14:19:01,839 --> 14:19:06,639 struct and we're going to call this 18995 14:19:06,639 --> 14:19:10,479 mostly we're going to call this point 18996 14:19:08,879 --> 14:19:13,199 and we're going to put in a few member 18997 14:19:10,480 --> 14:19:15,436 variables one is going to be mx the 18998 14:19:15,436 --> 14:19:18,319 and this is going to be our definition 18999 14:19:18,319 --> 14:19:23,116 but what you're going to see 19000 14:19:20,480 --> 14:19:26,080 is that it is possible to have multiple 19001 14:19:23,116 --> 14:19:28,480 definitions for classes for example if 19002 14:19:28,480 --> 14:19:32,319 have a copy of this in another file 19003 14:19:30,800 --> 14:19:34,880 we're going to do that we're going to 19004 14:19:34,879 --> 14:19:39,275 and we're going to try and build this 19005 14:19:36,839 --> 14:19:41,199 program you're going to see that it is 19006 14:19:39,275 --> 14:19:44,079 working we have multiple definitions of 19007 14:19:41,199 --> 14:19:46,079 these guys we can go in main for example 19008 14:19:47,199 --> 14:19:51,360 and we can up weld and see that the 19009 14:19:49,116 --> 14:19:54,319 program is going to work okay so if you 19010 14:19:51,360 --> 14:19:56,160 go in here for example and try to print 19011 14:19:56,160 --> 14:20:01,116 p1 and the print x we're going to do 19012 14:19:58,559 --> 14:20:03,040 that we're going to say p1 mx we're 19013 14:20:01,116 --> 14:20:05,360 going to be able to use this because 19014 14:20:03,040 --> 14:20:08,000 this is a struct the member variables 19015 14:20:05,360 --> 14:20:09,520 are public by default we can do this 19016 14:20:13,116 --> 14:20:20,400 p1y my let's do that and we can say 19017 14:20:16,800 --> 14:20:23,840 stdndl what is the problem here we need 19018 14:20:20,400 --> 14:20:25,596 to put our output stream operator 19019 14:20:23,839 --> 14:20:26,559 i think now the compiler is going to be 19020 14:20:30,400 --> 14:20:34,000 we're going to see that if we run the 19021 14:20:31,915 --> 14:20:36,159 program we're going to see our things 19022 14:20:34,000 --> 14:20:38,400 here and we have some junk values inside 19023 14:20:36,160 --> 14:20:40,800 because these things are not initialized 19024 14:20:38,400 --> 14:20:43,116 but if we initialize them to zero using 19025 14:20:40,800 --> 14:20:45,276 the braced initialization here 19026 14:20:45,275 --> 14:20:51,360 and we should see zero inside so let's 19027 14:20:47,839 --> 14:20:53,679 clear and run rooster now we have 0 in x 19028 14:20:51,360 --> 14:20:55,595 and y for our object here this is 19029 14:20:53,680 --> 14:20:58,639 working fine and we have multiple 19030 14:20:55,595 --> 14:21:01,199 definitions for our class but the 19031 14:20:58,639 --> 14:21:03,915 definitions can't show up in a single 19032 14:21:01,199 --> 14:21:06,000 translation unit for example if we took 19033 14:21:06,000 --> 14:21:10,160 and brought that back in main and put 19034 14:21:10,160 --> 14:21:15,040 our structure here if we try to weld the 19035 14:21:13,199 --> 14:21:17,755 compiler is going to complain because 19036 14:21:15,040 --> 14:21:20,879 now we have multiple definitions for the 19037 14:21:17,756 --> 14:21:22,319 same class in the same translation unit 19038 14:21:22,319 --> 14:21:27,756 we can have multiple definitions but the 19039 14:21:24,800 --> 14:21:29,680 definitions have to show up in different 19040 14:21:27,756 --> 14:21:31,436 translation units and that's what i want 19041 14:21:31,436 --> 14:21:36,319 redefinition of our point struct and it 19042 14:21:34,239 --> 14:21:38,639 is not good it is going to violate the 19043 14:21:36,319 --> 14:21:40,800 one definition rule okay now that you 19044 14:21:38,639 --> 14:21:43,040 have seen this we also want to see this 19045 14:21:43,040 --> 14:21:47,040 that also has some static member 19046 14:21:45,116 --> 14:21:49,200 variables we're going to take this out 19047 14:21:47,040 --> 14:21:51,840 because we have already seen this and 19048 14:21:49,199 --> 14:21:54,079 we're going to bring in our person class 19049 14:21:51,839 --> 14:21:56,239 like we used in the last lecture we're 19050 14:21:54,080 --> 14:21:59,040 going to add in a new header file it's 19051 14:22:02,879 --> 14:22:09,360 what did we do is this folder let's 19052 14:22:06,239 --> 14:22:11,436 remove this we didn't want a folder 19053 14:22:09,360 --> 14:22:13,360 we're going to delete this folder we 19054 14:22:13,360 --> 14:22:18,879 and we're going to just add a file 19055 14:22:18,879 --> 14:22:22,639 that's all file here and we're going to 19056 14:22:20,559 --> 14:22:24,479 put in the content the content is going 19057 14:22:22,639 --> 14:22:26,720 to be our personal class let's close 19058 14:22:24,480 --> 14:22:29,596 this so that you can see more of this 19059 14:22:26,720 --> 14:22:32,080 class here okay so we have our person 19060 14:22:29,595 --> 14:22:34,720 class here we have our include card we 19061 14:22:32,080 --> 14:22:36,480 have our person class definition we have 19062 14:22:34,720 --> 14:22:37,916 a constructor and we have a printer 19063 14:22:36,480 --> 14:22:40,080 phone function we have our member 19064 14:22:37,915 --> 14:22:42,319 variables and down below here you see 19065 14:22:40,080 --> 14:22:44,480 that we have a static variable which is 19066 14:22:42,319 --> 14:22:47,040 person account we need to put in the 19067 14:22:44,480 --> 14:22:49,520 definition for these guys so let's go 19068 14:22:47,040 --> 14:22:51,520 down and do that we're going to put in 19069 14:22:51,519 --> 14:22:56,559 constructor we're going to go on top 19070 14:22:53,519 --> 14:22:58,239 here and include our person header file 19071 14:22:56,559 --> 14:23:00,159 and we're going to put in place our 19072 14:22:58,239 --> 14:23:03,040 constructor we can go down and do that 19073 14:23:00,160 --> 14:23:04,956 we can say person and say person 19074 14:23:03,040 --> 14:23:07,436 that's going to be our constructor we're 19075 14:23:04,955 --> 14:23:09,595 going to go back to the class and grab 19076 14:23:07,436 --> 14:23:11,680 the parameters because i don't want to 19077 14:23:11,680 --> 14:23:14,800 so let's do that we're going to put them 19078 14:23:14,800 --> 14:23:19,840 we are going to put in our initializer 19079 14:23:17,436 --> 14:23:22,160 list i think we should also delete a 19080 14:23:19,839 --> 14:23:24,399 parenthesis here we're going to go down 19081 14:23:22,160 --> 14:23:26,240 and put in our initializer list we're 19082 14:23:24,400 --> 14:23:28,720 going to say full name we're going to 19083 14:23:26,239 --> 14:23:32,239 initialize this with name param 19084 14:23:28,720 --> 14:23:34,000 and we're going to also put in our age 19085 14:23:32,239 --> 14:23:36,079 and we're going to initialize this with 19086 14:23:34,000 --> 14:23:39,040 edge param the parameter we have from 19087 14:23:36,080 --> 14:23:41,916 the function and this is going to be all 19088 14:23:39,040 --> 14:23:43,680 inside the body we're going to implement 19089 14:23:41,915 --> 14:23:46,159 the static variable we're going to say 19090 14:23:43,680 --> 14:23:48,800 increment person count okay now select 19091 14:23:46,160 --> 14:23:51,360 that here and this is going to be good 19092 14:23:48,800 --> 14:23:52,956 we also need to initialize our static 19093 14:23:51,360 --> 14:23:56,160 variables so we're going to say ends 19094 14:23:52,955 --> 14:23:58,159 person person account let's do person 19095 14:23:56,160 --> 14:24:00,240 account and we're going to initialize 19096 14:23:58,160 --> 14:24:03,436 this to maybe eight like we did in the 19097 14:24:00,239 --> 14:24:05,915 last lecture and this should be all we 19098 14:24:03,436 --> 14:24:08,480 need here so if we build and run this 19099 14:24:05,915 --> 14:24:10,799 program i think it's going to run 19100 14:24:08,480 --> 14:24:12,640 let's select the main function here 19101 14:24:10,800 --> 14:24:14,400 and what problem do we have here let's 19102 14:24:12,639 --> 14:24:16,800 build and see we're going to let the 19103 14:24:16,800 --> 14:24:20,639 we're going to see that the world is 19104 14:24:18,239 --> 14:24:22,479 good and we can create person objects 19105 14:24:20,639 --> 14:24:24,639 and really use them in our program so 19106 14:24:22,480 --> 14:24:27,276 let's come down here and delete whatever 19107 14:24:24,639 --> 14:24:28,800 we had we're going to include the person 19108 14:24:27,275 --> 14:24:30,239 and we're going to go down here and 19109 14:24:28,800 --> 14:24:32,160 create a person object we're going to 19110 14:24:32,160 --> 14:24:37,040 p1 and we're going to specify the name 19111 14:24:37,040 --> 14:24:41,116 and we're going to say that they are 19112 14:24:41,116 --> 14:24:45,756 and we can preach information about this 19113 14:24:42,955 --> 14:24:48,000 person we can say person principle and 19114 14:24:45,756 --> 14:24:50,000 if we try to build this program let's 19115 14:24:50,000 --> 14:24:54,480 the world is going to be good as you see 19116 14:24:54,480 --> 14:24:58,080 and run rooster we're going to see jon 19117 14:24:58,080 --> 14:25:02,955 35 years old so this is working just 19118 14:25:02,955 --> 14:25:09,199 multiple definitions for the same class 19119 14:25:06,400 --> 14:25:11,040 in the same translation unit here for 19120 14:25:09,199 --> 14:25:13,680 example if we come in the main function 19121 14:25:11,040 --> 14:25:15,840 notice we are including a person so this 19122 14:25:13,680 --> 14:25:19,199 is bringing in the definition for a 19123 14:25:15,839 --> 14:25:21,275 person we have in our person.h here if 19124 14:25:19,199 --> 14:25:22,799 we also try to declare another class 19125 14:25:22,800 --> 14:25:28,400 even if we don't put anything in the 19126 14:25:25,915 --> 14:25:31,040 compiler is going to complain because we 19127 14:25:28,400 --> 14:25:34,720 have two definitions for the same name 19128 14:25:31,040 --> 14:25:36,319 which is person here if we try to build 19129 14:25:34,720 --> 14:25:38,480 i think we are going to get a compiler 19130 14:25:36,319 --> 14:25:41,040 error saying that we have a definition 19131 14:25:38,480 --> 14:25:42,880 you see we have a redefinition of person 19132 14:25:41,040 --> 14:25:45,360 so this is not allowed we can't really 19133 14:25:42,879 --> 14:25:48,719 do this but notice that this person 19134 14:25:45,360 --> 14:25:50,800 class is included both in some other 19135 14:25:48,720 --> 14:25:54,160 file which is a separate translation 19136 14:25:50,800 --> 14:25:56,319 unit and main we have it included here 19137 14:25:54,160 --> 14:25:58,956 so it is really in a separate 19138 14:25:56,319 --> 14:26:01,116 translation unit we have the definition 19139 14:25:58,955 --> 14:26:03,519 for the class in separate translation 19140 14:26:01,116 --> 14:26:05,680 units and that's not a problem but if 19141 14:26:03,519 --> 14:26:09,039 you try and put the same definition 19142 14:26:05,680 --> 14:26:10,955 multiple times in a single translation 19143 14:26:09,040 --> 14:26:13,360 unit you're going to get a problem and 19144 14:26:10,955 --> 14:26:16,080 you need to be aware of that what i want 19145 14:26:13,360 --> 14:26:18,559 you to see right here is that you can't 19146 14:26:16,080 --> 14:26:22,400 also have multiple definitions 19147 14:26:18,559 --> 14:26:25,595 for your static variables and the member 19148 14:26:22,400 --> 14:26:28,160 functions of your class for example if 19149 14:26:28,160 --> 14:26:32,880 and also put that in our main function 19150 14:26:30,559 --> 14:26:35,115 we can do that why not we're going to 19151 14:26:32,879 --> 14:26:37,595 come in our main function here and we're 19152 14:26:35,116 --> 14:26:39,040 going to put the same definitions here 19153 14:26:37,595 --> 14:26:40,955 now we're going to have a problem 19154 14:26:39,040 --> 14:26:43,680 because the linker is going to see two 19155 14:26:40,955 --> 14:26:45,680 definitions for the static variable and 19156 14:26:43,680 --> 14:26:47,275 the constructor for a person here and 19157 14:26:45,680 --> 14:26:49,840 this is going to be a problem you can't 19158 14:26:47,275 --> 14:26:50,955 really do that if we build we're going 19159 14:26:50,955 --> 14:26:55,436 a problem we're going to see 19160 14:26:55,436 --> 14:26:59,756 we have multiple definitions for the 19161 14:26:57,199 --> 14:27:02,479 constructor and we have multiple 19162 14:26:59,756 --> 14:27:04,559 definitions for person account and this 19163 14:27:02,480 --> 14:27:07,200 is not allowed you can't really do that 19164 14:27:04,559 --> 14:27:09,839 you can either remove the one from some 19165 14:27:07,199 --> 14:27:11,115 other file.cpp for example if we comment 19166 14:27:11,116 --> 14:27:16,160 let's do that we can come on top here 19167 14:27:13,915 --> 14:27:18,399 and comment this out and try to build 19168 14:27:16,160 --> 14:27:20,720 the program now the program is going to 19169 14:27:18,400 --> 14:27:22,800 work because we have one definition for 19170 14:27:20,720 --> 14:27:25,360 these guys you see the world is good we 19171 14:27:22,800 --> 14:27:28,000 can even run the program to show you 19172 14:27:25,360 --> 14:27:31,116 that it works or we can remove the one 19173 14:27:28,000 --> 14:27:33,680 in the main cpp file and we can leave 19174 14:27:31,116 --> 14:27:36,560 this alive so let's go back in the cpp 19175 14:27:33,680 --> 14:27:38,639 file the main cpp file i should say and 19176 14:27:36,559 --> 14:27:40,799 remove this and if we work we're going 19177 14:27:38,639 --> 14:27:42,479 to see that this is going to work and 19178 14:27:42,480 --> 14:27:49,040 compliant with what the c plus plus 19179 14:27:45,275 --> 14:27:50,720 standard expects us to do in our c plus 19180 14:27:49,040 --> 14:27:52,480 plus programs if we're around we're 19181 14:27:50,720 --> 14:27:54,400 going to see that this is going to work 19182 14:27:52,480 --> 14:27:56,956 and this is really cool this is really 19183 14:27:54,400 --> 14:27:59,275 all i wanted you to see in this lecture 19184 14:27:56,955 --> 14:28:02,879 the one definition rule which says that 19185 14:27:59,275 --> 14:28:06,079 you can't have multiple definitions for 19186 14:28:02,879 --> 14:28:09,360 the same name in your entire c plus plus 19187 14:28:06,080 --> 14:28:12,240 program but for classes we can violate 19188 14:28:09,360 --> 14:28:14,800 this rule a little bit and make the same 19189 14:28:12,239 --> 14:28:17,275 definition show up in multiple 19190 14:28:14,800 --> 14:28:19,276 translation units we are going to stop 19191 14:28:17,275 --> 14:28:22,000 here in this lecture in the next one 19192 14:28:19,275 --> 14:28:24,319 we're going to learn about linkage 19193 14:28:22,000 --> 14:28:25,360 go ahead and finish up here and make me 19194 14:28:25,360 --> 14:28:29,520 in this lecture we're going to learn 19195 14:28:26,800 --> 14:28:31,756 about functions a function is a reusable 19196 14:28:29,519 --> 14:28:35,435 piece of code that you can use multiple 19197 14:28:31,756 --> 14:28:38,400 times to do different things it can take 19198 14:28:35,436 --> 14:28:40,880 input and once it has input it can do 19199 14:28:38,400 --> 14:28:43,756 some processing and when the processing 19200 14:28:40,879 --> 14:28:46,079 is done the function can optionally give 19201 14:28:43,756 --> 14:28:48,480 you a result you can think of a function 19202 14:28:46,080 --> 14:28:50,955 as a machine which can take input and 19203 14:28:48,480 --> 14:28:52,480 give you some output when you give input 19204 14:28:50,955 --> 14:28:54,799 to the machine the machine is going to 19205 14:28:52,480 --> 14:28:57,520 do some processing and then it's going 19206 14:28:54,800 --> 14:28:59,680 to give you the output and the output is 19207 14:28:57,519 --> 14:29:01,595 going to be dependent on the input that 19208 14:28:59,680 --> 14:29:04,239 you gave to the machine this is really 19209 14:29:01,595 --> 14:29:06,399 how a function works in c plus plus and 19210 14:29:04,239 --> 14:29:09,115 once you have the machine set up you can 19211 14:29:06,400 --> 14:29:11,275 really reuse it however many times you 19212 14:29:09,116 --> 14:29:13,680 want let's look at how a function looks 19213 14:29:11,275 --> 14:29:16,239 in c plus plus the syntax is this you 19214 14:29:13,680 --> 14:29:18,879 first specify the return type which may 19215 14:29:16,239 --> 14:29:20,720 be and double or whatever after that 19216 14:29:18,879 --> 14:29:21,680 you're going to specify the function 19217 14:29:21,680 --> 14:29:25,756 and after that you're going to have a 19218 14:29:23,519 --> 14:29:27,275 pair of parentheses inside this 19219 14:29:25,756 --> 14:29:29,520 parenthesis you're going to have your 19220 14:29:27,275 --> 14:29:32,319 parameters which is going to be a comma 19221 14:29:29,519 --> 14:29:35,435 separated list of things you pass to the 19222 14:29:32,319 --> 14:29:37,756 function and after all these parameters 19223 14:29:35,436 --> 14:29:40,639 you're going to have a pair of curly 19224 14:29:37,756 --> 14:29:42,319 braces and inside those curly braces 19225 14:29:40,639 --> 14:29:44,319 we're going to have the body of the 19226 14:29:42,319 --> 14:29:46,639 function we're going to be doing our 19227 14:29:44,319 --> 14:29:49,680 processing in the body and when the 19228 14:29:46,639 --> 14:29:52,479 processing is done we can get the result 19229 14:29:49,680 --> 14:29:55,040 of the function through this return 19230 14:29:52,480 --> 14:29:57,200 mechanism here so if we want to get 19231 14:29:55,040 --> 14:29:59,520 something out of the function we have to 19232 14:29:57,199 --> 14:30:03,115 return that thing and it is going to be 19233 14:29:59,519 --> 14:30:05,435 visible to whoever used this function 19234 14:30:03,116 --> 14:30:08,480 here is a simple example of a function 19235 14:30:05,436 --> 14:30:10,720 with no input and output the syntax is 19236 14:30:08,480 --> 14:30:12,800 going to be this void means that this 19237 14:30:10,720 --> 14:30:15,275 function is not going to return anything 19238 14:30:12,800 --> 14:30:18,480 and you notice that our pair of 19239 14:30:15,275 --> 14:30:21,275 parentheses is empty because we have no 19240 14:30:18,480 --> 14:30:24,000 input in this function we have our curly 19241 14:30:21,275 --> 14:30:25,839 braces and inside these curly braces is 19242 14:30:24,000 --> 14:30:28,160 going to be the body of this function 19243 14:30:25,839 --> 14:30:30,719 it's going to do some processing and 19244 14:30:28,160 --> 14:30:33,116 when processing is done we return out of 19245 14:30:30,720 --> 14:30:34,955 this function now remember that i said 19246 14:30:33,116 --> 14:30:39,116 that this function doesn't return 19247 14:30:34,955 --> 14:30:41,116 anything because it is of void type here 19248 14:30:39,116 --> 14:30:43,680 but we can return and i'm going to show 19249 14:30:41,116 --> 14:30:46,080 you the exact syntax of how you can do 19250 14:30:43,680 --> 14:30:48,400 that okay so we can have many functions 19251 14:30:46,080 --> 14:30:50,560 in c plus plus but each function has to 19252 14:30:48,400 --> 14:30:53,040 be unique otherwise you're going to have 19253 14:30:50,559 --> 14:30:55,756 problems in your c plus plus programs a 19254 14:30:53,040 --> 14:30:58,080 function is going to be unique based on 19255 14:30:55,756 --> 14:31:00,000 its signature and the signature is 19256 14:31:00,000 --> 14:31:05,199 plus the function parameters if we go 19257 14:31:02,639 --> 14:31:06,955 back to our example here and try to 19258 14:31:05,199 --> 14:31:10,000 figure out the signature of this 19259 14:31:06,955 --> 14:31:12,559 function it's going to be function name 19260 14:31:10,000 --> 14:31:15,275 and plus the parameters and you see that 19261 14:31:12,559 --> 14:31:18,399 we have no parameters in here so this 19262 14:31:15,275 --> 14:31:21,519 signature is going to uniquely identify 19263 14:31:18,400 --> 14:31:23,436 this function in our c plus plus program 19264 14:31:21,519 --> 14:31:25,680 if we set up another function that has 19265 14:31:23,436 --> 14:31:27,436 this same function name and doesn't take 19266 14:31:25,680 --> 14:31:30,160 any parameter we're going to have 19267 14:31:27,436 --> 14:31:33,200 problems even if we happen to have a 19268 14:31:30,160 --> 14:31:35,360 different return value for the function 19269 14:31:33,199 --> 14:31:38,159 and don't worry if this is not making 19270 14:31:35,360 --> 14:31:40,319 sense yet we will see a lot of examples 19271 14:31:38,160 --> 14:31:42,560 and i am sure by the time we are done 19272 14:31:40,319 --> 14:31:45,040 with this lecture you will be a master 19273 14:31:42,559 --> 14:31:47,435 at using functions okay now that we have 19274 14:31:45,040 --> 14:31:49,520 seen how we can declare or define a 19275 14:31:47,436 --> 14:31:51,200 function in c plus plus let's see how we 19276 14:31:51,199 --> 14:31:55,519 and when you call a function in most 19277 14:31:53,199 --> 14:31:58,479 cases you will want to get something out 19278 14:31:55,519 --> 14:32:00,639 of that function so suppose here result 19279 14:31:58,480 --> 14:32:02,319 var is a variable that is going to catch 19280 14:32:02,319 --> 14:32:05,680 and we're going to call the function 19281 14:32:03,680 --> 14:32:08,160 with the syntax like this we're going to 19282 14:32:05,680 --> 14:32:11,199 say the function name we're going to put 19283 14:32:08,160 --> 14:32:13,680 our pair of parentheses here and we are 19284 14:32:11,199 --> 14:32:16,559 going to pass in the arguments or of the 19285 14:32:13,680 --> 14:32:18,800 values of our parameters this is the 19286 14:32:16,559 --> 14:32:20,479 syntax it is going to call the function 19287 14:32:18,800 --> 14:32:24,160 and the result of the function is going 19288 14:32:20,480 --> 14:32:26,800 to be assigned to this result var 19289 14:32:24,160 --> 14:32:28,956 variable of hours here is how you can 19290 14:32:26,800 --> 14:32:31,040 call a function that doesn't take any 19291 14:32:28,955 --> 14:32:33,360 parameter the parameter list is 19292 14:32:31,040 --> 14:32:35,436 basically going to be empty and now 19293 14:32:33,360 --> 14:32:38,239 we're going to look at a few examples of 19294 14:32:35,436 --> 14:32:40,480 how you can really declare functions in 19295 14:32:38,239 --> 14:32:43,199 your c plus plus program for example 19296 14:32:40,480 --> 14:32:45,360 here we have a function called into bar 19297 14:32:43,199 --> 14:32:47,595 it doesn't return anything so we can't 19298 14:32:45,360 --> 14:32:50,160 catch the result of this function and 19299 14:32:47,595 --> 14:32:53,115 it's going to have one input in the form 19300 14:32:50,160 --> 14:32:56,080 of an integer so the parameter is called 19301 14:32:53,116 --> 14:32:58,720 h here it's an integer and inside the 19302 14:32:56,080 --> 14:33:02,080 function we are going to test if 19303 14:32:58,720 --> 14:33:04,319 somebody is old enough to enter a bar so 19304 14:33:02,080 --> 14:33:06,639 we do a test to see if the age is 19305 14:33:04,319 --> 14:33:10,559 greater than 18 if it is we're going to 19306 14:33:06,639 --> 14:33:12,479 tell them please proceed f the h is not 19307 14:33:10,559 --> 14:33:15,040 greater than 18 we're going to tell them 19308 14:33:12,480 --> 14:33:16,956 you cannot enter this is an example of a 19309 14:33:15,040 --> 14:33:19,275 function you can set up in your c plus 19310 14:33:16,955 --> 14:33:21,040 plus programs here is another function 19311 14:33:19,275 --> 14:33:23,595 that is going to compute the maximum 19312 14:33:21,040 --> 14:33:26,239 between two numbers it is going to give 19313 14:33:23,595 --> 14:33:29,199 us a result so the return value is an 19314 14:33:26,239 --> 14:33:31,199 integer here the function name is max 19315 14:33:29,199 --> 14:33:33,839 and we're going to put in our list of 19316 14:33:31,199 --> 14:33:36,719 parameters and again these are the 19317 14:33:33,839 --> 14:33:37,755 parameters when you are defining your 19318 14:33:37,756 --> 14:33:42,955 this comma separated list of things we 19319 14:33:40,319 --> 14:33:45,040 have in this parenthesis are called 19320 14:33:42,955 --> 14:33:47,839 parameters and when you call the 19321 14:33:45,040 --> 14:33:50,319 function the values that you pass in the 19322 14:33:47,839 --> 14:33:53,039 places of these parameters are called 19323 14:33:50,319 --> 14:33:55,275 arguments so these are a few terms that 19324 14:33:53,040 --> 14:33:58,000 can really throw you off and i just want 19325 14:33:55,275 --> 14:33:59,595 you to be super clear about these things 19326 14:33:58,000 --> 14:34:01,839 once we have the signature of the 19327 14:33:59,595 --> 14:34:03,680 function set up we jump in the body and 19328 14:34:01,839 --> 14:34:06,239 we do whatever we want this function to 19329 14:34:03,680 --> 14:34:08,639 do so we're going to say if a is greater 19330 14:34:06,239 --> 14:34:11,680 than b then a must be the maximum we're 19331 14:34:08,639 --> 14:34:14,000 going to send it that back to whoever 19332 14:34:11,680 --> 14:34:15,840 called this function or in other words 19333 14:34:15,839 --> 14:34:20,795 and if a is not greater than b then b 19334 14:34:18,879 --> 14:34:22,795 must be the maximum and we're going to 19335 14:34:20,796 --> 14:34:24,880 return it that's what we're saying here 19336 14:34:22,796 --> 14:34:27,680 here is another example of a function 19337 14:34:24,879 --> 14:34:30,319 that doesn't have any output and doesn't 19338 14:34:27,680 --> 14:34:33,360 take any input it doesn't return 19339 14:34:30,319 --> 14:34:35,680 anything because the return type is void 19340 14:34:33,360 --> 14:34:38,720 and it doesn't take any input because 19341 14:34:35,680 --> 14:34:40,639 the parameter list here is empty and the 19342 14:34:38,720 --> 14:34:43,360 body of the function we're just going to 19343 14:34:40,639 --> 14:34:44,955 greet somebody and tell them hello there 19344 14:34:43,360 --> 14:34:48,000 and we're going to return out of the 19345 14:34:44,955 --> 14:34:50,955 function notice that for avoid function 19346 14:34:48,000 --> 14:34:53,519 we can either omit the return statement 19347 14:34:50,955 --> 14:34:55,915 here or we can just put it in like this 19348 14:34:53,519 --> 14:34:58,079 say return and the semicolon and 19349 14:34:55,915 --> 14:35:01,040 whatever c plus plus compiler you are 19350 14:34:58,080 --> 14:35:02,480 using is going to accept this as valid c 19351 14:35:02,480 --> 14:35:07,756 here is another example of a function 19352 14:35:04,639 --> 14:35:10,000 that returns something but doesn't take 19353 14:35:07,756 --> 14:35:12,955 any input the return value is going to 19354 14:35:10,000 --> 14:35:14,879 be an integer but we don't need to pass 19355 14:35:12,955 --> 14:35:17,275 something to this function when we call 19356 14:35:14,879 --> 14:35:20,239 it it's just going to give us output we 19357 14:35:17,275 --> 14:35:22,879 can call it and get our lucky number and 19358 14:35:20,239 --> 14:35:25,436 use that however we want in our c plus 19359 14:35:22,879 --> 14:35:27,680 plus application i think we have seen a 19360 14:35:25,436 --> 14:35:30,319 lot of examples of how we can set up 19361 14:35:27,680 --> 14:35:32,796 functions now let's see how we can call 19362 14:35:30,319 --> 14:35:34,796 these functions here is a simple c plus 19363 14:35:32,796 --> 14:35:37,276 plus program in the main function we 19364 14:35:34,796 --> 14:35:40,400 just set up a few variables a value b 19365 14:35:37,275 --> 14:35:42,955 value a and b here and we use those 19366 14:35:40,400 --> 14:35:45,360 variables to call functions remember we 19367 14:35:42,955 --> 14:35:47,436 set up an inter bar function the 19368 14:35:45,360 --> 14:35:50,400 function looked like this and it was 19369 14:35:47,436 --> 14:35:52,796 testing to see if somebody's age was 19370 14:35:50,400 --> 14:35:55,756 greater than 18. if the age is greater 19371 14:35:52,796 --> 14:35:58,319 than 18 they are going to enter the bar 19372 14:35:55,756 --> 14:36:01,116 if the edge is not greater than 18 we 19373 14:35:58,319 --> 14:36:03,680 won't let them end the function takes 19374 14:36:01,116 --> 14:36:05,680 input but it doesn't return anything so 19375 14:36:03,680 --> 14:36:07,840 we can call this function like we do 19376 14:36:07,839 --> 14:36:11,519 and you notice that we're not catching 19377 14:36:09,756 --> 14:36:13,595 something out of this function we're 19378 14:36:11,519 --> 14:36:16,879 just going to call it and we're going to 19379 14:36:13,595 --> 14:36:18,639 give it input which is 22 here and the 19380 14:36:16,879 --> 14:36:21,915 function is going to do its thing so 19381 14:36:18,639 --> 14:36:24,879 it's going to let us in because our age 19382 14:36:21,915 --> 14:36:28,399 is greater than 18. down here you see we 19383 14:36:24,879 --> 14:36:31,680 are calling the max function here we are 19384 14:36:28,400 --> 14:36:34,000 giving it two inputs a value and b value 19385 14:36:31,680 --> 14:36:36,080 and it's going to return something and 19386 14:36:34,000 --> 14:36:38,720 the return value of this function is 19387 14:36:36,080 --> 14:36:41,680 going to be used to initialize this 19388 14:36:38,720 --> 14:36:44,239 maximum number variable here this is 19389 14:36:41,680 --> 14:36:46,160 what we are doing if we go down we see 19390 14:36:44,239 --> 14:36:49,436 that we can call the maximum function 19391 14:36:46,160 --> 14:36:52,160 directly and use its return value in an 19392 14:36:49,436 --> 14:36:54,480 sddc out statement here and this is 19393 14:36:52,160 --> 14:36:55,360 going to print out the maximum between a 19394 14:36:55,360 --> 14:36:59,275 and down here you see we have the 19395 14:36:57,040 --> 14:37:01,436 function say hello which is going to 19396 14:36:59,275 --> 14:37:03,839 just greet somebody and not return 19397 14:37:01,436 --> 14:37:05,596 anything here is another example of how 19398 14:37:03,839 --> 14:37:07,359 you can call the lucky number you say 19399 14:37:05,595 --> 14:37:08,479 lucky number you pass a pair of 19400 14:37:08,480 --> 14:37:13,840 and this is going to return the lucky 19401 14:37:10,160 --> 14:37:16,880 number and print it out in this htdc out 19402 14:37:13,839 --> 14:37:19,839 statement here down here we set up a few 19403 14:37:16,879 --> 14:37:22,319 variables a and b and we are going to 19404 14:37:19,839 --> 14:37:25,115 use them to call the maximum functions 19405 14:37:22,319 --> 14:37:28,559 you see we call max a b or we can even 19406 14:37:25,116 --> 14:37:30,639 pass in number literals like we are 19407 14:37:30,639 --> 14:37:34,479 one thing i want you to be careful about 19408 14:37:32,400 --> 14:37:36,080 is implicit conversions that might 19409 14:37:34,480 --> 14:37:37,276 happen when you are calling you 19410 14:37:37,275 --> 14:37:42,079 suppose we have a minimum function like 19411 14:37:39,756 --> 14:37:45,595 we see here which is going to be taking 19412 14:37:42,080 --> 14:37:48,319 in integer parameters but here we are 19413 14:37:45,595 --> 14:37:51,435 calling the function with f and g 19414 14:37:48,319 --> 14:37:54,080 which happen to be of double type like 19415 14:37:51,436 --> 14:37:55,840 we see on top here so what is going to 19416 14:37:54,080 --> 14:37:58,240 happen is the compiler is going to 19417 14:37:55,839 --> 14:38:00,639 notice that what you are passing in is 19418 14:37:58,239 --> 14:38:03,360 not really what the function takes but 19419 14:38:00,639 --> 14:38:06,879 the compiler knows that it can insert a 19420 14:38:03,360 --> 14:38:09,040 transformation from double to integer 19421 14:38:06,879 --> 14:38:11,755 and it is going to do that without your 19422 14:38:09,040 --> 14:38:14,239 involvement so be really careful about 19423 14:38:11,756 --> 14:38:16,955 implicit conversions and make sure they 19424 14:38:14,239 --> 14:38:18,639 are working to your advantage otherwise 19425 14:38:16,955 --> 14:38:21,199 you're going to have really hard to 19426 14:38:18,639 --> 14:38:22,559 debug problems in your c plus plus 19427 14:38:22,559 --> 14:38:26,639 one other thing i want you to be aware 19428 14:38:24,400 --> 14:38:29,360 of is that if we have a function set up 19429 14:38:26,639 --> 14:38:33,199 like this this function is going to be 19430 14:38:29,360 --> 14:38:35,756 taking parameters as copies so if you 19431 14:38:33,199 --> 14:38:37,275 have two variables for example c and d 19432 14:38:35,756 --> 14:38:39,436 and you call this function with those 19433 14:38:37,275 --> 14:38:41,680 two variables you're not going to be 19434 14:38:39,436 --> 14:38:44,160 manipulating those two variables what 19435 14:38:41,680 --> 14:38:46,955 you will be working with inside the body 19436 14:38:44,160 --> 14:38:48,720 of this function are going to be copies 19437 14:38:46,955 --> 14:38:51,040 of the parameters that you passed in 19438 14:38:48,720 --> 14:38:53,200 here so if for example you happen to 19439 14:38:51,040 --> 14:38:55,680 increment either of the parameters for 19440 14:38:53,199 --> 14:38:56,955 example here we increment a and we 19441 14:38:56,955 --> 14:39:01,116 the changes we make here inside the body 19442 14:38:59,199 --> 14:39:03,435 of the function are just going to be 19443 14:39:01,116 --> 14:39:05,756 affecting copies we have inside the 19444 14:39:03,436 --> 14:39:08,796 function here and these copies are going 19445 14:39:05,756 --> 14:39:10,400 to die the moment we hit the end of this 19446 14:39:08,796 --> 14:39:12,560 function here this is something i want 19447 14:39:10,400 --> 14:39:15,200 you to be aware of and we will play with 19448 14:39:12,559 --> 14:39:17,680 that in more detail when we hit visual 19449 14:39:15,199 --> 14:39:19,275 studio code in a minute here is a simple 19450 14:39:17,680 --> 14:39:22,639 example of how you can call that 19451 14:39:19,275 --> 14:39:25,519 function we have two variables h and i 19452 14:39:22,639 --> 14:39:27,199 and we are going to print them out after 19453 14:39:25,519 --> 14:39:30,399 that we're going to call the function 19454 14:39:27,199 --> 14:39:33,039 we're going to store the result back in 19455 14:39:30,400 --> 14:39:35,200 mult results but the important thing is 19456 14:39:33,040 --> 14:39:36,239 that we call the function here with h 19457 14:39:36,239 --> 14:39:41,680 remember in the body of the function we 19458 14:39:38,160 --> 14:39:44,720 are incrementing h and i as arguments 19459 14:39:41,680 --> 14:39:46,879 but the changes we do inside of the body 19460 14:39:44,720 --> 14:39:50,080 of the function are not going to be 19461 14:39:46,879 --> 14:39:52,399 visible when the function returns if we 19462 14:39:50,080 --> 14:39:55,276 print h and i here we're just going to 19463 14:39:52,400 --> 14:39:56,400 see the same thing we saw before we call 19464 14:39:56,400 --> 14:40:00,796 i realize this is a lot of information 19465 14:39:58,400 --> 14:40:03,040 to take in now we are going to head over 19466 14:40:00,796 --> 14:40:05,200 to visual studio code and actually play 19467 14:40:05,199 --> 14:40:10,479 okay here we are in our working 19468 14:40:07,839 --> 14:40:12,879 directory the chapter is on functions 19469 14:40:10,480 --> 14:40:15,360 the current lecture is first hand on 19470 14:40:12,879 --> 14:40:18,000 functions we are going to grab our 19471 14:40:15,360 --> 14:40:19,756 template files like we always do we're 19472 14:40:19,756 --> 14:40:25,520 and we are going to open this in visual 19473 14:40:21,680 --> 14:40:27,595 studio code let's open the folder 19474 14:40:25,519 --> 14:40:28,719 and we are going to go in the correct 19475 14:40:28,720 --> 14:40:33,756 and choose first hand on functions here 19476 14:40:31,275 --> 14:40:36,720 this is what we want to open and we are 19477 14:40:33,756 --> 14:40:39,200 going to open our main cpp file and 19478 14:40:39,199 --> 14:40:42,795 and the first thing we want to do is to 19479 14:40:41,040 --> 14:40:45,680 set up a function that takes a single 19480 14:40:42,796 --> 14:40:48,080 parameter and doesn't give back any 19481 14:40:45,680 --> 14:40:50,480 result so the function is going to be 19482 14:40:48,080 --> 14:40:52,480 interbore we have seen that the function 19483 14:40:50,480 --> 14:40:54,560 is not going to return anything so it's 19484 14:40:52,480 --> 14:40:56,319 going to be void its return type is 19485 14:40:54,559 --> 14:40:59,115 going to be void the name of the 19486 14:40:56,319 --> 14:41:00,796 function is going to be interbar and 19487 14:40:59,116 --> 14:41:02,796 what we want to pass in here is a 19488 14:41:00,796 --> 14:41:05,360 variable to represent the age of 19489 14:41:02,796 --> 14:41:08,240 somebody and we know that the age can 19490 14:41:05,360 --> 14:41:11,680 really not be negative so it is 19491 14:41:08,239 --> 14:41:13,595 advised to at least use an unsigned 19492 14:41:11,680 --> 14:41:16,319 integer type so we're going to use 19493 14:41:13,595 --> 14:41:18,319 unsigned end and the parameter name is 19494 14:41:18,319 --> 14:41:22,879 once we have our parameter list we're 19495 14:41:20,559 --> 14:41:24,720 going to put in the function body and 19496 14:41:22,879 --> 14:41:26,795 remember the function body is going to 19497 14:41:28,879 --> 14:41:33,115 now this is a valid c plus plus function 19498 14:41:31,116 --> 14:41:34,796 it's not going to return anything it's 19499 14:41:33,116 --> 14:41:37,680 going to take a single parameter whose 19500 14:41:34,796 --> 14:41:40,240 type is unsigned and its name is 19501 14:41:37,680 --> 14:41:42,160 interbar we can get inside the function 19502 14:41:40,239 --> 14:41:44,955 and actually do whatever it is we want 19503 14:41:42,160 --> 14:41:47,520 to do so once we go in we're going to 19504 14:41:44,955 --> 14:41:50,479 test and see if the age is greater than 19505 14:41:47,519 --> 14:41:52,000 18 do something if the age is not 19506 14:41:50,480 --> 14:41:53,520 greater than 18 we're going to do 19507 14:41:52,000 --> 14:41:54,720 something else let's do that we're going 19508 14:41:54,720 --> 14:41:59,840 age is greater than 18 we're going to 19509 14:41:57,680 --> 14:42:03,360 tell them the age and tell them that 19510 14:41:59,839 --> 14:42:05,595 they can actually go in this is logical 19511 14:42:03,360 --> 14:42:08,319 and we're going to say else and in the 19512 14:42:08,319 --> 14:42:12,400 we're going to say something else 19513 14:42:10,239 --> 14:42:14,319 we're going to tell them sorry you are 19514 14:42:12,400 --> 14:42:16,880 too young for this and we are going to 19515 14:42:14,319 --> 14:42:19,360 get out of this block here and this is 19516 14:42:16,879 --> 14:42:21,680 really our function again let's try to 19517 14:42:19,360 --> 14:42:24,720 analyze the syntax here we have the 19518 14:42:21,680 --> 14:42:26,639 return type this is required in c plus 19519 14:42:24,720 --> 14:42:29,200 plus if you don't put it in you're going 19520 14:42:26,639 --> 14:42:31,116 to get a compiler error because you 19521 14:42:29,199 --> 14:42:33,435 can't have a function without a return 19522 14:42:31,116 --> 14:42:35,276 type in c plus plus you notice that we 19523 14:42:33,436 --> 14:42:37,520 have our squiggly lines here visual 19524 14:42:35,275 --> 14:42:40,000 studio code is not happy so we're going 19525 14:42:37,519 --> 14:42:42,879 to tell it the return type we're going 19526 14:42:40,000 --> 14:42:44,559 to save the function name after that 19527 14:42:42,879 --> 14:42:47,360 and after the function name we're going 19528 14:42:44,559 --> 14:42:49,915 to pass the parameters we can have any 19529 14:42:47,360 --> 14:42:52,400 number of parameters or inputs to the 19530 14:42:49,915 --> 14:42:54,159 function but in this case we just have 19531 14:42:52,400 --> 14:42:56,796 one because it is enough for our 19532 14:42:54,160 --> 14:42:58,796 purposes after that we have the body and 19533 14:42:56,796 --> 14:43:01,360 inside the body we do whatever it is we 19534 14:42:58,796 --> 14:43:03,840 want to do in this function here because 19535 14:43:01,360 --> 14:43:06,559 this function returns void we don't 19536 14:43:03,839 --> 14:43:08,795 really need to explicitly return from 19537 14:43:06,559 --> 14:43:11,519 this function here but this is an 19538 14:43:08,796 --> 14:43:14,000 exception only for functions that return 19539 14:43:11,519 --> 14:43:16,639 void for other return types you will 19540 14:43:14,000 --> 14:43:18,720 have to put in the return statement but 19541 14:43:16,639 --> 14:43:20,879 even for void functions you can put the 19542 14:43:18,720 --> 14:43:24,080 return statement if you want and you can 19543 14:43:24,080 --> 14:43:28,080 and you're going to put a semicolon 19544 14:43:25,519 --> 14:43:30,955 after this this is also valid c plus 19545 14:43:28,080 --> 14:43:33,116 plus syntax okay now we have declared 19546 14:43:30,955 --> 14:43:35,275 the function it is a valid c plus plus 19547 14:43:33,116 --> 14:43:37,116 functions let's try and call it we're 19548 14:43:35,275 --> 14:43:38,559 going to go down in main because that's 19549 14:43:37,116 --> 14:43:41,680 where we want to be calling our 19550 14:43:38,559 --> 14:43:44,079 functions for now and we are going to 19551 14:43:41,680 --> 14:43:46,000 say interbar we're going to call the 19552 14:43:44,080 --> 14:43:48,319 function and you use the function name 19553 14:43:46,000 --> 14:43:51,595 to call it and you're going to put in a 19554 14:43:48,319 --> 14:43:54,000 pair of parentheses and pass an argument 19555 14:43:51,595 --> 14:43:55,435 to the function again what you pass to 19556 14:43:54,000 --> 14:43:57,595 the function when you call it it's 19557 14:43:55,436 --> 14:43:58,955 called an argument but what you pass 19558 14:43:57,595 --> 14:44:01,519 through the function when you are 19559 14:43:58,955 --> 14:44:02,639 defining it like we did here that's a 19560 14:44:02,639 --> 14:44:07,116 okay so function parameters when 19561 14:44:04,720 --> 14:44:09,116 declaring functions and when you are 19562 14:44:07,116 --> 14:44:10,480 calling the function what you pass are 19563 14:44:10,480 --> 14:44:15,596 arguments and now we need to pass in 19564 14:44:12,879 --> 14:44:18,159 some age to this function and let's say 19565 14:44:15,595 --> 14:44:21,519 22 and if we do that this is going to be 19566 14:44:21,519 --> 14:44:26,719 notice that we have this squiggly line 19567 14:44:23,436 --> 14:44:27,680 it is because we forgot a semicolon here 19568 14:44:27,680 --> 14:44:33,840 c plus plus requires to have a semicolon 19569 14:44:30,879 --> 14:44:37,115 after each statement and now we have a 19570 14:44:33,839 --> 14:44:39,435 valid c plus program that is declaring a 19571 14:44:37,116 --> 14:44:42,319 function and calling a function down 19572 14:44:39,436 --> 14:44:44,880 here in main we can try to build it 19573 14:44:42,319 --> 14:44:46,720 let's use gcc to do that the world is 19574 14:44:44,879 --> 14:44:49,115 going to be good you see world finished 19575 14:44:46,720 --> 14:44:51,116 successfully and we can bring up a 19576 14:44:49,116 --> 14:44:53,200 terminal and actually try to run this 19577 14:44:51,116 --> 14:44:55,360 function here let's clear and run 19578 14:44:53,199 --> 14:44:57,360 rooster because our program is going to 19579 14:44:55,360 --> 14:44:59,840 be called rooster like we have been 19580 14:44:57,360 --> 14:45:02,559 doing all along and at the output is 19581 14:45:02,559 --> 14:45:08,239 please proceed notice that we have this 19582 14:45:05,360 --> 14:45:10,319 function defined once but we can call it 19583 14:45:08,239 --> 14:45:12,559 multiple times so for example if you go 19584 14:45:15,436 --> 14:45:19,596 and we try and build again let's build 19585 14:45:19,595 --> 14:45:24,239 the builder is going to be good we can 19586 14:45:21,519 --> 14:45:27,199 clear and run rooster now it's going to 19587 14:45:24,239 --> 14:45:28,479 say you are 15 please proceed why is 19588 14:45:28,480 --> 14:45:32,880 what are we really doing here uh if age 19589 14:45:30,796 --> 14:45:36,080 is greater than 10 we say 10 here we 19590 14:45:32,879 --> 14:45:38,879 should say 18. sorry for this we're 19591 14:45:38,879 --> 14:45:43,915 and now it should behave accordingly so 19592 14:45:43,915 --> 14:45:49,115 you are 22 please proceed you are too 19593 14:45:46,796 --> 14:45:52,160 young for this no offense because the 19594 14:45:49,116 --> 14:45:53,916 age is 15 here you can keep calling this 19595 14:45:52,160 --> 14:45:55,680 function and you're going to see things 19596 14:45:53,915 --> 14:45:57,519 printing out you can even call this 19597 14:45:55,680 --> 14:45:59,595 function in the loop i can't really 19598 14:45:57,519 --> 14:46:03,039 resist so let's do that we're going to 19599 14:46:03,040 --> 14:46:07,680 i and say start from 0 for example or 1 19600 14:46:07,680 --> 14:46:11,199 and we are going to say while i is less 19601 14:46:12,839 --> 14:46:18,000 20. we're going to increment uh you 19602 14:46:15,595 --> 14:46:20,319 already know how to use loops so we're 19603 14:46:18,000 --> 14:46:21,680 going to be using this as an example 19604 14:46:21,680 --> 14:46:25,756 comment out all these guys in front 19605 14:46:23,915 --> 14:46:27,435 because we don't want them to print 19606 14:46:27,436 --> 14:46:31,916 and what we're going to do is call the 19607 14:46:29,519 --> 14:46:32,879 function with whatever iteration we are 19608 14:46:32,879 --> 14:46:37,839 notice that we have a size t here and 19609 14:46:35,360 --> 14:46:40,080 the function is going to be taken 19610 14:46:37,839 --> 14:46:43,839 an unsigned integer so we're going to 19611 14:46:40,080 --> 14:46:47,680 have implicit conversions from size t 19612 14:46:43,839 --> 14:46:50,719 to end or if we really want this to not 19613 14:46:47,680 --> 14:46:53,199 cause any problems we can change the 19614 14:46:50,720 --> 14:46:56,400 type of our parameter to be size t this 19615 14:46:53,199 --> 14:46:58,879 is also valid c plus plus code let's say 19616 14:47:06,639 --> 14:47:09,199 watch what is going to happen this is 19617 14:47:09,199 --> 14:47:14,319 from 1 all the way to 19 and it's going 19618 14:47:12,000 --> 14:47:16,879 to be calling this function with 19619 14:47:14,319 --> 14:47:19,275 whatever iteration we might be at so 19620 14:47:16,879 --> 14:47:21,680 it's going to look from 1 all the way to 19621 14:47:21,680 --> 14:47:26,000 tell us we are too young for this but 19622 14:47:23,915 --> 14:47:29,595 for 19 it's going to let us in because 19623 14:47:26,000 --> 14:47:31,275 we will be old enough to enter a bar 19624 14:47:29,595 --> 14:47:34,559 let's try this i think it's going to be 19625 14:47:31,275 --> 14:47:36,319 fun we're going to weld with gcc 19626 14:47:36,319 --> 14:47:40,720 and run rooster ah sorry you're too 19627 14:47:38,955 --> 14:47:42,879 young for this you too young with us and 19628 14:47:40,720 --> 14:47:45,116 that once we hit 19 it's going to tell 19629 14:47:42,879 --> 14:47:47,199 us we are young enough to enter a bar 19630 14:47:45,116 --> 14:47:50,080 and we're going to go and and have as 19631 14:47:47,199 --> 14:47:53,039 much fun as we want so this is really 19632 14:47:50,080 --> 14:47:56,000 how you can declare a function remember 19633 14:47:53,040 --> 14:47:58,639 a function is uniquely identified by its 19634 14:47:56,000 --> 14:48:00,720 signature and the signature is really 19635 14:47:58,639 --> 14:48:02,879 the name of the function and the 19636 14:48:00,720 --> 14:48:04,239 parameters that you have in here okay 19637 14:48:02,879 --> 14:48:05,595 once you have the function declared 19638 14:48:04,239 --> 14:48:07,839 you're going to do whatever you want in 19639 14:48:05,595 --> 14:48:10,159 the body you are going to optionally 19640 14:48:07,839 --> 14:48:13,359 return something from the function and 19641 14:48:10,160 --> 14:48:15,276 once the function is declared or set up 19642 14:48:13,360 --> 14:48:17,840 you can go in main and call it and do 19643 14:48:15,275 --> 14:48:20,400 whatever it is you want with it okay now 19644 14:48:17,839 --> 14:48:22,639 i think we are mature enough to try out 19645 14:48:20,400 --> 14:48:24,160 a few other functions let's go down and 19646 14:48:22,639 --> 14:48:26,400 put in another function which is going 19647 14:48:24,160 --> 14:48:28,000 to compute the maximum of two numbers 19648 14:48:26,400 --> 14:48:30,000 the signature of this function is going 19649 14:48:28,000 --> 14:48:32,559 to be its name here and the parameters 19650 14:48:30,000 --> 14:48:34,400 notice that we are taking two parameters 19651 14:48:32,559 --> 14:48:37,115 because we want to be computing the 19652 14:48:34,400 --> 14:48:38,880 maximum between two numbers and this 19653 14:48:37,116 --> 14:48:40,796 function is going to return something we 19654 14:48:38,879 --> 14:48:43,199 want to get the maximum out of the 19655 14:48:40,796 --> 14:48:44,560 function and use it to do something else 19656 14:48:44,559 --> 14:48:48,239 so this is the declaration of the 19657 14:48:46,319 --> 14:48:49,839 function once we hit the body of the 19658 14:48:48,239 --> 14:48:52,239 function we are going to compute the 19659 14:48:49,839 --> 14:48:54,559 maximum we're going to say if a is 19660 14:48:52,239 --> 14:48:56,955 greater than b then a must be our 19661 14:48:54,559 --> 14:48:59,839 maximum so we're going to return that 19662 14:48:56,955 --> 14:49:01,519 and if a is not greater than b then b 19663 14:48:59,839 --> 14:49:04,159 must be the maximum we're going to 19664 14:49:01,519 --> 14:49:06,239 return b as our maximum here this is 19665 14:49:04,160 --> 14:49:08,560 what the function is doing we can go in 19666 14:49:06,239 --> 14:49:10,559 main and call it let's go down and 19667 14:49:08,559 --> 14:49:13,360 comment this out because we don't want 19668 14:49:10,559 --> 14:49:15,756 noise output from these guys so calling 19669 14:49:13,360 --> 14:49:17,915 interbore that's what we were doing now 19670 14:49:15,756 --> 14:49:20,559 we are calling max so we're going to go 19671 14:49:17,915 --> 14:49:23,115 down and set up a variable call it 19672 14:49:20,559 --> 14:49:26,559 result why not and we're going to 19673 14:49:23,116 --> 14:49:28,955 compute the maximum between 10 and 20 19674 14:49:26,559 --> 14:49:30,479 why not okay so let's see what we get 19675 14:49:28,955 --> 14:49:32,559 and we're going to print the maximum 19676 14:49:32,559 --> 14:49:38,399 max as resort okay now if we build this 19677 14:49:36,480 --> 14:49:39,916 program and run it try to guess what 19678 14:49:38,400 --> 14:49:41,596 we're going to get we're going to call 19679 14:49:41,595 --> 14:49:47,360 and what we pass in here are arguments 19680 14:49:44,639 --> 14:49:50,400 again we are not passing parameters here 19681 14:49:47,360 --> 14:49:52,955 we are going to pass in 10 and 20 as our 19682 14:49:50,400 --> 14:49:55,040 arguments and when we call the function 19683 14:49:52,955 --> 14:49:56,795 control is going to jump to the body of 19684 14:49:55,040 --> 14:49:58,955 the function and we are going to fall 19685 14:49:56,796 --> 14:50:01,200 inside here the arguments we're going to 19686 14:49:58,955 --> 14:50:03,756 pass in are just going to be copied in 19687 14:50:01,199 --> 14:50:05,680 the function and we will have them 19688 14:50:03,756 --> 14:50:07,436 through these names a and b so we're 19689 14:50:05,680 --> 14:50:09,915 going to test and see if the first 19690 14:50:07,436 --> 14:50:11,916 parameter is greater than the second in 19691 14:50:09,915 --> 14:50:13,839 this case the first parameter is not 19692 14:50:11,915 --> 14:50:16,000 greater than the second because the 19693 14:50:13,839 --> 14:50:18,639 first one is 10 the second is a 20. 19694 14:50:16,000 --> 14:50:20,480 let's go down and see that and the test 19695 14:50:18,639 --> 14:50:23,595 here is going to fail and we're going to 19696 14:50:20,480 --> 14:50:25,840 run whatever is in the else block here 19697 14:50:23,595 --> 14:50:28,000 and we are going to return b as our 19698 14:50:28,000 --> 14:50:32,080 b is going to basically be the result of 19699 14:50:32,080 --> 14:50:36,000 and that result is going to be assigned 19700 14:50:34,239 --> 14:50:38,479 to our research variable and we're going 19701 14:50:36,000 --> 14:50:40,879 to print it out with this sddc out 19702 14:50:38,480 --> 14:50:43,436 statement here so let's try and build 19703 14:50:40,879 --> 14:50:45,115 this we're going to build with gcc again 19704 14:50:43,436 --> 14:50:47,520 the world is going to be good we're 19705 14:50:45,116 --> 14:50:50,240 going to clear and run rooster you see 19706 14:50:47,519 --> 14:50:51,839 that maximum is 20. if we try and change 19707 14:50:50,239 --> 14:50:55,680 this for example make the first 19708 14:50:55,680 --> 14:51:00,239 let's see what maximum we get 19709 14:50:58,319 --> 14:51:02,955 going to clear and run rooster we're 19710 14:51:00,239 --> 14:51:04,955 going to say that maximum is 100 and 19711 14:51:02,955 --> 14:51:07,199 again you can call this function however 19712 14:51:04,955 --> 14:51:10,319 many times you want for example we can 19713 14:51:07,199 --> 14:51:13,115 call it again and say result equals 19714 14:51:10,319 --> 14:51:15,040 max using two variables that we have 19715 14:51:15,040 --> 14:51:19,275 and go on top and declare these two 19716 14:51:17,116 --> 14:51:22,955 variables why not we're going to say and 19717 14:51:19,275 --> 14:51:26,079 x and we're going to put in it 22 19718 14:51:22,955 --> 14:51:27,915 and we're going to say end y and put in 19719 14:51:27,915 --> 14:51:32,559 and now if we print the result we're 19720 14:51:30,239 --> 14:51:35,839 going to get 44 because that's the 19721 14:51:32,559 --> 14:51:38,479 maximum that was assigned back to our 19722 14:51:35,839 --> 14:51:40,159 research variable here let's weld again 19723 14:51:38,480 --> 14:51:42,720 we're going to run the task to build 19724 14:51:42,720 --> 14:51:47,916 you can clear and run rooster you see 19725 14:51:45,116 --> 14:51:50,639 max is 44 and this is really how you can 19726 14:51:47,915 --> 14:51:51,435 declare and use a maximum function like 19727 14:51:51,436 --> 14:51:55,520 we're going to comment this out because 19728 14:51:52,955 --> 14:51:58,000 we have a few more functions we want to 19729 14:51:55,519 --> 14:52:00,319 try out let's go on top and put in 19730 14:51:58,000 --> 14:52:02,796 another function and again notice that 19731 14:52:02,796 --> 14:52:07,680 before the main function here this is a 19732 14:52:05,275 --> 14:52:10,720 requirement of a c plus plus compiler 19733 14:52:07,680 --> 14:52:13,360 because before you use the function the 19734 14:52:10,720 --> 14:52:16,080 function must be declared somewhere so 19735 14:52:13,360 --> 14:52:18,559 we need to go before the main function 19736 14:52:16,080 --> 14:52:20,319 and declare our functions here the 19737 14:52:18,559 --> 14:52:22,319 function we're going to do now is say 19738 14:52:20,319 --> 14:52:24,480 hello here it's not going to take any 19739 14:52:22,319 --> 14:52:27,040 input and it's not going to give us any 19740 14:52:24,480 --> 14:52:29,756 output it's just going to say hello and 19741 14:52:27,040 --> 14:52:32,160 die off we can go down and call it this 19742 14:52:29,756 --> 14:52:35,040 is going to be super easy calling hello 19743 14:52:32,160 --> 14:52:37,520 and we're going to call it say hello see 19744 14:52:35,040 --> 14:52:39,840 that even visual studio code knows about 19745 14:52:37,519 --> 14:52:41,595 this function we can call it like this 19746 14:52:39,839 --> 14:52:43,039 it's going to be activated so we're 19747 14:52:41,595 --> 14:52:45,115 going to hit the body of the function 19748 14:52:43,040 --> 14:52:47,680 we're going to say hello there and the 19749 14:52:45,116 --> 14:52:49,520 function is going to return or die off 19750 14:52:47,680 --> 14:52:51,680 please know that you could omit this 19751 14:52:49,519 --> 14:52:54,079 return statement for functions that 19752 14:52:51,680 --> 14:52:55,680 return void so if we comment this guy 19753 14:52:54,080 --> 14:52:57,840 out the function is going to work 19754 14:52:55,680 --> 14:53:00,160 equally well you can use this however 19755 14:52:57,839 --> 14:53:03,839 you want now we're just going to build 19756 14:53:00,160 --> 14:53:05,436 with gcc and see this function run 19757 14:53:03,839 --> 14:53:08,319 the build is good we're going to run 19758 14:53:05,436 --> 14:53:10,796 rooster and it is saying hello there 19759 14:53:08,319 --> 14:53:12,080 let's go down and look at another 19760 14:53:12,080 --> 14:53:17,116 the next function is going to be lucky 19761 14:53:14,639 --> 14:53:19,275 number it's not going to take any input 19762 14:53:17,116 --> 14:53:22,080 but it is going to give us an output in 19763 14:53:19,275 --> 14:53:24,400 the form of an integer so we can call it 19764 14:53:22,080 --> 14:53:27,116 let's go down and comment out saying 19765 14:53:24,400 --> 14:53:28,400 hello here and say that we want to call 19766 14:53:28,400 --> 14:53:32,400 and we can go down and set up a variable 19767 14:53:30,480 --> 14:53:33,680 let's do that we're going to say int 19768 14:53:34,879 --> 14:53:39,595 are going to brace initialize this guy 19769 14:53:37,275 --> 14:53:42,795 so it's going to be initialized to zero 19770 14:53:39,595 --> 14:53:45,360 and we're going to say result 19771 14:53:42,796 --> 14:53:48,160 equals lucky number and we're going to 19772 14:53:45,360 --> 14:53:50,879 take the result returned by our lucky 19773 14:53:48,160 --> 14:53:52,720 number function and assign that to the 19774 14:53:50,879 --> 14:53:56,000 result variable and we can print it out 19775 14:53:52,720 --> 14:53:59,116 if we want so we can say sddc out and 19776 14:53:59,116 --> 14:54:04,480 and we're going to put that out here say 19777 14:54:05,360 --> 14:54:10,319 and if we build and run this program 19778 14:54:08,000 --> 14:54:12,796 we're going to get this guy printed out 19779 14:54:10,319 --> 14:54:15,519 we should see a 99 because that's what 19780 14:54:12,796 --> 14:54:18,400 we are returning from our function here 19781 14:54:15,519 --> 14:54:20,719 and resort is 99 as you see in the 19782 14:54:18,400 --> 14:54:22,480 output here so this is really how you 19783 14:54:22,480 --> 14:54:26,796 your functions a function has a unique 19784 14:54:24,559 --> 14:54:28,955 signature so for example into bar here 19785 14:54:31,595 --> 14:54:37,595 if you go down mats has its own name and 19786 14:54:34,559 --> 14:54:40,079 the parameters that it takes in if you 19787 14:54:37,595 --> 14:54:42,795 go down say hello and lucky number are 19788 14:54:40,080 --> 14:54:44,955 different in their own ways if you try 19789 14:54:42,796 --> 14:54:48,160 and set up another function that has the 19790 14:54:44,955 --> 14:54:49,915 same signature as max here your compiler 19791 14:54:48,160 --> 14:54:51,116 is going to freak out let's try and do 19792 14:54:49,915 --> 14:54:52,795 that we're going to set up another 19793 14:54:51,116 --> 14:54:54,720 function it's going to have a void 19794 14:54:52,796 --> 14:54:56,639 return type we're going to call it max 19795 14:54:54,720 --> 14:54:57,520 because we want to match this signature 19796 14:54:57,519 --> 14:55:02,719 and we're going to give it two 19797 14:55:02,720 --> 14:55:06,080 and we can do whatever we want in this 19798 14:55:04,319 --> 14:55:09,360 function notice that we have a squiggly 19799 14:55:06,080 --> 14:55:11,276 line if we open our problems tab here 19800 14:55:09,360 --> 14:55:13,199 you're going to see that visual studio 19801 14:55:11,275 --> 14:55:16,319 code has a few problems with this 19802 14:55:13,199 --> 14:55:19,115 because we are setting up a function 19803 14:55:16,319 --> 14:55:21,199 with the same signature twice let's try 19804 14:55:19,116 --> 14:55:23,436 and different make this a net and see 19805 14:55:21,199 --> 14:55:25,680 what we get right now we don't seem to 19806 14:55:23,436 --> 14:55:28,720 have a problem but if we get to run this 19807 14:55:25,680 --> 14:55:30,879 program we're going to have problems 19808 14:55:28,720 --> 14:55:33,040 let's go down and actually activate the 19809 14:55:30,879 --> 14:55:34,399 code to call the max function to really 19810 14:55:34,400 --> 14:55:38,796 because you need to know so we're going 19811 14:55:36,480 --> 14:55:41,040 to comment out calling lucky number and 19812 14:55:38,796 --> 14:55:43,520 we want to call max we're going to 19813 14:55:43,519 --> 14:55:48,239 and we are going to build this with gcc 19814 14:55:46,639 --> 14:55:50,239 notice what we're going to see uh-huh we 19815 14:55:48,239 --> 14:55:53,436 have problems here we have a compiler 19816 14:55:50,239 --> 14:55:54,795 error saying re definition of int max 19817 14:55:55,680 --> 14:56:01,040 one might think what if i name my 19818 14:55:58,720 --> 14:56:02,796 parameters differently here for example 19819 14:56:07,756 --> 14:56:11,756 using different names like this is not 19820 14:56:09,839 --> 14:56:14,239 going to solve your troubles because if 19821 14:56:11,756 --> 14:56:16,639 we try and uh bold again we're going to 19822 14:56:14,239 --> 14:56:18,559 get the same compiler error this is not 19823 14:56:16,639 --> 14:56:20,879 going to change your thing we don't have 19824 14:56:18,559 --> 14:56:23,519 enough tools to really understand this 19825 14:56:20,879 --> 14:56:24,719 problem but know that you can't have two 19826 14:56:24,720 --> 14:56:29,436 with the exact same signature in a 19827 14:56:27,275 --> 14:56:31,680 single c plus plus program and we're 19828 14:56:29,436 --> 14:56:34,400 going to learn more about this later but 19829 14:56:31,680 --> 14:56:37,040 note that the signature of a function 19830 14:56:34,400 --> 14:56:40,160 must be unique to it throughout your 19831 14:56:37,040 --> 14:56:41,756 entire c-plus program if you remember 19832 14:56:40,160 --> 14:56:44,560 this simple rule you're not going to 19833 14:56:41,756 --> 14:56:46,796 have many problems so let's try and weld 19834 14:56:44,559 --> 14:56:48,479 again to make sure we have no more 19835 14:56:48,480 --> 14:56:53,276 the bolt is good and we can run the 19836 14:56:50,796 --> 14:56:56,639 program to see our maximum printed out 19837 14:56:56,839 --> 14:57:02,159 rooster and max is 44 it is exactly what 19838 14:57:02,160 --> 14:57:06,480 the last thing i want to do in this 19839 14:57:04,080 --> 14:57:09,360 lecture is make it super clear in your 19840 14:57:06,480 --> 14:57:11,680 mind that what we pass to functions we 19841 14:57:11,680 --> 14:57:17,840 are copies so let's go on top and set up 19842 14:57:15,595 --> 14:57:20,559 a simple function the function is going 19843 14:57:17,839 --> 14:57:22,955 to increment and multiply it returns a 19844 14:57:20,559 --> 14:57:25,199 double type the name of the function is 19845 14:57:22,955 --> 14:57:27,680 increment and multiply and it is going 19846 14:57:25,199 --> 14:57:29,039 to take two parameters and a double a 19847 14:57:29,040 --> 14:57:34,239 once we hit the body of the function 19848 14:57:30,955 --> 14:57:36,159 we're going to increment a and b 19849 14:57:34,239 --> 14:57:38,000 the arguments that were passed to the 19850 14:57:36,160 --> 14:57:39,756 function and we're going to multiply 19851 14:57:38,000 --> 14:57:42,720 them and we're going to store the result 19852 14:57:39,756 --> 14:57:44,239 of that in a reserved variable the 19853 14:57:42,720 --> 14:57:46,559 result variable is what we're going to 19854 14:57:44,239 --> 14:57:48,319 return to the function and whoever 19855 14:57:46,559 --> 14:57:50,879 called this function is going to use 19856 14:57:48,319 --> 14:57:53,275 that to do whatever they want what we 19857 14:57:50,879 --> 14:57:55,915 really want to see is that the changes 19858 14:57:53,275 --> 14:57:59,360 we do inside the function are not going 19859 14:57:55,915 --> 14:58:01,680 to be visible to the arguments that we 19860 14:57:59,360 --> 14:58:04,239 pass to this function here to really 19861 14:58:01,680 --> 14:58:06,160 make this super clear let's go down in 19862 14:58:04,239 --> 14:58:08,879 main and set up a few variables we're 19863 14:58:06,160 --> 14:58:11,116 going to have a double h and the double 19864 14:58:08,879 --> 14:58:13,435 i the values are going to be three and 19865 14:58:11,116 --> 14:58:15,276 four we're going to print them out and 19866 14:58:13,436 --> 14:58:17,840 we're going to call our increment 19867 14:58:15,275 --> 14:58:18,955 multiply function notice that we pass h 19868 14:58:18,955 --> 14:58:24,319 to the function and the result of the 19869 14:58:21,116 --> 14:58:27,200 function is going to be stored in uh the 19870 14:58:24,319 --> 14:58:29,116 anc mult resort variable here but the 19871 14:58:27,199 --> 14:58:30,719 return value is really not important 19872 14:58:30,720 --> 14:58:36,080 is if the changes we do to the arguments 19873 14:58:34,000 --> 14:58:38,879 are going to be visible on the outside 19874 14:58:36,080 --> 14:58:40,720 of the function here so remember we have 19875 14:58:38,879 --> 14:58:42,955 h and i here we pass them to the 19876 14:58:40,720 --> 14:58:44,160 function once we hit the inside of the 19877 14:58:44,160 --> 14:58:48,320 we're going to increment a and b 19878 14:58:46,796 --> 14:58:50,796 and we are going to return from the 19879 14:58:50,796 --> 14:58:57,840 if a which was a 3 and b which was a 4 19880 14:58:55,275 --> 14:58:59,756 have been incremented to 4 and 5. that's 19881 14:58:57,839 --> 14:59:02,795 what we want to see so we're going to 19882 14:58:59,756 --> 14:59:05,595 boil this and run this so let's use gcc 19883 14:59:02,796 --> 14:59:07,276 to do that the bulk is going to be good 19884 14:59:09,116 --> 14:59:14,160 and notice what we have here let's go 19885 14:59:11,199 --> 14:59:18,479 down and look at the variables again 19886 14:59:14,160 --> 14:59:20,000 we set up h to contain a 3 so before we 19887 14:59:22,319 --> 14:59:26,080 okay so that's what we're going to print 19888 14:59:23,915 --> 14:59:27,040 here once we hit the inside of the 19889 14:59:27,040 --> 14:59:31,200 we are going to say what we have inside 19890 14:59:29,199 --> 14:59:33,915 the function before we do the operation 19891 14:59:31,199 --> 14:59:36,639 before the increment again a and b are 19892 14:59:33,915 --> 14:59:37,915 four so these are copies we have inside 19893 14:59:37,915 --> 14:59:43,275 do the increment and after we have the 19894 14:59:40,160 --> 14:59:45,200 increment a and b are going to be four 19895 14:59:43,275 --> 14:59:47,040 but once we hit the outside of the 19896 14:59:48,559 --> 14:59:52,559 with this guy here we're going to see 19897 14:59:55,040 --> 15:00:01,116 we didn't really affect h and i when we 19898 14:59:58,080 --> 15:00:04,560 did this increment here what we affected 19899 15:00:01,116 --> 15:00:05,360 are copies that were passed to a and b 19900 15:00:05,360 --> 15:00:08,720 and this is something you need to know 19901 15:00:07,199 --> 15:00:10,559 because some people will think that 19902 15:00:10,559 --> 15:00:16,879 h and i in my argument list here i can 19903 15:00:14,796 --> 15:00:19,596 manipulate h and i what you are 19904 15:00:16,879 --> 15:00:22,639 manipulating are copies that were made 19905 15:00:19,595 --> 15:00:24,559 by the compiler to pass the arguments to 19906 15:00:22,639 --> 15:00:26,479 the function here this is something you 19907 15:00:24,559 --> 15:00:28,639 need to keep in mind and we will learn 19908 15:00:26,480 --> 15:00:31,200 more about this but i wanted you to 19909 15:00:28,639 --> 15:00:33,199 learn this firsthand and this is really 19910 15:00:31,199 --> 15:00:35,275 all we set out to do in this lecture i 19911 15:00:33,199 --> 15:00:37,435 hope you have a better understanding of 19912 15:00:35,275 --> 15:00:39,436 what a function is and what you can do 19913 15:00:37,436 --> 15:00:41,360 with it in c plus plus we are going to 19914 15:00:39,436 --> 15:00:43,040 stop here in this lecture in the next 19915 15:00:41,360 --> 15:00:45,915 one we're going to learn a little more 19916 15:00:43,040 --> 15:00:47,360 about function declarations and function 19917 15:00:47,360 --> 15:00:51,680 go ahead and finish up here and meet me 19918 15:00:49,680 --> 15:00:54,000 there in this lecture we're going to 19919 15:00:51,680 --> 15:00:57,040 learn about function declarations and 19920 15:00:54,000 --> 15:00:58,720 function definitions the motivation for 19921 15:00:57,040 --> 15:01:01,680 the topic we're going to be talking 19922 15:00:58,720 --> 15:01:05,116 about here is that sometimes you really 19923 15:01:01,680 --> 15:01:07,116 want to separate a function header from 19924 15:01:05,116 --> 15:01:10,080 its implementation because you don't 19925 15:01:07,116 --> 15:01:12,080 want to expose all the details about how 19926 15:01:10,080 --> 15:01:14,560 you do stuff let's look at the simple 19927 15:01:12,080 --> 15:01:16,480 example in the last lecture we declared 19928 15:01:14,559 --> 15:01:18,955 a max function which was computing the 19929 15:01:16,480 --> 15:01:20,560 maximum between two numbers the output 19930 15:01:18,955 --> 15:01:22,720 was an integer so that's what we 19931 15:01:20,559 --> 15:01:25,595 returned the name of the function was 19932 15:01:22,720 --> 15:01:27,756 max and we had two parameters into a and 19933 15:01:25,595 --> 15:01:31,040 and b but what we did in the last 19934 15:01:27,756 --> 15:01:32,080 lecture was crop the entire function 19935 15:01:32,080 --> 15:01:36,480 in front of main but sometimes people 19936 15:01:34,400 --> 15:01:39,200 don't want to really see the details of 19937 15:01:36,480 --> 15:01:41,276 how you do stuff they just want to know 19938 15:01:39,199 --> 15:01:43,199 a summary of what your function does 19939 15:01:41,275 --> 15:01:45,756 what we can get out of the function and 19940 15:01:43,199 --> 15:01:48,000 what we need to give to the function for 19941 15:01:45,756 --> 15:01:49,916 it to do its job and that's what we're 19942 15:01:48,000 --> 15:01:52,559 going to see in this lecture if you look 19943 15:01:49,915 --> 15:01:54,239 here we have the function header in 19944 15:01:52,559 --> 15:01:56,795 front of the main function and the 19945 15:01:54,239 --> 15:01:59,115 function header is made up of its return 19946 15:01:56,796 --> 15:02:01,520 type the name of the function and the 19947 15:01:59,116 --> 15:02:03,520 list of the parameters and after that 19948 15:02:01,519 --> 15:02:06,955 we're going to put a semicolon this is 19949 15:02:03,519 --> 15:02:09,115 called a function header or a prototype 19950 15:02:06,955 --> 15:02:10,879 and when we do that the compiler is 19951 15:02:09,116 --> 15:02:13,596 going to be happy and it's going to work 19952 15:02:10,879 --> 15:02:16,399 with us to compile our program but when 19953 15:02:13,595 --> 15:02:18,955 we get to link our program to really put 19954 15:02:16,400 --> 15:02:20,639 it together and make one binary the 19955 15:02:18,955 --> 15:02:23,116 definition of the function is going to 19956 15:02:20,639 --> 15:02:25,360 be needed and if you look after main 19957 15:02:23,116 --> 15:02:27,596 here what we have is a function 19958 15:02:25,360 --> 15:02:30,000 definition if you look closely you see 19959 15:02:27,595 --> 15:02:32,879 that it is exactly the same thing we had 19960 15:02:30,000 --> 15:02:34,879 in the last lecture we have our header 19961 15:02:32,879 --> 15:02:37,915 information we don't have a semicolon 19962 15:02:34,879 --> 15:02:41,360 after our parameter list but we have a 19963 15:02:37,915 --> 15:02:44,399 pair of curly braces and inside these 19964 15:02:41,360 --> 15:02:45,436 curly braces we have the body of our 19965 15:02:46,480 --> 15:02:50,480 it is really easy to look at this 19966 15:02:48,319 --> 15:02:52,559 program because we don't have to look at 19967 15:02:50,480 --> 15:02:54,956 all these details about how this 19968 15:02:54,955 --> 15:02:59,839 first-hand information on how we can use 19969 15:02:57,360 --> 15:03:01,680 this function and that is what most 19970 15:02:59,839 --> 15:03:03,679 people are going to be interested in 19971 15:03:01,680 --> 15:03:06,160 about your function they want to really 19972 15:03:03,680 --> 15:03:08,319 want to go in and see how you do stuff 19973 15:03:06,160 --> 15:03:10,320 they will want to use your function to 19974 15:03:08,319 --> 15:03:12,720 get the output and use it to do whatever 19975 15:03:10,319 --> 15:03:15,116 it is they want to do if you look here 19976 15:03:12,720 --> 15:03:17,275 in the body of the main function we have 19977 15:03:15,116 --> 15:03:19,436 two variables a and b and we use them to 19978 15:03:17,275 --> 15:03:21,915 call the maximum function we can also 19979 15:03:19,436 --> 15:03:24,560 set up a minimum function which works in 19980 15:03:21,915 --> 15:03:26,479 the similar way and we call another 19981 15:03:24,559 --> 15:03:28,795 function called increment which is going 19982 15:03:26,480 --> 15:03:31,040 to increment and multiply whatever 19983 15:03:28,796 --> 15:03:33,520 arguments we pass to this function so 19984 15:03:31,040 --> 15:03:36,400 the main idea here is that we can split 19985 15:03:33,519 --> 15:03:37,839 our function in two parts one is going 19986 15:03:36,400 --> 15:03:39,040 to be the function header which is 19987 15:03:37,839 --> 15:03:41,755 really going to be the function 19988 15:03:39,040 --> 15:03:44,400 declaration and the other is going to be 19989 15:03:41,756 --> 15:03:46,160 the function body with all the details 19990 15:03:44,400 --> 15:03:48,639 about how the function works and that's 19991 15:03:46,160 --> 15:03:50,560 going to be our function definition and 19992 15:03:48,639 --> 15:03:52,400 once we have these two things in place 19993 15:03:50,559 --> 15:03:54,319 we can call the function in our main 19994 15:03:52,400 --> 15:03:56,639 function and do whatever we want with 19995 15:03:54,319 --> 15:03:59,040 that okay now it should be super clear 19996 15:03:56,639 --> 15:04:01,680 that we can separate our function in two 19997 15:03:59,040 --> 15:04:03,756 parts declaration and definition and 19998 15:04:01,680 --> 15:04:05,915 sometimes the declaration part is also 19999 15:04:03,756 --> 15:04:08,160 called a prototype that's a prototype of 20000 15:04:05,915 --> 15:04:09,519 a function and it is made up of the 20001 15:04:09,519 --> 15:04:13,519 together with the function name together 20002 15:04:11,756 --> 15:04:15,595 with the parameters and after that 20003 15:04:13,519 --> 15:04:18,955 you're going to add a semicolon to make 20004 15:04:15,595 --> 15:04:21,199 it valid c plus syntax and another thing 20005 15:04:18,955 --> 15:04:24,000 you should know is that the prototype 20006 15:04:21,199 --> 15:04:25,755 needs to come before wherever it is 20007 15:04:24,000 --> 15:04:29,275 called if you're trying to call a 20008 15:04:25,756 --> 15:04:31,680 function before its prototype is seen in 20009 15:04:29,275 --> 15:04:33,756 your c plus plus program you're going to 20010 15:04:31,680 --> 15:04:37,275 get a compiler error and now you might 20011 15:04:33,756 --> 15:04:39,840 be asking but why did our programs in 20012 15:04:37,275 --> 15:04:42,400 the last lecture work we didn't split 20013 15:04:39,839 --> 15:04:43,275 things up and our program was working 20014 15:04:44,080 --> 15:04:49,116 if you put a function definition in 20015 15:04:46,639 --> 15:04:51,436 front of the main function that function 20016 15:04:49,116 --> 15:04:54,160 definition is also going to double as a 20017 15:04:51,436 --> 15:04:56,796 function declaration that's why the code 20018 15:04:54,160 --> 15:04:58,880 in the last lecture worked okay now that 20019 15:04:56,796 --> 15:05:01,040 you have an idea about what a function 20020 15:04:58,879 --> 15:05:03,519 declaration and the function definition 20021 15:05:01,040 --> 15:05:06,559 is let's head over to visual studio code 20022 15:05:03,519 --> 15:05:08,795 and play with us okay here we are in our 20023 15:05:06,559 --> 15:05:11,680 working folder the current project is 20024 15:05:08,796 --> 15:05:14,000 function declarations and definitions we 20025 15:05:11,680 --> 15:05:16,160 are going to grab our templates files 20026 15:05:16,160 --> 15:05:20,796 we're going to put them in place 20027 15:05:18,480 --> 15:05:24,000 and we are going to open this guy in 20028 15:05:20,796 --> 15:05:25,756 visual studio code let's do that 20029 15:05:25,756 --> 15:05:31,840 and we are going to open our main cpp 20030 15:05:28,559 --> 15:05:33,680 file clean it up a little bit and let's 20031 15:05:31,839 --> 15:05:35,359 close this pane so that we have some 20032 15:05:33,680 --> 15:05:37,595 breathing space the first thing we're 20033 15:05:35,360 --> 15:05:39,116 going to do is set up a maximum function 20034 15:05:37,595 --> 15:05:41,360 you already know how to do that we're 20035 15:05:39,116 --> 15:05:43,200 going to specify the return type 20036 15:05:41,360 --> 15:05:44,955 we are going to specify the function 20037 15:05:43,199 --> 15:05:47,755 name and the parameters we're going to 20038 15:05:44,955 --> 15:05:50,000 take in it a and into b and we're going 20039 15:05:47,756 --> 15:05:52,000 to go in and do whatever we need to 20040 15:05:50,000 --> 15:05:54,559 compute the maximum between these two 20041 15:05:52,000 --> 15:05:55,595 guys we're going to say if a is greater 20042 15:05:56,480 --> 15:06:00,480 a must be the maximum so we're going to 20043 15:06:04,796 --> 15:06:09,116 okay this is the function and it is 20044 15:06:06,720 --> 15:06:11,756 really simple we can go down in main and 20045 15:06:11,756 --> 15:06:16,480 x and i put in a five for example 20046 15:06:16,480 --> 15:06:21,680 y and put in a two or a three put in 20047 15:06:21,680 --> 15:06:25,840 and then we're going to set up the 20048 15:06:25,839 --> 15:06:29,755 which is going to catch the maximum i 20049 15:06:27,756 --> 15:06:32,080 think we can do that and what we're 20050 15:06:29,756 --> 15:06:34,400 going to assign to this is whatever is 20051 15:06:32,080 --> 15:06:36,480 returned from our function call so we're 20052 15:06:34,400 --> 15:06:37,840 going to call this function with x and y 20053 15:06:36,480 --> 15:06:39,360 so we're going to be computing the 20054 15:06:39,360 --> 15:06:44,319 5 and 2 here the maximum is going to be 20055 15:06:42,000 --> 15:06:45,915 stored in this result variable and we're 20056 15:06:44,319 --> 15:06:47,680 going to print it out so we're going to 20057 15:06:47,680 --> 15:06:54,720 see out and i'm going to say max 20058 15:06:50,879 --> 15:06:57,115 as result this is going to do 20059 15:06:54,720 --> 15:06:59,275 okay let's try and build this and make 20060 15:06:59,275 --> 15:07:03,595 this is nothing new so far it is 20061 15:07:01,519 --> 15:07:05,595 everything we have done in the last 20062 15:07:05,595 --> 15:07:09,680 now the world is good we can clear and 20063 15:07:08,000 --> 15:07:12,080 run rooster we're going to get the 20064 15:07:09,680 --> 15:07:15,116 maximum as a five here and if we try to 20065 15:07:12,080 --> 15:07:16,796 change this things for example make y 12 20066 15:07:19,275 --> 15:07:24,239 and we are going to clear these thanks 20067 15:07:21,519 --> 15:07:25,275 and run rooster we should get max equals 20068 15:07:25,275 --> 15:07:31,756 this is all fine and dandy but sometimes 20069 15:07:28,879 --> 15:07:35,115 we don't really want to have the details 20070 15:07:31,756 --> 15:07:37,595 about how our function works shown in 20071 15:07:35,116 --> 15:07:40,480 front of the main function here and we 20072 15:07:37,595 --> 15:07:43,040 can split our function in two parts a 20073 15:07:40,480 --> 15:07:44,720 declaration and a definition the 20074 15:07:43,040 --> 15:07:47,040 declaration is really going to be the 20075 15:07:44,720 --> 15:07:49,040 header part of the function and that's 20076 15:07:47,040 --> 15:07:51,680 going to be the return value the name of 20077 15:07:49,040 --> 15:07:54,239 the function and the parameter list but 20078 15:07:51,680 --> 15:07:56,955 to make it a valid declaration we have 20079 15:07:54,239 --> 15:07:58,319 to put a semicolon after this so this is 20080 15:07:59,756 --> 15:08:04,319 sometimes it is also called a prototype 20081 15:08:04,480 --> 15:08:09,040 and please make sure you understand the 20082 15:08:06,319 --> 15:08:11,275 difference between a prototype and a 20083 15:08:09,040 --> 15:08:14,319 signature in the last lecture we saw 20084 15:08:11,275 --> 15:08:16,400 that the signature was only made of the 20085 15:08:14,319 --> 15:08:20,000 name of the function and the parameter 20086 15:08:16,400 --> 15:08:21,916 list but the return value was not part 20087 15:08:20,000 --> 15:08:24,559 of the function signature but the 20088 15:08:21,915 --> 15:08:27,755 prototype includes all these guys so 20089 15:08:30,160 --> 15:08:36,880 okay so now that we have our function 20090 15:08:32,879 --> 15:08:38,955 declaration setup we need to put in 20091 15:08:36,879 --> 15:08:42,079 a function definition because if you try 20092 15:08:38,955 --> 15:08:44,479 to compile this program and link it 20093 15:08:42,080 --> 15:08:45,840 you are going to get problems let's try 20094 15:08:44,480 --> 15:08:47,040 and do that before we put in the 20095 15:08:47,040 --> 15:08:50,955 we can weld with gcc we are learning so 20096 15:08:50,955 --> 15:08:53,839 have to fear breaking things a little 20097 15:08:53,839 --> 15:09:00,639 let's clear and try to build with gcc 20098 15:08:58,400 --> 15:09:03,116 and you're going to see ah we have a 20099 15:09:00,639 --> 15:09:05,756 problem here and what kind of problem we 20100 15:09:03,116 --> 15:09:08,720 have the c plus plus compiler is going 20101 15:09:05,756 --> 15:09:10,319 to compile this program but when we hit 20102 15:09:10,319 --> 15:09:14,639 i realize i haven't really talked about 20103 15:09:12,239 --> 15:09:17,595 this in more detail but we will do in 20104 15:09:14,639 --> 15:09:20,000 this lecture but the main problem here 20105 15:09:17,595 --> 15:09:22,720 is that we don't have the definition of 20106 15:09:20,000 --> 15:09:24,480 this function in place and the compiler 20107 15:09:22,720 --> 15:09:26,720 is going to search for it and not find 20108 15:09:24,480 --> 15:09:29,276 it and you see that it is saying 20109 15:09:29,275 --> 15:09:33,115 okay so it has seen the declaration of 20110 15:09:33,116 --> 15:09:37,360 but it doesn't know how the function 20111 15:09:35,595 --> 15:09:39,915 does its job because we don't have a 20112 15:09:37,360 --> 15:09:42,796 function body in here so to really make 20113 15:09:39,915 --> 15:09:45,040 this code work exactly as we want we 20114 15:09:42,796 --> 15:09:47,276 need to put in a definition and the 20115 15:09:45,040 --> 15:09:49,436 definition can show up anywhere in your 20116 15:09:47,275 --> 15:09:50,795 c plus plus program in this case we're 20117 15:09:50,796 --> 15:09:55,040 after the main function and the 20118 15:09:55,040 --> 15:10:00,080 what we have been using so far as our 20119 15:09:57,756 --> 15:10:02,639 function definition in the last lecture 20120 15:10:00,080 --> 15:10:05,680 but notice that it has the body 20121 15:10:02,639 --> 15:10:08,400 information and now when the compiler 20122 15:10:05,680 --> 15:10:11,199 tries to compile this program it will 20123 15:10:08,400 --> 15:10:13,436 know how the maximum function is 20124 15:10:11,199 --> 15:10:15,435 computing its maximum because we have 20125 15:10:13,436 --> 15:10:17,680 all that information in here so let's 20126 15:10:15,436 --> 15:10:21,200 say that this is our function definition 20127 15:10:17,680 --> 15:10:23,915 and shows up after main in this case and 20128 15:10:21,199 --> 15:10:25,755 we can try and build this and see if the 20129 15:10:23,915 --> 15:10:26,559 work is good now you see the world is 20130 15:10:26,559 --> 15:10:32,159 and if we try to run it we're going to 20131 15:10:29,116 --> 15:10:33,360 get our maximum exactly like we did when 20132 15:10:33,360 --> 15:10:38,160 the declaration and the definition 20133 15:10:35,839 --> 15:10:39,915 cramped up before main here but 20134 15:10:38,160 --> 15:10:42,480 hopefully now you can see that our 20135 15:10:39,915 --> 15:10:44,639 program is much cleaner because when you 20136 15:10:42,480 --> 15:10:46,640 look at this you know that we have a 20137 15:10:44,639 --> 15:10:48,720 function called max it's going to take 20138 15:10:46,639 --> 15:10:51,199 two parameters it's going to return 20139 15:10:48,720 --> 15:10:53,200 something and it is called mats so it 20140 15:10:51,199 --> 15:10:56,239 must be computing the maximum of these 20141 15:10:53,199 --> 15:10:58,079 two guys but we don't really care how 20142 15:10:56,239 --> 15:11:00,239 this function does its job how it 20143 15:10:58,080 --> 15:11:02,720 computes the maximum here and this is 20144 15:11:00,239 --> 15:11:05,839 the main message here we can split a 20145 15:11:02,720 --> 15:11:08,400 function in two parts a declaration and 20146 15:11:05,839 --> 15:11:11,039 a definition okay just to play around we 20147 15:11:08,400 --> 15:11:13,360 can also set up a minimum function 20148 15:11:11,040 --> 15:11:15,680 before we do that let's set up its 20149 15:11:13,360 --> 15:11:18,319 definition down here we're just going to 20150 15:11:15,680 --> 15:11:20,639 copy the maximum and adapt it 20151 15:11:18,319 --> 15:11:23,915 we're going to put a copy of that guy 20152 15:11:20,639 --> 15:11:25,595 here we're going to call it men 20153 15:11:23,915 --> 15:11:27,915 it's going to take two parameters it's 20154 15:11:25,595 --> 15:11:31,275 going to return an end but what we test 20155 15:11:27,915 --> 15:11:33,755 for is if a is less than b if a is less 20156 15:11:33,756 --> 15:11:38,796 if a is not less than b then b must be 20157 15:11:36,796 --> 15:11:40,880 our minimum and we can put the 20158 15:11:38,796 --> 15:11:43,520 declaration of this function on top here 20159 15:11:40,879 --> 15:11:47,040 so let's go there and say men 20160 15:11:47,040 --> 15:11:50,879 and now we can change the call here to 20161 15:11:48,879 --> 15:11:53,275 say mean and it is going to compute the 20162 15:11:50,879 --> 15:11:55,199 minimum why not let's do that we're 20163 15:11:53,275 --> 15:11:57,595 going to say min here and we're going to 20164 15:11:55,199 --> 15:11:59,115 grab whatever is the minimum between x 20165 15:11:57,595 --> 15:12:01,839 and y here and that's what we're going 20166 15:11:59,116 --> 15:12:04,080 to have in this result variable here 20167 15:12:01,839 --> 15:12:05,595 let's do that we're going to try and use 20168 15:12:07,360 --> 15:12:11,756 so we're going to clear and run rooster 20169 15:12:09,519 --> 15:12:13,595 now the minimum is five we are calling 20170 15:12:13,595 --> 15:12:17,915 in the last lecture we also set up a 20171 15:12:15,680 --> 15:12:19,756 function to increment and multiply let's 20172 15:12:19,756 --> 15:12:24,319 but split that into a declaration and 20173 15:12:22,559 --> 15:12:26,955 the definition just to practice a little 20174 15:12:24,319 --> 15:12:30,400 more we're going to call this and 20175 15:12:26,955 --> 15:12:32,795 malt and it's going to take a and b in 20176 15:12:30,400 --> 15:12:34,796 and inside and we're not going to put 20177 15:12:32,796 --> 15:12:37,520 the body on top here we're going to put 20178 15:12:34,796 --> 15:12:39,436 the body down with the other definitions 20179 15:12:37,519 --> 15:12:42,079 that we have for all these other 20180 15:12:39,436 --> 15:12:43,596 functions for max and mean here so we're 20181 15:12:42,080 --> 15:12:45,680 going to put in our body it's really 20182 15:12:43,595 --> 15:12:48,079 simple so we're going to put a body 20183 15:12:53,839 --> 15:12:58,239 and we're going to multiply that with an 20184 15:12:58,239 --> 15:13:01,915 and we're going to return that i think 20185 15:13:00,000 --> 15:13:04,160 this is going to be valid c plus plus 20186 15:13:01,915 --> 15:13:06,559 syntax and if we try to call this 20187 15:13:04,160 --> 15:13:09,360 function in main let's do that say in 20188 15:13:06,559 --> 15:13:11,595 mult and we're going to pass an x and y 20189 15:13:09,360 --> 15:13:14,559 the program is going to just work fine 20190 15:13:11,595 --> 15:13:15,360 we're going to grab the result and store 20191 15:13:15,360 --> 15:13:21,040 in our variable let's do that 20192 15:13:21,040 --> 15:13:25,840 and we're going to say result here this 20193 15:13:25,839 --> 15:13:30,079 and if we build and run this program 20194 15:13:28,239 --> 15:13:33,199 we're going to see the output here let's 20195 15:13:33,199 --> 15:13:38,955 the results is going to be 78 because if 20196 15:13:36,080 --> 15:13:42,080 we increment a 5 it becomes a 6. 20197 15:13:38,955 --> 15:13:46,159 if we increment y we're going to get 13 20198 15:13:42,080 --> 15:13:48,720 13 times 6 should be 78 let's be sure 20199 15:13:46,160 --> 15:13:51,116 we're going to bring up the calculator 20200 15:13:48,720 --> 15:13:54,000 let's do that so that you know that i am 20201 15:13:51,116 --> 15:13:56,720 not making these things up 78. so our 20202 15:13:54,000 --> 15:13:58,639 program is really working like it should 20203 15:13:56,720 --> 15:14:00,796 and this is the main message here we can 20204 15:13:58,639 --> 15:14:02,319 really split up functions into a 20205 15:14:02,319 --> 15:14:07,116 and a definition and that's going to 20206 15:14:04,639 --> 15:14:08,796 make our programs much easier to work 20207 15:14:08,796 --> 15:14:13,276 one thing you should know is that the 20208 15:14:10,720 --> 15:14:15,756 names of the variables aren't really 20209 15:14:13,275 --> 15:14:16,559 important in function declarations like 20210 15:14:16,559 --> 15:14:19,915 so for example we can take out the a and 20211 15:14:19,915 --> 15:14:25,435 or we can take out all the variable 20212 15:14:22,239 --> 15:14:27,275 names in our function declarations let's 20213 15:14:27,275 --> 15:14:32,319 and you might think is this program 20214 15:14:29,360 --> 15:14:34,720 going to compile well i hate to break it 20215 15:14:32,319 --> 15:14:37,199 to you but it is going to compile so 20216 15:14:37,199 --> 15:14:41,915 you're going to see that the both is 20217 15:14:38,559 --> 15:14:44,559 good so the function header or 20218 15:14:41,915 --> 15:14:46,079 declaration doesn't really care about 20219 15:14:46,080 --> 15:14:50,240 in the definitions we care about these 20220 15:14:48,239 --> 15:14:52,879 variable names because we are going to 20221 15:14:50,239 --> 15:14:55,040 be using them in the bodies but if you 20222 15:14:52,879 --> 15:14:58,719 want you can leave them out in your 20223 15:14:55,040 --> 15:15:00,879 function declarations but i like to keep 20224 15:14:58,720 --> 15:15:03,360 them in place because i like my 20225 15:15:00,879 --> 15:15:05,360 functions to be self-documented if 20226 15:15:03,360 --> 15:15:07,756 somebody looks at this they will know 20227 15:15:05,360 --> 15:15:09,595 the variable name here but if you really 20228 15:15:07,756 --> 15:15:12,319 want you can leave the variable names 20229 15:15:09,595 --> 15:15:13,839 out and it is going to work pretty well 20230 15:15:12,319 --> 15:15:15,199 this is really always enough to do in 20231 15:15:13,839 --> 15:15:17,435 this lecture i hope you found it 20232 15:15:15,199 --> 15:15:19,275 interesting we are going to stop here in 20233 15:15:17,436 --> 15:15:20,955 this lecture in the next one we are 20234 15:15:19,275 --> 15:15:24,000 going to see how we can split our 20235 15:15:20,955 --> 15:15:26,159 programs in multiple files now that we 20236 15:15:24,000 --> 15:15:27,436 know how to split into declaration and 20237 15:15:27,436 --> 15:15:31,436 go ahead and finish up here and meet me 20238 15:15:31,436 --> 15:15:36,080 in this lecture we're going to revisit 20239 15:15:33,680 --> 15:15:38,720 our c plus plus program compilation 20240 15:15:36,080 --> 15:15:40,880 model along the way we are also going to 20241 15:15:38,720 --> 15:15:43,040 see how we can work with functions 20242 15:15:43,040 --> 15:15:47,520 if you remember our compilation model 20243 15:15:45,436 --> 15:15:50,796 looked something like this you have seen 20244 15:15:47,519 --> 15:15:53,039 this slide a lot of times by now what it 20245 15:15:50,796 --> 15:15:55,520 describes is what happens when we 20246 15:15:53,040 --> 15:15:58,400 compile our program the moment we run 20247 15:15:55,519 --> 15:16:01,039 the compilation command our c plus 20248 15:15:58,400 --> 15:16:03,360 program which lives in the main cpp file 20249 15:16:01,040 --> 15:16:05,275 by now is going to be passed through the 20250 15:16:03,360 --> 15:16:07,199 compiler the compiler is going to 20251 15:16:05,275 --> 15:16:10,000 process it and it is going to come up 20252 15:16:07,199 --> 15:16:12,319 with a binary executable that is mostly 20253 15:16:10,000 --> 15:16:15,275 called rooster in this course and when 20254 15:16:12,319 --> 15:16:17,519 we execute this rooster file or binary 20255 15:16:15,275 --> 15:16:20,079 file we're going to get the program to 20256 15:16:17,519 --> 15:16:22,239 run the program is going to be executed 20257 15:16:20,080 --> 15:16:24,955 instruction by instruction when it is 20258 15:16:22,239 --> 15:16:26,795 loaded by the operating system and we 20259 15:16:24,955 --> 15:16:29,040 are mostly going to see the output of 20260 15:16:26,796 --> 15:16:31,596 that program on the terminal in this 20261 15:16:29,040 --> 15:16:35,116 course this is the compilation model we 20262 15:16:31,595 --> 15:16:38,319 know but now what we want to do is zoom 20263 15:16:35,116 --> 15:16:40,955 in on this compilation part here and see 20264 15:16:38,319 --> 15:16:43,275 what really happens under the hood 20265 15:16:40,955 --> 15:16:46,239 we are going to suppose that we have a 20266 15:16:43,275 --> 15:16:48,559 program which is made of a lot of source 20267 15:16:46,239 --> 15:16:51,360 files as we have here on the left 20268 15:16:48,559 --> 15:16:54,159 suppose we have two three or five source 20269 15:16:51,360 --> 15:16:57,040 files unlike what we have so far which 20270 15:16:54,160 --> 15:16:58,796 is one file the main cpp file when you 20271 15:16:57,040 --> 15:17:00,840 compile such a program you're going to 20272 15:17:00,839 --> 15:17:04,159 pre-processing and what this is going to 20273 15:17:04,160 --> 15:17:08,160 it's going to look for your include 20274 15:17:08,160 --> 15:17:12,880 and it is going to take whatever file 20275 15:17:10,400 --> 15:17:15,436 you are including and it is going to 20276 15:17:12,879 --> 15:17:18,159 paste that in place so for example here 20277 15:17:15,436 --> 15:17:19,360 you see we have an include io stream 20278 15:17:19,360 --> 15:17:25,756 when the pre-processing stage is run 20279 15:17:23,436 --> 15:17:28,880 this includes statement is going to be 20280 15:17:25,756 --> 15:17:32,160 replaced with whatever we have in this 20281 15:17:28,879 --> 15:17:34,239 io stream file here so this is going to 20282 15:17:32,160 --> 15:17:38,240 be wiped out and we're going to put in 20283 15:17:34,239 --> 15:17:40,319 the content that we have in io stream in 20284 15:17:38,239 --> 15:17:42,955 this exact place where we have the 20285 15:17:40,319 --> 15:17:45,040 include statement i hope this is clear 20286 15:17:42,955 --> 15:17:46,795 so when we go through preprocessing 20287 15:17:46,796 --> 15:17:52,720 a version of our source files 20288 15:17:49,519 --> 15:17:54,639 with the included parts put in our 20289 15:17:52,720 --> 15:17:56,720 source files we're basically going to go 20290 15:17:54,639 --> 15:17:59,839 copy everything in that file that we 20291 15:17:56,720 --> 15:18:02,796 include and we're going to paste inside 20292 15:17:59,839 --> 15:18:05,915 our new files here that we're going to 20293 15:18:02,796 --> 15:18:08,560 be referring to as translation units by 20294 15:18:05,915 --> 15:18:11,275 now these are translation units in a c 20295 15:18:11,275 --> 15:18:14,955 once you have this translation unit then 20296 15:18:13,436 --> 15:18:16,880 you're going to compile them they are 20297 15:18:14,955 --> 15:18:19,116 going to go through the compiler the 20298 15:18:16,879 --> 15:18:22,639 compiler is going to generate what we 20299 15:18:19,116 --> 15:18:25,596 call an object file and this is a binary 20300 15:18:22,639 --> 15:18:28,080 representation of the content you have 20301 15:18:25,595 --> 15:18:30,399 in your translation unit so each 20302 15:18:28,080 --> 15:18:33,520 translation unit is going to generate 20303 15:18:30,400 --> 15:18:36,480 one object file and for example if you 20304 15:18:33,519 --> 15:18:39,039 had 20 translation units in your program 20305 15:18:36,480 --> 15:18:41,200 you're going to have 20 object files 20306 15:18:39,040 --> 15:18:43,200 once we have object files they are going 20307 15:18:41,199 --> 15:18:44,319 to be processed by what we call the 20308 15:18:44,319 --> 15:18:49,756 and the linker is going to stitch them 20309 15:18:46,319 --> 15:18:52,159 together and have one single binary file 20310 15:18:49,756 --> 15:18:53,916 out of all these object files so we're 20311 15:18:52,160 --> 15:18:56,320 going to have something like this and 20312 15:18:53,915 --> 15:18:59,360 this is basically going to be our binary 20313 15:18:56,319 --> 15:19:01,595 that we can run on our target operating 20314 15:18:59,360 --> 15:19:03,680 system so by now you see that our 20315 15:19:01,595 --> 15:19:05,756 compilation model has expanded to 20316 15:19:05,756 --> 15:19:11,360 pre-processing compilation and linking 20317 15:19:09,040 --> 15:19:14,080 now you might be asking why am i showing 20318 15:19:11,360 --> 15:19:15,436 you this now well it is going to matter 20319 15:19:14,080 --> 15:19:18,560 because we're going to split to a 20320 15:19:15,436 --> 15:19:20,720 program across multiple files some of 20321 15:19:18,559 --> 15:19:23,199 those files are going to matter on the 20322 15:19:20,720 --> 15:19:25,275 compilation phase and some of those 20323 15:19:23,199 --> 15:19:28,079 files are going to matter on the linking 20324 15:19:25,275 --> 15:19:30,159 stage so we really need to make a clear 20325 15:19:28,080 --> 15:19:32,639 distinction between the compilation and 20326 15:19:30,160 --> 15:19:35,916 linking here okay this should be a more 20327 15:19:32,639 --> 15:19:37,756 complete compilation model to go by for 20328 15:19:35,915 --> 15:19:40,000 now now that we know about this 20329 15:19:37,756 --> 15:19:42,239 compilation model we can look at how we 20330 15:19:40,000 --> 15:19:44,400 can split our program across multiple 20331 15:19:42,239 --> 15:19:46,400 files suppose we have three functions 20332 15:19:44,400 --> 15:19:49,436 one we can use to get the maximum of two 20333 15:19:46,400 --> 15:19:51,436 numbers called max a function called men 20334 15:19:49,436 --> 15:19:53,916 we can use to get the minimum between 20335 15:19:51,436 --> 15:19:56,480 two numbers and a function we can use to 20336 15:19:53,915 --> 15:19:57,915 increment and multiply like we have seen 20337 15:19:56,480 --> 15:19:59,680 in the last lecture now the two 20338 15:19:57,915 --> 15:20:02,639 functions maths and men are going to 20339 15:19:59,680 --> 15:20:04,480 live in their own file and that file is 20340 15:20:02,639 --> 15:20:06,955 going to be split in two files we're 20341 15:20:04,480 --> 15:20:08,880 going to have a header file and a cpp 20342 15:20:06,955 --> 15:20:10,955 file which is going to contain the 20343 15:20:08,879 --> 15:20:13,435 implementation in the header what we're 20344 15:20:10,955 --> 15:20:15,519 going to have is just a prototype and we 20345 15:20:13,436 --> 15:20:18,239 are going to set up our code in a way 20346 15:20:15,519 --> 15:20:20,639 that our anchor mult function here is 20347 15:20:18,239 --> 15:20:22,559 going to live in zone file we're going 20348 15:20:20,639 --> 15:20:24,159 to split that in two files so we're 20349 15:20:22,559 --> 15:20:25,680 going to have a header file called 20350 15:20:30,080 --> 15:20:33,436 which is going to contain the 20351 15:20:31,275 --> 15:20:35,519 implementation of this function here 20352 15:20:33,436 --> 15:20:37,275 when we hit the main file what we're 20353 15:20:35,519 --> 15:20:40,000 going to do is nothing special we're 20354 15:20:37,275 --> 15:20:42,000 just going to include the headers and we 20355 15:20:40,000 --> 15:20:44,239 are going to use these functions like we 20356 15:20:42,000 --> 15:20:45,756 have been using them all along if you 20357 15:20:44,239 --> 15:20:48,079 look on the first line here we are 20358 15:20:45,756 --> 15:20:50,796 calling max on the second line we are 20359 15:20:48,080 --> 15:20:53,840 calling men and on the third line here 20360 15:20:50,796 --> 15:20:56,400 we are calling incur mult to do whatever 20361 15:20:53,839 --> 15:20:58,399 it does okay when you start splitting 20362 15:20:56,400 --> 15:21:01,436 your functions between the definitions 20363 15:20:58,400 --> 15:21:04,239 and the declarations remember the one 20364 15:21:01,436 --> 15:21:07,200 definition rule and this says that you 20365 15:21:04,239 --> 15:21:10,000 can't have a function defined 20366 15:21:07,199 --> 15:21:12,079 multiple times in your entire c-plus 20367 15:21:10,000 --> 15:21:14,239 class application if you do that the 20368 15:21:12,080 --> 15:21:16,560 lincoln stage is going to find those two 20369 15:21:16,559 --> 15:21:20,879 not know which one you really want to 20370 15:21:18,796 --> 15:21:22,240 use and you're going to get a linker 20371 15:21:20,879 --> 15:21:24,559 error we're going to see that when we 20372 15:21:22,239 --> 15:21:26,479 hit visual studio code in a minute 20373 15:21:24,559 --> 15:21:29,040 another thing is that you know that your 20374 15:21:26,480 --> 15:21:30,160 definition can really leave anywhere in 20375 15:21:30,160 --> 15:21:35,200 the linker is smart enough to parse your 20376 15:21:35,199 --> 15:21:40,399 the definition for whatever function 20377 15:21:37,595 --> 15:21:42,720 it's found in your source code and it's 20378 15:21:40,400 --> 15:21:45,520 going to keep looking until it finishes 20379 15:21:42,720 --> 15:21:47,756 looking in all translation units and 20380 15:21:45,519 --> 15:21:49,839 when it doesn't find the definition it's 20381 15:21:47,756 --> 15:21:51,756 going to give you a link error which is 20382 15:21:49,839 --> 15:21:53,595 probably going to say undefined 20383 15:21:51,756 --> 15:21:55,840 reference to something and we're going 20384 15:21:53,595 --> 15:21:57,680 to see an example of this in a minute 20385 15:21:55,839 --> 15:21:59,915 okay now that you know about this 20386 15:21:57,680 --> 15:22:03,199 updated compilation model and you have 20387 15:21:59,915 --> 15:22:05,360 an idea that we can split our functions 20388 15:22:03,199 --> 15:22:07,199 across multiple files we're going to 20389 15:22:05,360 --> 15:22:08,160 head over to visual studio code and play 20390 15:22:08,160 --> 15:22:14,240 okay here we are in our working folder 20391 15:22:11,436 --> 15:22:16,400 the current project is multiple files 20392 15:22:14,239 --> 15:22:18,879 here we're going to grab our template 20393 15:22:18,879 --> 15:22:23,839 and put that in the project of interest 20394 15:22:22,239 --> 15:22:25,915 and we're going to open this in visual 20395 15:22:23,839 --> 15:22:28,159 studio code let's do that we're going to 20396 15:22:25,915 --> 15:22:30,000 open the folder i'm going to choose the 20397 15:22:30,000 --> 15:22:34,319 and we are going to open up the main cpp 20398 15:22:34,319 --> 15:22:39,116 and clean it up a little bit we are 20399 15:22:36,955 --> 15:22:41,275 going to set up a max function and 20400 15:22:39,116 --> 15:22:44,080 remember the function has to show up 20401 15:22:44,080 --> 15:22:49,680 and uh we are going to split this into a 20402 15:22:47,116 --> 15:22:51,916 definition and a declaration i think we 20403 15:22:49,680 --> 15:22:53,275 already know how to do that so let's do 20404 15:22:53,275 --> 15:22:58,239 put a definition down here and we're 20405 15:22:55,519 --> 15:23:01,275 going to change what we have on top here 20406 15:22:58,239 --> 15:23:03,839 into a declaration let's say that to be 20407 15:23:01,275 --> 15:23:05,915 super clear and what we have down here 20408 15:23:03,839 --> 15:23:07,519 is a definition and we can call this 20409 15:23:05,915 --> 15:23:09,915 function we're going to set up the 20410 15:23:07,519 --> 15:23:11,275 variable which is going to store our max 20411 15:23:11,275 --> 15:23:15,360 and we're going to assign whatever we 20412 15:23:15,360 --> 15:23:22,080 to max here so we want to find the 20413 15:23:17,680 --> 15:23:23,840 maximum between 34 and 56 for example 20414 15:23:22,080 --> 15:23:26,880 and we're going to print this out i'm 20415 15:23:23,839 --> 15:23:29,435 going to say max is the maximum 20416 15:23:26,879 --> 15:23:31,595 and if we boil and run this program 20417 15:23:29,436 --> 15:23:34,319 let's use gcc to do that we have a 20418 15:23:31,595 --> 15:23:36,559 compiler error here what is that we need 20419 15:23:34,319 --> 15:23:38,480 a semicolon not a column here 20420 15:23:36,559 --> 15:23:41,360 i think the squiggly line is now going 20421 15:23:38,480 --> 15:23:43,200 to go away and our things are named the 20422 15:23:41,360 --> 15:23:45,436 same way the compiler is not going to 20423 15:23:43,199 --> 15:23:48,479 like it let's bring up a terminal so 20424 15:23:45,436 --> 15:23:50,639 that we see our problems tab expression 20425 15:23:48,480 --> 15:23:51,680 preceding parentheses or parallel must 20426 15:23:51,680 --> 15:23:57,116 ah okay the problem here is that these 20427 15:23:54,239 --> 15:23:59,199 two guys max and max are called 20428 15:23:57,116 --> 15:24:01,276 the same and this is going to confuse 20429 15:23:59,199 --> 15:24:04,319 the compiler a little bit so what we're 20430 15:24:01,275 --> 15:24:06,479 going to do is call this maximum 20431 15:24:06,480 --> 15:24:11,916 and we're going to do maximum here 20432 15:24:09,436 --> 15:24:14,840 and the compiler is going to be happy 20433 15:24:11,915 --> 15:24:18,079 now we can compile and i've built this 20434 15:24:14,839 --> 15:24:21,435 program world is good and if we run it 20435 15:24:18,080 --> 15:24:24,639 we should get our maximum printed out 20436 15:24:21,436 --> 15:24:29,436 let's see that the maximum is 56 20437 15:24:24,639 --> 15:24:31,595 and if we change 34 here to be 134 20438 15:24:29,436 --> 15:24:34,160 and both again we should grab that as 20439 15:24:31,595 --> 15:24:35,115 the maximum let's see if that's the case 20440 15:24:35,116 --> 15:24:41,840 run the program the maximum is 134. 20441 15:24:38,796 --> 15:24:44,560 so this is working exactly as we want 20442 15:24:41,839 --> 15:24:47,039 the next thing we want to do is to split 20443 15:24:44,559 --> 15:24:48,559 this function across multiple files so 20444 15:24:47,040 --> 15:24:51,116 we're going to set up two different 20445 15:24:48,559 --> 15:24:53,435 files one is going to be a header file 20446 15:24:51,116 --> 15:24:55,680 the other is going to be a cpp file and 20447 15:24:53,436 --> 15:24:57,520 you have to follow this structure in c 20448 15:24:55,680 --> 15:25:00,239 plus so we're going to go on the left 20449 15:24:57,519 --> 15:25:02,719 here and add a new file in visual studio 20450 15:25:00,239 --> 15:25:05,519 code you can click on this little icon 20451 15:25:02,720 --> 15:25:06,720 that says new file to create a new file 20452 15:25:05,519 --> 15:25:08,795 once you do that you're going to be 20453 15:25:06,720 --> 15:25:10,955 given this little window in which we can 20454 15:25:08,796 --> 15:25:14,560 type the name of the file we're going to 20455 15:25:14,559 --> 15:25:19,040 dot h it's going to be our header file 20456 15:25:17,519 --> 15:25:22,399 and we're going to add another one which 20457 15:25:19,040 --> 15:25:24,480 is going to say compare that cpp okay 20458 15:25:22,400 --> 15:25:27,275 our two files are going to be compare 20459 15:25:27,275 --> 15:25:32,079 and they are going to contain 20460 15:25:29,595 --> 15:25:34,559 our functions to compare stuff so we're 20461 15:25:32,080 --> 15:25:37,040 going to have in a function called max 20462 15:25:34,559 --> 15:25:39,435 and another function called men for 20463 15:25:37,040 --> 15:25:41,595 computing the minimum let's go there and 20464 15:25:39,436 --> 15:25:44,796 actually do that we're going to move our 20465 15:25:41,595 --> 15:25:46,639 declaration in the header file 20466 15:25:44,796 --> 15:25:48,880 so your declarations are going to be 20467 15:25:46,639 --> 15:25:51,915 leaving in header files and we're going 20468 15:25:48,879 --> 15:25:54,360 to grab our definition and move that 20469 15:25:54,360 --> 15:26:01,360 compare.cpp so i'm just going to cut it 20470 15:25:57,275 --> 15:26:02,955 out and paste that and compare cpp here 20471 15:26:01,360 --> 15:26:04,000 once we do that we're going to go back 20472 15:26:04,000 --> 15:26:08,955 and see that we have a problem now 20473 15:26:06,480 --> 15:26:10,796 the main function doesn't know what this 20474 15:26:08,955 --> 15:26:12,879 max function is because it's not 20475 15:26:10,796 --> 15:26:14,480 declared anymore and if we're trying to 20476 15:26:12,879 --> 15:26:16,795 compile our program we're basically 20477 15:26:14,480 --> 15:26:19,596 going to get the same error we see here 20478 15:26:16,796 --> 15:26:22,000 and visual studio code identifier max is 20479 15:26:19,595 --> 15:26:24,479 undefined because it doesn't know what 20480 15:26:22,000 --> 15:26:26,559 this function is if we ignore this and 20481 15:26:24,480 --> 15:26:29,116 try to weld let's do that we are 20482 15:26:26,559 --> 15:26:30,955 learning so no big deal here we're going 20483 15:26:30,955 --> 15:26:36,319 much weirder error but the message is 20484 15:26:34,239 --> 15:26:39,115 going to be the same maths was not 20485 15:26:36,319 --> 15:26:41,116 declared in the scope so the compiler 20486 15:26:39,116 --> 15:26:43,680 doesn't know what to do with whatever we 20487 15:26:41,116 --> 15:26:45,756 typed here but there is a way in c plus 20488 15:26:43,680 --> 15:26:49,116 plus we can solve this problem by 20489 15:26:45,756 --> 15:26:51,520 including the header that contains our 20490 15:26:49,116 --> 15:26:53,756 declaration for the function that we are 20491 15:26:51,519 --> 15:26:55,915 using here we can go back in main and do 20492 15:26:53,756 --> 15:26:59,200 that and we're going to say include and 20493 15:27:00,720 --> 15:27:04,080 that's going to bring in our header and 20494 15:27:02,720 --> 15:27:05,680 if you wait a minute you're going to see 20495 15:27:04,080 --> 15:27:09,756 that the squiggly line is going to go 20496 15:27:05,680 --> 15:27:12,796 away so what happened the preprocessor 20497 15:27:09,756 --> 15:27:15,520 is going to come in and copy whatever we 20498 15:27:12,796 --> 15:27:18,319 have inside compare.h it's basically 20499 15:27:15,519 --> 15:27:21,199 going to go and compare that age copy 20500 15:27:18,319 --> 15:27:24,080 everything we have in here and come back 20501 15:27:21,199 --> 15:27:27,115 in main and paste all that in the place 20502 15:27:24,080 --> 15:27:30,240 where we have the include directive here 20503 15:27:27,116 --> 15:27:33,360 this is called an include directive or 20504 15:27:30,239 --> 15:27:35,436 an include preprocessor directive this 20505 15:27:33,360 --> 15:27:37,915 is what the preprocessor is going to do 20506 15:27:35,436 --> 15:27:40,000 here and now if we try to build this 20507 15:27:37,915 --> 15:27:43,275 program let's try to do that we should 20508 15:27:40,000 --> 15:27:45,595 be fine world finished successfully and 20509 15:27:43,275 --> 15:27:48,159 we can run our program to see the 20510 15:27:45,595 --> 15:27:51,519 maximum printed out let's run rooster 20511 15:27:48,160 --> 15:27:53,596 maximum is 134 now everything is working 20512 15:27:51,519 --> 15:27:55,359 let's add another function here because 20513 15:27:53,595 --> 15:27:57,275 just having one function is really 20514 15:27:55,360 --> 15:27:59,915 boring so we're going to put in a main 20515 15:27:57,275 --> 15:28:01,040 function it's going to type into a and 20516 15:28:01,040 --> 15:28:05,680 and we're going to go in the cpp file 20517 15:28:03,199 --> 15:28:07,519 and put in another definition for the 20518 15:28:05,680 --> 15:28:10,160 minimum function here we're going to 20519 15:28:13,519 --> 15:28:18,399 let's call this man and we're going to 20520 15:28:15,756 --> 15:28:21,275 test for a is less than b this is going 20521 15:28:18,400 --> 15:28:24,400 to give us a minimum and now if we go 20522 15:28:21,275 --> 15:28:26,400 back in main cpp we will be able to use 20523 15:28:24,400 --> 15:28:30,319 our minimum function because it is 20524 15:28:26,400 --> 15:28:33,200 protein also by this preprocessor 20525 15:28:30,319 --> 15:28:35,680 directive we have here so we can go down 20526 15:28:33,199 --> 15:28:36,795 and say end minimum and we're going to 20527 15:28:40,000 --> 15:28:44,879 and 23 why not and we're going to print 20528 15:28:42,955 --> 15:28:47,519 the minimum here and we're going to 20529 15:28:47,519 --> 15:28:52,159 the bullet is going to be good if we run 20530 15:28:49,680 --> 15:28:54,559 this we should see both the maximum and 20531 15:28:52,160 --> 15:28:55,840 the minimum from these calls we make 20532 15:28:55,839 --> 15:29:01,275 okay by now you should have an idea 20533 15:28:57,839 --> 15:29:03,839 about how to split your functions across 20534 15:29:01,275 --> 15:29:07,115 multiple files you should also know that 20535 15:29:03,839 --> 15:29:09,519 you will have to include whatever header 20536 15:29:07,116 --> 15:29:11,916 contains the declarations for the 20537 15:29:09,519 --> 15:29:14,399 functions that you want to use another 20538 15:29:11,915 --> 15:29:16,955 thing i want you to see is that the 20539 15:29:14,400 --> 15:29:20,639 definitions for your functions don't 20540 15:29:16,955 --> 15:29:23,595 have to live in a file that is named the 20541 15:29:20,639 --> 15:29:25,595 same as the header file for example here 20542 15:29:23,595 --> 15:29:27,115 our header file is called compare and 20543 15:29:25,595 --> 15:29:29,839 the comparison tpp contains our 20544 15:29:27,116 --> 15:29:32,400 definitions but the definitions don't 20545 15:29:29,839 --> 15:29:35,275 have to live here they can really live 20546 15:29:32,400 --> 15:29:37,596 in any translation unit that you have in 20547 15:29:37,595 --> 15:29:42,795 and our c plus plus programs the 20548 15:29:39,915 --> 15:29:45,839 translation unit is mostly equivalent to 20549 15:29:42,796 --> 15:29:47,436 a cpp file so let's set up another file 20550 15:29:45,839 --> 15:29:49,359 and show you that we can move this 20551 15:29:47,436 --> 15:29:51,200 definition there and the program is 20552 15:29:49,360 --> 15:29:53,915 going to keep working we're going to add 20553 15:29:51,199 --> 15:29:55,680 another cpp file and call it some 20554 15:29:55,680 --> 15:29:59,360 that cpp it has to be a cpp file because 20555 15:29:59,360 --> 15:30:03,840 implementations and let's take the 20556 15:30:02,000 --> 15:30:06,319 implementations for example let's take 20557 15:30:03,839 --> 15:30:07,199 them all put them in another file why 20558 15:30:07,199 --> 15:30:12,079 we're going to grab all these guys here 20559 15:30:09,595 --> 15:30:14,159 let's catch them out and put them in 20560 15:30:14,160 --> 15:30:18,640 and i'm going to build this and see that 20561 15:30:18,639 --> 15:30:23,275 because the linker is smart enough to go 20562 15:30:23,275 --> 15:30:28,955 and whatever translation units that we 20563 15:30:25,839 --> 15:30:31,435 have set up in our project here and you 20564 15:30:28,955 --> 15:30:33,199 see the world is good we can upload this 20565 15:30:31,436 --> 15:30:35,360 we can run this i should say and we're 20566 15:30:33,199 --> 15:30:38,719 going to have our output here we can 20567 15:30:35,360 --> 15:30:40,680 even take one of these guys let it leave 20568 15:30:40,680 --> 15:30:45,040 compare.cpp file for example let's take 20569 15:30:45,040 --> 15:30:49,915 and we're going to bring that 20570 15:30:46,839 --> 15:30:50,795 back and we're going to work notice that 20571 15:30:50,796 --> 15:30:56,720 our maximum function is living in some 20572 15:30:54,000 --> 15:30:59,199 other file and that the minimum function 20573 15:30:56,720 --> 15:31:02,080 definition is leaving and compare that 20574 15:30:59,199 --> 15:31:04,399 cpp but if we will this is going to work 20575 15:31:04,400 --> 15:31:08,796 going to parse all the translation units 20576 15:31:08,796 --> 15:31:14,000 definition it needs to find and in this 20577 15:31:11,680 --> 15:31:17,116 case it will be looking for the maximum 20578 15:31:14,000 --> 15:31:19,915 here and it's going to find that in some 20579 15:31:19,915 --> 15:31:24,000 minimum function here it's going to be 20580 15:31:21,756 --> 15:31:26,000 looking and compare and it is going to 20581 15:31:24,000 --> 15:31:28,480 find it and the world is going to be 20582 15:31:26,000 --> 15:31:31,519 good and we can run the program again 20583 15:31:31,519 --> 15:31:37,115 one thing i really need to point out is 20584 15:31:34,239 --> 15:31:39,595 that if the definition is not available 20585 15:31:37,116 --> 15:31:42,160 let's say the minimum function here is 20586 15:31:39,595 --> 15:31:44,559 not there we can comment it out to hide 20587 15:31:42,160 --> 15:31:45,916 it to the compiler or hide it from the 20588 15:31:48,160 --> 15:31:53,040 we will get a linker error let's try to 20589 15:31:53,040 --> 15:31:58,239 uh-huh world finished with errol's the 20590 15:31:55,915 --> 15:32:02,000 terminal process terminated with execute 20591 15:31:58,239 --> 15:32:05,040 code -1 but the main message here is 20592 15:32:02,000 --> 15:32:08,000 that we have an undefined reference to 20593 15:32:05,040 --> 15:32:10,319 min and and so when we compile the 20594 15:32:08,000 --> 15:32:13,796 compiler is going to be happy with the 20595 15:32:10,319 --> 15:32:16,639 declarations that it has found in our 20596 15:32:13,796 --> 15:32:19,520 compare.h file if we go there we see 20597 15:32:16,639 --> 15:32:21,839 that we have a declaration for max and 20598 15:32:19,519 --> 15:32:23,435 the declaration for man the compiler is 20599 15:32:21,839 --> 15:32:26,000 going to be happy it's going to compile 20600 15:32:23,436 --> 15:32:28,720 our code and when the compiler compiles 20601 15:32:26,000 --> 15:32:31,040 it's going to generate object files and 20602 15:32:28,720 --> 15:32:33,595 it will hand the job over to the linker 20603 15:32:31,040 --> 15:32:36,559 to stitch all those little object files 20604 15:32:33,595 --> 15:32:38,639 together to have one single binary file 20605 15:32:36,559 --> 15:32:40,559 once the linker tries to do that it's 20606 15:32:38,639 --> 15:32:42,796 going to look at main because that's 20607 15:32:40,559 --> 15:32:44,639 where it's mostly going to start from 20608 15:32:42,796 --> 15:32:47,116 and it's going to see uh-huh main is 20609 15:32:44,639 --> 15:32:49,199 calling minimum here where is the 20610 15:32:47,116 --> 15:32:51,276 definition for the minimum function it's 20611 15:32:49,199 --> 15:32:54,000 going to parse every single translation 20612 15:32:51,275 --> 15:32:55,839 unit in the project it's going to come 20613 15:32:54,000 --> 15:32:57,199 here and compare that cpp it's not going 20614 15:32:55,839 --> 15:32:59,519 to find it it's going to look in 20615 15:32:57,199 --> 15:33:01,199 main.cpp it's not going to find it it's 20616 15:32:59,519 --> 15:33:03,519 going to look in some other file it's 20617 15:33:01,199 --> 15:33:05,115 not going to find the definition and the 20618 15:33:05,116 --> 15:33:09,436 i didn't find my definition for the 20619 15:33:07,040 --> 15:33:11,915 minimum function so i'm going to throw a 20620 15:33:09,436 --> 15:33:14,560 linker error and you the developer 20621 15:33:11,915 --> 15:33:16,239 you're going to fix this problem and 20622 15:33:14,559 --> 15:33:20,000 you're going to see this error here and 20623 15:33:16,239 --> 15:33:21,040 again this is a linker error gcc doesn't 20624 15:33:21,040 --> 15:33:25,680 more visible stuff to show you that this 20625 15:33:23,595 --> 15:33:30,000 is a linker error but what you should 20626 15:33:25,680 --> 15:33:33,915 look for is ld returned 1 here because 20627 15:33:30,000 --> 15:33:35,360 ld is the linker that gcc uses so this 20628 15:33:35,360 --> 15:33:39,840 i think i should show you how a linker 20629 15:33:37,275 --> 15:33:42,400 error looks if you are using a microsoft 20630 15:33:39,839 --> 15:33:46,079 compiler so we're going to build again 20631 15:33:42,400 --> 15:33:47,840 but now we're going to build with msvc 20632 15:33:46,080 --> 15:33:50,560 if you don't have visual studio that's 20633 15:33:47,839 --> 15:33:52,955 not a problem you can keep using gcc but 20634 15:33:50,559 --> 15:33:56,239 i just want you to see how different 20635 15:33:52,955 --> 15:33:57,839 compilers handle these arrows i have the 20636 15:33:56,239 --> 15:33:59,519 luxury to do that because i am 20637 15:33:57,839 --> 15:34:02,559 developing this course on a windows 20638 15:33:59,519 --> 15:34:04,955 machine and i have a microsoft compiler 20639 15:34:02,559 --> 15:34:07,360 handy that we can use directly from 20640 15:34:04,955 --> 15:34:09,116 visual studio code here so i am going to 20641 15:34:09,116 --> 15:34:12,880 and you're going to see that it is going 20642 15:34:10,559 --> 15:34:15,680 to start welding it is building 20643 15:34:12,879 --> 15:34:18,879 and it is going to fail and throw a link 20644 15:34:15,680 --> 15:34:22,080 arrow also so you see here some other 20645 15:34:22,879 --> 15:34:27,755 this object file was being taken and 20646 15:34:25,360 --> 15:34:30,639 stitched together with other object 20647 15:34:27,756 --> 15:34:32,880 files to put together a binary it didn't 20648 15:34:30,639 --> 15:34:34,479 find the definition for the minimum 20649 15:34:32,879 --> 15:34:36,879 function and it is going to throw a 20650 15:34:34,480 --> 15:34:39,680 linker error and in visual studio you're 20651 15:34:36,879 --> 15:34:40,879 going to have a problem like this l and 20652 15:34:40,879 --> 15:34:44,879 and it's going to give whatever version 20653 15:34:42,720 --> 15:34:47,436 of the compiler you are using but down 20654 15:34:44,879 --> 15:34:51,115 here you see unresolved external symbol 20655 15:34:47,436 --> 15:34:53,040 this is how visual studio says it and 20656 15:34:53,040 --> 15:34:57,915 an undefined reference to something this 20657 15:34:55,360 --> 15:35:00,239 is just visual studio's compiler's way 20658 15:34:57,915 --> 15:35:02,159 to say this and again it's going to say 20659 15:35:02,160 --> 15:35:06,720 it is basically the same error we saw 20660 15:35:04,319 --> 15:35:09,040 when we were using gcc in a minute video 20661 15:35:06,720 --> 15:35:11,275 studio the compiler from microsoft is 20662 15:35:09,040 --> 15:35:13,200 also going to put a lot of junk in our 20663 15:35:11,275 --> 15:35:17,199 folder here so let's remove all these 20664 15:35:13,199 --> 15:35:20,159 things i'm going to select them all 20665 15:35:20,160 --> 15:35:24,720 and they are going to go to my recycle 20666 15:35:22,160 --> 15:35:28,080 bin and we have another one here we 20667 15:35:24,720 --> 15:35:30,595 should remove let's remove it 20668 15:35:28,080 --> 15:35:33,520 and now we should go back in some other 20669 15:35:30,595 --> 15:35:35,839 file.cpp or compare.cpp that's where we 20670 15:35:33,519 --> 15:35:38,399 have a minimum function we can activate 20671 15:35:35,839 --> 15:35:40,239 it and if we're both now the world 20672 15:35:38,400 --> 15:35:42,720 should be good because the linker is 20673 15:35:40,239 --> 15:35:45,519 going to find this definition here 20674 15:35:42,720 --> 15:35:47,840 okay let's build with gcc because that's 20675 15:35:47,839 --> 15:35:52,399 now you see that the world is good we 20676 15:35:49,915 --> 15:35:54,955 can clear and run rooster and our 20677 15:35:52,400 --> 15:35:57,520 program is going to work as it should 20678 15:35:54,955 --> 15:36:00,400 this is really how you can split your 20679 15:35:57,519 --> 15:36:01,915 functions across multiple files the 20680 15:36:00,400 --> 15:36:04,480 headers are going to contain the 20681 15:36:01,915 --> 15:36:07,040 declarations the cpp files are going to 20682 15:36:04,480 --> 15:36:08,796 contain the definitions your definitions 20683 15:36:07,040 --> 15:36:11,915 can really leave anywhere it's going to 20684 15:36:08,796 --> 15:36:14,720 be a matter of what works better for 20685 15:36:11,915 --> 15:36:17,360 your project but for organizational 20686 15:36:14,720 --> 15:36:18,955 purposes your cpp files are going to be 20687 15:36:17,360 --> 15:36:21,756 basically like this you're going to have 20688 15:36:18,955 --> 15:36:23,275 some declarations in the header like 20689 15:36:23,275 --> 15:36:28,639 and in a corresponding cpp file you're 20690 15:36:26,239 --> 15:36:30,479 going to have and the definitions for 20691 15:36:28,639 --> 15:36:32,720 whatever declarations you have in the 20692 15:36:30,480 --> 15:36:34,480 header but you don't have to do it this 20693 15:36:32,720 --> 15:36:36,639 way if it makes sense for your 20694 15:36:34,480 --> 15:36:38,640 definition to show up somewhere else 20695 15:36:36,639 --> 15:36:40,639 please go ahead and do that c plus plus 20696 15:36:38,639 --> 15:36:43,199 is going to accept that this is really 20697 15:36:40,639 --> 15:36:45,519 all we set out to do in this lecture but 20698 15:36:43,199 --> 15:36:47,755 before we go i'm going to give you a 20699 15:36:45,519 --> 15:36:51,199 chance to set up another function 20700 15:36:47,756 --> 15:36:54,400 suppose we have a function called 20701 15:36:51,199 --> 15:36:56,319 anchor malt and we're going to have the 20702 15:36:54,400 --> 15:36:58,000 function set up in here we're going to 20703 15:36:59,839 --> 15:37:03,275 we already know how to do these things 20704 15:37:01,519 --> 15:37:07,595 we've used this example a couple of 20705 15:37:07,595 --> 15:37:12,795 and that's what we're going to return 20706 15:37:09,756 --> 15:37:14,080 now your job is to split this function 20707 15:37:14,080 --> 15:37:19,040 a declaration and a definition and put 20708 15:37:16,796 --> 15:37:21,756 those in two separate files one is going 20709 15:37:19,040 --> 15:37:23,319 to be called operations.h and the other 20710 15:37:23,319 --> 15:37:28,559 operations.cpp pause the video go ahead 20711 15:37:26,720 --> 15:37:31,520 set up your project like that and call 20712 15:37:28,559 --> 15:37:33,839 that function in main cpp and see if you 20713 15:37:31,519 --> 15:37:35,915 can pull that together okay post the 20714 15:37:33,839 --> 15:37:37,915 video do that and once you are done come 20715 15:37:35,915 --> 15:37:39,915 back again and see how i do this so 20716 15:37:37,915 --> 15:37:42,719 we're going to set up a new file called 20717 15:37:42,720 --> 15:37:47,360 it's going to be a header file dot h and 20718 15:37:45,680 --> 15:37:50,319 we're going to add another file called 20719 15:37:50,319 --> 15:37:56,239 that's going to be our cpp file which is 20720 15:37:52,955 --> 15:37:58,479 going to contain our definitions we're 20721 15:37:56,239 --> 15:38:01,115 going to go back in main and grab the 20722 15:37:58,480 --> 15:38:03,756 entire function here we don't want it in 20723 15:38:05,915 --> 15:38:10,399 we should go in the operations header 20724 15:38:10,400 --> 15:38:13,756 and we're going to put in our definition 20725 15:38:12,720 --> 15:38:15,756 and we're going to put in our 20726 15:38:13,756 --> 15:38:17,680 declaration the declaration doesn't have 20727 15:38:15,756 --> 15:38:19,595 a body so we're just going to put a 20728 15:38:17,680 --> 15:38:21,915 semicolon at the end here you already 20729 15:38:19,595 --> 15:38:24,159 know this we're going to go in the cpp 20730 15:38:21,915 --> 15:38:26,559 file and put in our definition 20731 15:38:24,160 --> 15:38:28,160 this is going to work and to be able to 20732 15:38:30,639 --> 15:38:34,796 corresponding header here why is this 20733 15:38:37,199 --> 15:38:44,639 it's cpp we're going to rename it 20734 15:38:40,160 --> 15:38:47,200 and we're going to change this to cpp 20735 15:38:44,639 --> 15:38:48,639 now we should be fine we should have our 20736 15:38:48,639 --> 15:38:53,756 properly and uh to use this function we 20737 15:38:51,595 --> 15:38:56,319 will need to include the corresponding 20738 15:38:53,756 --> 15:38:58,480 header which is operations.h 20739 15:38:56,319 --> 15:39:02,000 let's do that we're going to go in main 20740 15:39:09,199 --> 15:39:14,399 and notice that the way we include our 20741 15:39:11,839 --> 15:39:15,435 own headers is different from the way we 20742 15:39:15,436 --> 15:39:20,955 the libraries or headers that come with 20743 15:39:18,480 --> 15:39:22,720 the c plus plus standard library this is 20744 15:39:20,955 --> 15:39:25,040 a conversion that is used by many 20745 15:39:22,720 --> 15:39:27,595 compilers this is how we will have to do 20746 15:39:25,040 --> 15:39:29,840 things please remember this if you are 20747 15:39:27,595 --> 15:39:30,720 including your own header please put 20748 15:39:30,720 --> 15:39:36,160 in the quotes like we do here if you are 20749 15:39:33,680 --> 15:39:38,955 including a standard library header 20750 15:39:36,160 --> 15:39:40,400 please use angle brackets like this now 20751 15:39:40,400 --> 15:39:43,756 included we can go down here and say 20752 15:39:43,756 --> 15:39:46,400 we're going to set up two variables x 20753 15:39:46,400 --> 15:39:53,040 why not we're going to have a 4 here 20754 15:39:49,915 --> 15:39:55,519 and we're going to have a y which is a 20755 15:39:53,040 --> 15:39:57,756 five let's do that and we're going to 20756 15:39:55,519 --> 15:40:00,639 have an integer to store our results 20757 15:39:57,756 --> 15:40:04,000 it's going to say anchor mount and it's 20758 15:40:00,639 --> 15:40:06,159 going to do the operations on x 20759 15:40:04,000 --> 15:40:08,879 and y let's do that and we're going to 20760 15:40:06,160 --> 15:40:11,756 print the results here so we're going to 20761 15:40:12,319 --> 15:40:17,756 and it's going to be whatever we have in 20762 15:40:18,080 --> 15:40:22,480 okay if we build this program it is 20763 15:40:19,915 --> 15:40:25,360 going to work we're going to use gcc to 20764 15:40:22,480 --> 15:40:27,200 world the world is good if we run it we 20765 15:40:27,199 --> 15:40:32,955 see our output here going to run rooster 20766 15:40:30,160 --> 15:40:34,956 and the result is 24 because 20767 15:40:32,955 --> 15:40:36,639 okay if we increment x we're going to 20768 15:40:36,639 --> 15:40:40,319 and if we increment a 5 we're going to 20769 15:40:40,319 --> 15:40:45,436 if we multiply these guys we should get 20770 15:40:42,559 --> 15:40:47,756 a 30 and that's not what we have here so 20771 15:40:45,436 --> 15:40:49,275 what is happening let's look at our 20772 15:40:54,319 --> 15:40:59,275 because we want to increment immediately 20773 15:41:02,559 --> 15:41:06,955 and we are going to clear and run 20774 15:41:04,319 --> 15:41:09,275 rooster now we have the result that we 20775 15:41:06,955 --> 15:41:11,360 expect let's go back in main and look at 20776 15:41:09,275 --> 15:41:14,479 our program this is what we are using 20777 15:41:11,360 --> 15:41:16,319 here and this is the output we expect 20778 15:41:14,480 --> 15:41:18,720 this is really all we set out to do in 20779 15:41:16,319 --> 15:41:21,360 this lecture showing you how you can 20780 15:41:21,360 --> 15:41:26,239 functions across multiple files this is 20781 15:41:24,160 --> 15:41:28,640 really good if you want to organize your 20782 15:41:26,239 --> 15:41:30,795 project for example somebody who comes 20783 15:41:28,639 --> 15:41:32,559 to the header here is not going to be 20784 15:41:30,796 --> 15:41:34,720 bothered by looking at all the 20785 15:41:32,559 --> 15:41:38,239 definitions and the details of how we 20786 15:41:34,720 --> 15:41:40,880 compute this maximum and minimum and we 20787 15:41:38,239 --> 15:41:43,436 have the flexibility now to move our 20788 15:41:40,879 --> 15:41:45,839 headers in some other file because we 20789 15:41:43,436 --> 15:41:48,796 really want to keep the main function or 20790 15:41:45,839 --> 15:41:50,955 the main file here really clean we don't 20791 15:41:48,796 --> 15:41:53,200 want all those definitions to show up 20792 15:41:50,955 --> 15:41:55,915 here because it is possible for your 20793 15:41:53,199 --> 15:41:58,159 project to contain hundreds or even 20794 15:41:55,915 --> 15:42:00,795 thousands of definitions if you make all 20795 15:41:58,160 --> 15:42:03,436 those declarations show up in the main 20796 15:42:00,796 --> 15:42:06,400 cpp file here that's going to be really 20797 15:42:03,436 --> 15:42:09,040 hard to follow and work with now we have 20798 15:42:06,400 --> 15:42:11,520 the flexibility to split those guys 20799 15:42:09,040 --> 15:42:14,319 across multiple files and we can even 20800 15:42:11,519 --> 15:42:16,559 use lots of headers if that is what is 20801 15:42:14,319 --> 15:42:18,000 going to work for our project this is 20802 15:42:16,559 --> 15:42:20,079 really all we set out to do in this 20803 15:42:18,000 --> 15:42:22,319 lecture i hope you found it interesting 20804 15:42:20,080 --> 15:42:23,840 we are going to stop here in this one in 20805 15:42:22,319 --> 15:42:26,839 the next one we're going to see how we 20806 15:42:23,839 --> 15:42:29,839 can pass parameters to functions by 20807 15:42:26,839 --> 15:42:31,115 value go ahead and finish up here and 20808 15:42:31,116 --> 15:42:35,436 in this lecture we're going to see what 20809 15:42:32,720 --> 15:42:37,916 is meant by passing parameters to a 20810 15:42:35,436 --> 15:42:40,080 function by value and we're going to 20811 15:42:37,915 --> 15:42:42,159 look at an example of that here we have 20812 15:42:40,080 --> 15:42:43,840 a simple function called say age it's 20813 15:42:42,160 --> 15:42:46,560 going to take one parameter which is an 20814 15:42:43,839 --> 15:42:49,679 integer called age and in the body of 20815 15:42:46,559 --> 15:42:52,079 the function we are printing out the age 20816 15:42:49,680 --> 15:42:53,519 after incrementing it suppose this is 20817 15:42:53,519 --> 15:42:58,559 for your program the function is split 20818 15:42:56,080 --> 15:43:01,756 into declaration and definition we have 20819 15:42:58,559 --> 15:43:04,879 a declaration on top here and we have 20820 15:43:01,756 --> 15:43:06,796 our definition below the main function 20821 15:43:04,879 --> 15:43:08,795 in the main function we will attempt to 20822 15:43:06,796 --> 15:43:11,360 use this function we're going to call it 20823 15:43:08,796 --> 15:43:14,080 down here and we're going to pass a 20824 15:43:11,360 --> 15:43:17,275 local variable which is called age as 20825 15:43:14,080 --> 15:43:19,916 our age when we do that we expect this 20826 15:43:17,275 --> 15:43:21,839 to print you are 24 years old because 20827 15:43:19,915 --> 15:43:23,755 we're going to increment the age and 20828 15:43:21,839 --> 15:43:25,679 we're going to say hello you are 24 20829 15:43:23,756 --> 15:43:28,880 years old this is what this program is 20830 15:43:25,680 --> 15:43:31,915 going to do but we want to keep track 20831 15:43:28,879 --> 15:43:34,559 of how the outside variable here is 20832 15:43:31,915 --> 15:43:37,199 going to change and the age we have 20833 15:43:34,559 --> 15:43:39,915 passed as a parameter here because this 20834 15:43:37,199 --> 15:43:42,795 edge we pass as a parameter is not going 20835 15:43:39,915 --> 15:43:45,115 to be the same as the age we have on top 20836 15:43:42,796 --> 15:43:47,680 here what we pass here as a parameter is 20837 15:43:45,116 --> 15:43:50,080 going to be a copy of the actual 20838 15:43:47,680 --> 15:43:52,639 variable that we passed here and this 20839 15:43:50,080 --> 15:43:55,116 mechanism is called passing a parameter 20840 15:43:52,639 --> 15:43:57,756 by value and what you really pass is 20841 15:43:55,116 --> 15:43:59,916 going to be copy and what you work on 20842 15:43:57,756 --> 15:44:02,000 inside the body of the function is not 20843 15:43:59,915 --> 15:44:04,559 going to be the original stuff you 20844 15:44:02,000 --> 15:44:06,559 passed as an argument to the function so 20845 15:44:04,559 --> 15:44:08,319 we're going to try and prove that here 20846 15:44:06,559 --> 15:44:10,159 we're going to print the age before we 20847 15:44:08,319 --> 15:44:12,639 call the function we're going to print 20848 15:44:10,160 --> 15:44:14,720 the age after we call the function and 20849 15:44:12,639 --> 15:44:17,436 what we print here is going to be the 20850 15:44:14,720 --> 15:44:18,559 outside variable because here we are in 20851 15:44:20,400 --> 15:44:26,239 inside the say edge function what we're 20852 15:44:23,199 --> 15:44:28,719 going to be modifying is a locker copy 20853 15:44:26,239 --> 15:44:32,000 so we expect the edge here to stay the 20854 15:44:28,720 --> 15:44:33,756 same before and after the function call 20855 15:44:32,000 --> 15:44:36,559 so the way this is going to work when 20856 15:44:33,756 --> 15:44:38,319 control has this function here it's like 20857 15:44:36,559 --> 15:44:40,955 we're going to have a situation like we 20858 15:44:38,319 --> 15:44:43,680 have here on the left we're going to set 20859 15:44:40,955 --> 15:44:46,559 up local copies in the function that are 20860 15:44:43,680 --> 15:44:49,275 going to contain whatever was passed as 20861 15:44:46,559 --> 15:44:51,680 an argument to this function and this 20862 15:44:49,275 --> 15:44:53,756 edge here was passed as an argument to a 20863 15:44:51,680 --> 15:44:56,080 function so we're going to have a local 20864 15:44:53,756 --> 15:44:57,756 copy of this h and we're going to store 20865 15:44:56,080 --> 15:45:00,639 it and it is what we're going to be 20866 15:44:57,756 --> 15:45:03,040 manipulating in our program when the 20867 15:45:00,639 --> 15:45:05,360 function goes out of scope the local 20868 15:45:03,040 --> 15:45:07,040 copy is going to die and the outside 20869 15:45:05,360 --> 15:45:09,595 variable we have here is basically going 20870 15:45:07,040 --> 15:45:11,915 to be unaffected let's head over to 20871 15:45:09,595 --> 15:45:13,115 visual studio code and show you this in 20872 15:45:13,116 --> 15:45:18,160 okay here we are in our working folder 20873 15:45:15,915 --> 15:45:20,639 the current project is passed by value 20874 15:45:18,160 --> 15:45:22,880 we're going to grab our template files 20875 15:45:20,639 --> 15:45:25,595 and we're going to put them in place in 20876 15:45:22,879 --> 15:45:28,079 the correct project and we're going to 20877 15:45:25,595 --> 15:45:30,000 open this up in visual studio code let's 20878 15:45:28,080 --> 15:45:33,040 do that we're going to open the folder 20879 15:45:30,000 --> 15:45:36,400 that contains the project pass by value 20880 15:45:33,040 --> 15:45:39,520 let's do that we are going to open it up 20881 15:45:36,400 --> 15:45:42,080 and i'm going to go in the file and 20882 15:45:39,519 --> 15:45:44,079 clean it up a little bit and we are 20883 15:45:42,080 --> 15:45:46,000 going to set up a function it's going to 20884 15:45:49,040 --> 15:45:52,879 and it's going to take an integer which 20885 15:45:52,879 --> 15:45:57,199 and inside we're going to increment our 20886 15:45:55,040 --> 15:45:59,595 h i think this is what we saw in the 20887 15:45:57,199 --> 15:46:01,595 slides and we're going to say however 20888 15:45:59,595 --> 15:46:02,879 many years you are old we're going to 20889 15:46:04,480 --> 15:46:09,360 age old and we can set up a variable and 20890 15:46:07,595 --> 15:46:10,559 the main function we're going to call it 20891 15:46:10,559 --> 15:46:15,915 and it is going to contain 23 as our age 20892 15:46:14,239 --> 15:46:19,040 we're going to say age and we're going 20893 15:46:15,915 --> 15:46:20,719 to pass in our age here we can do that 20894 15:46:19,040 --> 15:46:22,796 if we're trying to run this program 20895 15:46:20,720 --> 15:46:25,916 let's try to do that we're going to 20896 15:46:22,796 --> 15:46:28,080 build with gcc and we are going to bring 20897 15:46:25,915 --> 15:46:30,079 in a terminal we can use to run this 20898 15:46:30,080 --> 15:46:33,596 visual studio code here let's run 20899 15:46:32,000 --> 15:46:36,080 booster we're going to say hello you are 20900 15:46:33,595 --> 15:46:38,319 24 years old and this is what we expect 20901 15:46:36,080 --> 15:46:39,840 because when this function is called 20902 15:46:38,319 --> 15:46:42,480 we're going to increment the age then 20903 15:46:39,839 --> 15:46:43,519 we're going to say the age but we want 20904 15:46:43,519 --> 15:46:49,199 what happens to the outside edge ear 20905 15:46:46,796 --> 15:46:51,276 if we modify the inside edge that we 20906 15:46:49,199 --> 15:46:53,680 have on this function here that was 20907 15:46:51,275 --> 15:46:55,436 passed as an argument and again what you 20908 15:46:53,680 --> 15:46:57,595 pass when you are declaring your 20909 15:46:55,436 --> 15:46:59,520 function is the parameter and what you 20910 15:46:57,595 --> 15:47:01,915 pass when you are calling the function 20911 15:46:59,519 --> 15:47:04,000 is called an argument in c plus plus 20912 15:47:01,915 --> 15:47:06,479 please remember this okay what we pass 20913 15:47:04,000 --> 15:47:09,680 as an argument is our anti-age variable 20914 15:47:06,480 --> 15:47:11,200 which is local to the main function 20915 15:47:09,680 --> 15:47:14,319 and we're going to see what happens to 20916 15:47:17,360 --> 15:47:21,040 i think this is descriptive enough and 20917 15:47:19,275 --> 15:47:23,595 we're going to print that and we're 20918 15:47:21,040 --> 15:47:25,680 going to go after the function is called 20919 15:47:23,595 --> 15:47:28,000 we're going to say the same thing but 20920 15:47:25,680 --> 15:47:28,796 we're going to say after call 20921 15:47:28,796 --> 15:47:32,796 by after we mean after we call the 20922 15:47:30,796 --> 15:47:34,720 function say h here i think this is 20923 15:47:32,796 --> 15:47:37,520 descriptive enough and we're going to 20924 15:47:34,720 --> 15:47:40,080 see that if incrementing the local 20925 15:47:37,519 --> 15:47:42,159 variable to the function here affects 20926 15:47:40,080 --> 15:47:45,040 the variable that we have and the scope 20927 15:47:42,160 --> 15:47:47,756 of the main function okay so let's see 20928 15:47:45,040 --> 15:47:50,239 that we're going to build with gcc as 20929 15:47:50,239 --> 15:47:54,879 and the wealth is good so let's clear 20930 15:47:52,720 --> 15:47:57,840 and run rooster you're going to see that 20931 15:47:54,879 --> 15:48:00,559 before the call we have 23 and after the 20932 15:47:57,839 --> 15:48:02,000 call we have 23 we didn't change the 20933 15:48:02,000 --> 15:48:06,239 this is a classical copy paste error 20934 15:48:06,239 --> 15:48:10,079 for our program to really be descriptive 20935 15:48:10,080 --> 15:48:15,116 okay the world is good we can clear and 20936 15:48:12,319 --> 15:48:18,879 run rooster and now we have before call 20937 15:48:15,116 --> 15:48:22,000 23 after call age is 23 it wasn't 20938 15:48:18,879 --> 15:48:24,239 changed by what we have inside here and 20939 15:48:22,000 --> 15:48:26,559 we can even go further and look at the 20940 15:48:24,239 --> 15:48:29,595 memory addresses of these guys 20941 15:48:26,559 --> 15:48:31,915 here we can try and print the address of 20942 15:48:29,595 --> 15:48:34,159 the age variable let's let's do that 20943 15:48:36,400 --> 15:48:41,040 and put that out here we're going to say 20944 15:48:41,040 --> 15:48:46,480 no big deal here we can close the pane 20945 15:48:43,199 --> 15:48:48,795 on the left here because it is taking up 20946 15:48:46,480 --> 15:48:50,956 valuable space and we're going to go to 20947 15:48:48,796 --> 15:48:53,040 the second statement and put in the 20948 15:48:53,040 --> 15:48:56,480 and we're going to print that out we can 20949 15:49:00,796 --> 15:49:04,956 and when we hit the body of our function 20950 15:49:03,199 --> 15:49:06,079 here we can also print the address of 20951 15:49:09,756 --> 15:49:15,436 address of age let's do that here and 20952 15:49:12,639 --> 15:49:17,839 we're going to print the address of the 20953 15:49:15,436 --> 15:49:20,880 local edge variable here which was 20954 15:49:17,839 --> 15:49:23,519 passed as a parameter here okay now we 20955 15:49:20,879 --> 15:49:27,519 can build our program and watch those 20956 15:49:23,519 --> 15:49:29,680 addresses we're going to build with gcc 20957 15:49:27,519 --> 15:49:31,275 the world is good we can clear 20958 15:49:31,275 --> 15:49:36,479 now you see that the outside variable is 20959 15:49:33,839 --> 15:49:38,000 going to leave at an address that ends 20960 15:49:38,000 --> 15:49:43,275 and the local variable is going to leave 20961 15:49:39,839 --> 15:49:46,079 at an address that ends in dc0 so this 20962 15:49:43,275 --> 15:49:48,400 is a completely new memory location that 20963 15:49:46,080 --> 15:49:51,916 we are using and it is just going to 20964 15:49:48,400 --> 15:49:54,000 contain a copy of what we passed as a 20965 15:49:51,915 --> 15:49:56,239 parameter here and the compiler is going 20966 15:49:54,000 --> 15:49:58,955 to take care of setting up this copy 20967 15:49:56,239 --> 15:50:00,559 variable and have it available for use 20968 15:49:58,955 --> 15:50:02,795 in the local function here we don't have 20969 15:50:00,559 --> 15:50:06,079 to worry about that but we have to be 20970 15:50:02,796 --> 15:50:08,956 aware that what we are working on and 20971 15:50:06,080 --> 15:50:11,200 the same age function here is a copy 20972 15:50:08,955 --> 15:50:12,639 it's not the original value this is 20973 15:50:11,199 --> 15:50:15,519 really all we set out to do in this 20974 15:50:12,639 --> 15:50:18,559 lecture making you aware of the call by 20975 15:50:15,519 --> 15:50:21,199 value mechanism and the main message is 20976 15:50:18,559 --> 15:50:23,435 that what you are working on in the body 20977 15:50:21,199 --> 15:50:25,915 of your function when it is called is 20978 15:50:23,436 --> 15:50:29,116 going to be a copy of what you have 20979 15:50:25,915 --> 15:50:30,795 passed as an argument when your function 20980 15:50:30,796 --> 15:50:35,116 in this lecture we're going to see how 20981 15:50:32,319 --> 15:50:37,360 we can pass parameters to functions by 20982 15:50:35,116 --> 15:50:39,276 pointer this is going to allow us to 20983 15:50:37,360 --> 15:50:41,595 avoid the copies that we were 20984 15:50:39,275 --> 15:50:45,115 experiencing when we were passing 20985 15:50:41,595 --> 15:50:47,756 parameters by value like we have seen in 20986 15:50:45,116 --> 15:50:50,240 the last few lectures here is a simple 20987 15:50:47,756 --> 15:50:52,400 example showing the syntax we can use to 20988 15:50:52,400 --> 15:50:57,436 on top here we have our function 20989 15:50:54,720 --> 15:50:59,840 declaration and down here we have our 20990 15:50:57,436 --> 15:51:02,080 function definition and you see that the 20991 15:50:59,839 --> 15:51:04,639 syntax is the same we are passing by 20992 15:51:02,080 --> 15:51:07,200 pointer and by pointer and the 20993 15:51:04,639 --> 15:51:09,199 definition here and another thing you 20994 15:51:07,199 --> 15:51:12,000 should be careful about is that when you 20995 15:51:09,199 --> 15:51:14,639 call a function that passes by pointer 20996 15:51:12,000 --> 15:51:16,559 you have to give it an address and 20997 15:51:14,639 --> 15:51:20,159 that's what we are doing here so we are 20998 15:51:16,559 --> 15:51:22,399 passing the address of our age variable 20999 15:51:20,160 --> 15:51:24,720 which is going to be passed by pointer 21000 15:51:22,400 --> 15:51:28,639 when we pass by pointer we want to be 21001 15:51:24,720 --> 15:51:31,360 modifying a copy inside the body of our 21002 15:51:28,639 --> 15:51:34,319 function if you look here we are 21003 15:51:31,360 --> 15:51:36,955 incrementing the edge variable but we 21004 15:51:34,319 --> 15:51:39,040 are doing this through the referencing 21005 15:51:36,955 --> 15:51:41,275 so when we pass a pointer here the 21006 15:51:39,040 --> 15:51:42,879 pointer is going to contain the address 21007 15:51:42,879 --> 15:51:48,079 and in the body of the function the 21008 15:51:45,595 --> 15:51:51,435 pointer we will have this edge pointer 21009 15:51:48,080 --> 15:51:54,319 is going to be pointing to the original 21010 15:51:51,436 --> 15:51:56,319 variable here okay so this edge pointer 21011 15:51:54,319 --> 15:51:57,595 is going to be pointing to the original 21012 15:51:57,595 --> 15:52:02,159 when we dereference it and modify the 21013 15:52:00,160 --> 15:52:03,840 variable we're going to be affecting the 21014 15:52:02,160 --> 15:52:05,916 original variable i really hope this 21015 15:52:03,839 --> 15:52:08,399 makes sense and this is really what 21016 15:52:05,915 --> 15:52:10,239 passing by pointer is all about you 21017 15:52:08,400 --> 15:52:12,560 declare your functions by passing in 21018 15:52:10,239 --> 15:52:14,795 pointers like this and in the definition 21019 15:52:12,559 --> 15:52:17,435 you do the same and when you call the 21020 15:52:14,796 --> 15:52:19,756 function you need to pass in an address 21021 15:52:17,436 --> 15:52:21,840 of the variable because that's what we 21022 15:52:19,756 --> 15:52:23,916 store in a pointer here and this is 21023 15:52:21,839 --> 15:52:26,719 going to work if we try to run this 21024 15:52:23,915 --> 15:52:29,435 example here we're going to print 23 21025 15:52:26,720 --> 15:52:31,360 because that's the original value we are 21026 15:52:29,436 --> 15:52:33,436 going to call the say age we are going 21027 15:52:31,360 --> 15:52:35,520 to increment and this is going to change 21028 15:52:33,436 --> 15:52:38,955 the original value if we print here 21029 15:52:35,519 --> 15:52:41,275 we're going to get 24 and when this 21030 15:52:38,955 --> 15:52:43,040 function terminates we're going to hit 21031 15:52:41,275 --> 15:52:46,239 this statement here and we're going to 21032 15:52:43,040 --> 15:52:48,400 print 24 this is what we're going to see 21033 15:52:46,239 --> 15:52:50,000 when we get to run this program here and 21034 15:52:48,400 --> 15:52:52,560 this is really all there is about 21035 15:52:50,000 --> 15:52:54,955 passing by pointer we are going to head 21036 15:52:52,559 --> 15:52:58,479 over to visual studio code and play with 21037 15:52:54,955 --> 15:53:00,795 us a little more okay here we are in our 21038 15:52:58,480 --> 15:53:03,200 working folder the current project is 21039 15:53:00,796 --> 15:53:05,520 passed by pointer we are going to grab 21040 15:53:05,519 --> 15:53:11,115 and we're going to put them in the 21041 15:53:07,275 --> 15:53:12,879 current project which is pass by pointer 21042 15:53:11,116 --> 15:53:14,880 we are going to open this in visual 21043 15:53:12,879 --> 15:53:18,079 studio code pretty quick we're going to 21044 15:53:14,879 --> 15:53:20,795 choose that pass by pointer we open this 21045 15:53:18,080 --> 15:53:21,596 and our main cpp file should show up 21046 15:53:21,595 --> 15:53:27,519 we're going to clean it up a little bit 21047 15:53:25,275 --> 15:53:29,756 we are actually going to grab the code 21048 15:53:27,519 --> 15:53:32,479 from the last lecture and put that in 21049 15:53:29,756 --> 15:53:34,796 here and that's going to be our starting 21050 15:53:32,480 --> 15:53:37,116 point let's close the left pane here so 21051 15:53:34,796 --> 15:53:39,840 that we have some breathing space the 21052 15:53:37,116 --> 15:53:42,319 function here say h is going to pass by 21053 15:53:39,839 --> 15:53:43,435 pointer so we should take out this cost 21054 15:53:43,436 --> 15:53:48,796 and uh have a pointer here like we had 21055 15:53:46,080 --> 15:53:50,160 in the slides we're going to do the same 21056 15:53:50,160 --> 15:53:54,080 definition here if we go down we're 21057 15:53:52,400 --> 15:53:56,400 going to do that we're going to pass by 21058 15:53:54,080 --> 15:53:59,360 pointer and we are going to uncomment 21059 15:53:56,400 --> 15:54:01,200 the line that increments our age here 21060 15:53:59,360 --> 15:54:03,520 okay after we do that you see that we 21061 15:54:01,199 --> 15:54:06,879 have a squiggly line at this line that 21062 15:54:03,519 --> 15:54:09,755 calls the say age function what we need 21063 15:54:06,879 --> 15:54:12,319 to do is to pass it the address of the 21064 15:54:09,756 --> 15:54:14,480 edge variable here like we saw in the 21065 15:54:12,319 --> 15:54:15,519 slide so this is the syntax we have to 21066 15:54:15,519 --> 15:54:19,915 once we do this this program is going to 21067 15:54:17,519 --> 15:54:22,399 be valid and if we run it here we're 21068 15:54:19,915 --> 15:54:24,399 going to print 23 because that's the 21069 15:54:22,400 --> 15:54:26,720 value we have in here we're going to 21070 15:54:24,400 --> 15:54:29,596 call the say age function we are going 21071 15:54:26,720 --> 15:54:32,720 to increment age and here we forgot to 21072 15:54:29,595 --> 15:54:35,680 do our dereferencing so what we need to 21073 15:54:32,720 --> 15:54:38,160 do here is to dereference the age and 21074 15:54:35,680 --> 15:54:40,239 increment it this is going to increment 21075 15:54:38,160 --> 15:54:42,640 the original variable here and we're 21076 15:54:40,239 --> 15:54:45,519 going to be accessing this through the 21077 15:54:42,639 --> 15:54:48,000 the referencing operator of our pointer 21078 15:54:45,519 --> 15:54:50,639 here if we hit this line and the 21079 15:54:48,000 --> 15:54:52,796 function here we're going to print 24 21080 15:54:52,796 --> 15:54:58,400 incremented through this statement here 21081 15:54:56,400 --> 15:55:02,000 and after the function call we're going 21082 15:54:58,400 --> 15:55:04,319 to hit this statement here on line 10 21083 15:55:02,000 --> 15:55:05,680 and it is going to print 24. this is 21084 15:55:05,680 --> 15:55:09,360 take a moment to make sure you really 21085 15:55:07,360 --> 15:55:12,400 understand this the things you need to 21086 15:55:09,360 --> 15:55:15,199 be careful about is the syntax we use to 21087 15:55:12,400 --> 15:55:17,275 declare the function here we say that we 21088 15:55:15,199 --> 15:55:19,839 are passing by pointer because of this 21089 15:55:17,275 --> 15:55:21,915 little star here and we have the same 21090 15:55:19,839 --> 15:55:24,239 thing when we hit the definition of the 21091 15:55:21,915 --> 15:55:26,319 function we have this little star so we 21092 15:55:24,239 --> 15:55:28,795 are passing by pointer another thing you 21093 15:55:26,319 --> 15:55:31,275 should remember is to pass the address 21094 15:55:28,796 --> 15:55:33,916 of a variable when you call the function 21095 15:55:31,275 --> 15:55:36,720 like we do here and please remember to 21096 15:55:33,915 --> 15:55:39,839 use the the reference operator whenever 21097 15:55:36,720 --> 15:55:42,080 you want to modify whatever variable is 21098 15:55:39,839 --> 15:55:44,079 leaving at the address that you passed 21099 15:55:42,080 --> 15:55:46,480 in here so that's why we are the 21100 15:55:44,080 --> 15:55:48,955 reference in here through the star 21101 15:55:46,480 --> 15:55:51,116 operator like we are doing here this is 21102 15:55:48,955 --> 15:55:53,116 really all you need to be careful about 21103 15:55:51,116 --> 15:55:54,720 so we are going to try and run this 21104 15:55:53,116 --> 15:55:57,916 program let's do that we're going to 21105 15:55:58,239 --> 15:56:02,720 the boiler is going to go through we 21106 15:56:02,720 --> 15:56:09,916 so that we can run this program and 21107 15:56:05,199 --> 15:56:11,435 again we expect to see a 23 24 and 24 21108 15:56:13,519 --> 15:56:19,680 okay here is our output before we call 21109 15:56:17,040 --> 15:56:23,200 the say age function the age is 23 the 21110 15:56:19,680 --> 15:56:25,680 address is dec when we hit the function 21111 15:56:25,680 --> 15:56:32,080 uh years old so here we should have the 21112 15:56:29,116 --> 15:56:34,639 reference to access something inside the 21113 15:56:32,080 --> 15:56:37,276 variable here so we should say you are 21114 15:56:34,639 --> 15:56:39,199 age old we need to dereference this guy 21115 15:56:37,275 --> 15:56:41,839 because now it is a pointer i forgot 21116 15:56:39,199 --> 15:56:43,199 that i really am sorry but we are 21117 15:56:43,199 --> 15:56:47,275 this is a good learning chance for you 21118 15:56:45,199 --> 15:56:50,159 not to make the same mistake as i am 21119 15:56:47,275 --> 15:56:52,795 making here so let's try and build again 21120 15:56:50,160 --> 15:56:54,400 we should see the correct output we are 21121 15:56:54,400 --> 15:56:58,880 and run rooster now we see that before 21122 15:56:58,879 --> 15:57:04,795 ages 23 when we call the function h 21123 15:57:01,756 --> 15:57:07,595 becomes 24 because we just incremented 21124 15:57:04,796 --> 15:57:09,200 through our reference operator and after 21125 15:57:07,595 --> 15:57:10,479 the function call the edge is going to 21126 15:57:10,480 --> 15:57:15,276 because we have modified the original 21127 15:57:12,955 --> 15:57:18,000 variable here through our the 21128 15:57:15,275 --> 15:57:20,159 referencing of the pointer here this is 21129 15:57:18,000 --> 15:57:22,319 really all there is about passing by 21130 15:57:20,160 --> 15:57:24,480 pointer this is the syntax you have to 21131 15:57:22,319 --> 15:57:26,559 pass a pointer in the declaration you 21132 15:57:24,480 --> 15:57:28,400 have to pass a pointer in the function 21133 15:57:26,559 --> 15:57:31,435 definition and when you call the 21134 15:57:28,400 --> 15:57:33,360 function you have to use the address of 21135 15:57:31,436 --> 15:57:35,520 the variable and pass that as an 21136 15:57:33,360 --> 15:57:37,840 argument to the function and whenever 21137 15:57:35,519 --> 15:57:39,275 you need to use the past parameter 21138 15:57:37,839 --> 15:57:42,479 you're going to go through the the 21139 15:57:39,275 --> 15:57:45,756 reference operator like we did here 21140 15:57:42,480 --> 15:57:46,720 and here in the body of our function 21141 15:57:46,720 --> 15:57:51,436 in this lecture we're going to see how 21142 15:57:48,239 --> 15:57:52,559 we can pass parameters to a function by 21143 15:57:52,559 --> 15:57:57,275 and this is another technique we can use 21144 15:57:54,559 --> 15:57:59,680 to avoid passing by value which is going 21145 15:57:57,275 --> 15:58:01,915 to create a copy and the syntax is 21146 15:57:59,680 --> 15:58:03,915 really simple if you look at the 21147 15:58:01,915 --> 15:58:07,115 declaration here you're going to see 21148 15:58:03,915 --> 15:58:09,275 that we say and reference and again if 21149 15:58:07,116 --> 15:58:11,520 we hit the definition we're going to say 21150 15:58:09,275 --> 15:58:13,519 into reference as you see here 21151 15:58:11,519 --> 15:58:16,159 when we call the function we don't have 21152 15:58:13,519 --> 15:58:17,680 to pass the address of operator we just 21153 15:58:16,160 --> 15:58:20,160 passed the variable because this is 21154 15:58:17,680 --> 15:58:22,720 going to be treated as a reference by 21155 15:58:20,160 --> 15:58:25,040 the compiler and in the body of the 21156 15:58:22,720 --> 15:58:27,680 function we don't have to go through 21157 15:58:25,040 --> 15:58:30,080 ugly the referencing like we did with 21158 15:58:27,680 --> 15:58:33,116 pointers and hopefully you can see that 21159 15:58:30,080 --> 15:58:35,360 if you want to avoid copies passing by 21160 15:58:33,116 --> 15:58:37,840 references is much cleaner you don't 21161 15:58:35,360 --> 15:58:40,559 have to use the address of operator when 21162 15:58:37,839 --> 15:58:42,639 you call the function like we do here 21163 15:58:40,559 --> 15:58:45,435 and you don't have to go through the 21164 15:58:42,639 --> 15:58:48,159 reference and to access and modify your 21165 15:58:45,436 --> 15:58:51,040 variables and the modification we do 21166 15:58:48,160 --> 15:58:54,080 here by incrementing age is going to be 21167 15:58:51,040 --> 15:58:56,319 seen on the outside because we are going 21168 15:58:54,080 --> 15:58:59,276 through a reference and the reference is 21169 15:58:56,319 --> 15:59:01,519 a true alliance to the variable that the 21170 15:58:59,275 --> 15:59:03,680 reference is referencing so by 21171 15:59:01,519 --> 15:59:05,680 incrementing the edge here and the body 21172 15:59:03,680 --> 15:59:08,955 of the function the change is going to 21173 15:59:05,680 --> 15:59:11,519 be seen after the function call before 21174 15:59:08,955 --> 15:59:12,955 the function here we're going to get 23 21175 15:59:11,519 --> 15:59:15,359 in the body of the function we're going 21176 15:59:12,955 --> 15:59:17,915 to get 24 because we just incremented 21177 15:59:15,360 --> 15:59:20,720 the function and after the function call 21178 15:59:17,915 --> 15:59:23,595 we're going to get a 24 because the 21179 15:59:20,720 --> 15:59:26,239 change was propagated to the original 21180 15:59:23,595 --> 15:59:28,559 variable and the reason is because we 21181 15:59:26,239 --> 15:59:30,720 are going through a reference here 21182 15:59:28,559 --> 15:59:33,199 let's head to visual studio code and 21183 15:59:30,720 --> 15:59:35,275 actually play with us a little more okay 21184 15:59:33,199 --> 15:59:37,915 here we are in visual studio code the 21185 15:59:35,275 --> 15:59:41,199 current project is passed by reference 21186 15:59:37,915 --> 15:59:44,319 1710 here so we're going to grab our 21187 15:59:41,199 --> 15:59:45,839 template files and put them in place 21188 15:59:45,839 --> 15:59:50,399 let's do that and we are going to open 21189 15:59:48,239 --> 15:59:52,955 this in visual studio code pretty quick 21190 15:59:50,400 --> 15:59:55,520 let's do that the main cpp file is going 21191 15:59:52,955 --> 15:59:57,680 to show up here again we are going to 21192 15:59:55,519 --> 16:00:00,319 grab the code from the last lecture and 21193 15:59:57,680 --> 16:00:02,720 use that as a starting point and we're 21194 16:00:00,319 --> 16:00:05,040 going to be modifying the signature of 21195 16:00:02,720 --> 16:00:07,275 our function here so what we want to do 21196 16:00:05,040 --> 16:00:09,595 is to pass by reference we're going to 21197 16:00:07,275 --> 16:00:12,639 say enter reference here you already 21198 16:00:09,595 --> 16:00:14,795 know references from a previous chapter 21199 16:00:12,639 --> 16:00:17,680 and we are going to hit the definition 21200 16:00:14,796 --> 16:00:19,756 here and do the same thing so let's say 21201 16:00:21,116 --> 16:00:25,436 and when we call the function we don't 21202 16:00:23,040 --> 16:00:27,360 need to pass the address of operator 21203 16:00:25,436 --> 16:00:29,756 because this is a reference we can use 21204 16:00:27,360 --> 16:00:31,915 it right away the compiler is going to 21205 16:00:29,756 --> 16:00:33,916 know that this is going to be a 21206 16:00:31,915 --> 16:00:35,275 reference to the original variable that 21207 16:00:35,275 --> 16:00:39,519 and in the body of the function let's 21208 16:00:37,519 --> 16:00:42,399 take out this last line here we don't 21209 16:00:39,519 --> 16:00:43,595 need it and in the body when we want to 21210 16:00:43,595 --> 16:00:48,079 the reference we can do that right away 21211 16:00:46,000 --> 16:00:51,595 without the referencing so we can just 21212 16:00:48,080 --> 16:00:53,596 say plus plus h here and the changes are 21213 16:00:51,595 --> 16:00:56,079 going to be propagated to the original 21214 16:00:53,595 --> 16:00:58,399 variable here because we are going 21215 16:00:56,080 --> 16:01:00,400 through a reference let's go down and 21216 16:00:58,400 --> 16:01:02,480 modify this because this is a reference 21217 16:01:02,480 --> 16:01:08,956 and we can close this left pane here so 21218 16:01:05,839 --> 16:01:11,115 that we have some more breathing space 21219 16:01:08,955 --> 16:01:14,080 and you see that visual studio code is 21220 16:01:11,116 --> 16:01:17,200 no longer giving us any squiggly lines 21221 16:01:14,080 --> 16:01:19,840 so this must be valid c plus plus code 21222 16:01:17,199 --> 16:01:23,519 again notice the syntaxes we are using 21223 16:01:19,839 --> 16:01:26,319 here we are passing a reference in our 21224 16:01:23,519 --> 16:01:28,795 declaration we are passing a reference 21225 16:01:26,319 --> 16:01:30,720 in our function definition here when we 21226 16:01:28,796 --> 16:01:33,200 call the function we don't need to do 21227 16:01:30,720 --> 16:01:34,955 any address of operator we pass the 21228 16:01:34,955 --> 16:01:39,040 and in the body of the function we don't 21229 16:01:36,879 --> 16:01:40,719 have to do any dereferencing we can 21230 16:01:40,720 --> 16:01:45,360 or modify the variable however we want 21231 16:01:43,199 --> 16:01:47,915 the changes are going to be propagated 21232 16:01:45,360 --> 16:01:49,199 to the original variable because we are 21233 16:01:49,199 --> 16:01:54,159 incrementation here is also going to 21234 16:01:51,360 --> 16:01:56,480 affect the original variable that was 21235 16:01:54,160 --> 16:01:59,436 declared in the main function here let's 21236 16:01:56,480 --> 16:02:01,116 take this out and down here if we need 21237 16:01:59,436 --> 16:02:03,040 to use our variable we're going to just 21238 16:02:01,116 --> 16:02:06,000 use it right away we don't need any 21239 16:02:03,040 --> 16:02:08,400 referencing and if we print the address 21240 16:02:06,000 --> 16:02:10,400 of age here we're going to see the same 21241 16:02:08,400 --> 16:02:12,720 address we see on the outside because 21242 16:02:10,400 --> 16:02:15,040 this is a true alias it's going to be 21243 16:02:12,720 --> 16:02:17,520 referencing the same variable that we 21244 16:02:15,040 --> 16:02:19,915 have in main here okay now that you have 21245 16:02:17,519 --> 16:02:22,000 a better idea about this we are going to 21246 16:02:19,915 --> 16:02:24,879 try and build this we're going to use 21247 16:02:22,000 --> 16:02:25,915 gcc as always you can see that the build 21248 16:02:25,915 --> 16:02:30,719 if we bring up a terminal to try and run 21249 16:02:30,720 --> 16:02:35,275 we can clear and run rooster 21250 16:02:33,116 --> 16:02:38,319 we're going to see that before we call 21251 16:02:35,275 --> 16:02:40,239 the function the h is 23 but the address 21252 16:02:40,239 --> 16:02:46,159 when we hit the function the edge is 24 21253 16:02:42,955 --> 16:02:48,239 because we just incremented that here 21254 16:02:46,160 --> 16:02:51,116 but notice that the address of the 21255 16:02:48,239 --> 16:02:53,756 variable is the same as on the outside 21256 16:02:51,116 --> 16:02:56,639 so we are modifying the same variable 21257 16:02:53,756 --> 16:02:59,520 and when the function is done the edge 21258 16:02:56,639 --> 16:03:02,479 is still going to be 24 because we just 21259 16:02:59,519 --> 16:03:04,795 modified the original variable through a 21260 16:03:02,480 --> 16:03:07,360 reference here okay this is the effect 21261 16:03:04,796 --> 16:03:10,000 of passing by reference you can use a 21262 16:03:07,360 --> 16:03:12,879 claim syntax to modify the original 21263 16:03:10,000 --> 16:03:15,839 variable in your functions and this is 21264 16:03:12,879 --> 16:03:18,399 going to avoid copies like we have seen 21265 16:03:15,839 --> 16:03:21,199 when we were passing parameters by value 21266 16:03:18,400 --> 16:03:23,596 a few lectures back i would like to 21267 16:03:21,199 --> 16:03:26,795 welcome you in this new chapter where we 21268 16:03:23,595 --> 16:03:29,115 will be focusing on how to get data out 21269 16:03:26,796 --> 16:03:31,756 of functions again the model of the 21270 16:03:29,116 --> 16:03:34,400 function is really what you see here it 21271 16:03:31,756 --> 16:03:36,955 is like a machine and you can give input 21272 16:03:34,400 --> 16:03:39,275 to that machine and you expect to get 21273 16:03:36,955 --> 16:03:41,680 something out of that machine this 21274 16:03:39,275 --> 16:03:42,400 chapter is going to focus on this part 21275 16:03:42,400 --> 16:03:46,239 and we're going to see the different 21276 16:03:43,915 --> 16:03:49,115 facilities offered by the c plus plus 21277 16:03:46,239 --> 16:03:51,275 programming language to get output from 21278 16:03:49,116 --> 16:03:53,680 functions and we are going to start in 21279 16:03:51,275 --> 16:03:56,639 the next lecture and see how we can use 21280 16:03:53,680 --> 16:03:59,275 input and output parameters go ahead and 21281 16:03:56,639 --> 16:04:00,796 finish up here and meet me there in this 21282 16:03:59,275 --> 16:04:03,839 lecture we're going to see how we can 21283 16:04:00,796 --> 16:04:05,276 use function parameters to get data out 21284 16:04:05,275 --> 16:04:10,795 here is a simple example and we have 21285 16:04:07,360 --> 16:04:13,680 three functions here we have max str 21286 16:04:10,796 --> 16:04:16,000 max and and max double and you notice 21287 16:04:13,680 --> 16:04:17,840 that we have three parameters for each 21288 16:04:16,000 --> 16:04:19,915 of these functions the first two 21289 16:04:17,839 --> 16:04:22,559 parameters are going to be input they 21290 16:04:19,915 --> 16:04:24,879 are going to get data and the function 21291 16:04:22,559 --> 16:04:27,756 and the third one is going to be the 21292 16:04:24,879 --> 16:04:29,755 output parameter and it is going to get 21293 16:04:29,756 --> 16:04:35,756 let's focus on max hdr here and really 21294 16:04:33,040 --> 16:04:38,559 drive the point home the key idea here 21295 16:04:35,756 --> 16:04:42,080 is that for the third parameter 21296 16:04:38,559 --> 16:04:44,639 we are using a modifiable reference and 21297 16:04:42,080 --> 16:04:46,880 if we use a reference the changes we do 21298 16:04:44,639 --> 16:04:50,879 inside the function are also going to be 21299 16:04:46,879 --> 16:04:53,519 visible on the outside and we explicitly 21300 16:04:50,879 --> 16:04:55,595 left out the const that we have on the 21301 16:04:53,519 --> 16:04:57,359 other parameters because we want to be 21302 16:04:57,360 --> 16:05:02,480 the output variable here inside the 21303 16:05:00,400 --> 16:05:04,720 function if we marked the third 21304 16:05:02,480 --> 16:05:06,560 parameter as const we would get a 21305 16:05:04,720 --> 16:05:09,040 compiler error if we're trying to do 21306 16:05:06,559 --> 16:05:11,595 something like this and assign something 21307 16:05:09,040 --> 16:05:13,520 to the output parameter here so the main 21308 16:05:11,595 --> 16:05:16,000 purpose of this function is to compute 21309 16:05:13,519 --> 16:05:18,639 the maximum between these two strings we 21310 16:05:16,000 --> 16:05:20,480 are going to say if input 1 is greater 21311 16:05:20,480 --> 16:05:26,240 then input 1 is going to be our maximum 21312 16:05:23,519 --> 16:05:29,359 and we are going to store that in our 21313 16:05:26,239 --> 16:05:32,400 output parameter here if input 1 is not 21314 16:05:29,360 --> 16:05:34,796 greater than input 2 then input 2 is our 21315 16:05:32,400 --> 16:05:38,319 maximum we're going to store that in our 21316 16:05:34,796 --> 16:05:40,480 output and whoever has access to input 2 21317 16:05:38,319 --> 16:05:42,639 outside the function is going to see the 21318 16:05:40,480 --> 16:05:44,400 changes that we are making in the body 21319 16:05:42,639 --> 16:05:46,239 of the function this is really the 21320 16:05:46,955 --> 16:05:50,639 send output from the function and make 21321 16:05:50,639 --> 16:05:55,275 when the function has returned and again 21322 16:05:53,199 --> 16:05:57,915 the key idea is that we set up the 21323 16:05:55,275 --> 16:05:59,680 function in a way that the changes we do 21324 16:05:57,915 --> 16:06:02,000 inside the function are going to be 21325 16:05:59,680 --> 16:06:04,796 visible on the outside and we are able 21326 16:06:02,000 --> 16:06:07,275 to do that because output here is a 21327 16:06:04,796 --> 16:06:09,840 reference this is really key we have 21328 16:06:07,275 --> 16:06:12,239 used references here but we can set up 21329 16:06:09,839 --> 16:06:14,955 the same thing using pointers because we 21330 16:06:12,239 --> 16:06:17,436 have seen that with pointers we can also 21331 16:06:14,955 --> 16:06:19,915 make our changes inside the function 21332 16:06:17,436 --> 16:06:22,400 visible to the outside the two other 21333 16:06:19,915 --> 16:06:24,319 examples here are really extensions of 21334 16:06:22,400 --> 16:06:27,275 the same thing we have two input 21335 16:06:24,319 --> 16:06:28,400 variables input 1 and input 2 21336 16:06:28,400 --> 16:06:33,200 output is going to store the output from 21337 16:06:30,639 --> 16:06:35,360 the function you notice that output is a 21338 16:06:33,199 --> 16:06:37,519 reference and because of this the 21339 16:06:35,360 --> 16:06:40,639 changes we do in here are going to be 21340 16:06:37,519 --> 16:06:43,199 visible when this function has returned 21341 16:06:40,639 --> 16:06:44,400 and we do exactly the same thing on max 21342 16:06:44,400 --> 16:06:48,880 but you see that it is using a pointer 21343 16:06:46,559 --> 16:06:50,795 okay i really wanted you to see this so 21344 16:06:48,879 --> 16:06:54,079 inside we're going to try and figure out 21345 16:06:50,796 --> 16:06:56,560 who is the maximum and if input 1 is the 21346 16:06:54,080 --> 16:06:58,955 maximum we're going to write that in our 21347 16:06:56,559 --> 16:07:01,199 output variable here and you notice that 21348 16:06:58,955 --> 16:07:04,720 we are the referencing here because this 21349 16:07:01,199 --> 16:07:07,360 guy is a pointer and if input 2 is our 21350 16:07:04,720 --> 16:07:09,436 maximum we're going to write that in our 21351 16:07:07,360 --> 16:07:12,239 output variable here one thing i don't 21352 16:07:09,436 --> 16:07:15,680 think i explained here is that we have a 21353 16:07:12,239 --> 16:07:19,040 cost std string reference here for maxed 21354 16:07:15,680 --> 16:07:21,199 hdr and the reason is that the input 21355 16:07:19,040 --> 16:07:23,436 variables aren't really meant to be 21356 16:07:21,199 --> 16:07:26,079 changed that's why we market them as 21357 16:07:23,436 --> 16:07:28,400 cost and if you want to modify them in 21358 16:07:26,080 --> 16:07:30,240 some way you can take the const away and 21359 16:07:28,400 --> 16:07:32,639 it is going to work okay this is really 21360 16:07:30,239 --> 16:07:35,360 the big idea we are playing with in this 21361 16:07:32,639 --> 16:07:38,479 lecture using function parameters as a 21362 16:07:35,360 --> 16:07:40,720 way to output data from the function and 21363 16:07:38,480 --> 16:07:43,200 here are a few ideas you should keep in 21364 16:07:40,720 --> 16:07:45,360 mind output parameters should be passed 21365 16:07:43,199 --> 16:07:48,000 in such a way that you can modify the 21366 16:07:45,360 --> 16:07:50,000 arguments from the inside the functions 21367 16:07:48,000 --> 16:07:51,436 and we have seen that we have two ways 21368 16:07:50,000 --> 16:07:54,080 to do that we could either use 21369 16:07:51,436 --> 16:07:56,796 references or even use pointers but 21370 16:07:54,080 --> 16:07:59,360 references are what you should prefer in 21371 16:07:56,796 --> 16:08:01,840 modern c plus plus input parameters 21372 16:07:59,360 --> 16:08:04,559 shouldn't be modified they are meant to 21373 16:08:01,839 --> 16:08:06,479 just get data in the function and that's 21374 16:08:04,559 --> 16:08:08,955 why in most cases you're going to see 21375 16:08:06,480 --> 16:08:11,116 that they are marked const okay now that 21376 16:08:08,955 --> 16:08:13,116 we have seen all these ideas i think it 21377 16:08:11,116 --> 16:08:15,200 is time we headed over to visual studio 21378 16:08:13,116 --> 16:08:17,840 code and actually played with them okay 21379 16:08:15,199 --> 16:08:20,159 here we are in our working folder the 21380 16:08:17,839 --> 16:08:22,399 current project is input and output 21381 16:08:20,160 --> 16:08:24,880 parameters we are going to grab our 21382 16:08:22,400 --> 16:08:27,275 template files like we always do we're 21383 16:08:24,879 --> 16:08:29,275 going to put them in place and we are 21384 16:08:27,275 --> 16:08:31,199 going to open this guy in visual studio 21385 16:08:29,275 --> 16:08:33,839 code we're going to just drag it and 21386 16:08:31,199 --> 16:08:35,839 drop on top of visual studio code here 21387 16:08:33,839 --> 16:08:37,915 and it is going to open up we have our 21388 16:08:35,839 --> 16:08:40,879 main function here we can close the pane 21389 16:08:37,915 --> 16:08:42,559 here and clean the file up a little bit 21390 16:08:40,879 --> 16:08:45,595 we're going to remove what we don't need 21391 16:08:42,559 --> 16:08:47,519 here and we're going to have a bare main 21392 16:08:45,595 --> 16:08:49,839 function we can play with here the first 21393 16:08:47,519 --> 16:08:52,719 thing we're going to do is to put in our 21394 16:08:49,839 --> 16:08:55,755 max str function it is nothing special 21395 16:08:52,720 --> 16:08:58,480 we just have two input parameters and 21396 16:08:55,756 --> 16:09:00,480 one output parameter the output 21397 16:08:58,480 --> 16:09:03,116 parameter is going to be what we use to 21398 16:09:00,480 --> 16:09:04,796 get output outside the function and 21399 16:09:03,116 --> 16:09:07,200 inside we're just going to compute the 21400 16:09:04,796 --> 16:09:09,200 maximum if input 1 is the maximum we're 21401 16:09:07,199 --> 16:09:11,915 going to write that in our output 21402 16:09:09,199 --> 16:09:14,159 parameter if input 2 happens to be our 21403 16:09:11,915 --> 16:09:16,879 maximum we're going to write that in our 21404 16:09:14,160 --> 16:09:19,360 output parameter because our output 21405 16:09:16,879 --> 16:09:21,360 parameter is a reference the changes we 21406 16:09:19,360 --> 16:09:24,239 make inside the function are going to be 21407 16:09:21,360 --> 16:09:27,116 visible on the outside because we will 21408 16:09:24,239 --> 16:09:29,595 be working on the original variable 21409 16:09:27,116 --> 16:09:32,080 through the reference which is really a 21410 16:09:29,595 --> 16:09:33,756 true alias here okay now that we have 21411 16:09:32,080 --> 16:09:36,560 the function set up like this we can 21412 16:09:33,756 --> 16:09:38,639 head in the main function and try it out 21413 16:09:36,559 --> 16:09:40,639 we're going to set up three variables of 21414 16:09:38,639 --> 16:09:43,040 type std strand i think we should 21415 16:09:40,639 --> 16:09:45,275 include the string library here let's do 21416 16:09:43,040 --> 16:09:48,239 that and we're going to have an out 21417 16:09:45,275 --> 16:09:51,360 string which is going to be our output 21418 16:09:48,239 --> 16:09:53,915 variable and we have two input strings 21419 16:09:51,360 --> 16:09:56,796 one is initialized with alabama the 21420 16:09:53,915 --> 16:10:00,239 other is initialized with bell view and 21421 16:09:56,796 --> 16:10:02,080 we're going to call our max sdr function 21422 16:10:00,239 --> 16:10:04,559 here you notice that it's not going to 21423 16:10:02,080 --> 16:10:06,880 be using the return mechanism we're 21424 16:10:04,559 --> 16:10:10,000 going to be grabbing the output from 21425 16:10:06,879 --> 16:10:13,275 this function using an output parameter 21426 16:10:10,000 --> 16:10:15,275 which is going to be our out hdr 21427 16:10:13,275 --> 16:10:17,040 variable here okay once the function is 21428 16:10:15,275 --> 16:10:21,040 done we're going to have the maximum 21429 16:10:17,040 --> 16:10:22,879 stored in our out sdr variable here and 21430 16:10:21,040 --> 16:10:25,275 we can print it out now that we have 21431 16:10:22,879 --> 16:10:27,360 this setup we can actually try and build 21432 16:10:25,275 --> 16:10:29,680 this we're going to use gcc let's do 21433 16:10:27,360 --> 16:10:32,639 that the build is good we can bring up a 21434 16:10:29,680 --> 16:10:34,400 terminal and clear and run rooster now 21435 16:10:32,639 --> 16:10:38,000 you see that the maximum is going to be 21436 16:10:34,400 --> 16:10:39,840 bell view let's try and change this to 21437 16:10:38,000 --> 16:10:41,595 casablanca why not and we're going to 21438 16:10:39,839 --> 16:10:44,239 see that the maximum now is going to be 21439 16:10:41,595 --> 16:10:47,275 casablanca and our function is going to 21440 16:10:44,239 --> 16:10:50,079 be smart enough to store the maximum in 21441 16:10:47,275 --> 16:10:51,915 this output variable here let's build 21442 16:10:50,080 --> 16:10:54,160 again the world is going to be good 21443 16:10:51,915 --> 16:10:56,559 going to clear and run rooster now the 21444 16:10:54,160 --> 16:10:59,276 maximum is casablanca hopefully you 21445 16:10:56,559 --> 16:11:02,159 understand that we are able to make the 21446 16:10:59,275 --> 16:11:06,000 changes in the function processed 21447 16:11:02,160 --> 16:11:08,400 because our output parameter here is 21448 16:11:06,000 --> 16:11:10,720 passed by reference this is a true 21449 16:11:08,400 --> 16:11:13,200 reference and any change we do to the 21450 16:11:10,720 --> 16:11:16,160 output parameter here is going to be 21451 16:11:13,199 --> 16:11:19,199 visible in whatever variable you passed 21452 16:11:16,160 --> 16:11:21,436 as an argument for this third argument 21453 16:11:19,199 --> 16:11:23,039 here this is what is making this work 21454 16:11:21,436 --> 16:11:24,480 now that this is working we're going to 21455 16:11:25,360 --> 16:11:29,756 put in another example for you to play 21456 16:11:28,319 --> 16:11:32,159 with the next function we're going to 21457 16:11:29,756 --> 16:11:35,520 play with is going to be max and as you 21458 16:11:32,160 --> 16:11:39,116 see here and it is very similar to our 21459 16:11:35,519 --> 16:11:42,000 max hdr but you notice that our inputs 21460 16:11:39,116 --> 16:11:44,720 are no longer references because we 21461 16:11:42,000 --> 16:11:47,116 don't really mind copying fundamental 21462 16:11:44,720 --> 16:11:49,756 types in c plus plus they are small in 21463 16:11:47,116 --> 16:11:52,160 memory so copying them is really cheap 21464 16:11:49,756 --> 16:11:54,955 we can pass them by value and this is 21465 16:11:52,160 --> 16:11:57,360 not going to be so wasteful in our 21466 16:11:54,955 --> 16:11:59,839 program but the first parameter must be 21467 16:11:57,360 --> 16:12:02,796 passed by reference because we want to 21468 16:11:59,839 --> 16:12:04,955 use it as an output parameter and we're 21469 16:12:02,796 --> 16:12:06,796 going to use it to get output from the 21470 16:12:04,955 --> 16:12:08,400 function and the way we're going to do 21471 16:12:06,796 --> 16:12:11,040 that we're going to write whatever 21472 16:12:08,400 --> 16:12:13,275 variable is the maximum in our output 21473 16:12:11,040 --> 16:12:15,360 parameter and the changes are going to 21474 16:12:13,275 --> 16:12:17,756 be visible on the outside now we are 21475 16:12:15,360 --> 16:12:19,756 going to head over in our main function 21476 16:12:17,756 --> 16:12:22,400 set up a few variables and play with us 21477 16:12:19,756 --> 16:12:25,040 so we're going to say and out and this 21478 16:12:22,400 --> 16:12:27,680 is going to be our output variable and 21479 16:12:25,040 --> 16:12:29,436 we're going to say input one let's say 21480 16:12:27,680 --> 16:12:32,319 in one and we're going to initialize 21481 16:12:29,436 --> 16:12:34,480 this with a 45 why not and we're going 21482 16:12:32,319 --> 16:12:37,275 to say ent in two and we're going to 21483 16:12:34,480 --> 16:12:39,360 initialize this with 23 why not now that 21484 16:12:37,275 --> 16:12:41,115 we have this we're going to say max and 21485 16:12:39,360 --> 16:12:44,000 and we're going to put in our input 21486 16:12:41,116 --> 16:12:46,955 we're going to say n1 and n2 then we're 21487 16:12:44,000 --> 16:12:49,116 going to say out and as our output 21488 16:12:46,955 --> 16:12:51,199 variable now that we have this we can 21489 16:12:49,116 --> 16:12:53,596 actually print out whatever number is 21490 16:12:51,199 --> 16:12:55,360 the maximum through an output statement 21491 16:12:53,595 --> 16:12:58,000 like this really simple we're going to 21492 16:12:55,360 --> 16:13:00,239 say max and and we're going to say out 21493 16:12:58,000 --> 16:13:02,319 to end that's where it is going to be 21494 16:13:00,239 --> 16:13:05,115 stored and we are able to do this 21495 16:13:02,319 --> 16:13:08,159 because again the third parameter here 21496 16:13:05,116 --> 16:13:10,560 is passed by reference and any change we 21497 16:13:08,160 --> 16:13:13,756 do and the function here is going to 21498 16:13:10,559 --> 16:13:16,000 stack even when the function here has 21499 16:13:13,756 --> 16:13:18,720 returned and we're going to have that 21500 16:13:16,000 --> 16:13:20,480 stored in our third parameter here and 21501 16:13:18,720 --> 16:13:23,595 this is a really common technique 21502 16:13:20,480 --> 16:13:27,276 especially in code that has to interface 21503 16:13:23,595 --> 16:13:29,360 with c code out there from c plus plus 21504 16:13:27,275 --> 16:13:31,436 and you really need to be aware of this 21505 16:13:29,360 --> 16:13:33,915 now if we run this program we're going 21506 16:13:31,436 --> 16:13:36,480 to see our maximum printed out and it is 21507 16:13:33,915 --> 16:13:38,479 going to be stored in our out variable 21508 16:13:36,480 --> 16:13:40,796 here let's do that we're going to use 21509 16:13:38,480 --> 16:13:42,956 gcc to world the world is good you can 21510 16:13:40,796 --> 16:13:45,596 clear and run rooster and you're going 21511 16:13:42,955 --> 16:13:49,040 to see that the maximum is 45 if we 21512 16:13:45,595 --> 16:13:51,839 change and store our maximum in into 21513 16:13:49,040 --> 16:13:54,639 here and for example let's make this a 21514 16:13:54,639 --> 16:13:59,239 let's build it in to take the changes 21515 16:13:59,239 --> 16:14:03,680 rooster.xe binary we're going to clear 21516 16:14:01,839 --> 16:14:06,000 and run rooster and you're going to see 21517 16:14:03,680 --> 16:14:08,400 that that's the maximum here we are able 21518 16:14:06,000 --> 16:14:11,275 to grab whatever is the maximum from 21519 16:14:08,400 --> 16:14:13,916 these two input parameters and store 21520 16:14:11,275 --> 16:14:17,360 that in our output parameter here 21521 16:14:13,915 --> 16:14:19,915 because we are passing that third guy as 21522 16:14:17,360 --> 16:14:21,915 a reference this is really important we 21523 16:14:19,915 --> 16:14:24,955 have seen that we can use references to 21524 16:14:21,915 --> 16:14:28,239 make changes we do in the function stick 21525 16:14:24,955 --> 16:14:30,159 and be visible in outside variables but 21526 16:14:28,239 --> 16:14:32,239 we can also use pointers and i am going 21527 16:14:30,160 --> 16:14:33,840 to show you an example of how we can do 21528 16:14:32,239 --> 16:14:36,400 that first we're going to comment this 21529 16:14:33,839 --> 16:14:38,955 out because we don't want noise output 21530 16:14:36,400 --> 16:14:41,840 in our program and we are going to go on 21531 16:14:38,955 --> 16:14:45,680 top and set up a max double function 21532 16:14:41,839 --> 16:14:48,000 which is going to grab our output using 21533 16:14:45,680 --> 16:14:50,400 pointers the name of the function is max 21534 16:14:48,000 --> 16:14:52,400 double it's not going to return anything 21535 16:14:50,400 --> 16:14:55,916 and it is going to take two input 21536 16:14:52,400 --> 16:14:58,319 parameters input 1 and input 2 and it is 21537 16:14:55,915 --> 16:15:00,719 going to tell us whichever of these 2 is 21538 16:14:58,319 --> 16:15:03,595 the maximum the maximum is going to be 21539 16:15:00,720 --> 16:15:05,840 stored in our output parameter but we 21540 16:15:03,595 --> 16:15:08,399 are using pointers to make these changes 21541 16:15:05,839 --> 16:15:10,719 inside the body of the function stick 21542 16:15:08,400 --> 16:15:12,239 and be visible on the outside of the 21543 16:15:10,720 --> 16:15:14,080 function so we are going to the 21544 16:15:12,239 --> 16:15:15,915 reference here because the third guy 21545 16:15:14,080 --> 16:15:18,240 here is a pointer and we're going to 21546 16:15:15,915 --> 16:15:20,559 store in whichever is the maximum and 21547 16:15:18,239 --> 16:15:22,879 when the function returns the maximum is 21548 16:15:20,559 --> 16:15:24,879 going to be visible on the outside so 21549 16:15:22,879 --> 16:15:26,955 let's go down and try this out we're 21550 16:15:24,879 --> 16:15:28,079 going to say double out double and we're 21551 16:15:28,080 --> 16:15:32,400 set up our input here we're going to say 21552 16:15:30,400 --> 16:15:33,239 in double one and we're going to make 21553 16:15:33,239 --> 16:15:39,595 45.8 why not and we're going to do a 21554 16:15:36,239 --> 16:15:43,040 second input parameter and int we should 21555 16:15:39,595 --> 16:15:45,199 say n not end sorry for this in double 21556 16:15:43,040 --> 16:15:47,360 for input double so we're going to say 21557 16:15:45,199 --> 16:15:50,000 double 2 and we're going to initialize 21558 16:15:50,000 --> 16:15:54,000 for example we can really put in 21559 16:15:51,839 --> 16:15:56,319 anything now that we have this we can 21560 16:15:54,000 --> 16:15:58,080 call our max double function let's do 21561 16:15:56,319 --> 16:16:01,680 that going to say max double we're going 21562 16:15:58,080 --> 16:16:03,596 to say in double one and in double two 21563 16:16:01,680 --> 16:16:07,275 let's do that and we're going to have 21564 16:16:03,595 --> 16:16:09,040 out double as our output parameter and 21565 16:16:07,275 --> 16:16:12,000 uh because it's a pointer we need to 21566 16:16:09,040 --> 16:16:14,639 pass this guy as an address so we're 21567 16:16:12,000 --> 16:16:16,559 going to say address of how to double 21568 16:16:14,639 --> 16:16:19,040 here that's how we're going to be able 21569 16:16:16,559 --> 16:16:21,519 to manipulate that inside the function 21570 16:16:19,040 --> 16:16:24,879 here once we do this we are going to 21571 16:16:21,519 --> 16:16:26,719 have our maximum stored and out double 21572 16:16:24,879 --> 16:16:29,680 and we can print that out so we're going 21573 16:16:26,720 --> 16:16:32,000 to say stdcl max double and we're going 21574 16:16:29,680 --> 16:16:36,319 to say mac and we're going to say out to 21575 16:16:32,000 --> 16:16:38,239 double std e and el and if we weld why 21576 16:16:36,319 --> 16:16:40,955 do we have a squiggly line here we don't 21577 16:16:38,239 --> 16:16:42,559 have a semicolon at the end of our 21578 16:16:40,955 --> 16:16:45,275 function called here so we're going to 21579 16:16:42,559 --> 16:16:47,360 build with gcc as always you're going to 21580 16:16:45,275 --> 16:16:51,436 see that the world is good we can clear 21581 16:16:51,436 --> 16:16:57,116 if we change and store our maximum and 21582 16:16:54,319 --> 16:16:58,796 in double to here we can change this guy 21583 16:16:58,796 --> 16:17:04,080 we're going to build and see that we are 21584 16:17:01,116 --> 16:17:06,240 actually grabbing the maximum regardless 21585 16:17:04,080 --> 16:17:08,080 of the variable in which it is stored if 21586 16:17:06,239 --> 16:17:11,115 we run rooster we're going to have our 21587 16:17:08,080 --> 16:17:12,955 maximum and this is working exactly as 21588 16:17:11,116 --> 16:17:14,560 we want this is really all we set out to 21589 16:17:12,955 --> 16:17:18,239 do in this lecture showing you how you 21590 16:17:14,559 --> 16:17:21,519 can use references to use parameters as 21591 16:17:18,239 --> 16:17:24,879 output channels for data to get outside 21592 16:17:21,519 --> 16:17:27,435 of the function and be used in variables 21593 16:17:24,879 --> 16:17:29,435 that are outside the function here i 21594 16:17:27,436 --> 16:17:31,275 really hope this makes sense and this is 21595 16:17:29,436 --> 16:17:32,880 a technique you need to be aware of 21596 16:17:31,275 --> 16:17:35,680 we're going to see other techniques to 21597 16:17:32,879 --> 16:17:37,199 do the same thing and uh starting in the 21598 16:17:35,680 --> 16:17:39,840 next lecture we're going to see how we 21599 16:17:37,199 --> 16:17:42,319 can return from functions for now i hope 21600 16:17:39,839 --> 16:17:44,159 you found this lecture interesting we 21601 16:17:42,319 --> 16:17:45,436 are going to stop here in this one in 21602 16:17:44,160 --> 16:17:48,796 the next one we're going to see how we 21603 16:17:45,436 --> 16:17:51,680 can use the return mechanism to get data 21604 16:17:48,796 --> 16:17:54,240 outside our function go ahead and finish 21605 16:17:54,239 --> 16:17:58,559 in this lecture we're going to see how 21606 16:17:55,680 --> 16:18:01,756 we can get data outside of a function 21607 16:17:58,559 --> 16:18:04,399 through the return mechanism and this is 21608 16:18:01,756 --> 16:18:06,400 going to be an alternative to the way we 21609 16:18:04,400 --> 16:18:08,639 have been doing things in the last 21610 16:18:06,400 --> 16:18:12,080 lecture where we used function 21611 16:18:08,639 --> 16:18:14,000 parameters as channels to get data 21612 16:18:14,000 --> 16:18:17,756 let's look at the simple example of how 21613 16:18:16,239 --> 16:18:19,595 this is done here we have a simple 21614 16:18:17,756 --> 16:18:21,595 function called sum the function is 21615 16:18:21,595 --> 16:18:27,115 and it is going to sum up two parameters 21616 16:18:24,000 --> 16:18:28,720 that we pass to at nta and end b 21617 16:18:27,116 --> 16:18:30,240 if we get inside the function we're 21618 16:18:28,720 --> 16:18:31,680 going to do something really simple 21619 16:18:30,239 --> 16:18:33,360 we're going to set up a new variable 21620 16:18:31,680 --> 16:18:35,040 which is local to the function we're 21621 16:18:33,360 --> 16:18:37,040 going to call that reserved it's going 21622 16:18:35,040 --> 16:18:39,275 to be the same type as our input 21623 16:18:37,040 --> 16:18:41,756 parameters and we're going to add up a 21624 16:18:39,275 --> 16:18:43,680 and b and store the results back in our 21625 16:18:43,680 --> 16:18:49,436 once we have this we're going to return 21626 16:18:45,756 --> 16:18:52,080 this for this variable to be visible by 21627 16:18:49,436 --> 16:18:53,840 whoever called this function here one 21628 16:18:52,080 --> 16:18:57,596 thing you should know is that this 21629 16:18:53,839 --> 16:19:00,479 variable is local to this function here 21630 16:18:57,595 --> 16:19:02,879 and it will be destroyed the moment we 21631 16:19:00,480 --> 16:19:04,640 hit the end of the function here and the 21632 16:19:04,639 --> 16:19:08,559 so if we go in main here we're going to 21633 16:19:06,639 --> 16:19:11,116 try and see an example of how we can 21634 16:19:08,559 --> 16:19:14,399 call this function we have two variables 21635 16:19:11,116 --> 16:19:16,796 a and b and we have a new result 21636 16:19:14,400 --> 16:19:19,200 variable set up in the main function 21637 16:19:16,796 --> 16:19:21,436 please note that this variable is very 21638 16:19:19,199 --> 16:19:22,399 different from the variable we have and 21639 16:19:22,400 --> 16:19:27,360 sum function here because they live in 21640 16:19:24,879 --> 16:19:30,159 different scopes okay we are calling our 21641 16:19:27,360 --> 16:19:33,520 sum function here and we are passing it 21642 16:19:30,160 --> 16:19:35,596 two arguments in the form of a and b 21643 16:19:33,519 --> 16:19:37,915 here the function is going to compute 21644 16:19:35,595 --> 16:19:40,319 our sum and it's going to return it and 21645 16:19:37,915 --> 16:19:42,879 we're going to store that back in our 21646 16:19:40,319 --> 16:19:45,436 local variable to the main function here 21647 16:19:42,879 --> 16:19:47,755 the result variable here again is local 21648 16:19:45,436 --> 16:19:49,275 to this main function okay once we call 21649 16:19:47,756 --> 16:19:52,080 this function we're going to have our 21650 16:19:49,275 --> 16:19:53,756 results stored in here and if we print 21651 16:19:52,080 --> 16:19:55,916 like we do here we're going to see it 21652 16:19:53,756 --> 16:19:59,520 printed out on the console in this case 21653 16:19:55,915 --> 16:20:02,479 it's going to print 50 because 34 adding 21654 16:19:59,519 --> 16:20:04,479 16 that's going to be 50. but one thing 21655 16:20:02,480 --> 16:20:06,956 i want you to know is that this is going 21656 16:20:04,480 --> 16:20:08,956 to return by value and what we mean by 21657 16:20:06,955 --> 16:20:12,159 this is that we're going to copy 21658 16:20:08,955 --> 16:20:15,116 whatever we return here and store a copy 21659 16:20:12,160 --> 16:20:17,276 of that in this result variable that we 21660 16:20:15,116 --> 16:20:18,955 have in the main function and we're 21661 16:20:18,955 --> 16:20:25,519 notice the stdc out statement we have 21662 16:20:21,915 --> 16:20:27,519 here this is going to print the address 21663 16:20:25,519 --> 16:20:29,755 of this local variable and once the 21664 16:20:27,519 --> 16:20:32,079 function terminates we're going to try 21665 16:20:29,756 --> 16:20:34,639 and print the address of this resort 21666 16:20:32,080 --> 16:20:36,639 variable in the local scope of the main 21667 16:20:34,639 --> 16:20:38,879 function and we're going to see two 21668 16:20:36,639 --> 16:20:41,436 different addresses and hopefully this 21669 16:20:38,879 --> 16:20:43,755 is going to prove that we are returning 21670 16:20:41,436 --> 16:20:46,319 a copy it's not the original variable 21671 16:20:43,756 --> 16:20:48,559 that we have in the sum function here 21672 16:20:46,319 --> 16:20:50,639 that we are returning okay hopefully 21673 16:20:48,559 --> 16:20:53,275 this drives the point home that we can 21674 16:20:50,639 --> 16:20:55,199 use the return mechanism to get data 21675 16:20:53,275 --> 16:20:58,720 outside the function and we're going to 21676 16:20:55,199 --> 16:21:00,719 grab the result in our result variable 21677 16:20:58,720 --> 16:21:02,720 here and we're going to use it however 21678 16:21:02,720 --> 16:21:08,000 but things don't always work like this 21679 16:21:06,000 --> 16:21:10,080 sometimes the compiler is going to 21680 16:21:10,080 --> 16:21:15,520 and not really returned by value but 21681 16:21:12,559 --> 16:21:18,000 returning by value is the default but if 21682 16:21:15,519 --> 16:21:20,479 the compiler notices that it can do 21683 16:21:18,000 --> 16:21:23,360 things better it is going to optimize 21684 16:21:20,480 --> 16:21:25,520 your returns by value to return by 21685 16:21:23,360 --> 16:21:28,480 reference and you're not going to make a 21686 16:21:25,519 --> 16:21:30,795 copy but you're going to somehow reuse 21687 16:21:28,480 --> 16:21:33,040 the local variable in the main function 21688 16:21:30,796 --> 16:21:35,360 let's look at a simple example to really 21689 16:21:33,040 --> 16:21:37,436 drive this home we have a function here 21690 16:21:35,360 --> 16:21:39,436 which is going to add strength it is 21691 16:21:37,436 --> 16:21:41,756 going to basically concatenate them 21692 16:21:39,436 --> 16:21:43,596 using the plus operator here and you 21693 16:21:41,756 --> 16:21:45,040 notice that inside the function here 21694 16:21:43,595 --> 16:21:47,915 we're going to set up a local variable 21695 16:21:45,040 --> 16:21:49,360 which is going to store our concatenated 21696 16:21:49,360 --> 16:21:53,595 and we're going to print the address of 21697 16:21:51,680 --> 16:21:55,680 the local string after that we're going 21698 16:21:53,595 --> 16:21:58,639 to return the strength so that it is 21699 16:21:55,680 --> 16:22:00,720 usable by whoever calls this function 21700 16:21:58,639 --> 16:22:03,360 let's go in main and see how we can use 21701 16:22:00,720 --> 16:22:05,916 this function here we are setting up a 21702 16:22:03,360 --> 16:22:08,720 variable called sdr result which is 21703 16:22:05,915 --> 16:22:10,639 going to store our result strength we 21704 16:22:08,720 --> 16:22:13,040 are saying add strings here we are 21705 16:22:10,639 --> 16:22:16,080 calling our function and we are passing 21706 16:22:13,040 --> 16:22:18,400 in two strings hello and world and we 21707 16:22:16,080 --> 16:22:20,480 want to concatenate these guys this is 21708 16:22:18,400 --> 16:22:21,916 going to give us our result and if we 21709 16:22:20,480 --> 16:22:24,480 print it out we're going to get hello 21710 16:22:21,915 --> 16:22:28,000 world printed out but the main message 21711 16:22:24,480 --> 16:22:30,720 here is that the address of the outside 21712 16:22:28,000 --> 16:22:33,680 string here is going to be the same as 21713 16:22:30,720 --> 16:22:35,360 the address we have on the inside here 21714 16:22:33,680 --> 16:22:37,436 and this is going to mean that the 21715 16:22:39,116 --> 16:22:44,880 to return the local variable because it 21716 16:22:42,400 --> 16:22:47,436 has seen that we could really be wasting 21717 16:22:44,879 --> 16:22:49,755 this guy here inside the function when 21718 16:22:47,436 --> 16:22:52,319 the function returns this string result 21719 16:22:49,756 --> 16:22:54,880 here is going to die off but the 21720 16:22:52,319 --> 16:22:57,436 compiler knows that we can reuse that 21721 16:22:54,879 --> 16:23:00,159 and avoid the work of copying this 21722 16:22:57,436 --> 16:23:02,160 string which might be really large 21723 16:23:00,160 --> 16:23:04,640 suppose this string has a thousand 21724 16:23:02,160 --> 16:23:07,276 characters inside and copying it would 21725 16:23:04,639 --> 16:23:10,000 be a waste so the compiler is not going 21726 16:23:07,275 --> 16:23:12,400 to return by value which is the default 21727 16:23:10,000 --> 16:23:14,955 it is going to do some kind of return by 21728 16:23:12,400 --> 16:23:16,796 reference but it's really not a return 21729 16:23:14,955 --> 16:23:18,639 by reference because this is a local 21730 16:23:16,796 --> 16:23:20,319 variable we can't really have a 21731 16:23:18,639 --> 16:23:22,239 reference to a local variable to a 21732 16:23:20,319 --> 16:23:24,559 function that would be really bad 21733 16:23:22,239 --> 16:23:27,040 because we might be able to modify it 21734 16:23:24,559 --> 16:23:29,360 when the variable is actually dead after 21735 16:23:27,040 --> 16:23:31,756 the function has returned so it is a 21736 16:23:29,360 --> 16:23:34,319 magic that the compiler is doing to save 21737 16:23:31,756 --> 16:23:36,400 you from unnecessary copies but you 21738 16:23:34,319 --> 16:23:39,519 should be aware of this and not make 21739 16:23:36,400 --> 16:23:41,360 your code rely on returning by value 21740 16:23:39,519 --> 16:23:43,275 okay now that we know this we're going 21741 16:23:41,360 --> 16:23:46,559 to head over to visual studio code and 21742 16:23:43,275 --> 16:23:48,795 actually play with us here we are in our 21743 16:23:46,559 --> 16:23:51,595 working folder the current project is 21744 16:23:48,796 --> 16:23:54,000 returning from functions by value we're 21745 16:23:51,595 --> 16:23:56,159 going to grab our template files pretty 21746 16:23:54,000 --> 16:23:58,400 quick we're going to copy them let's do 21747 16:23:56,160 --> 16:24:00,956 that and we're going to put them in the 21748 16:24:00,955 --> 16:24:05,595 and we're going to open this guy up in 21749 16:24:03,360 --> 16:24:06,559 visual studio code pretty quick let's do 21750 16:24:06,559 --> 16:24:11,915 now we have our main cpp file here we 21751 16:24:09,436 --> 16:24:14,000 can clean it up a little bit 21752 16:24:11,915 --> 16:24:15,519 and close the left pane here because we 21753 16:24:14,000 --> 16:24:17,436 don't need it the first thing we're 21754 16:24:15,519 --> 16:24:19,519 going to do is to put in our function 21755 16:24:17,436 --> 16:24:22,796 which is going to compute the sum of two 21756 16:24:19,519 --> 16:24:24,955 integers and return as the sum of these 21757 16:24:22,796 --> 16:24:27,200 two guys inside the function we're going 21758 16:24:24,955 --> 16:24:29,116 to set up a result variable which is 21759 16:24:27,199 --> 16:24:31,039 going to store the sum and we're going 21760 16:24:29,116 --> 16:24:33,276 to return it and it is going to be 21761 16:24:31,040 --> 16:24:35,275 visible to whoever has called this 21762 16:24:33,275 --> 16:24:37,436 function here again i am going to 21763 16:24:35,275 --> 16:24:40,319 emphasize that this is going to return 21764 16:24:37,436 --> 16:24:43,596 by value so what we return is really 21765 16:24:40,319 --> 16:24:45,275 going to be a copy of this variable here 21766 16:24:43,595 --> 16:24:47,756 and we're going to prove that by 21767 16:24:45,275 --> 16:24:50,639 printing the address of this variable 21768 16:24:47,756 --> 16:24:52,319 inside and outside the function here so 21769 16:24:50,639 --> 16:24:54,559 we're going to set up two variables in 21770 16:24:52,319 --> 16:24:57,519 main we're going to say and x let's call 21771 16:24:54,559 --> 16:25:00,239 our input parameters x and y we're going 21772 16:24:57,519 --> 16:25:02,559 to put in a five and the second guy is 21773 16:25:00,239 --> 16:25:05,115 going to be a y and it's going to store 21774 16:25:02,559 --> 16:25:07,040 an n9 why not after that we're going to 21775 16:25:05,116 --> 16:25:09,916 set up a variable which is going to 21776 16:25:07,040 --> 16:25:12,400 store the results let's call this result 21777 16:25:09,915 --> 16:25:14,795 why not and we're going to say some x 21778 16:25:12,400 --> 16:25:17,275 and y okay we are passing these guys by 21779 16:25:14,796 --> 16:25:20,160 value because we don't really care about 21780 16:25:17,275 --> 16:25:22,879 copying and and it is a fundamental type 21781 16:25:20,160 --> 16:25:25,520 it is really small in memory so it's 21782 16:25:22,879 --> 16:25:28,079 going to be very cheap to copy it's not 21783 16:25:25,519 --> 16:25:31,435 going to hurt the performance of our 21784 16:25:28,080 --> 16:25:33,276 application but what we want to do now 21785 16:25:33,275 --> 16:25:39,519 the address of the reserved variable in 21786 16:25:37,040 --> 16:25:41,756 the main function here and see that it 21787 16:25:41,756 --> 16:25:46,000 address of the variable we have inside 21788 16:25:44,319 --> 16:25:48,400 here another thing you should watch out 21789 16:25:46,000 --> 16:25:50,879 for is that we are able to get data 21790 16:25:48,400 --> 16:25:53,840 outside the function here the sum was a 21791 16:25:50,879 --> 16:25:56,559 local variable but we are able to get it 21792 16:25:53,839 --> 16:25:59,199 out of the function through the return 21793 16:25:56,559 --> 16:26:01,680 mechanism here okay before we go ahead 21794 16:25:59,199 --> 16:26:04,239 and run this program we're going to fix 21795 16:26:01,680 --> 16:26:07,116 the statement here we forgot to change 21796 16:26:04,239 --> 16:26:09,275 this to say out to mean that it is 21797 16:26:07,116 --> 16:26:12,319 outside the function here that's the 21798 16:26:09,275 --> 16:26:14,639 meaning we're going to build with gcc 21799 16:26:12,319 --> 16:26:16,559 the world is good we can clear and run 21800 16:26:14,639 --> 16:26:19,839 rooster now we're going to see that 21801 16:26:16,559 --> 16:26:22,000 inside the function the address of the 21802 16:26:24,239 --> 16:26:29,040 and outside the function when we hit 21803 16:26:27,116 --> 16:26:32,400 the end of the function here we're going 21804 16:26:29,040 --> 16:26:35,520 to see that the address ends in de4 so 21805 16:26:32,400 --> 16:26:38,000 this proves that the local variable 21806 16:26:35,519 --> 16:26:40,639 inside the function and the outside 21807 16:26:38,000 --> 16:26:42,879 variable here in the main function 21808 16:26:40,639 --> 16:26:44,955 leaving two different memory addresses 21809 16:26:42,879 --> 16:26:47,595 there are two different variables and 21810 16:26:44,955 --> 16:26:50,319 the return mechanism here is just going 21811 16:26:47,595 --> 16:26:52,639 to copy whatever we have in the local 21812 16:26:50,319 --> 16:26:56,000 variable and make it available on the 21813 16:26:52,639 --> 16:26:58,080 outside here so that we can assign it to 21814 16:26:56,000 --> 16:27:00,239 the reserved variable in our main 21815 16:26:58,080 --> 16:27:02,796 function here this is how the return 21816 16:27:00,239 --> 16:27:05,519 mechanism is working and it is going to 21817 16:27:02,796 --> 16:27:07,360 be returning by value by default it is 21818 16:27:05,519 --> 16:27:09,839 going to be making copies this is the 21819 16:27:07,360 --> 16:27:11,360 message here now that we know this i 21820 16:27:09,839 --> 16:27:13,755 want to show you that there are 21821 16:27:13,756 --> 16:27:18,559 if you return by value like this you 21822 16:27:15,756 --> 16:27:21,436 won't be really always using separate 21823 16:27:18,559 --> 16:27:23,199 memory addresses for the inside and the 21824 16:27:21,436 --> 16:27:25,200 outside here and we're going to prove 21825 16:27:23,199 --> 16:27:26,955 that with a function that is going to 21826 16:27:26,955 --> 16:27:31,680 let's comment what we have out because 21827 16:27:29,275 --> 16:27:34,000 we don't want this to confuse us so 21828 16:27:31,680 --> 16:27:36,239 we're going to bring this inside our 21829 16:27:34,000 --> 16:27:38,879 block comment here and we are going to 21830 16:27:36,239 --> 16:27:40,879 go below our sum function here and set 21831 16:27:38,879 --> 16:27:43,839 up a new function which is going to be 21832 16:27:40,879 --> 16:27:46,719 called add strands it is going to return 21833 16:27:43,839 --> 16:27:49,039 a string by value okay it is by value 21834 16:27:46,720 --> 16:27:51,200 because this return type here is not 21835 16:27:49,040 --> 16:27:53,360 qualified with anything like a pointer 21836 16:27:51,199 --> 16:27:56,159 or a reference so by default it is going 21837 16:27:53,360 --> 16:27:58,400 to be by value and inside you see we are 21838 16:27:56,160 --> 16:28:01,040 doing the exact same thing we did in the 21839 16:27:58,400 --> 16:28:03,360 sum function here we are adding up two 21840 16:28:01,040 --> 16:28:06,239 strands we are storing the results in a 21841 16:28:03,360 --> 16:28:08,239 local variable and we are returning that 21842 16:28:06,239 --> 16:28:11,680 but if we run this program you're going 21843 16:28:08,239 --> 16:28:14,079 to see that the address on the inside is 21844 16:28:11,680 --> 16:28:16,879 going to be the same as the address on 21845 16:28:14,080 --> 16:28:18,560 the outside so let's set up something 21846 16:28:16,879 --> 16:28:20,479 that we can play with here we're going 21847 16:28:18,559 --> 16:28:22,319 to go down in the main function and set 21848 16:28:20,480 --> 16:28:24,840 up a few variables we're going to say 21849 16:28:22,319 --> 16:28:28,080 hdd strand we're going to say 21850 16:28:24,839 --> 16:28:30,719 nstr1 and we're going to store in hello 21851 16:28:28,080 --> 16:28:31,756 let's do that and we're going to say std 21852 16:28:33,756 --> 16:28:38,480 and we're going to store and world by 21853 16:28:36,319 --> 16:28:40,796 now you know what i am trying to do here 21854 16:28:38,480 --> 16:28:42,796 i am trying to say hello world okay once 21855 16:28:40,796 --> 16:28:45,756 we have this we're going to set up a 21856 16:28:42,796 --> 16:28:48,956 resort strike so we're going to say std 21857 16:28:45,756 --> 16:28:51,436 strength and i say result str and we're 21858 16:28:48,955 --> 16:28:52,839 going to say add sdr and we're going to 21859 16:28:52,839 --> 16:28:59,199 nstr1 and in str2 this is how we do this 21860 16:28:57,519 --> 16:29:02,079 and after we do this we're going to 21861 16:28:59,199 --> 16:29:04,399 print out the address of the variable 21862 16:29:02,080 --> 16:29:08,880 here so let's do that and it is going to 21863 16:29:04,400 --> 16:29:11,840 be the address of result hdr let's say 21864 16:29:08,879 --> 16:29:14,719 that here and we also need to print the 21865 16:29:11,839 --> 16:29:17,519 actual result of this operation here so 21866 16:29:14,720 --> 16:29:20,639 let's do that we're going to say sddc 21867 16:29:17,519 --> 16:29:21,595 out result sdr we're going to print the 21868 16:29:21,595 --> 16:29:25,199 strength i don't think we did that on 21869 16:29:25,199 --> 16:29:30,719 sum function here but we can quickly go 21870 16:29:28,239 --> 16:29:33,115 there and prove that to you so let's 21871 16:29:30,720 --> 16:29:35,200 comment this out sorry for this this is 21872 16:29:33,116 --> 16:29:36,880 going to be confusing but i don't want 21873 16:29:38,720 --> 16:29:42,319 so we're going to see that if we print 21874 16:29:40,796 --> 16:29:45,520 this out we're going to get the sum 21875 16:29:42,319 --> 16:29:47,040 which is 14 printed out let's do that 21876 16:29:45,519 --> 16:29:50,479 pretty quick we're going to go down and 21877 16:29:47,040 --> 16:29:52,720 say sddcl and say result and we're going 21878 16:29:50,480 --> 16:29:55,840 to say result and we're going to build 21879 16:29:52,720 --> 16:29:58,000 it pretty quick with the gcc and if we 21880 16:29:55,839 --> 16:30:00,479 clear and run the rooster we're going to 21881 16:29:58,000 --> 16:30:02,239 see the result which is 14 here and you 21882 16:30:00,480 --> 16:30:04,796 can change these guys up to really play 21883 16:30:02,239 --> 16:30:07,436 with us for example if we make this 15 21884 16:30:04,796 --> 16:30:09,520 and put in a 9 we're going to get a 24 21885 16:30:07,436 --> 16:30:11,360 printed out let's build again 21886 16:30:09,519 --> 16:30:13,595 just to have fun with this and we're 21887 16:30:11,360 --> 16:30:15,840 going to clear and run rooster we're 21888 16:30:13,595 --> 16:30:18,319 going to get a 24 here now we can 21889 16:30:15,839 --> 16:30:20,955 comment this back i am happy i have 21890 16:30:18,319 --> 16:30:24,480 showed you this and we can uncomment our 21891 16:30:20,955 --> 16:30:27,116 string code here and uh build and run it 21892 16:30:24,480 --> 16:30:29,436 so we're going to build with gcc the 21893 16:30:27,116 --> 16:30:32,560 build is good we can clear and run 21894 16:30:29,436 --> 16:30:34,480 rooster and again what we want to see 21895 16:30:32,559 --> 16:30:36,720 is that the compiler is going to 21896 16:30:34,480 --> 16:30:39,916 optimize this out it is not going to 21897 16:30:36,720 --> 16:30:42,480 return by value like we had was the sum 21898 16:30:39,915 --> 16:30:44,879 function where we had the address on the 21899 16:30:42,480 --> 16:30:48,240 inside different from the address on the 21900 16:30:44,879 --> 16:30:50,559 outside you see here that inside ah we 21901 16:30:48,239 --> 16:30:52,879 forgot again to change this guy to out 21902 16:30:50,559 --> 16:30:54,879 uh sorry for this so we're going to make 21903 16:30:52,879 --> 16:30:56,955 this guy out and we're going to build 21904 16:30:54,879 --> 16:30:59,680 again and we're going to clear 21905 16:30:56,955 --> 16:31:03,839 clear and run rooster now we're going to 21906 16:30:59,680 --> 16:31:07,915 see that inside the address ends in d30 21907 16:31:03,839 --> 16:31:10,159 and outside the address ends in d30 this 21908 16:31:07,915 --> 16:31:12,635 can be really confusing because we said 21909 16:31:10,160 --> 16:31:15,200 we want to return by value by not 21910 16:31:12,635 --> 16:31:17,519 decorating our return type with a 21911 16:31:15,199 --> 16:31:19,435 pointer or a reference or something like 21912 16:31:17,519 --> 16:31:21,839 that it is going to be returning by 21913 16:31:19,436 --> 16:31:23,916 value and if you return by value you're 21914 16:31:21,839 --> 16:31:27,039 going to make copies and if we made the 21915 16:31:23,915 --> 16:31:29,040 copies the address on the inside and the 21916 16:31:27,040 --> 16:31:31,200 address on the outside should be 21917 16:31:29,040 --> 16:31:32,955 different but they are the same the 21918 16:31:31,199 --> 16:31:35,199 reason is the compiler is going to 21919 16:31:32,955 --> 16:31:36,400 notice that you are trying to copy 21920 16:31:36,400 --> 16:31:40,400 and it is going to see that the variable 21921 16:31:38,559 --> 16:31:42,720 here which is inside the function is 21922 16:31:40,400 --> 16:31:43,756 going to be wasted when the function 21923 16:31:43,756 --> 16:31:48,796 by default this should be really 21924 16:31:45,680 --> 16:31:50,720 destroyed and you should copy 21925 16:31:48,796 --> 16:31:52,480 because you are returning by value but 21926 16:31:50,720 --> 16:31:55,360 the compiler is going to insert an 21927 16:31:52,480 --> 16:31:57,360 optimization to do some magic and make 21928 16:31:55,360 --> 16:31:59,520 it look like you are returning by 21929 16:31:57,360 --> 16:32:01,915 reference and it is really not going to 21930 16:31:59,519 --> 16:32:04,479 make a copy so it is going to reuse the 21931 16:32:01,915 --> 16:32:06,159 address on the inside and hopefully this 21932 16:32:04,480 --> 16:32:08,636 is going to prove this to you the 21933 16:32:06,160 --> 16:32:10,480 message here is that you shouldn't rely 21934 16:32:10,480 --> 16:32:15,040 returning by value because if the 21935 16:32:12,720 --> 16:32:17,756 compiler sees that it can do a better 21936 16:32:15,040 --> 16:32:20,000 job than you it is going to optimize 21937 16:32:17,756 --> 16:32:22,720 your return by value to return by 21938 16:32:20,000 --> 16:32:25,275 reference and you're going to not be 21939 16:32:22,720 --> 16:32:28,635 making copies so don't make your code 21940 16:32:25,275 --> 16:32:31,239 rely on returning by value and making 21941 16:32:28,635 --> 16:32:34,319 copies and you should be aware of these 21942 16:32:31,239 --> 16:32:37,275 optimizations that the compiler can do 21943 16:32:34,319 --> 16:32:39,915 behind your back i would like to welcome 21944 16:32:37,275 --> 16:32:42,319 you in this new chapter where we will be 21945 16:32:39,915 --> 16:32:44,635 learning about function overloading 21946 16:32:42,319 --> 16:32:46,635 function overloading is a mechanism we 21947 16:32:46,635 --> 16:32:52,635 to have multiple copies of the same 21948 16:32:49,199 --> 16:32:54,159 function but taking different parameters 21949 16:32:52,635 --> 16:32:56,000 for example here you see that we have 21950 16:32:54,160 --> 16:32:58,720 different functions but all these 21951 16:32:56,000 --> 16:33:01,595 functions have the same function name 21952 16:32:58,720 --> 16:33:04,400 and these are said to be different 21953 16:33:01,595 --> 16:33:06,559 overloads of the same function there are 21954 16:33:04,400 --> 16:33:08,635 a couple of rules you need to be aware 21955 16:33:06,559 --> 16:33:10,479 of to be able to set up your overalls 21956 16:33:08,635 --> 16:33:11,839 correctly and get them to work and 21957 16:33:10,480 --> 16:33:14,720 that's what we're going to be learning 21958 16:33:11,839 --> 16:33:16,955 about in detail in this chapter but now 21959 16:33:14,720 --> 16:33:19,275 i just want you to see the flexibility 21960 16:33:16,955 --> 16:33:21,199 you can get with function overloading 21961 16:33:19,275 --> 16:33:24,400 for example here if we have a few 21962 16:33:21,199 --> 16:33:27,039 variables in our program we can call the 21963 16:33:24,400 --> 16:33:29,680 max function and the compiler is going 21964 16:33:27,040 --> 16:33:32,080 to select the most fitting overloading 21965 16:33:29,680 --> 16:33:35,040 here so for example if we call our 21966 16:33:32,080 --> 16:33:37,040 function with integer arguments the 21967 16:33:35,040 --> 16:33:39,840 compiler is going to choose this 21968 16:33:37,040 --> 16:33:42,559 overload here if we call a function with 21969 16:33:39,839 --> 16:33:45,519 std string arguments the compiler is 21970 16:33:42,559 --> 16:33:47,275 going to choose this overload here and 21971 16:33:45,519 --> 16:33:48,795 we don't really need to concern 21972 16:33:48,796 --> 16:33:52,480 with making sure we have the correct 21973 16:33:50,720 --> 16:33:54,635 arguments the compiler is going to 21974 16:33:52,480 --> 16:33:56,796 choose the correct function overload for 21975 16:33:54,635 --> 16:33:58,796 us and it is going to call it and it's 21976 16:33:56,796 --> 16:34:01,436 going to be very flexible to call these 21977 16:33:58,796 --> 16:34:04,160 functions from our c plus plus code this 21978 16:34:01,436 --> 16:34:06,635 is how function overload can be really 21979 16:34:04,160 --> 16:34:08,320 useful we are going to stop here in this 21980 16:34:06,635 --> 16:34:10,559 lecture in the next one we're going to 21981 16:34:08,319 --> 16:34:13,519 learn about how you can set up function 21982 16:34:10,559 --> 16:34:14,795 overloads based on different function 21983 16:34:14,796 --> 16:34:19,360 go ahead and finish up here and meet me 21984 16:34:17,199 --> 16:34:22,479 there in this lecture we're going to see 21985 16:34:19,360 --> 16:34:24,955 how we can overload functions based on 21986 16:34:22,480 --> 16:34:27,436 the parameters that we pass to the 21987 16:34:24,955 --> 16:34:29,839 function when we are declaring it or 21988 16:34:27,436 --> 16:34:32,080 defining it and here is a simple example 21989 16:34:29,839 --> 16:34:34,319 to drive the point home here we have a 21990 16:34:36,319 --> 16:34:40,635 it is taking two parameters which are of 21991 16:34:40,635 --> 16:34:46,319 and by overloading this function we will 21992 16:34:43,756 --> 16:34:48,955 have to use the same name as this 21993 16:34:46,319 --> 16:34:51,360 function here so the differences really 21994 16:34:48,955 --> 16:34:54,319 will be in the parameters that we pass 21995 16:34:51,360 --> 16:34:55,275 to the function because the return type 21996 16:34:55,275 --> 16:35:00,479 involved in defining the signature of 21997 16:34:58,480 --> 16:35:02,720 the function so if we try to set up 21998 16:35:00,480 --> 16:35:04,560 another function called max here which 21999 16:35:04,559 --> 16:35:09,519 same parameters as our max on top here 22000 16:35:07,839 --> 16:35:11,679 the difference is only going to be in 22001 16:35:09,519 --> 16:35:13,595 the return type the compiler is not 22002 16:35:11,680 --> 16:35:17,116 going to allow us to do this because the 22003 16:35:13,595 --> 16:35:19,756 return type isn't really significant 22004 16:35:17,116 --> 16:35:21,360 in making two functions different okay 22005 16:35:19,756 --> 16:35:24,400 the only way we have to set up a 22006 16:35:21,360 --> 16:35:27,275 function which is legal in c plus plus 22007 16:35:24,400 --> 16:35:29,680 is to change something about the 22008 16:35:27,275 --> 16:35:31,436 parameters that we have here we can 22009 16:35:29,680 --> 16:35:33,915 change the types like you see here for 22010 16:35:31,436 --> 16:35:36,000 example we change the types to b double 22011 16:35:33,915 --> 16:35:38,159 if the types are different we might even 22012 16:35:36,000 --> 16:35:40,080 change the order of the parameters you 22013 16:35:38,160 --> 16:35:43,520 just have to make sure the parameter 22014 16:35:40,080 --> 16:35:45,040 list is different in one way or another 22015 16:35:43,519 --> 16:35:48,079 that's how you're going to get this 22016 16:35:45,040 --> 16:35:49,915 function to run with the same names here 22017 16:35:48,080 --> 16:35:52,319 and if we manage to set up multiple 22018 16:35:49,915 --> 16:35:54,559 functions with the same name in c plus 22019 16:35:52,319 --> 16:35:56,955 plus these functions are going to be 22020 16:35:54,559 --> 16:35:59,040 called overloads of each other okay if 22021 16:35:56,955 --> 16:36:01,680 we go down here we have another overload 22022 16:35:59,040 --> 16:36:03,756 for the max function this overload is 22023 16:36:01,680 --> 16:36:05,680 going to take two parameters and the 22024 16:36:03,756 --> 16:36:08,239 parameters are going to be of type 22025 16:36:05,680 --> 16:36:10,635 string view they are different from what 22026 16:36:08,239 --> 16:36:13,915 we have on top here which is ant so this 22027 16:36:10,635 --> 16:36:16,319 is a legal overload for this max 22028 16:36:13,915 --> 16:36:18,479 function here and we can use it okay we 22029 16:36:16,319 --> 16:36:21,275 really have a few ways we can make our 22030 16:36:18,480 --> 16:36:23,916 function overloads by now we can change 22031 16:36:21,275 --> 16:36:26,079 the order of the parameter we can change 22032 16:36:23,915 --> 16:36:28,559 the number of the parameters or even we 22033 16:36:26,080 --> 16:36:31,680 can change the types of the parameters 22034 16:36:28,559 --> 16:36:34,159 again function overloads are a mechanism 22035 16:36:31,680 --> 16:36:36,879 we have in c plus plus to set up 22036 16:36:34,160 --> 16:36:39,520 multiple functions with the same name to 22037 16:36:36,879 --> 16:36:41,199 make our code easier to work with okay 22038 16:36:39,519 --> 16:36:43,755 now that you know that let's look at a 22039 16:36:41,199 --> 16:36:45,839 simple example of how we can call our 22040 16:36:43,756 --> 16:36:48,080 overloads we have a few variables 22041 16:36:45,839 --> 16:36:50,635 declared here we have ant variables we 22042 16:36:48,080 --> 16:36:52,560 have double variables and we have std 22043 16:36:52,559 --> 16:36:57,756 and if you go down here we are calling 22044 16:36:54,796 --> 16:37:00,080 our max function passing in integers so 22045 16:36:57,756 --> 16:37:02,000 the overload that takes integers for 22046 16:37:00,080 --> 16:37:05,116 this max function is going to be called 22047 16:37:02,000 --> 16:37:07,116 here down here we are passing in integer 22048 16:37:05,116 --> 16:37:10,000 literals so this is going to call the 22049 16:37:07,116 --> 16:37:12,480 int overload down here we are calling 22050 16:37:10,000 --> 16:37:15,040 the function with double parameters or 22051 16:37:12,480 --> 16:37:17,276 arguments so this is going to call our 22052 16:37:15,040 --> 16:37:20,319 double overload for the match function 22053 16:37:17,275 --> 16:37:22,879 here and down here we are passing in our 22054 16:37:20,319 --> 16:37:25,839 string view parameters and this is going 22055 16:37:22,879 --> 16:37:28,479 to call the overload that takes string 22056 16:37:25,839 --> 16:37:29,915 view parameters and the last guy here is 22057 16:37:29,915 --> 16:37:37,199 two string literals string literals can 22058 16:37:33,040 --> 16:37:40,796 implicitly convert to std string view so 22059 16:37:37,199 --> 16:37:43,360 our std string view overload for the max 22060 16:37:40,796 --> 16:37:44,635 function here is going to be called 22061 16:37:43,360 --> 16:37:46,879 now that we know this we're going to 22062 16:37:44,635 --> 16:37:49,116 head over to visual studio code and play 22063 16:37:46,879 --> 16:37:51,680 with this a little more okay here we are 22064 16:37:49,116 --> 16:37:54,720 in our working folder the current 22065 16:37:51,680 --> 16:37:57,756 project is overloading with different 22066 16:37:54,720 --> 16:38:00,239 parameters here we're going to grab our 22067 16:37:57,756 --> 16:38:02,239 template files like we always do we're 22068 16:38:02,239 --> 16:38:06,319 and we are going to open this guy in 22069 16:38:04,160 --> 16:38:08,720 visual studio code by dragging and 22070 16:38:06,319 --> 16:38:10,879 dropping on top of visual studio code 22071 16:38:08,720 --> 16:38:12,720 here this is going to open our project 22072 16:38:10,879 --> 16:38:15,680 here we're going to click on our main 22073 16:38:12,720 --> 16:38:18,000 cpp file and open it we're going to 22074 16:38:15,680 --> 16:38:20,635 clean it up a little bit and close the 22075 16:38:18,000 --> 16:38:23,519 left pane here we are going to go on top 22076 16:38:20,635 --> 16:38:26,239 here and put in our first max function 22077 16:38:23,519 --> 16:38:28,635 here the return type is and in this case 22078 16:38:26,239 --> 16:38:30,795 it is going to take two parameters and 22079 16:38:28,635 --> 16:38:33,199 it is going to return whichever number 22080 16:38:30,796 --> 16:38:35,756 is the maximum and we are taking 22081 16:38:33,199 --> 16:38:38,159 parameters in by value and returning by 22082 16:38:35,756 --> 16:38:39,840 value here nothing special we can go in 22083 16:38:38,160 --> 16:38:41,360 main and call this function and you 22084 16:38:39,839 --> 16:38:43,275 already know how to do that i don't 22085 16:38:41,360 --> 16:38:46,239 think i really need to do that 22086 16:38:43,275 --> 16:38:49,040 but now what we really are interested in 22087 16:38:46,239 --> 16:38:51,680 is being able to set up different 22088 16:38:49,040 --> 16:38:54,239 functions with the same function name 22089 16:38:51,680 --> 16:38:56,720 like we have here because if for example 22090 16:38:54,239 --> 16:38:58,795 you take this function and put that down 22091 16:38:56,720 --> 16:39:01,520 below we're going to make a copy of it 22092 16:38:58,796 --> 16:39:03,596 and only change the return type if we 22093 16:39:01,519 --> 16:39:06,559 try to compile this program you see that 22094 16:39:03,595 --> 16:39:08,879 even visual studio code is unhappy now 22095 16:39:06,559 --> 16:39:11,595 if we bring up a terminal it is going to 22096 16:39:08,879 --> 16:39:14,955 tell us cannot overload function 22097 16:39:11,595 --> 16:39:17,519 distinguished by the return type alone 22098 16:39:14,955 --> 16:39:20,319 so again the message is super clear here 22099 16:39:17,519 --> 16:39:22,559 the return type is not enough to make 22100 16:39:20,319 --> 16:39:26,559 two functions different because the 22101 16:39:22,559 --> 16:39:28,879 return type isn't involved in making the 22102 16:39:26,559 --> 16:39:30,879 signature unique what makes the 22103 16:39:28,879 --> 16:39:33,755 signature unique is either the function 22104 16:39:30,879 --> 16:39:36,079 name or the parameters that you pass to 22105 16:39:33,756 --> 16:39:38,480 the function here so we're going to make 22106 16:39:36,080 --> 16:39:40,160 this return type back a double and we're 22107 16:39:38,480 --> 16:39:42,720 going to figure out a way to make these 22108 16:39:40,160 --> 16:39:44,400 two functions different but have the 22109 16:39:42,720 --> 16:39:45,680 same name here because sometimes you 22110 16:39:45,680 --> 16:39:49,756 one way we have to do this is to change 22111 16:39:47,680 --> 16:39:51,519 the types of the parameters that we pass 22112 16:39:49,756 --> 16:39:53,040 in here so we're going to make them 22113 16:39:51,519 --> 16:39:55,115 double and we're going to make them 22114 16:39:53,040 --> 16:39:58,000 double here and this is going to be 22115 16:39:55,116 --> 16:40:00,400 valid c plus plus could these two 22116 16:39:58,000 --> 16:40:02,635 functions are different even if they 22117 16:40:00,400 --> 16:40:04,560 have the same name here they are two 22118 16:40:02,635 --> 16:40:06,879 different functions because they take 22119 16:40:04,559 --> 16:40:08,955 different parameters so if you try to 22120 16:40:08,955 --> 16:40:14,080 integers this one is going to be called 22121 16:40:11,595 --> 16:40:16,239 which takes integers n if you call it 22122 16:40:14,080 --> 16:40:17,916 with double arguments this guy here is 22123 16:40:16,239 --> 16:40:20,400 going to be called let's try and prove 22124 16:40:17,915 --> 16:40:24,079 this so we're going to go inside and say 22125 16:40:20,400 --> 16:40:26,000 stdc out and overload called i think 22126 16:40:24,080 --> 16:40:28,635 this is enough and we're going to go in 22127 16:40:26,000 --> 16:40:31,116 the double function here and say sddc 22128 16:40:31,116 --> 16:40:35,840 okay once we have this we also have to 22129 16:40:33,199 --> 16:40:37,275 change the return type here to double 22130 16:40:35,839 --> 16:40:39,839 because it's going to make things a 22131 16:40:37,275 --> 16:40:43,040 little bit consistent here but now we 22132 16:40:39,839 --> 16:40:44,879 want to go in our main function here 22133 16:40:43,040 --> 16:40:46,796 and set up a few variables we're going 22134 16:40:44,879 --> 16:40:48,879 to say and x and we're going to make 22135 16:40:46,796 --> 16:40:51,200 this a four and we're going to say into 22136 16:40:48,879 --> 16:40:52,319 y and we're going to make this a nine 22137 16:40:52,319 --> 16:40:57,519 we're going to set up also double 22138 16:40:59,436 --> 16:41:04,635 point four to make it a double variable 22139 16:41:02,559 --> 16:41:08,479 and we're going to say double 22140 16:41:09,680 --> 16:41:15,199 now we are going to call max we're going 22141 16:41:11,595 --> 16:41:17,680 to say auto result and say max and 22142 16:41:17,680 --> 16:41:22,319 what do you think we're going to return 22143 16:41:19,436 --> 16:41:24,635 here so what we really are interested in 22144 16:41:22,319 --> 16:41:26,796 is the return type of the result 22145 16:41:24,635 --> 16:41:29,360 variable here and the compiler is going 22146 16:41:26,796 --> 16:41:31,916 to deduce whatever is returned by this 22147 16:41:29,360 --> 16:41:34,480 function if we call the function with x 22148 16:41:31,915 --> 16:41:36,079 and y x and y are integers so the 22149 16:41:34,480 --> 16:41:38,240 compiler is going to search for a 22150 16:41:36,080 --> 16:41:39,840 function that takes integers and it's 22151 16:41:38,239 --> 16:41:41,756 going to find this guy here and that's 22152 16:41:41,756 --> 16:41:46,635 let's weld and run this program we are 22153 16:41:44,400 --> 16:41:49,916 not interested in looking at the output 22154 16:41:46,635 --> 16:41:52,479 here we're just going to see this std 22155 16:41:49,915 --> 16:41:55,040 out statement here print whatever 22156 16:41:52,480 --> 16:41:56,636 function was called okay we expect the 22157 16:41:55,040 --> 16:41:59,360 end one to be called so we're going to 22158 16:41:56,635 --> 16:42:02,400 see ant overload called on the terminal 22159 16:41:59,360 --> 16:42:04,400 here when we run this program so let's 22160 16:42:02,400 --> 16:42:07,275 do that we're going to build with gcc 22161 16:42:04,400 --> 16:42:09,840 like we always do the world is good so 22162 16:42:07,275 --> 16:42:12,319 we can clear and run rooster 22163 16:42:09,839 --> 16:42:14,795 we're going to see int overload called 22164 16:42:14,796 --> 16:42:19,200 we can set up two functions with the 22165 16:42:16,720 --> 16:42:22,319 same name and make them different only 22166 16:42:19,199 --> 16:42:24,795 in the parameters that they take and the 22167 16:42:22,319 --> 16:42:27,199 differences may be either in the types 22168 16:42:24,796 --> 16:42:29,680 of the parameter like we are doing here 22169 16:42:27,199 --> 16:42:31,915 they may also be in the order of the 22170 16:42:29,680 --> 16:42:34,160 parameters and we're going to see an 22171 16:42:31,915 --> 16:42:36,559 example of that in a minute but before 22172 16:42:34,160 --> 16:42:38,880 we do that we also need to try this with 22173 16:42:36,559 --> 16:42:41,115 a and b and show you that the double one 22174 16:42:38,879 --> 16:42:43,755 is going to be called because we are 22175 16:42:41,116 --> 16:42:45,756 passing in double arguments here so 22176 16:42:43,756 --> 16:42:48,796 let's build again the bullet is good we 22177 16:42:45,756 --> 16:42:51,916 can clear and run rooster this is going 22178 16:42:48,796 --> 16:42:54,400 to call our double overload hopefully 22179 16:42:51,915 --> 16:42:56,719 this really drives the point home 22180 16:42:54,400 --> 16:42:59,275 that we can set up multiple overloads 22181 16:42:56,720 --> 16:43:00,720 for the same function name and again the 22182 16:42:59,275 --> 16:43:03,040 differences are going to be in the 22183 16:43:00,720 --> 16:43:06,000 parameters that we pass here we have 22184 16:43:03,040 --> 16:43:08,480 been able to pass different types for 22185 16:43:06,000 --> 16:43:10,955 the parameters and this was a vehicle c 22186 16:43:08,480 --> 16:43:14,000 plus plus code but we can also set up 22187 16:43:10,955 --> 16:43:17,040 another overload just by changing the 22188 16:43:14,000 --> 16:43:18,635 types and the order of the parameters 22189 16:43:17,040 --> 16:43:21,360 let's do that we're going to make the 22190 16:43:18,635 --> 16:43:23,595 first parameter here and and once we do 22191 16:43:21,360 --> 16:43:26,480 that this is different from any of the 22192 16:43:23,595 --> 16:43:29,199 overloads we've had so this is a valid 22193 16:43:26,480 --> 16:43:31,436 overload for this max function here this 22194 16:43:29,199 --> 16:43:33,680 is going to work we can even change the 22195 16:43:31,436 --> 16:43:34,880 order of these two guys and it is also 22196 16:43:34,879 --> 16:43:39,199 a valid overload let's put in another 22197 16:43:39,199 --> 16:43:43,039 and we're going to change the first 22198 16:43:40,955 --> 16:43:46,239 parameter to double and the second one 22199 16:43:43,040 --> 16:43:48,635 to end and now we have four overloads 22200 16:43:46,239 --> 16:43:51,115 for the max function and again the 22201 16:43:48,635 --> 16:43:54,080 message here is that you just have to 22202 16:43:51,116 --> 16:43:56,635 make sure the parameters are different 22203 16:43:54,080 --> 16:43:59,040 in some way and your overload is going 22204 16:43:56,635 --> 16:44:00,720 to be valid and again i'm not telling 22205 16:43:59,040 --> 16:44:02,400 you to set up functions like this 22206 16:44:00,720 --> 16:44:05,116 because in this case this is really 22207 16:44:02,400 --> 16:44:07,520 useless but i am trying to expose this 22208 16:44:05,116 --> 16:44:10,560 facility to you so that you can use it 22209 16:44:07,519 --> 16:44:12,479 to your advantage if it makes sense for 22210 16:44:10,559 --> 16:44:15,360 whatever application you are designing 22211 16:44:12,480 --> 16:44:18,319 here so let's go down and put in 22212 16:44:15,360 --> 16:44:20,080 meaningful output i think we can come in 22213 16:44:18,319 --> 16:44:22,955 here and say the parameters that we are 22214 16:44:20,080 --> 16:44:25,200 passing in here so we can say and double 22215 16:44:22,955 --> 16:44:28,000 overload called and if we go down here 22216 16:44:25,199 --> 16:44:30,635 you can take this out and say double and 22217 16:44:28,000 --> 16:44:32,720 overload called and to get any of these 22218 16:44:30,635 --> 16:44:35,199 guys to be called we have to pass the 22219 16:44:32,720 --> 16:44:36,160 parameters or of the arguments in this 22220 16:44:36,160 --> 16:44:42,480 so if we pass in an end which is x x is 22221 16:44:39,839 --> 16:44:44,719 in it and we pass in a double which is a 22222 16:44:42,480 --> 16:44:47,520 b here this is going to call our 22223 16:44:44,720 --> 16:44:50,000 overload which takes the first argument 22224 16:44:47,519 --> 16:44:51,275 to be an integer and the second argument 22225 16:44:51,275 --> 16:44:55,756 let's prove that we're going to 22226 16:44:53,275 --> 16:44:57,839 build with the gcc the bolt is good so 22227 16:44:55,756 --> 16:45:00,720 we can clear and we can run rooster and 22228 16:44:57,839 --> 16:45:01,519 we see ant double called because x is an 22229 16:45:01,519 --> 16:45:07,595 and b is a double if we flip these guys 22230 16:45:04,720 --> 16:45:09,916 let's say b and x then we're going to 22231 16:45:07,595 --> 16:45:12,795 have the overload that takes the first 22232 16:45:09,915 --> 16:45:15,115 argument of double type and the second 22233 16:45:12,796 --> 16:45:17,116 argument of into type to be called and 22234 16:45:15,116 --> 16:45:19,840 that's this guy here and we're going to 22235 16:45:17,116 --> 16:45:22,480 see this message printed out on the 22236 16:45:19,839 --> 16:45:24,239 terminal here let's build with gcc to 22237 16:45:22,480 --> 16:45:25,040 really prove this so we're going to do 22238 16:45:25,040 --> 16:45:28,400 the bullet is good we can clear and run 22239 16:45:28,400 --> 16:45:32,480 and the double end overload called 22240 16:45:30,720 --> 16:45:35,595 hopefully you can really see that you 22241 16:45:32,480 --> 16:45:38,000 can set up all kinds of crazy overloads 22242 16:45:35,595 --> 16:45:39,680 for your functions the next thing i want 22243 16:45:38,000 --> 16:45:42,239 you to see is that we can even change 22244 16:45:39,680 --> 16:45:44,635 the number of the parameters for example 22245 16:45:42,239 --> 16:45:47,275 here we can set up another guy here and 22246 16:45:44,635 --> 16:45:49,839 i call it max it is going to be exactly 22247 16:45:47,275 --> 16:45:52,795 like the one on top here for the first 22248 16:45:49,839 --> 16:45:56,239 two parameters but let's add a third 22249 16:45:52,796 --> 16:45:57,840 parameter maybe make it an end and say c 22250 16:45:57,839 --> 16:46:02,559 a valid overload for the max function 22251 16:46:00,400 --> 16:46:04,000 here so we're going to say double and 22252 16:46:04,000 --> 16:46:08,796 three parameters i won't go into the 22253 16:46:06,720 --> 16:46:11,360 details of how we can compute the 22254 16:46:08,796 --> 16:46:13,436 maximum between these three guys what i 22255 16:46:11,360 --> 16:46:15,756 really am interested in is showing you 22256 16:46:13,436 --> 16:46:17,436 that you can set up overloads here so 22257 16:46:15,756 --> 16:46:20,480 for this case we are just going to 22258 16:46:17,436 --> 16:46:23,596 return let's just say a this is going to 22259 16:46:20,480 --> 16:46:25,200 work but this is a valid overload for 22260 16:46:23,595 --> 16:46:27,839 the match function and it is going to 22261 16:46:25,199 --> 16:46:30,159 compile so let's draw and do that we're 22262 16:46:27,839 --> 16:46:32,635 going to pass and a double a net and the 22263 16:46:30,160 --> 16:46:34,880 net so we're going to pass in b which is 22264 16:46:34,879 --> 16:46:39,360 let's see what is called here we can 22265 16:46:36,400 --> 16:46:41,520 compile with gcc the border is good we 22266 16:46:41,519 --> 16:46:44,879 try to come up with what is going to be 22267 16:46:43,275 --> 16:46:47,115 printed out here what is going to be 22268 16:46:44,879 --> 16:46:48,635 printed okay now that you have guessed 22269 16:46:47,116 --> 16:46:50,880 we're going to run and we're going to 22270 16:46:48,635 --> 16:46:53,595 see double and ant because this is going 22271 16:46:50,879 --> 16:46:56,319 to call this overload again you can 22272 16:46:53,595 --> 16:46:58,795 overload based on the differences in the 22273 16:46:56,319 --> 16:47:00,480 types of the parameters that you pass 22274 16:46:58,796 --> 16:47:03,276 you can change the order of the 22275 16:47:00,480 --> 16:47:06,480 parameters like we are doing here double 22276 16:47:03,275 --> 16:47:08,635 end and end double or you can even 22277 16:47:06,480 --> 16:47:11,276 change the number of the parameters 22278 16:47:08,635 --> 16:47:13,756 using these techniques you can make sure 22279 16:47:11,275 --> 16:47:14,879 that your overloads are different in 22280 16:47:14,879 --> 16:47:18,719 before i close this lecture i'm going to 22281 16:47:16,955 --> 16:47:20,400 show you that you can also use another 22282 16:47:20,400 --> 16:47:24,955 string view parameters let's go down 22283 16:47:24,955 --> 16:47:29,275 again the name of the function is max 22284 16:47:26,879 --> 16:47:31,040 the return type isn't really meaningful 22285 16:47:29,275 --> 16:47:33,275 in making sure two functions are 22286 16:47:31,040 --> 16:47:36,239 different but watch the parameters we 22287 16:47:33,275 --> 16:47:38,559 pass here the types are string view 22288 16:47:36,239 --> 16:47:40,879 and we have a and b here and we are 22289 16:47:38,559 --> 16:47:44,635 going to compute the maximum so if we go 22290 16:47:40,879 --> 16:47:46,879 down here and say max and pass n hello 22291 16:47:44,635 --> 16:47:49,756 and world the compiler is going to try 22292 16:47:46,879 --> 16:47:51,680 and look for a function that takes two 22293 16:47:49,756 --> 16:47:53,360 character array parameters it's not 22294 16:47:51,680 --> 16:47:55,840 going to find it because we don't have 22295 16:47:53,360 --> 16:47:57,840 that guy in here but it's going to see 22296 16:47:57,839 --> 16:48:03,115 take something i can come up with 22297 16:48:01,040 --> 16:48:04,720 through implicit conversions and the 22298 16:48:03,116 --> 16:48:06,480 compiler is going to see that it can 22299 16:48:06,480 --> 16:48:12,160 a string literal to a string view type 22300 16:48:09,915 --> 16:48:13,839 and it is going to insert an implicit 22301 16:48:13,839 --> 16:48:18,879 string literal to string view and this 22302 16:48:16,635 --> 16:48:21,199 function here is going to be called so 22303 16:48:18,879 --> 16:48:23,435 we're going to put in a an output 22304 16:48:21,199 --> 16:48:27,839 statement here we're going to say 22305 16:48:23,436 --> 16:48:30,480 string view string view overload called 22306 16:48:27,839 --> 16:48:32,879 and if we try and run this program let's 22307 16:48:30,480 --> 16:48:33,916 build with gcc make sure that the build 22308 16:48:33,915 --> 16:48:38,955 the world is good so we can clear and 22309 16:48:36,480 --> 16:48:41,680 run rooster this is going to show string 22310 16:48:38,955 --> 16:48:44,159 view string view overload called we are 22311 16:48:41,680 --> 16:48:46,480 able to call this guy here and again the 22312 16:48:44,160 --> 16:48:49,040 message is that we really have a lot of 22313 16:48:46,480 --> 16:48:52,400 flexibility in how we can set up 22314 16:48:49,040 --> 16:48:55,436 overloads for a function in c plus plus 22315 16:48:52,400 --> 16:48:57,916 and please use this facility at your 22316 16:48:55,436 --> 16:48:59,596 advantage if this makes things better 22317 16:48:57,915 --> 16:49:02,239 for whatever application you are 22318 16:48:59,595 --> 16:49:04,159 designing i would like to welcome you in 22319 16:49:02,239 --> 16:49:06,795 this new chapter where we're going to be 22320 16:49:04,160 --> 16:49:09,596 learning about lambda functions lambda 22321 16:49:06,796 --> 16:49:12,796 functions are a mechanism we have in c 22322 16:49:09,595 --> 16:49:15,040 plus plus to set up anonymous functions 22323 16:49:12,796 --> 16:49:17,116 once we have anonymous function set up 22324 16:49:15,040 --> 16:49:19,915 we can do all kinds of crazy things with 22325 16:49:17,116 --> 16:49:22,635 them for example we can give them names 22326 16:49:19,915 --> 16:49:25,040 and code them multiple times or we can 22327 16:49:22,635 --> 16:49:27,360 even call them directly without even 22328 16:49:25,040 --> 16:49:29,520 giving them a name i realize this can be 22329 16:49:27,360 --> 16:49:31,840 really cryptic so we're going to head 22330 16:49:29,519 --> 16:49:35,595 over to the next lecture and see how we 22331 16:49:31,839 --> 16:49:37,519 can declare and use our lambda functions 22332 16:49:35,595 --> 16:49:39,519 go ahead and finish up here 22333 16:49:37,519 --> 16:49:42,000 and meet me there in this lecture we're 22334 16:49:39,519 --> 16:49:44,879 going to see how we can declare and use 22335 16:49:42,000 --> 16:49:47,680 our lambda functions and again lambda 22336 16:49:44,879 --> 16:49:51,040 functions are a mechanism we have in c 22337 16:49:47,680 --> 16:49:53,360 plus plus to set up anonymous functions 22338 16:49:51,040 --> 16:49:55,520 and an anonymous function is a function 22339 16:49:53,360 --> 16:49:57,680 without a name we're going to see how we 22340 16:49:55,519 --> 16:49:59,915 can set up an anonymous function 22341 16:49:57,680 --> 16:50:02,000 optionally give it a name and call it or 22342 16:49:59,915 --> 16:50:04,319 we can even call it directly without 22343 16:50:02,000 --> 16:50:06,635 giving that a name let's see how we can 22344 16:50:04,319 --> 16:50:08,400 do that before we talk about anything 22345 16:50:06,635 --> 16:50:09,839 about lambda functions we're going to 22346 16:50:09,839 --> 16:50:15,115 the lambda function signature and there 22347 16:50:12,879 --> 16:50:16,879 is a special syntax you have to follow 22348 16:50:15,116 --> 16:50:18,796 if you want to set up a lambda function 22349 16:50:16,879 --> 16:50:21,199 in c plus plus the first thing you have 22350 16:50:18,796 --> 16:50:24,160 to put in place is angle brackets like 22351 16:50:21,199 --> 16:50:26,479 this and that's going to be your capture 22352 16:50:24,160 --> 16:50:27,916 list we are not going to talk anymore 22353 16:50:26,480 --> 16:50:30,560 about this we're going to have a chance 22354 16:50:27,915 --> 16:50:32,479 to talk about this in detail after your 22355 16:50:30,559 --> 16:50:35,040 capture list which is going to leave in 22356 16:50:32,480 --> 16:50:37,596 this pair of angle brackets you're going 22357 16:50:35,040 --> 16:50:39,915 to have the parameters to your lambda 22358 16:50:37,595 --> 16:50:41,839 functions and this is where you can pass 22359 16:50:39,915 --> 16:50:43,915 arguments if you want to call your 22360 16:50:41,839 --> 16:50:46,955 lambda function after that you're going 22361 16:50:43,915 --> 16:50:49,435 to have a return type for your lambda 22362 16:50:46,955 --> 16:50:52,239 function but you don't have to put this 22363 16:50:49,436 --> 16:50:53,916 in explicitly you can live this out and 22364 16:50:52,239 --> 16:50:56,400 the compiler is going to deduce your 22365 16:50:56,400 --> 16:51:01,916 after all these things you have to put 22366 16:50:58,480 --> 16:51:03,916 in a pair of curly braces and inside 22367 16:51:01,915 --> 16:51:06,399 these curly braces we're going to have 22368 16:51:03,915 --> 16:51:09,040 our function body and we can do whatever 22369 16:51:06,400 --> 16:51:11,116 we want our lambda function to do in 22370 16:51:09,040 --> 16:51:12,879 this function body here after this you 22371 16:51:11,116 --> 16:51:15,680 will have to remember to put your 22372 16:51:12,879 --> 16:51:17,040 semicolon to make whatever statements 22373 16:51:17,040 --> 16:51:21,360 this along the function to be valid and 22374 16:51:19,275 --> 16:51:23,839 this is the syntax you have to follow 22375 16:51:21,360 --> 16:51:26,319 here we have a simple example so we have 22376 16:51:23,839 --> 16:51:28,795 our lambda function we want to say hello 22377 16:51:26,319 --> 16:51:30,796 to the console this is what we have in 22378 16:51:28,796 --> 16:51:32,956 the body of the lambda function but 22379 16:51:30,796 --> 16:51:34,880 notice that we have our capture list 22380 16:51:34,879 --> 16:51:40,000 delimited by these angle brackets we 22381 16:51:37,756 --> 16:51:42,080 have our parameter list which is going 22382 16:51:40,000 --> 16:51:43,756 to be empty in this moment because this 22383 16:51:42,080 --> 16:51:46,560 lambda function doesn't take any 22384 16:51:43,756 --> 16:51:48,400 parameter and we have our body which is 22385 16:51:48,400 --> 16:51:54,635 these two curly braces and after that we 22386 16:51:51,519 --> 16:51:57,519 have our semicolon to make this a valid 22387 16:51:54,635 --> 16:51:59,680 c plus plus statement if we try and put 22388 16:51:57,519 --> 16:52:00,399 this thing in our c plus plus source 22389 16:52:00,400 --> 16:52:04,880 this is going to be a valid lambda 22390 16:52:02,400 --> 16:52:07,436 function but it isn't really useful by 22391 16:52:04,879 --> 16:52:09,595 now because we can't call it one way we 22392 16:52:07,436 --> 16:52:12,400 have to call it is to give it a name and 22393 16:52:09,595 --> 16:52:15,275 we can give it a name by assigning our 22394 16:52:12,400 --> 16:52:17,520 lambda function to a variable like we 22395 16:52:15,275 --> 16:52:20,319 see here once we have our lambda 22396 16:52:17,519 --> 16:52:22,719 function declared like this this entire 22397 16:52:20,319 --> 16:52:25,519 thing is going to be our lambda function 22398 16:52:22,720 --> 16:52:28,000 type we can take this and assign that to 22399 16:52:25,519 --> 16:52:28,879 a variable which is going to deduce the 22400 16:52:28,879 --> 16:52:32,719 using auto type deduction here this is 22401 16:52:32,720 --> 16:52:38,559 once this thing has a name then we can 22402 16:52:35,275 --> 16:52:40,720 call it with the syntax we have here and 22403 16:52:38,559 --> 16:52:43,275 if we run this code now it is going to 22404 16:52:40,720 --> 16:52:45,360 print hello world to the console i 22405 16:52:43,275 --> 16:52:48,879 realize this syntax can be really 22406 16:52:45,360 --> 16:52:50,480 cryptic but if you try to remember this 22407 16:52:48,879 --> 16:52:53,040 you're going to find that lambda 22408 16:52:50,480 --> 16:52:54,796 functions are really cool and later in 22409 16:52:53,040 --> 16:52:57,520 the course we will see that they really 22410 16:52:54,796 --> 16:53:00,160 make a lot of things easier in c plus 22411 16:52:57,519 --> 16:53:02,159 plus so try to remember the syntax here 22412 16:53:00,160 --> 16:53:04,796 and everything is going to flow from 22413 16:53:02,160 --> 16:53:06,796 here again the syntax is to have the 22414 16:53:04,796 --> 16:53:08,880 capture list which is going to be the 22415 16:53:06,796 --> 16:53:11,200 first thing the second thing is going to 22416 16:53:08,879 --> 16:53:13,040 be the parameters the third thing is 22417 16:53:11,199 --> 16:53:14,719 going to be the return type which is 22418 16:53:13,040 --> 16:53:16,879 optional you can leave this out and the 22419 16:53:14,720 --> 16:53:18,239 compiler is going to deduce this and 22420 16:53:16,879 --> 16:53:21,755 after this you're going to have your 22421 16:53:18,239 --> 16:53:24,239 pair of curly braces and the body of our 22422 16:53:21,756 --> 16:53:26,720 lambda function is going to leave within 22423 16:53:24,239 --> 16:53:28,559 these curly braces this is the syntax 22424 16:53:26,720 --> 16:53:30,955 you need to remember okay we have seen 22425 16:53:28,559 --> 16:53:33,435 that we can give a name to our lambda 22426 16:53:30,955 --> 16:53:36,000 function and call it but it is also 22427 16:53:33,436 --> 16:53:39,200 possible to call our lambda function 22428 16:53:36,000 --> 16:53:42,239 directly without giving it a name and we 22429 16:53:39,199 --> 16:53:45,275 do that by appending a pair of curly 22430 16:53:42,239 --> 16:53:47,199 braces to the lambda function definition 22431 16:53:45,275 --> 16:53:49,199 here so we have a lambda function we 22432 16:53:47,199 --> 16:53:52,319 have our curly braces and we have our 22433 16:53:49,199 --> 16:53:55,519 parameter list we have our body here and 22434 16:53:52,319 --> 16:53:57,756 after the closing curly brace we're 22435 16:53:55,519 --> 16:54:00,079 going to add a pair of parentheses to 22436 16:53:57,756 --> 16:54:02,480 call this lambda function and because 22437 16:54:00,080 --> 16:54:04,480 this lambda function takes no parameter 22438 16:54:02,480 --> 16:54:06,796 we're not going to put any argument 22439 16:54:04,480 --> 16:54:09,276 within our parenthesis here and this is 22440 16:54:06,796 --> 16:54:11,116 going to call our lambda function don't 22441 16:54:09,275 --> 16:54:12,879 worry if you find this confusing we're 22442 16:54:11,116 --> 16:54:14,720 going to head over to visual studio code 22443 16:54:12,879 --> 16:54:17,199 in a minute and all of this is going to 22444 16:54:14,720 --> 16:54:19,916 make sense okay so far we haven't really 22445 16:54:17,199 --> 16:54:22,319 seen that we can pass parameters to our 22446 16:54:19,915 --> 16:54:24,879 lambda function here is a simple example 22447 16:54:22,319 --> 16:54:27,116 that takes parameters this is a lambda 22448 16:54:24,879 --> 16:54:29,275 function because it follows the syntax 22449 16:54:27,116 --> 16:54:31,756 for lambda functions we have 22450 16:54:29,275 --> 16:54:34,559 our capture list we have our parameter 22451 16:54:31,756 --> 16:54:36,720 list we have our curly braces and the 22452 16:54:34,559 --> 16:54:38,955 body is going to live inside these guys 22453 16:54:36,720 --> 16:54:41,756 and we are calling this lambda function 22454 16:54:38,955 --> 16:54:43,199 directly with this parameter list the 22455 16:54:41,756 --> 16:54:45,756 lambda function is going to take two 22456 16:54:43,199 --> 16:54:48,399 parameters of double type we're going to 22457 16:54:45,756 --> 16:54:50,080 print out the sum of these two numbers 22458 16:54:48,400 --> 16:54:52,080 and that we are going to call this 22459 16:54:50,080 --> 16:54:55,040 lambda function directly notice that we 22460 16:54:57,595 --> 16:55:02,795 literals okay this is how you can set up 22461 16:55:00,160 --> 16:55:04,796 a lambda function that takes parameters 22462 16:55:02,796 --> 16:55:07,436 and call it directly another thing we 22463 16:55:04,796 --> 16:55:09,436 can do is make our lambda function 22464 16:55:07,436 --> 16:55:11,116 return something and that's really 22465 16:55:09,436 --> 16:55:12,880 simple we're going to set up the lambda 22466 16:55:11,116 --> 16:55:15,200 function like this so it's going to take 22467 16:55:15,199 --> 16:55:20,000 and here you see that we are returning a 22468 16:55:17,595 --> 16:55:21,839 plus b and we can do that the lambda 22469 16:55:20,000 --> 16:55:24,635 function is now going to return 22470 16:55:21,839 --> 16:55:28,079 something and this entire thing we have 22471 16:55:24,635 --> 16:55:29,595 here is going to be a lambda expression 22472 16:55:28,080 --> 16:55:31,916 the lambda function is going to be 22473 16:55:29,595 --> 16:55:34,239 called it is going to return something 22474 16:55:31,915 --> 16:55:36,479 and what the lambda function returns is 22475 16:55:34,239 --> 16:55:38,635 going to be assigned to this result 22476 16:55:36,480 --> 16:55:40,796 variable here and if we print it we're 22477 16:55:38,635 --> 16:55:42,955 going to get the sum of whatever pass in 22478 16:55:40,796 --> 16:55:45,116 here printed out on the console and 22479 16:55:42,955 --> 16:55:47,680 you're going to see it printed out on 22480 16:55:45,116 --> 16:55:50,240 the terminal it is also possible to put 22481 16:55:47,680 --> 16:55:52,879 your lambda function directly in an 22482 16:55:50,239 --> 16:55:56,000 output statement i realize this can be 22483 16:55:52,879 --> 16:55:59,199 really confusing but please look at this 22484 16:55:56,000 --> 16:56:02,160 entire lambda function is same here 22485 16:55:59,199 --> 16:56:04,239 as a single value because this is a 22486 16:56:02,160 --> 16:56:06,720 lambda function that is going to return 22487 16:56:04,239 --> 16:56:09,040 something and what is returned by this 22488 16:56:06,720 --> 16:56:11,436 lambda function is going to be printed 22489 16:56:11,436 --> 16:56:16,400 again i realize this may be confusing to 22490 16:56:14,160 --> 16:56:18,240 many of you guys we are going to head 22491 16:56:16,400 --> 16:56:19,840 over to visual studio code in a minute 22492 16:56:18,239 --> 16:56:21,680 and we're going to see this and you're 22493 16:56:19,839 --> 16:56:23,275 going to really understand okay the last 22494 16:56:21,680 --> 16:56:26,635 thing we're going to look at is that we 22495 16:56:23,275 --> 16:56:29,436 can specify a return type to our lambda 22496 16:56:26,635 --> 16:56:30,955 function and the syntax to do this is to 22497 16:56:30,955 --> 16:56:36,955 a greater than symbol and put your type 22498 16:56:34,160 --> 16:56:39,200 after that and when you do that now this 22499 16:56:36,955 --> 16:56:42,795 lambda function is going to be forced to 22500 16:56:39,199 --> 16:56:45,360 return double type even if you pass it 22501 16:56:42,796 --> 16:56:47,436 to parameters that are not of double 22502 16:56:45,360 --> 16:56:49,680 type we are going to add them up and the 22503 16:56:47,436 --> 16:56:51,680 return type is going to be implicitly 22504 16:56:49,680 --> 16:56:53,360 converted to double and that's what 22505 16:56:51,680 --> 16:56:55,595 we're going to return and we can print 22506 16:56:53,360 --> 16:56:57,840 it out on the console and we're going to 22507 16:56:55,595 --> 16:57:00,559 see an example of this in a minute when 22508 16:56:57,839 --> 16:57:02,559 we hit visual studio code in fact this 22509 16:57:00,559 --> 16:57:04,955 is really all i had to share with you in 22510 16:57:02,559 --> 16:57:07,519 the slides i realized many of these 22511 16:57:04,955 --> 16:57:09,839 things may be confusing we are going to 22512 16:57:07,519 --> 16:57:12,159 head over to visual studio code and play 22513 16:57:09,839 --> 16:57:14,000 with these things step by step and i am 22514 16:57:12,160 --> 16:57:17,116 sure you're going to understand here we 22515 16:57:14,000 --> 16:57:19,680 are in our working directory the current 22516 16:57:17,116 --> 16:57:21,680 project is declaring and using lambda 22517 16:57:19,680 --> 16:57:24,160 functions we're going to grab our 22518 16:57:21,680 --> 16:57:26,239 template files and put them in place 22519 16:57:24,160 --> 16:57:29,116 let's do that and we're going to grab 22520 16:57:26,239 --> 16:57:30,879 our folder here and drag and drop it in 22521 16:57:29,116 --> 16:57:33,040 visual studio code and this is going to 22522 16:57:30,879 --> 16:57:36,000 open it it is opened here we have our 22523 16:57:33,040 --> 16:57:38,319 main cpp file opened we can close the 22524 16:57:36,000 --> 16:57:40,480 left pane here because we don't want it 22525 16:57:38,319 --> 16:57:42,319 anymore and we can clean up our function 22526 16:57:40,480 --> 16:57:45,276 here and the first thing we're going to 22527 16:57:42,319 --> 16:57:48,239 do is to put in the syntax for a lambda 22528 16:57:45,275 --> 16:57:50,159 function here and use it as a reference 22529 16:57:48,239 --> 16:57:52,319 as we learn about this i think this is 22530 16:57:50,160 --> 16:57:54,080 going to make things a little easier so 22531 16:57:52,319 --> 16:57:56,559 the first thing we want to do is to 22532 16:57:54,080 --> 16:57:58,400 declare a lambda function and the first 22533 16:57:56,559 --> 16:58:00,795 thing we need to put in is a capture 22534 16:57:58,400 --> 16:58:03,200 list a capture list is going to be in 22535 16:58:00,796 --> 16:58:05,680 globe in this angle bracket so this is 22536 16:58:03,199 --> 16:58:07,755 our empty capture list and after this 22537 16:58:05,680 --> 16:58:10,000 we're going to put in a parameter list 22538 16:58:07,756 --> 16:58:12,239 which is going to leave inside this 22539 16:58:10,000 --> 16:58:13,519 parenthesis here after that we're going 22540 16:58:13,519 --> 16:58:17,519 an optional return type for now we're 22541 16:58:15,756 --> 16:58:20,000 going to ignore this and after that 22542 16:58:17,519 --> 16:58:22,955 we're going to put in our pair of curly 22543 16:58:20,000 --> 16:58:26,319 braces and inside this pair of curly 22544 16:58:22,955 --> 16:58:28,479 braces is where the body of our lambda 22545 16:58:26,319 --> 16:58:30,239 function is going to leave so inside 22546 16:58:28,480 --> 16:58:32,240 here we can do whatever we want in the 22547 16:58:30,239 --> 16:58:35,040 body of this lambda function for example 22548 16:58:32,239 --> 16:58:37,275 we can say hello world which is 22549 16:58:35,040 --> 16:58:39,436 the logical thing to do at this point so 22550 16:58:37,275 --> 16:58:41,275 we're going to say hello world here and 22551 16:58:39,436 --> 16:58:44,319 we're going to put in our new line 22552 16:58:41,275 --> 16:58:47,115 character with endl okay now we have a 22553 16:58:44,319 --> 16:58:50,159 lambda function declared but we have to 22554 16:58:47,116 --> 16:58:52,796 put in a closing semicolon because this 22555 16:58:50,160 --> 16:58:55,436 is a c plus plus statement okay now we 22556 16:58:52,796 --> 16:58:58,000 have our lambda function declared and 22557 16:58:55,436 --> 16:58:59,596 this is valid c plus plus code if we're 22558 16:58:58,000 --> 16:59:01,519 trying to compile this program you're 22559 16:58:59,595 --> 16:59:04,239 going to see that it is going to compile 22560 16:59:01,519 --> 16:59:06,399 fine cape world finished successfully 22561 16:59:04,239 --> 16:59:09,595 and if we try to run it it is not going 22562 16:59:06,400 --> 16:59:11,840 to do a thing because we are declaring 22563 16:59:09,595 --> 16:59:14,399 the lambda function here but we're not 22564 16:59:11,839 --> 16:59:16,399 calling it to get it to do things 22565 16:59:14,400 --> 16:59:18,720 so let's bring up a terminal and show 22566 16:59:16,400 --> 16:59:21,200 you that if we run this program it is 22567 16:59:18,720 --> 16:59:23,275 not going to do anything so let's do 22568 16:59:21,199 --> 16:59:24,635 clear and run rooster you're going to 22569 16:59:23,275 --> 16:59:27,115 see that it's not going to print 22570 16:59:24,635 --> 16:59:30,879 anything and we can even go down here 22571 16:59:27,116 --> 16:59:33,116 and put in a closing stdc out statement 22572 16:59:30,879 --> 16:59:35,115 saying it done to me that we are hitting 22573 16:59:33,116 --> 16:59:38,000 the end of the function here without 22574 16:59:35,116 --> 16:59:40,240 doing anything so if we vote again the 22575 16:59:38,000 --> 16:59:42,480 vote is going to be good we can clear 22576 16:59:40,239 --> 16:59:44,559 and we're going to run rooster you see 22577 16:59:42,480 --> 16:59:46,880 we are printing done we are hitting the 22578 16:59:44,559 --> 16:59:49,915 end of the function here and are we not 22579 16:59:46,879 --> 16:59:52,879 seeing hello world so this function here 22580 16:59:49,915 --> 16:59:55,360 or this lambda function here to be exact 22581 16:59:52,879 --> 16:59:57,435 is not being called one way we have to 22582 16:59:55,360 --> 16:59:59,436 call it is to give it a name 22583 16:59:57,436 --> 17:00:02,480 and we have seen that to give it a name 22584 16:59:59,436 --> 17:00:04,635 we have to assign the entire lambda 22585 17:00:02,480 --> 17:00:06,480 function thing we have here to a 22586 17:00:04,635 --> 17:00:08,239 variable and we're going to use auto 22587 17:00:06,480 --> 17:00:10,956 type deduction here so we're going to 22588 17:00:08,239 --> 17:00:14,319 say auto func and we're going to assign 22589 17:00:10,955 --> 17:00:16,080 our lambda function here to this 22590 17:00:14,319 --> 17:00:18,879 variable and this is going to be 22591 17:00:16,080 --> 17:00:21,520 basically a handle we have in our 22592 17:00:18,879 --> 17:00:24,000 program here to the lambda function and 22593 17:00:21,519 --> 17:00:26,319 we can use this handle to do things with 22594 17:00:24,000 --> 17:00:28,160 the lambda function but by now i want 22595 17:00:26,319 --> 17:00:31,040 you to see that even if we run the 22596 17:00:28,160 --> 17:00:33,276 program now it is not going to do 22597 17:00:31,040 --> 17:00:35,595 anything because we're still not calling 22598 17:00:33,275 --> 17:00:37,519 the lambda function here let's show you 22599 17:00:35,595 --> 17:00:39,360 that we're going to world again and the 22600 17:00:37,519 --> 17:00:42,239 world is going to be good no problem we 22601 17:00:39,360 --> 17:00:44,000 can clear and run rooster still not 22602 17:00:42,239 --> 17:00:46,635 hello world because we're not calling 22603 17:00:44,000 --> 17:00:49,436 our lambda function to call our lambda 22604 17:00:46,635 --> 17:00:53,199 function we have to use our handle here 22605 17:00:49,436 --> 17:00:55,916 which is this variable and use regular 22606 17:00:53,199 --> 17:00:58,559 function called syntax so we can go down 22607 17:00:55,915 --> 17:01:00,955 here and say func and we can put in our 22608 17:00:58,559 --> 17:01:02,955 parenthesis we're not putting in any 22609 17:01:00,955 --> 17:01:06,159 parameter because this lambda here 22610 17:01:02,955 --> 17:01:08,239 doesn't take any parameters and now if 22611 17:01:06,160 --> 17:01:10,480 we run this program it is going to say 22612 17:01:08,239 --> 17:01:13,519 hello world because this line here is 22613 17:01:10,480 --> 17:01:15,436 going to call this lambda function let's 22614 17:01:15,436 --> 17:01:21,275 we're going to use gcc to build 22615 17:01:17,839 --> 17:01:23,275 so now if we clear and run rooster we're 22616 17:01:21,275 --> 17:01:25,275 going to see hello world we can call 22617 17:01:23,275 --> 17:01:27,436 this function multiple times because we 22618 17:01:25,275 --> 17:01:29,199 have a handle to it the handle is going 22619 17:01:29,199 --> 17:01:32,955 throughout the scope of the main 22620 17:01:30,879 --> 17:01:35,519 function here so we're going to go down 22621 17:01:32,955 --> 17:01:38,000 and call it again and if we build we're 22622 17:01:35,519 --> 17:01:40,955 going to build with gcc the world is 22623 17:01:38,000 --> 17:01:43,360 good we can clear and run rooster we're 22624 17:01:40,955 --> 17:01:46,635 going to say hello world twice because 22625 17:01:43,360 --> 17:01:48,635 we are calling our lambda function twice 22626 17:01:46,635 --> 17:01:50,319 this is one way we have to call our 22627 17:01:50,319 --> 17:01:54,080 so we're going to comment this out 22628 17:01:52,400 --> 17:01:56,560 because we're going to see a few other 22629 17:01:54,080 --> 17:01:58,955 things and on top here what we were 22630 17:01:56,559 --> 17:02:01,199 doing is declaring around the function 22631 17:01:58,955 --> 17:02:03,839 and call it through an a now we're going 22632 17:02:01,199 --> 17:02:06,079 to go down set up a lambda function and 22633 17:02:03,839 --> 17:02:08,399 call it directly without giving it a 22634 17:02:06,080 --> 17:02:10,560 name because that's also a possibility 22635 17:02:08,400 --> 17:02:12,560 in c plus plus for that we're going to 22636 17:02:10,559 --> 17:02:14,559 grab our lambda function here and we're 22637 17:02:12,559 --> 17:02:16,879 going to copy it and we're going to say 22638 17:02:14,559 --> 17:02:18,879 declare a lambda function and call it 22639 17:02:16,879 --> 17:02:20,239 direct and the syntax to do that is 22640 17:02:20,239 --> 17:02:24,400 you set up your lambda function like we 22641 17:02:22,239 --> 17:02:26,720 have seen before you see this is our 22642 17:02:24,400 --> 17:02:28,635 lambda function statement here but 22643 17:02:26,720 --> 17:02:31,040 you're going to go to the closing curly 22644 17:02:31,040 --> 17:02:36,879 of parentheses this is going to declare 22645 17:02:33,839 --> 17:02:39,115 the lambda function and call it directly 22646 17:02:36,879 --> 17:02:41,595 notice that we don't have a handle to 22647 17:02:39,116 --> 17:02:44,160 this lambda function so if you do things 22648 17:02:41,595 --> 17:02:46,000 like this you will have only one chance 22649 17:02:44,160 --> 17:02:48,400 to call the lambda function so we're 22650 17:02:46,000 --> 17:02:50,796 going to say hello world once because we 22651 17:02:48,400 --> 17:02:53,596 are declaring the lambda function and 22652 17:02:50,796 --> 17:02:55,276 calling it directly here and after that 22653 17:02:53,595 --> 17:02:57,680 we're going to say done because we're 22654 17:02:55,275 --> 17:02:59,040 going to be hitting the end of the main 22655 17:02:59,040 --> 17:03:03,040 let's build the program and show you 22656 17:03:00,796 --> 17:03:05,596 that we're going to build with gcc like 22657 17:03:03,040 --> 17:03:07,595 we always do we're going to clear 22658 17:03:05,595 --> 17:03:09,756 and run rooster this is going to say 22659 17:03:07,595 --> 17:03:12,079 hello world and we're going to be done 22660 17:03:09,756 --> 17:03:15,520 here because we are calling this lambda 22661 17:03:12,080 --> 17:03:17,276 function once again notice that now we 22662 17:03:15,519 --> 17:03:19,915 don't have a handle to this lambda 22663 17:03:17,275 --> 17:03:22,479 function so we really have one chance to 22664 17:03:19,915 --> 17:03:25,040 call this and you will do this if you 22665 17:03:22,480 --> 17:03:27,436 just want to set up some function that 22666 17:03:25,040 --> 17:03:29,840 you want to call directly and you don't 22667 17:03:27,436 --> 17:03:32,080 care about calling it ever again in your 22668 17:03:29,839 --> 17:03:34,719 c plus plus application and we're going 22669 17:03:32,080 --> 17:03:37,200 to see that we have many kinds of 22670 17:03:34,720 --> 17:03:38,239 problems where we want to do these kinds 22671 17:03:38,239 --> 17:03:42,079 okay now that you have seen this the 22672 17:03:39,839 --> 17:03:44,159 next thing i want to show you is how to 22673 17:03:42,080 --> 17:03:46,560 set up a lambda function that takes 22674 17:03:44,160 --> 17:03:48,636 parameters and we're going to comment 22675 17:03:46,559 --> 17:03:51,915 out what we have on top here because we 22676 17:03:48,635 --> 17:03:53,595 don't want noise output on our terminal 22677 17:03:51,915 --> 17:03:56,159 and we're going to grab this lambda 22678 17:03:56,160 --> 17:04:00,720 so let's copy it we're going to go down 22679 17:03:58,160 --> 17:04:02,796 here and put it in and we want to pass 22680 17:04:00,720 --> 17:04:04,480 two parameters now let's pass in double 22681 17:04:02,796 --> 17:04:07,360 parameters we're going to pass in double 22682 17:04:04,480 --> 17:04:10,560 a and double d you can do that 22683 17:04:07,360 --> 17:04:13,595 and inside the lambda function we can 22684 17:04:10,559 --> 17:04:16,239 print the sum of these two variables so 22685 17:04:13,595 --> 17:04:18,239 we're going to say a plus b for example 22686 17:04:16,239 --> 17:04:21,519 and we're going to print out whatever is 22687 17:04:18,239 --> 17:04:22,879 the result of a plus b let's do that and 22688 17:04:21,519 --> 17:04:26,000 after we do that you're going to see 22689 17:04:22,879 --> 17:04:29,595 that we get a compiler error because now 22690 17:04:26,000 --> 17:04:32,239 our lambda function is taking parameters 22691 17:04:29,595 --> 17:04:34,079 but we are calling that with an empty 22692 17:04:34,080 --> 17:04:40,720 so we need to give it something to sum 22693 17:04:36,559 --> 17:04:43,360 up let's give it 10.0 for example and a 22694 17:04:40,720 --> 17:04:45,595 5.0 and see what we get and after this 22695 17:04:43,360 --> 17:04:48,635 we need to put in our semicolon because 22696 17:04:45,595 --> 17:04:51,435 this is a z plus plus statement and any 22697 17:04:48,635 --> 17:04:54,159 c plus plus statement needs to end with 22698 17:04:51,436 --> 17:04:56,796 a semicolon now try to think about what 22699 17:04:54,160 --> 17:04:59,040 we are doing here we are setting up a 22700 17:04:56,796 --> 17:05:00,720 lambda function okay it's going to end 22701 17:05:00,720 --> 17:05:05,436 curly brace and this lambda function is 22702 17:05:03,275 --> 17:05:06,319 going to take two parameters of double 22703 17:05:06,319 --> 17:05:10,319 and inside the lambda function we are 22704 17:05:08,319 --> 17:05:12,879 going to print out whatever is the sum 22705 17:05:10,319 --> 17:05:14,319 of these two parameters that we are 22706 17:05:14,319 --> 17:05:20,720 after that we are calling our lambda 22707 17:05:16,080 --> 17:05:24,000 function with two variables 10.0 and 5.0 22708 17:05:20,720 --> 17:05:25,680 or i should say two double literals 22709 17:05:24,000 --> 17:05:28,559 that's what we are calling our lambda 22710 17:05:25,680 --> 17:05:30,796 function with and now if the program 22711 17:05:28,559 --> 17:05:32,635 runs the lambda function is going to be 22712 17:05:30,796 --> 17:05:35,276 called once and we're going to print out 22713 17:05:32,635 --> 17:05:37,915 whatever is the sum of these two numbers 22714 17:05:35,275 --> 17:05:40,479 and that's going to be 15. let's build 22715 17:05:37,915 --> 17:05:42,795 so we're going to build with gcc 22716 17:05:40,480 --> 17:05:45,360 the world is good we can clear and run 22717 17:05:42,796 --> 17:05:47,520 rooster we're going to see a plus b is 22718 17:05:45,360 --> 17:05:50,080 15. we are calling a lambda function 22719 17:05:47,519 --> 17:05:52,079 here if we put in a 50 here for example 22720 17:05:50,080 --> 17:05:54,319 we're going to get 60 printed out 22721 17:05:52,080 --> 17:05:56,480 because that's going to be our sum okay 22722 17:05:54,319 --> 17:05:58,559 let's boil it again and see that print 22723 17:05:56,480 --> 17:06:01,680 it down if we run rooster we're going to 22724 17:05:58,559 --> 17:06:02,879 see 60 because that's the sum of 10 and 22725 17:06:02,879 --> 17:06:07,360 another thing we can do is actually 22726 17:06:05,360 --> 17:06:09,360 not call the lambda function directly 22727 17:06:07,360 --> 17:06:11,116 here we can actually comment this out i 22728 17:06:09,360 --> 17:06:13,756 don't want to take this out because you 22729 17:06:11,116 --> 17:06:15,756 might want to use this as a reference so 22730 17:06:13,756 --> 17:06:17,275 we're going to comment this out and set 22731 17:06:17,275 --> 17:06:21,915 that is going to name our lambda 22732 17:06:19,275 --> 17:06:23,680 function and then we will have a handle 22733 17:06:21,915 --> 17:06:25,360 to the lambda function and we can call 22734 17:06:25,360 --> 17:06:30,000 so let's take out the call we are doing 22735 17:06:28,239 --> 17:06:32,559 here and we're going to assign our 22736 17:06:30,000 --> 17:06:34,955 lambda function to a variable we're 22737 17:06:32,559 --> 17:06:38,079 going to say auto func one we can call 22738 17:06:34,955 --> 17:06:39,756 this func one no problem and now 22739 17:06:38,080 --> 17:06:42,480 we can call this lambda function 22740 17:06:39,756 --> 17:06:46,319 multiple times for example we can say 22741 17:06:42,480 --> 17:06:50,080 funk one and pass in 10 and 20 okay and 22742 17:06:46,319 --> 17:06:52,480 we can say funk one and pass in five 22743 17:06:52,480 --> 17:06:56,796 if we run this program it is going to 22744 17:06:54,400 --> 17:06:59,360 print the sum of 10 and 20 and it is 22745 17:06:56,796 --> 17:07:01,116 going to print the sum of five and seven 22746 17:06:59,360 --> 17:07:03,680 and it is going to be calling this 22747 17:07:01,116 --> 17:07:05,840 lambda function multiple times hopefully 22748 17:07:03,680 --> 17:07:08,559 you can see that if you want a chance to 22749 17:07:05,839 --> 17:07:10,795 call your lambda function multiple times 22750 17:07:08,559 --> 17:07:12,879 you will need to give it a handle like 22751 17:07:10,796 --> 17:07:14,160 we are doing here and then you can use 22752 17:07:14,160 --> 17:07:18,880 multiple times and call your lambda 22753 17:07:16,400 --> 17:07:21,360 function however many times you want so 22754 17:07:18,879 --> 17:07:23,595 let's build this program and see what we 22755 17:07:23,595 --> 17:07:28,955 i'm going to clear and run rooster 22756 17:07:26,319 --> 17:07:31,519 now we're going to see a plus b it's 30 22757 17:07:28,955 --> 17:07:34,159 as a result of this call here and we're 22758 17:07:34,160 --> 17:07:40,400 as a result of this call here notice 22759 17:07:36,955 --> 17:07:42,479 that we are passing an integer literals 22760 17:07:40,400 --> 17:07:45,520 so the compiler is going to insert an 22761 17:07:42,480 --> 17:07:47,520 implicit conversion from and to double 22762 17:07:45,519 --> 17:07:49,755 because our lambda function here is 22763 17:07:47,519 --> 17:07:52,719 taking double type you really need to be 22764 17:07:49,756 --> 17:07:54,559 aware of these implicit conversions and 22765 17:07:52,720 --> 17:07:56,796 make sure they are working to your 22766 17:07:54,559 --> 17:07:58,479 advantage if they are working against 22767 17:07:56,796 --> 17:08:01,840 you trying to find a way to make your 22768 17:07:58,480 --> 17:08:03,680 code work exactly like you want because 22769 17:08:01,839 --> 17:08:06,079 we are learning about all these cool 22770 17:08:03,680 --> 17:08:08,239 tricks in c plus plus you're going to be 22771 17:08:06,080 --> 17:08:10,080 using them at your advantage okay 22772 17:08:08,239 --> 17:08:11,915 hopefully now you know how you can set 22773 17:08:10,080 --> 17:08:14,080 up a lambda function that takes 22774 17:08:11,915 --> 17:08:16,635 parameters we're going to go down and 22775 17:08:14,080 --> 17:08:19,116 comment this out because now we want to 22776 17:08:16,635 --> 17:08:20,879 see how we can return something from a 22777 17:08:19,116 --> 17:08:22,796 lambda function and we're going to be 22778 17:08:20,879 --> 17:08:24,399 using the same lambda function here so 22779 17:08:22,796 --> 17:08:26,635 let's grab it and we're going to say 22780 17:08:24,400 --> 17:08:28,955 lambda function that returns something 22781 17:08:26,635 --> 17:08:31,360 and we're going to set up our lambda 22782 17:08:28,955 --> 17:08:32,559 function here let's put that in place 22783 17:08:32,559 --> 17:08:36,720 and instead of printing out the sum of a 22784 17:08:36,720 --> 17:08:41,680 we can return that we can go in our 22785 17:08:39,116 --> 17:08:44,560 lambda function and say return 22786 17:08:41,680 --> 17:08:47,116 a plus b okay now that we have this we 22787 17:08:44,559 --> 17:08:51,275 can either call it directly by saying 22788 17:08:47,116 --> 17:08:54,160 for example 10 and 20 but now if we try 22789 17:08:51,275 --> 17:08:56,795 and run this code it is not going to do 22790 17:08:56,796 --> 17:09:01,276 this lambda function is not printing to 22791 17:08:59,199 --> 17:09:04,000 the concept directly it is just 22792 17:09:01,275 --> 17:09:04,955 returning whatever is the sum of 10 and 22793 17:09:04,955 --> 17:09:09,275 and if we return it we're not doing 22794 17:09:07,199 --> 17:09:11,275 anything with whatever is returned from 22795 17:09:09,275 --> 17:09:12,879 this lambda function so we're not going 22796 17:09:11,275 --> 17:09:14,479 to print anything and we're just going 22797 17:09:14,480 --> 17:09:18,480 let's boil and make you see this because 22798 17:09:16,879 --> 17:09:19,680 you really need to be aware of these 22799 17:09:19,680 --> 17:09:24,160 so the build is good we can clear and 22800 17:09:21,839 --> 17:09:26,719 run rooster we're going to see down our 22801 17:09:24,160 --> 17:09:28,956 lambda function is returning something 22802 17:09:26,720 --> 17:09:30,400 but we're not doing anything with the 22803 17:09:30,400 --> 17:09:35,040 if we want to print the return value we 22804 17:09:32,635 --> 17:09:36,879 need to catch it somehow and we can put 22805 17:09:35,040 --> 17:09:39,840 that in a variable for example we can 22806 17:09:36,879 --> 17:09:42,635 say auto result and assign the return 22807 17:09:39,839 --> 17:09:44,955 value of our lambda function to this 22808 17:09:42,635 --> 17:09:47,839 variable now we have a chance to print 22809 17:09:44,955 --> 17:09:49,915 this for example by doing sddc out 22810 17:09:49,915 --> 17:09:54,879 print that out and save result now if we 22811 17:09:53,040 --> 17:09:57,360 build this program it is going to print 22812 17:09:54,879 --> 17:09:58,955 whatever is the sum of 10 and 20. let's 22813 17:09:57,360 --> 17:10:00,796 do that and show you that this is 22814 17:10:00,796 --> 17:10:08,720 so world is good so we can clear and run 22815 17:10:04,635 --> 17:10:11,519 rooster result is 30. if we pass in 10 22816 17:10:08,720 --> 17:10:14,239 and 50 for example we're going to get 22817 17:10:11,519 --> 17:10:16,399 the same thing let's pass in 60 here 22818 17:10:14,239 --> 17:10:18,635 so we're going to build with gcc the 22819 17:10:16,400 --> 17:10:21,596 build is good we can clear pretty quick 22820 17:10:18,635 --> 17:10:23,839 and run rooster now the sum is 70 and 22821 17:10:21,595 --> 17:10:26,319 this is how you can return stuff from 22822 17:10:23,839 --> 17:10:28,955 your lambda function and once you return 22823 17:10:26,319 --> 17:10:31,519 stuff you have to be sure you cut your 22824 17:10:28,955 --> 17:10:33,756 results and print them out another thing 22825 17:10:31,519 --> 17:10:36,000 you can do is print out the result 22826 17:10:33,756 --> 17:10:38,635 directly after you call your lambda 22827 17:10:36,000 --> 17:10:41,275 function so for example we can grab this 22828 17:10:38,635 --> 17:10:44,080 entire lambda function thing here and 22829 17:10:41,275 --> 17:10:46,319 put that in an sddc out statement so 22830 17:10:44,080 --> 17:10:48,880 we're going to put in a second one here 22831 17:10:46,319 --> 17:10:51,519 and say result or we can even comment 22832 17:10:48,879 --> 17:10:53,360 this out we don't want to be confused 22833 17:10:51,519 --> 17:10:56,239 when we look at the terminology we're 22834 17:10:53,360 --> 17:10:59,319 going to comment out the first sddc out 22835 17:10:56,239 --> 17:11:02,795 statement here and we're going to say 22836 17:10:59,319 --> 17:11:05,116 stdendl here and put in a placeholder 22837 17:11:02,796 --> 17:11:06,560 for our lambda function and we're going 22838 17:11:06,559 --> 17:11:11,040 if we do something like this again this 22839 17:11:08,635 --> 17:11:13,116 entire lambda function thing is going to 22840 17:11:11,040 --> 17:11:15,840 return the sum and that's what we're 22841 17:11:13,116 --> 17:11:18,560 going to print i realize this syntax is 22842 17:11:15,839 --> 17:11:21,275 really cryptic but it is going to work 22843 17:11:18,559 --> 17:11:23,680 let's try and build our program here the 22844 17:11:21,275 --> 17:11:25,680 world is good so we can clear and run 22845 17:11:23,680 --> 17:11:29,040 rooster we're going to see that result 22846 17:11:25,680 --> 17:11:32,955 is 70 and this is coming from our call 22847 17:11:29,040 --> 17:11:35,520 here which is nested inside our htdc out 22848 17:11:32,955 --> 17:11:37,519 statement you can do this if you want 22849 17:11:35,519 --> 17:11:40,159 another thing you can do as we have seen 22850 17:11:37,519 --> 17:11:42,795 before is give your lambda function a 22851 17:11:40,160 --> 17:11:44,796 name and call it repeatedly so for 22852 17:11:42,796 --> 17:11:47,040 example we can try and comment out what 22853 17:11:44,796 --> 17:11:48,796 we have here and set up another lambda 22854 17:11:48,796 --> 17:11:53,756 don't want to confuse you again so we're 22855 17:11:50,879 --> 17:11:55,360 going to grab our lambda function here 22856 17:11:53,756 --> 17:11:57,840 we're going to copy it because we don't 22857 17:11:55,360 --> 17:11:59,840 want to type this a thousand times 22858 17:11:57,839 --> 17:12:01,915 and we don't want to call this lambda 22859 17:11:59,839 --> 17:12:03,595 function directly instead we want to 22860 17:12:01,915 --> 17:12:07,199 give it a name and we're going to say 22861 17:12:03,595 --> 17:12:10,399 aro funk 2 and we're going to assign our 22862 17:12:07,199 --> 17:12:12,879 lambda function type to func one 22863 17:12:10,400 --> 17:12:14,955 now that we have this we can say auto 22864 17:12:14,955 --> 17:12:19,756 and we can say func and we can pass in 22865 17:12:17,275 --> 17:12:20,720 whatever we want let's put in 23 and a 22866 17:12:20,720 --> 17:12:25,275 and this is going to add up 23 and seven 22867 17:12:23,680 --> 17:12:27,756 we're going to grab the result and we're 22868 17:12:25,275 --> 17:12:29,915 going to assign that to result one 22869 17:12:27,756 --> 17:12:33,275 if we go down and say auto for example 22870 17:12:29,915 --> 17:12:34,635 result two and say for example funk one 22871 17:12:37,595 --> 17:12:41,915 why not and go down and print out these 22872 17:12:40,080 --> 17:12:44,319 result variables we're going to see the 22873 17:12:41,915 --> 17:12:47,115 sums printed out and again the message 22874 17:12:44,319 --> 17:12:49,199 is that we can use this handle to call 22875 17:12:47,116 --> 17:12:51,040 our lambda function multiple times so 22876 17:12:49,199 --> 17:12:52,239 let's go down and print this out we're 22877 17:12:52,239 --> 17:12:56,635 result one and we're going to say result 22878 17:12:54,635 --> 17:12:58,955 one here we're going to go down and say 22879 17:12:56,635 --> 17:13:00,796 result two and we're going to print that 22880 17:12:58,955 --> 17:13:03,680 out and we can even call this lambda 22881 17:13:00,796 --> 17:13:05,840 function directly and put that in sddc 22882 17:13:03,680 --> 17:13:08,635 out here so let's say that we're going 22883 17:13:05,839 --> 17:13:11,199 to say result to 3 for example or direct 22884 17:13:08,635 --> 17:13:13,360 call i think this is descriptive enough 22885 17:13:13,360 --> 17:13:18,080 one the name of our lambda function or 22886 17:13:15,680 --> 17:13:20,955 the handle to the lambda function and we 22887 17:13:18,080 --> 17:13:22,560 can put in a five and a two for example 22888 17:13:20,955 --> 17:13:25,519 and we're going to see this print out 22889 17:13:22,559 --> 17:13:29,040 the result if we build our program let's 22890 17:13:25,519 --> 17:13:32,000 do that with gcc the bolt is going to be 22891 17:13:29,040 --> 17:13:35,436 good we can clear and run rooster we're 22892 17:13:32,000 --> 17:13:38,559 going to have result 1 is 30 which is 23 22893 17:13:35,436 --> 17:13:42,080 and 7 so the sum is 30. the second one 22894 17:13:38,559 --> 17:13:44,239 should say 54 because that's the sum and 22895 17:13:42,080 --> 17:13:47,436 the third one should say 7 because 22896 17:13:44,239 --> 17:13:49,275 that's the sum of 5 and 2 and it is what 22897 17:13:47,436 --> 17:13:51,680 we have here and after that we're going 22898 17:13:49,275 --> 17:13:54,159 to see that our program is done this is 22899 17:13:51,680 --> 17:13:56,635 how you can set up a lambda function 22900 17:13:54,160 --> 17:13:58,796 that returns something and we have seen 22901 17:13:56,635 --> 17:14:01,040 many ways we can work with us let's 22902 17:13:58,796 --> 17:14:03,596 comment this out because there is one 22903 17:14:01,040 --> 17:14:06,239 more thing i want you to see and that's 22904 17:14:03,595 --> 17:14:08,955 along the function whose return type is 22905 17:14:06,239 --> 17:14:11,040 specified explicitly okay we are going 22906 17:14:08,955 --> 17:14:12,795 to reuse the same lambda function we 22907 17:14:11,040 --> 17:14:14,319 have been using all along and we're 22908 17:14:14,319 --> 17:14:19,839 and we're going to go down and put it 22909 17:14:16,796 --> 17:14:22,319 down here and now we want to specify the 22910 17:14:19,839 --> 17:14:25,039 return type as a double the way we do 22911 17:14:22,319 --> 17:14:27,680 that we go after our parameter list and 22912 17:14:25,040 --> 17:14:29,595 we put in a dash symbol and we put in a 22913 17:14:27,680 --> 17:14:32,319 greater than symbol and then we're going 22914 17:14:29,595 --> 17:14:34,879 to specify the return type of our lambda 22915 17:14:32,319 --> 17:14:37,595 function let's say that we want our 22916 17:14:34,879 --> 17:14:39,915 return type to be ant this is how we say 22917 17:14:37,595 --> 17:14:42,955 it now our function here is going to 22918 17:14:39,915 --> 17:14:45,435 return end regardless of the types that 22919 17:14:42,955 --> 17:14:47,199 we put in so what the compiler is going 22920 17:14:45,436 --> 17:14:48,955 to do when we call this lambda function 22921 17:14:47,199 --> 17:14:50,479 it is going to take the parameters it is 22922 17:14:48,955 --> 17:14:52,879 going to add them up for example now 22923 17:14:50,480 --> 17:14:54,956 they are double so it's going to add a 22924 17:14:52,879 --> 17:14:57,040 and b as double it's going to produce a 22925 17:14:54,955 --> 17:15:00,319 double variable then it is going to 22926 17:14:57,040 --> 17:15:03,275 convert that to end and return it you 22927 17:15:00,319 --> 17:15:06,319 notice that now we have forced our 22928 17:15:03,275 --> 17:15:07,519 lambda function to really return a 22929 17:15:08,720 --> 17:15:14,160 before the return type was deduced from 22930 17:15:11,519 --> 17:15:16,719 the operation that we did here and 22931 17:15:14,160 --> 17:15:18,796 before we were returning double because 22932 17:15:16,720 --> 17:15:21,116 that's what the compiler was deducing 22933 17:15:18,796 --> 17:15:24,000 for a and b here to really drive the 22934 17:15:21,116 --> 17:15:27,756 point home let's assign this lambda 22935 17:15:24,000 --> 17:15:30,239 function to func one or func three let's 22936 17:15:27,756 --> 17:15:32,000 do that we're going to say func three 22937 17:15:30,239 --> 17:15:35,519 and this is a lambda function that is 22938 17:15:32,000 --> 17:15:36,955 going to explicitly return and and we're 22939 17:15:35,519 --> 17:15:38,955 going to set up another one which is 22940 17:15:36,955 --> 17:15:40,720 going to deduce the return type we're 22941 17:15:40,720 --> 17:15:45,595 the return type explicitly so we're 22942 17:15:42,955 --> 17:15:46,879 going to take out our return type here 22943 17:15:46,879 --> 17:15:53,115 and we're going to go down and say auto 22944 17:15:49,199 --> 17:15:56,239 result 3 and we're going to say funk 3 22945 17:15:53,116 --> 17:15:58,480 and add up two double variables let's 22946 17:15:56,239 --> 17:16:00,479 set up these variables explicitly so 22947 17:15:58,480 --> 17:16:03,116 that you really see what is happening so 22948 17:16:00,480 --> 17:16:05,276 we're going to say it a and put in 22949 17:16:05,275 --> 17:16:10,159 why not and let's make this guy a double 22950 17:16:08,080 --> 17:16:12,240 because that's what our function here 22951 17:16:10,160 --> 17:16:14,956 expects and we're going to do another 22952 17:16:12,239 --> 17:16:17,839 one do double b and we're going to put 22953 17:16:17,839 --> 17:16:22,479 for example and we are going to use this 22954 17:16:20,559 --> 17:16:24,559 variables as arguments here we're going 22955 17:16:22,480 --> 17:16:26,480 to say a and b and we're going to say 22956 17:16:27,275 --> 17:16:32,879 this should be funk 4 sorry for this 22957 17:16:30,635 --> 17:16:36,080 and i'm going to say result 4 and this 22958 17:16:32,879 --> 17:16:38,239 should say result i am making many typos 22959 17:16:36,080 --> 17:16:41,040 so we're going to assign our lambda 22960 17:16:38,239 --> 17:16:44,479 function the result of our lambda 22961 17:16:41,040 --> 17:16:46,160 function i should say to result for here 22962 17:16:47,595 --> 17:16:53,839 now we want to see the types of result 3 22963 17:16:50,955 --> 17:16:55,519 and result 4 here we're going to go down 22964 17:16:53,839 --> 17:16:59,199 and print them out we're going to say 22965 17:16:55,519 --> 17:17:00,399 sddc out size of this is one way we have 22966 17:17:03,040 --> 17:17:07,595 we're going to pass in result three 22967 17:17:05,040 --> 17:17:09,915 we're going to say size of result three 22968 17:17:07,595 --> 17:17:13,199 and we're going to go down and say size 22969 17:17:09,915 --> 17:17:15,435 of result for okay we have our code in 22970 17:17:13,199 --> 17:17:18,795 place and what we want to see 22971 17:17:15,436 --> 17:17:22,720 is the type that is deduced by the func 22972 17:17:18,796 --> 17:17:25,040 3 lambda function here we are explicitly 22973 17:17:22,720 --> 17:17:26,955 turning the return type into end the 22974 17:17:25,040 --> 17:17:29,116 parameters we pass in are going to be 22975 17:17:26,955 --> 17:17:31,436 double that's what we have here we are 22976 17:17:29,116 --> 17:17:33,596 going to add up a and b and the result 22977 17:17:31,436 --> 17:17:35,596 of this is also going to be double but 22978 17:17:33,595 --> 17:17:39,115 the compiler is going to notice that we 22979 17:17:35,595 --> 17:17:41,756 are returning and explicitly here and it 22980 17:17:39,116 --> 17:17:44,635 is going to take this double result and 22981 17:17:41,756 --> 17:17:45,436 transform that to end and we're going to 22982 17:17:45,436 --> 17:17:49,756 and we're going to get the result of 22983 17:17:47,040 --> 17:17:52,160 that assigned to func 3 here for the 22984 17:17:49,756 --> 17:17:54,635 second lambda function here we are going 22985 17:17:52,160 --> 17:17:57,276 to automatically deduce the return type 22986 17:17:54,635 --> 17:17:59,680 so what we return here is going to be 22987 17:17:57,275 --> 17:18:02,479 a double type and we should see a size 22988 17:17:59,680 --> 17:18:06,000 of it for research 3 here printed out so 22989 17:18:02,480 --> 17:18:08,400 we should see a 4 on my system and for 22990 17:18:06,000 --> 17:18:10,796 the second statement here we should see 22991 17:18:08,400 --> 17:18:13,436 a size of double printed out this is one 22992 17:18:10,796 --> 17:18:15,520 way we have to try and prove this so 22993 17:18:13,436 --> 17:18:17,756 let's build this program and see what we 22994 17:18:15,519 --> 17:18:20,079 get okay the world is good we can clear 22995 17:18:17,756 --> 17:18:23,200 and run rooster and we're going to see 22996 17:18:20,080 --> 17:18:25,840 that this is exactly what we expect here 22997 17:18:23,199 --> 17:18:28,479 if we want we can even print result 3 22998 17:18:25,839 --> 17:18:31,359 and result 4 and see what we get so 22999 17:18:28,480 --> 17:18:32,956 let's do that we are learning so let's 23000 17:18:31,360 --> 17:18:36,000 experiment with this a little bit we're 23001 17:18:32,955 --> 17:18:39,519 going to say result 3 and we should go 23002 17:18:36,000 --> 17:18:42,000 back and say result three if i can type 23003 17:18:39,519 --> 17:18:45,680 e and dl here and we're going to go down 23004 17:18:42,000 --> 17:18:47,199 and say sddc out result for okay if we 23005 17:18:47,199 --> 17:18:52,079 we're going to see the results printed 23006 17:18:49,519 --> 17:18:53,519 down the both is good we can clear and 23007 17:18:53,519 --> 17:18:59,595 now we have result is 10 for the 23008 17:18:59,595 --> 17:19:05,915 first lambda function and for the second 23009 17:19:02,239 --> 17:19:08,955 one result is 10.4 you can see that for 23010 17:19:05,915 --> 17:19:11,275 the first one the result was transformed 23011 17:19:08,955 --> 17:19:13,436 from double to and and for the second 23012 17:19:11,275 --> 17:19:16,159 one we deduced double so we still have 23013 17:19:13,436 --> 17:19:18,160 our decimal point here hopefully this 23014 17:19:16,160 --> 17:19:20,880 makes sense and this is really all we 23015 17:19:18,160 --> 17:19:22,720 set out to do in this lecture showing 23016 17:19:20,879 --> 17:19:25,040 you different ways you can set up your 23017 17:19:22,720 --> 17:19:27,275 lambda functions we saw the syntax of 23018 17:19:25,040 --> 17:19:29,275 setting up a lambda function we saw that 23019 17:19:27,275 --> 17:19:32,000 you can declare a lambda function and 23020 17:19:29,275 --> 17:19:34,159 call it through a name like func here we 23021 17:19:32,000 --> 17:19:36,160 went down and so that you can declare a 23022 17:19:34,160 --> 17:19:38,160 lambda function and call it directly 23023 17:19:36,160 --> 17:19:40,480 through the syntax you see here we saw 23024 17:19:38,160 --> 17:19:42,956 that how we could specify parameters to 23025 17:19:40,480 --> 17:19:44,400 our lambda function this is the syntax 23026 17:19:42,955 --> 17:19:46,239 and once you do this you have to 23027 17:19:44,400 --> 17:19:48,955 remember to call your lambda functions 23028 17:19:46,239 --> 17:19:52,159 with parameters like we see here we can 23029 17:19:48,955 --> 17:19:54,720 even use a handle given to our lambda 23030 17:19:52,160 --> 17:19:57,200 function and call our lambda function 23031 17:19:54,720 --> 17:19:59,040 multiple times after that we saw that we 23032 17:19:57,199 --> 17:20:01,275 could return something from a lambda 23033 17:19:59,040 --> 17:20:02,239 function and we had many chances to play 23034 17:20:03,436 --> 17:20:09,116 finished by looking how we could specify 23035 17:20:06,080 --> 17:20:12,160 the return type explicitly and the 23036 17:20:09,116 --> 17:20:13,756 syntax to do that is what you see here 23037 17:20:12,160 --> 17:20:15,596 we are going to stop here in this 23038 17:20:13,756 --> 17:20:18,720 lecture the next one we're going to see 23039 17:20:15,595 --> 17:20:21,756 how we can use our capture lists here 23040 17:20:18,720 --> 17:20:23,595 and make them do useful things 23041 17:20:25,436 --> 17:20:30,080 in this lecture we're going to see how 23042 17:20:26,955 --> 17:20:32,879 we can use capture lists in our lambda 23043 17:20:30,080 --> 17:20:35,680 functions and do some useful things we 23044 17:20:32,879 --> 17:20:38,239 have seen that the syntax of our lambda 23045 17:20:35,680 --> 17:20:40,720 functions includes these angle brackets 23046 17:20:38,239 --> 17:20:43,275 here but we haven't really used them to 23047 17:20:40,720 --> 17:20:45,840 do anything for what we have done in the 23048 17:20:43,275 --> 17:20:48,159 last lecture they were empty all the 23049 17:20:45,839 --> 17:20:50,399 time in this lecture we're going to use 23050 17:20:48,160 --> 17:20:52,636 them here is a simple example of how we 23051 17:20:52,635 --> 17:20:57,116 we have two variables a and b they are 23052 17:20:55,116 --> 17:21:00,240 of double type and they contain these 23053 17:20:57,116 --> 17:21:02,796 values 10 and 20. but sometimes we want 23054 17:21:00,239 --> 17:21:04,319 to use thanks outside the scope of the 23055 17:21:04,319 --> 17:21:08,635 inside the lambda function here and if 23056 17:21:06,635 --> 17:21:10,635 you set up your lambda function with an 23057 17:21:08,635 --> 17:21:12,635 empty capture list suppose you don't 23058 17:21:12,635 --> 17:21:16,635 and you try to use a and b like this 23059 17:21:14,796 --> 17:21:17,840 you're going to get a compiler error 23060 17:21:17,839 --> 17:21:24,795 the body of the lambda function doesn't 23061 17:21:20,955 --> 17:21:27,275 have access to the outer context here it 23062 17:21:24,796 --> 17:21:29,840 can only use things inside the lambda 23063 17:21:27,275 --> 17:21:32,159 functions or events that you pass to the 23064 17:21:29,839 --> 17:21:34,955 lambda function here but it can't 23065 17:21:32,160 --> 17:21:37,596 directly access things outside the 23066 17:21:34,955 --> 17:21:40,159 lambda functions and we can have access 23067 17:21:37,595 --> 17:21:42,635 to thanks that are declared and defined 23068 17:21:40,160 --> 17:21:44,880 outside the lambda function through our 23069 17:21:42,635 --> 17:21:47,756 capture list if you look here we are 23070 17:21:44,879 --> 17:21:49,519 saying a and b and this is the syntax we 23071 17:21:49,519 --> 17:21:55,435 these two variables a and b and have 23072 17:21:52,796 --> 17:21:57,596 access to them inside our lambda 23073 17:21:55,436 --> 17:22:00,160 function here and this is what capture 23074 17:21:57,595 --> 17:22:03,435 lists are really for giving you access 23075 17:22:00,160 --> 17:22:05,436 to things that are declared outside the 23076 17:22:03,436 --> 17:22:08,000 scope of the lambda function because if 23077 17:22:05,436 --> 17:22:11,040 you really think about it this lambda 23078 17:22:08,000 --> 17:22:12,239 function thing we have here is really a 23079 17:22:14,400 --> 17:22:19,360 that has these behaviors well then so 23080 17:22:16,955 --> 17:22:21,839 that we can call it and do things but it 23081 17:22:19,360 --> 17:22:23,116 doesn't really have direct access to the 23082 17:22:23,116 --> 17:22:27,436 where it was declared and you have to 23083 17:22:25,275 --> 17:22:29,519 give it access through the capture 23084 17:22:27,436 --> 17:22:31,116 mechanism here okay now that you know 23085 17:22:29,519 --> 17:22:33,595 this we're going to see different ways 23086 17:22:31,116 --> 17:22:36,160 we can use the capture mechanism one 23087 17:22:33,595 --> 17:22:38,079 thing we can do is capture by value and 23088 17:22:36,160 --> 17:22:40,720 if we capture by value what we're going 23089 17:22:38,080 --> 17:22:42,635 to have inside the lambda function is 23090 17:22:40,720 --> 17:22:44,955 going to be a copy suppose we have a 23091 17:22:42,635 --> 17:22:47,519 variable here which is c the value is 23092 17:22:44,955 --> 17:22:49,275 going to be 42 as you see here we're 23093 17:22:47,519 --> 17:22:51,755 going to set up a lambda function and 23094 17:22:51,756 --> 17:22:56,480 by value in this lambda function here 23095 17:22:54,480 --> 17:22:58,160 and what we're going to do outside the 23096 17:22:56,480 --> 17:23:00,400 lambda function notice the lambda 23097 17:22:58,160 --> 17:23:02,400 function has a handle so we can call it 23098 17:23:00,400 --> 17:23:04,480 we're going to loop five times 23099 17:23:02,400 --> 17:23:07,116 and at each iteration we're going to 23100 17:23:04,480 --> 17:23:08,480 call our lambda function and print the 23101 17:23:08,480 --> 17:23:12,636 but after that we're going to increment 23102 17:23:10,400 --> 17:23:14,239 our c variable and this is going to 23103 17:23:14,239 --> 17:23:19,040 outer variable here and what we want to 23104 17:23:16,796 --> 17:23:21,756 see is that the changes we are doing on 23105 17:23:19,040 --> 17:23:23,840 the outside here are picked up by the 23106 17:23:21,756 --> 17:23:26,400 inner value that we have in our lambda 23107 17:23:23,839 --> 17:23:28,635 function here and ahead to break it to 23108 17:23:26,400 --> 17:23:30,720 you this is going to not be the case 23109 17:23:28,635 --> 17:23:33,436 because what we have inside the lambda 23110 17:23:30,720 --> 17:23:35,200 function is a copy of the outside 23111 17:23:33,436 --> 17:23:38,000 variable here so what we're going to 23112 17:23:35,199 --> 17:23:40,399 have is for the outer value to go from 23113 17:23:38,000 --> 17:23:42,955 42 all the way to 47 we're going to go 23114 17:23:40,400 --> 17:23:45,360 five times incrementing this little guy 23115 17:23:42,955 --> 17:23:47,595 but the thing on the inside is not going 23116 17:23:45,360 --> 17:23:50,160 to change because it is a copy the 23117 17:23:47,595 --> 17:23:52,795 incrementation here is affecting the 23118 17:23:50,160 --> 17:23:55,116 outside variable here but it is not 23119 17:23:52,796 --> 17:23:57,680 affecting the copy we have inside our 23120 17:23:55,116 --> 17:24:00,240 lambda function so for all these loops 23121 17:23:57,680 --> 17:24:02,000 here we're going to see 42 print it down 23122 17:24:00,239 --> 17:24:04,159 and we're going to see this in a minute 23123 17:24:02,000 --> 17:24:06,080 when we hit visual studio code let's say 23124 17:24:06,080 --> 17:24:10,635 excuse my typing here okay so now you 23125 17:24:08,480 --> 17:24:12,796 know that we can capture by a value and 23126 17:24:10,635 --> 17:24:15,595 you know what this really means another 23127 17:24:12,796 --> 17:24:18,160 thing we can do is capture by reference 23128 17:24:15,595 --> 17:24:20,955 and the syntax to do that is to add an 23129 17:24:18,160 --> 17:24:22,956 ampersand symbol in front of the 23130 17:24:20,955 --> 17:24:25,519 variable that we capture here and if we 23131 17:24:22,955 --> 17:24:27,915 do this and run the semicolon we run in 23132 17:24:25,519 --> 17:24:30,635 the last lecture we're going to see now 23133 17:24:27,915 --> 17:24:32,719 that the changes we do on the outside 23134 17:24:30,635 --> 17:24:35,595 are going to be picked up by the inner 23135 17:24:32,720 --> 17:24:38,319 side of our lambda function because we 23136 17:24:35,595 --> 17:24:41,115 are capturing by reference what we have 23137 17:24:38,319 --> 17:24:43,756 inside our lambda function here is a 23138 17:24:41,116 --> 17:24:45,360 true reference to the outside the 23139 17:24:43,756 --> 17:24:48,319 variable here and we're going to see 23140 17:24:45,360 --> 17:24:50,400 that now the outside and the inside are 23141 17:24:48,319 --> 17:24:53,519 going to be incrementing together and 23142 17:24:50,400 --> 17:24:55,840 we're going to have 43 42 44 and things 23143 17:24:53,519 --> 17:24:57,755 like that printed out on the console 23144 17:24:55,839 --> 17:24:59,915 okay this is really all i have to share 23145 17:24:57,756 --> 17:25:02,480 with you in this lecture showing you how 23146 17:24:59,915 --> 17:25:04,795 you can use your capture lists and that 23147 17:25:02,480 --> 17:25:06,880 you can capture either by value or by 23148 17:25:04,796 --> 17:25:08,635 reference we're going to head over to 23149 17:25:06,879 --> 17:25:11,199 visual studio code and play with this a 23150 17:25:08,635 --> 17:25:13,436 little more okay here we are in our 23151 17:25:11,199 --> 17:25:16,159 working folder the current project is 23152 17:25:13,436 --> 17:25:18,080 capture lists we're going to grab our 23153 17:25:16,160 --> 17:25:19,756 template files pretty quick and we're 23154 17:25:18,080 --> 17:25:20,635 going to put them in place we're going 23155 17:25:20,635 --> 17:25:25,199 put them in the current project and 23156 17:25:23,116 --> 17:25:27,596 we're going to open this guy in visual 23157 17:25:25,199 --> 17:25:29,839 studio code by dragging and dropping in 23158 17:25:27,595 --> 17:25:33,360 place here and we're going to open our 23159 17:25:29,839 --> 17:25:35,519 main cpp file clean it up a little bit 23160 17:25:33,360 --> 17:25:38,160 and close the left pane here what we 23161 17:25:35,519 --> 17:25:40,239 want to do in here is uh learn a little 23162 17:25:38,160 --> 17:25:42,880 more about capture lists and we're going 23163 17:25:40,239 --> 17:25:46,400 to put in a piece of code to play with 23164 17:25:42,879 --> 17:25:49,435 just that we have two variables a and 10 23165 17:25:46,400 --> 17:25:51,436 here and we have our lambda function 23166 17:25:49,436 --> 17:25:53,436 setup and let's take out our capture 23167 17:25:51,436 --> 17:25:54,720 list so that you can see the compiler 23168 17:25:54,720 --> 17:25:59,595 if we try to use them as is here so we 23169 17:25:57,680 --> 17:26:02,635 have our two variables and we have a 23170 17:25:59,595 --> 17:26:05,199 lambda function set up here and inside 23171 17:26:02,635 --> 17:26:08,080 the lambda function notice that we are 23172 17:26:05,199 --> 17:26:10,239 trying to access the variables that have 23173 17:26:08,080 --> 17:26:12,635 been declared on the outside of the 23174 17:26:10,239 --> 17:26:14,239 lambda function here and by default if 23175 17:26:12,635 --> 17:26:16,239 we try to do this we're going to get a 23176 17:26:14,239 --> 17:26:19,199 compiler error because these two 23177 17:26:16,239 --> 17:26:21,199 variables don't exist in the scope of 23178 17:26:19,199 --> 17:26:23,839 the lambda function here let's bring up 23179 17:26:21,199 --> 17:26:25,755 a terminal and see what visual studio 23180 17:26:25,756 --> 17:26:30,160 and it is going to say an enclosing 23181 17:26:27,680 --> 17:26:31,915 function local variable cannot be 23182 17:26:31,915 --> 17:26:36,795 lambda body unless it is in the capture 23183 17:26:35,040 --> 17:26:39,360 list and you see that this is really 23184 17:26:36,796 --> 17:26:41,200 descriptive okay we have sent this error 23185 17:26:39,360 --> 17:26:43,520 but we want to see what the compiler 23186 17:26:41,199 --> 17:26:46,719 says about this we're going to pass this 23187 17:26:43,519 --> 17:26:49,359 through gcc and we should get a similar 23188 17:26:46,720 --> 17:26:52,000 error it is going to say a is not 23189 17:26:49,360 --> 17:26:55,116 captured and it is going to go down here 23190 17:26:52,000 --> 17:26:57,360 and say b is not captured here and this 23191 17:26:55,116 --> 17:27:00,560 is the error we are trying to use 23192 17:26:57,360 --> 17:27:02,720 outside variables in our lambda function 23193 17:27:00,559 --> 17:27:03,839 but to really do that we need to capture 23194 17:27:03,839 --> 17:27:09,359 and the syntax to capture our variables 23195 17:27:06,160 --> 17:27:12,480 is to put them inside our angle brackets 23196 17:27:09,360 --> 17:27:14,635 here with a comma separate to the list 23197 17:27:12,480 --> 17:27:16,956 of the variables that we want to capture 23198 17:27:14,635 --> 17:27:19,199 so we're going to say a comma b because 23199 17:27:16,955 --> 17:27:20,955 that's what we want to capture here and 23200 17:27:20,955 --> 17:27:24,720 visual studio code should be happy it is 23201 17:27:22,879 --> 17:27:27,915 not happy i think we need to compile 23202 17:27:24,720 --> 17:27:29,275 this for it to be happy so let's do that 23203 17:27:29,275 --> 17:27:35,115 our variables are being captured and we 23204 17:27:32,239 --> 17:27:38,400 are summing them up and we are printing 23205 17:27:35,116 --> 17:27:41,200 the result of adding them up here by 23206 17:27:38,400 --> 17:27:43,200 calling the function through a handle we 23207 17:27:41,199 --> 17:27:45,435 have set up here hopefully this is 23208 17:27:43,199 --> 17:27:46,879 really making sense let's build the 23209 17:27:49,275 --> 17:27:53,519 you're going to see that the world is 23210 17:27:50,720 --> 17:27:57,916 good we can clear and run rooster we 23211 17:27:53,519 --> 17:28:01,039 should see a plus b is 30 but the values 23212 17:27:57,915 --> 17:28:03,199 are getting inside the lambda function 23213 17:28:01,040 --> 17:28:06,080 through our capture list they are not 23214 17:28:03,199 --> 17:28:08,079 going through the parameter list here 23215 17:28:06,080 --> 17:28:09,276 this is really something you need to be 23216 17:28:09,275 --> 17:28:15,199 now that you know how capture lists work 23217 17:28:12,635 --> 17:28:17,436 let's see that you can capture either by 23218 17:28:15,199 --> 17:28:20,000 value or by reference and we're going to 23219 17:28:17,436 --> 17:28:22,160 see what that means exactly i am going 23220 17:28:20,000 --> 17:28:24,879 to go down here and put in a piece of 23221 17:28:22,160 --> 17:28:27,276 code to play with capturing by value 23222 17:28:24,879 --> 17:28:30,079 okay let's do that we have a variable 23223 17:28:30,080 --> 17:28:34,080 and we set up a lambda function which is 23224 17:28:34,080 --> 17:28:38,480 with this syntax here and this is the 23225 17:28:38,480 --> 17:28:43,200 like we did on top here so we are going 23226 17:28:40,879 --> 17:28:44,719 to be capturing like this and if we do 23227 17:28:43,199 --> 17:28:46,719 this we're going to be capturing by 23228 17:28:44,720 --> 17:28:49,275 value the meaning of that is that what 23229 17:28:46,720 --> 17:28:51,595 we have inside our lambda function is 23230 17:28:49,275 --> 17:28:54,079 not going to be the original value here 23231 17:28:51,595 --> 17:28:55,115 it is going to be a copy of what we have 23232 17:28:55,116 --> 17:29:00,319 what we can do here is use this thing in 23233 17:28:57,839 --> 17:29:02,795 a loop and really prove this 23234 17:29:00,319 --> 17:29:05,199 so we're going to loop five times from 23235 17:29:02,796 --> 17:29:08,000 zero all the way to four because this is 23236 17:29:05,199 --> 17:29:09,275 going to go until i is no longer less 23237 17:29:09,275 --> 17:29:13,115 and inside we're going to print the 23238 17:29:11,040 --> 17:29:15,040 outer value which is going to be 23239 17:29:13,116 --> 17:29:17,116 the c here after that we're going to 23240 17:29:15,040 --> 17:29:18,955 call our lambda function which is going 23241 17:29:17,116 --> 17:29:21,200 to predict the inner value in the lambda 23242 17:29:18,955 --> 17:29:23,915 function and we're going to see that the 23243 17:29:21,199 --> 17:29:26,000 incrementation we do on the outside 23244 17:29:23,915 --> 17:29:28,479 which is going to affect the outer value 23245 17:29:26,000 --> 17:29:30,720 here is going to be visible inside the 23246 17:29:28,480 --> 17:29:32,480 lambda function here and that's not 23247 17:29:30,720 --> 17:29:34,955 going to be the case the change we do 23248 17:29:32,480 --> 17:29:36,956 here is not going to be picked up by the 23249 17:29:34,955 --> 17:29:39,275 lambda function here because what we 23250 17:29:36,955 --> 17:29:41,595 have inside is a copy so what we're 23251 17:29:39,275 --> 17:29:46,000 going to see is for the outer value to 23252 17:29:41,595 --> 17:29:48,319 go from 42 43 43 45 and it is going to 23253 17:29:46,000 --> 17:29:51,116 go until we hit the end of this loop 23254 17:29:48,319 --> 17:29:53,519 here but the inside value is not going 23255 17:29:51,116 --> 17:29:56,400 to change because the change we do in 23256 17:29:53,519 --> 17:29:59,275 here is not going to affect the copy 23257 17:29:56,400 --> 17:30:01,436 that we have in our lambda function here 23258 17:29:59,275 --> 17:30:03,519 hopefully this makes sense and again if 23259 17:30:01,436 --> 17:30:04,400 you have a problem please make sure you 23260 17:30:05,756 --> 17:30:10,080 course platform and i will do the best i 23261 17:30:07,915 --> 17:30:12,399 can to help you out for now we're going 23262 17:30:10,080 --> 17:30:15,520 to build and run this program you see 23263 17:30:12,400 --> 17:30:17,756 the world is good so let's clear and run 23264 17:30:15,519 --> 17:30:21,595 rooster you're going to see outer value 23265 17:30:17,756 --> 17:30:25,520 is 43 outer value is 43 outer value is 23266 17:30:21,595 --> 17:30:28,879 44 outer value is 45 the outer value is 23267 17:30:25,519 --> 17:30:30,559 changing but the inner value is staying 23268 17:30:32,000 --> 17:30:36,879 hopefully this proves that the outer 23269 17:30:34,239 --> 17:30:39,199 value and the inner value are two 23270 17:30:36,879 --> 17:30:41,680 different things but we can even go 23271 17:30:39,199 --> 17:30:43,680 further and print the addresses of the 23272 17:30:41,680 --> 17:30:44,400 outer value and the inner value let's do 23273 17:30:44,400 --> 17:30:50,635 we're going to add that to our statement 23274 17:30:47,040 --> 17:30:53,040 here and say the address of this guy 23275 17:30:50,635 --> 17:30:55,915 so we're going to say address of outer 23276 17:30:53,040 --> 17:30:57,680 value or say address of inner value we 23277 17:30:57,680 --> 17:31:02,480 to really be descriptive in our terminal 23278 17:31:00,559 --> 17:31:03,519 here so we're going to say the address 23279 17:31:03,519 --> 17:31:08,159 and on the outer value we're going to 23280 17:31:05,595 --> 17:31:10,955 add another piece of information and say 23281 17:31:08,160 --> 17:31:13,360 address of outer value and we're going 23282 17:31:13,360 --> 17:31:17,680 and this is going to pick up the outer 23283 17:31:15,275 --> 17:31:20,635 value that we have here and this is 23284 17:31:17,680 --> 17:31:22,720 going to use the inner value we have in 23285 17:31:22,720 --> 17:31:27,520 let's try and work and hopefully these 23286 17:31:24,955 --> 17:31:29,756 two addresses the inner and outer 23287 17:31:27,519 --> 17:31:31,755 addresses are going to be different 23288 17:31:29,756 --> 17:31:34,720 that's what we want to see here and 23289 17:31:31,756 --> 17:31:37,200 really proving that what we have inside 23290 17:31:34,720 --> 17:31:40,080 the lambda function is a copy through 23291 17:31:37,199 --> 17:31:42,559 the capture mechanism so let's run the 23292 17:31:40,080 --> 17:31:45,360 program the world was good so the outer 23293 17:31:42,559 --> 17:31:48,635 value the address is the e4 the inner 23294 17:31:45,360 --> 17:31:50,720 value the address is de0 we are proving 23295 17:31:48,635 --> 17:31:54,000 that what we have inside our lambda 23296 17:31:50,720 --> 17:31:56,796 function is really a copy so this is how 23297 17:31:54,000 --> 17:31:58,955 you can capture by value and that's 23298 17:31:56,796 --> 17:32:00,956 going to make a copy but sometimes you 23299 17:31:58,955 --> 17:32:02,720 really want to capture by reference and 23300 17:32:00,955 --> 17:32:05,116 we're going to see how you can do that 23301 17:32:02,720 --> 17:32:07,040 so we're going to comment out the code 23302 17:32:05,116 --> 17:32:09,756 to capture by value here we're going to 23303 17:32:07,040 --> 17:32:11,915 grab the code and reuse it because 23304 17:32:09,756 --> 17:32:14,080 that's going to be very similar to what 23305 17:32:11,915 --> 17:32:16,319 we had before we're going to say capture 23306 17:32:14,080 --> 17:32:18,635 by reference we're going to put in our 23307 17:32:16,319 --> 17:32:21,199 code and we're going to change it a tiny 23308 17:32:18,635 --> 17:32:22,955 bit what we're going to do is add an 23309 17:32:22,955 --> 17:32:27,915 in front of our variable here that we 23310 17:32:25,756 --> 17:32:29,116 are capturing and we're going to say 23311 17:32:29,116 --> 17:32:34,240 the moment we do this what we have 23312 17:32:31,595 --> 17:32:37,040 inside the lambda function is no longer 23313 17:32:34,239 --> 17:32:40,239 a copy it's going to be a true reference 23314 17:32:37,040 --> 17:32:43,360 to the outer value here and now the 23315 17:32:40,239 --> 17:32:45,360 changes we do to our outer value are 23316 17:32:43,360 --> 17:32:47,116 going to be visible inside the lambda 23317 17:32:45,360 --> 17:32:49,595 function here and you're going to see 23318 17:32:47,116 --> 17:32:51,840 the addresses to be the same 23319 17:32:49,595 --> 17:32:54,720 let's build and really see this we're 23320 17:32:51,839 --> 17:32:57,199 going to use gcc to build our program 23321 17:32:54,720 --> 17:32:59,595 the world is good we can clear now and 23322 17:32:57,199 --> 17:33:02,719 run rooster and we're going to see that 23323 17:32:59,595 --> 17:33:05,435 the outer value let's make sure we have 23324 17:33:02,720 --> 17:33:08,080 some spaces after these guys and let's 23325 17:33:05,436 --> 17:33:10,080 go up and do that and both again because 23326 17:33:08,080 --> 17:33:11,436 things are really cramped together and i 23327 17:33:11,436 --> 17:33:14,880 so the boost is good we can clear and 23328 17:33:14,879 --> 17:33:18,559 so we're going to see the outer value 23329 17:33:16,720 --> 17:33:21,436 and the inner value are changing 23330 17:33:18,559 --> 17:33:22,399 together and the addresses are now the 23331 17:33:22,400 --> 17:33:27,840 because we have captured by reference 23332 17:33:25,275 --> 17:33:30,319 the inner value and the outer value are 23333 17:33:27,839 --> 17:33:32,795 referencing the same variable we have 23334 17:33:30,319 --> 17:33:34,559 outside here this is really all we set 23335 17:33:32,796 --> 17:33:37,116 out to do in this lecture i hope you 23336 17:33:34,559 --> 17:33:39,595 found it interesting the main idea was 23337 17:33:37,116 --> 17:33:42,080 the capture mechanism we have with our 23338 17:33:39,595 --> 17:33:45,199 lambda functions to capture thanks 23339 17:33:42,080 --> 17:33:47,756 outside the context of lambda functions 23340 17:33:45,199 --> 17:33:50,000 we can capture by specifying a comma 23341 17:33:47,756 --> 17:33:52,239 separate to the list of the variables 23342 17:33:50,000 --> 17:33:54,879 that we want to capture and we have the 23343 17:33:52,239 --> 17:33:55,915 option to either capture by value by 23344 17:33:55,915 --> 17:34:01,199 the variable directly in the capture 23345 17:33:57,915 --> 17:34:03,755 list like we do here or by prepending an 23346 17:34:01,199 --> 17:34:05,915 ampersand symbol here and capture by 23347 17:34:03,756 --> 17:34:07,916 reference and you are going to have to 23348 17:34:05,915 --> 17:34:10,479 choose whichever way you want to capture 23349 17:34:07,915 --> 17:34:12,319 your variables now that you know about 23350 17:34:12,319 --> 17:34:15,680 we are going to stop here in this 23351 17:34:13,915 --> 17:34:18,319 lecture in the next one we're going to 23352 17:34:15,680 --> 17:34:20,480 see how we can capture everything from 23353 17:34:18,319 --> 17:34:21,595 the outside context to the lambda 23354 17:34:23,519 --> 17:34:28,319 and meet me there in the last lecture we 23355 17:34:25,756 --> 17:34:31,040 saw that we could capture things outside 23356 17:34:28,319 --> 17:34:32,720 the context of the lambda function now 23357 17:34:31,040 --> 17:34:36,319 we want to see that we can capture 23358 17:34:32,720 --> 17:34:38,480 everything living outside the context of 23359 17:34:36,319 --> 17:34:41,275 the lambda function the syntax to do 23360 17:34:38,480 --> 17:34:44,560 that is ridiculously simple if you want 23361 17:34:41,275 --> 17:34:46,720 to capture everything by value you just 23362 17:34:44,559 --> 17:34:48,879 put an equal sign in your lambda 23363 17:34:46,720 --> 17:34:50,880 function capture list and if you do 23364 17:34:48,879 --> 17:34:53,360 something like this now you're going to 23365 17:34:50,879 --> 17:34:55,435 have access to everything outside the 23366 17:34:53,360 --> 17:34:57,680 scope of the lambda function for example 23367 17:34:55,436 --> 17:35:00,400 we can access the c variable here from 23368 17:34:57,680 --> 17:35:02,955 the lambda function but if we have any 23369 17:35:00,400 --> 17:35:05,040 more variables declared outside the 23370 17:35:02,955 --> 17:35:08,319 scope of the lambda function we will 23371 17:35:05,040 --> 17:35:11,595 have access to them inside our lambda 23372 17:35:08,319 --> 17:35:14,319 function here again this is enabled by 23373 17:35:11,595 --> 17:35:17,040 the syntax that we use here by putting 23374 17:35:14,319 --> 17:35:19,360 an equal sign inside a lambda function 23375 17:35:17,040 --> 17:35:21,275 notice that if you use the equal sign 23376 17:35:19,360 --> 17:35:23,915 like we do here we're going to be 23377 17:35:21,275 --> 17:35:25,595 capturing by value what we have inside 23378 17:35:23,915 --> 17:35:28,399 the lambda function is going to be a 23379 17:35:25,595 --> 17:35:31,839 copy if you don't want to capture all by 23380 17:35:28,400 --> 17:35:34,400 value you can capture all by reference 23381 17:35:31,839 --> 17:35:37,039 and again the syntax is really simple 23382 17:35:34,400 --> 17:35:39,840 all you have to do is put an ampersand 23383 17:35:37,040 --> 17:35:41,436 symbol inside your capture list the 23384 17:35:39,839 --> 17:35:44,635 moment you do this you're going to have 23385 17:35:41,436 --> 17:35:47,040 access to everything outside the context 23386 17:35:44,635 --> 17:35:49,680 of your lambda function but that's going 23387 17:35:47,040 --> 17:35:51,756 to be by reference what you have inside 23388 17:35:49,680 --> 17:35:55,275 the lambda function are going to be true 23389 17:35:51,756 --> 17:35:58,796 references to the outside variables and 23390 17:35:55,275 --> 17:36:00,635 you can do pretty nasty things with them 23391 17:35:58,796 --> 17:36:02,720 because the changes you do inside the 23392 17:36:00,635 --> 17:36:05,519 lambda function are going to be visible 23393 17:36:02,720 --> 17:36:07,436 outside the lambda function and any 23394 17:36:05,519 --> 17:36:09,839 change you do from the outside is also 23395 17:36:07,436 --> 17:36:12,080 going to be visible inside the lambda 23396 17:36:09,839 --> 17:36:14,239 function so if you do this make sure 23397 17:36:12,080 --> 17:36:16,720 this is what you want and you can use 23398 17:36:14,239 --> 17:36:18,319 this to your advantage okay now that you 23399 17:36:16,720 --> 17:36:20,080 know this we're going to head over to 23400 17:36:18,319 --> 17:36:22,796 visual studio code and play with us a 23401 17:36:20,080 --> 17:36:25,040 little more okay here we are in our 23402 17:36:22,796 --> 17:36:28,080 working folder the current project is 23403 17:36:25,040 --> 17:36:31,360 capture all lists we're going to 23404 17:36:28,080 --> 17:36:33,436 grab our template files pretty quick and 23405 17:36:31,360 --> 17:36:35,436 we're going to put them in place and 23406 17:36:33,436 --> 17:36:37,840 we're going to open this little guy in 23407 17:36:35,436 --> 17:36:40,160 visual studio code let's do that we are 23408 17:36:37,839 --> 17:36:43,839 going to have our folder open we can 23409 17:36:40,160 --> 17:36:46,160 open our main cpp file clean it up like 23410 17:36:43,839 --> 17:36:48,000 we always do and we're going to close 23411 17:36:46,160 --> 17:36:50,320 the left pane here the first thing we 23412 17:36:48,000 --> 17:36:51,915 want to do is to capture everything by 23413 17:36:50,319 --> 17:36:54,400 value and we're going to put in the code 23414 17:36:51,915 --> 17:36:56,955 to do that which should be very easy to 23415 17:36:54,400 --> 17:36:59,840 understand by now and again the syntax 23416 17:36:56,955 --> 17:37:02,720 to capture everything by value is by 23417 17:36:59,839 --> 17:37:05,039 putting an equal sign within your 23418 17:37:02,720 --> 17:37:07,116 capture list you have to put that within 23419 17:37:05,040 --> 17:37:09,436 these angle brackets and that's going to 23420 17:37:07,116 --> 17:37:11,200 capture everything by value now now if 23421 17:37:09,436 --> 17:37:14,319 we run the program we're going to see 23422 17:37:11,199 --> 17:37:15,915 that c has been captured by value if we 23423 17:37:14,319 --> 17:37:17,839 look around we're going to see that the 23424 17:37:17,839 --> 17:37:22,879 change but the inner value is going to 23425 17:37:20,400 --> 17:37:24,796 stay the same this is the same behavior 23426 17:37:22,879 --> 17:37:26,795 we have seen before by capturing by 23427 17:37:24,796 --> 17:37:29,436 value so let's build again we're going 23428 17:37:26,796 --> 17:37:31,916 to build with gcc the world is good we 23429 17:37:29,436 --> 17:37:34,160 can clear and uh bring up a terminal so 23430 17:37:31,915 --> 17:37:37,115 that we can clear properly so let's do 23431 17:37:34,160 --> 17:37:39,436 clear and run rooster now we see that 23432 17:37:37,116 --> 17:37:42,796 the outer value is changing but the 23433 17:37:39,436 --> 17:37:46,160 inner value is not changing and again 23434 17:37:42,796 --> 17:37:48,796 this is classic capturing by value if 23435 17:37:46,160 --> 17:37:50,796 you want to capture all by reference 23436 17:37:48,796 --> 17:37:52,160 you're going to use the ampersand symbol 23437 17:37:50,796 --> 17:37:55,040 here and let's do that we're going to 23438 17:37:52,160 --> 17:37:57,360 comment this out and we're going to take 23439 17:37:57,360 --> 17:38:02,559 use the code here we can do that we're 23440 17:38:00,000 --> 17:38:05,360 going to go down and put in a code and 23441 17:38:02,559 --> 17:38:08,479 say that we are capturing by reference 23442 17:38:05,360 --> 17:38:11,199 we should say capturing all by reference 23443 17:38:08,480 --> 17:38:14,000 and all we have to do to make that work 23444 17:38:11,199 --> 17:38:17,519 by reference is to put our ampersand 23445 17:38:14,000 --> 17:38:19,199 symbol within this angle brackets here 23446 17:38:17,519 --> 17:38:21,359 and this is going to capture everything 23447 17:38:19,199 --> 17:38:23,039 by reference if we run now we're going 23448 17:38:21,360 --> 17:38:25,595 to see that the changes we do from the 23449 17:38:23,040 --> 17:38:26,955 outside are going to be visible from the 23450 17:38:26,955 --> 17:38:32,400 let's weld with gcc and see that this is 23451 17:38:29,756 --> 17:38:35,116 actually the case the bolt is good as 23452 17:38:32,400 --> 17:38:36,796 you see here we can clear and run the 23453 17:38:35,116 --> 17:38:40,400 rooster we're going to see that the 23454 17:38:36,796 --> 17:38:42,880 outer value is 42 the outer value is 23455 17:38:40,400 --> 17:38:46,480 changing and the inner value is changing 23456 17:38:42,879 --> 17:38:48,479 as you see in our output here and this 23457 17:38:46,480 --> 17:38:51,116 is really how you can capture 23458 17:38:48,480 --> 17:38:52,560 by reference just to play with this 23459 17:38:51,116 --> 17:38:55,756 we're going to set up another variable 23460 17:38:52,559 --> 17:38:58,239 call it d and we're going to say it 23461 17:38:55,756 --> 17:39:00,480 contains a 5 and we can try to print 23462 17:38:58,239 --> 17:39:02,000 that inside our inner value here and it 23463 17:39:00,480 --> 17:39:04,000 is going to work you're going to see 23464 17:39:02,000 --> 17:39:07,040 that it has been captured we can say 23465 17:39:04,000 --> 17:39:09,360 sddc out inner value let's say that it 23466 17:39:07,040 --> 17:39:12,080 is the d variable we are trying to 23467 17:39:09,360 --> 17:39:14,480 access here and we're going to say d and 23468 17:39:12,080 --> 17:39:16,635 it is going to be visible inside our 23469 17:39:14,480 --> 17:39:19,520 lambda function here because it was 23470 17:39:16,635 --> 17:39:22,479 captured by reference here let's build 23471 17:39:19,519 --> 17:39:24,955 again we're going to use gcc to do that 23472 17:39:22,480 --> 17:39:27,840 the bolt is good we can clear and if we 23473 17:39:24,955 --> 17:39:29,519 run booster we should see d printed out 23474 17:39:30,879 --> 17:39:36,399 d and we are able to access it through 23475 17:39:34,160 --> 17:39:39,200 the capture mechanism and we are 23476 17:39:36,400 --> 17:39:41,520 capturing everything in the context by 23477 17:39:39,199 --> 17:39:43,275 reference here this is really all we set 23478 17:39:41,519 --> 17:39:45,755 out to do in this lecture i hope you 23479 17:39:43,275 --> 17:39:48,559 found it interesting i hate to say it 23480 17:39:45,756 --> 17:39:50,955 but this happens to be the last lecture 23481 17:39:48,559 --> 17:39:53,199 in this chapter on lambda functions we 23482 17:39:50,955 --> 17:39:55,680 still have a lot to learn about lambda 23483 17:39:53,199 --> 17:39:57,755 functions but unfortunately we don't 23484 17:39:55,680 --> 17:39:59,840 really have enough tools to understand 23485 17:39:57,756 --> 17:40:01,436 that now but for now we're going to stop 23486 17:39:59,839 --> 17:40:03,435 here and learn a little more things 23487 17:40:01,436 --> 17:40:05,916 about c plus plus then we're going to 23488 17:40:03,436 --> 17:40:08,880 come back and see some crazy things you 23489 17:40:05,915 --> 17:40:10,719 can really do with lambda functions i 23490 17:40:08,879 --> 17:40:12,559 would like to welcome you in this new 23491 17:40:10,720 --> 17:40:14,955 chapter where we're going to be learning 23492 17:40:12,559 --> 17:40:18,000 about function templates function 23493 17:40:14,955 --> 17:40:21,595 templates are a mechanism we have in c 23494 17:40:18,000 --> 17:40:24,720 plus to set up blueprint for functions 23495 17:40:21,595 --> 17:40:26,559 and have one point of control to set up 23496 17:40:24,720 --> 17:40:28,720 the function but the compiler is going 23497 17:40:26,559 --> 17:40:31,275 to generate the actual function that 23498 17:40:28,720 --> 17:40:33,116 gets called when it sees the function 23499 17:40:31,275 --> 17:40:35,275 call so this is going to solve the 23500 17:40:33,116 --> 17:40:37,276 problem we had before with function 23501 17:40:35,275 --> 17:40:39,595 overloads if you look at the code here 23502 17:40:37,275 --> 17:40:42,400 we have multiple overloads for the 23503 17:40:39,595 --> 17:40:45,435 maximum function but what we really are 23504 17:40:42,400 --> 17:40:48,635 doing in the body of these functions 23505 17:40:45,436 --> 17:40:51,840 is the same and suppose you have 23506 17:40:48,635 --> 17:40:53,839 12 or 13 or 20 overloads of the same 23507 17:40:51,839 --> 17:40:56,319 function you're going to be basically 23508 17:40:53,839 --> 17:40:58,079 repeating the same logic in the bodies 23509 17:40:56,319 --> 17:41:00,955 of this function and this code 23510 17:40:58,080 --> 17:41:02,720 repetition is bad function templates are 23511 17:41:02,720 --> 17:41:06,955 solve problems like this the way they 23512 17:41:05,040 --> 17:41:09,520 work you set up a blueprint for your 23513 17:41:06,955 --> 17:41:12,080 function and this is the syntax to do 23514 17:41:09,519 --> 17:41:14,479 something like that you say template 23515 17:41:12,080 --> 17:41:16,000 type name t here don't worry about this 23516 17:41:14,480 --> 17:41:19,200 syntax we're going to have a chance to 23517 17:41:16,000 --> 17:41:21,436 go through it in detail but if you do 23518 17:41:19,199 --> 17:41:24,399 things like this this is going to be a 23519 17:41:21,436 --> 17:41:26,319 blueprint for your maximum function and 23520 17:41:24,400 --> 17:41:30,000 the compiler is going to use this 23521 17:41:26,319 --> 17:41:31,756 blueprint to generate an actual function 23522 17:41:30,000 --> 17:41:34,000 when you call this function and this is 23523 17:41:31,756 --> 17:41:36,160 going to make things super easy i 23524 17:41:34,000 --> 17:41:39,116 realized some of this is really cryptic 23525 17:41:36,160 --> 17:41:41,596 so let's head over to the next lecture 23526 17:41:39,116 --> 17:41:44,080 and let you set up your very first 23527 17:41:41,595 --> 17:41:46,635 function template go ahead and finish up 23528 17:41:44,080 --> 17:41:49,040 here and meet me there in this lecture 23529 17:41:46,635 --> 17:41:51,436 we're going to set up our very first 23530 17:41:49,040 --> 17:41:54,239 function template and again function 23531 17:41:51,436 --> 17:41:57,200 templates are a mechanism we have in c 23532 17:41:54,239 --> 17:42:00,000 plus plus to avoid code repetition for 23533 17:41:57,199 --> 17:42:02,879 example if you are setting up function 23534 17:42:00,000 --> 17:42:05,519 overloads if you look here we have our 23535 17:42:02,879 --> 17:42:07,680 max function and we have multiple 23536 17:42:05,519 --> 17:42:10,879 overloads for this function we can call 23537 17:42:07,680 --> 17:42:12,955 it with and double and string view here 23538 17:42:10,879 --> 17:42:15,680 but if you look inside you see that we 23539 17:42:12,955 --> 17:42:18,239 really are repeating the same logic 23540 17:42:15,680 --> 17:42:21,595 and it is even possible to have dozens 23541 17:42:18,239 --> 17:42:23,680 of overloads if you want to support tons 23542 17:42:23,680 --> 17:42:29,275 and this code repetition is really bad 23543 17:42:27,040 --> 17:42:32,000 function templates are a mechanism we 23544 17:42:29,275 --> 17:42:33,519 have in place to avoid things like this 23545 17:42:33,519 --> 17:42:38,559 you set up a function blueprint using 23546 17:42:36,160 --> 17:42:40,160 the syntax you see here and your 23547 17:42:38,559 --> 17:42:41,595 function template is going to be 23548 17:42:41,595 --> 17:42:46,795 with this template type name teeth and 23549 17:42:44,239 --> 17:42:48,559 here type name t is going to be in this 23550 17:42:48,559 --> 17:42:53,040 and then you're going to put the 23551 17:42:50,000 --> 17:42:55,436 blueprint for your function t is a 23552 17:42:53,040 --> 17:42:58,559 placeholder for the types that you use 23553 17:42:55,436 --> 17:43:01,436 in your function you see that t is in 23554 17:42:58,559 --> 17:43:03,756 the return type here it's in the 23555 17:43:01,436 --> 17:43:05,360 parameters that we pass to the function 23556 17:43:03,756 --> 17:43:07,436 and because it is the same character 23557 17:43:05,360 --> 17:43:09,275 here all these things have to be of the 23558 17:43:07,436 --> 17:43:11,680 same type the return type the first 23559 17:43:09,275 --> 17:43:14,400 parameter the second parameter they have 23560 17:43:11,680 --> 17:43:16,319 to be of the same type okay so once you 23561 17:43:14,400 --> 17:43:17,916 have your function template set up like 23562 17:43:16,319 --> 17:43:20,480 this you're going to implement it like 23563 17:43:17,915 --> 17:43:23,199 we see down below here it is nothing 23564 17:43:20,480 --> 17:43:25,116 complicated we just set up the prototype 23565 17:43:25,116 --> 17:43:30,240 and inside we put our implementation you 23566 17:43:27,680 --> 17:43:32,319 see here we have separated this into 23567 17:43:30,239 --> 17:43:34,795 function declaration and function 23568 17:43:32,319 --> 17:43:37,519 definition but inside you see that it is 23569 17:43:34,796 --> 17:43:39,916 the same logic we had to compute our 23570 17:43:37,519 --> 17:43:42,719 maximum between these two parameters 23571 17:43:39,915 --> 17:43:45,595 that we pass to this function 23572 17:43:42,720 --> 17:43:48,955 so how does this work when the compiler 23573 17:43:45,595 --> 17:43:51,915 sees you call this max function for 23574 17:43:48,955 --> 17:43:54,239 example as we are doing here it's going 23575 17:43:51,915 --> 17:43:56,795 to look at the types that you pass in 23576 17:43:56,796 --> 17:44:03,520 are of ant type so it is going to 23577 17:43:59,436 --> 17:44:05,840 replace the t in here for and and it is 23578 17:44:03,519 --> 17:44:06,879 going to generate a function initial 23579 17:44:06,879 --> 17:44:10,719 and this really brings up a big point 23580 17:44:08,879 --> 17:44:13,519 about function templates many people 23581 17:44:10,720 --> 17:44:16,160 think that function templates are actual 23582 17:44:13,519 --> 17:44:18,795 c plus plus code they're not they're 23583 17:44:16,160 --> 17:44:21,840 just a blueprint from which the compiler 23584 17:44:18,796 --> 17:44:23,596 is going to generate the actual function 23585 17:44:21,839 --> 17:44:26,559 that is called by the compiler for 23586 17:44:23,595 --> 17:44:27,435 example when you issue a call like this 23587 17:44:27,436 --> 17:44:32,720 function templates are not real c plus 23588 17:44:29,519 --> 17:44:35,039 plus code they are just a blueprint that 23589 17:44:32,720 --> 17:44:36,080 the compiler uses to generate your 23590 17:44:36,080 --> 17:44:40,720 based on the arguments you pass to the 23591 17:44:38,400 --> 17:44:43,275 function when you call it so when we 23592 17:44:40,720 --> 17:44:45,595 issue the call like we do here an int 23593 17:44:43,275 --> 17:44:47,680 version of our maximum function is going 23594 17:44:45,595 --> 17:44:50,559 to be created so the compiler is 23595 17:44:47,680 --> 17:44:52,796 basically going to go in and replace n 23596 17:44:50,559 --> 17:44:54,635 and for t here the return type is going 23597 17:44:52,796 --> 17:44:56,400 to be nt the first parameter is going to 23598 17:44:54,635 --> 17:44:57,680 be end the second parameter is going to 23599 17:44:56,400 --> 17:44:59,436 be empty it's going to go in the 23600 17:44:57,680 --> 17:45:01,436 definition it's going to do that and 23601 17:44:59,436 --> 17:45:04,239 we're going to have an it function 23602 17:45:01,436 --> 17:45:06,635 if we issue a call with c and d like we 23603 17:45:04,239 --> 17:45:09,199 do here in the second line here then the 23604 17:45:06,635 --> 17:45:11,436 compiler is going to see that c and d 23605 17:45:09,199 --> 17:45:13,360 are of double type so it is going to 23606 17:45:11,436 --> 17:45:14,955 generate another function from this 23607 17:45:13,360 --> 17:45:17,275 blueprint here and it is going to 23608 17:45:14,955 --> 17:45:19,595 replace in double then we're going to 23609 17:45:17,275 --> 17:45:21,040 have another double function if we go to 23610 17:45:19,595 --> 17:45:22,635 the third line here we're going to do 23611 17:45:21,040 --> 17:45:24,955 the same we're going to call it with 23612 17:45:22,635 --> 17:45:27,680 strengths and it is going to generate a 23613 17:45:24,955 --> 17:45:30,559 stringed version of the maximum function 23614 17:45:27,680 --> 17:45:33,040 here so what we achieve with function 23615 17:45:33,040 --> 17:45:37,520 several overloads for the same function 23616 17:45:35,360 --> 17:45:40,559 and we have one point of control in our 23617 17:45:37,519 --> 17:45:43,275 code but the compiler is still going to 23618 17:45:40,559 --> 17:45:45,275 generate this function overloads but one 23619 17:45:43,275 --> 17:45:47,436 thing about this is that if a function 23620 17:45:45,275 --> 17:45:50,479 is not used for example if you never 23621 17:45:47,436 --> 17:45:52,239 call a maximum function with string 23622 17:45:50,480 --> 17:45:54,636 then the string version is never going 23623 17:45:52,239 --> 17:45:57,275 to be created the compiler is going to 23624 17:45:54,635 --> 17:45:59,436 generate the overloads that you only 23625 17:45:57,275 --> 17:46:01,595 call in your c plus plus program and 23626 17:45:59,436 --> 17:46:04,160 this is really all function templates 23627 17:46:01,595 --> 17:46:06,319 are all about they give you a mechanism 23628 17:46:04,160 --> 17:46:07,840 to set up a blueprint for your function 23629 17:46:06,319 --> 17:46:11,116 and then the compiler is going to 23630 17:46:07,839 --> 17:46:13,359 generate actual functions based on what 23631 17:46:11,116 --> 17:46:14,796 you call in your c plus plus code okay 23632 17:46:13,360 --> 17:46:16,796 we have saved this but i want to 23633 17:46:16,796 --> 17:46:22,796 function templates are just blueprints 23634 17:46:19,595 --> 17:46:24,479 they're not real c plus plus could and 23635 17:46:22,796 --> 17:46:26,400 when we hit visual studio code in a 23636 17:46:24,480 --> 17:46:29,520 minute we're going to see a tool 23637 17:46:26,400 --> 17:46:32,480 that allows us to see how the compiler 23638 17:46:29,519 --> 17:46:35,115 instantiates your function templates 23639 17:46:32,480 --> 17:46:37,916 based on the arguments you call your 23640 17:46:35,116 --> 17:46:40,160 function with but keep this in mind 23641 17:46:37,915 --> 17:46:43,275 function templates are not real c plus 23642 17:46:40,160 --> 17:46:45,520 plus code the rio c plus plus functions 23643 17:46:43,275 --> 17:46:47,915 get generated by the compiler 23644 17:46:45,519 --> 17:46:50,635 when you call your function and another 23645 17:46:47,915 --> 17:46:53,680 thing i wanted to point out is that 23646 17:46:50,635 --> 17:46:55,680 once a function template instance is 23647 17:46:53,680 --> 17:46:59,275 generated for example the interversion 23648 17:46:55,680 --> 17:47:01,199 of our maximum function template then if 23649 17:46:59,275 --> 17:47:03,275 it is needed again the compiler is not 23650 17:47:01,199 --> 17:47:05,680 going to generate that again it's going 23651 17:47:03,275 --> 17:47:07,756 to see if there is one generator already 23652 17:47:05,680 --> 17:47:09,436 and it is going to reuse that okay this 23653 17:47:07,756 --> 17:47:11,916 is really all i have to share about 23654 17:47:09,436 --> 17:47:14,000 function templates at this point in this 23655 17:47:11,915 --> 17:47:15,915 chapter for now we're going to head over 23656 17:47:14,000 --> 17:47:18,796 to visual studio code and play with this 23657 17:47:15,915 --> 17:47:21,360 a little more okay here we are in our 23658 17:47:18,796 --> 17:47:23,116 working directory the current project is 23659 17:47:21,360 --> 17:47:25,915 trying out function templates we're 23660 17:47:23,116 --> 17:47:27,756 going to grab our template project and 23661 17:47:25,915 --> 17:47:30,319 one thing i should point out here is 23662 17:47:27,756 --> 17:47:34,080 that our template project has been now 23663 17:47:30,319 --> 17:47:36,000 updated to have the launch.json file 23664 17:47:34,080 --> 17:47:38,635 which is going to allow us to debug our 23665 17:47:36,000 --> 17:47:41,519 programs so you should use a template 23666 17:47:38,635 --> 17:47:43,275 that includes the launch.json file here 23667 17:47:41,519 --> 17:47:45,275 that's going to allow us to debug your 23668 17:47:43,275 --> 17:47:47,360 programs and we're going to do that in a 23669 17:47:45,275 --> 17:47:49,839 minute when we have set up the example 23670 17:47:47,360 --> 17:47:52,879 here so we're going to grab our template 23671 17:47:49,839 --> 17:47:54,955 files and put them in place and we're 23672 17:47:52,879 --> 17:47:57,519 going to open this little guy in visual 23673 17:47:54,955 --> 17:48:00,319 studio code by dragging and dropping on 23674 17:47:57,519 --> 17:48:02,319 top here this is going to do our job 23675 17:48:00,319 --> 17:48:04,400 then we're going to open up the main cpp 23676 17:48:02,319 --> 17:48:06,559 file and i think we should close the 23677 17:48:04,400 --> 17:48:07,916 left pane here we don't need it anymore 23678 17:48:06,559 --> 17:48:10,239 and we're going to clean this up a 23679 17:48:07,915 --> 17:48:12,399 little bit so one thing i want to do is 23680 17:48:10,239 --> 17:48:14,400 to first do things the bad way so we're 23681 17:48:12,400 --> 17:48:16,560 going to set up a max function 23682 17:48:16,559 --> 17:48:21,595 and it's going to take end parameters 23683 17:48:18,955 --> 17:48:23,436 and we're going to return the maximum 23684 17:48:21,595 --> 17:48:26,319 between a and b and the way we do that 23685 17:48:23,436 --> 17:48:28,160 we say a greater than b we are going to 23686 17:48:26,319 --> 17:48:29,680 use our ternary operator here we're 23687 17:48:28,160 --> 17:48:32,480 going to put a question mark we're going 23688 17:48:29,680 --> 17:48:34,796 to return a or b and this is going to be 23689 17:48:32,480 --> 17:48:38,000 our function but suppose we need another 23690 17:48:34,796 --> 17:48:40,240 function that works on doubles if we put 23691 17:48:38,000 --> 17:48:42,635 double arguments in here the compiler is 23692 17:48:40,239 --> 17:48:45,436 going to insert implicit conversions 23693 17:48:42,635 --> 17:48:48,879 from double to end this may seem to work 23694 17:48:45,436 --> 17:48:50,720 but suppose you use a parameter type 23695 17:48:48,879 --> 17:48:52,795 that is not convertible to end for 23696 17:48:50,720 --> 17:48:54,840 example you use a string or something 23697 17:48:54,839 --> 17:49:01,199 here just for an example so we're going 23698 17:49:04,400 --> 17:49:08,560 and we should make these const 23699 17:49:06,080 --> 17:49:11,360 references because we're not interested 23700 17:49:08,559 --> 17:49:13,435 in modifying these strings here 23701 17:49:13,436 --> 17:49:18,319 and we're going to say const std string 23702 17:49:16,160 --> 17:49:20,560 reference a this is much better it's 23703 17:49:18,319 --> 17:49:23,436 going to avoid unnecessary copies of 23704 17:49:20,559 --> 17:49:25,115 strings and we're going to say const htd 23705 17:49:25,116 --> 17:49:29,680 be here then inside we're not going to 23706 17:49:27,839 --> 17:49:32,399 do anything different we're just going 23707 17:49:29,680 --> 17:49:35,199 to use the same logic okay let's do that 23708 17:49:32,400 --> 17:49:38,560 suppose you have another function 23709 17:49:35,199 --> 17:49:41,435 which takes what should i say long long 23710 17:49:38,559 --> 17:49:44,795 end if we need that we need to copy this 23711 17:49:41,436 --> 17:49:46,480 function and put that in okay we need to 23712 17:49:44,796 --> 17:49:48,796 do that and we're going to change this 23713 17:49:46,480 --> 17:49:50,240 to long long end and we're going to do 23714 17:49:50,239 --> 17:49:55,915 long long and our parameter and we're 23715 17:49:53,360 --> 17:49:58,160 going to say the same here and you see 23716 17:49:55,915 --> 17:50:00,719 we are setting up a lot of functions 23717 17:49:58,160 --> 17:50:03,840 with basically the same thing inside and 23718 17:50:00,720 --> 17:50:06,635 that this is a lot of code copies 23719 17:50:03,839 --> 17:50:09,915 suppose we have this function maybe 20 23720 17:50:06,635 --> 17:50:12,319 times it is possible or even worse the 23721 17:50:09,915 --> 17:50:15,115 logic in this function is really long 23722 17:50:12,319 --> 17:50:17,680 maybe 20 or 30 lines and we will have to 23723 17:50:15,116 --> 17:50:19,596 repeat this thing in aku so the 23724 17:50:17,680 --> 17:50:22,319 designers of the c plus plus language 23725 17:50:19,595 --> 17:50:25,360 saw this problem and they said why don't 23726 17:50:22,319 --> 17:50:27,756 we set up a one point of control 23727 17:50:25,360 --> 17:50:31,360 where we can set up our logic to do the 23728 17:50:27,756 --> 17:50:32,720 maximum and we let the compiler generate 23729 17:50:32,720 --> 17:50:39,436 functions based on the arguments we call 23730 17:50:35,915 --> 17:50:41,040 our functions with and they came up with 23731 17:50:39,436 --> 17:50:42,400 function templates and that's what we're 23732 17:50:41,040 --> 17:50:44,160 going to see so we're going to take out 23733 17:50:42,400 --> 17:50:47,680 all the other functions and we're going 23734 17:50:44,160 --> 17:50:50,160 to turn this maximum and function into a 23735 17:50:47,680 --> 17:50:52,160 function template the syntax to do that 23736 17:50:52,160 --> 17:50:56,000 so we're going to say template this is 23737 17:50:54,000 --> 17:50:58,720 what you say you put an angle bracket 23738 17:50:56,000 --> 17:51:01,436 and you say type name and you specify 23739 17:50:58,720 --> 17:51:03,360 the placeholder for the type in this 23740 17:51:01,436 --> 17:51:04,720 angle brackets here this is the syntax 23741 17:51:04,720 --> 17:51:10,400 then you have to specify your function 23742 17:51:07,199 --> 17:51:12,879 in terms of this placeholder t it is 23743 17:51:10,400 --> 17:51:15,116 going to take the place of the type so 23744 17:51:12,879 --> 17:51:17,115 for example here we want to return and 23745 17:51:15,116 --> 17:51:19,436 we're going to change this to t and the 23746 17:51:17,116 --> 17:51:21,916 parameters are going to be taken by 23747 17:51:23,436 --> 17:51:28,560 and now we have a legal c plus plus 23748 17:51:26,319 --> 17:51:30,720 function template it is going to take 23749 17:51:28,559 --> 17:51:34,239 this template here and use it to 23750 17:51:30,720 --> 17:51:36,160 generate actual c plus plus functions so 23751 17:51:34,239 --> 17:51:39,595 for example if we go in the main 23752 17:51:36,160 --> 17:51:42,956 function here and say ent result 23753 17:51:39,595 --> 17:51:45,199 equals max maximum we should say and say 23754 17:51:42,955 --> 17:51:47,680 x and y for example let's create these 23755 17:51:45,199 --> 17:51:49,915 variables on top here so we're going to 23756 17:51:47,680 --> 17:51:52,319 say ent x and we're going to put in a 23757 17:51:49,915 --> 17:51:55,435 five and we're going to create a y and 23758 17:51:52,319 --> 17:51:57,360 put in a seven why not now this function 23759 17:51:55,436 --> 17:51:58,635 is going to store the result and print 23760 17:51:58,635 --> 17:52:02,479 let's do that so that we can see that 23761 17:52:00,796 --> 17:52:05,596 this actually works so we're going to 23762 17:52:02,480 --> 17:52:08,240 say result and say results this is going 23763 17:52:05,595 --> 17:52:10,399 to do and if we build and run this 23764 17:52:08,239 --> 17:52:11,915 program we're going to use gcc you're 23765 17:52:10,400 --> 17:52:13,040 going to see that the world is going to 23766 17:52:13,040 --> 17:52:18,796 and uh world finished successfully we 23767 17:52:15,519 --> 17:52:20,955 can run this program like we always do 23768 17:52:18,796 --> 17:52:23,276 let's bring up a terminal and clear a 23769 17:52:20,955 --> 17:52:24,635 little bit and run rooster you're going 23770 17:52:23,275 --> 17:52:27,199 to see that we're going to see that 23771 17:52:24,635 --> 17:52:29,839 result now is 7 because that's the 23772 17:52:27,199 --> 17:52:33,115 maximum you see it is working here so 23773 17:52:29,839 --> 17:52:34,879 how is this mechanism working here the 23774 17:52:33,116 --> 17:52:37,360 compiler is basically looking in the 23775 17:52:34,879 --> 17:52:39,755 main function here and it is noticing 23776 17:52:37,360 --> 17:52:43,040 the call and it is going to look around 23777 17:52:39,756 --> 17:52:44,796 and see if it can find a declaration for 23778 17:52:43,040 --> 17:52:47,360 the maximum function here it's not going 23779 17:52:44,796 --> 17:52:49,116 to find it because there is none then 23780 17:52:47,360 --> 17:52:51,275 it's going to look at the function 23781 17:52:49,116 --> 17:52:54,080 templates it has in here and it's going 23782 17:52:51,275 --> 17:52:56,400 to see that it can do a replacement and 23783 17:52:54,080 --> 17:52:58,560 replacing the types that we are passing 23784 17:52:56,400 --> 17:53:00,635 in here so it is going to do that it is 23785 17:52:58,559 --> 17:53:02,319 going to take this template and it is 23786 17:53:02,319 --> 17:53:08,239 t by end and then it is going to 23787 17:53:05,275 --> 17:53:10,319 generate a function that basically looks 23788 17:53:08,239 --> 17:53:13,115 like what we have here we're going to 23789 17:53:10,319 --> 17:53:14,720 copy this and we're going to put this in 23790 17:53:13,116 --> 17:53:16,400 the compiler is going to take the first 23791 17:53:14,720 --> 17:53:18,319 t here it's going to turn that into a 23792 17:53:16,400 --> 17:53:20,239 net it's going to take the second one 23793 17:53:18,319 --> 17:53:23,040 here it's going to turn that into an end 23794 17:53:20,239 --> 17:53:25,595 it's going to take the third one here 23795 17:53:23,040 --> 17:53:28,080 and turn that into an it and we're going 23796 17:53:25,595 --> 17:53:30,159 to have a function definition for the 23797 17:53:28,080 --> 17:53:31,360 call here this is what the compiler is 23798 17:53:31,360 --> 17:53:36,720 one thing some of you might be thinking 23799 17:53:33,915 --> 17:53:38,955 is there a way to really prove this and 23800 17:53:36,720 --> 17:53:39,916 somebody was nice enough to design a 23801 17:53:41,436 --> 17:53:46,720 this function or template instances 23802 17:53:44,319 --> 17:53:49,199 generated by the compiler and i happen 23803 17:53:46,720 --> 17:53:51,595 to have that opened it is a web-based 23804 17:53:49,199 --> 17:53:53,719 tool so you can use this in your browser 23805 17:53:55,275 --> 17:54:01,199 and then we're going to grab the code we 23806 17:53:57,360 --> 17:54:04,160 had in our main cpp file we're going to 23807 17:54:01,199 --> 17:54:07,360 copy it all and we're going to go back 23808 17:54:04,160 --> 17:54:09,040 to our browser and remove whatever i had 23809 17:54:07,360 --> 17:54:11,040 in here because i don't want this 23810 17:54:09,040 --> 17:54:13,200 anymore and i'm going to put in my 23811 17:54:11,040 --> 17:54:16,319 function template if we want we can even 23812 17:54:13,199 --> 17:54:18,319 take this sddc out statement because we 23813 17:54:16,319 --> 17:54:21,756 don't want all the junk that comes with 23814 17:54:18,319 --> 17:54:24,239 that we are just interested in looking 23815 17:54:24,239 --> 17:54:29,199 generated by the compiler now that we 23816 17:54:26,559 --> 17:54:31,435 have the code in here we're going to see 23817 17:54:29,199 --> 17:54:33,595 what the compiler generates because of 23818 17:54:31,436 --> 17:54:35,436 the call we have here i think i could 23819 17:54:33,595 --> 17:54:36,720 increase the size of the font here so 23820 17:54:36,720 --> 17:54:39,680 can see this i think this is a little 23821 17:54:39,680 --> 17:54:44,840 then if you hit this play button here 23822 17:54:42,319 --> 17:54:47,360 let's click on it it's going to do its 23823 17:54:44,839 --> 17:54:48,955 processing and then the code generated 23824 17:54:47,360 --> 17:54:51,360 by the compiler is going to show up to 23825 17:54:48,955 --> 17:54:52,635 the right here you see it has detected 23826 17:54:52,635 --> 17:54:58,239 called the function template on line 13 23827 17:54:55,199 --> 17:54:59,680 here you see this line is highlighted 23828 17:54:58,239 --> 17:55:02,319 and then if you go to the right we're 23829 17:54:59,680 --> 17:55:04,559 going to see the functions generated by 23830 17:55:02,319 --> 17:55:06,635 the compiler so if you go here you see 23831 17:55:06,635 --> 17:55:12,239 of our function template and another 23832 17:55:09,519 --> 17:55:14,719 thing i should prove here is that 23833 17:55:12,239 --> 17:55:16,400 template instances that are not needed 23834 17:55:14,720 --> 17:55:18,319 are not going to be generated by the 23835 17:55:16,400 --> 17:55:21,436 compiler another way to say this the 23836 17:55:18,319 --> 17:55:24,480 compiler is only going to generate the 23837 17:55:21,436 --> 17:55:27,200 template instances it needs and again a 23838 17:55:24,480 --> 17:55:29,840 template instance is what you have here 23839 17:55:27,199 --> 17:55:32,559 the function the actual function that is 23840 17:55:29,839 --> 17:55:34,879 callable by the compiler generated from 23841 17:55:32,559 --> 17:55:37,519 your blueprint so if for example we 23842 17:55:37,519 --> 17:55:42,635 parameters to double let's do that okay 23843 17:55:39,915 --> 17:55:44,879 so we changed our types to double we're 23844 17:55:42,635 --> 17:55:46,635 going to hit the play button again 23845 17:55:44,879 --> 17:55:48,879 you're going to see that now it is going 23846 17:55:46,635 --> 17:55:51,275 to generate a double function here you 23847 17:55:48,879 --> 17:55:52,955 see that here if we change this for 23848 17:55:52,955 --> 17:55:58,559 what can we use let's use long long end 23849 17:55:55,839 --> 17:56:02,319 like we did in our lecture we can do 23850 17:55:58,559 --> 17:56:04,559 that we can say long long and and if we 23851 17:56:02,319 --> 17:56:07,436 hit the play button that's what is going 23852 17:56:04,559 --> 17:56:10,159 to be generated by the compiler so 23853 17:56:07,436 --> 17:56:13,596 hopefully this proves that the compiler 23854 17:56:10,160 --> 17:56:16,400 is only going to generate the instances 23855 17:56:13,595 --> 17:56:18,879 that are needed for the calls you do in 23856 17:56:16,400 --> 17:56:21,116 your c plus plus program and this is 23857 17:56:18,879 --> 17:56:22,955 really function templates are all about 23858 17:56:21,116 --> 17:56:25,040 okay now that you know this let's set up 23859 17:56:22,955 --> 17:56:26,479 a few more examples to see that this 23860 17:56:26,480 --> 17:56:29,680 so let's change the types of these guys 23861 17:56:29,680 --> 17:56:33,915 double let's do that and we're going to 23862 17:56:32,160 --> 17:56:37,200 play with this a little more in visual 23863 17:56:33,915 --> 17:56:41,955 studio code and let's use double 23864 17:56:41,955 --> 17:56:48,720 78.7 here let's do that 78.7 and we're 23865 17:56:46,319 --> 17:56:50,635 going to build this and one thing i 23866 17:56:48,720 --> 17:56:52,880 should point out is that template 23867 17:56:50,635 --> 17:56:55,199 instances are generated when the 23868 17:56:52,879 --> 17:56:56,879 compiler is doing its job and then they 23869 17:56:56,879 --> 17:57:02,239 in the binary representation of your 23870 17:56:59,519 --> 17:57:05,115 source code so the world is good as you 23871 17:57:02,239 --> 17:57:08,319 see here we can clear and run a rooster 23872 17:57:05,116 --> 17:57:11,200 we're going to see that the result is 78 23873 17:57:08,319 --> 17:57:13,595 and it is transformed to end because we 23874 17:57:11,199 --> 17:57:15,435 are assigning to end here to really get 23875 17:57:13,595 --> 17:57:18,079 to the correct results here we should 23876 17:57:15,436 --> 17:57:20,000 use auto type deduction if we do that 23877 17:57:18,080 --> 17:57:22,480 the compiler is going to deduce the type 23878 17:57:20,000 --> 17:57:24,955 of result here based on the return type 23879 17:57:22,480 --> 17:57:27,360 of this function or the function 23880 17:57:24,955 --> 17:57:29,116 template instance i should say 23881 17:57:27,360 --> 17:57:31,360 now we're going to change this to auto 23882 17:57:31,360 --> 17:57:36,319 and if we both again we should see a 23883 17:57:33,199 --> 17:57:38,719 double printed out correctly the both is 23884 17:57:36,319 --> 17:57:41,275 good we can clear and run the rooster 23885 17:57:38,720 --> 17:57:44,796 now we have the correct value printed 23886 17:57:41,275 --> 17:57:47,115 out from our function template here 23887 17:57:44,796 --> 17:57:49,436 let's change this to std string we can 23888 17:57:52,080 --> 17:57:57,436 and we're going to change these two guys 23889 17:57:53,839 --> 17:58:01,435 to std string why not std string x is 23890 17:57:57,436 --> 17:58:04,720 going to be a string electro let's use 23891 17:58:01,436 --> 17:58:07,520 hello for example and uh for 23892 17:58:04,720 --> 17:58:10,080 y we're going to say std strength and 23893 17:58:07,519 --> 17:58:12,079 we're going to put in a world here okay 23894 17:58:10,080 --> 17:58:14,400 this is going to do and you notice that 23895 17:58:12,080 --> 17:58:16,560 we don't even have a compiler error 23896 17:58:14,400 --> 17:58:18,400 visual studio code is not complaining 23897 17:58:16,559 --> 17:58:21,115 the compiler is going to look at the 23898 17:58:18,400 --> 17:58:23,596 types we call our function with then it 23899 17:58:21,116 --> 17:58:26,000 is going to generate is an sdd string 23900 17:58:23,595 --> 17:58:28,879 version of this function and what it is 23901 17:58:26,000 --> 17:58:31,915 going to do it's going to replace in t 23902 17:58:28,879 --> 17:58:35,595 and replace that for std strand we can 23903 17:58:31,915 --> 17:58:37,595 actually put this thing entirely in 23904 17:58:35,595 --> 17:58:39,519 cpp inside so let's copy this code 23905 17:58:37,595 --> 17:58:41,435 before we run it so that you can really 23906 17:58:39,519 --> 17:58:43,115 see what is happening here we're going 23907 17:58:41,436 --> 17:58:45,200 to take out whatever we had before we're 23908 17:58:43,116 --> 17:58:46,880 going to put in our code and then we're 23909 17:58:45,199 --> 17:58:48,879 going to hit the play button here it's 23910 17:58:46,879 --> 17:58:51,199 going to generate our thumb we wait for 23911 17:58:48,879 --> 17:58:54,079 it to do its thing and if you look here 23912 17:58:54,080 --> 17:59:00,000 std strength this is really a compiler 23913 17:58:56,519 --> 17:59:02,079 representation of std string we don't 23914 17:59:00,000 --> 17:59:04,879 have enough tools to really understand 23915 17:59:02,080 --> 17:59:06,955 this but hopefully you can see that it 23916 17:59:06,955 --> 17:59:12,559 with some other type we don't know yet 23917 17:59:09,436 --> 17:59:14,955 std basic string car it has a few things 23918 17:59:12,559 --> 17:59:17,360 inside that we don't really know yet we 23919 17:59:14,955 --> 17:59:19,839 will have to learn a few more things 23920 17:59:17,360 --> 17:59:21,199 about this as we progress in the course 23921 17:59:21,199 --> 17:59:25,839 i want to prove to you that the compiler 23922 17:59:23,275 --> 17:59:28,720 is doing the replacement you see t was 23923 17:59:25,839 --> 17:59:30,879 replaced by this thing here std basic 23924 17:59:28,720 --> 17:59:34,080 strength and we have angle brackets and 23925 17:59:30,879 --> 17:59:36,239 this entire thing is really hdd strength 23926 17:59:34,080 --> 17:59:37,916 in the eyes of the compiler okay now 23927 17:59:36,239 --> 17:59:40,400 that we know this let's go back to 23928 17:59:37,915 --> 17:59:43,595 visual studio code and run this we're 23929 17:59:40,400 --> 17:59:45,756 going to build with gcc as we always do 23930 17:59:43,595 --> 17:59:46,879 the world is good we can clear and run 23931 17:59:46,879 --> 17:59:51,115 going to see that the maximum is world 23932 17:59:49,040 --> 17:59:53,040 and this is going to be using the 23933 17:59:53,040 --> 17:59:57,275 and this is going to compare two 23934 17:59:55,116 --> 18:00:00,160 strengths based on this and we know that 23935 18:00:00,160 --> 18:00:04,880 in lexicographical order so the 23936 18:00:03,116 --> 18:00:06,400 comparison that is done here is 23937 18:00:06,400 --> 18:00:11,275 for this strength here or i should say 23938 18:00:08,879 --> 18:00:14,479 it is comparing things in alphabetical 23939 18:00:11,275 --> 18:00:17,115 order in abcde and things like that okay 23940 18:00:14,480 --> 18:00:18,636 now hopefully you have a big idea about 23941 18:00:17,116 --> 18:00:20,955 function templates they are just 23942 18:00:20,955 --> 18:00:26,000 to generate actual functions and this is 23943 18:00:23,915 --> 18:00:28,000 going to allow you to avoid multiple 23944 18:00:26,000 --> 18:00:28,955 function overloads as we have seen 23945 18:00:28,955 --> 18:00:33,199 one thing i want you to see 23946 18:00:30,955 --> 18:00:36,159 is that you have to be sure that the 23947 18:00:33,199 --> 18:00:38,635 logic you do inside your function is 23948 18:00:36,160 --> 18:00:40,636 supported by the types that you pass to 23949 18:00:38,635 --> 18:00:42,080 the function and i'm going to show you 23950 18:00:40,635 --> 18:00:43,756 what i mean here we're going to set up 23951 18:00:42,080 --> 18:00:46,000 another function template we're going to 23952 18:00:43,756 --> 18:00:48,239 say template type named we're going to 23953 18:00:46,000 --> 18:00:50,559 set up another one pedem and we're going 23954 18:00:50,559 --> 18:00:54,159 this however many times you want and 23955 18:00:52,720 --> 18:00:57,680 then we're going to set up the function 23956 18:00:54,160 --> 18:00:59,360 which is going to do a multiplication so 23957 18:00:57,680 --> 18:01:01,116 we're going to say t that's going to be 23958 18:00:59,360 --> 18:01:03,116 the return value and we're going to say 23959 18:01:01,116 --> 18:01:05,680 multiply and we're going to multiply two 23960 18:01:03,116 --> 18:01:08,080 parameters we're going to say t a and t 23961 18:01:05,680 --> 18:01:11,040 b okay and we're going to put in the 23962 18:01:08,080 --> 18:01:12,880 logic and we're going to just return a 23963 18:01:12,879 --> 18:01:18,479 multiplication operator in c plus plus 23964 18:01:16,160 --> 18:01:21,116 and we can use this function so for 23965 18:01:18,480 --> 18:01:23,116 example we can say we can comment this 23966 18:01:21,116 --> 18:01:25,680 out and we can set up two variables 23967 18:01:23,116 --> 18:01:27,436 we're going to say and x and we're going 23968 18:01:25,680 --> 18:01:28,796 to put in whatever we had before let's 23969 18:01:28,796 --> 18:01:33,360 and end to y we're going to say 7 i 23970 18:01:31,680 --> 18:01:35,360 think this is what we used before you 23971 18:01:33,360 --> 18:01:37,199 can really put in whatever you want and 23972 18:01:35,360 --> 18:01:39,199 now we're going to call the multiply 23973 18:01:37,199 --> 18:01:41,755 function template so what this is going 23974 18:01:39,199 --> 18:01:44,159 to do is to generate a function template 23975 18:01:41,756 --> 18:01:46,796 instance which is going to be of type 23976 18:01:44,160 --> 18:01:49,116 and it's going to do a replacement in 23977 18:01:46,796 --> 18:01:51,040 here and replace t with ant and then 23978 18:01:49,116 --> 18:01:52,560 we're going to multiply two end 23979 18:01:51,040 --> 18:01:55,116 parameters that are passed to this 23980 18:01:52,559 --> 18:01:57,360 function and we're going to return end 23981 18:01:55,116 --> 18:01:59,276 and if we do that we're going to get 35 23982 18:01:57,360 --> 18:02:02,080 printed out let's prove this we're going 23983 18:01:59,275 --> 18:02:05,756 to use gcc to bolt the bolt is good we 23984 18:02:02,080 --> 18:02:07,680 can clear and run rooster we get a 35 if 23985 18:02:05,756 --> 18:02:10,319 we change this to double it is going to 23986 18:02:07,680 --> 18:02:13,879 work so let's change this to double and 23987 18:02:10,319 --> 18:02:16,796 maybe put in five point one and 23988 18:02:13,879 --> 18:02:18,795 7.0 why not and then we're going to see 23989 18:02:16,796 --> 18:02:21,680 what we get we're going to build with 23990 18:02:18,796 --> 18:02:23,000 gcc the build is good we can clear run 23991 18:02:26,000 --> 18:02:33,436 but watch what happens if we try 23992 18:02:29,275 --> 18:02:35,839 to use the std string types okay i want 23993 18:02:33,436 --> 18:02:38,955 you to see that so we're going to enable 23994 18:02:35,839 --> 18:02:41,199 our std strength variables here 23995 18:02:38,955 --> 18:02:43,519 and if we try to bolt we're going to get 23996 18:02:41,199 --> 18:02:45,519 a compiler error but i want you to try 23997 18:02:43,519 --> 18:02:48,399 and guess why why do we have this 23998 18:02:45,519 --> 18:02:49,680 compiler if we look at what gcc is 23999 18:02:49,680 --> 18:02:56,160 it is saying an instantiation of our 24000 18:02:52,955 --> 18:02:58,559 function template required from here and 24001 18:02:58,559 --> 18:03:02,239 multiplication here and this is the 24002 18:03:02,239 --> 18:03:06,479 the compiler is going to do what it 24003 18:03:04,400 --> 18:03:09,360 knows to do it is going to do a 24004 18:03:06,480 --> 18:03:11,436 replacement in our function template and 24005 18:03:11,436 --> 18:03:16,400 by std strength because that's the type 24006 18:03:13,839 --> 18:03:19,359 we are using as an argument here the 24007 18:03:16,400 --> 18:03:21,596 problem is we can't really multiply two 24008 18:03:19,360 --> 18:03:23,756 strands if you try to multiply two 24009 18:03:21,595 --> 18:03:26,479 strands like we do here it's not going 24010 18:03:23,756 --> 18:03:28,635 to make sense what do you expect to get 24011 18:03:26,480 --> 18:03:30,400 if you multiply two strings it doesn't 24012 18:03:28,635 --> 18:03:32,319 make sense the compiler doesn't know 24013 18:03:30,400 --> 18:03:34,319 what to do with this operation it's 24014 18:03:32,319 --> 18:03:36,879 going to give you a compiler error okay 24015 18:03:34,319 --> 18:03:39,275 to really drive this home let's go back 24016 18:03:39,275 --> 18:03:43,839 and put in the code here and see what is 24017 18:03:43,839 --> 18:03:48,000 you should use these tools to your 24018 18:03:45,756 --> 18:03:50,480 advantage because they really help in 24019 18:03:48,000 --> 18:03:52,400 understanding what is going on so let's 24020 18:03:50,480 --> 18:03:54,240 hit the play button here i just put in 24021 18:03:52,400 --> 18:03:57,040 my code and it is going to say 24022 18:03:54,239 --> 18:04:00,239 compilation failed because we have the 24023 18:03:57,040 --> 18:04:01,915 same error cpp insights needs to compile 24024 18:04:01,915 --> 18:04:07,435 to give you the template instantiations 24025 18:04:05,116 --> 18:04:10,240 but here you see that it is basically 24026 18:04:07,436 --> 18:04:12,955 giving us the same error let's try and 24027 18:04:10,239 --> 18:04:15,680 see the entire thing here and see if we 24028 18:04:12,955 --> 18:04:17,519 see the real error and we can't really 24029 18:04:15,680 --> 18:04:20,000 scroll around with this i'm having 24030 18:04:17,519 --> 18:04:22,955 trouble scrolling around here i can use 24031 18:04:20,000 --> 18:04:25,595 these buttons i guess okay so let's see 24032 18:04:22,955 --> 18:04:28,080 if we see the actual error and we're not 24033 18:04:25,595 --> 18:04:30,159 seeing it here auto result multiply 24034 18:04:28,080 --> 18:04:32,319 error generated so we're going to get an 24035 18:04:30,160 --> 18:04:34,880 error if we try to instantiate our 24036 18:04:32,319 --> 18:04:37,436 template with the types that we are 24037 18:04:34,879 --> 18:04:40,479 passing in here which are hdd strength 24038 18:04:37,436 --> 18:04:42,635 cpp insights isn't really helpful here 24039 18:04:40,480 --> 18:04:45,116 but hopefully you understand the problem 24040 18:04:42,635 --> 18:04:49,040 the problem is if the compiler tries to 24041 18:04:45,116 --> 18:04:51,756 generate a template instance using std 24042 18:04:49,040 --> 18:04:53,520 strand multiplying two strands isn't 24043 18:04:51,756 --> 18:04:55,595 going to make sense so the compiler is 24044 18:04:53,519 --> 18:04:57,915 going to give you a compiler error 24045 18:04:55,595 --> 18:05:00,159 because this is not supported so the 24046 18:04:57,915 --> 18:05:02,319 message here is that you should make 24047 18:05:00,160 --> 18:05:04,796 sure that the template instances 24048 18:05:02,319 --> 18:05:08,000 generated by the compilers support 24049 18:05:04,796 --> 18:05:10,160 whatever operations you are doing in the 24050 18:05:08,000 --> 18:05:11,839 body of your function template this is 24051 18:05:11,839 --> 18:05:15,595 another thing i want you to see before 24052 18:05:13,360 --> 18:05:16,400 we close this off i'm going to take out 24053 18:05:16,400 --> 18:05:21,200 problematic line here and we're going to 24054 18:05:18,400 --> 18:05:23,275 go back to our maximum function what i 24055 18:05:21,199 --> 18:05:25,360 want you to see is that if you pass 24056 18:05:23,275 --> 18:05:27,680 different types for this function 24057 18:05:25,360 --> 18:05:30,000 template you're going to get a compiler 24058 18:05:27,680 --> 18:05:32,559 error and what do i mean by that let's 24059 18:05:30,000 --> 18:05:35,680 take out our std strength because it's 24060 18:05:32,559 --> 18:05:38,795 no longer useful for our purposes here 24061 18:05:35,680 --> 18:05:42,239 and we're going to just use double and 8 24062 18:05:38,796 --> 18:05:43,520 parameters let's use ant here 24063 18:05:46,080 --> 18:05:50,319 and then we call our maximum function 24064 18:05:48,480 --> 18:05:52,796 you see that we have a squiggly line 24065 18:05:50,319 --> 18:05:56,000 because the function template is forced 24066 18:05:52,796 --> 18:05:57,596 to use the same type for the return type 24067 18:05:56,000 --> 18:06:00,160 the first parameter and the second 24068 18:05:57,595 --> 18:06:02,795 parameter here because the placeholder 24069 18:06:00,160 --> 18:06:04,956 is the same so the compiler is supposed 24070 18:06:02,796 --> 18:06:07,520 to use the same thing for the first 24071 18:06:04,955 --> 18:06:09,519 argument here the second argument and 24072 18:06:07,519 --> 18:06:11,680 the return type that we get from this 24073 18:06:09,519 --> 18:06:13,519 maximum function here but the problem is 24074 18:06:11,680 --> 18:06:15,595 we are passing in parameters of 24075 18:06:13,519 --> 18:06:17,359 different types and this is going to 24076 18:06:15,595 --> 18:06:20,399 give us a compiler error because the 24077 18:06:17,360 --> 18:06:23,520 compiler want to know what to do with 24078 18:06:20,400 --> 18:06:26,635 the types that you pass in as arguments 24079 18:06:23,519 --> 18:06:29,115 and if we look at our problems tab here 24080 18:06:26,635 --> 18:06:31,839 visual studio code should give us an 24081 18:06:29,116 --> 18:06:33,840 error no match for operator uh this is 24082 18:06:31,839 --> 18:06:35,275 the error we had before we can try and 24083 18:06:33,839 --> 18:06:38,319 build again we're going to get a 24084 18:06:35,275 --> 18:06:39,756 compiler error again but this time it's 24085 18:06:38,319 --> 18:06:43,275 going to be different it's going to say 24086 18:06:39,756 --> 18:06:45,595 no instance of function template maximum 24087 18:06:43,275 --> 18:06:48,079 matches the argument list that we are 24088 18:06:45,595 --> 18:06:48,955 using here which is double and end 24089 18:06:48,955 --> 18:06:52,955 this is a problem but we will see ways 24090 18:06:51,360 --> 18:06:55,595 around this as we progress in the 24091 18:06:52,955 --> 18:06:57,839 chapter but what you should know 24092 18:06:55,595 --> 18:07:00,319 is that if we set up a function template 24093 18:06:57,839 --> 18:07:03,039 like this this is going to enforce for 24094 18:07:00,319 --> 18:07:05,595 all the types where we will place in t 24095 18:07:03,040 --> 18:07:07,595 to be the same if they're not the same 24096 18:07:05,595 --> 18:07:09,519 the compiler won't know how to do its 24097 18:07:07,595 --> 18:07:11,595 replacement and it is going to give you 24098 18:07:09,519 --> 18:07:14,159 a compiler error okay the last thing i 24099 18:07:11,595 --> 18:07:17,435 want you to see is that we can use the 24100 18:07:14,160 --> 18:07:19,596 debugger to debug function templates so 24101 18:07:17,436 --> 18:07:21,436 let's turn this back to end because we 24102 18:07:21,436 --> 18:07:27,596 so let's take out the decimal point here 24103 18:07:24,559 --> 18:07:30,079 so that we have proper intellectuals and 24104 18:07:27,595 --> 18:07:32,635 if we compile again this should compile 24105 18:07:30,080 --> 18:07:36,160 okay the world is good now what really 24106 18:07:32,635 --> 18:07:36,955 happens if you try to debug a program 24107 18:07:38,480 --> 18:07:43,360 are you going to be working on the 24108 18:07:40,720 --> 18:07:46,160 template instances because remember the 24109 18:07:43,360 --> 18:07:48,480 function templates are going to generate 24110 18:07:46,160 --> 18:07:50,880 function template instances with actual 24111 18:07:48,480 --> 18:07:53,116 types for example our template instance 24112 18:07:50,879 --> 18:07:56,955 here is going to have ants because we 24113 18:07:53,116 --> 18:07:59,116 are passing ant as argument here so how 24114 18:07:56,955 --> 18:08:02,080 are we exactly going to debug this let's 24115 18:07:59,116 --> 18:08:03,360 try this we're going to set up a break 24116 18:08:03,360 --> 18:08:08,080 on the call to our maximum function 24117 18:08:06,239 --> 18:08:10,000 and we're going to start debugging we're 24118 18:08:11,839 --> 18:08:16,239 and we're going to use the debugging 24119 18:08:13,519 --> 18:08:18,795 tools that come with gcc we have seen 24120 18:08:16,239 --> 18:08:20,559 how to do that in the last chapter and 24121 18:08:18,796 --> 18:08:23,596 we're going to start debugging let's 24122 18:08:20,559 --> 18:08:25,275 minimize this guy here so that we have 24123 18:08:25,275 --> 18:08:30,879 and we're going to run our debugger 24124 18:08:28,160 --> 18:08:33,520 let's maximize this a little bit 24125 18:08:30,879 --> 18:08:35,199 and push this a little to the left 24126 18:08:33,519 --> 18:08:37,519 and we're going to start debugging and 24127 18:08:35,199 --> 18:08:39,275 see what really happens here so we hit 24128 18:08:37,519 --> 18:08:41,839 the green button here we're going to 24129 18:08:39,275 --> 18:08:43,275 start debugging our code is building now 24130 18:08:43,275 --> 18:08:48,239 the binary has been passed to the 24131 18:08:45,680 --> 18:08:50,796 debugger and now we have hit our break 24132 18:08:48,239 --> 18:08:53,115 point here so let's minimize this a 24133 18:08:50,796 --> 18:08:55,680 little bit so that we have an easier way 24134 18:08:53,116 --> 18:08:57,360 to see this we have hit or break point 24135 18:08:55,680 --> 18:08:59,595 you see the locals are what we have in 24136 18:08:57,360 --> 18:09:01,915 the main function which happens to be to 24137 18:08:59,595 --> 18:09:03,595 the top of the call stack here 24138 18:09:01,915 --> 18:09:05,680 okay now we want to jump into the 24139 18:09:03,595 --> 18:09:07,275 maximum function and see the types that 24140 18:09:07,275 --> 18:09:12,635 so what we want to do here is head the 24141 18:09:09,595 --> 18:09:14,795 step into function and you notice it is 24142 18:09:12,635 --> 18:09:17,756 going to jump into our function template 24143 18:09:14,796 --> 18:09:20,160 here but the locals here are not giving 24144 18:09:17,756 --> 18:09:22,239 us the types and this is because this is 24145 18:09:20,160 --> 18:09:24,320 a function template it's not going to 24146 18:09:24,319 --> 18:09:30,159 because it's not easy to get those here 24147 18:09:27,199 --> 18:09:32,399 but if we look at the call stack you see 24148 18:09:30,160 --> 18:09:34,160 that we have information about the types 24149 18:09:32,400 --> 18:09:36,560 of the function that is being called we 24150 18:09:36,559 --> 18:09:40,635 and it is going to return and you see 24151 18:09:38,480 --> 18:09:42,480 that in the angle brackets here and 24152 18:09:40,635 --> 18:09:45,436 again this is possibly another way to 24153 18:09:42,480 --> 18:09:47,596 look at what template instances were 24154 18:09:45,436 --> 18:09:49,520 generated by the compiler because we see 24155 18:09:47,595 --> 18:09:52,319 the function called here is taken into 24156 18:09:49,519 --> 18:09:54,879 parameters and it is going to return end 24157 18:09:52,319 --> 18:09:57,116 let's kill our debugger because this is 24158 18:09:54,879 --> 18:09:59,199 really all we want to see and change the 24159 18:09:57,116 --> 18:10:01,596 types to double here and we're going to 24160 18:09:59,199 --> 18:10:03,519 see that the function to the top of the 24161 18:10:03,519 --> 18:10:09,435 taking double parameters so let's change 24162 18:10:06,239 --> 18:10:13,360 this back to five point something 5.5 24163 18:10:09,436 --> 18:10:15,275 maybe 7 and 0.9 why not and we're going 24164 18:10:15,275 --> 18:10:21,436 but uh what we really are interested in 24165 18:10:17,756 --> 18:10:23,360 is seeing the function template instance 24166 18:10:21,436 --> 18:10:25,200 generated by the compiler because of 24167 18:10:23,360 --> 18:10:27,116 this call here and we can do this 24168 18:10:25,199 --> 18:10:29,039 through the debugger this is a little 24169 18:10:27,116 --> 18:10:31,276 bit convoluted because you have to set 24170 18:10:29,040 --> 18:10:33,275 up breakpoints and whatever but it is 24171 18:10:31,275 --> 18:10:36,319 helpful if you don't want to use things 24172 18:10:33,275 --> 18:10:38,319 like cpp insights as we have seen before 24173 18:10:36,319 --> 18:10:40,400 and again this is also useful because 24174 18:10:38,319 --> 18:10:42,480 you will need to debug your code if it 24175 18:10:40,400 --> 18:10:45,275 is using function templates let's run 24176 18:10:42,480 --> 18:10:47,276 this to really prove our point here 24177 18:10:45,275 --> 18:10:49,360 the world is going to go through the 24178 18:10:47,275 --> 18:10:50,239 binary executable is going to be passed 24179 18:10:50,239 --> 18:10:55,040 the debugger and we're going to hit our 24180 18:10:52,080 --> 18:10:57,680 breakpoint here again you see our locals 24181 18:10:55,040 --> 18:10:59,200 are not giving us the types here but we 24182 18:10:59,199 --> 18:11:03,435 the function call stack to really see 24183 18:11:01,116 --> 18:11:06,400 this we have hit the breakpoint here at 24184 18:11:03,436 --> 18:11:09,360 line 15 so we can step into the call 24185 18:11:06,400 --> 18:11:12,880 and if we hit the body of the maximum 24186 18:11:09,360 --> 18:11:15,520 function here and look at the call stack 24187 18:11:12,879 --> 18:11:18,879 we're going to see that the parameters 24188 18:11:15,519 --> 18:11:21,115 we have in our function call are of 24189 18:11:18,879 --> 18:11:22,719 double type and again this hopefully 24190 18:11:22,720 --> 18:11:26,955 that the compiler has actually generated 24191 18:11:26,955 --> 18:11:31,680 function template instance okay since we 24192 18:11:29,756 --> 18:11:35,116 are debugging we can actually try and 24193 18:11:31,680 --> 18:11:37,199 follow this program let's hit next so 24194 18:11:35,116 --> 18:11:38,400 that we can hit the end of this function 24195 18:11:38,400 --> 18:11:43,200 finish it and hit the main function by 24196 18:11:40,955 --> 18:11:46,000 the moment we hit the main function 24197 18:11:43,199 --> 18:11:47,519 results is going to be 7.9 which is what 24198 18:11:46,000 --> 18:11:49,756 we have here and it is what we're going 24199 18:11:47,519 --> 18:11:51,519 to print we can just step over this 24200 18:11:49,756 --> 18:11:54,400 because we don't want to step into the 24201 18:11:51,519 --> 18:11:56,239 logics of sddc out so step over we're 24202 18:11:54,400 --> 18:11:58,400 going to print our result and our 24203 18:11:56,239 --> 18:12:01,040 debugging session is going to basically 24204 18:11:58,400 --> 18:12:03,520 end because we have hit the end of the 24205 18:12:01,040 --> 18:12:06,160 main function let's try and use the 24206 18:12:03,519 --> 18:12:08,479 compiler from microsoft to see the 24207 18:12:06,160 --> 18:12:10,480 information it gives us because these 24208 18:12:08,480 --> 18:12:12,796 things can be different depending on the 24209 18:12:10,480 --> 18:12:15,040 compiler and the debugging tools that 24210 18:12:12,796 --> 18:12:17,835 you are using we have the luxury to have 24211 18:12:15,040 --> 18:12:19,755 these two set up here so all we have to 24212 18:12:17,835 --> 18:12:22,243 do is switch the tool we want to use 24213 18:12:19,755 --> 18:12:24,720 here and this is going to be using the 24214 18:12:22,244 --> 18:12:27,436 one from microsoft if you are not on 24215 18:12:24,720 --> 18:12:30,244 windows or you don't want to use the 24216 18:12:27,436 --> 18:12:33,115 compiler from microsoft and its 24217 18:12:30,244 --> 18:12:35,360 debugging tools you can just use gcc and 24218 18:12:33,115 --> 18:12:37,115 its debugging tools here i just want you 24219 18:12:35,360 --> 18:12:39,436 to see different ways to do the same 24220 18:12:37,115 --> 18:12:42,080 things and that compilers and debuggers 24221 18:12:39,436 --> 18:12:44,000 can give us different information we're 24222 18:12:42,080 --> 18:12:46,000 going to try and debug with this so 24223 18:12:44,000 --> 18:12:47,600 we're going to hit the green button here 24224 18:12:46,000 --> 18:12:50,639 notice that this is going to be using 24225 18:12:47,600 --> 18:12:53,116 the compiler from microsoft if we look 24226 18:12:50,639 --> 18:12:55,600 at the world output here you see that we 24227 18:12:55,600 --> 18:13:00,080 it is what we are using to build our 24228 18:12:57,755 --> 18:13:02,155 binary the binary is going to be passed 24229 18:13:00,080 --> 18:13:05,516 to the debugger and then we're going to 24230 18:13:02,156 --> 18:13:07,600 hit the breakpoint here as we know 24231 18:13:05,516 --> 18:13:10,000 and then we're going to have our output 24232 18:13:10,000 --> 18:13:15,279 you see in the main function we don't 24233 18:13:12,475 --> 18:13:17,599 see the types of our locals why is that 24234 18:13:15,279 --> 18:13:19,919 i don't know why but we see the values 24235 18:13:19,919 --> 18:13:25,755 and if we hit the step into button to 24236 18:13:23,039 --> 18:13:27,515 jump into our maximum function a is 24237 18:13:25,755 --> 18:13:30,475 going to have this value b is going to 24238 18:13:27,516 --> 18:13:32,244 have this value and we look at the 24239 18:13:30,475 --> 18:13:34,796 template instance we can see the same 24240 18:13:32,244 --> 18:13:37,196 information but we are using a different 24241 18:13:34,796 --> 18:13:40,080 compiler this is really how you can 24242 18:13:37,195 --> 18:13:42,555 debug your code that uses function 24243 18:13:40,080 --> 18:13:46,000 templates and this is going to give us a 24244 18:13:42,555 --> 18:13:49,195 peek into the actual template instance 24245 18:13:46,000 --> 18:13:51,360 that was generated by our compiler this 24246 18:13:49,195 --> 18:13:53,436 is really all i had to share in this 24247 18:13:51,360 --> 18:13:55,516 lecture and before i let you go let's 24248 18:13:53,436 --> 18:13:57,360 summarize a little bit function 24249 18:13:55,516 --> 18:13:59,279 templates are blueprints we have seen 24250 18:13:57,360 --> 18:14:01,600 that the compiler is going to use those 24251 18:13:59,279 --> 18:14:04,399 to generate actual function template 24252 18:14:01,600 --> 18:14:05,116 instances real function definitions and 24253 18:14:05,115 --> 18:14:08,875 declarations are going to be created 24254 18:14:07,195 --> 18:14:11,516 when you call the function with 24255 18:14:08,876 --> 18:14:14,555 arguments if the template parameters are 24256 18:14:11,516 --> 18:14:16,476 of the same type maybe t and t then the 24257 18:14:14,555 --> 18:14:18,399 arguments you call the function with 24258 18:14:16,475 --> 18:14:20,555 must also match otherwise you'll get a 24259 18:14:18,399 --> 18:14:22,875 compiler error we have seen this okay 24260 18:14:20,555 --> 18:14:25,115 here we have a point we haven't already 24261 18:14:22,876 --> 18:14:27,360 covered template instances won't always 24262 18:14:25,115 --> 18:14:29,195 do what you want a good example is when 24263 18:14:27,360 --> 18:14:31,436 you call our maximum function with 24264 18:14:29,195 --> 18:14:32,243 pointers so let's go back and really say 24265 18:14:32,244 --> 18:14:37,360 because i want this to cover as much as 24266 18:14:34,796 --> 18:14:39,600 possible about function templates 24267 18:14:37,360 --> 18:14:41,436 so let's go back to our code and we're 24268 18:14:39,600 --> 18:14:42,960 going to modify things a little bit okay 24269 18:14:41,436 --> 18:14:44,400 so what we're going to do here is take 24270 18:14:42,960 --> 18:14:47,280 out whatever we have here we're going to 24271 18:14:47,279 --> 18:14:51,279 x is going to be 5 as we have seen 24272 18:14:51,279 --> 18:14:55,516 y is going to be seven let's use this 24273 18:14:54,320 --> 18:14:57,116 and then we're going to set up two 24274 18:14:55,516 --> 18:14:58,400 pointer variables we're going to say 24275 18:14:58,399 --> 18:15:02,639 px and we're going to initialize this 24276 18:15:00,555 --> 18:15:04,639 with the address of x nothing 24277 18:15:02,639 --> 18:15:06,796 sophisticated so far and we're going to 24278 18:15:04,639 --> 18:15:09,516 say end p y and we're going to 24279 18:15:06,796 --> 18:15:11,756 initialize this with the address of y we 24280 18:15:09,516 --> 18:15:14,156 can do this the problem is going to come 24281 18:15:11,755 --> 18:15:15,435 when we try to call our function here 24282 18:15:15,436 --> 18:15:21,279 pointers let's use px and py to call 24283 18:15:18,475 --> 18:15:24,243 this function and py and what this is 24284 18:15:21,279 --> 18:15:26,475 going to do is to generate a template 24285 18:15:24,244 --> 18:15:28,080 instance that takes end pointer this is 24286 18:15:26,475 --> 18:15:30,000 what the compiler knows to do we're 24287 18:15:28,080 --> 18:15:32,796 going to take the types that we pass in 24288 18:15:30,000 --> 18:15:34,720 here which happen to be end pointer and 24289 18:15:32,796 --> 18:15:36,080 we're going to change the return type to 24290 18:15:34,720 --> 18:15:37,919 end pointer we're going to change the 24291 18:15:36,080 --> 18:15:39,835 first parameter to end pointer the 24292 18:15:37,919 --> 18:15:41,919 second parameter to end pointer and 24293 18:15:39,835 --> 18:15:43,679 we're going to compute the maximum 24294 18:15:41,919 --> 18:15:46,156 if we compute the maximum here we're 24295 18:15:43,679 --> 18:15:48,875 going to be comparing pointers okay 24296 18:15:46,156 --> 18:15:51,756 notice this and if we try to do that we 24297 18:15:48,876 --> 18:15:55,279 might not get what we want because what 24298 18:15:51,755 --> 18:15:57,360 we get now depends on the address used 24299 18:15:55,279 --> 18:16:00,156 to store these guys in memory hopefully 24300 18:15:57,360 --> 18:16:02,475 you can see that and let's try and debug 24301 18:16:00,156 --> 18:16:06,876 this program so that you can really see 24302 18:16:02,475 --> 18:16:09,039 this we're going to use the tools from g 24303 18:16:09,039 --> 18:16:13,279 and we're going to try and debug it 24304 18:16:10,475 --> 18:16:15,115 let's do that it is going to run through 24305 18:16:13,279 --> 18:16:18,000 the debugger and what we're going to 24306 18:16:15,115 --> 18:16:20,555 have is our terminal here i think this 24307 18:16:18,000 --> 18:16:23,195 is not needed anymore so let's put this 24308 18:16:20,555 --> 18:16:27,195 to the right here and minimize a little 24309 18:16:23,195 --> 18:16:30,243 bit and now you notice that px contains 24310 18:16:27,195 --> 18:16:32,243 an address and py contains an address 24311 18:16:30,244 --> 18:16:34,000 hopefully you can see that in the body 24312 18:16:32,244 --> 18:16:36,720 of our template instance we're going to 24313 18:16:34,000 --> 18:16:38,796 be comparing these addresses because 24314 18:16:36,720 --> 18:16:40,796 we're comparing what we pass by value 24315 18:16:38,796 --> 18:16:43,919 here and what we pass are these two 24316 18:16:40,796 --> 18:16:46,080 addresses so whichever is the maximum in 24317 18:16:43,919 --> 18:16:49,599 these guys and i think that's going to 24318 18:16:46,080 --> 18:16:52,244 be dd4 here it is the maximum so 24319 18:16:49,600 --> 18:16:55,279 whatever is contained in px is going to 24320 18:16:52,244 --> 18:16:56,639 be considered the maximum let's kill 24321 18:16:55,279 --> 18:16:59,436 this because we have seen this 24322 18:16:56,639 --> 18:17:01,755 information so if we have a value in px 24323 18:16:59,436 --> 18:17:03,600 and that happens to be at a bigger 24324 18:17:01,755 --> 18:17:05,679 address it's going to be considered the 24325 18:17:03,600 --> 18:17:07,360 maximum let's see what we print if we 24326 18:17:05,679 --> 18:17:10,875 run this program we're going to build 24327 18:17:07,360 --> 18:17:12,475 with gcc let's do that the world is good 24328 18:17:12,475 --> 18:17:17,919 and run rooster now you see result is 24329 18:17:15,516 --> 18:17:20,000 some address because we are returning a 24330 18:17:17,919 --> 18:17:22,399 pointer and printing that out but we can 24331 18:17:20,000 --> 18:17:25,279 easily dereference this and get to the 24332 18:17:22,399 --> 18:17:28,243 value inside this pointer let's build 24333 18:17:25,279 --> 18:17:30,796 again we can clear and run rooster this 24334 18:17:28,244 --> 18:17:32,244 is going to give us result is 5 and 24335 18:17:30,796 --> 18:17:35,600 hopefully you can see that this is 24336 18:17:32,244 --> 18:17:38,400 really bad and not correct we are trying 24337 18:17:35,600 --> 18:17:41,600 to compute the maximum between x and y 24338 18:17:38,399 --> 18:17:44,555 and we are seeing that the maximum is 24339 18:17:41,600 --> 18:17:46,960 five so how can five beat the maximum 24340 18:17:44,555 --> 18:17:49,360 between five and seven the problem is 24341 18:17:46,960 --> 18:17:52,556 we're not comparing the values here we 24342 18:17:49,360 --> 18:17:54,244 are comparing the addresses stored in 24343 18:17:52,555 --> 18:17:55,679 these pointers because that's what we 24344 18:17:54,244 --> 18:17:57,836 are passing here hopefully you can 24345 18:17:55,679 --> 18:18:00,795 really see this and we can prove this 24346 18:17:57,835 --> 18:18:03,279 again through our debugger we can run 24347 18:18:00,796 --> 18:18:05,600 this through the debugger we're going to 24348 18:18:03,279 --> 18:18:07,679 hit the breakpoint let's minimize this 24349 18:18:05,600 --> 18:18:09,680 so that we can see our terminal window 24350 18:18:07,679 --> 18:18:11,835 here and we're going to move this around 24351 18:18:09,679 --> 18:18:14,719 a little bit and now we have hit our 24352 18:18:11,835 --> 18:18:16,796 break point if we hit next to jump into 24353 18:18:14,720 --> 18:18:20,000 the function you see that what we are 24354 18:18:16,796 --> 18:18:22,720 really comparing are a and b here and a 24355 18:18:20,000 --> 18:18:25,115 contains a much larger address so it is 24356 18:18:22,720 --> 18:18:27,516 the pointer that is going to be returned 24357 18:18:25,115 --> 18:18:29,679 and if we dereference that we're going 24358 18:18:27,516 --> 18:18:32,639 to be dereferencing the pointer that 24359 18:18:29,679 --> 18:18:35,435 contains this x value and the message 24360 18:18:32,639 --> 18:18:37,195 here is that you should be careful 24361 18:18:35,436 --> 18:18:39,279 that your function templates are really 24362 18:18:37,195 --> 18:18:41,599 what doing what you want somebody might 24363 18:18:39,279 --> 18:18:43,679 do this if they are not careful just 24364 18:18:41,600 --> 18:18:46,156 pass in pointer and expect to get the 24365 18:18:43,679 --> 18:18:49,039 maximum but the compiler is just going 24366 18:18:46,156 --> 18:18:50,960 to compare pointer addresses and you're 24367 18:18:49,039 --> 18:18:53,360 going to get something you probably 24368 18:18:50,960 --> 18:18:55,756 don't expect okay this is what is meant 24369 18:18:53,360 --> 18:18:57,600 by template instances won't always do 24370 18:18:55,755 --> 18:18:59,835 what you want a good example is when you 24371 18:18:57,600 --> 18:19:01,756 call a maximum function with pointers 24372 18:18:59,835 --> 18:19:04,000 you're going to get a wrong value 24373 18:19:01,755 --> 18:19:05,919 possibly there are tools like cpp 24374 18:19:04,000 --> 18:19:07,755 insights which can show the template in 24375 18:19:05,919 --> 18:19:10,319 spatial instruments we have seen that 24376 18:19:07,755 --> 18:19:12,155 and again the arguments passed to a 24377 18:19:10,320 --> 18:19:14,796 function template must support the 24378 18:19:12,156 --> 18:19:16,555 operations that you are doing in your 24379 18:19:14,796 --> 18:19:18,244 function template we have seen an 24380 18:19:16,555 --> 18:19:20,399 example of this by setting up a 24381 18:19:18,244 --> 18:19:22,720 multiplication function template and we 24382 18:19:20,399 --> 18:19:25,435 saw that we had a compiler error if we 24383 18:19:22,720 --> 18:19:27,755 try to pass std strength arguments 24384 18:19:25,436 --> 18:19:30,000 because we can't multiply two strings 24385 18:19:27,755 --> 18:19:32,399 this is really all i had to share in 24386 18:19:30,000 --> 18:19:35,039 this lecture i apologize it turned out 24387 18:19:32,399 --> 18:19:37,360 to be lengthy but i really had to put 24388 18:19:35,039 --> 18:19:39,679 this in one lecture so that i can really 24389 18:19:37,360 --> 18:19:42,080 show you different sides of the same 24390 18:19:39,679 --> 18:19:43,755 things we are going to stop here in this 24391 18:19:42,080 --> 18:19:46,960 lecture and the next one we're going to 24392 18:19:43,755 --> 18:19:49,115 learn about template type deduction and 24393 18:19:49,115 --> 18:19:52,639 go ahead and finish up here 24394 18:19:52,639 --> 18:19:56,639 in this lecture we're going to learn 24395 18:19:53,919 --> 18:19:59,679 about template type deduction and 24396 18:19:56,639 --> 18:20:02,960 explicit arguments what we mean by 24397 18:19:59,679 --> 18:20:04,959 template type deduction is the mechanism 24398 18:20:04,960 --> 18:20:10,476 to deduce the type it would use to 24399 18:20:08,080 --> 18:20:12,796 set up our template instance from the 24400 18:20:10,475 --> 18:20:14,243 arguments that we passed to our function 24401 18:20:14,244 --> 18:20:18,080 for example the compiler is going to 24402 18:20:15,755 --> 18:20:20,243 look at a and b here in a call to the 24403 18:20:18,080 --> 18:20:22,400 maximum function and it is going to 24404 18:20:20,244 --> 18:20:24,720 notice that they are of into type and 24405 18:20:22,399 --> 18:20:27,279 then it is going to say okay you are 24406 18:20:24,720 --> 18:20:30,156 calling your function with it types and 24407 18:20:27,279 --> 18:20:33,436 i see that your function templates takes 24408 18:20:30,156 --> 18:20:36,080 three replacers in here so i am going to 24409 18:20:33,436 --> 18:20:38,080 replace these guys with and and it is 24410 18:20:36,080 --> 18:20:40,639 going to guess that from the call that 24411 18:20:38,080 --> 18:20:42,796 you make here there's no other mechanism 24412 18:20:40,639 --> 18:20:45,516 to help the compiler out this is really 24413 18:20:42,796 --> 18:20:47,360 what template type deduction is about 24414 18:20:45,516 --> 18:20:50,000 and for the first example here we are 24415 18:20:50,000 --> 18:20:55,195 nt is going to be deduced for the second 24416 18:20:52,080 --> 18:20:57,436 function call we are passing in double 24417 18:20:55,195 --> 18:20:59,679 parameter so a double function template 24418 18:20:57,436 --> 18:21:02,400 instance is going to be generated for 24419 18:20:59,679 --> 18:21:04,155 the third call here we are using std 24420 18:21:04,156 --> 18:21:10,080 so we are going to deduce a template 24421 18:21:07,279 --> 18:21:12,475 instance that uses hdd string here this 24422 18:21:10,080 --> 18:21:14,555 is what we mean by template type 24423 18:21:12,475 --> 18:21:17,279 deduction but we have seen that 24424 18:21:14,555 --> 18:21:19,755 automatic template type deduction can 24425 18:21:17,279 --> 18:21:22,555 really have problems especially if you 24426 18:21:19,755 --> 18:21:24,720 pass parameters of different types for 24427 18:21:22,555 --> 18:21:26,319 example if we call a maximum function 24428 18:21:26,320 --> 18:21:31,040 a and c which happen to be end and 24429 18:21:29,195 --> 18:21:33,436 double type then we're going to have a 24430 18:21:31,039 --> 18:21:34,795 problem because this function template 24431 18:21:34,796 --> 18:21:40,080 parameters with different types because 24432 18:21:37,516 --> 18:21:42,876 the replacer is the same the compiler is 24433 18:21:40,080 --> 18:21:45,360 going to enforce that all the template 24434 18:21:42,876 --> 18:21:47,116 arguments are of the same type and the 24435 18:21:45,360 --> 18:21:49,516 template arguments are going to be what 24436 18:21:47,115 --> 18:21:51,835 you pass in here so a and b must be the 24437 18:21:49,516 --> 18:21:53,836 same if they happen to be not the same 24438 18:21:51,835 --> 18:21:55,515 for example ant and double you're going 24439 18:21:55,516 --> 18:22:00,720 now we're going to see that we can use 24440 18:21:57,436 --> 18:22:02,555 explicit template arguments and again i 24441 18:22:00,720 --> 18:22:05,360 want to make it very clear the 24442 18:22:02,555 --> 18:22:07,755 difference between a template parameter 24443 18:22:05,360 --> 18:22:09,755 and the template argument a template 24444 18:22:07,755 --> 18:22:12,155 parameter is what you have in your 24445 18:22:09,755 --> 18:22:15,039 function declaration or a definition so 24446 18:22:12,156 --> 18:22:16,476 what we have in our function setup here 24447 18:22:16,475 --> 18:22:22,796 parameters ta here and tb are template 24448 18:22:20,000 --> 18:22:25,360 parameters but what we have when we call 24449 18:22:22,796 --> 18:22:28,475 a function template is going to be 24450 18:22:25,360 --> 18:22:29,360 template arguments for example c and d 24451 18:22:29,360 --> 18:22:34,080 are template arguments and we're going 24452 18:22:31,600 --> 18:22:36,720 to be using this terminology in this 24453 18:22:34,080 --> 18:22:38,555 chapter and many more chapters to come 24454 18:22:36,720 --> 18:22:40,720 in this course make sure this is super 24455 18:22:38,555 --> 18:22:44,796 clear so what we're going to look at is 24456 18:22:40,720 --> 18:22:46,399 using explicit template arguments and 24457 18:22:46,399 --> 18:22:51,755 is a mechanism through which you can 24458 18:22:48,639 --> 18:22:53,919 force the compiler to use some type to 24459 18:22:51,755 --> 18:22:56,000 generate your template instance and the 24460 18:22:53,919 --> 18:22:58,639 syntax to do this is really simple it is 24461 18:22:56,000 --> 18:23:00,399 what we have here we say maximum and we 24462 18:23:00,399 --> 18:23:05,039 to use to generate our template instance 24463 18:23:03,115 --> 18:23:07,115 in angle brackets like we see here so 24464 18:23:05,039 --> 18:23:10,000 this is going to use double so this is 24465 18:23:07,115 --> 18:23:12,399 going to explicitly tell the compiler we 24466 18:23:10,000 --> 18:23:14,399 want the double version called and it's 24467 18:23:12,399 --> 18:23:16,555 going to generate that and that's what 24468 18:23:14,399 --> 18:23:18,959 is going to be called by this call we 24469 18:23:16,555 --> 18:23:21,436 have here a good benefit with this is 24470 18:23:18,960 --> 18:23:24,080 that now this is going to support 24471 18:23:21,436 --> 18:23:25,756 template arguments of different types so 24472 18:23:24,080 --> 18:23:27,756 if we make the call like this the 24473 18:23:25,755 --> 18:23:30,155 compiler is going to see that uh-huh we 24474 18:23:27,755 --> 18:23:33,679 are calling with intent double types 24475 18:23:30,156 --> 18:23:36,156 because a is ant and c is double 24476 18:23:33,679 --> 18:23:36,875 but it's going to see that you want 24477 18:23:36,876 --> 18:23:42,400 double template instance generated now 24478 18:23:39,919 --> 18:23:45,039 the compiler will have the possibility 24479 18:23:42,399 --> 18:23:47,755 to do implicit conversions from other 24480 18:23:45,039 --> 18:23:50,000 types to double and if this implicit 24481 18:23:47,755 --> 18:23:52,155 conversion fails then we're going to get 24482 18:23:50,000 --> 18:23:53,115 a compiler error so if you look here for 24483 18:23:53,115 --> 18:23:59,360 this is the example for that we are 24484 18:23:54,876 --> 18:24:02,000 passing in a and e and a is ant and e is 24485 18:23:59,360 --> 18:24:03,600 an std strength we cannot do an implicit 24486 18:24:03,600 --> 18:24:08,156 std strength to double so this is going 24487 18:24:06,156 --> 18:24:11,516 to give us a compiler error and this is 24488 18:24:08,156 --> 18:24:14,080 really all template type deduction and 24489 18:24:11,516 --> 18:24:15,836 explicit arguments are about we can use 24490 18:24:14,080 --> 18:24:18,639 these things to really make our code 24491 18:24:15,835 --> 18:24:20,156 easier now that you have this understood 24492 18:24:18,639 --> 18:24:21,679 we're going to head over to visual 24493 18:24:20,156 --> 18:24:24,156 studio code and play with this a little 24494 18:24:21,679 --> 18:24:26,243 more here we are in our working 24495 18:24:24,156 --> 18:24:29,116 directory the current project is 24496 18:24:26,244 --> 18:24:31,360 template type deductions and explicit 24497 18:24:29,115 --> 18:24:33,835 arguments we're going to grab our 24498 18:24:31,360 --> 18:24:36,080 template files pretty quick and put them 24499 18:24:33,835 --> 18:24:38,156 in place and after that we're going to 24500 18:24:36,080 --> 18:24:40,555 open this in visual studio code let's 24501 18:24:38,156 --> 18:24:42,720 drag and drop on top of visual studio 24502 18:24:40,555 --> 18:24:46,399 code here this is going to open our 24503 18:24:42,720 --> 18:24:47,436 folder and we can open our main cpp file 24504 18:24:47,436 --> 18:24:51,680 set up our function template let's clean 24505 18:24:49,835 --> 18:24:54,475 this up a little bit and we're going to 24506 18:24:51,679 --> 18:24:56,399 set up our function template let's type 24507 18:24:54,475 --> 18:24:58,639 this out to just to practice so we're 24508 18:24:56,399 --> 18:25:01,195 going to say template and we're going to 24509 18:24:58,639 --> 18:25:02,960 say type name and we're going to say t 24510 18:25:01,195 --> 18:25:05,755 and if you want you can put this on a 24511 18:25:02,960 --> 18:25:08,320 single line but i prefer to put this on 24512 18:25:05,755 --> 18:25:10,475 separate lines but let's show you that 24513 18:25:08,320 --> 18:25:12,320 so we're going to say t here and we're 24514 18:25:10,475 --> 18:25:14,399 going to say maximum and we're going to 24515 18:25:12,320 --> 18:25:17,756 say the parameters they are going to be 24516 18:25:14,399 --> 18:25:19,360 of type t and we can pass them by value 24517 18:25:17,755 --> 18:25:21,835 just for now and after this we're going 24518 18:25:19,360 --> 18:25:23,600 to go in the body of our function 24519 18:25:21,835 --> 18:25:26,720 template and then we can return 24520 18:25:23,600 --> 18:25:28,796 whichever parameter is the maximum we're 24521 18:25:26,720 --> 18:25:31,516 going to say a greater than b here we're 24522 18:25:28,796 --> 18:25:34,000 going to use our ternary operator and if 24523 18:25:31,516 --> 18:25:36,320 a is greater we're going to return a if 24524 18:25:34,000 --> 18:25:38,320 b is greater we're going to return b and 24525 18:25:36,320 --> 18:25:40,400 this is our function here okay now that 24526 18:25:38,320 --> 18:25:42,720 this function is set up let's set up a 24527 18:25:40,399 --> 18:25:44,959 few variables to really play with that 24528 18:25:42,720 --> 18:25:46,876 so we're going to include the string 24529 18:25:44,960 --> 18:25:49,116 library because we're going to be using 24530 18:25:46,876 --> 18:25:52,476 it a little bit and we're going to go 24531 18:25:49,115 --> 18:25:56,475 down in the main function and put in our 24532 18:25:52,475 --> 18:26:00,155 few variables we have a through f here a 24533 18:25:56,475 --> 18:26:04,155 and b are end c and d are of type double 24534 18:26:00,156 --> 18:26:06,244 and e and f are of type std strand the 24535 18:26:04,156 --> 18:26:09,195 first thing we can do we can say auto 24536 18:26:06,244 --> 18:26:11,920 max and we can assign whichever is the 24537 18:26:09,195 --> 18:26:13,755 maximum between a and b and once we do 24538 18:26:11,919 --> 18:26:16,796 this we're going to have that maximum 24539 18:26:13,755 --> 18:26:19,679 stored in max here but what i want you 24540 18:26:16,796 --> 18:26:22,639 to see is what the compiler does when it 24541 18:26:19,679 --> 18:26:25,115 sees the code like this the compiler has 24542 18:26:25,115 --> 18:26:30,475 to know the function template instance 24543 18:26:28,320 --> 18:26:33,040 it should really generate it is going to 24544 18:26:30,475 --> 18:26:34,875 look at the types we have here a and b 24545 18:26:33,039 --> 18:26:38,000 it's going to see that they are of type 24546 18:26:34,876 --> 18:26:40,156 and it's going to see that it can use 24547 18:26:38,000 --> 18:26:41,919 the function template we have here and 24548 18:26:40,156 --> 18:26:43,279 it's going to use that and generate a 24549 18:26:41,919 --> 18:26:45,599 template instance that it's going to 24550 18:26:43,279 --> 18:26:48,080 take and and it is going to call it to 24551 18:26:45,600 --> 18:26:50,080 get our maximum here again we can prove 24552 18:26:48,080 --> 18:26:51,835 this through the debugger we're going to 24553 18:26:50,080 --> 18:26:55,279 set up a breakpoint we're going to go to 24554 18:26:51,835 --> 18:26:57,360 our run tab here and we're going to run 24555 18:26:57,360 --> 18:27:01,919 we're going to build our thing and when 24556 18:26:59,436 --> 18:27:05,040 we hit the main function you're going to 24557 18:27:01,919 --> 18:27:08,399 see that a is 10 b is 23 which is what 24558 18:27:05,039 --> 18:27:11,195 we have here cd we have our values n and 24559 18:27:08,399 --> 18:27:13,919 std string because it is a compound type 24560 18:27:11,195 --> 18:27:15,516 or a custom type we can expand on this 24561 18:27:13,919 --> 18:27:17,679 and really see more information about 24562 18:27:15,516 --> 18:27:19,836 this but we don't have enough tools to 24563 18:27:17,679 --> 18:27:22,795 really understand this by now so we're 24564 18:27:19,835 --> 18:27:25,279 going to just to use this as a wrapper 24565 18:27:22,796 --> 18:27:27,360 on our stream information here now we 24566 18:27:25,279 --> 18:27:30,244 want to call the maximum function and 24567 18:27:27,360 --> 18:27:32,080 see what was generated by the compiler 24568 18:27:30,244 --> 18:27:34,000 and we can step into this function 24569 18:27:32,080 --> 18:27:34,796 because we have hit the breakpoint by 24570 18:27:34,796 --> 18:27:39,279 so if we hit step into we're going to 24571 18:27:37,279 --> 18:27:41,755 step into this function and we're going 24572 18:27:39,279 --> 18:27:44,244 to see that it is an int function 24573 18:27:41,755 --> 18:27:46,399 instance that was generated this is 24574 18:27:46,399 --> 18:27:51,115 if we change our call here to use c and 24575 18:27:49,360 --> 18:27:53,360 d we're going to generate a double 24576 18:27:51,115 --> 18:27:55,360 instance so let's do that we're going to 24577 18:27:53,360 --> 18:27:56,960 generate a double instance and if we 24578 18:27:55,360 --> 18:27:59,195 print this out we're going to see this 24579 18:27:56,960 --> 18:28:01,116 printed out but we can use the debugger 24580 18:27:59,195 --> 18:28:03,195 to really prove this not that we know 24581 18:28:01,115 --> 18:28:05,515 how to use it we don't have to look 24582 18:28:03,195 --> 18:28:07,279 through the terminal output window but 24583 18:28:05,516 --> 18:28:09,680 you can do that if you want you can set 24584 18:28:07,279 --> 18:28:12,244 up sddc out and print max here you're 24585 18:28:09,679 --> 18:28:13,919 going to see it printed out so let's run 24586 18:28:12,244 --> 18:28:16,400 our debugger we're going to see the 24587 18:28:13,919 --> 18:28:18,399 template instance that was generated 24588 18:28:16,399 --> 18:28:21,279 through the divider we're going to hit 24589 18:28:18,399 --> 18:28:23,515 the breakpoint at line 17 here 24590 18:28:21,279 --> 18:28:25,835 if we step inside we're going to see the 24591 18:28:23,516 --> 18:28:26,796 local variables for our function here a 24592 18:28:26,796 --> 18:28:31,516 and we're going to have the maximum 24593 18:28:29,115 --> 18:28:33,360 function to the top of the call stack 24594 18:28:31,516 --> 18:28:35,836 and you're going to see that it is a 24595 18:28:33,360 --> 18:28:37,360 double instance for our function 24596 18:28:37,360 --> 18:28:43,195 okay this is what we mean by 24597 18:28:39,919 --> 18:28:45,195 template type deduction it is the 24598 18:28:45,195 --> 18:28:50,796 that the compiler uses to know 24599 18:28:48,156 --> 18:28:53,195 which template instance to generate from 24600 18:28:50,796 --> 18:28:55,040 your function calls but as we have seen 24601 18:28:53,195 --> 18:28:56,475 in the last lecture this can be 24602 18:28:56,475 --> 18:29:02,080 one way this can be bad is if you pass 24603 18:28:59,600 --> 18:29:05,360 different types to the maximum function 24604 18:29:02,080 --> 18:29:07,680 here let's pass in a and c for example 24605 18:29:05,360 --> 18:29:09,516 or a and d this is going to work equally 24606 18:29:07,679 --> 18:29:12,475 well you see that we get a compiler 24607 18:29:09,516 --> 18:29:15,680 error because the compiler now it says 24608 18:29:12,475 --> 18:29:18,080 that we are calling with different types 24609 18:29:15,679 --> 18:29:20,079 but the template parameters here are 24610 18:29:18,080 --> 18:29:22,320 really of the same type they should be 24611 18:29:20,080 --> 18:29:24,960 of the same type because t is going to 24612 18:29:22,320 --> 18:29:26,876 be replaced for a single time so the 24613 18:29:24,960 --> 18:29:28,960 call we do here is going to fail and if 24614 18:29:26,876 --> 18:29:31,279 we run this through the compiler again 24615 18:29:28,960 --> 18:29:32,876 let's do that we're going to use gcc 24616 18:29:31,279 --> 18:29:34,720 we're going to get a compiler error and 24617 18:29:32,876 --> 18:29:36,796 it is going to say deduce conflicting 24618 18:29:36,796 --> 18:29:41,360 and then double so it can't really know 24619 18:29:39,436 --> 18:29:43,756 which one you want you want ent or 24620 18:29:41,360 --> 18:29:46,639 double the compiler doesn't know and it 24621 18:29:43,755 --> 18:29:48,399 is going to give us a compiler error but 24622 18:29:46,639 --> 18:29:50,720 now we're going to see a mechanism we 24623 18:29:48,399 --> 18:29:53,835 can use to make this work and that's 24624 18:29:50,720 --> 18:29:56,720 going to be explicit template arguments 24625 18:29:53,835 --> 18:29:59,115 okay what we can do to do this we can go 24626 18:29:56,720 --> 18:30:02,244 down and put in our call we're going to 24627 18:29:59,115 --> 18:30:03,835 say auto and say result or max we can 24628 18:30:02,244 --> 18:30:05,436 call this however we want we're going to 24629 18:30:03,835 --> 18:30:07,195 say the name of the function and we're 24630 18:30:07,195 --> 18:30:12,475 the template type that we want generated 24631 18:30:10,080 --> 18:30:14,876 for our function template here so if we 24632 18:30:12,475 --> 18:30:17,360 do something like this with double 24633 18:30:14,876 --> 18:30:19,600 inside angle brackets here we're going 24634 18:30:17,360 --> 18:30:22,555 to be basically telling the compiler 24635 18:30:19,600 --> 18:30:25,516 please generate a double template 24636 18:30:22,555 --> 18:30:28,000 instance for this call we are doing here 24637 18:30:25,516 --> 18:30:29,600 so if we pass in c and d for example 24638 18:30:28,000 --> 18:30:31,835 this is going to give us what we want 24639 18:30:29,600 --> 18:30:34,320 and we can print this out if we 24640 18:30:31,835 --> 18:30:37,835 want so let's say max okay this is going 24641 18:30:34,320 --> 18:30:39,280 to work if we build let's build with gcc 24642 18:30:39,279 --> 18:30:43,600 the build is going to be good because we 24643 18:30:43,600 --> 18:30:48,639 arguments and if we run this we should 24644 18:30:46,000 --> 18:30:51,360 see our maximum let's bring up a 24645 18:30:48,639 --> 18:30:53,679 terminal window and a visual studio code 24646 18:30:51,360 --> 18:30:55,680 here so that we can use that to see 24647 18:30:53,679 --> 18:30:57,919 things running we're going to clear and 24648 18:30:55,679 --> 18:31:02,243 run rooster you see that we have our 24649 18:30:57,919 --> 18:31:04,555 maximum here but using explicit template 24650 18:31:02,244 --> 18:31:07,279 arguments like we are doing here is 24651 18:31:04,555 --> 18:31:10,080 going to allow us to even pass different 24652 18:31:07,279 --> 18:31:12,080 types for our template arguments and for 24653 18:31:10,080 --> 18:31:14,475 example now we can change the first 24654 18:31:12,080 --> 18:31:16,400 argument here to ba which is going to be 24655 18:31:14,475 --> 18:31:19,755 an end and we're going to leave the 24656 18:31:16,399 --> 18:31:21,919 second one to bd which is of type double 24657 18:31:19,755 --> 18:31:24,079 now the compiler is going to see that 24658 18:31:21,919 --> 18:31:26,959 we're going to generate a template 24659 18:31:24,080 --> 18:31:28,960 instance of type double and it is going 24660 18:31:26,960 --> 18:31:31,756 to know that it can do implicit 24661 18:31:28,960 --> 18:31:34,556 conversions for the arguments we pass in 24662 18:31:31,755 --> 18:31:36,639 here so this is going to work if we pass 24663 18:31:34,555 --> 18:31:39,599 an argument that is implicitly 24664 18:31:36,639 --> 18:31:42,720 convertible to the type we passed in the 24665 18:31:39,600 --> 18:31:44,960 angle brackets here and let's debug this 24666 18:31:42,720 --> 18:31:47,516 again to show you that we're going to 24667 18:31:44,960 --> 18:31:49,196 generate a double template instance that 24668 18:31:47,516 --> 18:31:52,400 is going to be called and give us the 24669 18:31:49,195 --> 18:31:55,195 maximum that we print here so we can run 24670 18:31:52,399 --> 18:31:57,679 our debugger by hitting the green run 24671 18:31:55,195 --> 18:31:59,195 button here let's minimize this so that 24672 18:31:59,195 --> 18:32:04,399 our terminal window and it is down here 24673 18:32:02,000 --> 18:32:07,039 let's grab it and put it to the left so 24674 18:32:04,399 --> 18:32:08,795 that we can really see things printed 24675 18:32:08,796 --> 18:32:13,360 we have hit the breakpoint here and we 24676 18:32:11,516 --> 18:32:15,116 have our local variables you can look at 24677 18:32:13,360 --> 18:32:17,279 this if you want it is basically what we 24678 18:32:15,115 --> 18:32:19,919 have in the main function here but what 24679 18:32:17,279 --> 18:32:20,876 we want is to step into this maximum 24680 18:32:22,639 --> 18:32:27,755 the template instance that was generated 24681 18:32:25,279 --> 18:32:30,156 you see that it is of a double type here 24682 18:32:27,755 --> 18:32:34,000 okay hopefully now you can see that even 24683 18:32:30,156 --> 18:32:36,000 if we passed different values for our 24684 18:32:34,000 --> 18:32:38,960 template arguments i should say 24685 18:32:36,000 --> 18:32:42,080 different types because a is of type and 24686 18:32:38,960 --> 18:32:44,556 and d is of type double but we're not 24687 18:32:42,080 --> 18:32:47,436 getting a compiler error the compiler is 24688 18:32:44,555 --> 18:32:50,796 going to insert implicit conversions 24689 18:32:47,436 --> 18:32:53,600 from whatever arguments we pass here to 24690 18:32:50,796 --> 18:32:56,400 the explicit template argument that we 24691 18:32:53,600 --> 18:32:58,556 specified here again the explicit 24692 18:32:56,399 --> 18:33:01,195 template argument like we are doing here 24693 18:32:58,555 --> 18:33:03,516 using the syntax with angle brackets and 24694 18:33:01,195 --> 18:33:06,399 putting in our type is going to tell the 24695 18:33:03,516 --> 18:33:09,116 compiler please use this type and 24696 18:33:06,399 --> 18:33:11,679 replace that for t in our function 24697 18:33:09,115 --> 18:33:14,155 template here and generate a template 24698 18:33:11,679 --> 18:33:16,399 instance we can call to service this 24699 18:33:14,156 --> 18:33:18,639 call here and we're going to do implicit 24700 18:33:18,639 --> 18:33:22,876 and to double because that's what we 24701 18:33:20,555 --> 18:33:24,876 want then d is going to be of double 24702 18:33:22,876 --> 18:33:26,555 type we're not going to do any implicit 24703 18:33:24,876 --> 18:33:28,476 conversion then we're going to call this 24704 18:33:26,555 --> 18:33:30,475 function we're going to return a double 24705 18:33:28,475 --> 18:33:32,319 from it because that's our template 24706 18:33:30,475 --> 18:33:34,155 instance and we're going to print that 24707 18:33:32,320 --> 18:33:36,720 out here this is what is happening and 24708 18:33:34,156 --> 18:33:39,436 we can prove that a double instance was 24709 18:33:36,720 --> 18:33:42,639 generated through our call stack here we 24710 18:33:39,436 --> 18:33:44,475 see the maximum function is taking in 24711 18:33:42,639 --> 18:33:48,399 double parameters and it's going to 24712 18:33:44,475 --> 18:33:50,243 return double as our explicit 24713 18:33:48,399 --> 18:33:54,155 template argument here this is the 24714 18:33:50,244 --> 18:33:56,556 syntax that gcc uses is using but we 24715 18:33:54,156 --> 18:33:59,360 know now that it is a double template 24716 18:33:56,555 --> 18:34:02,000 instance for a function call here okay 24717 18:33:59,360 --> 18:34:04,796 if you're not happy with what we are 24718 18:34:02,000 --> 18:34:07,516 seeing with the debugger you can also 24719 18:34:04,796 --> 18:34:09,279 use cpp insights to really prove this 24720 18:34:07,516 --> 18:34:11,600 let's open this up we're going to say 24721 18:34:11,600 --> 18:34:16,000 let's do that and we're going to have it 24722 18:34:13,835 --> 18:34:17,835 open here okay we're going to 24723 18:34:16,000 --> 18:34:20,475 erase whatever we have in here and we're 24724 18:34:17,835 --> 18:34:23,436 going to put in another piece of code we 24725 18:34:20,475 --> 18:34:25,599 have our maximum function i should say 24726 18:34:23,436 --> 18:34:27,436 template function or function template 24727 18:34:25,600 --> 18:34:30,876 and we have alcohol here and it is 24728 18:34:27,436 --> 18:34:32,720 passing different types a and d remember 24729 18:34:30,876 --> 18:34:35,600 the last time we did something like this 24730 18:34:32,720 --> 18:34:38,475 by passing in different types for the 24731 18:34:35,600 --> 18:34:40,796 template argument this guy failed it 24732 18:34:38,475 --> 18:34:43,115 couldn't really generate a function 24733 18:34:40,796 --> 18:34:45,919 template instance because the template 24734 18:34:43,115 --> 18:34:48,080 parameters we have on top here specify 24735 18:34:45,919 --> 18:34:50,475 that these guys should be of the same 24736 18:34:48,080 --> 18:34:53,516 type but now we are able to use 24737 18:34:50,475 --> 18:34:56,319 different types as arguments because we 24738 18:34:53,516 --> 18:34:58,476 have an explicit argument specified 24739 18:34:56,320 --> 18:35:00,960 let's run this and see the template 24740 18:34:58,475 --> 18:35:02,959 instance that is generated and you can 24741 18:35:00,960 --> 18:35:06,244 see that it is of double type we 24742 18:35:02,960 --> 18:35:08,960 basically replaced every instance of t 24743 18:35:06,244 --> 18:35:11,279 here with double and this function is 24744 18:35:08,960 --> 18:35:13,280 going to be called to service the call 24745 18:35:11,279 --> 18:35:15,279 we are making here okay this is really 24746 18:35:13,279 --> 18:35:17,195 all we set out to do in this lecture i 24747 18:35:15,279 --> 18:35:19,600 hope you found it interesting the main 24748 18:35:17,195 --> 18:35:22,796 message was template type deduction a 24749 18:35:19,600 --> 18:35:24,960 default mechanism c plus plus uses to 24750 18:35:22,796 --> 18:35:27,680 know which template instances to 24751 18:35:24,960 --> 18:35:30,156 generate from your function calls but if 24752 18:35:27,679 --> 18:35:33,279 the defaults aren't working for you you 24753 18:35:30,156 --> 18:35:36,400 can use explicit template arguments to 24754 18:35:33,279 --> 18:35:39,039 specify the template instance you want 24755 18:35:36,399 --> 18:35:40,720 to generate it we are going to stop here 24756 18:35:39,039 --> 18:35:42,795 in this lecture in the next one we're 24757 18:35:40,720 --> 18:35:45,195 going to see how we can pass template 24758 18:35:45,195 --> 18:35:49,115 go ahead and finish up here and meet me 24759 18:35:47,516 --> 18:35:51,436 there in this lecture we're going to 24760 18:35:49,115 --> 18:35:54,319 show you that you can pass your template 24761 18:35:51,436 --> 18:35:56,244 type parameters by reference and we're 24762 18:35:54,320 --> 18:35:58,320 going to look at a simple example to 24763 18:35:56,244 --> 18:36:00,320 really drive this home what we have seen 24764 18:35:58,320 --> 18:36:03,116 so far is that we can do things like 24765 18:36:00,320 --> 18:36:05,280 this and specify our t like this and 24766 18:36:03,115 --> 18:36:08,475 this is going to generate a template 24767 18:36:05,279 --> 18:36:11,115 instance that passes our arguments by 24768 18:36:08,475 --> 18:36:13,755 value for example if we pass in double a 24769 18:36:11,115 --> 18:36:16,243 and double b here for our maximum we're 24770 18:36:13,755 --> 18:36:18,555 going to generate a template instance 24771 18:36:16,244 --> 18:36:20,796 which takes double as parameters and 24772 18:36:18,555 --> 18:36:23,115 returns a double here and this is going 24773 18:36:20,796 --> 18:36:25,516 to be doing things by value okay this is 24774 18:36:23,115 --> 18:36:27,515 what i want you to focus on right now it 24775 18:36:25,516 --> 18:36:30,476 is that this is going to do things by 24776 18:36:27,516 --> 18:36:32,400 value so if we go in our function here 24777 18:36:30,475 --> 18:36:34,720 and compute the maximum you're going to 24778 18:36:32,399 --> 18:36:37,195 see that the addresses we have on the 24779 18:36:34,720 --> 18:36:39,919 outside are going to be different from 24780 18:36:37,195 --> 18:36:42,639 the addresses we have on the inside here 24781 18:36:39,919 --> 18:36:44,959 if we try to print them out but you can 24782 18:36:42,639 --> 18:36:47,436 also pass your template parameters by 24783 18:36:44,960 --> 18:36:50,000 reference and the syntax to do that is 24784 18:36:47,436 --> 18:36:52,400 ridiculously easy you just use this like 24785 18:36:50,000 --> 18:36:54,639 you would use any reference we say cast 24786 18:36:52,399 --> 18:36:58,000 t reference here and this is going to 24787 18:36:54,639 --> 18:37:00,000 just replace in whatever type that you 24788 18:36:58,000 --> 18:37:02,555 pass when you call the function here for 24789 18:37:00,000 --> 18:37:05,516 example here we are passing in double 24790 18:37:02,555 --> 18:37:08,319 arguments so this is going to deduce a 24791 18:37:05,516 --> 18:37:11,279 double template instance which basically 24792 18:37:08,320 --> 18:37:13,196 says cost double reference maximum cost 24793 18:37:11,279 --> 18:37:15,436 double reference a and cost double 24794 18:37:13,195 --> 18:37:17,516 reference b and this is going to be the 24795 18:37:15,436 --> 18:37:20,320 function that is called so this is going 24796 18:37:17,516 --> 18:37:22,639 to really do things by reference and the 24797 18:37:20,320 --> 18:37:25,680 values we have inside a function 24798 18:37:22,639 --> 18:37:27,919 template body here are going to be true 24799 18:37:25,679 --> 18:37:29,679 references to the values we have on the 24800 18:37:27,919 --> 18:37:31,599 outside if we happen to do any 24801 18:37:29,679 --> 18:37:33,679 modification inside the body of the 24802 18:37:31,600 --> 18:37:35,920 function the modification is going to be 24803 18:37:33,679 --> 18:37:38,000 visible on the outside what we're going 24804 18:37:35,919 --> 18:37:40,080 to be doing in this lecture is just 24805 18:37:38,000 --> 18:37:42,080 printing the addresses here and we're 24806 18:37:40,080 --> 18:37:45,436 going to see that they are the same here 24807 18:37:42,080 --> 18:37:48,000 on the outside and inside the body of 24808 18:37:45,436 --> 18:37:50,320 our function template here okay this is 24809 18:37:48,000 --> 18:37:52,720 what we want to really play with in this 24810 18:37:50,320 --> 18:37:54,960 lecture but before we head over to 24811 18:37:52,720 --> 18:37:57,195 visual studio code i want to show you 24812 18:37:54,960 --> 18:37:59,836 that you can confuse your compiler if 24813 18:37:57,195 --> 18:38:02,399 you are not careful if you set up these 24814 18:37:59,835 --> 18:38:04,555 two function templates you see here 24815 18:38:02,399 --> 18:38:06,319 they are going to be overloads and we 24816 18:38:04,555 --> 18:38:08,243 can use function overloads with 24817 18:38:06,320 --> 18:38:10,476 templates but for now we're just going 24818 18:38:08,244 --> 18:38:12,960 to focus on the parameters that we are 24819 18:38:10,475 --> 18:38:15,115 passing in here the first one is taken 24820 18:38:15,115 --> 18:38:19,360 and the second one is taking thanks by 24821 18:38:19,360 --> 18:38:24,000 and if we issue a call like this we're 24822 18:38:21,600 --> 18:38:26,556 going to have the same problem we have 24823 18:38:24,000 --> 18:38:28,320 seen with function overloads before this 24824 18:38:26,555 --> 18:38:30,639 is not going to know whether you are 24825 18:38:28,320 --> 18:38:32,800 calling by value or by reference and 24826 18:38:30,639 --> 18:38:34,720 this is going to cause a compiler error 24827 18:38:32,800 --> 18:38:36,160 i really want you to be aware of this 24828 18:38:34,720 --> 18:38:38,160 okay now that you know this we're going 24829 18:38:36,160 --> 18:38:40,160 to head over to visual studio code and 24830 18:38:38,160 --> 18:38:42,800 really play with us a little more okay 24831 18:38:40,160 --> 18:38:45,512 here we are in our working folder the 24832 18:38:42,800 --> 18:38:47,680 current project is template parameters 24833 18:38:45,512 --> 18:38:50,800 by reference we're going to grab our 24834 18:38:47,679 --> 18:38:52,871 template files and put them in place so 24835 18:38:50,800 --> 18:38:55,512 let's do that and we are going to open 24836 18:38:52,872 --> 18:38:58,080 this little guy in visual studio code by 24837 18:38:55,512 --> 18:39:00,160 dragging and dropping here the folder is 24838 18:38:58,080 --> 18:39:02,080 going to open up we're going to open it 24839 18:39:00,160 --> 18:39:05,919 and we're going to close the left pane 24840 18:39:02,080 --> 18:39:08,000 here and clean up our main file here and 24841 18:39:05,919 --> 18:39:10,319 we are going to put in our function 24842 18:39:08,000 --> 18:39:12,399 template one thing i want you to see in 24843 18:39:10,320 --> 18:39:14,480 this lecture is that it is possible to 24844 18:39:12,399 --> 18:39:16,799 separate these things into function 24845 18:39:14,479 --> 18:39:18,800 declaration and function definition 24846 18:39:16,800 --> 18:39:20,872 let's just try and do that here so that 24847 18:39:18,800 --> 18:39:22,551 you can really see this and then you're 24848 18:39:20,872 --> 18:39:24,639 going to have to choose what you like a 24849 18:39:22,551 --> 18:39:27,039 little more so let's grab a function 24850 18:39:24,639 --> 18:39:29,279 header here and use that as a prototype 24851 18:39:27,039 --> 18:39:31,439 we're going to put that in front of the 24852 18:39:29,279 --> 18:39:34,551 main function and that's going to be our 24853 18:39:31,440 --> 18:39:37,191 declaration and if we go down after the 24854 18:39:34,551 --> 18:39:39,440 main function here what we have is our 24855 18:39:37,191 --> 18:39:41,831 definition this could work and this is 24856 18:39:39,440 --> 18:39:44,080 going to generate template instances for 24857 18:39:41,831 --> 18:39:45,919 the calls we do in the main function 24858 18:39:44,080 --> 18:39:47,919 here okay now that we have this we can 24859 18:39:45,919 --> 18:39:50,319 head in the main function and really 24860 18:39:47,919 --> 18:39:51,759 play with the function template here 24861 18:39:50,320 --> 18:39:53,600 we're going to set up two double 24862 18:39:51,759 --> 18:39:56,399 parameters and we're going to call the 24863 18:39:53,600 --> 18:39:58,639 function for example we say addon result 24864 18:39:56,399 --> 18:40:00,319 and we're going to call the maximum 24865 18:39:58,639 --> 18:40:02,551 function and we're going to pass in a 24866 18:40:02,551 --> 18:40:08,159 default template type deduction and it 24867 18:40:05,039 --> 18:40:10,319 is going to generate a double function 24868 18:40:08,160 --> 18:40:12,080 template instance here but what we 24869 18:40:10,320 --> 18:40:14,480 really want to prove is that this is 24870 18:40:12,080 --> 18:40:16,720 going to pass things by value and one 24871 18:40:14,479 --> 18:40:20,159 way we have to prove this is to bridge 24872 18:40:16,720 --> 18:40:22,320 the addresses both on the outside and on 24873 18:40:20,160 --> 18:40:24,240 the inside of the function here you see 24874 18:40:22,320 --> 18:40:26,240 we are already doing that in our 24875 18:40:24,240 --> 18:40:28,639 function template body we are going to 24876 18:40:26,240 --> 18:40:32,080 print the address of a so we can really 24877 18:40:28,639 --> 18:40:33,279 grab this and we use this stdc out 24878 18:40:33,279 --> 18:40:37,512 and we are going to change it to say out 24879 18:40:36,080 --> 18:40:40,160 here and we're going to print the 24880 18:40:37,512 --> 18:40:42,800 address of a let's grab this again and 24881 18:40:40,160 --> 18:40:44,720 use this after the function call and 24882 18:40:44,720 --> 18:40:50,160 is some address on the outside so for 24883 18:40:47,831 --> 18:40:54,639 example we may have something like zero 24884 18:40:50,160 --> 18:40:57,040 x and a one one one abc which is some 24885 18:40:54,639 --> 18:40:59,191 hex number here and that's the same 24886 18:40:57,039 --> 18:41:01,119 thing we're going to have on the outside 24887 18:40:59,191 --> 18:41:04,000 because the value we have before the 24888 18:41:01,119 --> 18:41:05,831 function call and the value we have 24889 18:41:04,000 --> 18:41:08,551 after the function call are going to be 24890 18:41:05,831 --> 18:41:11,191 leaving at the same address but what we 24891 18:41:08,551 --> 18:41:13,440 have inside the function body is going 24892 18:41:11,191 --> 18:41:15,679 to be a copy so it's going to live at 24893 18:41:13,440 --> 18:41:16,960 some other address for example let's say 24894 18:41:16,960 --> 18:41:20,872 and uh the main point here is that what 24895 18:41:19,119 --> 18:41:22,551 we have on the outside is going to be 24896 18:41:20,872 --> 18:41:24,551 different from what we have on the east 24897 18:41:22,551 --> 18:41:27,191 side and hopefully this is going to 24898 18:41:24,551 --> 18:41:30,080 prove that we are passing by value so 24899 18:41:27,191 --> 18:41:32,159 let's print out our maximum here and see 24900 18:41:30,080 --> 18:41:35,360 the addresses printed out we're going to 24901 18:41:32,160 --> 18:41:38,400 build with gcc and we're going to build 24902 18:41:35,360 --> 18:41:40,240 successfully we can bring up a terminal 24903 18:41:38,399 --> 18:41:42,479 to play with us a little more we can 24904 18:41:40,240 --> 18:41:45,040 clear and run rooster we're going to see 24905 18:41:42,479 --> 18:41:48,720 that the address on the outside ends in 24906 18:41:45,039 --> 18:41:51,511 dd8 and the address on the outside ends 24907 18:41:48,720 --> 18:41:53,759 with dd8 after the function call but 24908 18:41:51,512 --> 18:41:56,160 inside you see that we have a different 24909 18:41:53,759 --> 18:41:58,799 address and this is going to tell us 24910 18:41:56,160 --> 18:42:01,360 that we are passing by value and what we 24911 18:41:58,800 --> 18:42:04,400 have inside the body of the function is 24912 18:42:01,360 --> 18:42:06,720 basically a copy again i don't expect 24913 18:42:04,399 --> 18:42:08,551 you to see the same addresses as me here 24914 18:42:06,720 --> 18:42:10,872 because your operating system is going 24915 18:42:08,551 --> 18:42:13,039 to set up the addresses however it wants 24916 18:42:10,872 --> 18:42:15,279 the main point here is that the 24917 18:42:13,039 --> 18:42:17,191 addresses you see on the outside are 24918 18:42:15,279 --> 18:42:19,600 going to be different from the addresses 24919 18:42:17,191 --> 18:42:22,159 you see on the inside here and this 24920 18:42:19,600 --> 18:42:24,639 proves that we are passing by value but 24921 18:42:22,160 --> 18:42:27,120 what if you don't really want to pass by 24922 18:42:24,639 --> 18:42:29,600 value and make your function template 24923 18:42:27,119 --> 18:42:31,512 work by reference well we can do that we 24924 18:42:29,600 --> 18:42:33,600 can change our function here to do 24925 18:42:31,512 --> 18:42:34,400 things by reference and all we have to 24926 18:42:34,399 --> 18:42:39,919 is use t reference like this okay and 24927 18:42:37,512 --> 18:42:42,551 when we start using references we really 24928 18:42:39,919 --> 18:42:45,440 need to be careful and make sure people 24929 18:42:42,551 --> 18:42:48,080 don't modify things without our 24930 18:42:45,440 --> 18:42:50,639 knowledge and a good practice is to mark 24931 18:42:48,080 --> 18:42:52,639 your things as cost references so we're 24932 18:42:50,639 --> 18:42:55,191 going to do that and we are going to do 24933 18:42:52,639 --> 18:42:57,360 the same in our function definition so 24934 18:42:55,191 --> 18:43:00,080 we're going to change this to cost t 24935 18:42:57,360 --> 18:43:02,720 reference let's do that and let's say 24936 18:43:00,080 --> 18:43:05,191 cost t reference for the first parameter 24937 18:43:02,720 --> 18:43:07,919 here and say the same for the second 24938 18:43:05,191 --> 18:43:10,319 parameter here and this should be it we 24939 18:43:07,919 --> 18:43:12,551 shouldn't be able to use this reference 24940 18:43:10,320 --> 18:43:14,552 to modify the data inside the body of 24941 18:43:12,551 --> 18:43:16,872 the function and our function is a 24942 18:43:14,551 --> 18:43:18,639 little safer but what the compiler is 24943 18:43:16,872 --> 18:43:20,479 going to do is really what it's been 24944 18:43:18,639 --> 18:43:22,160 doing all along it's going to look at 24945 18:43:20,479 --> 18:43:23,599 the arguments that we pass to the 24946 18:43:22,160 --> 18:43:26,080 function call here it's going to see 24947 18:43:23,600 --> 18:43:28,872 that it is double and it's going to do 24948 18:43:26,080 --> 18:43:31,759 default template type deduction so it's 24949 18:43:28,872 --> 18:43:34,000 going to generate a double template 24950 18:43:31,759 --> 18:43:36,639 instance for the function template here 24951 18:43:34,000 --> 18:43:38,080 and it's going to replace in double and 24952 18:43:36,639 --> 18:43:40,479 we're going to have const double 24953 18:43:38,080 --> 18:43:42,872 reference maximum cost double reference 24954 18:43:40,479 --> 18:43:46,159 a constant double reference b 24955 18:43:42,872 --> 18:43:48,479 and we can prove this by going into cpp 24956 18:43:46,160 --> 18:43:51,040 insight i think we can do that so we are 24957 18:43:48,479 --> 18:43:52,959 here in cpp insights we can take out 24958 18:43:51,039 --> 18:43:55,511 whatever we had we're going to put in 24959 18:43:57,360 --> 18:44:03,119 to generate our instances and here we 24960 18:44:00,399 --> 18:44:05,679 have our template instance here 24961 18:44:03,119 --> 18:44:08,871 generated you see that it is going to 24962 18:44:05,679 --> 18:44:10,719 generate a double version of our 24963 18:44:08,872 --> 18:44:13,279 function template here and this is what 24964 18:44:10,720 --> 18:44:16,240 we have so const double reference 24965 18:44:13,279 --> 18:44:18,399 maximum and it is going to put in a 24966 18:44:16,240 --> 18:44:20,639 double explicit argument here but this 24967 18:44:18,399 --> 18:44:23,439 is business of the compiler what we 24968 18:44:20,639 --> 18:44:25,440 really care about is that it generated a 24969 18:44:23,440 --> 18:44:27,600 double template instance for our 24970 18:44:25,440 --> 18:44:29,040 function template here now if we run 24971 18:44:27,600 --> 18:44:31,512 this it is going to be passing 24972 18:44:29,039 --> 18:44:33,511 parameters by reference and what we have 24973 18:44:31,512 --> 18:44:36,160 on the inside should have the same 24974 18:44:33,512 --> 18:44:37,512 address as what we have on the outside 24975 18:44:37,512 --> 18:44:40,960 to really drive this home we should 24976 18:44:40,960 --> 18:44:46,240 our address to abc to comply with what 24977 18:44:44,080 --> 18:44:48,240 we have on the outside here we can try 24978 18:44:46,240 --> 18:44:50,400 and build our program we're going to use 24979 18:44:48,240 --> 18:44:52,480 the gcc to do that the build is going to 24980 18:44:50,399 --> 18:44:54,639 be good we're going to clear and if we 24981 18:44:52,479 --> 18:44:59,440 run rooster we should see the addresses 24982 18:44:54,639 --> 18:45:02,479 being the same out d e 0 in d e 0 and 24983 18:44:59,440 --> 18:45:04,551 out d e 0 and you see now we are passing 24984 18:45:02,479 --> 18:45:07,039 our parameters by reference and if you 24985 18:45:04,551 --> 18:45:10,399 want to set up a function template that 24986 18:45:07,039 --> 18:45:12,239 passes template parameters by reference 24987 18:45:12,240 --> 18:45:16,552 and i think we can also try and debug 24988 18:45:14,479 --> 18:45:18,959 this and see if we can see any more 24989 18:45:16,551 --> 18:45:21,759 information on how this works we're 24990 18:45:18,960 --> 18:45:24,160 going to hit our run tab here and we're 24991 18:45:21,759 --> 18:45:26,000 going to hit the green button to start 24992 18:45:24,160 --> 18:45:29,600 debugging this program we're going to be 24993 18:45:26,000 --> 18:45:32,320 using the debugging tools from gcc 24994 18:45:29,600 --> 18:45:35,360 and we see our local variables here we 24995 18:45:32,320 --> 18:45:37,120 have hit the breakpoint and we have the 24996 18:45:35,360 --> 18:45:40,320 main function to the top of the call 24997 18:45:37,119 --> 18:45:42,479 stack this is what we have here and if 24998 18:45:40,320 --> 18:45:44,240 we try and step into the maximum 24999 18:45:42,479 --> 18:45:47,191 function we're going to see that the 25000 18:45:44,240 --> 18:45:49,832 template instance now is using const 25001 18:45:47,191 --> 18:45:52,720 double references and this is the 25002 18:45:49,831 --> 18:45:55,599 template instance that was generated i 25003 18:45:52,720 --> 18:45:58,551 think this is all we can really see here 25004 18:45:55,600 --> 18:46:00,080 and let's stop this and show you one 25005 18:46:00,080 --> 18:46:03,831 what i'm going to show you is that you 25006 18:46:03,831 --> 18:46:09,039 the function template that passes by 25007 18:46:07,119 --> 18:46:10,799 value and the other one passing by 25008 18:46:09,039 --> 18:46:12,719 reference if you do that you're going to 25009 18:46:10,800 --> 18:46:14,240 have problems when you issue the call 25010 18:46:12,720 --> 18:46:16,720 like this because the compiler is going 25011 18:46:14,240 --> 18:46:19,120 to be confused let's change the second 25012 18:46:16,720 --> 18:46:21,191 one here to pass by value so we're going 25013 18:46:19,119 --> 18:46:22,959 to remove the cost reference here i 25014 18:46:21,191 --> 18:46:25,279 think it's going to do let's do that 25015 18:46:22,960 --> 18:46:28,240 pretty quick we'll remove the cost and 25016 18:46:25,279 --> 18:46:31,119 the reference we remove the cost and the 25017 18:46:28,240 --> 18:46:33,919 reference here and we use this guy as 25018 18:46:31,119 --> 18:46:36,799 our function prototype in front of the 25019 18:46:33,919 --> 18:46:39,360 main function here and how can we say 25020 18:46:36,800 --> 18:46:41,120 that we're going to go on top here and 25021 18:46:39,360 --> 18:46:43,680 say that okay we're going to put our 25022 18:46:41,119 --> 18:46:45,831 semicolon and this is going to be our 25023 18:46:43,679 --> 18:46:47,679 declaration but the moment we do this 25024 18:46:45,831 --> 18:46:49,759 you see that our compiler is going to be 25025 18:46:47,679 --> 18:46:52,399 confused because we have two possible 25026 18:46:49,759 --> 18:46:55,191 candidates for the call here it can 25027 18:46:52,399 --> 18:46:56,959 either be by value or by reference and 25028 18:46:55,191 --> 18:46:59,279 if we try to build this program we're 25029 18:46:56,960 --> 18:47:01,040 going to get a compiler error and it is 25030 18:46:59,279 --> 18:47:03,360 basically going to be saying what is 25031 18:47:01,039 --> 18:47:05,759 wrong here so it's going to say call 25032 18:47:03,360 --> 18:47:08,720 overloaded maximum double ambiguous 25033 18:47:05,759 --> 18:47:11,511 because we have two candidates again one 25034 18:47:08,720 --> 18:47:14,240 by reference one by value and this call 25035 18:47:11,512 --> 18:47:16,479 can be surfaced by both of these 25036 18:47:14,240 --> 18:47:18,639 candidates here so the compiler is going 25037 18:47:16,479 --> 18:47:21,440 to give us a compiler error and we have 25038 18:47:18,639 --> 18:47:23,679 no choice but to use either of these two 25039 18:47:23,679 --> 18:47:28,551 in our code and for example if we take 25040 18:47:26,000 --> 18:47:31,039 out the one by value here and leaving 25041 18:47:28,551 --> 18:47:32,639 the one by reference so let's comment 25042 18:47:31,039 --> 18:47:34,799 this out again if we're boiled we're 25043 18:47:32,639 --> 18:47:36,399 going to bolt you see the world is good 25044 18:47:34,800 --> 18:47:38,551 and we're going to print our data 25045 18:47:36,399 --> 18:47:40,871 however we have it in our code we see 25046 18:47:38,551 --> 18:47:43,039 the addresses are the same and another 25047 18:47:40,872 --> 18:47:45,600 thing i don't think i really proved to 25048 18:47:43,039 --> 18:47:48,639 you is that we are passing by const 25049 18:47:45,600 --> 18:47:50,400 reference here so the template instance 25050 18:47:48,639 --> 18:47:52,479 that is going to be generated is going 25051 18:47:50,399 --> 18:47:54,399 to be passing by const double reference 25052 18:47:52,479 --> 18:47:57,512 because we are passing in double 25053 18:47:54,399 --> 18:48:00,000 parameters but because of this cost in 25054 18:47:57,512 --> 18:48:03,119 front of our arguments here we want to 25055 18:48:00,000 --> 18:48:05,440 be able to modify the data referenced by 25056 18:48:03,119 --> 18:48:08,080 this name here if we go inside for 25057 18:48:05,440 --> 18:48:10,639 example and say plus plus a try to 25058 18:48:08,080 --> 18:48:12,160 modify a in any way through this 25059 18:48:10,639 --> 18:48:13,919 reference we're going to get a compiler 25060 18:48:12,160 --> 18:48:17,192 error let's work again and we're going 25061 18:48:13,919 --> 18:48:18,240 to see that and uh world finished with 25062 18:48:20,160 --> 18:48:24,639 increment of read only reference a 25063 18:48:22,800 --> 18:48:26,551 because we are passing by const 25064 18:48:24,639 --> 18:48:29,119 reference here so you should really 25065 18:48:26,551 --> 18:48:31,440 remember this even if we are doing the 25066 18:48:29,119 --> 18:48:32,479 things in the context of function 25067 18:48:32,479 --> 18:48:35,759 this is really all we set out to do in 25068 18:48:34,080 --> 18:48:38,160 this lecture showing you that you can 25069 18:48:35,759 --> 18:48:41,039 set up function templates that take 25070 18:48:38,160 --> 18:48:43,512 parameters by reference and contrasting 25071 18:48:41,039 --> 18:48:45,831 this with passing by value and seeing 25072 18:48:43,512 --> 18:48:47,760 the differences between these two things 25073 18:48:45,831 --> 18:48:49,279 we're going to stop here in this lecture 25074 18:48:47,759 --> 18:48:52,319 and the next one we're going to learn 25075 18:48:49,279 --> 18:48:54,551 about template spatialization 25076 18:48:52,320 --> 18:48:55,512 go ahead and finish up here and meet me 25077 18:48:55,512 --> 18:48:59,680 in this lecture we're going to learn 25078 18:48:56,800 --> 18:49:02,160 about template specialization and this 25079 18:48:59,679 --> 18:49:05,599 is a feature we have in c plus plus to 25080 18:49:02,160 --> 18:49:08,240 bypass the default mechanism of how 25081 18:49:05,600 --> 18:49:10,872 function templates work in c plus plus 25082 18:49:08,240 --> 18:49:12,872 okay let's make myself super clear here 25083 18:49:10,872 --> 18:49:15,192 we have a simple function template we 25084 18:49:12,872 --> 18:49:17,279 have seen a thousand times by now it is 25085 18:49:15,191 --> 18:49:20,239 the maximum function and it is going to 25086 18:49:17,279 --> 18:49:22,399 compute the maximum between a and b here 25087 18:49:20,240 --> 18:49:24,720 and it is going to return that it is a 25088 18:49:22,399 --> 18:49:26,551 function template so if we pass in a and 25089 18:49:24,720 --> 18:49:29,279 b which are of type and we're going to 25090 18:49:26,551 --> 18:49:32,720 generate a net instance of the template 25091 18:49:29,279 --> 18:49:34,800 here if we pass in c and d of double 25092 18:49:32,720 --> 18:49:37,600 time we're going to generate a double 25093 18:49:34,800 --> 18:49:40,551 instance of the function template here 25094 18:49:37,600 --> 18:49:43,360 if we pass in hdd string parameters 25095 18:49:40,551 --> 18:49:45,599 we're going to generate an std string 25096 18:49:43,360 --> 18:49:47,680 instance of this template here and this 25097 18:49:45,600 --> 18:49:49,680 is going to basically be comparing the 25098 18:49:47,679 --> 18:49:52,319 parameters we pass so we're basically 25099 18:49:49,679 --> 18:49:55,511 going to say if a is greater than b then 25100 18:49:52,320 --> 18:49:58,080 return a if a is not greater than b then 25101 18:49:55,512 --> 18:50:00,320 return b okay that's going to work if 25102 18:49:58,080 --> 18:50:02,400 what we're doing is what we have here 25103 18:50:00,320 --> 18:50:06,000 but the moment we pass something like 25104 18:50:02,399 --> 18:50:08,000 what we have here closed car pointers 25105 18:50:06,000 --> 18:50:10,080 then we're going to have a problem 25106 18:50:08,000 --> 18:50:13,279 because we're going to be calling our 25107 18:50:10,080 --> 18:50:14,800 function template with cost car pointer 25108 18:50:15,679 --> 18:50:21,359 default mechanism is going to deduce 25109 18:50:18,720 --> 18:50:24,399 a template instance that works on cost 25110 18:50:21,360 --> 18:50:26,000 car pointer arguments here and what 25111 18:50:24,399 --> 18:50:28,959 we're going to be comparing now are 25112 18:50:26,000 --> 18:50:32,000 going to be pointers to the string 25113 18:50:28,960 --> 18:50:33,512 rather than the string data itself and 25114 18:50:32,000 --> 18:50:36,240 hopefully you can see that this is going 25115 18:50:33,512 --> 18:50:38,639 to be a problem template specialization 25116 18:50:36,240 --> 18:50:41,600 is a mechanism we have to tell the 25117 18:50:38,639 --> 18:50:44,160 compiler for a template function 25118 18:50:41,600 --> 18:50:47,360 if i pass you this type please don't do 25119 18:50:44,160 --> 18:50:48,240 the default thing you do of replacing in 25120 18:50:48,240 --> 18:50:53,440 argument type for the template parameter 25121 18:50:50,960 --> 18:50:55,360 instead use the implementation that i am 25122 18:50:53,440 --> 18:50:57,600 going to give you and you tell that to 25123 18:50:55,360 --> 18:51:00,551 the compiler using the syntax we have 25124 18:50:57,600 --> 18:51:03,600 here you put a template keyword here you 25125 18:51:00,551 --> 18:51:06,319 put an empty pair of angle brackets and 25126 18:51:03,600 --> 18:51:08,240 then you specify your function the way 25127 18:51:06,320 --> 18:51:10,400 you want it you see it's going to return 25128 18:51:08,240 --> 18:51:12,639 a const card pointer the function name 25129 18:51:10,399 --> 18:51:14,399 is maximum which should be the same as 25130 18:51:14,399 --> 18:51:19,511 it's going to be specialized for const 25131 18:51:16,639 --> 18:51:21,831 car pointer so we specify an explicit 25132 18:51:19,512 --> 18:51:24,551 template argument here and then we pass 25133 18:51:21,831 --> 18:51:27,119 our parameters which are also of cost 25134 18:51:24,551 --> 18:51:29,360 card pointer and when we do this then we 25135 18:51:27,119 --> 18:51:31,440 can head in the body of the function and 25136 18:51:29,360 --> 18:51:33,512 do the comparison however we want okay 25137 18:51:31,440 --> 18:51:36,160 so how do we do this comparison here 25138 18:51:33,512 --> 18:51:38,551 here is a simple implementation of the 25139 18:51:36,160 --> 18:51:40,720 function template we just saw on the 25140 18:51:38,551 --> 18:51:42,319 previous slide you see that it really is 25141 18:51:40,720 --> 18:51:44,800 the same thing we have the template 25142 18:51:42,320 --> 18:51:47,760 keyword we have our angle brackets and 25143 18:51:44,800 --> 18:51:50,080 we have the function header here it's 25144 18:51:47,759 --> 18:51:52,319 going to return cost car pointer it's 25145 18:51:50,080 --> 18:51:55,040 going to be called maximum and an 25146 18:51:52,320 --> 18:51:57,440 explicit template argument is specified 25147 18:51:55,039 --> 18:52:00,479 within these angle brackets and then we 25148 18:51:57,440 --> 18:52:02,479 have our parameters what is really 25149 18:52:00,479 --> 18:52:04,239 important about this function is that 25150 18:52:02,479 --> 18:52:06,159 it's not going to do the comparison 25151 18:52:04,240 --> 18:52:08,080 we've been doing all along it's going to 25152 18:52:08,080 --> 18:52:12,800 and it's a c plus plus standard library 25153 18:52:10,320 --> 18:52:15,600 which is specialized for comparing c 25154 18:52:12,800 --> 18:52:17,800 strings okay if we come at cpp reference 25155 18:52:15,600 --> 18:52:21,120 here and look at a function called 25156 18:52:17,800 --> 18:52:23,040 htrcmp we have seen this before i guess 25157 18:52:21,119 --> 18:52:24,399 it is going to compare two null 25158 18:52:24,399 --> 18:52:28,871 lexicographically and you see this is 25159 18:52:26,240 --> 18:52:30,800 really what we want we have two const 25160 18:52:28,872 --> 18:52:32,320 car pointers one is going to be the 25161 18:52:30,800 --> 18:52:34,000 first parameter the other is going to be 25162 18:52:32,320 --> 18:52:36,400 the second parameter we're going to 25163 18:52:34,000 --> 18:52:38,160 compare them and look at the return 25164 18:52:36,399 --> 18:52:40,639 value we get from this function we're 25165 18:52:38,160 --> 18:52:43,192 going to get a negative return value if 25166 18:52:40,639 --> 18:52:45,919 the first parameter comes before the 25167 18:52:43,191 --> 18:52:48,080 second parameter in lexicographical 25168 18:52:45,919 --> 18:52:49,360 order we're going to get a zero if they 25169 18:52:49,360 --> 18:52:54,399 and we're going to get a positive value 25170 18:52:51,600 --> 18:52:57,120 if the first parameter comes after the 25171 18:52:54,399 --> 18:52:58,479 second parameter in lexicographical 25172 18:52:57,119 --> 18:53:00,639 order so we're going to use this 25173 18:52:58,479 --> 18:53:03,512 information here and compare 25174 18:53:00,639 --> 18:53:06,000 our two c strings here and return 25175 18:53:03,512 --> 18:53:07,600 something in visual studio code okay so 25176 18:53:06,000 --> 18:53:10,800 this is the function here it's going to 25177 18:53:07,600 --> 18:53:12,800 be using the logic from hdr cmp 25178 18:53:10,800 --> 18:53:15,192 and notice what we return we're going to 25179 18:53:15,191 --> 18:53:19,512 compares the greater to the other so 25180 18:53:17,119 --> 18:53:22,080 we're going to do strcmp we're going to 25181 18:53:19,512 --> 18:53:23,512 pass in our two arguments here and we're 25182 18:53:23,512 --> 18:53:28,551 if this returns something greater than 0 25183 18:53:31,080 --> 18:53:35,040 lexicographically this is the comparison 25184 18:53:33,191 --> 18:53:36,639 we want to do here and otherwise you're 25185 18:53:35,039 --> 18:53:38,871 going to return b and this is going to 25186 18:53:36,639 --> 18:53:41,119 compare our strings and not compare 25187 18:53:38,872 --> 18:53:43,040 pointers directly which is what we are 25188 18:53:41,119 --> 18:53:45,119 trying to avoid here and you see that 25189 18:53:43,039 --> 18:53:48,479 template specialization is really 25190 18:53:45,119 --> 18:53:51,279 helping out here now if we have this n 25191 18:53:48,479 --> 18:53:53,119 and the compiler sees a call like this 25192 18:53:51,279 --> 18:53:55,679 it's not going to compare pointers it's 25193 18:53:53,119 --> 18:53:58,080 going to use the template specialization 25194 18:53:55,679 --> 18:53:59,279 because the compiler knows that this is 25195 18:53:59,279 --> 18:54:04,319 fit for the call that we are doing here 25196 18:54:02,000 --> 18:54:06,479 because we are passing cost card 25197 18:54:04,320 --> 18:54:08,872 pointers this is how template 25198 18:54:06,479 --> 18:54:11,831 specialization works and it can really 25199 18:54:08,872 --> 18:54:14,160 come in handy if you are doing things 25200 18:54:11,831 --> 18:54:16,239 that work with pointers we are going to 25201 18:54:14,160 --> 18:54:18,160 head over to visual studio code and play 25202 18:54:16,240 --> 18:54:20,320 with this a little more okay here we are 25203 18:54:18,160 --> 18:54:22,400 in our working folder the current 25204 18:54:20,320 --> 18:54:25,192 project is template specialization we're 25205 18:54:22,399 --> 18:54:27,511 going to grab our template files 25206 18:54:25,191 --> 18:54:29,440 we're going to put them in place and 25207 18:54:27,512 --> 18:54:32,240 we're going to open this in visual 25208 18:54:29,440 --> 18:54:34,479 studio code by dragging and dropping on 25209 18:54:32,240 --> 18:54:36,080 top of visual studio code here this is 25210 18:54:34,479 --> 18:54:38,159 going to open our project and we're 25211 18:54:36,080 --> 18:54:39,919 going to clean it up a little bit and 25212 18:54:38,160 --> 18:54:41,832 we're going to close the left pane here 25213 18:54:39,919 --> 18:54:43,599 we don't need it anymore the first thing 25214 18:54:41,831 --> 18:54:46,000 we're going to do is to put in our 25215 18:54:43,600 --> 18:54:48,480 function template the same thing we have 25216 18:54:46,000 --> 18:54:50,800 been doing for a gazillion times by now 25217 18:54:48,479 --> 18:54:53,679 the maximum function is going to return 25218 18:54:50,800 --> 18:54:55,760 whichever is greater between a and b and 25219 18:54:53,679 --> 18:54:57,439 it is doing this by value after that 25220 18:54:55,759 --> 18:54:59,599 we're going to head over in the main 25221 18:54:57,440 --> 18:55:01,440 function and set up a piece of code to 25222 18:54:59,600 --> 18:55:03,360 really play with us we're going to set 25223 18:55:01,440 --> 18:55:06,960 up a few variables and we're going to 25224 18:55:03,360 --> 18:55:09,039 call our functions so we're going to say 25225 18:55:06,960 --> 18:55:11,192 maximum a and b this is going to return 25226 18:55:09,039 --> 18:55:14,000 the maximum between a and b the compiler 25227 18:55:11,191 --> 18:55:16,159 is going to deduce that the template 25228 18:55:14,000 --> 18:55:18,240 instance should be of ant type so it's 25229 18:55:16,160 --> 18:55:20,000 going to generate that and call it it's 25230 18:55:18,240 --> 18:55:22,000 going to give us the return value here 25231 18:55:20,000 --> 18:55:23,919 we can print it out and see it and we're 25232 18:55:22,000 --> 18:55:26,639 going to basically do the same 25233 18:55:23,919 --> 18:55:27,919 for double type and the string types 25234 18:55:26,639 --> 18:55:30,080 here and you're going to see that this 25235 18:55:27,919 --> 18:55:32,639 is going to work just fine so let's 25236 18:55:30,080 --> 18:55:35,919 build this we're going to use gcc to do 25237 18:55:32,639 --> 18:55:38,479 that the bolt is good we can clear and i 25238 18:55:35,919 --> 18:55:40,319 bring up a terminal to run this 25239 18:55:38,479 --> 18:55:43,679 and really see the output here we can 25240 18:55:40,320 --> 18:55:46,080 clear and run rooster you see max end is 25241 18:55:43,679 --> 18:55:48,079 23 because that's what we have here max 25242 18:55:48,080 --> 18:55:53,600 and that's what we have here and we 25243 18:55:50,720 --> 18:55:55,919 called max double to be of int type we 25244 18:55:53,600 --> 18:55:57,760 should use auto type deduction here to 25245 18:55:57,759 --> 18:56:00,959 because the compiler can do this better 25246 18:56:00,960 --> 18:56:05,280 that's what we're going to do and we're 25247 18:56:02,800 --> 18:56:08,479 going to deduce max sdr here and we're 25248 18:56:05,279 --> 18:56:10,959 going to build again let's weld with gcc 25249 18:56:08,479 --> 18:56:13,512 because that's my favorite compiler i'm 25250 18:56:10,960 --> 18:56:16,000 going to clear and run rooster now we 25251 18:56:13,512 --> 18:56:18,320 have correct output we have 23 which is 25252 18:56:18,320 --> 18:56:24,160 we have 34.7 which was deduced to be of 25253 18:56:21,919 --> 18:56:26,639 type double here and then we have the 25254 18:56:24,160 --> 18:56:28,552 maximum strength which happens to be 25255 18:56:28,551 --> 18:56:32,440 when comparing strengths the default is 25256 18:56:32,440 --> 18:56:38,240 lexicographically and what that means 25257 18:56:35,119 --> 18:56:40,000 the one that comes after the other in 25258 18:56:38,240 --> 18:56:42,800 alphabetical order is going to be 25259 18:56:40,000 --> 18:56:45,279 greater by that logic world is greater 25260 18:56:42,800 --> 18:56:47,120 than hello and it is what we are seeing 25261 18:56:45,279 --> 18:56:50,160 printed out here okay this is working 25262 18:56:47,119 --> 18:56:52,479 just fine but what if we use cost card 25263 18:56:50,160 --> 18:56:55,120 pointers and try to call our maximum 25264 18:56:52,479 --> 18:56:57,039 function template and by that i mean 25265 18:56:55,119 --> 18:56:58,159 doing something like this we have two 25266 18:56:58,160 --> 18:57:04,000 g and h which are of type cost card 25267 18:57:01,600 --> 18:57:06,080 pointers so they are c strings they are 25268 18:57:04,000 --> 18:57:08,551 going to be now terminated because we 25269 18:57:06,080 --> 18:57:10,960 are initializing them with a string 25270 18:57:08,551 --> 18:57:13,599 electron and we are comparing them here 25271 18:57:10,960 --> 18:57:15,760 we are saying maximum g and h and we're 25272 18:57:13,600 --> 18:57:18,160 going to print whichever is the maximum 25273 18:57:15,759 --> 18:57:19,919 after we return that from the function 25274 18:57:18,160 --> 18:57:21,512 when the compiler sees the call like 25275 18:57:19,919 --> 18:57:23,679 this it's going to do what it knows to 25276 18:57:21,512 --> 18:57:26,639 do it's going to replace an 25277 18:57:23,679 --> 18:57:28,399 scar pointer in our function template 25278 18:57:26,639 --> 18:57:30,080 here so it's going to return cost card 25279 18:57:28,399 --> 18:57:32,319 pointer it's going to pass the 25280 18:57:30,080 --> 18:57:33,600 parameters as coast guard pointers and 25281 18:57:32,320 --> 18:57:36,080 we're going to be comparing these 25282 18:57:33,600 --> 18:57:39,120 pointers here and we're going to return 25283 18:57:36,080 --> 18:57:41,119 whichever pointer is greater again here 25284 18:57:39,119 --> 18:57:43,191 you must be noticing the problem we're 25285 18:57:41,119 --> 18:57:45,360 going to return whichever pointer is 25286 18:57:43,191 --> 18:57:47,831 greater and we're not going to be 25287 18:57:45,360 --> 18:57:50,399 returning the actual string data we're 25288 18:57:47,831 --> 18:57:52,799 going to be returning just the address 25289 18:57:50,399 --> 18:57:55,511 and this can be really wrong because if 25290 18:57:52,800 --> 18:57:57,832 the addresses happen to be messed up 25291 18:57:55,512 --> 18:58:00,720 we're going to get the wrong output so 25292 18:57:57,831 --> 18:58:02,871 let's try and prove this again for that 25293 18:58:00,720 --> 18:58:05,040 i am going to comment out what we had on 25294 18:58:02,872 --> 18:58:06,400 top here because this could mess things 25295 18:58:06,399 --> 18:58:10,639 so i'm going to comment all this now and 25296 18:58:10,639 --> 18:58:15,360 so we're going to look at this line here 25297 18:58:15,360 --> 18:58:22,160 cos car pointer result and i say equals 25298 18:58:18,800 --> 18:58:24,160 max c mom g and h and i printed the 25299 18:58:22,160 --> 18:58:26,080 result here this is going to make it 25300 18:58:24,160 --> 18:58:29,440 easier to step into this function 25301 18:58:26,080 --> 18:58:31,440 without the noise from stdc out here so 25302 18:58:29,440 --> 18:58:33,760 we're going to set up our breakpoint 25303 18:58:33,759 --> 18:58:37,511 run tab and we're going to debug this 25304 18:58:36,000 --> 18:58:40,399 program and we're going to hit this 25305 18:58:37,512 --> 18:58:43,360 breakpoint here let's see the types that 25306 18:58:40,399 --> 18:58:45,511 we have inside our function call 25307 18:58:43,360 --> 18:58:48,720 so now that we are here we're going to 25308 18:58:45,512 --> 18:58:51,832 see that our locals are g and h and they 25309 18:58:48,720 --> 18:58:54,240 contain our strength if we go inside 25310 18:58:51,831 --> 18:58:56,399 we're going to see that g is really a 25311 18:58:54,240 --> 18:58:59,279 pointer to the first element in our 25312 18:59:03,279 --> 18:59:08,000 and h is going to be a pointer to a 25313 18:59:05,831 --> 18:59:10,479 that's what we have here okay 25314 18:59:08,000 --> 18:59:13,039 and result is going to contain some junk 25315 18:59:10,479 --> 18:59:15,599 data because it's not initialized yet 25316 18:59:13,039 --> 18:59:18,551 but we can step into this function 25317 18:59:15,600 --> 18:59:21,040 and see things happening a again is an 25318 18:59:18,551 --> 18:59:23,191 address and b is an address you see that 25319 18:59:21,039 --> 18:59:25,360 the debugger is really smart it's 25320 18:59:23,191 --> 18:59:27,512 noticing that this is a pointer to a 25321 18:59:25,360 --> 18:59:29,831 strength and it's going to show us the 25322 18:59:27,512 --> 18:59:33,360 string here but what we compare are 25323 18:59:29,831 --> 18:59:36,239 these two addresses and you see that b 25324 18:59:33,360 --> 18:59:40,160 lives in a larger address it leaves at 25325 18:59:36,240 --> 18:59:43,440 address that ends in 55 and a lives at 25326 18:59:40,160 --> 18:59:45,919 the address that ends in 50. so if we 25327 18:59:43,440 --> 18:59:48,720 really execute this function we're going 25328 18:59:45,919 --> 18:59:51,119 to return the pointer to b but you see 25329 18:59:48,720 --> 18:59:52,639 that b is not really the greater string 25330 18:59:51,119 --> 18:59:54,639 and this is really something bad you 25331 18:59:52,639 --> 18:59:56,720 should be aware of this this is what the 25332 18:59:54,639 --> 18:59:59,440 compiler is doing by default and it is 25333 18:59:56,720 --> 19:00:02,240 going to give us a result that is not 25334 18:59:59,440 --> 19:00:05,119 correct let's close this and see a way 25335 19:00:02,240 --> 19:00:07,680 we can solve this problem and we can use 25336 19:00:05,119 --> 19:00:10,319 template specialization for this what we 25337 19:00:07,679 --> 19:00:12,551 really need is a way to compare c 25338 19:00:10,320 --> 19:00:14,080 strings and not compare pointers like we 25339 19:00:12,551 --> 19:00:16,720 are doing here we're going to do that 25340 19:00:14,080 --> 19:00:18,800 using a template specialization 25341 19:00:16,720 --> 19:00:21,440 and the syntax for this to work you say 25342 19:00:18,800 --> 19:00:24,320 template and you specify an empty pair 25343 19:00:21,440 --> 19:00:26,400 of angle brackets and then you specify 25344 19:00:24,320 --> 19:00:28,160 whatever function you want to use so 25345 19:00:26,399 --> 19:00:30,079 we're going to go down here and specify 25346 19:00:28,160 --> 19:00:31,040 that we want to return the cost card 25347 19:00:31,039 --> 19:00:34,871 the function is going to be called 25348 19:00:32,399 --> 19:00:38,079 maximum we're going to specify an 25349 19:00:34,872 --> 19:00:40,400 explicit template argument which is of 25350 19:00:38,080 --> 19:00:42,872 const car pointer because we want this 25351 19:00:40,399 --> 19:00:43,831 template instance to work for scar 25352 19:00:43,831 --> 19:00:47,679 so once we have this end we're going to 25353 19:00:45,831 --> 19:00:50,639 specify our parameters we're going to 25354 19:00:47,679 --> 19:00:53,039 say cos car pointer a and cost car 25355 19:00:50,639 --> 19:00:55,759 pointer b and we're going to head into 25356 19:00:53,039 --> 19:00:58,159 the body of our function template here 25357 19:00:55,759 --> 19:01:00,479 now we have our two parameters and we're 25358 19:00:58,160 --> 19:01:02,800 going to compare them as if they were 25359 19:01:00,479 --> 19:01:04,720 const color strengths and we have seen 25360 19:01:02,800 --> 19:01:07,360 that we have a built-in facility from 25361 19:01:04,720 --> 19:01:10,639 the c plus plus standard library it is 25362 19:01:07,360 --> 19:01:12,639 this htr cmp function we can use it to 25363 19:01:10,639 --> 19:01:14,800 compare two strengths and we're going to 25364 19:01:12,639 --> 19:01:17,191 use the logic that we have here so if 25365 19:01:14,800 --> 19:01:19,360 the first parameter is less than the 25366 19:01:17,191 --> 19:01:21,191 second parameter i am talking in a 25367 19:01:19,360 --> 19:01:23,039 lexicographical order we're going to 25368 19:01:21,191 --> 19:01:24,871 return something negative if they are 25369 19:01:23,039 --> 19:01:27,511 equal dexicographically we're going to 25370 19:01:27,512 --> 19:01:33,119 first parameter is greater than the 25371 19:01:30,240 --> 19:01:36,000 second parameter lexicographically we're 25372 19:01:33,119 --> 19:01:38,871 going to return a positive value so 25373 19:01:36,000 --> 19:01:41,119 let's transform this logic into a 25374 19:01:38,872 --> 19:01:46,320 ternary operator we're going to say 25375 19:01:41,119 --> 19:01:47,599 return sdd str cmp we can say that and 25376 19:01:46,320 --> 19:01:50,160 if you have access to this function 25377 19:01:47,600 --> 19:01:52,320 we're going to need to include c string 25378 19:01:50,160 --> 19:01:55,440 let's do that we're going to include the 25379 19:01:52,320 --> 19:01:58,160 c string library and now we can say this 25380 19:01:55,440 --> 19:02:00,400 and we're going to pass in a and b and 25381 19:01:58,160 --> 19:02:02,480 we're going to ask ourselves is this 25382 19:02:00,399 --> 19:02:05,439 greater than zero okay if it's greater 25383 19:02:02,479 --> 19:02:07,360 than zero then if the first parameter is 25384 19:02:05,440 --> 19:02:09,832 greater than the second parameter let's 25385 19:02:07,360 --> 19:02:10,720 see graphically so we're going to return 25386 19:02:10,720 --> 19:02:14,320 if it's not the case we're going to 25387 19:02:12,080 --> 19:02:16,960 return b this is our ternary operator 25388 19:02:14,320 --> 19:02:19,360 here and it is going to work now that we 25389 19:02:16,960 --> 19:02:22,639 have this n the compiler is going to 25390 19:02:19,360 --> 19:02:25,039 know we have a template specialization 25391 19:02:22,639 --> 19:02:27,039 for a const car pointer if we call the 25392 19:02:25,039 --> 19:02:29,511 function like we did here calling the 25393 19:02:27,039 --> 19:02:31,759 maximum function the compiler is not 25394 19:02:29,512 --> 19:02:34,240 going to use this function template we 25395 19:02:31,759 --> 19:02:37,831 have on top here it's going to see that 25396 19:02:34,240 --> 19:02:40,240 this template specialization is better 25397 19:02:37,831 --> 19:02:42,871 for the cost card pointer type that we 25398 19:02:40,240 --> 19:02:45,120 are passing here and now if we try to 25399 19:02:42,872 --> 19:02:47,512 build and run the program the boat is 25400 19:02:45,119 --> 19:02:49,440 going to be good if we clear and run 25401 19:02:47,512 --> 19:02:51,279 rooster we're going to see that now the 25402 19:02:51,279 --> 19:02:56,319 let's see what we see if we don't have 25403 19:02:54,320 --> 19:02:58,240 this specialization and we're going to 25404 19:02:56,320 --> 19:03:00,160 comment this out because i don't think i 25405 19:02:58,240 --> 19:03:02,552 really showed you we're going to build 25406 19:03:00,160 --> 19:03:04,320 again and again if we do this 25407 19:03:02,551 --> 19:03:06,159 this function template is going to be 25408 19:03:04,320 --> 19:03:08,872 used and you know that it is going to 25409 19:03:06,160 --> 19:03:10,872 deduce a function that compares pointers 25410 19:03:08,872 --> 19:03:13,600 directly and that's going to be really 25411 19:03:10,872 --> 19:03:16,400 bad we can build this with gcc and 25412 19:03:13,600 --> 19:03:18,720 really see what we get as output we can 25413 19:03:16,399 --> 19:03:21,439 clear and run rooster now you see that 25414 19:03:18,720 --> 19:03:23,600 we are saying animal is the maximum 25415 19:03:21,440 --> 19:03:27,680 between these two guys but that's not 25416 19:03:23,600 --> 19:03:30,639 true animal is less than wild in 25417 19:03:27,679 --> 19:03:32,079 lexicographical order we need a way to 25418 19:03:32,080 --> 19:03:36,639 c strings properly and our template 25419 19:03:36,639 --> 19:03:43,039 is really doing that using the htd crmp 25420 19:03:40,240 --> 19:03:44,872 function and it is working pretty well 25421 19:03:44,872 --> 19:03:49,360 specialization is all about you're going 25422 19:03:47,039 --> 19:03:51,439 to do this if the default function 25423 19:03:49,360 --> 19:03:53,919 template you have is really not working 25424 19:03:51,440 --> 19:03:56,320 according to your needs for the type 25425 19:03:53,919 --> 19:03:58,000 that you want to use for this and use 25426 19:03:56,320 --> 19:04:00,160 this to your advantage because it's 25427 19:03:58,000 --> 19:04:02,320 available to you and now you know how to 25428 19:04:00,160 --> 19:04:04,639 use this the syntax is really simple you 25429 19:04:02,320 --> 19:04:07,192 say template and you say angle brackets 25430 19:04:04,639 --> 19:04:09,600 and then you specify whichever 25431 19:04:07,191 --> 19:04:12,000 implementation you want for your 25432 19:04:09,600 --> 19:04:14,000 specific type for example here our 25433 19:04:12,000 --> 19:04:16,872 specific type for which we want to 25434 19:04:14,000 --> 19:04:17,759 specify this function template is cost 25435 19:04:17,759 --> 19:04:22,399 you specify that as an explicit 25436 19:04:20,551 --> 19:04:25,039 template argument and then you specify 25437 19:04:22,399 --> 19:04:27,360 your return value your parameters and 25438 19:04:25,039 --> 19:04:29,759 you do whatever logic you need to do to 25439 19:04:27,360 --> 19:04:32,320 do things right in the body of this 25440 19:04:29,759 --> 19:04:34,551 template specialization here i would 25441 19:04:32,320 --> 19:04:36,240 like to welcome you in this new chapter 25442 19:04:34,551 --> 19:04:40,000 where we're going to be learning about 25443 19:04:36,240 --> 19:04:42,320 concepts concepts are a mechanism we can 25444 19:04:42,320 --> 19:04:47,512 restrictions on the template parameters 25445 19:04:45,039 --> 19:04:49,439 in our function templates for example we 25446 19:04:47,512 --> 19:04:51,680 can use this technique to say that we 25447 19:04:49,440 --> 19:04:53,680 want our function to be only called with 25448 19:04:51,679 --> 19:04:55,919 integers and if you call it with 25449 19:04:53,679 --> 19:04:58,079 something that isn't an integer it's 25450 19:04:55,919 --> 19:05:00,551 going to give you a compiler one thing i 25451 19:04:58,080 --> 19:05:03,279 should emphasize is that concepts are 25452 19:05:00,551 --> 19:05:05,119 one of the big four features in c plus 25453 19:05:03,279 --> 19:05:07,279 plus 20. so we're going to be learning 25454 19:05:05,119 --> 19:05:09,440 about this in this chapter we have just 25455 19:05:07,279 --> 19:05:12,551 said that we use them to place 25456 19:05:09,440 --> 19:05:14,400 constraints on our template parameters 25457 19:05:12,551 --> 19:05:16,872 but we saw that we could do something 25458 19:05:14,399 --> 19:05:19,119 like that in the last chapter where we 25459 19:05:16,872 --> 19:05:21,680 could use type trades and static 25460 19:05:19,119 --> 19:05:22,959 concerns for example if you look at the 25461 19:05:21,679 --> 19:05:25,359 function here it's going to print a 25462 19:05:22,960 --> 19:05:28,160 number but we have a requirement for 25463 19:05:25,360 --> 19:05:30,240 this number to be an integral type 25464 19:05:28,160 --> 19:05:32,480 through the static assert that we have 25465 19:05:30,240 --> 19:05:35,440 here we're going to see that we can do 25466 19:05:32,479 --> 19:05:37,759 something really similar with concepts 25467 19:05:35,440 --> 19:05:40,832 but it's going to be much cleaner and 25468 19:05:37,759 --> 19:05:43,039 that the logic to enforce our 25469 19:05:40,831 --> 19:05:45,191 constraints is not going to be in the 25470 19:05:43,039 --> 19:05:47,360 function body it's going to be somewhat 25471 19:05:45,191 --> 19:05:49,440 in the function declaration in the 25472 19:05:47,360 --> 19:05:50,872 constraints that we put on the template 25473 19:05:49,440 --> 19:05:53,760 parameter here we're going to see 25474 19:05:50,872 --> 19:05:56,080 specific syntaxes of how you can do this 25475 19:05:53,759 --> 19:05:59,039 starting in the next lecture there are 25476 19:05:56,080 --> 19:06:01,279 two sides to concepts in c plus plus 25477 19:05:59,039 --> 19:06:03,119 there are standard built-in concepts 25478 19:06:01,279 --> 19:06:05,759 that come with the c plus plus 25479 19:06:03,119 --> 19:06:08,399 programming language but if for some 25480 19:06:05,759 --> 19:06:10,319 reason these happen not to be enough you 25481 19:06:08,399 --> 19:06:12,319 can build your own and we're going to 25482 19:06:10,320 --> 19:06:15,040 see how we can do that later in this 25483 19:06:12,320 --> 19:06:17,280 chapter here are a few examples of 25484 19:06:15,039 --> 19:06:19,599 concepts you can use in your c plus plus 25485 19:06:17,279 --> 19:06:21,919 program you can use the integral 25486 19:06:19,600 --> 19:06:24,320 concepts to enforce that your parameters 25487 19:06:21,919 --> 19:06:26,399 be integrals you can use the floating 25488 19:06:24,320 --> 19:06:29,040 point concept to enforce that your 25489 19:06:26,399 --> 19:06:31,119 template parameters be of floating point 25490 19:06:29,039 --> 19:06:33,919 type you can do all kinds of crazy 25491 19:06:31,119 --> 19:06:35,440 things okay concepts are really cool and 25492 19:06:33,919 --> 19:06:38,240 they are going to make your function 25493 19:06:35,440 --> 19:06:40,320 templates much safer to work with we are 25494 19:06:38,240 --> 19:06:42,872 going to stop here in this lecture and 25495 19:06:40,320 --> 19:06:45,040 show you how you can use concepts 25496 19:06:42,872 --> 19:06:47,760 starting in the next lecture go ahead 25497 19:06:45,039 --> 19:06:49,599 and finish up here and meet me there in 25498 19:06:47,759 --> 19:06:53,279 this lecture we're going to learn about 25499 19:06:49,600 --> 19:06:56,400 concepts again concepts are a mechanism 25500 19:06:53,279 --> 19:06:59,831 we have in place in c plus 20 to place 25501 19:06:56,399 --> 19:07:01,759 constraints on our function templates 25502 19:06:59,831 --> 19:07:03,119 for example we can use concepts to 25503 19:07:03,119 --> 19:07:08,551 that we want our function template to 25504 19:07:05,600 --> 19:07:10,639 only be called with integers or doubles 25505 19:07:10,639 --> 19:07:15,512 and when somebody violates our concept 25506 19:07:13,679 --> 19:07:17,511 they're going to get a compiler error in 25507 19:07:15,512 --> 19:07:19,360 this lecture i am going to show you how 25508 19:07:17,512 --> 19:07:21,832 you can set up your concept and the 25509 19:07:19,360 --> 19:07:23,600 different syntaxes you can use and how 25510 19:07:23,600 --> 19:07:28,480 but before we do that please note that 25511 19:07:25,919 --> 19:07:31,360 there are two sides to concepts there 25512 19:07:28,479 --> 19:07:34,319 are concepts that are standard and built 25513 19:07:31,360 --> 19:07:36,399 into the c plus plus standard library 25514 19:07:34,320 --> 19:07:37,920 and those are ready to use and these are 25515 19:07:36,399 --> 19:07:40,720 what we're going to be using in this 25516 19:07:37,919 --> 19:07:43,360 lecture but know that you can also build 25517 19:07:40,720 --> 19:07:45,759 your own concepts and use them to do 25518 19:07:43,360 --> 19:07:48,320 whatever it is you want to do here are a 25519 19:07:45,759 --> 19:07:50,319 few examples of built-in concepts we can 25520 19:07:48,320 --> 19:07:53,192 use from the c plus plus standard 25521 19:07:50,320 --> 19:07:55,040 library we have for example a concept we 25522 19:07:55,039 --> 19:08:00,639 that people call our functions only with 25523 19:07:57,679 --> 19:08:02,551 integral types if they violate this 25524 19:08:00,639 --> 19:08:04,800 they're going to get a compiler error we 25525 19:08:02,551 --> 19:08:07,039 can also enforce for our template 25526 19:08:04,800 --> 19:08:09,192 parameters to be floating point we can 25527 19:08:07,039 --> 19:08:11,279 do all kinds of crazy things okay now 25528 19:08:09,191 --> 19:08:13,759 that you know this let's see how we can 25529 19:08:11,279 --> 19:08:16,240 set up a concept on our function 25530 19:08:13,759 --> 19:08:18,239 templates and this is the first syntax 25531 19:08:16,240 --> 19:08:21,120 we're going to look at all you really 25532 19:08:18,240 --> 19:08:23,192 have to do is to put in your template 25533 19:08:21,119 --> 19:08:26,159 declaration like we have been doing all 25534 19:08:23,191 --> 19:08:29,679 along you also have to add in the 25535 19:08:26,160 --> 19:08:31,512 requires keyword here after the requires 25536 19:08:29,679 --> 19:08:34,799 keyword you're going to specify your 25537 19:08:31,512 --> 19:08:37,512 concept and specify your template 25538 19:08:34,800 --> 19:08:39,919 parameter in angle brackets here 25539 19:08:37,512 --> 19:08:42,240 when you have this thing in front of 25540 19:08:39,919 --> 19:08:44,551 your function template like we have here 25541 19:08:42,240 --> 19:08:47,680 the function template can only work 25542 19:08:44,551 --> 19:08:50,399 right if the concept in here is 25543 19:08:47,679 --> 19:08:53,279 satisfied and this concept in this case 25544 19:08:50,399 --> 19:08:56,079 is going to be satisfied if somebody 25545 19:08:53,279 --> 19:08:59,119 tries to call this function template 25546 19:08:56,080 --> 19:09:00,872 only with integral types if they violate 25547 19:08:59,119 --> 19:09:03,191 that again they are going to get a 25548 19:09:00,872 --> 19:09:05,680 compiler error okay once we have this 25549 19:09:03,191 --> 19:09:08,799 concept set up we can look at the piece 25550 19:09:05,679 --> 19:09:11,119 of code that tries to use it and try to 25551 19:09:08,800 --> 19:09:12,800 guess what is going to happen here is a 25552 19:09:11,119 --> 19:09:15,191 piece of code we would set up in the 25553 19:09:12,800 --> 19:09:17,919 main function and try to call a function 25554 19:09:15,191 --> 19:09:20,479 template we have two variables of car 25555 19:09:17,919 --> 19:09:23,679 type we know that car is an integral 25556 19:09:20,479 --> 19:09:25,831 type so if we issue a call like this 25557 19:09:23,679 --> 19:09:28,159 this call is going to work the template 25558 19:09:25,831 --> 19:09:29,919 instance is going to be generated and 25559 19:09:28,160 --> 19:09:32,080 it's going to be called and give us the 25560 19:09:29,919 --> 19:09:35,039 results that we can print here this is 25561 19:09:32,080 --> 19:09:38,160 right on the second piece of code here 25562 19:09:35,039 --> 19:09:40,000 we have two variables of type and 25563 19:09:38,160 --> 19:09:42,639 we're going to call them we're going to 25564 19:09:40,000 --> 19:09:45,512 call our function template with and 25565 19:09:42,639 --> 19:09:47,279 and this is also an integral type int is 25566 19:09:45,512 --> 19:09:50,000 an integral type so this is going to 25567 19:09:47,279 --> 19:09:51,600 generate our function template instance 25568 19:09:50,000 --> 19:09:54,240 and we're going to call it and get the 25569 19:09:51,600 --> 19:09:56,800 results that we print here and if we try 25570 19:09:54,240 --> 19:09:58,400 to call our function template with 25571 19:09:58,399 --> 19:10:02,399 the concept is not going to be satisfied 25572 19:10:00,639 --> 19:10:04,960 and we're going to get some kind of 25573 19:10:02,399 --> 19:10:06,871 error and our function template instance 25574 19:10:04,960 --> 19:10:09,832 is not going to be generated this is 25575 19:10:06,872 --> 19:10:13,120 really how you can use concepts to force 25576 19:10:09,831 --> 19:10:15,439 some rules on users of your function 25577 19:10:13,119 --> 19:10:17,512 templates if i can say it like that we 25578 19:10:15,440 --> 19:10:19,512 have been able to do something like this 25579 19:10:17,512 --> 19:10:22,240 using typed traits and the static 25580 19:10:19,512 --> 19:10:24,872 asserts in the last chapter but concepts 25581 19:10:22,240 --> 19:10:27,040 are much cleaner and that they are kind 25582 19:10:24,872 --> 19:10:28,800 of built into the signature of your 25583 19:10:27,039 --> 19:10:30,871 function you don't have to go in the 25584 19:10:28,800 --> 19:10:33,440 body of your function and set up static 25585 19:10:30,872 --> 19:10:36,720 asserts the function is already 25586 19:10:33,440 --> 19:10:38,800 supporting concepts when you declare and 25587 19:10:36,720 --> 19:10:40,960 define it and this is really cool okay 25588 19:10:38,800 --> 19:10:43,192 this is our first syntax we can use to 25589 19:10:43,191 --> 19:10:47,679 for our function templates using the 25590 19:10:45,360 --> 19:10:50,479 requires close after our template 25591 19:10:47,679 --> 19:10:53,191 declaration and following that with 25592 19:10:50,479 --> 19:10:55,360 whatever concept we want to enforce on 25593 19:10:53,191 --> 19:10:57,512 our function template here please note 25594 19:10:55,360 --> 19:10:58,720 that you can also use type traits 25595 19:10:58,720 --> 19:11:03,680 in the required clause here and this is 25596 19:11:00,720 --> 19:11:05,680 going to work and this really says that 25597 19:11:03,679 --> 19:11:07,279 all you really have to put after the 25598 19:11:07,279 --> 19:11:13,679 is an expression that can be evaluated 25599 19:11:09,919 --> 19:11:16,639 at compile time but also results in a 25600 19:11:13,679 --> 19:11:19,359 boolean value once you satisfy these few 25601 19:11:16,639 --> 19:11:21,512 requirements you can really set up your 25602 19:11:19,360 --> 19:11:24,639 concept however you want you just have 25603 19:11:21,512 --> 19:11:27,680 to be careful that the expiration can be 25604 19:11:24,639 --> 19:11:30,551 evaluated at compile time and that it 25605 19:11:27,679 --> 19:11:32,719 evaluates to a boolean value and when 25606 19:11:30,551 --> 19:11:35,599 the expression evaluates to true the 25607 19:11:32,720 --> 19:11:37,831 concept is going to succeed if the 25608 19:11:35,600 --> 19:11:39,600 expression evaluates to false you're 25609 19:11:37,831 --> 19:11:42,080 going to get a compiler error because 25610 19:11:39,600 --> 19:11:44,160 the concept is not satisfied this is how 25611 19:11:42,080 --> 19:11:46,800 these things work now that we have seen 25612 19:11:44,160 --> 19:11:48,872 the first syntax i think it is big time 25613 19:11:46,800 --> 19:11:51,760 we looked at the second way you can set 25614 19:11:48,872 --> 19:11:53,919 up concepts and that's going to be in 25615 19:11:51,759 --> 19:11:55,831 your template declaration you're going 25616 19:11:53,919 --> 19:11:58,399 to say template put in your angle 25617 19:11:55,831 --> 19:12:00,639 brackets and inside instead of saying 25618 19:11:58,399 --> 19:12:03,511 type name t like we have been doing all 25619 19:12:00,639 --> 19:12:06,872 along you just specify your concept 25620 19:12:03,512 --> 19:12:09,760 directly and save the template parameter 25621 19:12:06,872 --> 19:12:12,080 like this once you do this this concept 25622 19:12:09,759 --> 19:12:14,159 is going to be enforced on this function 25623 19:12:12,080 --> 19:12:16,160 template here this is just another 25624 19:12:14,160 --> 19:12:18,720 syntax to really do the same thing we 25625 19:12:16,160 --> 19:12:21,512 also have a third syntax we can use if 25626 19:12:18,720 --> 19:12:23,919 we use auto to set up our function 25627 19:12:21,512 --> 19:12:26,872 templates and the way this works is to 25628 19:12:23,919 --> 19:12:30,000 just add your concept in front of the 25629 19:12:26,872 --> 19:12:31,919 auto keyword and once you do this the 25630 19:12:30,000 --> 19:12:33,919 compiler is going to enforce for the 25631 19:12:31,919 --> 19:12:35,279 first parameter and the second parameter 25632 19:12:35,279 --> 19:12:41,279 to satisfy the integral concepts so if 25633 19:12:38,960 --> 19:12:43,360 you have to use auto to set up function 25634 19:12:41,279 --> 19:12:45,600 templates this is how you can use 25635 19:12:45,600 --> 19:12:50,000 ways in which people use your function 25636 19:12:47,831 --> 19:12:52,871 we still have another way we can use 25637 19:12:50,000 --> 19:12:54,720 concepts in c plus plus and this is how 25638 19:12:52,872 --> 19:12:56,872 you do it you specify your template 25639 19:12:54,720 --> 19:12:59,759 declaration like we have been doing all 25640 19:12:56,872 --> 19:13:02,872 along you specify your function template 25641 19:12:59,759 --> 19:13:05,119 but after the parameter list as we see 25642 19:13:02,872 --> 19:13:07,600 here you're going to put your requires 25643 19:13:05,119 --> 19:13:10,159 close and you can put that in this place 25644 19:13:07,600 --> 19:13:12,552 here and it is going to work i realize 25645 19:13:10,160 --> 19:13:14,552 these are many syntaxes to process for 25646 19:13:12,551 --> 19:13:16,479 the first time but some of these 25647 19:13:14,551 --> 19:13:19,440 syntaxes are going to work better in 25648 19:13:16,479 --> 19:13:21,759 some situations and they are there just 25649 19:13:19,440 --> 19:13:24,240 to make it flexible for you to use 25650 19:13:21,759 --> 19:13:26,239 concepts in your c plus plus code okay 25651 19:13:24,240 --> 19:13:28,400 now that we have seen different ways we 25652 19:13:26,240 --> 19:13:30,552 can set up concepts for our function 25653 19:13:28,399 --> 19:13:32,871 templates i think this big time we 25654 19:13:30,551 --> 19:13:35,679 headed over to visual studio code and 25655 19:13:32,872 --> 19:13:37,832 tried them out okay here we are in our 25656 19:13:35,679 --> 19:13:40,079 working folder the current chapter is 25657 19:13:37,831 --> 19:13:42,639 concepts the current lecture is using 25658 19:13:40,080 --> 19:13:45,279 concepts we're going to grab our 25659 19:13:42,639 --> 19:13:46,479 template files we are going to put them 25660 19:13:46,479 --> 19:13:50,800 and we are going to open this thing in 25661 19:13:48,800 --> 19:13:53,279 visual studio code by dragging and 25662 19:13:50,800 --> 19:13:55,832 dropping on top of visual studio code 25663 19:13:53,279 --> 19:13:57,831 here we do that this is going to open 25664 19:13:55,831 --> 19:14:01,439 our folder we're going to open up our 25665 19:13:57,831 --> 19:14:02,959 main cpp file and close the sidebar here 25666 19:14:01,440 --> 19:14:04,551 and we're going to clean things up a 25667 19:14:02,960 --> 19:14:06,320 little bit and we're going to set up a 25668 19:14:04,551 --> 19:14:08,000 function template which is going to add 25669 19:14:06,320 --> 19:14:10,080 two numbers we already know how to do 25670 19:14:08,000 --> 19:14:12,399 that so we're going to say template type 25671 19:14:10,080 --> 19:14:14,872 name and it's going to have a template 25672 19:14:12,399 --> 19:14:17,039 parameter of t then we're going to set 25673 19:14:14,872 --> 19:14:19,192 up the function like we have been doing 25674 19:14:17,039 --> 19:14:22,079 all along we're going to say add we're 25675 19:14:19,191 --> 19:14:25,039 going to say t a and t b and this is 25676 19:14:22,080 --> 19:14:27,680 going to return the sum of a and b once 25677 19:14:25,039 --> 19:14:30,079 you have the function in we can go in 25678 19:14:27,679 --> 19:14:32,399 the main function and set up code that 25679 19:14:30,080 --> 19:14:34,551 would call this function and if we try 25680 19:14:32,399 --> 19:14:37,759 to compile this program it is going to 25681 19:14:34,551 --> 19:14:40,239 work now because we are not placing any 25682 19:14:37,759 --> 19:14:43,039 constraints on this function template 25683 19:14:40,240 --> 19:14:45,192 here let's try to build with gcc like we 25684 19:14:43,039 --> 19:14:47,831 have been doing all along you see it is 25685 19:14:45,191 --> 19:14:50,159 going to weld okay you see the world is 25686 19:14:47,831 --> 19:14:52,239 good world finished successfully and if 25687 19:14:50,160 --> 19:14:54,639 we want we can run this application and 25688 19:14:52,240 --> 19:14:57,760 it is going to print result a 25689 19:14:54,639 --> 19:14:59,679 result b let's take this out because we 25690 19:14:57,759 --> 19:15:02,000 haven't done this yet but it's not going 25691 19:14:59,679 --> 19:15:03,919 to print result c but if we want we can 25692 19:15:02,000 --> 19:15:04,960 really print it it's going to work so 25693 19:15:04,960 --> 19:15:11,120 put that in and we're going to say 25694 19:15:07,119 --> 19:15:13,440 result c and print result c here and 25695 19:15:11,119 --> 19:15:14,799 world again let's world now to take this 25696 19:15:14,800 --> 19:15:19,600 world is good we can bring up a terminal 25697 19:15:16,800 --> 19:15:22,400 to try this out and we can clear and run 25698 19:15:19,600 --> 19:15:24,872 rooster now you see result a is 30 25699 19:15:22,399 --> 19:15:27,679 because that's the sum of a and b here 25700 19:15:24,872 --> 19:15:31,360 result b should be 16 which is what we 25701 19:15:27,679 --> 19:15:34,000 have result c is 13 which is what we 25702 19:15:31,360 --> 19:15:35,279 should get by adding these two double 25703 19:15:35,279 --> 19:15:41,919 now we wanted to constrain this function 25704 19:15:38,479 --> 19:15:44,239 to only work if somebody passes integral 25705 19:15:41,919 --> 19:15:46,551 types and when we put that constraint in 25706 19:15:44,240 --> 19:15:49,760 place it is going to work if we add up 25707 19:15:46,551 --> 19:15:52,159 two character variables because they are 25708 19:15:49,759 --> 19:15:54,319 integral types it's going to work if we 25709 19:15:52,160 --> 19:15:57,040 add up two integer variables because 25710 19:15:54,320 --> 19:15:58,552 they are integral types but once we hit 25711 19:15:58,551 --> 19:16:03,759 with the double variables we should get 25712 19:16:00,551 --> 19:16:05,919 a compiler error because the constraint 25713 19:16:03,759 --> 19:16:08,159 is not satisfied let's do that we're 25714 19:16:05,919 --> 19:16:11,039 going to comment the double thing first 25715 19:16:08,160 --> 19:16:13,120 because we want to really isolate it and 25716 19:16:11,039 --> 19:16:14,719 see it and see the compiler error that 25717 19:16:13,119 --> 19:16:17,440 we get and we're going to add a 25718 19:16:14,720 --> 19:16:20,080 constraint to our function template 25719 19:16:17,440 --> 19:16:22,479 using concepts okay if you want to use 25720 19:16:20,080 --> 19:16:24,479 concepts in c plus plus 20 the first 25721 19:16:22,479 --> 19:16:27,440 thing you need to do is to include the 25722 19:16:24,479 --> 19:16:28,959 library that gives us access to concepts 25723 19:16:27,440 --> 19:16:31,512 and we're going to do that by saying 25724 19:16:28,960 --> 19:16:33,440 include concepts this is easy now that 25725 19:16:31,512 --> 19:16:36,080 we have this end we're going to use the 25726 19:16:33,440 --> 19:16:38,400 first syntax we saw in the slides and 25727 19:16:36,080 --> 19:16:40,551 we're going to add the requires close 25728 19:16:38,399 --> 19:16:43,511 after the template declaration 25729 19:16:40,551 --> 19:16:45,039 and we're going to say std integral and 25730 19:16:43,512 --> 19:16:46,639 we're going to say the template 25731 19:16:45,039 --> 19:16:49,599 parameter here i think this is the 25732 19:16:46,639 --> 19:16:52,399 syntax okay watch what happens if we do 25733 19:16:49,600 --> 19:16:54,639 this down in the code we are calling the 25734 19:16:55,512 --> 19:17:00,400 integer types so this is going to 25735 19:16:57,191 --> 19:17:03,191 compile fine because the concept here 25736 19:17:00,399 --> 19:17:05,679 is satisfied this concept is going to 25737 19:17:03,191 --> 19:17:08,720 enforce for the template parameters we 25738 19:17:05,679 --> 19:17:12,079 call a function template with to be 25739 19:17:08,720 --> 19:17:14,960 integral types let's build and see if we 25740 19:17:12,080 --> 19:17:16,872 get to compile successfully you see both 25741 19:17:14,960 --> 19:17:19,040 finished good everything is going to 25742 19:17:16,872 --> 19:17:21,279 work and if we run our program we should 25743 19:17:19,039 --> 19:17:23,679 see results a and result to be printed 25744 19:17:21,279 --> 19:17:25,919 out let's run the program we see our 25745 19:17:23,679 --> 19:17:29,759 result here now we're going to comment 25746 19:17:25,919 --> 19:17:32,000 out the others and leave in the double 25747 19:17:29,759 --> 19:17:33,831 example and we're going to uncomment 25748 19:17:32,000 --> 19:17:36,399 that and i want you to see the problem 25749 19:17:36,399 --> 19:17:40,479 our function template with double 25750 19:17:38,160 --> 19:17:42,400 variables here you see visual studio 25751 19:17:40,479 --> 19:17:44,800 code is already complaining it is saying 25752 19:17:42,399 --> 19:17:47,279 there is something wrong if we look at 25753 19:17:44,800 --> 19:17:48,960 the problems tab is going to say no 25754 19:17:47,279 --> 19:17:50,959 instance of function template add 25755 19:17:48,960 --> 19:17:53,440 matches the argument list but this is 25756 19:17:50,960 --> 19:17:56,639 really not descriptive enough but the 25757 19:17:53,440 --> 19:17:59,360 problem is our concept is not satisfied 25758 19:17:56,639 --> 19:18:03,119 by calling our function template with 25759 19:17:59,360 --> 19:18:05,600 double types because we clearly said 25760 19:18:03,119 --> 19:18:08,551 that we want the function to only be 25761 19:18:05,600 --> 19:18:10,480 called with integral types let's build 25762 19:18:08,551 --> 19:18:12,080 our program we're going to pass this 25763 19:18:12,080 --> 19:18:16,800 and we're going to get a compiler error 25764 19:18:14,240 --> 19:18:18,320 that says that some concept was 25765 19:18:18,320 --> 19:18:25,440 and we see we see in function main 25766 19:18:21,512 --> 19:18:28,479 use of function t add with double with 25767 19:18:25,440 --> 19:18:30,720 unsatisfied constraints and this is 25768 19:18:28,479 --> 19:18:32,639 really clear when we see an error like 25769 19:18:30,720 --> 19:18:35,191 this we will go and look at the 25770 19:18:32,639 --> 19:18:37,360 constraints that the function has and 25771 19:18:35,191 --> 19:18:39,599 we're going to see if we satisfied those 25772 19:18:39,600 --> 19:18:42,960 like we did in the main function here 25773 19:18:41,191 --> 19:18:45,599 and we will know that the function 25774 19:18:42,960 --> 19:18:48,080 doesn't support non-integral types and 25775 19:18:45,600 --> 19:18:50,552 we will fix this problem this is really 25776 19:18:50,551 --> 19:18:55,679 in writing safer function templates now 25777 19:18:53,440 --> 19:18:57,832 that we have seen the first syntax i 25778 19:18:55,679 --> 19:19:00,479 think it is big time we looked at the 25779 19:18:57,831 --> 19:19:01,919 other syntaxes so let's do that let's 25780 19:19:00,479 --> 19:19:04,872 copy the code here because we're going 25781 19:19:01,919 --> 19:19:07,119 to be reusing this a couple of times and 25782 19:19:04,872 --> 19:19:10,240 adapt it instead of typing this entire 25783 19:19:07,119 --> 19:19:12,959 thing the second syntax involves getting 25784 19:19:10,240 --> 19:19:16,480 rid of the requires close all together 25785 19:19:12,960 --> 19:19:19,192 and just using the concept instead of 25786 19:19:16,479 --> 19:19:21,599 type name here so i'm going to say std 25787 19:19:19,191 --> 19:19:23,919 integral and this is going to be enough 25788 19:19:21,600 --> 19:19:27,040 this is going to tell the compiler to 25789 19:19:23,919 --> 19:19:27,831 enforce this concept for this function 25790 19:19:27,831 --> 19:19:33,919 for all the template parameters that go 25791 19:19:31,191 --> 19:19:36,399 in the place of t here i hope this makes 25792 19:19:33,919 --> 19:19:38,720 sense now if we go back we see that we 25793 19:19:36,399 --> 19:19:41,039 still have our squiggly lines here if we 25794 19:19:38,720 --> 19:19:43,360 try to compile we will get the same 25795 19:19:41,039 --> 19:19:46,319 compiler error okay and you see we have 25796 19:19:43,360 --> 19:19:49,191 the same compiler error function add t 25797 19:19:46,320 --> 19:19:50,800 called with unsatisfied constraints i 25798 19:19:49,191 --> 19:19:53,599 think if you go down we're going to see 25799 19:19:50,800 --> 19:19:57,832 even more stuff out of this it's going 25800 19:19:53,600 --> 19:20:00,639 to say required the satisfaction of std 25801 19:19:57,831 --> 19:20:03,191 integral with t but now we are trying to 25802 19:20:00,639 --> 19:20:06,240 call this with double so this is really 25803 19:20:03,191 --> 19:20:08,159 going to lead to a compiler error so 25804 19:20:06,240 --> 19:20:10,400 this is the second syntax and it is 25805 19:20:08,160 --> 19:20:13,192 going to do the same thing now that we 25806 19:20:10,399 --> 19:20:15,599 have seen this we also have a third way 25807 19:20:13,191 --> 19:20:18,720 we can do this and that's through the 25808 19:20:15,600 --> 19:20:21,279 auto mechanism let's set up a function 25809 19:20:18,720 --> 19:20:24,320 using auto so we're going to say auto 25810 19:20:21,279 --> 19:20:26,319 add auto a and auto b we have seen that 25811 19:20:24,320 --> 19:20:28,720 we can do that in the last chapter and 25812 19:20:26,320 --> 19:20:31,120 we're going to return a and b a plus b i 25813 19:20:28,720 --> 19:20:33,360 should say if we do this you see that 25814 19:20:31,119 --> 19:20:35,919 the squiggly line goes away and we can 25815 19:20:33,360 --> 19:20:37,831 add these things up and print the result 25816 19:20:35,919 --> 19:20:40,240 the return type is going to be deduced 25817 19:20:37,831 --> 19:20:41,831 by the compiler automatically but know 25818 19:20:40,240 --> 19:20:44,240 that this is going to generate a 25819 19:20:41,831 --> 19:20:46,319 function template behind the scenes this 25820 19:20:44,240 --> 19:20:48,960 is just a nicer syntax we have to use 25821 19:20:46,320 --> 19:20:52,000 auto to set up functions that work for 25822 19:20:48,960 --> 19:20:55,040 multiple types let's build and uh see 25823 19:20:52,000 --> 19:20:57,831 how gcc handles this the world is good 25824 19:20:55,039 --> 19:21:00,159 because we have no constraints here and 25825 19:20:57,831 --> 19:21:03,119 if we run our program it's going to work 25826 19:21:00,160 --> 19:21:07,440 it's going to give us 13 as the sum of 25827 19:21:03,119 --> 19:21:09,512 11.1 and 1.9 but we can also place 25828 19:21:07,440 --> 19:21:12,639 constraints on this function here and 25829 19:21:09,512 --> 19:21:15,360 you do that by specifying your concept 25830 19:21:12,639 --> 19:21:18,639 in front of the auto keyword here so for 25831 19:21:15,360 --> 19:21:21,039 example we can say std integral and std 25832 19:21:18,639 --> 19:21:23,759 integral fold the second parameter here 25833 19:21:21,039 --> 19:21:26,719 let's say that and once we do this if we 25834 19:21:23,759 --> 19:21:28,551 go down we should see our squiggly line 25835 19:21:26,720 --> 19:21:31,360 on the call of the function here because 25836 19:21:28,551 --> 19:21:34,159 now the way we are calling this function 25837 19:21:31,360 --> 19:21:37,512 is not valid the function clearly says 25838 19:21:34,160 --> 19:21:41,440 that it wants to be called only with 25839 19:21:37,512 --> 19:21:43,680 arguments that satisfy this concept here 25840 19:21:41,440 --> 19:21:45,279 if we're trying to build again with gcc 25841 19:21:43,679 --> 19:21:47,599 we're going to get the semicompiler 25842 19:21:45,279 --> 19:21:50,119 error let's do that and it's going to 25843 19:21:50,119 --> 19:21:55,831 unsatisfied constraints the same error 25844 19:21:52,872 --> 19:21:58,720 and if we go down we should see that it 25845 19:21:58,720 --> 19:22:03,831 concept to be satisfied here so this is 25846 19:22:01,759 --> 19:22:06,479 the third syntax you can use if you 25847 19:22:03,831 --> 19:22:08,639 happen to be using the auto syntax for 25848 19:22:06,479 --> 19:22:10,639 your function templates let's comment 25849 19:22:08,639 --> 19:22:12,872 this out because we still have another 25850 19:22:10,639 --> 19:22:15,360 syntax we need to learn about this is 25851 19:22:12,872 --> 19:22:17,680 going to be our syntax three and we're 25852 19:22:15,360 --> 19:22:19,440 going to comment this out and the last 25853 19:22:19,440 --> 19:22:23,832 just leaving in our function template 25854 19:22:22,160 --> 19:22:26,080 let's copy this because i don't want to 25855 19:22:23,831 --> 19:22:28,159 type this again we're going to go down 25856 19:22:26,080 --> 19:22:30,240 and say syntax form and put in our 25857 19:22:28,160 --> 19:22:32,960 function template we're going to take 25858 19:22:30,240 --> 19:22:36,240 this requires close we're going to cut 25859 19:22:32,960 --> 19:22:38,552 it out and we're going to put it after 25860 19:22:38,551 --> 19:22:41,191 like this and this is going to be valid 25861 19:22:41,191 --> 19:22:46,799 syntax to enforce a concept on your 25862 19:22:44,160 --> 19:22:49,360 function template if we go down you see 25863 19:22:46,800 --> 19:22:51,440 we have this wiggly line and i should 25864 19:22:49,360 --> 19:22:54,000 prove that this works with other 25865 19:22:51,440 --> 19:22:56,551 integral types here let's just comment 25866 19:22:54,000 --> 19:22:59,919 out the double line here and uncomment 25867 19:22:56,551 --> 19:23:01,919 the other we had for car and ant if we 25868 19:22:59,919 --> 19:23:03,679 try to build this program now it is 25869 19:23:01,919 --> 19:23:06,639 going to work fine because the 25870 19:23:03,679 --> 19:23:09,831 constraints are satisfied but the moment 25871 19:23:06,639 --> 19:23:12,080 we uncomment the calls with double types 25872 19:23:09,831 --> 19:23:14,551 we're going to get a squiggly line which 25873 19:23:12,080 --> 19:23:17,440 is a good warning that we have a problem 25874 19:23:14,551 --> 19:23:19,919 but if we ignore the squiggly lines 25875 19:23:17,440 --> 19:23:22,720 and adjust build with gcc we're going to 25876 19:23:19,919 --> 19:23:25,512 get the same compiler error and it's 25877 19:23:22,720 --> 19:23:27,759 going to say that to call our function 25878 19:23:27,759 --> 19:23:33,279 the integral concept for the arguments 25879 19:23:31,191 --> 19:23:35,512 that we pass here and the double 25880 19:23:33,279 --> 19:23:37,759 arguments we're passing in here are 25881 19:23:35,512 --> 19:23:40,080 violating our concept so we get a 25882 19:23:37,759 --> 19:23:43,191 compiler error this is really how you 25883 19:23:40,080 --> 19:23:45,680 can use concepts in c plus plus i tried 25884 19:23:43,191 --> 19:23:47,831 and came up with simple examples to show 25885 19:23:45,679 --> 19:23:49,359 you different syntaxes you can use 25886 19:23:49,360 --> 19:23:54,960 and this is really it try to get 25887 19:23:52,000 --> 19:23:57,191 yourself familiar with these syntaxes 25888 19:23:54,960 --> 19:23:59,120 and it's not going to be really hard to 25889 19:23:57,191 --> 19:24:01,919 use concepts in your c plus plus 25890 19:23:59,119 --> 19:24:04,551 programs okay this is really all i had 25891 19:24:01,919 --> 19:24:07,759 to share in terms of the syntaxes you 25892 19:24:04,551 --> 19:24:09,512 can use with c plus plus 20 concepts if 25893 19:24:07,759 --> 19:24:12,000 you go to the documentation you're going 25894 19:24:09,512 --> 19:24:14,240 to have more information about this but 25895 19:24:12,000 --> 19:24:16,720 we're going to do a few more lectures to 25896 19:24:14,240 --> 19:24:19,680 really drive this dancehall for now you 25897 19:24:16,720 --> 19:24:22,551 can come to cpp reference here and see 25898 19:24:19,679 --> 19:24:25,279 more concepts that you can use we have a 25899 19:24:22,551 --> 19:24:27,599 lot of this and we can really use this 25900 19:24:25,279 --> 19:24:29,759 to tune our function templates however 25901 19:24:27,600 --> 19:24:31,760 we want okay for example you see our 25902 19:24:29,759 --> 19:24:34,871 integral concepts you see a floating 25903 19:24:31,759 --> 19:24:37,039 point concepts and there are even more 25904 19:24:37,039 --> 19:24:41,439 classes but we haven't learned about 25905 19:24:39,279 --> 19:24:43,831 classes yet and we will do that starting 25906 19:24:41,440 --> 19:24:47,119 at the next chapter but now i just want 25907 19:24:43,831 --> 19:24:49,679 you to be familiar with concepts that 25908 19:24:47,119 --> 19:24:51,599 were introduced in c plus plus 20. and 25909 19:24:49,679 --> 19:24:53,831 here is another piece of documentation 25910 19:24:51,600 --> 19:24:56,552 you can look at just to complement what 25911 19:24:53,831 --> 19:24:58,799 we are talking about in this chapter but 25912 19:24:56,551 --> 19:25:00,551 i wouldn't recommend reading this just 25913 19:24:58,800 --> 19:25:02,872 yet because it's just going to confuse 25914 19:25:00,551 --> 19:25:04,639 you there is a lot of things we still 25915 19:25:02,872 --> 19:25:06,639 need to learn to really be able to 25916 19:25:04,639 --> 19:25:08,800 understand this so try to go through 25917 19:25:06,639 --> 19:25:11,191 this chapter and i am going to try and 25918 19:25:08,800 --> 19:25:13,512 break this down in ways that you can 25919 19:25:11,191 --> 19:25:16,239 understand this the main message is that 25920 19:25:13,512 --> 19:25:18,872 there are more concepts here you can try 25921 19:25:16,240 --> 19:25:20,800 in your c plus plus code if it makes 25922 19:25:18,872 --> 19:25:23,040 sense for the problem you are trying to 25923 19:25:20,800 --> 19:25:24,960 solve with c plus plus we are going to 25924 19:25:23,039 --> 19:25:26,799 stop here in this lecture in the next 25925 19:25:24,960 --> 19:25:29,760 one we're going to show you how you can 25926 19:25:26,800 --> 19:25:32,000 set up your own concepts in c plus plus 25927 19:25:29,759 --> 19:25:33,831 go ahead and finish up here and meet me 25928 19:25:32,000 --> 19:25:36,720 there in this lecture we're going to see 25929 19:25:33,831 --> 19:25:38,639 how you can build your own concepts and 25930 19:25:36,720 --> 19:25:41,440 this is going to bring us into the 25931 19:25:38,639 --> 19:25:44,080 second part of concepts we can use in c 25932 19:25:41,440 --> 19:25:46,479 plus plus in the last lecture we used 25933 19:25:44,080 --> 19:25:48,479 the concepts from the standard library 25934 19:25:46,479 --> 19:25:51,119 that are built into the c plus plus 25935 19:25:48,479 --> 19:25:53,440 programming language starting in c plus 25936 19:25:51,119 --> 19:25:56,720 plus 20. in this one we're going to be 25937 19:25:53,440 --> 19:25:59,119 building our own concepts and without 25938 19:25:56,720 --> 19:26:01,360 waiting anymore let's see how you can do 25939 19:25:59,119 --> 19:26:03,679 that and this is the syntax you can use 25940 19:26:01,360 --> 19:26:05,831 to do that and it is really simple the 25941 19:26:03,679 --> 19:26:08,319 way you do that you say a template 25942 19:26:05,831 --> 19:26:11,191 declaration specifying the template 25943 19:26:08,320 --> 19:26:13,280 parameter you want to constrain and then 25944 19:26:11,191 --> 19:26:16,319 you say concept you say the keyword 25945 19:26:13,279 --> 19:26:19,279 concept you give your concept a name you 25946 19:26:16,320 --> 19:26:22,000 put the equal sign after that and then 25947 19:26:19,279 --> 19:26:24,720 you're going to specify the requirements 25948 19:26:22,000 --> 19:26:28,960 for your concept so for example here we 25949 19:26:24,720 --> 19:26:31,360 are using a type trait to specify that 25950 19:26:28,960 --> 19:26:34,080 this type trait is going to determine 25951 19:26:31,360 --> 19:26:36,872 what works for this concept here and 25952 19:26:34,080 --> 19:26:39,680 what this is going to do is to enforce 25953 19:26:36,872 --> 19:26:41,832 for our template parameter to only be 25954 19:26:39,679 --> 19:26:43,831 integral if it's not integral we're 25955 19:26:41,831 --> 19:26:46,239 going to get a compiler error and one 25956 19:26:43,831 --> 19:26:49,191 thing i should tell you is that the std 25957 19:26:46,240 --> 19:26:51,512 integral concept is exactly built like 25958 19:26:49,191 --> 19:26:53,599 this in the c plus plus standard library 25959 19:26:51,512 --> 19:26:55,600 this is really cool so if you want to 25960 19:26:53,600 --> 19:26:58,000 set up your own concept and you want to 25961 19:26:55,600 --> 19:26:59,512 do that using typed traits you can do 25962 19:26:58,000 --> 19:27:03,039 that like this and it is going to 25963 19:26:59,512 --> 19:27:06,479 enforce for your template parameters to 25964 19:27:03,039 --> 19:27:09,360 satisfy that typed rate or to be exact 25965 19:27:06,479 --> 19:27:12,399 this concept is going to be satisfied 25966 19:27:09,360 --> 19:27:14,639 when the type trait evaluates to true if 25967 19:27:12,399 --> 19:27:17,119 the typed rate evaluates to false at 25968 19:27:14,639 --> 19:27:19,279 compile time the concept is not going to 25969 19:27:17,119 --> 19:27:21,759 be satisfied that's another way to look 25970 19:27:19,279 --> 19:27:24,319 at that down here we have another syntax 25971 19:27:21,759 --> 19:27:26,799 we can use if we have multiple 25972 19:27:24,320 --> 19:27:30,160 statements in our concept all you have 25973 19:27:26,800 --> 19:27:32,551 to do is specify the concept keyword you 25974 19:27:30,160 --> 19:27:35,440 give it a name you go to the equal sign 25975 19:27:32,551 --> 19:27:37,512 and then you add a requires close the 25976 19:27:35,440 --> 19:27:39,512 requires close is going to specify the 25977 19:27:37,512 --> 19:27:42,240 template parameters for which you want 25978 19:27:39,512 --> 19:27:45,440 to enforce your concept and then you're 25979 19:27:42,240 --> 19:27:48,000 going to put a pair of curly braces and 25980 19:27:45,440 --> 19:27:51,191 inside that you're going to specify the 25981 19:27:48,000 --> 19:27:53,039 syntax you want your concept to satisfy 25982 19:27:53,039 --> 19:27:58,551 for the two parameters to be 25983 19:27:55,679 --> 19:28:00,871 supporting the multiplication operator 25984 19:27:58,551 --> 19:28:03,039 if this multiplication operator is not 25985 19:28:00,872 --> 19:28:05,192 supported for the template parameters 25986 19:28:03,039 --> 19:28:09,119 that we passed the concept is going to 25987 19:28:05,191 --> 19:28:11,599 fail and again i should emphasize that 25988 19:28:09,119 --> 19:28:13,759 this doesn't check for the value of a 25989 19:28:11,600 --> 19:28:16,160 multiplied by b it's just going to 25990 19:28:13,759 --> 19:28:18,551 enforce from the syntax to be supported 25991 19:28:16,160 --> 19:28:21,680 so for example if you call a function 25992 19:28:18,551 --> 19:28:24,080 template with this concept and you try 25993 19:28:21,679 --> 19:28:26,399 to pass in integers it's going to work 25994 19:28:24,080 --> 19:28:29,440 because you can multiply integers but if 25995 19:28:26,399 --> 19:28:31,360 you pass two strings this is going to 25996 19:28:29,440 --> 19:28:33,760 fail because it doesn't really make 25997 19:28:31,360 --> 19:28:36,080 sense to multiply strings okay this is 25998 19:28:33,759 --> 19:28:38,479 the syntax now we only have one 25999 19:28:36,080 --> 19:28:41,119 statement to enforce inside but if you 26000 19:28:38,479 --> 19:28:42,959 want you can even put more statements if 26001 19:28:41,119 --> 19:28:45,679 you go down here we have another example 26002 19:28:42,960 --> 19:28:48,000 called incrementable this is going to 26003 19:28:45,679 --> 19:28:50,639 require for the template parameter to be 26004 19:28:48,000 --> 19:28:53,512 incrementable we specify the concept key 26005 19:28:50,639 --> 19:28:56,319 world we say the concept name we put in 26006 19:28:53,512 --> 19:28:58,639 our equal sign and then we say requires 26007 19:28:56,320 --> 19:29:01,192 we put on template parameter and put in 26008 19:28:58,639 --> 19:29:04,720 our requirements for any template 26009 19:29:01,191 --> 19:29:07,119 parameter that is using this concept the 26010 19:29:04,720 --> 19:29:09,279 syntax here must make sense so you must 26011 19:29:07,119 --> 19:29:11,679 be able to take the value that you get 26012 19:29:09,279 --> 19:29:13,600 from here and for example say i plus 26013 19:29:11,679 --> 19:29:16,319 equals one this is going to be valid 26014 19:29:13,600 --> 19:29:18,240 syntax you're going to say plus plus a 26015 19:29:16,320 --> 19:29:21,440 and plus plus b all these are going to 26016 19:29:18,240 --> 19:29:23,600 be valid c plus plus syntaxes for the 26017 19:29:21,440 --> 19:29:26,000 template parameter that you try to call 26018 19:29:23,600 --> 19:29:27,832 this function template with and if this 26019 19:29:26,000 --> 19:29:30,160 doesn't work you're going to get a 26020 19:29:27,831 --> 19:29:32,720 compiler error because your template 26021 19:29:30,160 --> 19:29:34,960 argument isn't really satisfying this 26022 19:29:32,720 --> 19:29:37,831 concept so this is really how you can 26023 19:29:34,960 --> 19:29:40,000 set up your own concepts in c plus plus 26024 19:29:37,831 --> 19:29:42,319 once you have your own concepts set up 26025 19:29:40,000 --> 19:29:44,720 you can use them like we have been using 26026 19:29:42,320 --> 19:29:47,280 the other concept from the ziploc plus 26027 19:29:44,720 --> 19:29:50,551 standard library for example we can use 26028 19:29:47,279 --> 19:29:53,759 syntax one to put a requires clues and 26029 19:29:50,551 --> 19:29:56,551 say that we want to satisfy our own 26030 19:29:53,759 --> 19:29:59,191 concept with this particular syntax you 26031 19:29:56,551 --> 19:30:01,679 will have to put in these angle brackets 26032 19:29:59,191 --> 19:30:03,831 and put your template parameter inside 26033 19:30:01,679 --> 19:30:06,799 this is the syntax you have to use you 26034 19:30:03,831 --> 19:30:09,599 can also use syntax 2 and specify my 26035 19:30:06,800 --> 19:30:11,680 integral directly in this angle brackets 26036 19:30:09,600 --> 19:30:14,000 in your template declaration and this is 26037 19:30:11,679 --> 19:30:16,399 also going to work we can even use this 26038 19:30:14,000 --> 19:30:18,551 with the auto syntax for our function 26039 19:30:16,399 --> 19:30:20,479 templates it is going to work okay now 26040 19:30:18,551 --> 19:30:22,551 that you have sent this i think it is 26041 19:30:20,479 --> 19:30:24,800 time we headed over to visual studio 26042 19:30:22,551 --> 19:30:27,360 code and play with us a little more okay 26043 19:30:24,800 --> 19:30:29,512 here we are in our working folder the 26044 19:30:27,360 --> 19:30:31,759 current project is building your own 26045 19:30:29,512 --> 19:30:34,000 concepts we're going to grab our 26046 19:30:31,759 --> 19:30:35,919 template files like we always do and 26047 19:30:34,000 --> 19:30:38,479 we're going to put them in place and 26048 19:30:35,919 --> 19:30:41,360 we're going to open this thing in visual 26049 19:30:38,479 --> 19:30:44,239 studio code let's do that we have our 26050 19:30:41,360 --> 19:30:45,759 project open here or folder i should say 26051 19:30:44,240 --> 19:30:47,832 we're going to clean this up a little 26052 19:30:45,759 --> 19:30:50,720 bit and we're going to include our 26053 19:30:47,831 --> 19:30:52,959 concepts library and we wanted to set up 26054 19:30:50,720 --> 19:30:56,479 our own concept the first syntax we're 26055 19:30:52,960 --> 19:30:59,280 going to see is by using type trades and 26056 19:30:56,479 --> 19:31:01,440 we're going to say template type name t 26057 19:30:59,279 --> 19:31:04,399 and we're going to say this concept we 26058 19:31:01,440 --> 19:31:06,080 want this template parameter to satisfy 26059 19:31:04,399 --> 19:31:08,720 we're going to say concept and we're 26060 19:31:06,080 --> 19:31:10,551 going to say my integral which is going 26061 19:31:08,720 --> 19:31:12,720 to be the name of our concept we're 26062 19:31:10,551 --> 19:31:14,000 going to put an ecosign and then we're 26063 19:31:14,000 --> 19:31:21,679 std integral v and specify that we want 26064 19:31:18,551 --> 19:31:23,919 this type trait to be satisfied for this 26065 19:31:21,679 --> 19:31:26,399 concept here and after this we need to 26066 19:31:23,919 --> 19:31:29,512 put in our semicolon and i think we need 26067 19:31:26,399 --> 19:31:32,000 to include the type traits library 26068 19:31:29,512 --> 19:31:35,440 because we are using that we should say 26069 19:31:32,000 --> 19:31:37,440 std is integral so sorry for this and 26070 19:31:35,440 --> 19:31:40,479 this is going to work now okay now that 26071 19:31:37,440 --> 19:31:43,119 we have the concept set up we can use it 26072 19:31:40,479 --> 19:31:45,191 like we have been using other concept 26073 19:31:43,119 --> 19:31:47,440 for example we can set up a function 26074 19:31:45,191 --> 19:31:50,479 which is going to add up two numbers and 26075 19:31:47,440 --> 19:31:52,872 it is going to use this concept here we 26076 19:31:50,479 --> 19:31:54,639 have multiple syntaxes we can use so 26077 19:31:52,872 --> 19:31:56,960 let's go down and try this out we're 26078 19:31:54,639 --> 19:31:59,360 going to say template and say type name 26079 19:31:56,960 --> 19:32:02,080 t and we're going to require this 26080 19:31:59,360 --> 19:32:04,479 concept to be satisfied for our function 26081 19:32:02,080 --> 19:32:07,831 template we saw that we could use the 26082 19:32:04,479 --> 19:32:10,159 requires close here and say my integral 26083 19:32:07,831 --> 19:32:12,551 and say the template parameter we want 26084 19:32:10,160 --> 19:32:14,320 to enforce this on and after this we're 26085 19:32:12,551 --> 19:32:16,159 going to just put in our function 26086 19:32:14,320 --> 19:32:18,080 template we're going to say add what we 26087 19:32:16,160 --> 19:32:20,639 have been doing all along in the last 26088 19:32:18,080 --> 19:32:23,759 lecture we're going to say t a 26089 19:32:20,639 --> 19:32:26,479 and t b and we're going to return a plus 26090 19:32:23,759 --> 19:32:28,871 b now this function is going to support 26091 19:32:26,479 --> 19:32:31,440 our concept let's go down and set up two 26092 19:32:28,872 --> 19:32:33,919 variables we're going to say and x 26093 19:32:31,440 --> 19:32:36,479 and we're going to say 6 for example and 26094 19:32:33,919 --> 19:32:39,119 into y and i'm going to make this a 7 26095 19:32:36,479 --> 19:32:41,440 why not and we're going to say add 26096 19:32:39,119 --> 19:32:43,831 x and y once we do this you see that we 26097 19:32:41,440 --> 19:32:46,320 have no problem because the concept is 26098 19:32:43,831 --> 19:32:49,831 satisfied for the arguments that we are 26099 19:32:46,320 --> 19:32:52,480 passing in here the concept is satisfied 26100 19:32:49,831 --> 19:32:54,959 because the type trait here is going to 26101 19:32:52,479 --> 19:32:57,119 return true for this type here because 26102 19:32:54,960 --> 19:32:59,280 it is an integral type when the type 26103 19:32:57,119 --> 19:33:01,759 trait returns true then the concept is 26104 19:32:59,279 --> 19:33:04,160 going to be satisfied if the type trait 26105 19:33:01,759 --> 19:33:06,959 returns false the concept is going to 26106 19:33:04,160 --> 19:33:08,960 fail let's try and bolt and really drive 26107 19:33:06,960 --> 19:33:11,440 this home you see that the world is good 26108 19:33:08,960 --> 19:33:13,440 our function is working so the function 26109 19:33:11,440 --> 19:33:15,600 template and stands for this code here 26110 19:33:13,440 --> 19:33:17,832 is going to be successfully generated 26111 19:33:15,600 --> 19:33:19,192 and we're going to call it when the 26112 19:33:19,191 --> 19:33:24,720 now let's change the type of these guys 26113 19:33:21,831 --> 19:33:26,959 to double and try to violate our concept 26114 19:33:24,720 --> 19:33:29,440 and make it fail once we do this you see 26115 19:33:26,960 --> 19:33:31,360 the call here has a squiggly line and a 26116 19:33:29,440 --> 19:33:34,000 visual studio code is not going to say 26117 19:33:31,360 --> 19:33:36,960 that it is a concept problem which is a 26118 19:33:34,000 --> 19:33:39,191 shame but we can still run this through 26119 19:33:36,960 --> 19:33:41,280 the compiler and the compiler is going 26120 19:33:39,191 --> 19:33:44,639 to tell us what is wrong so it's going 26121 19:33:41,279 --> 19:33:47,440 to say use of function add with 26122 19:33:44,639 --> 19:33:49,360 unsatisfied constraints and if we go 26123 19:33:47,440 --> 19:33:52,320 down we're going to see the exact 26124 19:33:49,360 --> 19:33:55,039 constraints we violated and we're going 26125 19:33:52,320 --> 19:33:57,832 to say required for the satisfaction of 26126 19:33:55,039 --> 19:33:59,919 my integral with t we are calling this 26127 19:33:57,831 --> 19:34:03,279 with double and double is going to 26128 19:33:59,919 --> 19:34:05,039 evaluate this to false hopefully you can 26129 19:34:03,279 --> 19:34:08,000 see that you can really set up your own 26130 19:34:05,039 --> 19:34:09,679 concepts using the syntax here and we 26131 19:34:08,000 --> 19:34:11,440 can use our concept with all the 26132 19:34:09,679 --> 19:34:13,599 syntaxes we have seen in the last 26133 19:34:11,440 --> 19:34:16,479 lecture so for example we can take out 26134 19:34:13,600 --> 19:34:18,800 the requires close all together here and 26135 19:34:18,800 --> 19:34:23,192 template declaration here so we're going 26136 19:34:20,800 --> 19:34:25,832 to say my integral and if we do this 26137 19:34:23,191 --> 19:34:28,239 we're going to fail again because the 26138 19:34:25,831 --> 19:34:30,399 concept is not going to be satisfied 26139 19:34:28,240 --> 19:34:33,279 we're going to have the same problem we 26140 19:34:30,399 --> 19:34:35,831 can change the syntax to use the auto 26141 19:34:33,279 --> 19:34:39,279 syntax but before we do that let's 26142 19:34:35,831 --> 19:34:42,159 change this back to type name t and then 26143 19:34:39,279 --> 19:34:44,639 put our requires close after the 26144 19:34:42,160 --> 19:34:46,552 parameter list of our function template 26145 19:34:46,551 --> 19:34:51,759 and we're going to say my integral and 26146 19:34:48,960 --> 19:34:54,000 specify the template parameter and this 26147 19:34:51,759 --> 19:34:56,399 is a valid function template with a 26148 19:34:54,000 --> 19:34:57,600 concept applied to app and if we both 26149 19:34:56,399 --> 19:35:00,551 again we're going to get the same 26150 19:34:57,600 --> 19:35:03,440 compiler error so you see the same error 26151 19:35:00,551 --> 19:35:05,599 and we can use the auto syntax let's do 26152 19:35:03,440 --> 19:35:08,000 that we're going to take out the 26153 19:35:05,600 --> 19:35:10,552 template declaration here we're going to 26154 19:35:08,000 --> 19:35:14,551 take out the requires close here and 26155 19:35:10,551 --> 19:35:16,000 we're going to change our t to auto and 26156 19:35:14,551 --> 19:35:19,279 the way we do that we're going to say 26157 19:35:16,000 --> 19:35:21,279 std no no std we are using our own 26158 19:35:19,279 --> 19:35:24,160 concept so we're going to say my 26159 19:35:21,279 --> 19:35:26,800 integral auto this is going to work and 26160 19:35:24,160 --> 19:35:29,279 we're going to say my integral auto and 26161 19:35:26,800 --> 19:35:31,832 my integral auto here the moment we do 26162 19:35:29,279 --> 19:35:34,872 this you see that we have squiggly lines 26163 19:35:31,831 --> 19:35:36,479 here because our concept is violated but 26164 19:35:34,872 --> 19:35:38,800 if we're trying to build again we're 26165 19:35:36,479 --> 19:35:40,959 going to use gcc to do that the build is 26166 19:35:38,800 --> 19:35:43,760 going to fail you see that the same 26167 19:35:40,960 --> 19:35:46,872 concept is violated and we are able to 26168 19:35:43,759 --> 19:35:49,119 use our concept throughout the four 26169 19:35:46,872 --> 19:35:50,800 syntaxes that we saw in the last lecture 26170 19:35:49,119 --> 19:35:53,191 and this is really cool now that we have 26171 19:35:50,800 --> 19:35:55,192 this we're going to comment this out and 26172 19:35:53,191 --> 19:35:58,799 we're going to see that we can set up 26173 19:35:55,191 --> 19:36:01,440 concepts that have requirements spanning 26174 19:35:58,800 --> 19:36:03,512 multiple statements or multiple lines 26175 19:36:01,440 --> 19:36:05,919 okay let's go down and do that suppose 26176 19:36:03,512 --> 19:36:07,832 we want to set up a concept that is 26177 19:36:05,919 --> 19:36:10,639 going to enforce for the templates 26178 19:36:07,831 --> 19:36:13,279 parameters to be multipliable 26179 19:36:10,639 --> 19:36:15,679 with the multiplication operator we have 26180 19:36:13,279 --> 19:36:17,440 here we can use this syntax here we're 26181 19:36:15,679 --> 19:36:19,279 going to put in our template declaration 26182 19:36:17,440 --> 19:36:20,960 we're going to say concept we're going 26183 19:36:19,279 --> 19:36:22,872 to say the concept name we're going to 26184 19:36:20,960 --> 19:36:25,680 put in an equal sign and then we're 26185 19:36:22,872 --> 19:36:28,400 going to put in a requires close 26186 19:36:25,679 --> 19:36:30,399 with our template parameters and and 26187 19:36:28,399 --> 19:36:33,119 after that we're going to put a pair of 26188 19:36:30,399 --> 19:36:35,679 curly braces and inside the curlies 26189 19:36:33,119 --> 19:36:38,551 we're going to put the requirements for 26190 19:36:35,679 --> 19:36:41,191 example in this case we require for the 26191 19:36:38,551 --> 19:36:43,599 template parameter here to support the 26192 19:36:41,191 --> 19:36:45,831 multiplication operator between two of 26193 19:36:43,600 --> 19:36:48,000 these types if the multiplication 26194 19:36:45,831 --> 19:36:49,759 operator is not supported we're going to 26195 19:36:48,000 --> 19:36:52,240 have a problem and that this is the 26196 19:36:49,759 --> 19:36:54,080 concept that is going to enforce that 26197 19:36:52,240 --> 19:36:56,080 one thing you should know is that this 26198 19:36:56,080 --> 19:37:02,639 that multiplying a and b is valid syntax 26199 19:37:00,080 --> 19:37:04,639 it's not going to check the value of a 26200 19:37:02,639 --> 19:37:07,119 multiplied by b and you should really 26201 19:37:04,639 --> 19:37:09,039 understand this now that we have this n 26202 19:37:07,119 --> 19:37:12,159 we can set up a function to use these 26203 19:37:09,039 --> 19:37:14,319 two guys why not use the function we 26204 19:37:12,160 --> 19:37:16,320 have seen before to add two numbers 26205 19:37:14,320 --> 19:37:18,400 let's do that and we're going to put in 26206 19:37:16,320 --> 19:37:22,640 our requires close and we're going to 26207 19:37:18,399 --> 19:37:24,720 require now to support the multipliable 26208 19:37:22,639 --> 19:37:27,759 concept and we're going to specify our 26209 19:37:24,720 --> 19:37:30,080 template parameter we have the concept n 26210 19:37:27,759 --> 19:37:32,239 so we can use that to do whatever we 26211 19:37:30,080 --> 19:37:35,512 want now we're going to say t 26212 19:37:32,240 --> 19:37:38,240 add and we're going to say t a and t b 26213 19:37:35,512 --> 19:37:40,240 and we're going to return a plus b this 26214 19:37:38,240 --> 19:37:42,872 is the same function we've been using 26215 19:37:40,240 --> 19:37:45,120 all along okay now we have our concept 26216 19:37:42,872 --> 19:37:47,440 in place and we have a function template 26217 19:37:45,119 --> 19:37:51,440 that is going to use this concept 26218 19:37:47,440 --> 19:37:53,191 let's see if we can add up x and y and 26219 19:37:51,440 --> 19:37:55,119 see what we get we're going to world 26220 19:37:53,191 --> 19:37:57,191 with gcc we're going to see that the 26221 19:37:55,119 --> 19:38:01,039 world is going to be good because our 26222 19:37:57,191 --> 19:38:03,919 concept here is satisfied we are passing 26223 19:38:01,039 --> 19:38:06,399 double types for our function template 26224 19:38:03,919 --> 19:38:08,399 and it is legal to multiply two double 26225 19:38:06,399 --> 19:38:10,239 variables if we multiply two doubles 26226 19:38:08,399 --> 19:38:12,799 we're going to get a value and that's 26227 19:38:10,240 --> 19:38:15,040 going to be valid c plus plus syntax but 26228 19:38:12,800 --> 19:38:17,600 let's see what happens if we try to 26229 19:38:15,039 --> 19:38:20,000 change our arguments to hdd strength 26230 19:38:17,600 --> 19:38:22,639 let's do that so we're going to say x is 26231 19:38:20,000 --> 19:38:25,440 hello and we're going to say why is 26232 19:38:22,639 --> 19:38:27,831 world why not let's go down and put in a 26233 19:38:25,440 --> 19:38:30,800 semicolon and the moment we do that you 26234 19:38:27,831 --> 19:38:34,239 see that we have a problem and visual 26235 19:38:30,800 --> 19:38:36,400 studio code is not good at this yet 26236 19:38:36,399 --> 19:38:42,159 and run this through gcc we're going to 26237 19:38:38,800 --> 19:38:44,240 get that our concept is violated let's 26238 19:38:42,160 --> 19:38:47,279 look at the compiler error here it's 26239 19:38:44,240 --> 19:38:49,512 going to say constraints not satisfied 26240 19:38:47,279 --> 19:38:52,319 and it's going to tell us that we are 26241 19:38:49,512 --> 19:38:54,800 calling the add function with the values 26242 19:38:52,320 --> 19:38:56,872 that can't be multiplied it's going to 26243 19:38:54,800 --> 19:38:59,512 say the required expression here is 26244 19:38:56,872 --> 19:39:01,680 invalid and this is how you can set up a 26245 19:38:59,512 --> 19:39:02,800 concept like this and you can really use 26246 19:39:02,800 --> 19:39:08,240 put constraints however you want on your 26247 19:39:05,440 --> 19:39:11,040 function templates but here we have just 26248 19:39:08,240 --> 19:39:13,919 used one statement inside our concept 26249 19:39:11,039 --> 19:39:15,511 here we can even put in more so to drive 26250 19:39:13,919 --> 19:39:17,831 this home we're going to set up another 26251 19:39:15,512 --> 19:39:20,000 concept let's go down and put that in 26252 19:39:17,831 --> 19:39:22,319 place the concept is going to be called 26253 19:39:20,000 --> 19:39:24,639 incremental it's going to be using the 26254 19:39:22,320 --> 19:39:26,640 same syntax we saw here but we're going 26255 19:39:24,639 --> 19:39:29,191 to be putting a requirement on the 26256 19:39:26,639 --> 19:39:32,080 template parameter here to support 26257 19:39:29,191 --> 19:39:34,799 incrementation and we want for every 26258 19:39:32,080 --> 19:39:37,512 type that goes in the place of t here we 26259 19:39:34,800 --> 19:39:40,400 must be able to say a plus equals one 26260 19:39:37,512 --> 19:39:42,551 this must be valid c plus plus syntax we 26261 19:39:40,399 --> 19:39:45,119 must be able to say plus plus a this 26262 19:39:42,551 --> 19:39:47,599 must be valid c plus plus syntax and we 26263 19:39:45,119 --> 19:39:50,720 must be able to say a plus plus this 26264 19:39:47,600 --> 19:39:53,040 must be valid c plus plus syntax 26265 19:39:50,720 --> 19:39:56,000 now that we have this we can change our 26266 19:39:53,039 --> 19:39:58,719 add function to use this concept why not 26267 19:39:56,000 --> 19:40:01,600 we're going to say incrementable and now 26268 19:39:58,720 --> 19:40:04,399 this function is going to be required to 26269 19:40:01,600 --> 19:40:06,800 use things that we can increment on and 26270 19:40:04,399 --> 19:40:08,720 you see it is failing already because we 26271 19:40:06,800 --> 19:40:10,872 can't really increment strengths what 26272 19:40:08,720 --> 19:40:13,040 does that even mean so let's build and 26273 19:40:10,872 --> 19:40:16,000 show you the compiler error okay you see 26274 19:40:13,039 --> 19:40:18,639 incremental is not satisfied and that 26275 19:40:16,000 --> 19:40:21,831 the required expressions aren't valid 26276 19:40:18,639 --> 19:40:25,279 you see plus plus a is not valid a plus 26277 19:40:21,831 --> 19:40:28,080 plus is not valid and a plus equals one 26278 19:40:25,279 --> 19:40:30,240 should not also be valid but we're not 26279 19:40:28,080 --> 19:40:33,680 seeing that here but hopefully you see 26280 19:40:30,240 --> 19:40:36,400 the message here that our concept is 26281 19:40:33,679 --> 19:40:39,119 failing here if we comment out our 26282 19:40:36,399 --> 19:40:40,871 strengths here and uncomment double all 26283 19:40:39,119 --> 19:40:43,440 these operations we have here are 26284 19:40:40,872 --> 19:40:46,551 supported for the double type 26285 19:40:43,440 --> 19:40:49,119 so we should not fail here if we try to 26286 19:40:46,551 --> 19:40:50,872 compile this because the concept is 26287 19:40:53,440 --> 19:40:56,479 compiler to build you see the build is 26288 19:40:56,479 --> 19:41:01,759 and we can clear and we can bring up a 26289 19:40:58,720 --> 19:41:04,800 terminal to really see this that our 26290 19:41:01,759 --> 19:41:06,479 binary was generated let's do the ir 26291 19:41:04,800 --> 19:41:08,960 we're going to see our rooster 26292 19:41:06,479 --> 19:41:10,872 executable here and if we go down let's 26293 19:41:08,960 --> 19:41:13,280 say done we're going to see that if we 26294 19:41:10,872 --> 19:41:15,832 build again to bring this in 26295 19:41:13,279 --> 19:41:18,000 consideration clear run rooster the 26296 19:41:15,831 --> 19:41:21,599 program was generated and it is really 26297 19:41:18,000 --> 19:41:23,279 running because the concept here 26298 19:41:23,279 --> 19:41:30,240 with our call using double arguments 26299 19:41:27,600 --> 19:41:32,160 here okay so this is really all i set 26300 19:41:30,240 --> 19:41:34,639 out to share with you guys in this 26301 19:41:32,160 --> 19:41:37,120 lecture i hope you know the syntaxes you 26302 19:41:34,639 --> 19:41:39,279 can use to create your own concepts you 26303 19:41:37,119 --> 19:41:41,759 can either use type traits like we have 26304 19:41:39,279 --> 19:41:45,600 seen here and the syntax is to put the 26305 19:41:41,759 --> 19:41:47,919 keyword concept put in your concept name 26306 19:41:45,600 --> 19:41:50,160 put an ecosign and then specify your 26307 19:41:47,919 --> 19:41:51,679 type trait here this is going to work if 26308 19:41:50,160 --> 19:41:53,760 you need something a little more 26309 19:41:51,679 --> 19:41:55,919 complicated then you're going to use 26310 19:41:55,919 --> 19:42:01,919 requirements for your concept in these 26311 19:41:59,119 --> 19:42:04,399 curly braces you can either put one 26312 19:42:01,919 --> 19:42:07,279 statement like we did here or you can 26313 19:42:04,399 --> 19:42:08,799 even put as many statements as you want 26314 19:42:07,279 --> 19:42:10,800 just remember to end them with 26315 19:42:08,800 --> 19:42:13,512 semicolons and another thing you 26316 19:42:10,800 --> 19:42:16,320 shouldn't really take lightly is that 26317 19:42:13,512 --> 19:42:18,080 this is just going to check for syntax 26318 19:42:16,320 --> 19:42:21,280 it's not going to check for the value 26319 19:42:18,080 --> 19:42:23,119 you get after you add one to a here for 26320 19:42:21,279 --> 19:42:25,360 example it's not going to check for the 26321 19:42:23,119 --> 19:42:28,551 value you get after you multiply b it's 26322 19:42:25,360 --> 19:42:32,639 just going to see that a multiply b 26323 19:42:28,551 --> 19:42:35,440 is valid syntax for the types that our 26324 19:42:32,639 --> 19:42:37,831 template parameter is using here we are 26325 19:42:35,440 --> 19:42:40,160 going to stop here in this lecture this 26326 19:42:37,831 --> 19:42:43,039 one we're going to zoom in a little more 26327 19:42:40,160 --> 19:42:44,320 on the requires close here go ahead and 26328 19:42:44,320 --> 19:42:49,120 and meet me there in this lecture we're 26329 19:42:46,479 --> 19:42:52,080 going to learn about some more things we 26330 19:42:49,119 --> 19:42:54,799 can do with our requires close but 26331 19:42:52,080 --> 19:42:57,759 before we do that let's look at the 26332 19:42:54,800 --> 19:43:00,639 kinds of requirements we can put in our 26333 19:42:57,759 --> 19:43:02,799 requires close the first one is a simple 26334 19:43:00,639 --> 19:43:04,960 requirement which is what we have seen 26335 19:43:02,800 --> 19:43:06,479 so far we can also do a nested 26336 19:43:04,960 --> 19:43:08,720 requirement and we're going to learn 26337 19:43:06,479 --> 19:43:10,959 about this in a minute and we can do a 26338 19:43:08,720 --> 19:43:13,512 compound requirement which is going to 26339 19:43:10,960 --> 19:43:16,480 allow us to specify more things about 26340 19:43:13,512 --> 19:43:19,191 what we require for our concept there 26341 19:43:16,479 --> 19:43:21,119 are also type requirements but these are 26342 19:43:19,191 --> 19:43:22,639 really advanced for the scope of this 26343 19:43:21,119 --> 19:43:24,479 course so we're not going to talk about 26344 19:43:22,639 --> 19:43:27,759 these in this course what we're going to 26345 19:43:24,479 --> 19:43:30,239 look at are these three first here so 26346 19:43:27,759 --> 19:43:32,080 let's look at a simple requirement this 26347 19:43:30,240 --> 19:43:35,192 is what we have seen already here we 26348 19:43:32,080 --> 19:43:38,080 have our template declaration we have a 26349 19:43:35,191 --> 19:43:40,319 concept declared the concept name is 26350 19:43:38,080 --> 19:43:42,639 tiny type we're going to put in our 26351 19:43:40,320 --> 19:43:45,192 requires close we're going to put in our 26352 19:43:42,639 --> 19:43:48,000 parameter list and we're going to put in 26353 19:43:45,191 --> 19:43:50,799 our curly braces inside the requires 26354 19:43:48,000 --> 19:43:54,240 close we have a statement here which is 26355 19:43:50,800 --> 19:43:55,832 going to enforce that size of t is less 26356 19:43:55,831 --> 19:44:01,279 and what this really means it's going to 26357 19:43:58,080 --> 19:44:04,000 check that size of t the syntax here is 26358 19:44:01,279 --> 19:44:06,479 correct and this can really throw people 26359 19:44:04,000 --> 19:44:09,119 off i have said this in the last lecture 26360 19:44:06,479 --> 19:44:11,599 but i set up this little example to 26361 19:44:09,119 --> 19:44:14,479 really drive this home and show you that 26362 19:44:11,600 --> 19:44:16,800 if you are not careful your concepts are 26363 19:44:14,479 --> 19:44:19,279 going to be really wrong this is going 26364 19:44:16,800 --> 19:44:22,240 to check for the syntax so for example 26365 19:44:19,279 --> 19:44:25,191 if we call a function template with this 26366 19:44:25,191 --> 19:44:30,080 the concept is going to be satisfied 26367 19:44:27,191 --> 19:44:32,551 even if the size of the car is less than 26368 19:44:30,080 --> 19:44:34,800 4 obviously but that's not going to be 26369 19:44:32,551 --> 19:44:37,679 the job of this concept here this 26370 19:44:34,800 --> 19:44:40,160 concept is not going to enforce for the 26371 19:44:37,679 --> 19:44:42,479 expression here to evaluate to true it's 26372 19:44:40,160 --> 19:44:45,919 going to just check the syntax and if 26373 19:44:42,479 --> 19:44:48,639 you pass something smaller than a net in 26374 19:44:45,919 --> 19:44:50,872 a function template with this concept 26375 19:44:48,639 --> 19:44:53,191 this is going to be valid c plus plus 26376 19:44:50,872 --> 19:44:55,919 syntax so the concept is going to work 26377 19:44:53,191 --> 19:44:58,720 so now you must be asking how can i 26378 19:44:55,919 --> 19:45:00,160 enforce for the value of this expression 26379 19:45:00,160 --> 19:45:06,720 using concept well for that you will 26380 19:45:03,679 --> 19:45:08,799 have to use nested requirements and a 26381 19:45:06,720 --> 19:45:11,831 nested requirement is going to have 26382 19:45:08,800 --> 19:45:14,551 another requires keyword inserted in 26383 19:45:11,831 --> 19:45:16,720 front of it now if you use the syntax 26384 19:45:14,551 --> 19:45:19,599 like this now this is going to check 26385 19:45:16,720 --> 19:45:22,080 that size of t is actually less than 4. 26386 19:45:19,600 --> 19:45:24,480 if it's not less than 4 this is going to 26387 19:45:22,080 --> 19:45:26,320 return false here and the concept is 26388 19:45:24,479 --> 19:45:29,360 going to fail and this is how you can 26389 19:45:26,320 --> 19:45:31,920 use nested requirements to enforce for 26390 19:45:29,360 --> 19:45:33,831 some expressions to be true in your 26391 19:45:31,919 --> 19:45:35,679 requires close another kind of 26392 19:45:33,831 --> 19:45:38,871 requirement you can set up for your 26393 19:45:35,679 --> 19:45:41,759 concept is the compound requirement and 26394 19:45:38,872 --> 19:45:43,760 this is going to allow you to check if 26395 19:45:41,759 --> 19:45:46,551 something doesn't throw an exception you 26396 19:45:43,759 --> 19:45:48,159 can do that using the no accept keyword 26397 19:45:46,551 --> 19:45:50,959 but we're not going to use this now 26398 19:45:48,160 --> 19:45:53,360 because this is not in scope for what we 26399 19:45:50,960 --> 19:45:56,000 can do at this point in the course but 26400 19:45:53,360 --> 19:45:59,600 you can also check the return type of an 26401 19:45:56,000 --> 19:46:02,479 expression and divorce that it satisfies 26402 19:45:59,600 --> 19:46:05,760 some type trait for example so here we 26403 19:46:02,479 --> 19:46:08,639 are saying for our concept and if we 26404 19:46:05,759 --> 19:46:11,679 pass in two parameters the syntax for 26405 19:46:08,639 --> 19:46:14,639 adding a and b must be correct and 26406 19:46:11,679 --> 19:46:18,079 supported for those types but the result 26407 19:46:14,639 --> 19:46:20,160 of a and b must also be convertible to 26408 19:46:18,080 --> 19:46:21,680 ant and you can do something like this 26409 19:46:20,160 --> 19:46:23,919 and this is going to work you can 26410 19:46:21,679 --> 19:46:26,479 enforce these kinds of things using 26411 19:46:23,919 --> 19:46:28,720 compound requirements okay now that you 26412 19:46:26,479 --> 19:46:31,679 know about these kinds of requirements 26413 19:46:28,720 --> 19:46:34,080 you can put in your requires close let's 26414 19:46:31,679 --> 19:46:36,000 head over to visual studio code and play 26415 19:46:34,080 --> 19:46:38,479 with us a little more okay here we are 26416 19:46:36,000 --> 19:46:41,440 in our working folder the current 26417 19:46:38,479 --> 19:46:43,919 project is zooming in on requires close 26418 19:46:41,440 --> 19:46:46,080 we're going to grab our template files 26419 19:46:43,919 --> 19:46:47,919 and we're going to put them in place and 26420 19:46:46,080 --> 19:46:50,240 we're going to open that in visual 26421 19:46:47,919 --> 19:46:52,399 studio code pretty quick by dragging and 26422 19:46:50,240 --> 19:46:54,240 dropping here this is going to open up 26423 19:46:52,399 --> 19:46:56,239 our folder we're going to do the usual 26424 19:46:54,240 --> 19:46:59,120 we're going to close the pane here we're 26425 19:46:56,240 --> 19:47:01,512 going to clean things up a little bit 26426 19:46:59,119 --> 19:47:03,919 and we're going to include our concepts 26427 19:47:01,512 --> 19:47:05,680 library let's do that we are going to 26428 19:47:03,919 --> 19:47:07,512 set up a simple concept we're going to 26429 19:47:05,679 --> 19:47:10,079 say template and we're going to say what 26430 19:47:07,512 --> 19:47:12,479 the concept is the concept is going to 26431 19:47:10,080 --> 19:47:13,600 be called the tiny type and we're going 26432 19:47:13,600 --> 19:47:19,360 what makes this concept a tiny time 26433 19:47:16,800 --> 19:47:21,192 we're going to put in our requires close 26434 19:47:19,360 --> 19:47:23,680 and we're going to put in the template 26435 19:47:21,191 --> 19:47:25,919 parameter call it t why not and we're 26436 19:47:23,679 --> 19:47:28,479 going to put in our closing semicolon 26437 19:47:25,919 --> 19:47:31,279 here now what is going to make this 26438 19:47:28,479 --> 19:47:33,191 concept a tiny type is that the size of 26439 19:47:31,279 --> 19:47:35,360 the template parameter view is going to 26440 19:47:33,191 --> 19:47:37,440 be less than four let's suppose we are 26441 19:47:35,360 --> 19:47:38,240 doing the function and we wanted to use 26442 19:47:38,240 --> 19:47:43,600 integral types that are smaller than 26443 19:47:41,360 --> 19:47:45,680 four bytes in memory suppose you want to 26444 19:47:45,679 --> 19:47:49,679 in the problem that you are trying to 26445 19:47:47,600 --> 19:47:52,480 solve in c plus plus the way we can 26446 19:47:49,679 --> 19:47:55,359 enforce this we can say size of t is 26447 19:47:52,479 --> 19:47:57,831 supposed to be less than four let's say 26448 19:47:55,360 --> 19:47:59,119 size of properly can't type can i okay 26449 19:47:57,831 --> 19:48:01,191 the moment we do this you're going to 26450 19:47:59,119 --> 19:48:03,360 see that this is valid c plus plus 26451 19:48:01,191 --> 19:48:06,000 syntax so we're not going to have any 26452 19:48:03,360 --> 19:48:08,551 squiggly lines but what you should know 26453 19:48:06,000 --> 19:48:11,039 is that this is a simple requirement in 26454 19:48:08,551 --> 19:48:12,399 c plus plus concepts and what this is 26455 19:48:12,399 --> 19:48:17,831 it's just going to enforce the syntax of 26456 19:48:15,600 --> 19:48:20,552 what we do here what we put here must be 26457 19:48:17,831 --> 19:48:22,720 valid c plus plus syntax so only 26458 19:48:20,551 --> 19:48:24,872 enforces syntax this is what you should 26459 19:48:22,720 --> 19:48:27,600 really remember okay now that we have 26460 19:48:24,872 --> 19:48:30,000 this end let's try it out and show you 26461 19:48:27,600 --> 19:48:31,600 some problems you can really run into 26462 19:48:30,000 --> 19:48:33,440 we're going to set up two character 26463 19:48:31,600 --> 19:48:35,279 variables we're going to call the first 26464 19:48:33,440 --> 19:48:39,119 one x we're going to initialize this 26465 19:48:35,279 --> 19:48:41,440 with 57 why not 67 and we're going to 26466 19:48:39,119 --> 19:48:44,959 put in the other variable which is going 26467 19:48:41,440 --> 19:48:47,040 to be y and it's going to have a 56 n 26468 19:48:44,960 --> 19:48:49,120 you can put in whatever you want just 26469 19:48:47,039 --> 19:48:51,831 make sure that you are within the range 26470 19:48:49,119 --> 19:48:54,399 of the card type which is between 0 and 26471 19:48:51,831 --> 19:48:56,959 255 if you remember okay now that we 26472 19:48:54,399 --> 19:48:59,919 have this n i realize we don't have a 26473 19:48:56,960 --> 19:49:01,680 function we can try this on let's set up 26474 19:48:59,919 --> 19:49:03,919 another function pretty quick we're 26475 19:49:01,679 --> 19:49:06,399 going to say tiny type auto we can do 26476 19:49:03,919 --> 19:49:09,440 that you already know this syntax and 26477 19:49:06,399 --> 19:49:12,551 we're going to say add tiny type auto a 26478 19:49:09,440 --> 19:49:15,191 and a tiny type auto p we can do that 26479 19:49:12,551 --> 19:49:18,080 and we're going to return a plus b now 26480 19:49:15,191 --> 19:49:19,599 that we have this let's say a and b down 26481 19:49:18,080 --> 19:49:22,000 here and call this function we're going 26482 19:49:19,600 --> 19:49:24,160 to say x and y and we're going to try 26483 19:49:22,000 --> 19:49:26,872 and compile this program and it is going 26484 19:49:24,160 --> 19:49:29,192 to compile fine because the type we are 26485 19:49:26,872 --> 19:49:31,440 using to call the function which is car 26486 19:49:29,191 --> 19:49:33,119 its size is going to be less than four 26487 19:49:31,440 --> 19:49:34,400 okay so this is going to work and it is 26488 19:49:34,399 --> 19:49:40,159 let's build with gcc you're going to see 26489 19:49:36,720 --> 19:49:42,720 that the build is good let's try and uh 26490 19:49:40,160 --> 19:49:45,360 change the type here to end or even 26491 19:49:42,720 --> 19:49:47,680 double you know double is 8 bytes in 26492 19:49:45,360 --> 19:49:49,919 memory so we expect this to probably 26493 19:49:47,679 --> 19:49:52,319 give us something wrong double type is 26494 19:49:49,919 --> 19:49:54,240 eight bytes in memory so we expect the 26495 19:49:54,240 --> 19:49:59,760 to be false because the size of t is 26496 19:49:57,191 --> 19:50:02,239 going to be eight and eight is not less 26497 19:49:59,759 --> 19:50:04,871 than four this is going to be false so 26498 19:50:02,240 --> 19:50:05,760 some might expect this concept here to 26499 19:50:05,759 --> 19:50:11,191 but let's try and really prove you wrong 26500 19:50:08,551 --> 19:50:13,360 we're going to build with gcc and you 26501 19:50:11,191 --> 19:50:16,799 see it's building successfully what this 26502 19:50:13,360 --> 19:50:19,191 is really doing is that with simple 26503 19:50:16,800 --> 19:50:21,360 requirements like we have here c plus 26504 19:50:19,191 --> 19:50:24,639 plus is only going to enforce for this 26505 19:50:21,360 --> 19:50:26,720 to be valid c plus plus syntax and size 26506 19:50:26,720 --> 19:50:32,080 is going to be valid c plus plus syntax 26507 19:50:29,360 --> 19:50:34,479 even if this expression here evaluates 26508 19:50:32,080 --> 19:50:36,960 to false this is not going to enforce 26509 19:50:34,479 --> 19:50:38,959 for the value of this expression to be 26510 19:50:36,960 --> 19:50:41,360 false you can't do that with simple 26511 19:50:38,960 --> 19:50:44,480 requirements what you will do is check 26512 19:50:41,360 --> 19:50:46,720 that this is valid syntax now what do we 26513 19:50:44,479 --> 19:50:48,551 do if we really want to enforce that 26514 19:50:48,551 --> 19:50:53,831 less than four if you want that you 26515 19:50:50,872 --> 19:50:55,600 should use nested requirements and the 26516 19:50:53,831 --> 19:50:58,080 syntax to do that is really simple we 26517 19:50:55,600 --> 19:51:00,000 can go down here and put that in we're 26518 19:50:58,080 --> 19:51:02,800 going to say requires we're going to add 26519 19:51:00,000 --> 19:51:04,960 a requires keyword in front of this then 26520 19:51:02,800 --> 19:51:06,800 we're going to put our expression here 26521 19:51:04,960 --> 19:51:09,600 let's copy it and put it in i don't want 26522 19:51:06,800 --> 19:51:12,320 to type this again the moment we do this 26523 19:51:09,600 --> 19:51:15,760 now compilation is going to fail if we 26524 19:51:12,320 --> 19:51:16,552 pass a type whose size is not less than 26525 19:51:16,551 --> 19:51:20,080 and let's try and build again you're 26526 19:51:18,551 --> 19:51:22,000 going to see that the world is going to 26527 19:51:20,080 --> 19:51:24,551 fail now and we're going to have a 26528 19:51:22,000 --> 19:51:27,360 failed concept and you see world 26529 19:51:24,551 --> 19:51:29,679 finished with errors we have constraints 26530 19:51:27,360 --> 19:51:31,512 not satisfied and if we go down here 26531 19:51:29,679 --> 19:51:34,239 we're going to see required for the 26532 19:51:31,512 --> 19:51:37,512 satisfaction of tiny type auto and we 26533 19:51:34,240 --> 19:51:40,320 passed in a double type whose size is 8 26534 19:51:37,512 --> 19:51:43,119 bytes in memory and it is less than four 26535 19:51:40,320 --> 19:51:46,640 bytes you see the requirement here it is 26536 19:51:43,119 --> 19:51:49,191 not satisfied we can do this using 26537 19:51:46,639 --> 19:51:51,679 nested requirements okay if we go down 26538 19:51:49,191 --> 19:51:54,000 for example and change this to car some 26539 19:51:51,679 --> 19:51:56,719 people call this char but i call this 26540 19:51:54,000 --> 19:51:57,679 car that's my habit if we change this to 26541 19:51:57,679 --> 19:52:01,679 and try to bolt again you're going to 26542 19:51:59,119 --> 19:52:04,551 see that it is going to weld fine 26543 19:52:01,679 --> 19:52:07,359 because now what is wrong here let's see 26544 19:52:04,551 --> 19:52:10,239 what we got wrong huh we are getting a 26545 19:52:07,360 --> 19:52:11,759 compiler error here what is the problem 26546 19:52:11,759 --> 19:52:17,039 let's go up and try to analyze this a 26547 19:52:14,720 --> 19:52:19,512 little bit it's saying deduce the return 26548 19:52:17,039 --> 19:52:21,191 type does not satisfy placeholder 26549 19:52:21,191 --> 19:52:25,759 and we are requiring for the return type 26550 19:52:23,759 --> 19:52:28,000 of this guy to be tiny time uh-huh i 26551 19:52:25,759 --> 19:52:30,871 think i know what the problem is the 26552 19:52:28,000 --> 19:52:32,720 problem is the arithmetic operations 26553 19:52:30,872 --> 19:52:35,120 like the addition we're doing here are 26554 19:52:32,720 --> 19:52:37,919 not supported for types which are 26555 19:52:35,119 --> 19:52:40,000 smaller than ant and is the smallest 26556 19:52:37,919 --> 19:52:42,160 type we can use with arithmetic 26557 19:52:40,000 --> 19:52:44,160 operations so what the compiler is going 26558 19:52:42,160 --> 19:52:47,512 to do is going to do implicit 26559 19:52:44,160 --> 19:52:49,440 conversions from car to end and inside 26560 19:52:47,512 --> 19:52:52,320 here what we will have are going to be 26561 19:52:49,440 --> 19:52:54,479 ant the result of this is going to be 26562 19:52:52,320 --> 19:52:56,960 ant and the return type is going to be 26563 19:52:56,960 --> 19:53:01,920 is going to violate the requirement we 26564 19:52:59,440 --> 19:53:04,160 have here which is saying that the size 26565 19:53:01,919 --> 19:53:06,800 of tiny type here should be less than 26566 19:53:04,160 --> 19:53:09,680 four and the return of ant is not less 26567 19:53:06,800 --> 19:53:12,080 than four so we can try and fix this by 26568 19:53:09,679 --> 19:53:14,479 putting an equal sign here and i think 26569 19:53:12,080 --> 19:53:16,479 it's going to work let's do that and 26570 19:53:14,479 --> 19:53:18,319 world again this is a good learning 26571 19:53:16,479 --> 19:53:20,159 experience and now you see that the 26572 19:53:18,320 --> 19:53:22,480 build is going to work because now the 26573 19:53:20,160 --> 19:53:25,192 return type is going to satisfy our 26574 19:53:22,479 --> 19:53:27,440 requirements so let's prove that we are 26575 19:53:25,191 --> 19:53:29,512 getting a net out of this actually we're 26576 19:53:27,440 --> 19:53:31,919 going to do our own result and we're 26577 19:53:29,512 --> 19:53:33,919 going to say equal and grab the result 26578 19:53:31,919 --> 19:53:36,160 of this guy we should really prove these 26579 19:53:33,919 --> 19:53:38,800 thanks and after this we're going to 26580 19:53:36,160 --> 19:53:41,440 print the results we can do that and we 26581 19:53:38,800 --> 19:53:43,832 can even go further and print the size 26582 19:53:41,440 --> 19:53:46,160 of result here so we're going to say 26583 19:53:43,831 --> 19:53:49,439 size of result okay now that we have 26584 19:53:46,160 --> 19:53:52,480 this end we can weld with gcc and the 26585 19:53:49,440 --> 19:53:55,040 world is good don't mind this squiggly 26586 19:53:52,479 --> 19:53:56,720 line it is probably visual studio code 26587 19:53:55,039 --> 19:54:00,639 messing with us you see that the world 26588 19:53:56,720 --> 19:54:03,191 is good we can now bring up a terminal 26589 19:54:00,639 --> 19:54:05,360 and run this and see the size of the 26590 19:54:03,191 --> 19:54:07,831 return type and really prove 26591 19:54:05,360 --> 19:54:10,960 that the compiler turned that into an 26592 19:54:07,831 --> 19:54:13,511 end even if we called our function with 26593 19:54:10,960 --> 19:54:15,760 car arguments this is interesting so 26594 19:54:13,512 --> 19:54:18,479 clear and run rooster now we're going to 26595 19:54:15,759 --> 19:54:21,039 see results is going to be 123 and the 26596 19:54:18,479 --> 19:54:24,159 size of the result is for even if we 26597 19:54:21,039 --> 19:54:26,239 passed in car types as our arguments 26598 19:54:24,160 --> 19:54:29,040 okay so hopefully this proves that you 26599 19:54:26,240 --> 19:54:31,600 can set up nested requirements to 26600 19:54:29,039 --> 19:54:33,679 enforce for the expression here to be 26601 19:54:31,600 --> 19:54:36,400 checked another type of requirement you 26602 19:54:33,679 --> 19:54:38,399 can do is a compound requirement let's 26603 19:54:36,399 --> 19:54:40,479 put this in and we're going to put in a 26604 19:54:38,399 --> 19:54:42,639 simple example to drive this home i 26605 19:54:40,479 --> 19:54:46,000 think i should take this function and 26606 19:54:42,639 --> 19:54:48,319 put that below our concepts so a 26607 19:54:46,000 --> 19:54:51,191 compound requirement looks like this you 26608 19:54:48,320 --> 19:54:53,360 put your expression in acrylics like 26609 19:54:51,191 --> 19:54:56,000 this and then you can even put 26610 19:54:53,360 --> 19:54:58,000 requirements on the values of the 26611 19:54:56,000 --> 19:55:00,720 expression for example we can enforce 26612 19:54:58,000 --> 19:55:03,279 that the value of a plus b here should 26613 19:55:00,720 --> 19:55:05,919 be convertible to and if it's not 26614 19:55:03,279 --> 19:55:08,000 convertible to end the concept here is 26615 19:55:05,919 --> 19:55:10,800 going to fail this is a compound 26616 19:55:08,000 --> 19:55:13,191 requirement you can even put no except 26617 19:55:10,800 --> 19:55:15,600 keywords in here but we're not going to 26618 19:55:13,191 --> 19:55:18,000 go this far because we haven't really 26619 19:55:15,600 --> 19:55:19,920 learned about exceptions so for now i'm 26620 19:55:18,000 --> 19:55:22,000 going to test this out i just wanted you 26621 19:55:19,919 --> 19:55:24,319 to be aware that you can do that now 26622 19:55:22,000 --> 19:55:26,639 that we have this concept n let's play 26623 19:55:24,320 --> 19:55:29,120 with it and see how we can make it break 26624 19:55:26,639 --> 19:55:31,600 thanks we're going to change our concept 26625 19:55:29,119 --> 19:55:33,440 to edible let's say edible here and 26626 19:55:31,600 --> 19:55:34,480 we're going to say edible and we're 26627 19:55:34,479 --> 19:55:39,119 put in our car types here and it is 26628 19:55:39,119 --> 19:55:44,799 we can add two car variables and this is 26629 19:55:42,160 --> 19:55:47,279 going to work because we can add two car 26630 19:55:44,800 --> 19:55:48,960 variables and this is valid syntax what 26631 19:55:47,279 --> 19:55:50,959 we're going to get out of that is going 26632 19:55:48,960 --> 19:55:52,960 to be an ant because remember the 26633 19:55:50,960 --> 19:55:55,760 compiler is going to insert implicit 26634 19:55:52,960 --> 19:55:58,960 conversions from card to end because it 26635 19:55:55,759 --> 19:56:01,919 can't really add two card types and is 26636 19:55:58,960 --> 19:56:04,639 the smallest thing you can do arithmetic 26637 19:56:01,919 --> 19:56:06,240 operations on so the result of this is 26638 19:56:04,639 --> 19:56:07,831 going to be an end so a lens is 26639 19:56:06,240 --> 19:56:10,160 convertible to an edge this is going to 26640 19:56:07,831 --> 19:56:12,551 work let's build and show you that this 26641 19:56:10,160 --> 19:56:15,360 is going to work we're going to weld 26642 19:56:12,551 --> 19:56:17,191 this with the gcc you see build is good 26643 19:56:15,360 --> 19:56:20,000 we can try and run this and see what we 26644 19:56:17,191 --> 19:56:23,039 get after calling the function we get 26645 19:56:20,000 --> 19:56:25,279 result is 123 and this is going to be 26646 19:56:23,039 --> 19:56:27,360 the size of four because we get a net 26647 19:56:25,279 --> 19:56:29,600 out of this let's try and change this to 26648 19:56:27,360 --> 19:56:32,160 double and see what we get so double 26649 19:56:29,600 --> 19:56:34,720 let's say double here and if we would we 26650 19:56:32,160 --> 19:56:36,552 expect this to work because adding up 26651 19:56:34,720 --> 19:56:37,512 two doubles is going to give you a 26652 19:56:37,512 --> 19:56:40,872 and we're going to return a double from 26653 19:56:40,872 --> 19:56:45,440 and uh a double is convertible to a net 26654 19:56:43,919 --> 19:56:48,319 so we're going to return a double from 26655 19:56:45,440 --> 19:56:49,832 this guy and this should say eight now 26656 19:56:48,320 --> 19:56:51,760 because that's what we are returning 26657 19:56:49,831 --> 19:56:54,000 from the function here let's build with 26658 19:56:51,759 --> 19:56:56,720 gcc you see the both is good we can 26659 19:56:54,000 --> 19:57:00,000 clear and run rooster this is going to 26660 19:56:56,720 --> 19:57:01,919 say size of result is eight because now 26661 19:57:00,000 --> 19:57:04,080 what we are returning is going to be a 26662 19:57:01,919 --> 19:57:07,279 double from this function here and 26663 19:57:04,080 --> 19:57:10,160 double satisfies our concept adding up 26664 19:57:07,279 --> 19:57:12,160 two doubles is valid syntax and what we 26665 19:57:10,160 --> 19:57:14,480 get out of that is going to be a double 26666 19:57:12,160 --> 19:57:16,720 and a double is convertible to a net 26667 19:57:14,479 --> 19:57:19,279 this is going to work let's try and 26668 19:57:16,720 --> 19:57:21,440 change our parameters here to strength 26669 19:57:19,279 --> 19:57:23,512 we're going to comment out our x and y 26670 19:57:21,440 --> 19:57:26,872 variables here and we're going to set up 26671 19:57:23,512 --> 19:57:28,720 two strings going to say std string x 26672 19:57:26,872 --> 19:57:30,551 and we're going to say hello and you 26673 19:57:28,720 --> 19:57:33,279 know what we're going to say and why 26674 19:57:30,551 --> 19:57:35,919 we're going to say std strength y 26675 19:57:33,279 --> 19:57:37,759 and we're going to say world why not and 26676 19:57:35,919 --> 19:57:40,551 the moment we do this you see that we 26677 19:57:37,759 --> 19:57:44,000 have a problem why do we have a problem 26678 19:57:40,551 --> 19:57:46,399 because it is now a requirement for the 26679 19:57:44,000 --> 19:57:48,639 result of a and b to be convertible to 26680 19:57:46,399 --> 19:57:51,831 end remember that you can add up two 26681 19:57:48,639 --> 19:57:55,039 strands for example you can say auto s 26682 19:57:51,831 --> 19:57:57,039 and say x plus y this is going to work 26683 19:57:55,039 --> 19:57:58,871 because you can add up two strings and 26684 19:57:57,039 --> 19:58:02,000 this is going to concatenate them so 26685 19:57:58,872 --> 19:58:04,551 this is valid syntax why are we failing 26686 19:58:02,000 --> 19:58:07,191 here because we have a requirement for 26687 19:58:04,551 --> 19:58:09,759 this to be convertible to an end and the 26688 19:58:07,191 --> 19:58:12,399 strength isn't really convertible to an 26689 19:58:09,759 --> 19:58:14,639 end so this is going to fail let's build 26690 19:58:12,399 --> 19:58:16,871 this program and show you that this is 26691 19:58:14,639 --> 19:58:20,240 actually the case we're going to see 26692 19:58:16,872 --> 19:58:22,720 world finished with arrows and let's see 26693 19:58:20,240 --> 19:58:25,279 the error we have okay so constraints 26694 19:58:22,720 --> 19:58:28,320 not satisfied it's going to say required 26695 19:58:25,279 --> 19:58:30,720 the satisfaction of audible with auto 26696 19:58:28,320 --> 19:58:31,512 here in requirement and it's going to 26697 19:58:31,512 --> 19:58:36,639 a plus b does not satisfy the type 26698 19:58:34,240 --> 19:58:39,919 requirement because what we get after 26699 19:58:36,639 --> 19:58:42,000 adding a and b is not convertible to an 26700 19:58:42,000 --> 19:58:47,039 a plus b convertible to an end this is 26701 19:58:44,639 --> 19:58:50,080 going to fail okay so let's try and 26702 19:58:47,039 --> 19:58:52,719 remove this requirement to convert to 26703 19:58:50,080 --> 19:58:54,551 end and see that this actually works 26704 19:58:52,720 --> 19:58:56,160 we're going to remove this i'm going to 26705 19:58:56,160 --> 19:59:00,960 and try to build again i am going to use 26706 19:58:58,479 --> 19:59:03,360 gcc to build and now you see that this 26707 19:59:00,960 --> 19:59:06,080 is welding because we no longer 26708 19:59:03,360 --> 19:59:08,399 have the requirement of the return value 26709 19:59:06,080 --> 19:59:11,119 of this expression or result i should 26710 19:59:08,399 --> 19:59:13,360 say the result of this function 26711 19:59:11,119 --> 19:59:15,039 now doesn't have to be convertible to an 26712 19:59:13,360 --> 19:59:17,360 okay let's put this back because i want 26713 19:59:15,039 --> 19:59:19,439 you to see this later as a reference and 26714 19:59:17,360 --> 19:59:22,551 now you know how you can use compound 26715 19:59:19,440 --> 19:59:25,279 requirements to really put constraints 26716 19:59:22,551 --> 19:59:27,360 on your function templates this is 26717 19:59:25,279 --> 19:59:29,759 really all we set out to do in this 26718 19:59:27,360 --> 19:59:32,639 lecture i hope you found it interesting 26719 19:59:29,759 --> 19:59:35,119 i think i should comment out these guys 26720 19:59:32,639 --> 19:59:37,919 because i don't like to leave you with 26721 19:59:35,119 --> 19:59:39,831 code that has compiler errors let's turn 26722 19:59:37,919 --> 19:59:41,919 this back to double so that this works 26723 19:59:39,831 --> 19:59:44,239 we're going to weld again okay the world 26724 19:59:41,919 --> 19:59:45,919 is good and we are going to stop here in 26725 19:59:44,240 --> 19:59:48,480 this lecture in the next one we're going 26726 19:59:45,919 --> 19:59:50,551 to see how we can combine concepts and 26727 19:59:50,551 --> 19:59:54,959 go ahead and finish up here and meet me 26728 19:59:52,872 --> 19:59:57,680 there in this lecture we're going to see 26729 19:59:54,960 --> 20:00:00,160 how we can combine concepts using 26730 19:59:57,679 --> 20:00:03,279 logical operators we can combine 26731 20:00:00,160 --> 20:00:06,552 concepts using the and operator and the 26732 20:00:03,279 --> 20:00:09,600 r operator as you see here let's look at 26733 20:00:06,551 --> 20:00:12,080 a simple example here we have a concept 26734 20:00:09,600 --> 20:00:14,720 it is time type we have seen before and 26735 20:00:12,080 --> 20:00:17,119 it's going to check that the size of the 26736 20:00:14,720 --> 20:00:19,279 template parameter we pass is less than 26737 20:00:17,119 --> 20:00:21,599 four it's going to enforce the syntax 26738 20:00:19,279 --> 20:00:23,679 with a simple requirement but it's also 26739 20:00:21,600 --> 20:00:25,680 going to enforce for the value of this 26740 20:00:23,679 --> 20:00:28,159 expression to be true if the value is 26741 20:00:25,679 --> 20:00:30,551 not true the concept is going to fail 26742 20:00:28,160 --> 20:00:33,192 and we are using a nested requirement 26743 20:00:30,551 --> 20:00:35,599 for this so this is our concept here 26744 20:00:33,191 --> 20:00:38,479 let's try and set up a function and see 26745 20:00:35,600 --> 20:00:41,440 how we can combine this concept with 26746 20:00:38,479 --> 20:00:43,440 other concepts here we have a function 26747 20:00:41,440 --> 20:00:45,760 it is going to return a t the name of 26748 20:00:43,440 --> 20:00:48,160 the function is func it is taking in our 26749 20:00:45,759 --> 20:00:50,159 parameter and here we have the body of 26750 20:00:48,160 --> 20:00:52,960 the function within these two curly 26751 20:00:50,160 --> 20:00:55,832 braces but what we have here is a 26752 20:00:52,960 --> 20:00:58,320 concept we want to apply to this 26753 20:00:55,831 --> 20:01:00,319 function here and the first line here is 26754 20:00:58,320 --> 20:01:01,440 going to show that we can combine the 26755 20:01:01,440 --> 20:01:06,872 like we do here so this entire thing is 26756 20:01:03,759 --> 20:01:09,039 going to be a logical combination of 26757 20:01:06,872 --> 20:01:11,440 concepts so we can require that the 26758 20:01:09,039 --> 20:01:14,799 template parameter be either an integral 26759 20:01:11,440 --> 20:01:17,600 type or a floating point type using the 26760 20:01:14,800 --> 20:01:20,160 or operator here another thing we can do 26761 20:01:17,600 --> 20:01:22,400 is use the and operator as you see down 26762 20:01:20,160 --> 20:01:24,080 here in the second line and we're going 26763 20:01:24,960 --> 20:01:30,720 template parameter is both integral and 26764 20:01:28,320 --> 20:01:33,120 a tiny type and the compiler is going to 26765 20:01:30,720 --> 20:01:35,119 enforce these things as we are about to 26766 20:01:33,119 --> 20:01:37,831 see in a minute when we hit visual 26767 20:01:35,119 --> 20:01:40,639 studio code you can even specify your 26768 20:01:37,831 --> 20:01:43,279 concepts in place for example the third 26769 20:01:40,639 --> 20:01:46,160 line here is going to say we require for 26770 20:01:43,279 --> 20:01:48,160 the concept to be integral so it should 26771 20:01:46,160 --> 20:01:51,760 be an integral type and we're going to 26772 20:01:48,160 --> 20:01:54,639 specify our tiny type concept in place 26773 20:01:51,759 --> 20:01:56,720 you see we are doing that right here in 26774 20:01:54,639 --> 20:01:58,479 the function declaration and this is 26775 20:01:56,720 --> 20:02:01,191 going to work this is a valid c plus 26776 20:01:58,479 --> 20:02:02,639 plus syntax but i don't recommend this 26777 20:02:01,191 --> 20:02:05,599 because this is going to make your 26778 20:02:02,639 --> 20:02:07,039 function declarations really ugly so now 26779 20:02:05,600 --> 20:02:08,960 that you know this we're going to head 26780 20:02:07,039 --> 20:02:11,039 over to visual studio code and play with 26781 20:02:08,960 --> 20:02:14,240 this a little more it's just using the 26782 20:02:11,039 --> 20:02:16,799 or and and operator to combine concepts 26783 20:02:14,240 --> 20:02:18,960 okay here we are in our working folder 26784 20:02:16,800 --> 20:02:21,192 the current project is combining 26785 20:02:18,960 --> 20:02:23,600 concepts we're going to grab our 26786 20:02:21,191 --> 20:02:25,759 template files pretty quick and put them 26787 20:02:23,600 --> 20:02:28,240 in place and we're going to open this in 26788 20:02:25,759 --> 20:02:30,319 visual studio code as we always do by 26789 20:02:28,240 --> 20:02:32,800 dragging and dropping here we have our 26790 20:02:30,320 --> 20:02:34,960 file we're going to open that up and 26791 20:02:32,800 --> 20:02:37,440 close the left pane here i don't like it 26792 20:02:34,960 --> 20:02:40,080 and we're going to include our concepts 26793 20:02:37,440 --> 20:02:41,680 library let's do that we have this and 26794 20:02:40,080 --> 20:02:44,639 the first thing we're going to do is to 26795 20:02:41,679 --> 20:02:47,119 put in our tiny type concept here so it 26796 20:02:44,639 --> 20:02:49,360 is tiny type it's going to require that 26797 20:02:47,119 --> 20:02:51,191 the syntax for the size of t less than 26798 20:02:49,360 --> 20:02:53,119 4b supported this is a simple 26799 20:02:51,191 --> 20:02:56,239 requirement again and we're going to 26800 20:02:53,119 --> 20:02:57,759 require that this expression be true and 26801 20:02:57,759 --> 20:03:02,639 using a nested requirement okay now that 26802 20:03:00,160 --> 20:03:05,600 we have this concept we can combine this 26803 20:03:02,639 --> 20:03:07,600 with other concepts let's use the same 26804 20:03:05,600 --> 20:03:09,680 function we've been using all along so 26805 20:03:07,600 --> 20:03:12,320 we're going to say template type name 26806 20:03:09,679 --> 20:03:14,079 team and we're going to put in our 26807 20:03:12,320 --> 20:03:17,440 requires clause so we're going to say 26808 20:03:14,080 --> 20:03:18,639 requires we can use std intergroup and 26809 20:03:18,639 --> 20:03:24,800 and say or it should be htd floating 26810 20:03:22,320 --> 20:03:28,000 point i think we have this end this is a 26811 20:03:24,800 --> 20:03:30,160 concept we have which is built into the 26812 20:03:28,000 --> 20:03:32,399 c plus plus standard library we can use 26813 20:03:30,160 --> 20:03:35,279 this now that we have this we can then 26814 20:03:32,399 --> 20:03:38,239 say the signature of a function t x is 26815 20:03:35,279 --> 20:03:40,720 going to add up t a and t b and it's 26816 20:03:38,240 --> 20:03:42,480 going to return the sum of a and b now 26817 20:03:40,720 --> 20:03:45,040 you can only call this function with 26818 20:03:42,479 --> 20:03:47,679 either integers or floating points if 26819 20:03:45,039 --> 20:03:49,439 you try to deviate from this the concept 26820 20:03:47,679 --> 20:03:51,831 is going to fail and we're going to get 26821 20:03:49,440 --> 20:03:54,080 a compiler error let's go down and try 26822 20:03:51,831 --> 20:03:57,191 this out we're going to say and x and 26823 20:03:54,080 --> 20:03:59,440 put in a 6 for example and into y and 26824 20:03:57,191 --> 20:04:02,159 put in a 4 and we're going to add this 26825 20:03:59,440 --> 20:04:04,479 up we're going to say add x and y and if 26826 20:04:02,160 --> 20:04:06,552 we do this this is going to work because 26827 20:04:04,479 --> 20:04:09,759 this is an integer so the concept here 26828 20:04:06,551 --> 20:04:11,191 is going to succeed let's weld with gcc 26829 20:04:09,759 --> 20:04:13,511 and show you that the build is good the 26830 20:04:11,191 --> 20:04:15,039 world is good so no problem we can 26831 20:04:13,512 --> 20:04:17,919 change this to double it is going to 26832 20:04:15,039 --> 20:04:20,799 work because double is a floating point 26833 20:04:17,919 --> 20:04:22,399 let's say double properly and if we blow 26834 20:04:20,800 --> 20:04:25,120 it again the bullet is going to go 26835 20:04:22,399 --> 20:04:27,831 through but if we try and use for 26836 20:04:25,119 --> 20:04:30,639 example a strength let's say std 26837 20:04:27,831 --> 20:04:32,799 strength string that's a strength and 26838 20:04:30,639 --> 20:04:35,831 we're going to change this into double 26839 20:04:32,800 --> 20:04:39,192 quotes we can do that and say std 26840 20:04:35,831 --> 20:04:42,239 strength and i put this in quotes again 26841 20:04:39,191 --> 20:04:44,871 double quotes okay if we do this you see 26842 20:04:42,240 --> 20:04:48,000 we have a squiggly line and if we walk 26843 20:04:44,872 --> 20:04:50,800 we're going to have violated our concept 26844 20:04:48,000 --> 20:04:53,191 because the parameters that we pass 26845 20:04:50,800 --> 20:04:55,832 don't satisfy this concept it's neither 26846 20:04:53,191 --> 20:04:58,239 an integral type or a floating point 26847 20:04:55,831 --> 20:05:01,119 type and if we go up here we're going to 26848 20:04:58,240 --> 20:05:03,832 see that we have violated a concept 26849 20:05:01,119 --> 20:05:06,080 constraint not satisfied and it's going 26850 20:05:03,831 --> 20:05:09,039 to say the concept that was not 26851 20:05:06,080 --> 20:05:11,119 satisfied here which is right here okay 26852 20:05:09,039 --> 20:05:13,439 you can combine two concepts using the 26853 20:05:11,119 --> 20:05:14,479 or operator but we can also use the and 26854 20:05:14,479 --> 20:05:18,872 let's go down and try and put up a 26855 20:05:16,479 --> 20:05:21,039 simple example that is going to do that 26856 20:05:18,872 --> 20:05:23,440 now what we're going to require for 26857 20:05:21,039 --> 20:05:24,719 people to use this function is to 26858 20:05:24,720 --> 20:05:30,800 both tiny type and integral so we're 26859 20:05:27,831 --> 20:05:32,551 going to say requires std integral i 26860 20:05:30,800 --> 20:05:33,832 think we have this n and we're going to 26861 20:05:33,831 --> 20:05:38,080 tiny time and we're going to say our 26862 20:05:35,440 --> 20:05:40,872 template parameter here the moment we do 26863 20:05:38,080 --> 20:05:44,000 this and let's turn this back to being 26864 20:05:40,872 --> 20:05:46,960 at let's say ant x and put in a seven 26865 20:05:44,000 --> 20:05:49,919 what we had before and at y and let's 26866 20:05:46,960 --> 20:05:51,360 say four or a five doesn't matter 26867 20:05:51,360 --> 20:05:57,440 let's see our thing again so tiny type 26868 20:05:54,872 --> 20:05:58,400 requires for the size to be less or 26869 20:05:58,399 --> 20:06:02,399 to four so ant is going to work this is 26870 20:06:00,720 --> 20:06:04,960 going to build because the concept is 26871 20:06:02,399 --> 20:06:07,119 going to be satisfied it is both 26872 20:06:07,119 --> 20:06:13,119 let's work and see this run you see the 26873 20:06:10,000 --> 20:06:16,320 world is good our concept is satisfied 26874 20:06:13,119 --> 20:06:18,639 but the moment we try and use something 26875 20:06:18,639 --> 20:06:23,360 we're going to have a problem 26876 20:06:20,639 --> 20:06:26,240 let's use a double for example we're 26877 20:06:23,360 --> 20:06:29,039 going to change this to double and world 26878 20:06:26,240 --> 20:06:32,400 we expect compilation to fail because 26879 20:06:29,039 --> 20:06:34,551 the type which is double doesn't satisfy 26880 20:06:32,399 --> 20:06:36,959 the tiny type concept and we're going to 26881 20:06:34,551 --> 20:06:39,360 see that here okay so we're going to see 26882 20:06:36,960 --> 20:06:41,760 weld finished with arrows and we're 26883 20:06:39,360 --> 20:06:44,479 going to have a lengthy error here but 26884 20:06:41,759 --> 20:06:47,360 the meat of this is that we have a 26885 20:06:44,479 --> 20:06:48,959 constraint which is not satisfied 26886 20:06:48,960 --> 20:06:55,120 that for required for the satisfaction 26887 20:06:51,679 --> 20:06:57,599 of std integral so double is not going 26888 20:06:55,119 --> 20:06:59,360 to satisfy this i think double was a 26889 20:06:57,600 --> 20:07:01,680 really bad example because it's not 26890 20:06:59,360 --> 20:07:04,000 integral so integral is going to fail 26891 20:07:01,679 --> 20:07:05,039 what if we pass in long ant okay which 26892 20:07:05,039 --> 20:07:10,319 an integral type but it's not going to 26893 20:07:07,039 --> 20:07:12,639 satisfy tiny type so let's say long end 26894 20:07:10,320 --> 20:07:15,600 and we're going to build now we are 26895 20:07:12,639 --> 20:07:18,319 going to violate let's use long long ant 26896 20:07:15,600 --> 20:07:20,552 because i think long end is not large 26897 20:07:18,320 --> 20:07:23,280 enough so we're going to build now we're 26898 20:07:20,551 --> 20:07:25,599 going to fail because tiny type is not 26899 20:07:23,279 --> 20:07:27,919 satisfied you see required for a 26900 20:07:25,600 --> 20:07:31,040 dissatisfaction of tiny type and it's 26901 20:07:27,919 --> 20:07:34,399 going to say size of t is less than 4 is 26902 20:07:31,039 --> 20:07:37,831 not satisfied because guess what the 26903 20:07:34,399 --> 20:07:40,159 size of lung lung ant is greater than 4. 26904 20:07:37,831 --> 20:07:44,159 you can try and prove that by taking out 26905 20:07:40,160 --> 20:07:47,440 this guy here and saying stdc out size 26906 20:07:44,160 --> 20:07:49,279 of a long long end we're going to see 26907 20:07:49,279 --> 20:07:52,551 and if we're boiled now we're not going 26908 20:07:50,872 --> 20:07:54,551 to violate our concepts because we're 26909 20:07:54,551 --> 20:07:58,872 the word is going to be good what we 26910 20:07:56,479 --> 20:08:01,512 want to do is run the program and see 26911 20:07:58,872 --> 20:08:05,360 the size of lung lung ant we can do this 26912 20:08:01,512 --> 20:08:08,240 rooster eight the size of lung lung ant 26913 20:08:05,360 --> 20:08:11,512 is eight and it is going to violate our 26914 20:08:08,240 --> 20:08:13,680 tiny type concept okay you can do things 26915 20:08:11,512 --> 20:08:16,639 like this and combine the concepts but 26916 20:08:13,679 --> 20:08:19,279 we can even put our concepts in place we 26917 20:08:16,639 --> 20:08:21,679 can do something crazy and for example 26918 20:08:19,279 --> 20:08:24,639 say let's copy this and we're going to 26919 20:08:21,679 --> 20:08:27,039 go down and say requires integral but 26920 20:08:24,639 --> 20:08:28,479 we're going to put timely type in place 26921 20:08:27,039 --> 20:08:31,039 for example we're going to grab the 26922 20:08:28,479 --> 20:08:33,759 requires close here and copy it 26923 20:08:31,039 --> 20:08:35,511 and put that in place of tiny type here 26924 20:08:35,512 --> 20:08:39,191 and you're going to see that this is 26925 20:08:36,800 --> 20:08:41,279 going to be valid c plus plus syntax if 26926 20:08:39,191 --> 20:08:43,831 we take out the semicolon because it 26927 20:08:41,279 --> 20:08:45,440 shouldn't be there but look at this this 26928 20:08:43,831 --> 20:08:48,159 is really ugly this is going to make 26929 20:08:45,440 --> 20:08:50,479 your functions really hard to read but 26930 20:08:48,160 --> 20:08:52,552 it is going to work exactly the same way 26931 20:08:50,479 --> 20:08:55,279 it did if we call this function we're 26932 20:08:52,551 --> 20:08:57,512 going to violate tiny type let's do that 26933 20:08:55,279 --> 20:09:00,080 we're going to build with gcc we have an 26934 20:08:57,512 --> 20:09:02,872 error let's bring this up so that we can 26935 20:09:00,080 --> 20:09:05,680 see what is wrong nested requirement 26936 20:09:02,872 --> 20:09:08,479 size of t is not satisfied this is a 26937 20:09:05,679 --> 20:09:10,159 syntax you can use but i don't really 26938 20:09:08,479 --> 20:09:12,479 recommend this because this is going to 26939 20:09:10,160 --> 20:09:14,720 make you code hard to read and this is 26940 20:09:12,479 --> 20:09:16,800 really all we set out to do in this 26941 20:09:14,720 --> 20:09:20,000 lecture looking at how we can combine 26942 20:09:16,800 --> 20:09:22,960 our concepts using the and operator and 26943 20:09:20,000 --> 20:09:26,000 the or operator and you can really use 26944 20:09:22,960 --> 20:09:28,639 this in all the syntaxes for concepts we 26945 20:09:26,000 --> 20:09:30,960 have seen but i just used this in this 26946 20:09:28,639 --> 20:09:32,639 lecture because it is simpler to show 26947 20:09:30,960 --> 20:09:34,480 we are going to stop here in this 26948 20:09:32,639 --> 20:09:37,512 lecture the next one we're going to look 26949 20:09:34,479 --> 20:09:39,919 at concepts and auto go ahead and finish 26950 20:09:37,512 --> 20:09:41,512 up here and meet me there in this 26951 20:09:39,919 --> 20:09:44,160 lecture we're going to zoom in on 26952 20:09:41,512 --> 20:09:46,000 concepts and the auto key world we have 26953 20:09:44,160 --> 20:09:48,639 already seen that we can do something 26954 20:09:46,000 --> 20:09:51,191 like this use the auto keywords with our 26955 20:09:48,639 --> 20:09:53,360 functions and let the compiler generate 26956 20:09:51,191 --> 20:09:55,279 a function template behind the scenes 26957 20:09:53,360 --> 20:09:58,320 and this worked pretty well we could use 26958 20:09:55,279 --> 20:10:00,479 this syntax to put constraints on our 26959 20:09:58,320 --> 20:10:03,280 other keywords and we saw that the 26960 20:10:00,479 --> 20:10:05,759 compiler could actually enforce this and 26961 20:10:03,279 --> 20:10:08,639 throw a compiler error if our concept 26962 20:10:05,759 --> 20:10:11,279 here is violated what i haven't shown 26963 20:10:08,639 --> 20:10:13,919 you is that you can even enforce 26964 20:10:11,279 --> 20:10:15,919 concepts on variables that you declare 26965 20:10:13,919 --> 20:10:18,000 you can do something like this set up a 26966 20:10:15,919 --> 20:10:21,119 variable say auto x and call the 26967 20:10:18,000 --> 20:10:23,679 variable x and enforce for the variable 26968 20:10:21,119 --> 20:10:26,551 to be an integral type i don't really 26969 20:10:23,679 --> 20:10:29,039 think you have much use for this because 26970 20:10:26,551 --> 20:10:31,831 once you call the function and assign a 26971 20:10:29,039 --> 20:10:34,079 value to a variable you already have an 26972 20:10:31,831 --> 20:10:36,479 idea of what is going to come out of the 26973 20:10:34,080 --> 20:10:38,720 function but if you have a use case for 26974 20:10:36,479 --> 20:10:40,551 something like this please go ahead and 26975 20:10:38,720 --> 20:10:43,360 use this it is going to enforce your 26976 20:10:40,551 --> 20:10:45,759 concepts you can even use concepts like 26977 20:10:43,360 --> 20:10:48,399 this set up a variable and assign a 26978 20:10:45,759 --> 20:10:51,511 literal to the variable name using the 26979 20:10:48,399 --> 20:10:54,319 auto keyword here but i personally find 26980 20:10:51,512 --> 20:10:55,832 this useless because you are putting in 26981 20:10:55,831 --> 20:11:00,319 why do you want to put a protection if 26982 20:10:58,872 --> 20:11:02,240 you know that you are putting in a 26983 20:11:00,320 --> 20:11:05,040 double literal then the variable should 26984 20:11:02,240 --> 20:11:07,440 be a double type i find this a little 26985 20:11:05,039 --> 20:11:09,360 bit useless but it is supported in c 26986 20:11:07,440 --> 20:11:11,832 plus plus if you have a use case for 26987 20:11:09,360 --> 20:11:13,360 this please go ahead and use this now 26988 20:11:11,831 --> 20:11:15,599 that you know this we're going to head 26989 20:11:13,360 --> 20:11:18,240 over to visual studio code and play with 26990 20:11:15,600 --> 20:11:20,320 us a little more okay here we are in our 26991 20:11:18,240 --> 20:11:22,872 working folder the current project is 26992 20:11:20,320 --> 20:11:24,960 concepts and auto we're going to grab 26993 20:11:22,872 --> 20:11:27,120 our template files and we're going to 26994 20:11:24,960 --> 20:11:29,440 put them in place and we're going to 26995 20:11:27,119 --> 20:11:31,360 open this up in visual studio code let's 26996 20:11:29,440 --> 20:11:33,279 do that pretty quick we're going to have 26997 20:11:31,360 --> 20:11:36,399 our folder open we're going to open the 26998 20:11:33,279 --> 20:11:38,551 main cpp file close the left pane and 26999 20:11:36,399 --> 20:11:41,360 clean things up a little bit and we're 27000 20:11:38,551 --> 20:11:43,512 going to include our concepts library 27001 20:11:41,360 --> 20:11:45,191 and we're going to put in our function 27002 20:11:43,512 --> 20:11:46,720 which is going to be using the auto 27003 20:11:46,720 --> 20:11:51,759 really make the syntax nice and cool but 27004 20:11:49,600 --> 20:11:54,960 we're going to be constraining our auto 27005 20:11:51,759 --> 20:11:56,639 parameters here to use integral types if 27006 20:11:54,960 --> 20:11:59,040 we call this function with something 27007 20:11:56,639 --> 20:12:00,639 that is not an integer or any trigger 27008 20:11:59,039 --> 20:12:02,871 type really we're going to get a 27009 20:12:00,639 --> 20:12:05,720 compiler error so if we go down here for 27010 20:12:02,872 --> 20:12:09,360 example and say add and say 27011 20:12:05,720 --> 20:12:14,160 10.9 and 0.1 we're going to get a 27012 20:12:09,360 --> 20:12:16,639 compiler error because 10.9 and 0.1 are 27013 20:12:14,160 --> 20:12:19,120 double literals and we only support 27014 20:12:16,639 --> 20:12:21,191 integral types in this function here so 27015 20:12:19,119 --> 20:12:23,039 let's work and show you the error but 27016 20:12:21,191 --> 20:12:25,440 you must know this by now because we 27017 20:12:23,039 --> 20:12:27,279 have done this like a gazillion times 27018 20:12:25,440 --> 20:12:29,760 the concept integral is going to be 27019 20:12:27,279 --> 20:12:32,800 violated and it's going to say required 27020 20:12:29,759 --> 20:12:34,720 for the satisfaction of integral so the 27021 20:12:32,800 --> 20:12:36,639 concept is failing here we're going to 27022 20:12:34,720 --> 20:12:39,600 get a compiler error but if we change 27023 20:12:36,639 --> 20:12:41,600 this for example to 10 and 5 put in 27024 20:12:39,600 --> 20:12:42,872 whatever you want which is an integer 27025 20:12:41,600 --> 20:12:44,800 this is going to work we're going to 27026 20:12:44,800 --> 20:12:49,760 you see the wealth is good so you can 27027 20:12:46,720 --> 20:12:51,831 use auto with concepts like this we have 27028 20:12:49,759 --> 20:12:53,759 seen this syntax already a couple of 27029 20:12:51,831 --> 20:12:55,919 times in this chapter what we haven't 27030 20:12:53,759 --> 20:12:59,039 seen is that for example you can set up 27031 20:12:55,919 --> 20:13:02,240 a variable in main and call this x y 27032 20:12:59,039 --> 20:13:05,191 naught and say this variable should 27033 20:13:02,240 --> 20:13:06,960 satisfy the concept and specify the 27034 20:13:05,191 --> 20:13:09,279 concept you want for example let's say 27035 20:13:06,960 --> 20:13:11,192 std integral and if you do something 27036 20:13:09,279 --> 20:13:12,800 like this let's see what we get from 27037 20:13:11,191 --> 20:13:14,871 this we're going to world you see the 27038 20:13:12,800 --> 20:13:17,360 work is good because if we add up these 27039 20:13:14,872 --> 20:13:19,512 guys we're going to generate an integer 27040 20:13:17,360 --> 20:13:22,080 instance for this function and we're 27041 20:13:19,512 --> 20:13:23,832 going to deduce the return type as ant 27042 20:13:22,080 --> 20:13:26,479 so the return type is going to be 27043 20:13:23,831 --> 20:13:29,759 integral and it's going to be assignable 27044 20:13:26,479 --> 20:13:31,679 in our variable here but if we change to 27045 20:13:29,759 --> 20:13:34,239 floating point i think we have this 27046 20:13:31,679 --> 20:13:36,399 concept in place yes we do but it is 27047 20:13:34,240 --> 20:13:38,639 going to fail because what we get from 27048 20:13:36,399 --> 20:13:41,191 this function is going to be an integer 27049 20:13:38,639 --> 20:13:43,119 and we can't assign to this because this 27050 20:13:43,119 --> 20:13:48,239 what you try to assign to this variable 27051 20:13:46,080 --> 20:13:50,000 here should be a floating point this is 27052 20:13:48,240 --> 20:13:52,240 the meaning here and if we do this we're 27053 20:13:50,000 --> 20:13:54,800 going to get to the error probably so we 27054 20:13:52,240 --> 20:13:57,440 have the arrow and it's going to say 27055 20:13:54,800 --> 20:13:59,192 constraint not satisfied and it is going 27056 20:13:59,191 --> 20:14:03,759 constraint we didn't satisfy which is 27057 20:14:01,679 --> 20:14:06,319 its floating point here this is how you 27058 20:14:03,759 --> 20:14:08,799 can put constraints on your variables 27059 20:14:06,320 --> 20:14:11,512 even when you are declaring them so 27060 20:14:08,800 --> 20:14:12,800 another thing we can try here is to 27061 20:14:12,800 --> 20:14:18,720 a literal to this variable here let's 27062 20:14:15,360 --> 20:14:20,551 put in a 3.9 and see if this actually 27063 20:14:22,872 --> 20:14:27,832 you see the build is good and the 27064 20:14:24,872 --> 20:14:30,479 requirement is for what we assigned to 27065 20:14:27,831 --> 20:14:33,360 this variable here should be a double 27066 20:14:30,479 --> 20:14:36,399 and a 3.9 is already a double so no 27067 20:14:33,360 --> 20:14:38,080 problem but if we try and make this 27068 20:14:38,080 --> 20:14:42,639 this is going to fail so let's build 27069 20:14:40,639 --> 20:14:45,279 again you see the concept is going to 27070 20:14:42,639 --> 20:14:48,551 fail and this is how you can use these 27071 20:14:45,279 --> 20:14:50,720 things to constrain your variables i 27072 20:14:48,551 --> 20:14:53,119 personally find this a little bit 27073 20:14:50,720 --> 20:14:56,551 useless at least in this case here 27074 20:14:53,119 --> 20:14:57,679 because i know that i have an integer 27075 20:14:57,679 --> 20:15:02,479 and i am assigning this to a variable 27076 20:15:00,000 --> 20:15:03,440 why do i need the help of concepts to 27077 20:15:03,440 --> 20:15:07,832 constrain my variable here but in the 27078 20:15:05,919 --> 20:15:11,679 case where you are calling a function 27079 20:15:07,831 --> 20:15:14,080 for example using a 5 and an 8 27080 20:15:11,679 --> 20:15:16,799 this can come in handy because it's not 27081 20:15:14,080 --> 20:15:18,720 easy to keep track of the return type 27082 20:15:16,800 --> 20:15:21,440 you get from a function because the 27083 20:15:18,720 --> 20:15:24,399 function might have a return expression 27084 20:15:21,440 --> 20:15:26,551 that has multiple different variables 27085 20:15:24,399 --> 20:15:28,871 and it's not easy to keep track of what 27086 20:15:26,551 --> 20:15:31,919 you get back from a function and you can 27087 20:15:28,872 --> 20:15:33,832 use a constraint like this to make sure 27088 20:15:31,919 --> 20:15:35,919 what you get from the function is what 27089 20:15:33,831 --> 20:15:37,831 you want and if it's not what you want 27090 20:15:35,919 --> 20:15:40,000 you're going to throw a compiler error 27091 20:15:37,831 --> 20:15:42,159 because the concept you use is going to 27092 20:15:40,000 --> 20:15:43,831 fail i would like to welcome you in this 27093 20:15:42,160 --> 20:15:46,872 new chapter where we're going to be 27094 20:15:43,831 --> 20:15:49,439 learning about classes classes are a 27095 20:15:46,872 --> 20:15:52,479 mechanism we have in c plus plus to 27096 20:15:49,440 --> 20:15:54,800 build our own types and use them like we 27097 20:15:52,479 --> 20:15:57,119 have been using other bulletin types 27098 20:15:54,800 --> 20:15:59,120 like and to double or whatever and we're 27099 20:15:57,119 --> 20:16:03,279 going to be able to do that by setting 27100 20:15:59,119 --> 20:16:05,599 up blueprints of our class types and 27101 20:16:03,279 --> 20:16:08,479 we're going to be using those blueprints 27102 20:16:05,600 --> 20:16:10,639 to build actual objects that we can use 27103 20:16:08,479 --> 20:16:12,959 in our c plus plus program so for 27104 20:16:10,639 --> 20:16:16,160 example we can have a class as a 27105 20:16:12,960 --> 20:16:19,600 blueprint and we can create real class 27106 20:16:16,160 --> 20:16:21,760 objects in our c plus plus program as we 27107 20:16:19,600 --> 20:16:24,320 need them and we're going to be learning 27108 20:16:21,759 --> 20:16:25,919 all about this in the next few chapters 27109 20:16:24,320 --> 20:16:28,320 we are going to head over in the next 27110 20:16:25,919 --> 20:16:31,039 lecture and let you set up your very 27111 20:16:28,320 --> 20:16:33,760 first c plus plus class go ahead and 27112 20:16:31,039 --> 20:16:35,919 finish up here and meet me there in this 27113 20:16:33,759 --> 20:16:39,191 lecture you are going to build your 27114 20:16:35,919 --> 20:16:42,080 first c plus plus class classes are a 27115 20:16:39,191 --> 20:16:45,039 mechanism we have in c plus plus to be 27116 20:16:42,080 --> 20:16:47,279 able to build our own types if you 27117 20:16:45,039 --> 20:16:50,000 remember so far in this course we have 27118 20:16:47,279 --> 20:16:50,959 been using basic types like ant and 27119 20:16:50,960 --> 20:16:55,600 and we were using them like this we said 27120 20:16:53,360 --> 20:16:57,360 the type of the variable we give the 27121 20:16:55,600 --> 20:16:59,760 name of the variable and then we 27122 20:16:57,360 --> 20:17:03,279 initialize this variable and we can use 27123 20:16:59,759 --> 20:17:05,919 this throughout our program now we are 27124 20:17:03,279 --> 20:17:08,800 at a point where we need to start 27125 20:17:05,919 --> 20:17:12,720 designing our own types suppose we want 27126 20:17:08,800 --> 20:17:15,192 to build our own type that models a 27127 20:17:12,720 --> 20:17:17,440 person for example suppose a person is 27128 20:17:17,440 --> 20:17:23,760 the age and the address and we want to 27129 20:17:23,759 --> 20:17:29,679 and use them as players for example and 27130 20:17:27,119 --> 20:17:31,679 when we have the properties of a person 27131 20:17:29,679 --> 20:17:33,439 we can do all kinds of crazy things with 27132 20:17:31,679 --> 20:17:35,919 them we can make them jump up for 27133 20:17:33,440 --> 20:17:38,479 example if we are designing a game we 27134 20:17:35,919 --> 20:17:40,800 can make them run we can make them fly 27135 20:17:38,479 --> 20:17:42,239 we can do all kinds of crazy things in 27136 20:17:40,800 --> 20:17:44,720 this lecture we're going to use a 27137 20:17:42,240 --> 20:17:48,000 cylinder as an example and we're going 27138 20:17:44,720 --> 20:17:51,119 to model a cylinder in our c plus plus 27139 20:17:48,000 --> 20:17:54,000 program a cylinder is defined by two 27140 20:17:51,119 --> 20:17:56,871 things it has a base radius which is 27141 20:17:54,000 --> 20:17:58,240 modeled by this r you see here and it 27142 20:17:58,240 --> 20:18:03,512 and when we have this information about 27143 20:18:00,720 --> 20:18:05,279 our cylinder we can really do all kinds 27144 20:18:03,512 --> 20:18:08,551 of crazy things for example we can 27145 20:18:05,279 --> 20:18:10,872 compute its area the base area and the 27146 20:18:10,872 --> 20:18:16,400 r squared this is a formula you get from 27147 20:18:13,679 --> 20:18:18,159 your basic mathematics education 27148 20:18:16,399 --> 20:18:21,511 you can compute the volume of the 27149 20:18:18,160 --> 20:18:24,160 cylinder and the formula to do that is 27150 20:18:21,512 --> 20:18:26,639 area multiplied by the height and we can 27151 20:18:24,160 --> 20:18:30,000 really do all kinds of crazy things with 27152 20:18:26,639 --> 20:18:32,160 the cylinder if we have this information 27153 20:18:30,000 --> 20:18:35,679 so we're going to use this information 27154 20:18:32,160 --> 20:18:38,160 and model a cylinder we can use in our c 27155 20:18:35,679 --> 20:18:41,039 plus plus program and we're going to be 27156 20:18:38,160 --> 20:18:43,192 using classes to do that the syntax to 27157 20:18:41,039 --> 20:18:45,439 set up your class in c plus plus it's 27158 20:18:43,191 --> 20:18:48,551 really simple you say class this is the 27159 20:18:45,440 --> 20:18:50,960 keyword you have to put in here you say 27160 20:18:48,551 --> 20:18:53,679 the name of your class in this case it 27161 20:18:50,960 --> 20:18:57,120 is cylander as you see here and then we 27162 20:18:53,679 --> 20:18:59,759 put a pair of curly braces in place the 27163 20:18:57,119 --> 20:19:02,239 start is on top here and the end is down 27164 20:18:59,759 --> 20:19:04,319 here and after your class definition 27165 20:19:02,240 --> 20:19:07,120 you're going to put a semicolon inside 27166 20:19:04,320 --> 20:19:09,440 the class we really have two parts we 27167 20:19:07,119 --> 20:19:11,599 have member variables which are going to 27168 20:19:09,440 --> 20:19:14,240 model the properties that your class is 27169 20:19:11,600 --> 20:19:16,872 going to have in this case we have two 27170 20:19:14,240 --> 20:19:19,120 properties that really define a cylinder 27171 20:19:16,872 --> 20:19:21,760 we have the best radius and we have the 27172 20:19:19,119 --> 20:19:24,959 height we have decided to model these 27173 20:19:21,759 --> 20:19:27,599 things as double types inside our class 27174 20:19:24,960 --> 20:19:30,720 and this is going to make the first part 27175 20:19:27,600 --> 20:19:33,279 of our class the member variables the 27176 20:19:30,720 --> 20:19:37,360 second part is going to be made of 27177 20:19:33,279 --> 20:19:40,639 behaviors or functions that do things on 27178 20:19:37,360 --> 20:19:43,440 the class using the properties and one 27179 20:19:40,639 --> 20:19:45,440 function we have is the volume function 27180 20:19:43,440 --> 20:19:48,720 which is going to help us compute the 27181 20:19:45,440 --> 20:19:51,832 volume of our cylinder here if you look 27182 20:19:48,720 --> 20:19:54,000 at its syntax it really is a function we 27183 20:19:51,831 --> 20:19:56,319 have a return type we have the name of 27184 20:19:54,000 --> 20:19:58,320 the function we have the parameter list 27185 20:19:56,320 --> 20:20:01,680 which is empty by now and we have our 27186 20:19:58,320 --> 20:20:03,832 curly braces that delimit the body of 27187 20:20:01,679 --> 20:20:05,439 our function inside the function we're 27188 20:20:03,831 --> 20:20:08,239 going to do nothing special we're just 27189 20:20:05,440 --> 20:20:10,320 going to return the volume of our 27190 20:20:08,240 --> 20:20:13,040 function here and if you remember the 27191 20:20:13,039 --> 20:20:19,119 multiplied by the height and this thing 27192 20:20:16,399 --> 20:20:20,799 here is going to model the base area you 27193 20:20:20,800 --> 20:20:25,832 multiplied by the square of the radius 27194 20:20:24,240 --> 20:20:27,680 and then we're going to multiply this by 27195 20:20:25,831 --> 20:20:30,551 the height and we're going to return the 27196 20:20:27,679 --> 20:20:32,000 volume for use by whoever is calling 27197 20:20:32,000 --> 20:20:36,720 one thing we haven't really talked about 27198 20:20:34,160 --> 20:20:40,080 is this public thing you see here 27199 20:20:36,720 --> 20:20:42,160 and what this says is that the members 27200 20:20:40,080 --> 20:20:44,960 that we have in this class are going to 27201 20:20:42,160 --> 20:20:47,512 be accessible from the outside of the 27202 20:20:44,960 --> 20:20:49,832 class for now we can't really understand 27203 20:20:47,512 --> 20:20:52,160 this but when we hit visual studio code 27204 20:20:49,831 --> 20:20:53,191 i'm going to show you what this exactly 27205 20:20:53,191 --> 20:20:58,000 now that we have the class declared we 27206 20:20:55,679 --> 20:21:00,551 can really use it like we use any other 27207 20:20:58,000 --> 20:21:03,119 types in c plus plus for example we can 27208 20:21:03,119 --> 20:21:07,512 and set up a variable called cylinder 27209 20:21:05,279 --> 20:21:10,080 one as you see here is type is going to 27210 20:21:07,512 --> 20:21:12,960 be cylinder and notice that this is 27211 20:21:10,080 --> 20:21:15,119 really a type that we build ourselves 27212 20:21:12,960 --> 20:21:17,760 and we can really use it like we use 27213 20:21:15,119 --> 20:21:20,551 other variables for example we can call 27214 20:21:17,759 --> 20:21:22,720 it function using the syntax here and 27215 20:21:20,551 --> 20:21:25,279 print the volume this is going to print 27216 20:21:22,720 --> 20:21:27,831 the volume using the volume function 27217 20:21:25,279 --> 20:21:30,872 that we just set up in a minute if we go 27218 20:21:27,831 --> 20:21:33,511 down we can modify things about our 27219 20:21:30,872 --> 20:21:36,320 cylinder for example we can use the dot 27220 20:21:33,512 --> 20:21:38,479 notation here to change the base radius 27221 20:21:36,320 --> 20:21:41,192 and change the height and if we try to 27222 20:21:38,479 --> 20:21:43,512 print the volume again this information 27223 20:21:41,191 --> 20:21:46,080 is going to be picked up and be used by 27224 20:21:43,512 --> 20:21:48,479 the volume function to compute the 27225 20:21:46,080 --> 20:21:50,400 volume of our cylinder here this is 27226 20:21:48,479 --> 20:21:52,319 really cool if you want you can also set 27227 20:21:50,399 --> 20:21:54,239 up another cylinder like we do here and 27228 20:21:52,320 --> 20:21:56,872 we're going to print the volume and we 27229 20:21:54,240 --> 20:21:59,192 can really do all kinds of crazy things 27230 20:21:56,872 --> 20:22:01,760 with our types like this okay this is 27231 20:21:59,191 --> 20:22:04,799 our class and we can really use it to do 27232 20:22:01,759 --> 20:22:07,279 a lot before we head over to visual 27233 20:22:04,800 --> 20:22:09,919 studio code and play with us i want to 27234 20:22:07,279 --> 20:22:11,279 bring to your attention that members of 27235 20:22:11,279 --> 20:22:16,800 are private by default and what i mean 27236 20:22:14,240 --> 20:22:19,832 by that is that for example if we don't 27237 20:22:16,800 --> 20:22:21,192 put this public keywords in here we want 27238 20:22:21,191 --> 20:22:27,279 to use the volume function for example 27239 20:22:24,080 --> 20:22:30,000 like we have used it in our main 27240 20:22:30,000 --> 20:22:35,279 if the member is private it won't be 27241 20:22:32,479 --> 20:22:37,512 usable outside the class this is what we 27242 20:22:35,279 --> 20:22:39,831 mean here we are trying to use this 27243 20:22:37,512 --> 20:22:42,400 member in the main function 27244 20:22:39,831 --> 20:22:45,360 outside the class definition it won't be 27245 20:22:42,399 --> 20:22:48,399 accessible if it is private and we make 27246 20:22:45,360 --> 20:22:50,479 it public by putting the public keyword 27247 20:22:48,399 --> 20:22:52,720 and the column like we do here 27248 20:22:50,479 --> 20:22:55,119 everything after this public keyword is 27249 20:22:52,720 --> 20:22:57,512 going to be public until we change this 27250 20:22:55,119 --> 20:23:00,399 to private somewhere in our program if 27251 20:22:57,512 --> 20:23:02,800 we need that now that you have an idea 27252 20:23:00,399 --> 20:23:05,599 about how you can declare and use your 27253 20:23:02,800 --> 20:23:07,440 class in c plus plus let's head over to 27254 20:23:05,600 --> 20:23:10,240 visual studio code and play with us a 27255 20:23:07,440 --> 20:23:12,720 little more okay here we are in our 27256 20:23:10,240 --> 20:23:14,872 working folder the current project is 27257 20:23:12,720 --> 20:23:17,360 your first class you are going to build 27258 20:23:14,872 --> 20:23:19,760 your very first class in c plus plus 27259 20:23:17,360 --> 20:23:22,080 we're going to grab our template files 27260 20:23:19,759 --> 20:23:24,399 we're going to copy them and put them in 27261 20:23:22,080 --> 20:23:26,720 place and then we're going to open this 27262 20:23:24,399 --> 20:23:29,119 little guy in visual studio code by 27263 20:23:26,720 --> 20:23:31,040 dragging and dropping here this is going 27264 20:23:29,119 --> 20:23:33,440 to open our folder we're going to close 27265 20:23:31,039 --> 20:23:35,759 the left pane here and clean things up a 27266 20:23:33,440 --> 20:23:38,872 little bit the first thing we want to do 27267 20:23:38,872 --> 20:23:43,040 but before we do that we're going to 27268 20:23:40,639 --> 20:23:45,119 need the pi variable because remember 27269 20:23:45,119 --> 20:23:51,191 for use in computing the area and the 27270 20:23:48,000 --> 20:23:53,279 volume of our cylinder that we want to 27271 20:23:51,191 --> 20:23:54,959 model like we did in the slides so the 27272 20:23:53,279 --> 20:23:57,191 way we're going to do that i am going to 27273 20:23:54,960 --> 20:23:58,720 declare a const variable and i am going 27274 20:23:58,720 --> 20:24:04,479 and i am going to initialize this with 27275 20:24:01,119 --> 20:24:07,279 pi to get pi i'm going to cheat a little 27276 20:24:04,479 --> 20:24:10,239 bit i'm going to go in my calculator and 27277 20:24:07,279 --> 20:24:12,479 let's see if i can get pi here and i 27278 20:24:10,240 --> 20:24:15,040 click on this and i'm going to grab pi 27279 20:24:12,479 --> 20:24:17,831 here and copy it this is the easiest way 27280 20:24:15,039 --> 20:24:19,679 i can find so i'm going to put it in 27281 20:24:17,831 --> 20:24:21,759 here and this is going to be our 27282 20:24:19,679 --> 20:24:24,319 variable now that we have this we're 27283 20:24:21,759 --> 20:24:27,511 going to set up our class and that to 27284 20:24:24,320 --> 20:24:29,760 define a class you say class you have to 27285 20:24:27,512 --> 20:24:31,600 put in this keyword and then we're going 27286 20:24:29,759 --> 20:24:34,159 to say the name of the class the name of 27287 20:24:31,600 --> 20:24:36,160 the class is going to be cylinder and 27288 20:24:34,160 --> 20:24:38,960 after we do this we're going to put a 27289 20:24:36,160 --> 20:24:41,440 pair of curly braces and we're going to 27290 20:24:38,960 --> 20:24:43,680 end this with a semi-colon you have to 27291 20:24:41,440 --> 20:24:45,919 remember this if you don't put it here 27292 20:24:43,679 --> 20:24:47,119 the ziploc plus compiler is going to 27293 20:24:47,119 --> 20:24:51,919 now that we have the class blueprint 27294 20:24:49,831 --> 20:24:53,679 here we're going to put in our member 27295 20:24:51,919 --> 20:24:56,319 variable and these are going to be the 27296 20:24:53,679 --> 20:24:58,959 variables we use to model the base 27297 20:24:56,320 --> 20:25:01,040 radius and the height of our cylinder 27298 20:24:58,960 --> 20:25:03,120 we're going to put in a double variable 27299 20:25:01,039 --> 20:25:05,511 and we're going to call this base radius 27300 20:25:03,119 --> 20:25:07,759 and we're going to brace initialize this 27301 20:25:05,512 --> 20:25:10,240 to zero we can do this and we're going 27302 20:25:07,759 --> 20:25:12,479 to set up another one and call it height 27303 20:25:10,240 --> 20:25:14,400 and we're going to also initialize this 27304 20:25:12,479 --> 20:25:17,512 to zero okay now that we have this we're 27305 20:25:14,399 --> 20:25:20,000 going to put functions or methods in our 27306 20:25:17,512 --> 20:25:22,400 class and we do that by just putting the 27307 20:25:20,000 --> 20:25:24,872 function definition in our class so our 27308 20:25:22,399 --> 20:25:26,639 function is going to be returning double 27309 20:25:24,872 --> 20:25:27,832 because it's going to be returning the 27310 20:25:27,831 --> 20:25:32,399 of our cylinder so it's going to return 27311 20:25:30,800 --> 20:25:34,960 double here the name is going to be 27312 20:25:32,399 --> 20:25:37,360 volume and it's going to be computing 27313 20:25:34,960 --> 20:25:40,080 the volume and returning that remember 27314 20:25:37,360 --> 20:25:42,960 the formula to compute the volume 27315 20:25:40,080 --> 20:25:45,440 is going to be pi we have a pi variable 27316 20:25:42,960 --> 20:25:48,160 on top and we're going to multiply by 27317 20:25:45,440 --> 20:25:50,800 this radius twice because the base 27318 20:25:48,160 --> 20:25:53,040 radius has to be squared let's do that 27319 20:25:50,800 --> 20:25:55,040 and multiply by base radius again and 27320 20:25:53,039 --> 20:25:58,319 then we're going to multiply by height 27321 20:25:55,039 --> 20:26:00,551 and notice that these are guys declared 27322 20:25:58,320 --> 20:26:02,872 in the body of the class if i can say 27323 20:26:00,551 --> 20:26:05,279 that this is the definition of the class 27324 20:26:02,872 --> 20:26:08,160 we are able to use this guys because we 27325 20:26:05,279 --> 20:26:10,479 are inside this class here okay now that 27326 20:26:08,160 --> 20:26:13,360 we have this our class is really defined 27327 20:26:10,479 --> 20:26:15,512 but we can't really use it quite yet 27328 20:26:13,360 --> 20:26:17,600 what do i mean by that let's go in the 27329 20:26:15,512 --> 20:26:20,240 main function and we're going to set up 27330 20:26:17,600 --> 20:26:23,680 a variable of type cylinder we can do 27331 20:26:20,240 --> 20:26:26,400 that and let's call this cylinder one if 27332 20:26:23,679 --> 20:26:28,319 we do this and try to compile this this 27333 20:26:26,399 --> 20:26:29,439 is going to work fine let's do that 27334 20:26:31,512 --> 20:26:36,000 world finished successfully this is 27335 20:26:33,440 --> 20:26:38,800 welding but we have a function called 27336 20:26:36,000 --> 20:26:42,000 volume here and we can try and call it 27337 20:26:38,800 --> 20:26:44,080 using the dot notation that we saw in 27338 20:26:42,000 --> 20:26:46,800 the slides let's try and print the 27339 20:26:44,080 --> 20:26:48,872 volume of our cylinder here and see what 27340 20:26:46,800 --> 20:26:50,720 we get so we're going to say volume we 27341 20:26:48,872 --> 20:26:53,600 can save that and we're going to say 27342 20:26:50,720 --> 20:26:55,600 cylinder one and do volume okay let's 27343 20:26:53,600 --> 20:26:57,440 see if we can call this function here 27344 20:26:55,600 --> 20:27:00,552 and the moment we do that you see that 27345 20:26:57,440 --> 20:27:02,800 we have a squiggly line here so visual 27346 20:27:00,551 --> 20:27:05,512 studio code has detected that something 27347 20:27:02,800 --> 20:27:08,000 is wrong let's hit the problems tab here 27348 20:27:05,512 --> 20:27:12,551 and see what we get it's going to say 27349 20:27:08,000 --> 20:27:14,720 expected an identifier on line what this 27350 20:27:14,720 --> 20:27:19,440 uh we put three columns here let's 27351 20:27:19,440 --> 20:27:26,320 and the error is going to change to be 27352 20:27:23,191 --> 20:27:29,191 suddenly the volume declared at line 6 27353 20:27:26,320 --> 20:27:31,832 is inaccessible from this location here 27354 20:27:29,191 --> 20:27:33,191 this is what the error says and it is 27355 20:27:33,191 --> 20:27:40,000 because members of the class are private 27356 20:27:36,800 --> 20:27:42,080 by default so they want to be accessible 27357 20:27:40,000 --> 20:27:44,160 from the outside of the class like we 27358 20:27:42,080 --> 20:27:46,960 are doing in the main function here 27359 20:27:44,160 --> 20:27:48,639 notice that we are able to access them 27360 20:27:46,960 --> 20:27:51,600 from the inside of the class you see 27361 20:27:48,639 --> 20:27:53,759 base radius is declared down here and we 27362 20:27:51,600 --> 20:27:56,240 are able to use in the function because 27363 20:27:53,759 --> 20:27:59,679 the function volume here is inside the 27364 20:27:56,240 --> 20:28:02,160 class so the members are accessible even 27365 20:27:59,679 --> 20:28:04,551 if they are private but private members 27366 20:28:02,160 --> 20:28:06,872 can't be accessible from the outside of 27367 20:28:04,551 --> 20:28:08,551 the class like we are doing in the main 27368 20:28:06,872 --> 20:28:10,800 function here that's why we have this 27369 20:28:08,551 --> 20:28:13,191 problem let's see what the compiler says 27370 20:28:10,800 --> 20:28:14,960 we're going to pass this through gcc and 27371 20:28:13,191 --> 20:28:18,319 we're going to get a compiler error that 27372 20:28:14,960 --> 20:28:20,639 we expect and the compiler is really 27373 20:28:18,320 --> 20:28:23,192 specific on this it's going to say 27374 20:28:20,639 --> 20:28:26,080 double cylinder volume the volume 27375 20:28:23,191 --> 20:28:28,871 function from the cylinder class is 27376 20:28:26,080 --> 20:28:31,919 private within this context and which 27377 20:28:28,872 --> 20:28:34,639 context the line 19 when we are trying 27378 20:28:31,919 --> 20:28:37,279 to use it in the main function so if you 27379 20:28:34,639 --> 20:28:40,319 really want thanks inside the class to 27380 20:28:37,279 --> 20:28:43,119 be accessible from the outside you need 27381 20:28:40,320 --> 20:28:45,920 to make them public and change them from 27382 20:28:43,119 --> 20:28:47,919 private how do we do that it is really 27383 20:28:45,919 --> 20:28:50,000 simple you use the syntax we saw in the 27384 20:28:47,919 --> 20:28:53,039 slides we're going to say public and put 27385 20:28:50,000 --> 20:28:55,512 a column here and when you do this it is 27386 20:28:53,039 --> 20:28:58,399 advised to align your things a little 27387 20:28:55,512 --> 20:29:01,440 bit properly so when we do this 27388 20:28:58,399 --> 20:29:04,159 everything after this public keyword is 27389 20:29:01,440 --> 20:29:07,440 going to be public in this class and it 27390 20:29:04,160 --> 20:29:09,360 will be accessible from the outside 27391 20:29:07,440 --> 20:29:11,512 let's try and build now but before we 27392 20:29:09,360 --> 20:29:14,479 build let's show you that the squiggly 27393 20:29:11,512 --> 20:29:17,279 line should be gone visual studio code 27394 20:29:14,479 --> 20:29:19,512 is still confused let's try and vault 27395 20:29:17,279 --> 20:29:21,831 let's pass this through gcc now you see 27396 20:29:19,512 --> 20:29:23,680 that the world is good okay now if you 27397 20:29:21,831 --> 20:29:26,159 try and print the volume here we're 27398 20:29:23,679 --> 20:29:29,191 going to get zero because the base 27399 20:29:26,160 --> 20:29:31,680 radius is braced initialized to zero the 27400 20:29:29,191 --> 20:29:33,119 height is zero and if we multiply like 27401 20:29:31,679 --> 20:29:35,759 this we're going to get a zero and 27402 20:29:33,119 --> 20:29:37,831 return that let's try and run this 27403 20:29:35,759 --> 20:29:40,159 program because we just build it 27404 20:29:37,831 --> 20:29:43,279 successfully we can bring up a terminal 27405 20:29:40,160 --> 20:29:45,832 to do that and let's clear and run 27406 20:29:43,279 --> 20:29:48,639 rooster and we can see that the volume 27407 20:29:45,831 --> 20:29:50,959 is zero and it is what we expect okay 27408 20:29:48,639 --> 20:29:54,000 now our class is working but there is 27409 20:29:50,960 --> 20:29:57,120 really something bad about it and 27410 20:29:54,000 --> 20:29:59,360 that is because the public keyword is 27411 20:29:57,119 --> 20:30:01,512 going to make everything public in this 27412 20:29:59,360 --> 20:30:04,080 class the member variables are also 27413 20:30:01,512 --> 20:30:06,080 going to be public so we can go down 27414 20:30:04,080 --> 20:30:08,000 here and print the base radius and the 27415 20:30:06,080 --> 20:30:10,240 height for example let's do that and 27416 20:30:08,000 --> 20:30:12,800 show you that this is the case so we're 27417 20:30:10,240 --> 20:30:15,760 going to say cylinder one and say base 27418 20:30:12,800 --> 20:30:18,400 radius this is going to print and we're 27419 20:30:15,759 --> 20:30:21,039 going to say hi we're going to print 27420 20:30:18,399 --> 20:30:23,831 this out if we try and print these guys 27421 20:30:21,039 --> 20:30:26,399 let's build and pass this through gcc 27422 20:30:23,831 --> 20:30:28,479 you see the bolt is good and if we clear 27423 20:30:26,399 --> 20:30:31,679 and run rooster we're going to see that 27424 20:30:28,479 --> 20:30:33,599 this radius and height are or accessible 27425 20:30:31,679 --> 20:30:36,000 to show you that this can also be 27426 20:30:33,600 --> 20:30:38,639 different from zero let's change the 27427 20:30:36,000 --> 20:30:42,320 base radius to one and the height to one 27428 20:30:38,639 --> 20:30:44,479 to make it a unit cylinder let's try and 27429 20:30:42,320 --> 20:30:46,920 weld we're going to change these guys to 27430 20:30:44,479 --> 20:30:49,831 one and the volume is going to be 27431 20:30:46,919 --> 20:30:52,160 3.14 or something let's run rooster 27432 20:30:49,831 --> 20:30:54,479 we're going to see our volume here and 27433 20:30:52,160 --> 20:30:57,360 we have the base radius and the height 27434 20:30:54,479 --> 20:30:59,119 but this is really bad design because 27435 20:30:59,119 --> 20:31:04,159 in most cases you're going to want to 27436 20:31:01,191 --> 20:31:07,039 keep them private from the outside 27437 20:31:04,160 --> 20:31:09,120 because users of your class don't really 27438 20:31:07,039 --> 20:31:11,039 care about the member variables they 27439 20:31:09,119 --> 20:31:12,871 just want to do things with your class 27440 20:31:11,039 --> 20:31:15,511 for example calling the volume function 27441 20:31:12,872 --> 20:31:18,720 here so what we usually do in 27442 20:31:15,512 --> 20:31:21,279 practice is to flag our member variables 27443 20:31:18,720 --> 20:31:24,080 as private and we can do that using the 27444 20:31:21,279 --> 20:31:27,191 private keyword as we do here and at the 27445 20:31:24,080 --> 20:31:29,512 moment we do this everything after this 27446 20:31:27,191 --> 20:31:31,919 private keyword is going to be private 27447 20:31:29,512 --> 20:31:34,400 to the class it is only going to be 27448 20:31:31,919 --> 20:31:36,551 accessible from the inside of the class 27449 20:31:34,399 --> 20:31:38,319 but if we try to access that from the 27450 20:31:36,551 --> 20:31:41,279 outside we're going to get the same 27451 20:31:38,320 --> 20:31:44,320 compiler error we saw before when we 27452 20:31:41,279 --> 20:31:46,319 tried to access private stuff from the 27453 20:31:44,320 --> 20:31:48,000 class in the main function here visual 27454 20:31:46,320 --> 20:31:49,920 studio code is going to give us a 27455 20:31:48,000 --> 20:31:52,639 problem saying that these member 27456 20:31:49,919 --> 20:31:54,959 variables are not accessible from this 27457 20:31:52,639 --> 20:31:57,512 context here and if we pass this through 27458 20:31:54,960 --> 20:32:00,160 gcc we're going to get a clear compiler 27459 20:31:57,512 --> 20:32:01,600 error saying that the height 27460 20:32:01,600 --> 20:32:07,120 base radius are now private in the class 27461 20:32:04,720 --> 20:32:09,279 and we can't access them from the 27462 20:32:07,119 --> 20:32:12,080 outside this is really what i wanted you 27463 20:32:09,279 --> 20:32:14,639 to see and this is good design okay now 27464 20:32:12,080 --> 20:32:16,960 that we have our class declared and that 27465 20:32:14,639 --> 20:32:19,440 we can use it in the main function i 27466 20:32:16,960 --> 20:32:22,160 want you to see that it is possible to 27467 20:32:19,440 --> 20:32:24,720 change these member variables but to do 27468 20:32:22,160 --> 20:32:26,400 that we will need to turn this back to 27469 20:32:24,720 --> 20:32:29,119 public and we're going to see a better 27470 20:32:26,399 --> 20:32:30,871 way to do this later but for now let's 27471 20:32:29,119 --> 20:32:33,512 make them public and show you that we 27472 20:32:30,872 --> 20:32:35,760 can change things around and make our 27473 20:32:33,512 --> 20:32:37,760 cylinders a little more interesting so 27474 20:32:35,759 --> 20:32:40,159 now that they are public we can go in 27475 20:32:37,759 --> 20:32:42,239 the main function and do crazy things 27476 20:32:40,160 --> 20:32:44,240 change the data for example we can 27477 20:32:42,240 --> 20:32:46,400 change our member variables to be 27478 20:32:44,240 --> 20:32:49,832 something else let's go down and say 27479 20:32:46,399 --> 20:32:53,039 cylinder one and say base radius we can 27480 20:32:49,831 --> 20:32:55,279 use the dot notation and put an ecosign 27481 20:32:53,039 --> 20:32:58,799 and give another value to our base 27482 20:32:55,279 --> 20:33:01,119 radius let's make it 10 for example and 27483 20:32:58,800 --> 20:33:03,192 change the height to be something else 27484 20:33:01,119 --> 20:33:06,479 we can change that and we're going to 27485 20:33:03,191 --> 20:33:08,479 put in a 3 for example why not now if we 27486 20:33:06,479 --> 20:33:10,639 print the volume again we're going to 27487 20:33:08,479 --> 20:33:12,399 get another value let's copy this and 27488 20:33:12,399 --> 20:33:18,399 our changes here and if we build the 27489 20:33:15,831 --> 20:33:19,439 program it should well define the world 27490 20:33:19,440 --> 20:33:24,240 so we can hit enter and clear and if we 27491 20:33:22,399 --> 20:33:25,871 run rooster we're going to get 27492 20:33:25,872 --> 20:33:31,279 3.14 first here the first line here and 27493 20:33:29,600 --> 20:33:33,680 the second line is going to give us 27494 20:33:31,279 --> 20:33:35,919 another volume and if we multiply these 27495 20:33:35,919 --> 20:33:39,919 and if we use our formula to compute the 27496 20:33:38,320 --> 20:33:42,480 volume we're going to basically get the 27497 20:33:39,919 --> 20:33:45,119 same thing let's try and prove this with 27498 20:33:42,479 --> 20:33:48,239 our calculator we have it around so we 27499 20:33:45,119 --> 20:33:49,440 have pi in here we're going to multiply 27500 20:33:51,440 --> 20:33:56,639 the radius squared we're going to put 27501 20:33:54,240 --> 20:33:58,960 100 here and we're going to multiply 27502 20:33:56,639 --> 20:34:01,720 this with 3 which is our height let's 27503 20:34:01,720 --> 20:34:06,399 942.47 which is what we have here 27504 20:34:04,320 --> 20:34:08,320 roughly the same thing so you see that 27505 20:34:06,399 --> 20:34:10,799 our function is really computing our 27506 20:34:08,320 --> 20:34:12,800 volume we can even go down and change 27507 20:34:10,800 --> 20:34:15,760 the height to something else for example 27508 20:34:12,800 --> 20:34:17,680 we're going to change the height to 27509 20:34:15,759 --> 20:34:19,759 let's make an eight and i printed the 27510 20:34:17,679 --> 20:34:22,399 volume again you can keep playing with 27511 20:34:22,399 --> 20:34:27,511 that our volume function is computing 27512 20:34:24,720 --> 20:34:29,279 our volume using the data that we have 27513 20:34:27,512 --> 20:34:32,080 in the member variables this is the 27514 20:34:29,279 --> 20:34:34,240 message here let's build again the world 27515 20:34:32,080 --> 20:34:36,872 should be good we're going to clear and 27516 20:34:34,240 --> 20:34:39,760 run rooster now we see another 27517 20:34:36,872 --> 20:34:42,000 number for our volume and this is really 27518 20:34:39,759 --> 20:34:43,919 how you can declare a class again you 27519 20:34:42,000 --> 20:34:46,720 use the syntax you see here class you 27520 20:34:43,919 --> 20:34:49,119 say the class name you put a pair of 27521 20:34:46,720 --> 20:34:50,639 curly braces and you put your semicolon 27522 20:34:50,639 --> 20:34:55,759 members of your class are private by 27523 20:34:53,191 --> 20:34:58,319 default but you can make them public if 27524 20:34:55,759 --> 20:35:00,239 you want using the public keyword as we 27525 20:34:58,320 --> 20:35:02,080 did here inside your class you're going 27526 20:35:00,240 --> 20:35:03,919 to have two parts you're going to have 27527 20:35:02,080 --> 20:35:06,800 the member variables which are going to 27528 20:35:03,919 --> 20:35:09,360 be properties that define what your 27529 20:35:06,800 --> 20:35:12,240 class is and we're going to have 27530 20:35:09,360 --> 20:35:14,800 functions or methods let's say that and 27531 20:35:12,240 --> 20:35:17,279 these are going to be behaviors 27532 20:35:14,800 --> 20:35:19,832 of your class you're going to go through 27533 20:35:17,279 --> 20:35:21,919 these functions to make your classes do 27534 20:35:19,831 --> 20:35:24,159 things and once you have the class 27535 20:35:21,919 --> 20:35:25,119 definition in place you can use it to 27536 20:35:25,119 --> 20:35:30,319 variables as we do in the main function 27537 20:35:27,600 --> 20:35:33,040 here and the variables we create from 27538 20:35:30,320 --> 20:35:35,760 our class are usually called objects in 27539 20:35:33,039 --> 20:35:39,039 c plus plus terminology okay the class 27540 20:35:35,759 --> 20:35:42,479 is really a blueprint and we create 27541 20:35:39,039 --> 20:35:45,360 objects using that blueprint and objects 27542 20:35:42,479 --> 20:35:47,440 are going to have runtime data that we 27543 20:35:45,360 --> 20:35:49,919 store in our class okay so for example 27544 20:35:47,440 --> 20:35:52,000 if we hit this point here the base 27545 20:35:49,919 --> 20:35:54,000 radius is going to be 10 and the height 27546 20:35:52,000 --> 20:35:57,512 is going to be eight and that's going to 27547 20:35:54,000 --> 20:36:00,160 be runtime information for our class 27548 20:35:57,512 --> 20:36:03,279 object stored in this variable that we 27549 20:36:00,160 --> 20:36:05,680 call cylinder one here this is really 27550 20:36:03,279 --> 20:36:08,080 what i wanted you to see okay let's try 27551 20:36:05,679 --> 20:36:10,871 and recap what we know about classes so 27552 20:36:08,080 --> 20:36:13,600 far a class can have member variables 27553 20:36:10,872 --> 20:36:16,080 but so far we have used member variables 27554 20:36:13,600 --> 20:36:18,720 as stack variables if we go back for 27555 20:36:16,080 --> 20:36:21,040 example you see that we say base radius 27556 20:36:18,720 --> 20:36:23,191 here you notice it is not a pointer or a 27557 20:36:21,039 --> 20:36:26,000 reference or anything it is a stack 27558 20:36:23,191 --> 20:36:29,440 variable inside our class so member 27559 20:36:26,000 --> 20:36:32,320 variables can only be stack variables 27560 20:36:29,440 --> 20:36:34,639 like we have here but they can also be 27561 20:36:32,320 --> 20:36:37,832 pointers but they can never be 27562 20:36:34,639 --> 20:36:41,039 references and the reason is a reference 27563 20:36:37,831 --> 20:36:42,959 can never be left uninitialized and in 27564 20:36:41,039 --> 20:36:45,599 the classes when we declare member 27565 20:36:42,960 --> 20:36:48,480 variables the ability to leave them 27566 20:36:45,600 --> 20:36:50,872 uninitialized is really important and we 27567 20:36:48,479 --> 20:36:53,512 can't really do that with references i 27568 20:36:50,872 --> 20:36:56,240 can't really go into the details of that 27569 20:36:53,512 --> 20:36:58,479 as we will see that as we progress in 27570 20:36:56,240 --> 20:37:00,320 the chapter but know that member 27571 20:36:58,479 --> 20:37:03,039 variables can either be stacked 27572 20:37:00,320 --> 20:37:05,760 variables or pointers but they can never 27573 20:37:03,039 --> 20:37:08,000 be references in z plus okay that's what 27574 20:37:05,759 --> 20:37:11,279 we just said here members can't be 27575 20:37:08,000 --> 20:37:13,831 references classes can have functions or 27576 20:37:11,279 --> 20:37:16,399 methods that let them do things we saw 27577 20:37:13,831 --> 20:37:19,191 an example of this with our volume 27578 20:37:16,399 --> 20:37:21,759 function class methods have access to 27579 20:37:19,191 --> 20:37:24,000 the member variables regardless of 27580 20:37:21,759 --> 20:37:26,720 whether they are public or private we 27581 20:37:24,000 --> 20:37:30,000 have seen that the volume function will 27582 20:37:26,720 --> 20:37:32,399 have access to our member variables 27583 20:37:30,000 --> 20:37:35,360 regardless of whether it is public or 27584 20:37:32,399 --> 20:37:38,551 private and last but not least private 27585 20:37:35,360 --> 20:37:40,872 members of classes aren't accessible 27586 20:37:38,551 --> 20:37:43,119 from the outside as we saw in the main 27587 20:37:40,872 --> 20:37:45,760 function you can't access them from the 27588 20:37:43,119 --> 20:37:47,759 main function you can only do that from 27589 20:37:45,759 --> 20:37:49,599 the class definition itself this is 27590 20:37:47,759 --> 20:37:52,000 really all i had to share in this 27591 20:37:49,600 --> 20:37:54,320 lecture i hope you found it interesting 27592 20:37:52,000 --> 20:37:56,080 we are going to stop here in this one in 27593 20:37:54,320 --> 20:37:58,960 the next one we're going to see how 27594 20:37:58,960 --> 20:38:04,160 from constructors go ahead and finish up 27595 20:38:01,440 --> 20:38:06,800 here and meet me there in this lecture 27596 20:38:04,160 --> 20:38:10,080 we're going to learn about constructors 27597 20:38:06,800 --> 20:38:12,240 and constructors are special class 27598 20:38:12,240 --> 20:38:18,080 that are called by the compiler to 27599 20:38:15,119 --> 20:38:20,720 construct your class objects and they 27600 20:38:18,080 --> 20:38:23,119 are special because they have no return 27601 20:38:20,720 --> 20:38:26,080 type a constructor can never have a 27602 20:38:23,119 --> 20:38:28,399 return type as you see here they have 27603 20:38:26,080 --> 20:38:31,040 the same name as the class so for 27604 20:38:28,399 --> 20:38:33,599 example for our class in the last 27605 20:38:31,039 --> 20:38:35,439 lecture that was called silenter the 27606 20:38:33,600 --> 20:38:37,360 constructors are also going to have the 27607 20:38:35,440 --> 20:38:41,040 same name as the class they are going to 27608 20:38:37,360 --> 20:38:43,600 be called also slander constructors can 27609 20:38:41,039 --> 20:38:46,079 have parameters but if it makes sense 27610 20:38:43,600 --> 20:38:48,480 for whatever it is you are trying to do 27611 20:38:46,080 --> 20:38:51,512 you can also leave the parameters out 27612 20:38:48,479 --> 20:38:54,551 and constructors are usually used to 27613 20:38:51,512 --> 20:38:57,119 initialize the member variables and put 27614 20:38:54,551 --> 20:38:59,279 them in a state where you want them in 27615 20:38:59,279 --> 20:39:03,679 let's look at how you can declare 27616 20:39:01,440 --> 20:39:06,720 constructors for your classes 27617 20:39:03,679 --> 20:39:08,551 here is our cylinder class you see we 27618 20:39:06,720 --> 20:39:11,360 have our member variables we had they 27619 20:39:11,360 --> 20:39:15,759 a public section which is going to have 27620 20:39:13,512 --> 20:39:17,832 our functions or behaviors as you see 27621 20:39:15,759 --> 20:39:20,720 here the member variables are what we 27622 20:39:17,831 --> 20:39:24,080 have seen from the last lecture but if 27623 20:39:20,720 --> 20:39:24,872 you look here we have a few new things 27624 20:39:24,872 --> 20:39:29,440 we have two things that look like 27625 20:39:27,279 --> 20:39:30,959 functions but they don't have return 27626 20:39:29,440 --> 20:39:32,000 values that's something you should 27627 20:39:32,000 --> 20:39:37,512 they have a parameter list as you see 27628 20:39:34,479 --> 20:39:39,679 here and we have a pair of curly braces 27629 20:39:37,512 --> 20:39:41,919 because these are really functions and 27630 20:39:39,679 --> 20:39:44,639 these are special functions that are 27631 20:39:41,919 --> 20:39:47,759 going to be called by the compiler to 27632 20:39:44,639 --> 20:39:50,551 initialize your class objects if you 27633 20:39:47,759 --> 20:39:52,080 look here in this example we are 27634 20:39:52,080 --> 20:39:57,119 constructor with no parameters to 27635 20:39:54,320 --> 20:39:59,680 initialize our member variables to 2 and 27636 20:39:57,119 --> 20:40:01,191 we have another constructor as you see 27637 20:39:59,679 --> 20:40:03,919 down here which is going to take 27638 20:40:01,191 --> 20:40:06,319 parameters directly and it's going to 27639 20:40:03,919 --> 20:40:09,360 take the radius parameter and the height 27640 20:40:06,320 --> 20:40:11,512 parameter the parameters are of double 27641 20:40:09,360 --> 20:40:14,000 type because that's what we expect in 27642 20:40:11,512 --> 20:40:16,479 our class and we are passing them by 27643 20:40:14,000 --> 20:40:18,872 value here because they are fundamental 27644 20:40:16,479 --> 20:40:20,959 types and we don't really mind copying 27645 20:40:18,872 --> 20:40:23,832 them but please keep in mind that they 27646 20:40:20,960 --> 20:40:26,800 are going to be passed by value and what 27647 20:40:23,831 --> 20:40:30,159 you have inside your constructor body 27648 20:40:26,800 --> 20:40:32,240 are going to be copies of the original 27649 20:40:30,160 --> 20:40:34,320 arguments that were passed to your 27650 20:40:32,240 --> 20:40:37,120 constructor and this is really how you 27651 20:40:34,320 --> 20:40:39,120 declare constructors for your c plus 27652 20:40:37,119 --> 20:40:41,440 plus classes constructors can either 27653 20:40:39,119 --> 20:40:44,319 have parameters like we have again on 27654 20:40:41,440 --> 20:40:46,639 this example here or they can have no 27655 20:40:44,320 --> 20:40:48,480 parameters and this is going to be a 27656 20:40:46,639 --> 20:40:50,872 default constructor we're going to see 27657 20:40:48,479 --> 20:40:52,319 that this is going to be called when you 27658 20:40:52,320 --> 20:40:58,240 variables of this cylinder type without 27659 20:40:55,679 --> 20:40:59,919 passing the parenthesis like we do when 27660 20:40:58,240 --> 20:41:01,512 we call this second constructor here 27661 20:40:59,919 --> 20:41:03,599 we're going to see this in a minute okay 27662 20:41:01,512 --> 20:41:05,440 this is all i think i have to share in 27663 20:41:03,600 --> 20:41:08,000 the slides about how you declare 27664 20:41:05,440 --> 20:41:09,680 constructors let's head over to visual 27665 20:41:08,000 --> 20:41:12,320 studio code and play with this a little 27666 20:41:09,679 --> 20:41:14,319 more okay here we are in our working 27667 20:41:12,320 --> 20:41:17,680 folder the current project is 27668 20:41:14,320 --> 20:41:20,000 constructors we're going to put in our 27669 20:41:17,679 --> 20:41:22,000 template files pretty quick let's do 27670 20:41:20,000 --> 20:41:24,320 that and we are going to open this in 27671 20:41:22,000 --> 20:41:26,240 visual studio code by dragging and 27672 20:41:26,240 --> 20:41:31,760 this is going to open our folder let's 27673 20:41:31,759 --> 20:41:35,831 and we're going to take out what we 27674 20:41:33,279 --> 20:41:39,039 don't need in the main function and on 27675 20:41:35,831 --> 20:41:40,959 top here and i am going to reuse code 27676 20:41:39,039 --> 20:41:43,599 from the last lecture because we don't 27677 20:41:40,960 --> 20:41:45,512 want to type that again so let's put in 27678 20:41:43,600 --> 20:41:48,320 our class definition we have our 27679 20:41:45,512 --> 20:41:51,360 variable pi which is going to be used to 27680 20:41:48,320 --> 20:41:53,680 compute the volume in the class here and 27681 20:41:51,360 --> 20:41:56,080 we have member variables okay now that 27682 20:41:53,679 --> 20:41:58,719 we are here let's change the member 27683 20:41:56,080 --> 20:42:01,279 variables to be private because making 27684 20:41:58,720 --> 20:42:03,680 them public is really going to be bad 27685 20:42:01,279 --> 20:42:05,759 design it's going to expose them in the 27686 20:42:03,679 --> 20:42:08,799 main function and people can mess with 27687 20:42:05,759 --> 20:42:12,479 them and really change the logic of how 27688 20:42:08,800 --> 20:42:14,872 our class works without our concept it 27689 20:42:12,479 --> 20:42:17,119 is advised to make your member variables 27690 20:42:14,872 --> 20:42:20,080 private unless you really have a 27691 20:42:17,119 --> 20:42:22,871 compelling reasons to make them public 27692 20:42:20,080 --> 20:42:24,960 okay now that we have our class here 27693 20:42:22,872 --> 20:42:26,551 let's set up constructors for it so 27694 20:42:24,960 --> 20:42:29,040 we're going to go down here in the 27695 20:42:26,551 --> 20:42:30,872 public scope and say constructors and 27696 20:42:29,039 --> 20:42:33,039 we're going to set up the first 27697 20:42:30,872 --> 20:42:35,360 constructor which is not going to take 27698 20:42:33,039 --> 20:42:37,511 any parameter so we're going to say the 27699 20:42:35,360 --> 20:42:40,320 name of the class remember a constructor 27700 20:42:37,512 --> 20:42:43,360 doesn't have a return type and it has to 27701 20:42:40,320 --> 20:42:45,192 have the same name as the class so we 27702 20:42:43,360 --> 20:42:48,720 need to comply with that and we're going 27703 20:42:45,191 --> 20:42:50,239 to put our pair of curly braces and 27704 20:42:48,720 --> 20:42:53,191 inside we're going to initialize our 27705 20:42:50,240 --> 20:42:54,480 member variables however we want so what 27706 20:42:53,191 --> 20:42:57,440 we're going to do here we're going to 27707 20:42:54,479 --> 20:43:00,479 set the base radius to something let's 27708 20:42:57,440 --> 20:43:02,400 make it a 2 like we saw in the slides no 27709 20:43:00,479 --> 20:43:04,639 problem with that and we're going to set 27710 20:43:02,399 --> 20:43:07,191 the height to something else and we are 27711 20:43:04,639 --> 20:43:10,160 going to set the height to 2 as well we 27712 20:43:07,191 --> 20:43:13,191 can do that you can use what you want 27713 20:43:10,160 --> 20:43:16,000 and now we have our constructor and it 27714 20:43:13,191 --> 20:43:18,399 is initializing our thanks okay now that 27715 20:43:16,000 --> 20:43:20,800 we have the constructor in place 27716 20:43:18,399 --> 20:43:23,831 how is it really called by the compiler 27717 20:43:20,800 --> 20:43:26,479 and notice that our constructor is in 27718 20:43:23,831 --> 20:43:28,959 the public scope of the class 27719 20:43:26,479 --> 20:43:31,599 if it isn't public you want to be able 27720 20:43:28,960 --> 20:43:34,320 to access it from the outside and the 27721 20:43:31,600 --> 20:43:36,080 compiler will fail to build your objects 27722 20:43:34,320 --> 20:43:38,000 and we're going to see this in a minute 27723 20:43:36,080 --> 20:43:40,400 but before we do that let's try and 27724 20:43:38,000 --> 20:43:42,800 create a cylinder object now so we're 27725 20:43:40,399 --> 20:43:45,360 going to say cylinder and say cylinder 27726 20:43:42,800 --> 20:43:48,720 one like we did in the last lecture 27727 20:43:45,360 --> 20:43:52,639 and when we do this notice what we see 27728 20:43:48,720 --> 20:43:55,279 if we print the volume of our cylinder 27729 20:43:52,639 --> 20:43:58,240 object here this is an object because 27730 20:43:55,279 --> 20:44:00,240 we're creating this from our blueprint 27731 20:43:58,240 --> 20:44:02,160 which is the class definition here you 27732 20:44:00,240 --> 20:44:04,720 really need to know this this is a class 27733 20:44:02,160 --> 20:44:06,720 object let's say that and we're going to 27734 20:44:04,720 --> 20:44:08,800 print its volume we're going to say 27735 20:44:06,720 --> 20:44:11,191 cylinder one and we're going to call our 27736 20:44:08,800 --> 20:44:12,639 volume method we should say and if we 27737 20:44:11,191 --> 20:44:14,159 build you're going to see that this is 27738 20:44:14,160 --> 20:44:19,440 the volume is not going to be coming 27739 20:44:16,160 --> 20:44:22,639 from the initial values we have here we 27740 20:44:19,440 --> 20:44:25,119 will have changed these guys to 2 27741 20:44:22,639 --> 20:44:26,960 from our constructor here because the 27742 20:44:25,119 --> 20:44:29,831 compiler is going to look at this line 27743 20:44:26,960 --> 20:44:32,320 here and see that we are trying to build 27744 20:44:29,831 --> 20:44:34,239 a silent object it's going to look at 27745 20:44:32,320 --> 20:44:36,960 the class definition and it is going to 27746 20:44:34,240 --> 20:44:39,192 say do i have any constructor 27747 20:44:36,960 --> 20:44:41,360 that i can use to build an object from 27748 20:44:39,191 --> 20:44:43,831 the syntax i see here and it's going to 27749 20:44:41,360 --> 20:44:45,680 see that it has a constructor that 27750 20:44:43,831 --> 20:44:48,871 doesn't take any parameter and it's 27751 20:44:45,679 --> 20:44:50,959 going to use it to build our object and 27752 20:44:48,872 --> 20:44:53,680 it's going to be using this information 27753 20:44:50,960 --> 20:44:55,512 to build our cylinder object so the base 27754 20:44:53,679 --> 20:44:57,359 radius is going to be two the height is 27755 20:44:55,512 --> 20:44:59,832 going to be two and it's going to use 27756 20:44:59,831 --> 20:45:04,159 that we print here you really need to 27757 20:45:05,440 --> 20:45:11,680 if gcc accepts this you see the both is 27758 20:45:08,872 --> 20:45:13,760 good now we can bring up a terminal and 27759 20:45:11,679 --> 20:45:16,399 clear and run rooster you're going to 27760 20:45:13,759 --> 20:45:19,679 see that we have 25 something which is a 27761 20:45:16,399 --> 20:45:22,479 result of using this information we have 27762 20:45:19,679 --> 20:45:24,959 here if we take pi multiplied by this 27763 20:45:22,479 --> 20:45:27,279 base radius multiply by this base radius 27764 20:45:24,960 --> 20:45:29,280 again and multiply by height we're going 27765 20:45:27,279 --> 20:45:31,679 to get this here you can try this out it 27766 20:45:29,279 --> 20:45:34,720 is going to be what you see here and 27767 20:45:31,679 --> 20:45:37,599 this hopefully proves that the compiler 27768 20:45:34,720 --> 20:45:39,512 is using our constructor here but we can 27769 20:45:39,512 --> 20:45:44,800 let's set up a breakpoint here 27770 20:45:42,800 --> 20:45:47,760 and we're going to run this through our 27771 20:45:44,800 --> 20:45:48,479 debugger we can come back to the run tab 27772 20:45:50,399 --> 20:45:55,191 and we're going to try and use the 27773 20:45:55,191 --> 20:46:00,080 with the gcc compiler that's what we can 27774 20:45:57,679 --> 20:46:01,119 use if you are on windows you can also 27775 20:46:01,119 --> 20:46:05,599 tools that come with the compiler from 27776 20:46:03,440 --> 20:46:08,240 microsoft but we're not going to use 27777 20:46:05,600 --> 20:46:10,080 that now gcc is going to be fine we're 27778 20:46:08,240 --> 20:46:12,872 going to start the debugging and we 27779 20:46:10,080 --> 20:46:15,440 should hit our breakpoint when the build 27780 20:46:12,872 --> 20:46:17,192 is done let's wait a minute okay we have 27781 20:46:17,191 --> 20:46:22,479 we can minimize this a little bit and i 27782 20:46:20,160 --> 20:46:24,720 show the terminal to the right here i 27783 20:46:22,479 --> 20:46:27,191 think we can do that let's do that let's 27784 20:46:24,720 --> 20:46:29,279 put that a little bit to the right here 27785 20:46:27,191 --> 20:46:32,639 and minimize a little bit okay we have 27786 20:46:29,279 --> 20:46:34,479 our thunk in place we have hit our break 27787 20:46:32,639 --> 20:46:36,800 point you see these are the local 27788 20:46:34,479 --> 20:46:40,080 variables we have here we have a 27789 20:46:36,800 --> 20:46:42,240 cylinder object which is not initialized 27790 20:46:40,080 --> 20:46:45,440 yet because you see what we have inside 27791 20:46:42,240 --> 20:46:48,480 is really junk data we haven't put in 27792 20:46:45,440 --> 20:46:51,119 anything yet but now that we are at this 27793 20:46:48,479 --> 20:46:54,639 break point here we can hit step into 27794 20:46:51,119 --> 20:46:57,360 and step into the code that constructs 27795 20:46:54,639 --> 20:47:01,119 our cylinder object because at this 27796 20:46:57,360 --> 20:47:02,872 point liner 25 here hasn't run yet so 27797 20:47:01,119 --> 20:47:05,360 we're going to step into and you're 27798 20:47:02,872 --> 20:47:08,551 going to see that we hit the constructor 27799 20:47:09,360 --> 20:47:14,240 this again proves that our constructor 27800 20:47:11,759 --> 20:47:15,360 is being used to build our cylinder 27801 20:47:15,360 --> 20:47:19,440 now we can move to the next line we're 27802 20:47:17,119 --> 20:47:22,239 going to set up the base radius and if 27803 20:47:19,440 --> 20:47:24,479 we hit next here this is going to set 27804 20:47:24,479 --> 20:47:29,279 and you see that height is in one 27805 20:47:26,639 --> 20:47:31,279 because we haven't set it up yet but if 27806 20:47:29,279 --> 20:47:33,039 we execute this line you're going to see 27807 20:47:31,279 --> 20:47:33,919 that height is also going to change to 27808 20:47:33,919 --> 20:47:38,872 and now if we hit next we're going to 27809 20:47:36,080 --> 20:47:40,872 get out of our constructor and our 27810 20:47:38,872 --> 20:47:43,512 cylinder object will be already 27811 20:47:40,872 --> 20:47:46,160 constructed if we look again here we're 27812 20:47:43,512 --> 20:47:49,040 going to see that radius is 2 height is 27813 20:47:46,160 --> 20:47:50,720 2 and we can print the volume and if we 27814 20:47:49,039 --> 20:47:53,279 hit step over we're going to see our 27815 20:47:50,720 --> 20:47:56,399 volume printed out here hopefully again 27816 20:47:53,279 --> 20:47:58,319 this proves that our constructor here is 27817 20:47:58,320 --> 20:48:04,400 objects and it is doing that because it 27818 20:48:01,440 --> 20:48:07,440 is the syntax here we don't pass any 27819 20:48:04,399 --> 20:48:10,159 parameter to our cylinder object as we 27820 20:48:07,440 --> 20:48:12,800 created so this must be calling the 27821 20:48:10,160 --> 20:48:15,192 default constructor which doesn't take 27822 20:48:12,800 --> 20:48:17,760 any parameter this is the message here 27823 20:48:15,191 --> 20:48:20,799 let's close our debugging session and 27824 20:48:17,759 --> 20:48:22,159 maximize our visual studio code instance 27825 20:48:20,800 --> 20:48:23,832 because we're going to set up another 27826 20:48:22,160 --> 20:48:26,240 constructor which is going to take 27827 20:48:23,831 --> 20:48:28,551 parameters now and you have seen the 27828 20:48:26,240 --> 20:48:30,639 syntax to do that in the slides it is 27829 20:48:28,551 --> 20:48:32,239 nothing complicated we're going to say 27830 20:48:30,639 --> 20:48:33,919 the name of the constructor which is 27831 20:48:32,240 --> 20:48:35,600 going to be the name of the class and 27832 20:48:35,600 --> 20:48:40,160 and say rad param for radius and we're 27833 20:48:42,639 --> 20:48:46,080 height and we're going to make this 27834 20:48:44,551 --> 20:48:49,191 double and after that we're going to 27835 20:48:46,080 --> 20:48:51,831 head in the body of our constructor and 27836 20:48:49,191 --> 20:48:54,239 we're going to initialize our guides to 27837 20:48:51,831 --> 20:48:55,919 the parameters that will pass to the 27838 20:48:54,240 --> 20:48:58,080 function here and this is really 27839 20:48:55,919 --> 20:49:00,551 powerful after we have this constructor 27840 20:49:00,551 --> 20:49:05,512 parameters that will be used to 27841 20:49:02,800 --> 20:49:08,080 construct our cylinder object so we can 27842 20:49:08,080 --> 20:49:13,512 red param and we can say height and say 27843 20:49:11,191 --> 20:49:16,159 height param here and these are going to 27844 20:49:13,512 --> 20:49:18,720 be used to construct our object now we 27845 20:49:16,160 --> 20:49:20,872 can head over in the main function and 27846 20:49:18,720 --> 20:49:23,831 change how we construct our cylinder 27847 20:49:20,872 --> 20:49:25,440 object if we want to use tan as the 27848 20:49:27,679 --> 20:49:33,511 height we can do that by passing 27849 20:49:29,831 --> 20:49:35,439 arguments to our constructor here 27850 20:49:33,512 --> 20:49:37,919 let's run this through the debugger 27851 20:49:35,440 --> 20:49:39,512 again and see that our information here 27852 20:49:39,512 --> 20:49:44,551 now this constructor here is going to be 27853 20:49:42,080 --> 20:49:47,040 chosen by the compiler because it is 27854 20:49:44,551 --> 20:49:50,000 more fitting for the call that we are 27855 20:49:47,039 --> 20:49:53,119 doing here let's run our debugging 27856 20:49:50,000 --> 20:49:55,600 session the program is going to be bolt 27857 20:49:53,119 --> 20:49:58,479 and we are going to try and maximize 27858 20:49:55,600 --> 20:50:00,800 this a little bit and bring up our 27859 20:49:58,479 --> 20:50:03,360 terminal output okay we have hit the 27860 20:50:00,800 --> 20:50:05,760 breakpoint here we can step into our 27861 20:50:03,360 --> 20:50:08,000 constructor and notice the constructor 27862 20:50:05,759 --> 20:50:10,399 that the compiler chooses our 27863 20:50:08,000 --> 20:50:13,440 constructor that takes parameters is 27864 20:50:10,399 --> 20:50:16,159 chosen here and we can do things inside 27865 20:50:13,440 --> 20:50:18,960 let's hit next we're going to initialize 27866 20:50:16,160 --> 20:50:21,360 the base radius to 10 and we're going to 27867 20:50:21,360 --> 20:50:26,320 four okay we can see that to the left 27868 20:50:23,759 --> 20:50:28,000 here if we hit next the height is going 27869 20:50:26,320 --> 20:50:29,680 to be changed to four and our 27870 20:50:28,000 --> 20:50:31,191 constructor is going to be done we're 27871 20:50:31,191 --> 20:50:35,512 in the main function again and if we 27872 20:50:33,440 --> 20:50:38,160 step over now we're going to print a new 27873 20:50:35,512 --> 20:50:41,040 volume which is based on the information 27874 20:50:38,160 --> 20:50:43,760 we used to construct our object here 27875 20:50:41,039 --> 20:50:46,639 this is really what constructors do they 27876 20:50:43,759 --> 20:50:49,191 are used by the compiler to build your 27877 20:50:46,639 --> 20:50:52,000 objects and we saw the syntax we can use 27878 20:50:49,191 --> 20:50:54,639 to set up constructors for our own 27879 20:50:52,000 --> 20:50:57,679 classes okay now you must be saying if 27880 20:50:54,639 --> 20:50:58,960 we need constructors to build our 27881 20:50:58,960 --> 20:51:02,400 why was the code in the last lecture 27882 20:51:02,399 --> 20:51:06,959 in the last lecture we did something 27883 20:51:04,320 --> 20:51:10,080 like this we said cylinder cylinder one 27884 20:51:06,960 --> 20:51:12,800 cylinder one and this was welding and 27885 20:51:10,080 --> 20:51:15,040 compiling fine and let's try and take 27886 20:51:12,800 --> 20:51:17,760 out our constructors we can do that 27887 20:51:15,039 --> 20:51:19,119 let's comment them out and if you look 27888 20:51:17,759 --> 20:51:21,439 you're going to see that the code is 27889 20:51:19,119 --> 20:51:24,319 going to compile fine even if we don't 27890 20:51:21,440 --> 20:51:27,040 have any constructors in here let's 27891 20:51:24,320 --> 20:51:30,080 build with gcc to show you that the code 27892 20:51:27,039 --> 20:51:32,239 is going to compile both is good so how 27893 20:51:32,240 --> 20:51:37,600 constructing our objects even if we 27894 20:51:35,119 --> 20:51:40,159 don't have any constructors in here well 27895 20:51:37,600 --> 20:51:42,872 the reason is the compiler is going to 27896 20:51:40,160 --> 20:51:44,800 set up its own constructor and what it 27897 20:51:42,872 --> 20:51:47,360 is going to set up is a constructor 27898 20:51:44,800 --> 20:51:49,680 which is really empty and doesn't have 27899 20:51:47,360 --> 20:51:52,479 anything inside it's basically going to 27900 20:51:49,679 --> 20:51:54,799 be as if we put in an empty constructor 27901 20:51:52,479 --> 20:51:57,759 let's copy this and actually show you 27902 20:51:54,800 --> 20:52:00,400 this we can do that and we're going to 27903 20:51:57,759 --> 20:52:02,551 make this constructor empty and not do 27904 20:52:00,399 --> 20:52:04,720 anything in the body this is the 27905 20:52:02,551 --> 20:52:07,919 constructor that your compiler is going 27906 20:52:04,720 --> 20:52:09,440 to generate and if we're both now and i 27907 20:52:09,440 --> 20:52:13,919 set up a break point here you're going 27908 20:52:11,512 --> 20:52:16,479 to see that our empty constructor is 27909 20:52:13,919 --> 20:52:18,160 going to be chosen let's debug and show 27910 20:52:16,479 --> 20:52:20,399 you that okay you see that we're going 27911 20:52:18,160 --> 20:52:22,400 to hit the breakpoint here and if we 27912 20:52:20,399 --> 20:52:24,399 step into the compiler is going to 27913 20:52:22,399 --> 20:52:27,191 choose this guy and it's not going to do 27914 20:52:24,399 --> 20:52:29,279 anything so it's basically going to take 27915 20:52:29,279 --> 20:52:33,679 they are and it's going to use the 27916 20:52:31,279 --> 20:52:34,399 information to print the volume here we 27917 20:52:35,919 --> 20:52:41,191 and get out of our constructor 27918 20:52:41,191 --> 20:52:47,360 step over we should print our volume 27919 20:52:44,639 --> 20:52:48,479 which is going to be 3.14 because the 27920 20:52:50,872 --> 20:52:55,919 initial values we have in our member 27921 20:52:53,119 --> 20:52:58,639 variables here so again the message is 27922 20:52:55,919 --> 20:53:01,191 if you don't have any constructor in 27923 20:52:58,639 --> 20:53:03,600 your class the compiler is going to 27924 20:53:01,191 --> 20:53:05,512 generate an empty constructor for you 27925 20:53:03,600 --> 20:53:08,000 and the empty constructor is going to 27926 20:53:05,512 --> 20:53:09,832 take no parameters and it's not going to 27927 20:53:08,000 --> 20:53:13,039 have anything in the body it's going to 27928 20:53:09,831 --> 20:53:15,831 be an empty constructor and it is called 27929 20:53:13,039 --> 20:53:18,239 a default constructor generated by the 27930 20:53:15,831 --> 20:53:19,919 compiler this is really all i had to 27931 20:53:18,240 --> 20:53:22,160 share in this lecture i hope you found 27932 20:53:19,919 --> 20:53:24,240 it interesting now you should have 27933 20:53:22,160 --> 20:53:26,872 enough knowledge to build your own 27934 20:53:24,240 --> 20:53:29,040 constructors for your classes we are 27935 20:53:26,872 --> 20:53:30,800 going to stop here in this lecture the 27936 20:53:29,039 --> 20:53:32,799 next one we're going to learn about 27937 20:53:34,639 --> 20:53:38,080 and meet me there in this lecture we're 27938 20:53:38,080 --> 20:53:43,512 constructors and this is a syntax you 27939 20:53:43,512 --> 20:53:48,639 generate a default constructor for you 27940 20:53:46,320 --> 20:53:51,120 all you have to do is do things like 27941 20:53:48,639 --> 20:53:52,800 this you're going to say the name of the 27942 20:53:51,119 --> 20:53:54,399 constructor which is going to be the 27943 20:53:52,800 --> 20:53:56,872 same name as the class you're going to 27944 20:53:54,399 --> 20:53:59,831 put in an empty parameter list and then 27945 20:53:56,872 --> 20:54:01,919 you're going to say equals default 27946 20:53:59,831 --> 20:54:05,039 if you do this the compiler is going to 27947 20:54:01,919 --> 20:54:05,831 generate a default empty constructor for 27948 20:54:05,831 --> 20:54:10,080 in the last lecture we have seen that we 27949 20:54:07,919 --> 20:54:13,039 can set up our own constructors 27950 20:54:10,080 --> 20:54:15,759 basically like this what i haven't told 27951 20:54:13,039 --> 20:54:19,191 you is that the moment you set up your 27952 20:54:15,759 --> 20:54:22,080 own constructor the compiler is going to 27953 20:54:19,191 --> 20:54:24,479 not generate the default constructor and 27954 20:54:22,080 --> 20:54:27,360 if you want to keep the ability to 27955 20:54:24,479 --> 20:54:29,831 construct objects without passing 27956 20:54:27,360 --> 20:54:32,080 parameters you're going to have to put 27957 20:54:29,831 --> 20:54:35,360 in your own default constructor and you 27958 20:54:32,080 --> 20:54:37,600 can use this syntax here to do that 27959 20:54:35,360 --> 20:54:39,680 let's head over to visual studio code 27960 20:54:37,600 --> 20:54:42,400 and talk about this a little more okay 27961 20:54:39,679 --> 20:54:44,399 here we are in our working folder the 27962 20:54:44,399 --> 20:54:49,039 constructors we're going to grab our 27963 20:54:46,960 --> 20:54:51,440 template files and we're going to put 27964 20:54:49,039 --> 20:54:53,679 them in place and we are going to open 27965 20:54:51,440 --> 20:54:55,512 this in visual studio code by dragging 27966 20:54:53,679 --> 20:54:58,399 and dropping here and we're going to 27967 20:54:55,512 --> 20:55:00,800 open our main cpp file let's take out 27968 20:54:58,399 --> 20:55:03,039 what we don't need we're going to clean 27969 20:55:00,800 --> 20:55:05,600 the main function here and we are going 27970 20:55:03,039 --> 20:55:07,511 to bring in the class definition we had 27971 20:55:07,512 --> 20:55:12,800 if you remember we had two constructors 27972 20:55:10,160 --> 20:55:16,320 and one constructor which didn't take 27973 20:55:12,800 --> 20:55:18,551 any parameter and another constructor 27974 20:55:16,320 --> 20:55:21,040 which was initializing our member 27975 20:55:18,551 --> 20:55:24,000 variables with parameters that we passed 27976 20:55:21,039 --> 20:55:27,439 in this parameter list here but in this 27977 20:55:24,000 --> 20:55:30,080 lecture let's take out this constructor 27978 20:55:27,440 --> 20:55:32,720 that doesn't take any parameter 27979 20:55:30,080 --> 20:55:35,279 and show you that when you set up your 27980 20:55:32,720 --> 20:55:37,600 own constructor the compiler is not 27981 20:55:35,279 --> 20:55:39,440 going to generate a default constructor 27982 20:55:37,600 --> 20:55:42,552 for you and we're going to lose the 27983 20:55:39,440 --> 20:55:45,119 ability to construct objects without 27984 20:55:42,551 --> 20:55:46,720 passing parameters the syntax we 27985 20:55:45,119 --> 20:55:49,512 basically see here we're going to say 27986 20:55:46,720 --> 20:55:52,320 cylinder and say cylinder one let's use 27987 20:55:49,512 --> 20:55:54,160 a lowercase c and if we do this you see 27988 20:55:52,320 --> 20:55:56,400 that we have a squiggly line the 27989 20:55:54,160 --> 20:56:00,400 compiler is not going to generate an 27990 20:55:56,399 --> 20:56:02,000 empty non-parameter taking constructor 27991 20:56:00,399 --> 20:56:04,319 because it sees that we have a 27992 20:56:02,000 --> 20:56:07,191 constructor here this is how c plus plus 27993 20:56:04,320 --> 20:56:09,360 compilers work if they see that you have 27994 20:56:07,191 --> 20:56:11,440 any constructor in your class they're 27995 20:56:09,360 --> 20:56:13,360 not going to generate a default empty 27996 20:56:11,440 --> 20:56:15,279 constructor and if we try to build we're 27997 20:56:13,360 --> 20:56:17,279 going to get an arrow that says 27998 20:56:17,279 --> 20:56:21,440 we don't have a constructor that can 27999 20:56:19,279 --> 20:56:23,919 basically construct an object from the 28000 20:56:21,440 --> 20:56:26,479 line here it's going to say no matching 28001 20:56:23,919 --> 20:56:28,240 function call for cylinder cylinder you 28002 20:56:26,479 --> 20:56:30,872 see here it is trying to call a 28003 20:56:28,240 --> 20:56:32,639 constructor that doesn't take any 28004 20:56:30,872 --> 20:56:35,600 parameter but we have no such 28005 20:56:32,639 --> 20:56:37,831 constructor in our code we could go in 28006 20:56:37,831 --> 20:56:43,279 and for example say cylinder and put an 28007 20:56:40,872 --> 20:56:46,000 empty parameter list and put an empty 28008 20:56:43,279 --> 20:56:47,919 body now this line here is going to work 28009 20:56:46,000 --> 20:56:50,960 because we have a default empty 28010 20:56:47,919 --> 20:56:53,191 constructor and if we weld you see that 28011 20:56:50,960 --> 20:56:56,552 the build is going to be good okay we 28012 20:56:53,191 --> 20:56:59,679 are building fine but having to type all 28013 20:56:56,551 --> 20:57:01,039 this if what you really want is an empty 28014 20:57:01,039 --> 20:57:06,239 is too much type in and in c plus plus 28015 20:57:03,919 --> 20:57:08,639 we have a syntax we can use to let the 28016 20:57:06,240 --> 20:57:10,720 compiler generate this kind of 28017 20:57:08,639 --> 20:57:13,759 constructor for us and the way we do 28018 20:57:10,720 --> 20:57:15,759 that is say cylinder and we say the name 28019 20:57:13,759 --> 20:57:18,479 of the constructor we say equals and we 28020 20:57:15,759 --> 20:57:20,319 say default if we save this the compiler 28021 20:57:18,479 --> 20:57:22,399 is going to generate this constructor 28022 20:57:20,320 --> 20:57:25,440 for us and the line here is going to 28023 20:57:22,399 --> 20:57:28,319 work so let's bring this up and build 28024 20:57:25,440 --> 20:57:29,760 again we're going to build with gcc 28025 20:57:28,320 --> 20:57:32,872 you're going to see that the build is 28026 20:57:29,759 --> 20:57:35,439 good and we can bring up the terminal 28027 20:57:32,872 --> 20:57:37,279 and clear and run rooster we're not 28028 20:57:35,440 --> 20:57:40,720 going to see anything because we're not 28029 20:57:37,279 --> 20:57:42,319 printing the volume of our cylinder here 28030 20:57:40,720 --> 20:57:44,720 but we can do that let's call this 28031 20:57:42,320 --> 20:57:46,800 cylinder one to be consistent with what 28032 20:57:44,720 --> 20:57:49,040 we did in the last few lectures and 28033 20:57:46,800 --> 20:57:51,120 we're going to say hddc out and print 28034 20:57:49,039 --> 20:57:54,319 the volume of this cylinder we're going 28035 20:57:51,119 --> 20:57:56,799 to say volume and call our volume method 28036 20:57:54,320 --> 20:57:58,960 and now that we have this we can weld 28037 20:57:56,800 --> 20:58:01,760 we're going to use gcc to do that the 28038 20:57:58,960 --> 20:58:03,512 build is good we can go down and clear 28039 20:58:01,759 --> 20:58:06,000 and run rooster we're going to see that 28040 20:58:03,512 --> 20:58:08,000 we see our volume here and this is 28041 20:58:06,000 --> 20:58:10,399 really all i wanted to share with you in 28042 20:58:08,000 --> 20:58:13,279 this lecture that you can use this 28043 20:58:10,399 --> 20:58:16,399 default syntax here to set up an empty 28044 20:58:13,279 --> 20:58:18,800 default constructor for your classes and 28045 20:58:18,800 --> 20:58:25,279 is that your constructors have to be 28046 20:58:22,080 --> 20:58:27,600 public to be able to be called like this 28047 20:58:25,279 --> 20:58:29,831 and to show you that let's go up and 28048 20:58:27,600 --> 20:58:31,600 actually change this to private and show 28049 20:58:29,831 --> 20:58:33,511 you what happens the moment we do this 28050 20:58:31,600 --> 20:58:36,320 we're going to get a squiggly line in 28051 20:58:33,512 --> 20:58:39,040 the main function here and if we try to 28052 20:58:36,320 --> 20:58:41,192 build but before we build let's show you 28053 20:58:39,039 --> 20:58:44,079 what visual studio code is saying it's 28054 20:58:41,191 --> 20:58:46,159 going to say cylinder cylinder declared 28055 20:58:44,080 --> 20:58:49,119 is inaccessible so you see the 28056 20:58:46,160 --> 20:58:51,120 constructor which is declared inside of 28057 20:58:51,119 --> 20:58:56,720 it accessible from the outside when we 28058 20:58:54,000 --> 20:59:00,639 try to call it to build our cylinder 28059 20:58:56,720 --> 20:59:02,720 objects so for constructors to be usable 28060 20:59:00,639 --> 20:59:05,360 on the outside here and both your 28061 20:59:02,720 --> 20:59:08,399 objects they have to be public let's 28062 20:59:05,360 --> 20:59:09,831 build and see what gcc says about this 28063 20:59:08,399 --> 20:59:12,479 we basically going to have the same 28064 20:59:09,831 --> 20:59:14,959 error the constructor is private within 28065 20:59:12,479 --> 20:59:16,872 this context so we can't really call it 28066 20:59:14,960 --> 20:59:19,512 so if we want our code to build we need 28067 20:59:16,872 --> 20:59:20,639 to make our constructors public and this 28068 20:59:20,639 --> 20:59:25,279 you need to know let's build again and 28069 20:59:22,872 --> 20:59:27,440 see that the cold is now building the 28070 20:59:25,279 --> 20:59:29,279 world is good and this is really all i 28071 20:59:27,440 --> 20:59:31,360 had to share in this lecture we are 28072 20:59:29,279 --> 20:59:32,959 going to stop here in this one in the 28073 20:59:31,360 --> 20:59:36,639 next one we're going to see how we can 28074 20:59:32,960 --> 20:59:39,040 use setters and getters to modify things 28075 20:59:39,039 --> 20:59:42,239 go ahead and finish up here and meet me 28076 20:59:42,240 --> 20:59:46,480 in this lecture we're going to learn 28077 20:59:43,679 --> 20:59:49,511 about setters and gators and these are 28078 20:59:46,479 --> 20:59:52,551 methods in our class that we can use to 28079 20:59:49,512 --> 20:59:55,119 read member variables or modify data in 28080 20:59:52,551 --> 20:59:58,080 our member variables let's look at a 28081 20:59:55,119 --> 21:00:00,959 simple example and by the way i am sorry 28082 20:59:58,080 --> 21:00:03,600 for my bad pronunciation of cylinder 28083 21:00:00,960 --> 21:00:05,832 here i have said it was cylinder and 28084 21:00:03,600 --> 21:00:08,639 after some careful thought i checked 28085 21:00:05,831 --> 21:00:11,119 online for the pronunciation on this and 28086 21:00:08,639 --> 21:00:13,919 i found that it was cylinder sorry for 28087 21:00:11,119 --> 21:00:16,159 this if it was bothering you i apologize 28088 21:00:13,919 --> 21:00:18,551 for the bad pronunciation and i am going 28089 21:00:16,160 --> 21:00:21,600 to do this right starting from now okay 28090 21:00:18,551 --> 21:00:23,919 here we have our cylinder class we have 28091 21:00:21,600 --> 21:00:26,800 a few member variables and we have the 28092 21:00:23,919 --> 21:00:28,872 base radius we have the height and now 28093 21:00:26,800 --> 21:00:31,279 we want to go through some public 28094 21:00:28,872 --> 21:00:33,192 functions to do operations on these 28095 21:00:31,279 --> 21:00:35,759 member variables because now that they 28096 21:00:33,191 --> 21:00:38,479 are private we can't really modify them 28097 21:00:35,759 --> 21:00:40,959 or manipulate them from the outside and 28098 21:00:38,479 --> 21:00:43,119 we really need to do things with them so 28099 21:00:40,960 --> 21:00:45,360 if we want to read things from them we 28100 21:00:43,119 --> 21:00:47,360 will use the getter functions 28101 21:00:45,360 --> 21:00:50,240 and if you look here this is a simple 28102 21:00:47,360 --> 21:00:52,960 function the only special thing about it 28103 21:00:50,240 --> 21:00:55,040 is that it leaves inside the class it 28104 21:00:52,960 --> 21:00:56,872 has a return value it has a function 28105 21:00:55,039 --> 21:00:59,439 name it has a parameter list and then 28106 21:00:56,872 --> 21:01:01,512 the body we're just going to return the 28107 21:00:59,440 --> 21:01:04,240 member variable of interest because this 28108 21:01:01,512 --> 21:01:06,551 is a getter we use that to get stuff 28109 21:01:04,240 --> 21:01:08,400 from the class this is the meaning here 28110 21:01:06,551 --> 21:01:10,399 here we have another getter function 28111 21:01:08,399 --> 21:01:13,439 which is going to return the height and 28112 21:01:10,399 --> 21:01:16,871 notice that these functions live in the 28113 21:01:13,440 --> 21:01:19,040 public scope of our class this is key 28114 21:01:16,872 --> 21:01:22,479 because we want to be able to go through 28115 21:01:19,039 --> 21:01:25,039 them to modify and do things with our 28116 21:01:22,479 --> 21:01:27,919 member variables but they will be used 28117 21:01:25,039 --> 21:01:29,831 from the outside so they must be public 28118 21:01:27,919 --> 21:01:33,119 otherwise they want to be accessible 28119 21:01:29,831 --> 21:01:35,191 from the outside we also have a pair of 28120 21:01:33,119 --> 21:01:38,319 setup functions they are going to be 28121 21:01:35,191 --> 21:01:40,720 modifying our member variables so set 28122 21:01:38,320 --> 21:01:42,872 base radius is going to take a parameter 28123 21:01:40,720 --> 21:01:44,960 that it's going to use to set a base 28124 21:01:42,872 --> 21:01:46,639 radius set height is going to do the 28125 21:01:44,960 --> 21:01:49,600 same it's going to take a parameter and 28126 21:01:46,639 --> 21:01:52,240 it's going to use that to assign a value 28127 21:01:49,600 --> 21:01:55,040 to our height member variable here and 28128 21:01:52,240 --> 21:01:56,160 because these functions are members of 28129 21:01:56,160 --> 21:02:02,552 they have access to our member variables 28130 21:01:59,831 --> 21:02:04,871 regardless of whether they are public or 28131 21:02:02,551 --> 21:02:06,399 private so this is going to work we're 28132 21:02:04,872 --> 21:02:08,400 going to go through them from the 28133 21:02:06,399 --> 21:02:11,439 outside and they are going to do the 28134 21:02:08,399 --> 21:02:14,159 dairy work for us and do things using 28135 21:02:11,440 --> 21:02:15,760 our member variables to which we don't 28136 21:02:15,759 --> 21:02:20,080 from the outside this is the meaning 28137 21:02:17,600 --> 21:02:22,080 here now that you have an idea about 28138 21:02:20,080 --> 21:02:23,759 this we're going to head over to visual 28139 21:02:22,080 --> 21:02:26,720 studio code and play with us a little 28140 21:02:23,759 --> 21:02:28,959 more okay here we are in our working 28141 21:02:26,720 --> 21:02:30,800 folder the current project is setters 28142 21:02:28,960 --> 21:02:33,120 and getters we're going to grab our 28143 21:02:30,800 --> 21:02:34,320 template files pretty quick and put them 28144 21:02:34,320 --> 21:02:38,872 setters and getters it is let's grab the 28145 21:02:36,960 --> 21:02:40,800 folder and we're going to open this in 28146 21:02:38,872 --> 21:02:42,872 video studio code by dragging and 28147 21:02:40,800 --> 21:02:45,760 dropping here this is going to open our 28148 21:02:42,872 --> 21:02:47,680 folder we're going to open our main cpp 28149 21:02:45,759 --> 21:02:49,831 file and we're going to clean it up a 28150 21:02:47,679 --> 21:02:53,039 little bit we are going to grab the 28151 21:02:49,831 --> 21:02:55,679 cylinder class from our previous lecture 28152 21:02:53,039 --> 21:02:57,919 and use that as a starting point here 28153 21:02:55,679 --> 21:03:00,551 and if you look we have a pair of 28154 21:02:57,919 --> 21:03:02,160 constructors here we have the volume 28155 21:03:00,551 --> 21:03:04,959 function which is going to give us the 28156 21:03:02,160 --> 21:03:07,360 volume of our cylinder here and we have 28157 21:03:04,960 --> 21:03:10,240 our member variables now what we want to 28158 21:03:07,360 --> 21:03:13,119 do is to be able to do things with these 28159 21:03:10,240 --> 21:03:15,192 member variables from the outside in the 28160 21:03:13,119 --> 21:03:17,512 main function here and we're going to go 28161 21:03:15,191 --> 21:03:20,479 through setter and getter methods which 28162 21:03:17,512 --> 21:03:22,872 must be public so let's do that so we're 28163 21:03:20,479 --> 21:03:25,279 going to set up our getter first so 28164 21:03:22,872 --> 21:03:27,279 we're going to say double get base 28165 21:03:25,279 --> 21:03:29,759 radius it's going to return double it's 28166 21:03:27,279 --> 21:03:32,160 not going to take anything and and in 28167 21:03:29,759 --> 21:03:34,399 the body we're just going to return our 28168 21:03:32,160 --> 21:03:36,320 radius let's return that okay so we're 28169 21:03:34,399 --> 21:03:38,479 going to set up another getter which is 28170 21:03:36,320 --> 21:03:40,552 going to give us the height and it's not 28171 21:03:38,479 --> 21:03:43,512 going to take anything and it's going to 28172 21:03:40,551 --> 21:03:46,159 just return our height member variable 28173 21:03:43,512 --> 21:03:49,040 now these are our getters they are going 28174 21:03:46,160 --> 21:03:50,960 to be getting things from the class 28175 21:03:49,039 --> 21:03:52,719 object we're going to set up other 28176 21:03:50,960 --> 21:03:54,960 functions that are going to be our 28177 21:03:52,720 --> 21:03:57,119 setters they're not going to return 28178 21:03:54,960 --> 21:03:59,120 anything and we're going to say set base 28179 21:03:57,119 --> 21:04:01,360 radius and it's going to take a double 28180 21:03:59,119 --> 21:04:03,279 parameter in and we're going to use that 28181 21:04:01,360 --> 21:04:05,440 to set our base radius so we're going to 28182 21:04:03,279 --> 21:04:07,360 say base radius and we're going to say 28183 21:04:05,440 --> 21:04:09,832 equals red parameter here and we're 28184 21:04:07,360 --> 21:04:11,680 going to do the same for our height so 28185 21:04:09,831 --> 21:04:13,511 it's not going to return anything the 28186 21:04:11,679 --> 21:04:15,279 function here and we're going to say set 28187 21:04:13,512 --> 21:04:17,832 height and we're going to take our 28188 21:04:15,279 --> 21:04:20,240 parameter n so i'm going to say double 28189 21:04:17,831 --> 21:04:21,039 height param and we're going to use that 28190 21:04:22,080 --> 21:04:27,279 stuff to our member variable we're going 28191 21:04:24,639 --> 21:04:30,240 to say hi and we're going to say height 28192 21:04:27,279 --> 21:04:33,191 param okay so this is our thing here now 28193 21:04:30,240 --> 21:04:35,760 we have two functions to get stuff from 28194 21:04:33,191 --> 21:04:38,639 our class and we have two functions to 28195 21:04:35,759 --> 21:04:41,039 set data to our member variables here 28196 21:04:38,639 --> 21:04:44,240 and again remember that we can't have 28197 21:04:41,039 --> 21:04:46,159 direct access to these member variables 28198 21:04:44,240 --> 21:04:48,000 on the outside of the class and if we 28199 21:04:46,160 --> 21:04:50,552 want to do that we're going to be able 28200 21:04:48,000 --> 21:04:52,872 to do that through our setter and getter 28201 21:04:50,551 --> 21:04:54,551 functions now for these functions 28202 21:04:52,872 --> 21:04:57,279 setters and getters to be accessible 28203 21:04:54,551 --> 21:04:59,759 from the outside they needed to leave in 28204 21:04:57,279 --> 21:05:01,679 the public section of our class you see 28205 21:04:59,759 --> 21:05:05,191 this public keyword here is going to 28206 21:05:01,679 --> 21:05:07,919 apply to anything after it until we 28207 21:05:05,191 --> 21:05:10,080 change this to private software so the 28208 21:05:07,919 --> 21:05:11,831 public scope is basically going from the 28209 21:05:11,831 --> 21:05:17,191 all the way to our set height function 28210 21:05:14,720 --> 21:05:19,279 and once we put this private keyword 28211 21:05:17,191 --> 21:05:21,831 here everything after that is going to 28212 21:05:19,279 --> 21:05:23,831 be private this is how these things work 28213 21:05:21,831 --> 21:05:25,919 so let's try and use this in main we're 28214 21:05:23,831 --> 21:05:28,639 going to set up a cylinder object 28215 21:05:25,919 --> 21:05:31,360 cylinder one and let's put in for 28216 21:05:28,639 --> 21:05:33,440 example two and three why not and now if 28217 21:05:31,360 --> 21:05:36,551 we want to have access to the base 28218 21:05:33,440 --> 21:05:39,440 radius remember if we tried to access 28219 21:05:36,551 --> 21:05:41,759 that directly let's say that if we try 28220 21:05:39,440 --> 21:05:44,080 to do something like cylinder one and 28221 21:05:41,759 --> 21:05:45,679 say base radius you're going to see that 28222 21:05:44,080 --> 21:05:47,191 we're going to have a squiggly line 28223 21:05:45,679 --> 21:05:48,871 meaning that there's something wrong 28224 21:05:47,191 --> 21:05:49,919 with our call here let's bring up a 28225 21:05:49,919 --> 21:05:53,440 and see what visual studio code thinks 28226 21:05:51,759 --> 21:05:55,439 about this membership base radius 28227 21:05:53,440 --> 21:05:57,440 declared this inaccessible here if we 28228 21:05:55,440 --> 21:05:59,600 try to weld we probably going to get a 28229 21:05:57,440 --> 21:06:02,320 compiler error that says that base 28230 21:05:59,600 --> 21:06:04,480 radius is private in this scope and 28231 21:06:02,320 --> 21:06:07,040 that's what we have here declare the 28232 21:06:04,479 --> 21:06:09,440 private here so we can't access this 28233 21:06:07,039 --> 21:06:12,079 from the main function here but now that 28234 21:06:09,440 --> 21:06:14,551 we have our getter functions we can go 28235 21:06:12,080 --> 21:06:16,872 through them and have access to 28236 21:06:14,551 --> 21:06:19,759 private member variables and notice that 28237 21:06:16,872 --> 21:06:21,680 because we are returning by value here 28238 21:06:19,759 --> 21:06:24,399 what return is going to be a copy you 28239 21:06:21,679 --> 21:06:26,399 really need to keep an eye for whether 28240 21:06:24,399 --> 21:06:29,599 what you are manipulating is the 28241 21:06:26,399 --> 21:06:31,511 original stuff or a copy so here we're 28242 21:06:29,600 --> 21:06:33,920 going to be returning by value and we're 28243 21:06:31,512 --> 21:06:36,800 going to return a copy but this is still 28244 21:06:33,919 --> 21:06:39,119 useful in some cases so here we can use 28245 21:06:36,800 --> 21:06:42,240 allocator let's take out this here and 28246 21:06:39,119 --> 21:06:45,279 say get base radius it is a method or a 28247 21:06:42,240 --> 21:06:47,832 function so we need to put our function 28248 21:06:45,279 --> 21:06:50,000 call here and now if we try to build 28249 21:06:47,831 --> 21:06:52,959 let's pass this through gcc and see what 28250 21:06:50,000 --> 21:06:55,512 we get you see the build is good we can 28251 21:06:52,960 --> 21:06:57,040 run this program let's clear and run 28252 21:06:55,512 --> 21:06:59,512 rooster you're going to see that the 28253 21:06:57,039 --> 21:07:01,919 base radius is two now now that we have 28254 21:06:59,512 --> 21:07:05,440 this sense we can actually modify stuff 28255 21:07:01,919 --> 21:07:07,831 about our cylinder here let's put in 10 28256 21:07:05,440 --> 21:07:09,832 and 10 just to be able to follow this 28257 21:07:07,831 --> 21:07:12,639 easily and we're going to print the 28258 21:07:09,831 --> 21:07:14,959 volume let's say volume and we're going 28259 21:07:12,639 --> 21:07:16,720 to say volume here we're not going to 28260 21:07:14,960 --> 21:07:19,040 use the getter here i'm going to say 28261 21:07:16,720 --> 21:07:21,360 volume this is going to print our volume 28262 21:07:19,039 --> 21:07:24,319 but we're going to modify our member 28263 21:07:21,360 --> 21:07:26,080 variables remember this is an object of 28264 21:07:26,080 --> 21:07:31,040 so we're going to say cylinder 1 set 28265 21:07:28,800 --> 21:07:33,680 base radius we can do that and we can 28266 21:07:31,039 --> 21:07:36,871 change this to 100 for example and we 28267 21:07:33,679 --> 21:07:39,359 can change the height that height we can 28268 21:07:36,872 --> 21:07:41,832 use this member function because we have 28269 21:07:39,360 --> 21:07:44,320 it now and we're going to set this to 10 28270 21:07:41,831 --> 21:07:46,159 why not now if we print the volume 28271 21:07:44,320 --> 21:07:49,440 you're going to see that the volume has 28272 21:07:46,160 --> 21:07:50,480 changed because we are going through our 28273 21:07:50,479 --> 21:07:54,639 setter and getters methods so the 28274 21:07:52,720 --> 21:07:56,960 cylinder is now going to be using this 28275 21:07:54,639 --> 21:07:59,039 data and for the first case here the 28276 21:07:56,960 --> 21:08:01,440 cylinder is going to be using the data 28277 21:07:59,039 --> 21:08:03,759 we used in our constructor here let's 28278 21:08:01,440 --> 21:08:06,479 both and run this application the build 28279 21:08:03,759 --> 21:08:07,831 is good we can clear and run rooster now 28280 21:08:06,479 --> 21:08:10,399 for the first time we're going to see 28281 21:08:07,831 --> 21:08:13,039 that our volume is this number and for 28282 21:08:10,399 --> 21:08:16,159 the second time our volume is going to 28283 21:08:13,039 --> 21:08:19,360 be multiplied by 10 because we changed 28284 21:08:16,160 --> 21:08:21,279 the base radius to 100 and if you put 28285 21:08:19,360 --> 21:08:23,360 these numbers in you're going to see 28286 21:08:21,279 --> 21:08:26,319 that they make sense we are able to 28287 21:08:23,360 --> 21:08:28,639 modify our member variables through our 28288 21:08:26,320 --> 21:08:30,960 setter and getter functions and again 28289 21:08:28,639 --> 21:08:33,440 for this setter and getter functions to 28290 21:08:30,960 --> 21:08:35,360 work they have to be in the public scope 28291 21:08:33,440 --> 21:08:36,872 of your class because if you make them 28292 21:08:35,360 --> 21:08:38,960 private they're not going to be 28293 21:08:36,872 --> 21:08:40,720 accessible from the outside and you're 28294 21:08:38,960 --> 21:08:43,040 going to get a compiler error let's try 28295 21:08:40,720 --> 21:08:45,191 and do that we are learning so we're not 28296 21:08:43,039 --> 21:08:47,439 afraid to break things a little bit so 28297 21:08:45,191 --> 21:08:50,080 let's make them private we can do that 28298 21:08:47,440 --> 21:08:51,832 and bring this a little bit to the left 28299 21:08:50,080 --> 21:08:54,400 and at the moment we do that we should 28300 21:08:51,831 --> 21:08:57,119 have a few squiggly lines in our main 28301 21:08:54,399 --> 21:08:59,511 function and we do expect this because 28302 21:08:57,119 --> 21:09:01,512 they are private now so we have no 28303 21:08:59,512 --> 21:09:04,160 business accessing them from the main 28304 21:09:01,512 --> 21:09:06,320 function like this they are meant to be 28305 21:09:04,160 --> 21:09:08,400 used from the inside of the class if 28306 21:09:06,320 --> 21:09:10,872 they are private like this and if you 28307 21:09:08,399 --> 21:09:13,191 try to use them from the outside you're 28308 21:09:10,872 --> 21:09:15,040 going to get a compiler error let's pass 28309 21:09:13,191 --> 21:09:17,360 this through gcc to really see the 28310 21:09:15,039 --> 21:09:20,079 compiler error and we're going to get 28311 21:09:17,360 --> 21:09:22,720 set height is private from the context 28312 21:09:20,080 --> 21:09:24,479 of the main function and if we go up i 28313 21:09:22,720 --> 21:09:28,080 think we're going to see the same thing 28314 21:09:24,479 --> 21:09:29,919 for a set base radius and that's here 28315 21:09:28,080 --> 21:09:32,080 this is really all we set out to do in 28316 21:09:29,919 --> 21:09:34,319 this lecture showing you how you can use 28317 21:09:32,080 --> 21:09:36,960 setters and getters to manipulate your 28318 21:09:34,320 --> 21:09:39,360 member variables from the outside 28319 21:09:36,960 --> 21:09:41,120 and i hope you found this useful we are 28320 21:09:39,360 --> 21:09:42,720 going to stop here in this lecture in 28321 21:09:41,119 --> 21:09:45,831 the next one we're going to show you how 28322 21:09:42,720 --> 21:09:49,191 you can split your class across multiple 28323 21:09:45,831 --> 21:09:50,959 files but before i let you go let's fix 28324 21:09:49,191 --> 21:09:53,039 this so that we don't have a compiler 28325 21:09:50,960 --> 21:09:55,680 error here so we're going to remove this 28326 21:09:53,039 --> 21:09:57,599 private keyword we have put here and 28327 21:09:55,679 --> 21:10:00,159 we're going to build again to make sure 28328 21:09:57,600 --> 21:10:03,040 the world is good let's use gcc for that 28329 21:10:00,160 --> 21:10:05,192 the world is good and i will see you in 28330 21:10:03,039 --> 21:10:07,919 the next lecture go ahead and finish up 28331 21:10:05,191 --> 21:10:10,080 here and meet me there in this lecture i 28332 21:10:07,919 --> 21:10:13,119 am going to show you how we can split 28333 21:10:10,080 --> 21:10:16,080 our class information across multiple 28334 21:10:13,119 --> 21:10:18,479 files so far what we have been doing is 28335 21:10:16,080 --> 21:10:21,919 really cramping everything in the main 28336 21:10:18,479 --> 21:10:24,800 function we had our constant declaration 28337 21:10:21,919 --> 21:10:27,039 we had our cylinder class definition and 28338 21:10:24,800 --> 21:10:30,240 we had our main function to use these 28339 21:10:27,039 --> 21:10:32,239 things all cramped into the main cpp 28340 21:10:30,240 --> 21:10:34,240 file now we're going to see how we can 28341 21:10:32,240 --> 21:10:38,000 do things better the first thing we can 28342 21:10:34,240 --> 21:10:40,800 do is move our constant in another file 28343 21:10:38,000 --> 21:10:43,360 because if you think about it users of 28344 21:10:40,800 --> 21:10:46,400 your class in the main function don't 28345 21:10:43,360 --> 21:10:49,191 care about this constant pi because it 28346 21:10:46,399 --> 21:10:51,599 really is an implementation detail 28347 21:10:49,191 --> 21:10:53,599 that should leave somewhere else or even 28348 21:10:51,600 --> 21:10:55,920 inside the cylinder class but we're 28349 21:10:53,600 --> 21:10:57,920 going to put this in a separate file 28350 21:10:55,919 --> 21:11:00,639 because that's going to give us room to 28351 21:10:57,919 --> 21:11:03,360 put as a constant if we need to do that 28352 21:11:00,639 --> 21:11:05,600 so if we split things up like this 28353 21:11:03,360 --> 21:11:08,320 when we want to use the pi variable we 28354 21:11:05,600 --> 21:11:10,400 will need to include the constants file 28355 21:11:08,320 --> 21:11:12,872 because that's going to give us access 28356 21:11:10,399 --> 21:11:15,039 to it but we don't have to maintain it 28357 21:11:12,872 --> 21:11:17,120 in the main function so this is a little 28358 21:11:15,039 --> 21:11:19,831 better but another thing we can do is 28359 21:11:17,119 --> 21:11:22,639 remove the cylinder in another separate 28360 21:11:19,831 --> 21:11:25,360 file and do something like this so now 28361 21:11:22,639 --> 21:11:27,600 you see that our cylinder declaration 28362 21:11:25,360 --> 21:11:29,831 and definition has moved in a file 28363 21:11:32,240 --> 21:11:35,512 living in another file called 28364 21:11:35,512 --> 21:11:40,639 and if we want to use our cylinder class 28365 21:11:37,831 --> 21:11:43,119 like we have been using it all along we 28366 21:11:40,639 --> 21:11:45,279 will need to include the header that 28367 21:11:43,119 --> 21:11:47,599 brings it in and this is how we're going 28368 21:11:45,279 --> 21:11:50,240 to do it and we will be able to use it 28369 21:11:47,600 --> 21:11:52,872 in the main function if we set things up 28370 21:11:50,240 --> 21:11:55,512 like this but we can go even further we 28371 21:11:52,872 --> 21:11:58,240 can split our class declaration and 28372 21:11:55,512 --> 21:11:59,600 definition in two separate files what 28373 21:11:58,240 --> 21:12:01,512 we're going to have in the header is 28374 21:11:59,600 --> 21:12:02,400 going to be really a preview of the 28375 21:12:02,399 --> 21:12:05,959 and in the cpp file which is going to be 28376 21:12:05,960 --> 21:12:11,440 cylinder.cpp we're going to have the 28377 21:12:08,000 --> 21:12:13,191 implementation details of our class and 28378 21:12:11,440 --> 21:12:15,680 again this is going to make things a 28379 21:12:13,191 --> 21:12:17,599 little easier to manage because it's 28380 21:12:15,679 --> 21:12:20,719 really not good to have everything 28381 21:12:17,600 --> 21:12:23,512 cramped into your main cpp file it 28382 21:12:20,720 --> 21:12:26,399 should really have high level details 28383 21:12:23,512 --> 21:12:28,551 about how your application works and all 28384 21:12:26,399 --> 21:12:31,360 the low level details should be split 28385 21:12:28,551 --> 21:12:33,831 into different files this is good design 28386 21:12:31,360 --> 21:12:36,080 now that you have an idea about this 28387 21:12:33,831 --> 21:12:37,759 let's head over to visual studio code 28388 21:12:36,080 --> 21:12:40,320 and show you how you can do this okay 28389 21:12:37,759 --> 21:12:43,119 here we are in our working folder the 28390 21:12:40,320 --> 21:12:46,400 current project is class across multiple 28391 21:12:43,119 --> 21:12:48,639 files we're going to grab our template 28392 21:12:46,399 --> 21:12:50,639 files and put them in place and we're 28393 21:12:48,639 --> 21:12:52,639 going to open this in visual studio code 28394 21:12:50,639 --> 21:12:55,440 so that we can really play with us this 28395 21:12:52,639 --> 21:12:58,399 is going to open our folder we can open 28396 21:12:55,440 --> 21:13:00,479 the main cpp file we are going to clean 28397 21:12:58,399 --> 21:13:03,039 it up a little bit and we are going to 28398 21:13:00,479 --> 21:13:05,440 grab our class definition and put that 28399 21:13:03,039 --> 21:13:07,439 on top here so this is our cylinder 28400 21:13:05,440 --> 21:13:10,000 class we have the constructors we have 28401 21:13:07,440 --> 21:13:12,080 the functions the volume function here 28402 21:13:10,000 --> 21:13:14,551 and we have our setters and getters 28403 21:13:12,080 --> 21:13:16,720 inside down we have our member variables 28404 21:13:14,551 --> 21:13:19,440 which happen to be private to this class 28405 21:13:16,720 --> 21:13:20,399 here now what we want to do is to 28406 21:13:20,399 --> 21:13:26,000 our code better and the first thing we 28407 21:13:22,872 --> 21:13:28,800 need to do is to move this constant from 28408 21:13:26,000 --> 21:13:31,831 the main file here because it's really 28409 21:13:28,800 --> 21:13:33,832 not that useful at this point here users 28410 21:13:31,831 --> 21:13:36,080 of your classes won't really care about 28411 21:13:33,831 --> 21:13:38,319 what pi is because it is an 28412 21:13:36,080 --> 21:13:40,320 implementation detail to the cylinder 28413 21:13:38,320 --> 21:13:42,800 class because we are using that to 28414 21:13:40,320 --> 21:13:45,192 compute our volume so this constant 28415 21:13:42,800 --> 21:13:46,960 really has no business being in the main 28416 21:13:45,191 --> 21:13:48,551 file we're going to move this in a 28417 21:13:46,960 --> 21:13:50,240 separate file we're going to set up a 28418 21:13:48,551 --> 21:13:52,959 new file we're going to come to the left 28419 21:13:50,240 --> 21:13:55,040 here click on the plus icon here that 28420 21:13:52,960 --> 21:13:57,720 says new file we're going to say 28421 21:13:55,039 --> 21:13:59,831 constants and we're going to call this 28422 21:13:57,720 --> 21:14:02,800 constants.h because it's going to be a 28423 21:13:59,831 --> 21:14:03,831 header file and we can just come from 28424 21:14:03,831 --> 21:14:08,239 main cpp file we're going to copy this 28425 21:14:06,320 --> 21:14:11,120 we're going to cut this actually and 28426 21:14:08,240 --> 21:14:13,832 we're going to put this in our separate 28427 21:14:13,831 --> 21:14:18,159 after we do this we're going to come in 28428 21:14:15,759 --> 21:14:20,799 our main cpp file and we're going to 28429 21:14:18,160 --> 21:14:24,160 include that header file so let's do 28430 21:14:20,800 --> 21:14:25,600 that we're going to include constants.h 28431 21:14:24,160 --> 21:14:28,080 and the moment we do that you're going 28432 21:14:25,600 --> 21:14:30,720 to see now visual studio code knows 28433 21:14:28,080 --> 21:14:32,800 where this constant is but we don't have 28434 21:14:30,720 --> 21:14:34,720 to deal with that directly here another 28435 21:14:32,800 --> 21:14:37,279 benefit of doing things like this is 28436 21:14:34,720 --> 21:14:40,399 that we can go in this header file and 28437 21:14:37,279 --> 21:14:42,399 declare thousands of other constants and 28438 21:14:40,399 --> 21:14:46,239 we can have access to them in the main 28439 21:14:42,399 --> 21:14:48,720 function by just using this single line 28440 21:14:46,240 --> 21:14:51,040 that includes our file here so this is 28441 21:14:48,720 --> 21:14:52,479 good design if we build our application 28442 21:14:51,039 --> 21:14:54,799 you're going to see that it is going to 28443 21:14:52,479 --> 21:14:57,679 work let's try and set up a cylinder 28444 21:14:54,800 --> 21:15:00,720 object we're going to say cylinder 1 and 28445 21:14:57,679 --> 21:15:02,551 we're going to put 10 and 10 n as our 28446 21:15:00,720 --> 21:15:04,800 parameters and we're going to print the 28447 21:15:02,551 --> 21:15:07,191 volume of this little guy so this is 28448 21:15:04,800 --> 21:15:08,800 easy to do now we're going to say volume 28449 21:15:07,191 --> 21:15:10,799 and we're going to print that out 28450 21:15:08,800 --> 21:15:13,120 cylinder one and we're going to call the 28451 21:15:10,800 --> 21:15:15,680 volume method and this is going to print 28452 21:15:13,119 --> 21:15:19,440 our thumb and again i am going to be 28453 21:15:15,679 --> 21:15:21,439 using the terms functions and methods 28454 21:15:19,440 --> 21:15:23,760 interchangeably in this course and what 28455 21:15:21,440 --> 21:15:25,600 i mean in the context of the class 28456 21:15:23,759 --> 21:15:27,679 that's going to be the member functions 28457 21:15:25,600 --> 21:15:30,160 of the class so don't be confused by 28458 21:15:27,679 --> 21:15:32,959 this right here if we build and run this 28459 21:15:30,160 --> 21:15:35,680 application it should be working right 28460 21:15:32,960 --> 21:15:37,040 because we have everything we need for 28461 21:15:37,039 --> 21:15:41,279 let's see if the world was good the 28462 21:15:39,191 --> 21:15:43,512 build was good we can bring up a 28463 21:15:41,279 --> 21:15:45,831 terminal and actually run this let's 28464 21:15:43,512 --> 21:15:47,832 clear and run rooster this is going to 28465 21:15:45,831 --> 21:15:49,919 print our volume so the code is working 28466 21:15:47,831 --> 21:15:52,479 right the second thing we want to do is 28467 21:15:49,919 --> 21:15:54,551 to move this class all together in 28468 21:15:52,479 --> 21:15:56,800 another file because we don't really 28469 21:15:56,800 --> 21:16:01,760 here in the main function what we really 28470 21:15:59,119 --> 21:16:04,399 want is to use stuff and we don't care 28471 21:16:01,759 --> 21:16:07,439 about how those things are set up in the 28472 21:16:04,399 --> 21:16:09,599 same logic we used when we used std 28473 21:16:07,440 --> 21:16:12,400 strength we didn't really care about how 28474 21:16:09,600 --> 21:16:14,639 hdd strength is put together all we 28475 21:16:12,399 --> 21:16:18,079 really wanted was to include the class 28476 21:16:14,639 --> 21:16:20,160 that brings it in and use it in our code 28477 21:16:18,080 --> 21:16:22,800 we're going to be setting up the same 28478 21:16:20,160 --> 21:16:24,720 behavior for our cylinder class here and 28479 21:16:22,800 --> 21:16:27,279 it is going to be given in its own 28480 21:16:24,720 --> 21:16:28,872 separate file so let's set up another 28481 21:16:27,279 --> 21:16:31,512 header file we're going to call this 28482 21:16:28,872 --> 21:16:33,919 cylinder dot h and we're going to grab 28483 21:16:31,512 --> 21:16:36,720 our class definition all together so 28484 21:16:33,919 --> 21:16:39,279 we're going to go from the bottom here 28485 21:16:36,720 --> 21:16:41,360 and select everything that includes our 28486 21:16:39,279 --> 21:16:43,831 class we're going to cut this out we're 28487 21:16:41,360 --> 21:16:46,000 going to go in our cylinder header and 28488 21:16:43,831 --> 21:16:47,679 we're going to paste this in there and 28489 21:16:46,000 --> 21:16:50,160 we're going to come back to our main 28490 21:16:47,679 --> 21:16:52,639 file and include our cylinder class so 28491 21:16:50,160 --> 21:16:54,872 let's do that we're going to include and 28492 21:16:52,639 --> 21:16:56,960 we're going to say cylinder.h 28493 21:16:54,872 --> 21:16:59,440 and this is going to make things work 28494 21:16:56,960 --> 21:17:01,760 remember the preprocessor is going to 28495 21:16:59,440 --> 21:17:04,000 put these things on the preprocessor is 28496 21:17:01,759 --> 21:17:06,319 basically going to go in constants it's 28497 21:17:04,000 --> 21:17:08,872 going to take whatever is inside and 28498 21:17:06,320 --> 21:17:11,280 then it's going to paste that here and 28499 21:17:08,872 --> 21:17:13,192 it's going to take everything inside our 28500 21:17:11,279 --> 21:17:15,759 cylinder class and it's going to paste 28501 21:17:13,191 --> 21:17:18,159 that here so it's basically going to be 28502 21:17:15,759 --> 21:17:20,399 like we are declaring these things in 28503 21:17:18,160 --> 21:17:23,040 the main function but our code is going 28504 21:17:20,399 --> 21:17:25,360 to be better organized now if we try to 28505 21:17:23,039 --> 21:17:27,919 build this program it is going to world 28506 21:17:25,360 --> 21:17:30,399 exactly like it did because we have all 28507 21:17:27,919 --> 21:17:32,720 the information for its world let's 28508 21:17:30,399 --> 21:17:35,119 build world has finished successfully so 28509 21:17:32,720 --> 21:17:36,960 the world is good and we can run this 28510 21:17:35,119 --> 21:17:39,191 program and it's going to do what it was 28511 21:17:36,960 --> 21:17:41,680 doing all along we can clear and run 28512 21:17:39,191 --> 21:17:43,512 rooster don't worry about visual studio 28513 21:17:41,679 --> 21:17:45,919 code here i don't know what the problem 28514 21:17:43,512 --> 21:17:48,320 is with that it's just having ah it's 28515 21:17:48,320 --> 21:17:53,512 constant here because it's not included 28516 21:17:50,320 --> 21:17:56,720 in here so what we can do is say include 28517 21:17:53,512 --> 21:17:58,639 and include our constants header and if 28518 21:17:56,720 --> 21:18:01,360 we do this the problem here is going to 28519 21:17:58,639 --> 21:18:03,360 go away but we're going to have a 28520 21:18:01,360 --> 21:18:05,600 problem if we're trying to run this 28521 21:18:03,360 --> 21:18:08,080 program now because try to think about 28522 21:18:05,600 --> 21:18:09,920 it in the main function we are including 28523 21:18:11,679 --> 21:18:17,439 header of cylinder we are also including 28524 21:18:14,551 --> 21:18:19,679 this little guy here so if the processor 28525 21:18:19,679 --> 21:18:24,399 this and it's going to have this little 28526 21:18:21,919 --> 21:18:26,399 guy inside our main function and the 28527 21:18:24,399 --> 21:18:29,039 cylinder is also going to include this 28528 21:18:26,399 --> 21:18:31,439 because it's including this header so 28529 21:18:29,039 --> 21:18:34,000 when the preprocessor is done we're 28530 21:18:31,440 --> 21:18:36,240 going to have our constant here declared 28531 21:18:34,000 --> 21:18:38,320 twice and if we try to build this 28532 21:18:36,240 --> 21:18:40,720 program you can expect bad things to 28533 21:18:38,320 --> 21:18:43,512 happen because it's really not good to 28534 21:18:40,720 --> 21:18:47,040 have the same thing defined more than 28535 21:18:43,512 --> 21:18:48,720 once in your entire program and let's 28536 21:18:47,039 --> 21:18:50,239 try and build and show you this 28537 21:18:48,720 --> 21:18:51,040 hopefully we're going to get a compiler 28538 21:18:51,039 --> 21:18:55,439 and it's going to blow up in our face 28539 21:18:53,039 --> 21:18:58,639 it's going to say hero redefinition of 28540 21:18:55,440 --> 21:19:01,760 const double pi so what is going on here 28541 21:18:58,639 --> 21:19:04,319 we are including this thing twice once 28542 21:19:01,759 --> 21:19:06,639 here and at the second time it's coming 28543 21:19:04,320 --> 21:19:09,280 in directly from the cylinder class 28544 21:19:06,639 --> 21:19:11,919 because we are including that in this 28545 21:19:09,279 --> 21:19:14,000 location here and what the preprocessor 28546 21:19:11,919 --> 21:19:16,551 is basically going to do it's going to 28547 21:19:14,000 --> 21:19:19,831 copy the entire file here and it's going 28548 21:19:16,551 --> 21:19:22,080 to paste that in the place of cylinder.h 28549 21:19:19,831 --> 21:19:24,319 here and this constants header is 28550 21:19:22,080 --> 21:19:26,160 basically going to be included twice so 28551 21:19:24,320 --> 21:19:28,800 it's going to be redefined here that's 28552 21:19:26,160 --> 21:19:32,160 the problem we have one way we have to 28553 21:19:28,800 --> 21:19:34,800 solve this in c plus plus is by using 28554 21:19:32,160 --> 21:19:37,680 include chords and the way we do that we 28555 21:19:34,800 --> 21:19:39,440 come in our constants header and we say 28556 21:19:42,240 --> 21:19:48,000 if something is not defined yet and 28557 21:19:45,039 --> 21:19:49,919 let's say cylinder underscore h and 28558 21:19:49,919 --> 21:19:56,160 cylinder underscore h and we're going to 28559 21:19:52,479 --> 21:19:57,599 come down to the end and say and f this 28560 21:19:57,600 --> 21:20:03,600 preprocessor to only do something or 28561 21:20:00,960 --> 21:20:07,832 include the code we have within these 28562 21:20:03,600 --> 21:20:10,720 things if the condition on top here is 28563 21:20:07,831 --> 21:20:14,080 satisfied so for example the code inside 28564 21:20:10,720 --> 21:20:15,440 here is going to be put in our c plus 28565 21:20:15,440 --> 21:20:21,360 if the cylinder name here is not defined 28566 21:20:18,960 --> 21:20:24,400 in the eyes of the preprocessor so it's 28567 21:20:21,360 --> 21:20:26,872 going to say if cylinder underscore h is 28568 21:20:24,399 --> 21:20:29,360 not defined i am going to define that 28569 21:20:26,872 --> 21:20:31,120 and i am going to put this code here but 28570 21:20:29,360 --> 21:20:32,872 if it's already defined it's going to 28571 21:20:31,119 --> 21:20:34,871 skip all these things and it's not going 28572 21:20:32,872 --> 21:20:37,120 to include this okay so this is the 28573 21:20:34,872 --> 21:20:39,120 logic here and i noticed this is really 28574 21:20:37,119 --> 21:20:41,119 not a cylinder file so we're going to 28575 21:20:41,119 --> 21:20:46,000 underscore h let's say that and let's do 28576 21:20:44,000 --> 21:20:48,639 the same here and this is going to do 28577 21:20:46,000 --> 21:20:51,119 our thing and we're going to go in our 28578 21:20:48,639 --> 21:20:54,080 cylinder header and do the same thing 28579 21:20:51,119 --> 21:20:56,799 and put in our include guard it is 28580 21:20:54,080 --> 21:20:58,551 basically a guard that is going to guard 28581 21:20:58,551 --> 21:21:03,831 being included more than once 28582 21:21:01,440 --> 21:21:06,240 so let's do that in our cylinder header 28583 21:21:03,831 --> 21:21:08,871 so we're going to wrap this inside our 28584 21:21:06,240 --> 21:21:10,872 include guards we're going to say if n 28585 21:21:08,872 --> 21:21:12,551 def you see visual studio code is really 28586 21:21:10,872 --> 21:21:14,551 helping out here and we're going to put 28587 21:21:12,551 --> 21:21:16,872 in the name we're going to use for this 28588 21:21:14,551 --> 21:21:19,191 this is a cylinder class so i think it 28589 21:21:16,872 --> 21:21:21,512 makes sense to call this cylinder 28590 21:21:19,191 --> 21:21:24,799 underscore h and we're going to do the 28591 21:21:21,512 --> 21:21:27,360 same thing here define cylinder and 28592 21:21:24,800 --> 21:21:29,440 we're going to end f at the end of the 28593 21:21:27,360 --> 21:21:31,279 file here so we're going to say end f 28594 21:21:29,440 --> 21:21:33,680 and again this is the syntax of the 28595 21:21:31,279 --> 21:21:35,440 preprocessor i don't want to get too 28596 21:21:33,679 --> 21:21:37,919 much into this because we will learn 28597 21:21:35,440 --> 21:21:41,040 about this later in the course but we 28598 21:21:37,919 --> 21:21:44,232 can now use this to guard against 28599 21:21:41,039 --> 21:21:46,079 something being included more than once 28600 21:21:44,232 --> 21:21:48,080 and if we're trying to build the program 28601 21:21:46,080 --> 21:21:50,479 now hopefully it's going to world let's 28602 21:21:48,080 --> 21:21:53,919 use the gcc and now you see that the 28603 21:21:50,479 --> 21:21:56,551 world is good even if we are including 28604 21:21:53,919 --> 21:21:59,679 the constant header multiple times in 28605 21:21:56,551 --> 21:22:02,000 the main file here we can even go down 28606 21:21:59,679 --> 21:22:03,599 and do that like a thousand times so 28607 21:22:02,000 --> 21:22:05,919 let's do that and you're going to see 28608 21:22:03,600 --> 21:22:09,440 that if we try to compile it is going to 28609 21:22:05,919 --> 21:22:11,360 boil define because now we have a guard 28610 21:22:09,440 --> 21:22:13,360 guarding against including this thing 28611 21:22:11,360 --> 21:22:15,831 more than once the preprocessor is 28612 21:22:13,360 --> 21:22:16,800 basically going to come in here and say 28613 21:22:16,800 --> 21:22:21,919 the constant's name is not defined i'm 28614 21:22:21,919 --> 21:22:26,399 and i am going to put the code here in 28615 21:22:24,160 --> 21:22:29,040 the source file that includes me 28616 21:22:26,399 --> 21:22:30,959 if it happens to be already defined this 28617 21:22:29,039 --> 21:22:33,919 condition here is going to fail it's not 28618 21:22:30,960 --> 21:22:35,680 going to do any more than in this file 28619 21:22:33,919 --> 21:22:38,000 the preprocessor is basically going to 28620 21:22:35,679 --> 21:22:40,719 give up on this file here this is what i 28621 21:22:38,000 --> 21:22:43,279 want you to know so now we should be 28622 21:22:40,720 --> 21:22:45,759 able to split our code into multiple 28623 21:22:43,279 --> 21:22:47,119 files and now that i think about it we 28624 21:22:45,759 --> 21:22:49,119 don't really need to include the 28625 21:22:47,119 --> 21:22:51,440 constants header here because it's going 28626 21:22:49,119 --> 21:22:53,679 to be broadened by the cylinder class 28627 21:22:51,440 --> 21:22:56,160 but it was a good opportunity to teach 28628 21:22:53,679 --> 21:22:57,679 you about the include guards here now 28629 21:22:56,160 --> 21:22:59,279 that you notice we're going to remove 28630 21:22:57,679 --> 21:23:01,919 this and we're going to build and show 28631 21:22:59,279 --> 21:23:04,639 you that this is going to work anyway so 28632 21:23:01,919 --> 21:23:06,959 weld is good we can run our program 28633 21:23:04,639 --> 21:23:08,551 clear and run rooster this is going to 28634 21:23:06,960 --> 21:23:11,760 work the last thing i want to show you 28635 21:23:08,551 --> 21:23:14,800 is that we can go even further and split 28636 21:23:11,759 --> 21:23:16,871 our class into two separate files 28637 21:23:14,800 --> 21:23:19,919 because people who come to the header 28638 21:23:16,872 --> 21:23:23,192 file are usually interested in your 28639 21:23:19,919 --> 21:23:25,279 class definition they are really not 28640 21:23:23,191 --> 21:23:27,512 interested in your implementation 28641 21:23:25,279 --> 21:23:30,232 details for example they want to see 28642 21:23:27,512 --> 21:23:32,160 that you have a volume function and they 28643 21:23:30,232 --> 21:23:34,400 want to know that they can call that 28644 21:23:32,160 --> 21:23:36,639 function but they don't really want to 28645 21:23:34,399 --> 21:23:38,639 know how you compute your volume and we 28646 21:23:36,639 --> 21:23:41,512 can really make events simple for them 28647 21:23:38,639 --> 21:23:44,160 and hide the implementation details in a 28648 21:23:41,512 --> 21:23:46,232 separate cpp file so let's do that we're 28649 21:23:44,160 --> 21:23:48,960 going to set up a new cpp file we're 28650 21:23:46,232 --> 21:23:50,480 going to call this cylinder that's cpp 28651 21:23:55,279 --> 21:24:01,191 in the cpp file and i am doing this just 28652 21:23:58,800 --> 21:24:02,872 to show you that it is possible in 28653 21:24:01,191 --> 21:24:04,551 practice you're going to have to decide 28654 21:24:02,872 --> 21:24:06,639 for yourself what you want to keep in 28655 21:24:04,551 --> 21:24:09,919 the header file and what you want to 28656 21:24:06,639 --> 21:24:11,512 keep in the cpp file so the cylinder 28657 21:24:09,919 --> 21:24:14,319 default constructor here is going to 28658 21:24:11,512 --> 21:24:16,960 stay this way but we can move the other 28659 21:24:14,320 --> 21:24:18,800 constructor here in the cpp file and the 28660 21:24:16,960 --> 21:24:20,800 way we do that we're going to just grab 28661 21:24:18,800 --> 21:24:23,120 this and i am going to change this line 28662 21:24:20,800 --> 21:24:25,120 by line so that you can see this 28663 21:24:23,119 --> 21:24:27,599 and in the cpp file the first thing we 28664 21:24:25,119 --> 21:24:30,000 want to do is to include the cylinder 28665 21:24:27,600 --> 21:24:32,639 header because the implementations here 28666 21:24:32,639 --> 21:24:36,720 and we're going to put in our 28667 21:24:33,759 --> 21:24:39,759 constructor and modify it to be fit for 28668 21:24:36,720 --> 21:24:42,479 living in a cpp file so let's come here 28669 21:24:39,759 --> 21:24:44,399 and do that the way we tell this that 28670 21:24:42,479 --> 21:24:47,191 this is really an implementation of a 28671 21:24:44,399 --> 21:24:49,039 function we have in our header here is 28672 21:24:47,191 --> 21:24:50,959 to put the class name in front we're 28673 21:24:49,039 --> 21:24:53,191 going to say cylinder and put two 28674 21:24:50,960 --> 21:24:55,120 columns the moment we do that the 28675 21:24:53,191 --> 21:24:58,000 compiler is going to know that this is 28676 21:24:55,119 --> 21:24:59,679 an implementation of a function we have 28677 21:24:58,000 --> 21:25:02,639 in our header file and this is the 28678 21:24:59,679 --> 21:25:05,191 function here and now we can take out 28679 21:25:02,639 --> 21:25:07,512 the body here and live this as a 28680 21:25:05,191 --> 21:25:09,759 prototype for the function which is our 28681 21:25:07,512 --> 21:25:12,232 constructor view we can do this 28682 21:25:09,759 --> 21:25:13,919 okay if we do this in world the code is 28683 21:25:12,232 --> 21:25:16,960 going to world it's going to construct 28684 21:25:13,919 --> 21:25:18,232 our objects let's go back in main cpb 28685 21:25:18,232 --> 21:25:24,160 constructor we are using it is we can 28686 21:25:20,960 --> 21:25:26,320 weld and make sure that what we just did 28687 21:25:24,160 --> 21:25:28,872 didn't break anything we are building 28688 21:25:26,320 --> 21:25:31,280 and we can clear things up a little bit 28689 21:25:28,872 --> 21:25:34,479 and run rooster this is going to give us 28690 21:25:31,279 --> 21:25:35,919 our volume and we have started to split 28691 21:25:35,919 --> 21:25:40,080 in our class here some things are going 28692 21:25:38,000 --> 21:25:41,279 to leave in the header file some other 28693 21:25:40,080 --> 21:25:43,759 things are going to leave in the 28694 21:25:41,279 --> 21:25:45,512 implementation file so we have moved our 28695 21:25:43,759 --> 21:25:47,511 constructor here let's move the volume 28696 21:25:45,512 --> 21:25:49,680 function we can grab it and i move 28697 21:25:47,512 --> 21:25:51,512 things line by line i think this is the 28698 21:25:51,512 --> 21:25:56,639 and i think we can move this things a 28699 21:25:53,919 --> 21:25:58,399 little bit to the left let's do that and 28700 21:25:56,639 --> 21:26:01,039 we're going to say that this is a 28701 21:25:58,399 --> 21:26:03,119 function implementation for some 28702 21:26:01,039 --> 21:26:05,599 function that is declared in the class 28703 21:26:03,119 --> 21:26:07,679 here so we're going to say cylinder and 28704 21:26:05,600 --> 21:26:09,360 we're going to put two columns this is 28705 21:26:07,679 --> 21:26:12,399 going to tell the compiler this is an 28706 21:26:09,360 --> 21:26:15,600 implementation of the function we have 28707 21:26:12,399 --> 21:26:18,720 in our class and the return type has to 28708 21:26:15,600 --> 21:26:21,279 live in front of this scope resolution 28709 21:26:18,720 --> 21:26:23,759 operator okay what we have here cylinder 28710 21:26:21,279 --> 21:26:26,080 column column is called the scope 28711 21:26:23,759 --> 21:26:29,191 resolution operator and is basically 28712 21:26:26,080 --> 21:26:32,400 telling the compiler that our function 28713 21:26:29,191 --> 21:26:34,799 called volume leaves in the scope of the 28714 21:26:32,399 --> 21:26:37,119 cylinder class this is what the syntax 28715 21:26:34,800 --> 21:26:39,192 here is saying and this little thing 28716 21:26:37,119 --> 21:26:40,399 here is called a scope resolution 28717 21:26:40,399 --> 21:26:45,360 okay so we have moved our volume 28718 21:26:42,720 --> 21:26:48,000 function we can now take the body out 28719 21:26:45,360 --> 21:26:50,232 from the class and keep the prototype 28720 21:26:48,000 --> 21:26:52,232 here we can go down and really practice 28721 21:26:50,232 --> 21:26:54,232 on the setters and gators so let's do 28722 21:26:52,232 --> 21:26:56,639 that so we're going to grab them all 28723 21:26:54,232 --> 21:26:59,360 together because now you have the gist 28724 21:26:56,639 --> 21:27:02,399 about how this works we can copy them 28725 21:26:59,360 --> 21:27:04,232 and move them into the cpp file and 28726 21:27:02,399 --> 21:27:07,119 we're going to fix the code lined by 28727 21:27:04,232 --> 21:27:11,040 line here so let's make sure they are 28728 21:27:07,119 --> 21:27:13,279 aligned properly we have two left to do 28729 21:27:11,039 --> 21:27:15,279 let's do that and i really have to show 28730 21:27:13,279 --> 21:27:18,160 you this life because some people are 28731 21:27:15,279 --> 21:27:20,639 going to have problems regarding how i 28732 21:27:18,160 --> 21:27:23,192 did this so i want you to see this live 28733 21:27:20,639 --> 21:27:25,440 as i do this i'm not going to edit this 28734 21:27:23,191 --> 21:27:28,000 out so we're going to put our scope 28735 21:27:25,440 --> 21:27:30,000 resolution operator after our return 28736 21:27:28,000 --> 21:27:32,639 type here i'm going to say cylinder 28737 21:27:30,000 --> 21:27:35,679 cylinder can't type can i okay we have 28738 21:27:32,639 --> 21:27:37,759 this in and we can really copy this and 28739 21:27:35,679 --> 21:27:40,319 paste this into place wherever it is 28740 21:27:37,759 --> 21:27:42,479 needed so i'm going to paste that here i 28741 21:27:40,320 --> 21:27:44,960 am going to paste that here and i am 28742 21:27:42,479 --> 21:27:47,440 going to paste that here now we have 28743 21:27:44,960 --> 21:27:49,720 successfully moved all the functions in 28744 21:27:49,720 --> 21:27:54,960 cylinder.cpp and we can go back in our 28745 21:27:52,800 --> 21:27:57,360 header and remove the bodies because we 28746 21:27:54,960 --> 21:27:59,040 don't need them here if we keep them 28747 21:27:57,360 --> 21:28:00,960 here we're going to have two definitions 28748 21:27:59,039 --> 21:28:03,679 for this functions and we're going to 28749 21:28:00,960 --> 21:28:05,440 have compiler errors let's take this out 28750 21:28:03,679 --> 21:28:07,359 we're going to take the body out we're 28751 21:28:05,440 --> 21:28:09,360 going to take the body out and we're 28752 21:28:07,360 --> 21:28:11,919 going to take the body out let's do that 28753 21:28:11,919 --> 21:28:17,119 and now if we build our program it 28754 21:28:14,320 --> 21:28:19,040 should work we're going to work with gcc 28755 21:28:17,119 --> 21:28:21,360 see the world is good and if we try to 28756 21:28:19,039 --> 21:28:23,679 run the program it should give us what 28757 21:28:21,360 --> 21:28:24,479 it was given before this is the volume 28758 21:28:24,479 --> 21:28:29,759 one thing i want you to see is that you 28759 21:28:26,720 --> 21:28:32,320 don't want to keep two definitions for 28760 21:28:29,759 --> 21:28:34,720 your functions so for example we can 28761 21:28:32,320 --> 21:28:36,400 grab the body here and bring this back 28762 21:28:34,720 --> 21:28:38,399 in the header file i want you to see 28763 21:28:36,399 --> 21:28:41,191 what's going to happen if you do that i 28764 21:28:38,399 --> 21:28:43,191 am going to paste that in here and i am 28765 21:28:41,191 --> 21:28:46,000 going to move things a little bit to the 28766 21:28:43,191 --> 21:28:48,319 right to align things properly here so 28767 21:28:46,000 --> 21:28:51,191 if we do this the compiler is going to 28768 21:28:48,320 --> 21:28:53,040 see that we are calling that constructor 28769 21:28:51,191 --> 21:28:55,360 and the compiler is going to have no 28770 21:28:53,039 --> 21:28:57,831 problem with us but once we hit the 28771 21:28:55,360 --> 21:28:59,759 linker stage the linker is going to find 28772 21:28:57,831 --> 21:29:02,000 that we have two definitions for the 28773 21:28:59,759 --> 21:29:04,319 constructor here we have one in the 28774 21:29:04,320 --> 21:29:09,600 implementation file and this is going to 28775 21:29:07,039 --> 21:29:12,399 raise a compiler error saying we have 28776 21:29:09,600 --> 21:29:14,960 two definitions for the constructor here 28777 21:29:12,399 --> 21:29:17,039 and the linker want to know which one to 28778 21:29:14,960 --> 21:29:18,800 use so it's going to give you an error 28779 21:29:17,039 --> 21:29:21,191 let's build and really show you this 28780 21:29:18,800 --> 21:29:22,872 because this can be hard to debug if you 28781 21:29:21,191 --> 21:29:25,919 don't know what is happening okay you 28782 21:29:22,872 --> 21:29:28,720 see we have world finished with errors 28783 21:29:25,919 --> 21:29:31,279 and the problem is cylinder cylinder 28784 21:29:28,720 --> 21:29:33,680 double double is redefined we don't want 28785 21:29:31,279 --> 21:29:36,232 to have two definitions for the sum 28786 21:29:33,679 --> 21:29:39,039 function so once you move things from 28787 21:29:36,232 --> 21:29:41,600 header to implementation file be sure 28788 21:29:39,039 --> 21:29:43,759 you don't have any more definitions left 28789 21:29:41,600 --> 21:29:46,552 in the header or you're going to have 28790 21:29:43,759 --> 21:29:49,439 really hard to debug problems 28791 21:29:46,551 --> 21:29:52,720 and now i think we have our program 28792 21:29:49,440 --> 21:29:55,600 organized fairly well for what we know 28793 21:29:52,720 --> 21:29:58,720 so far about c plus plus we have our 28794 21:29:55,600 --> 21:30:00,872 constants living in another header file 28795 21:29:58,720 --> 21:30:03,279 and we have learnt about include cards 28796 21:30:00,872 --> 21:30:05,600 what we can use to guard against our 28797 21:30:03,279 --> 21:30:08,232 stuff being included more than once by 28798 21:30:05,600 --> 21:30:10,480 the preprocessor we have our cylinder 28799 21:30:08,232 --> 21:30:12,552 class living in its own file and again 28800 21:30:10,479 --> 21:30:14,639 we have an include guard here and you 28801 21:30:12,551 --> 21:30:16,872 can see that this class now is really 28802 21:30:14,639 --> 21:30:18,872 easy to look at we can look at the 28803 21:30:16,872 --> 21:30:21,120 information about the member functions 28804 21:30:18,872 --> 21:30:23,192 we have for example we have a default 28805 21:30:21,119 --> 21:30:25,599 constructor we have a constructor that 28806 21:30:23,191 --> 21:30:28,399 takes two parameters but we don't see 28807 21:30:25,600 --> 21:30:30,720 the details about how these things do 28808 21:30:28,399 --> 21:30:33,439 their jobs for example we don't see the 28809 21:30:30,720 --> 21:30:36,551 details about how the volume is computed 28810 21:30:33,440 --> 21:30:38,400 those details are hidden in the cpp file 28811 21:30:36,551 --> 21:30:40,872 and if you want to see them you need to 28812 21:30:38,399 --> 21:30:43,599 go to this implementation file here this 28813 21:30:40,872 --> 21:30:46,160 is a good design and i encourage to 28814 21:30:43,600 --> 21:30:48,000 split your c plus plus programs in 28815 21:30:46,160 --> 21:30:50,160 multiple files like this this is going 28816 21:30:48,000 --> 21:30:52,399 to make your life easier in this lecture 28817 21:30:50,160 --> 21:30:55,512 we're going to see how we can manage our 28818 21:30:52,399 --> 21:30:58,319 class objects through pointers and in 28819 21:30:55,512 --> 21:31:01,191 most cases we need to manage our class 28820 21:30:58,320 --> 21:31:04,160 object through pointers if we are using 28821 21:31:01,191 --> 21:31:06,399 some form of dynamic memory allocation 28822 21:31:04,160 --> 21:31:08,960 here is a simple example we have our 28823 21:31:06,399 --> 21:31:11,119 cylinder class included and down here 28824 21:31:08,960 --> 21:31:13,360 you see that we can create cylinder 28825 21:31:11,119 --> 21:31:15,919 objects on the stack if we use the 28826 21:31:13,360 --> 21:31:18,080 syntax like we do here the objects are 28827 21:31:15,919 --> 21:31:20,080 going to be created on the stack and we 28828 21:31:18,080 --> 21:31:22,720 have seen that we can access the 28829 21:31:20,080 --> 21:31:25,512 behaviors or data inside our class 28830 21:31:22,720 --> 21:31:27,360 object using the data notation like we 28831 21:31:25,512 --> 21:31:30,400 do here for example here we are calling 28832 21:31:27,360 --> 21:31:32,479 our volume function but sometimes we 28833 21:31:30,399 --> 21:31:35,039 want to manage our objects through 28834 21:31:32,479 --> 21:31:37,440 pointers for example if you look at the 28835 21:31:35,039 --> 21:31:40,399 syntax we have here we are saying 28836 21:31:37,440 --> 21:31:42,320 cylinder and using the asterisk here 28837 21:31:40,399 --> 21:31:45,279 this is going to say that this is going 28838 21:31:42,320 --> 21:31:47,360 to be a pointer to a cylinder type 28839 21:31:45,279 --> 21:31:51,191 and i'm going to give it a name c2 and 28840 21:31:47,360 --> 21:31:53,680 we can allocate our object on the heap 28841 21:31:51,191 --> 21:31:56,720 using the new operator this is going to 28842 21:31:53,679 --> 21:31:58,719 dynamically allocate space from the heap 28843 21:31:56,720 --> 21:32:02,800 and that memory space is going to be 28844 21:31:58,720 --> 21:32:04,960 used to store our cylinder object now 28845 21:32:02,800 --> 21:32:08,080 the interesting thing is going to happen 28846 21:32:04,960 --> 21:32:10,872 when you try to access behaviors from 28847 21:32:08,080 --> 21:32:13,360 your cylinder object here for example if 28848 21:32:10,872 --> 21:32:16,400 we look at this line here we are trying 28849 21:32:13,360 --> 21:32:18,232 to print the volume of this cylinder and 28850 21:32:16,399 --> 21:32:20,799 you notice that we have to go through 28851 21:32:18,232 --> 21:32:24,320 ugly dereferencing here we have to 28852 21:32:20,800 --> 21:32:27,440 dereference the object and then call our 28853 21:32:24,320 --> 21:32:29,360 method through the dot operator here but 28854 21:32:27,440 --> 21:32:31,600 what i want you to see is that we can 28855 21:32:29,360 --> 21:32:34,320 avoid this ugly the reference that we 28856 21:32:31,600 --> 21:32:37,440 are doing here and use the pointer 28857 21:32:34,320 --> 21:32:40,080 access notation this dash greater than 28858 21:32:37,440 --> 21:32:42,800 symbol you see here is going to allow us 28859 21:32:40,080 --> 21:32:45,680 to access stuff directly using the 28860 21:32:42,800 --> 21:32:48,400 pointer to an object that is dynamically 28861 21:32:45,679 --> 21:32:51,279 allocated on the hip okay after we are 28862 21:32:48,399 --> 21:32:53,679 done using our cylinder object remember 28863 21:32:51,279 --> 21:32:55,831 it is memory that was dynamically 28864 21:32:53,679 --> 21:32:57,679 allocated from the hip we have to 28865 21:32:55,831 --> 21:33:00,319 release the memory so that it can be 28866 21:32:57,679 --> 21:33:02,551 used by the operating system and we 28867 21:33:00,320 --> 21:33:05,680 release the memory using the delete 28868 21:33:02,551 --> 21:33:07,599 operator as we do here okay the idea i 28869 21:33:05,679 --> 21:33:10,551 wanted to introduce here is that we can 28870 21:33:07,600 --> 21:33:12,960 use players to manage our cylinder 28871 21:33:10,551 --> 21:33:16,000 objects and this is one way we can do 28872 21:33:12,960 --> 21:33:19,040 this and once we are using a pointer as 28873 21:33:16,000 --> 21:33:22,551 a handle to our cylinder object we can 28874 21:33:19,039 --> 21:33:25,599 use the pointer access operator here to 28875 21:33:22,551 --> 21:33:27,759 access stuff from our class objects 28876 21:33:25,600 --> 21:33:29,440 easily and another thing you should know 28877 21:33:27,759 --> 21:33:32,231 is that we can actually create a 28878 21:33:29,440 --> 21:33:34,551 cylinder object and store in the address 28879 21:33:32,232 --> 21:33:36,160 of objects that we have on the stack and 28880 21:33:34,551 --> 21:33:38,800 we're going to see this in a minute when 28881 21:33:36,160 --> 21:33:41,832 we hit visual studio code now that you 28882 21:33:38,800 --> 21:33:43,512 have an idea about this let's head over 28883 21:33:41,831 --> 21:33:46,231 to visual studio code and play with this 28884 21:33:43,512 --> 21:33:48,800 a little more okay here we are in our 28885 21:33:46,232 --> 21:33:51,440 working folder the current project is 28886 21:33:48,800 --> 21:33:54,160 managing class objects through pointers 28887 21:33:51,440 --> 21:33:56,080 we can see the entire name here and now 28888 21:33:54,160 --> 21:33:58,800 we're not going to grab our template 28889 21:33:56,080 --> 21:34:02,000 project because we want to start from 28890 21:33:58,800 --> 21:34:04,320 the project we had in this lecture on a 28891 21:34:02,000 --> 21:34:06,639 class across multiple files so we're 28892 21:34:04,320 --> 21:34:08,400 going to go in that project and grab 28893 21:34:08,399 --> 21:34:12,479 except for the executable because we 28894 21:34:12,479 --> 21:34:17,191 and we're going to go in our current 28895 21:34:14,399 --> 21:34:18,639 project and paste in the code and this 28896 21:34:17,191 --> 21:34:21,512 is going to give us a good starting 28897 21:34:18,639 --> 21:34:23,759 point so we can grab this and open 28898 21:34:21,512 --> 21:34:25,279 this project in visual studio code it's 28899 21:34:23,759 --> 21:34:27,360 going to open the folder and we're going 28900 21:34:25,279 --> 21:34:29,512 to have everything we want to start 28901 21:34:27,360 --> 21:34:32,639 doing what we want to do in this lecture 28902 21:34:29,512 --> 21:34:35,512 here so now we should have access to our 28903 21:34:32,639 --> 21:34:38,160 cylinder class and if you look here we 28904 21:34:35,512 --> 21:34:41,760 have an object of cylinder type already 28905 21:34:38,160 --> 21:34:43,600 created by this line on liner number six 28906 21:34:41,759 --> 21:34:46,639 here and we have seen that we can use 28907 21:34:43,600 --> 21:34:49,760 the syntax like cylinder volume here 28908 21:34:46,639 --> 21:34:53,119 using the dot call operator to access 28909 21:34:49,759 --> 21:34:54,959 stuff from our class object and this is 28910 21:34:53,119 --> 21:34:57,119 going to work but we want to see that we 28911 21:34:54,960 --> 21:34:59,192 can use pointers to manage our object 28912 21:34:57,119 --> 21:35:01,679 here and the first thing i want you to 28913 21:34:59,191 --> 21:35:04,319 see is that we can manage a stack object 28914 21:35:01,679 --> 21:35:06,231 through pointers and to really show this 28915 21:35:06,232 --> 21:35:10,639 another variable it's going to be a 28916 21:35:07,831 --> 21:35:12,720 cylinder pointer and it's going to be p 28917 21:35:10,639 --> 21:35:15,279 cylinder one it's going to be a pointer 28918 21:35:12,720 --> 21:35:18,080 to cylinder one here that's what i mean 28919 21:35:15,279 --> 21:35:20,639 and we can give it an address of 28920 21:35:18,080 --> 21:35:22,960 cylinder one as a value so we can do 28921 21:35:20,639 --> 21:35:25,831 that for example and put in the address 28922 21:35:22,960 --> 21:35:27,832 of operator and this is going to work 28923 21:35:25,831 --> 21:35:30,551 now we can go through our pointer here 28924 21:35:27,831 --> 21:35:32,871 and do stuff with this cylinder object 28925 21:35:30,551 --> 21:35:35,440 but the syntax is going to be pretty 28926 21:35:32,872 --> 21:35:37,512 ugly so for example let's try and print 28927 21:35:37,512 --> 21:35:42,800 we're going to say htdcl and say volume 28928 21:35:40,551 --> 21:35:45,039 can save that and we're going to have to 28929 21:35:42,800 --> 21:35:47,512 go through the dereference operator so 28930 21:35:45,039 --> 21:35:50,231 if we do something like p cylinder one 28931 21:35:47,512 --> 21:35:53,119 and try to use the dot operator 28932 21:35:50,232 --> 21:35:56,080 you're going to see that we have the 28933 21:35:53,119 --> 21:35:59,119 volume function but if we try to call it 28934 21:35:56,080 --> 21:36:01,600 let's do that let's make sure we are 28935 21:35:59,119 --> 21:36:03,831 using the dot operator here if we try to 28936 21:36:01,600 --> 21:36:05,512 compile this program it's not going to 28937 21:36:05,512 --> 21:36:11,119 we are trying to call stuff from a 28938 21:36:08,160 --> 21:36:13,360 pointer and remember a pointer is just 28939 21:36:11,119 --> 21:36:16,000 going to store an address it doesn't 28940 21:36:13,360 --> 21:36:18,479 really have information about the type 28941 21:36:16,000 --> 21:36:20,000 that it is pointing to if we try to 28942 21:36:18,479 --> 21:36:22,720 compile this program we're going to get 28943 21:36:20,000 --> 21:36:23,679 a compiler error so let's use gcc to do 28944 21:36:23,679 --> 21:36:28,959 and we're going to get request for 28945 21:36:25,600 --> 21:36:31,512 member volume in pc lender 1. remember p 28946 21:36:28,960 --> 21:36:33,680 cylinder 1 is just a pointer it's not an 28947 21:36:31,512 --> 21:36:35,680 object so we can't really call the 28948 21:36:35,679 --> 21:36:40,079 if we want to use the dot call operator 28949 21:36:38,232 --> 21:36:42,320 here we have to the reference and 28950 21:36:42,320 --> 21:36:47,600 object that the pointer is pointing to 28951 21:36:45,119 --> 21:36:49,279 and we do that using the star operator 28952 21:36:47,600 --> 21:36:51,192 you already know how to dereference a 28953 21:36:49,279 --> 21:36:53,360 pointer so we can dereference our 28954 21:36:51,191 --> 21:36:55,679 pointer and then call the volume 28955 21:36:53,360 --> 21:36:57,360 function on what the pointer is pointing 28956 21:36:55,679 --> 21:37:00,159 to and this is going to work it's going 28957 21:36:57,360 --> 21:37:02,160 to print out volume let's build now now 28958 21:37:00,160 --> 21:37:04,160 that we are dereferencing correctly and 28959 21:37:02,160 --> 21:37:07,120 you see that this is going to work and 28960 21:37:04,160 --> 21:37:09,440 if we try to print this let's bring up a 28961 21:37:07,119 --> 21:37:11,360 terminal so that we can run this program 28962 21:37:09,440 --> 21:37:14,000 let's run rooster you're going to see 28963 21:37:11,360 --> 21:37:17,279 that we have our volume here so to 28964 21:37:14,000 --> 21:37:19,360 really get to what is pointed to 28965 21:37:17,279 --> 21:37:21,191 you need to use the reference operator 28966 21:37:19,360 --> 21:37:23,831 and then call whatever method you want 28967 21:37:21,191 --> 21:37:26,159 to call on your object but we can do 28968 21:37:23,831 --> 21:37:28,959 things even easier there is a syntax we 28969 21:37:26,160 --> 21:37:30,960 can use to use the pointer directly here 28970 21:37:28,960 --> 21:37:33,280 because going through all this 28971 21:37:30,960 --> 21:37:35,280 dereferencing ugliness is really not 28972 21:37:33,279 --> 21:37:37,831 cool so we're going to grab this and 28973 21:37:35,279 --> 21:37:40,479 show you that we can use the dash 28974 21:37:37,831 --> 21:37:41,679 greater than operator that's how i 28975 21:37:41,679 --> 21:37:45,439 and if we use that we don't need to the 28976 21:37:45,440 --> 21:37:50,960 all we need to do is use the dash 28977 21:37:48,479 --> 21:37:51,679 greater than symbol here and it's going 28978 21:37:51,679 --> 21:37:57,191 directly tell the compiler don't code 28979 21:37:54,720 --> 21:37:59,440 stuff on the pointer directly do the 28980 21:37:57,191 --> 21:38:01,512 reference the pointer internally and 28981 21:37:59,440 --> 21:38:04,232 then call the volume function on 28982 21:38:01,512 --> 21:38:07,279 whatever you get after referencing so 28983 21:38:04,232 --> 21:38:09,600 this is basically going to push the the 28984 21:38:07,279 --> 21:38:12,551 referencing ugliness here behind the 28985 21:38:09,600 --> 21:38:15,120 curtains of the compiler and we can use 28986 21:38:12,551 --> 21:38:17,440 a nice syntax like this and i think this 28987 21:38:15,119 --> 21:38:19,831 is much better than what we were doing 28988 21:38:17,440 --> 21:38:21,680 here so if we try to bolt now we're 28989 21:38:19,831 --> 21:38:23,279 going to build and print our volume 28990 21:38:21,679 --> 21:38:26,079 you're going to see the bolt is good you 28991 21:38:23,279 --> 21:38:28,399 can clear and run rooster you see we see 28992 21:38:26,080 --> 21:38:31,919 our volume here so this is what i want 28993 21:38:28,399 --> 21:38:35,191 you to see it is possible to manage your 28994 21:38:31,919 --> 21:38:37,039 cylinder objects through pointers and 28995 21:38:35,191 --> 21:38:39,759 this is one way we can do this by 28996 21:38:37,039 --> 21:38:41,831 managing a stack object through pointers 28997 21:38:39,759 --> 21:38:44,159 and this is what we are doing here if 28998 21:38:41,831 --> 21:38:47,191 you want to call stuff on your class 28999 21:38:44,160 --> 21:38:50,000 object you can either do the ugly stuff 29000 21:38:47,191 --> 21:38:52,639 we do here or you can use the nice 29001 21:38:50,000 --> 21:38:54,551 pointer access operator and it is going 29002 21:38:52,639 --> 21:38:57,119 to be cool okay now that we have seen 29003 21:38:54,551 --> 21:38:59,440 this let's see another use case in which 29004 21:38:57,119 --> 21:39:01,679 we're going to be creating a cylinder 29005 21:38:59,440 --> 21:39:03,832 object on the heap through the new 29006 21:39:01,679 --> 21:39:06,159 operator okay we can go down and save 29007 21:39:03,831 --> 21:39:07,679 that we're going to say cylinder pointer 29008 21:39:07,679 --> 21:39:12,719 c lender 2. we can call it like this and 29009 21:39:10,720 --> 21:39:15,040 then we're going to say new cylinder and 29010 21:39:12,720 --> 21:39:16,399 you're going to see that we can call a 29011 21:39:16,399 --> 21:39:21,919 we have two types of constructors we can 29012 21:39:19,039 --> 21:39:24,159 call we're going to call our constructor 29013 21:39:21,919 --> 21:39:26,720 that takes two parameters and we can 29014 21:39:26,720 --> 21:39:31,831 100 why not oh let's put the two after 29015 21:39:29,759 --> 21:39:34,551 here we can put in whatever we want this 29016 21:39:31,831 --> 21:39:36,479 is going to create a new cylinder object 29017 21:39:34,551 --> 21:39:38,872 the space for the cylinder object is 29018 21:39:36,479 --> 21:39:41,360 going to be allocated on the hip so this 29019 21:39:38,872 --> 21:39:43,360 is a hip object and because we are using 29020 21:39:43,360 --> 21:39:47,279 the other thing you should have in mind 29021 21:39:45,279 --> 21:39:49,759 is that you will need to release this 29022 21:39:47,279 --> 21:39:52,080 memory and let's go and do that directly 29023 21:39:49,759 --> 21:39:54,720 this is a good practice because you will 29024 21:39:52,080 --> 21:39:56,800 forget to release your memory if you 29025 21:39:54,720 --> 21:39:59,279 don't really keep this in mind so we are 29026 21:39:56,800 --> 21:40:02,479 going to delete p cylinder 2 because 29027 21:39:59,279 --> 21:40:05,191 that's what we used to allocate our hip 29028 21:40:02,479 --> 21:40:07,679 memory here okay now that we have taken 29029 21:40:05,191 --> 21:40:10,479 care of memory management we can go in 29030 21:40:07,679 --> 21:40:12,719 and use our p cylinder to object 29031 21:40:10,479 --> 21:40:16,872 and for example we can print its volume 29032 21:40:12,720 --> 21:40:18,872 by using the stdc out statement here so 29033 21:40:16,872 --> 21:40:21,120 let's go down and save that 29034 21:40:18,872 --> 21:40:22,960 let's align this properly and we're 29035 21:40:22,960 --> 21:40:27,280 cylinder 2 to really make this super 29036 21:40:25,279 --> 21:40:28,720 clear and then we're going to say p 29037 21:40:28,720 --> 21:40:33,919 and call the volume function here this 29038 21:40:30,800 --> 21:40:36,639 is going to work let's draw in world the 29039 21:40:33,919 --> 21:40:38,800 world is good we can clear and run 29040 21:40:36,639 --> 21:40:41,039 rooster this is going to give us our 29041 21:40:38,800 --> 21:40:43,040 volume and if we try and use this 29042 21:40:41,039 --> 21:40:45,039 information to create a cylinder we're 29043 21:40:43,039 --> 21:40:47,759 going to see that the volume is what we 29044 21:40:45,039 --> 21:40:49,360 see here we can even try and print its 29045 21:40:49,360 --> 21:40:54,639 let's do that we're going to say sdd cl 29046 21:40:52,639 --> 21:40:56,960 we're going to say p cylinder 2 and 29047 21:40:54,639 --> 21:40:59,039 we're going to say base radius we can 29048 21:40:56,960 --> 21:41:01,192 hit the dot operator here you're going 29049 21:40:59,039 --> 21:41:03,679 to see we're going to have a few options 29050 21:41:01,191 --> 21:41:05,919 in a visual studio code here if we 29051 21:41:03,679 --> 21:41:07,679 double click on get base radius here 29052 21:41:05,919 --> 21:41:09,919 you're going to see that visual studio 29053 21:41:07,679 --> 21:41:12,079 code is going to use the pointer access 29054 21:41:09,919 --> 21:41:14,872 operator here and this is really cool so 29055 21:41:12,080 --> 21:41:17,831 we can hit std and yeah we can close the 29056 21:41:14,872 --> 21:41:20,479 left pane here because we need some more 29057 21:41:17,831 --> 21:41:23,439 breathing space and if we try to 29058 21:41:20,479 --> 21:41:26,319 build and run this program and we have a 29059 21:41:23,440 --> 21:41:29,040 compiler error here and the reason is 29060 21:41:26,320 --> 21:41:32,640 get base radius is a function 29061 21:41:29,039 --> 21:41:34,479 if we go back in our cylinder class we 29062 21:41:32,639 --> 21:41:37,512 see that we have a function called get 29063 21:41:34,479 --> 21:41:39,759 base radius so if we want to call this 29064 21:41:37,512 --> 21:41:42,400 in the main function we need to call 29065 21:41:39,759 --> 21:41:45,119 that as a function so we need to put a 29066 21:41:42,399 --> 21:41:47,279 pair of parentheses here and if we build 29067 21:41:45,119 --> 21:41:48,399 we should build without a problem let's 29068 21:41:48,399 --> 21:41:52,319 you see the build is good and if we run 29069 21:41:50,479 --> 21:41:54,959 the program we're going to see exactly 29070 21:41:52,320 --> 21:41:58,320 what we expect let's run a rooster we're 29071 21:41:54,960 --> 21:42:01,040 going to see what we expect here and the 29072 21:41:58,320 --> 21:42:02,720 message here was really that you can 29073 21:42:02,720 --> 21:42:07,919 class object through pointers one case 29074 21:42:06,160 --> 21:42:10,080 is when you have a pointer that is 29075 21:42:07,919 --> 21:42:12,319 pointing to a stack object and we saw 29076 21:42:10,080 --> 21:42:15,119 that we could do that here we can also 29077 21:42:12,320 --> 21:42:17,512 use pointer notation to allocate 29078 21:42:15,119 --> 21:42:19,919 objects on the heap and point to them 29079 21:42:17,512 --> 21:42:21,600 with our pointer here and whenever we 29080 21:42:19,919 --> 21:42:24,551 are using a pointer we have the option 29081 21:42:21,600 --> 21:42:26,872 to use the pointer call operator here to 29082 21:42:24,551 --> 21:42:29,360 do things easily i am going to be 29083 21:42:26,872 --> 21:42:32,000 calling this the pointer call operator 29084 21:42:29,360 --> 21:42:34,080 to mean this from here on if we happen 29085 21:42:32,000 --> 21:42:36,720 to need this and you should know what i 29086 21:42:34,080 --> 21:42:38,639 mean by that this is really all i set 29087 21:42:36,720 --> 21:42:40,720 out to share in this lecture i hope you 29088 21:42:38,639 --> 21:42:42,800 found it interesting we are going to 29089 21:42:40,720 --> 21:42:44,320 stop here in this lecture the next one 29090 21:42:42,800 --> 21:42:45,680 we're going to learn about these 29091 21:42:45,679 --> 21:42:49,831 go ahead and finish up here and meet me 29092 21:42:48,080 --> 21:42:52,080 there in this lecture we're going to 29093 21:42:49,831 --> 21:42:54,871 learn about these structures these 29094 21:42:52,080 --> 21:42:57,191 structures are spatial functions or 29095 21:42:54,872 --> 21:43:00,479 methods in a class that are called by 29096 21:42:57,191 --> 21:43:03,360 the compiler to destroy your object and 29097 21:43:00,479 --> 21:43:06,080 these are going to be especially useful 29098 21:43:03,360 --> 21:43:07,831 if you are for example allocating memory 29099 21:43:07,831 --> 21:43:13,191 let's look at the simple example here we 29100 21:43:10,232 --> 21:43:15,440 have a class called dog it has two 29101 21:43:13,191 --> 21:43:18,319 sections the public section and the 29102 21:43:15,440 --> 21:43:20,080 private section in the public section we 29103 21:43:18,320 --> 21:43:22,000 have two constructors a default 29104 21:43:20,080 --> 21:43:24,639 constructor that doesn't take any 29105 21:43:22,000 --> 21:43:26,479 parameter and a constructor that takes 29106 21:43:26,479 --> 21:43:31,679 the parameters are going to be used to 29107 21:43:29,039 --> 21:43:33,360 initialize our member variables and they 29108 21:43:31,679 --> 21:43:35,919 are passed by a value here for 29109 21:43:33,360 --> 21:43:38,320 simplicity and they are going to be used 29110 21:43:35,919 --> 21:43:41,440 to initialize these member variables 29111 21:43:38,320 --> 21:43:43,360 doug name dog breed and dog age 29112 21:43:41,440 --> 21:43:46,400 the special thing about this class is 29113 21:43:43,360 --> 21:43:49,119 that it has a destructor and you declare 29114 21:43:49,119 --> 21:43:53,831 syntax here he puts a tilde character in 29115 21:43:51,831 --> 21:43:56,639 front you say the name of the class and 29116 21:43:53,831 --> 21:43:59,119 then you put an empty parameter list it 29117 21:43:56,639 --> 21:44:01,919 is also possible to declare a destructor 29118 21:43:59,119 --> 21:44:04,399 and put in its entire body in the header 29119 21:44:01,919 --> 21:44:05,919 and this is the syntax to do this 29120 21:44:04,399 --> 21:44:08,551 inside the body you're going to do 29121 21:44:05,919 --> 21:44:11,360 whatever it is you need to destroy your 29122 21:44:08,551 --> 21:44:12,959 object for example if the class here is 29123 21:44:12,960 --> 21:44:17,920 constructor the destructor is the best 29124 21:44:15,759 --> 21:44:20,319 place to release that and this is an 29125 21:44:17,919 --> 21:44:23,191 example of how you can do that here is a 29126 21:44:20,320 --> 21:44:26,400 more direct example of how we allocate 29127 21:44:23,191 --> 21:44:28,319 for dynamic memory in the constructor 29128 21:44:26,399 --> 21:44:30,639 and release that memory in the 29129 21:44:28,320 --> 21:44:33,760 destructor we have our three variables 29130 21:44:30,639 --> 21:44:36,720 but we have chosen to allocate 29131 21:44:33,759 --> 21:44:39,679 our dog edge variable on the heap using 29132 21:44:36,720 --> 21:44:41,831 dynamic memory allocation and after we 29133 21:44:39,679 --> 21:44:44,551 dynamically allocate a piece of memory 29134 21:44:41,831 --> 21:44:46,399 to store our age variable we're going to 29135 21:44:44,551 --> 21:44:49,279 set that to 0 because we are in a 29136 21:44:46,399 --> 21:44:52,479 constructor if we are using the three 29137 21:44:49,279 --> 21:44:55,759 parameter constructor here we might use 29138 21:44:52,479 --> 21:44:58,159 the past parameter to initialize our age 29139 21:44:55,759 --> 21:45:00,080 variable but the important thing here is 29140 21:45:00,080 --> 21:45:07,600 dynamically educated memory on the hip 29141 21:45:03,191 --> 21:45:10,159 here and when our object dies we need to 29142 21:45:07,600 --> 21:45:12,800 remember to release that memory and the 29143 21:45:10,160 --> 21:45:14,800 destructor is the best place to release 29144 21:45:12,800 --> 21:45:17,919 that memory as you see here we say 29145 21:45:14,800 --> 21:45:20,080 delete dark age when our object dies and 29146 21:45:17,919 --> 21:45:22,399 the compiler is going to call this 29147 21:45:20,080 --> 21:45:23,440 function when it's about to kill the 29148 21:45:23,440 --> 21:45:29,191 so you might ask when are destructors 29149 21:45:27,119 --> 21:45:31,919 really called we are going to start by 29150 21:45:29,191 --> 21:45:34,551 looking at obvious cases here for 29151 21:45:31,919 --> 21:45:36,000 example when a local stack object goes 29152 21:45:36,000 --> 21:45:40,000 the destructor is going to be called 29153 21:45:37,759 --> 21:45:42,231 because that object is going to be wiped 29154 21:45:40,000 --> 21:45:43,191 out from memory so this is an obvious 29155 21:45:43,191 --> 21:45:48,551 in which the compiler will need to call 29156 21:45:45,279 --> 21:45:51,191 a destructor also when a hip object is 29157 21:45:48,551 --> 21:45:53,360 released from memory using delete the 29158 21:45:51,191 --> 21:45:55,512 destructor is also going to be called 29159 21:45:53,360 --> 21:45:57,831 but there are some indirect ways in 29160 21:45:55,512 --> 21:45:59,919 which the structures are also called for 29161 21:45:57,831 --> 21:46:01,759 example when an object is passed by a 29162 21:46:01,759 --> 21:46:07,599 this is going to call the destructor to 29163 21:46:04,720 --> 21:46:10,232 kill the object when the object goes out 29164 21:46:07,600 --> 21:46:12,000 of scope in the function because when an 29165 21:46:10,232 --> 21:46:14,160 object is passed by value we're going to 29166 21:46:12,000 --> 21:46:17,191 have a copy in the local scope of the 29167 21:46:14,160 --> 21:46:19,120 function and when the function exits 29168 21:46:17,191 --> 21:46:20,551 that local copy is going to be killed 29169 21:46:19,119 --> 21:46:23,119 and we're going to see an example of 29170 21:46:20,551 --> 21:46:25,360 that another case where you might have 29171 21:46:23,119 --> 21:46:27,831 the the structure called is when a local 29172 21:46:25,360 --> 21:46:30,080 object is returned from a function 29173 21:46:27,831 --> 21:46:32,720 another copy is going to be created to 29174 21:46:30,080 --> 21:46:34,960 be used outside the function and the 29175 21:46:32,720 --> 21:46:37,360 copy inside the function is going to be 29176 21:46:34,960 --> 21:46:40,480 killed but this is not guaranteed as 29177 21:46:37,360 --> 21:46:43,191 some compiler may optimize your return 29178 21:46:40,479 --> 21:46:44,639 value and use some kind of reference so 29179 21:46:43,191 --> 21:46:47,440 you're not guaranteed to see this 29180 21:46:44,639 --> 21:46:49,360 behavior but it is possible to see it 29181 21:46:47,440 --> 21:46:52,000 now that you have an idea about 29182 21:46:49,360 --> 21:46:54,160 destructors it's time we headed over to 29183 21:46:52,000 --> 21:46:56,800 visual studio code and play with us a 29184 21:46:54,160 --> 21:46:58,960 little more okay here we are in our 29185 21:46:56,800 --> 21:47:00,960 working folder the current project is 29186 21:46:58,960 --> 21:47:02,872 the structures we're going to grab our 29187 21:47:00,960 --> 21:47:05,192 template files pretty quick and we're 29188 21:47:02,872 --> 21:47:07,512 going to put them in place and after 29189 21:47:05,191 --> 21:47:09,512 that we're going to open our folder in 29190 21:47:07,512 --> 21:47:12,320 visual studio code by dragging and 29191 21:47:09,512 --> 21:47:15,440 dropping here this is going to open our 29192 21:47:12,320 --> 21:47:18,232 folder and we will see our main cpp file 29193 21:47:15,440 --> 21:47:20,551 here we can open it and i clean up and 29194 21:47:18,232 --> 21:47:22,960 remove what we don't need we have an 29195 21:47:20,551 --> 21:47:24,872 unneeded breakpoint here we can remove 29196 21:47:22,960 --> 21:47:26,720 that and we're going to clean up the 29197 21:47:24,872 --> 21:47:29,680 main function the first thing we want to 29198 21:47:26,720 --> 21:47:32,320 do is to put in our dog class i am going 29199 21:47:29,679 --> 21:47:34,079 to just put this in here because i don't 29200 21:47:32,320 --> 21:47:36,160 want you to watch me type all this 29201 21:47:34,080 --> 21:47:38,320 that's just going to waste your time we 29202 21:47:36,160 --> 21:47:40,800 have two sections in the dog class we 29203 21:47:38,320 --> 21:47:43,440 have a public section which is going to 29204 21:47:40,800 --> 21:47:45,040 now contain our constructors and we have 29205 21:47:43,440 --> 21:47:47,360 a private section which is going to 29206 21:47:45,039 --> 21:47:49,759 contain three member variables we have 29207 21:47:47,360 --> 21:47:52,080 two strengths one for the name and the 29208 21:47:49,759 --> 21:47:54,319 other for the breed of our dog and we 29209 21:47:52,080 --> 21:47:56,232 have another one which is an end pointer 29210 21:47:54,320 --> 21:47:59,040 which is going to store the edge of the 29211 21:47:56,232 --> 21:48:00,800 dog and you see that this is initialized 29212 21:47:59,039 --> 21:48:03,511 to no pointer one thing you should 29213 21:48:00,800 --> 21:48:06,232 notice is that for our parameter taken 29214 21:48:03,512 --> 21:48:09,191 constructor we are passing the string 29215 21:48:06,232 --> 21:48:12,639 information here using string views to 29216 21:48:09,191 --> 21:48:14,319 avoid copies with the std string view 29217 21:48:12,639 --> 21:48:16,319 type you already know this because we 29218 21:48:14,320 --> 21:48:18,552 learned about this but for this to work 29219 21:48:16,320 --> 21:48:20,872 we will need to include a string view so 29220 21:48:18,551 --> 21:48:23,440 let's do that and now we have our class 29221 21:48:20,872 --> 21:48:26,320 and we can use it however we want 29222 21:48:23,440 --> 21:48:29,191 so but before we do we need to put in 29223 21:48:26,320 --> 21:48:31,192 the implementations for these functions 29224 21:48:29,191 --> 21:48:33,599 one way we can do that is do that 29225 21:48:31,191 --> 21:48:35,919 directly in the function here but i want 29226 21:48:33,600 --> 21:48:38,400 you to see that you can also do that 29227 21:48:35,919 --> 21:48:40,160 directly outside the class here i don't 29228 21:48:38,399 --> 21:48:42,079 think i have shown you this but it is 29229 21:48:40,160 --> 21:48:44,872 possible to do that so we're going to 29230 21:48:42,080 --> 21:48:47,831 grab the prototype for our constructor 29231 21:48:44,872 --> 21:48:49,919 and say the scope resolution operator 29232 21:48:47,831 --> 21:48:52,479 and we're going to just paste in what we 29233 21:48:49,919 --> 21:48:54,551 have copied which is the prototype here 29234 21:48:52,479 --> 21:48:56,231 and we're going to go inside put in our 29235 21:48:54,551 --> 21:48:59,039 body and in the body we're going to 29236 21:48:56,232 --> 21:49:01,040 initialize these three guys 29237 21:48:59,039 --> 21:49:03,679 so the name is going to be initialized 29238 21:49:01,039 --> 21:49:06,719 to nem param and the bridge is going to 29239 21:49:03,679 --> 21:49:09,439 be initialized to breed the param we can 29240 21:49:06,720 --> 21:49:12,551 do that and the age is going to be 29241 21:49:09,440 --> 21:49:14,479 dynamically allocated from the hip you 29242 21:49:12,551 --> 21:49:17,440 see that it is a pointer so we're going 29243 21:49:14,479 --> 21:49:20,720 to say ph equals new and because we're 29244 21:49:17,440 --> 21:49:23,440 going to allocate dynamic space to store 29245 21:49:20,720 --> 21:49:26,399 an end and we're going to put data in 29246 21:49:23,440 --> 21:49:28,160 using our good old dereference operator 29247 21:49:28,160 --> 21:49:33,360 equals whatever ph parameter they passed 29248 21:49:31,600 --> 21:49:35,192 and i think we should say edge here 29249 21:49:33,360 --> 21:49:37,600 because it's not a pointer so we're 29250 21:49:35,191 --> 21:49:40,551 going to say age i think this is going 29251 21:49:37,600 --> 21:49:42,080 to do better age param this is probably 29252 21:49:40,551 --> 21:49:45,039 going to be better and we're going to 29253 21:49:42,080 --> 21:49:48,160 say age param okay so now we have our 29254 21:49:45,039 --> 21:49:50,551 constructor n and it's going to allocate 29255 21:49:48,160 --> 21:49:53,680 memory from the heap and we're going to 29256 21:49:50,551 --> 21:49:56,319 put data in that new memory location 29257 21:49:53,679 --> 21:49:58,551 that has just been allocated to our 29258 21:49:56,320 --> 21:50:00,160 class here so this is our class and we 29259 21:49:58,551 --> 21:50:02,800 can go in the main function and really 29260 21:50:00,160 --> 21:50:05,040 use it however we want for example we 29261 21:50:02,800 --> 21:50:07,360 can set up a dog object so we're going 29262 21:50:05,039 --> 21:50:09,360 to say my dog and we're going to give it 29263 21:50:07,360 --> 21:50:12,320 a name the name is going to be fluffy 29264 21:50:09,360 --> 21:50:14,232 and we're going to put the breed in and 29265 21:50:12,320 --> 21:50:16,232 it's going to just be a shepherd and 29266 21:50:14,232 --> 21:50:18,720 we're going to put in the age which is 29267 21:50:16,232 --> 21:50:20,800 going to be for example a 2. let's say 29268 21:50:20,800 --> 21:50:25,120 this is valid c plus plus code it's 29269 21:50:22,872 --> 21:50:27,832 going to be setting up an object of our 29270 21:50:25,119 --> 21:50:29,759 class here and we can world to see that 29271 21:50:27,831 --> 21:50:32,479 this program is going to world without a 29272 21:50:29,759 --> 21:50:34,000 problem so both is good but if we run 29273 21:50:32,479 --> 21:50:36,720 the program we're not going to see 29274 21:50:34,000 --> 21:50:38,551 anything but we don't really need to see 29275 21:50:36,720 --> 21:50:40,872 our constructor here called because we 29276 21:50:38,551 --> 21:50:43,191 already know about constructors what we 29277 21:50:40,872 --> 21:50:45,919 want to learn about are these structures 29278 21:50:43,191 --> 21:50:48,551 and these are spatial methods that are 29279 21:50:45,919 --> 21:50:51,119 called by the compiler when your object 29280 21:50:48,551 --> 21:50:54,080 dies let's set up a destructor and see 29281 21:50:51,119 --> 21:50:56,479 it called when our dog objects die the 29282 21:50:54,080 --> 21:50:58,400 syntax to do that is to use the tilde 29283 21:50:56,479 --> 21:51:00,080 character here you're going to say the 29284 21:50:58,399 --> 21:51:02,319 name of the class you're going to put an 29285 21:51:00,080 --> 21:51:04,720 empty parameter list and if you want you 29286 21:51:02,320 --> 21:51:07,040 can put a body and do whatever you want 29287 21:51:04,720 --> 21:51:10,232 to do in the body of your destructor 29288 21:51:07,039 --> 21:51:13,119 here but again we're going to move the 29289 21:51:10,232 --> 21:51:15,919 entire definition of the destructor to 29290 21:51:13,119 --> 21:51:17,599 the outside so let's go down and save 29291 21:51:15,919 --> 21:51:19,279 that we're going to say the scope 29292 21:51:17,600 --> 21:51:21,920 resolution operator we're going to say 29293 21:51:19,279 --> 21:51:24,720 dog and we're going to put in our body 29294 21:51:21,919 --> 21:51:27,919 and the first thing we want to do in our 29295 21:51:24,720 --> 21:51:31,279 destructor is to release this memory 29296 21:51:27,919 --> 21:51:33,440 that was dynamically allocated for us if 29297 21:51:31,279 --> 21:51:35,831 we don't release it when this dark 29298 21:51:33,440 --> 21:51:37,832 object dies we're going to link memory 29299 21:51:35,831 --> 21:51:39,679 and that's really bad so let's do that 29300 21:51:37,831 --> 21:51:42,871 directly in the destructor we can say 29301 21:51:39,679 --> 21:51:45,439 delete and say ph this is going to 29302 21:51:42,872 --> 21:51:48,080 release the memory that was allocated in 29303 21:51:45,440 --> 21:51:51,119 the constructor here and we have access 29304 21:51:48,080 --> 21:51:54,320 to this ph variable because the 29305 21:51:51,119 --> 21:51:56,639 destructor is part of our class 29306 21:51:54,320 --> 21:51:58,640 any member function of your class is 29307 21:51:56,639 --> 21:52:00,960 going to have access to any member 29308 21:51:58,639 --> 21:52:02,080 variable even if it happens to be 29309 21:52:02,080 --> 21:52:07,600 okay so we forgot our tilde character 29310 21:52:05,039 --> 21:52:10,079 let's put that in here and this is going 29311 21:52:07,600 --> 21:52:12,480 to be valid c plus plus code but to 29312 21:52:10,080 --> 21:52:14,800 really see things happening here let's 29313 21:52:12,479 --> 21:52:17,279 put in an sddc out statement we're going 29314 21:52:14,800 --> 21:52:19,040 to say doug destructor called 4 and 29315 21:52:17,279 --> 21:52:21,039 we're going to say the name of the dog 29316 21:52:19,039 --> 21:52:23,191 let's say name and we're going to say 29317 21:52:21,039 --> 21:52:26,159 the same thing in the constructor we're 29318 21:52:23,191 --> 21:52:29,279 going to say hddcl and say dog 29319 21:52:26,160 --> 21:52:31,360 constructor cold for and we're going to 29320 21:52:29,279 --> 21:52:33,679 say the dog name going to use our member 29321 21:52:31,360 --> 21:52:36,479 variable and now we have our thing in 29322 21:52:33,679 --> 21:52:39,119 place we have the destructor declared in 29323 21:52:36,479 --> 21:52:41,191 the class definition and this is the 29324 21:52:39,119 --> 21:52:43,191 syntax again this is what we really want 29325 21:52:41,191 --> 21:52:45,440 to learn about in this lecture we have 29326 21:52:43,191 --> 21:52:48,479 the tilde character we have the class 29327 21:52:45,440 --> 21:52:50,800 name and we put an empty parameter list 29328 21:52:48,479 --> 21:52:53,759 let me tell you that it is not possible 29329 21:52:50,800 --> 21:52:54,872 to pass parameters to your destructor if 29330 21:52:53,759 --> 21:52:57,360 you do that you're going to get a 29331 21:52:54,872 --> 21:53:00,872 compiler error the structures will 29332 21:52:57,360 --> 21:53:03,512 always have no parameters in c plus plus 29333 21:53:00,872 --> 21:53:05,760 here we have the definition of our 29334 21:53:03,512 --> 21:53:07,760 destructor and it is going to release 29335 21:53:05,759 --> 21:53:10,479 whatever memory we have dynamically 29336 21:53:07,759 --> 21:53:12,399 allocated from the constructor here if 29337 21:53:12,399 --> 21:53:16,551 we're going to see our constructor 29338 21:53:14,000 --> 21:53:19,512 called as a result of this line here 29339 21:53:16,551 --> 21:53:20,959 because we are constructing a dog object 29340 21:53:19,512 --> 21:53:22,872 and we're going to see the destructor 29341 21:53:20,960 --> 21:53:24,000 called when the main function is about 29342 21:53:24,000 --> 21:53:28,320 let's put an stdc out state to mention 29343 21:53:26,160 --> 21:53:31,040 the main function here and say done just 29344 21:53:28,320 --> 21:53:32,720 to prove that we have hit the end of our 29345 21:53:31,039 --> 21:53:35,279 main function here i think this is 29346 21:53:32,720 --> 21:53:37,919 useful and we're going to build so let's 29347 21:53:35,279 --> 21:53:39,440 do that we're going to build with gcc 29348 21:53:37,919 --> 21:53:42,080 you're going to see that the world is 29349 21:53:39,440 --> 21:53:44,872 good so we can clear and run or program 29350 21:53:42,080 --> 21:53:46,720 let's hit enter here and clear and run 29351 21:53:44,872 --> 21:53:49,192 rooster you're going to see that the 29352 21:53:46,720 --> 21:53:52,000 constructor was called we are going to 29353 21:53:49,191 --> 21:53:54,871 say done when the main function is about 29354 21:53:52,000 --> 21:53:57,831 to end but before the main function ends 29355 21:53:54,872 --> 21:53:59,919 we're going to destroy any stack data 29356 21:53:57,831 --> 21:54:02,720 that we have in the main function and 29357 21:53:59,919 --> 21:54:05,039 our dog object is on the stats so it's 29358 21:54:02,720 --> 21:54:07,600 going to be destroyed as part of that 29359 21:54:05,039 --> 21:54:09,511 process and the destructor for dog is 29360 21:54:07,600 --> 21:54:11,512 going to be called and we're going to 29361 21:54:09,512 --> 21:54:13,760 release our memory and we're going to 29362 21:54:11,512 --> 21:54:15,119 say that dog destructor was called for 29363 21:54:15,119 --> 21:54:21,440 we have assigned to our dog here so this 29364 21:54:18,320 --> 21:54:23,512 is how destructors are called when your 29365 21:54:23,512 --> 21:54:29,832 and we didn't put in the correct name 29366 21:54:26,960 --> 21:54:31,600 we said fluffy let's say fluffy because 29367 21:54:29,831 --> 21:54:33,679 that's what i wanted and now you see 29368 21:54:31,600 --> 21:54:35,920 that our destructor is really being 29369 21:54:33,679 --> 21:54:38,231 called and releasing memory however we 29370 21:54:35,919 --> 21:54:40,872 want but we can also go a little further 29371 21:54:38,232 --> 21:54:42,872 and show you another example of a case 29372 21:54:40,872 --> 21:54:45,360 where the structures are called and 29373 21:54:42,872 --> 21:54:47,760 we're going to look at a function that 29374 21:54:45,360 --> 21:54:49,600 sets up a local object of a dog we're 29375 21:54:47,759 --> 21:54:51,919 going to see the dog constructed and 29376 21:54:49,600 --> 21:54:54,552 we're going to see it destroyed when the 29377 21:54:51,919 --> 21:54:56,551 function is about to end let's do that 29378 21:54:54,551 --> 21:54:58,551 we're going to say void some funk and 29379 21:54:56,551 --> 21:55:00,720 it's going to be just some function to 29380 21:54:58,551 --> 21:55:04,159 play with us we're going to 29381 21:55:00,720 --> 21:55:05,191 move our dog declaration in some funk 29382 21:55:05,191 --> 21:55:08,720 and this is really it we're going to 29383 21:55:08,720 --> 21:55:12,160 and when we call it we're going to 29384 21:55:10,080 --> 21:55:14,400 construct the object here when the 29385 21:55:12,160 --> 21:55:15,919 function is about to end the compiler is 29386 21:55:14,399 --> 21:55:18,159 going to call the destructor because 29387 21:55:15,919 --> 21:55:19,599 it's going to destroy this object here 29388 21:55:18,160 --> 21:55:22,552 and we're going to see the destructor 29389 21:55:19,600 --> 21:55:24,480 called by that point the some funk 29390 21:55:22,551 --> 21:55:26,399 function is going to be popped off the 29391 21:55:24,479 --> 21:55:27,919 top of the stack we're going to hit the 29392 21:55:26,399 --> 21:55:30,799 main function here and we're going to 29393 21:55:27,919 --> 21:55:32,872 say done and the program is going to end 29394 21:55:30,800 --> 21:55:34,720 let's call some funky name here so we're 29395 21:55:32,872 --> 21:55:37,440 going to say some funk and we're going 29396 21:55:34,720 --> 21:55:39,831 to world to really see this happening so 29397 21:55:37,440 --> 21:55:41,919 we have worked successfully we can clear 29398 21:55:39,831 --> 21:55:44,000 clear properly and run rooster we're 29399 21:55:41,919 --> 21:55:46,399 going to see constructor called for 29400 21:55:44,000 --> 21:55:48,720 fluffy the structure called for fluffy 29401 21:55:46,399 --> 21:55:51,439 and you see done we have hit the main 29402 21:55:48,720 --> 21:55:54,000 function and really finished executing 29403 21:55:51,440 --> 21:55:56,080 our program here we can even see this 29404 21:55:54,000 --> 21:55:59,191 through the debugger we're going to set 29405 21:55:56,080 --> 21:56:01,360 up three break points one in the main 29406 21:55:59,191 --> 21:56:03,191 function here before we call the some 29407 21:56:01,360 --> 21:56:06,232 funk function we're going to set up 29408 21:56:03,191 --> 21:56:08,319 another one in our destructor before we 29409 21:56:06,232 --> 21:56:11,040 print the message here and we're going 29410 21:56:08,320 --> 21:56:13,360 to put another one in the constructor 29411 21:56:11,039 --> 21:56:16,479 when we are about to print this message 29412 21:56:13,360 --> 21:56:19,279 here on line 20. after we set up these 29413 21:56:16,479 --> 21:56:22,479 break points we kind of kill the 29414 21:56:19,279 --> 21:56:24,800 terminal here and minimize a little bit 29415 21:56:22,479 --> 21:56:27,440 let's kill these things here so that we 29416 21:56:24,800 --> 21:56:30,479 have a clean ah we have a lot of windows 29417 21:56:27,440 --> 21:56:34,160 here okay we have our editor here 29418 21:56:30,479 --> 21:56:37,119 and now if we go to the tab here that 29419 21:56:34,160 --> 21:56:39,440 says start or run we're going to be able 29420 21:56:37,119 --> 21:56:42,080 to click the green button here to start 29421 21:56:39,440 --> 21:56:44,320 our debugging session we are going to 29422 21:56:42,080 --> 21:56:46,400 have a terminal here so let's bring that 29423 21:56:44,320 --> 21:56:49,760 to the side so that we can see things 29424 21:56:46,399 --> 21:56:52,159 printed out nicely let's do like this 29425 21:56:49,759 --> 21:56:54,639 and now we have hit the break point in 29426 21:56:52,160 --> 21:56:56,800 the main function we can step into 29427 21:56:54,639 --> 21:56:59,759 the sum func function and we're going to 29428 21:56:56,800 --> 21:57:01,440 do that using the step into button here 29429 21:56:59,759 --> 21:57:03,511 let's click that we're going to jump 29430 21:57:03,512 --> 21:57:09,512 now we have the option to step into but 29431 21:57:06,160 --> 21:57:12,552 this is going to jump us into the logic 29432 21:57:09,512 --> 21:57:15,040 of transforming the string literal here 29433 21:57:12,551 --> 21:57:16,959 into a string view because if you see 29434 21:57:15,039 --> 21:57:19,191 the constructor is going to take string 29435 21:57:16,960 --> 21:57:20,400 view parameters we can actually see this 29436 21:57:20,399 --> 21:57:24,871 and hit the step into button you see 29437 21:57:22,232 --> 21:57:27,512 that we are in the string view class and 29438 21:57:24,872 --> 21:57:30,872 we don't really want this so let's step 29439 21:57:27,512 --> 21:57:32,960 out and we're going to step over this 29440 21:57:30,872 --> 21:57:35,040 so that we can hit the next breakpoint 29441 21:57:32,960 --> 21:57:38,400 and you see the next breakpoint is in 29442 21:57:35,039 --> 21:57:39,759 our constructor and this proves that the 29443 21:57:39,759 --> 21:57:45,039 constructor here to build our object 29444 21:57:42,800 --> 21:57:47,600 if we step over this we're going to 29445 21:57:45,039 --> 21:57:50,000 print the message from the constructor 29446 21:57:47,600 --> 21:57:51,120 and if we keep going we're going to get 29447 21:57:51,119 --> 21:57:55,679 our constructor and you see where this 29448 21:57:54,000 --> 21:57:58,479 is going to go this is going to go in 29449 21:57:55,679 --> 21:58:01,511 the destructor because before we leave 29450 21:57:58,479 --> 21:58:03,599 the some func function our dot objects 29451 21:58:01,512 --> 21:58:05,832 is going to be destroyed so we're going 29452 21:58:03,600 --> 21:58:08,800 to release our memory and we're going to 29453 21:58:05,831 --> 21:58:12,000 branch the message from our constructor 29454 21:58:08,800 --> 21:58:14,080 and if we hit next and a step out of 29455 21:58:12,000 --> 21:58:16,399 this i think we can do that because this 29456 21:58:14,080 --> 21:58:19,919 is logic we don't want we're going to 29457 21:58:16,399 --> 21:58:22,000 try and step over this and now if we hit 29458 21:58:19,919 --> 21:58:24,080 next we're going to come back in some 29459 21:58:22,000 --> 21:58:26,320 funk and some funk is already going to 29460 21:58:24,080 --> 21:58:27,831 be finished and if we hit next we're 29461 21:58:26,320 --> 21:58:30,320 going to come back in main and we're 29462 21:58:27,831 --> 21:58:32,720 going to print done and the program is 29463 21:58:30,320 --> 21:58:34,080 basically going to be done hopefully you 29464 21:58:34,080 --> 21:58:39,360 the compiler is going to call our 29465 21:58:36,551 --> 21:58:41,512 destructor after it leaves some funk 29466 21:58:39,360 --> 21:58:44,320 here because it's going to be destroying 29467 21:58:41,512 --> 21:58:46,720 the local object that we have inside 29468 21:58:44,320 --> 21:58:48,640 some funk here so this is one case where 29469 21:58:46,720 --> 21:58:51,512 the destructor is going to be called by 29470 21:58:48,639 --> 21:58:54,720 the compiler and it is if you have a 29471 21:58:51,512 --> 21:58:57,279 local object that needs to be destroyed 29472 21:58:54,720 --> 21:59:00,232 when function scope finishes another 29473 21:58:57,279 --> 21:59:02,000 case where the destructor may be called 29474 21:59:04,399 --> 21:59:09,511 parameter by value so let's change some 29475 21:59:07,512 --> 21:59:11,512 function here and make it take something 29476 21:59:09,512 --> 21:59:14,479 by value so we're going to pass a dog 29477 21:59:11,512 --> 21:59:16,479 inside and we're going to say dog param 29478 21:59:14,479 --> 21:59:18,959 this is going to do okay now that we 29479 21:59:16,479 --> 21:59:21,039 have changed some func here we can 29480 21:59:18,960 --> 21:59:22,960 really do all kinds of crazy things with 29481 21:59:21,039 --> 21:59:26,000 it for example we can set up our dog 29482 21:59:22,960 --> 21:59:29,440 object inside our main function here so 29483 21:59:26,000 --> 21:59:31,600 let's do that we can say dog and dog the 29484 21:59:29,440 --> 21:59:32,872 variable name is going to be dog we can 29485 21:59:32,872 --> 21:59:37,440 a name which is going to be fluffy it's 29486 21:59:35,191 --> 21:59:39,599 going to have a breed of shepherd and we 29487 21:59:37,440 --> 21:59:42,080 can give it the age of 2 like we have 29488 21:59:39,600 --> 21:59:45,279 been doing all along we can call our 29489 21:59:42,080 --> 21:59:47,191 function with the dog object here but be 29490 21:59:45,279 --> 21:59:49,360 careful about what is going to happen 29491 21:59:47,191 --> 21:59:51,679 here when we do something like this 29492 21:59:49,360 --> 21:59:53,600 we're going to be passing by value and 29493 21:59:51,679 --> 21:59:56,719 the compiler is going to see that it 29494 21:59:56,720 --> 22:00:03,119 now we haven't really told our class 29495 21:59:59,679 --> 22:00:05,359 here how to copy dog objects and we do 29496 22:00:03,119 --> 22:00:07,679 that through constructors we call copy 29497 22:00:05,360 --> 22:00:09,512 constructors we haven't really learned 29498 22:00:07,679 --> 22:00:12,159 about yet but what the compiler is going 29499 22:00:09,512 --> 22:00:14,720 to do by default is copy these things 29500 22:00:12,160 --> 22:00:16,872 member by member so it's going to set up 29501 22:00:14,720 --> 22:00:20,000 a doug copy object which is going to be 29502 22:00:16,872 --> 22:00:22,232 initialized with empty strings and a 29503 22:00:20,000 --> 22:00:25,119 null pointer here and then it's going to 29504 22:00:25,119 --> 22:00:29,599 that we have here it's going to take the 29505 22:00:27,191 --> 22:00:31,919 name and copy that in the name member 29506 22:00:29,600 --> 22:00:34,480 variable of our dark copy it's going to 29507 22:00:31,919 --> 22:00:36,551 take the breed and copy that n in the 29508 22:00:34,479 --> 22:00:38,959 second member variable here and it's 29509 22:00:36,551 --> 22:00:41,119 going to take the age from our dog and 29510 22:00:38,960 --> 22:00:42,960 copy that in the problem is going to 29511 22:00:41,119 --> 22:00:45,599 come to this pointer here it's going to 29512 22:00:42,960 --> 22:00:47,920 copy the address and it's not going to 29513 22:00:45,600 --> 22:00:50,080 copy what it's pointed to and this is 29514 22:00:47,919 --> 22:00:51,679 going to be a recipe for a disaster i 29515 22:00:50,080 --> 22:00:54,080 don't really want to go through this 29516 22:00:51,679 --> 22:00:56,159 right now but if we try to run this 29517 22:00:54,080 --> 22:00:58,960 program it's not going to work like we 29518 22:00:56,160 --> 22:01:01,192 expect for example if we pass fluffy 29519 22:01:01,191 --> 22:01:06,720 things that we don't expect so let's try 29520 22:01:03,679 --> 22:01:09,679 to build we're going to build with gcc 29521 22:01:06,720 --> 22:01:11,919 and the bolt is good so what we expect 29522 22:01:09,679 --> 22:01:14,159 is to have the constructor of fluffy 29523 22:01:11,919 --> 22:01:17,360 here called and we're going to copy the 29524 22:01:14,160 --> 22:01:20,000 dot object for use in some funk here and 29525 22:01:17,360 --> 22:01:21,831 to set up this copy it's not this 29526 22:01:20,000 --> 22:01:22,872 constructor here that is going to be 29527 22:01:22,872 --> 22:01:26,551 so it's not our constructor here which 29528 22:01:24,960 --> 22:01:27,600 is going to be called so we're not going 29529 22:01:27,600 --> 22:01:32,720 this called for the copy that we set up 29530 22:01:30,320 --> 22:01:35,512 inside some funk another thing is 29531 22:01:32,720 --> 22:01:37,831 because now these two dog objects are 29532 22:01:35,512 --> 22:01:41,440 pointing to the same thing because what 29533 22:01:37,831 --> 22:01:44,159 we just copied is the address as we see 29534 22:01:41,440 --> 22:01:46,000 in our member variable here the two dog 29535 22:01:44,160 --> 22:01:48,400 objects are going to be pointing to the 29536 22:01:46,000 --> 22:01:50,551 same thing in memory and when we hit the 29537 22:01:48,399 --> 22:01:52,639 destructor we're going to call delete 29538 22:01:50,551 --> 22:01:55,039 twice and this is going to be a recipe 29539 22:01:52,639 --> 22:01:57,191 for disaster our application could 29540 22:01:55,039 --> 22:02:00,000 really crash you already know that 29541 22:01:57,191 --> 22:02:02,080 deleting a pointer twice is really bad 29542 22:02:00,000 --> 22:02:04,000 we shouldn't do this and we are doing 29543 22:02:02,080 --> 22:02:06,400 this here because we are copying i 29544 22:02:04,000 --> 22:02:07,440 realize this is a slightly confusing 29545 22:02:07,440 --> 22:02:11,919 but what i want you to know is that if 29546 22:02:09,600 --> 22:02:12,720 you pass a class object by value like 29547 22:02:12,720 --> 22:02:18,160 the copy you have inside is going to 29548 22:02:15,360 --> 22:02:20,639 cause for the destructor to be called 29549 22:02:18,160 --> 22:02:22,320 when the function exits because the copy 29550 22:02:20,639 --> 22:02:24,639 is going to be scoped inside the 29551 22:02:22,320 --> 22:02:26,640 function here so let's take out this 29552 22:02:24,639 --> 22:02:28,720 example because it really is confusing 29553 22:02:26,639 --> 22:02:30,720 but i hope you know that and i am going 29554 22:02:28,720 --> 22:02:32,960 to show you that another case where you 29555 22:02:30,720 --> 22:02:37,191 might see the destructor called is when 29556 22:02:32,960 --> 22:02:39,680 you call the destructor explicitly using 29557 22:02:37,191 --> 22:02:41,919 the delete operator on your object so we 29558 22:02:39,679 --> 22:02:44,079 can set up some func again let's do that 29559 22:02:41,919 --> 22:02:45,679 some funk and we're going to repurpose 29560 22:02:44,080 --> 22:02:48,960 it and inside we're going to set up a 29561 22:02:45,679 --> 22:02:51,511 dag object so let's grab this and reuse 29562 22:02:48,960 --> 22:02:53,832 that in our function here and we're 29563 22:02:51,512 --> 22:02:56,800 going to say dog pointer so let's say 29564 22:02:53,831 --> 22:02:59,511 dog pointer we're going to say p dog p 29565 22:02:56,800 --> 22:03:02,400 underscore dog it's going to be equals 29566 22:02:59,512 --> 22:03:04,720 to new dog and we're going to pass in 29567 22:03:02,399 --> 22:03:07,039 what we need to initialize our dog 29568 22:03:04,720 --> 22:03:09,040 object with this is going to create a 29569 22:03:07,039 --> 22:03:11,759 dark object on the hip and it is going 29570 22:03:09,039 --> 22:03:14,159 to be pointed to by p dog here 29571 22:03:11,759 --> 22:03:17,119 now i want you to see that if we don't 29572 22:03:14,160 --> 22:03:20,000 explicitly release the memory that we 29573 22:03:17,119 --> 22:03:22,319 allocate through the new operator we're 29574 22:03:20,000 --> 22:03:24,960 going to leak memory so we created an 29575 22:03:22,320 --> 22:03:27,440 object on the hip here but we're not 29576 22:03:24,960 --> 22:03:29,280 deleting that in some funk when some 29577 22:03:27,440 --> 22:03:31,600 funk exits we're going to lose this 29578 22:03:29,279 --> 22:03:34,160 pointer this variable because it is on 29579 22:03:31,600 --> 22:03:36,232 the stack itself and we won't have any 29580 22:03:34,160 --> 22:03:38,320 way to release this memory let's call 29581 22:03:36,232 --> 22:03:40,000 some funk here and really show you this 29582 22:03:38,320 --> 22:03:42,480 so we're going to call some funk and 29583 22:03:40,000 --> 22:03:44,872 we're going to build let's use gcc for 29584 22:03:42,479 --> 22:03:47,279 that the build is good we can bring this 29585 22:03:44,872 --> 22:03:48,960 up a little bit and clear and run 29586 22:03:47,279 --> 22:03:51,279 rooster we're going to see that the 29587 22:03:48,960 --> 22:03:54,000 constructor for fluffy was called 29588 22:03:51,279 --> 22:03:55,679 because of this new operator but we 29589 22:03:54,000 --> 22:03:57,191 didn't see the destructor called and 29590 22:03:57,191 --> 22:04:02,080 so for the destructor to be called we 29591 22:03:59,440 --> 22:04:04,720 need to explicitly release the memory in 29592 22:04:02,080 --> 22:04:07,512 this function here and we do that by 29593 22:04:04,720 --> 22:04:09,919 using the delete keyword and saying the 29594 22:04:07,512 --> 22:04:12,232 pointer that is pointing to the memory 29595 22:04:09,919 --> 22:04:14,319 that we want to release if we do this 29596 22:04:12,232 --> 22:04:16,960 this is going to cause for the the 29597 22:04:14,320 --> 22:04:18,720 structure of doubt to be called and if 29598 22:04:16,960 --> 22:04:20,480 we run we're going to see our destructor 29599 22:04:18,720 --> 22:04:23,119 called let's do that we're going to use 29600 22:04:20,479 --> 22:04:25,119 gcc the bolt is good you can clear and 29601 22:04:23,119 --> 22:04:27,440 run rooster now you see that the 29602 22:04:25,119 --> 22:04:30,639 constructor is called full fluffy and we 29603 22:04:27,440 --> 22:04:32,551 have the destructor cold this is really 29604 22:04:30,639 --> 22:04:35,191 all i wanted to share with you in this 29605 22:04:32,551 --> 22:04:37,679 lecture i apologize if you found it 29606 22:04:35,191 --> 22:04:40,551 confusing but if you have any problem i 29607 22:04:37,679 --> 22:04:42,871 am always available please do ask and i 29608 22:04:40,551 --> 22:04:45,599 will do the best i can to help you out 29609 22:04:42,872 --> 22:04:47,600 so the destructor is a special method 29610 22:04:45,600 --> 22:04:50,080 which is going to be called when your 29611 22:04:47,600 --> 22:04:52,320 object dies it is going to be called 29612 22:04:50,080 --> 22:04:54,479 when an object is passed by a value to a 29613 22:04:52,320 --> 22:04:56,400 function we have seen an example that 29614 22:04:54,479 --> 22:04:58,479 it's going to be called when a local 29615 22:04:56,399 --> 22:05:00,871 object is returned from a function by 29616 22:04:58,479 --> 22:05:02,872 value but this is going to be compiler 29617 22:05:00,872 --> 22:05:05,512 dependent because some compilers are 29618 22:05:02,872 --> 22:05:08,080 going to optimize this out and make it 29619 22:05:05,512 --> 22:05:10,160 look like when you returned by reference 29620 22:05:08,080 --> 22:05:13,360 so i don't want to go into this because 29621 22:05:10,160 --> 22:05:15,279 this is subject to compiler optimization 29622 22:05:13,360 --> 22:05:17,680 but other cases where you're going to 29623 22:05:15,279 --> 22:05:20,720 have your destructor called is when a 29624 22:05:17,679 --> 22:05:23,511 local stack object goes out of scope and 29625 22:05:23,512 --> 22:05:27,680 through the delete key world this is 29626 22:05:25,919 --> 22:05:30,000 really all i had to share in this 29627 22:05:27,679 --> 22:05:32,000 lecture i hope you found it interesting 29628 22:05:30,000 --> 22:05:33,679 we are going to stop here in this one 29629 22:05:32,000 --> 22:05:36,479 and the next one we're going to see the 29630 22:05:33,679 --> 22:05:38,639 order in which constructors and these 29631 22:05:36,479 --> 22:05:40,800 structures are called and you really 29632 22:05:44,720 --> 22:05:50,000 and this lecture i wanted to raise your 29633 22:05:47,360 --> 22:05:52,479 awareness on the order in which 29634 22:05:50,000 --> 22:05:54,872 constructors and the structures are 29635 22:05:52,479 --> 22:05:57,599 called suppose we have our class which 29636 22:05:54,872 --> 22:05:59,832 is dug here and we have the constructor 29637 22:05:57,600 --> 22:06:02,320 which is doing its thing in the body 29638 22:05:59,831 --> 22:06:04,399 here and we have the destructor which is 29639 22:06:02,320 --> 22:06:07,120 releasing our memory and printing 29640 22:06:04,399 --> 22:06:08,799 something out so suppose we have created 29641 22:06:08,800 --> 22:06:13,279 of this class here and they have 29642 22:06:10,872 --> 22:06:15,512 different names and they have the same 29643 22:06:13,279 --> 22:06:16,399 breed and different edges as you see 29644 22:06:16,399 --> 22:06:21,679 and if we run this code in main 29645 22:06:19,360 --> 22:06:24,551 what do you think will see printed from 29646 22:06:21,679 --> 22:06:26,871 the constructors and the destructors if 29647 22:06:24,551 --> 22:06:28,872 you asked me and i was learning about c 29648 22:06:26,872 --> 22:06:30,551 plus plus i would say that the 29649 22:06:28,872 --> 22:06:33,440 constructors are going to be called in 29650 22:06:30,551 --> 22:06:35,360 order dog one dog two dog three dog four 29651 22:06:33,440 --> 22:06:37,760 they are going to be constructed in this 29652 22:06:35,360 --> 22:06:40,399 order and the destructors would be 29653 22:06:37,759 --> 22:06:42,720 called in the same order but this is not 29654 22:06:40,399 --> 22:06:45,599 true the destructors are going to be 29655 22:06:42,720 --> 22:06:47,119 called in the reverse order as you see 29656 22:06:47,119 --> 22:06:52,551 so we're going to call this constructor 29657 22:06:48,720 --> 22:06:55,191 for doug d1 delegate2w3 doge4 which is 29658 22:06:52,551 --> 22:06:57,279 exactly what i expected but you're going 29659 22:06:55,191 --> 22:07:00,399 to see that the destructors are going to 29660 22:06:57,279 --> 22:07:02,959 be called in reverse order so the object 29661 22:07:02,960 --> 22:07:07,680 is going to be destroyed first as you 29662 22:07:05,512 --> 22:07:09,600 see here and the object that was 29663 22:07:07,679 --> 22:07:10,799 constructed first is going to be 29664 22:07:10,800 --> 22:07:16,551 last and the compiler follows this order 29665 22:07:14,232 --> 22:07:19,600 to make it easy for it to do things we 29666 22:07:16,551 --> 22:07:21,191 can't really get into those details but 29667 22:07:19,600 --> 22:07:23,120 for now we're going to head over to 29668 22:07:21,191 --> 22:07:25,679 visual studio code and play with us a 29669 22:07:23,119 --> 22:07:28,159 little more okay here we are in our 29670 22:07:25,679 --> 22:07:30,959 working folder the current project is 29671 22:07:28,160 --> 22:07:32,872 order of constructor and destructor 29672 22:07:30,960 --> 22:07:35,280 calls here we're going to grab our 29673 22:07:32,872 --> 22:07:37,832 template project pretty quick 29674 22:07:35,279 --> 22:07:39,831 and we're going to put them in place the 29675 22:07:37,831 --> 22:07:41,919 files i mean and we're going to open 29676 22:07:39,831 --> 22:07:44,319 this in visual studio code like we have 29677 22:07:41,919 --> 22:07:47,440 been doing all along this is going to 29678 22:07:44,320 --> 22:07:50,872 open up our folder we can close the left 29679 22:07:47,440 --> 22:07:53,119 sidebar here and let's clean up our main 29680 22:07:50,872 --> 22:07:55,919 function we are going to bring in the 29681 22:07:53,119 --> 22:07:58,080 class for dog from the last lecture this 29682 22:07:58,080 --> 22:08:02,720 no change and we're going to include the 29683 22:08:00,639 --> 22:08:04,960 string review library for this to work 29684 22:08:02,720 --> 22:08:06,960 so we're going to include string view 29685 22:08:04,960 --> 22:08:09,120 here and once we do this we're going to 29686 22:08:06,960 --> 22:08:10,552 head over in the main function and set 29687 22:08:10,551 --> 22:08:15,599 objects of the dog class here but before 29688 22:08:13,440 --> 22:08:18,872 we go there notice that we have a few 29689 22:08:15,600 --> 22:08:21,360 htdc out statements in our constructor 29690 22:08:18,872 --> 22:08:23,680 and destructor and we want to see the 29691 22:08:21,360 --> 22:08:26,479 order in which they are called so we're 29692 22:08:23,679 --> 22:08:28,719 going to set up four objects of our dog 29693 22:08:26,479 --> 22:08:32,080 we're going to say dog dog one and we're 29694 22:08:28,720 --> 22:08:34,720 going to say the name to be won and it's 29695 22:08:32,080 --> 22:08:37,279 going to be a shepherd and it's going to 29696 22:08:34,720 --> 22:08:39,512 be for example 2 years old why not we 29697 22:08:37,279 --> 22:08:41,831 should say dog here and we are going to 29698 22:08:39,512 --> 22:08:44,000 copy these four times here and we're 29699 22:08:44,000 --> 22:08:48,551 the other is going to be dog 3 and this 29700 22:08:48,551 --> 22:08:52,000 and i'm going to say the same thing here 29701 22:08:54,399 --> 22:08:58,319 the names are what is really important 29702 22:08:56,639 --> 22:09:00,319 because we're going to see them printed 29703 22:08:58,320 --> 22:09:03,280 out here but if you want you can change 29704 22:09:00,320 --> 22:09:05,680 the ages let's make this a three let's 29705 22:09:03,279 --> 22:09:07,919 make this a five let's make this a one 29706 22:09:05,679 --> 22:09:10,639 why not for dougie for and now we're 29707 22:09:07,919 --> 22:09:12,080 going to run our program if we run it 29708 22:09:10,639 --> 22:09:14,960 we're going to see the constructors 29709 22:09:12,080 --> 22:09:18,000 called in the order doggy one doggie 2 29710 22:09:14,960 --> 22:09:20,232 w3 dog e4 and the destructors are going 29711 22:09:18,000 --> 22:09:21,440 to be called in the reverse order 29712 22:09:21,440 --> 22:09:26,160 all the way to doggy one let's boil and 29713 22:09:23,831 --> 22:09:28,639 show you that this is actually the case 29714 22:09:26,160 --> 22:09:31,600 so the build is good we can bring up a 29715 22:09:28,639 --> 22:09:34,000 terminal and really run this let's clear 29716 22:09:31,600 --> 22:09:36,400 and run rooster you're going to see that 29717 22:09:34,000 --> 22:09:38,720 dog constructor called for a doki one 29718 22:09:36,399 --> 22:09:41,759 all the way to dog e4 and now we have 29719 22:09:38,720 --> 22:09:44,639 the dagger destructor called from doge4 29720 22:09:41,759 --> 22:09:47,119 all the way to doge one and the compiler 29721 22:09:44,639 --> 22:09:49,831 is going to enforce this order for 29722 22:09:47,119 --> 22:09:52,479 calling constructors and destructors 29723 22:09:49,831 --> 22:09:55,511 because it makes it easy for it to 29724 22:09:52,479 --> 22:09:58,159 manage the memory of objects for example 29725 22:09:55,512 --> 22:10:00,800 if duck one has been around for a long 29726 22:09:58,160 --> 22:10:03,040 time and it has because it's going to be 29727 22:10:06,080 --> 22:10:11,759 after dog four here there might be other 29728 22:10:09,119 --> 22:10:15,360 objects that depend on dog one so that's 29729 22:10:11,759 --> 22:10:18,319 why the object that was created the last 29730 22:10:15,360 --> 22:10:21,191 is deleted first because chances for 29731 22:10:18,320 --> 22:10:23,680 other objects to depend on that last 29732 22:10:21,191 --> 22:10:26,159 object are really slim and you can 29733 22:10:23,679 --> 22:10:29,279 delete it without any problem but the 29734 22:10:26,160 --> 22:10:31,192 object that was created first might have 29735 22:10:29,279 --> 22:10:34,720 other dependencies and you're going to 29736 22:10:31,191 --> 22:10:36,799 be deleting dependent object first 29737 22:10:34,720 --> 22:10:39,191 before you delete that object i think 29738 22:10:36,800 --> 22:10:41,512 this is the logic the compiler follows 29739 22:10:39,191 --> 22:10:43,599 to really enforce this order here and 29740 22:10:41,512 --> 22:10:46,320 you need to be aware of that and make 29741 22:10:43,600 --> 22:10:49,192 sure that at any time in your c-plus 29742 22:10:46,320 --> 22:10:51,680 plus application this is working to your 29743 22:10:49,191 --> 22:10:53,440 advantage this is really all i had to 29744 22:10:51,679 --> 22:10:55,919 share in this lecture i hope you found 29745 22:10:53,440 --> 22:10:57,680 it interesting we are going to stop here 29746 22:10:55,919 --> 22:11:00,639 in this one and the next one we're going 29747 22:10:57,679 --> 22:11:03,439 to learn about the this pointer go ahead 29748 22:11:00,639 --> 22:11:04,872 and finish up here and meet me there 29749 22:11:03,440 --> 22:11:07,919 in this lecture we're going to learn 29750 22:11:04,872 --> 22:11:11,040 about the this pointer and this is a 29751 22:11:07,919 --> 22:11:14,160 special pointer that is maintained by c 29752 22:11:11,039 --> 22:11:17,191 plus plus to really help you manipulate 29753 22:11:14,160 --> 22:11:20,320 the current object and it is the current 29754 22:11:17,191 --> 22:11:23,512 object for which a given method is being 29755 22:11:20,320 --> 22:11:26,160 executed here is a simple example with 29756 22:11:23,512 --> 22:11:28,551 our dog class and we're going to show 29757 22:11:26,160 --> 22:11:30,000 some usefulness for the this pointer the 29758 22:11:28,551 --> 22:11:32,479 first thing we're going to look at is 29759 22:11:30,000 --> 22:11:35,600 the syntax for example here we might 29760 22:11:32,479 --> 22:11:38,231 need to print the address in memory 29761 22:11:35,600 --> 22:11:40,552 where the object was created and do that 29762 22:11:38,232 --> 22:11:42,320 from the inside of the function and to 29763 22:11:40,551 --> 22:11:44,639 do that we use the this pointer for 29764 22:11:42,320 --> 22:11:46,720 example we can put out a message and we 29765 22:11:44,639 --> 22:11:49,360 can do that through a statement like we 29766 22:11:46,720 --> 22:11:52,479 have here we are printing dog dog name 29767 22:11:49,360 --> 22:11:55,039 constructed at a given memory address 29768 22:11:52,479 --> 22:11:58,231 and we use the this keyword to get the 29769 22:11:55,039 --> 22:12:00,959 address of the character object for 29770 22:11:58,232 --> 22:12:03,360 which a specific method or function is 29771 22:12:00,960 --> 22:12:06,160 being called in this case it was the 29772 22:12:03,360 --> 22:12:08,160 constructor of the dot object here we 29773 22:12:06,160 --> 22:12:10,552 can also do the same thing for the other 29774 22:12:08,160 --> 22:12:13,760 constructor and you see we are using the 29775 22:12:10,551 --> 22:12:16,479 this pointer this is one of the uses you 29776 22:12:13,759 --> 22:12:18,959 have for the this pointer again the this 29777 22:12:16,479 --> 22:12:21,679 pointer is a special pointer that is 29778 22:12:18,960 --> 22:12:23,440 maintained by the c plus plus system and 29779 22:12:21,679 --> 22:12:26,639 it is going to be pointing to the 29780 22:12:23,440 --> 22:12:29,600 current object that is being manipulated 29781 22:12:26,639 --> 22:12:31,600 by the z plus plus run time and it's 29782 22:12:29,600 --> 22:12:35,120 going to be manipulated through a 29783 22:12:31,600 --> 22:12:37,279 special function of the class object 29784 22:12:35,119 --> 22:12:40,639 that is being called this is the meaning 29785 22:12:37,279 --> 22:12:43,919 here here is another use case for the 29786 22:12:40,639 --> 22:12:47,119 this pointer it may be used to resolve 29787 22:12:43,919 --> 22:12:49,360 conflicts when you have a parameter and 29788 22:12:47,119 --> 22:12:51,191 a member variable that are named the 29789 22:12:49,360 --> 22:12:54,232 same way for example here we are calling 29790 22:12:51,191 --> 22:12:56,871 the set name function it has a parameter 29791 22:12:54,232 --> 22:12:59,680 with a name doug name and it has a 29792 22:12:56,872 --> 22:13:02,160 member variable called dog name so if 29793 22:12:59,679 --> 22:13:04,159 you do a statement like we have on top 29794 22:13:02,160 --> 22:13:06,160 here the compiler is going to be 29795 22:13:04,160 --> 22:13:09,040 confused they're going to say dog name 29796 22:13:06,160 --> 22:13:11,512 equals dog name and it won't really know 29797 22:13:09,039 --> 22:13:14,639 which dog name you are referring to we 29798 22:13:11,512 --> 22:13:18,400 can resolve this conflict by saying 29799 22:13:14,639 --> 22:13:20,639 this and say pointer access notation 29800 22:13:18,399 --> 22:13:22,639 and say dog name and this thing here is 29801 22:13:20,639 --> 22:13:24,319 going to be the member variable and 29802 22:13:22,639 --> 22:13:27,191 we're going to be assigning to the 29803 22:13:24,320 --> 22:13:29,832 member variable from the parameter this 29804 22:13:27,191 --> 22:13:32,720 is the meaning here and we can use the 29805 22:13:29,831 --> 22:13:34,959 desperator to resolve such conflicts 29806 22:13:32,720 --> 22:13:37,680 another use we have for use pointer is 29807 22:13:34,960 --> 22:13:39,832 if we want to set up chained calls for 29808 22:13:37,679 --> 22:13:42,719 example using pointers and we might need 29809 22:13:39,831 --> 22:13:45,360 to do something like this so we have a 29810 22:13:42,720 --> 22:13:48,320 dog pointer set up on top here and we 29811 22:13:45,360 --> 22:13:50,232 can do something with our new object for 29812 22:13:48,320 --> 22:13:53,512 example we can call a function called 29813 22:13:50,232 --> 22:13:55,512 printful on a dog object after we print 29814 22:13:53,512 --> 22:13:57,760 the information we're going to set some 29815 22:13:55,512 --> 22:14:00,160 information on the doc and you see we 29816 22:13:57,759 --> 22:14:02,639 are setting all of the information 29817 22:14:00,160 --> 22:14:05,120 through one statement the way we achieve 29818 22:14:02,639 --> 22:14:08,000 this our setters are going to be 29819 22:14:05,119 --> 22:14:10,319 returning a pointer to the current 29820 22:14:08,000 --> 22:14:13,119 object and we're going to be grabbing 29821 22:14:13,119 --> 22:14:17,440 on it directly because it's a pointer 29822 22:14:15,119 --> 22:14:19,679 and we are going to be using the pointer 29823 22:14:17,440 --> 22:14:21,760 access notation so we're going to call 29824 22:14:19,679 --> 22:14:23,679 set name set name is going to return a 29825 22:14:21,759 --> 22:14:26,319 pointer we're going to use that pointer 29826 22:14:23,679 --> 22:14:28,000 to say to the breed and set the breed is 29827 22:14:26,320 --> 22:14:30,720 going to return a pointer we're going to 29828 22:14:28,000 --> 22:14:33,600 use that pointer to set the age and 29829 22:14:30,720 --> 22:14:35,440 after this entire line all the 29830 22:14:33,600 --> 22:14:37,920 information is going to change about our 29831 22:14:35,440 --> 22:14:39,919 dog and we can see that here and after 29832 22:14:37,919 --> 22:14:41,759 we do whatever we want with our dog 29833 22:14:39,919 --> 22:14:43,831 object we're going to release it from 29834 22:14:41,759 --> 22:14:46,871 memory so this is another thing you can 29835 22:14:43,831 --> 22:14:49,279 achieve using the this pointer and this 29836 22:14:46,872 --> 22:14:51,680 is how you can set up these setters it 29837 22:14:49,279 --> 22:14:53,759 is nothing complicated we have set name 29838 22:14:51,679 --> 22:14:56,639 it's going to return the dot pointer and 29839 22:14:53,759 --> 22:14:58,959 we're going to return this okay and this 29840 22:14:56,639 --> 22:15:02,232 is going to be a pointer or the memory 29841 22:14:58,960 --> 22:15:04,800 address of the current object of the dog 29842 22:15:02,232 --> 22:15:07,360 class we have the same setup for seti 29843 22:15:04,800 --> 22:15:10,160 dog breed we return the dog pointer and 29844 22:15:07,360 --> 22:15:13,191 we return this the same setup for set 29845 22:15:10,160 --> 22:15:14,160 doug age we return a dog pointer and we 29846 22:15:14,160 --> 22:15:19,832 this we can do the same thing using 29847 22:15:16,639 --> 22:15:22,399 references but our setup is going to be 29848 22:15:19,831 --> 22:15:25,279 somewhat a little bit different 29849 22:15:22,399 --> 22:15:27,831 we have our dog object here and we're 29850 22:15:25,279 --> 22:15:29,759 going to do our change call here so 29851 22:15:27,831 --> 22:15:30,959 we're going to say set name through the 29852 22:15:30,960 --> 22:15:35,360 because we have a dot pointer here but 29853 22:15:33,360 --> 22:15:38,479 after that we're going to return a 29854 22:15:35,360 --> 22:15:39,919 reference from the functions here so set 29855 22:15:38,479 --> 22:15:41,679 name is going to return a reference 29856 22:15:39,919 --> 22:15:43,440 we're going to go through that reference 29857 22:15:43,440 --> 22:15:47,600 set dog breed is going to return a 29858 22:15:45,679 --> 22:15:50,719 reference and we are going to go through 29859 22:15:47,600 --> 22:15:52,800 that reference to set the dog age and if 29860 22:15:50,720 --> 22:15:55,040 we print the information now we're going 29861 22:15:52,800 --> 22:15:57,832 to see that we have new information in 29862 22:15:55,039 --> 22:16:00,231 our dog object and after we are done 29863 22:15:57,831 --> 22:16:03,119 with our dog object we have to remember 29864 22:16:00,232 --> 22:16:06,400 to release it from memory because it was 29865 22:16:03,119 --> 22:16:09,039 allocated dynamically from the heap here 29866 22:16:06,399 --> 22:16:11,279 is how you can set this up it really is 29867 22:16:09,039 --> 22:16:13,759 nothing complicated we just have our 29868 22:16:11,279 --> 22:16:16,399 setters and we are going to return a 29869 22:16:13,759 --> 22:16:18,479 reference but before we return through 29870 22:16:16,399 --> 22:16:21,511 the desperator we will need to 29871 22:16:18,479 --> 22:16:24,231 dereference the displayer and get to the 29872 22:16:21,512 --> 22:16:26,551 actual thing that is being pointed to 29873 22:16:24,232 --> 22:16:28,800 that's why we have this star operator 29874 22:16:26,551 --> 22:16:30,080 here and for the other setters we're 29875 22:16:28,800 --> 22:16:31,832 going to do the same thing we're going 29876 22:16:30,080 --> 22:16:34,872 to return a reference we're going to 29877 22:16:31,831 --> 22:16:36,799 return a dereferenced version of this 29878 22:16:34,872 --> 22:16:38,551 we're going to return a dog reference 29879 22:16:36,800 --> 22:16:41,360 for set dog age and we're going to 29880 22:16:38,551 --> 22:16:42,959 return a dereferenced this pointer 29881 22:16:41,360 --> 22:16:46,080 because this is the reference we are 29882 22:16:42,960 --> 22:16:48,480 returning here so this is what the this 29883 22:16:46,080 --> 22:16:50,479 pointer is about now we're going to head 29884 22:16:48,479 --> 22:16:53,119 over to visual studio code and play with 29885 22:16:50,479 --> 22:16:55,599 us a little more okay here we are in our 29886 22:16:53,119 --> 22:16:58,231 working folder the current project is 29887 22:16:55,600 --> 22:17:00,320 the displayer we're going to grab our 29888 22:16:58,232 --> 22:17:02,480 template files and we're going to put 29889 22:17:00,320 --> 22:17:04,720 them in place and then we're going to 29890 22:17:02,479 --> 22:17:06,959 open this in visual studio code by 29891 22:17:04,720 --> 22:17:08,800 dragging and dropping here this is going 29892 22:17:06,960 --> 22:17:10,720 to open our folder we're going to open 29893 22:17:08,800 --> 22:17:13,040 the main cpp file and we're going to 29894 22:17:13,039 --> 22:17:18,231 so that we have some more space to work 29895 22:17:15,279 --> 22:17:20,080 with i am going to put in a version of 29896 22:17:20,080 --> 22:17:25,680 it is pretty limited right now it just 29897 22:17:23,279 --> 22:17:27,512 has a destructor and a constructor and a 29898 22:17:25,679 --> 22:17:29,359 constructor and we have our member 29899 22:17:27,512 --> 22:17:31,279 variables and we're going to 29900 22:17:29,360 --> 22:17:33,680 tune this a little bit so that we can 29901 22:17:31,279 --> 22:17:36,872 work with us however we want the first 29902 22:17:33,679 --> 22:17:40,000 thing we want to do is to print the 29903 22:17:36,872 --> 22:17:42,800 addresses of dug objects that we 29904 22:17:40,000 --> 22:17:45,119 construct and destruct here and we're 29905 22:17:42,800 --> 22:17:48,720 going to do that in our constructor so 29906 22:17:45,119 --> 22:17:51,279 let's go to our hd dc out statement here 29907 22:17:48,720 --> 22:17:53,360 and we're going to say add to mean the 29908 22:17:51,279 --> 22:17:55,831 memory address where the object was 29909 22:17:53,360 --> 22:17:59,119 constructed and we're going to say this 29910 22:17:55,831 --> 22:18:02,080 so when we do this this is going to be 29911 22:17:59,119 --> 22:18:05,039 the address in memory of the current 29912 22:18:02,080 --> 22:18:07,440 object that is being constructed 29913 22:18:05,039 --> 22:18:10,159 by our constructor here and we're going 29914 22:18:07,440 --> 22:18:12,232 to say the same thing in our destructor 29915 22:18:10,160 --> 22:18:14,552 we're going to say at and we're going to 29916 22:18:12,232 --> 22:18:17,760 say the memory address where the current 29917 22:18:14,551 --> 22:18:19,512 object is being destroyed from so after 29918 22:18:19,512 --> 22:18:24,479 if we go in the main function and for 29919 22:18:22,232 --> 22:18:26,720 example set up a doug object we're going 29920 22:18:24,479 --> 22:18:27,831 to say dark we're going to say dog one 29921 22:18:26,720 --> 22:18:30,080 and we're going to give it the 29922 22:18:27,831 --> 22:18:32,080 information we're going to give it the 29923 22:18:30,080 --> 22:18:33,680 name which is going to be fluffy we're 29924 22:18:32,080 --> 22:18:35,279 going to give it the breed which is 29925 22:18:33,679 --> 22:18:37,359 going to be a shepherd and we're going 29926 22:18:35,279 --> 22:18:39,191 to give it the age of two like we have 29927 22:18:37,360 --> 22:18:40,720 been doing all along when we do 29928 22:18:39,191 --> 22:18:42,319 something like this this is going to 29929 22:18:40,720 --> 22:18:45,360 call the constructor because we are 29930 22:18:42,320 --> 22:18:47,360 trying to set up an object and before 29931 22:18:45,360 --> 22:18:49,360 the main function ends we're going to 29932 22:18:47,360 --> 22:18:51,600 call the destructor and we're going to 29933 22:18:49,360 --> 22:18:53,191 see things happening here let's say done 29934 22:18:51,600 --> 22:18:55,600 so that we know that the code is 29935 22:18:53,191 --> 22:18:58,000 actually running i'm going to say sddc 29936 22:18:55,600 --> 22:18:58,872 out done here okay we have the setup 29937 22:18:58,872 --> 22:19:04,080 we can uh build and run the application 29938 22:19:01,512 --> 22:19:06,479 we're going to use gcc to build and the 29939 22:19:04,080 --> 22:19:09,512 build is good we can bring up a terminal 29940 22:19:06,479 --> 22:19:11,599 to try and run the application and now 29941 22:19:09,512 --> 22:19:14,720 if we run rooster we're going to see 29942 22:19:11,600 --> 22:19:17,440 that dog constructor called for a fluffy 29943 22:19:14,720 --> 22:19:20,000 at this memory location and the current 29944 22:19:17,440 --> 22:19:21,919 object is this object here which is 29945 22:19:20,000 --> 22:19:23,919 being constructed and we're going to 29946 22:19:21,919 --> 22:19:26,000 have access to its memory through the 29947 22:19:23,919 --> 22:19:29,440 this pointer hopefully you can see how 29948 22:19:26,000 --> 22:19:31,679 powerful this is if we didn't have the 29949 22:19:29,440 --> 22:19:32,872 this pointer the only way to get the 29950 22:19:32,872 --> 22:19:38,479 this object would be to grab the address 29951 22:19:35,831 --> 22:19:41,279 from the main function or from wherever 29952 22:19:38,479 --> 22:19:44,231 we are setting up our object here but we 29953 22:19:41,279 --> 22:19:46,639 have access to this address from the 29954 22:19:44,232 --> 22:19:48,872 inside of the functions for the class 29955 22:19:46,639 --> 22:19:51,759 and this is really cool so we can 29956 22:19:48,872 --> 22:19:53,360 manipulate these addresses if we need to 29957 22:19:51,759 --> 22:19:55,831 and we're going to show you another use 29958 22:19:53,360 --> 22:19:59,039 for this so suppose we need to set up 29959 22:19:55,831 --> 22:20:01,360 setters for our class here so we need a 29960 22:19:59,039 --> 22:20:03,599 setter to set the name the breed and to 29961 22:20:01,360 --> 22:20:05,279 set the age here so let's set up our 29962 22:20:03,600 --> 22:20:07,600 setters we're going to say setters and 29963 22:20:07,600 --> 22:20:12,720 name or let's be explicit set dog name 29964 22:20:10,960 --> 22:20:15,512 that's going to be better and we're 29965 22:20:12,720 --> 22:20:18,320 going to pass a string view we're going 29966 22:20:15,512 --> 22:20:20,080 to say stds friend of you and we're 29967 22:20:18,320 --> 22:20:22,720 going to say name program and we're 29968 22:20:20,080 --> 22:20:25,191 going to put the bodies in our class 29969 22:20:22,720 --> 22:20:28,639 here but i would encourage you to be 29970 22:20:25,191 --> 22:20:31,119 consistent if you are doing this in real 29971 22:20:28,639 --> 22:20:33,279 code we are learning here and we are 29972 22:20:31,119 --> 22:20:35,512 taking some shortcuts so that we can do 29973 22:20:33,279 --> 22:20:38,160 things simply but what we want to see 29974 22:20:35,512 --> 22:20:40,872 now is what happens if we have a 29975 22:20:38,160 --> 22:20:44,080 parameter named the same as a member 29976 22:20:44,080 --> 22:20:48,400 this name to the member variable called 29977 22:20:46,080 --> 22:20:49,831 the name so if you have a case like this 29978 22:20:48,399 --> 22:20:52,159 you're really going to be in trouble 29979 22:20:49,831 --> 22:20:53,831 because if you say name equals name 29980 22:20:53,831 --> 22:20:57,679 we should get a compiler error or a 29981 22:20:55,759 --> 22:20:59,439 squiggly line because the compiler is 29982 22:20:59,440 --> 22:21:03,360 which name are you using here is it the 29983 22:21:01,440 --> 22:21:05,760 memorial is it the parameter it's not 29984 22:21:03,360 --> 22:21:07,600 going to know and let's try to build and 29985 22:21:05,759 --> 22:21:09,279 see what we get we're going to use the 29986 22:21:09,279 --> 22:21:15,831 and surprisingly gcc is taking this 29987 22:21:13,119 --> 22:21:17,831 and it's probably knowing that we have a 29988 22:21:15,831 --> 22:21:19,679 member variable named name and it's 29989 22:21:17,831 --> 22:21:22,959 going to assign the parameter to that 29990 22:21:19,679 --> 22:21:25,831 member variable so behind the scenes gcc 29991 22:21:22,960 --> 22:21:29,280 is going to change this to this it's 29992 22:21:25,831 --> 22:21:31,119 going to use this name equals name 29993 22:21:29,279 --> 22:21:34,080 this is the syntax we want which is 29994 22:21:31,119 --> 22:21:36,551 really not confusing but what we have on 29995 22:21:34,080 --> 22:21:38,551 top here is going to be confusing and 29996 22:21:36,551 --> 22:21:40,959 some compilers are not even going to 29997 22:21:38,551 --> 22:21:43,039 take this let's leave this in and 29998 22:21:40,960 --> 22:21:45,192 actually build it in with us i want to 29999 22:21:43,039 --> 22:21:48,079 see that this actually works so we're 30000 22:21:45,191 --> 22:21:50,871 going to weld successfully and if we go 30001 22:21:50,872 --> 22:21:55,832 dog one set dog name let's say pumbaa 30002 22:21:54,000 --> 22:21:57,679 let's see what happens if we do 30003 22:21:55,831 --> 22:22:00,159 something like this we're going to try 30004 22:21:57,679 --> 22:22:02,231 and build again and gcc is going to take 30005 22:22:00,160 --> 22:22:03,832 this and this is really cool so let's 30006 22:22:02,232 --> 22:22:06,720 put in a function which is going to 30007 22:22:03,831 --> 22:22:08,871 print information about our dog we can 30008 22:22:06,720 --> 22:22:11,119 do that down here and it's going to 30009 22:22:08,872 --> 22:22:13,512 return nothing it's going to be called 30010 22:22:11,119 --> 22:22:15,512 print info and it's going to basically 30011 22:22:13,512 --> 22:22:17,600 print the name of the breed and the age 30012 22:22:15,512 --> 22:22:19,600 so it's going to say sddc out we're 30013 22:22:17,600 --> 22:22:21,120 going to say the address where the dot 30014 22:22:21,119 --> 22:22:25,512 i think that's more meaningful we're 30015 22:22:23,279 --> 22:22:26,399 going to say this and then we're going 30016 22:22:26,399 --> 22:22:32,399 put a parenthesis and say the name i 30017 22:22:28,551 --> 22:22:34,800 think we can put a an angle bracket here 30018 22:22:32,399 --> 22:22:38,799 and say name and we're going to say the 30019 22:22:34,800 --> 22:22:41,120 dog name we can go down and say the age 30020 22:22:38,800 --> 22:22:43,440 or the breed and say the age and we're 30021 22:22:41,119 --> 22:22:44,959 going to dereference the age member 30022 22:22:43,440 --> 22:22:47,919 variable because it's a pointer we're 30023 22:22:44,960 --> 22:22:50,160 going to say the reference ph here and i 30024 22:22:47,919 --> 22:22:53,440 think this is going to be it now we can 30025 22:22:50,160 --> 22:22:56,480 put a new line character std e ndm and 30026 22:22:53,440 --> 22:22:58,639 if we go down now and say printable 30027 22:22:56,479 --> 22:22:59,759 before we set the name we're going to 30028 22:23:01,119 --> 22:23:05,599 printful we're going to print the 30029 22:23:02,872 --> 22:23:07,680 information about this dog and after we 30030 22:23:05,600 --> 22:23:10,160 set the dog name we're going to go down 30031 22:23:07,679 --> 22:23:12,000 and also say dog one principle you can 30032 22:23:10,160 --> 22:23:14,080 play with these things and i really 30033 22:23:12,000 --> 22:23:16,639 encourage you to do so so we're going to 30034 22:23:14,080 --> 22:23:19,191 build this we're going to use gcc so the 30035 22:23:16,639 --> 22:23:20,872 world is good we can clear and run 30036 22:23:19,191 --> 22:23:23,039 rooster this is going to say the 30037 22:23:20,872 --> 22:23:24,551 constructor was called for fluffy at the 30038 22:23:24,551 --> 22:23:28,800 we're going to say the dog we're going 30039 22:23:27,119 --> 22:23:31,919 to print the information and we're going 30040 22:23:28,800 --> 22:23:34,080 to say it's a dog object at this address 30041 22:23:31,919 --> 22:23:35,919 the name is fluffy and we're going to 30042 22:23:34,080 --> 22:23:37,919 print the dog for the second time you're 30043 22:23:35,919 --> 22:23:40,319 going to see that the name is still 30044 22:23:37,919 --> 22:23:41,360 fluffy and we have a problem here why is 30045 22:23:41,360 --> 22:23:48,232 why is our object not being modified 30046 22:23:44,720 --> 22:23:50,639 let's go up and see our set dog name 30047 22:23:48,232 --> 22:23:53,040 function here so we're going to 30048 22:23:50,639 --> 22:23:55,360 remove this and we're going to use the 30049 22:23:53,039 --> 22:23:58,231 this pointer and we're going to 30050 22:23:55,360 --> 22:24:00,639 weld again and let's clear now and run 30051 22:23:58,232 --> 22:24:03,680 rooster and now you see that the changes 30052 22:24:00,639 --> 22:24:06,872 are actually making sense so what is 30053 22:24:03,679 --> 22:24:09,599 happening with the assignment here the 30054 22:24:06,872 --> 22:24:11,832 compiler is assigning the value of the 30055 22:24:09,600 --> 22:24:14,080 variable to the variable itself and this 30056 22:24:11,831 --> 22:24:16,399 is really doing nothing and it is not 30057 22:24:14,080 --> 22:24:18,400 what you expect i was fooled a little 30058 22:24:16,399 --> 22:24:20,799 bit by the compiler because this was 30059 22:24:18,399 --> 22:24:23,511 compiling but it's not going to do what 30060 22:24:20,800 --> 22:24:26,160 you want when you call set doug name you 30061 22:24:23,512 --> 22:24:28,800 probably want to take this name and 30062 22:24:26,160 --> 22:24:31,120 assign that to the member variable name 30063 22:24:28,800 --> 22:24:33,360 of this class but this is not what it's 30064 22:24:31,119 --> 22:24:34,799 going to do let's build again and really 30065 22:24:34,800 --> 22:24:39,192 so it is a really good advice to 30066 22:24:37,279 --> 22:24:41,191 practice and really try things out 30067 22:24:39,191 --> 22:24:43,119 yourself we're going to run a rooster 30068 22:24:41,191 --> 22:24:45,831 you're going to see that after we set 30069 22:24:43,119 --> 22:24:47,679 the dog name here to pumbaa and we try 30070 22:24:45,831 --> 22:24:49,599 to print the information the information 30071 22:24:47,679 --> 22:24:51,679 is not going to be end the name is not 30072 22:24:49,600 --> 22:24:54,000 going to be pumbaa for the second call 30073 22:24:51,679 --> 22:24:56,079 here and what this is really doing is 30074 22:24:54,000 --> 22:24:58,232 doing nothing here this does nothing and 30075 22:24:56,080 --> 22:24:59,600 you really need to know this if you want 30076 22:24:59,600 --> 22:25:03,120 your remember variable you will have to 30077 22:25:01,512 --> 22:25:04,400 go through the this pointer and this is 30078 22:25:04,399 --> 22:25:10,159 take the name member variable of 30079 22:25:07,360 --> 22:25:12,800 the current object and assign the value 30080 22:25:10,160 --> 22:25:15,040 in name the name parameter to that and 30081 22:25:12,800 --> 22:25:17,440 that's going to set your name to 30082 22:25:15,039 --> 22:25:20,159 whatever new name you passed when you 30083 22:25:17,440 --> 22:25:22,160 called the set dog name function here so 30084 22:25:20,160 --> 22:25:23,919 hopefully you really understand this now 30085 22:25:22,160 --> 22:25:26,639 if we both are going to see that the 30086 22:25:23,919 --> 22:25:29,119 pumbaa name has been applied let's boil 30087 22:25:26,639 --> 22:25:31,279 again so we're going to clear and run 30088 22:25:29,119 --> 22:25:33,759 rooster and we're going to see now that 30089 22:25:31,279 --> 22:25:35,360 the name is pumbaa and there is some 30090 22:25:35,360 --> 22:25:40,800 constructors we don't have a space 30091 22:25:37,759 --> 22:25:43,039 before at here and it is really annoying 30092 22:25:40,800 --> 22:25:45,279 to look at here so let's build again and 30093 22:25:43,039 --> 22:25:47,360 fix this we are going to use gcc the 30094 22:25:45,279 --> 22:25:50,551 world is good we can clear and run 30095 22:25:47,360 --> 22:25:52,639 rooster and now we have what we want so 30096 22:25:50,551 --> 22:25:55,831 this is the second use case you might 30097 22:25:52,639 --> 22:25:58,479 have for this pointer through setters 30098 22:25:55,831 --> 22:26:01,759 let's use the parameter that is named 30099 22:25:58,479 --> 22:26:03,191 the same as your member variables so 30100 22:26:01,759 --> 22:26:05,039 we're going to set up all the other 30101 22:26:03,191 --> 22:26:07,599 setters we're going to set up a setter 30102 22:26:05,039 --> 22:26:10,479 for the breed we're going to say std 30103 22:26:07,600 --> 22:26:12,800 strength view and it's going to be breed 30104 22:26:10,479 --> 22:26:14,399 we can do that because now we have tools 30105 22:26:12,800 --> 22:26:17,120 to really do this properly we're going 30106 22:26:14,399 --> 22:26:20,000 to say this breed and we're going to say 30107 22:26:17,119 --> 22:26:22,319 equals read this is going to do and we 30108 22:26:20,000 --> 22:26:24,232 can set the age and we're going to say 30109 22:26:24,232 --> 22:26:28,720 dog age and we're going to pass in a 30110 22:26:26,080 --> 22:26:31,040 value and we're going to go inside what 30111 22:26:28,720 --> 22:26:32,639 is it called it's called ph i don't 30112 22:26:31,039 --> 22:26:35,759 think it makes sense to call the 30113 22:26:32,639 --> 22:26:37,759 parameter ph because it's not a pointer 30114 22:26:35,759 --> 22:26:39,439 so we're going to go inside and just the 30115 22:26:37,759 --> 22:26:40,959 reference of a pointer so we're going to 30116 22:26:40,960 --> 22:26:46,720 and we're going to say this so we're 30117 22:26:43,360 --> 22:26:49,512 going to go inside and say this ph 30118 22:26:46,720 --> 22:26:51,919 equals h i think this is going to work 30119 22:26:49,512 --> 22:26:53,600 and we need to reference this pointer 30120 22:26:51,919 --> 22:26:55,679 because what we have as a member 30121 22:26:53,600 --> 22:26:57,920 variable is the pointer so we need to go 30122 22:26:55,679 --> 22:26:59,759 through the ugly syntax you see here and 30123 22:26:57,919 --> 22:27:01,360 this is going to set our age so we can 30124 22:26:59,759 --> 22:27:03,439 go through this and really change the 30125 22:27:01,360 --> 22:27:06,800 information so we're going to change the 30126 22:27:03,440 --> 22:27:08,960 breed here set dog breed let's go down 30127 22:27:06,800 --> 22:27:11,440 and choose that and we're going to say 30128 22:27:08,960 --> 22:27:13,440 wirefox terrier and we're going to go 30129 22:27:11,440 --> 22:27:16,720 down and set the age we're going to say 30130 22:27:13,440 --> 22:27:19,680 guilt 1 set dog age and we're going to 30131 22:27:16,720 --> 22:27:22,160 say for why not so we change the 30132 22:27:19,679 --> 22:27:24,079 information of our dog through our 30133 22:27:22,160 --> 22:27:25,600 setters here and we're going to print 30134 22:27:25,600 --> 22:27:30,000 let's build again we're going to use gcc 30135 22:27:27,831 --> 22:27:32,399 for that the build is good we can clear 30136 22:27:30,000 --> 22:27:34,479 and run rooster this is going to give us 30137 22:27:32,399 --> 22:27:37,191 new information for the second print 30138 22:27:34,479 --> 22:27:39,440 here so name is going to be pumba breed 30139 22:27:37,191 --> 22:27:41,039 is going to be wirefox terrier and age 30140 22:27:39,440 --> 22:27:43,919 is going to be four and we have the 30141 22:27:41,039 --> 22:27:46,479 information for our dog object here okay 30142 22:27:43,919 --> 22:27:48,959 now we have this and we want to see how 30143 22:27:48,960 --> 22:27:53,832 using the this pointer and in this case 30144 22:27:51,512 --> 22:27:56,639 we will be using pointers let's change 30145 22:27:53,831 --> 22:27:58,799 our setters to return dog pointers we 30146 22:27:56,639 --> 22:28:00,399 can do that and we're going to go down 30147 22:27:58,800 --> 22:28:02,160 to say to dog breed we're going to 30148 22:28:02,160 --> 22:28:07,360 and we're going to go to set dog age and 30149 22:28:04,872 --> 22:28:09,512 we're going to return a dog pointer now 30150 22:28:07,360 --> 22:28:11,119 after we do this we will need to return 30151 22:28:09,512 --> 22:28:14,400 these pointers and we're going to go 30152 22:28:11,119 --> 22:28:16,479 inside and say return this remember this 30153 22:28:14,399 --> 22:28:19,191 contains the memory of the current 30154 22:28:16,479 --> 22:28:21,831 object so we can return this and fit 30155 22:28:19,191 --> 22:28:23,831 that to a returned pointer so we can go 30156 22:28:21,831 --> 22:28:25,439 down in all the other functions and 30157 22:28:23,831 --> 22:28:28,159 really save this we're going to say 30158 22:28:25,440 --> 22:28:30,160 return thus we return the memory address 30159 22:28:30,160 --> 22:28:34,720 through the setters that we are doing 30160 22:28:32,551 --> 22:28:38,000 here we're going to return this 30161 22:28:34,720 --> 22:28:40,000 and after we do this we can change the 30162 22:28:38,000 --> 22:28:42,720 setting we are doing here to b1 30163 22:28:40,000 --> 22:28:44,399 statement and we can comment this out i 30164 22:28:42,720 --> 22:28:47,360 think we can do that and what we're 30165 22:28:44,399 --> 22:28:48,319 going to say is dope one and we're going 30166 22:28:48,320 --> 22:28:53,192 set dog name and we're going to pass in 30167 22:28:50,720 --> 22:28:55,759 pumbaa but this is going to return a 30168 22:28:53,191 --> 22:28:58,231 pointer now set dog name is returning a 30169 22:28:55,759 --> 22:29:00,399 pointer to the current object and you 30170 22:28:58,232 --> 22:29:02,639 can see that here this is what we are 30171 22:29:00,399 --> 22:29:05,039 returning so we can go through this 30172 22:29:02,639 --> 22:29:08,000 pointer and call some other functions 30173 22:29:05,039 --> 22:29:10,231 for example we can say set dot breed 30174 22:29:08,000 --> 22:29:11,831 directly and the visual studio code is 30175 22:29:10,232 --> 22:29:14,000 going to figure out that what we are 30176 22:29:11,831 --> 22:29:16,959 returning is a pointer and it's going to 30177 22:29:14,000 --> 22:29:19,440 change our access notation here to a 30178 22:29:16,960 --> 22:29:22,000 pointer axis notation and we're going to 30179 22:29:19,440 --> 22:29:24,232 use this in one single statement so 30180 22:29:22,000 --> 22:29:26,160 we're going to put in our breed and 30181 22:29:24,232 --> 22:29:28,480 we're going to call the function to set 30182 22:29:26,160 --> 22:29:30,320 the age okay you see that it uses 30183 22:29:28,479 --> 22:29:32,000 pointer access notation and this is 30184 22:29:30,320 --> 22:29:33,920 really cool so we're going to put in our 30185 22:29:32,000 --> 22:29:36,160 four and you're going to see that this 30186 22:29:33,919 --> 22:29:38,319 actually works and this is really cool 30187 22:29:36,160 --> 22:29:41,600 if you don't want to do thanks in three 30188 22:29:38,320 --> 22:29:44,080 steps you can give your users the option 30189 22:29:41,600 --> 22:29:46,160 to do things in one step using returned 30190 22:29:44,080 --> 22:29:48,320 pointers like this and this is really 30191 22:29:46,160 --> 22:29:51,440 cool i use this all the time for my 30192 22:29:48,320 --> 22:29:54,160 classes that i design in my applications 30193 22:29:51,440 --> 22:29:55,919 so if we're both we're going to see that 30194 22:29:54,160 --> 22:29:58,480 the world is good and we're going to 30195 22:29:55,919 --> 22:30:01,512 clear and run rooster we're going to see 30196 22:29:58,479 --> 22:30:04,319 that the information is n name is pumbaa 30197 22:30:01,512 --> 22:30:06,800 breed is waterfox terrier and the age is 30198 22:30:04,320 --> 22:30:09,920 for and we were able to do all these 30199 22:30:06,800 --> 22:30:12,872 changes in one go so we can even change 30200 22:30:09,919 --> 22:30:15,191 this to use references here we are using 30201 22:30:12,872 --> 22:30:17,440 pointers so let's say that we are using 30202 22:30:15,191 --> 22:30:20,551 pointers but we can also use references 30203 22:30:17,440 --> 22:30:22,720 let's go on top here and say that this 30204 22:30:20,551 --> 22:30:24,800 is using pointers and we're going to 30205 22:30:22,720 --> 22:30:26,720 comment this out but before we do we're 30206 22:30:24,800 --> 22:30:28,479 going to copy all this because we're 30207 22:30:29,919 --> 22:30:34,080 comment all this out and we're going to 30208 22:30:31,600 --> 22:30:36,080 go down and say change calls using 30209 22:30:34,080 --> 22:30:38,232 references and we're going to go down 30210 22:30:36,080 --> 22:30:41,119 and put in our code and we're going to 30211 22:30:38,232 --> 22:30:43,512 change our setters to return references 30212 22:30:41,119 --> 22:30:45,599 instead of pointers we can do that so 30213 22:30:43,512 --> 22:30:48,080 we're going to change the star here to 30214 22:30:45,600 --> 22:30:50,232 an ampersand symbol here and this is 30215 22:30:48,080 --> 22:30:52,320 going to do the magic so we have a dog 30216 22:30:50,232 --> 22:30:54,480 reference returned from all these 30217 22:30:52,320 --> 22:30:56,640 functions so because we are no longer 30218 22:30:54,479 --> 22:30:58,551 returning a pointer returning this 30219 22:30:56,639 --> 22:31:00,872 directly is going to give us a compiler 30220 22:30:58,551 --> 22:31:03,360 error we need to dereference this 30221 22:31:00,872 --> 22:31:05,760 pointer and get to what what is pointed 30222 22:31:03,360 --> 22:31:08,160 to now the compiler will know that we 30223 22:31:05,759 --> 22:31:10,479 are returning a reference to the current 30224 22:31:08,160 --> 22:31:13,360 object and this is the syntax we can use 30225 22:31:10,479 --> 22:31:16,231 to do this let's go down and dereference 30226 22:31:13,360 --> 22:31:17,759 all the returned pointers and if we go 30227 22:31:16,232 --> 22:31:20,320 down we're going to see that this is 30228 22:31:17,759 --> 22:31:23,119 good already and we can go down and set 30229 22:31:20,320 --> 22:31:25,192 up our chained call using references and 30230 22:31:23,119 --> 22:31:27,759 we're going to go down and say dog one 30231 22:31:25,191 --> 22:31:29,759 we're going to set dog name and we're 30232 22:31:29,759 --> 22:31:33,119 what we have been doing all along now 30233 22:31:31,512 --> 22:31:36,320 we're going to use the dot access 30234 22:31:33,119 --> 22:31:38,871 notation because what we are returning 30235 22:31:36,320 --> 22:31:40,720 is a reference and you're going to see 30236 22:31:38,872 --> 22:31:43,832 that visual studio code is going to pick 30237 22:31:40,720 --> 22:31:46,960 this up the breed is going to be wirefox 30238 22:31:43,831 --> 22:31:49,439 and we're going to say set dog age and 30239 22:31:46,960 --> 22:31:51,512 we're going to pass in our four and 30240 22:31:49,440 --> 22:31:53,600 you're going to see that this is valid c 30241 22:31:51,512 --> 22:31:55,191 plus syntax and if we print the 30242 22:31:53,600 --> 22:31:58,639 information we're going to have new 30243 22:31:55,191 --> 22:32:00,959 information applied to our dog object 30244 22:31:58,639 --> 22:32:02,720 here we can weld and see that this is 30245 22:32:00,960 --> 22:32:05,440 actually working you see the world is 30246 22:32:02,720 --> 22:32:07,279 good we can clear and run rooster and 30247 22:32:05,440 --> 22:32:10,960 we're going to see that information has 30248 22:32:07,279 --> 22:32:12,399 been applied to our dog object so these 30249 22:32:12,399 --> 22:32:16,551 showing some of the usefulness for the 30250 22:32:14,720 --> 22:32:18,960 this pointer we have seen that we can 30251 22:32:16,551 --> 22:32:21,759 use the this pointer to print the memory 30252 22:32:18,960 --> 22:32:24,552 address of the current object if we go 30253 22:32:21,759 --> 22:32:26,871 down and look at our constructor and the 30254 22:32:24,551 --> 22:32:29,512 destructor we are printing the address 30255 22:32:26,872 --> 22:32:31,360 of the current object in our debug 30256 22:32:29,512 --> 22:32:33,440 statement here and this is really cool 30257 22:32:31,360 --> 22:32:35,919 we can use the this pointer to resolve 30258 22:32:33,440 --> 22:32:38,232 conflicts in our setters for example we 30259 22:32:35,919 --> 22:32:40,720 can use a parameter that has the same 30260 22:32:38,232 --> 22:32:43,192 name as the member variable and it's not 30261 22:32:40,720 --> 22:32:45,600 going to be really confusing and lead to 30262 22:32:43,191 --> 22:32:48,319 bad results it's going to change the 30263 22:32:45,600 --> 22:32:50,080 actual member variable and change it to 30264 22:32:48,320 --> 22:32:52,320 whatever you pass as the parameter and 30265 22:32:50,080 --> 22:32:55,040 this is really cool we can also use the 30266 22:32:52,320 --> 22:32:56,872 this pointer to set up change calls for 30267 22:32:55,039 --> 22:32:58,799 our function and we have seen that we 30268 22:32:56,872 --> 22:33:00,872 can do that either through returning 30269 22:32:58,800 --> 22:33:03,760 pointers like we are doing here and 30270 22:33:00,872 --> 22:33:06,720 using the this pointer as a return 30271 22:33:03,759 --> 22:33:09,511 object for our function here or we can 30272 22:33:06,720 --> 22:33:11,440 use returned pointers to do this and if 30273 22:33:09,512 --> 22:33:14,320 we do that we're going to be able to 30274 22:33:11,440 --> 22:33:17,279 call our functions in one go like we are 30275 22:33:14,320 --> 22:33:20,232 doing here using pointers or do the same 30276 22:33:17,279 --> 22:33:22,872 thing using references like we are doing 30277 22:33:20,232 --> 22:33:24,480 here this is really all we set out to do 30278 22:33:22,872 --> 22:33:26,720 in this lecture i hope you found it 30279 22:33:24,479 --> 22:33:28,399 interesting we are going to stop here in 30280 22:33:26,720 --> 22:33:31,040 this one the next one we're going to 30281 22:33:28,399 --> 22:33:33,279 learn about struct go ahead and finish 30282 22:33:31,039 --> 22:33:35,191 up here and meet me there in this 30283 22:33:33,279 --> 22:33:37,360 lecture we're going to learn about the 30284 22:33:35,191 --> 22:33:40,159 struct keyword and this is another 30285 22:33:37,360 --> 22:33:42,639 syntax you can use to create classes in 30286 22:33:40,160 --> 22:33:44,552 your c plus plus program we have seen 30287 22:33:42,639 --> 22:33:47,360 that we can already use the class 30288 22:33:44,551 --> 22:33:49,279 keyword to create our classes i have to 30289 22:33:47,360 --> 22:33:51,919 break it to you that we can also use 30290 22:33:49,279 --> 22:33:54,232 struct to do the same so the syntax here 30291 22:33:51,919 --> 22:33:56,080 is going to create a class called cat 30292 22:33:54,232 --> 22:33:58,320 and it's going to have a member variable 30293 22:33:56,080 --> 22:34:01,600 called string name the difference 30294 22:33:58,320 --> 22:34:04,720 between struct and class is that for 30295 22:34:01,600 --> 22:34:07,192 classes members are going to be private 30296 22:34:04,720 --> 22:34:09,680 by default and when you do something 30297 22:34:07,191 --> 22:34:11,679 like this and create class objects you 30298 22:34:09,679 --> 22:34:13,679 want to be able to access the name here 30299 22:34:11,679 --> 22:34:15,511 directly for example from the main 30300 22:34:13,679 --> 22:34:17,919 function if you do that you're going to 30301 22:34:15,512 --> 22:34:21,040 get a compiler error because class 30302 22:34:17,919 --> 22:34:23,599 members are private by default but if 30303 22:34:21,039 --> 22:34:26,000 you use struct members are going to be 30304 22:34:23,600 --> 22:34:28,872 public by default and you will have 30305 22:34:26,000 --> 22:34:31,119 direct access from the outside this is 30306 22:34:28,872 --> 22:34:33,279 the only difference between class and 30307 22:34:31,119 --> 22:34:35,440 struct otherwise you can use them 30308 22:34:33,279 --> 22:34:37,360 interchangeably and they are going to do 30309 22:34:35,440 --> 22:34:39,512 the same things we have been seeing for 30310 22:34:37,360 --> 22:34:41,512 classes they really are the same things 30311 22:34:39,512 --> 22:34:43,512 the only difference is that classes are 30312 22:34:41,512 --> 22:34:46,639 going to have private member variables 30313 22:34:43,512 --> 22:34:49,512 by default and struct will have public 30314 22:34:46,639 --> 22:34:51,831 member variables by default but we can 30315 22:34:49,512 --> 22:34:54,800 change this defaults by putting in our 30316 22:34:51,831 --> 22:34:57,191 own public and private sections inside 30317 22:34:54,800 --> 22:34:59,440 our class so for example here you see 30318 22:34:57,191 --> 22:35:01,599 that we have changed our dog class to 30319 22:34:59,440 --> 22:35:04,160 have a public member variable through 30320 22:35:01,600 --> 22:35:06,160 the public keyword here and we can do 30321 22:35:04,160 --> 22:35:08,232 the same for cat for example we can put 30322 22:35:06,160 --> 22:35:10,232 in a public section and a private 30323 22:35:08,232 --> 22:35:12,800 section and it's going to behave like 30324 22:35:10,232 --> 22:35:15,360 classes we have been welding so far 30325 22:35:12,800 --> 22:35:19,120 one common use we have for struct is 30326 22:35:15,360 --> 22:35:21,759 when we have to set up classes that only 30327 22:35:19,119 --> 22:35:24,231 have public member variables for example 30328 22:35:21,759 --> 22:35:26,959 you can need to set up a point 30329 22:35:24,232 --> 22:35:28,720 model in your c plus plus program and 30330 22:35:26,960 --> 22:35:30,800 you can set up a class like this you can 30331 22:35:28,720 --> 22:35:32,720 say struct point you're going to put in 30332 22:35:30,800 --> 22:35:34,720 the member variables and they are going 30333 22:35:32,720 --> 22:35:36,720 to be directly accessible from the 30334 22:35:34,720 --> 22:35:39,600 outside and this is something you will 30335 22:35:36,720 --> 22:35:41,680 see a lot out there so this is really 30336 22:35:39,600 --> 22:35:43,680 all about struct we're going to head 30337 22:35:41,679 --> 22:35:46,479 over to visual studio code and play with 30338 22:35:43,679 --> 22:35:48,799 us a little more okay here we are in our 30339 22:35:46,479 --> 22:35:51,360 working folder the current project is 30340 22:35:48,800 --> 22:35:53,192 struct we're going to grab our template 30341 22:35:51,360 --> 22:35:56,479 files and we're going to put them in 30342 22:35:53,191 --> 22:35:58,551 place struct that's all project here 30343 22:35:56,479 --> 22:36:00,159 and we're going to open this in visual 30344 22:36:00,160 --> 22:36:04,232 let's grab extract and drag and drop on 30345 22:36:02,232 --> 22:36:07,192 top of visual studio code here this is 30346 22:36:04,232 --> 22:36:09,919 going to open our folder we can now open 30347 22:36:07,191 --> 22:36:11,679 the main cpp file and clean things up a 30348 22:36:09,919 --> 22:36:13,919 little bit okay so we're going to set up 30349 22:36:11,679 --> 22:36:16,479 a simple class called dog we're going to 30350 22:36:13,919 --> 22:36:18,800 say class dog and inside we're going to 30351 22:36:16,479 --> 22:36:21,191 have a member variable called name it's 30352 22:36:18,800 --> 22:36:23,680 going to be an std strand let's say dog 30353 22:36:21,191 --> 22:36:25,679 name or m name to mean that it is a 30354 22:36:23,679 --> 22:36:28,399 member variable this is a syntax you 30355 22:36:25,679 --> 22:36:30,551 will see a lot in c plus plus out there 30356 22:36:28,399 --> 22:36:33,279 so let's use this from now on we're 30357 22:36:30,551 --> 22:36:36,080 going to say m name and we're going to 30358 22:36:33,279 --> 22:36:38,319 have another class let's put our 30359 22:36:36,080 --> 22:36:40,551 semicolon we're going to use struct to 30360 22:36:38,320 --> 22:36:43,040 set up another one and it's going to be 30361 22:36:40,551 --> 22:36:44,959 called dog let's call this cat and we 30362 22:36:43,039 --> 22:36:46,799 are going to go inside and set up our 30363 22:36:44,960 --> 22:36:48,960 member variable we're going to say std 30364 22:36:46,800 --> 22:36:51,680 strength m name and it's not going to 30365 22:36:48,960 --> 22:36:54,639 have anything m so if we create objects 30366 22:36:51,679 --> 22:36:56,871 of these classes and try to access them 30367 22:36:54,639 --> 22:36:59,039 we're going to see the fundamental 30368 22:36:56,872 --> 22:37:00,720 difference between structure and dog 30369 22:36:59,039 --> 22:37:03,039 let's create a dog and we're going to 30370 22:37:00,720 --> 22:37:05,191 say dog one and we're going to create a 30371 22:37:03,039 --> 22:37:08,871 cat and we're going to say cat one and 30372 22:37:05,191 --> 22:37:10,871 if we do dog 1 and try to access the m 30373 22:37:10,872 --> 22:37:15,120 maybe set it to something else let's say 30374 22:37:13,191 --> 22:37:18,319 fluffy we're going to have a compiler 30375 22:37:15,119 --> 22:37:20,479 error because members of a class are 30376 22:37:18,320 --> 22:37:21,600 private by default and we can't really 30377 22:37:21,600 --> 22:37:25,600 so we are trying to access something 30378 22:37:23,440 --> 22:37:27,440 that is private from the outside we're 30379 22:37:25,600 --> 22:37:29,680 going to get a compiler error okay so we 30380 22:37:27,440 --> 22:37:31,919 have a squiggly line here but if we try 30381 22:37:29,679 --> 22:37:34,319 to do the same for a cat it's going to 30382 22:37:31,919 --> 22:37:37,759 go through because cat is destruct and 30383 22:37:34,320 --> 22:37:39,440 members of structs are public by default 30384 22:37:37,759 --> 22:37:41,919 let's do that and really show you this 30385 22:37:39,440 --> 22:37:44,160 we're going to say cat1 and we're going 30386 22:37:41,919 --> 22:37:47,440 to say m name and we're going to give it 30387 22:37:44,160 --> 22:37:49,600 a name and let's say johnny i am making 30388 22:37:47,440 --> 22:37:51,760 this up i don't know if there is any cat 30389 22:37:49,600 --> 22:37:53,279 named johnny but we're going to use this 30390 22:37:51,759 --> 22:37:55,599 and you're going to see that we don't 30391 22:37:53,279 --> 22:37:58,080 have any squiggly line we're going to 30392 22:37:55,600 --> 22:37:59,920 build and see if gcc is going to take 30393 22:37:58,080 --> 22:38:02,720 this it's going to take it the world is 30394 22:37:59,919 --> 22:38:04,800 good we can bring up a terminal and run 30395 22:38:02,720 --> 22:38:06,551 the program and we're not going to see 30396 22:38:04,800 --> 22:38:09,120 anything because we're not printing 30397 22:38:06,551 --> 22:38:11,831 anything from the main function here but 30398 22:38:09,119 --> 22:38:15,039 if we want we can print this name of our 30399 22:38:11,831 --> 22:38:17,599 cat we're going to say sddc out and say 30400 22:38:15,039 --> 22:38:20,479 cat one name and if we build we're going 30401 22:38:17,600 --> 22:38:23,512 to see that so the both should be good 30402 22:38:20,479 --> 22:38:25,440 it is good we can clear and run rooster 30403 22:38:23,512 --> 22:38:27,191 and we have johnny printed out and this 30404 22:38:25,440 --> 22:38:29,919 is the name of volca and this is really 30405 22:38:27,191 --> 22:38:32,000 the difference between class and strut 30406 22:38:29,919 --> 22:38:34,479 members of classes are going to be 30407 22:38:32,000 --> 22:38:36,872 private by default members of struct are 30408 22:38:34,479 --> 22:38:38,872 going to be public by default but we can 30409 22:38:36,872 --> 22:38:41,360 change these defaults for example let's 30410 22:38:38,872 --> 22:38:42,800 make everything public we can do that no 30411 22:38:42,800 --> 22:38:48,800 and we can move this a little bit to the 30412 22:38:45,679 --> 22:38:51,599 right so that things are aligned nicely 30413 22:38:48,800 --> 22:38:54,800 and we can do the same for struct okay 30414 22:38:51,600 --> 22:38:56,872 so let's align this now if we do this we 30415 22:38:54,800 --> 22:38:58,800 will be able to go in the main function 30416 22:38:56,872 --> 22:39:00,479 and say dog name and change that and 30417 22:38:58,800 --> 22:39:03,832 we're going to be able to see that if we 30418 22:39:00,479 --> 22:39:08,000 print that out so let's go down and say 30419 22:39:03,831 --> 22:39:09,360 stdc out and say dog1 name and print 30420 22:39:09,360 --> 22:39:13,512 and if we build now we're going to see 30421 22:39:11,119 --> 22:39:15,440 the name of our dog printed out okay 30422 22:39:13,512 --> 22:39:18,320 well this is good we can bring this up 30423 22:39:15,440 --> 22:39:20,232 and clear a little bit and run rooster 30424 22:39:18,320 --> 22:39:22,960 we're going to see fluffy our dog name 30425 22:39:20,232 --> 22:39:25,832 and johnny our cat name so this is 30426 22:39:22,960 --> 22:39:27,760 really all about classes and struct they 30427 22:39:25,831 --> 22:39:29,511 are the same things but the only 30428 22:39:27,759 --> 22:39:31,831 difference is the defaults that come 30429 22:39:29,512 --> 22:39:34,800 with these things but the common use we 30430 22:39:31,831 --> 22:39:37,831 have for struct is when we want to set 30431 22:39:34,800 --> 22:39:40,320 up a class that only has direct member 30432 22:39:37,831 --> 22:39:43,039 variables and we don't intend to put 30433 22:39:40,320 --> 22:39:45,280 methods into that class a good example 30434 22:39:43,039 --> 22:39:47,759 of this is the struct off point so 30435 22:39:45,279 --> 22:39:50,080 suppose we want to model points in our c 30436 22:39:47,759 --> 22:39:52,231 plus plus program so we can do something 30437 22:39:50,080 --> 22:39:55,040 like this and a point is going to have 30438 22:39:52,232 --> 22:39:57,832 two coordinates x and y we're going to 30439 22:39:55,039 --> 22:39:59,919 say double y and from this point on we 30440 22:39:57,831 --> 22:40:02,319 will be able to create point objects and 30441 22:39:59,919 --> 22:40:04,800 print them out however we want so for 30442 22:40:02,320 --> 22:40:07,600 example we can go down and say point 30443 22:40:04,800 --> 22:40:11,040 point one and from this point we can go 30444 22:40:07,600 --> 22:40:13,360 in and say 0.1 and say x and assign a 30445 22:40:11,039 --> 22:40:17,039 value to x let's put in a 10 and we're 30446 22:40:13,360 --> 22:40:18,720 going to go down and say point 1 y and 30447 22:40:17,039 --> 22:40:19,479 we're going to give it a value let's put 30448 22:40:19,479 --> 22:40:24,231 55.5 why not and we can do whatever we 30449 22:40:22,232 --> 22:40:25,760 want with this point for example we can 30450 22:40:24,232 --> 22:40:28,232 set up a function which is going to 30451 22:40:25,759 --> 22:40:30,720 print a point we can do that let's say 30452 22:40:28,232 --> 22:40:33,192 void print point and we're going to pass 30453 22:40:30,720 --> 22:40:35,040 the point by value or we can even use 30454 22:40:33,191 --> 22:40:37,599 references because this is going to be 30455 22:40:35,039 --> 22:40:39,279 an object const point reference i think 30456 22:40:37,600 --> 22:40:41,760 this is going to do and we're going to 30457 22:40:39,279 --> 22:40:43,039 go inside and print the information 30458 22:40:43,039 --> 22:40:48,231 hddc out point and we're going to put 30459 22:40:45,600 --> 22:40:51,760 the coordinates on we're going to say x 30460 22:40:48,232 --> 22:40:54,480 is going to be point x and y is going to 30461 22:40:51,759 --> 22:40:57,119 be point y why not and we're going to 30462 22:40:54,479 --> 22:40:59,440 put in our closing square bracket and 30463 22:40:59,440 --> 22:41:04,551 to put a new line character after our 30464 22:41:02,080 --> 22:41:07,680 print here now we can go down and say 30465 22:41:04,551 --> 22:41:10,080 print point and pass on point one 30466 22:41:07,679 --> 22:41:11,759 and if we do that we're going to build 30467 22:41:10,080 --> 22:41:14,479 and we're going to do that through gcc 30468 22:41:11,759 --> 22:41:16,799 you see the both is good now if we print 30469 22:41:14,479 --> 22:41:19,479 we should see the information about our 30470 22:41:26,639 --> 22:41:30,160 we can go down and change the 30471 22:41:28,399 --> 22:41:33,439 information about our point for example 30472 22:41:30,160 --> 22:41:35,720 we can copy this and go down and change 30473 22:41:39,872 --> 22:41:44,551 2.7 why not and if we print the 30474 22:41:42,800 --> 22:41:46,479 information about our point we're going 30475 22:41:44,551 --> 22:41:48,479 to see that we have new information and 30476 22:41:46,479 --> 22:41:50,000 we're going to print point one and 30477 22:41:48,479 --> 22:41:52,319 therefore world we're going to see the 30478 22:41:50,000 --> 22:41:55,119 new information so the build is good we 30479 22:41:52,320 --> 22:41:57,120 can clear and run rooster now you see 30480 22:41:55,119 --> 22:41:59,119 new information in our point so this is 30481 22:41:57,119 --> 22:42:01,191 really the difference between class and 30482 22:41:59,119 --> 22:42:03,919 struct the only difference is going to 30483 22:42:01,191 --> 22:42:07,039 be in the defaults and struct is mostly 30484 22:42:03,919 --> 22:42:09,919 useful if you want to set up classes 30485 22:42:07,039 --> 22:42:12,231 that only have public member variables 30486 22:42:09,919 --> 22:42:14,959 and you don't really want to use 30487 22:42:12,232 --> 22:42:17,279 functions or methods in that class and 30488 22:42:14,960 --> 22:42:19,360 we have an example of that here and if 30489 22:42:17,279 --> 22:42:21,119 you have a case like that you will use 30490 22:42:19,360 --> 22:42:24,160 struct your code is going to be much 30491 22:42:21,119 --> 22:42:26,479 easier to read and understand from the 30492 22:42:24,160 --> 22:42:29,040 perspective of other c plus plus 30493 22:42:26,479 --> 22:42:30,639 developers we are going to stop here in 30494 22:42:29,039 --> 22:42:34,000 this lecture and the last one we're 30495 22:42:30,639 --> 22:42:36,399 going to learn about the size of objects 30496 22:42:34,000 --> 22:42:37,440 go ahead and finish up here and meet me 30497 22:42:37,440 --> 22:42:42,551 in this lecture we're going to learn 30498 22:42:38,872 --> 22:42:43,919 about sizes of class objects and try to 30499 22:42:43,919 --> 22:42:49,440 what are we going to see if we try to 30500 22:42:46,551 --> 22:42:51,919 print the size of our class here called 30501 22:42:49,440 --> 22:42:54,639 wrapper inside we have two member 30502 22:42:51,919 --> 22:42:56,479 variables which are of and type 30503 22:42:54,639 --> 22:43:00,000 and we have a member variable which is 30504 22:42:56,479 --> 22:43:01,360 of std string type what do you think 30505 22:43:01,360 --> 22:43:05,831 when we print the size of this wrapper 30506 22:43:05,831 --> 22:43:11,119 try to think about it so what c plus 30507 22:43:08,160 --> 22:43:13,760 plus does it's going to only account for 30508 22:43:11,119 --> 22:43:15,759 the size of your member variables so 30509 22:43:13,759 --> 22:43:17,511 it's going to add to the sizes of these 30510 22:43:15,759 --> 22:43:19,599 guys in memory so we know an end is 30511 22:43:17,512 --> 22:43:21,440 going to take four bytes in memory so 30512 22:43:19,600 --> 22:43:23,760 we're going to have eight bytes for 30513 22:43:21,440 --> 22:43:26,160 these two ants and we're going to have 30514 22:43:23,759 --> 22:43:28,479 an unspecified amount of memory for a 30515 22:43:26,160 --> 22:43:32,000 strength because a strength is 30516 22:43:28,479 --> 22:43:35,440 internally implemented as a class and it 30517 22:43:32,000 --> 22:43:37,512 stores its data as cost car pointers so 30518 22:43:37,512 --> 22:43:43,919 and the size of an object doesn't really 30519 22:43:41,440 --> 22:43:46,400 account for thanks you are referring to 30520 22:43:43,919 --> 22:43:48,800 using pointers it may count to the size 30521 22:43:46,399 --> 22:43:51,511 of the pointer itself but it's not going 30522 22:43:48,800 --> 22:43:53,279 to count what is pointed to and in the 30523 22:43:51,512 --> 22:43:54,639 case of string for example we really 30524 22:43:54,639 --> 22:43:58,872 the range of things that we are pointing 30525 22:43:57,039 --> 22:44:01,831 to so we're not going to have correct 30526 22:43:58,872 --> 22:44:04,400 information with hdd string here but in 30527 22:44:01,831 --> 22:44:07,360 general the size of an object is going 30528 22:44:04,399 --> 22:44:09,511 to be the sum of the sizes of its member 30529 22:44:07,360 --> 22:44:11,831 variables the functions are not going to 30530 22:44:09,512 --> 22:44:14,160 be counted then the functions are just 30531 22:44:11,831 --> 22:44:16,080 helper functions that live somewhere in 30532 22:44:14,160 --> 22:44:19,120 memory but they're not going to be 30533 22:44:16,080 --> 22:44:21,360 accounted for in the size of your object 30534 22:44:19,119 --> 22:44:23,831 another reason you might think functions 30535 22:44:21,360 --> 22:44:26,232 are not accounted for in the size of the 30536 22:44:23,831 --> 22:44:29,039 object is that really functions are 30537 22:44:26,232 --> 22:44:31,680 affiliated with the class itself 30538 22:44:29,039 --> 22:44:33,759 functions are not affiliated with class 30539 22:44:31,679 --> 22:44:35,359 objects the class is going to be the 30540 22:44:33,759 --> 22:44:38,000 blueprint from which we're going to 30541 22:44:35,360 --> 22:44:39,919 generate thousands of objects if we want 30542 22:44:38,000 --> 22:44:42,639 but the functions or the methods are 30543 22:44:39,919 --> 22:44:45,119 going to be associated to the blueprint 30544 22:44:42,639 --> 22:44:47,679 itself so it really wouldn't make sense 30545 22:44:45,119 --> 22:44:51,119 to associate the size of those functions 30546 22:44:47,679 --> 22:44:53,439 in memory to class objects now that you 30547 22:44:51,119 --> 22:44:54,959 have this idea let's head over to visual 30548 22:44:53,440 --> 22:44:57,512 studio code and play with this a little 30549 22:44:54,960 --> 22:44:59,512 more okay we have saved that the size is 30550 22:44:57,512 --> 22:45:01,919 mostly going to be the sum of the sizes 30551 22:44:59,512 --> 22:45:04,720 for the member variables but this is not 30552 22:45:01,919 --> 22:45:05,759 always correct because of a phenomenon 30553 22:45:07,919 --> 22:45:13,119 binary alignment is a thing most 30554 22:45:10,399 --> 22:45:16,000 compilers do when they have member 30555 22:45:13,119 --> 22:45:18,551 variables of different types and member 30556 22:45:16,000 --> 22:45:20,720 variables that have the size of four 30557 22:45:18,551 --> 22:45:23,599 bytes in memory are going to be stored 30558 22:45:20,720 --> 22:45:26,479 at memory locations that are multiples 30559 22:45:23,600 --> 22:45:29,760 of four and because of this phenomenon 30560 22:45:26,479 --> 22:45:32,000 we might have gaps between our memory if 30561 22:45:29,759 --> 22:45:34,639 we have variables for example that are 30562 22:45:32,000 --> 22:45:36,800 of two bytes in memory and four bytes in 30563 22:45:34,639 --> 22:45:39,679 memory and we're going to have gaps 30564 22:45:36,800 --> 22:45:42,232 inside and the size of the object is 30565 22:45:39,679 --> 22:45:44,799 going to be slightly off the sum we 30566 22:45:42,232 --> 22:45:46,960 might do for the sizes of our object i 30567 22:45:44,800 --> 22:45:48,960 realize this may be confusing we're 30568 22:45:46,960 --> 22:45:50,720 going to head over to visual studio code 30569 22:45:48,960 --> 22:45:53,360 and we're going to see a few examples 30570 22:45:50,720 --> 22:45:56,000 okay here we are in our working folder 30571 22:45:53,360 --> 22:45:58,720 the current project is size of class 30572 22:45:56,000 --> 22:46:01,119 objects we're going to grab our template 30573 22:45:58,720 --> 22:46:03,831 files pretty quick and we're going to 30574 22:46:01,119 --> 22:46:06,479 put them in place and we're going to 30575 22:46:03,831 --> 22:46:08,479 open our project in visual studio code 30576 22:46:06,479 --> 22:46:11,360 by dragging and dropping here we have 30577 22:46:08,479 --> 22:46:12,639 our main cpp file we can close the left 30578 22:46:12,639 --> 22:46:16,551 and we can clean the main function here 30579 22:46:15,039 --> 22:46:19,360 we're going to set up a simple class 30580 22:46:16,551 --> 22:46:22,080 let's call this dog why not we have been 30581 22:46:19,360 --> 22:46:24,320 using dove so let's use this again 30582 22:46:22,080 --> 22:46:26,479 we're going to put our closing semicolon 30583 22:46:24,320 --> 22:46:29,192 we're going to put a public section and 30584 22:46:26,479 --> 22:46:30,959 it's going to just contain a constructor 30585 22:46:29,191 --> 22:46:32,871 and we're going to say dog and we're 30586 22:46:30,960 --> 22:46:35,680 going to use the default and we're going 30587 22:46:32,872 --> 22:46:38,551 to put in a private section and 30588 22:46:35,679 --> 22:46:39,599 in here we're going to put in a size t 30589 22:46:39,600 --> 22:46:45,192 this is the leg count of the dog why not 30590 22:46:42,551 --> 22:46:48,319 let's say we are modeling this okay so 30591 22:46:45,191 --> 22:46:50,551 now we have our dog class in place we 30592 22:46:48,320 --> 22:46:52,080 can create objects of this dog class 30593 22:46:52,080 --> 22:46:57,759 dog one and if we do this we are 30594 22:46:54,551 --> 22:46:59,279 interested in knowing the size of our 30595 22:46:59,279 --> 22:47:06,160 so we are going to say stdc out size of 30596 22:47:03,119 --> 22:47:08,000 dog and save that and uh fingers crossed 30597 22:47:06,160 --> 22:47:10,400 we're going to see the size of our dog 30598 22:47:08,000 --> 22:47:12,639 object here and try to guess what we're 30599 22:47:10,399 --> 22:47:15,511 going to get in the slides we have seen 30600 22:47:12,639 --> 22:47:17,831 that the size is mostly going to be 30601 22:47:15,512 --> 22:47:18,960 the sum of the size of the member 30602 22:47:18,960 --> 22:47:24,000 and let's print the size of size t here 30603 22:47:21,600 --> 22:47:25,832 because size d is our only member 30604 22:47:24,000 --> 22:47:28,479 variable so i think it's going to be 30605 22:47:25,831 --> 22:47:30,231 helpful to see its size printed out 30606 22:47:28,479 --> 22:47:32,319 so we're going to have some context if 30607 22:47:30,232 --> 22:47:34,872 we do that we're going to say size of 30608 22:47:34,872 --> 22:47:38,872 save that and we're going to build this 30609 22:47:36,800 --> 22:47:41,120 program let's do that we're going to 30610 22:47:38,872 --> 22:47:42,232 pass this through gcc you see the both 30611 22:47:42,232 --> 22:47:47,040 and we're going to bring up a terminal 30612 22:47:44,232 --> 22:47:50,160 and run this see what happens okay we 30613 22:47:47,039 --> 22:47:53,439 see size of size t is eight and the size 30614 22:47:50,160 --> 22:47:56,552 of our dark object is eight and this is 30615 22:47:53,440 --> 22:47:59,360 really cool because we see that the size 30616 22:47:56,551 --> 22:48:00,959 of our class object is really the size 30617 22:48:00,960 --> 22:48:05,192 member variables that we have in here 30618 22:48:03,191 --> 22:48:07,191 let's set up another member variable and 30619 22:48:05,191 --> 22:48:10,080 really show you this so we're going to 30620 22:48:07,191 --> 22:48:11,919 have arm account okay so we are just 30621 22:48:11,919 --> 22:48:17,119 i don't aim for this class here to make 30622 22:48:14,639 --> 22:48:20,000 some sense practically we just want to 30623 22:48:17,119 --> 22:48:23,279 see how the member variables affect the 30624 22:48:20,000 --> 22:48:25,600 size of our class object now we have two 30625 22:48:23,279 --> 22:48:27,759 member variables and we're going to try 30626 22:48:25,600 --> 22:48:30,160 and world we're going to use gcc to do 30627 22:48:27,759 --> 22:48:31,279 that and we're going to clear and run 30628 22:48:31,279 --> 22:48:34,872 now you see that size of doug is 16 and 30629 22:48:34,872 --> 22:48:40,639 proven that the size of an object is 30630 22:48:37,919 --> 22:48:42,959 only accounting for the sizes of the 30631 22:48:40,639 --> 22:48:45,600 member variables it's not going to count 30632 22:48:42,960 --> 22:48:48,320 the functions that we have here so if 30633 22:48:45,600 --> 22:48:50,400 for example we had a function that says 30634 22:48:48,320 --> 22:48:52,800 printful and we have another one that 30635 22:48:50,399 --> 22:48:55,360 says do something this is not going to 30636 22:48:52,800 --> 22:48:58,720 change the size of our object because 30637 22:48:55,360 --> 22:49:01,191 functions aren't accounted for in the 30638 22:49:01,191 --> 22:49:06,551 so let's build and really show you this 30639 22:49:04,399 --> 22:49:08,639 the world is good we can clear and run 30640 22:49:06,551 --> 22:49:11,919 rooster we see that we have the same 30641 22:49:08,639 --> 22:49:14,872 sizes the size of dog is 16 the size of 30642 22:49:11,919 --> 22:49:16,872 size t is eight we can even use object 30643 22:49:14,872 --> 22:49:19,440 names here for example we are using the 30644 22:49:16,872 --> 22:49:20,479 class itself but we can use dog one here 30645 22:49:19,440 --> 22:49:23,040 and we're going to get the same 30646 22:49:20,479 --> 22:49:25,759 information let's both and we're going 30647 22:49:25,759 --> 22:49:30,799 and you see that we have the same size 30648 22:49:28,160 --> 22:49:32,400 so if we change this thing to put in a 30649 22:49:30,800 --> 22:49:35,832 pointer we're going to see something 30650 22:49:32,399 --> 22:49:38,479 else let's put in an end pointer 30651 22:49:35,831 --> 22:49:40,871 and let's say ph like we have been doing 30652 22:49:38,479 --> 22:49:43,279 all along and see what we get and we 30653 22:49:40,872 --> 22:49:45,680 know that the size of a pointer on my 30654 22:49:43,279 --> 22:49:48,551 system let's print that out so that we 30655 22:49:45,679 --> 22:49:51,759 have no doubt let's for example say size 30656 22:49:48,551 --> 22:49:54,399 of end pointer is going to be size of 30657 22:49:51,759 --> 22:49:55,279 end pointer okay let's build we're going 30658 22:49:55,279 --> 22:50:00,399 pass this through gcc and the both is 30659 22:49:58,000 --> 22:50:02,479 going to be good we can clear and run 30660 22:50:00,399 --> 22:50:05,439 rooster we're going to see that we have 30661 22:50:02,479 --> 22:50:07,360 24 because we have two member variables 30662 22:50:05,440 --> 22:50:10,800 that are going to be eight bytes in 30663 22:50:07,360 --> 22:50:13,039 memory of size t they are of type size t 30664 22:50:10,800 --> 22:50:15,919 and we have a pointer which is going to 30665 22:50:13,039 --> 22:50:18,551 have a size of h in memory but we don't 30666 22:50:15,919 --> 22:50:20,399 care what this pointer is pointing to 30667 22:50:18,551 --> 22:50:22,479 if for example this pointer is pointing 30668 22:50:22,479 --> 22:50:26,479 we don't care we're just going to 30669 22:50:24,160 --> 22:50:29,600 account for the end pointer here and 30670 22:50:26,479 --> 22:50:32,231 this is really phenomenal because if for 30671 22:50:29,600 --> 22:50:33,279 example we tried to set up an sdd string 30672 22:50:33,279 --> 22:50:39,191 and by now you should know that std 30673 22:50:35,831 --> 22:50:41,759 strand itself is implemented as a class 30674 22:50:39,191 --> 22:50:44,080 in c plus plus so it's really a class 30675 22:50:41,759 --> 22:50:45,679 and we can print its size so let's set 30676 22:50:44,080 --> 22:50:48,551 up a string object here we're going to 30677 22:50:45,679 --> 22:50:50,799 say std strung and we're going to say 30678 22:50:48,551 --> 22:50:52,800 name and we're going to initialize this 30679 22:50:50,800 --> 22:50:55,192 with hello world okay we're going to see 30680 22:50:52,800 --> 22:50:57,120 something really strange here so if we 30681 22:50:55,191 --> 22:51:00,231 print the size of name here we're going 30682 22:50:57,119 --> 22:51:02,319 to say size of name and we're going to 30683 22:51:00,232 --> 22:51:03,760 print that let's see what we get we're 30684 22:51:05,600 --> 22:51:10,400 see the block is good and can clear and 30685 22:51:08,639 --> 22:51:12,872 run roaster we're going to see that size 30686 22:51:12,872 --> 22:51:18,080 let's change the text inside our string 30687 22:51:16,399 --> 22:51:20,159 and see that we change accordingly 30688 22:51:18,080 --> 22:51:23,919 because this can really be confusing 30689 22:51:20,160 --> 22:51:26,000 let's say i am the king of the universe 30690 22:51:23,919 --> 22:51:28,232 why not and we're going to build again 30691 22:51:26,000 --> 22:51:30,551 let's see what we get fingers crossed 30692 22:51:28,232 --> 22:51:32,720 this is going to blow your mind you see 30693 22:51:30,551 --> 22:51:35,360 we have the same size for name even if 30694 22:51:32,720 --> 22:51:38,000 we are changing the text in here so what 30695 22:51:35,360 --> 22:51:41,440 is really happening the strength is 30696 22:51:38,000 --> 22:51:42,399 storing our text here as cost card 30697 22:51:42,399 --> 22:51:47,679 and what we have is a pointer inside and 30698 22:51:47,679 --> 22:51:52,799 is a member variable which is a pointer 30699 22:51:50,399 --> 22:51:55,599 like we have here so what we will be 30700 22:51:52,800 --> 22:51:57,760 accounting for in the strength class is 30701 22:51:55,600 --> 22:52:00,232 the size of the pointer but it's not the 30702 22:51:57,759 --> 22:52:03,279 size of what is pointed to okay so the 30703 22:52:00,232 --> 22:52:06,000 car pointer may be pointing to thousands 30704 22:52:03,279 --> 22:52:08,160 of characters in memory but as long as 30705 22:52:06,000 --> 22:52:10,232 the compiler is concerned it's just 30706 22:52:08,160 --> 22:52:12,232 going to use the pointer member variable 30707 22:52:10,232 --> 22:52:14,080 and it's going to account for it in the 30708 22:52:12,232 --> 22:52:15,512 size of your object hopefully you can 30709 22:52:15,512 --> 22:52:19,440 the size of our string objects is going 30710 22:52:19,440 --> 22:52:24,400 regardless of how large is this strand 30711 22:52:22,232 --> 22:52:26,320 this is really something you need to 30712 22:52:24,399 --> 22:52:28,551 understand otherwise this is going to 30713 22:52:26,320 --> 22:52:30,960 throw you off as you design your c plus 30714 22:52:28,551 --> 22:52:33,679 plus applications and this is really all 30715 22:52:30,960 --> 22:52:35,512 i had to share in this lecture the size 30716 22:52:33,679 --> 22:52:37,511 of your object is mostly going to be 30717 22:52:37,512 --> 22:52:41,919 member variables you have and the size 30718 22:52:39,679 --> 22:52:44,719 is going to be the sum of the sizes of 30719 22:52:41,919 --> 22:52:46,959 your member variables i would like to 30720 22:52:44,720 --> 22:52:48,479 welcome you in this new chapter where 30721 22:52:46,960 --> 22:52:51,280 we're going to be learning about 30722 22:52:48,479 --> 22:52:54,000 inheritance and inheritance is a feature 30723 22:52:54,000 --> 22:52:59,440 to allow us to build classes in terms of 30724 22:52:59,440 --> 22:53:04,080 and what i mean here is that we can 30725 22:53:01,191 --> 22:53:06,871 build complete hierarchies of classes 30726 22:53:04,080 --> 22:53:09,759 that depend on other classes for example 30727 22:53:06,872 --> 22:53:12,639 if we think of a person as a type we can 30728 22:53:09,759 --> 22:53:15,191 have different types of persons we can 30729 22:53:12,639 --> 22:53:18,232 have a doctor a doctor is a person we 30730 22:53:15,191 --> 22:53:20,720 can have a physician a physician is a 30731 22:53:18,232 --> 22:53:23,120 person we can have an engineer an 30732 22:53:20,720 --> 22:53:25,919 engineer as a person we can have really 30733 22:53:23,119 --> 22:53:28,639 all kinds of persons and we can capture 30734 22:53:25,919 --> 22:53:31,191 these kinds of relationships in c plus 30735 22:53:28,639 --> 22:53:34,160 plus using inheritance for example here 30736 22:53:31,191 --> 22:53:36,551 we have an inheritance hierarchy we can 30737 22:53:34,160 --> 22:53:38,400 set up a person type in our c plus 30738 22:53:36,551 --> 22:53:42,231 program and put in whatever member 30739 22:53:38,399 --> 22:53:44,399 variables we need to define a person for 30740 22:53:42,232 --> 22:53:46,800 whatever purposes we are building our c 30741 22:53:44,399 --> 22:53:49,191 plus plus application for once we have 30742 22:53:46,800 --> 22:53:51,512 this type we can build other types that 30743 22:53:49,191 --> 22:53:54,000 depend on this person for example we can 30744 22:53:51,512 --> 22:53:57,040 build an engineer type which is really 30745 22:53:54,000 --> 22:53:59,191 going to embody the person type inside 30746 22:53:57,039 --> 22:54:02,639 so what we will be doing is really 30747 22:53:59,191 --> 22:54:05,119 setting up a layer of a person type and 30748 22:54:02,639 --> 22:54:08,080 in our engineer type which is going to 30749 22:54:05,119 --> 22:54:10,720 depend on person we will boil the novel 30750 22:54:08,080 --> 22:54:13,360 layer on top of that and really reuse 30751 22:54:10,720 --> 22:54:15,759 that person type without having to 30752 22:54:13,360 --> 22:54:17,512 rebuild this from scratch hopefully you 30753 22:54:15,759 --> 22:54:20,000 can see that this is going to improve 30754 22:54:17,512 --> 22:54:22,080 code reuse we can also reuse this 30755 22:54:20,000 --> 22:54:23,831 engineer type and well the other types 30756 22:54:22,080 --> 22:54:26,320 on top of it for example we can have a 30757 22:54:23,831 --> 22:54:28,399 civil engineer we can have an electrical 30758 22:54:26,320 --> 22:54:31,120 engineer we can have all kinds of crazy 30759 22:54:28,399 --> 22:54:32,159 engineers modeled in our c plus plus 30760 22:54:32,160 --> 22:54:36,552 using the inheritance and this is going 30761 22:54:33,831 --> 22:54:39,039 to be really cool so this is what we are 30762 22:54:36,551 --> 22:54:41,360 going to be exploring in this chapter 30763 22:54:39,039 --> 22:54:44,551 looking at the facilities that c plus 30764 22:54:41,360 --> 22:54:46,720 plus provides to capture these kinds of 30765 22:54:44,551 --> 22:54:49,679 relationships in our c plus plus 30766 22:54:46,720 --> 22:54:51,759 programs please note that inheritance is 30767 22:54:49,679 --> 22:54:54,479 a really big feature and it is a 30768 22:54:51,759 --> 22:54:57,439 defining feature of object oriented 30769 22:54:54,479 --> 22:55:00,319 programming c plus plus object oriented 30770 22:54:57,440 --> 22:55:03,360 programming is a paradigm or a way of 30771 22:55:00,320 --> 22:55:05,920 thinking about programming that uses 30772 22:55:03,360 --> 22:55:08,232 objects and the most basic defining 30773 22:55:05,919 --> 22:55:10,872 feature of object oriented programming 30774 22:55:08,232 --> 22:55:13,040 in c plus plus is classes that we have 30775 22:55:10,872 --> 22:55:15,760 learned about using classes we can 30776 22:55:13,039 --> 22:55:17,831 define things for example we can set up 30777 22:55:15,759 --> 22:55:20,000 a car we can set up a house we can set 30778 22:55:17,831 --> 22:55:22,080 up a spaceship we can do all kinds of 30779 22:55:20,000 --> 22:55:24,551 crazy things with classes and ziplocs 30780 22:55:22,080 --> 22:55:27,360 plus inheritance is going to build on 30781 22:55:24,551 --> 22:55:30,000 top of classes and allow us to build 30782 22:55:27,360 --> 22:55:31,759 classes that depend on each other and 30783 22:55:31,759 --> 22:55:36,159 hierarchies that are going to make our 30784 22:55:34,000 --> 22:55:39,039 design much easier to look at and 30785 22:55:36,160 --> 22:55:41,512 understand and work with okay so this is 30786 22:55:39,039 --> 22:55:43,439 what we are up against in this chapter 30787 22:55:41,512 --> 22:55:46,872 learning about inheritance and really 30788 22:55:43,440 --> 22:55:49,680 exposing you to as much as possible to 30789 22:55:46,872 --> 22:55:52,080 let you use inheritance in an easier way 30790 22:55:49,679 --> 22:55:54,079 in your c plus class programs we are 30791 22:55:52,080 --> 22:55:56,960 going to start in the next lecture and 30792 22:55:54,080 --> 22:55:59,119 try out inheritance for the first time 30793 22:55:56,960 --> 22:56:01,440 go ahead and finish up here and meet me 30794 22:55:59,119 --> 22:56:04,399 there in this lecture we're going to try 30795 22:56:01,440 --> 22:56:05,680 out inheritance for the first time in 30796 22:56:05,679 --> 22:56:11,279 but before we do that let's remind 30797 22:56:07,600 --> 22:56:13,512 ourselves of what inheritance really is 30798 22:56:11,279 --> 22:56:14,479 inheritance is a feature we have in c 30799 22:56:14,479 --> 22:56:20,720 to be able to build new classes in terms 30800 22:56:17,679 --> 22:56:23,039 of the classes we have predefined 30801 22:56:20,720 --> 22:56:25,360 and the new classes are going to be 30802 22:56:23,039 --> 22:56:28,399 stealing features from the already 30803 22:56:25,360 --> 22:56:31,119 pre-existing classes if i can say it 30804 22:56:28,399 --> 22:56:33,511 like that and the more classes we build 30805 22:56:31,119 --> 22:56:36,231 in terms of others we're going to end up 30806 22:56:33,512 --> 22:56:38,232 with what we call an inheritance tree 30807 22:56:36,232 --> 22:56:40,639 which is basically going to go from the 30808 22:56:38,232 --> 22:56:43,360 top to the bottom with the most 30809 22:56:40,639 --> 22:56:45,759 fundamental class on top and the derived 30810 22:56:43,360 --> 22:56:48,000 class at the bottom here we have a 30811 22:56:45,759 --> 22:56:49,511 simple example for example we can have a 30812 22:56:51,600 --> 22:56:56,960 defining the basic features of a person 30813 22:56:54,800 --> 22:57:00,400 but based on that personal class we 30814 22:56:56,960 --> 22:57:03,512 might want to derive other classes that 30815 22:57:00,399 --> 22:57:05,279 build on top of this person class for 30816 22:57:03,512 --> 22:57:07,760 example we might want to define an 30817 22:57:05,279 --> 22:57:10,399 engineer and a doctor and we know that 30818 22:57:07,759 --> 22:57:12,959 an engineer is also a person so they are 30819 22:57:10,399 --> 22:57:15,511 going to inherit some features from the 30820 22:57:12,960 --> 22:57:18,232 person class the same goes for the 30821 22:57:15,512 --> 22:57:21,040 doctor class it is going to inherit 30822 22:57:18,232 --> 22:57:23,600 some features from person but all these 30823 22:57:21,039 --> 22:57:26,159 derived classes are also going to add 30824 22:57:23,600 --> 22:57:28,639 their own features to the person class 30825 22:57:26,160 --> 22:57:30,720 siplus plus allows us to build these 30826 22:57:30,720 --> 22:57:36,232 here is another simple example we have a 30827 22:57:33,831 --> 22:57:39,279 vehicle class to the top of our 30828 22:57:36,232 --> 22:57:42,000 inheritance hierarchy and below this 30829 22:57:39,279 --> 22:57:44,319 class we might derive a track class 30830 22:57:42,000 --> 22:57:46,399 which is also a vehicle we might have a 30831 22:57:44,320 --> 22:57:49,280 minivan we might have a truck we might 30832 22:57:46,399 --> 22:57:52,000 have a pickup truck we may really do all 30833 22:57:49,279 --> 22:57:54,551 kinds of crazy vehicles which are going 30834 22:57:52,000 --> 22:57:57,191 to inherit the basic features from the 30835 22:57:54,551 --> 22:57:58,800 vehicle class and add their own 30836 22:57:58,800 --> 22:58:04,320 on top of the vehicle class so this is 30837 22:58:00,872 --> 22:58:06,800 the basic idea behind inheritance in c 30838 22:58:04,320 --> 22:58:09,040 plus plus so the examples we're going to 30839 22:58:09,039 --> 22:58:12,719 so here is a simple example of what 30840 22:58:11,191 --> 22:58:14,551 we're going to do in this lecture we're 30841 22:58:12,720 --> 22:58:17,040 going to set up a person class which is 30842 22:58:17,039 --> 22:58:21,439 it is going to be defining two member 30843 22:58:18,960 --> 22:58:23,512 variables which are going to be of std 30844 22:58:21,440 --> 22:58:25,832 string type we're going to have a last 30845 22:58:23,512 --> 22:58:27,191 name for the person and the first name 30846 22:58:27,191 --> 22:58:32,871 we are going to derive from this class 30847 22:58:32,872 --> 22:58:37,192 the player class is going to inherit 30848 22:58:35,039 --> 22:58:39,679 these features from the person class so 30849 22:58:37,191 --> 22:58:42,551 it is going to have a last name and a 30850 22:58:39,679 --> 22:58:45,359 first name built into it but that's 30851 22:58:42,551 --> 22:58:48,080 going to be our base or our starting 30852 22:58:45,360 --> 22:58:50,160 point we are going to add to this base 30853 22:58:48,080 --> 22:58:52,872 and add another member variable from 30854 22:58:50,160 --> 22:58:56,232 player and we're going to be having the 30855 22:58:52,872 --> 22:58:58,639 player class which is really a person 30856 22:58:56,232 --> 22:59:01,600 but it is going to add things to person 30857 22:58:58,639 --> 22:59:02,551 to customize on what we can do with a 30858 22:59:02,551 --> 22:59:08,159 that we wouldn't originally be able to 30859 22:59:05,119 --> 22:59:10,639 do or have with a person class this is 30860 22:59:08,160 --> 22:59:12,720 the basic idea okay now that we know 30861 22:59:10,639 --> 22:59:14,479 this we're going to set up our personal 30862 22:59:12,720 --> 22:59:16,960 class which is going to be our base 30863 22:59:14,479 --> 22:59:19,440 class it is going to have our two member 30864 22:59:16,960 --> 22:59:20,639 variables first the name and last name 30865 22:59:19,440 --> 22:59:22,551 we're going to have a bunch of 30866 22:59:20,639 --> 22:59:25,191 constructors and we're going to have a 30867 22:59:22,551 --> 22:59:27,599 stream output operator to be able to 30868 22:59:25,191 --> 22:59:30,399 print this on the console and see our 30869 22:59:27,600 --> 22:59:33,040 information here nothing special but now 30870 22:59:30,399 --> 22:59:36,079 that this class is done we can build 30871 22:59:33,039 --> 22:59:38,231 other classes on top of this class so 30872 22:59:36,080 --> 22:59:40,000 what we can do for example we can derive 30873 22:59:40,000 --> 22:59:45,679 so the syntax is what you see here we 30874 22:59:41,919 --> 22:59:47,831 say class player we say column column 30875 22:59:47,831 --> 22:59:53,191 and specify the class that we are 30876 22:59:50,160 --> 22:59:55,832 inheriting from so the parent class for 30877 22:59:53,191 --> 22:59:58,399 this class is going to be person or in 30878 22:59:55,831 --> 23:00:00,551 other words we're going to be deriving 30879 22:59:58,399 --> 23:00:03,599 from the person class and we're going to 30880 23:00:00,551 --> 23:00:05,679 be doing that publicly okay for now i 30881 23:00:03,600 --> 23:00:07,600 don't want you to worry too much about 30882 23:00:05,679 --> 23:00:10,399 this public keyword we're going to be 30883 23:00:07,600 --> 23:00:12,960 learning this in steps just know that 30884 23:00:10,399 --> 23:00:14,639 this is the syntax you use to derive 30885 23:00:14,639 --> 23:00:19,279 now the moment we do this the player 30886 23:00:17,119 --> 23:00:22,639 class is going to have the person 30887 23:00:19,279 --> 23:00:25,360 information embedded inside but we will 30888 23:00:22,639 --> 23:00:27,279 also have the ability to customize or 30889 23:00:27,279 --> 23:00:31,600 at the level of the player class this is 30890 23:00:29,919 --> 23:00:34,800 what we do we have a private member 30891 23:00:31,600 --> 23:00:36,960 variable and it is going to specify the 30892 23:00:34,800 --> 23:00:39,512 game at the player place we're going to 30893 23:00:36,960 --> 23:00:42,720 have our own set of constructors we can 30894 23:00:39,512 --> 23:00:46,720 really build player objects but they are 30895 23:00:42,720 --> 23:00:49,360 going to have a person part inside that 30896 23:00:46,720 --> 23:00:51,040 is going to store information about this 30897 23:00:51,039 --> 23:00:54,719 you notice we don't have a member 30898 23:00:52,960 --> 23:00:56,400 variable here for the first name we 30899 23:00:54,720 --> 23:00:58,639 don't have a member variable here for 30900 23:00:56,399 --> 23:01:01,191 the last name but that information is 30901 23:00:58,639 --> 23:01:03,759 going to be baked into this player 30902 23:01:01,191 --> 23:01:07,440 object and this is really how 30903 23:01:03,759 --> 23:01:08,959 inheritance helps us out we can derive 30904 23:01:08,960 --> 23:01:14,160 and build on top of them without having 30905 23:01:11,831 --> 23:01:16,720 to redo the fundamental work to build 30906 23:01:14,160 --> 23:01:19,512 our base classes we can just build on 30907 23:01:16,720 --> 23:01:22,232 top of them and really do things faster 30908 23:01:19,512 --> 23:01:24,080 and more efficiently okay so here are a 30909 23:01:22,232 --> 23:01:27,120 few things you need to know about the 30910 23:01:24,080 --> 23:01:29,831 public inheritance that we just did and 30911 23:01:27,119 --> 23:01:32,639 we are saying public inheritance because 30912 23:01:32,639 --> 23:01:37,679 public person this is doing public 30913 23:01:35,512 --> 23:01:40,639 inheritance and we're going to see what 30914 23:01:37,679 --> 23:01:43,599 this gives us so with public inheritance 30915 23:01:40,639 --> 23:01:46,720 derived classes can access and use 30916 23:01:43,600 --> 23:01:49,600 public members of the base class 30917 23:01:46,720 --> 23:01:52,000 but the derived class can't directly 30918 23:01:49,600 --> 23:01:54,800 access private members from the base 30919 23:01:52,000 --> 23:01:58,551 class i want you to let this sync end 30920 23:01:54,800 --> 23:02:01,360 what this means from the player class we 30921 23:01:58,551 --> 23:02:04,231 can't directly access the private member 30922 23:02:01,360 --> 23:02:07,039 variables we had in person remember our 30923 23:02:04,232 --> 23:02:10,639 first name and last name are private to 30924 23:02:07,039 --> 23:02:13,919 person and we can't directly access them 30925 23:02:10,639 --> 23:02:17,512 in any deriving class that is doing 30926 23:02:13,919 --> 23:02:19,831 public inheritance okay so in our player 30927 23:02:17,512 --> 23:02:21,512 class if we try to access the first name 30928 23:02:19,831 --> 23:02:23,360 and the last name we're going to have 30929 23:02:21,512 --> 23:02:25,760 compiler errors because we can't 30930 23:02:23,360 --> 23:02:27,919 directly access them but not to worry 30931 23:02:25,759 --> 23:02:31,191 here we can still go through 30932 23:02:27,919 --> 23:02:32,399 the public getters that we have in the 30933 23:02:32,399 --> 23:02:37,039 to access the private member variables 30934 23:02:34,960 --> 23:02:39,280 from the base class and that's what 30935 23:02:37,039 --> 23:02:41,511 we're going to do in this lecture here 30936 23:02:39,279 --> 23:02:43,360 because we don't have access to more 30937 23:02:41,512 --> 23:02:46,232 powerful tools we're going to learn 30938 23:02:43,360 --> 23:02:48,960 about in the next few lectures the same 30939 23:02:46,232 --> 23:02:50,552 is also going to apply to friends of the 30940 23:02:50,551 --> 23:02:55,759 they are going to have access to private 30941 23:02:52,800 --> 23:02:57,919 members of the derived class but they 30942 23:02:57,919 --> 23:03:04,479 the private members of the base class if 30943 23:03:00,960 --> 23:03:05,832 you try to access the private parts of a 30944 23:03:05,831 --> 23:03:10,639 from a friend of the derived class 30945 23:03:08,639 --> 23:03:11,679 you're going to have a bunch of compiler 30946 23:03:11,679 --> 23:03:15,511 okay now that you know this we're going 30947 23:03:13,279 --> 23:03:17,279 to head over to visual studio code and 30948 23:03:15,512 --> 23:03:18,872 play with this a little more and really 30949 23:03:18,872 --> 23:03:24,080 here we are in our working folder the 30950 23:03:21,440 --> 23:03:26,551 current project is first try on 30951 23:03:24,080 --> 23:03:28,720 inheritance we're going to grab our 30952 23:03:26,551 --> 23:03:30,080 template files and put them in place 30953 23:03:30,080 --> 23:03:34,080 and we are going to open this in visual 30954 23:03:32,000 --> 23:03:36,872 studio code pretty quick by dragging and 30955 23:03:34,080 --> 23:03:38,960 dropping here let's do that properly and 30956 23:03:36,872 --> 23:03:41,360 we're going to have our main cpp file we 30957 23:03:38,960 --> 23:03:43,760 can clean this up a little bit and we're 30958 23:03:41,360 --> 23:03:46,399 going to add our classes the first class 30959 23:03:43,759 --> 23:03:48,479 we're going to add is the person class 30960 23:03:46,399 --> 23:03:52,079 so we are going to create its header 30961 23:03:48,479 --> 23:03:54,551 file and let's do the cpp file i am 30962 23:03:52,080 --> 23:03:56,872 going to grab the code i have lying on 30963 23:03:54,551 --> 23:03:59,512 my drive for the header here i am going 30964 23:03:56,872 --> 23:04:01,120 to put the class in we are including the 30965 23:03:59,512 --> 23:04:03,440 classes we're going to need we need 30966 23:04:01,119 --> 23:04:05,831 string and io stream here because we're 30967 23:04:03,440 --> 23:04:08,000 going to be outputting thanks through 30968 23:04:08,000 --> 23:04:12,399 and down here we have our two member 30969 23:04:10,000 --> 23:04:15,360 variables we have first name and last 30970 23:04:12,399 --> 23:04:17,191 name they are of std string type and we 30971 23:04:15,360 --> 23:04:19,919 have a bunch of constructors here a 30972 23:04:17,191 --> 23:04:22,959 default constructor and one that takes 30973 23:04:19,919 --> 23:04:26,000 two parameters we are taking them by 30974 23:04:22,960 --> 23:04:28,232 copy here this is bad we should at least 30975 23:04:26,000 --> 23:04:29,512 pass them by reference here let's 30976 23:04:31,440 --> 23:04:36,160 and we're going to make the second one a 30977 23:04:33,360 --> 23:04:38,232 reference as well let's do this and we 30978 23:04:36,160 --> 23:04:40,960 have a destructor which is going to do 30979 23:04:38,232 --> 23:04:43,760 nothing because we really aren't doing 30980 23:04:40,960 --> 23:04:46,080 any dynamic memory allocation here we're 30981 23:04:43,759 --> 23:04:47,759 going to head over to the cpp file and 30982 23:04:46,080 --> 23:04:50,160 put in the code for that we're going to 30983 23:04:47,759 --> 23:04:51,831 include the person and we're going to do 30984 23:04:51,831 --> 23:04:56,231 implementations i should say and we're 30985 23:04:54,320 --> 23:04:59,280 going to pass our parameters by 30986 23:04:56,232 --> 23:05:01,440 reference again let's do this and our 30987 23:04:59,279 --> 23:05:05,119 constructor should be done now we have a 30988 23:05:01,440 --> 23:05:08,232 person class that we can use to derive 30989 23:05:05,119 --> 23:05:11,191 other classes and use this class as a 30990 23:05:08,232 --> 23:05:14,480 foundation to build other classes to 30991 23:05:11,191 --> 23:05:16,799 visualize this again our class person is 30992 23:05:14,479 --> 23:05:18,319 going to be the base class for our 30993 23:05:18,320 --> 23:05:22,080 so if we build a player class it's going 30994 23:05:22,080 --> 23:05:27,119 a person part of it which is going to be 30995 23:05:25,191 --> 23:05:29,679 the foundation on top of which we're 30996 23:05:27,119 --> 23:05:31,759 going to build notice that in player we 30997 23:05:29,679 --> 23:05:34,079 don't need to set up last name and first 30998 23:05:31,759 --> 23:05:36,871 name they are going to come from our 30999 23:05:34,080 --> 23:05:39,440 personal part of player all we need to 31000 23:05:36,872 --> 23:05:42,400 do in player is set up the game member 31001 23:05:39,440 --> 23:05:44,960 variable to make him a full player who 31002 23:05:42,399 --> 23:05:47,360 can have the game they happen to be 31003 23:05:44,960 --> 23:05:48,960 playing this is the idea here okay so 31004 23:05:47,360 --> 23:05:50,800 now that we know this we're going to put 31005 23:05:48,960 --> 23:05:54,000 in our player class we're going to 31006 23:05:50,800 --> 23:05:56,720 create the files for it let's do player 31007 23:05:54,000 --> 23:05:58,800 dot h we're going to put this in and 31008 23:05:58,800 --> 23:06:03,600 that cpp we're going to put him and 31009 23:06:01,512 --> 23:06:07,119 we are going to hop over to the header 31010 23:06:03,600 --> 23:06:09,279 file and put in our code the class is 31011 23:06:07,119 --> 23:06:11,599 going to be player and it is going to be 31012 23:06:09,279 --> 23:06:14,959 inheriting from person this is the 31013 23:06:11,600 --> 23:06:17,040 syntax again we say colon colon we say 31014 23:06:14,960 --> 23:06:19,120 the kind of inheritance we want to do in 31015 23:06:17,039 --> 23:06:21,039 this case it is public and then we say 31016 23:06:19,119 --> 23:06:24,231 the class that we are going to be 31017 23:06:21,039 --> 23:06:26,399 inheriting from from this point on the 31018 23:06:24,232 --> 23:06:28,232 player class is going to have a person 31019 23:06:26,399 --> 23:06:30,479 part of it and we are going to prove 31020 23:06:28,232 --> 23:06:32,720 this in a minute when we run this code 31021 23:06:30,479 --> 23:06:35,599 here notice that we can build player 31022 23:06:32,720 --> 23:06:38,000 objects by just passing in the game 31023 23:06:35,600 --> 23:06:40,960 parameter here which is going to be used 31024 23:06:38,000 --> 23:06:43,119 to initialize our member variable let's 31025 23:06:40,960 --> 23:06:44,872 also make it a reference because this is 31026 23:06:43,119 --> 23:06:47,831 good practice it is going to avoid 31027 23:06:44,872 --> 23:06:50,080 copies you can even use string views but 31028 23:06:47,831 --> 23:06:52,551 that's going to be your choice 31029 23:06:50,080 --> 23:06:55,680 we are going to hop over to the cpp file 31030 23:06:52,551 --> 23:06:58,080 and put in our implementation 31031 23:06:55,679 --> 23:07:00,399 and this is going to complete our setup 31032 23:06:58,080 --> 23:07:03,680 for the player class which is going to 31033 23:07:00,399 --> 23:07:06,799 be inheriting from our person class here 31034 23:07:03,679 --> 23:07:09,919 we can actually head over to the main 31035 23:07:06,800 --> 23:07:11,760 cpp file and include the player class 31036 23:07:09,919 --> 23:07:14,160 let's do this we're going to say player 31037 23:07:11,759 --> 23:07:16,959 and we can create player objects let's 31038 23:07:14,160 --> 23:07:18,720 look at the code again for a player we 31039 23:07:16,960 --> 23:07:21,680 have a constructor which is going to 31040 23:07:18,720 --> 23:07:24,720 take a string parameter we can use this 31041 23:07:21,679 --> 23:07:26,551 to create a player and specify the game 31042 23:07:27,279 --> 23:07:33,440 for now we don't have enough tools to 31043 23:07:30,800 --> 23:07:36,320 put in the data that would initialize 31044 23:07:33,440 --> 23:07:38,479 the member variables in the base class 31045 23:07:36,320 --> 23:07:39,920 which is person here if we go back to 31046 23:07:38,479 --> 23:07:42,159 person we're going to see that we have 31047 23:07:39,919 --> 23:07:44,800 the first name and the last name 31048 23:07:44,800 --> 23:07:49,760 these member variables are going to be 31049 23:07:46,720 --> 23:07:52,720 inherited because a player is inheriting 31050 23:07:49,759 --> 23:07:55,919 from a person this is the syntax to do 31051 23:07:52,720 --> 23:07:58,872 that but even if the player class has a 31052 23:07:55,919 --> 23:08:02,479 person part inside of it we can't really 31053 23:07:58,872 --> 23:08:05,600 access private members from person 31054 23:08:02,479 --> 23:08:08,399 from a player object we're going to see 31055 23:08:05,600 --> 23:08:11,040 a few ways we can get around this and 31056 23:08:08,399 --> 23:08:13,831 one of those is to go public methods 31057 23:08:11,039 --> 23:08:16,000 from the person class to access 31058 23:08:13,831 --> 23:08:17,599 private parts of the person class this 31059 23:08:16,000 --> 23:08:20,872 is going to give us access to these 31060 23:08:17,600 --> 23:08:23,279 member variables if you go down in our 31061 23:08:20,872 --> 23:08:26,000 stream output operator you're going to 31062 23:08:23,279 --> 23:08:28,479 see that we are using these getters here 31063 23:08:26,000 --> 23:08:31,279 the parameter is a player objects that 31064 23:08:28,479 --> 23:08:33,679 we want to print out but to get access 31065 23:08:31,279 --> 23:08:36,319 to the names we are getting the first 31066 23:08:33,679 --> 23:08:39,039 name and the last name through our 31067 23:08:36,320 --> 23:08:41,600 getters here before we really explore 31068 23:08:39,039 --> 23:08:44,079 this any further i think it is a good 31069 23:08:41,600 --> 23:08:46,480 idea to go in the main cpp file and 31070 23:08:44,080 --> 23:08:48,400 create a player object so we're going to 31071 23:08:46,479 --> 23:08:50,959 do that we're going to say p1 and we're 31072 23:08:48,399 --> 23:08:54,231 going to specify that they play 31073 23:08:50,960 --> 23:08:56,080 basketball why not okay so we have our 31074 23:08:56,080 --> 23:09:00,479 and we can really do all we want with 31075 23:08:58,000 --> 23:09:03,039 that for example we can print the 31076 23:09:00,479 --> 23:09:05,599 information about this player object if 31077 23:09:03,039 --> 23:09:07,679 you remember in player we have a stream 31078 23:09:05,600 --> 23:09:09,600 output operator which is going to print 31079 23:09:09,600 --> 23:09:16,000 and here we have a problem because this 31080 23:09:12,720 --> 23:09:18,232 guy is taking the data by reference 31081 23:09:16,000 --> 23:09:20,551 and this is not a reference this is a 31082 23:09:18,232 --> 23:09:21,512 temporary object that we are passing 31083 23:09:21,512 --> 23:09:27,600 so we could go back and change this to 31084 23:09:24,551 --> 23:09:29,599 take a string view parameter 31085 23:09:27,600 --> 23:09:32,480 let's go back and do that we're going to 31086 23:09:29,600 --> 23:09:34,232 include a string view here in player and 31087 23:09:32,479 --> 23:09:37,759 we're going to change our parameter to 31088 23:09:34,232 --> 23:09:40,720 be std string view by value and we're 31089 23:09:37,759 --> 23:09:43,360 going to do that in the cpp file let's 31090 23:09:40,720 --> 23:09:46,720 do that we're going to say std string 31091 23:09:43,360 --> 23:09:48,399 view and this should take care of our 31092 23:09:46,720 --> 23:09:50,720 problem here you see that the problem 31093 23:09:48,399 --> 23:09:53,599 goes away now that we have this guy we 31094 23:09:50,720 --> 23:09:56,399 can print it out we can say sddc out 31095 23:09:53,600 --> 23:09:58,552 player and i put the information out 31096 23:09:56,399 --> 23:10:01,191 okay so now that we have this we cannot 31097 23:09:58,551 --> 23:10:02,872 try and build our program we're going to 31098 23:10:02,872 --> 23:10:07,279 our favorite compiler and you're going 31099 23:10:05,119 --> 23:10:10,399 to see that we have a problem undefined 31100 23:10:07,279 --> 23:10:12,399 reference to player destructor let's go 31101 23:10:10,399 --> 23:10:15,679 back in the header and see that we have 31102 23:10:12,399 --> 23:10:18,959 a destructor but we probably didn't put 31103 23:10:15,679 --> 23:10:22,000 a destructor in the cpp file so let's do 31104 23:10:18,960 --> 23:10:24,160 that or because we're not doing any 31105 23:10:22,000 --> 23:10:27,039 dynamic memory allocation we can even 31106 23:10:24,160 --> 23:10:28,720 completely take out this destructor and 31107 23:10:27,039 --> 23:10:31,039 we're just going to use the one the 31108 23:10:28,720 --> 23:10:32,080 compiler generates for us this is going 31109 23:10:32,080 --> 23:10:37,512 much easier to handle in our code so we 31110 23:10:34,479 --> 23:10:40,872 can build again with gcc okay the world 31111 23:10:37,512 --> 23:10:43,119 is good we can run this program 31112 23:10:40,872 --> 23:10:44,800 and let's see if we have a powershell 31113 23:10:43,119 --> 23:10:47,599 window we don't we're going to bring up 31114 23:10:44,800 --> 23:10:49,360 a new one and we're going to run rooster 31115 23:10:47,600 --> 23:10:52,639 we should see information about our 31116 23:10:49,360 --> 23:10:55,279 player so player the game is basketball 31117 23:10:52,639 --> 23:10:58,232 the names are mysterious person because 31118 23:10:55,279 --> 23:11:00,551 that's the default data we inherit from 31119 23:10:58,232 --> 23:11:02,232 a person if you go back to person you're 31120 23:11:02,232 --> 23:11:08,160 the default data in first name is 31121 23:11:05,279 --> 23:11:11,279 mysterious the default data in last name 31122 23:11:08,160 --> 23:11:12,639 is person and it is what we see printed 31123 23:11:12,639 --> 23:11:18,720 in our powershell window when we print 31124 23:11:15,191 --> 23:11:20,399 information about our player object 31125 23:11:18,720 --> 23:11:23,191 another thing i want you to see is that 31126 23:11:23,191 --> 23:11:28,551 that any player object is going to have 31127 23:11:28,551 --> 23:11:34,159 backed in and that's going to be storing 31128 23:11:31,279 --> 23:11:36,720 the information that is modeled into our 31129 23:11:34,160 --> 23:11:39,832 person class and we can use a debugger 31130 23:11:36,720 --> 23:11:43,512 to do that for this to work i am going 31131 23:11:39,831 --> 23:11:45,679 to put a break point here on line 8 31132 23:11:43,512 --> 23:11:48,080 which is printing things out 31133 23:11:45,679 --> 23:11:50,799 this break point is going to be hit 31134 23:11:48,080 --> 23:11:53,919 after our object is set up and we can 31135 23:11:50,800 --> 23:11:55,192 really see the data inside this guy here 31136 23:11:53,919 --> 23:11:57,679 so what we're going to do we're going to 31137 23:11:57,679 --> 23:12:01,679 and we're going to use g plus plus we're 31138 23:11:59,919 --> 23:12:04,160 going to use the debugging tools that 31139 23:12:01,679 --> 23:12:05,511 come with gcc because this is our 31140 23:12:04,160 --> 23:12:06,872 favorite we're going to hit the green 31141 23:12:06,872 --> 23:12:12,000 this is going to start our debugging 31142 23:12:09,191 --> 23:12:14,000 session a binary is going to be built 31143 23:12:12,000 --> 23:12:16,160 and it is going to be plugged into our 31144 23:12:14,000 --> 23:12:17,360 debugger and we're going to hit a break 31145 23:12:17,360 --> 23:12:22,232 now if you look on the left here you're 31146 23:12:19,191 --> 23:12:24,000 going to see a title that says locals 31147 23:12:22,232 --> 23:12:27,040 we're going to expand this and we're 31148 23:12:24,000 --> 23:12:29,919 going to see that the p1 object actually 31149 23:12:27,039 --> 23:12:32,000 has a person part of it and you see that 31150 23:12:29,919 --> 23:12:34,639 visual studio code is being helpful here 31151 23:12:32,000 --> 23:12:36,320 it is saying that this is the base class 31152 23:12:34,639 --> 23:12:38,319 and if we expand we're going to see that 31153 23:12:36,320 --> 23:12:41,360 we have our member variables we have 31154 23:12:38,320 --> 23:12:43,680 first name and last name and this is a 31155 23:12:41,360 --> 23:12:46,639 player object that we are looking at 31156 23:12:43,679 --> 23:12:49,599 don't forget that and on top of our 31157 23:12:46,639 --> 23:12:51,919 person part we also have our own member 31158 23:12:49,600 --> 23:12:53,760 variable which is the game that we play 31159 23:12:51,919 --> 23:12:55,119 and it is right here hopefully this 31160 23:12:55,119 --> 23:13:02,231 that any derived class object is going 31161 23:12:58,479 --> 23:13:04,399 to have a base part embedded into it and 31162 23:13:02,232 --> 23:13:06,552 that's going to be the foundation on top 31163 23:13:04,399 --> 23:13:08,799 of which the derived class is going to 31164 23:13:06,551 --> 23:13:11,191 be building its own tank this is the 31165 23:13:08,800 --> 23:13:13,512 basic idea here okay now that you've 31166 23:13:11,191 --> 23:13:14,639 seen this we're going to kill our debug 31167 23:13:14,639 --> 23:13:19,679 and we're going to see some of the 31168 23:13:17,600 --> 23:13:21,760 things you need to be careful about when 31169 23:13:19,679 --> 23:13:23,679 doing public inheritance like we are 31170 23:13:21,759 --> 23:13:26,319 doing here the first thing i want you to 31171 23:13:23,679 --> 23:13:27,919 see is that you don't have access to 31172 23:13:27,919 --> 23:13:31,759 of the base class if you go back to 31173 23:13:31,759 --> 23:13:36,231 let's kill this so that you're not 31174 23:13:33,759 --> 23:13:38,231 confused if we go back to our personal 31175 23:13:36,232 --> 23:13:41,040 class we're going to see that our first 31176 23:13:38,232 --> 23:13:44,400 name and last name are private 31177 23:13:41,039 --> 23:13:45,439 so they want to be accessible to any 31178 23:13:45,440 --> 23:13:50,800 that does public inheritance like we are 31179 23:13:48,639 --> 23:13:53,279 doing here they are just going to be 31180 23:13:50,800 --> 23:13:56,800 private they are going to be part of the 31181 23:13:53,279 --> 23:13:59,360 class but the derived class would have 31182 23:13:56,800 --> 23:14:02,551 access to them this is the idea here and 31183 23:13:59,360 --> 23:14:05,440 if we go in our output stream operator 31184 23:14:02,551 --> 23:14:08,159 and comment out what we have here and 31185 23:14:05,440 --> 23:14:10,479 try to directly access these member 31186 23:14:08,160 --> 23:14:13,440 variables let's do that i am going to 31187 23:14:10,479 --> 23:14:16,720 copy the code here and put that on top 31188 23:14:13,440 --> 23:14:19,919 and we are not going to go through the 31189 23:14:16,720 --> 23:14:22,960 getter methods from the base class we 31190 23:14:19,919 --> 23:14:25,119 are going to try and access m first name 31191 23:14:22,960 --> 23:14:26,960 let's go back and look at the names for 31192 23:14:25,119 --> 23:14:29,191 the member variables it is first name 31193 23:14:26,960 --> 23:14:31,360 and last name let's use that we're going 31194 23:14:29,191 --> 23:14:34,319 to say first name you see that through 31195 23:14:31,360 --> 23:14:36,639 intellisense but trying to access that 31196 23:14:34,320 --> 23:14:38,400 is going to give us squiggly lines you 31197 23:14:36,639 --> 23:14:40,872 see that we have this in our 31198 23:14:38,399 --> 23:14:42,799 autocomplete but we have squiggly lines 31199 23:14:40,872 --> 23:14:45,680 we can't access these if we try to 31200 23:14:42,800 --> 23:14:48,160 compile our program let's do that again 31201 23:14:45,679 --> 23:14:50,231 we're going to use gcc and we are going 31202 23:14:48,160 --> 23:14:52,872 to get compiler errors and the compiler 31203 23:14:50,232 --> 23:14:55,120 errors are basically going to say 31204 23:14:52,872 --> 23:14:58,160 first name is private within this 31205 23:14:55,119 --> 23:15:02,000 context again the meaning is that 31206 23:14:58,160 --> 23:15:04,639 first name is private to the base class 31207 23:15:02,000 --> 23:15:07,440 and we have no business accessing that 31208 23:15:07,440 --> 23:15:12,000 the player class here this is the 31209 23:15:09,279 --> 23:15:14,479 meaning so we can't really access that 31210 23:15:12,000 --> 23:15:16,872 okay i realize this is a global function 31211 23:15:14,479 --> 23:15:19,440 but even if we try to do that from a 31212 23:15:16,872 --> 23:15:22,080 member function let's do that from 31213 23:15:19,440 --> 23:15:24,720 a constructor we're going to try and say 31214 23:15:22,080 --> 23:15:27,279 first name okay i realize the access we 31215 23:15:24,720 --> 23:15:29,440 are trying to do from the output stream 31216 23:15:27,279 --> 23:15:32,319 operator is really not a good example 31217 23:15:29,440 --> 23:15:35,119 because this is not a member function it 31218 23:15:32,320 --> 23:15:37,280 is a global freestanding function so we 31219 23:15:35,119 --> 23:15:38,959 can try and change the first name and 31220 23:15:37,279 --> 23:15:40,959 last name from the constructor for 31221 23:15:38,960 --> 23:15:42,960 example and we're going to see that this 31222 23:15:40,960 --> 23:15:45,680 is not going to work either so we can 31223 23:15:42,960 --> 23:15:48,400 take this out and we're going to 31224 23:15:45,679 --> 23:15:51,119 remove our block comma so we're going to 31225 23:15:48,399 --> 23:15:53,119 take out what we just did here 31226 23:15:51,119 --> 23:15:55,512 and we're going to uncomment our good 31227 23:15:53,119 --> 23:15:57,679 code here which is using getter member 31228 23:15:55,512 --> 23:15:59,680 functions from the base class and we're 31229 23:15:59,679 --> 23:16:05,279 the first name and last name from the 31230 23:16:01,919 --> 23:16:07,759 base class and change those into our 31231 23:16:05,279 --> 23:16:09,440 constructor of the derived class we're 31232 23:16:07,759 --> 23:16:12,000 going to try and change for example the 31233 23:16:09,440 --> 23:16:14,232 first name and make it john and we're 31234 23:16:12,000 --> 23:16:15,512 going to take the last name and change 31235 23:16:15,512 --> 23:16:20,800 snow let's do that and let's go back and 31236 23:16:18,000 --> 23:16:22,479 make sure we have the correct names 31237 23:16:20,800 --> 23:16:24,720 and you see that we have squiggly lines 31238 23:16:22,479 --> 23:16:27,360 we can't really do this because these 31239 23:16:24,720 --> 23:16:30,399 are private members of the base class 31240 23:16:27,360 --> 23:16:33,279 and we can't access them from a derived 31241 23:16:30,399 --> 23:16:35,279 class they are part of the player class 31242 23:16:33,279 --> 23:16:37,679 which is a derived class but we don't 31243 23:16:35,279 --> 23:16:40,080 have access to them we can't modify them 31244 23:16:37,679 --> 23:16:41,759 or do anything we can't read from them 31245 23:16:40,080 --> 23:16:43,919 we can't write from them this is the 31246 23:16:41,759 --> 23:16:46,231 message here if we try to build our 31247 23:16:43,919 --> 23:16:47,191 program we're going to use gcc to do 31248 23:16:47,191 --> 23:16:53,360 we are going to get compiler errors and 31249 23:16:49,512 --> 23:16:55,680 uh let's see the compiler error we get 31250 23:16:53,360 --> 23:16:58,800 again the error is that first name and 31251 23:16:55,679 --> 23:17:01,439 last name are private to the base class 31252 23:16:58,800 --> 23:17:03,680 and we have no business accessing them 31253 23:17:01,440 --> 23:17:06,160 from a derived class like we have here 31254 23:17:03,679 --> 23:17:07,191 so if you need any kind of access to a 31255 23:17:07,191 --> 23:17:11,512 member of the base class you will have 31256 23:17:11,512 --> 23:17:15,832 something that is public and a good 31257 23:17:13,512 --> 23:17:17,600 example is getter methods which are 31258 23:17:17,600 --> 23:17:22,480 change things so for example if we want 31259 23:17:20,080 --> 23:17:24,232 the ability to change the first name and 31260 23:17:22,479 --> 23:17:26,319 the last name we can set up a bunch of 31261 23:17:24,232 --> 23:17:28,800 setter methods let's do that we're going 31262 23:17:26,320 --> 23:17:29,832 to say setters and we're going to say 31263 23:17:30,872 --> 23:17:35,760 first name and we are going to pass an 31264 23:17:35,759 --> 23:17:41,119 let's do that we're going to say string 31265 23:17:41,119 --> 23:17:46,799 this is going to modify our class so we 31266 23:17:44,551 --> 23:17:48,399 are not going to mark this as a custom 31267 23:17:46,800 --> 23:17:49,680 member function and we're going to say 31268 23:17:53,831 --> 23:17:59,439 we are going to set up a setter for the 31269 23:17:56,872 --> 23:18:01,760 last name set and we're going to pass a 31270 23:17:59,440 --> 23:18:04,232 string view here and we're going to say 31271 23:18:01,759 --> 23:18:06,399 ln for the last name again this is not 31272 23:18:04,232 --> 23:18:09,279 going to be a cast member function so 31273 23:18:06,399 --> 23:18:13,279 we're going to say last name and we can 31274 23:18:09,279 --> 23:18:16,319 pass in ln as our new name here now that 31275 23:18:13,279 --> 23:18:18,959 we have these end we can take out 31276 23:18:16,320 --> 23:18:21,600 these things we have in the constructor 31277 23:18:18,960 --> 23:18:23,280 i think the job for this is done and 31278 23:18:21,600 --> 23:18:26,400 this is going to give you compiler 31279 23:18:23,279 --> 23:18:28,080 errors and we can go in our main cpp 31280 23:18:26,399 --> 23:18:31,439 file and change the names for this 31281 23:18:28,080 --> 23:18:32,320 person here for example we can say p1 31282 23:18:32,320 --> 23:18:37,440 first name we're going to pass john and 31283 23:18:35,600 --> 23:18:40,160 we can also set to the last name we're 31284 23:18:40,160 --> 23:18:44,960 last name and we're going to pass snow 31285 23:18:43,039 --> 23:18:46,799 and this is going to do if we build and 31286 23:18:44,960 --> 23:18:48,720 run this program we're going to see that 31287 23:18:46,800 --> 23:18:50,232 the world is going to be good this is 31288 23:18:50,232 --> 23:18:54,552 and if we run we should see the 31289 23:18:52,232 --> 23:18:57,512 information printed out we want to join 31290 23:18:54,551 --> 23:18:58,800 snow printed out and the game should be 31291 23:18:58,800 --> 23:19:04,320 let's clear and run rooster we're going 31292 23:19:01,440 --> 23:19:06,720 to see that the data is in here the name 31293 23:19:04,320 --> 23:19:09,600 the names are jon snow and we have our 31294 23:19:06,720 --> 23:19:12,160 game to be basketball here but notice 31295 23:19:09,600 --> 23:19:15,680 these are base methods and we are 31296 23:19:12,160 --> 23:19:18,480 calling them from a derived object and 31297 23:19:15,679 --> 23:19:20,719 this is living proof that the public 31298 23:19:18,479 --> 23:19:23,360 members from the base class are going to 31299 23:19:23,360 --> 23:19:29,039 from a derived class that does public 31300 23:19:26,960 --> 23:19:31,280 inheritance like we are doing here and 31301 23:19:29,039 --> 23:19:33,599 this is really the main message you can 31302 23:19:31,279 --> 23:19:36,319 inherit publicly and have access to 31303 23:19:33,600 --> 23:19:39,760 public stuff from the base class and use 31304 23:19:36,320 --> 23:19:42,080 them in your derived class but private 31305 23:19:39,759 --> 23:19:44,639 data is going to still be private to the 31306 23:19:42,080 --> 23:19:47,119 base class you can't use that in a 31307 23:19:44,639 --> 23:19:49,039 derived class this is the message here 31308 23:19:47,119 --> 23:19:52,000 and this is really all i wanted you to 31309 23:19:49,039 --> 23:19:54,159 see in this lecture the big thing is to 31310 23:19:52,000 --> 23:19:57,440 notice the syntax you use to do 31311 23:19:54,160 --> 23:19:59,832 inheritance and if you do this the 31312 23:19:57,440 --> 23:20:00,800 player class is going to be derived from 31313 23:20:00,800 --> 23:20:06,232 and it will have a person part 31314 23:20:03,919 --> 23:20:09,039 baked inside and we can build on that 31315 23:20:06,232 --> 23:20:11,440 foundation and for example put in our 31316 23:20:09,039 --> 23:20:12,959 own member variables and do all kinds of 31317 23:20:12,960 --> 23:20:17,680 public stuff from the base class are 31318 23:20:14,960 --> 23:20:20,160 going to be inherited and usable from a 31319 23:20:17,679 --> 23:20:22,639 derived class we just saw that by 31320 23:20:20,160 --> 23:20:25,360 setting up a bunch of setters that live 31321 23:20:22,639 --> 23:20:28,551 in the public scope of the base class 31322 23:20:25,360 --> 23:20:31,680 and they were usable from the outside 31323 23:20:28,551 --> 23:20:34,399 because they will be inherited as public 31324 23:20:31,679 --> 23:20:36,479 in a derived class so we can really call 31325 23:20:34,399 --> 23:20:38,720 these methods and use them from the 31326 23:20:36,479 --> 23:20:41,119 outside like we are doing here but 31327 23:20:38,720 --> 23:20:43,600 private data from the base class is not 31328 23:20:41,119 --> 23:20:46,231 going to be accessible and usable from a 31329 23:20:43,600 --> 23:20:48,320 derived class and we saw that by setting 31330 23:20:46,232 --> 23:20:50,000 up this piece of code in the constructor 31331 23:20:48,320 --> 23:20:52,480 of player this is going to give us a 31332 23:20:50,000 --> 23:20:55,279 compiler error saying that first name 31333 23:20:52,479 --> 23:20:57,759 and last name are private to the base 31334 23:20:55,279 --> 23:20:59,360 class we are going to stop here in this 31335 23:20:57,759 --> 23:21:01,439 lecture in the next one we're going to 31336 23:20:59,360 --> 23:21:04,551 learn about another kind of access 31337 23:21:01,440 --> 23:21:06,639 specifier we have in c plus plus and 31338 23:21:06,639 --> 23:21:10,319 so go ahead and finish up here and meet 31339 23:21:10,320 --> 23:21:15,920 in this lecture we're going to learn 31340 23:21:12,000 --> 23:21:18,720 about the protected access specifier you 31341 23:21:15,919 --> 23:21:20,639 can use in your c plus plus class 31342 23:21:18,720 --> 23:21:22,232 and we're going to be reusing the same 31343 23:21:20,639 --> 23:21:23,919 example from the last lecture we're 31344 23:21:22,232 --> 23:21:26,552 going to have a person class which is 31345 23:21:23,919 --> 23:21:28,872 going to act as our base class and we're 31346 23:21:26,551 --> 23:21:32,080 going to be deriving from this person 31347 23:21:28,872 --> 23:21:33,760 and creating a new class called player 31348 23:21:32,080 --> 23:21:35,919 the player class is going to have a 31349 23:21:35,919 --> 23:21:40,959 and we can use this as a foundation to 31350 23:21:38,551 --> 23:21:42,800 build other fans in player class for 31351 23:21:40,960 --> 23:21:45,120 example we can set up our own member 31352 23:21:42,800 --> 23:21:47,600 variables and do all kinds of crazy 31353 23:21:45,119 --> 23:21:49,831 things and this is the setup we used in 31354 23:21:47,600 --> 23:21:52,872 the last lecture we set up a person 31355 23:21:49,831 --> 23:21:55,191 class and our members were private as 31356 23:21:52,872 --> 23:21:56,320 you saw here but in the last lecture we 31357 23:21:56,320 --> 23:22:01,192 private member variables are not going 31358 23:21:58,320 --> 23:22:05,192 to be accessible and usable from derived 31359 23:22:01,191 --> 23:22:07,679 classes that do public inheritance and 31360 23:22:05,191 --> 23:22:10,231 sometimes we want members from the base 31361 23:22:07,679 --> 23:22:14,319 class to at least be accessible and 31362 23:22:10,232 --> 23:22:17,279 usable from a derived class but still be 31363 23:22:14,320 --> 23:22:20,160 inaccessible from the outside and we can 31364 23:22:17,279 --> 23:22:22,639 achieve this using the protected access 31365 23:22:20,160 --> 23:22:24,800 specifier so for example we can change 31366 23:22:22,639 --> 23:22:27,759 the access specifier of our member 31367 23:22:24,800 --> 23:22:30,320 variables from private to protected and 31368 23:22:27,759 --> 23:22:32,959 at the moment we do this we will be able 31369 23:22:30,320 --> 23:22:35,920 to access for example read from and 31370 23:22:32,960 --> 23:22:38,552 write into these member variables from 31371 23:22:35,919 --> 23:22:41,440 derived classes so for example we will 31372 23:22:38,551 --> 23:22:44,319 be able to modify these guys from a 31373 23:22:41,440 --> 23:22:46,232 constructor of the derived class and we 31374 23:22:44,320 --> 23:22:48,960 thought that that was impossible from 31375 23:22:46,232 --> 23:22:51,440 the last lecture but still we want to be 31376 23:22:48,960 --> 23:22:54,080 able to use these from the outside of 31377 23:22:51,440 --> 23:22:55,600 our derived class which is person here 31378 23:22:54,080 --> 23:22:57,919 so we're going to still do public 31379 23:22:55,600 --> 23:23:00,400 inheritance like from the last lecture 31380 23:22:57,919 --> 23:23:03,191 but now that our member variables in the 31381 23:23:00,399 --> 23:23:05,360 base class are protected we will have 31382 23:23:03,191 --> 23:23:07,831 the ability to do whatever we want with 31383 23:23:05,360 --> 23:23:10,720 them in the derived class and when we 31384 23:23:07,831 --> 23:23:13,279 say in the derived class we mean from 31385 23:23:10,720 --> 23:23:15,119 any method from the derived class for 31386 23:23:13,279 --> 23:23:18,319 example we can go in our constructor 31387 23:23:15,119 --> 23:23:20,639 here and then set data into the member 31388 23:23:18,320 --> 23:23:23,192 variables from the base class and that's 31389 23:23:20,639 --> 23:23:25,039 going to work as we're going to see 31390 23:23:23,191 --> 23:23:26,959 in a minute now that you know this we're 31391 23:23:25,039 --> 23:23:29,039 going to head over to visual studio code 31392 23:23:26,960 --> 23:23:31,440 and play with this a little more okay 31393 23:23:29,039 --> 23:23:34,079 again our member variables from the base 31394 23:23:31,440 --> 23:23:36,551 class are going to be protected 31395 23:23:34,080 --> 23:23:38,800 okay so we can really say that here 31396 23:23:36,551 --> 23:23:41,440 let's say protected and at the moment we 31397 23:23:38,800 --> 23:23:44,080 do this they are going to be accessible 31398 23:23:41,440 --> 23:23:46,232 and usable from a derived class so from 31399 23:23:44,080 --> 23:23:48,160 the derived class we will have the first 31400 23:23:48,160 --> 23:23:52,960 so from the derived class we will have 31401 23:23:50,320 --> 23:23:55,440 our members baked then we will have a 31402 23:23:52,960 --> 23:23:57,832 last name and the first name and because 31403 23:23:55,440 --> 23:24:00,551 they are protected in the base class and 31404 23:23:57,831 --> 23:24:02,799 we are doing public inheritance we will 31405 23:24:00,551 --> 23:24:04,720 be able for example to set data into 31406 23:24:02,800 --> 23:24:06,479 these member variables from a derived 31407 23:24:06,479 --> 23:24:10,551 really cool now that you know this we're 31408 23:24:08,479 --> 23:24:12,399 going to head over to visual studio code 31409 23:24:10,551 --> 23:24:15,599 and play with us a little more okay so 31410 23:24:12,399 --> 23:24:17,831 here we are in our here we are in our 31411 23:24:15,600 --> 23:24:20,160 working folder the current project is 31412 23:24:17,831 --> 23:24:22,319 protected members we're going to grab 31413 23:24:20,160 --> 23:24:24,720 the code from the last lecture and reuse 31414 23:24:22,320 --> 23:24:27,680 that because that's going to have our 31415 23:24:24,720 --> 23:24:30,551 inheritance hierarchy already set up we 31416 23:24:27,679 --> 23:24:33,119 will have our person and player class 31417 23:24:30,551 --> 23:24:35,759 and we can reuse them we will have our 31418 23:24:33,119 --> 23:24:38,959 person and player classes and we will 31419 23:24:35,759 --> 23:24:40,959 reuse them without needing to completely 31420 23:24:38,960 --> 23:24:43,832 set them up from scratch that's going to 31421 23:24:40,960 --> 23:24:45,832 save us a few seconds so we're going to 31422 23:24:45,831 --> 23:24:49,599 and we are going to open this little guy 31423 23:24:47,831 --> 23:24:51,511 in visual studio code by dragging and 31424 23:24:49,600 --> 23:24:53,760 dropping here and we will have our 31425 23:24:51,512 --> 23:24:56,320 person class which is going to be the 31426 23:24:53,759 --> 23:24:58,399 base class it's going to have our member 31427 23:24:56,320 --> 23:25:00,640 variables which are going to be private 31428 23:24:58,399 --> 23:25:02,399 from the last lecture we will have a 31429 23:25:00,639 --> 23:25:05,440 player class which is going to be 31430 23:25:02,399 --> 23:25:07,191 inherited publicly from the player 31431 23:25:05,440 --> 23:25:09,512 we will have a player class which is 31432 23:25:07,191 --> 23:25:12,080 going to be publicly inheriting from 31433 23:25:09,512 --> 23:25:14,320 person and we have our main cpp file 31434 23:25:12,080 --> 23:25:16,000 which is going to set up a player object 31435 23:25:14,320 --> 23:25:19,440 and try to use it and we're going to see 31436 23:25:16,000 --> 23:25:21,360 how this goes in this lecture so what we 31437 23:25:19,440 --> 23:25:24,639 want to do is to change the access 31438 23:25:21,360 --> 23:25:26,960 specifier in the base class and make 31439 23:25:24,639 --> 23:25:29,360 that protected we can do that we can say 31440 23:25:26,960 --> 23:25:32,080 protect it and this is going to change a 31441 23:25:29,360 --> 23:25:33,191 few things about this class what this is 31442 23:25:33,191 --> 23:25:38,479 if you do public inheritance from this 31443 23:25:35,759 --> 23:25:40,639 person class you will have the ability 31444 23:25:38,479 --> 23:25:43,679 to do whatever you want with these 31445 23:25:40,639 --> 23:25:45,759 member variables from a derived class 31446 23:25:43,679 --> 23:25:47,679 you will have the ability to write into 31447 23:25:45,759 --> 23:25:50,080 these member variables and change the 31448 23:25:47,679 --> 23:25:52,231 data that we have inside you will also 31449 23:25:50,080 --> 23:25:54,232 have the ability to read from them and 31450 23:25:52,232 --> 23:25:56,720 that's something we couldn't do from the 31451 23:25:54,232 --> 23:25:58,639 last lecture and this may come in handy 31452 23:25:56,720 --> 23:26:01,040 sometimes another thing you should know 31453 23:25:58,639 --> 23:26:03,600 is that these members are still not 31454 23:26:01,039 --> 23:26:06,719 going to be accessible from the outside 31455 23:26:03,600 --> 23:26:08,872 so from the standpoint of the main 31456 23:26:06,720 --> 23:26:11,600 function for example here they are going 31457 23:26:08,872 --> 23:26:13,600 to behave as if they are private because 31458 23:26:11,600 --> 23:26:16,872 they want to be accessible from the 31459 23:26:13,600 --> 23:26:20,480 outside they will only be accessible and 31460 23:26:16,872 --> 23:26:23,040 modifiable from derived classes 31461 23:26:20,479 --> 23:26:25,679 that do public inheritance this is 31462 23:26:23,039 --> 23:26:27,919 really important to understand now that 31463 23:26:25,679 --> 23:26:30,479 we have this we can really do all kinds 31464 23:26:27,919 --> 23:26:33,279 of crazy things for example we can go in 31465 23:26:30,479 --> 23:26:35,191 our constructor of player and comment 31466 23:26:33,279 --> 23:26:38,232 what wasn't working from the last 31467 23:26:35,191 --> 23:26:40,319 lecture and this is going to work now so 31468 23:26:38,232 --> 23:26:42,080 let's uncomment this and we're going to 31469 23:26:40,320 --> 23:26:45,120 see that the squiggly lines are going to 31470 23:26:42,080 --> 23:26:47,191 go away and if we build a player object 31471 23:26:45,119 --> 23:26:50,000 using this constructor we're going to 31472 23:26:47,191 --> 23:26:52,479 see that we're going to have jon snow in 31473 23:26:50,000 --> 23:26:54,399 even if we don't set using the setters 31474 23:26:52,479 --> 23:26:55,360 from the base class so we can take these 31475 23:26:55,360 --> 23:26:59,119 and if we print information about this 31476 23:26:57,360 --> 23:27:01,919 player we're going to see the game to be 31477 23:26:59,119 --> 23:27:05,759 basketball and the names are going to be 31478 23:27:01,919 --> 23:27:08,080 john snow because we are setting up this 31479 23:27:05,759 --> 23:27:10,639 in the constructor of player that's no 31480 23:27:08,080 --> 23:27:13,831 longer that's no longer going to be the 31481 23:27:10,639 --> 23:27:17,191 data we get from the constructor of a 31482 23:27:13,831 --> 23:27:20,479 person object which was here 31483 23:27:17,191 --> 23:27:22,720 in the base class let's try to build and 31484 23:27:20,479 --> 23:27:25,831 run this program so that you can really 31485 23:27:22,720 --> 23:27:28,399 see this i just used clan here but it is 31486 23:27:25,831 --> 23:27:31,511 also going to work no big deal here the 31487 23:27:28,399 --> 23:27:34,000 build is going to be good we can clear 31488 23:27:31,512 --> 23:27:36,080 and bring up a powershell window we can 31489 23:27:34,000 --> 23:27:37,759 use to run this program we're going to 31490 23:27:36,080 --> 23:27:40,000 say rooster and you're going to see that 31491 23:27:40,000 --> 23:27:44,639 having the information the game is going 31492 23:27:42,320 --> 23:27:48,080 to be basketball the names are going to 31493 23:27:44,639 --> 23:27:50,551 be jon snow and we are setting this data 31494 23:27:48,080 --> 23:27:53,360 from a derived class and we have the 31495 23:27:50,551 --> 23:27:56,720 ability to do this because these members 31496 23:27:53,360 --> 23:27:59,119 are now protected in the base class so 31497 23:27:56,720 --> 23:28:00,639 they are accessible and usable from a 31498 23:28:00,639 --> 23:28:06,080 now that we have this ability we can 31499 23:28:02,399 --> 23:28:08,399 really change how our class at least the 31500 23:28:06,080 --> 23:28:12,400 derived class works we're going to 31501 23:28:08,399 --> 23:28:14,399 change it to have two more parameters 31502 23:28:12,399 --> 23:28:17,360 and we're going to pass the first name 31503 23:28:14,399 --> 23:28:20,551 and the last name and pass this data 31504 23:28:17,360 --> 23:28:22,551 back into the base class by setting the 31505 23:28:20,551 --> 23:28:24,639 data in the body of the constructor this 31506 23:28:22,551 --> 23:28:26,959 is going to work so let's add our 31507 23:28:24,639 --> 23:28:28,080 parameters we're going to say std string 31508 23:28:30,160 --> 23:28:34,800 first name param and we're going to put 31509 23:28:32,800 --> 23:28:37,360 in another one which is going to also be 31510 23:28:34,800 --> 23:28:39,919 a string view parameter and it's going 31511 23:28:37,360 --> 23:28:42,000 to be last name param we can say that 31512 23:28:39,919 --> 23:28:44,551 and we're going to hop over in our 31513 23:28:42,000 --> 23:28:46,639 implementation let's grab what we just 31514 23:28:44,551 --> 23:28:50,759 added because we will need to put that 31515 23:28:46,639 --> 23:28:53,119 in our cpp file we are going to go in 31516 23:28:50,759 --> 23:28:55,599 player.cpp and we're going to add this 31517 23:28:53,119 --> 23:28:58,000 data we're going to put that here 31518 23:28:55,600 --> 23:29:00,639 we can close the left sidebar to have 31519 23:28:58,000 --> 23:29:03,279 some more breathing room and then we're 31520 23:29:03,279 --> 23:29:07,279 an initializer list here because this is 31521 23:29:05,360 --> 23:29:10,232 really going to be useless we are 31522 23:29:07,279 --> 23:29:11,279 changing data in the body anyway so what 31523 23:29:11,279 --> 23:29:15,679 is say game param or a game and we're 31524 23:29:15,679 --> 23:29:21,039 gamipuram i am really sorry i am not 31525 23:29:18,232 --> 23:29:23,832 following the same convention for the 31526 23:29:21,039 --> 23:29:26,319 naming of my member variables but we can 31527 23:29:23,831 --> 23:29:29,119 change this later so let's say first 31528 23:29:26,320 --> 23:29:31,360 name we are going to pass in first name 31529 23:29:29,119 --> 23:29:33,759 ram and for the last name we're going to 31530 23:29:31,360 --> 23:29:36,639 say last name param and that's going to 31531 23:29:33,759 --> 23:29:40,551 do now we have the ability to go in the 31532 23:29:36,639 --> 23:29:42,720 main cpp file and create player objects 31533 23:29:40,551 --> 23:29:45,119 and specify the first name and the last 31534 23:29:42,720 --> 23:29:47,191 name and this is going to be much more 31535 23:29:45,119 --> 23:29:50,479 flexible so let's go back to the 31536 23:29:47,191 --> 23:29:53,919 constructor and see the order of our 31537 23:29:50,479 --> 23:29:56,872 parameters game first name last name so 31538 23:29:53,919 --> 23:29:58,080 we can come here and say first name is 31539 23:29:58,080 --> 23:30:02,400 john and the last name is going to be 31540 23:30:00,232 --> 23:30:04,552 snow and if we build and run we're going 31541 23:30:02,399 --> 23:30:06,479 to see the information printed out here 31542 23:30:04,551 --> 23:30:08,479 and now we have the flexibility to be 31543 23:30:08,479 --> 23:30:13,679 in a constructor and this is going to 31544 23:30:10,639 --> 23:30:16,720 make the job of building our objects 31545 23:30:13,679 --> 23:30:19,359 much easier and we can forward this data 31546 23:30:16,720 --> 23:30:22,000 back into the base class using the 31547 23:30:19,360 --> 23:30:23,759 protected mechanism that we just learned 31548 23:30:22,000 --> 23:30:26,080 about in this lecture and this is really 31549 23:30:23,759 --> 23:30:28,799 cool so let's build we're going to pass 31550 23:30:26,080 --> 23:30:31,512 this through gcc and the world is going 31551 23:30:28,800 --> 23:30:33,279 to be good we can clear and run rooster 31552 23:30:31,512 --> 23:30:35,600 we're going to see that the data is 31553 23:30:33,279 --> 23:30:36,639 passed back to the base class this is 31554 23:30:36,639 --> 23:30:41,600 really well okay so this is really all i 31555 23:30:38,872 --> 23:30:44,080 wanted you to see in this lecture we can 31556 23:30:41,600 --> 23:30:45,680 set up protected member variables and 31557 23:30:44,080 --> 23:30:49,040 they are going to be accessible and 31558 23:30:45,679 --> 23:30:52,000 usable from any derived class that does 31559 23:30:49,039 --> 23:30:54,639 public inheritance but they want to be 31560 23:30:52,000 --> 23:30:58,232 accessible from the outside so if you 31561 23:30:54,639 --> 23:31:01,119 try and for example say p1 and say 31562 23:30:58,232 --> 23:31:02,872 first name and try to change that to 31563 23:31:01,119 --> 23:31:04,799 something else let's say daniel for 31564 23:31:02,872 --> 23:31:07,279 example you're going to have compiler 31565 23:31:04,800 --> 23:31:09,040 errors because this is not going to work 31566 23:31:07,279 --> 23:31:11,831 so let's try and build and show you the 31567 23:31:09,039 --> 23:31:14,399 compiler error okay so protected members 31568 23:31:11,831 --> 23:31:16,551 are not accessible from the outside 31569 23:31:14,399 --> 23:31:19,759 they will be just accessible from 31570 23:31:16,551 --> 23:31:22,639 derived classes or to be more specific 31571 23:31:19,759 --> 23:31:25,679 they are going to be accessible from 31572 23:31:22,639 --> 23:31:27,600 the members of the derived class and by 31573 23:31:25,679 --> 23:31:29,359 members i mean member functions because 31574 23:31:27,600 --> 23:31:32,080 that's where you will be accessing and 31575 23:31:29,360 --> 23:31:35,360 modifying stuff so let's go back to the 31576 23:31:32,080 --> 23:31:37,600 main cpp file and remove this 31577 23:31:35,360 --> 23:31:39,440 and this is really all i wanted to share 31578 23:31:37,600 --> 23:31:42,320 with you and i hope you found this 31579 23:31:39,440 --> 23:31:43,760 lecture interesting we are going to stop 31580 23:31:43,759 --> 23:31:48,799 and in the next one we're going to zoom 31581 23:31:45,600 --> 23:31:51,832 in on the syntax here we are using to 31582 23:31:48,800 --> 23:31:54,080 derive from other classes we just did 31583 23:31:51,831 --> 23:31:55,679 the public inheritance here but in the 31584 23:31:54,080 --> 23:31:59,040 next lecture we're going to see that it 31585 23:31:55,679 --> 23:32:01,119 is possible to actually do protected and 31586 23:31:59,039 --> 23:32:03,759 private inheritance and we're going to 31587 23:32:01,119 --> 23:32:06,000 see what this means in the next lecture 31588 23:32:03,759 --> 23:32:07,759 so go ahead and finish up here and meet 31589 23:32:06,000 --> 23:32:11,279 me there in this lecture we're going to 31590 23:32:07,759 --> 23:32:13,511 learn about base class access specifiers 31591 23:32:11,279 --> 23:32:15,919 and in case you didn't know we have been 31592 23:32:13,512 --> 23:32:18,720 using base class access specifiers in 31593 23:32:15,919 --> 23:32:21,191 this chapter all along and to really 31594 23:32:18,720 --> 23:32:23,680 understand this let's look at our player 31595 23:32:21,191 --> 23:32:27,119 class which is inheriting from person 31596 23:32:23,679 --> 23:32:29,759 and we are doing public inheritance the 31597 23:32:27,119 --> 23:32:32,551 public keyword here is a base class 31598 23:32:29,759 --> 23:32:33,759 access specifier and it is going to 31599 23:32:33,759 --> 23:32:40,551 how accessible our base class members 31600 23:32:37,512 --> 23:32:43,119 from a derived class and we have three 31601 23:32:40,551 --> 23:32:45,831 options okay so if we do public 31602 23:32:43,119 --> 23:32:47,679 inheritance like we see here in our 31603 23:32:47,679 --> 23:32:52,000 anything that is public in the base 31604 23:32:49,831 --> 23:32:54,799 class is also going to be public you see 31605 23:32:52,000 --> 23:32:55,759 m1 here is also public in the derived 31606 23:32:55,759 --> 23:33:00,479 anything that is protected is going to 31607 23:32:57,679 --> 23:33:02,959 stay protected and anything that is 31608 23:33:00,479 --> 23:33:06,639 private in the base class is also going 31609 23:33:02,960 --> 23:33:08,800 to stay private in the derived class 31610 23:33:06,639 --> 23:33:09,831 try and take a moment to look at this 31611 23:33:09,831 --> 23:33:16,319 the member access specifiers we have in 31612 23:33:13,039 --> 23:33:17,511 the base class are going to stay intact 31613 23:33:17,512 --> 23:33:22,720 public inheritance again anything that 31614 23:33:20,399 --> 23:33:25,360 is public in base is going to be public 31615 23:33:22,720 --> 23:33:27,831 and derived anything that is protected 31616 23:33:25,360 --> 23:33:30,960 in base is going to be protected and 31617 23:33:27,831 --> 23:33:32,639 derived and anything that is private in 31618 23:33:32,639 --> 23:33:39,119 private in derived this is one kind of 31619 23:33:36,000 --> 23:33:42,720 inheritance we can do in c plus plus but 31620 23:33:39,119 --> 23:33:44,871 we can also do protected inheritance and 31621 23:33:42,720 --> 23:33:47,360 the syntax to do that you just use the 31622 23:33:44,872 --> 23:33:49,760 protected keyword here you say class 31623 23:33:47,360 --> 23:33:52,872 player column column and specify that 31624 23:33:49,759 --> 23:33:55,360 you want to do protected inheritance and 31625 23:33:52,872 --> 23:33:58,320 then you specify the base class you want 31626 23:33:55,360 --> 23:34:00,232 to inherit from the moment you do this 31627 23:33:58,320 --> 23:34:02,640 anything that is public in the base 31628 23:34:00,232 --> 23:34:04,160 class is going to be protected in the 31629 23:34:04,160 --> 23:34:08,960 anything that is protected is going to 31630 23:34:06,000 --> 23:34:11,512 stay protected anything that is private 31631 23:34:11,512 --> 23:34:17,360 and in one way you can see that we can 31632 23:34:13,759 --> 23:34:18,959 use these base class access specifiers 31633 23:34:18,960 --> 23:34:24,800 the access specifier in derived class 31634 23:34:21,831 --> 23:34:26,871 but we can't make the access specifiers 31635 23:34:26,872 --> 23:34:31,600 more relaxed we can only make things 31636 23:34:28,960 --> 23:34:33,832 more constrained or more restricted for 31637 23:34:31,600 --> 23:34:36,720 example the m1 member variable was 31638 23:34:33,831 --> 23:34:40,159 public but now we have made it become 31639 23:34:36,720 --> 23:34:42,872 protected by using the protected base 31640 23:34:40,160 --> 23:34:45,360 access specifier here okay so now you 31641 23:34:42,872 --> 23:34:48,232 know that we can do public and protected 31642 23:34:45,360 --> 23:34:51,279 inheritance but it is also possible to 31643 23:34:48,232 --> 23:34:54,232 do private inheritance and you do that 31644 23:34:51,279 --> 23:34:56,639 by using a private base class access 31645 23:34:54,232 --> 23:34:58,400 specifier the moment you do this 31646 23:34:56,639 --> 23:35:00,551 anything that is public in the base 31647 23:34:58,399 --> 23:35:03,360 class is going to become private in the 31648 23:35:00,551 --> 23:35:05,119 derived class anything that is protected 31649 23:35:03,360 --> 23:35:07,831 in the base class is going to become 31650 23:35:05,119 --> 23:35:09,360 private in the derived class and 31651 23:35:07,831 --> 23:35:12,159 anything that is private is going to 31652 23:35:09,360 --> 23:35:13,680 stay private in the derived class and 31653 23:35:12,160 --> 23:35:16,720 again you see that this is going to 31654 23:35:13,679 --> 23:35:18,959 constrain thanks things that were public 31655 23:35:16,720 --> 23:35:21,360 are now private in derived and we can't 31656 23:35:18,960 --> 23:35:23,832 really do anything with them from any 31657 23:35:21,360 --> 23:35:26,399 other class that might derive from our 31658 23:35:23,831 --> 23:35:29,191 player class so this is the setup we 31659 23:35:26,399 --> 23:35:31,511 have okay to sum this up through the 31660 23:35:29,191 --> 23:35:35,599 base class access specifier we can 31661 23:35:31,512 --> 23:35:38,479 control how relaxed or constrained 31662 23:35:35,600 --> 23:35:40,320 is the access to base class members from 31663 23:35:38,479 --> 23:35:42,319 the derived class and this is really 31664 23:35:40,320 --> 23:35:44,800 important to understand if you want to 31665 23:35:42,320 --> 23:35:47,280 say as relaxed as they are in the base 31666 23:35:44,800 --> 23:35:49,192 class you can use public inheritance if 31667 23:35:47,279 --> 23:35:50,399 you want to constrain a little bit you 31668 23:35:50,399 --> 23:35:54,479 protected inheritance and things are 31669 23:35:52,320 --> 23:35:57,680 going to be inherited as protected 31670 23:35:54,479 --> 23:35:59,919 except for the private data in the base 31671 23:35:57,679 --> 23:36:02,079 class if you want things to be super 31672 23:35:59,919 --> 23:36:04,160 constrained you can use the private 31673 23:36:02,080 --> 23:36:06,160 inheritance that we just learned about 31674 23:36:04,160 --> 23:36:09,440 and everything in your derived class is 31675 23:36:06,160 --> 23:36:11,512 going to be private and you can use this 31676 23:36:09,440 --> 23:36:14,080 if it makes sense for the application 31677 23:36:11,512 --> 23:36:16,160 you are designing and you can use these 31678 23:36:14,080 --> 23:36:18,551 kinds of features if they make sense for 31679 23:36:16,160 --> 23:36:20,960 whatever application you are designing 31680 23:36:18,551 --> 23:36:23,119 with c plus plus one thing you should 31681 23:36:20,960 --> 23:36:26,320 know though is that regardless of the 31682 23:36:23,119 --> 23:36:29,191 base class access specifier we use 31683 23:36:26,320 --> 23:36:32,160 private members of the base class can 31684 23:36:29,191 --> 23:36:34,399 never be accessible from a derived class 31685 23:36:32,160 --> 23:36:37,120 so anything that is private is going to 31686 23:36:34,399 --> 23:36:39,360 stay private and you have no way to 31687 23:36:37,119 --> 23:36:40,959 relax things a little bit that's not 31688 23:36:39,360 --> 23:36:43,360 going to work you should really know 31689 23:36:40,960 --> 23:36:46,080 this now that you know these kinds of 31690 23:36:43,360 --> 23:36:47,680 inheritances we can do in ziplus plus 31691 23:36:46,080 --> 23:36:49,759 we're going to head over to the next 31692 23:36:47,679 --> 23:36:51,759 lecture and play with them a little more 31693 23:36:49,759 --> 23:36:54,159 in this lecture we're going to play with 31694 23:36:51,759 --> 23:36:56,159 the base class access specifiers we 31695 23:36:54,160 --> 23:36:58,160 learned about in the last lecture in 31696 23:36:56,160 --> 23:37:01,512 other words we're going to play with 31697 23:36:58,160 --> 23:37:03,760 public protected and private inheritance 31698 23:37:01,512 --> 23:37:05,680 in visual studio code so let's review a 31699 23:37:03,759 --> 23:37:07,679 little bit what we talked about in the 31700 23:37:05,679 --> 23:37:10,079 last lecture we say that we can have 31701 23:37:07,679 --> 23:37:12,959 public inheritance and the syntax to do 31702 23:37:10,080 --> 23:37:15,119 that is what you see here and at the 31703 23:37:12,960 --> 23:37:17,040 moment you do this what was public in 31704 23:37:15,119 --> 23:37:19,279 the basic class is going to be public in 31705 23:37:17,039 --> 23:37:21,599 the derived class what was protected is 31706 23:37:19,279 --> 23:37:24,319 going to stay protected what was private 31707 23:37:21,600 --> 23:37:26,639 is going to become private in the 31708 23:37:24,320 --> 23:37:28,480 derived class in other words public 31709 23:37:26,639 --> 23:37:31,360 inheritance is really not going to 31710 23:37:28,479 --> 23:37:33,119 change the accessibility of the member 31711 23:37:31,360 --> 23:37:35,831 variables we have in the base class 31712 23:37:33,119 --> 23:37:38,551 we're going to inherit the data as it 31713 23:37:35,831 --> 23:37:40,551 was with the same access level we had in 31714 23:37:38,551 --> 23:37:43,360 the base class this is a defining 31715 23:37:40,551 --> 23:37:45,679 feature of public inheritance in c plus 31716 23:37:43,360 --> 23:37:49,119 plus we can also do protected 31717 23:37:45,679 --> 23:37:52,639 inheritance and what this is going to do 31718 23:37:49,119 --> 23:37:55,919 is to make protected the highest access 31719 23:37:52,639 --> 23:37:57,919 level we can have in the derived class 31720 23:37:55,919 --> 23:37:59,919 so anything that was public in the base 31721 23:37:57,919 --> 23:38:01,679 class is going to become protected 31722 23:37:59,919 --> 23:38:03,919 anything that was protected is going to 31723 23:38:01,679 --> 23:38:05,919 stay protected because that's the 31724 23:38:05,919 --> 23:38:10,551 and anything that was private in the 31725 23:38:07,759 --> 23:38:13,279 base class is going to be private in the 31726 23:38:10,551 --> 23:38:15,279 derived class we say private in the 31727 23:38:13,279 --> 23:38:17,831 sense that it is going to be private to 31728 23:38:15,279 --> 23:38:21,600 the base class so you can't really use 31729 23:38:17,831 --> 23:38:22,799 m3 from the player class in this case if 31730 23:38:21,600 --> 23:38:25,680 you're trying to do that you're going to 31731 23:38:22,800 --> 23:38:28,551 get a compiler error okay we can also do 31732 23:38:25,679 --> 23:38:31,359 private inheritance and the syntax to do 31733 23:38:28,551 --> 23:38:33,440 that is what you see here and now 31734 23:38:31,360 --> 23:38:34,720 private is going to be the highest level 31735 23:38:34,720 --> 23:38:39,600 in the player class in terms of the 31736 23:38:37,119 --> 23:38:41,599 members that we get from the base class 31737 23:38:39,600 --> 23:38:43,600 so anything that was public is going to 31738 23:38:41,600 --> 23:38:45,760 become private anything that was 31739 23:38:43,600 --> 23:38:48,000 protected is going to become private 31740 23:38:45,759 --> 23:38:49,919 anything that was private to the base 31741 23:38:48,000 --> 23:38:52,639 class is going to stay private to the 31742 23:38:49,919 --> 23:38:53,759 base class this is what we mean here but 31743 23:38:53,759 --> 23:39:01,679 m1 and m2 just became public to the 31744 23:38:58,551 --> 23:39:04,800 player class if anybody else tries to 31745 23:39:01,679 --> 23:39:07,119 derive or inherit from a player these 31746 23:39:04,800 --> 23:39:10,400 things are not going to be accessible 31747 23:39:07,119 --> 23:39:13,039 and this is in some way the player class 31748 23:39:10,399 --> 23:39:15,679 being selfish it is saying i am going to 31749 23:39:13,039 --> 23:39:18,479 get everything i can from my parent 31750 23:39:15,679 --> 23:39:21,279 class or my base class but i am going to 31751 23:39:18,479 --> 23:39:23,360 make it all mine nobody else can take 31752 23:39:21,279 --> 23:39:24,399 this and this is the kind of setup we 31753 23:39:24,399 --> 23:39:28,959 with private inheritance okay so now 31754 23:39:27,039 --> 23:39:30,959 that we have this in mind we're going to 31755 23:39:28,960 --> 23:39:32,800 play with us in visual studio code we're 31756 23:39:30,960 --> 23:39:34,232 going to set up a bunch of classes we're 31757 23:39:32,800 --> 23:39:36,551 going to set up a personal class we're 31758 23:39:34,232 --> 23:39:38,639 going to be inheriting from we are going 31759 23:39:36,551 --> 23:39:41,360 to set up a nurse class which is going 31760 23:39:38,639 --> 23:39:43,440 to do protected inheritance and we're 31761 23:39:41,360 --> 23:39:45,919 going to set up an engineer class which 31762 23:39:43,440 --> 23:39:47,760 is going to do private inheritance and 31763 23:39:45,919 --> 23:39:50,319 we're going to really play with this and 31764 23:39:47,759 --> 23:39:52,720 see how this plays out in real c plus 31765 23:39:50,320 --> 23:39:54,960 plus code so let's do this here we are 31766 23:39:52,720 --> 23:39:58,960 in our working folder the current 31767 23:39:54,960 --> 23:40:00,720 project is base access specifier a demo 31768 23:39:58,960 --> 23:40:02,320 we're going to do a demo on this but 31769 23:40:00,720 --> 23:40:05,759 before we do that we're going to grab 31770 23:40:02,320 --> 23:40:07,680 our template files and put them in place 31771 23:40:05,759 --> 23:40:09,759 and we are going to open this little guy 31772 23:40:07,679 --> 23:40:11,919 in visual studio code by dragging and 31773 23:40:09,759 --> 23:40:13,679 dropping here this is going to give us a 31774 23:40:11,919 --> 23:40:16,080 good starting point we're going to have 31775 23:40:13,679 --> 23:40:18,000 our main cpp file we're going to clean 31776 23:40:16,080 --> 23:40:19,600 it up and we're going to set up our base 31777 23:40:18,000 --> 23:40:22,479 class which is going to be the person 31778 23:40:19,600 --> 23:40:24,720 class so let's put this in from scratch 31779 23:40:22,479 --> 23:40:26,872 we're going to add our files we're going 31780 23:40:24,720 --> 23:40:28,639 to say person page and we're going to 31781 23:40:28,639 --> 23:40:33,600 we are going to hop over in our header 31782 23:40:30,872 --> 23:40:35,919 file and put in our code nothing special 31783 23:40:33,600 --> 23:40:37,832 so far we have an include gourd we have 31784 23:40:35,919 --> 23:40:40,160 our person class it has a bunch of 31785 23:40:37,831 --> 23:40:42,959 member variables we have the full name 31786 23:40:40,160 --> 23:40:45,680 which is public we have the age which is 31787 23:40:42,960 --> 23:40:49,040 protected and we have the address which 31788 23:40:45,679 --> 23:40:51,279 is private to this class here this is 31789 23:40:49,039 --> 23:40:53,599 going to be our starting point 31790 23:40:51,279 --> 23:40:55,759 we also have a bunch of constructors we 31791 23:40:53,600 --> 23:40:57,920 have a default one we also have another 31792 23:40:55,759 --> 23:41:00,551 one which is going to take all the data 31793 23:40:57,919 --> 23:41:03,119 we can use the constructor person object 31794 23:41:00,551 --> 23:41:05,440 and here we are passing a cost string by 31795 23:41:03,119 --> 23:41:07,599 reference i think we can fix this and 31796 23:41:05,440 --> 23:41:10,232 pass a string view this is going to be 31797 23:41:07,600 --> 23:41:12,480 better let's say string view and we're 31798 23:41:10,232 --> 23:41:15,680 going to change the first parameter here 31799 23:41:12,479 --> 23:41:18,000 to be an std string view by value this 31800 23:41:15,679 --> 23:41:21,119 is going to be much better okay we have 31801 23:41:18,000 --> 23:41:24,080 our class we can hop over in the cpp 31802 23:41:21,119 --> 23:41:26,080 file and put in our implementation we 31803 23:41:24,080 --> 23:41:28,320 are going to change the constructor a 31804 23:41:26,080 --> 23:41:31,119 little bit and make sure we are taking 31805 23:41:28,320 --> 23:41:33,440 an std string view by value and if you 31806 23:41:31,119 --> 23:41:36,080 wanted to type all this code you can get 31807 23:41:33,440 --> 23:41:37,919 it from the resource section of this 31808 23:41:36,080 --> 23:41:40,000 lecture i am going to attach all the 31809 23:41:37,919 --> 23:41:42,232 source code and you can download and use 31810 23:41:40,000 --> 23:41:44,720 this however you want to really make 31811 23:41:42,232 --> 23:41:46,960 your learning experience as pleasant as 31812 23:41:44,720 --> 23:41:49,119 possible so we have our constructor here 31813 23:41:46,960 --> 23:41:50,960 it is going to forward the data into our 31814 23:41:49,119 --> 23:41:53,279 member variables the full name is going 31815 23:41:50,960 --> 23:41:54,800 to go into our full name member variable 31816 23:41:53,279 --> 23:41:56,399 we're going to grab the age and the 31817 23:41:54,800 --> 23:41:58,720 address and we're going to store all 31818 23:41:56,399 --> 23:42:00,399 this information we also have an output 31819 23:41:58,720 --> 23:42:02,872 stream operator which is going to be 31820 23:42:00,399 --> 23:42:04,871 printing all this information here it is 31821 23:42:02,872 --> 23:42:07,120 going to go through the getters because 31822 23:42:04,872 --> 23:42:10,000 we have a person object passed as a 31823 23:42:07,119 --> 23:42:12,399 parameter here and this is going to work 31824 23:42:10,000 --> 23:42:15,191 we can go in the main cpp file and 31825 23:42:12,399 --> 23:42:17,191 include our person class and create an 31826 23:42:15,191 --> 23:42:20,080 object of it we can do something like 31827 23:42:17,191 --> 23:42:22,959 person person one and we can pass the 31828 23:42:20,080 --> 23:42:25,759 data that our constructor expects we can 31829 23:42:22,960 --> 23:42:27,832 pass in the full name the age and the 31830 23:42:25,759 --> 23:42:30,799 address so let's do that we're going to 31831 23:42:27,831 --> 23:42:33,511 say the full name daniel gray why not 31832 23:42:30,800 --> 23:42:35,360 and the age is going to be 27 we can put 31833 23:42:33,512 --> 23:42:37,832 in whatever we want and we're going to 31834 23:42:35,360 --> 23:42:40,800 put in the address we're going to say 31835 23:42:37,831 --> 23:42:43,360 blue sky streak you can say two three 31836 23:42:40,800 --> 23:42:46,160 three for example and say the number of 31837 23:42:43,360 --> 23:42:48,960 the house to b56 why not this is a 31838 23:42:46,160 --> 23:42:52,320 possible address for a person so we can 31839 23:42:48,960 --> 23:42:54,552 say person one or we can use htdc out to 31840 23:42:52,320 --> 23:42:57,760 print this out because we have an output 31841 23:42:54,551 --> 23:42:59,360 stream operator for our person class we 31842 23:42:57,759 --> 23:43:02,159 can do this we're going to say person 31843 23:42:59,360 --> 23:43:04,000 one and we're going to put him out and 31844 23:43:02,160 --> 23:43:06,832 if we build and run this program we're 31845 23:43:04,000 --> 23:43:09,360 going to see that this is going to work 31846 23:43:06,831 --> 23:43:11,759 hopefully and we have a problem because 31847 23:43:09,360 --> 23:43:14,320 we don't have a semicolon at the end of 31848 23:43:11,759 --> 23:43:17,119 line 6 here let's fix that we're going 31849 23:43:14,320 --> 23:43:19,040 to run this task to build with gcc the 31850 23:43:17,119 --> 23:43:21,512 builder is going to be good we can bring 31851 23:43:21,512 --> 23:43:25,600 and we're going to use this to run our 31852 23:43:23,119 --> 23:43:27,831 program if we say rooster we're going to 31853 23:43:25,600 --> 23:43:30,480 see that we have the person printed down 31854 23:43:27,831 --> 23:43:35,119 the full name is daniel gray the age is 31855 23:43:30,479 --> 23:43:38,000 27 the address is blue sky street 233 31856 23:43:35,119 --> 23:43:40,399 and the number of the house is 56 so 31857 23:43:38,000 --> 23:43:42,720 this is a possible implementation for 31858 23:43:40,399 --> 23:43:44,871 the person class but we are really not 31859 23:43:42,720 --> 23:43:46,872 interested in setting up a prison class 31860 23:43:44,872 --> 23:43:49,680 and doing things with that we are 31861 23:43:46,872 --> 23:43:52,872 interested in inheriting from the person 31862 23:43:49,679 --> 23:43:55,759 class and learn about different kinds of 31863 23:43:52,872 --> 23:43:57,512 base class access specifiers we can do 31864 23:43:55,759 --> 23:44:00,479 so we're going to set up a player class 31865 23:43:57,512 --> 23:44:02,720 which is going to inherit publicly from 31866 23:44:00,479 --> 23:44:04,639 the person class let's do that we're 31867 23:44:02,720 --> 23:44:06,960 going to put in our player we're going 31868 23:44:04,639 --> 23:44:09,119 to put in the header file let's say 31869 23:44:06,960 --> 23:44:12,400 player not person and we're going to say 31870 23:44:09,119 --> 23:44:14,799 player.h we're going to put in a cpp 31871 23:44:12,399 --> 23:44:17,831 file we are going to hop over in the 31872 23:44:14,800 --> 23:44:20,400 header of player and put in our class 31873 23:44:17,831 --> 23:44:22,000 let's close the left sidebar here so 31874 23:44:20,399 --> 23:44:23,919 that you can really see everything we 31875 23:44:23,919 --> 23:44:28,800 we have the class player declared it is 31876 23:44:26,479 --> 23:44:29,759 going to do public inheritance from 31877 23:44:29,759 --> 23:44:34,639 and what this is going to do is that 31878 23:44:32,720 --> 23:44:37,512 anything that is public in person is 31879 23:44:34,639 --> 23:44:39,360 going to stay public in player anything 31880 23:44:37,512 --> 23:44:41,040 that is protected is going to stay 31881 23:44:43,512 --> 23:44:48,000 private is going to stay private so we 31882 23:44:46,000 --> 23:44:50,479 want to be able to access private data 31883 23:44:48,000 --> 23:44:52,080 from a person object for example we 31884 23:44:52,080 --> 23:44:57,191 the address member variable here because 31885 23:44:54,399 --> 23:44:58,871 it is private but we can use the full 31886 23:45:02,160 --> 23:45:07,600 let's make sure we have a cpp file and 31887 23:45:07,600 --> 23:45:12,800 so let's hop over to the cpp file and 31888 23:45:09,831 --> 23:45:15,191 put in a possible implementation we are 31889 23:45:12,800 --> 23:45:17,680 going to include the person and player 31890 23:45:15,191 --> 23:45:20,080 because we're going to be using these 31891 23:45:17,679 --> 23:45:22,719 and we are going to set up a constructor 31892 23:45:20,080 --> 23:45:25,600 which is not going to do anything for a 31893 23:45:22,720 --> 23:45:27,119 player we also have our output stream 31894 23:45:25,600 --> 23:45:29,920 operator which is going to print the 31895 23:45:27,119 --> 23:45:32,399 data it is going to be using the getters 31896 23:45:29,919 --> 23:45:33,831 we have in the base class and we have 31897 23:45:33,831 --> 23:45:38,479 because we are doing public inheritance 31898 23:45:36,479 --> 23:45:40,800 so they are going to be public and we 31899 23:45:38,479 --> 23:45:43,279 can call them from the outside like we 31900 23:45:40,800 --> 23:45:46,232 are doing here notice that this is an 31901 23:45:43,279 --> 23:45:48,551 outside player object and we are calling 31902 23:45:46,232 --> 23:45:50,400 our public members on it and this is 31903 23:45:48,551 --> 23:45:52,000 going to work we also have a destructor 31904 23:45:50,399 --> 23:45:54,551 and which is really not going to do 31905 23:45:52,000 --> 23:45:57,119 anything but we're going to keep it in 31906 23:45:54,551 --> 23:45:59,759 here because it's not going to hurt so 31907 23:45:59,759 --> 23:46:04,871 is to see if we can access thanks from 31908 23:46:02,479 --> 23:46:07,191 the base class the first thing we have 31909 23:46:07,191 --> 23:46:12,871 full name and i think we can actually 31910 23:46:09,679 --> 23:46:15,831 copy these blanks or we cannot just use 31911 23:46:12,872 --> 23:46:18,320 them m full name mh and m address we're 31912 23:46:15,831 --> 23:46:21,511 going to try and use them in the derived 31913 23:46:18,320 --> 23:46:24,000 class so let's try and say m full name 31914 23:46:21,512 --> 23:46:24,720 and change the full name for example to 31915 23:46:24,720 --> 23:46:28,720 daniel gray or jon snow and we're going 31916 23:46:30,639 --> 23:46:36,160 55 why not and we're going to try and 31917 23:46:33,360 --> 23:46:38,551 access the address let's see what we 31918 23:46:36,160 --> 23:46:40,960 called our address member variable it is 31919 23:46:38,551 --> 23:46:43,599 m address we can grab that and use that 31920 23:46:40,960 --> 23:46:45,760 in our class if we try to access that 31921 23:46:43,600 --> 23:46:47,440 we're going to get a compiler error 31922 23:46:47,440 --> 23:46:52,400 private to the base class you notice 31923 23:46:50,080 --> 23:46:54,400 that if we even try to put some junk 31924 23:46:52,399 --> 23:46:57,511 data we're going to have a squiggly line 31925 23:46:54,399 --> 23:46:59,360 because this is not accessible here and 31926 23:46:57,512 --> 23:47:01,760 visual studio code is going to say that 31927 23:47:01,759 --> 23:47:06,959 because it is private to the base class 31928 23:47:04,320 --> 23:47:10,160 so we can access m full name because it 31929 23:47:06,960 --> 23:47:13,680 was inherited as public so this is going 31930 23:47:10,160 --> 23:47:17,120 to be okay we can use age because it was 31931 23:47:13,679 --> 23:47:19,679 inherited as protected we can use it in 31932 23:47:17,119 --> 23:47:22,319 a derived class but we can't use the 31933 23:47:19,679 --> 23:47:24,871 address because it was inherited as 31934 23:47:22,320 --> 23:47:27,120 private and it is private to the base 31935 23:47:24,872 --> 23:47:28,800 class so this is not going to work even 31936 23:47:27,119 --> 23:47:30,639 if we try to call biola code we're going 31937 23:47:28,800 --> 23:47:32,479 to get a compiler error let's try to do 31938 23:47:30,639 --> 23:47:35,191 that so that you can really see for 31939 23:47:32,479 --> 23:47:39,191 yourself okay so we have a compiler 31940 23:47:35,191 --> 23:47:41,119 error and this was declared in the base 31941 23:47:39,191 --> 23:47:43,599 class as private this is what the 31942 23:47:41,119 --> 23:47:46,799 compiler error is saying so we shouldn't 31943 23:47:43,600 --> 23:47:48,480 really be doing this the derived class 31944 23:47:48,479 --> 23:47:53,831 modifying a private member of the base 31945 23:47:51,919 --> 23:47:55,440 class this is the message here so we're 31946 23:47:53,831 --> 23:47:56,959 going to comment this out and say that 31947 23:47:55,440 --> 23:47:59,760 this is going to give you a compiler 31948 23:47:56,960 --> 23:48:02,552 error but we are also going to create a 31949 23:47:59,759 --> 23:48:05,279 player object and try to access these 31950 23:48:02,551 --> 23:48:07,279 guys and see how accesses from the 31951 23:48:05,279 --> 23:48:09,440 outside so we're going to go in the main 31952 23:48:07,279 --> 23:48:12,080 cpp file and we're going to include 31953 23:48:09,440 --> 23:48:14,720 player dot h and we're going to create a 31954 23:48:12,080 --> 23:48:17,600 player object we can put in a separator 31955 23:48:14,720 --> 23:48:19,360 and say a bunch of dashes here this is 31956 23:48:17,600 --> 23:48:21,600 going to do i think this is going to 31957 23:48:19,360 --> 23:48:24,479 work and we're going to create a player 31958 23:48:21,600 --> 23:48:27,040 object we can say player and player and 31959 23:48:24,479 --> 23:48:29,039 not put any parenthesis here this is 31960 23:48:27,039 --> 23:48:33,039 going to call the default constructor 31961 23:48:29,039 --> 23:48:35,119 that we have in our player class and we 31962 23:48:33,039 --> 23:48:37,191 can try to use this but before we do 31963 23:48:35,119 --> 23:48:39,759 let's try and build and see that this is 31964 23:48:37,191 --> 23:48:42,319 going to build just fine we're going to 31965 23:48:39,759 --> 23:48:44,080 work with gcc let's do that we're going 31966 23:48:42,320 --> 23:48:47,192 to see that the world is good we can try 31967 23:48:44,080 --> 23:48:50,720 and access stuff from this player object 31968 23:48:47,191 --> 23:48:52,479 we can say player and say m full name 31969 23:48:50,720 --> 23:48:54,160 you see that this is accessible we can 31970 23:48:54,160 --> 23:49:01,192 to samwell jackson we can do that but if 31971 23:48:58,160 --> 23:49:03,279 we try and say player mh we're going to 31972 23:49:01,191 --> 23:49:06,479 see that this is not accessible here 31973 23:49:03,279 --> 23:49:07,919 because this was inherited as protected 31974 23:49:06,479 --> 23:49:11,279 if we go back to person we're going to 31975 23:49:07,919 --> 23:49:14,080 see that the age is protected so it will 31976 23:49:11,279 --> 23:49:18,080 be accessible in derived classes just 31977 23:49:14,080 --> 23:49:20,960 like we did in our play method of the 31978 23:49:18,080 --> 23:49:24,000 player class but we can't access the age 31979 23:49:20,960 --> 23:49:25,280 from the outside of a player object 31980 23:49:24,000 --> 23:49:27,279 because that's not going to be 31981 23:49:25,279 --> 23:49:30,872 accessible from the outside this is a 31982 23:49:27,279 --> 23:49:32,800 defining feature of the protected access 31983 23:49:30,872 --> 23:49:34,320 level so this is not going to work even 31984 23:49:32,800 --> 23:49:38,232 if we try to build we're going to get a 31985 23:49:34,320 --> 23:49:41,760 compiler error along the lines of this 31986 23:49:38,232 --> 23:49:43,600 is not accessible in this context and it 31987 23:49:41,759 --> 23:49:46,000 was declared in the base class so we 31988 23:49:43,600 --> 23:49:47,920 can't really access it from the outside 31989 23:49:46,000 --> 23:49:50,160 this is the compiler error here so we're 31990 23:49:47,919 --> 23:49:51,679 going to comment this out and say that 31991 23:49:50,160 --> 23:49:53,600 this is going to give you a compiler 31992 23:49:51,679 --> 23:49:55,759 error okay so i hope you know that you 31993 23:49:53,600 --> 23:49:59,040 can't even have a chance accessing a 31994 23:49:55,759 --> 23:50:02,080 private member of a base class from a 31995 23:49:59,039 --> 23:50:05,279 derived object in the main function like 31996 23:50:02,080 --> 23:50:08,080 we do here so if we try and do player 31997 23:50:05,279 --> 23:50:09,919 and say m address this is not going to 31998 23:50:08,080 --> 23:50:12,080 work okay so if we try to change this 31999 23:50:09,919 --> 23:50:13,360 for example to some junk data this is 32000 23:50:12,080 --> 23:50:16,872 not going to work we're going to get a 32001 23:50:13,360 --> 23:50:19,279 compiler error because this was declared 32002 23:50:16,872 --> 23:50:20,872 private in the base class and it is 32003 23:50:19,279 --> 23:50:23,191 private to the base class you can't 32004 23:50:20,872 --> 23:50:25,192 really access it from the outside you 32005 23:50:23,191 --> 23:50:28,000 can't even access it from a derived 32006 23:50:25,191 --> 23:50:29,831 class so let alone access this on the 32007 23:50:28,000 --> 23:50:31,279 outside in the main function so this is 32008 23:50:29,831 --> 23:50:33,360 not going to work it is going to give 32009 23:50:31,279 --> 23:50:35,191 you a compiler error but we're going to 32010 23:50:33,360 --> 23:50:37,919 weld and show you the compiler error 32011 23:50:35,191 --> 23:50:40,479 because we are learning here so we're 32012 23:50:37,919 --> 23:50:43,679 going to get a compiler error that says 32013 23:50:40,479 --> 23:50:45,759 m address is private within the context 32014 23:50:43,679 --> 23:50:47,831 where we are trying to use it so we 32015 23:50:45,759 --> 23:50:50,000 can't really use it from the outside 32016 23:50:47,831 --> 23:50:52,551 here again this is going to give us a 32017 23:50:50,000 --> 23:50:55,039 compiler error let's say that here and 32018 23:50:52,551 --> 23:50:57,599 this is really what i wanted you to 32019 23:50:55,039 --> 23:50:59,511 learn about public inheritance anything 32020 23:50:57,600 --> 23:51:02,320 that is public in the base class is 32021 23:50:59,512 --> 23:51:04,160 going to be public in the derived class 32022 23:51:02,320 --> 23:51:06,872 anything that is protected is going to 32023 23:51:04,160 --> 23:51:10,000 stay protected and what that means it 32024 23:51:06,872 --> 23:51:12,639 will be usable in the derived class but 32025 23:51:10,000 --> 23:51:14,232 you can't use that from the outside and 32026 23:51:12,639 --> 23:51:16,319 anything that is private in the base 32027 23:51:14,232 --> 23:51:18,639 class is going to stay private so you 32028 23:51:16,320 --> 23:51:20,872 can't use that from the derived class 32029 23:51:18,639 --> 23:51:22,639 and you can't even use that from the 32030 23:51:20,872 --> 23:51:25,279 outside like we are doing in the main 32031 23:51:22,639 --> 23:51:27,600 function here this is not going to work 32032 23:51:25,279 --> 23:51:29,279 okay so now that we have this in place 32033 23:51:27,600 --> 23:51:30,960 we're going to set up another class that 32034 23:51:30,960 --> 23:51:35,360 protected inheritance and that's going 32035 23:51:33,039 --> 23:51:38,079 to be our nurse class so we're going to 32036 23:51:35,360 --> 23:51:40,800 put this in let's put in a nurse header 32037 23:51:38,080 --> 23:51:44,080 file this is going to be nurse.h we're 32038 23:51:40,800 --> 23:51:45,919 also going to put in nurse.cpp okay so 32039 23:51:44,080 --> 23:51:48,800 we're going to head over in the header 32040 23:51:45,919 --> 23:51:51,039 file and put in the code for our nurse 32041 23:51:48,800 --> 23:51:53,279 class we're going to say cluster nurse 32042 23:51:51,039 --> 23:51:55,831 and say protected to mean that we want 32043 23:51:53,279 --> 23:51:57,440 to do protected inheritance and what 32044 23:51:55,831 --> 23:52:00,399 this is going to do anything that is 32045 23:51:57,440 --> 23:52:03,279 public in the base class is going to be 32046 23:52:00,399 --> 23:52:05,119 protected in the derived class which is 32047 23:52:03,279 --> 23:52:07,360 our nurse class anything that is 32048 23:52:05,119 --> 23:52:09,191 protected is going to stay protected 32049 23:52:07,360 --> 23:52:10,800 anything that is private is going to 32050 23:52:10,800 --> 23:52:15,279 to the base class this is what we mean 32051 23:52:12,960 --> 23:52:18,160 here okay now that we have this end we 32052 23:52:15,279 --> 23:52:20,639 can put in the implementation for our 32053 23:52:18,160 --> 23:52:22,800 nurse class and we're going to have a 32054 23:52:20,639 --> 23:52:24,800 constructor and we're going to have our 32055 23:52:22,800 --> 23:52:26,720 output stream operator and we're going 32056 23:52:24,800 --> 23:52:28,720 to have a destructor which is really not 32057 23:52:26,720 --> 23:52:31,919 going to do anything because we don't 32058 23:52:28,720 --> 23:52:33,191 have any dynamic memory allocation in 32059 23:52:33,191 --> 23:52:38,551 we are going to try and build this code 32060 23:52:35,759 --> 23:52:41,191 to see that it is going to work we have 32061 23:52:38,551 --> 23:52:44,159 a squiggly line in the main cpp file but 32062 23:52:41,191 --> 23:52:46,639 it was because of the line 15 here and 32063 23:52:44,160 --> 23:52:49,192 it has been commented down let's see if 32064 23:52:46,639 --> 23:52:51,119 we can build this program okay you see 32065 23:52:49,191 --> 23:52:52,720 that the world is going to be good and 32066 23:52:54,320 --> 23:52:59,760 thanks from our output stream operator 32067 23:52:56,960 --> 23:53:03,280 you see we have an earth operand we are 32068 23:52:59,759 --> 23:53:05,279 able to call this gateful name and get 32069 23:53:05,279 --> 23:53:09,919 because they are now protected in the 32070 23:53:07,679 --> 23:53:11,511 nurse class but we are able to call them 32071 23:53:11,512 --> 23:53:18,080 because this operator is a friend of our 32072 23:53:15,512 --> 23:53:20,160 nurse class here i want you to say this 32073 23:53:18,080 --> 23:53:23,191 some of you are going to ask 32074 23:53:20,160 --> 23:53:25,760 aren't we using an object and calling a 32075 23:53:23,191 --> 23:53:28,399 method using the dot operator this 32076 23:53:25,759 --> 23:53:31,599 qualifies as calling thanks from the 32077 23:53:28,399 --> 23:53:34,639 outside and yes that's true but this 32078 23:53:31,600 --> 23:53:38,000 method is really like a member function 32079 23:53:34,639 --> 23:53:40,639 in that it has access to private and 32080 23:53:38,000 --> 23:53:43,191 protected data from our nurse class and 32081 23:53:40,639 --> 23:53:45,360 the reason is it is a friend of the 32082 23:53:43,191 --> 23:53:46,479 nurse class here that's why we are able 32083 23:53:46,479 --> 23:53:51,119 something like this but what i really 32084 23:53:48,639 --> 23:53:54,319 want you to see now that that doubt is 32085 23:53:51,119 --> 23:53:56,720 cleared is the kind of access we have to 32086 23:53:54,320 --> 23:53:59,440 thanks from the base class we will still 32087 23:53:56,720 --> 23:54:02,479 be able to use m full name here because 32088 23:53:59,440 --> 23:54:05,680 it was inherited as protected so we can 32089 23:54:02,479 --> 23:54:08,639 use it from a derived class we can say m 32090 23:54:05,679 --> 23:54:10,319 full name jon snow this is going to work 32091 23:54:10,320 --> 23:54:15,360 inherited as protected now it is 32092 23:54:12,960 --> 23:54:17,512 protected in the eyes of the nurse class 32093 23:54:15,360 --> 23:54:19,680 this is what we mean here so this is 32094 23:54:17,512 --> 23:54:23,680 okay this is going to work we can get 32095 23:54:19,679 --> 23:54:25,831 the age we can use that and we can say 32096 23:54:25,831 --> 23:54:30,399 23 for example this is going to work 32097 23:54:27,919 --> 23:54:33,119 because this is protected but we can't 32098 23:54:30,399 --> 23:54:34,720 use the address if we do that we want to 32099 23:54:33,119 --> 23:54:37,360 be able to change it we will get a 32100 23:54:37,360 --> 23:54:41,191 this is private to the base class this 32101 23:54:39,440 --> 23:54:42,960 is the same thing we saw in the last 32102 23:54:41,191 --> 23:54:45,039 lecture this is going to give us a 32103 23:54:42,960 --> 23:54:48,232 compiler error if we try to build this 32104 23:54:45,039 --> 23:54:50,319 program let's try and build it to really 32105 23:54:48,232 --> 23:54:53,440 prove this the body is going to be good 32106 23:54:50,320 --> 23:54:55,512 without line 17 here but if we 32107 23:54:53,440 --> 23:54:58,000 uncommented and trying to build the 32108 23:54:55,512 --> 23:55:00,080 program let's do that we are going to 32109 23:54:58,000 --> 23:55:03,191 get a compiler error and the compiler 32110 23:55:00,080 --> 23:55:05,600 error is going to say let's go up and 32111 23:55:03,191 --> 23:55:06,799 see if we can get this message it's 32112 23:55:06,800 --> 23:55:12,400 m address is private within this context 32113 23:55:10,000 --> 23:55:15,119 where we are trying to use it and that's 32114 23:55:12,399 --> 23:55:16,959 line 17 of the nurse class if we go 32115 23:55:15,119 --> 23:55:18,799 there we're going to find the offending 32116 23:55:16,960 --> 23:55:20,552 line here so we're going to comment this 32117 23:55:18,800 --> 23:55:22,320 out and say that this is going to give 32118 23:55:20,551 --> 23:55:24,639 us a compiler error okay so you 32119 23:55:22,320 --> 23:55:26,960 shouldn't do this now that we know 32120 23:55:24,639 --> 23:55:28,232 things from the inside let's see how 32121 23:55:28,232 --> 23:55:34,400 on the outside we are going to hop over 32122 23:55:31,191 --> 23:55:36,959 to the main cpp file and include our 32123 23:55:34,399 --> 23:55:39,399 nurse class and we're going to create a 32124 23:55:36,960 --> 23:55:42,160 nurse object we can go down and say 32125 23:55:39,399 --> 23:55:44,399 stdcl and we're going to put in our 32126 23:55:42,160 --> 23:55:46,160 dashes and we're going to create a nurse 32127 23:55:44,399 --> 23:55:48,720 object i think we have a default 32128 23:55:46,160 --> 23:55:51,600 constructor for a nurse we're going to 32129 23:55:48,720 --> 23:55:53,919 say nurse 1 and we're going to basically 32130 23:55:51,600 --> 23:55:56,800 try and do the same things we did for 32131 23:55:53,919 --> 23:55:59,119 the player object from the outside if we 32132 23:55:56,800 --> 23:56:01,360 try to access the full name now it's not 32133 23:55:59,119 --> 23:56:02,639 going to be accessible because it was 32134 23:56:02,639 --> 23:56:08,720 with protected access level so if we do 32135 23:56:06,080 --> 23:56:11,119 nurse and say m full name you're going 32136 23:56:08,720 --> 23:56:13,191 to see that autocomplete is not here 32137 23:56:11,119 --> 23:56:16,479 this is a good sign that this is not 32138 23:56:13,191 --> 23:56:18,639 accessible from this place if we say 32139 23:56:16,479 --> 23:56:20,479 davy jones we're going to get a compiler 32140 23:56:18,639 --> 23:56:23,440 error and if we try to build we're going 32141 23:56:20,479 --> 23:56:25,440 to get a descriptive compiler error at 32142 23:56:23,440 --> 23:56:28,800 least i hope so let's build we're going 32143 23:56:25,440 --> 23:56:30,479 to see that with world with errors and 32144 23:56:28,800 --> 23:56:33,192 if we go up we're going to have our 32145 23:56:30,479 --> 23:56:36,319 error nurse was not declared in this 32146 23:56:33,191 --> 23:56:38,639 scope did you mean nurse one yes we met 32147 23:56:36,320 --> 23:56:41,680 nurse one sorry for that we're going to 32148 23:56:38,639 --> 23:56:43,512 build again thank you gcc for pointing 32149 23:56:41,679 --> 23:56:46,479 this out we are going to get another 32150 23:56:43,512 --> 23:56:48,160 kind of error and if we look at our 32151 23:56:46,479 --> 23:56:50,639 error here we're going to see that m 32152 23:56:48,160 --> 23:56:54,000 full name is inaccessible within this 32153 23:56:50,639 --> 23:56:57,512 context because it is a protected member 32154 23:56:54,000 --> 23:57:00,399 of the nurse class now it was inherited 32155 23:56:57,512 --> 23:57:02,872 as protected and we can't access it from 32156 23:57:00,399 --> 23:57:05,439 the outside we can access that from the 32157 23:57:02,872 --> 23:57:07,360 inside because it is protected but if we 32158 23:57:05,440 --> 23:57:09,360 try to access it from the outside we're 32159 23:57:07,360 --> 23:57:11,279 going to get a compiler error and this 32160 23:57:11,279 --> 23:57:16,872 of protected inheritance even things 32161 23:57:14,551 --> 23:57:19,440 that were public in the base class are 32162 23:57:16,872 --> 23:57:21,600 going to become protected in the derived 32163 23:57:19,440 --> 23:57:23,040 class so this is the message here and 32164 23:57:21,600 --> 23:57:25,680 this is going to give us a compiler 32165 23:57:23,039 --> 23:57:28,319 error okay so if we also try to access 32166 23:57:28,320 --> 23:57:32,552 h this is also going to give us a 32167 23:57:30,479 --> 23:57:35,119 compiler arrow let's try to put in an 32168 23:57:32,551 --> 23:57:36,639 edge here let's put in 51. you're going 32169 23:57:35,119 --> 23:57:38,080 to see that we're going to get the same 32170 23:57:38,080 --> 23:57:43,360 let's really try these things out and 32171 23:57:40,320 --> 23:57:45,040 learn about every possible way we can 32172 23:57:45,039 --> 23:57:49,599 if we look at the error we're going to 32173 23:57:47,119 --> 23:57:52,639 see that image is protected within this 32174 23:57:49,600 --> 23:57:54,960 context so we can't really access it so 32175 23:57:52,639 --> 23:57:57,360 this is the compiler error we get and i 32176 23:57:54,960 --> 23:57:59,040 hope you know that if you try and access 32177 23:57:57,360 --> 23:58:01,440 the address you're also going to get a 32178 23:57:59,039 --> 23:58:04,231 compiler error because that's private to 32179 23:58:01,440 --> 23:58:07,119 the base class so we can't even use that 32180 23:58:04,232 --> 23:58:08,872 from a derived class like nurse here 32181 23:58:07,119 --> 23:58:10,871 that's not going to work let's say that 32182 23:58:08,872 --> 23:58:13,040 here we have a compiler error and we're 32183 23:58:10,872 --> 23:58:16,000 going to set up another class which is 32184 23:58:13,039 --> 23:58:18,079 going to inherit privately and see how 32185 23:58:16,000 --> 23:58:19,919 that works out the class is going to be 32186 23:58:18,080 --> 23:58:22,720 engineer we're going to see that the 32187 23:58:19,919 --> 23:58:24,800 error here goes away so let's create our 32188 23:58:22,720 --> 23:58:27,639 engineer class we're going to put in a 32189 23:58:24,800 --> 23:58:31,760 header file that's going to be our 32190 23:58:27,639 --> 23:58:34,479 engineer.h we are also going to put in 32191 23:58:31,759 --> 23:58:37,119 cpp okay so we're going to hop over to 32192 23:58:34,479 --> 23:58:39,512 our header file and put in our engineer 32193 23:58:37,119 --> 23:58:42,231 class we're going to say engineer and 32194 23:58:39,512 --> 23:58:44,400 say that we want to inherit privately 32195 23:58:42,232 --> 23:58:46,480 from the person class this is the syntax 32196 23:58:44,399 --> 23:58:49,511 here and this is going to do private 32197 23:58:46,479 --> 23:58:51,119 inheritance and what this means is that 32198 23:58:49,512 --> 23:58:54,080 anything that is public is going to 32199 23:58:51,119 --> 23:58:56,159 become private to this class anything 32200 23:58:54,080 --> 23:58:58,400 that is protected in the base class is 32201 23:58:56,160 --> 23:59:00,480 going to become private to this class 32202 23:58:58,399 --> 23:59:03,279 and anything that was private in the 32203 23:59:00,479 --> 23:59:06,551 base class is going to stay private we 32204 23:59:03,279 --> 23:59:09,512 won't have access to that whatsoever in 32205 23:59:06,551 --> 23:59:12,080 the derived class directly we are going 32206 23:59:09,512 --> 23:59:15,119 to hop over to the cpp file and put in 32207 23:59:12,080 --> 23:59:17,360 our implementations we're going to have 32208 23:59:15,119 --> 23:59:19,512 a default constructor for engineer which 32209 23:59:17,360 --> 23:59:21,512 is not going to be taking any parameter 32210 23:59:19,512 --> 23:59:24,000 we're going to have our output stream 32211 23:59:21,512 --> 23:59:26,320 operator which is going to still work 32212 23:59:24,000 --> 23:59:29,360 because it is a friend of the engineer 32213 23:59:26,320 --> 23:59:32,480 class so all these things that were 32214 23:59:29,360 --> 23:59:35,600 inherited to be private are going to be 32215 23:59:32,479 --> 23:59:37,831 private to the engineer class and any 32216 23:59:35,600 --> 23:59:40,160 friend of the engineer class will have 32217 23:59:37,831 --> 23:59:42,871 access to that that's why we are able to 32218 23:59:40,160 --> 23:59:45,440 call get full name get age and get 32219 23:59:42,872 --> 23:59:47,832 address here and get these things to 32220 23:59:45,440 --> 23:59:50,872 work okay now that this is out of the 32221 23:59:47,831 --> 23:59:52,231 way we can try and access our thanks 32222 23:59:50,872 --> 23:59:55,279 we're going to try and do the same 32223 23:59:52,232 --> 23:59:57,279 things we did in our nurse header we're 32224 23:59:55,279 --> 23:59:58,800 going to copy the code because we are 32225 23:59:57,279 --> 24:00:00,872 trying to access the same member 32226 23:59:58,800 --> 24:00:03,832 variables and we're going to see how 32227 24:00:00,872 --> 24:00:06,479 this fairs in our engineer class this is 32228 24:00:03,831 --> 24:00:08,551 going to save us a few seconds 32229 24:00:06,479 --> 24:00:10,720 if we try to access the full name this 32230 24:00:08,551 --> 24:00:13,279 is going to work because it is private 32231 24:00:10,720 --> 24:00:15,040 to this class if we try to access the 32232 24:00:13,279 --> 24:00:17,119 edge this is going to work because all 32233 24:00:17,119 --> 24:00:22,720 to this class here but if we try to 32234 24:00:20,160 --> 24:00:25,192 access the address this is going to give 32235 24:00:22,720 --> 24:00:27,680 us a compiler error because address is 32236 24:00:25,191 --> 24:00:29,279 private to the base class if we go back 32237 24:00:27,679 --> 24:00:30,639 to person we're going to see that this 32238 24:00:30,639 --> 24:00:35,600 and if you try to inherit from this 32239 24:00:33,119 --> 24:00:38,479 class in any way you're not going to be 32240 24:00:35,600 --> 24:00:40,480 able to change this access level this 32241 24:00:38,479 --> 24:00:43,440 member variable is always going to stay 32242 24:00:40,479 --> 24:00:46,159 private to the base class and no derived 32243 24:00:43,440 --> 24:00:48,479 class will be able to modify or read 32244 24:00:46,160 --> 24:00:50,720 from this in any way so this is the 32245 24:00:48,479 --> 24:00:52,639 setup we have here if we try to build 32246 24:00:50,720 --> 24:00:54,800 we're going to get a compiler error 32247 24:00:52,639 --> 24:00:56,720 let's first take this out and show you 32248 24:00:56,720 --> 24:01:02,000 just fine without this line here 32249 24:00:59,831 --> 24:01:04,639 and i am going to build again and show 32250 24:01:02,000 --> 24:01:06,551 you that if we bring that in we're going 32251 24:01:04,639 --> 24:01:08,960 to get a compiler error so the build is 32252 24:01:06,551 --> 24:01:12,080 good we can go back in our engineer 32253 24:01:08,960 --> 24:01:14,639 class and bring in the offending line we 32254 24:01:12,080 --> 24:01:17,040 can uncomment this and if we want we're 32255 24:01:14,639 --> 24:01:19,759 going to get a compiler error let's make 32256 24:01:17,039 --> 24:01:22,079 sure you see the compiler error here so 32257 24:01:19,759 --> 24:01:23,919 the world was finished with errors and 32258 24:01:22,080 --> 24:01:26,160 if we go up we're going to see that we 32259 24:01:23,919 --> 24:01:28,959 have an error here and the error is 32260 24:01:26,160 --> 24:01:31,600 going to say m address is private within 32261 24:01:28,960 --> 24:01:34,639 this context and the context is what we 32262 24:01:34,639 --> 24:01:38,960 engineer class so we can go there and 32263 24:01:36,800 --> 24:01:40,639 find the offending line and we're going 32264 24:01:38,960 --> 24:01:43,120 to comment this out and this is going to 32265 24:01:40,639 --> 24:01:45,759 give us a compiler error now you know 32266 24:01:43,119 --> 24:01:48,479 this but this is how things are going to 32267 24:01:45,759 --> 24:01:51,511 work from the inside but we also need to 32268 24:01:48,479 --> 24:01:54,479 look at how things fare from the outside 32269 24:01:51,512 --> 24:01:57,279 let's go back in our main cpp file we 32270 24:01:54,479 --> 24:01:59,360 are going to include our engineer class 32271 24:01:57,279 --> 24:02:02,319 and we are going to create an engineer 32272 24:01:59,360 --> 24:02:05,039 object we can go down and say sddc out 32273 24:02:02,320 --> 24:02:08,160 and put in a bunch of dashes to separate 32274 24:02:05,039 --> 24:02:10,871 on thanks and we can create a 32275 24:02:08,160 --> 24:02:14,160 engineer object and that's going to be 32276 24:02:10,872 --> 24:02:15,832 engineer one and if we try to access the 32277 24:02:14,160 --> 24:02:18,160 full name that's not going to work 32278 24:02:20,720 --> 24:02:26,479 if we say engineer one and try to say m 32279 24:02:23,759 --> 24:02:28,871 full name and try to change the name in 32280 24:02:28,872 --> 24:02:34,479 olivier godson i am making this up i 32281 24:02:31,831 --> 24:02:36,479 don't know if this name exists but the 32282 24:02:34,479 --> 24:02:38,551 point is that this is not going to work 32283 24:02:36,479 --> 24:02:41,440 it is going to give you a compiler error 32284 24:02:38,551 --> 24:02:44,720 because m full name is now private to 32285 24:02:41,440 --> 24:02:47,119 the engineer class it was inherited to 32286 24:02:44,720 --> 24:02:48,479 be private to the engineer class and if 32287 24:02:47,119 --> 24:02:51,360 you try to do this you're going to get a 32288 24:02:48,479 --> 24:02:53,759 compiler error let's try and work with 32289 24:02:51,360 --> 24:02:55,759 gcc to really show you this and the 32290 24:02:53,759 --> 24:02:57,439 world is going to be finished with 32291 24:02:55,759 --> 24:02:58,959 errors and if we go up we're going to 32292 24:02:58,960 --> 24:03:04,800 full name is inaccessible from within 32293 24:03:01,440 --> 24:03:09,279 this context and the offending line is 32294 24:03:04,800 --> 24:03:11,440 at line 27 of the main cpp file if we go 32295 24:03:09,279 --> 24:03:12,872 there we're going to find our line and 32296 24:03:11,440 --> 24:03:15,360 we're going to say that this is going to 32297 24:03:12,872 --> 24:03:19,279 give us a compiler error so let's say 32298 24:03:15,360 --> 24:03:21,759 that and we can't even access the age 32299 24:03:19,279 --> 24:03:24,232 and the address member variables the age 32300 24:03:21,759 --> 24:03:27,679 is also going to be private to engineer 32301 24:03:24,232 --> 24:03:28,720 but the address is private to the base 32302 24:03:28,720 --> 24:03:32,639 so that's not going to work either so we 32303 24:03:30,960 --> 24:03:35,760 can't really access that from the 32304 24:03:32,639 --> 24:03:38,080 outside because it is now private to the 32305 24:03:35,759 --> 24:03:40,080 engineer class so let's go down and 32306 24:03:38,080 --> 24:03:43,919 really show you that we're going to go 32307 24:03:40,080 --> 24:03:45,680 down and say engineer one and say mh if 32308 24:03:43,919 --> 24:03:47,512 we try to do that that's not going to 32309 24:03:45,679 --> 24:03:49,279 work that's going to give us a compiler 32310 24:03:47,512 --> 24:03:51,919 error and if we try to build we're going 32311 24:03:49,279 --> 24:03:54,399 to get a compiler error along the same 32312 24:03:51,919 --> 24:03:56,319 lines as what we just got here 32313 24:03:54,399 --> 24:03:58,231 the book is going to finish with errors 32314 24:03:56,320 --> 24:04:00,800 and if we look at our error we're going 32315 24:03:58,232 --> 24:04:04,720 to see that mh is protected within this 32316 24:04:00,800 --> 24:04:07,360 context so we can't use it at line 28 of 32317 24:04:04,720 --> 24:04:09,512 the main cpp file this is going to give 32318 24:04:07,360 --> 24:04:12,160 us a compiler error let's do that we're 32319 24:04:09,512 --> 24:04:15,440 going to say that here and if we try and 32320 24:04:12,160 --> 24:04:17,600 access the m address member variable 32321 24:04:15,440 --> 24:04:21,040 that's not also going to work because 32322 24:04:17,600 --> 24:04:23,192 this is even more restricted for access 32323 24:04:21,039 --> 24:04:25,191 it is private to the base class so we 32324 24:04:23,191 --> 24:04:27,440 can't access it from a derived class 32325 24:04:25,191 --> 24:04:29,191 directly we can't even access that from 32326 24:04:27,440 --> 24:04:31,919 the outside so you should really know 32327 24:04:29,191 --> 24:04:33,919 this so if we put in some junk data here 32328 24:04:31,919 --> 24:04:36,872 and try to build this is going to give 32329 24:04:33,919 --> 24:04:40,319 us a compiler error that says that this 32330 24:04:36,872 --> 24:04:43,279 is not accessible from the main cpp file 32331 24:04:40,320 --> 24:04:45,360 like we are trying to do on line 29 here 32332 24:04:43,279 --> 24:04:47,279 so we're going to get an error and the 32333 24:04:45,360 --> 24:04:50,000 error is going to say m address is 32334 24:04:47,279 --> 24:04:53,679 private within this context and the 32335 24:04:50,000 --> 24:04:55,279 offending line is at 29 of the main cpp 32336 24:04:53,679 --> 24:04:56,719 file we're going to take this out and 32337 24:04:55,279 --> 24:04:58,639 we're going to say that this is going to 32338 24:04:56,720 --> 24:05:01,119 give us a compiler error this is really 32339 24:04:58,639 --> 24:05:03,360 all i wanted you to see in this lecture 32340 24:05:01,119 --> 24:05:05,831 and before we wrap this up let's try and 32341 24:05:03,360 --> 24:05:08,720 build again and make the error go away 32342 24:05:05,831 --> 24:05:11,119 because i don't like to leave you with 32343 24:05:08,720 --> 24:05:13,759 code that has compiler errors and the 32344 24:05:11,119 --> 24:05:16,231 book is good and this is really all i 32345 24:05:13,759 --> 24:05:19,039 wanted you to see in this lecture that 32346 24:05:16,232 --> 24:05:21,440 you can do all kinds of inheritances in 32347 24:05:19,039 --> 24:05:23,191 your c plus plus code you can do public 32348 24:05:21,440 --> 24:05:25,191 inheritance you can do protected 32349 24:05:23,191 --> 24:05:27,360 inheritance you can do private 32350 24:05:25,191 --> 24:05:29,831 inheritance and we had a chance to look 32351 24:05:27,360 --> 24:05:32,639 at all these scenarios in this lecture 32352 24:05:29,831 --> 24:05:35,439 here the message is the same as we saw 32353 24:05:32,639 --> 24:05:37,831 in the last lecture if you do public 32354 24:05:35,440 --> 24:05:40,551 inheritance anything that is public in 32355 24:05:37,831 --> 24:05:42,551 the base class is going to stay public 32356 24:05:40,551 --> 24:05:44,720 in the derived class anything that is 32357 24:05:42,551 --> 24:05:46,319 protected is going to stay protected 32358 24:05:44,720 --> 24:05:49,040 anything that is private is going to 32359 24:05:46,320 --> 24:05:52,080 stay private and this is really 32360 24:05:49,039 --> 24:05:54,159 misleading in a way the private data 32361 24:05:52,080 --> 24:05:56,800 from the base class is not going to be 32362 24:05:54,160 --> 24:06:00,800 private to the derived class it's going 32363 24:05:56,800 --> 24:06:03,360 to stay private to the base class so if 32364 24:06:00,800 --> 24:06:04,639 you try to use this in any way from the 32365 24:06:03,360 --> 24:06:07,039 derived class you're going to get 32366 24:06:04,639 --> 24:06:10,160 compiler errors because you can't access 32367 24:06:07,039 --> 24:06:12,719 that even if it is part of you player 32368 24:06:10,160 --> 24:06:15,192 has a person part of it but it can't 32369 24:06:12,720 --> 24:06:17,279 access private members from the base 32370 24:06:15,191 --> 24:06:19,512 class you should really know this we 32371 24:06:17,279 --> 24:06:21,039 have seen that we can also do protected 32372 24:06:19,512 --> 24:06:23,279 inheritance and that's going to 32373 24:06:23,279 --> 24:06:28,232 the highest level of access to be 32374 24:06:25,679 --> 24:06:30,551 protected so anything that is public is 32375 24:06:28,232 --> 24:06:33,120 going to be stripped down to protected 32376 24:06:30,551 --> 24:06:35,440 access anything that is protected is 32377 24:06:33,119 --> 24:06:38,080 going to stay protected anything that is 32378 24:06:35,440 --> 24:06:40,639 private is going to be private to the 32379 24:06:38,080 --> 24:06:42,639 base class you should really make this 32380 24:06:40,639 --> 24:06:45,039 super clear we can also do private 32381 24:06:42,639 --> 24:06:47,919 inheritance as we saw and this is going 32382 24:06:45,039 --> 24:06:50,479 to strip down the highest level access 32383 24:06:47,919 --> 24:06:52,959 to be private so anything that is public 32384 24:06:50,479 --> 24:06:54,872 is going to be constrained to be private 32385 24:06:52,960 --> 24:06:57,832 anything that is protected is going to 32386 24:06:54,872 --> 24:07:00,400 be inherited as protected to the player 32387 24:06:57,831 --> 24:07:03,119 class but anything that was private in 32388 24:07:00,399 --> 24:07:05,679 the base class is going to stay private 32389 24:07:03,119 --> 24:07:07,919 to the base class even the player class 32390 24:07:05,679 --> 24:07:10,639 won't have access to this member 32391 24:07:07,919 --> 24:07:12,872 directly if we really need access to 32392 24:07:10,639 --> 24:07:15,039 private data from the base class we 32393 24:07:12,872 --> 24:07:17,512 might go through public getters or 32394 24:07:15,039 --> 24:07:20,319 anything we get from the base class but 32395 24:07:17,512 --> 24:07:22,400 we can't access m3 directly and use it 32396 24:07:20,320 --> 24:07:24,640 from the derived class that's going to 32397 24:07:22,399 --> 24:07:26,799 give us a compiler error this is really 32398 24:07:24,639 --> 24:07:29,191 all i wanted you to see in this lecture 32399 24:07:26,800 --> 24:07:31,279 and i hope you find this interesting we 32400 24:07:29,191 --> 24:07:32,871 are going to stop here in this lecture 32401 24:07:31,279 --> 24:07:36,160 in the next one we're going to try and 32402 24:07:32,872 --> 24:07:38,160 zoom in on private inheritance and show 32403 24:07:36,160 --> 24:07:39,440 you some things you really need to be 32404 24:07:39,440 --> 24:07:42,479 so go ahead and finish up here and meet 32405 24:07:42,479 --> 24:07:47,119 in this lecture we're going to zoom in 32406 24:07:44,399 --> 24:07:49,039 on private inheritance and we're going 32407 24:07:47,119 --> 24:07:52,399 to see that the class that does a 32408 24:07:49,039 --> 24:07:54,551 private inheritance is really selfish 32409 24:07:52,399 --> 24:07:56,551 here is a simple structure we will be 32410 24:07:54,551 --> 24:07:58,720 using in this lecture here we will set 32411 24:07:56,551 --> 24:08:01,512 up a person class which is going to act 32412 24:07:58,720 --> 24:08:04,080 as our base class we are going to 32413 24:08:01,512 --> 24:08:06,400 inherit from this class privately and 32414 24:08:06,399 --> 24:08:12,000 and what this is going to do it's going 32415 24:08:08,399 --> 24:08:15,039 to take everything from the class person 32416 24:08:12,000 --> 24:08:17,919 and make it private to the engineer 32417 24:08:15,039 --> 24:08:20,231 class so the m1 member variable which 32418 24:08:17,919 --> 24:08:22,959 was public in the base class is going to 32419 24:08:20,232 --> 24:08:25,919 be private to the engineer class this is 32420 24:08:22,960 --> 24:08:28,080 what we have here the m2 member variable 32421 24:08:25,919 --> 24:08:31,191 which was protected is going to become a 32422 24:08:28,080 --> 24:08:32,080 private in engineer that's what we have 32423 24:08:32,080 --> 24:08:37,600 and the m3 member variable which was 32424 24:08:34,399 --> 24:08:40,871 private to person class is going to stay 32425 24:08:37,600 --> 24:08:43,120 private to person in the eyes of 32426 24:08:40,872 --> 24:08:45,279 engineer this is the situation we have 32427 24:08:43,119 --> 24:08:48,231 here so the problem is going to really 32428 24:08:45,279 --> 24:08:52,800 show up when somebody else tries to 32429 24:08:48,232 --> 24:08:55,832 inherit from engineer because m1 and m2 32430 24:08:52,800 --> 24:08:59,512 are now private to engineer they will be 32431 24:08:55,831 --> 24:09:03,119 inherited but still inaccessible to the 32432 24:08:59,512 --> 24:09:07,191 civil engineer class here m1 and m2 will 32433 24:09:03,119 --> 24:09:10,319 be private to the engineer class but m3 32434 24:09:07,191 --> 24:09:12,871 will be private to the base class which 32435 24:09:10,320 --> 24:09:15,512 is person so hopefully you can see the 32436 24:09:12,872 --> 24:09:16,551 message that if a member variable is 32437 24:09:16,551 --> 24:09:21,039 it can't be inherited in any way it 32438 24:09:19,119 --> 24:09:23,679 doesn't matter which kind of inheritance 32439 24:09:21,039 --> 24:09:28,399 you do so starting from engineer which 32440 24:09:23,679 --> 24:09:31,039 has made m1 and m2 private to itself 32441 24:09:28,399 --> 24:09:34,720 it's no longer possible to forward these 32442 24:09:31,039 --> 24:09:36,551 guys down in the inheritance hierarchy 32443 24:09:34,720 --> 24:09:38,399 another way to look at this is to see 32444 24:09:36,551 --> 24:09:41,440 that the engineer class is really 32445 24:09:38,399 --> 24:09:43,759 selfish it is saying i am going to take 32446 24:09:41,440 --> 24:09:46,232 everything i can get from my parent 32447 24:09:46,232 --> 24:09:51,120 inheriting privately but nobody else 32448 24:09:49,191 --> 24:09:52,959 will be able to take things from me 32449 24:09:51,119 --> 24:09:55,831 because i'm going to make them private 32450 24:09:52,960 --> 24:09:58,320 to myself so even if civil engineer is 32451 24:09:58,320 --> 24:10:03,600 that's not going to give him any access 32452 24:10:00,800 --> 24:10:06,160 to these member variables which are now 32453 24:10:03,600 --> 24:10:08,960 private to the engineer class hopefully 32454 24:10:06,160 --> 24:10:12,000 you can really see this and private 32455 24:10:08,960 --> 24:10:14,960 inheritance is going to really constrain 32456 24:10:12,000 --> 24:10:17,279 what you can forward down to other 32457 24:10:14,960 --> 24:10:19,832 classes that are going to be inheriting 32458 24:10:17,279 --> 24:10:22,720 from you this is the message i am trying 32459 24:10:19,831 --> 24:10:25,191 to convey here okay so here is the same 32460 24:10:22,720 --> 24:10:27,360 example in code we have our person class 32461 24:10:25,191 --> 24:10:29,440 it's going to have our member variables 32462 24:10:27,360 --> 24:10:31,600 we're going to have the full name the 32463 24:10:29,440 --> 24:10:35,040 age and the address they are going to 32464 24:10:31,600 --> 24:10:37,600 have public protected and private access 32465 24:10:35,039 --> 24:10:40,159 we are going to inherit from person and 32466 24:10:37,600 --> 24:10:42,800 create our engineer class we are going 32467 24:10:40,160 --> 24:10:45,279 to privately inherit from person and 32468 24:10:42,800 --> 24:10:48,960 this is going to take everything we get 32469 24:10:45,279 --> 24:10:51,512 from person and make it private to the 32470 24:10:48,960 --> 24:10:52,800 engineer class and one exception is 32471 24:10:52,800 --> 24:10:57,600 what was already private to the base 32472 24:10:55,440 --> 24:11:00,080 class which is our person class here 32473 24:10:57,600 --> 24:11:03,040 this is going to still stay private to 32474 24:11:00,080 --> 24:11:05,919 the person class regardless of what kind 32475 24:11:03,039 --> 24:11:08,000 of inheritance you do from person but 32476 24:11:05,919 --> 24:11:10,639 any other thing if we have any member 32477 24:11:08,000 --> 24:11:12,872 variable which are public or protected 32478 24:11:10,639 --> 24:11:15,831 or even if we have some member functions 32479 24:11:12,872 --> 24:11:18,479 which are public or protected everything 32480 24:11:15,831 --> 24:11:20,959 is going to be private to the engineer 32481 24:11:18,479 --> 24:11:24,399 class because we have done private 32482 24:11:20,960 --> 24:11:27,360 inheritance here so if we inherit from 32483 24:11:24,399 --> 24:11:30,479 engineer and do any kind of inheritance 32484 24:11:27,360 --> 24:11:32,479 we are going to do the most relaxed kind 32485 24:11:30,479 --> 24:11:35,119 of inheritance which is going to keep 32486 24:11:32,479 --> 24:11:37,599 the kind of access we had in the base 32487 24:11:35,119 --> 24:11:40,159 class but this is not going to improve 32488 24:11:37,600 --> 24:11:43,680 anything everything is going to stay 32489 24:11:40,160 --> 24:11:45,440 private to the engineer class and the 32490 24:11:43,679 --> 24:11:48,399 civil engineer is really not going to 32491 24:11:45,440 --> 24:11:50,720 have any access to any member variable 32492 24:11:48,399 --> 24:11:53,039 or member function that is inherited 32493 24:11:50,720 --> 24:11:54,720 from the engineer class and we are going 32494 24:11:53,039 --> 24:11:56,959 to see this in a minute when we hit 32495 24:11:56,960 --> 24:12:01,920 so again if a class is doing private 32496 24:11:59,679 --> 24:12:04,231 inheritance it's really another way to 32497 24:12:01,919 --> 24:12:07,039 say this class is selfish it's going to 32498 24:12:04,232 --> 24:12:09,279 take everything it can from the parent 32499 24:12:07,039 --> 24:12:12,319 class but it's not going to forward 32500 24:12:09,279 --> 24:12:14,720 nothing to downstream inheritance 32501 24:12:12,320 --> 24:12:17,760 classes and this is the setup i really 32502 24:12:14,720 --> 24:12:20,160 want you to understand here and only do 32503 24:12:17,759 --> 24:12:22,479 private inheritance when you want this 32504 24:12:20,160 --> 24:12:23,832 kind of behavior now that you know this 32505 24:12:22,479 --> 24:12:25,679 we're going to head over to visual 32506 24:12:23,831 --> 24:12:26,551 studio code and play with us a little 32507 24:12:26,551 --> 24:12:32,319 here we are in our working folder the 32508 24:12:29,279 --> 24:12:34,551 current project is closing in on private 32509 24:12:32,320 --> 24:12:37,680 inheritance we're going to zoom in on 32510 24:12:34,551 --> 24:12:40,080 this and really understand the effects 32511 24:12:37,679 --> 24:12:42,000 of doing private inheritance we're going 32512 24:12:40,080 --> 24:12:43,759 to grab the code from the previous 32513 24:12:42,000 --> 24:12:45,191 lecture and we're going to grab the 32514 24:12:46,399 --> 24:12:51,439 the person class these are going to be 32515 24:12:49,039 --> 24:12:54,231 our starting point here make sure you 32516 24:12:51,440 --> 24:12:56,639 grab the dot vs code folder if you are 32517 24:12:54,232 --> 24:12:58,639 using vs code with this course that's 32518 24:12:56,639 --> 24:13:01,600 going to give you the basic settings we 32519 24:12:58,639 --> 24:13:04,479 need to do our c plus plus program i am 32520 24:13:01,600 --> 24:13:07,832 going to copy these guys down and put 32521 24:13:04,479 --> 24:13:10,479 these in our current project and again 32522 24:13:07,831 --> 24:13:13,360 make sure you have the main cpp file the 32523 24:13:10,479 --> 24:13:15,512 person class and the engineer class and 32524 24:13:13,360 --> 24:13:17,119 these are going to be our starting point 32525 24:13:15,512 --> 24:13:19,191 so we're going to open this little guy 32526 24:13:17,119 --> 24:13:22,231 in visual studio code by dragging and 32527 24:13:19,191 --> 24:13:25,039 dropping here we're going to look at our 32528 24:13:22,232 --> 24:13:28,320 main cpp program we are not going to 32529 24:13:25,039 --> 24:13:29,759 include nurse because we don't have this 32530 24:13:29,759 --> 24:13:35,039 and we can really take out everything we 32531 24:13:32,479 --> 24:13:37,599 have in the main function we're going to 32532 24:13:35,039 --> 24:13:40,079 be adding our own thing here we also 32533 24:13:37,600 --> 24:13:42,320 don't need the player class here we're 32534 24:13:40,080 --> 24:13:45,040 just going to be using person and 32535 24:13:42,320 --> 24:13:46,960 engineer let's look at the person class 32536 24:13:45,039 --> 24:13:48,799 it is going to have the member variables 32537 24:13:48,800 --> 24:13:53,120 we are going to have a public member 32538 24:13:50,720 --> 24:13:55,831 variable the full name a protected 32539 24:13:53,119 --> 24:13:58,479 member variable mh and a private member 32540 24:13:55,831 --> 24:14:01,511 variable m address here we are going to 32541 24:13:58,479 --> 24:14:04,639 create an engineer class which is going 32542 24:14:01,512 --> 24:14:06,479 to privately inherit from person 32543 24:14:04,639 --> 24:14:09,191 so it is going to make everything 32544 24:14:06,479 --> 24:14:12,720 private to itself the engineer class is 32545 24:14:09,191 --> 24:14:15,679 going to have a person part in at but 32546 24:14:12,720 --> 24:14:17,831 the member variables we get from the 32547 24:14:15,679 --> 24:14:22,000 person class are going to be stripped 32548 24:14:17,831 --> 24:14:24,399 down to private access level except for 32549 24:14:22,000 --> 24:14:26,639 the member variables which are already 32550 24:14:24,399 --> 24:14:29,191 private in the base class and that 32551 24:14:26,639 --> 24:14:31,360 happens to be the address so it doesn't 32552 24:14:29,191 --> 24:14:33,599 matter which kind of inheritance you do 32553 24:14:31,360 --> 24:14:36,160 the m address member variable is going 32554 24:14:33,600 --> 24:14:38,552 to still be not accessible to 32555 24:14:36,160 --> 24:14:41,600 inheritance classes this is going to 32556 24:14:38,551 --> 24:14:44,479 still be private to the base class which 32557 24:14:41,600 --> 24:14:46,639 is the person class here so the 32558 24:14:44,479 --> 24:14:50,080 full name is going to be stripped down 32559 24:14:46,639 --> 24:14:52,160 to private access level in engineer the 32560 24:14:50,080 --> 24:14:54,960 edge is also going to be stripped down 32561 24:14:52,160 --> 24:14:58,639 to private access and these are going to 32562 24:14:54,960 --> 24:15:01,192 be private now to the engineer class and 32563 24:14:58,639 --> 24:15:03,831 any class that tries to inherit from 32564 24:15:01,191 --> 24:15:06,720 engineer is not going to have access to 32565 24:15:03,831 --> 24:15:08,551 these guys so if we do any kind of 32566 24:15:06,720 --> 24:15:10,551 inheritance from engineer we want to 32567 24:15:08,551 --> 24:15:12,800 have access to full name we want to have 32568 24:15:10,551 --> 24:15:16,000 access to mh because these are going to 32569 24:15:12,800 --> 24:15:18,160 be private to this class here this is 32570 24:15:16,000 --> 24:15:20,320 the setup we want to understand in this 32571 24:15:18,160 --> 24:15:22,000 lecture so what we're going to do we're 32572 24:15:20,320 --> 24:15:25,120 going to set up a new class which is 32573 24:15:22,000 --> 24:15:28,000 going to inherit from our engineer class 32574 24:15:25,119 --> 24:15:29,759 and it is going to do public inheritance 32575 24:15:28,000 --> 24:15:31,679 and we're going to see how this works 32576 24:15:29,759 --> 24:15:33,439 we're going to create a civil engineer 32577 24:15:31,679 --> 24:15:35,511 class which is going to inherit from 32578 24:15:33,440 --> 24:15:37,680 engineer so let's do this we're going to 32579 24:15:35,512 --> 24:15:40,000 create a header file for our civil 32580 24:15:37,679 --> 24:15:41,919 engineer class and we're going to create 32581 24:15:41,919 --> 24:15:46,479 we are going to head over in our header 32582 24:15:44,399 --> 24:15:49,439 file and put in the class it's not going 32583 24:15:46,479 --> 24:15:51,440 to be anything sophisticated we're just 32584 24:15:49,440 --> 24:15:53,440 going to have in our include guard we're 32585 24:15:51,440 --> 24:15:55,600 going to create a civil engineer class 32586 24:15:53,440 --> 24:15:58,232 which is going to publicly inherit from 32587 24:15:55,600 --> 24:16:01,192 engineer but remember everything was 32588 24:15:58,232 --> 24:16:03,832 made private to the engineer class so 32589 24:16:01,191 --> 24:16:05,759 even if we're doing public inheritance 32590 24:16:03,831 --> 24:16:08,080 the full name member variable is going 32591 24:16:05,759 --> 24:16:10,080 to be private to engineer the age is 32592 24:16:08,080 --> 24:16:12,479 going to be private to engineer and 32593 24:16:10,080 --> 24:16:14,872 engineer won't have any access to these 32594 24:16:12,479 --> 24:16:17,360 guys but before we really play with that 32595 24:16:14,872 --> 24:16:19,040 we're going to put in our implementation 32596 24:16:19,039 --> 24:16:24,079 so let's put in the code here we're 32597 24:16:21,279 --> 24:16:26,232 going to put in the implementation and 32598 24:16:24,080 --> 24:16:29,191 we're going to have our constructor in 32599 24:16:26,232 --> 24:16:32,480 place but notice what happens on our 32600 24:16:29,191 --> 24:16:35,512 output stream operator the output stream 32601 24:16:32,479 --> 24:16:38,159 operator is a friend of the civil 32602 24:16:35,512 --> 24:16:40,639 engineer class so it should have access 32603 24:16:38,160 --> 24:16:43,120 to anything we have in our civil 32604 24:16:40,639 --> 24:16:44,399 engineer class but notice what is 32605 24:16:44,399 --> 24:16:49,919 the get full name method is not working 32606 24:16:49,919 --> 24:16:54,800 private to the upstream class which 32607 24:16:54,800 --> 24:16:59,512 our engineer class and we can't really 32608 24:16:57,191 --> 24:17:02,720 access them even if we know that they 32609 24:16:59,512 --> 24:17:04,960 are really part of the interface of the 32610 24:17:02,720 --> 24:17:08,232 person class we have them as public 32611 24:17:04,960 --> 24:17:11,440 members of the person class but when the 32612 24:17:08,232 --> 24:17:14,552 engineer class did private inheritance 32613 24:17:11,440 --> 24:17:17,919 it made everything that is not private 32614 24:17:14,551 --> 24:17:20,319 to person private to itself so these 32615 24:17:17,919 --> 24:17:23,360 gators are going to be private to 32616 24:17:20,320 --> 24:17:25,760 engineer when the engineer does private 32617 24:17:23,360 --> 24:17:27,759 inheritance the member variables which 32618 24:17:25,759 --> 24:17:30,231 are public and protected all these 32619 24:17:27,759 --> 24:17:32,959 things are going to be stripped down to 32620 24:17:30,232 --> 24:17:35,832 private access level so they are going 32621 24:17:32,960 --> 24:17:37,832 to be private to engineer and any kind 32622 24:17:35,831 --> 24:17:39,919 of inheritance you're going to do is not 32623 24:17:37,831 --> 24:17:42,720 going to change that they are going to 32624 24:17:39,919 --> 24:17:45,512 be inherited as private to the engineer 32625 24:17:42,720 --> 24:17:48,720 class that's why we can't even call them 32626 24:17:45,512 --> 24:17:51,600 from a function which is a friend of our 32627 24:17:48,720 --> 24:17:53,279 civil engineer class and we know that if 32628 24:17:51,600 --> 24:17:55,680 you mark something as a friend it's 32629 24:17:53,279 --> 24:17:58,551 really going to have access to anything 32630 24:17:55,679 --> 24:18:01,599 you have including private members but 32631 24:17:58,551 --> 24:18:03,679 we can't call any of the members from 32632 24:18:01,600 --> 24:18:06,000 the engineer class because everything is 32633 24:18:03,679 --> 24:18:08,000 now private to the engineer class this 32634 24:18:06,000 --> 24:18:10,000 is what i want you to see if you do 32635 24:18:08,000 --> 24:18:11,831 private inheritance this is going to 32636 24:18:11,831 --> 24:18:16,959 anything from the upstream class and 32637 24:18:14,479 --> 24:18:18,872 make that private to the class that is 32638 24:18:16,960 --> 24:18:22,000 doing private inheritance this is the 32639 24:18:18,872 --> 24:18:24,320 message here and again if you go in any 32640 24:18:22,000 --> 24:18:27,279 method of the civil engineer class and 32641 24:18:24,320 --> 24:18:29,360 try to do any kind of access for example 32642 24:18:29,360 --> 24:18:34,000 method is not going to board well we're 32643 24:18:32,080 --> 24:18:37,440 going to see that this is inaccessible 32644 24:18:34,000 --> 24:18:39,600 here if we try to access m full name 32645 24:18:37,440 --> 24:18:41,680 for example change this to be something 32646 24:18:41,679 --> 24:18:46,079 gray this is not going to work if you 32647 24:18:46,080 --> 24:18:50,639 and change it to 45 for example this is 32648 24:18:48,551 --> 24:18:52,872 not going to work we already know that 32649 24:18:50,639 --> 24:18:54,479 the address is private to person so we 32650 24:18:52,872 --> 24:18:58,160 shouldn't really try to access it 32651 24:18:54,479 --> 24:19:00,959 because it is inaccessible regardless of 32652 24:18:58,160 --> 24:19:03,192 any kind of inheritance you do but the 32653 24:19:00,960 --> 24:19:06,080 members that were public from person 32654 24:19:03,191 --> 24:19:09,440 can't be accessed from any class that is 32655 24:19:06,080 --> 24:19:11,279 inheriting from person even if we know 32656 24:19:09,440 --> 24:19:13,760 that these things are public or 32657 24:19:11,279 --> 24:19:15,919 protected in the person class 32658 24:19:13,759 --> 24:19:18,720 and this is really what i want you to 32659 24:19:15,919 --> 24:19:20,319 see if we try and build our program 32660 24:19:18,720 --> 24:19:22,872 we're going to get a bunch of compiler 32661 24:19:20,320 --> 24:19:23,760 errors saying that we can't access these 32662 24:19:23,759 --> 24:19:29,119 from our civil engineer class we are 32663 24:19:26,960 --> 24:19:31,040 going to use the gcc compiler here 32664 24:19:29,119 --> 24:19:33,279 because it is our favorite and we're 32665 24:19:31,039 --> 24:19:35,831 going to see our compiler errors here 32666 24:19:33,279 --> 24:19:39,360 okay so we're going to get a bunch of 32667 24:19:35,831 --> 24:19:41,279 errors let's go up and see all of them 32668 24:19:39,360 --> 24:19:44,831 we're going to get an error saying that 32669 24:19:41,279 --> 24:19:48,800 getfullname is inaccessible within the 32670 24:19:44,831 --> 24:19:51,759 context of the c4 engineer header class 32671 24:19:48,800 --> 24:19:54,960 at line 13 we are calling this method 32672 24:19:51,759 --> 24:19:57,679 that's not going to work we have another 32673 24:19:54,960 --> 24:20:01,040 error here saying that the getfullname 32674 24:19:57,679 --> 24:20:03,191 method is not accessible from line 13 of 32675 24:20:03,191 --> 24:20:07,831 okay it is really the same error but if 32676 24:20:05,600 --> 24:20:10,800 we go down we see that we get a bunch of 32677 24:20:07,831 --> 24:20:13,831 other errors we have m full name which 32678 24:20:10,800 --> 24:20:16,232 is not accessible we have m edge which 32679 24:20:13,831 --> 24:20:18,871 is not accessible we can't really do 32680 24:20:16,232 --> 24:20:20,872 these kinds of things from the civil 32681 24:20:18,872 --> 24:20:23,512 engineer class because all these are 32682 24:20:20,872 --> 24:20:26,960 going to become private to the upstream 32683 24:20:23,512 --> 24:20:29,119 class that did private inheritance so 32684 24:20:29,119 --> 24:20:34,871 show you that even if we can't access 32685 24:20:32,000 --> 24:20:36,551 these guys they are still part of our 32686 24:20:34,872 --> 24:20:39,279 civil engineer class and we're going to 32687 24:20:36,551 --> 24:20:41,831 use a debugger to do that i am going to 32688 24:20:39,279 --> 24:20:44,160 take out these things or i can comment 32689 24:20:41,831 --> 24:20:46,159 them out and say that they are going to 32690 24:20:44,160 --> 24:20:48,160 give you compiler errors i think this is 32691 24:20:46,160 --> 24:20:49,760 going to be more descriptive so let's 32692 24:20:49,759 --> 24:20:54,399 and access to the edge is also going to 32693 24:20:52,000 --> 24:20:56,872 give you a compiler error and we're 32694 24:20:54,399 --> 24:20:59,119 going to go down and show you that we're 32695 24:20:56,872 --> 24:21:01,760 also going to have the same problem in 32696 24:20:59,119 --> 24:21:04,231 our output stream operator so if you 32697 24:21:01,759 --> 24:21:07,360 really want your output stream operator 32698 24:21:04,232 --> 24:21:09,192 to work here what you can do is put some 32699 24:21:09,191 --> 24:21:15,119 in the engineer class and make it give 32700 24:21:12,080 --> 24:21:18,232 you access to the members that were 32701 24:21:15,119 --> 24:21:20,399 eclipsed to private access in this class 32702 24:21:18,232 --> 24:21:22,160 here for example you can return the full 32703 24:21:20,399 --> 24:21:24,399 name you see that we can access that 32704 24:21:22,160 --> 24:21:27,040 here you can return the age but you 32705 24:21:24,399 --> 24:21:29,511 still don't have access to the m address 32706 24:21:27,039 --> 24:21:32,000 member variable because this is private 32707 24:21:29,512 --> 24:21:34,232 to the person class but i really don't 32708 24:21:32,000 --> 24:21:35,831 even want to do this because doing this 32709 24:21:35,831 --> 24:21:41,599 is sign of bad design in the first place 32710 24:21:39,039 --> 24:21:44,000 so if you need to do those kinds of 32711 24:21:41,600 --> 24:21:46,872 getters and forward them down to 32712 24:21:44,000 --> 24:21:49,600 inheritance classes why not change the 32713 24:21:46,872 --> 24:21:52,080 design in the first place and not to do 32714 24:21:49,600 --> 24:21:54,480 private inheritance here for example if 32715 24:21:52,080 --> 24:21:57,600 you change this inheritance to protected 32716 24:21:54,479 --> 24:22:00,959 or public these members are going to be 32717 24:21:57,600 --> 24:22:04,080 directly automatically forwarded down to 32718 24:22:00,960 --> 24:22:06,960 any inheritance class so just know that 32719 24:22:04,080 --> 24:22:09,279 you can put in public getters that are 32720 24:22:06,960 --> 24:22:11,680 going to give access to the full name 32721 24:22:09,279 --> 24:22:14,639 and age but you still want to be able to 32722 24:22:14,639 --> 24:22:19,831 from the inheritance class because this 32723 24:22:17,119 --> 24:22:22,080 is the design you achieve by marking 32724 24:22:19,831 --> 24:22:24,159 something as private in the base class 32725 24:22:22,080 --> 24:22:26,551 if you want the address to be forwarded 32726 24:22:24,160 --> 24:22:28,872 down you might change the access level 32727 24:22:26,551 --> 24:22:31,440 we have here and maybe make it protected 32728 24:22:28,872 --> 24:22:33,919 or public so this is the ideas i really 32729 24:22:31,440 --> 24:22:36,551 want you to grasp i am going to comment 32730 24:22:33,919 --> 24:22:38,160 out the offending code here because i 32731 24:22:36,551 --> 24:22:41,039 want you to be able to build this 32732 24:22:38,160 --> 24:22:43,832 program and see that even if we can't 32733 24:22:41,039 --> 24:22:46,799 access this get full name and get 32734 24:22:43,831 --> 24:22:49,831 address members from inheriting classes 32735 24:22:46,800 --> 24:22:52,400 they are still part of our interface we 32736 24:22:49,831 --> 24:22:55,039 just can't have access to them because 32737 24:22:52,399 --> 24:22:57,759 of the choices we have made in upstream 32738 24:22:55,039 --> 24:23:00,551 classes so what i am going to do i am 32739 24:22:57,759 --> 24:23:03,831 going to go in my main cpp file and i am 32740 24:23:00,551 --> 24:23:06,720 going to create an engineer object 32741 24:23:03,831 --> 24:23:09,279 i will also include civil engineer here 32742 24:23:06,720 --> 24:23:12,000 let's do that and i am going to create 32743 24:23:09,279 --> 24:23:14,720 an engineer object and i am going to 32744 24:23:12,000 --> 24:23:18,639 create a civil engineer object 32745 24:23:14,720 --> 24:23:20,639 and i am going to just say done here and 32746 24:23:18,639 --> 24:23:23,039 i am going to put the break point on 32747 24:23:20,639 --> 24:23:24,232 line 12. we are going to use a debugger 32748 24:23:24,232 --> 24:23:29,360 that engineer objects and the civil 32749 24:23:29,360 --> 24:23:34,639 still have a person part of them we just 32750 24:23:32,232 --> 24:23:37,832 can't access the interface that was 32751 24:23:34,639 --> 24:23:41,279 eclipsed by the engineer class so we are 32752 24:23:37,831 --> 24:23:43,119 going to hit the run tab here 32753 24:23:43,119 --> 24:23:47,759 start our debugging session by clicking 32754 24:23:47,759 --> 24:23:52,231 we are going to build our program and if 32755 24:23:50,232 --> 24:23:55,120 the build is good we're going to plug 32756 24:23:52,232 --> 24:23:57,512 the binary that was generated into our 32757 24:23:55,119 --> 24:24:00,080 debugger and we are going to hit our 32758 24:23:57,512 --> 24:24:02,551 break point in a minute once we hit our 32759 24:24:00,080 --> 24:24:04,400 break point we are going to look at the 32760 24:24:02,551 --> 24:24:06,399 local variables we have in the main 32761 24:24:04,399 --> 24:24:09,039 function we're going to see them on the 32762 24:24:06,399 --> 24:24:12,231 left here if we expand we're going to 32763 24:24:09,039 --> 24:24:14,639 see that we have an engineer one object 32764 24:24:12,232 --> 24:24:17,192 and we have a civil engineer one object 32765 24:24:14,639 --> 24:24:19,512 if we look at our engineer we're going 32766 24:24:17,191 --> 24:24:22,959 to see that we have a person part of 32767 24:24:19,512 --> 24:24:25,040 this which is our base class and if we 32768 24:24:22,960 --> 24:24:29,440 look at civil engineer it's also going 32769 24:24:25,039 --> 24:24:32,479 to have a base of engineer so any civil 32770 24:24:29,440 --> 24:24:34,872 engineer is also going to be an engineer 32771 24:24:32,479 --> 24:24:38,551 but the engineer part of the civil 32772 24:24:34,872 --> 24:24:40,720 engineer also has a person part of that 32773 24:24:38,551 --> 24:24:42,551 you should really see this information 32774 24:24:40,720 --> 24:24:45,191 and the main message i want to convey 32775 24:24:42,551 --> 24:24:48,159 here is that all these objects have a 32776 24:24:45,191 --> 24:24:51,599 person part of them we just can't access 32777 24:24:48,160 --> 24:24:54,552 that part in objects of our inheritance 32778 24:24:51,600 --> 24:24:57,279 classes because of the design decisions 32779 24:24:54,551 --> 24:24:59,599 we have made let's close this off now 32780 24:24:57,279 --> 24:25:01,919 that you have seen this and again 32781 24:25:01,919 --> 24:25:05,759 inheritance hierarchy the base class is 32782 24:25:05,759 --> 24:25:08,479 and it's going to have these member 32783 24:25:08,479 --> 24:25:13,191 but if we do private inheritance from 32784 24:25:10,960 --> 24:25:16,480 the person class anything that is not 32785 24:25:13,191 --> 24:25:19,512 public is going to be eclipsed down to 32786 24:25:16,479 --> 24:25:21,679 private level access so any other class 32787 24:25:19,512 --> 24:25:24,160 that is going to inherit from our 32788 24:25:21,679 --> 24:25:26,551 engineer class now is not going to have 32789 24:25:24,160 --> 24:25:29,040 direct access to these members because 32790 24:25:26,551 --> 24:25:31,831 they are going to become private to this 32791 24:25:29,039 --> 24:25:33,679 class here and that's the experience we 32792 24:25:33,679 --> 24:25:38,479 use any of this methods and members we 32793 24:25:38,479 --> 24:25:41,759 this is not going to work because they 32794 24:25:41,759 --> 24:25:46,080 private to the engineer class this is 32795 24:25:43,600 --> 24:25:47,440 the message here so the get full name 32796 24:25:47,440 --> 24:25:52,872 methods are not going to work if we try 32797 24:25:50,232 --> 24:25:55,040 to use the contract count member which 32798 24:25:52,872 --> 24:25:57,440 comes from the engineer class that's not 32799 24:25:55,039 --> 24:25:59,119 also going to work if we look in our 32800 24:25:57,440 --> 24:26:00,551 header and try to use any of these 32801 24:25:59,119 --> 24:26:02,799 members we're going to get compiler 32802 24:26:00,551 --> 24:26:05,440 errors because all these guys are now 32803 24:26:02,800 --> 24:26:07,512 private to the engineer class this is 32804 24:26:05,440 --> 24:26:10,080 really all i wanted to share in this 32805 24:26:07,512 --> 24:26:12,320 lecture let's head over to the cpp file 32806 24:26:10,080 --> 24:26:14,479 and comment this out again because i 32807 24:26:12,320 --> 24:26:16,640 don't want to leave you with these 32808 24:26:14,479 --> 24:26:19,279 compiler errors i hope you found this 32809 24:26:19,279 --> 24:26:23,360 we are going to stop here in this one in 32810 24:26:21,679 --> 24:26:26,551 the next one we're going to see that we 32811 24:26:23,360 --> 24:26:27,759 can actually resurrect some members back 32812 24:26:27,759 --> 24:26:32,720 if they were eclipsed by the kind of 32813 24:26:30,160 --> 24:26:33,760 inheritance we did from an upstream 32814 24:26:33,759 --> 24:26:37,439 go ahead and finish up here and meet me 32815 24:26:37,440 --> 24:26:42,160 in this lecture we're going to explore 32816 24:26:39,440 --> 24:26:44,960 how we can resurrect members back in 32817 24:26:42,160 --> 24:26:47,919 scope so what do we mean here suppose we 32818 24:26:44,960 --> 24:26:50,320 have our trusty inheritance tree here we 32819 24:26:47,919 --> 24:26:52,479 have a base class person the engineer 32820 24:26:50,320 --> 24:26:54,480 class is going to do private inheritance 32821 24:26:52,479 --> 24:26:55,440 and it's going to really be selfish and 32822 24:26:55,440 --> 24:27:00,639 i am going to take everything i can from 32823 24:26:57,600 --> 24:27:04,320 the upstream person class but i am going 32824 24:27:00,639 --> 24:27:06,960 to make everything my own and nobody 32825 24:27:04,320 --> 24:27:10,232 inherited from me will have access to 32826 24:27:06,960 --> 24:27:12,552 these guys for example m1 and m2 are 32827 24:27:10,232 --> 24:27:13,512 going to be private to the engineer 32828 24:27:13,512 --> 24:27:19,040 starting here when the engineer class 32829 24:27:16,320 --> 24:27:21,440 does private inheritance so any other 32830 24:27:19,039 --> 24:27:23,360 class for example civil engineer which 32831 24:27:23,360 --> 24:27:27,440 the engineer class in any way won't have 32832 24:27:27,440 --> 24:27:34,000 to the m1 and m2 member variables so 32833 24:27:31,279 --> 24:27:36,872 suppose for some reason that we have 32834 24:27:34,000 --> 24:27:40,000 this kind of setup here but again we 32835 24:27:36,872 --> 24:27:42,551 want some members to be accessible to 32836 24:27:42,551 --> 24:27:47,279 suppose for example we want the m1 32837 24:27:45,119 --> 24:27:50,159 member variable to still be accessible 32838 24:27:47,279 --> 24:27:52,639 to downstream classes while we have done 32839 24:27:50,160 --> 24:27:55,040 private inheritance c plus plus is 32840 24:27:52,639 --> 24:27:57,600 really weird in a way it is going to 32841 24:27:55,039 --> 24:28:00,000 work hard to really prevent you to do 32842 24:27:57,600 --> 24:28:01,832 thanks and that gives you a back door 32843 24:28:01,831 --> 24:28:06,399 allow the same thing you were trying to 32844 24:28:04,160 --> 24:28:08,872 avoid in the same place it is the same 32845 24:28:06,399 --> 24:28:11,831 kind of logic we have here it allows you 32846 24:28:08,872 --> 24:28:15,120 to use private inheritance to prevent 32847 24:28:11,831 --> 24:28:18,080 downstream classes to access to anything 32848 24:28:15,119 --> 24:28:20,159 you get from upstream classes but then 32849 24:28:18,080 --> 24:28:23,279 again it allows you to change that and 32850 24:28:20,160 --> 24:28:25,279 relax the access to downstream classes 32851 24:28:23,279 --> 24:28:29,279 for example we can change the access of 32852 24:28:25,279 --> 24:28:32,800 m1 and m2 make it more relaxed make it 32853 24:28:29,279 --> 24:28:35,119 public or protected if we wanted that so 32854 24:28:32,800 --> 24:28:36,872 let's take a look at some code because i 32855 24:28:35,119 --> 24:28:39,360 know that these things can really be 32856 24:28:36,872 --> 24:28:41,040 confusing okay we are going to start 32857 24:28:39,360 --> 24:28:43,512 from a person class which is going to 32858 24:28:41,039 --> 24:28:45,191 have a bunch of member variables we will 32859 24:28:43,512 --> 24:28:46,720 also have a bunch of getters that are 32860 24:28:45,191 --> 24:28:49,279 going to return all these member 32861 24:28:46,720 --> 24:28:52,872 variables and we are going to do private 32862 24:28:49,279 --> 24:28:56,080 inheritance from person in an engineer 32863 24:28:52,872 --> 24:28:58,800 class this is going to strip down access 32864 24:28:56,080 --> 24:28:59,680 to the members we get from person 32865 24:28:59,679 --> 24:29:04,551 private level access for example they 32866 24:29:02,399 --> 24:29:06,799 get full name member function the gate 32867 24:29:04,551 --> 24:29:09,831 age the gate address member functions 32868 24:29:09,831 --> 24:29:15,191 engineer class now but for some reason 32869 24:29:12,800 --> 24:29:18,720 you might want to resurrect them 32870 24:29:15,191 --> 24:29:21,599 to a more relaxed access level and you 32871 24:29:18,720 --> 24:29:23,360 do that using the using keyword so the 32872 24:29:21,600 --> 24:29:24,960 first thing you need to do is to choose 32873 24:29:23,360 --> 24:29:27,119 the section in which you're going to be 32874 24:29:24,960 --> 24:29:29,192 putting these using statements for 32875 24:29:27,119 --> 24:29:31,119 example because we put them in a 32876 24:29:31,119 --> 24:29:35,599 all these guys are going to be 32877 24:29:32,639 --> 24:29:38,399 resurrected with a nexus level of 32878 24:29:35,600 --> 24:29:40,552 protected if we put them in a public 32879 24:29:38,399 --> 24:29:43,759 section they are going to be resurrected 32880 24:29:40,551 --> 24:29:46,231 with a public access level and they are 32881 24:29:43,759 --> 24:29:48,799 going to be accessible from inheriting 32882 24:29:46,232 --> 24:29:51,040 classes one thing you should know is 32883 24:29:51,039 --> 24:29:56,959 something that is already private to an 32884 24:29:54,399 --> 24:29:58,871 upstream class for example we know that 32885 24:29:56,960 --> 24:30:01,440 our m address member variable was 32886 24:29:58,872 --> 24:30:04,232 private to the person class so we can't 32887 24:30:01,440 --> 24:30:06,080 really resurrect that in an inheritance 32888 24:30:04,232 --> 24:30:08,320 class if we do that we're going to get a 32889 24:30:06,080 --> 24:30:09,831 compiler error and hopefully this is 32890 24:30:08,320 --> 24:30:12,080 going to drive the point home that 32891 24:30:09,831 --> 24:30:14,551 private inheritance is really really 32892 24:30:12,080 --> 24:30:17,512 serious and that it's going to strip 32893 24:30:14,551 --> 24:30:20,639 down access to private level access in 32894 24:30:17,512 --> 24:30:23,040 an inheritance class and any downstream 32895 24:30:20,639 --> 24:30:26,232 inheritance class will never be able to 32896 24:30:23,039 --> 24:30:29,599 resurrect those member variables to a 32897 24:30:26,232 --> 24:30:32,320 more relaxed access level so because 32898 24:30:29,600 --> 24:30:35,279 everything here is inherited as private 32899 24:30:32,320 --> 24:30:37,192 by default any downstream inheriting 32900 24:30:35,279 --> 24:30:39,512 class will never be able to resurrect 32901 24:30:37,191 --> 24:30:42,000 these guys for example to public or 32902 24:30:39,512 --> 24:30:44,160 protected scope this is the message here 32903 24:30:42,000 --> 24:30:46,320 and you can use this kind of design if 32904 24:30:44,160 --> 24:30:48,000 you want that but personally i don't 32905 24:30:46,320 --> 24:30:50,160 like to do this because it's going to 32906 24:30:50,160 --> 24:30:55,512 hard to read and understand if you want 32907 24:30:52,960 --> 24:30:58,160 these guys to be public or protected why 32908 24:30:55,512 --> 24:31:00,232 not make them public or protected in the 32909 24:30:58,160 --> 24:31:03,040 first place and really not use private 32910 24:31:00,232 --> 24:31:05,360 inheritance that's going to be much more 32911 24:31:03,039 --> 24:31:07,831 direct to see and understand and your 32912 24:31:05,360 --> 24:31:10,232 code is going to be easier to work with 32913 24:31:07,831 --> 24:31:12,959 for you in the future or for some other 32914 24:31:10,232 --> 24:31:14,720 developers that will get to work on your 32915 24:31:12,960 --> 24:31:16,800 code in the future so this is the 32916 24:31:14,720 --> 24:31:18,800 message here you can use this feature to 32917 24:31:18,800 --> 24:31:24,872 to a relaxed access level and now that 32918 24:31:22,479 --> 24:31:27,039 you have an idea about this we're going 32919 24:31:24,872 --> 24:31:28,720 to head over to visual studio code and 32920 24:31:28,720 --> 24:31:33,831 here we are in our working folder the 32921 24:31:31,191 --> 24:31:36,159 current project is resurrecting members 32922 24:31:33,831 --> 24:31:38,399 back in context we are going to 32923 24:31:36,160 --> 24:31:40,320 completely reuse the code from the last 32924 24:31:38,399 --> 24:31:42,079 lecture because it's going to give us a 32925 24:31:40,320 --> 24:31:44,800 good starting point we can copy 32926 24:31:42,080 --> 24:31:47,831 everything except for the binary here we 32927 24:31:44,800 --> 24:31:50,232 don't need that in our current project 32928 24:31:47,831 --> 24:31:51,919 we're going to put in our starter code 32929 24:31:50,232 --> 24:31:54,160 and we're going to open this little guy 32930 24:31:51,919 --> 24:31:56,399 in visual studio code so we're going to 32931 24:31:54,160 --> 24:31:58,400 have our code we're going to have our 32932 24:31:56,399 --> 24:32:00,551 main function and we're going to have 32933 24:31:58,399 --> 24:32:02,231 all these objects created here i'm 32934 24:32:00,551 --> 24:32:04,720 really not sure if we're going to need 32935 24:32:02,232 --> 24:32:06,400 this yet so let's take this out and 32936 24:32:04,720 --> 24:32:08,639 we're going to look at our personal 32937 24:32:06,399 --> 24:32:11,119 class it is going to have our members 32938 24:32:08,639 --> 24:32:13,191 here and we are going to have a bunch of 32939 24:32:11,119 --> 24:32:15,119 getters which are going to be returning 32940 24:32:13,191 --> 24:32:16,871 these member variables and we have a 32941 24:32:15,119 --> 24:32:19,360 constructor here which is really going 32942 24:32:16,872 --> 24:32:21,832 to build a person object nothing 32943 24:32:19,360 --> 24:32:23,680 complicated here we have an engineer 32944 24:32:21,831 --> 24:32:26,551 class which is going to privately 32945 24:32:23,679 --> 24:32:29,439 inherit from person and we know that 32946 24:32:26,551 --> 24:32:32,639 this is going to eclipse everything that 32947 24:32:29,440 --> 24:32:35,360 is not private in the person class to 32948 24:32:32,639 --> 24:32:37,440 private level access so everything is 32949 24:32:35,360 --> 24:32:40,160 going to be private to the engineer 32950 24:32:37,440 --> 24:32:43,040 class now but sometimes we don't want to 32951 24:32:40,160 --> 24:32:45,360 do that for example if we go down in our 32952 24:32:46,960 --> 24:32:51,360 the get full name and the getage methods 32953 24:32:49,679 --> 24:32:53,679 are going to be private to the app 32954 24:32:51,360 --> 24:32:56,551 stream engineer class and that's going 32955 24:32:53,679 --> 24:32:58,719 to break the code for our output stream 32956 24:32:56,551 --> 24:33:00,800 operator and it's not going to work 32957 24:32:58,720 --> 24:33:03,440 because we want to have access to get 32958 24:33:00,800 --> 24:33:05,832 full name and get age and that's also 32959 24:33:03,440 --> 24:33:08,320 going to kill access we had on the get 32960 24:33:05,831 --> 24:33:11,360 address method which would give us 32961 24:33:08,320 --> 24:33:14,080 access to the address member that we 32962 24:33:11,360 --> 24:33:16,232 have in our person class let's make sure 32963 24:33:16,232 --> 24:33:20,400 i am confused here we have a lot of 32964 24:33:23,039 --> 24:33:27,279 it is down here so we have the get 32965 24:33:25,119 --> 24:33:29,679 address method which is going to return 32966 24:33:27,279 --> 24:33:32,639 the address we want to have access to 32967 24:33:29,679 --> 24:33:35,279 that in our civil engineer class to be 32968 24:33:32,639 --> 24:33:37,759 able to print all this information if we 32969 24:33:35,279 --> 24:33:40,872 also want to print the contract count we 32970 24:33:37,759 --> 24:33:43,039 can go in our engineer class and put in 32971 24:33:40,872 --> 24:33:45,192 a method which is going to return that 32972 24:33:45,191 --> 24:33:49,759 and say get contract count and this is 32973 24:33:47,919 --> 24:33:51,759 going to be a const member function 32974 24:33:49,759 --> 24:33:53,919 because it's not going to be modifying 32975 24:33:51,759 --> 24:33:55,759 our object we're just going to return a 32976 24:33:53,919 --> 24:33:58,399 value we're going to return the contract 32977 24:33:55,759 --> 24:34:01,919 count and we can go in our civil 32978 24:33:58,399 --> 24:34:03,279 engineer implementation file and change 32979 24:34:04,551 --> 24:34:08,959 get contract account we can do that okay 32980 24:34:06,960 --> 24:34:11,120 we have this method now we want this to 32981 24:34:08,960 --> 24:34:13,512 work this is going to work because we 32982 24:34:11,119 --> 24:34:15,919 are doing public inheritance from the 32983 24:34:13,512 --> 24:34:18,160 civil engineer class but we want this to 32984 24:34:15,919 --> 24:34:20,639 work we want to get full name get age 32985 24:34:18,160 --> 24:34:23,760 and get address to work here and we can 32986 24:34:20,639 --> 24:34:26,872 use the using feature to resurrect these 32987 24:34:23,759 --> 24:34:29,679 members to an access level which will 32988 24:34:26,872 --> 24:34:32,320 allow our civil engineer class to pick 32989 24:34:29,679 --> 24:34:35,039 them up so we can go in our engineer 32990 24:34:32,320 --> 24:34:36,960 class and set up for example a public or 32991 24:34:35,039 --> 24:34:39,119 protected scope we're going to use 32992 24:34:36,960 --> 24:34:40,960 protected because we want these to only 32993 24:34:40,960 --> 24:34:44,800 inherited classes for example so we're 32994 24:34:42,960 --> 24:34:46,872 going to say protected and we're going 32995 24:34:44,800 --> 24:34:48,551 to say using this is the syntax we're 32996 24:34:46,872 --> 24:34:50,639 going to say person and we're going to 32997 24:34:48,551 --> 24:34:53,831 say get full name we're going to say 32998 24:34:50,639 --> 24:34:55,831 using person get age going to choose 32999 24:34:53,831 --> 24:34:59,039 this and we are going to say using 33000 24:34:55,831 --> 24:35:01,439 person and say get address i think we 33001 24:34:59,039 --> 24:35:03,360 have that method here the moment we do 33002 24:35:01,440 --> 24:35:04,872 this we're going to go in our civil 33003 24:35:03,360 --> 24:35:07,759 engineer class you're going to see that 33004 24:35:04,872 --> 24:35:11,680 it is going to work now because what we 33005 24:35:07,759 --> 24:35:14,551 just did is resurrect these members to 33006 24:35:11,679 --> 24:35:17,279 have protected access level in the 33007 24:35:14,551 --> 24:35:18,959 engineer class so the inheritance 33008 24:35:17,279 --> 24:35:21,919 classes are going to look at this and 33009 24:35:18,960 --> 24:35:24,160 see that they have a protected access 33010 24:35:21,919 --> 24:35:26,551 level in the engineer class and they are 33011 24:35:24,160 --> 24:35:28,320 going to be forwarded down to any 33012 24:35:26,551 --> 24:35:30,000 inheritance class we could also change 33013 24:35:28,320 --> 24:35:32,232 this to public this is also going to 33014 24:35:30,000 --> 24:35:35,119 work they are going to be inherited as 33015 24:35:32,232 --> 24:35:37,279 public and hopefully you can see that 33016 24:35:35,119 --> 24:35:39,759 any member that you resurrect it's going 33017 24:35:37,279 --> 24:35:42,872 to take the excess level of the section 33018 24:35:39,759 --> 24:35:45,511 where you resurrect it if we resurrect 33019 24:35:42,872 --> 24:35:47,919 them in a public scope they are going to 33020 24:35:45,512 --> 24:35:50,232 be resurrected with a public access 33021 24:35:47,919 --> 24:35:52,800 level if we resurrect them in a 33022 24:35:50,232 --> 24:35:54,800 protected scope they are going to be 33023 24:35:54,800 --> 24:36:00,080 protected access level and this is the 33024 24:35:56,960 --> 24:36:03,440 message here now if you happen to have a 33025 24:36:00,080 --> 24:36:06,479 bunch of overloads in an upstream class 33026 24:36:03,440 --> 24:36:07,360 for example suppose we have a bunch of 33027 24:36:07,360 --> 24:36:13,831 functions in the person class we can go 33028 24:36:10,639 --> 24:36:15,191 in and put in let's do that we can go 33029 24:36:15,191 --> 24:36:20,799 double or ent let's say and that's going 33030 24:36:18,160 --> 24:36:22,000 to be much simpler we can say add and 33031 24:36:22,000 --> 24:36:26,320 a and say ent b and say ent c we're 33032 24:36:26,320 --> 24:36:31,040 three things that we are adding this is 33033 24:36:28,720 --> 24:36:33,279 going to be a const member function it's 33034 24:36:31,039 --> 24:36:36,159 not going to be modifying our members in 33035 24:36:33,279 --> 24:36:38,080 any way so we're going to return 33036 24:36:38,080 --> 24:36:42,960 plus c this is something we can return 33037 24:36:42,960 --> 24:36:47,120 and we're going to do an overload of the 33038 24:36:49,119 --> 24:36:54,399 the addition of two parameters so we're 33039 24:36:51,831 --> 24:36:57,511 going to only have a and b and i think 33040 24:36:54,399 --> 24:36:59,759 we can use a and b in the first 33041 24:36:57,512 --> 24:37:01,832 overload here that's going to be much 33042 24:36:59,759 --> 24:37:04,231 easier to follow so we have one that 33043 24:37:04,232 --> 24:37:08,232 and we have another one which is going 33044 24:37:05,600 --> 24:37:11,192 to add three parameters if we 33045 24:37:08,232 --> 24:37:14,160 privately inherit from person these guys 33046 24:37:11,191 --> 24:37:17,119 are going to be stripped down to private 33047 24:37:14,160 --> 24:37:19,919 level access in the engineer class and 33048 24:37:17,119 --> 24:37:22,639 they want to be accessible from in any 33049 24:37:19,919 --> 24:37:25,919 other downstream class so for example if 33050 24:37:22,639 --> 24:37:27,759 we head over in our civil engineer class 33051 24:37:27,759 --> 24:37:32,159 10 to two for example we're going to get 33052 24:37:30,399 --> 24:37:35,119 a compiler error because we don't have 33053 24:37:32,160 --> 24:37:38,639 access to this but we can also resurrect 33054 24:37:35,119 --> 24:37:40,399 these ad members in our engineer class 33055 24:37:38,639 --> 24:37:41,759 so let's go there and we're going to set 33056 24:37:41,759 --> 24:37:47,919 section in this class here and we're 33057 24:37:44,399 --> 24:37:50,399 going to say using person add this is 33058 24:37:47,919 --> 24:37:53,512 going to resurrect all the overloads 33059 24:37:50,399 --> 24:37:55,191 that we have in our upstream class and 33060 24:37:53,512 --> 24:37:57,512 this is going to be the case because we 33061 24:37:55,191 --> 24:37:58,399 resurrect only based on the name of the 33062 24:37:59,119 --> 24:38:05,440 the same name for a member function can 33063 24:38:02,000 --> 24:38:08,320 be shared by many overloads so if we do 33064 24:38:05,440 --> 24:38:10,000 this kind of resurrecting we are doing 33065 24:38:10,000 --> 24:38:14,720 member function from person we are going 33066 24:38:12,160 --> 24:38:17,360 to get all the overloads if we go in 33067 24:38:14,720 --> 24:38:19,119 civil engineer we will be able to use 33068 24:38:17,360 --> 24:38:21,680 the member function taking two 33069 24:38:19,119 --> 24:38:24,319 parameters we will also be able to use 33070 24:38:21,679 --> 24:38:26,479 the one that takes three parameters they 33071 24:38:24,320 --> 24:38:28,872 are all going to be resurrected here 33072 24:38:26,479 --> 24:38:30,319 this is the message i want to convey if 33073 24:38:28,872 --> 24:38:32,551 we do this you're going to see that this 33074 24:38:30,320 --> 24:38:35,920 is going to work and our code is going 33075 24:38:32,551 --> 24:38:37,512 to just world fine let's build and see 33076 24:38:35,919 --> 24:38:40,551 if the build is good actually we're 33077 24:38:37,512 --> 24:38:42,400 going to pass this through gcc the world 33078 24:38:40,551 --> 24:38:45,039 is going to be good so everything is 33079 24:38:42,399 --> 24:38:47,039 working just fine and the message i 33080 24:38:45,039 --> 24:38:49,360 really wanted to convey is that it is 33081 24:38:47,039 --> 24:38:53,119 possible to resurrect members 33082 24:38:49,360 --> 24:38:56,232 in a more resurrected access level if 33083 24:38:53,119 --> 24:39:00,159 you are inheriting using a really 33084 24:38:56,232 --> 24:39:02,720 constrained base class access specifier 33085 24:39:00,160 --> 24:39:03,680 for example we are inheriting using a 33086 24:39:03,679 --> 24:39:07,511 access specifier this is going to 33087 24:39:05,679 --> 24:39:10,079 eclipse everything we get from the 33088 24:39:07,512 --> 24:39:12,320 upstream class to private level but if 33089 24:39:10,080 --> 24:39:15,119 you think that's too much and you have a 33090 24:39:12,320 --> 24:39:18,080 few members that you would like to put 33091 24:39:15,119 --> 24:39:20,639 in a more relaxed access level you can 33092 24:39:18,080 --> 24:39:23,191 use this resurrecting feature and it is 33093 24:39:20,639 --> 24:39:25,440 going to make things accessible in 33094 24:39:23,191 --> 24:39:27,759 downstream classes if that's what you 33095 24:39:25,440 --> 24:39:29,360 want in your program and again i hope 33096 24:39:27,759 --> 24:39:31,831 this drives the point to home that 33097 24:39:29,360 --> 24:39:34,320 ziploc plus is going to sometimes work 33098 24:39:31,831 --> 24:39:36,871 really hard to prevent you from doing 33099 24:39:34,320 --> 24:39:39,120 one thing and then again give you a back 33100 24:39:36,872 --> 24:39:41,680 door you can go through to do that same 33101 24:39:39,119 --> 24:39:44,159 thing this is the same thing here we are 33102 24:39:41,679 --> 24:39:46,079 doing private inheritance but we aren't 33103 24:39:48,000 --> 24:39:54,080 relax the effects of private inheritance 33104 24:39:50,960 --> 24:39:56,639 by bringing things in a more relaxed 33105 24:39:54,080 --> 24:39:59,040 access level and you can use this if it 33106 24:39:56,639 --> 24:40:00,639 makes sense for your code again i don't 33107 24:39:59,039 --> 24:40:02,871 like this personally because it's going 33108 24:40:00,639 --> 24:40:04,720 to make code confusing if other 33109 24:40:02,872 --> 24:40:06,479 developers see that you are doing 33110 24:40:04,720 --> 24:40:08,479 private inheritance they are going to 33111 24:40:06,479 --> 24:40:10,159 expect everything from the app stream 33112 24:40:10,160 --> 24:40:14,080 to the class that is doing private 33113 24:40:11,919 --> 24:40:16,232 inheritance and if they see that they 33114 24:40:14,080 --> 24:40:18,960 can use this thanks that should be 33115 24:40:16,232 --> 24:40:20,800 private by default and downstream 33116 24:40:18,960 --> 24:40:23,360 classes this is really going to confuse 33117 24:40:20,800 --> 24:40:25,192 them and it is going to cost them more 33118 24:40:23,360 --> 24:40:27,440 minutes to look at your code to really 33119 24:40:25,191 --> 24:40:29,679 understand what is going on so if you 33120 24:40:27,440 --> 24:40:33,119 really want this dance to be accessible 33121 24:40:29,679 --> 24:40:35,599 in downstream classes why not do public 33122 24:40:33,119 --> 24:40:38,159 or protected inheritance in the first 33123 24:40:35,600 --> 24:40:40,552 place this is my argument here but it is 33124 24:40:38,160 --> 24:40:42,960 going to be your choice my job here is 33125 24:40:40,551 --> 24:40:45,039 to make you aware of the possibilities 33126 24:40:42,960 --> 24:40:47,440 that you have in the ziploc plus 33127 24:40:45,039 --> 24:40:49,439 programming language okay the last thing 33128 24:40:47,440 --> 24:40:51,680 i want you to see in this lecture is 33129 24:40:49,440 --> 24:40:54,080 that if there is something that was 33130 24:40:51,679 --> 24:40:56,319 already private in an app stream class 33131 24:40:54,080 --> 24:40:58,080 you can't resurrect that if you try to 33132 24:40:56,320 --> 24:41:00,000 do that you're going to get a compiler 33133 24:40:58,080 --> 24:41:02,320 error this is also going to drive the 33134 24:41:00,000 --> 24:41:04,720 point home that you can also resurrect 33135 24:41:02,320 --> 24:41:07,280 member variables so far what we have 33136 24:41:04,720 --> 24:41:09,919 only resurrected was all member 33137 24:41:07,279 --> 24:41:11,831 functions and we can also resurrect 33138 24:41:09,919 --> 24:41:15,279 member variables so we're going to try 33139 24:41:11,831 --> 24:41:17,759 and resurrect the m address member in 33140 24:41:15,279 --> 24:41:20,232 the engineer class and let's see how 33141 24:41:17,759 --> 24:41:22,319 this works out so let's go in our 33142 24:41:20,232 --> 24:41:25,279 engineer class we can go in our 33143 24:41:22,320 --> 24:41:27,192 protected section we can do this really 33144 24:41:25,279 --> 24:41:29,759 in any kind of section we're going to 33145 24:41:27,191 --> 24:41:32,319 say using and we're going to say person 33146 24:41:29,759 --> 24:41:34,399 and we're going to say m address you see 33147 24:41:32,320 --> 24:41:36,552 that this is not going to be 33148 24:41:34,399 --> 24:41:38,231 available in autocomplete this is going 33149 24:41:36,551 --> 24:41:40,159 to be a sign that this is going to give 33150 24:41:38,232 --> 24:41:41,680 us a compiler error and if we're trying 33151 24:41:40,160 --> 24:41:43,440 to do that we're going to see that we're 33152 24:41:41,679 --> 24:41:46,079 going to get a compiler error the 33153 24:41:43,440 --> 24:41:50,400 message here is that if something is 33154 24:41:46,080 --> 24:41:53,040 already private in a nap stream class 33155 24:41:50,399 --> 24:41:54,000 you can't resurrect it it is it claps 33156 24:41:55,119 --> 24:41:59,512 and hopefully this drives the point home 33157 24:41:57,039 --> 24:42:02,799 again that private inheritance is really 33158 24:41:59,512 --> 24:42:06,000 serious for example in our civil 33159 24:42:02,800 --> 24:42:08,400 engineer class we can't resurrect 33160 24:42:06,000 --> 24:42:11,039 something that was already eclipsed to 33161 24:42:08,399 --> 24:42:14,079 private level access by the engineer 33162 24:42:11,039 --> 24:42:16,551 class let's go in our personal class and 33163 24:42:14,080 --> 24:42:17,759 see if we have any other member function 33164 24:42:17,759 --> 24:42:21,919 play with we're going to put in a member 33165 24:42:19,759 --> 24:42:23,599 function that says do something we're 33166 24:42:23,600 --> 24:42:27,760 do something and it's going to be 33167 24:42:25,679 --> 24:42:29,439 constant it's just going to say hello we 33168 24:42:27,759 --> 24:42:30,639 can do something like this we are 33169 24:42:30,639 --> 24:42:36,000 and this method is going to be eclipsed 33170 24:42:32,960 --> 24:42:37,920 down to private level access because we 33171 24:42:36,000 --> 24:42:40,960 are doing private inheritance from 33172 24:42:37,919 --> 24:42:42,800 person let's go back in our engineer and 33173 24:42:40,960 --> 24:42:44,552 comment this out because it is going to 33174 24:42:42,800 --> 24:42:45,832 give you a compiler error if you build 33175 24:42:45,831 --> 24:42:49,759 a weird compiler error i think i should 33176 24:42:48,000 --> 24:42:51,919 really show you this because we are 33177 24:42:49,759 --> 24:42:53,919 learning here so we're going to try and 33178 24:42:51,919 --> 24:42:56,000 build our program anyway we're going to 33179 24:42:56,000 --> 24:42:59,360 and we're going to see the kind of 33180 24:42:59,360 --> 24:43:05,600 and we are going to see that there's no 33181 24:43:02,232 --> 24:43:09,040 member std this is a problem we just 33182 24:43:05,600 --> 24:43:12,720 introduced in our class here i think to 33183 24:43:09,039 --> 24:43:15,759 solve this problem we can just put our 33184 24:43:12,720 --> 24:43:18,160 definition in the cpp file we can do 33185 24:43:15,759 --> 24:43:20,439 that we're going to grab this and we're 33186 24:43:18,160 --> 24:43:23,120 going to put the definition in 33187 24:43:20,440 --> 24:43:25,512 person.cpp because we have io stream 33188 24:43:23,119 --> 24:43:28,159 included in there this is going to make 33189 24:43:25,512 --> 24:43:30,872 our code work much better let's do this 33190 24:43:28,160 --> 24:43:33,919 we're going to say void and say person 33191 24:43:30,872 --> 24:43:36,320 and we're going to say sddc out hello 33192 24:43:33,919 --> 24:43:38,319 this is going to fix our problem here 33193 24:43:36,320 --> 24:43:40,232 and the only problem we will have will 33194 24:43:40,232 --> 24:43:46,232 where we are trying to access things 33195 24:43:42,872 --> 24:43:48,960 that are now private to the base class 33196 24:43:46,232 --> 24:43:50,872 why do we have this weekly line here 33197 24:43:50,872 --> 24:43:55,760 and try to build really make sure that 33198 24:43:55,759 --> 24:44:02,399 and it was a visual studio code messing 33199 24:43:58,960 --> 24:44:03,680 with us so we're going to bring this in 33200 24:44:02,399 --> 24:44:06,639 and the world and we're going to see the 33201 24:44:03,679 --> 24:44:09,039 kind of compiler error we get here 33202 24:44:06,639 --> 24:44:10,639 this is what i am trying to make you see 33203 24:44:09,039 --> 24:44:13,511 you see the bullet is finished with 33204 24:44:10,639 --> 24:44:16,872 errors if we go back we're going to see 33205 24:44:13,512 --> 24:44:19,119 that string person m address is private 33206 24:44:16,872 --> 24:44:20,960 within this context we can't really 33207 24:44:19,119 --> 24:44:23,679 resurrect it if we do this we're going 33208 24:44:20,960 --> 24:44:26,080 to get this compiler error here this is 33209 24:44:23,679 --> 24:44:27,439 the message you shouldn't really try to 33210 24:44:27,440 --> 24:44:32,080 something that is already private to an 33211 24:44:30,232 --> 24:44:34,800 upstream class and that's what we are 33212 24:44:32,080 --> 24:44:37,040 doing here we are trying to resurrect a 33213 24:44:34,800 --> 24:44:39,192 member which is private to person and 33214 24:44:37,039 --> 24:44:41,439 this is not allowed in c plus plus if we 33215 24:44:39,191 --> 24:44:42,479 do that we're going to get a compiler 33216 24:44:42,479 --> 24:44:48,231 okay so but what i want you to see here 33217 24:44:44,399 --> 24:44:51,360 is that a public member which was in the 33218 24:44:48,232 --> 24:44:54,720 base class was also eclipsed down to 33219 24:44:51,360 --> 24:44:57,360 private level access in engineer so if 33220 24:44:57,360 --> 24:45:01,440 the do something method in our civil 33221 24:45:00,000 --> 24:45:03,831 engineer class we're going to get a 33222 24:45:01,440 --> 24:45:06,080 compiler error so let's try and do that 33223 24:45:03,831 --> 24:45:08,231 we're going to put a public section why 33224 24:45:08,232 --> 24:45:13,400 and we're going to go down and say using 33225 24:45:13,512 --> 24:45:17,680 and it say do something we have this 33226 24:45:15,759 --> 24:45:20,000 method here but if we do this we're 33227 24:45:17,679 --> 24:45:22,871 going to get a compiler error because 33228 24:45:20,000 --> 24:45:25,440 this method is now private to the 33229 24:45:22,872 --> 24:45:28,160 engineer class and why is it private to 33230 24:45:25,440 --> 24:45:30,551 engineer well engineer is doing private 33231 24:45:28,160 --> 24:45:33,279 inheritance that's going to accept 33232 24:45:30,551 --> 24:45:36,000 everything to private level access by 33233 24:45:33,279 --> 24:45:39,279 default so this member is private to 33234 24:45:36,000 --> 24:45:41,440 engineer we are inheriting from engineer 33235 24:45:41,440 --> 24:45:46,160 and this is going to be already private 33236 24:45:43,679 --> 24:45:49,439 to engineer and we can't resurrect this 33237 24:45:46,160 --> 24:45:51,680 back so if you eclipse something using 33238 24:45:49,440 --> 24:45:54,960 private inheritance you want to be able 33239 24:45:51,679 --> 24:45:58,000 to resurrect that to a more relaxed 33240 24:45:54,960 --> 24:45:59,832 access level in downstream classes this 33241 24:45:58,000 --> 24:46:01,831 is the message i am trying to convey 33242 24:45:59,831 --> 24:46:03,279 here so this is also going to give us a 33243 24:46:03,279 --> 24:46:07,191 and if we try to build our program we're 33244 24:46:05,279 --> 24:46:09,679 going to see that let's pass this 33245 24:46:07,191 --> 24:46:12,080 through gcc we are going to see that's 33246 24:46:09,679 --> 24:46:14,319 the world finished with errors and if we 33247 24:46:12,080 --> 24:46:17,191 go back we are going to get some kind of 33248 24:46:14,320 --> 24:46:20,720 error that says that do something is 33249 24:46:17,191 --> 24:46:22,799 private to some class it is inaccessible 33250 24:46:20,720 --> 24:46:25,440 within this context this is the message 33251 24:46:22,800 --> 24:46:28,479 we get from this particular compiler we 33252 24:46:25,440 --> 24:46:30,960 are using but the bottom line is that 33253 24:46:28,479 --> 24:46:33,119 you shouldn't really try to resurrect 33254 24:46:30,960 --> 24:46:34,960 something that is already private to an 33255 24:46:33,119 --> 24:46:37,119 upstream class and this is what we are 33256 24:46:34,960 --> 24:46:39,512 doing here you shouldn't really do that 33257 24:46:37,119 --> 24:46:41,039 let's comment this out and get a code to 33258 24:46:41,039 --> 24:46:45,831 so the web is good and we are all fine 33259 24:46:43,440 --> 24:46:47,440 here this is really all i had to share 33260 24:46:45,831 --> 24:46:50,000 in this lecture i hope you found it 33261 24:46:47,440 --> 24:46:51,760 interesting we are going to stop here in 33262 24:46:50,000 --> 24:46:55,191 this one the next one we're going to 33263 24:46:51,759 --> 24:46:56,551 explore how constructors work with 33264 24:46:56,551 --> 24:47:00,080 go ahead and finish up here and meet me 33265 24:47:00,080 --> 24:47:05,040 in this lecture we're going to zoom in 33266 24:47:01,919 --> 24:47:06,399 at how derived objects are built and 33267 24:47:05,039 --> 24:47:09,279 we're going to zoom in on the 33268 24:47:06,399 --> 24:47:12,479 constructors that we have been ignoring 33269 24:47:09,279 --> 24:47:14,720 on purpose so far so here we have the 33270 24:47:12,479 --> 24:47:17,039 inheritance hierarchy we will be using 33271 24:47:14,720 --> 24:47:19,191 it is what we are familiar with at this 33272 24:47:17,039 --> 24:47:20,871 point we have a personal class which is 33273 24:47:19,191 --> 24:47:23,119 going to be our base class we have an 33274 24:47:20,872 --> 24:47:25,600 engineer class which is now going to be 33275 24:47:23,119 --> 24:47:29,191 doing public inheritance because we have 33276 24:47:25,600 --> 24:47:31,760 seen enough of private inheritance our 33277 24:47:29,191 --> 24:47:32,799 engineer class is now going to be nice 33278 24:47:32,800 --> 24:47:38,320 i am inheriting from person but i also 33279 24:47:35,360 --> 24:47:40,720 want my downstream classes to have 33280 24:47:38,320 --> 24:47:42,720 access to the cool stuff i am getting 33281 24:47:42,720 --> 24:47:47,440 our engineer class is behaving now we 33282 24:47:45,512 --> 24:47:49,512 are also going to have a civil engineer 33283 24:47:47,440 --> 24:47:52,000 class which is going to publicly inherit 33284 24:47:49,512 --> 24:47:55,440 from engineer but what we want to focus 33285 24:47:52,000 --> 24:47:57,512 on here is what we get from base classes 33286 24:47:55,440 --> 24:48:00,320 so the engineer class is going to have a 33287 24:47:57,512 --> 24:48:03,191 person part of that but it is also going 33288 24:48:00,320 --> 24:48:06,480 to build on top of that and add an 33289 24:48:03,191 --> 24:48:09,759 engineer level and customize what kind 33290 24:48:06,479 --> 24:48:12,000 of person it is so if we inherit from 33291 24:48:09,759 --> 24:48:14,231 engineer and that weld our civil 33292 24:48:12,000 --> 24:48:16,551 engineer class the first link to be 33293 24:48:14,232 --> 24:48:19,040 built by c plus plus is the person part 33294 24:48:16,551 --> 24:48:21,279 of this it is going to build an engineer 33295 24:48:19,039 --> 24:48:24,079 level of this and then we are going to 33296 24:48:24,080 --> 24:48:30,080 to add the customizations that the civil 33297 24:48:30,080 --> 24:48:35,440 and a c plus plus is going to do this 33298 24:48:32,639 --> 24:48:37,831 using constructors by default we are 33299 24:48:35,440 --> 24:48:40,160 going to be using default constructors 33300 24:48:37,831 --> 24:48:42,479 if we don't specify a way for the 33301 24:48:40,160 --> 24:48:44,000 basical structure to be called c plus 33302 24:48:42,479 --> 24:48:47,512 plus is going to call the default 33303 24:48:44,000 --> 24:48:50,320 constructor to build your base objects 33304 24:48:47,512 --> 24:48:52,960 so if we set up something like this and 33305 24:48:50,320 --> 24:48:54,872 try to run a program we're going to get 33306 24:48:52,960 --> 24:48:56,639 output like this we're going to see that 33307 24:48:54,872 --> 24:48:58,960 the person default constructor is going 33308 24:48:56,639 --> 24:49:01,831 to be called first and notice that we 33309 24:48:58,960 --> 24:49:04,160 are building a civil engineer object so 33310 24:49:01,831 --> 24:49:06,479 the default constructor from the most 33311 24:49:04,160 --> 24:49:08,552 base class is going to be called then 33312 24:49:06,479 --> 24:49:11,191 we're going to be building an engineer 33313 24:49:08,551 --> 24:49:13,679 layer on top of the person stuff that we 33314 24:49:11,191 --> 24:49:17,119 get then we're going to build a civil 33315 24:49:13,679 --> 24:49:19,359 engineer layer on top of the engineer 33316 24:49:17,119 --> 24:49:21,279 stuff that we have already got and the 33317 24:49:19,360 --> 24:49:23,512 compiler is going to do this using the 33318 24:49:21,279 --> 24:49:25,119 default constructors by default so we're 33319 24:49:23,512 --> 24:49:27,191 going to call the default constructor 33320 24:49:25,119 --> 24:49:29,279 for person first we're going to call the 33321 24:49:27,191 --> 24:49:31,119 default constructor for engineer and 33322 24:49:29,279 --> 24:49:33,360 then we're going to call the default 33323 24:49:31,119 --> 24:49:36,159 constructor for civil engineer to 33324 24:49:33,360 --> 24:49:38,000 complete the construction of our object 33325 24:49:36,160 --> 24:49:40,080 the object will be complete at this 33326 24:49:38,000 --> 24:49:43,440 point and we will be able to use it 33327 24:49:40,080 --> 24:49:45,119 later on in our program here and 33328 24:49:43,440 --> 24:49:46,400 hopefully this is going to drive the 33329 24:49:46,399 --> 24:49:51,360 that you should really really always 33330 24:49:49,039 --> 24:49:54,319 provide to the photoconstructors for 33331 24:49:51,360 --> 24:49:56,960 your classes because the compiler may 33332 24:49:54,320 --> 24:50:00,080 call these default constructors in 33333 24:49:56,960 --> 24:50:03,600 unexpected ways especially if your class 33334 24:50:00,080 --> 24:50:05,759 is part of an inheritance hierarchy for 33335 24:50:03,600 --> 24:50:08,552 example if somebody tries to build the 33336 24:50:05,759 --> 24:50:11,439 civil engineer object and you don't have 33337 24:50:08,551 --> 24:50:13,191 a default constructor for a person the 33338 24:50:11,440 --> 24:50:14,872 compiler is going to try and code that 33339 24:50:13,191 --> 24:50:17,512 and it's not going to find that and it 33340 24:50:14,872 --> 24:50:20,320 is going to throw a compiler error as we 33341 24:50:17,512 --> 24:50:22,232 are about to see in a minute okay so the 33342 24:50:20,320 --> 24:50:24,400 message really in this lecture is that 33343 24:50:22,232 --> 24:50:27,120 the most base part of your class is 33344 24:50:24,399 --> 24:50:28,639 going to be bolt so for example for our 33345 24:50:27,119 --> 24:50:30,959 civil engineer class we're going to 33346 24:50:28,639 --> 24:50:33,039 build the prison port first then we're 33347 24:50:30,960 --> 24:50:34,639 going to build the engineer park and 33348 24:50:33,039 --> 24:50:36,871 then we're going to build the civil 33349 24:50:34,639 --> 24:50:39,679 engineer part of that and then we're 33350 24:50:36,872 --> 24:50:42,320 going to finish the construction of our 33351 24:50:39,679 --> 24:50:43,831 object this is the message here now that 33352 24:50:42,320 --> 24:50:45,760 you notice we're going to head over to 33353 24:50:43,831 --> 24:50:46,959 visual studio code and play with this a 33354 24:50:46,960 --> 24:50:52,400 here we are in our working folder the 33355 24:50:49,759 --> 24:50:55,191 current project is default argument 33356 24:50:52,399 --> 24:50:56,959 constructor with inheritance we are 33357 24:50:55,191 --> 24:50:58,639 going to reuse the code from the last 33358 24:50:56,960 --> 24:51:01,280 lecture because that's going to give us 33359 24:50:58,639 --> 24:51:03,279 the classes that we need to play 33360 24:51:01,279 --> 24:51:04,959 with what we want to do in this lecture 33361 24:51:03,279 --> 24:51:07,919 and that's playing with default 33362 24:51:04,960 --> 24:51:09,512 constructors let's put in our starter 33363 24:51:07,919 --> 24:51:12,160 code and we're going to open this little 33364 24:51:09,512 --> 24:51:13,919 guy in visual studio code let's do this 33365 24:51:12,160 --> 24:51:16,320 so we're going to have our classes we're 33366 24:51:13,919 --> 24:51:17,831 going to have the main function which we 33367 24:51:17,831 --> 24:51:23,039 and we are going to look at the person 33368 24:51:20,720 --> 24:51:25,759 class and make sure we have a default 33369 24:51:23,039 --> 24:51:28,000 constructor we are not going to default 33370 24:51:25,759 --> 24:51:32,479 it this time because we want to have a 33371 24:51:28,000 --> 24:51:34,160 body we can put our output statements in 33372 24:51:32,479 --> 24:51:36,319 so in this case we're going to go in the 33373 24:51:34,160 --> 24:51:38,080 cpp file and put in a default 33374 24:51:36,320 --> 24:51:41,512 constructor we're going to say person 33375 24:51:38,080 --> 24:51:43,191 and person and person here and this is 33376 24:51:41,512 --> 24:51:47,040 going to be our constructor we're going 33377 24:51:43,191 --> 24:51:48,720 to say stdcl defaults constructor for 33378 24:51:48,720 --> 24:51:53,680 okay so the personal class is done we 33379 24:51:51,119 --> 24:51:56,479 have a default constructor that works as 33380 24:51:53,679 --> 24:51:58,479 we want and we're going to go to our 33381 24:51:56,479 --> 24:52:01,119 engineer class and we're going to change 33382 24:51:58,479 --> 24:52:03,440 it to do public inheritance i think this 33383 24:52:01,119 --> 24:52:04,231 is going to be much easier to deal with 33384 24:52:04,232 --> 24:52:09,440 and we're going to remove all these 33385 24:52:06,960 --> 24:52:11,512 resurrection we were doing here from the 33386 24:52:09,440 --> 24:52:14,000 last lecture because this is no longer 33387 24:52:14,000 --> 24:52:18,960 and what we really want is to look at 33388 24:52:16,320 --> 24:52:21,120 our default constructor which is really 33389 24:52:18,960 --> 24:52:23,192 in here already let's look at the 33390 24:52:21,119 --> 24:52:26,319 implementation all we really need to do 33391 24:52:23,191 --> 24:52:27,759 is put in sddc out and say default 33392 24:52:31,039 --> 24:52:36,399 and we're going to hop over to the civil 33393 24:52:33,759 --> 24:52:38,959 engineer class and see if we have a 33394 24:52:36,399 --> 24:52:41,360 default constructor and we do have it 33395 24:52:38,960 --> 24:52:44,232 and we're going to go to the cpp file 33396 24:52:49,360 --> 24:52:54,720 so this is going to basically do what we 33397 24:52:52,000 --> 24:52:57,360 want and what we really want to see 33398 24:52:54,720 --> 24:52:59,680 is how or the order in which our 33399 24:52:57,360 --> 24:53:02,720 constructors are going to be called if 33400 24:52:59,679 --> 24:53:04,639 we build a civil engineer object as we 33401 24:53:02,720 --> 24:53:06,551 have seen here the person part is going 33402 24:53:04,639 --> 24:53:08,872 to be built first so this is going to 33403 24:53:06,551 --> 24:53:11,119 call the constructor from person and 33404 24:53:08,872 --> 24:53:13,600 because we are not doing any kind of 33405 24:53:11,119 --> 24:53:15,599 customization on which constructor is 33406 24:53:13,600 --> 24:53:17,440 going to be called the compiler is going 33407 24:53:15,600 --> 24:53:19,040 to call the default constructor by 33408 24:53:17,440 --> 24:53:21,191 default so we're going to build the 33409 24:53:19,039 --> 24:53:22,871 person port first and we're going to see 33410 24:53:21,191 --> 24:53:25,191 the default constructor for a person 33411 24:53:22,872 --> 24:53:27,760 called after that we're going to build 33412 24:53:27,759 --> 24:53:32,159 and this is going to call the default 33413 24:53:29,440 --> 24:53:34,639 constructor for engineer and after that 33414 24:53:32,160 --> 24:53:36,720 we're going to weld the civil engineer 33415 24:53:34,639 --> 24:53:39,512 part of us and that's going to call the 33416 24:53:36,720 --> 24:53:41,191 civil engineer default constructor this 33417 24:53:41,191 --> 24:53:47,119 if we build a civil engineer object so 33418 24:53:44,800 --> 24:53:49,040 we are going to hop over in the main cpp 33419 24:53:47,119 --> 24:53:51,831 file and actually build the civil 33420 24:53:49,039 --> 24:53:55,360 engineer object we're going to say civil 33421 24:53:51,831 --> 24:53:57,439 engineer and we're going to say ce1 we 33422 24:53:55,360 --> 24:54:00,232 can do that and we're going to see what 33423 24:53:57,440 --> 24:54:03,040 happens let's try and build this program 33424 24:54:00,232 --> 24:54:05,832 we're going to build that with gcc the 33425 24:54:03,039 --> 24:54:08,319 book is going to be good we could run it 33426 24:54:05,831 --> 24:54:10,000 let's bring up a powershell window we 33427 24:54:10,000 --> 24:54:14,639 we want to bring up a powershell window 33428 24:54:16,479 --> 24:54:20,000 and you're going to see that default 33429 24:54:17,831 --> 24:54:22,479 constructor for a person called the 33430 24:54:20,000 --> 24:54:24,800 first constructor for engineer called 33431 24:54:24,800 --> 24:54:30,080 engineer called this is what we want to 33432 24:54:27,759 --> 24:54:31,831 see and hopefully this proves that the 33433 24:54:30,080 --> 24:54:35,279 compiler is really going to build a 33434 24:54:31,831 --> 24:54:36,871 civil engineer object in parts the 33435 24:54:35,279 --> 24:54:38,959 compiler is going to see that it is a 33436 24:54:36,872 --> 24:54:41,680 derived object and it's going to see all 33437 24:54:38,960 --> 24:54:44,080 the classes that this is deriving from 33438 24:54:41,679 --> 24:54:46,399 so it is going to build the most base 33439 24:54:44,080 --> 24:54:48,720 part first after that it is going to 33440 24:54:46,399 --> 24:54:51,919 build other layers until it reaches the 33441 24:54:48,720 --> 24:54:55,040 level where we are at at this civil 33442 24:54:51,919 --> 24:54:57,512 engineer objects again if we put another 33443 24:54:55,039 --> 24:54:58,959 statement out here i'm going to say 33444 24:54:58,960 --> 24:55:04,400 and we're going to say sdd-endl 33445 24:55:01,919 --> 24:55:07,039 and i am doing this so that i just have 33446 24:55:04,399 --> 24:55:09,439 a point where i can put my breakpoint 33447 24:55:07,039 --> 24:55:12,159 some ides are not going to like it if 33448 24:55:09,440 --> 24:55:14,639 you put a breakpoint at an empty line so 33449 24:55:12,160 --> 24:55:16,552 that's why i am doing it here and i am 33450 24:55:14,639 --> 24:55:19,512 going to try and debug and really show 33451 24:55:16,551 --> 24:55:21,599 you that we have an engineer and a 33452 24:55:19,512 --> 24:55:23,512 person part in us i have shown you this 33453 24:55:21,600 --> 24:55:24,872 before but it doesn't really hurt to 33454 24:55:24,872 --> 24:55:30,232 because debugging is really cool it's 33455 24:55:27,679 --> 24:55:32,319 going to give you more information than 33456 24:55:32,320 --> 24:55:37,280 we're going to hit our break point here 33457 24:55:37,279 --> 24:55:42,160 locals we're going to have our civil 33458 24:55:39,279 --> 24:55:44,080 engineer object which is in scope now 33459 24:55:42,160 --> 24:55:47,512 and you see that we have an engineer 33460 24:55:44,080 --> 24:55:49,759 part of us and the engineer has a person 33461 24:55:47,512 --> 24:55:51,760 partner so what the compiler is really 33462 24:55:49,759 --> 24:55:54,720 going to do when it is building a civil 33463 24:55:51,759 --> 24:55:57,279 engineer object it is going to build the 33464 24:55:54,720 --> 24:55:59,512 most based part of us so it is going to 33465 24:55:57,279 --> 24:56:01,679 build a personal part after that it is 33466 24:55:59,512 --> 24:56:03,600 going to build an engineer layer on top 33467 24:56:01,679 --> 24:56:06,231 of that and then it is going to world 33468 24:56:03,600 --> 24:56:09,120 the servo engineer layer and that's what 33469 24:56:06,232 --> 24:56:11,512 we see in our output when we get to run 33470 24:56:09,119 --> 24:56:13,360 our program here if we go back we're 33471 24:56:11,512 --> 24:56:15,512 going to see that we still have our 33472 24:56:15,512 --> 24:56:19,279 let's bring up a terminal window we can 33473 24:56:17,759 --> 24:56:21,360 use to run this we're going to run 33474 24:56:19,279 --> 24:56:24,160 rooster and we're going to see the order 33475 24:56:21,360 --> 24:56:26,160 in which our constructors are called 33476 24:56:24,160 --> 24:56:28,480 this is really cool now that we have 33477 24:56:26,160 --> 24:56:30,400 just debugged this program you can take 33478 24:56:28,479 --> 24:56:32,959 out this hello line because it's really 33479 24:56:30,399 --> 24:56:35,360 useless and this is really what i wanted 33480 24:56:32,960 --> 24:56:38,639 you to see constructors are going to be 33481 24:56:35,360 --> 24:56:41,191 called in your inheritance hierarchy 33482 24:56:41,191 --> 24:56:45,679 going down to the most specialized one 33483 24:56:44,160 --> 24:56:47,680 so we're going to call the most best 33484 24:56:45,679 --> 24:56:49,919 constructor which is person and we're 33485 24:56:47,679 --> 24:56:52,479 going to keep going until we reach the 33486 24:56:49,919 --> 24:56:56,160 most specialized which is a civil 33487 24:56:52,479 --> 24:56:58,399 engineer here to build our object and 33488 24:56:56,160 --> 24:57:00,960 our object is really going to be build 33489 24:56:58,399 --> 24:57:03,191 up in layers this is the message i am 33490 24:57:00,960 --> 24:57:05,120 trying to convey here this is really all 33491 24:57:03,191 --> 24:57:07,512 i had to share in this lecture i hope 33492 24:57:05,119 --> 24:57:09,831 you found it interesting we are going to 33493 24:57:07,512 --> 24:57:11,832 stop here in this one the next one we're 33494 24:57:09,831 --> 24:57:15,279 going to see that we can actually 33495 24:57:11,831 --> 24:57:17,831 actively call a base class constructor 33496 24:57:15,279 --> 24:57:21,360 from our derived class and that's going 33497 24:57:17,831 --> 24:57:23,679 to add much more flexibility in how our 33498 24:57:21,360 --> 24:57:25,279 objects are built we are not going to 33499 24:57:23,679 --> 24:57:28,159 call the default constructor we're going 33500 24:57:25,279 --> 24:57:30,551 to be calling our own constructors and 33501 24:57:28,160 --> 24:57:34,320 have much more control on how our 33502 24:57:30,551 --> 24:57:36,551 derived objects are set up in the world 33503 24:57:34,320 --> 24:57:38,000 go ahead and finish up here and meet me 33504 24:57:38,000 --> 24:57:43,191 in this lecture we're going to see a way 33505 24:57:40,232 --> 24:57:44,552 we can instruct for custom constructors 33506 24:57:44,551 --> 24:57:49,039 when our derived objects are being built 33507 24:57:49,039 --> 24:57:54,639 in the last lecture we saw a figure like 33508 24:57:51,759 --> 24:57:57,119 this we had an inheritance hierarchy 33509 24:57:54,639 --> 24:57:59,831 that went from person to engineer and to 33510 24:57:57,119 --> 24:58:02,080 civil engineer and we saw that if we set 33511 24:57:59,831 --> 24:58:04,319 up a civil engineer object a personal 33512 24:58:02,080 --> 24:58:06,000 part is going to be built first an 33513 24:58:04,320 --> 24:58:08,000 engineer part is going to be built on 33514 24:58:06,000 --> 24:58:09,600 top of that and the civil engineer part 33515 24:58:09,600 --> 24:58:13,600 built and then we're going to have the 33516 24:58:11,191 --> 24:58:16,551 construction complete for our civil 33517 24:58:13,600 --> 24:58:18,872 engineer object but in the last lecture 33518 24:58:16,551 --> 24:58:21,279 we saw that this was calling the default 33519 24:58:18,872 --> 24:58:24,551 constructors and sometimes you don't 33520 24:58:21,279 --> 24:58:27,191 want this you want your objects built in 33521 24:58:24,551 --> 24:58:29,039 a specific way for example through a 33522 24:58:27,191 --> 24:58:30,959 bunch of parameters that you pass to 33523 24:58:29,039 --> 24:58:32,799 your constructor what if you want this 33524 24:58:30,960 --> 24:58:35,600 kind of setup this is what we are going 33525 24:58:32,800 --> 24:58:37,680 to learn about in this lecture and what 33526 24:58:35,600 --> 24:58:39,512 we want to end up with is something like 33527 24:58:37,679 --> 24:58:41,191 this we will be able to build a person 33528 24:58:39,512 --> 24:58:43,191 object by passing in a bunch of 33529 24:58:43,191 --> 24:58:47,679 the full name we can pass in the age i 33530 24:58:45,831 --> 24:58:50,231 think this is the age we can pass in an 33531 24:58:47,679 --> 24:58:52,079 address we can build an engineer object 33532 24:58:50,232 --> 24:58:54,480 by passing in this kind of information 33533 24:58:52,080 --> 24:58:56,800 we can pass in the name the age and the 33534 24:58:54,479 --> 24:58:59,759 address but we can also pass in the 33535 24:58:56,800 --> 24:59:02,639 contract count i think this is the new 33536 24:58:59,759 --> 24:59:04,871 member variable we had in our engineer 33537 24:59:02,639 --> 24:59:06,872 class but we're not going to stop there 33538 24:59:04,872 --> 24:59:08,872 we can also build the civil engineer 33539 24:59:06,872 --> 24:59:11,040 object and we can pass in the 33540 24:59:08,872 --> 24:59:13,279 information again a civil engineer is 33541 24:59:11,039 --> 24:59:15,119 also a person so we're going to be 33542 24:59:13,279 --> 24:59:16,872 passing the information that is going to 33543 24:59:15,119 --> 24:59:19,919 initialize the person part and that 33544 24:59:16,872 --> 24:59:22,720 happens to be the name the age and the 33545 24:59:19,919 --> 24:59:24,232 address but a civil engineer is also an 33546 24:59:22,720 --> 24:59:27,360 engineer so we're going to pass 33547 24:59:24,232 --> 24:59:28,960 information that portrays to the level 33548 24:59:27,360 --> 24:59:31,191 of an engineer and that's going to be 33549 24:59:28,960 --> 24:59:33,512 the contract count and we're going to 33550 24:59:31,191 --> 24:59:36,639 pass the information to initialize the 33551 24:59:33,512 --> 24:59:39,119 civil engineer part of this object and 33552 24:59:36,639 --> 24:59:41,119 that's going to be the road strength 33553 24:59:39,119 --> 24:59:42,639 and hopefully from this you can see that 33554 24:59:42,639 --> 24:59:47,440 to forward all this information to the 33555 24:59:44,960 --> 24:59:49,832 constructor where this information is 33556 24:59:47,440 --> 24:59:53,040 needed for example if we are building a 33557 24:59:49,831 --> 24:59:56,319 civil engineer object we want to forward 33558 24:59:53,039 --> 24:59:59,191 the name the age and the address to the 33559 24:59:56,320 --> 25:00:01,512 person part of us so we need to parse 33560 24:59:59,191 --> 25:00:06,231 this to our personal constructor in a 33561 25:00:01,512 --> 25:00:08,960 way we need to pass the 31 here to our 33562 25:00:06,232 --> 25:00:12,000 engineer constructor because this 33563 25:00:08,960 --> 25:00:14,720 information is going to be stored at our 33564 25:00:12,000 --> 25:00:18,000 engineer level and we need to store the 33565 25:00:14,720 --> 25:00:20,720 road strength information into the civil 33566 25:00:18,000 --> 25:00:23,039 engineer part of us because that's where 33567 25:00:20,720 --> 25:00:24,960 this member variable is declared 33568 25:00:23,039 --> 25:00:27,279 hopefully you can see the need to call 33569 25:00:24,960 --> 25:00:29,600 custom constructors which are not to 33570 25:00:27,279 --> 25:00:32,160 default these custom constructors are 33571 25:00:29,600 --> 25:00:35,279 going to allow us to forward information 33572 25:00:32,160 --> 25:00:37,919 when building our objects like we are 33573 25:00:35,279 --> 25:00:40,319 doing here in the main function so let's 33574 25:00:37,919 --> 25:00:42,232 see how we can do this some of you might 33575 25:00:40,320 --> 25:00:44,960 think uh-huh if i am building an 33576 25:00:42,232 --> 25:00:47,120 engineer object i can just grab the 33577 25:00:44,960 --> 25:00:49,600 information and pass that to my base 33578 25:00:47,119 --> 25:00:51,512 class and that is simple right so we can 33579 25:00:49,600 --> 25:00:54,480 grab all this information we can grab 33580 25:00:51,512 --> 25:00:57,191 the full name the age and the address 33581 25:00:54,479 --> 25:00:59,759 and pass that to our base class using 33582 25:00:57,191 --> 25:01:01,599 the syntaxes we have here if we are 33583 25:00:59,759 --> 25:01:03,599 doing public inheritance we're going to 33584 25:01:01,600 --> 25:01:06,400 have access to these guys anyway so we 33585 25:01:03,600 --> 25:01:09,279 can say full name equals full name mag 33586 25:01:06,399 --> 25:01:10,551 equals age and say m address equals 33587 25:01:11,919 --> 25:01:18,720 for the address is actually private to 33588 25:01:15,600 --> 25:01:21,120 the person class and we can't do 33589 25:01:18,720 --> 25:01:23,279 something like this if we try to access 33590 25:01:21,119 --> 25:01:25,679 the address member variable from the 33591 25:01:23,279 --> 25:01:27,600 base class and modify it we're going to 33592 25:01:25,679 --> 25:01:30,319 get a compiler error that says that the 33593 25:01:27,600 --> 25:01:33,279 address is actually private to the base 33594 25:01:30,320 --> 25:01:35,680 person class we can't do this so this is 33595 25:01:33,279 --> 25:01:38,479 not going to work okay some of you might 33596 25:01:35,679 --> 25:01:41,191 think uh-huh what if i try to use an 33597 25:01:38,479 --> 25:01:43,679 initializer list and do something like 33598 25:01:41,191 --> 25:01:46,479 this so i can say m full name and pass 33599 25:01:43,679 --> 25:01:49,039 in my information i can say mh and put 33600 25:01:46,479 --> 25:01:50,800 in my information i can say m address 33601 25:01:49,039 --> 25:01:52,231 and put in my information and then 33602 25:01:52,232 --> 25:01:57,279 the member variable that actually 33603 25:01:54,720 --> 25:02:00,000 belongs to the engineer class that we 33604 25:01:57,279 --> 25:02:01,679 are trying to build an object for here 33605 25:02:00,000 --> 25:02:04,320 and hopefully this is going to work 33606 25:02:01,679 --> 25:02:07,191 except the compiler is not also going to 33607 25:02:04,320 --> 25:02:10,400 accept this the reason is with 33608 25:02:07,191 --> 25:02:13,191 initializer lists you can't initialize a 33609 25:02:10,399 --> 25:02:15,439 member variable that doesn't belong to 33610 25:02:13,191 --> 25:02:17,831 the class that you are trying to build 33611 25:02:15,440 --> 25:02:20,800 an object for so for example here we are 33612 25:02:17,831 --> 25:02:23,439 trying to initialize m full name mh and 33613 25:02:20,800 --> 25:02:26,160 m address but these member variables 33614 25:02:23,440 --> 25:02:28,320 don't belong to the engineer class that 33615 25:02:26,160 --> 25:02:30,232 we are trying to build an object for so 33616 25:02:28,320 --> 25:02:32,552 we're going to get a compiler error here 33617 25:02:30,232 --> 25:02:34,480 this is not going to work it is not 33618 25:02:34,479 --> 25:02:40,159 to build or set up this member variables 33619 25:02:37,512 --> 25:02:42,160 so this is not going to work okay so 33620 25:02:40,160 --> 25:02:45,440 how are we supposed to forward 33621 25:02:42,160 --> 25:02:46,639 information from derived classes to base 33622 25:02:46,639 --> 25:02:51,512 not to worry here we can still use 33623 25:02:48,872 --> 25:02:54,232 initializer lists but we are going to 33624 25:02:51,512 --> 25:02:56,960 use constructors whose job is going to 33625 25:02:54,232 --> 25:02:59,360 be to initialize these member variables 33626 25:02:56,960 --> 25:03:01,280 so if we are building an engineer object 33627 25:02:59,360 --> 25:03:04,080 and we have information that really 33628 25:03:01,279 --> 25:03:05,919 belongs to the person part of us we're 33629 25:03:04,080 --> 25:03:08,720 going to call the person constructor to 33630 25:03:05,919 --> 25:03:10,639 actually use this information to build 33631 25:03:08,720 --> 25:03:13,119 the personal part of us and we're going 33632 25:03:10,639 --> 25:03:16,080 to be calling the person constructor in 33633 25:03:13,119 --> 25:03:18,479 our initializer list here and after we 33634 25:03:16,080 --> 25:03:20,800 do that we're going to initialize a 33635 25:03:18,479 --> 25:03:24,080 member variable that actually belongs to 33636 25:03:20,800 --> 25:03:25,919 us in our initializer list this is going 33637 25:03:24,080 --> 25:03:28,639 to forward information from derived 33638 25:03:25,919 --> 25:03:30,872 classes in this case the engineer is the 33639 25:03:28,639 --> 25:03:33,279 derived class and the information is 33640 25:03:30,872 --> 25:03:35,680 going to be forwarded to the person 33641 25:03:33,279 --> 25:03:37,600 constructor which is our base class this 33642 25:03:37,600 --> 25:03:41,192 okay now that you know this we're going 33643 25:03:39,119 --> 25:03:42,871 to head over to visual studio code and 33644 25:03:42,872 --> 25:03:47,680 here we are in our working folder the 33645 25:03:45,279 --> 25:03:50,319 current project is constructors with 33646 25:03:47,679 --> 25:03:52,159 inheritance we are going to grab the 33647 25:03:50,320 --> 25:03:54,480 code from the last lecture because 33648 25:03:52,160 --> 25:03:55,680 that's going to be our starting point 33649 25:03:59,831 --> 25:04:03,919 we are going to open this little guy in 33650 25:04:02,080 --> 25:04:06,320 visual studio code by dragging and 33651 25:04:03,919 --> 25:04:08,639 dropping here and this is going to give 33652 25:04:06,320 --> 25:04:11,040 us our starter code we're going to have 33653 25:04:08,639 --> 25:04:13,512 a person class an engineer class and a 33654 25:04:11,039 --> 25:04:15,511 civil engineer class and this is 33655 25:04:13,512 --> 25:04:18,639 basically going to set up the hierarchy 33656 25:04:15,512 --> 25:04:21,360 we have here and what we want to achieve 33657 25:04:18,639 --> 25:04:22,872 is to be able to call our own custom 33658 25:04:22,872 --> 25:04:27,512 when we build our object in layers 33659 25:04:25,512 --> 25:04:29,512 information is going to be coming from a 33660 25:04:29,512 --> 25:04:33,512 and we are going to initialize the 33661 25:04:31,600 --> 25:04:34,400 person part of us with relevant 33662 25:04:34,399 --> 25:04:38,639 we're going to initialize the engineer 33663 25:04:36,232 --> 25:04:40,480 part of us with relevant information and 33664 25:04:38,639 --> 25:04:42,960 we're going to initialize the civil 33665 25:04:40,479 --> 25:04:44,800 engineer part of us with relevant 33666 25:04:42,960 --> 25:04:47,760 information this is what we want to 33667 25:04:44,800 --> 25:04:50,160 achieve okay for that to work we need to 33668 25:04:47,759 --> 25:04:52,871 not call default constructors we want to 33669 25:04:50,160 --> 25:04:55,512 call actual constructors that are going 33670 25:04:52,872 --> 25:04:58,479 to grab the information and forward that 33671 25:04:55,512 --> 25:05:00,160 in our member variables for example in 33672 25:04:58,479 --> 25:05:01,919 person we have a constructor which is 33673 25:05:00,160 --> 25:05:03,192 going to take three pieces of 33674 25:05:03,191 --> 25:05:08,639 and it is going to pass this information 33675 25:05:05,600 --> 25:05:11,360 into our member variables we need to 33676 25:05:08,639 --> 25:05:14,831 apply a few changes here for example our 33677 25:05:11,360 --> 25:05:17,680 third parameter is passed as a cost 33678 25:05:14,831 --> 25:05:20,000 strength member type we are going to 33679 25:05:17,679 --> 25:05:22,319 change this to be std strength view 33680 25:05:20,000 --> 25:05:24,080 because this is going to save us 33681 25:05:24,080 --> 25:05:27,600 let's say string view here this is going 33682 25:05:27,600 --> 25:05:32,800 and this is going to forward information 33683 25:05:30,320 --> 25:05:36,640 in a way we want we're going to change 33684 25:05:32,800 --> 25:05:38,400 our third parameter to bhtd string view 33685 25:05:36,639 --> 25:05:41,360 let's say string view here we're going 33686 25:05:38,399 --> 25:05:44,551 to say std string view this is what we 33687 25:05:41,360 --> 25:05:46,960 have in our header we're going to be 33688 25:05:44,551 --> 25:05:48,551 having the same signature for our 33689 25:05:50,551 --> 25:05:57,440 we have m full name mh and we have m 33690 25:05:55,119 --> 25:05:59,679 address here let's try to build and see 33691 25:05:57,440 --> 25:06:02,400 if the error goes away sometimes visual 33692 25:05:59,679 --> 25:06:03,919 studio code is playing games on us 33693 25:06:02,399 --> 25:06:05,759 if we get a compiler error we're going 33694 25:06:03,919 --> 25:06:08,000 to try and fix that you see that it is 33695 25:06:05,759 --> 25:06:10,080 visual studio code playing with us the 33696 25:06:10,960 --> 25:06:15,440 we have our constructor for person it is 33697 25:06:13,191 --> 25:06:18,000 going to forward information using our 33698 25:06:15,440 --> 25:06:19,760 initializer list here we're going to 33699 25:06:19,759 --> 25:06:23,919 engineer class if we go there we're 33700 25:06:22,000 --> 25:06:26,000 going to see that we don't have any 33701 25:06:23,919 --> 25:06:29,279 constructor which is going to grab 33702 25:06:26,000 --> 25:06:31,919 information in any meaningful way and 33703 25:06:31,919 --> 25:06:37,039 so we are going to put in a constructor 33704 25:06:34,320 --> 25:06:40,400 that looks like the one we have in our 33705 25:06:37,039 --> 25:06:42,551 person class so we can just grab this i 33706 25:06:40,399 --> 25:06:44,231 like copy and pasting because that's 33707 25:06:44,232 --> 25:06:48,800 a few seconds so i am going to put my 33708 25:06:46,551 --> 25:06:50,551 constructor here only it is not going to 33709 25:06:48,800 --> 25:06:53,040 be called the person it is going to be 33710 25:06:50,551 --> 25:06:54,399 engineer and i am going to add a 33711 25:06:54,399 --> 25:06:59,599 member and this is going to be an 33712 25:06:56,551 --> 25:07:01,191 integer here to keep things simpler 33713 25:07:01,191 --> 25:07:06,551 implementation of this constructor in 33714 25:07:03,512 --> 25:07:08,160 our engineer class we can go down here 33715 25:07:06,551 --> 25:07:10,231 and put that in front of other 33716 25:07:08,160 --> 25:07:12,320 constructors let's look at the order we 33717 25:07:12,320 --> 25:07:17,600 header because i like to be consistent 33718 25:07:14,720 --> 25:07:19,680 the default constructor is first so 33719 25:07:17,600 --> 25:07:21,760 this constructor is going to be second 33720 25:07:19,679 --> 25:07:23,919 and we're going to say that it belongs 33721 25:07:23,919 --> 25:07:28,479 using a scope resolution operator here 33722 25:07:26,639 --> 25:07:30,232 and we are going to put in our 33723 25:07:30,232 --> 25:07:36,232 now what we want to do is to call 33724 25:07:33,831 --> 25:07:38,551 the constructor for person to initialize 33725 25:07:36,232 --> 25:07:40,000 a person part of us and then we're going 33726 25:07:38,551 --> 25:07:41,512 to grab the information from the 33727 25:07:40,000 --> 25:07:44,720 contract count and we're going to 33728 25:07:41,512 --> 25:07:45,919 forward that in our own member variable 33729 25:07:44,720 --> 25:07:47,360 the way we're going to achieve this 33730 25:07:45,919 --> 25:07:49,512 we're going to call the person 33731 25:07:47,360 --> 25:07:51,831 constructor and we're going to pass in 33732 25:07:49,512 --> 25:07:54,479 the full name we're going to pass the 33733 25:07:51,831 --> 25:07:56,871 age and we're going to pass the address 33734 25:07:54,479 --> 25:07:59,039 and then we're going to initialize our 33735 25:07:56,872 --> 25:08:00,872 own member variable which is contract 33736 25:08:00,872 --> 25:08:04,639 let's go to the header and look at that 33737 25:08:02,960 --> 25:08:06,232 we're going to see that our member 33738 25:08:04,639 --> 25:08:08,551 variable is contract count so we're 33739 25:08:06,232 --> 25:08:11,512 going to say contract count and we're 33740 25:08:08,551 --> 25:08:13,831 going to say contract count param we're 33741 25:08:11,512 --> 25:08:16,551 going to name this differently because 33742 25:08:13,831 --> 25:08:19,679 we don't want issues with naming here so 33743 25:08:16,551 --> 25:08:21,360 let's name our parameter here param 33744 25:08:19,679 --> 25:08:23,831 this is going to not confuse the 33745 25:08:21,360 --> 25:08:26,479 compiler and once we have this end we 33746 25:08:23,831 --> 25:08:28,080 can just put in the body which is going 33747 25:08:28,080 --> 25:08:32,800 initialization is now done in our 33748 25:08:32,800 --> 25:08:37,040 so if we do this we're going to see that 33749 25:08:34,960 --> 25:08:39,760 the compiler is going to be happy with 33750 25:08:37,039 --> 25:08:40,799 our thing here we're going to go down in 33751 25:08:43,279 --> 25:08:46,551 and we're going to grab the constructor 33752 25:08:45,039 --> 25:08:48,719 we have an engineer because we're going 33753 25:08:46,551 --> 25:08:51,191 to be using this as a starting point 33754 25:08:48,720 --> 25:08:52,800 we're going to copy it and go in our 33755 25:08:51,191 --> 25:08:54,080 civil engineer class we're going to see 33756 25:08:54,080 --> 25:08:58,831 constructor we're going to put this and 33757 25:08:56,639 --> 25:09:01,679 we're going to name this civil 33758 25:08:58,831 --> 25:09:03,831 engineer and we're going to add a 33759 25:09:03,831 --> 25:09:06,799 that we want to initialize so we're 33760 25:09:06,800 --> 25:09:10,639 std string view because we want to 33761 25:09:08,800 --> 25:09:13,600 forward string data we're going to say 33762 25:09:10,639 --> 25:09:16,960 std string view here and we're going to 33763 25:09:13,600 --> 25:09:18,800 say speciality and we are going to put 33764 25:09:18,800 --> 25:09:24,551 in our cpp file for a civil engineer 33765 25:09:22,479 --> 25:09:26,231 we're going to put this below our 33766 25:09:26,232 --> 25:09:31,512 let's grab this i lost the desk somehow 33767 25:09:31,512 --> 25:09:35,760 and we're going to paste down here 33768 25:09:34,000 --> 25:09:38,479 we're going to say that this constructor 33769 25:09:35,759 --> 25:09:40,831 belongs to the civil engineer class i'm 33770 25:09:38,479 --> 25:09:42,872 going to use our scope resolution 33771 25:09:40,831 --> 25:09:46,000 operator and after this we're going to 33772 25:09:42,872 --> 25:09:48,639 put our initializer list in civil 33773 25:09:46,000 --> 25:09:50,551 engineer we will call our engineer 33774 25:09:50,551 --> 25:09:56,000 and it is going to be taking four thanks 33775 25:09:53,512 --> 25:09:57,512 we're going to pass in the full name 33776 25:09:56,000 --> 25:09:59,600 we're going to pass in the age we're 33777 25:09:57,512 --> 25:10:01,832 going to pass in the address and we're 33778 25:09:59,600 --> 25:10:05,040 going to pass in the contract account 33779 25:10:01,831 --> 25:10:07,831 which we will get from our constructor 33780 25:10:05,039 --> 25:10:11,039 from civil engineer but we will also 33781 25:10:07,831 --> 25:10:12,639 initialize our specialty member 33782 25:10:11,039 --> 25:10:15,279 variables so we're going to say m 33783 25:10:12,639 --> 25:10:18,080 speciality and we're going to pass the 33784 25:10:18,080 --> 25:10:22,479 and this is going to initialize our then 33785 25:10:20,160 --> 25:10:24,720 we're going to put in an empty body and 33786 25:10:22,479 --> 25:10:28,959 our information is going to be properly 33787 25:10:24,720 --> 25:10:31,119 forwarded to our base classes so if we 33788 25:10:28,960 --> 25:10:32,960 build the civil engineer object proper 33789 25:10:31,119 --> 25:10:35,119 information is going to be forwarded to 33790 25:10:35,119 --> 25:10:40,639 and the proper information is going to 33791 25:10:37,039 --> 25:10:43,279 be forwarded to the person part of us 33792 25:10:40,639 --> 25:10:47,360 now that we have this in place we can 33793 25:10:43,279 --> 25:10:48,399 put in our htdc out statements to let us 33794 25:10:48,399 --> 25:10:53,439 which constructor was actually called to 33795 25:10:51,039 --> 25:10:56,319 build authen we're going to say that it 33796 25:10:53,440 --> 25:10:57,919 was our custom constructor that was 33797 25:10:56,320 --> 25:10:59,040 called let's say that here we're going 33798 25:10:59,039 --> 25:11:04,551 customer constructor called for civil 33799 25:11:01,279 --> 25:11:07,512 engineer we are going to go to our 33800 25:11:04,551 --> 25:11:11,039 engineer class and also say the same 33801 25:11:07,512 --> 25:11:13,512 thing let's go to this cpp file for that 33802 25:11:11,039 --> 25:11:15,360 and we're going to put in our htdc out 33803 25:11:13,512 --> 25:11:18,320 statement and we're going to say custom 33804 25:11:15,360 --> 25:11:21,600 constructor we can copy this to be 33805 25:11:18,320 --> 25:11:24,000 consistent with what we had in our 33806 25:11:21,600 --> 25:11:25,832 constructor default constructor i should 33807 25:11:24,000 --> 25:11:28,551 say and we're going to say custom 33808 25:11:25,831 --> 25:11:30,871 constructor called for our engineer 33809 25:11:28,551 --> 25:11:32,319 class we're going to go back to person 33810 25:11:30,872 --> 25:11:34,639 and we're going to go to the 33811 25:11:32,320 --> 25:11:36,720 implementation file and we're going to 33812 25:11:34,639 --> 25:11:38,720 copy the message from our default 33813 25:11:36,720 --> 25:11:41,680 constructor and we're going to put this 33814 25:11:38,720 --> 25:11:44,399 in and say that the custom constructor 33815 25:11:41,679 --> 25:11:46,871 was called for person here so now that 33816 25:11:44,399 --> 25:11:49,679 we have these guys in we can go to our 33817 25:11:46,872 --> 25:11:52,720 main cpp file and include the classes 33818 25:11:49,679 --> 25:11:55,119 they are included here we can try to 33819 25:11:52,720 --> 25:11:57,440 create a person object just like we saw 33820 25:11:55,119 --> 25:11:59,279 in the slides and we're going to try and 33821 25:11:57,440 --> 25:12:01,512 build this program and see which 33822 25:11:59,279 --> 25:12:03,039 constructor is called to set up this 33823 25:12:03,039 --> 25:12:08,719 this shouldn't be rocket science by now 33824 25:12:05,679 --> 25:12:10,551 we know that our custom constructor is 33825 25:12:08,720 --> 25:12:13,191 going to be called because we are 33826 25:12:10,551 --> 25:12:15,440 passing three parameters which should be 33827 25:12:13,191 --> 25:12:18,639 forwarded to our member variables 33828 25:12:15,440 --> 25:12:21,512 through this initializer last so let's 33829 25:12:18,639 --> 25:12:23,679 build with gcc and make sure this is 33830 25:12:21,512 --> 25:12:25,279 what we expect we're going to use our 33831 25:12:25,279 --> 25:12:30,551 the bolt is going to be good as you see 33832 25:12:27,440 --> 25:12:32,720 here and if we bring up our powershell 33833 25:12:30,551 --> 25:12:34,800 window and run rooster we're going to 33834 25:12:32,720 --> 25:12:37,600 see that our custom constructor is 33835 25:12:34,800 --> 25:12:40,080 called let's bring up the main cpp file 33836 25:12:37,600 --> 25:12:42,160 so that we can see our thing we are 33837 25:12:40,080 --> 25:12:44,400 passing this information here and we are 33838 25:12:42,160 --> 25:12:46,320 printing our person object and we're 33839 25:12:44,399 --> 25:12:49,759 going to see that the full name is jon 33840 25:12:46,320 --> 25:12:52,232 snow the age is 27 and the address is 33841 25:12:49,759 --> 25:12:54,639 winterfell called and all this gibberish 33842 25:12:52,232 --> 25:12:57,040 we have here our constructor is really 33843 25:12:54,639 --> 25:12:59,512 being called okay so this is working we 33844 25:12:57,039 --> 25:13:01,439 can also set up an engineer object and 33845 25:12:59,512 --> 25:13:04,720 see that the information is being 33846 25:13:01,440 --> 25:13:08,000 properly propagated to the person part 33847 25:13:04,720 --> 25:13:10,551 of our engineer object because engineer 33848 25:13:10,551 --> 25:13:15,039 we are passing the information for the 33849 25:13:12,479 --> 25:13:18,231 full name the age the address but we are 33850 25:13:15,039 --> 25:13:20,639 also passing in the contract count which 33851 25:13:18,232 --> 25:13:23,832 should be used to initialize the 33852 25:13:20,639 --> 25:13:26,720 engineer part of us so we are going to 33853 25:13:23,831 --> 25:13:28,231 run the task to build with gcc 33854 25:13:26,720 --> 25:13:31,040 the build is going to go through 33855 25:13:28,232 --> 25:13:33,192 successfully and we can bring up our 33856 25:13:31,039 --> 25:13:34,079 powershell window and run the program 33857 25:13:34,080 --> 25:13:38,320 we're going to see that we're going to 33858 25:13:36,000 --> 25:13:41,039 branch the information for person custom 33859 25:13:38,320 --> 25:13:43,600 constructor for person called but what 33860 25:13:41,039 --> 25:13:46,399 we really are interested in here 33861 25:13:43,600 --> 25:13:48,232 is seeing that the custom constructor 33862 25:13:48,232 --> 25:13:54,800 for building our engineer object and 33863 25:13:51,279 --> 25:13:57,119 then we call the engineer constructor 33864 25:13:54,800 --> 25:13:59,512 and again this falls in line with what 33865 25:13:57,119 --> 25:14:01,679 we saw in the last lecture the most 33866 25:13:59,512 --> 25:14:05,191 based constructor is called first and 33867 25:14:01,679 --> 25:14:07,191 then the must specialize constructor is 33868 25:14:05,191 --> 25:14:10,231 called last in this case we are just 33869 25:14:07,191 --> 25:14:13,599 setting up thanks in such a way that our 33870 25:14:10,232 --> 25:14:17,680 custom constructors are called and we do 33871 25:14:13,600 --> 25:14:21,440 that from our engineer class by calling 33872 25:14:17,679 --> 25:14:23,831 the base constructor explicitly 33873 25:14:21,440 --> 25:14:26,400 using the syntax that we have here in 33874 25:14:23,831 --> 25:14:28,639 our initializer list after we have the 33875 25:14:26,399 --> 25:14:31,511 base constructor called we are going to 33876 25:14:28,639 --> 25:14:33,191 initialize our own member variables and 33877 25:14:33,191 --> 25:14:37,759 and in this way the information for the 33878 25:14:35,600 --> 25:14:40,232 current class will be initialized 33879 25:14:37,759 --> 25:14:42,399 properly and this is what we see here i 33880 25:14:40,232 --> 25:14:44,480 think this is a little bit misleading 33881 25:14:42,399 --> 25:14:45,439 because we still have the information 33882 25:14:45,440 --> 25:14:50,872 the person objects that we bought 33883 25:14:47,911 --> 25:14:52,872 printed out here we can comment this out 33884 25:14:50,872 --> 25:14:55,279 and build again we're going to pass this 33885 25:14:52,872 --> 25:14:57,440 through gcc to build our program the 33886 25:14:55,279 --> 25:14:59,911 build is going to be good we can 33887 25:14:57,440 --> 25:15:01,512 clear and run rooster and we're going to 33888 25:14:59,911 --> 25:15:04,319 see that the custom constructor is 33889 25:15:01,512 --> 25:15:07,040 called for a person to set up the person 33890 25:15:04,320 --> 25:15:09,192 part of us and then another layer of 33891 25:15:07,039 --> 25:15:12,799 engineer is going to be set up in our 33892 25:15:09,191 --> 25:15:15,191 object to initialize the contract count 33893 25:15:12,800 --> 25:15:16,872 member variable part of us and we're 33894 25:15:15,191 --> 25:15:18,319 going to have the information here so if 33895 25:15:16,872 --> 25:15:20,232 we print the full name we're going to 33896 25:15:18,320 --> 25:15:22,552 get daniel gray the age is going to be 33897 25:15:20,232 --> 25:15:24,800 41. the address is going to be the 33898 25:15:22,551 --> 25:15:26,720 gibberish we expect here and the 33899 25:15:24,800 --> 25:15:29,279 contract count is going to be passed in 33900 25:15:26,720 --> 25:15:33,119 successfully and this is really cool we 33901 25:15:29,279 --> 25:15:36,160 can also comment this out and set up a 33902 25:15:33,119 --> 25:15:38,799 civil engineer object we can do that 33903 25:15:36,160 --> 25:15:40,552 okay so let's go down and put this in 33904 25:15:38,800 --> 25:15:43,192 place we're going to create a civil 33905 25:15:40,551 --> 25:15:45,831 engineer object we can close up 33906 25:15:43,191 --> 25:15:47,679 the left side bar here to have some more 33907 25:15:45,831 --> 25:15:49,511 breathing room we're going to build a 33908 25:15:47,679 --> 25:15:52,079 civil engineer object we're going to 33909 25:15:49,512 --> 25:15:55,360 pass in full name age address contract 33910 25:15:52,080 --> 25:15:59,040 count and a speciality which i think we 33911 25:15:55,360 --> 25:16:00,399 expect from our civil engineer custom 33912 25:16:00,399 --> 25:16:04,479 that's going to be an std strength 33913 25:16:02,551 --> 25:16:06,551 that's what we are passing in here i 33914 25:16:04,479 --> 25:16:09,759 should say that's going to be 33915 25:16:06,551 --> 25:16:12,319 a string view but we can pass in string 33916 25:16:09,759 --> 25:16:13,679 data just like this and the string view 33917 25:16:13,679 --> 25:16:17,191 so what we're going to see here we're 33918 25:16:15,360 --> 25:16:19,600 going to see the person constructor 33919 25:16:17,191 --> 25:16:21,911 called the person custom constructor i 33920 25:16:19,600 --> 25:16:24,800 should say we're going to see the 33921 25:16:21,911 --> 25:16:26,959 engineer custom constructor called and 33922 25:16:26,960 --> 25:16:31,760 the civil engineer custom constructor 33923 25:16:29,440 --> 25:16:32,872 called to pass and forward all this 33924 25:16:32,872 --> 25:16:39,040 in related parts of our civil engineer 33925 25:16:36,479 --> 25:16:41,831 object let's build and see that this is 33926 25:16:39,039 --> 25:16:43,360 actually going to even compile 33927 25:16:41,831 --> 25:16:44,799 the book is going to be good we can 33928 25:16:44,800 --> 25:16:49,040 and run rooster and we're going to see 33929 25:16:46,551 --> 25:16:50,872 that this is exactly what we expect the 33930 25:16:49,039 --> 25:16:53,119 custom constructor for person is going 33931 25:16:53,119 --> 25:16:57,512 the custom constructor for engineer is 33932 25:16:54,960 --> 25:17:00,000 going to be called and then the custom 33933 25:16:57,512 --> 25:17:01,512 constructor for a civil engineer is 33934 25:17:01,512 --> 25:17:06,080 and this really falls in line with what 33935 25:17:03,679 --> 25:17:08,871 we expect if we are building a civil 33936 25:17:06,080 --> 25:17:11,911 engineer object and the civil engineer 33937 25:17:08,872 --> 25:17:15,279 class happens to be inheriting both from 33938 25:17:11,911 --> 25:17:19,119 engineer and from person we're going to 33939 25:17:15,279 --> 25:17:20,872 have the person part of us world first 33940 25:17:19,119 --> 25:17:23,119 and because of this we're going to see 33941 25:17:20,872 --> 25:17:25,760 the person constructor called but in 33942 25:17:23,119 --> 25:17:28,159 this lecture the rio focus is getting 33943 25:17:25,759 --> 25:17:30,159 our custom constructor called so the 33944 25:17:28,160 --> 25:17:32,400 person custom constructor is going to be 33945 25:17:30,160 --> 25:17:34,720 called after that we're going to call 33946 25:17:32,399 --> 25:17:37,119 the custom constructor for the engineer 33947 25:17:34,720 --> 25:17:40,232 part of us and that's going to set up 33948 25:17:37,119 --> 25:17:42,959 the engineer specific member variables 33949 25:17:40,232 --> 25:17:45,512 in our object after that we're going to 33950 25:17:42,960 --> 25:17:48,232 set up another layer of civil engineer 33951 25:17:45,512 --> 25:17:50,160 in our object and this is going to 33952 25:17:48,232 --> 25:17:52,480 initialize the member variable that 33953 25:17:50,160 --> 25:17:55,512 belongs to the civil engineer 33954 25:17:52,479 --> 25:17:58,399 part of us in our object so this is 33955 25:17:55,512 --> 25:18:00,639 really the main goal in this lecture 33956 25:17:58,399 --> 25:18:03,439 showing you that you can set up fans in 33957 25:18:00,639 --> 25:18:06,399 such a way that your custom constructors 33958 25:18:03,440 --> 25:18:07,680 are called when setting up your object 33959 25:18:07,679 --> 25:18:12,639 as you see here okay now that we have 33960 25:18:10,160 --> 25:18:14,552 this working i want you to see that 33961 25:18:12,639 --> 25:18:16,551 there are some bad ways to do these 33962 25:18:14,551 --> 25:18:19,599 things and some people are actually 33963 25:18:16,551 --> 25:18:22,551 going to try to set things up in that 33964 25:18:19,600 --> 25:18:24,800 way the first bad way is to try and 33965 25:18:22,551 --> 25:18:27,599 initialize our member variables in the 33966 25:18:24,800 --> 25:18:30,551 body of the class if you do things like 33967 25:18:27,600 --> 25:18:32,800 this again this is going to work because 33968 25:18:30,551 --> 25:18:35,512 full name and age are going to be 33969 25:18:32,800 --> 25:18:38,080 inherited publicly from the person class 33970 25:18:35,512 --> 25:18:40,320 so the code here is going to work but we 33971 25:18:38,080 --> 25:18:42,872 have no way to initialize the address 33972 25:18:40,320 --> 25:18:45,440 member variable because it is private to 33973 25:18:42,872 --> 25:18:47,440 the person class so if we try to access 33974 25:18:45,440 --> 25:18:49,832 that we're going to get a compiler error 33975 25:18:47,440 --> 25:18:52,479 let's try this out in code and see the 33976 25:18:49,831 --> 25:18:54,959 actual compiler error to try this out i 33977 25:18:52,479 --> 25:18:57,759 am going to comment out the good code 33978 25:18:54,960 --> 25:18:59,360 here and i am going to copy and put in 33979 25:18:57,759 --> 25:19:02,231 something that is going to be 33980 25:18:59,360 --> 25:19:04,479 problematic as we saw in the slides i am 33981 25:19:02,232 --> 25:19:07,192 going to paste in my constructor here i 33982 25:19:04,479 --> 25:19:09,119 should close my powershell window here 33983 25:19:07,191 --> 25:19:11,360 and i am going to take out my 33984 25:19:09,119 --> 25:19:13,440 initializer lust because we're not going 33985 25:19:13,440 --> 25:19:19,512 and we're going to just grab the data 33986 25:19:15,600 --> 25:19:21,192 and use that to initialize our data in 33987 25:19:19,512 --> 25:19:23,512 our member variable here so we're going 33988 25:19:21,191 --> 25:19:26,639 to grab the full name and say m full 33989 25:19:23,512 --> 25:19:29,680 name equals full name we can do that 33990 25:19:26,639 --> 25:19:31,360 we're going to say mh equals h that's 33991 25:19:29,679 --> 25:19:34,159 the information we get we're going to 33992 25:19:31,360 --> 25:19:37,119 say m address and you see that we don't 33993 25:19:34,160 --> 25:19:38,720 even have that in autocomplete so we're 33994 25:19:37,119 --> 25:19:41,512 going to pass the address we're going to 33995 25:19:38,720 --> 25:19:43,600 be stubborn and ignore these squiggly 33996 25:19:41,512 --> 25:19:45,760 lines and we're going to say m 33997 25:19:43,600 --> 25:19:49,192 speciality and we're going to grab the 33998 25:19:45,759 --> 25:19:51,911 speciality and pass that in but you see 33999 25:19:49,191 --> 25:19:52,871 that line 26 has a bunch of squiggly 34000 25:19:52,872 --> 25:19:57,832 and the reason is the m address member 34001 25:19:55,440 --> 25:20:00,872 variable is invisible and not 34002 25:19:57,831 --> 25:20:03,119 manipulable from a civil engineer class 34003 25:20:00,872 --> 25:20:05,680 again the reason is that the address 34004 25:20:03,119 --> 25:20:08,799 member variable is private to the person 34005 25:20:05,679 --> 25:20:12,639 class so the civil engineer class has no 34006 25:20:08,800 --> 25:20:15,512 business initializing this in the body 34007 25:20:12,639 --> 25:20:18,232 this is not going to work so if we try 34008 25:20:15,512 --> 25:20:20,800 and build this we're going to pass this 34009 25:20:18,232 --> 25:20:22,960 through gcc we are going to see that the 34010 25:20:20,800 --> 25:20:25,680 world is going to finish with errors and 34011 25:20:22,960 --> 25:20:28,639 if we go up we're going to probably see 34012 25:20:25,679 --> 25:20:31,039 the error person m address is private 34013 25:20:28,639 --> 25:20:33,600 within this context and we have no 34014 25:20:31,039 --> 25:20:37,360 business accessing that in the civil 34015 25:20:33,600 --> 25:20:39,512 engineer cpp file at line 26 this is 34016 25:20:37,360 --> 25:20:41,512 what we see here and this is the 34017 25:20:39,512 --> 25:20:44,080 offending line we shouldn't really be 34018 25:20:41,512 --> 25:20:46,000 doing this if we comment out this line 34019 25:20:44,080 --> 25:20:48,000 this is going to work but our member 34020 25:20:48,000 --> 25:20:54,000 with a junk value and this is really not 34021 25:20:51,119 --> 25:20:55,191 good because our address that we worked 34022 25:20:55,191 --> 25:21:00,319 to set up in our parameter here is just 34023 25:20:57,679 --> 25:21:02,159 going to be ignored so this is bad you 34024 25:21:00,320 --> 25:21:04,400 shouldn't really be initializing your 34025 25:21:02,160 --> 25:21:07,360 member variables like this especially if 34026 25:21:04,399 --> 25:21:09,279 you have an inheritance hierarchy in 34027 25:21:07,360 --> 25:21:11,831 your classes so we're going to comment 34028 25:21:09,279 --> 25:21:13,911 this out and say that this is really bad 34029 25:21:11,831 --> 25:21:15,831 it's going to give you compiler errors 34030 25:21:13,911 --> 25:21:18,232 and some of you are going to say okay 34031 25:21:15,831 --> 25:21:21,360 this is not working but what if we try 34032 25:21:18,232 --> 25:21:23,760 to use an initializer list directly 34033 25:21:21,360 --> 25:21:25,360 let's see if that actually works 34034 25:21:25,360 --> 25:21:30,720 set up another constructor here and we 34035 25:21:27,911 --> 25:21:33,119 are going to set up the data in our 34036 25:21:30,720 --> 25:21:34,831 initializer list we're going to say 34037 25:21:34,831 --> 25:21:39,911 name and pass in the full name we can do 34038 25:21:37,759 --> 25:21:42,000 that let's try this out we're going to 34039 25:21:42,000 --> 25:21:48,080 and we're going to pass age we can do 34040 25:21:44,479 --> 25:21:50,551 that and we're going to grab the address 34041 25:21:48,080 --> 25:21:52,160 and we're going to pass an address here 34042 25:21:50,551 --> 25:21:54,231 and after that we're going to say m 34043 25:21:52,160 --> 25:21:56,639 spatiality and we're going to pass in 34044 25:21:56,639 --> 25:22:00,232 what if we do something like this but 34045 25:21:59,119 --> 25:22:02,159 you're going to see that we have 34046 25:22:00,232 --> 25:22:05,120 squiggly lines here and the problem with 34047 25:22:02,160 --> 25:22:08,639 this way of doing things is that the 34048 25:22:05,119 --> 25:22:10,799 civil engineer class has no business 34049 25:22:10,800 --> 25:22:17,192 that belong to the person class in the 34050 25:22:13,759 --> 25:22:19,439 initializer list your initializer list 34051 25:22:17,191 --> 25:22:21,759 only has the capability to initialize 34052 25:22:19,440 --> 25:22:25,680 the member variables that belong to the 34053 25:22:21,759 --> 25:22:28,080 current class and m full name mh and m 34054 25:22:25,679 --> 25:22:30,399 address don't belong to the cipher 34055 25:22:28,080 --> 25:22:32,479 engineer class that's why we're going to 34056 25:22:30,399 --> 25:22:34,871 have a compiler error if we try to set 34057 25:22:32,479 --> 25:22:37,039 up things in this way if we're trying to 34058 25:22:34,872 --> 25:22:39,680 build the program we're going to get a 34059 25:22:39,679 --> 25:22:43,439 we're going to see that the build is 34060 25:22:41,039 --> 25:22:46,479 going to finish with errors and it is 34061 25:22:43,440 --> 25:22:49,360 going to say expected initializer before 34062 25:22:49,360 --> 25:22:54,232 and we need a column column here sorry 34063 25:22:51,759 --> 25:22:56,399 for this so this is not the error we 34064 25:22:54,232 --> 25:22:58,720 expect but you see that the moment we do 34065 25:22:56,399 --> 25:23:02,000 this we have a bunch of squiggly lines 34066 25:22:58,720 --> 25:23:04,320 on full name age and address and let's 34067 25:23:02,000 --> 25:23:06,399 build and see the problem we have and 34068 25:23:04,320 --> 25:23:08,800 we're going to build with errors as we 34069 25:23:08,800 --> 25:23:13,832 we are going to see our errors civil 34070 25:23:11,360 --> 25:23:16,160 engineer does not have any field called 34071 25:23:13,831 --> 25:23:18,319 m full name we're going to see that 34072 25:23:16,160 --> 25:23:20,800 civil engineer doesn't have any field 34073 25:23:18,320 --> 25:23:24,320 called mh and civil engineer doesn't 34074 25:23:20,800 --> 25:23:27,192 have any field named m full name or m 34075 25:23:24,320 --> 25:23:29,440 address here that's the problem we have 34076 25:23:27,191 --> 25:23:32,319 no business initializing these member 34077 25:23:29,440 --> 25:23:34,551 variables that don't belong to the civil 34078 25:23:32,320 --> 25:23:37,120 engineer class in the first place so 34079 25:23:34,551 --> 25:23:39,191 this is also not going to work the best 34080 25:23:37,119 --> 25:23:42,231 bet we have already is to call the best 34081 25:23:39,191 --> 25:23:44,720 constructor and let that initialize the 34082 25:23:42,232 --> 25:23:46,480 member variables that belong to our 34083 25:23:46,479 --> 25:23:52,159 in the inheritance hierarchy for example 34084 25:23:49,831 --> 25:23:54,479 in our civil engineer constructor here 34085 25:23:54,479 --> 25:23:59,440 constructor to initialize the engineer 34086 25:23:57,759 --> 25:24:01,439 part of us and we're going to be 34087 25:23:59,440 --> 25:24:03,360 forwarding the information the full name 34088 25:24:01,440 --> 25:24:06,232 the age the address and the contract 34089 25:24:03,360 --> 25:24:08,551 account the engineer class is in turn 34090 25:24:06,232 --> 25:24:10,720 going to call its base constructor to 34091 25:24:08,551 --> 25:24:13,119 forward the information about the full 34092 25:24:10,720 --> 25:24:14,872 name of the agent the address and it is 34093 25:24:13,119 --> 25:24:17,191 going to initialize its own member 34094 25:24:14,872 --> 25:24:19,440 variable m construct count and the 34095 25:24:17,191 --> 25:24:21,599 information is going to be passed in 34096 25:24:19,440 --> 25:24:23,119 correctly and if we try to build our 34097 25:24:21,600 --> 25:24:25,512 class now we're going to see that 34098 25:24:23,119 --> 25:24:27,512 everything is going to work and i really 34099 25:24:25,512 --> 25:24:29,360 wanted you to be aware of this because 34100 25:24:27,512 --> 25:24:31,040 the moment you understand this you're 34101 25:24:29,360 --> 25:24:33,119 going to set up your inheritance 34102 25:24:31,039 --> 25:24:34,719 hierarchies in a way that is going to 34103 25:24:33,119 --> 25:24:37,759 make sense and you're not going to be 34104 25:24:34,720 --> 25:24:39,911 getting all these weird compiler errors 34105 25:24:37,759 --> 25:24:41,831 this is really all we set out to do in 34106 25:24:39,911 --> 25:24:45,279 this lecture showing you how you can set 34107 25:24:41,831 --> 25:24:48,159 up your own custom constructors in your 34108 25:24:45,279 --> 25:24:49,911 own inheritance hierarchy and at the 34109 25:24:48,160 --> 25:24:52,960 moment you do that you're going to get 34110 25:24:49,911 --> 25:24:54,959 your classes called in the order that 34111 25:24:52,960 --> 25:24:57,120 you expect things are going to be set up 34112 25:24:54,960 --> 25:24:59,040 in layers the base constructors are 34113 25:24:57,119 --> 25:25:01,119 going to be called in a way that you 34114 25:24:59,039 --> 25:25:03,511 expect and your objects are going to be 34115 25:25:01,119 --> 25:25:06,080 built in a way that you can print them 34116 25:25:03,512 --> 25:25:08,800 and see the information that you expect 34117 25:25:06,080 --> 25:25:10,872 set up in your object if you want you 34118 25:25:08,800 --> 25:25:13,360 can also use a debugger to see the 34119 25:25:10,872 --> 25:25:15,440 information in your thumb so we're going 34120 25:25:13,360 --> 25:25:17,680 to set up a breakpoint here and we're 34121 25:25:17,679 --> 25:25:21,599 debugging tab we're going to hit the 34122 25:25:19,512 --> 25:25:24,232 green button to start debugging this 34123 25:25:21,600 --> 25:25:26,480 program the binary is going to be set up 34124 25:25:24,232 --> 25:25:29,360 once the vote is good the binary is 34125 25:25:26,479 --> 25:25:31,599 going to be passed in our debugger and 34126 25:25:29,360 --> 25:25:34,232 we are going to hit our breakpoint in a 34127 25:25:31,600 --> 25:25:37,360 minute let's wait for this so we have 34128 25:25:34,232 --> 25:25:40,400 hit the break point at line 20 and if we 34129 25:25:37,360 --> 25:25:42,800 expand our civil engineer object in our 34130 25:25:40,399 --> 25:25:46,000 locals we're going to see that we have 34131 25:25:42,800 --> 25:25:48,551 an engineer part of us we can expand 34132 25:25:46,000 --> 25:25:49,831 that and see that we also have a person 34133 25:25:49,831 --> 25:25:54,720 if we look at person we're going to see 34134 25:25:51,512 --> 25:25:56,720 that the full name is we can see john 34135 25:25:54,720 --> 25:26:00,639 travolta which is what we are passing 34136 25:25:56,720 --> 25:26:03,040 here we can also look at our age the age 34137 25:26:03,039 --> 25:26:07,599 and we're going to also have information 34138 25:26:07,600 --> 25:26:13,279 if we expand this we're going to see 34139 25:26:09,679 --> 25:26:16,399 that the address is this gibb brush that 34140 25:26:19,039 --> 25:26:23,191 number 89 we can see this information 34141 25:26:21,360 --> 25:26:26,399 here and you can really expand and see 34142 25:26:23,191 --> 25:26:28,319 all these things that make up our object 34143 25:26:26,399 --> 25:26:30,959 for example we can see the information 34144 25:26:28,320 --> 25:26:33,120 about the contract count which is 31 and 34145 25:26:30,960 --> 25:26:35,832 it is what we are passing when we build 34146 25:26:33,119 --> 25:26:40,000 this object here and the speciality 34147 25:26:35,831 --> 25:26:42,000 happens to be road strength as we see in 34148 25:26:40,000 --> 25:26:44,639 our locals here you can really see all 34149 25:26:42,000 --> 25:26:46,551 these things we are going to stop here 34150 25:26:44,639 --> 25:26:49,360 in this lecture in the next one we're 34151 25:26:46,551 --> 25:26:53,119 going to explore how copy constructors 34152 25:26:49,360 --> 25:26:54,872 work in our inheritance hierarchy so go 34153 25:26:56,399 --> 25:27:01,439 in this lecture we want to explore how 34154 25:27:01,440 --> 25:27:07,040 with our inheritance hierarchies and we 34155 25:27:04,960 --> 25:27:09,440 will still be using the same example we 34156 25:27:07,039 --> 25:27:12,159 have been using in this chapter we will 34157 25:27:09,440 --> 25:27:14,639 have this inheritance hierarchy and we 34158 25:27:12,160 --> 25:27:17,040 will still be using the same example we 34159 25:27:14,639 --> 25:27:18,720 have been using in this chapter the base 34160 25:27:17,039 --> 25:27:21,119 class is going to be person we're going 34161 25:27:18,720 --> 25:27:24,232 to be inheriting from that publicly to 34162 25:27:21,119 --> 25:27:26,319 create an engineer class and we will be 34163 25:27:24,232 --> 25:27:28,800 inheriting from the engineer class 34164 25:27:26,320 --> 25:27:31,040 publicly to create the civil engineer 34165 25:27:28,800 --> 25:27:34,000 class and with this we want to be able 34166 25:27:31,039 --> 25:27:36,399 to copy construct objects and the code 34167 25:27:34,000 --> 25:27:38,872 we want to be running is something like 34168 25:27:36,399 --> 25:27:41,439 this we have an engineer object we want 34169 25:27:38,872 --> 25:27:44,160 to be able to create another object from 34170 25:27:41,440 --> 25:27:46,551 this engineer object using the syntax 34171 25:27:44,160 --> 25:27:48,872 here and from what we already know this 34172 25:27:46,551 --> 25:27:50,959 is going to call our copy constructor 34173 25:27:48,872 --> 25:27:53,120 the funny thing is this is going to work 34174 25:27:50,960 --> 25:27:55,360 by default even if you don't put in your 34175 25:27:53,119 --> 25:27:57,360 copy constructor the reason is the 34176 25:27:55,360 --> 25:27:59,512 compiler is going to insert a copy 34177 25:27:57,360 --> 25:28:01,831 constructor for you and things are going 34178 25:27:59,512 --> 25:28:05,832 to be copied in a way that you expect 34179 25:28:01,831 --> 25:28:08,399 because none of our classes is using 34180 25:28:05,831 --> 25:28:10,399 dynamic memory allocation so everything 34181 25:28:08,399 --> 25:28:12,399 is just going to fall in place and it is 34182 25:28:10,399 --> 25:28:14,319 going to work but if you happen to need 34183 25:28:12,399 --> 25:28:15,911 to set up your own copy constructor 34184 25:28:14,320 --> 25:28:17,680 we're going to see how you could do that 34185 25:28:15,911 --> 25:28:19,279 in this lecture and we're going to start 34186 25:28:17,679 --> 25:28:21,831 by the base class we're going to set up 34187 25:28:19,279 --> 25:28:24,551 a copy constructor for the person class 34188 25:28:21,831 --> 25:28:26,720 and this is nothing complicated we just 34189 25:28:24,551 --> 25:28:28,872 pass our parameter by reference this is 34190 25:28:26,720 --> 25:28:31,279 going to be our source object and we 34191 25:28:28,872 --> 25:28:33,680 will be copying the data from our source 34192 25:28:31,279 --> 25:28:36,232 object to initialize the member 34193 25:28:33,679 --> 25:28:39,119 variables in the current object that we 34194 25:28:36,232 --> 25:28:40,872 are copying here again this is a copy 34195 25:28:39,119 --> 25:28:43,119 constructor which will be copy 34196 25:28:40,872 --> 25:28:46,160 constructing from the parameter that we 34197 25:28:43,119 --> 25:28:49,599 pass by reference now how do we do the 34198 25:28:46,160 --> 25:28:52,080 constructor for the engineer class which 34199 25:28:49,600 --> 25:28:54,160 happens to be inheriting from person try 34200 25:28:52,080 --> 25:28:56,551 to think about that one way to do things 34201 25:28:54,160 --> 25:28:59,512 is to set up things like this and this 34202 25:28:56,551 --> 25:29:03,440 will be just initializing the engineer 34203 25:28:59,512 --> 25:29:06,080 layer of our class but nothing is going 34204 25:29:03,440 --> 25:29:07,760 to be initializing the person part of us 34205 25:29:06,080 --> 25:29:10,320 and this is bad this is going to call 34206 25:29:07,759 --> 25:29:12,720 the default constructor to initialize 34207 25:29:10,320 --> 25:29:15,760 the person part of us and we will just 34208 25:29:12,720 --> 25:29:17,680 be left in with junk data that is 34209 25:29:15,759 --> 25:29:20,799 initializing the person object by 34210 25:29:17,679 --> 25:29:23,279 default and all the data we have in our 34211 25:29:20,800 --> 25:29:26,160 parameter engineer object is just going 34212 25:29:23,279 --> 25:29:28,479 to be lost for the person part of us so 34213 25:29:26,160 --> 25:29:30,960 we don't want to do this this is bad 34214 25:29:28,479 --> 25:29:33,279 design you shouldn't do this some of you 34215 25:29:30,960 --> 25:29:35,192 are going to say but what if we call the 34216 25:29:33,279 --> 25:29:38,319 best constructor we have seen that we 34217 25:29:35,191 --> 25:29:40,871 could do that right but remember this is 34218 25:29:38,320 --> 25:29:42,640 a copy constructor what we have on the 34219 25:29:45,440 --> 25:29:49,832 if you set up a syntax like this with 34220 25:29:47,679 --> 25:29:52,159 the copy constructor the compiler is 34221 25:29:49,831 --> 25:29:55,119 going to set up a temporary person 34222 25:29:52,160 --> 25:29:57,911 object and a copy from that object to 34223 25:29:55,119 --> 25:30:00,319 initialize the person part of us so this 34224 25:29:57,911 --> 25:30:02,959 is going to be creating a temporary copy 34225 25:30:00,320 --> 25:30:06,400 object containing the same data we have 34226 25:30:02,960 --> 25:30:08,800 in our engineer object so to some extent 34227 25:30:06,399 --> 25:30:11,679 this is going to work but the problem we 34228 25:30:08,800 --> 25:30:14,400 still have is that we are using the m 34229 25:30:11,679 --> 25:30:16,871 address member variable directly from an 34230 25:30:14,399 --> 25:30:19,911 engineer object and from what we already 34231 25:30:16,872 --> 25:30:22,320 know this m address member variable is 34232 25:30:19,911 --> 25:30:24,639 private to the person class and we have 34233 25:30:24,639 --> 25:30:28,551 from an engineer construct so this is 34234 25:30:26,720 --> 25:30:31,040 going to give us a compiler error but 34235 25:30:28,551 --> 25:30:34,080 you know that it is possible to set up a 34236 25:30:31,039 --> 25:30:36,871 getter in the engineer class that is 34237 25:30:34,080 --> 25:30:38,960 going to return this address and you can 34238 25:30:36,872 --> 25:30:41,360 call that together here and get this 34239 25:30:38,960 --> 25:30:43,512 thing to initialize your data this is 34240 25:30:41,360 --> 25:30:45,600 going to work but this is really weird 34241 25:30:43,512 --> 25:30:47,279 you shouldn't do this this is bad design 34242 25:30:45,600 --> 25:30:49,360 there is a better way i am going to show 34243 25:30:47,279 --> 25:30:51,759 you but before we look at that wave 34244 25:30:49,360 --> 25:30:54,479 let's try and summarize the problems we 34245 25:30:51,759 --> 25:30:56,159 have with this way of doing things this 34246 25:30:54,479 --> 25:30:59,119 is not going to be reusing the copy 34247 25:30:56,160 --> 25:31:01,832 constructor we have worked hard to set 34248 25:30:59,119 --> 25:31:04,551 up in person so this is just going to be 34249 25:31:01,831 --> 25:31:07,279 called duplication another thing is that 34250 25:31:04,551 --> 25:31:10,080 the m address member variable is private 34251 25:31:07,279 --> 25:31:12,160 to person so if we try and use it like 34252 25:31:10,080 --> 25:31:15,360 this we're going to get a compiler error 34253 25:31:12,160 --> 25:31:17,680 again it is possible to set up a getter 34254 25:31:15,360 --> 25:31:20,160 that could return that and we could use 34255 25:31:17,679 --> 25:31:23,679 that getter in this place but this is 34256 25:31:20,160 --> 25:31:26,320 bad design we are really forced to set 34257 25:31:23,679 --> 25:31:28,319 up a getter that is possibly only used 34258 25:31:26,320 --> 25:31:30,480 in this place that's bad design we don't 34259 25:31:28,320 --> 25:31:32,232 want to do something like this another 34260 25:31:30,479 --> 25:31:35,191 thing is that this is going to create a 34261 25:31:32,232 --> 25:31:38,480 temporary copy of person from which we 34262 25:31:35,191 --> 25:31:40,799 will be copying to initialize the person 34263 25:31:38,479 --> 25:31:43,831 part of us we don't want to be making 34264 25:31:40,800 --> 25:31:46,000 that copy because we hate copies in c 34265 25:31:43,831 --> 25:31:48,319 plus plus code so the best way to do 34266 25:31:46,000 --> 25:31:51,039 this is to set up your copy constructor 34267 25:31:48,320 --> 25:31:54,480 in this way we are going to say person 34268 25:31:51,039 --> 25:31:56,799 and directly passing our engineer object 34269 25:31:54,479 --> 25:32:00,080 the compiler is smart enough to see that 34270 25:31:56,800 --> 25:32:03,040 we are passing an engineer object to 34271 25:32:00,080 --> 25:32:05,759 initialize a person object and what the 34272 25:32:03,039 --> 25:32:09,039 compiler is going to do is really smart 34273 25:32:05,759 --> 25:32:11,360 suppose our engineer object is made up 34274 25:32:09,039 --> 25:32:14,159 of two layers the inner layer is going 34275 25:32:11,360 --> 25:32:16,800 to be containing the information about 34276 25:32:14,160 --> 25:32:18,960 the person part of us and the outer 34277 25:32:16,800 --> 25:32:21,760 layer is going to be containing the 34278 25:32:18,960 --> 25:32:23,512 engineer part of us okay so this is the 34279 25:32:21,759 --> 25:32:25,039 setup we have so the compiler is going 34280 25:32:23,512 --> 25:32:27,512 to see that we are passing in an 34281 25:32:25,039 --> 25:32:29,679 engineer object to initialize a person 34282 25:32:27,512 --> 25:32:31,911 object and what it is going to do is 34283 25:32:29,679 --> 25:32:35,279 really smart it is going to strip off 34284 25:32:31,911 --> 25:32:37,119 all this engineer part if i can describe 34285 25:32:35,279 --> 25:32:39,279 it like this we're going to strip off 34286 25:32:39,279 --> 25:32:45,679 that we have in our engineer object and 34287 25:32:42,551 --> 25:32:48,639 we will be just left with the person 34288 25:32:45,679 --> 25:32:50,479 part that we passed then to initialize 34289 25:32:48,639 --> 25:32:52,872 our person object and this is really 34290 25:32:50,479 --> 25:32:55,191 smart and it is going to work so again 34291 25:32:52,872 --> 25:32:57,360 the syntax is what you see here 34292 25:32:55,191 --> 25:33:00,080 we say person we pass in our source 34293 25:32:57,360 --> 25:33:02,551 object and then we can initialize the 34294 25:33:00,080 --> 25:33:04,232 member variable that belong to the 34295 25:33:02,551 --> 25:33:06,479 engineer part of us we will be 34296 25:33:04,232 --> 25:33:08,000 initializing contract count and this is 34297 25:33:06,479 --> 25:33:09,512 going to work now that you know this 34298 25:33:08,000 --> 25:33:11,440 we're going to head over to visual 34299 25:33:09,512 --> 25:33:14,000 studio code and play with us a little 34300 25:33:11,440 --> 25:33:16,551 more here we are in our working folder 34301 25:33:14,000 --> 25:33:19,440 the current project is copy constructors 34302 25:33:16,551 --> 25:33:22,080 with inheritance we will be reusing the 34303 25:33:19,440 --> 25:33:24,160 code from the last lecture so we're 34304 25:33:22,080 --> 25:33:26,720 going to grab everything from that 34305 25:33:24,160 --> 25:33:30,080 except for the binary and we're going to 34306 25:33:26,720 --> 25:33:32,551 copy that and put that in our current 34307 25:33:30,080 --> 25:33:34,960 project let's do this and we are going 34308 25:33:32,551 --> 25:33:37,512 to open this bad boy in visual studio 34309 25:33:34,960 --> 25:33:40,160 code by dragging and dropping here and 34310 25:33:37,512 --> 25:33:42,720 we will have our starting point we will 34311 25:33:40,160 --> 25:33:46,080 have our person class the engineer class 34312 25:33:42,720 --> 25:33:49,191 and the civil engineer class and we will 34313 25:33:46,080 --> 25:33:51,279 just be doing public inheritance to make 34314 25:33:49,191 --> 25:33:54,080 things easier here so engineer is going 34315 25:33:51,279 --> 25:33:56,232 to publicly inherit from person and 34316 25:33:54,080 --> 25:33:58,872 civil engineer is going to publicly 34317 25:33:56,232 --> 25:34:00,960 inherit from engineer and this is 34318 25:33:58,872 --> 25:34:03,600 everything we had from the last lecture 34319 25:34:00,960 --> 25:34:06,639 nothing special but what we want is to 34320 25:34:03,600 --> 25:34:09,360 set up copy constructors in such a way 34321 25:34:06,639 --> 25:34:12,319 that we will be able to build objects 34322 25:34:09,360 --> 25:34:14,800 from others for example if we leave in 34323 25:34:12,320 --> 25:34:16,872 our engineer object i think this is what 34324 25:34:14,800 --> 25:34:19,832 we used in the slides we're going to 34325 25:34:19,831 --> 25:34:25,759 we want to be able to create a another 34326 25:34:23,119 --> 25:34:28,231 engineer let's say engineer two and 34327 25:34:25,759 --> 25:34:31,119 above that from engineer one the thing 34328 25:34:28,232 --> 25:34:32,960 that might be puzzling to some of you is 34329 25:34:31,119 --> 25:34:35,360 that this is going to work out of the 34330 25:34:32,960 --> 25:34:37,832 box even if we didn't really put in a 34331 25:34:35,360 --> 25:34:40,000 copy constructor but the thing is the 34332 25:34:37,831 --> 25:34:42,720 compiler is going to insert a copy 34333 25:34:40,000 --> 25:34:45,440 constructor for you but that constructor 34334 25:34:45,440 --> 25:34:50,232 copy so it is basically going to be 34335 25:34:47,600 --> 25:34:52,400 copying thanks memberwise it is going to 34336 25:34:50,232 --> 25:34:55,440 take the contract count from engineer 34337 25:34:52,399 --> 25:34:57,191 one and copy that into engineer two and 34338 25:34:55,440 --> 25:34:58,551 we are basically going to have the same 34339 25:34:58,551 --> 25:35:03,831 engineer one in engineer two let's try 34340 25:35:01,440 --> 25:35:06,400 and print engineer two can do that we're 34341 25:35:03,831 --> 25:35:09,511 going to say and two and we're going to 34342 25:35:06,399 --> 25:35:12,231 put this out and we are going to run the 34343 25:35:09,512 --> 25:35:14,000 task to build with gcc that's what we 34344 25:35:12,232 --> 25:35:16,552 want here and you're going to see that 34345 25:35:14,000 --> 25:35:19,039 it is going to work out of the box if we 34346 25:35:16,551 --> 25:35:21,440 bring up a powershell window and run 34347 25:35:19,039 --> 25:35:23,911 rooster we're going to see that we have 34348 25:35:21,440 --> 25:35:26,160 the data in here we have engineer full 34349 25:35:23,911 --> 25:35:28,479 name daniel gray that's the data we have 34350 25:35:26,160 --> 25:35:31,120 in engineer one but notice that we are 34351 25:35:28,479 --> 25:35:33,279 printing engineer two here the age is 34352 25:35:31,119 --> 25:35:35,831 going to be 41 and the address is going 34353 25:35:33,279 --> 25:35:37,831 to be exactly what we had in engineer 34354 25:35:35,831 --> 25:35:39,679 one and you're going to see that the 34355 25:35:37,831 --> 25:35:42,639 constructors are going to be called to 34356 25:35:39,679 --> 25:35:44,319 set up engineer 2 and the custom 34357 25:35:42,639 --> 25:35:46,160 constructor for a person is going to be 34358 25:35:44,320 --> 25:35:47,912 called the custom constructor for 34359 25:35:46,160 --> 25:35:49,192 engineer is going to be called and this 34360 25:35:49,191 --> 25:35:54,639 what we expect here but what if we want 34361 25:35:52,000 --> 25:35:57,119 to set up our own copy constructor and 34362 25:35:54,639 --> 25:35:59,759 the need to do that might arise 34363 25:35:57,119 --> 25:36:02,799 if we happen to be doing some dynamic 34364 25:35:59,759 --> 25:36:05,439 memory allocation in our classes suppose 34365 25:36:02,800 --> 25:36:07,680 maybe person class has some dynamic 34366 25:36:05,440 --> 25:36:09,832 memory allocation it has some pointer 34367 25:36:07,679 --> 25:36:12,399 member variable and in the personal 34368 25:36:09,831 --> 25:36:14,720 custom constructor we are doing some 34369 25:36:12,399 --> 25:36:16,799 dynamic memory allocation to initialize 34370 25:36:14,720 --> 25:36:19,119 that pointer we may be doing the same 34371 25:36:16,800 --> 25:36:21,512 thing at the engineer level or the civil 34372 25:36:19,119 --> 25:36:23,831 engineer level and that will force you 34373 25:36:21,512 --> 25:36:25,360 to set up your own copy constructor and 34374 25:36:23,831 --> 25:36:28,000 we're going to see how you could do that 34375 25:36:25,360 --> 25:36:29,831 in your classes we are going to start 34376 25:36:28,000 --> 25:36:32,872 with the person class we're going to go 34377 25:36:29,831 --> 25:36:35,599 on top and set up a copy constructor in 34378 25:36:32,872 --> 25:36:37,192 front of our destructor here we're going 34379 25:36:35,600 --> 25:36:40,080 to say person and we're going to say 34380 25:36:37,191 --> 25:36:42,159 const person reference source 34381 25:36:40,080 --> 25:36:44,080 this is the syntax you use to set up 34382 25:36:42,160 --> 25:36:46,872 your copy constructor you already know 34383 25:36:44,080 --> 25:36:48,639 this from a previous chapter and we're 34384 25:36:46,872 --> 25:36:51,760 going to put in the implementation for 34385 25:36:48,639 --> 25:36:54,872 this we're going to go in the cpp file 34386 25:36:51,759 --> 25:36:55,831 before the destructor or we should go 34387 25:36:55,831 --> 25:37:00,399 our custom constructor it doesn't really 34388 25:36:58,320 --> 25:37:02,800 matter where you put this and we're 34389 25:37:00,399 --> 25:37:05,191 going to put in our scope resolution 34390 25:37:02,800 --> 25:37:07,440 operator let's do person here and we 34391 25:37:05,191 --> 25:37:09,911 will just be copying the data from the 34392 25:37:07,440 --> 25:37:12,160 source person so we're going to put in 34393 25:37:09,911 --> 25:37:14,399 our initializer list and we're going to 34394 25:37:12,160 --> 25:37:16,160 say m full name we're going to grab the 34395 25:37:14,399 --> 25:37:18,720 name from the source person we're going 34396 25:37:16,160 --> 25:37:21,440 to say source m full name we're going to 34397 25:37:18,720 --> 25:37:23,911 initialize the age we're going to say mh 34398 25:37:21,440 --> 25:37:26,639 and we're going to grab the age from the 34399 25:37:26,639 --> 25:37:31,512 and we can go to the next line so that 34400 25:37:28,872 --> 25:37:34,160 we see the entire thing here and say m 34401 25:37:31,512 --> 25:37:36,800 address and we're going to say source 34402 25:37:34,160 --> 25:37:39,440 and say m address this is going to set 34403 25:37:36,800 --> 25:37:41,512 up our constructor and down below we can 34404 25:37:39,440 --> 25:37:43,600 put in our body this is going to work 34405 25:37:41,512 --> 25:37:46,000 but this is everything we have seen so 34406 25:37:43,600 --> 25:37:47,832 far this is nothing new the syntax to 34407 25:37:46,000 --> 25:37:50,639 set up your copy constructor you just 34408 25:37:47,831 --> 25:37:52,551 pass it by const reference if you don't 34409 25:37:50,639 --> 25:37:55,679 pass by reference here you're going to 34410 25:37:52,551 --> 25:37:56,639 get an endless chain of copy constructor 34411 25:37:56,639 --> 25:38:00,639 if this is confusing to you please go 34412 25:37:58,551 --> 25:38:03,440 back to the chapter on classes you're 34413 25:38:00,639 --> 25:38:05,679 going to see everything about this and 34414 25:38:03,440 --> 25:38:08,000 this is really all we need to do here to 34415 25:38:05,679 --> 25:38:10,479 have a copy constructor for a person now 34416 25:38:08,000 --> 25:38:13,191 let's do things at the engineer level 34417 25:38:10,479 --> 25:38:15,191 we're going to go to our engineer class 34418 25:38:13,191 --> 25:38:16,959 and we need to set up a copy constructor 34419 25:38:15,191 --> 25:38:19,440 the syntax is going to be the same we're 34420 25:38:16,960 --> 25:38:22,320 going to say engineer and we will be 34421 25:38:19,440 --> 25:38:24,400 constructing from a cost engineer object 34422 25:38:22,320 --> 25:38:27,440 we pass as a parameter here so we're 34423 25:38:24,399 --> 25:38:29,679 going to say cost engineer by reference 34424 25:38:27,440 --> 25:38:32,479 and say source and the problem is going 34425 25:38:29,679 --> 25:38:34,719 to be how we implement this thing we're 34426 25:38:32,479 --> 25:38:37,831 going to copy this and go in our 34427 25:38:34,720 --> 25:38:40,160 engineer cpp file we're going to put our 34428 25:38:37,831 --> 25:38:42,159 copy constructor after our customer 34429 25:38:40,160 --> 25:38:44,000 constructor and we're going to put in 34430 25:38:42,160 --> 25:38:46,000 our scope resolution operator we're 34431 25:38:44,000 --> 25:38:48,479 going to say engineer and we're going to 34432 25:38:46,000 --> 25:38:50,720 put in our body let's go below and put 34433 25:38:48,479 --> 25:38:53,440 that in if we do things like this this 34434 25:38:50,720 --> 25:38:55,911 is just going to initialize our engineer 34435 25:38:53,440 --> 25:38:58,479 with junk data we don't want that but 34436 25:38:55,911 --> 25:39:01,679 some of you might say uh-huh i don't 34437 25:38:58,479 --> 25:39:04,959 know how to call my base constructor but 34438 25:39:01,679 --> 25:39:07,359 at least i can initialize my contract 34439 25:39:04,960 --> 25:39:09,600 count okay let's go back and see if we 34440 25:39:07,360 --> 25:39:12,080 have this member variable here yes it is 34441 25:39:09,600 --> 25:39:14,552 called contract count we can say 34442 25:39:12,080 --> 25:39:17,119 contract count here and we are going to 34443 25:39:14,551 --> 25:39:19,279 initialize this with source contract 34444 25:39:17,119 --> 25:39:21,759 count this is going to work but this is 34445 25:39:19,279 --> 25:39:24,399 going to call the default constructor of 34446 25:39:21,759 --> 25:39:27,599 person to initialize the person part of 34447 25:39:24,399 --> 25:39:30,159 us and let's go back in our main cpp 34448 25:39:27,600 --> 25:39:32,232 file and see what we are using to try 34449 25:39:30,160 --> 25:39:34,872 things out we are building an engineer 34450 25:39:32,232 --> 25:39:37,512 object from another let's try this out 34451 25:39:34,872 --> 25:39:40,000 and see if our copy constructor is 34452 25:39:37,512 --> 25:39:42,800 called here so we're going to say custom 34453 25:39:40,000 --> 25:39:44,720 copy constructor called for engineer 34454 25:39:42,800 --> 25:39:46,551 we're going to put in the message and 34455 25:39:44,720 --> 25:39:49,119 we're going to say custom copy 34456 25:39:46,551 --> 25:39:51,759 constructor and we're going to build our 34457 25:39:49,119 --> 25:39:54,639 code let's build we're going to pass 34458 25:39:51,759 --> 25:39:57,039 this through gcc to see what happens the 34459 25:39:54,639 --> 25:39:58,872 build is going to be good and let's see 34460 25:39:57,039 --> 25:40:01,439 what happens we're going to clear and 34461 25:39:58,872 --> 25:40:03,192 run rooster we are going to see that the 34462 25:40:01,440 --> 25:40:05,119 constructors are going to be called to 34463 25:40:05,119 --> 25:40:10,000 but we see that the copy constructor for 34464 25:40:10,000 --> 25:40:14,080 and what i want to do to really make 34465 25:40:11,911 --> 25:40:16,319 this super clear is put a separator 34466 25:40:14,080 --> 25:40:18,160 between these two guys to really know 34467 25:40:16,320 --> 25:40:20,400 what is happening here i am going to put 34468 25:40:18,160 --> 25:40:22,552 in a bunch of dashes just like this i am 34469 25:40:20,399 --> 25:40:24,399 going to put in my new line character 34470 25:40:22,551 --> 25:40:26,319 and i am going to bolt again the bolt is 34471 25:40:24,399 --> 25:40:29,511 going to be good as you see here we can 34472 25:40:26,320 --> 25:40:31,680 clear and run rooster now we are going 34473 25:40:29,512 --> 25:40:34,320 to see that the default constructor for 34474 25:40:31,679 --> 25:40:37,511 person was called to set up the person 34475 25:40:34,320 --> 25:40:40,640 part of us in our engineer copy 34476 25:40:37,512 --> 25:40:43,600 constructor and the reason is we are not 34477 25:40:40,639 --> 25:40:46,399 doing anything to call the custom copy 34478 25:40:46,399 --> 25:40:50,871 and let's go back in person and make 34479 25:40:48,160 --> 25:40:52,400 sure we are printing proper information 34480 25:40:52,399 --> 25:40:57,279 when we get to do this right we are not 34481 25:40:54,639 --> 25:40:59,679 putting in any message here so we should 34482 25:40:57,279 --> 25:41:02,551 do that we are going to say custom 34483 25:40:59,679 --> 25:41:05,279 constructor or custom copy constructor 34484 25:41:02,551 --> 25:41:07,039 called for person but this is not called 34485 25:41:05,279 --> 25:41:08,959 you see that we are calling the default 34486 25:41:07,039 --> 25:41:12,079 constructor and this is going to 34487 25:41:08,960 --> 25:41:14,232 initialize our person part with junk 34488 25:41:12,080 --> 25:41:15,600 data if we print engineer two here 34489 25:41:14,232 --> 25:41:18,160 you're going to see that the full name 34490 25:41:15,600 --> 25:41:21,040 is none the edge is zero and the address 34491 25:41:18,160 --> 25:41:24,000 is none so we are basically getting the 34492 25:41:21,039 --> 25:41:25,911 default data we have in our person 34493 25:41:24,000 --> 25:41:27,191 object let's go back and really show you 34494 25:41:27,191 --> 25:41:31,279 default full name is going to be none 34495 25:41:29,440 --> 25:41:33,360 the default address is going to be none 34496 25:41:31,279 --> 25:41:36,399 of the edge is going to be 0 by default 34497 25:41:33,360 --> 25:41:38,872 and this is what we get because in our 34498 25:41:36,399 --> 25:41:42,159 copy constructor we're not doing 34499 25:41:38,872 --> 25:41:44,720 anything to get our customer constructor 34500 25:41:42,160 --> 25:41:46,720 in person to be called okay so this is 34501 25:41:44,720 --> 25:41:49,360 not going to work this is bad we don't 34502 25:41:46,720 --> 25:41:51,119 want to do something like this but let's 34503 25:41:49,360 --> 25:41:53,831 comment this out and show you another 34504 25:41:51,119 --> 25:41:56,319 way we might try to do things and we are 34505 25:41:53,831 --> 25:41:58,080 going to copy our copy constructor let's 34506 25:41:56,320 --> 25:42:00,232 go down and do this we're going to put 34507 25:41:58,080 --> 25:42:03,040 in another copy of this but we don't 34508 25:42:00,232 --> 25:42:04,800 want to just initialize our contract 34509 25:42:04,800 --> 25:42:09,120 instead we want to do something else 34510 25:42:07,119 --> 25:42:11,119 we're going to put a comma here and 34511 25:42:09,119 --> 25:42:13,119 we're going to say person some of you 34512 25:42:11,119 --> 25:42:16,000 might be thinking what if we call the 34513 25:42:13,119 --> 25:42:17,831 base constructor directly we can do 34514 25:42:16,000 --> 25:42:20,960 something like this we can say person 34515 25:42:17,831 --> 25:42:24,319 and we can say source m full name we can 34516 25:42:20,960 --> 25:42:27,120 do that we can say source mh and some of 34517 25:42:24,320 --> 25:42:29,912 you are going to say source m address 34518 25:42:27,119 --> 25:42:32,159 let's do that source m address and we're 34519 25:42:29,911 --> 25:42:35,360 going to build the person object and use 34520 25:42:32,160 --> 25:42:37,832 that to initialize the person part of us 34521 25:42:35,360 --> 25:42:40,720 we are basically going to be calling the 34522 25:42:37,831 --> 25:42:42,799 other person constructor that we have 34523 25:42:40,720 --> 25:42:44,872 which takes three parameters this 34524 25:42:42,800 --> 25:42:46,320 constructor here this is what we are 34525 25:42:46,320 --> 25:42:51,192 in our copy constructor but as we saw in 34526 25:42:49,512 --> 25:42:53,191 the slides this is going to have a bunch 34527 25:42:51,191 --> 25:42:55,759 of problems this is going to be creating 34528 25:42:53,191 --> 25:42:57,759 a temporary person object that we are 34529 25:42:55,759 --> 25:43:00,080 going to be copying from to set up the 34530 25:42:57,759 --> 25:43:01,599 person part of us but another problem we 34531 25:43:00,080 --> 25:43:03,831 have which is actually going to cause a 34532 25:43:01,600 --> 25:43:06,320 compiler error is that we don't have 34533 25:43:03,831 --> 25:43:08,551 access to the m address member variable 34534 25:43:06,320 --> 25:43:10,232 from an engineer constructor and this is 34535 25:43:08,551 --> 25:43:12,639 going to give us a compiler error let's 34536 25:43:10,232 --> 25:43:14,400 actually try and compile this program to 34537 25:43:12,639 --> 25:43:15,911 really show you this we are learning we 34538 25:43:15,911 --> 25:43:21,911 as many compiler errors as we can so the 34539 25:43:19,279 --> 25:43:24,399 world is finished with errors and if we 34540 25:43:21,911 --> 25:43:27,440 go up we're going to see that our error 34541 25:43:24,399 --> 25:43:30,000 is the m address member variable is 34542 25:43:27,440 --> 25:43:32,639 private to the person class and we have 34543 25:43:30,000 --> 25:43:35,831 no business accessing that in the 34544 25:43:32,639 --> 25:43:38,551 engineer cpp file at line 27 in our 34545 25:43:35,831 --> 25:43:41,439 constructor here and this is going to be 34546 25:43:38,551 --> 25:43:44,000 our offending line we can't do this one 34547 25:43:41,440 --> 25:43:46,000 could go in our person class and set up 34548 25:43:44,000 --> 25:43:48,960 a public getter method which is going to 34549 25:43:46,000 --> 25:43:50,872 be inherited by engineer and we could 34550 25:43:48,960 --> 25:43:53,120 call that to return the address let's 34551 25:43:50,872 --> 25:43:54,479 actually do that to show you that you 34552 25:43:54,479 --> 25:43:59,759 so we are going to go in and put in a 34553 25:43:58,000 --> 25:44:02,080 public method which is going to return 34554 25:43:59,759 --> 25:44:04,479 the address we actually have that here 34555 25:44:02,080 --> 25:44:06,800 so we can call that we can say 34556 25:44:04,479 --> 25:44:09,119 source get address this is going to work 34557 25:44:06,800 --> 25:44:11,600 this is going to set up our thumb 34558 25:44:09,119 --> 25:44:13,679 let's try to build because i think a 34559 25:44:11,600 --> 25:44:15,912 visual studio code is messing with us 34560 25:44:13,679 --> 25:44:18,159 here okay you see the both is good it is 34561 25:44:15,911 --> 25:44:20,800 visual studio code which wasn't 34562 25:44:18,160 --> 25:44:22,400 understanding our code here and if we 34563 25:44:20,800 --> 25:44:24,400 run our program we're going to see that 34564 25:44:22,399 --> 25:44:26,231 things are going to work exactly as we 34565 25:44:26,232 --> 25:44:30,320 run the program here we're going to 34566 25:44:27,759 --> 25:44:32,231 clear and run rooster we're going to see 34567 25:44:30,320 --> 25:44:34,960 that now the information is being 34568 25:44:32,232 --> 25:44:37,911 forwarded properly but we are setting up 34569 25:44:34,960 --> 25:44:39,440 a temporary person copy which we are 34570 25:44:39,440 --> 25:44:45,512 to then forward the data to the person 34571 25:44:42,080 --> 25:44:47,680 part of us this is not good design and 34572 25:44:45,512 --> 25:44:50,320 what we really want is the setup that is 34573 25:44:47,679 --> 25:44:53,119 going to allow us to call the copy 34574 25:44:50,320 --> 25:44:55,512 constructor that we worked hard to set 34575 25:44:53,119 --> 25:44:57,759 up in our base class here let's go back 34576 25:44:55,512 --> 25:44:59,600 to person and see that if we go up we're 34577 25:44:57,759 --> 25:45:01,759 going to see that we have a copy 34578 25:44:59,600 --> 25:45:04,160 constructor we really worked hard to set 34579 25:45:01,759 --> 25:45:06,639 up we wanted to reuse this in our 34580 25:45:04,160 --> 25:45:09,279 engineer class and that's what we're 34581 25:45:06,639 --> 25:45:11,279 going to see here so this is working but 34582 25:45:11,279 --> 25:45:16,551 and we should really try to reuse our 34583 25:45:14,000 --> 25:45:19,512 code as much as we can so what we're 34584 25:45:16,551 --> 25:45:21,599 going to do is set up a mechanism that 34585 25:45:19,512 --> 25:45:24,400 is going to reuse the copy constructor 34586 25:45:21,600 --> 25:45:26,960 we have in person and all we need to do 34587 25:45:24,399 --> 25:45:29,191 to get that to work we can say person 34588 25:45:26,960 --> 25:45:32,320 and then call the copy constructor of 34589 25:45:29,191 --> 25:45:35,039 person by directly passing in our source 34590 25:45:32,320 --> 25:45:37,440 object notice that we don't need to set 34591 25:45:35,039 --> 25:45:40,079 up a temporary person object we will 34592 25:45:37,440 --> 25:45:42,320 directly be copying from our engineer 34593 25:45:40,080 --> 25:45:45,440 object but this is going to do the 34594 25:45:42,320 --> 25:45:48,640 slicing we saw in the slides again if we 34595 25:45:45,440 --> 25:45:50,800 have two layers on our object the inner 34596 25:45:48,639 --> 25:45:54,232 layer is going to be the person part of 34597 25:45:50,800 --> 25:45:55,192 us and the outer green layer is going to 34598 25:45:57,512 --> 25:46:01,119 what the compiler is going to do the 34599 25:45:59,440 --> 25:46:02,960 compiler is going to see that we are 34600 25:46:03,679 --> 25:46:08,959 engineer object to initialize the person 34601 25:46:07,119 --> 25:46:11,440 part of us here and it is going to say 34602 25:46:08,960 --> 25:46:13,832 that we just need the red part we don't 34603 25:46:11,440 --> 25:46:16,232 need the green part here the compiler is 34604 25:46:13,831 --> 25:46:18,720 really smart so it is going to slice off 34605 25:46:21,831 --> 25:46:26,720 person part and it is going to use that 34606 25:46:24,160 --> 25:46:29,680 to initialize the person part of our 34607 25:46:26,720 --> 25:46:33,911 engineer object and this will be reusing 34608 25:46:29,679 --> 25:46:35,759 the copy constructor we have in our 34609 25:46:33,911 --> 25:46:38,800 person object then this is really cool 34610 25:46:35,759 --> 25:46:41,360 this is the optimal way to set up your 34611 25:46:38,800 --> 25:46:44,232 copical structures if you have an 34612 25:46:41,360 --> 25:46:46,000 inheritance hierarchy so let's try and 34613 25:46:44,232 --> 25:46:48,720 build the code again we're going to pass 34614 25:46:46,000 --> 25:46:50,479 this through gcc to see if this actually 34615 25:46:48,720 --> 25:46:52,720 works the bullet is going to be good we 34616 25:46:50,479 --> 25:46:54,551 can clear and run rooster we're going to 34617 25:46:52,720 --> 25:46:56,872 see that the copy constructor is going 34618 25:46:54,551 --> 25:46:59,440 to be called for person to set up the 34619 25:46:56,872 --> 25:47:01,279 person part of us and then the custom 34620 25:46:59,440 --> 25:47:03,600 copy constructor for engineer is going 34621 25:47:01,279 --> 25:47:06,319 to be called to set up the engineer part 34622 25:47:03,600 --> 25:47:08,480 of us and if we look at the information 34623 25:47:06,320 --> 25:47:10,232 we have in the main function you're 34624 25:47:08,479 --> 25:47:13,360 going to see that it is the same data 34625 25:47:10,232 --> 25:47:16,080 daniel gray for the full name ages 41 34626 25:47:13,360 --> 25:47:18,551 address is this gibberish here and the 34627 25:47:16,080 --> 25:47:21,279 contract count is 12 it is exactly what 34628 25:47:18,551 --> 25:47:23,679 we passed here and our copy constructor 34629 25:47:21,279 --> 25:47:27,039 is working now that we have this working 34630 25:47:23,679 --> 25:47:30,719 we can safely reuse the same ideas 34631 25:47:27,039 --> 25:47:33,679 in our civil engineer copy constructor 34632 25:47:30,720 --> 25:47:35,440 we can head over in our civil engineer 34633 25:47:33,679 --> 25:47:37,439 and set up the copy constructor so let's 34634 25:47:37,440 --> 25:47:43,191 close the left sidebar here so that we 34635 25:47:39,759 --> 25:47:46,159 see the entire thing we are going to say 34636 25:47:43,191 --> 25:47:47,911 civil engineer and we want to set up a 34637 25:47:46,160 --> 25:47:51,040 copy constructor so we're going to say 34638 25:47:47,911 --> 25:47:53,191 cast civil engineer here and we will be 34639 25:47:51,039 --> 25:47:55,911 passing this by reference we're going to 34640 25:47:53,191 --> 25:47:59,360 say source and we're going to put the 34641 25:47:55,911 --> 25:48:02,399 implementation of this in our cpp file 34642 25:47:59,360 --> 25:48:05,360 we can hop over there and go below our 34643 25:48:02,399 --> 25:48:06,720 custom constructor we can put in our 34644 25:48:05,360 --> 25:48:09,191 constructor here we're going to say 34645 25:48:06,720 --> 25:48:11,040 civil engineer and put in our scope 34646 25:48:09,191 --> 25:48:14,231 resolution operator and we're going to 34647 25:48:11,039 --> 25:48:16,159 go down and put in the body and in our 34648 25:48:14,232 --> 25:48:18,720 initializer list we're going to call the 34649 25:48:16,160 --> 25:48:21,760 copy constructor from engineer because 34650 25:48:18,720 --> 25:48:24,232 remember civil engineer is inheriting 34651 25:48:21,759 --> 25:48:27,039 from the engineer class so we are going 34652 25:48:24,232 --> 25:48:29,360 to call the engineer copy constructor 34653 25:48:27,039 --> 25:48:31,439 we're going to pass in source and again 34654 25:48:29,360 --> 25:48:35,039 this is going to slice off the civil 34655 25:48:31,440 --> 25:48:37,760 engineer port and only use the engineer 34656 25:48:35,039 --> 25:48:41,279 part of us to construct an engineer 34657 25:48:37,759 --> 25:48:44,159 object from this sliced of engineer 34658 25:48:41,279 --> 25:48:46,160 object we get from our copy constructor 34659 25:48:44,160 --> 25:48:49,760 here and this is really cool after that 34660 25:48:46,160 --> 25:48:52,872 we will initialize our contract count 34661 25:48:49,759 --> 25:48:55,279 member variable oh it is m speciality if 34662 25:48:52,872 --> 25:48:58,639 i remember correctly and we're going to 34663 25:48:55,279 --> 25:49:01,279 grab a source m speciality and we will 34664 25:48:58,639 --> 25:49:04,399 use that to initialize the civil 34665 25:49:01,279 --> 25:49:06,959 engineer part of us here let's go in and 34666 25:49:04,399 --> 25:49:09,191 say that the custom copy constructor is 34667 25:49:06,960 --> 25:49:11,600 being called here and we're going to say 34668 25:49:09,191 --> 25:49:14,319 custom copy constructor called for a 34669 25:49:11,600 --> 25:49:18,000 civil engineer what did we do here we 34670 25:49:14,320 --> 25:49:20,080 said engineer let's do engineer this is 34671 25:49:18,000 --> 25:49:23,360 what we want and we're going to say 34672 25:49:20,080 --> 25:49:25,911 engineer here properly sorry for this 34673 25:49:23,360 --> 25:49:28,232 but this is no big deal and now that we 34674 25:49:25,911 --> 25:49:30,959 have this in place we can actually build 34675 25:49:28,232 --> 25:49:32,800 civil engineer objects and copy 34676 25:49:32,800 --> 25:49:37,832 comment out the engineering stuff we 34677 25:49:34,800 --> 25:49:39,680 have and we can just put this in a black 34678 25:49:37,831 --> 25:49:42,479 comment and we're going to copy 34679 25:49:39,679 --> 25:49:44,551 construct civil engineers how cool is 34680 25:49:42,479 --> 25:49:46,872 that we're going to set up a civil 34681 25:49:46,872 --> 25:49:51,440 and this is going to be a civil engineer 34682 25:49:49,679 --> 25:49:53,039 and we're going to say daniel gray we're 34683 25:49:51,440 --> 25:49:55,440 going to put in all the data and we're 34684 25:49:53,039 --> 25:49:57,360 going to specify the speciality to be 34685 25:49:55,440 --> 25:49:59,279 road strength this is something i just 34686 25:49:59,279 --> 25:50:04,551 if this kind of speciality exists and we 34687 25:50:02,639 --> 25:50:07,831 will set up another civil engineer 34688 25:50:04,551 --> 25:50:10,319 object called n2 which we are going to 34689 25:50:07,831 --> 25:50:13,191 be copy constructing from and we will 34690 25:50:10,320 --> 25:50:15,680 see what happens when we copy construct 34691 25:50:13,191 --> 25:50:18,399 this thing here we should call our copy 34692 25:50:15,679 --> 25:50:21,439 constructor in civil engineer and the 34693 25:50:18,399 --> 25:50:23,279 things should propagate up all the way 34694 25:50:21,440 --> 25:50:25,760 to the person part of us and we're going 34695 25:50:23,279 --> 25:50:27,679 to see what happens let's build this 34696 25:50:25,759 --> 25:50:30,639 code we're going to pass this through 34697 25:50:27,679 --> 25:50:32,719 gcc as we always do the budget is going 34698 25:50:30,639 --> 25:50:35,039 to be good so this is good news we're 34699 25:50:32,720 --> 25:50:36,551 going to clear and run rooster and we're 34700 25:50:35,039 --> 25:50:38,719 going to see that the custom copy 34701 25:50:36,551 --> 25:50:40,399 constructor is called for person the 34702 25:50:38,720 --> 25:50:43,119 custom copy constructor is going to be 34703 25:50:40,399 --> 25:50:44,720 called for engineer and the custom copy 34704 25:50:43,119 --> 25:50:47,039 constructor is going to be called full 34705 25:50:44,720 --> 25:50:49,191 civil engineer and you see that things 34706 25:50:47,039 --> 25:50:51,439 are really propagating all the way to 34707 25:50:49,191 --> 25:50:53,679 the person part and we are calling and 34708 25:50:51,440 --> 25:50:55,360 reusing our copy constructors and this 34709 25:50:53,679 --> 25:50:57,119 is really cool if you go down you're 34710 25:50:55,360 --> 25:50:59,680 going to see that the information is 34711 25:50:57,119 --> 25:51:02,080 printed out correctly the full name is 34712 25:50:59,679 --> 25:51:05,119 daniel gray which we have in engineer 34713 25:51:02,080 --> 25:51:08,080 one the age is 41 the address is all 34714 25:51:05,119 --> 25:51:10,959 this gibberish the contract count is 12 34715 25:51:08,080 --> 25:51:12,080 and the speciality is road strength and 34716 25:51:12,080 --> 25:51:18,080 properly reusing our copy constructors 34717 25:51:15,191 --> 25:51:19,911 in our inheritance hierarchy this is 34718 25:51:18,080 --> 25:51:21,911 really all i had to share in this 34719 25:51:19,911 --> 25:51:24,720 lecture again the syntax you should be 34720 25:51:21,911 --> 25:51:26,959 using to set up your copy constructors 34721 25:51:24,720 --> 25:51:30,479 is what we see here this is going to be 34722 25:51:26,960 --> 25:51:33,360 reusing the code we have in our upper 34723 25:51:30,479 --> 25:51:35,759 classes in our hierarchy we will be 34724 25:51:33,360 --> 25:51:37,831 avoiding the temporary copies we saw 34725 25:51:35,759 --> 25:51:40,399 when we called the other constructor 34726 25:51:37,831 --> 25:51:43,360 directly and this is much cleaner it is 34727 25:51:40,399 --> 25:51:45,511 reusing code we have in other classes we 34728 25:51:43,360 --> 25:51:47,119 are going to stop here in this lecture 34729 25:51:45,512 --> 25:51:50,160 in the next one we're going to see that 34730 25:51:47,119 --> 25:51:52,871 it is actually possible to inherit base 34731 25:51:50,160 --> 25:51:55,120 constructors go ahead and finish up here 34732 25:51:52,872 --> 25:51:57,760 and meet me there in this lecture we're 34733 25:51:55,119 --> 25:52:00,871 going to explore the fact that we can 34734 25:51:57,759 --> 25:52:03,511 inherit base constructors and use them 34735 25:52:00,872 --> 25:52:05,360 in our derived classes we will build 34736 25:52:03,512 --> 25:52:07,440 we're using the same hierarchy we have 34737 25:52:05,360 --> 25:52:09,831 been using so we will have the person 34738 25:52:07,440 --> 25:52:12,479 class and we will be inheriting from 34739 25:52:09,831 --> 25:52:14,159 this person to create an engineer class 34740 25:52:12,479 --> 25:52:17,279 and that's what we're going to be using 34741 25:52:14,160 --> 25:52:20,160 to learn about this concept here so the 34742 25:52:17,279 --> 25:52:22,720 personal class is what we know from the 34743 25:52:20,160 --> 25:52:25,120 previous lectures we have a bunch of 34744 25:52:22,720 --> 25:52:27,680 member variables here we have the full 34745 25:52:25,119 --> 25:52:29,831 name the age and the address and we have 34746 25:52:27,679 --> 25:52:32,639 a bunch of constructors now we have a 34747 25:52:29,831 --> 25:52:35,439 default constructor a custom constructor 34748 25:52:32,639 --> 25:52:38,319 and a copy constructor that we set up 34749 25:52:35,440 --> 25:52:40,800 tirelessly in the last lecture now what 34750 25:52:38,320 --> 25:52:42,960 do we mean when we say that we can 34751 25:52:40,800 --> 25:52:45,279 inherit a constructor well based 34752 25:52:42,960 --> 25:52:48,160 constructors are not inherited by 34753 25:52:45,279 --> 25:52:51,512 default and what we mean by that is that 34754 25:52:48,160 --> 25:52:54,872 by default it is really not possible 34755 25:52:51,512 --> 25:52:58,000 to get derived classes to use base 34756 25:52:54,872 --> 25:53:00,232 constructors to set up things but it is 34757 25:52:58,000 --> 25:53:03,831 possible to tell the compiler to use the 34758 25:53:00,232 --> 25:53:06,800 base constructor to set up our own 34759 25:53:03,831 --> 25:53:08,799 objects for example we can put our 34760 25:53:08,800 --> 25:53:14,400 using person column column person this 34761 25:53:12,160 --> 25:53:17,600 is going to tell the compiler when 34762 25:53:14,399 --> 25:53:20,399 welding engineer objects don't use your 34763 25:53:17,600 --> 25:53:22,400 own constructor set up a base like a 34764 25:53:20,399 --> 25:53:25,759 constructor which is going to only 34765 25:53:22,399 --> 25:53:27,360 initialize the base member variables and 34766 25:53:25,759 --> 25:53:30,000 the compiler is going to see this 34767 25:53:27,360 --> 25:53:32,720 statement and it is going to generate a 34768 25:53:30,000 --> 25:53:35,039 constructor for you that looks something 34769 25:53:32,720 --> 25:53:36,720 like this so it is going to be engineer 34770 25:53:35,039 --> 25:53:39,360 it is going to be taking in the full 34771 25:53:36,720 --> 25:53:41,279 name the age and the address and it is 34772 25:53:39,360 --> 25:53:44,320 going to be forwarding the work to 34773 25:53:41,279 --> 25:53:46,720 initialize your object to the person 34774 25:53:44,320 --> 25:53:48,872 class and this is all this constructor 34775 25:53:46,720 --> 25:53:51,512 is going to do hopefully you can see 34776 25:53:48,872 --> 25:53:53,911 that this is called inheriting a base 34777 25:53:51,512 --> 25:53:57,191 constructor because all this constructor 34778 25:53:53,911 --> 25:54:00,000 is really doing is initializing the base 34779 25:53:57,191 --> 25:54:03,119 member variables it's not initializing 34780 25:54:00,000 --> 25:54:05,679 any member variable that belongs to the 34781 25:54:03,119 --> 25:54:07,360 engineer class so the engineer part of 34782 25:54:05,679 --> 25:54:10,479 us is going to be basically empty 34783 25:54:07,360 --> 25:54:12,960 containing junk data that we get by just 34784 25:54:10,479 --> 25:54:15,759 calling the default constructor and we 34785 25:54:12,960 --> 25:54:18,480 will only be initializing the data 34786 25:54:15,759 --> 25:54:20,959 belonging to the person part of us this 34787 25:54:18,479 --> 25:54:23,759 is really what i want you to see again 34788 25:54:20,960 --> 25:54:25,440 the syntax is what we see here 34789 25:54:23,759 --> 25:54:27,439 this is going to tell the compiler to 34790 25:54:25,440 --> 25:54:30,232 generate a constructor that is only 34791 25:54:27,440 --> 25:54:32,639 going to initialize the base member 34792 25:54:30,232 --> 25:54:36,232 variables but this constructor will be 34793 25:54:32,639 --> 25:54:37,191 callable and usable from the engineer 34794 25:54:37,191 --> 25:54:43,191 okay so this is really what we mean 34795 25:54:39,512 --> 25:54:45,119 by inheriting constructors one thing you 34796 25:54:43,191 --> 25:54:47,191 should know is that the constructor is 34797 25:54:45,119 --> 25:54:48,231 going to be inherited with whatever 34798 25:54:48,232 --> 25:54:54,080 specifier it had in the base class so if 34799 25:54:51,440 --> 25:54:56,000 our constructor was in the public scope 34800 25:54:54,080 --> 25:54:58,479 it's going to be inherited with public 34801 25:54:56,000 --> 25:55:01,279 access so we will be able to use it from 34802 25:54:58,479 --> 25:55:03,360 the outside and world engineer objects 34803 25:55:01,279 --> 25:55:05,759 if it was in a protected scope we want 34804 25:55:03,360 --> 25:55:08,080 to be able to use it from the outside if 34805 25:55:05,759 --> 25:55:10,159 it was private we won't even be able to 34806 25:55:08,080 --> 25:55:12,400 use it from a derived class you should 34807 25:55:10,160 --> 25:55:15,279 really know that so it is going to be 34808 25:55:12,399 --> 25:55:18,639 inherited with whatever access specifier 34809 25:55:15,279 --> 25:55:20,872 we had in our base class now that we 34810 25:55:18,639 --> 25:55:23,279 know this let's see some things you 34811 25:55:20,872 --> 25:55:25,760 should really be careful about the first 34812 25:55:23,279 --> 25:55:28,232 is that copy constructors are not 34813 25:55:25,759 --> 25:55:30,319 inheritable if you try to do that you're 34814 25:55:28,232 --> 25:55:32,400 going to get a compiler error but you 34815 25:55:30,320 --> 25:55:34,400 won't usually notice this because the 34816 25:55:32,399 --> 25:55:36,399 compiler is going to insert copy 34817 25:55:34,399 --> 25:55:38,159 constructors for you anyway this is 34818 25:55:36,399 --> 25:55:40,959 going to become a problem if you are 34819 25:55:38,160 --> 25:55:42,960 doing some dynamic memory allocation and 34820 25:55:40,960 --> 25:55:44,960 you're going to get pointers copied this 34821 25:55:42,960 --> 25:55:47,040 is really bad if you don't know about 34822 25:55:44,960 --> 25:55:49,360 this please go back to the chapter on 34823 25:55:47,039 --> 25:55:51,759 classes and you're going to see all the 34824 25:55:49,360 --> 25:55:53,911 story about shallow copying and deep 34825 25:55:51,759 --> 25:55:55,911 copying we talked about these things 34826 25:55:53,911 --> 25:55:58,160 another thing you should know is that 34827 25:55:55,911 --> 25:56:00,399 inherited constructors are really based 34828 25:55:58,160 --> 25:56:03,512 constructors and what we mean by this is 34829 25:56:00,399 --> 25:56:05,911 that they will only be initializing the 34830 25:56:03,512 --> 25:56:07,911 base member variables because they are 34831 25:56:05,911 --> 25:56:10,399 based constructors they have no 34832 25:56:07,911 --> 25:56:12,800 knowledge of derived member variables 34833 25:56:10,399 --> 25:56:14,959 that's why they only initialize base 34834 25:56:12,800 --> 25:56:17,279 member variables again as we said the 34835 25:56:14,960 --> 25:56:19,512 constructors are inherited with whatever 34836 25:56:17,279 --> 25:56:21,679 access specifier they had in the base 34837 25:56:19,512 --> 25:56:25,279 class and it is possible to set up your 34838 25:56:21,679 --> 25:56:27,191 own constructors on top of the inherited 34839 25:56:25,279 --> 25:56:28,872 constructors and you can use these 34840 25:56:27,191 --> 25:56:30,551 things another thing i should really 34841 25:56:28,872 --> 25:56:32,720 tell you is that inheritance 34842 25:56:30,551 --> 25:56:34,959 constructors is going to make your code 34843 25:56:32,720 --> 25:56:37,600 really confusing it is a technical 34844 25:56:34,960 --> 25:56:40,000 possibility but i strongly advise 34845 25:56:37,600 --> 25:56:41,912 against using this in your code because 34846 25:56:40,000 --> 25:56:43,600 it is just going to confuse people 34847 25:56:41,911 --> 25:56:45,599 people are going to see for example an 34848 25:56:43,600 --> 25:56:47,912 engineer constructor and they are going 34849 25:56:45,600 --> 25:56:50,552 to think that that constructor is going 34850 25:56:47,911 --> 25:56:52,872 to even initializing the engineer member 34851 25:56:50,551 --> 25:56:55,279 variables but it's not going to do that 34852 25:56:52,872 --> 25:56:57,600 because it is a base constructor it's 34853 25:56:55,279 --> 25:56:59,831 only going to be initializing the base 34854 25:56:57,600 --> 25:57:02,160 member variables and that may lead to 34855 25:56:59,831 --> 25:57:04,551 some confusion or even really bad 34856 25:57:02,160 --> 25:57:06,000 problems in your code so don't use this 34857 25:57:04,551 --> 25:57:08,551 it is just going to make things 34858 25:57:06,000 --> 25:57:11,279 confusing but i wanted you to be aware 34859 25:57:08,551 --> 25:57:14,231 of this and if you see this kind of 34860 25:57:11,279 --> 25:57:16,160 setup in classes out there you will know 34861 25:57:14,232 --> 25:57:18,720 what they mean this is just going to 34862 25:57:16,160 --> 25:57:20,872 inherit a base constructor and use it to 34863 25:57:18,720 --> 25:57:22,320 build objects now that we know this 34864 25:57:20,872 --> 25:57:23,911 we're going to head over to visual 34865 25:57:22,320 --> 25:57:26,960 studio code and play with us a little 34866 25:57:23,911 --> 25:57:30,232 more here we are in our working folder 34867 25:57:26,960 --> 25:57:33,120 the current project is inheriting based 34868 25:57:30,232 --> 25:57:35,440 constructors we are going to steal code 34869 25:57:33,119 --> 25:57:38,399 from the last lecture and we will be 34870 25:57:35,440 --> 25:57:40,800 using the main cpp file the person class 34871 25:57:38,399 --> 25:57:43,511 and the engineer class this is just 34872 25:57:40,800 --> 25:57:45,512 going to be enough for our purposes here 34873 25:57:43,512 --> 25:57:48,320 we're going to go back in our current 34874 25:57:45,512 --> 25:57:50,479 project and we're going to put this in 34875 25:57:48,320 --> 25:57:52,552 and we will open this in visual studio 34876 25:57:50,479 --> 25:57:54,551 code by dragging and dropping here this 34877 25:57:52,551 --> 25:57:56,800 is going to give us our thumb 34878 25:57:54,551 --> 25:57:59,512 and let's go to person and see that we 34879 25:57:56,800 --> 25:58:01,360 have our constructors and we have a 34880 25:57:59,512 --> 25:58:03,512 default constructor we have a custom 34881 25:58:01,360 --> 25:58:05,512 constructor and we have a copy 34882 25:58:03,512 --> 25:58:08,400 constructor one thing you should know is 34883 25:58:05,512 --> 25:58:10,479 that copy constructors are not inherited 34884 25:58:08,399 --> 25:58:12,871 so you will need to set up your copy 34885 25:58:10,479 --> 25:58:14,959 constructors explicitly if you want to 34886 25:58:12,872 --> 25:58:16,800 use them know that they want to be 34887 25:58:14,960 --> 25:58:18,872 coming from the upstream class you 34888 25:58:16,800 --> 25:58:21,600 should know that okay so this is the 34889 25:58:18,872 --> 25:58:23,600 message here now let's look at our 34890 25:58:21,600 --> 25:58:26,639 engineer class the engineer class is 34891 25:58:23,600 --> 25:58:30,000 publicly inheriting from person and it 34892 25:58:26,639 --> 25:58:32,479 has a bunch of its own constructors but 34893 25:58:30,000 --> 25:58:35,512 what we want to do is to take out all 34894 25:58:32,479 --> 25:58:38,639 these constructors and we're going to 34895 25:58:35,512 --> 25:58:40,320 take them out i think we can uh yeah 34896 25:58:38,639 --> 25:58:42,639 we're going to take them out let's do 34897 25:58:40,320 --> 25:58:44,800 that and we're going to go in our cpp 34898 25:58:42,639 --> 25:58:46,800 file and also take out all these 34899 25:58:44,800 --> 25:58:48,639 constructors here we're going to comment 34900 25:58:46,800 --> 25:58:50,720 them out i think we can take out all 34901 25:58:48,639 --> 25:58:53,039 these bad code we got from previous 34902 25:58:50,720 --> 25:58:55,600 lectures and we're going to just comment 34903 25:58:53,039 --> 25:58:58,719 out our constructors we're going to 34904 25:58:55,600 --> 25:59:01,832 close them all with a blog comment and 34905 25:58:58,720 --> 25:59:02,720 what we are going to do in our class is 34906 25:59:02,720 --> 25:59:07,360 using person person we're going to say 34907 25:59:04,639 --> 25:59:10,399 using person person this is going to 34908 25:59:07,360 --> 25:59:12,551 inherit the constructors from person and 34909 25:59:10,399 --> 25:59:14,639 now that we have this in place the 34910 25:59:12,551 --> 25:59:17,759 compiler is really going to generate a 34911 25:59:14,639 --> 25:59:19,759 base like constructor in our engineer 34912 25:59:17,759 --> 25:59:22,080 class in other words we're going to have 34913 25:59:19,759 --> 25:59:24,799 a default engineer constructor which is 34914 25:59:22,080 --> 25:59:27,040 just going to not initialize anything we 34915 25:59:24,800 --> 25:59:29,192 are going to have a custom constructor 34916 25:59:27,039 --> 25:59:31,039 which is going to take three parameters 34917 25:59:29,191 --> 25:59:33,039 but it is going to be an engineer 34918 25:59:31,039 --> 25:59:35,911 constructor and let's prove this we're 34919 25:59:33,039 --> 25:59:37,439 going to go in our main cpp file and 34920 25:59:35,911 --> 25:59:40,000 we're going to take out everything we 34921 25:59:37,440 --> 25:59:41,911 have here and we don't have a civil 34922 25:59:40,000 --> 25:59:44,080 engineer file so we shouldn't really 34923 25:59:41,911 --> 25:59:46,872 include that and we are going to create 34924 25:59:44,080 --> 25:59:48,872 an engineer object we can say and one 34925 25:59:46,872 --> 25:59:50,320 using the default constructor we can try 34926 25:59:48,872 --> 25:59:53,360 and print that out and you're going to 34927 25:59:50,320 --> 25:59:55,120 see that this code seems to work in 34928 25:59:53,360 --> 25:59:56,800 visual studio code we can try and build 34929 25:59:55,119 --> 26:00:00,000 this we're going to pass this through 34930 26:00:00,000 --> 26:00:04,320 set up a powershell window we can use to 34931 26:00:04,320 --> 26:00:08,160 and we're going to see that the default 34932 26:00:06,000 --> 26:00:10,232 constructor for person was called to 34933 26:00:10,232 --> 26:00:15,512 and the data is really junk data we have 34934 26:00:12,800 --> 26:00:18,639 in person so what the compiler is really 34935 26:00:15,512 --> 26:00:21,440 doing behind the scenes is to generate a 34936 26:00:18,639 --> 26:00:23,679 bezel-like constructor at the engineer 34937 26:00:21,440 --> 26:00:25,680 level and it is going to call the base 34938 26:00:23,679 --> 26:00:28,959 constructor to initialize the person 34939 26:00:25,679 --> 26:00:31,039 part of us so the data is just going to 34940 26:00:28,960 --> 26:00:32,480 be junk data we have in person and 34941 26:00:31,039 --> 26:00:34,551 you're going to see that the contract 34942 26:00:32,479 --> 26:00:36,639 count is going to be zero this is just a 34943 26:00:34,551 --> 26:00:38,800 default constructor which is just going 34944 26:00:36,639 --> 26:00:41,279 to grab whatever default data we have in 34945 26:00:38,800 --> 26:00:43,600 our object you see the contract count is 34946 26:00:41,279 --> 26:00:46,399 zero if we go back in person we're going 34947 26:00:43,600 --> 26:00:48,720 to see that the full name is none which 34948 26:00:46,399 --> 26:00:50,799 is what we see here the age is zero and 34949 26:00:48,720 --> 26:00:53,512 the address is none this is a default 34950 26:00:50,800 --> 26:00:55,360 constructor but we can even explicitly 34951 26:00:53,512 --> 26:00:57,279 pass in the data and that's going to 34952 26:00:55,360 --> 26:00:58,639 also work so we can pass in the full 34953 26:00:58,639 --> 26:01:03,600 daniel gray we can pass in the edge to 34954 26:01:00,872 --> 26:01:05,360 b23 for example and we can pass in a 34955 26:01:03,600 --> 26:01:07,440 junk address let's say something like 34956 26:01:05,360 --> 26:01:10,551 this and you see that this is going to 34957 26:01:07,440 --> 26:01:11,911 work even if we don't have any engineer 34958 26:01:11,911 --> 26:01:15,512 that takes three parameters let's go 34959 26:01:13,911 --> 26:01:18,800 back and you're going to see that all 34960 26:01:15,512 --> 26:01:20,639 our constructors are commented out but 34961 26:01:18,800 --> 26:01:23,360 this is working because we told the 34962 26:01:23,360 --> 26:01:30,080 a base like constructor for our engineer 34963 26:01:27,039 --> 26:01:33,191 so it is going to also have in 34964 26:01:30,080 --> 26:01:35,831 an engineer constructor that looks like 34965 26:01:33,191 --> 26:01:38,399 this constructor here which takes in 34966 26:01:35,831 --> 26:01:40,551 three parameters and this is really cool 34967 26:01:38,399 --> 26:01:42,079 and this is really crucial to understand 34968 26:01:40,551 --> 26:01:44,479 so again we are going to set up a 34969 26:01:42,080 --> 26:01:46,639 constructor for engineer so it is going 34970 26:01:44,479 --> 26:01:49,679 to be an engineer constructor but it is 34971 26:01:46,639 --> 26:01:51,759 going to be taking three parameters just 34972 26:01:49,679 --> 26:01:53,759 like the person constructor here and 34973 26:01:51,759 --> 26:01:55,831 this is the message i really want to 34974 26:01:53,759 --> 26:01:57,191 convey here so if we run this code 34975 26:01:55,831 --> 26:02:00,000 you're going to see that it is going to 34976 26:01:57,191 --> 26:02:02,231 work it is going to call our baseline 34977 26:02:00,000 --> 26:02:04,399 constructor for engineer and we're going 34978 26:02:02,232 --> 26:02:06,080 to have the data forwarded and but 34979 26:02:04,399 --> 26:02:08,231 you're going to see that the contract 34980 26:02:06,080 --> 26:02:10,400 count is not going to be taken into 34981 26:02:08,232 --> 26:02:13,040 account so it is going to still contain 34982 26:02:10,399 --> 26:02:15,119 some default data if we try and pass 34983 26:02:13,039 --> 26:02:16,871 that in we're going to see that we're 34984 26:02:15,119 --> 26:02:19,440 going to get a compiler error because 34985 26:02:16,872 --> 26:02:21,600 now we have no such constructor that 34986 26:02:19,440 --> 26:02:24,639 takes four parameters the only one we 34987 26:02:21,600 --> 26:02:26,872 have is one that is generated because of 34988 26:02:24,639 --> 26:02:29,759 this statement here and this is going to 34989 26:02:26,872 --> 26:02:32,639 be a baseline constructor which is just 34990 26:02:29,759 --> 26:02:34,871 going to take three parameters just like 34991 26:02:32,639 --> 26:02:37,831 this constructor here and another thing 34992 26:02:34,872 --> 26:02:39,680 i don't think i made clear enough is 34993 26:02:37,831 --> 26:02:41,831 that this is going to bring in all the 34994 26:02:39,679 --> 26:02:44,159 constructors you have notice that we 34995 26:02:41,831 --> 26:02:46,479 just put in this statement but it is 34996 26:02:44,160 --> 26:02:49,760 going to grab all these constructors we 34997 26:02:46,479 --> 26:02:52,399 have except for the copy constructors 34998 26:02:49,759 --> 26:02:54,399 which are not inherited in c plus plus 34999 26:02:52,399 --> 26:02:55,679 so you should really know this if we 35000 26:02:54,399 --> 26:02:58,000 build this code we're going to get a 35001 26:02:55,679 --> 26:02:59,279 compiler error because the compiler 35002 26:02:59,279 --> 26:03:04,080 what to do with the 45 parameter that we 35003 26:03:01,911 --> 26:03:05,911 have here but let's build and really 35004 26:03:04,080 --> 26:03:08,872 show you this you need to see this 35005 26:03:05,911 --> 26:03:11,360 compiler error with your own eyes let's 35006 26:03:08,872 --> 26:03:13,832 go in so the world is finished with 35007 26:03:11,360 --> 26:03:15,440 errors and we have an error saying no 35008 26:03:13,831 --> 26:03:18,231 matching function called to this 35009 26:03:15,440 --> 26:03:19,911 constructor which takes four parameters 35010 26:03:18,232 --> 26:03:21,911 if we take this out we're going to see 35011 26:03:19,911 --> 26:03:24,319 that this is going to work this is going 35012 26:03:21,911 --> 26:03:25,599 to use the compiler generated 35013 26:03:25,600 --> 26:03:30,552 as a result of this statement we have in 35014 26:03:28,232 --> 26:03:32,480 our code now if we will we're going to 35015 26:03:30,551 --> 26:03:34,720 see that this is going to work okay the 35016 26:03:32,479 --> 26:03:36,479 build is good can clear and run rooster 35017 26:03:34,720 --> 26:03:39,680 we're going to see that we have the full 35018 26:03:36,479 --> 26:03:41,911 name to be daniel gray we have the h2b23 35019 26:03:39,679 --> 26:03:44,799 we have our address which is really some 35020 26:03:41,911 --> 26:03:47,679 junk data and the contract count has no 35021 26:03:44,800 --> 26:03:50,320 data in it is just the default data we 35022 26:03:47,679 --> 26:03:53,039 have in our member variable here we have 35023 26:03:50,320 --> 26:03:55,680 no way to initialize this from a base 35024 26:03:53,039 --> 26:03:58,000 like a constructor but not everything is 35025 26:03:55,679 --> 26:04:00,079 really bad news if you don't want to be 35026 26:03:58,000 --> 26:04:02,639 limited by the constructors that are 35027 26:04:00,080 --> 26:04:04,872 generated by your compiler as a result 35028 26:04:02,639 --> 26:04:07,911 of inheritance you can set up your own 35029 26:04:04,872 --> 26:04:10,720 constructors for example we can uncover 35030 26:04:07,911 --> 26:04:12,959 our engineer constructor here and bring 35031 26:04:10,720 --> 26:04:15,600 it out of our comment we can do 35032 26:04:12,960 --> 26:04:18,720 something like this we can go in the 35033 26:04:15,600 --> 26:04:22,080 implementation file and also bring this 35034 26:04:18,720 --> 26:04:24,080 out of our comment section and now we 35035 26:04:22,080 --> 26:04:25,911 will be able to use this constructor 35036 26:04:24,080 --> 26:04:28,000 which is going to take the fourth 35037 26:04:25,911 --> 26:04:31,119 parameter here and do something with 35038 26:04:28,000 --> 26:04:32,960 that now if we go back in main the code 35039 26:04:31,119 --> 26:04:34,551 with three parameters is going to work 35040 26:04:32,960 --> 26:04:38,232 it is going to use the compiler 35041 26:04:34,551 --> 26:04:41,119 generated constructor but if we pass the 35042 26:04:38,232 --> 26:04:43,279 contract count to b4 this is also going 35043 26:04:41,119 --> 26:04:46,159 to work because it is going to be using 35044 26:04:43,279 --> 26:04:47,679 our own constructor here if we build the 35045 26:04:46,160 --> 26:04:50,320 code we're going to see that it is going 35046 26:04:47,679 --> 26:04:51,439 to work just fine let's use gcc to show 35047 26:04:51,440 --> 26:04:54,800 you're going to see that the world is 35048 26:04:56,399 --> 26:05:01,360 now our contract count is taken into 35049 26:04:59,119 --> 26:05:03,440 account this is really cool this is 35050 26:05:01,360 --> 26:05:06,080 really all i wanted you to see in this 35051 26:05:03,440 --> 26:05:09,119 lecture that you can let the compiler 35052 26:05:06,080 --> 26:05:12,080 generate the baseline constructor using 35053 26:05:09,119 --> 26:05:14,551 the syntax we have in our header here 35054 26:05:12,080 --> 26:05:17,360 this using statement can really show up 35055 26:05:14,551 --> 26:05:19,679 anywhere but it is going to inherit the 35056 26:05:17,360 --> 26:05:22,479 constructor with whatever access 35057 26:05:19,679 --> 26:05:24,719 specifier the constructor had in the 35058 26:05:22,479 --> 26:05:27,679 base class and you should really keep 35059 26:05:24,720 --> 26:05:29,191 these points in mind especially the last 35060 26:05:29,191 --> 26:05:34,231 inheriting constructors is going to make 35061 26:05:31,279 --> 26:05:36,800 your code confusing people are going to 35062 26:05:34,232 --> 26:05:38,400 notice that they can call constructors 35063 26:05:36,800 --> 26:05:41,192 that are really not going to be 35064 26:05:38,399 --> 26:05:43,511 initializing derived member variables 35065 26:05:41,191 --> 26:05:46,159 and this may cause serious problems in 35066 26:05:43,512 --> 26:05:48,232 your code we are going to stop here in 35067 26:05:46,160 --> 26:05:51,440 this lecture in the next one we're going 35068 26:05:48,232 --> 26:05:53,760 to explore how inheritance works with 35069 26:05:51,440 --> 26:05:56,080 these structures so far we have been 35070 26:05:53,759 --> 26:05:58,639 ignoring the structures we are going to 35071 26:05:56,080 --> 26:06:00,479 focus on them in the next lecture so go 35072 26:05:58,639 --> 26:06:02,232 ahead and finish up here and meet me 35073 26:06:00,479 --> 26:06:05,119 there in this lecture we're going to 35074 26:06:02,232 --> 26:06:07,911 focus on how destructors work with 35075 26:06:05,119 --> 26:06:10,639 inheritance and suppose we have an 35076 26:06:07,911 --> 26:06:13,119 inheritance hierarchy like we have been 35077 26:06:10,639 --> 26:06:15,191 using all along in this chapter so our 35078 26:06:13,119 --> 26:06:17,039 base class is person we have the 35079 26:06:15,191 --> 26:06:19,191 engineer class which is publicly 35080 26:06:17,039 --> 26:06:21,439 inheriting from person and we have a 35081 26:06:19,191 --> 26:06:25,119 civil engineer class which is inheriting 35082 26:06:21,440 --> 26:06:28,232 from engineer publicly so when we create 35083 26:06:25,119 --> 26:06:31,039 a civil engineer object we have seen 35084 26:06:28,232 --> 26:06:33,120 that the burst part is built first so we 35085 26:06:31,039 --> 26:06:35,511 are going to build the personal part of 35086 26:06:33,119 --> 26:06:37,679 us we are going to build the engineer 35087 26:06:35,512 --> 26:06:39,680 part of us on top of that and we are 35088 26:06:37,679 --> 26:06:42,551 going to build the civil engineer part 35089 26:06:39,679 --> 26:06:44,319 of us on top of that so the constructors 35090 26:06:42,551 --> 26:06:46,551 are going to be called in this order we 35091 26:06:44,320 --> 26:06:48,872 will call the person constructor first 35092 26:06:46,551 --> 26:06:51,440 engineer constructor after that and then 35093 26:06:48,872 --> 26:06:54,800 a civil engineer constructor after that 35094 26:06:51,440 --> 26:06:57,279 to set up our civil engineer object the 35095 26:06:54,800 --> 26:06:59,832 thing i want you to see in this lecture 35096 26:06:57,279 --> 26:07:02,479 is that the structures are called in the 35097 26:06:59,831 --> 26:07:04,639 reverse order to that so the most 35098 26:07:02,479 --> 26:07:07,440 specialized destructor is going to be 35099 26:07:04,639 --> 26:07:10,232 called first then we're going to destroy 35100 26:07:07,440 --> 26:07:13,040 the engineer part of us and the person 35101 26:07:10,232 --> 26:07:15,040 part of us is going to be destroyed last 35102 26:07:13,039 --> 26:07:17,679 so we are going to construct things in 35103 26:07:15,039 --> 26:07:20,231 this order from the base part to the 35104 26:07:17,679 --> 26:07:24,159 most specialized one and then we are 35105 26:07:20,232 --> 26:07:26,080 going to destroy thanks in this order so 35106 26:07:24,160 --> 26:07:28,639 these structures are going to be called 35107 26:07:28,639 --> 26:07:33,440 constructors that's the message i want 35108 26:07:31,279 --> 26:07:35,191 to convey here and we're going to set up 35109 26:07:33,440 --> 26:07:37,440 a simple example to play with us we're 35110 26:07:35,191 --> 26:07:39,119 going to create a civil engineer object 35111 26:07:37,440 --> 26:07:41,600 and we're going to see the order in 35112 26:07:39,119 --> 26:07:43,759 which these things are called and let's 35113 26:07:41,600 --> 26:07:46,400 head over to visual studio code and 35114 26:07:43,759 --> 26:07:48,959 actually do this here we are in our 35115 26:07:46,399 --> 26:07:51,679 working folder the current project is 35116 26:07:48,960 --> 26:07:54,080 inheritance and destructors we are going 35117 26:07:51,679 --> 26:07:56,639 to be reusing the code from a previous 35118 26:07:54,080 --> 26:07:58,720 lecture i don't think the last lecture 35119 26:07:56,639 --> 26:08:01,191 is a good example because we don't have 35120 26:07:58,720 --> 26:08:03,831 a civil engineer class so we are going 35121 26:08:01,191 --> 26:08:06,080 to go to the lecture before that and use 35122 26:08:03,831 --> 26:08:08,231 the code from our lecture or the copy 35123 26:08:06,080 --> 26:08:10,160 constructors with inheritance that's 35124 26:08:08,232 --> 26:08:12,800 going to give us all the classes we have 35125 26:08:10,160 --> 26:08:14,960 person engineer and civil engineer and 35126 26:08:12,800 --> 26:08:17,360 we need that in this lecture here so 35127 26:08:14,960 --> 26:08:20,232 let's grab all these things we're going 35128 26:08:17,360 --> 26:08:21,512 to copy them and put them in our current 35129 26:08:21,512 --> 26:08:26,551 which is inheritance and the structures 35130 26:08:24,399 --> 26:08:28,799 and we are going to open this in visual 35131 26:08:26,551 --> 26:08:31,119 studio code and it is going to be our 35132 26:08:28,800 --> 26:08:33,512 starting point here so we have person 35133 26:08:31,119 --> 26:08:36,959 engineer and civil engineer let's see 35134 26:08:33,512 --> 26:08:39,512 what we are doing and the main cpp file 35135 26:08:36,960 --> 26:08:42,480 we can take out all the junk here or we 35136 26:08:39,512 --> 26:08:44,232 can leave in the civil engineer object 35137 26:08:42,479 --> 26:08:45,512 we are creating here and we're going to 35138 26:08:45,512 --> 26:08:50,320 the constructors and the instructors are 35139 26:08:48,320 --> 26:08:52,872 called we're going to go to person and 35140 26:08:50,320 --> 26:08:56,080 make sure we have a constructor and we 35141 26:08:52,872 --> 26:08:58,960 have a bunch of constructors and 35142 26:08:56,080 --> 26:09:01,512 let's go to the cpp file and see that we 35143 26:08:58,960 --> 26:09:02,800 have messages in our constructors this 35144 26:09:02,800 --> 26:09:08,232 what we expect but we want to also put a 35145 26:09:05,279 --> 26:09:11,360 message in the destructor let's say sddc 35146 26:09:08,232 --> 26:09:13,512 out and say destructor for person called 35147 26:09:11,360 --> 26:09:15,279 this is going to do we're going to go in 35148 26:09:13,512 --> 26:09:17,360 our engineer class we're going to see 35149 26:09:15,279 --> 26:09:19,279 that we have a bunch of constructors we 35150 26:09:17,360 --> 26:09:22,000 have a destructor here we're going to 35151 26:09:22,000 --> 26:09:26,551 and make sure we take out the bad code 35152 26:09:24,960 --> 26:09:29,832 here because we don't really need that 35153 26:09:26,551 --> 26:09:32,159 we only need good code and we make sure 35154 26:09:29,831 --> 26:09:34,000 that we have output statements in our 35155 26:09:32,160 --> 26:09:36,320 constructors to know when they are 35156 26:09:34,000 --> 26:09:38,800 called looks like we are good we're 35157 26:09:36,320 --> 26:09:41,760 going to put an sddc out statement in 35158 26:09:38,800 --> 26:09:43,832 our destructor for engineer or engineer 35159 26:09:41,759 --> 26:09:46,319 called this is going to do and we are 35160 26:09:43,831 --> 26:09:47,759 going to hop over in our civil engineer 35161 26:09:46,320 --> 26:09:50,400 class we're going to have a bunch of 35162 26:09:47,759 --> 26:09:51,759 constructors we're going to have a civil 35163 26:09:53,119 --> 26:09:58,231 we can go in the cpp file and make sure 35164 26:09:55,679 --> 26:10:00,871 that we have output statements in our 35165 26:09:58,232 --> 26:10:02,960 civil engineer constructors looks like 35166 26:10:00,872 --> 26:10:05,832 we are good here we can take out all the 35167 26:10:02,960 --> 26:10:08,552 bad code we don't need and we are going 35168 26:10:05,831 --> 26:10:11,191 to put a message in our destructor here 35169 26:10:08,551 --> 26:10:14,080 so let's do that the the structure 35170 26:10:11,191 --> 26:10:16,479 called for civil engineer i think this 35171 26:10:14,080 --> 26:10:18,551 is going to be good enough let's go back 35172 26:10:16,479 --> 26:10:20,551 and make sure we have the same messages 35173 26:10:20,551 --> 26:10:24,959 and we have a destructor for person 35174 26:10:22,720 --> 26:10:26,479 called message and we're going to try 35175 26:10:26,479 --> 26:10:31,679 constructors and destructors are called 35176 26:10:29,279 --> 26:10:34,080 this is the order we want to make sure 35177 26:10:31,679 --> 26:10:36,319 we understand if you want you can even 35178 26:10:34,080 --> 26:10:38,479 use a debugger to follow things but i am 35179 26:10:36,320 --> 26:10:40,320 not going to do that here i am just 35180 26:10:38,479 --> 26:10:41,599 going to weld and show you the output 35181 26:10:41,600 --> 26:10:44,320 hopefully that's going to be proof 35182 26:10:44,320 --> 26:10:49,600 that things happen in the order that i 35183 26:10:47,119 --> 26:10:51,831 just described in the slides the build 35184 26:10:49,600 --> 26:10:54,320 is good we can clear we can bring up a 35185 26:10:51,831 --> 26:10:56,231 powershell window and then clear and 35186 26:10:54,320 --> 26:10:57,832 we're going to run rooster we're going 35187 26:10:56,232 --> 26:11:01,440 to see that the constructor is going to 35188 26:10:57,831 --> 26:11:02,799 be called for person for civil engineer 35189 26:11:01,440 --> 26:11:05,911 and you're going to see that things are 35190 26:11:02,800 --> 26:11:08,479 going to happen from the must-based 35191 26:11:05,911 --> 26:11:11,360 constructor to the most specialized 35192 26:11:08,479 --> 26:11:13,911 constructor so we are going to call 35193 26:11:11,360 --> 26:11:15,600 person engineer and civil engineer but 35194 26:11:13,911 --> 26:11:17,599 you're going to see that the structures 35195 26:11:15,600 --> 26:11:18,480 are going to be called in the reverse 35196 26:11:18,479 --> 26:11:22,479 the most specialized the destructor is 35197 26:11:20,479 --> 26:11:24,479 going to be called the first then we're 35198 26:11:22,479 --> 26:11:26,319 going to call the distractor for 35199 26:11:24,479 --> 26:11:29,119 engineer then we're going to call the 35200 26:11:26,320 --> 26:11:30,872 destructor for the base class so things 35201 26:11:29,119 --> 26:11:33,599 are going to be happening in reverse 35202 26:11:30,872 --> 26:11:35,512 order we will build thanks starting from 35203 26:11:35,512 --> 26:11:40,960 finish by the most specialized class 35204 26:11:38,160 --> 26:11:42,960 which is doing the inheritance and then 35205 26:11:40,960 --> 26:11:45,600 we're going to call the destructor for 35206 26:11:42,960 --> 26:11:48,232 the most specialized class first and 35207 26:11:45,600 --> 26:11:50,639 then the destructor for the most based 35208 26:11:48,232 --> 26:11:52,400 class is going to be cold last you 35209 26:11:50,639 --> 26:11:54,872 should really make sure you understand 35210 26:11:52,399 --> 26:11:57,039 this because sometimes your things are 35211 26:11:54,872 --> 26:11:58,720 going to be depending on this order and 35212 26:11:57,039 --> 26:12:00,959 you need to understand this and this is 35213 26:11:58,720 --> 26:12:03,360 really the main message here that the 35214 26:12:00,960 --> 26:12:04,320 structures are ridiculed in the reverse 35215 26:12:06,080 --> 26:12:09,279 we are going to stop here in this 35216 26:12:07,679 --> 26:12:12,479 lecture in the next one we're going to 35217 26:12:09,279 --> 26:12:15,039 see that we can actually reuse names in 35218 26:12:12,479 --> 26:12:16,720 our inheritance hierarchy and we're 35219 26:12:15,039 --> 26:12:18,871 going to see how that works out in the 35220 26:12:16,720 --> 26:12:21,440 next lecture so go ahead and finish up 35221 26:12:18,872 --> 26:12:23,440 here and meet me there in this lecture 35222 26:12:21,440 --> 26:12:27,191 we're going to see that we can reuse 35223 26:12:23,440 --> 26:12:29,760 names in our inheritance hierarchy 35224 26:12:27,191 --> 26:12:32,399 and what we mean by that is that we can 35225 26:12:29,759 --> 26:12:34,959 have a parent class for example and have 35226 26:12:32,399 --> 26:12:38,639 a child class inheriting from the parent 35227 26:12:34,960 --> 26:12:41,280 class and have exactly the same names 35228 26:12:38,639 --> 26:12:43,512 for the member variables and types or 35229 26:12:41,279 --> 26:12:45,911 the same signatures for functions for 35230 26:12:43,512 --> 26:12:48,320 example in the parent class you see that 35231 26:12:45,911 --> 26:12:50,479 we have a member function called 35232 26:12:48,320 --> 26:12:53,600 printfur it is going to be printing some 35233 26:12:50,479 --> 26:12:56,399 information we can have a function named 35234 26:12:53,600 --> 26:12:58,639 exactly the same in the child class and 35235 26:12:56,399 --> 26:13:00,639 c plus plus is going to allow this we 35236 26:12:58,639 --> 26:13:03,279 can also do the same things for member 35237 26:13:00,639 --> 26:13:06,720 variables for example we have m var 35238 26:13:03,279 --> 26:13:09,831 which is of type and we have exactly the 35239 26:13:06,720 --> 26:13:12,232 same thing in our child class and c plus 35240 26:13:09,831 --> 26:13:14,799 plus is going to allow this now the 35241 26:13:12,232 --> 26:13:17,832 problem is if you create an object of 35242 26:13:14,800 --> 26:13:20,080 child which method will be called if you 35243 26:13:20,080 --> 26:13:25,191 what c plus plus does is it is going to 35244 26:13:25,191 --> 26:13:30,080 things you have in the base class if you 35245 26:13:27,279 --> 26:13:33,191 happen to set up exactly the same thing 35246 26:13:30,080 --> 26:13:36,551 in a derived class so for example the 35247 26:13:33,191 --> 26:13:39,039 members in our child class are going to 35248 26:13:36,551 --> 26:13:41,599 hide the members we had in the base 35249 26:13:39,039 --> 26:13:44,159 class and if we call these methods on a 35250 26:13:41,600 --> 26:13:46,960 child object we are going to get the 35251 26:13:44,160 --> 26:13:49,600 versions in the child class called if we 35252 26:13:46,960 --> 26:13:51,680 call these methods on a parent object we 35253 26:13:49,600 --> 26:13:53,360 are going to get the parent version 35254 26:13:51,679 --> 26:13:55,359 called and we're going to see the data 35255 26:13:53,360 --> 26:13:57,831 in the parent class printed out this is 35256 26:13:55,360 --> 26:14:00,399 the message here it is possible to use 35257 26:13:57,831 --> 26:14:03,511 the same names in both parent and child 35258 26:14:00,399 --> 26:14:05,911 classes and if you do that things in 35259 26:14:03,512 --> 26:14:08,720 child classes are going to hide or 35260 26:14:05,911 --> 26:14:10,800 override what we had in the parent class 35261 26:14:08,720 --> 26:14:13,191 this is the message i want to convey 35262 26:14:10,800 --> 26:14:15,832 here and here is a simple example to 35263 26:14:13,191 --> 26:14:18,399 drive home how you can use these things 35264 26:14:15,831 --> 26:14:20,399 for example we can create a child object 35265 26:14:20,399 --> 26:14:24,871 and if we call a method on the child 35266 26:14:23,039 --> 26:14:27,759 object this is going to call the method 35267 26:14:24,872 --> 26:14:31,192 in child but it is possible to force the 35268 26:14:27,759 --> 26:14:32,959 compiler to call the version in the base 35269 26:14:31,191 --> 26:14:35,512 class and you're going to see this 35270 26:14:32,960 --> 26:14:38,872 syntax used somewhere in your code out 35271 26:14:35,512 --> 26:14:41,440 there so the syntax is to say the object 35272 26:14:38,872 --> 26:14:44,232 name you put it up you say the name for 35273 26:14:41,440 --> 26:14:46,400 the parent class and then you call the 35274 26:14:44,232 --> 26:14:48,480 method using the scope resolution 35275 26:14:46,399 --> 26:14:50,720 operator here and this is going to hold 35276 26:14:48,479 --> 26:14:52,159 the method in the parent okay now that 35277 26:14:50,720 --> 26:14:54,160 you know this we're going to head over 35278 26:14:52,160 --> 26:14:56,872 to visual studio code and play with this 35279 26:14:54,160 --> 26:14:59,279 a little more here we are in our working 35280 26:14:56,872 --> 26:15:02,400 folder the current project is reused 35281 26:14:59,279 --> 26:15:05,440 symbols and inheritance we are going to 35282 26:15:02,399 --> 26:15:07,279 just grab the template files because we 35283 26:15:05,440 --> 26:15:09,440 are going to be building things from 35284 26:15:07,279 --> 26:15:12,399 scratch here and we're going to put in 35285 26:15:09,440 --> 26:15:14,872 our files no big deal here and we can 35286 26:15:12,399 --> 26:15:18,079 open this in visual studio code this is 35287 26:15:14,872 --> 26:15:21,040 going to give us a bare minimum main cpp 35288 26:15:18,080 --> 26:15:22,960 file we can clean it up a little bit and 35289 26:15:21,039 --> 26:15:24,639 we're going to set up our parent class 35290 26:15:22,960 --> 26:15:27,360 let's do that pretty fast we're going to 35291 26:15:24,639 --> 26:15:29,119 say parent and set up the header and 35292 26:15:27,360 --> 26:15:32,000 we're going to set up the implementation 35293 26:15:29,119 --> 26:15:35,599 cpp file we are going to set up the 35294 26:15:32,000 --> 26:15:38,639 header file and this is going to do 35295 26:15:35,600 --> 26:15:40,872 everything we want we have a constructor 35296 26:15:38,639 --> 26:15:43,512 which is really defaulted here so let's 35297 26:15:40,872 --> 26:15:45,600 say default and we cannot default the 35298 26:15:43,512 --> 26:15:47,832 destructor can we default it let's do 35299 26:15:45,600 --> 26:15:50,400 this and see what happens and we don't 35300 26:15:47,831 --> 26:15:52,319 really need to do anything in the cpp 35301 26:15:50,399 --> 26:15:54,959 file we can leave it empty this is not 35302 26:15:52,320 --> 26:15:57,512 going to hurt anything and with this we 35303 26:15:54,960 --> 26:15:58,872 can create parent objects let's do that 35304 26:15:58,872 --> 26:16:02,800 we're going to hop over to the main cpp 35305 26:16:00,720 --> 26:16:04,800 file and include current and make sure 35306 26:16:02,800 --> 26:16:07,192 that we can use that and we're going to 35307 26:16:04,800 --> 26:16:10,232 create a parent object let's do that 35308 26:16:07,191 --> 26:16:13,039 we're going to say parent p1 and say 35309 26:16:10,232 --> 26:16:15,279 p1 print values we can do that and we 35310 26:16:13,039 --> 26:16:17,759 can work and run this program i think it 35311 26:16:15,279 --> 26:16:20,232 is going to work just fine okay the 35312 26:16:17,759 --> 26:16:23,119 build is good we can clear or we can set 35313 26:16:20,232 --> 26:16:25,360 up a powershell window and a clear 35314 26:16:23,119 --> 26:16:27,911 and run rooster this is going to say the 35315 26:16:25,360 --> 26:16:29,831 value in parent is 100 which is what we 35316 26:16:27,911 --> 26:16:32,872 have by default here we have a simple 35317 26:16:29,831 --> 26:16:35,599 member variable which is of ant type it 35318 26:16:32,872 --> 26:16:38,320 is protected because we want it to be 35319 26:16:35,600 --> 26:16:40,000 inherited and we have a bunch of 35320 26:16:38,320 --> 26:16:41,600 constructors here we have a constructor 35321 26:16:40,000 --> 26:16:43,360 which is going to take a value but we're 35322 26:16:41,600 --> 26:16:45,512 not going to use that here the default 35323 26:16:43,360 --> 26:16:47,680 is just fine you can use that if you 35324 26:16:45,512 --> 26:16:49,600 want and we have a member function which 35325 26:16:47,679 --> 26:16:51,831 is going to print the value in parent 35326 26:16:49,600 --> 26:16:53,680 now that this is working we can set up a 35327 26:16:51,831 --> 26:16:56,231 child class which is going to be 35328 26:16:53,679 --> 26:16:58,079 inheriting from the parent class here so 35329 26:16:56,232 --> 26:17:01,192 let's do that we're going to set up the 35330 26:16:58,080 --> 26:17:03,759 child header file child dot h and we're 35331 26:17:01,191 --> 26:17:06,551 going to set up a cpp file for that that 35332 26:17:03,759 --> 26:17:08,959 cpp so we are going to put the data in 35333 26:17:06,551 --> 26:17:11,679 the header file again it is nothing 35334 26:17:08,960 --> 26:17:13,680 complicated it is just a simple class 35335 26:17:11,679 --> 26:17:16,479 which is going to be inheriting from 35336 26:17:13,679 --> 26:17:17,831 parent and we are going to have our own 35337 26:17:16,479 --> 26:17:20,159 member variable which is going to 35338 26:17:17,831 --> 26:17:22,720 eclipse what we had in the parent so the 35339 26:17:20,160 --> 26:17:25,911 names here must be the same m a member 35340 26:17:22,720 --> 26:17:28,960 of r if we go in parent it is m member 35341 26:17:25,911 --> 26:17:31,360 for and what we have in child is going 35342 26:17:28,960 --> 26:17:34,080 to eclipse what we had in the parent 35343 26:17:31,360 --> 26:17:36,399 class in other words if we print the 35344 26:17:34,080 --> 26:17:39,680 member variable in child we're going to 35345 26:17:36,399 --> 26:17:43,191 see 1000 printed out here we want to see 35346 26:17:39,679 --> 26:17:45,359 100 that we have in the parent printed 35347 26:17:43,191 --> 26:17:48,000 out hopefully this is going to drive the 35348 26:17:45,360 --> 26:17:50,399 point home that if you have members 35349 26:17:50,399 --> 26:17:56,079 what you have in your derived classes is 35350 26:17:53,119 --> 26:17:57,831 going to eclipse or hide what we had in 35351 26:17:56,080 --> 26:18:01,191 the parent class this is the message 35352 26:17:57,831 --> 26:18:04,080 here but even if this is the case c plus 35353 26:18:01,191 --> 26:18:07,512 plus allows us to specify that we want 35354 26:18:04,080 --> 26:18:10,960 to grab the data in parent from a child 35355 26:18:07,512 --> 26:18:13,440 object for example in our show values 35356 26:18:10,960 --> 26:18:16,232 method we can print the value in child 35357 26:18:13,440 --> 26:18:18,551 directly using m member war but we can 35358 26:18:16,232 --> 26:18:21,192 also specify that we want the value in 35359 26:18:18,551 --> 26:18:24,720 the parent class because even if this is 35360 26:18:21,191 --> 26:18:27,360 a child object it has a current part of 35361 26:18:24,720 --> 26:18:30,000 it so we can access the data in parent 35362 26:18:27,360 --> 26:18:32,720 and print that if the data happens to be 35363 26:18:30,000 --> 26:18:34,720 accessible from our derived class and 35364 26:18:32,720 --> 26:18:37,512 that's why we set up our member variable 35365 26:18:34,720 --> 26:18:39,759 to be protected for it to be accessible 35366 26:18:37,512 --> 26:18:41,832 in our derived classes this is going to 35367 26:18:39,759 --> 26:18:44,000 allow us to print something like this 35368 26:18:41,831 --> 26:18:46,231 and this is going to really work now 35369 26:18:44,000 --> 26:18:48,320 that we have this in place we can go in 35370 26:18:48,320 --> 26:18:53,120 and only include the child we can do 35371 26:18:50,960 --> 26:18:55,600 that and we're going to create a child 35372 26:18:53,119 --> 26:18:57,440 object or we can just take out 35373 26:18:57,440 --> 26:19:01,911 and adjust putting the code we can use 35374 26:18:59,440 --> 26:19:03,680 to play with this things we can create 35375 26:19:03,679 --> 26:19:07,679 and on this child we can print for we're 35376 26:19:06,160 --> 26:19:10,080 going to see that the data is going to 35377 26:19:07,679 --> 26:19:13,191 be printed out but we can also use this 35378 26:19:10,080 --> 26:19:14,800 syntax to print the data in the parent 35379 26:19:13,191 --> 26:19:17,119 part of us and we're going to see that 35380 26:19:14,800 --> 26:19:20,160 the data is printed out we can also show 35381 26:19:17,119 --> 26:19:22,720 the values and make sure we see this 35382 26:19:20,160 --> 26:19:25,192 from a member function of the child 35383 26:19:22,720 --> 26:19:26,800 class this is going to call this member 35384 26:19:25,191 --> 26:19:28,479 and we're going to get to the value in 35385 26:19:26,800 --> 26:19:31,832 child and we're going to get the value 35386 26:19:28,479 --> 26:19:34,080 in parent printed out on the console now 35387 26:19:31,831 --> 26:19:36,479 that we have this we can actually try 35388 26:19:34,080 --> 26:19:37,512 and work this we're going to use gcc to 35389 26:19:37,512 --> 26:19:42,160 we're going to see that we're going to 35390 26:19:39,679 --> 26:19:44,319 finish building with errors what is the 35391 26:19:42,160 --> 26:19:46,872 problem here we have an undefined 35392 26:19:44,320 --> 26:19:49,192 reference to child destructor 35393 26:19:46,872 --> 26:19:52,000 we didn't put in a definition for the 35394 26:19:49,191 --> 26:19:54,799 child destructor so let's go there 35395 26:19:52,000 --> 26:19:56,551 and default it we can do that we're 35396 26:19:54,800 --> 26:20:00,320 going to say equals default and the 35397 26:19:56,551 --> 26:20:02,872 compiler is going to generate one for us 35398 26:20:00,320 --> 26:20:05,192 and now if we build we're going to build 35399 26:20:02,872 --> 26:20:08,000 with gcc again the bolt is going to be 35400 26:20:05,191 --> 26:20:09,512 good you can clear and run rooster and 35401 26:20:08,000 --> 26:20:12,720 we're going to see that the value in 35402 26:20:09,512 --> 26:20:15,360 child is 33 because we passed that in 35403 26:20:12,720 --> 26:20:17,680 explicitly and this is going to call the 35404 26:20:15,360 --> 26:20:20,232 constructor we have in child that 35405 26:20:17,679 --> 26:20:22,399 happens to be this guy here and you see 35406 26:20:20,232 --> 26:20:24,480 that we are really keeping things simple 35407 26:20:22,399 --> 26:20:27,831 we are not calling the base constructor 35408 26:20:24,479 --> 26:20:30,231 here the compiler is just going to call 35409 26:20:27,831 --> 26:20:32,551 the default constructor we have in 35410 26:20:30,232 --> 26:20:34,480 parent here and this is going to just 35411 26:20:32,551 --> 26:20:37,599 work the main message here is that you 35412 26:20:34,479 --> 26:20:40,800 can set up member variables and member 35413 26:20:37,600 --> 26:20:43,192 functions named exactly the same way in 35414 26:20:40,800 --> 26:20:45,360 the parent class and in child classes 35415 26:20:43,191 --> 26:20:48,479 and what you have in your derived 35416 26:20:45,360 --> 26:20:51,440 classes is going to hide what we have in 35417 26:20:48,479 --> 26:20:54,080 our current classes so if we call a 35418 26:20:51,440 --> 26:20:56,160 method on a child object for example 35419 26:20:54,080 --> 26:20:58,232 like we are doing here this is going to 35420 26:20:56,160 --> 26:21:00,480 call the method in child but we still 35421 26:20:58,232 --> 26:21:02,960 have the ability to call data in the 35422 26:21:00,479 --> 26:21:05,279 parent part of us and this is what we do 35423 26:21:02,960 --> 26:21:07,040 here i would like to welcome you in this 35424 26:21:05,279 --> 26:21:09,759 new chapter where we're going to be 35425 26:21:07,039 --> 26:21:12,799 learning about polymorphism and 35426 26:21:09,759 --> 26:21:15,279 polymorphism is the setup we can do in 35427 26:21:15,279 --> 26:21:21,039 to use a base pointer and manage derived 35428 26:21:18,399 --> 26:21:23,191 objects suppose we have an inheritance 35429 26:21:21,039 --> 26:21:26,319 hierarchy like we have here we have 35430 26:21:23,191 --> 26:21:28,871 shaped as our base class and we can 35431 26:21:26,320 --> 26:21:31,360 inherit from oval and create a circle 35432 26:21:28,872 --> 26:21:34,160 class imagine that we can create all 35433 26:21:31,360 --> 26:21:36,399 kinds of crazy shapes that inherit from 35434 26:21:34,160 --> 26:21:38,320 shape in our simplest path program for 35435 26:21:36,399 --> 26:21:40,399 example we have a rectangle class we 35436 26:21:38,320 --> 26:21:42,960 have a triangle class we have all kinds 35437 26:21:40,399 --> 26:21:45,679 of crazy shapes now if we have this 35438 26:21:42,960 --> 26:21:48,000 inheritance hierarchy here is something 35439 26:21:45,679 --> 26:21:50,719 we might want to do we might want to set 35440 26:21:48,000 --> 26:21:53,600 up a base pointer that is managing a 35441 26:21:50,720 --> 26:21:56,399 derived object and set up code like this 35442 26:21:53,600 --> 26:21:58,480 for example we say shape pointer and i 35443 26:21:56,399 --> 26:22:01,439 set up a pointer variable here and use 35444 26:21:58,479 --> 26:22:04,231 this pointer to manage a circle object 35445 26:22:01,440 --> 26:22:07,119 that we dynamically allocate for on this 35446 26:22:04,232 --> 26:22:09,600 line here we can also do a shape pointer 35447 26:22:07,119 --> 26:22:11,512 that is managing a rectangle we can set 35448 26:22:09,600 --> 26:22:14,232 up a shape pointer that is managing a 35449 26:22:11,512 --> 26:22:16,551 novel object we can really set up a base 35450 26:22:14,232 --> 26:22:19,192 pointer that is managing any kind of 35451 26:22:16,551 --> 26:22:21,360 object we support in our inheritance 35452 26:22:19,191 --> 26:22:23,679 hierarchy now some of you are going to 35453 26:22:21,360 --> 26:22:27,039 ask a few questions one of them is going 35454 26:22:23,679 --> 26:22:30,319 to be is this even legal c plus plus 35455 26:22:27,039 --> 26:22:33,439 syntax and the answer is yes because if 35456 26:22:30,320 --> 26:22:35,280 circle is inherited somehow from shape a 35457 26:22:33,440 --> 26:22:37,911 circle is really a shape and if 35458 26:22:35,279 --> 26:22:40,399 rectangle is inheriting from shape it is 35459 26:22:37,911 --> 26:22:41,759 a shape and if we try to assign a circle 35460 26:22:40,399 --> 26:22:44,319 to a shape we're going to see that 35461 26:22:41,759 --> 26:22:46,959 that's going to work so this is valid c 35462 26:22:44,320 --> 26:22:49,120 plus plus syntax and we can do the same 35463 26:22:46,960 --> 26:22:50,872 thing using references as we see here we 35464 26:22:49,119 --> 26:22:52,399 can set up a reference that is going to 35465 26:22:52,399 --> 26:22:57,511 a circle object that we have through 35466 26:22:55,119 --> 26:22:59,831 circle one we can set up a reference to 35467 26:22:57,512 --> 26:23:02,320 manage a rectangle that we have in shape 35468 26:22:59,831 --> 26:23:04,639 two we can set up a reference to manage 35469 26:23:02,320 --> 26:23:05,912 an awful object we can really do all 35470 26:23:08,160 --> 26:23:12,480 why would we want to do something like 35471 26:23:10,160 --> 26:23:14,960 this now try and think about your 35472 26:23:12,479 --> 26:23:17,599 inheritance hierarchy here and try to 35473 26:23:14,960 --> 26:23:20,080 think about what if you want to 35474 26:23:17,600 --> 26:23:22,800 draw this kind of ship for example if 35475 26:23:20,080 --> 26:23:24,960 you didn't use polymorphism you would 35476 26:23:22,800 --> 26:23:27,911 need to set up all kinds of crazy 35477 26:23:24,960 --> 26:23:29,912 methods to draw each kind of object and 35478 26:23:27,911 --> 26:23:32,319 we have no way around this because these 35479 26:23:29,911 --> 26:23:34,479 are different types if you have a method 35480 26:23:32,320 --> 26:23:35,760 that takes a novel and you pass in a 35481 26:23:34,479 --> 26:23:38,399 circle that's going to give you a 35482 26:23:35,759 --> 26:23:40,479 compiler error but with polymorphism we 35483 26:23:38,399 --> 26:23:43,191 can set up a single method which takes a 35484 26:23:40,479 --> 26:23:45,679 shape pointer or a shape reference 35485 26:23:43,191 --> 26:23:48,639 and we can pass in all kinds of crazy 35486 26:23:45,679 --> 26:23:50,551 objects whose class derive from shape so 35487 26:23:48,639 --> 26:23:52,232 for example we can pass in the address 35488 26:23:50,551 --> 26:23:54,231 of the circle and this function is going 35489 26:23:52,232 --> 26:23:56,160 to draw a circle we can pass in the 35490 26:23:54,232 --> 26:23:58,232 address of a rectangle this is going to 35491 26:23:56,160 --> 26:24:00,080 draw a rectangle we can pass in the 35492 26:23:58,232 --> 26:24:02,320 address of a novel and this is going to 35493 26:24:00,080 --> 26:24:04,720 draw a novel hopefully you can see how 35494 26:24:02,320 --> 26:24:07,120 this is useful we can even do this using 35495 26:24:04,720 --> 26:24:09,119 references so if we pass in a rectangle 35496 26:24:07,119 --> 26:24:11,360 this is going to draw a rectangle if we 35497 26:24:09,119 --> 26:24:14,319 pass in a circle this is going to draw a 35498 26:24:11,360 --> 26:24:17,119 circle you get the idea this is one of 35499 26:24:14,320 --> 26:24:20,232 the benefits of using polymorphism again 35500 26:24:17,119 --> 26:24:23,512 polymorphism is about using base class 35501 26:24:20,232 --> 26:24:26,480 pointers or references to manage derived 35502 26:24:23,512 --> 26:24:29,191 objects in our inheritance hierarchies 35503 26:24:26,479 --> 26:24:31,512 another benefit of polymorphism is to 35504 26:24:29,191 --> 26:24:33,679 allow us to be able to store 35505 26:24:31,512 --> 26:24:36,551 different kinds of objects in a single 35506 26:24:33,679 --> 26:24:39,191 collection remember an array can store 35507 26:24:36,551 --> 26:24:41,599 objects of different types if you say 35508 26:24:39,191 --> 26:24:43,191 tap an end array and try to store in a 35509 26:24:41,600 --> 26:24:44,720 string or a double you're going to get a 35510 26:24:43,191 --> 26:24:47,512 compiler error because that's not 35511 26:24:44,720 --> 26:24:49,911 allowed so if you have an array that is 35512 26:24:47,512 --> 26:24:52,000 going to store circles you can't put in 35513 26:24:49,911 --> 26:24:54,872 a novel you can't put in a rectangle you 35514 26:24:52,000 --> 26:24:57,759 can put in a star you can't really do 35515 26:24:54,872 --> 26:25:00,479 that because an array by design is going 35516 26:24:57,759 --> 26:25:02,551 to store objects of the same type but we 35517 26:25:00,479 --> 26:25:04,872 can avoid this limitation using 35518 26:25:02,551 --> 26:25:07,759 polymorphism again we can set up an 35519 26:25:04,872 --> 26:25:10,551 array that is going to store base class 35520 26:25:07,759 --> 26:25:13,439 pointers and we can store in all kinds 35521 26:25:10,551 --> 26:25:16,231 of crazy objects whose class derived 35522 26:25:13,440 --> 26:25:18,232 from this base class here so here we can 35523 26:25:16,232 --> 26:25:20,232 store in a circle object we can store in 35524 26:25:18,232 --> 26:25:23,192 a novel we can store in any kind of 35525 26:25:20,232 --> 26:25:26,400 object that is part of our inheritance 35526 26:25:23,191 --> 26:25:28,159 hierarchy with the base class as shape 35527 26:25:26,399 --> 26:25:29,831 and this is going to work and this is 35528 26:25:28,160 --> 26:25:31,360 going to be using polymorphism for 35529 26:25:29,831 --> 26:25:33,831 example if you look through this 35530 26:25:31,360 --> 26:25:35,512 collection calling the joe method for 35531 26:25:33,831 --> 26:25:37,759 the first element this is going to call 35532 26:25:35,512 --> 26:25:39,191 the draw method on the circle for the 35533 26:25:37,759 --> 26:25:41,191 second element we're going to call the 35534 26:25:39,191 --> 26:25:42,080 draw method on a novel and this is going 35535 26:25:42,080 --> 26:25:46,400 really cool this is something we can 35536 26:25:43,759 --> 26:25:48,551 achieve with polymorphism and i want to 35537 26:25:46,399 --> 26:25:50,639 back up a little bit and talk about what 35538 26:25:48,551 --> 26:25:53,759 is really meant by polymorphism 35539 26:25:50,639 --> 26:25:56,232 polymorphism really means multiple forms 35540 26:25:53,759 --> 26:25:58,720 and in my opinion what this means is 35541 26:25:56,232 --> 26:26:01,360 that the base class pointer or base 35542 26:25:58,720 --> 26:26:03,279 class reference can take multiple forms 35543 26:26:01,360 --> 26:26:05,360 at one point it might be managing a 35544 26:26:03,279 --> 26:26:07,279 circle at one point it might be managing 35545 26:26:05,360 --> 26:26:10,080 a rectangle at one point it might be 35546 26:26:07,279 --> 26:26:13,191 managing a novel object so it is a basic 35547 26:26:10,080 --> 26:26:14,960 pointer but it can take multiple forms 35548 26:26:13,191 --> 26:26:18,399 because it can be managing multiple 35549 26:26:14,960 --> 26:26:21,120 kinds of objects whose class are part of 35550 26:26:18,399 --> 26:26:23,511 our inheritance hierarchy this is what 35551 26:26:21,119 --> 26:26:25,679 we mean by polymorphism so again the 35552 26:26:23,512 --> 26:26:27,832 idea is that we can use the base class 35553 26:26:25,679 --> 26:26:30,399 pointer or reference to manage all kinds 35554 26:26:27,831 --> 26:26:32,799 of crazy objects in our program and this 35555 26:26:30,399 --> 26:26:35,439 is going to have many benefits as we are 35556 26:26:32,800 --> 26:26:37,760 about to experience in this chapter here 35557 26:26:35,440 --> 26:26:39,832 i am really excited to be explaining 35558 26:26:37,759 --> 26:26:42,399 this to you this is one of the most 35559 26:26:39,831 --> 26:26:44,319 powerful features in c plus plus we are 35560 26:26:42,399 --> 26:26:46,639 going to start in the next lecture and 35561 26:26:44,320 --> 26:26:49,280 show you that you don't get polymorphism 35562 26:26:46,639 --> 26:26:52,000 by default in c plus plus and what you 35563 26:26:49,279 --> 26:26:54,551 get is static binding go ahead and 35564 26:26:52,000 --> 26:26:57,039 finish up here and meet me there 35565 26:26:54,551 --> 26:26:59,440 manage derived objects in our c plus 35566 26:26:57,039 --> 26:27:02,399 plus program hopefully this is going to 35567 26:26:59,440 --> 26:27:04,720 give you a clear idea on why we might 35568 26:27:02,399 --> 26:27:06,399 need to do this so let's look at our 35569 26:27:04,720 --> 26:27:08,232 classes that we're going to be using 35570 26:27:06,399 --> 26:27:10,551 here we're going to set up a shape class 35571 26:27:08,232 --> 26:27:12,800 which is going to be representing our 35572 26:27:10,551 --> 26:27:15,360 shapes this is going to be our base 35573 26:27:12,800 --> 26:27:17,360 class that's the basic use for this here 35574 26:27:15,360 --> 26:27:20,000 it has a simple member variable which is 35575 26:27:17,360 --> 26:27:22,720 going to store the description for our 35576 26:27:20,000 --> 26:27:24,639 shape it is also going to have our draw 35577 26:27:22,720 --> 26:27:26,800 method which is really not going to draw 35578 26:27:24,639 --> 26:27:29,679 anything it is just going to say the 35579 26:27:26,800 --> 26:27:31,512 description for our shape here once we 35580 26:27:29,679 --> 26:27:33,511 have this shape we can create other 35581 26:27:31,512 --> 26:27:35,680 shapes from it for example we can create 35582 26:27:33,512 --> 26:27:38,080 a novel class which is going to be 35583 26:27:35,679 --> 26:27:40,479 representing a shape like this it is 35584 26:27:38,080 --> 26:27:43,191 going to have two member variables one 35585 26:27:40,479 --> 26:27:45,512 is going to be mx radius the other is 35586 26:27:43,191 --> 26:27:47,119 going to be m y radius this may 35587 26:27:47,119 --> 26:27:53,119 the radiuses for our oval here for 35588 26:27:50,551 --> 26:27:55,360 example the x radius might be something 35589 26:27:53,119 --> 26:27:57,039 like this the y radius might be 35590 26:27:55,360 --> 26:27:59,191 something like this and this is how 35591 26:27:57,039 --> 26:28:01,511 we're going to be representing 35592 26:27:59,191 --> 26:28:03,039 this shape here we also have a draw 35593 26:28:01,512 --> 26:28:05,832 method which is going to print the 35594 26:28:03,039 --> 26:28:07,599 information about this shape here it is 35595 26:28:05,831 --> 26:28:09,759 going to say the description that we're 35596 26:28:07,600 --> 26:28:12,000 going to be inheriting publicly from 35597 26:28:09,759 --> 26:28:14,479 shape but it is also going to say the 35598 26:28:12,000 --> 26:28:17,279 radius information for this shape it is 35599 26:28:14,479 --> 26:28:19,831 going to give us the x radius and the y 35600 26:28:17,279 --> 26:28:21,759 radius here and we have a bunch of 35601 26:28:19,831 --> 26:28:23,511 constructors here we have a default 35602 26:28:21,759 --> 26:28:25,831 constructor which is really not going to 35603 26:28:23,512 --> 26:28:27,360 do anything we also have a constructor 35604 26:28:25,831 --> 26:28:30,551 that is going to be taking two 35605 26:28:27,360 --> 26:28:32,960 parameters to forward the data in our 35606 26:28:30,551 --> 26:28:34,872 member variables here this is the kind 35607 26:28:32,960 --> 26:28:37,600 of class we're going to set up in our 35608 26:28:34,872 --> 26:28:40,551 program we also have another derived 35609 26:28:37,600 --> 26:28:42,912 class from oval and this is going to be 35610 26:28:40,551 --> 26:28:47,191 a circle class it is going to be a 35611 26:28:42,911 --> 26:28:50,551 specialization on oval in which both x 35612 26:28:47,191 --> 26:28:52,479 and y radius are equal so the x radius 35613 26:28:50,551 --> 26:28:54,551 is going to be something like this and 35614 26:28:52,479 --> 26:28:57,360 the y radius is going to be something 35615 26:28:54,551 --> 26:28:59,911 like this and both of these are going to 35616 26:28:57,360 --> 26:29:02,720 be equal so this is going to give us a 35617 26:28:59,911 --> 26:29:05,039 circle shape we also have a draw method 35618 26:29:02,720 --> 26:29:07,911 which is going to print the description 35619 26:29:05,039 --> 26:29:10,319 and the radius we can really pick any 35620 26:29:07,911 --> 26:29:12,399 radius we can pick the x radius we can 35621 26:29:10,320 --> 26:29:14,552 pick the y radius they are really going 35622 26:29:12,399 --> 26:29:17,360 to be the same because they are equal 35623 26:29:14,551 --> 26:29:19,119 for our circle shape we also have a 35624 26:29:17,360 --> 26:29:21,600 simple constructor which is going to 35625 26:29:19,119 --> 26:29:23,440 take the radius and we are going to be 35626 26:29:23,440 --> 26:29:29,440 to forward the data to our oval part of 35627 26:29:26,800 --> 26:29:32,160 us and we're going to be setting this as 35628 26:29:29,440 --> 26:29:34,639 both x and y radius this is the setup we 35629 26:29:32,160 --> 26:29:37,040 have here okay now that we have our 35630 26:29:34,639 --> 26:29:39,600 inheritance hierarchy we're going to try 35631 26:29:37,039 --> 26:29:42,399 and use it and really explore the idea 35632 26:29:39,600 --> 26:29:45,040 of static binding in c plus plus 35633 26:29:42,399 --> 26:29:47,039 inheritance hierarchies so here we are 35634 26:29:47,039 --> 26:29:52,639 objects of our classes we have a shape 35635 26:29:50,160 --> 26:29:55,440 object we have an oval object and we 35636 26:29:52,639 --> 26:29:58,479 have a circle object we are going to go 35637 26:29:55,440 --> 26:30:00,160 through a base pointer and try to manage 35638 26:30:00,160 --> 26:30:03,832 in our first line we're going to set up 35639 26:30:02,000 --> 26:30:06,872 a shape pointer and we're going to store 35640 26:30:03,831 --> 26:30:09,759 in the address of shape one and what we 35641 26:30:06,872 --> 26:30:12,720 would want in our c plus plus program is 35642 26:30:09,759 --> 26:30:15,191 that if we call the draw method on this 35643 26:30:12,720 --> 26:30:17,680 shape pointer we would want the shape 35644 26:30:15,191 --> 26:30:20,551 draw method to be called because we have 35645 26:30:17,679 --> 26:30:22,959 a shape object stored in our base 35646 26:30:20,551 --> 26:30:25,911 pointer here or in other words we are 35647 26:30:22,960 --> 26:30:28,552 using a base pointer to manage this 35648 26:30:25,911 --> 26:30:32,080 object in memory down here we store a 35649 26:30:28,551 --> 26:30:34,080 novel object in our shape pointer or in 35650 26:30:32,080 --> 26:30:37,360 other words we're going to be using this 35651 26:30:34,080 --> 26:30:40,000 base pointer to manage an actual oval 35652 26:30:37,360 --> 26:30:42,479 object in memory and the setup we want 35653 26:30:40,000 --> 26:30:44,872 is that if we call the draw method on 35654 26:30:42,479 --> 26:30:47,831 this shape pointer now we are going to 35655 26:30:44,872 --> 26:30:49,832 get the oval draw method to be called 35656 26:30:49,831 --> 26:30:54,639 and down here we have another example 35657 26:30:52,080 --> 26:30:57,680 where we are using this base pointer to 35658 26:30:54,639 --> 26:31:00,080 manage a circle object we are going to 35659 26:30:57,679 --> 26:31:02,000 store the address of the circle object 35660 26:31:00,080 --> 26:31:04,479 in this base pointer this is how we do 35661 26:31:04,479 --> 26:31:10,551 a draw method on this base pointer we 35662 26:31:07,759 --> 26:31:13,911 want the circle draw method to be called 35663 26:31:10,551 --> 26:31:18,159 so with polymorphism what we really want 35664 26:31:13,911 --> 26:31:20,639 is to call a method on the base pointer 35665 26:31:18,160 --> 26:31:23,360 and get the most specific possible 35666 26:31:20,639 --> 26:31:25,600 method called on our pointer this is the 35667 26:31:23,360 --> 26:31:28,399 setup we want but this is not what we 35668 26:31:25,600 --> 26:31:31,192 get by default in c plus plus if we 35669 26:31:28,399 --> 26:31:33,759 happen to do this and run our program as 35670 26:31:31,191 --> 26:31:36,080 is now with the current knowledge that 35671 26:31:33,759 --> 26:31:37,831 we have on the first line here we're 35672 26:31:36,080 --> 26:31:40,000 going to get the shape draw method 35673 26:31:37,831 --> 26:31:42,000 called this is what we want but on the 35674 26:31:40,000 --> 26:31:44,720 second example here we will see that we 35675 26:31:42,000 --> 26:31:47,279 see the shape draw method also called 35676 26:31:44,720 --> 26:31:49,512 and on this line we will also see that 35677 26:31:47,279 --> 26:31:52,080 we get the shape draw method to be 35678 26:31:49,512 --> 26:31:54,960 called this is not what we want and this 35679 26:31:52,080 --> 26:31:58,000 is static binding so what is happening 35680 26:31:54,960 --> 26:32:00,872 here is the compiler is looking at the 35681 26:31:58,000 --> 26:32:03,911 type of the pointer and it is using that 35682 26:32:00,872 --> 26:32:06,080 to decide which draw method to call so 35683 26:32:03,911 --> 26:32:08,959 the compiler is so the compiler is 35684 26:32:06,080 --> 26:32:11,440 basically saying i have a shape pointer 35685 26:32:08,960 --> 26:32:14,080 so i am going to call the joe method on 35686 26:32:11,440 --> 26:32:16,960 the shape class down here it is going to 35687 26:32:14,080 --> 26:32:19,512 see that it also has a shape pointer and 35688 26:32:16,960 --> 26:32:22,080 it is going to call the draw method on 35689 26:32:19,512 --> 26:32:24,551 shape and down here it is going to see 35690 26:32:22,080 --> 26:32:27,040 that it has a shape pointer and it is 35691 26:32:24,551 --> 26:32:30,000 going to call the jaw method on the 35692 26:32:27,039 --> 26:32:31,759 shape class this is what static binding 35693 26:32:30,000 --> 26:32:35,360 is all about the compiler is going to 35694 26:32:31,759 --> 26:32:37,679 resolve the joe method statically and 35695 26:32:35,360 --> 26:32:39,680 what that means is that the compiler is 35696 26:32:37,679 --> 26:32:42,319 going to look at the type of the pointer 35697 26:32:39,679 --> 26:32:46,000 that we have and it is going to use that 35698 26:32:42,320 --> 26:32:48,720 to decide which joe method to call in 35699 26:32:46,000 --> 26:32:51,039 our inheritance hierarchy so this is the 35700 26:32:48,720 --> 26:32:53,360 default and it is called static binding 35701 26:32:51,039 --> 26:32:55,759 in c plus plus we also get the same 35702 26:32:53,360 --> 26:32:58,639 behavior if we try to use a base 35703 26:32:55,759 --> 26:33:01,191 reference to manage our actual object in 35704 26:32:58,639 --> 26:33:04,232 memory here we have the same kinds of 35705 26:33:01,191 --> 26:33:07,512 objects we had in a previous slide and 35706 26:33:04,232 --> 26:33:10,639 we are using a base reference to manage 35707 26:33:07,512 --> 26:33:13,279 our object and what we really want is 35708 26:33:10,639 --> 26:33:16,160 that if we go through a base reference 35709 26:33:13,279 --> 26:33:19,512 to call our draw method we will get the 35710 26:33:16,160 --> 26:33:22,080 most specific draw method called so if 35711 26:33:19,512 --> 26:33:24,872 our base reference is managing a shape 35712 26:33:22,080 --> 26:33:27,440 we will get the shape draw method called 35713 26:33:24,872 --> 26:33:30,000 if our base reference is managing an 35714 26:33:27,440 --> 26:33:33,360 alpha object we want the oval draw 35715 26:33:30,000 --> 26:33:36,320 method to be called here and if our base 35716 26:33:33,360 --> 26:33:39,440 reference is managing a circle object we 35717 26:33:36,320 --> 26:33:41,280 want the circle joe method called here 35718 26:33:39,440 --> 26:33:43,191 this is what we want but again the 35719 26:33:41,279 --> 26:33:45,512 compiler is just going to look at the 35720 26:33:43,191 --> 26:33:47,360 type of the reference so for the first 35721 26:33:45,512 --> 26:33:50,000 line here it's going to say i have a 35722 26:33:47,360 --> 26:33:51,911 shape reference so i am going to call 35723 26:33:50,000 --> 26:33:54,320 the shape draw method that's what we're 35724 26:33:51,911 --> 26:33:56,160 going to get here on the second line 35725 26:33:54,320 --> 26:33:59,040 here it's going to say uh-huh i have a 35726 26:33:56,160 --> 26:34:01,040 shape reference so i will call 35727 26:33:59,039 --> 26:34:02,399 the draw method of the shape class 35728 26:34:01,039 --> 26:34:04,871 that's what we're going to see here by 35729 26:34:02,399 --> 26:34:07,360 default and on this line here it is 35730 26:34:04,872 --> 26:34:09,512 going to say i have a shape reference so 35731 26:34:07,360 --> 26:34:11,360 i am going to call the draw method on 35732 26:34:09,512 --> 26:34:14,400 the shape class and this is the behavior 35733 26:34:11,360 --> 26:34:16,960 we get by default and this is static 35734 26:34:14,399 --> 26:34:19,039 binding in action okay so now that we 35735 26:34:16,960 --> 26:34:22,480 have seen what static binding is all 35736 26:34:19,039 --> 26:34:24,959 about let's re-examine the idea of why 35737 26:34:22,479 --> 26:34:26,639 we need polymorphism and i am going to 35738 26:34:24,960 --> 26:34:29,120 give you a simple example here suppose 35739 26:34:26,639 --> 26:34:32,080 we have a bunch of shapes we manage in 35740 26:34:29,119 --> 26:34:34,551 our programs and we want to draw all 35741 26:34:32,080 --> 26:34:37,119 these shapes if we want to draw a circle 35742 26:34:34,551 --> 26:34:39,360 with the current setup we have we would 35743 26:34:37,119 --> 26:34:41,360 set up a draw circle method if we want 35744 26:34:39,360 --> 26:34:44,000 to draw a novel we would need to set up 35745 26:34:41,360 --> 26:34:46,232 a draw over method and this will call 35746 26:34:44,000 --> 26:34:48,960 our circle draw method and this will 35747 26:34:46,232 --> 26:34:51,120 call our oval joe method this is going 35748 26:34:48,960 --> 26:34:54,160 to do what we want but try to think 35749 26:34:51,119 --> 26:34:57,831 about this if we have 100 shapes in our 35750 26:34:54,160 --> 26:34:59,600 program we will need to set up 100 such 35751 26:34:57,831 --> 26:35:01,759 methods and this is a mess i don't think 35752 26:34:59,600 --> 26:35:04,400 you want to do something like this 35753 26:35:01,759 --> 26:35:06,720 and we have another simple case that i 35754 26:35:04,399 --> 26:35:10,000 want you to consider suppose we want to 35755 26:35:06,720 --> 26:35:12,000 store all these objects in collections 35756 26:35:10,000 --> 26:35:14,800 and we're going to use an array to try 35757 26:35:12,000 --> 26:35:16,720 and drive this point home here if we 35758 26:35:14,800 --> 26:35:18,160 want to store these guys in collections 35759 26:35:16,720 --> 26:35:21,911 we will need to set up different 35760 26:35:18,160 --> 26:35:24,800 collections because as we have it now we 35761 26:35:21,911 --> 26:35:27,119 can't store different types in an array 35762 26:35:24,800 --> 26:35:29,832 so what we would need to do now is to 35763 26:35:27,119 --> 26:35:32,639 set up an array for circles and set up 35764 26:35:29,831 --> 26:35:34,479 an array for ovals and do more 35765 26:35:32,639 --> 26:35:36,720 collections for different kinds of 35766 26:35:34,479 --> 26:35:38,872 shapes that we support in our program 35767 26:35:36,720 --> 26:35:40,872 and if we want to draw them we have no 35768 26:35:38,872 --> 26:35:43,192 choice but to set up different kinds of 35769 26:35:40,872 --> 26:35:44,960 loops and each of these loops is going 35770 26:35:43,191 --> 26:35:47,039 to be looping on each of these 35771 26:35:44,960 --> 26:35:49,040 collections here hopefully you can see 35772 26:35:47,039 --> 26:35:51,360 that this is by design we don't want to 35773 26:35:49,039 --> 26:35:53,191 do this we want to set up something that 35774 26:35:51,360 --> 26:35:56,551 is really easier to manage in our 35775 26:35:53,191 --> 26:35:59,039 program and polymorphism was 35776 26:35:56,551 --> 26:36:02,319 and polymorphism and c plus plus was 35777 26:35:59,039 --> 26:36:03,279 designed to exactly solve this kind of 35778 26:36:03,279 --> 26:36:08,160 what we really want in our c plus plus 35779 26:36:05,039 --> 26:36:10,159 programs is to set up one draw method 35780 26:36:08,160 --> 26:36:12,720 for example we can give it a base 35781 26:36:10,160 --> 26:36:15,279 pointer and it doesn't matter which kind 35782 26:36:12,720 --> 26:36:17,759 of object we pass it when we call this 35783 26:36:15,279 --> 26:36:20,872 we will get that method to be called at 35784 26:36:17,759 --> 26:36:23,191 run time when we actually need to draw 35785 26:36:20,872 --> 26:36:25,279 this shape if we pass in a circle this 35786 26:36:23,191 --> 26:36:28,799 is going to draw the draw method on a 35787 26:36:25,279 --> 26:36:32,000 circle if we pass in a novel it will 35788 26:36:28,800 --> 26:36:34,551 call the draw method on our oval object 35789 26:36:32,000 --> 26:36:36,800 if we pass in a rectangle for example it 35790 26:36:34,551 --> 26:36:39,191 is going to call the draw method on our 35791 26:36:36,800 --> 26:36:41,279 rectangle object this is what we want we 35792 26:36:39,191 --> 26:36:43,039 also want the same kind of setup if for 35793 26:36:41,279 --> 26:36:44,800 example our functions are taking 35794 26:36:43,039 --> 26:36:46,959 reference and this is what we're going 35795 26:36:44,800 --> 26:36:49,279 to get this is what we want with 35796 26:36:46,960 --> 26:36:51,760 polymorphism and this is really going to 35797 26:36:49,279 --> 26:36:54,319 improve on the design that we had 35798 26:36:51,759 --> 26:36:56,639 earlier when we needed to set up a 35799 26:36:54,320 --> 26:36:58,640 specific method for each shape and we 35800 26:36:56,639 --> 26:37:00,960 would end up with hundreds of draw 35801 26:36:58,639 --> 26:37:02,960 methods and that's really bad it's not 35802 26:37:00,960 --> 26:37:05,360 manageable this is also going to solve 35803 26:37:05,360 --> 26:37:09,759 we really need to set up one collection 35804 26:37:07,600 --> 26:37:12,480 which is going to be taking into account 35805 26:37:09,759 --> 26:37:15,279 all the ships that we manage in our 35806 26:37:12,479 --> 26:37:16,639 inheritance hierarchy so the setup we do 35807 26:37:15,279 --> 26:37:18,551 is something like this we're going to 35808 26:37:16,639 --> 26:37:21,440 set up an array that is going to be 35809 26:37:18,551 --> 26:37:24,319 storing pointers to shape and we can 35810 26:37:21,440 --> 26:37:26,400 store in the address of a shape we can 35811 26:37:24,320 --> 26:37:28,080 store in the address of a novel we can 35812 26:37:26,399 --> 26:37:30,000 store in the address of the circle 35813 26:37:28,080 --> 26:37:32,080 because if you really think about it a 35814 26:37:30,000 --> 26:37:33,911 circle is a shape a novel is the shape 35815 26:37:32,080 --> 26:37:36,720 and the shape is the shape so this is 35816 26:37:33,911 --> 26:37:38,551 going to work and when we get to loop 35817 26:37:36,720 --> 26:37:41,191 through this collection and call the 35818 26:37:38,551 --> 26:37:43,440 draw method for the first item here we 35819 26:37:41,191 --> 26:37:45,512 will call the draw method on the shape 35820 26:37:43,440 --> 26:37:48,960 for the second element here we will call 35821 26:37:45,512 --> 26:37:51,040 the joe method on the oval object and 35822 26:37:48,960 --> 26:37:54,000 for the third element here we will call 35823 26:37:51,039 --> 26:37:56,399 the draw method on our circle object and 35824 26:37:54,000 --> 26:37:59,119 we will draw a shape a novel and a 35825 26:37:56,399 --> 26:38:01,439 circle with just one loop and this is 35826 26:37:59,119 --> 26:38:03,679 really cool this is the design we want 35827 26:38:01,440 --> 26:38:06,232 now this is not the default behavior we 35828 26:38:03,679 --> 26:38:08,399 get in our c plus plus classes if we set 35829 26:38:06,232 --> 26:38:10,800 up an inheritance hierarchy 35830 26:38:08,399 --> 26:38:13,439 with our draw methods what we get by 35831 26:38:10,800 --> 26:38:16,639 default is static binding but we will 35832 26:38:13,440 --> 26:38:19,191 see how we can get dynamic binding or 35833 26:38:16,639 --> 26:38:20,872 polymorphism in the next lecture for 35834 26:38:19,191 --> 26:38:22,639 this one we're going to explore static 35835 26:38:20,872 --> 26:38:24,551 binding and we're going to head over to 35836 26:38:22,639 --> 26:38:27,191 visual studio code and play with us a 35837 26:38:24,551 --> 26:38:29,440 little more here we are in our working 35838 26:38:27,191 --> 26:38:32,231 folder the current project is static 35839 26:38:29,440 --> 26:38:35,040 binding with inheritance we are going to 35840 26:38:32,232 --> 26:38:37,360 grab our template files this is going to 35841 26:38:35,039 --> 26:38:40,231 be our starting point here we're going 35842 26:38:37,360 --> 26:38:42,872 to put in these files and i am going to 35843 26:38:40,232 --> 26:38:44,639 put in our classes to save time 35844 26:38:42,872 --> 26:38:47,192 if you want you can download these 35845 26:38:47,191 --> 26:38:50,799 resource section on this lecture you're 35846 26:38:48,960 --> 26:38:52,800 going to find them and you can use them 35847 26:38:50,800 --> 26:38:54,872 as a starting point this is going to 35848 26:38:52,800 --> 26:38:56,960 save us a few seconds because we don't 35849 26:38:54,872 --> 26:38:59,192 really want to type these classes you 35850 26:38:56,960 --> 26:39:02,232 already know how to create classes if 35851 26:38:59,191 --> 26:39:04,639 you made it this far in the course so i 35852 26:39:02,232 --> 26:39:07,040 am going to drag this and drop here to 35853 26:39:04,639 --> 26:39:08,720 open in visual studio code and this is 35854 26:39:07,039 --> 26:39:11,439 going to give us a good starting point 35855 26:39:08,720 --> 26:39:13,759 here we have a base class which is our 35856 26:39:11,440 --> 26:39:15,832 shape class it is going to have a simple 35857 26:39:13,759 --> 26:39:18,551 member variable which is going to be the 35858 26:39:15,831 --> 26:39:21,279 description one thing i think we need to 35859 26:39:18,551 --> 26:39:23,759 improve on this is to use a string view 35860 26:39:23,759 --> 26:39:28,399 so let's include string view we can 35861 26:39:26,639 --> 26:39:31,119 include that here we're going to say 35862 26:39:28,399 --> 26:39:32,871 include string view because we are using 35863 26:39:31,119 --> 26:39:36,479 modern c plus plus and we're going to 35864 26:39:32,872 --> 26:39:39,192 change this to be an sdd string view by 35865 26:39:36,479 --> 26:39:41,679 value we can do this okay so we have our 35866 26:39:39,191 --> 26:39:43,512 class here and we have a draw method 35867 26:39:41,679 --> 26:39:45,279 which is really not going to do anything 35868 26:39:43,512 --> 26:39:47,512 special it's going to just print the 35869 26:39:45,279 --> 26:39:50,720 description here and we're going to see 35870 26:39:47,512 --> 26:39:53,760 it printed out on the output stream 35871 26:39:50,720 --> 26:39:56,080 if we look at the cpp file we just have 35872 26:39:53,759 --> 26:39:58,720 an implementation of our constructor and 35873 26:39:56,080 --> 26:40:01,680 we have a destructor we're going to fix 35874 26:39:58,720 --> 26:40:05,191 our parameter here to make it an std 35875 26:40:01,679 --> 26:40:06,719 string view let's look at our oval class 35876 26:40:09,440 --> 26:40:13,760 and we have a bunch of member variables 35877 26:40:11,600 --> 26:40:16,160 here two to be exact one is going to be 35878 26:40:13,759 --> 26:40:19,360 the x radius the other is going to be 35879 26:40:16,160 --> 26:40:21,120 our y radius we have a constructor here 35880 26:40:19,360 --> 26:40:24,232 which is going to be taking three 35881 26:40:21,119 --> 26:40:28,399 parameters two parameters for the data 35882 26:40:24,232 --> 26:40:30,480 for our radius here and we have a 35883 26:40:28,399 --> 26:40:33,119 third parameter which is going to store 35884 26:40:30,479 --> 26:40:36,399 our description data we're going to also 35885 26:40:33,119 --> 26:40:38,959 change this to bstd string view and this 35886 26:40:36,399 --> 26:40:41,039 is going to do we have a draw method 35887 26:40:38,960 --> 26:40:44,480 which is going to be printing the 35888 26:40:41,039 --> 26:40:46,551 description and our radius data and this 35889 26:40:44,479 --> 26:40:50,231 is really it we don't need anything 35890 26:40:46,551 --> 26:40:51,831 fancy here let's look at our oval cpp 35891 26:40:50,232 --> 26:40:54,400 file it is going to have the 35892 26:40:51,831 --> 26:40:56,399 implementation for our constructor 35893 26:40:56,399 --> 26:41:01,911 we just need to change the type for our 35894 26:40:59,440 --> 26:41:04,400 description here and make this an sdd 35895 26:41:01,911 --> 26:41:06,959 string view let's do this and we will 35896 26:41:04,399 --> 26:41:10,000 look at our circle class which is going 35897 26:41:10,000 --> 26:41:14,232 publicly it won't have any member 35898 26:41:12,639 --> 26:41:18,000 variable because it's just going to 35899 26:41:14,232 --> 26:41:20,360 forward the data to the over portoverse 35900 26:41:18,000 --> 26:41:23,039 it doesn't really need to do any other 35901 26:41:20,360 --> 26:41:24,551 specialization but what we need is a 35902 26:41:23,039 --> 26:41:26,479 method which is going to return the 35903 26:41:24,551 --> 26:41:30,080 radius because we want to print that 35904 26:41:26,479 --> 26:41:33,039 here so we can head over in our oval 35905 26:41:30,080 --> 26:41:34,960 class and set up a protected section 35906 26:41:33,039 --> 26:41:36,959 which is going to give us these methods 35907 26:41:34,960 --> 26:41:39,440 here so i'm just going to put them in 35908 26:41:36,960 --> 26:41:41,680 here to save on some time it is 35909 26:41:39,440 --> 26:41:44,720 protected because we want this to be 35910 26:41:41,679 --> 26:41:47,511 usable in derived classes but we don't 35911 26:41:44,720 --> 26:41:49,600 want this usable from the outside this 35912 26:41:47,512 --> 26:41:52,479 is the setup we can achieve by making 35913 26:41:49,600 --> 26:41:55,512 these guys protected here get x rad is 35914 26:41:52,479 --> 26:41:57,831 going to return the x radius get y rad 35915 26:41:55,512 --> 26:42:00,080 is going to return the y radius they are 35916 26:41:57,831 --> 26:42:03,360 const because we don't intend for this 35917 26:42:00,080 --> 26:42:06,320 to modify our object and if we go in our 35918 26:42:03,360 --> 26:42:08,800 circle class this is going to work now 35919 26:42:08,800 --> 26:42:14,800 information on our circle object and we 35920 26:42:11,360 --> 26:42:17,039 will see on our output stream if we look 35921 26:42:14,800 --> 26:42:19,680 at the implementation of our circle 35922 26:42:17,039 --> 26:42:22,159 class it is nothing complicated it is 35923 26:42:19,679 --> 26:42:24,231 just going to take the double radius and 35924 26:42:22,160 --> 26:42:27,680 it is going to forward the data to the 35925 26:42:24,232 --> 26:42:29,832 oval part of us and our description here 35926 26:42:29,831 --> 26:42:34,799 to take into account that we want to use 35927 26:42:32,479 --> 26:42:38,959 std string view here so let's change 35928 26:42:34,800 --> 26:42:42,160 this to bhd string view we can do that 35929 26:42:38,960 --> 26:42:45,600 and we are going to head over in our cpp 35930 26:42:42,160 --> 26:42:48,552 file and change that as well std string 35931 26:42:45,600 --> 26:42:51,040 view you can do that and now our classes 35932 26:42:48,551 --> 26:42:54,479 are really complete we can head over in 35933 26:42:51,039 --> 26:42:56,159 our main cpp file we can clean that up a 35934 26:42:54,479 --> 26:42:58,080 little bit and we're going to remove 35935 26:42:58,080 --> 26:43:02,232 we are going to include our classes so 35936 26:43:00,160 --> 26:43:03,040 we're going to include shape let's do 35937 26:43:03,039 --> 26:43:09,279 we are going to put in oval let's do 35938 26:43:05,831 --> 26:43:11,439 that oval dot h and we are going to put 35939 26:43:09,279 --> 26:43:13,512 in circle and we are going to create 35940 26:43:11,440 --> 26:43:15,760 objects of these classes and try to 35941 26:43:13,512 --> 26:43:18,000 print their information so we're going 35942 26:43:15,759 --> 26:43:19,511 to set up a shape called the shape one 35943 26:43:18,000 --> 26:43:21,600 we're going to set up a novel called 35944 26:43:19,512 --> 26:43:23,360 oval one we're going to set up a circle 35945 26:43:21,600 --> 26:43:25,832 called circle one and we're going to see 35946 26:43:23,360 --> 26:43:28,720 what kind of information we see 35947 26:43:25,831 --> 26:43:30,639 if we try to call the job method again 35948 26:43:28,720 --> 26:43:33,512 in shape the draw method is just going 35949 26:43:30,639 --> 26:43:35,600 to say that we are calling shape joe 35950 26:43:33,512 --> 26:43:38,000 in alvo we are going to say that we are 35951 26:43:35,600 --> 26:43:40,639 drawing an oval and we're going to say 35952 26:43:38,000 --> 26:43:42,320 the description and the radius in circle 35953 26:43:40,639 --> 26:43:44,800 we're going to say that we are calling 35954 26:43:42,320 --> 26:43:47,120 the draw method on the circle object and 35955 26:43:44,800 --> 26:43:49,512 say the description and the radius here 35956 26:43:47,119 --> 26:43:52,871 this is the information we want so we 35957 26:43:49,512 --> 26:43:55,680 are going to try and build this program 35958 26:43:52,872 --> 26:43:57,760 using gcc our favorite compiler you can 35959 26:43:55,679 --> 26:44:00,079 really use any compiler you have your 35960 26:43:57,759 --> 26:44:02,479 hands on but we are going to use gcc 35961 26:44:00,080 --> 26:44:05,512 here because it is our favorite and 35962 26:44:02,479 --> 26:44:08,231 we're going to have a bunch of errors 35963 26:44:05,512 --> 26:44:10,320 what it is that we have here what is it 35964 26:44:08,232 --> 26:44:11,360 that we have here let's go in shape 35965 26:44:11,360 --> 26:44:17,279 at line 4 and see what we have and we 35966 26:44:14,960 --> 26:44:19,040 have a j here that we don't want let's 35967 26:44:17,279 --> 26:44:20,872 take this out and we're going to build 35968 26:44:19,039 --> 26:44:23,511 it and we're going to pass this through 35969 26:44:20,872 --> 26:44:26,080 gcc and the bolt is going to be good we 35970 26:44:23,512 --> 26:44:28,639 can bring up a powershell window to run 35971 26:44:26,080 --> 26:44:30,960 our program let's run rooster and we're 35972 26:44:28,639 --> 26:44:33,831 going to see that shape draw was called 35973 26:44:30,960 --> 26:44:36,720 drawing shape one you see we have just 35974 26:44:33,831 --> 26:44:38,799 created shape one here and we are 35975 26:44:38,800 --> 26:44:43,832 we are going to call the oval drill 35976 26:44:41,360 --> 26:44:45,600 method and we're going to print 35977 26:44:43,831 --> 26:44:48,000 the information on that you see that it 35978 26:44:45,600 --> 26:44:50,960 is over one here and we're going to put 35979 26:44:48,000 --> 26:44:54,080 out our radius data x radius is going to 35980 26:44:50,960 --> 26:44:56,232 be 2 as we specified here y radius is 35981 26:44:54,080 --> 26:44:58,800 going to be 3.5 and this is our 35982 26:44:56,232 --> 26:45:01,120 information we are also going to call 35983 26:44:58,800 --> 26:45:03,600 the draw method on our circle object 35984 26:45:01,119 --> 26:45:05,759 this is what we get and we're going to 35985 26:45:03,600 --> 26:45:08,320 say we are drawing circle 1 and the 35986 26:45:05,759 --> 26:45:11,439 radius is going to be 3 3 this is our 35987 26:45:08,320 --> 26:45:14,160 information here but we don't want to go 35988 26:45:11,440 --> 26:45:16,800 through direct objects to manage our 35989 26:45:14,160 --> 26:45:20,320 data here what we want is to either go 35990 26:45:16,800 --> 26:45:22,232 through base pointers or base references 35991 26:45:20,320 --> 26:45:24,160 so we are going to set up a shape 35992 26:45:22,232 --> 26:45:26,480 pointer let's do that we're going to say 35993 26:45:24,160 --> 26:45:27,832 shift because that's our base class and 35994 26:45:27,831 --> 26:45:33,360 shape ptr to represent our pointer and 35995 26:45:30,960 --> 26:45:35,512 we're going to store in the address of 35996 26:45:33,360 --> 26:45:37,360 shape one we can do something like this 35997 26:45:35,512 --> 26:45:39,832 and that's really see what is happening 35998 26:45:37,360 --> 26:45:42,160 we're going to comment out our calls to 35999 26:45:39,831 --> 26:45:46,231 draw methods here and we're going to see 36000 26:45:42,160 --> 26:45:48,320 what happens so if we say shape ptr and 36001 26:45:46,232 --> 26:45:50,480 call the draw method we can do that 36002 26:45:48,320 --> 26:45:52,800 let's try and build our program 36003 26:45:50,479 --> 26:45:54,231 we're going to pass this through gcc 36004 26:45:52,800 --> 26:45:56,080 you're going to see that it is going to 36005 26:45:54,232 --> 26:45:58,400 work and it is going to do what we 36006 26:45:56,080 --> 26:46:00,639 expect if we run this program here you 36007 26:45:58,399 --> 26:46:03,360 can clear and run rooster we're going to 36008 26:46:00,639 --> 26:46:05,440 see that shape draw was called and we 36009 26:46:03,360 --> 26:46:08,232 are going to be drawing shape one here 36010 26:46:05,440 --> 26:46:11,191 this is what we expect but if we go down 36011 26:46:08,232 --> 26:46:13,680 and say we don't want to call the draw 36012 26:46:11,191 --> 26:46:15,759 method here we are going to store in 36013 26:46:13,679 --> 26:46:19,439 let's say shape ptr and we're going to 36014 26:46:15,759 --> 26:46:20,479 store in the address of our over one 36015 26:46:20,479 --> 26:46:26,231 and we want to call the draw method here 36016 26:46:23,831 --> 26:46:28,319 now if you do this as we saw in the 36017 26:46:28,320 --> 26:46:35,120 is to get the draw method on our actual 36018 26:46:32,160 --> 26:46:37,040 oval object called and if we will for 36019 26:46:35,119 --> 26:46:39,191 example drawing things on the screen we 36020 26:46:37,039 --> 26:46:41,039 would draw a novel this is what we want 36021 26:46:39,191 --> 26:46:42,720 here but this is not what we're going to 36022 26:46:41,039 --> 26:46:43,831 get you're going to see that we still 36023 26:46:43,831 --> 26:46:48,399 job method from the shape class called 36024 26:46:46,551 --> 26:46:51,679 and it is going to and it is going to 36025 26:46:48,399 --> 26:46:53,599 say drawing over one let's try and build 36026 26:46:51,679 --> 26:46:55,911 this program and i really want you to 36027 26:46:53,600 --> 26:46:57,912 see this so the builder is good we can 36028 26:46:55,911 --> 26:46:59,360 clear and run rooster you're going to 36029 26:46:59,360 --> 26:47:03,911 row even if we are storing again 36030 26:47:03,911 --> 26:47:09,279 object so this is the default behavior 36031 26:47:07,119 --> 26:47:12,639 we get and this is static binding that 36032 26:47:09,279 --> 26:47:15,039 we get by default in our inheritance 36033 26:47:12,639 --> 26:47:16,639 hierarchies so what the compiler is 36034 26:47:15,039 --> 26:47:19,039 really going to do here it's going to 36035 26:47:16,639 --> 26:47:21,360 look at the type of the pointer that we 36036 26:47:19,039 --> 26:47:24,399 have here it is going to say i have a 36037 26:47:21,360 --> 26:47:26,872 shape pointer so i am going to call the 36038 26:47:24,399 --> 26:47:29,279 jaw method on the shape type that i have 36039 26:47:26,872 --> 26:47:30,080 in my pointer here and we are going to 36040 26:47:30,080 --> 26:47:35,680 that this is calling the draw method on 36041 26:47:32,479 --> 26:47:38,319 our shape and this is not what we want 36042 26:47:35,679 --> 26:47:41,119 in most cases we are going to see that 36043 26:47:38,320 --> 26:47:43,512 we get the same behavior if we store an 36044 26:47:41,119 --> 26:47:45,440 a circle object let's do that we're 36045 26:47:43,512 --> 26:47:48,232 going to say shift pointer and we're 36046 26:47:45,440 --> 26:47:50,479 going to store in the address of our 36047 26:47:48,232 --> 26:47:53,760 circle one and then we're going to call 36048 26:47:50,479 --> 26:47:57,191 our draw method on our shape pointer 36049 26:47:53,759 --> 26:47:59,679 again this is going to draw a shape it's 36050 26:47:57,191 --> 26:48:02,231 not going to draw a circle that we 36051 26:47:59,679 --> 26:48:04,479 really are managing with our base 36052 26:48:02,232 --> 26:48:05,600 pointer here and some of you must be 36053 26:48:05,600 --> 26:48:11,680 why are we able to store circle pointers 36054 26:48:09,191 --> 26:48:14,319 for example in a shape well a circle is 36055 26:48:11,679 --> 26:48:16,959 a shape according to our inheritance 36056 26:48:14,320 --> 26:48:19,120 hierarchy if we take a closer look again 36057 26:48:16,960 --> 26:48:22,960 here you're going to see that oval is 36058 26:48:19,119 --> 26:48:25,599 going to be inheriting from shape 36059 26:48:22,960 --> 26:48:28,639 and the circle is going to be inheriting 36060 26:48:25,600 --> 26:48:30,000 from oval so from what we learned in the 36061 26:48:30,000 --> 26:48:36,000 oval has a shape part in it and the 36062 26:48:32,872 --> 26:48:38,551 circle has a novel part in it which in 36063 26:48:36,000 --> 26:48:40,720 turn is going to have a shaped board in 36064 26:48:38,551 --> 26:48:43,279 it so from this understanding it 36065 26:48:40,720 --> 26:48:45,759 shouldn't really be surprising that we 36066 26:48:45,759 --> 26:48:52,000 novel object with a shape pointer or the 36067 26:48:49,440 --> 26:48:53,040 fact that we are able to manage a circle 36068 26:48:53,039 --> 26:48:58,319 using a shape pointer this is why we are 36069 26:48:55,600 --> 26:49:00,800 able to do something like this okay so i 36070 26:48:58,320 --> 26:49:03,280 hope this is super clear but again we 36071 26:49:00,800 --> 26:49:05,512 are not getting the default behavior 36072 26:49:03,279 --> 26:49:08,319 that we want but we will see how we can 36073 26:49:05,512 --> 26:49:10,720 achieve that in the next lecture for now 36074 26:49:08,320 --> 26:49:13,120 i really want you to understand what 36075 26:49:10,720 --> 26:49:15,119 static binding is all about and it is 36076 26:49:13,119 --> 26:49:17,679 the fact that the compiler is going to 36077 26:49:15,119 --> 26:49:20,319 look at the type of the pointer here and 36078 26:49:17,679 --> 26:49:21,831 use that to decide which method it is 36079 26:49:21,831 --> 26:49:25,599 in our inheritance hierarchy the 36080 26:49:24,160 --> 26:49:28,400 compiler for example is going to see 36081 26:49:25,600 --> 26:49:31,192 that we have a circle object stored n at 36082 26:49:28,399 --> 26:49:34,479 lineup 25 here but when it wants to 36083 26:49:31,191 --> 26:49:36,399 decide which joe version to call it is 36084 26:49:34,479 --> 26:49:38,800 going to look at the type of this 36085 26:49:36,399 --> 26:49:41,360 pointer here and it is going to use that 36086 26:49:38,800 --> 26:49:43,120 to decide which draw method to call and 36087 26:49:41,360 --> 26:49:45,512 in this case it is going to call the 36088 26:49:43,119 --> 26:49:48,231 shape version because the type of this 36089 26:49:45,512 --> 26:49:50,639 pointer is shape okay so i really hope 36090 26:49:48,232 --> 26:49:52,800 this is super clear here we are going to 36091 26:49:50,639 --> 26:49:55,831 get the same behavior if we try to 36092 26:49:52,800 --> 26:49:58,160 manage our objects using references for 36093 26:49:55,831 --> 26:49:59,679 example if we go through a shape 36094 26:49:58,160 --> 26:50:02,400 reference let's do that we're going to 36095 26:49:59,679 --> 26:50:04,639 say shape reference and say shape ref 36096 26:50:02,399 --> 26:50:06,959 and we are going to store in shape one 36097 26:50:06,960 --> 26:50:13,040 and we want to call the draw method on 36098 26:50:09,911 --> 26:50:15,440 ship reference let's do that so let's 36099 26:50:13,039 --> 26:50:18,079 call the joe method and you're going to 36100 26:50:15,440 --> 26:50:19,040 see that we will get the shape method 36101 26:50:19,039 --> 26:50:23,119 and this is really making sense because 36102 26:50:23,119 --> 26:50:27,191 managed by this reference here so if we 36103 26:50:25,512 --> 26:50:29,360 build our program let's do that we're 36104 26:50:27,191 --> 26:50:31,039 going to pass this through gcc 36105 26:50:29,360 --> 26:50:32,800 you're going to see that we get what we 36106 26:50:31,039 --> 26:50:35,191 expect the build is going to be good 36107 26:50:32,800 --> 26:50:37,680 we're going to clear and run rooster and 36108 26:50:35,191 --> 26:50:40,720 we see shape drill called this is what 36109 26:50:37,679 --> 26:50:43,119 we want but if we use this reference to 36110 26:50:40,720 --> 26:50:45,680 manage derived object for example let's 36111 26:50:43,119 --> 26:50:48,551 put in over one we can do that we're 36112 26:50:45,679 --> 26:50:51,759 going to see that we don't get the most 36113 26:50:48,551 --> 26:50:54,399 specific draw method for oval here 36114 26:50:51,759 --> 26:50:56,319 called we will still get the method 36115 26:50:54,399 --> 26:50:59,039 for a shape called let's build and show 36116 26:50:56,320 --> 26:51:01,280 you this this is static binding again 36117 26:50:59,039 --> 26:51:02,799 okay the build is good we can clear and 36118 26:51:01,279 --> 26:51:05,831 run rooster we're going to see that 36119 26:51:02,800 --> 26:51:08,872 shape draw it is drawing we don't get 36120 26:51:05,831 --> 26:51:10,959 the ovo draw method called we get shape 36121 26:51:08,872 --> 26:51:12,960 drill method again the compiler is going 36122 26:51:10,960 --> 26:51:15,192 to look at the type of this reference by 36123 26:51:12,960 --> 26:51:17,832 default and it is going to use that to 36124 26:51:15,191 --> 26:51:20,319 decide which draw method to call if we 36125 26:51:17,831 --> 26:51:23,279 even put in a circle we are going to get 36126 26:51:20,320 --> 26:51:25,512 the same behavior let's build with gcc 36127 26:51:23,279 --> 26:51:27,911 the bolt is good we can clear and run 36128 26:51:25,512 --> 26:51:29,512 rooster we see that shape draw is called 36129 26:51:29,512 --> 26:51:34,232 what we want okay so this is static 36130 26:51:31,831 --> 26:51:36,959 binding and it is the default behavior 36131 26:51:34,232 --> 26:51:40,320 we get if we have an inheritance 36132 26:51:36,960 --> 26:51:43,192 hierarchy with the same method spread 36133 26:51:40,320 --> 26:51:45,600 across each derived class we're going to 36134 26:51:43,191 --> 26:51:47,911 get the shape version called even if 36135 26:51:45,600 --> 26:51:50,480 what we are really managing with a shape 36136 26:51:47,911 --> 26:51:52,479 pointer or a shape reference is a 36137 26:51:50,479 --> 26:51:55,119 derived object i really want you to 36138 26:51:52,479 --> 26:51:58,000 understand this now let's see why this 36139 26:51:55,119 --> 26:52:00,399 is really badly designed by default in c 36140 26:51:58,000 --> 26:52:02,872 plus plus if you happen to need to pass 36141 26:52:00,399 --> 26:52:04,799 ship parameters to functions for example 36142 26:52:02,872 --> 26:52:06,960 we might do something like this we might 36143 26:52:04,800 --> 26:52:08,960 need a function to draw a circle we 36144 26:52:06,960 --> 26:52:11,680 might need a function to draw a novel 36145 26:52:08,960 --> 26:52:14,080 and if you happen to have 100 objects 36146 26:52:11,679 --> 26:52:17,191 you can draw in your program you have no 36147 26:52:14,080 --> 26:52:19,512 choice but to set up 100 drawing methods 36148 26:52:17,191 --> 26:52:21,599 or functions this is really bad design 36149 26:52:19,512 --> 26:52:23,680 you don't want to do something like this 36150 26:52:21,600 --> 26:52:25,832 so what you would do in your program for 36151 26:52:27,679 --> 26:52:32,799 and you would say for example draw 36152 26:52:29,759 --> 26:52:35,279 circle and pass in a circle and say draw 36153 26:52:32,800 --> 26:52:37,512 oval do we have a draw or full function 36154 26:52:35,279 --> 26:52:40,800 let's see yes we have that we can go 36155 26:52:37,512 --> 26:52:43,279 down and say draw oval and pass in our 36156 26:52:40,800 --> 26:52:44,872 oval one this is going to work just fine 36157 26:52:43,279 --> 26:52:47,600 but hopefully you can see that this is 36158 26:52:44,872 --> 26:52:50,000 bad design if you have a lot of shapes 36159 26:52:47,600 --> 26:52:52,232 in your program this is going to quickly 36160 26:52:50,000 --> 26:52:54,800 become and manageable and you don't want 36161 26:52:52,232 --> 26:52:56,720 to do something like this what we want 36162 26:52:56,720 --> 26:53:02,080 to only have one method and get to the c 36163 26:52:59,759 --> 26:53:05,119 plus plus runtime to resolve which one 36164 26:53:02,080 --> 26:53:08,080 is called at runtime when we get to pass 36165 26:53:05,119 --> 26:53:10,399 our actual objects and we will only have 36166 26:53:08,080 --> 26:53:13,191 one function which is going to take into 36167 26:53:10,399 --> 26:53:14,959 account hundreds of shapes that we might 36168 26:53:13,191 --> 26:53:17,360 potentially have in our c plus plus 36169 26:53:14,960 --> 26:53:19,512 program this is going to be much easier 36170 26:53:17,360 --> 26:53:22,160 to manage and we will actually be able 36171 26:53:19,512 --> 26:53:24,551 to achieve this in the next lecture so 36172 26:53:22,160 --> 26:53:26,872 stay tuned for that another problem with 36173 26:53:24,551 --> 26:53:29,440 us is if you happen to need to store 36174 26:53:26,872 --> 26:53:31,120 these shapes in collections if you need 36175 26:53:29,440 --> 26:53:33,600 to set up something like this you will 36176 26:53:31,119 --> 26:53:36,799 have no choice but to set up different 36177 26:53:33,600 --> 26:53:39,120 collections for each type for example we 36178 26:53:36,800 --> 26:53:41,760 might set up a collection for a circle a 36179 26:53:39,119 --> 26:53:44,479 collection for oval and here we are just 36180 26:53:41,759 --> 26:53:46,399 using arrays to model our collections 36181 26:53:44,479 --> 26:53:49,119 this is really bad design if you happen 36182 26:53:46,399 --> 26:53:52,551 to have 100 shapes in your program you 36183 26:53:49,119 --> 26:53:54,399 will set up 100 such collections and the 36184 26:53:52,551 --> 26:53:56,319 only difference is that they are just 36185 26:53:54,399 --> 26:53:58,000 different types and this is really bad 36186 26:53:56,320 --> 26:54:00,640 design because even if they are 36187 26:53:58,000 --> 26:54:02,720 different types they are related and 36188 26:54:00,639 --> 26:54:05,191 they are all shapes in our c plus plus 36189 26:54:02,720 --> 26:54:07,191 program and what we really want is to 36190 26:54:05,191 --> 26:54:09,599 set up one collection which is going to 36191 26:54:07,191 --> 26:54:11,599 take into account all these kinds of 36192 26:54:09,600 --> 26:54:14,000 shapes that we might have in our c plus 36193 26:54:11,600 --> 26:54:16,480 plus program and we will be able to 36194 26:54:14,000 --> 26:54:18,320 achieve this again in the next lecture 36195 26:54:16,479 --> 26:54:20,639 but the whole point of this lecture was 36196 26:54:18,320 --> 26:54:22,800 to show you static binding that we get 36197 26:54:20,639 --> 26:54:26,000 by default with our inheritance 36198 26:54:22,800 --> 26:54:29,279 hierarchies static binding may be bad if 36199 26:54:26,000 --> 26:54:31,679 you really are managing a lot of related 36200 26:54:29,279 --> 26:54:34,319 objects in your c plus plus program 36201 26:54:31,679 --> 26:54:35,359 and all these objects have the same 36202 26:54:35,360 --> 26:54:40,800 that each derived class is specializing 36203 26:54:38,399 --> 26:54:43,911 but you want the most specialized method 36204 26:54:40,800 --> 26:54:46,800 to be called on the actual object that 36205 26:54:43,911 --> 26:54:48,959 you are managing with a base pointer or 36206 26:54:46,800 --> 26:54:51,440 a base reference hopefully this is super 36207 26:54:48,960 --> 26:54:54,000 clear i apologize this lecture turned 36208 26:54:51,440 --> 26:54:56,479 out to be really long but i wanted you 36209 26:54:54,000 --> 26:54:58,551 to make this super clear and everything 36210 26:54:56,479 --> 26:55:00,639 else we do in the reminder of this 36211 26:54:58,551 --> 26:55:02,639 chapter is really going to fall in place 36212 26:55:00,639 --> 26:55:04,872 so make sure you understand this and if 36213 26:55:02,639 --> 26:55:07,039 you have a problem do you ask me i will 36214 26:55:04,872 --> 26:55:08,639 do the best i can to help you out this 36215 26:55:07,039 --> 26:55:10,871 is really all i had to share in this 36216 26:55:08,639 --> 26:55:12,720 lecture i hope you found it interesting 36217 26:55:10,872 --> 26:55:14,400 we are going to stop here in this 36218 26:55:12,720 --> 26:55:16,872 lecture in the next one we're going to 36219 26:55:14,399 --> 26:55:20,079 show you how you can achieve dynamic 36220 26:55:16,872 --> 26:55:21,192 binding using virtual functions in c 36221 26:55:21,191 --> 26:55:25,512 go ahead and finish up here and meet me 36222 26:55:23,440 --> 26:55:28,800 there in this lecture we're going to see 36223 26:55:25,512 --> 26:55:30,551 how we can achieve dynamic binding or 36224 26:55:30,551 --> 26:55:35,279 using virtual functions in our 36225 26:55:32,960 --> 26:55:37,912 inheritance hierarchies in the last 36226 26:55:35,279 --> 26:55:40,319 lecture we had a hierarchy like this 36227 26:55:37,911 --> 26:55:43,279 where the base class was shaped we had 36228 26:55:40,320 --> 26:55:45,832 an overclass which was publicly deriving 36229 26:55:43,279 --> 26:55:48,000 or inheriting from shape and we had a 36230 26:55:45,831 --> 26:55:50,231 circle class which was publicly 36231 26:55:48,000 --> 26:55:53,279 inherited from over and the problem we 36232 26:55:50,232 --> 26:55:56,480 had was that if we happen to be managing 36233 26:55:56,479 --> 26:56:02,080 shape pointers or shape references we 36234 26:55:59,600 --> 26:56:04,160 don't get the correct methods called if 36235 26:56:04,160 --> 26:56:09,440 on our base pointer or base reference 36236 26:56:07,512 --> 26:56:12,160 and this was really bad and this is 36237 26:56:09,440 --> 26:56:14,400 something we want in practical c plus 36238 26:56:12,160 --> 26:56:16,400 plus programs i am here to tell you in 36239 26:56:14,399 --> 26:56:17,191 this lecture that you can just achieve 36240 26:56:17,191 --> 26:56:22,639 by doing a tiny modification on your 36241 26:56:20,160 --> 26:56:25,192 inheritance hierarchy all you need to do 36242 26:56:22,639 --> 26:56:27,600 is to mark the methods you need to be 36243 26:56:27,600 --> 26:56:33,832 virtual and we do that by inserting this 36244 26:56:30,639 --> 26:56:35,440 virtual keyword in front of our function 36245 26:56:33,831 --> 26:56:37,599 so we're going to do that on our shape 36246 26:56:35,440 --> 26:56:40,000 class we're going to do that on our over 36247 26:56:37,600 --> 26:56:42,800 class and we're going to do that on our 36248 26:56:40,000 --> 26:56:45,279 circle class once we do this the c plus 36249 26:56:42,800 --> 26:56:47,832 plus compiler is going to know i want to 36250 26:56:45,279 --> 26:56:50,160 be looking at the type of the pointer or 36251 26:56:47,831 --> 26:56:53,360 reference to know which kind of method 36252 26:56:50,160 --> 26:56:55,832 to call and my inheritance hierarchy if 36253 26:56:53,360 --> 26:56:58,639 i happen to be using a base pointer or a 36254 26:56:55,831 --> 26:57:00,399 base reference to manage a derived 36255 26:56:58,639 --> 26:57:02,800 object and we're going to be making this 36256 26:57:00,399 --> 26:57:04,639 super clear in a minute and if you are 36257 26:57:02,800 --> 26:57:06,639 lost please bear with me you're going to 36258 26:57:04,639 --> 26:57:10,080 understand so let's do this we're going 36259 26:57:06,639 --> 26:57:12,232 to modify our shape class and change our 36260 26:57:12,232 --> 26:57:16,800 as you see here we're going to head over 36261 26:57:14,479 --> 26:57:18,959 in our oval class we're going to modify 36262 26:57:16,800 --> 26:57:21,760 our draw method and mark it to be 36263 26:57:18,960 --> 26:57:24,080 virtual using the virtual keyword here 36264 26:57:21,759 --> 26:57:27,911 and we're going to move in our circle 36265 26:57:24,080 --> 26:57:29,759 class and change it to be virtual as you 36266 26:57:27,911 --> 26:57:30,720 see down here this is really all we need 36267 26:57:30,720 --> 26:57:36,160 from what we had in the last lecture and 36268 26:57:33,600 --> 26:57:38,160 this is going to be magic let's look at 36269 26:57:36,160 --> 26:57:41,040 what we can achieve now we're going to 36270 26:57:38,160 --> 26:57:43,192 set up three objects shape oval and 36271 26:57:41,039 --> 26:57:45,119 circle as you see here and we're going 36272 26:57:43,191 --> 26:57:46,959 to be calling our method which is going 36273 26:57:45,119 --> 26:57:50,000 to be taking references and we're going 36274 26:57:46,960 --> 26:57:52,552 to see that we get the correct most 36275 26:57:50,000 --> 26:57:55,279 specific method called so for example 36276 26:57:52,551 --> 26:57:57,512 here we will call the shape joe down 36277 26:57:55,279 --> 26:58:00,479 here we will call the oval draw method 36278 26:57:57,512 --> 26:58:02,720 because we just passed in a novel object 36279 26:58:00,479 --> 26:58:05,360 and on the third line here we will get 36280 26:58:02,720 --> 26:58:07,680 the circle joe method called and we will 36281 26:58:05,360 --> 26:58:09,360 see it printed out on the console this 36282 26:58:07,679 --> 26:58:12,000 is really cool this is what we want to 36283 26:58:09,360 --> 26:58:14,800 achieve if we want to call our methods 36284 26:58:12,000 --> 26:58:16,872 that take pointers we can do that and on 36285 26:58:14,800 --> 26:58:19,192 the first line here we will call shape 36286 26:58:16,872 --> 26:58:21,832 draw on the second line here we will 36287 26:58:19,191 --> 26:58:24,080 call overdraw and on the third line here 36288 26:58:21,831 --> 26:58:26,319 we will call circuit draw this is what 36289 26:58:24,080 --> 26:58:29,119 we want to achieve and if we happen to 36290 26:58:26,320 --> 26:58:31,120 be setting up a roll base pointer like 36291 26:58:31,119 --> 26:58:36,799 a derived object like this or another 36292 26:58:34,399 --> 26:58:39,439 way to say this if we happen to be using 36293 26:58:36,800 --> 26:58:42,160 this base pointer to manage a derived 36294 26:58:39,440 --> 26:58:44,800 object and we call the draw method on 36295 26:58:42,160 --> 26:58:47,279 the base pointer the compiler is going 36296 26:58:44,800 --> 26:58:50,720 to know that it will need to resolve 36297 26:58:47,279 --> 26:58:53,759 this draw method dynamically the reason 36298 26:58:50,720 --> 26:58:56,000 is this is a virtual method and if a 36299 26:58:53,759 --> 26:58:56,720 method is virtual the compiler want to 36300 26:58:56,720 --> 26:59:00,320 static binding like we did in the last 36301 26:58:58,800 --> 26:59:04,400 lecture the compiler is going to be 36302 26:59:00,320 --> 26:59:06,160 doing dynamic binding and with dynamic 36303 26:59:04,399 --> 26:59:08,551 binding the compiler is not going to 36304 26:59:06,160 --> 26:59:11,279 look at the type of the pointer it is 36305 26:59:08,551 --> 26:59:14,551 going to look at the type of the actual 36306 26:59:11,279 --> 26:59:16,872 object that the pointer is managing and 36307 26:59:14,551 --> 26:59:19,039 this is going to give us the behavior 36308 26:59:16,872 --> 26:59:20,800 that we wanted starting from the last 36309 26:59:19,039 --> 26:59:22,231 lecture okay now that you know this 36310 26:59:20,800 --> 26:59:24,080 we're going to head over to visual 36311 26:59:22,232 --> 26:59:26,960 studio code and play with us a little 36312 26:59:24,080 --> 26:59:29,759 more here we are in our working folder 36313 26:59:26,960 --> 26:59:32,320 the current project is polymorphism with 36314 26:59:29,759 --> 26:59:34,319 virtual functions we are going to be 36315 26:59:32,320 --> 26:59:35,680 reusing the code from the last lecture 36316 26:59:34,320 --> 26:59:37,440 because that's going to give us 36317 26:59:39,600 --> 26:59:43,832 binary file here which is rooster and 36318 26:59:42,160 --> 26:59:46,000 we're going to copy everything we want 36319 26:59:43,831 --> 26:59:48,479 we're going to put that in the current 36320 26:59:46,000 --> 26:59:51,119 project and we're going to open this in 36321 26:59:48,479 --> 26:59:54,080 visual studio code we are going to have 36322 26:59:51,119 --> 26:59:55,512 our main cpp file with a bunch of 36323 26:59:55,512 --> 27:00:01,119 and we will just need to modify this a 36324 26:59:58,232 --> 27:00:03,600 little bit to achieve dynamic binding as 36325 27:00:01,119 --> 27:00:06,479 we saw in the slides all we need to do 36326 27:00:03,600 --> 27:00:08,720 is to mark all these draw methods as 36327 27:00:06,479 --> 27:00:11,599 virtual so we're going to say virtual 36328 27:00:08,720 --> 27:00:14,000 void draw in the shape class we're going 36329 27:00:11,600 --> 27:00:15,760 to go in the oval class and do that 36330 27:00:14,000 --> 27:00:18,320 we're going to mark our method as 36331 27:00:15,759 --> 27:00:21,511 virtual and we are going to go in our 36332 27:00:18,320 --> 27:00:23,680 circle class and mark our draw method as 36333 27:00:21,512 --> 27:00:25,760 virtual let's do that and at the moment 36334 27:00:23,679 --> 27:00:29,191 we do this we're going to get dynamic 36335 27:00:25,759 --> 27:00:31,360 binding behavior in our inheritance 36336 27:00:29,191 --> 27:00:34,159 hierarchy here and this is really cool 36337 27:00:31,360 --> 27:00:36,720 so let's go back and try to play with 36338 27:00:34,160 --> 27:00:39,512 what we had before i am going to comment 36339 27:00:36,720 --> 27:00:42,551 out everything we had and we're going to 36340 27:00:39,512 --> 27:00:45,360 be doing things in step here let's go 36341 27:00:42,551 --> 27:00:46,399 through the code that uses base pointers 36342 27:00:46,399 --> 27:00:49,831 that's where we started out from in the 36343 27:00:52,320 --> 27:00:57,760 mark that here let's put in some space 36344 27:00:54,399 --> 27:01:00,319 to make it easy to follow so we are 36345 27:00:57,759 --> 27:01:02,551 going to uncomment the line here that 36346 27:01:00,320 --> 27:01:05,600 calls the draw method when we have a 36347 27:01:02,551 --> 27:01:08,000 shape object stored in our base pointer 36348 27:01:05,600 --> 27:01:10,552 or when we are using a base pointer to 36349 27:01:08,000 --> 27:01:12,639 manage a shape object here we can also 36350 27:01:10,551 --> 27:01:15,039 comment out all these lines because we 36351 27:01:12,639 --> 27:01:17,679 don't want this to do anything 36352 27:01:15,039 --> 27:01:20,231 and let's see yes this is what we want 36353 27:01:17,679 --> 27:01:22,719 we are setting up our shapes and we are 36354 27:01:20,232 --> 27:01:25,279 using a base pointer to manage an actual 36355 27:01:22,720 --> 27:01:27,512 shape object and if we call the draw 36356 27:01:25,279 --> 27:01:29,440 method we expect this to call 36357 27:01:27,512 --> 27:01:31,119 shape draw and this is what we're going 36358 27:01:29,440 --> 27:01:33,832 to get let's build our program we're 36359 27:01:31,119 --> 27:01:35,759 going to pass this through gcc our 36360 27:01:33,831 --> 27:01:38,159 favorite compiler the build is going to 36361 27:01:35,759 --> 27:01:40,319 be good we can bring up a powershell 36362 27:01:38,160 --> 27:01:42,000 window we can use to play with us if we 36363 27:01:40,320 --> 27:01:44,400 call rooster we're going to see that 36364 27:01:42,000 --> 27:01:46,551 shape draw is called and we are drawing 36365 27:01:44,399 --> 27:01:49,360 shape one now if we change this to 36366 27:01:46,551 --> 27:01:51,440 manage an actual oval object we can do 36367 27:01:49,360 --> 27:01:54,960 that we can do that through this line 36368 27:01:51,440 --> 27:01:57,279 here and we expect this to call overdraw 36369 27:01:54,960 --> 27:02:00,160 and because the draw method is now 36370 27:01:57,279 --> 27:02:02,479 virtual this will use dynamic binding 36371 27:02:00,160 --> 27:02:04,720 again with dynamic binding we are 36372 27:02:02,479 --> 27:02:07,599 telling the compiler don't look at the 36373 27:02:04,720 --> 27:02:10,551 type of the base pointer look at the 36374 27:02:07,600 --> 27:02:12,480 type of the actual object that the 36375 27:02:10,551 --> 27:02:14,639 pointer is managing and the compiler is 36376 27:02:12,479 --> 27:02:17,279 going to see that this object is a novel 36377 27:02:17,279 --> 27:02:22,160 the oval version of the draw method here 36378 27:02:20,639 --> 27:02:24,720 and this is really cool this is what we 36379 27:02:22,160 --> 27:02:27,192 want let's run the task to build with 36380 27:02:24,720 --> 27:02:29,360 gcc again the world is going to be good 36381 27:02:27,191 --> 27:02:31,831 as you see here we can clear 36382 27:02:29,360 --> 27:02:34,232 and if we run booster look at this 36383 27:02:31,831 --> 27:02:37,679 overdraw calls and we are going to be 36384 27:02:34,232 --> 27:02:39,680 drawing our oval object and this is the 36385 27:02:37,679 --> 27:02:42,319 data we have inside we can use this 36386 27:02:39,679 --> 27:02:44,639 radius data to draw this on the screen 36387 27:02:42,320 --> 27:02:46,400 if that's what we are after in our c 36388 27:02:44,639 --> 27:02:49,279 plus plus program and this is really 36389 27:02:46,399 --> 27:02:52,159 cool we can even use our base pointer to 36390 27:02:49,279 --> 27:02:54,232 manage a circle object we can do that 36391 27:02:52,160 --> 27:02:57,360 and if we do that and call the joe 36392 27:02:54,232 --> 27:03:00,000 method because this draw method is now 36393 27:02:57,360 --> 27:03:02,551 virtual the compiler will note that it 36394 27:03:00,000 --> 27:03:05,512 doesn't need to look at the type of the 36395 27:03:02,551 --> 27:03:06,399 pointer that we are using to manage our 36396 27:03:06,399 --> 27:03:11,191 the compiler is going to look at the 36397 27:03:08,160 --> 27:03:13,512 type of the actual object that the base 36398 27:03:11,191 --> 27:03:16,000 pointer is managing and in this case it 36399 27:03:13,512 --> 27:03:19,191 happens to be a circle object so here we 36400 27:03:16,000 --> 27:03:21,600 will call circle draw and this is going 36401 27:03:19,191 --> 27:03:23,679 to draw a circle on the screen and this 36402 27:03:21,600 --> 27:03:26,232 is really going to be super cool so 36403 27:03:26,232 --> 27:03:29,760 and we're going to see this in action 36404 27:03:28,232 --> 27:03:32,160 the build is going to be good we can 36405 27:03:29,759 --> 27:03:34,551 clear and run rooster and you see that 36406 27:03:32,160 --> 27:03:37,120 we are drawing a circle and this is 36407 27:03:34,551 --> 27:03:40,000 really cool and just using virtual 36408 27:03:37,119 --> 27:03:42,231 functions we can really achieve powerful 36409 27:03:40,000 --> 27:03:45,039 features and you see that this wasn't 36410 27:03:42,232 --> 27:03:48,872 really hard to achieve all we had to do 36411 27:03:45,039 --> 27:03:50,871 was to mark our inherited methods as a 36412 27:03:48,872 --> 27:03:53,832 virtual and this is going to tell the 36413 27:03:50,872 --> 27:03:55,832 compiler to do dynamic binding and this 36414 27:03:53,831 --> 27:03:58,159 is going to be super cool let's see that 36415 27:03:55,831 --> 27:04:00,799 we can also achieve the same thing using 36416 27:03:58,160 --> 27:04:03,680 base references i am not going to run 36417 27:04:03,679 --> 27:04:08,871 base reference i am not going to be 36418 27:04:06,320 --> 27:04:11,120 running the code where we are using a 36419 27:04:12,639 --> 27:04:17,440 object because that's going to be really 36420 27:04:14,720 --> 27:04:19,191 boring but what we can do is put in an 36421 27:04:17,440 --> 27:04:22,320 awful object for example you can say 36422 27:04:19,191 --> 27:04:24,959 over one and say shape ref and call the 36423 27:04:22,320 --> 27:04:27,040 draw method again this is dynamic 36424 27:04:24,960 --> 27:04:29,360 binding because the draw method is 36425 27:04:27,039 --> 27:04:31,599 virtual so the compiler is not going to 36426 27:04:29,360 --> 27:04:33,831 look at the type of the reference to 36427 27:04:31,600 --> 27:04:36,552 decide which version of the draw method 36428 27:04:33,831 --> 27:04:39,679 to call it is going to look at the 36429 27:04:36,551 --> 27:04:42,000 actual object that this reference is 36430 27:04:39,679 --> 27:04:44,719 managing and it is going to see that the 36431 27:04:42,000 --> 27:04:48,000 type of the object is awful so c plus 36432 27:04:44,720 --> 27:04:50,000 plus is going to call the oval version 36433 27:04:48,000 --> 27:04:52,551 of the draw method that's what we're 36434 27:04:50,000 --> 27:04:54,160 going to get if we run this program here 36435 27:04:52,551 --> 27:04:56,551 let's build and see this we're going to 36436 27:04:54,160 --> 27:04:59,040 pass this through gcc the voltage should 36437 27:04:56,551 --> 27:05:01,679 be good as we see here we are going to 36438 27:04:59,039 --> 27:05:04,319 clear and run rooster you see we are 36439 27:05:01,679 --> 27:05:06,719 drawing oval if we happen to be using 36440 27:05:04,320 --> 27:05:07,760 this to manage our circle object let's 36441 27:05:07,759 --> 27:05:12,639 we will see that we will get the circle 36442 27:05:10,399 --> 27:05:14,639 method called okay let's build and we're 36443 27:05:12,639 --> 27:05:16,479 going to pass this through gcc to do 36444 27:05:14,639 --> 27:05:19,191 that the both is going to be good we can 36445 27:05:16,479 --> 27:05:21,911 clear and run rooster you see that we 36446 27:05:19,191 --> 27:05:25,679 are drawing a circle because our base 36447 27:05:21,911 --> 27:05:29,039 pointer is now managing a circle object 36448 27:05:25,679 --> 27:05:32,000 now because we are using dynamic binding 36449 27:05:29,039 --> 27:05:33,911 or late binding as some people call it 36450 27:05:32,000 --> 27:05:36,080 we can really do all kinds of crazy 36451 27:05:33,911 --> 27:05:38,800 things the first thing we can achieve is 36452 27:05:36,080 --> 27:05:41,360 set up one single method which is really 36453 27:05:38,800 --> 27:05:43,279 going to draw any kind of shape we throw 36454 27:05:41,360 --> 27:05:44,872 at it so let's do something like this 36455 27:05:43,279 --> 27:05:47,119 we're going to say void and we're going 36456 27:05:44,872 --> 27:05:50,232 to say draw shape and it is going to 36457 27:05:47,119 --> 27:05:51,911 take a shape pointer let's say s and the 36458 27:05:50,232 --> 27:05:54,720 only thing we're going to do in this 36459 27:05:51,911 --> 27:05:56,399 function and say s draw this is going to 36460 27:05:54,720 --> 27:05:58,551 call the draw method on whatever 36461 27:05:56,399 --> 27:06:01,511 parameter that we pass in here 36462 27:05:58,551 --> 27:06:04,551 and watch this if we go down here and 36463 27:06:01,512 --> 27:06:08,000 uncomment this section on drawing shapes 36464 27:06:04,551 --> 27:06:10,319 if we say draw and pass in a circle for 36465 27:06:08,000 --> 27:06:13,039 example or let's pass an oval and do 36466 27:06:10,320 --> 27:06:15,040 this one level and to do this level by 36467 27:06:13,039 --> 27:06:16,719 level we're going to pass in over one 36468 27:06:15,039 --> 27:06:18,871 and we should pass an address because 36469 27:06:16,720 --> 27:06:21,512 this is taking a pointer let's so let's 36470 27:06:18,872 --> 27:06:23,279 do this and if we build and run this 36471 27:06:21,512 --> 27:06:25,600 program let's see what happens here 36472 27:06:23,279 --> 27:06:27,360 we're going to build with gcc the world 36473 27:06:25,600 --> 27:06:30,400 is going to be good as you see here if 36474 27:06:27,360 --> 27:06:33,360 we clear and run rooster you see 36475 27:06:30,399 --> 27:06:35,599 and let's take out this thing using base 36476 27:06:33,360 --> 27:06:37,191 references here because this is just 36477 27:06:35,600 --> 27:06:40,320 going to confuse us we're going to build 36478 27:06:37,191 --> 27:06:42,639 again and i use gcc for this the build 36479 27:06:40,320 --> 27:06:44,800 is going to be good within clear and run 36480 27:06:42,639 --> 27:06:47,911 rooster you see oval drill because we 36481 27:06:44,800 --> 27:06:50,320 are passing in a novel object if we pass 36482 27:06:47,911 --> 27:06:52,720 in our shape let's say shape one for 36483 27:06:50,320 --> 27:06:54,480 example we're going to build the book is 36484 27:06:52,720 --> 27:06:57,440 going to be good as you see here if we 36485 27:06:54,479 --> 27:06:59,831 clear and run rooster we're going to see 36486 27:06:57,440 --> 27:07:00,872 shape draw if we pass in circle let's do 36487 27:07:00,872 --> 27:07:05,360 and the bold again we're going to build 36488 27:07:02,551 --> 27:07:07,119 with gcc we're going to clear and run 36489 27:07:05,360 --> 27:07:09,360 rooster you're going to see that circle 36490 27:07:07,119 --> 27:07:12,799 draw is called and this is really cool 36491 27:07:09,360 --> 27:07:14,639 we can only use a single method and this 36492 27:07:12,800 --> 27:07:16,872 method is going to take any shape we 36493 27:07:14,639 --> 27:07:19,600 throw at it and it is going to draw it 36494 27:07:16,872 --> 27:07:21,600 if we happen to be having 100 kinds of 36495 27:07:19,600 --> 27:07:23,279 shapes in our program we can just for 36496 27:07:21,600 --> 27:07:25,600 example loop through a collection 36497 27:07:23,279 --> 27:07:28,319 containing those shapes and if we call 36498 27:07:25,600 --> 27:07:30,320 the draw method on any shape we store in 36499 27:07:28,320 --> 27:07:32,720 our collection we're going to get the 36500 27:07:30,320 --> 27:07:35,040 correct shape drone and this is super 36501 27:07:32,720 --> 27:07:37,191 cool this is really powerful and i hope 36502 27:07:35,039 --> 27:07:39,679 you are excited to be able to achieve 36503 27:07:37,191 --> 27:07:42,000 this in c plus plus now we can also 36504 27:07:39,679 --> 27:07:44,639 achieve the same thing by using a method 36505 27:07:42,000 --> 27:07:48,479 that takes a reference for example so we 36506 27:07:44,639 --> 27:07:50,800 can say void joe shape let's say v1 and 36507 27:07:48,479 --> 27:07:53,679 we're going to pass a reference 36508 27:07:50,800 --> 27:07:56,551 we can say const and pass a reference 36509 27:07:53,679 --> 27:07:59,279 and say sr to say ship reference we can 36510 27:07:56,551 --> 27:08:01,360 do that and we're going to say sjo we're 36511 27:07:59,279 --> 27:08:03,440 going to call the jaw method on this 36512 27:08:01,360 --> 27:08:06,399 reference and let's see what happens if 36513 27:08:03,440 --> 27:08:08,800 we try this out let's say sr that's what 36514 27:08:06,399 --> 27:08:11,599 we should say here and this is going to 36515 27:08:08,800 --> 27:08:14,551 be taken by our visual studio code 36516 27:08:11,600 --> 27:08:15,760 editor if we go in here and change this 36517 27:08:15,759 --> 27:08:21,191 draw shape v1 we can pass in shape one 36518 27:08:19,279 --> 27:08:22,551 we can do that because this is taking a 36519 27:08:22,551 --> 27:08:27,599 if we build with gcc we are going to see 36520 27:08:25,512 --> 27:08:29,279 that the world is good we can clear 36521 27:08:27,600 --> 27:08:31,279 let's clear we're going to run the 36522 27:08:29,279 --> 27:08:33,512 rooster this is going to call shape draw 36523 27:08:31,279 --> 27:08:36,000 because we are using a reference to 36524 27:08:33,512 --> 27:08:38,000 manage a shaped object we can also pass 36525 27:08:36,000 --> 27:08:41,440 it over one and we're going to build 36526 27:08:38,000 --> 27:08:43,600 again and see how this works the bolt is 36527 27:08:41,440 --> 27:08:45,440 going to be good we can clear and run 36528 27:08:43,600 --> 27:08:47,760 rooster we're going to say overdraw and 36529 27:08:45,440 --> 27:08:50,639 we're going to see our information here 36530 27:08:47,759 --> 27:08:52,871 if we pass in our circle object 36531 27:08:50,639 --> 27:08:55,191 and the bolt we're going to pass this 36532 27:08:52,872 --> 27:08:57,440 through gcc the bolt is going to be good 36533 27:08:55,191 --> 27:08:59,599 we can clear and run rooster this is 36534 27:08:57,440 --> 27:09:02,320 going to draw a circle for us this is 36535 27:08:59,600 --> 27:09:04,552 really cool we have one method again 36536 27:09:02,320 --> 27:09:07,440 that is taking a parameter by reference 36537 27:09:04,551 --> 27:09:09,911 but we can use this method to draw any 36538 27:09:07,440 --> 27:09:12,232 kind of shape in our inheritance 36539 27:09:09,911 --> 27:09:15,191 hierarchy this is going to work really 36540 27:09:12,232 --> 27:09:17,680 well and it is doing dynamic binding 36541 27:09:15,191 --> 27:09:20,551 which is powered by virtual functions we 36542 27:09:17,679 --> 27:09:22,871 are using in our inheritance hierarchy 36543 27:09:20,551 --> 27:09:25,679 here you should really keep this in mind 36544 27:09:22,872 --> 27:09:28,720 now let's go back and see if we can even 36545 27:09:25,679 --> 27:09:30,639 use row pointers to do stuff i am going 36546 27:09:28,720 --> 27:09:33,191 to comment this out and i am going to 36547 27:09:30,639 --> 27:09:34,800 say roll pointers because i want you to 36548 27:09:33,191 --> 27:09:36,551 see something we're going to set up a 36549 27:09:34,800 --> 27:09:37,760 shape pointer we're going to say shape 36550 27:09:37,759 --> 27:09:41,279 do we have that on top here let's see if 36551 27:09:41,279 --> 27:09:46,319 yes we have shape ptr so let's say shape 36552 27:09:46,320 --> 27:09:50,552 and pass it a novel object we're going 36553 27:09:48,479 --> 27:09:53,279 to pass a novel again and we're going to 36554 27:09:50,551 --> 27:09:56,000 pass the address of 01 this is going to 36555 27:09:53,279 --> 27:09:58,319 do we can call the draw method and get 36556 27:09:58,320 --> 27:10:03,192 or get it called using dynamic binding 36557 27:10:03,191 --> 27:10:08,799 and say draw this is going to call the 36558 27:10:05,911 --> 27:10:11,831 correct version but one thing i want you 36559 27:10:08,800 --> 27:10:14,800 to see is that if a method is not 36560 27:10:11,831 --> 27:10:15,911 polymorphic or if a method is not 36561 27:10:15,911 --> 27:10:21,039 you want to get it called using 36562 27:10:18,039 --> 27:10:22,799 polymorphism for example if we go to our 36563 27:10:21,039 --> 27:10:25,679 oval class we're going to see that we 36564 27:10:22,800 --> 27:10:27,832 have a bunch of getters let's go in and 36565 27:10:25,679 --> 27:10:31,191 make them public so that they are 36566 27:10:27,831 --> 27:10:33,159 callable from the outside and if we try 36567 27:10:33,160 --> 27:10:38,320 polymorphically or using dynamic binding 36568 27:10:36,479 --> 27:10:41,119 that's not going to work let's do that 36569 27:10:38,320 --> 27:10:42,960 we're going to say get x rad and the 36570 27:10:41,119 --> 27:10:45,119 reason we might want to do something 36571 27:10:42,960 --> 27:10:46,000 like this is because we have this method 36572 27:10:46,000 --> 27:10:52,080 and it is set up on our oval class 36573 27:10:49,440 --> 27:10:54,960 so because we are using a base pointer 36574 27:10:52,080 --> 27:10:57,191 to manage a novel object some of you 36575 27:10:54,960 --> 27:11:00,232 might think uh-huh i might be able to 36576 27:10:57,191 --> 27:11:03,279 call this method because i am able to 36577 27:11:00,232 --> 27:11:06,720 call the job method which is also in the 36578 27:11:03,279 --> 27:11:08,720 public scope of our overclass but that's 36579 27:11:06,720 --> 27:11:10,872 not going to work because this method 36580 27:11:10,872 --> 27:11:16,720 dynamic binding or polymorphism is only 36581 27:11:14,320 --> 27:11:19,760 going to work if your methods are 36582 27:11:16,720 --> 27:11:22,551 virtual and they are set up across your 36583 27:11:19,759 --> 27:11:24,399 inheritance hierarchy so this is not 36584 27:11:22,551 --> 27:11:26,159 going to work if you try to do something 36585 27:11:24,399 --> 27:11:28,799 like this this is going to give you a 36586 27:11:26,160 --> 27:11:30,552 compiler error let's try and build and 36587 27:11:28,800 --> 27:11:33,440 see the compiler error and we're going 36588 27:11:30,551 --> 27:11:36,479 to see an error that says that no such 36589 27:11:33,440 --> 27:11:38,320 method on this object let's go here 36590 27:11:38,320 --> 27:11:42,320 class shape has no member name to get x 36591 27:11:42,320 --> 27:11:46,960 because the method is not virtual the 36592 27:11:44,479 --> 27:11:49,512 compiler is going to do static binding 36593 27:11:46,960 --> 27:11:51,600 and it is going to be using the type of 36594 27:11:49,512 --> 27:11:54,000 the pointer which is shaped to resolve 36595 27:11:51,600 --> 27:11:56,000 this method and it is going to see that 36596 27:11:54,000 --> 27:11:58,399 in shape we don't have a method called 36597 27:11:56,000 --> 27:12:00,639 get xrad and the compiler is going to 36598 27:11:58,399 --> 27:12:02,799 throw this compiler error here this is 36599 27:12:00,639 --> 27:12:05,119 what we get so this is not going to work 36600 27:12:02,800 --> 27:12:06,960 and i want you to be aware of this 36601 27:12:05,119 --> 27:12:10,080 another thing i want you to see 36602 27:12:06,960 --> 27:12:11,600 is that because we are using dynamic 36603 27:12:11,600 --> 27:12:16,720 we don't need we don't need to set up 36604 27:12:14,080 --> 27:12:18,960 all kinds of crazy collections to take 36605 27:12:16,720 --> 27:12:22,160 into account different shapes that we 36606 27:12:18,960 --> 27:12:24,320 have in our c plus plus program what 36607 27:12:22,160 --> 27:12:27,192 we're going to do here is use a base 36608 27:12:24,320 --> 27:12:30,480 pointer to manage the data that we store 36609 27:12:27,191 --> 27:12:33,360 in our array here so all i am going to 36610 27:12:30,479 --> 27:12:35,119 do is remove all this dirt here because 36611 27:12:33,360 --> 27:12:37,119 this is really dirt you don't want to do 36612 27:12:35,119 --> 27:12:39,191 something like that and we're going to 36613 27:12:37,119 --> 27:12:42,231 use a base pointer which is a shape 36614 27:12:39,191 --> 27:12:44,231 pointer and use that to set up an array 36615 27:12:42,232 --> 27:12:47,440 which can handle any kind of shape we 36616 27:12:44,232 --> 27:12:50,000 have in our inheritance hierarchy so we 36617 27:12:47,440 --> 27:12:52,320 can store it shape objects we can store 36618 27:12:50,000 --> 27:12:54,960 it over objects we can store in circle 36619 27:12:52,320 --> 27:12:56,640 objects and if we look like we do here 36620 27:12:54,960 --> 27:12:58,720 we're going to get the correct method 36621 27:12:56,639 --> 27:13:01,191 called and this is going to be super 36622 27:12:58,720 --> 27:13:03,680 cool so let's try and run this program 36623 27:13:01,191 --> 27:13:05,911 we're going to pass this through gcc why 36624 27:13:03,679 --> 27:13:08,959 do we have the red thing here let's 36625 27:13:05,911 --> 27:13:11,191 build and see if it goes away maybe this 36626 27:13:08,960 --> 27:13:13,120 is visual studio code having a problem 36627 27:13:11,191 --> 27:13:15,360 with our code the world is good so 36628 27:13:13,119 --> 27:13:17,512 visual studio code was playing with us 36629 27:13:15,360 --> 27:13:19,191 we're going to clear and run rooster 36630 27:13:17,512 --> 27:13:22,551 you're going to see that we're going to 36631 27:13:19,191 --> 27:13:24,159 be looping through this array here and 36632 27:13:22,551 --> 27:13:26,159 on the first iteration we're going to 36633 27:13:24,160 --> 27:13:28,480 call shape draw because the first thing 36634 27:13:26,160 --> 27:13:30,552 is the shape on the second iteration 36635 27:13:28,479 --> 27:13:33,191 we're going to call overdraw because the 36636 27:13:30,551 --> 27:13:34,159 second thing is a novel and we're going 36637 27:13:34,160 --> 27:13:39,279 hit the third iteration and call circle 36638 27:13:36,720 --> 27:13:41,831 draw because the third thing in this 36639 27:13:39,279 --> 27:13:44,080 collection is a circle hopefully you can 36640 27:13:41,831 --> 27:13:47,191 see that this is going to simplify our 36641 27:13:44,080 --> 27:13:48,479 programs a lot compared to what we were 36642 27:13:48,479 --> 27:13:52,159 in the last lecture and this is really 36643 27:13:50,160 --> 27:13:54,720 all i had to share in this lecture 36644 27:13:52,160 --> 27:13:58,480 showing you that you can achieve dynamic 36645 27:13:54,720 --> 27:14:01,040 binding or polymorphism or late binding 36646 27:13:58,479 --> 27:14:03,831 as some people call it using virtual 36647 27:14:01,039 --> 27:14:06,719 functions in your inheritance hierarchy 36648 27:14:03,831 --> 27:14:09,511 again all we had to do was mark our 36649 27:14:06,720 --> 27:14:11,360 member functions as a virtual and the 36650 27:14:09,512 --> 27:14:14,160 compiler is going to know that it can 36651 27:14:11,360 --> 27:14:17,039 set up dynamic polymorphism for your 36652 27:14:14,160 --> 27:14:20,232 types if you happen to be managing them 36653 27:14:17,039 --> 27:14:21,911 using a base pointer or a base reference 36654 27:14:20,232 --> 27:14:23,680 we are going to stop here in this 36655 27:14:21,911 --> 27:14:26,551 lecture and the next one we're going to 36656 27:14:23,679 --> 27:14:28,799 learn about slicing and the size of 36657 27:14:28,800 --> 27:14:32,872 go ahead and finish up here and meet me 36658 27:14:31,119 --> 27:14:37,039 there in this lecture we're going to 36659 27:14:32,872 --> 27:14:40,160 explore the size of polymorphic objects 36660 27:14:37,039 --> 27:14:42,159 or objects that use dynamic binding and 36661 27:14:40,160 --> 27:14:45,192 we're going to learn about the concept 36662 27:14:42,160 --> 27:14:48,400 of slicing if you happen to assign a 36663 27:14:45,191 --> 27:14:50,231 derived object to a base object we're 36664 27:14:48,399 --> 27:14:52,231 going to see how this works out in the 36665 27:14:50,232 --> 27:14:54,872 last two lectures we saw that we could 36666 27:14:52,232 --> 27:14:57,192 achieve static binding using a setup 36667 27:14:54,872 --> 27:15:00,000 like this we can set up an inheritance 36668 27:14:57,191 --> 27:15:02,799 hierarchy and we're going to have oval 36669 27:15:00,000 --> 27:15:06,399 derive or inherit from shape and we're 36670 27:15:02,800 --> 27:15:09,440 going to have circle inherit or derive 36671 27:15:06,399 --> 27:15:11,439 from over here and our methods are going 36672 27:15:09,440 --> 27:15:13,600 to be row they are not going to be 36673 27:15:11,440 --> 27:15:16,080 virtual on anything and this is going to 36674 27:15:13,600 --> 27:15:18,552 do static binding if you happen to 36675 27:15:16,080 --> 27:15:21,040 manage a derived object through a base 36676 27:15:18,551 --> 27:15:23,360 pointer or reference you want to get 36677 27:15:21,039 --> 27:15:25,511 dynamic holes you will get static 36678 27:15:23,360 --> 27:15:28,000 binding and the compiler will use the 36679 27:15:25,512 --> 27:15:31,119 base pointer or base reference to decide 36680 27:15:28,000 --> 27:15:32,872 which draw method to call on your object 36681 27:15:31,119 --> 27:15:36,080 we also saw that we could achieve 36682 27:15:32,872 --> 27:15:38,639 dynamic binding using virtual functions 36683 27:15:36,080 --> 27:15:41,680 and all we needed to do was mark our 36684 27:15:38,639 --> 27:15:44,119 methods as virtual in our inheritance 36685 27:15:41,679 --> 27:15:47,911 hierarchy here and this would give us 36686 27:15:44,119 --> 27:15:50,231 polymorphism or dynamic binding but one 36687 27:15:47,911 --> 27:15:52,479 thing you should know is that dynamic 36688 27:15:50,232 --> 27:15:55,600 binding is not free you're going to be 36689 27:15:52,479 --> 27:15:57,512 paying in terms of memory to get all 36690 27:15:55,600 --> 27:15:59,192 these cool things we saw in the last 36691 27:15:57,512 --> 27:16:01,360 lecture and we're going to be proving 36692 27:15:59,191 --> 27:16:03,512 that in this lecture what we're going to 36693 27:16:01,360 --> 27:16:05,911 be doing is basically set up our 36694 27:16:03,512 --> 27:16:08,400 inheritance hierarchy and we are going 36695 27:16:05,911 --> 27:16:10,232 to set that up in such a way that it is 36696 27:16:08,399 --> 27:16:12,000 going to be using static binding we're 36697 27:16:10,232 --> 27:16:14,400 going to see the sizes we get printed 36698 27:16:12,000 --> 27:16:17,360 out here and we're going to use dynamic 36699 27:16:14,399 --> 27:16:19,911 binding and compare the sizes and the 36700 27:16:17,360 --> 27:16:22,872 constitution is going to be that with 36701 27:16:19,911 --> 27:16:25,759 dynamic binding your objects are going 36702 27:16:22,872 --> 27:16:28,800 to be much larger because your simplest 36703 27:16:25,759 --> 27:16:30,319 plus program will need to keep track of 36704 27:16:32,960 --> 27:16:37,360 function calls dynamically and that 36705 27:16:35,279 --> 27:16:40,319 information is stored in what we call 36706 27:16:37,360 --> 27:16:42,551 virtual tables but i am not going to go 36707 27:16:40,320 --> 27:16:45,512 into that here because it is really out 36708 27:16:42,551 --> 27:16:47,599 of scope of a course like this but i 36709 27:16:45,512 --> 27:16:50,639 want you to be aware of this if you use 36710 27:16:47,600 --> 27:16:53,279 dynamic if you use dynamic binding your 36711 27:16:50,639 --> 27:16:55,039 objects are going to be much larger okay 36712 27:16:53,279 --> 27:16:56,232 the first thing i want to bring your 36713 27:16:56,232 --> 27:17:01,040 is object slicing and we're going to 36714 27:16:58,800 --> 27:17:03,512 look at a simple example here we have 36715 27:17:01,039 --> 27:17:06,079 our objects we have a shape object we 36716 27:17:03,512 --> 27:17:10,080 have an oval object and we have a circle 36717 27:17:06,080 --> 27:17:14,080 object and down here we want to assign 36718 27:17:10,080 --> 27:17:16,800 our circle object to a shape object 36719 27:17:14,080 --> 27:17:19,191 now notice that here we are not using a 36720 27:17:16,800 --> 27:17:21,600 pointer or a reference these are just 36721 27:17:19,191 --> 27:17:23,759 the row objects that we are assigning to 36722 27:17:21,600 --> 27:17:25,440 each other and what the competitor is 36723 27:17:25,440 --> 27:17:30,872 it's going to notice that a circle has a 36724 27:17:28,160 --> 27:17:33,832 shape according to it and it is going to 36725 27:17:30,872 --> 27:17:35,832 strip off the outer layers or in other 36726 27:17:33,831 --> 27:17:38,319 words it is going to strip off the 36727 27:17:35,831 --> 27:17:40,551 circle layer it is going to strip off 36728 27:17:38,320 --> 27:17:43,360 the oval layer and it is going to leave 36729 27:17:40,551 --> 27:17:46,399 in the shape part and that's what it is 36730 27:17:43,360 --> 27:17:49,039 going to assign to our shape object here 36731 27:17:46,399 --> 27:17:51,360 let's visualize this our shape class 36732 27:17:49,039 --> 27:17:55,191 looks like what we have here on the left 36733 27:17:51,360 --> 27:17:57,440 our oval class has a shape part into it 36734 27:17:55,191 --> 27:17:59,831 and our circle is going to have an over 36735 27:17:57,440 --> 27:18:02,160 part into it and that oval part is going 36736 27:17:59,831 --> 27:18:05,119 to have a shape part of it and this is 36737 27:18:02,160 --> 27:18:06,872 the setup that we have here now if we do 36738 27:18:05,119 --> 27:18:08,639 our assignment we're going to do 36739 27:18:06,872 --> 27:18:10,400 something like this the compiler is 36740 27:18:08,639 --> 27:18:12,551 going to see that we are already 36741 27:18:12,551 --> 27:18:16,639 to a shape and the compiler is going to 36742 27:18:14,399 --> 27:18:19,511 notice that what we have on the left is 36743 27:18:16,639 --> 27:18:22,639 just a shape and we don't have enough 36744 27:18:19,512 --> 27:18:24,960 space to store information about oval 36745 27:18:22,639 --> 27:18:26,872 and the circle here so what the compiler 36746 27:18:24,960 --> 27:18:28,872 is going to do it is going to strip off 36747 27:18:26,872 --> 27:18:31,680 all these things that we don't really 36748 27:18:28,872 --> 27:18:32,551 need and it is going to leave in only 36749 27:18:32,551 --> 27:18:38,551 shape part that we can assign on what we 36750 27:18:36,000 --> 27:18:40,551 have to the left so it is going to strip 36751 27:18:38,551 --> 27:18:42,479 off all these stands and we are going to 36752 27:18:40,551 --> 27:18:44,479 be left with something like this and 36753 27:18:42,479 --> 27:18:46,639 then what we have on the right is going 36754 27:18:44,479 --> 27:18:50,319 to be assigned on the left and we will 36755 27:18:46,639 --> 27:18:52,960 have our data stored in our ship to 36756 27:18:50,320 --> 27:18:55,192 object and if we try to use it that's 36757 27:18:52,960 --> 27:18:57,832 what you're going to get in memory so 36758 27:18:55,191 --> 27:18:59,440 this is referred to as slicing again the 36759 27:18:57,831 --> 27:19:01,511 compiler is going to notice that it 36760 27:18:59,440 --> 27:19:03,040 doesn't really have enough space on the 36761 27:19:03,039 --> 27:19:07,511 everything we have to the right of the 36762 27:19:04,872 --> 27:19:09,680 assignment here and it is going to strip 36763 27:19:09,679 --> 27:19:15,599 everything we don't need and it is only 36764 27:19:12,639 --> 27:19:18,720 going to leave in the shape part and 36765 27:19:15,600 --> 27:19:21,440 that's what it is going to assign on our 36766 27:19:18,720 --> 27:19:23,759 object that we have on the left of the 36767 27:19:21,440 --> 27:19:27,279 assignment operator here and this is 36768 27:19:23,759 --> 27:19:29,360 referred to as slicing in c plus plus 36769 27:19:27,279 --> 27:19:31,512 terminology i just want you to be aware 36770 27:19:29,360 --> 27:19:34,000 of this and we're going to see a lot of 36771 27:19:31,512 --> 27:19:35,600 this starting from now now that you know 36772 27:19:34,000 --> 27:19:37,440 this we're going to head over to visual 36773 27:19:35,600 --> 27:19:40,160 studio code and play with this a little 36774 27:19:37,440 --> 27:19:42,800 more here we are in our working folder 36775 27:19:40,160 --> 27:19:46,320 the current project is size of 36776 27:19:42,800 --> 27:19:48,000 polymorphic objects and slicing we are 36777 27:19:46,320 --> 27:19:50,552 going to grab the code from the last 36778 27:19:48,000 --> 27:19:53,600 lecture so let's grab everything except 36779 27:19:50,551 --> 27:19:55,759 for the binary file we are going to copy 36780 27:19:53,600 --> 27:19:58,480 everything and we're going to put that 36781 27:19:55,759 --> 27:20:00,551 in the current folder and we're going to 36782 27:19:58,479 --> 27:20:03,039 open this little guy in visual studio 36783 27:20:00,551 --> 27:20:05,512 code by dragging and dropping here this 36784 27:20:03,039 --> 27:20:09,191 is going to give us our project and we 36785 27:20:05,512 --> 27:20:11,512 can head over in the main cpp file 36786 27:20:09,191 --> 27:20:14,231 and clean up all the mess we got from 36787 27:20:11,512 --> 27:20:16,639 the last previous lectures we can also 36788 27:20:14,232 --> 27:20:18,800 take out these functions because i don't 36789 27:20:18,800 --> 27:20:24,000 and we are going to check out on our 36790 27:20:21,279 --> 27:20:26,720 classes notice that we are using dynamic 36791 27:20:24,000 --> 27:20:29,191 polymorphism because we have virtual 36792 27:20:26,720 --> 27:20:31,831 functions here so if we happen to be 36793 27:20:29,191 --> 27:20:35,039 using base pointers or base references 36794 27:20:31,831 --> 27:20:37,911 to manage our derived objects we will 36795 27:20:35,039 --> 27:20:40,639 get polymorphic results okay the first 36796 27:20:37,911 --> 27:20:43,679 thing we want to see is the size of 36797 27:20:40,639 --> 27:20:46,399 objects for our classes here because we 36798 27:20:43,679 --> 27:20:48,959 are using virtual functions let's head 36799 27:20:46,399 --> 27:20:51,191 over in the main cpp file and we're 36800 27:20:48,960 --> 27:20:53,440 going to put in a piece of code which is 36801 27:20:51,191 --> 27:20:55,279 just going to use the size of operator 36802 27:20:55,279 --> 27:21:00,479 of objects of these classes we can try 36803 27:20:58,399 --> 27:21:02,871 to build and run this program we're 36804 27:21:00,479 --> 27:21:05,599 going to use gcc here the world is just 36805 27:21:02,872 --> 27:21:07,600 going to be good we can bring up a 36806 27:21:05,600 --> 27:21:09,120 powershell window we can use to run 36807 27:21:09,119 --> 27:21:13,831 we're going to do that new terminal this 36808 27:21:11,831 --> 27:21:16,479 is going to bring us our window here we 36809 27:21:13,831 --> 27:21:19,599 can clear and run rooster we're going to 36810 27:21:19,600 --> 27:21:25,360 so you see that these are our sizes so 36811 27:21:22,639 --> 27:21:28,232 let's say that with uh dynamic is going 36812 27:21:25,360 --> 27:21:31,279 to be 40 for the base class with dynamic 36813 27:21:28,232 --> 27:21:32,400 portal morphism we're going to get 56 36814 27:21:32,399 --> 27:21:38,319 oval object and we're going to get 36815 27:21:38,320 --> 27:21:44,720 circle object here now we're going to 36816 27:21:41,039 --> 27:21:46,231 remove the setup that uses polymorphism 36817 27:21:44,720 --> 27:21:48,639 and we're going to see the results that 36818 27:21:46,232 --> 27:21:51,040 we get all we need to do is to remove 36819 27:21:48,639 --> 27:21:53,759 the virtual keyword here we can comment 36820 27:21:51,039 --> 27:21:56,479 it out you can do something like this in 36821 27:21:53,759 --> 27:21:59,191 shape and we're going to hop over in 36822 27:21:56,479 --> 27:22:00,872 oval and comment out the virtual key 36823 27:21:59,191 --> 27:22:02,319 world we can do something like this and 36824 27:22:02,320 --> 27:22:06,872 work without removing this completely 36825 27:22:04,639 --> 27:22:09,679 from our code because we still need to 36826 27:22:06,872 --> 27:22:12,160 leave this in as a reference we're going 36827 27:22:09,679 --> 27:22:13,119 to hop over to circle and do the same 36828 27:22:13,119 --> 27:22:17,911 and now that we have this setup we can 36829 27:22:15,279 --> 27:22:19,512 hop over to our main cpp file we're 36830 27:22:17,911 --> 27:22:22,080 going to see that we don't have any 36831 27:22:19,512 --> 27:22:24,639 squiggly lines of some kind so this is 36832 27:22:22,080 --> 27:22:26,720 going to be accepted by the compiler but 36833 27:22:24,639 --> 27:22:28,960 if we build and run this program we're 36834 27:22:26,720 --> 27:22:30,960 going to see different sizes let's see 36835 27:22:28,960 --> 27:22:33,912 if the world is good it is good as you 36836 27:22:30,960 --> 27:22:36,080 see here we can clear and run rooster 36837 27:22:36,080 --> 27:22:43,600 static binding the base object is 32 36838 27:22:39,911 --> 27:22:45,119 bytes the awful object is going to be 48 36839 27:22:43,600 --> 27:22:48,080 and hopefully you can see that with 36840 27:22:45,119 --> 27:22:50,231 dynamic polymorphism our objects are 36841 27:22:48,080 --> 27:22:52,160 going to be much larger because the 36842 27:22:50,232 --> 27:22:54,480 compiler has to keep track of the 36843 27:22:52,160 --> 27:22:56,720 information that allows it to 36844 27:22:56,720 --> 27:23:02,872 virtual function calls that's why our 36845 27:22:59,191 --> 27:23:04,959 objects are larger with polymorphism and 36846 27:23:02,872 --> 27:23:07,680 you should know that you are not getting 36847 27:23:04,960 --> 27:23:11,040 polymorphic results for free you are 36848 27:23:07,679 --> 27:23:13,679 going to be paying in terms of much more 36849 27:23:11,039 --> 27:23:16,079 memory that you use for your objects in 36850 27:23:13,679 --> 27:23:17,911 your c plus plus programs this is the 36851 27:23:16,080 --> 27:23:19,831 first thing i want you to see in this 36852 27:23:17,911 --> 27:23:23,360 lecture the second thing i want you to 36853 27:23:19,831 --> 27:23:25,191 be aware of is the concept of slicing 36854 27:23:23,360 --> 27:23:27,440 and this is going to happen if you 36855 27:23:27,440 --> 27:23:32,551 derived object and assign that to a row 36856 27:23:30,551 --> 27:23:34,800 based object let's do something like 36857 27:23:32,551 --> 27:23:36,872 that we are going to create a circle 36858 27:23:34,800 --> 27:23:39,360 object and then we're going to say shape 36859 27:23:36,872 --> 27:23:41,680 and say shape here and we're going to 36860 27:23:41,679 --> 27:23:46,159 this is something we could do 36861 27:23:43,512 --> 27:23:48,320 but notice that the compiler is not 36862 27:23:46,160 --> 27:23:50,080 giving us any kind of compiler error 36863 27:23:48,320 --> 27:23:52,232 because we can assign a circle to a 36864 27:23:50,080 --> 27:23:54,872 shape because a circle is really a shape 36865 27:23:52,232 --> 27:23:57,832 but from what we know in our inheritance 36866 27:23:54,872 --> 27:24:00,320 hierarchy a circle is really much more 36867 27:23:57,831 --> 27:24:03,439 than a row shape a circle has awful 36868 27:24:00,320 --> 27:24:05,760 information in it and it also has circle 36869 27:24:03,440 --> 27:24:08,080 information in it so it is really much 36870 27:24:05,759 --> 27:24:10,319 larger and much wider than a shape 36871 27:24:08,080 --> 27:24:12,960 object but the compiler is going to see 36872 27:24:10,320 --> 27:24:15,120 that we are trying to assign a circle to 36873 27:24:12,960 --> 27:24:16,552 a shape and it is going to be smart 36874 27:24:16,551 --> 27:24:21,360 that we don't really need oval and 36875 27:24:18,720 --> 27:24:23,680 circle information in our shape object 36876 27:24:21,360 --> 27:24:26,160 that we are storing here so the compiler 36877 27:24:23,679 --> 27:24:29,039 is going to slice off circle and oval 36878 27:24:26,160 --> 27:24:30,960 information and it is going to leave in 36879 27:24:29,039 --> 27:24:33,511 shape information and that's the only 36880 27:24:30,960 --> 27:24:35,440 thing we will be able to use with this 36881 27:24:33,512 --> 27:24:38,080 shape variable that we have in our c 36882 27:24:35,440 --> 27:24:40,232 plus plus program so if we say shape 36883 27:24:38,080 --> 27:24:42,320 drill you can try to guess what we are 36884 27:24:40,232 --> 27:24:45,600 going to get and let's go back and 36885 27:24:42,320 --> 27:24:47,192 re-enable polymorphism because some of 36886 27:24:45,600 --> 27:24:49,360 you might think we are going to get 36887 27:24:49,360 --> 27:24:54,551 with this setup here that's not what 36888 27:24:51,679 --> 27:24:57,119 we're going to get because we are not 36889 27:24:54,551 --> 27:24:58,479 going through bezel references or base 36890 27:24:58,479 --> 27:25:03,599 do what we are doing in this example on 36891 27:25:01,039 --> 27:25:06,159 slicing here so because we are not using 36892 27:25:03,600 --> 27:25:08,480 base references or base pointers in this 36893 27:25:06,160 --> 27:25:10,800 line here the compiler is going to see 36894 27:25:08,479 --> 27:25:13,279 that these are raw objects and it is 36895 27:25:10,800 --> 27:25:16,479 going to do slicing and slice of things 36896 27:25:13,279 --> 27:25:19,279 we don't need in our shape variable 36897 27:25:16,479 --> 27:25:21,679 that we are setting up on line 16 here 36898 27:25:19,279 --> 27:25:23,831 so if we call the draw method this is 36899 27:25:23,831 --> 27:25:27,831 it is not going to do dynamic 36900 27:25:25,440 --> 27:25:29,760 polymorphism here you should be aware of 36901 27:25:27,831 --> 27:25:32,080 this if you're not using the base 36902 27:25:29,759 --> 27:25:35,679 pointers or references and you hope to 36903 27:25:32,080 --> 27:25:37,040 get dynamic polymorphism results i hate 36904 27:25:35,679 --> 27:25:39,279 to break it to you but you're going to 36905 27:25:37,039 --> 27:25:41,360 be disappointed because the compiler is 36906 27:25:39,279 --> 27:25:43,679 going to slice off derived class 36907 27:25:41,360 --> 27:25:46,479 information and all you're going to be 36908 27:25:43,679 --> 27:25:48,871 left with is base class information here 36909 27:25:46,479 --> 27:25:51,119 that's why this is going to call ship 36910 27:25:48,872 --> 27:25:52,960 draw let's build and run and really make 36911 27:25:51,119 --> 27:25:55,911 you see this this is going to go through 36912 27:25:52,960 --> 27:25:58,160 gcc our favorite compiler the bullet is 36913 27:25:55,911 --> 27:26:00,399 going to be good we cannot clear and run 36914 27:25:58,160 --> 27:26:03,192 rooster you're going to see that it is 36915 27:26:00,399 --> 27:26:06,639 going to say shape draw even if we try 36916 27:26:03,191 --> 27:26:08,399 to assign a circle to a shape here this 36917 27:26:06,639 --> 27:26:11,039 is going to disappoint us because this 36918 27:26:08,399 --> 27:26:12,399 is really going to slice off circle and 36919 27:26:12,399 --> 27:26:18,479 and it is just going to leave in the 36920 27:26:14,800 --> 27:26:19,760 shape nucleus that we have in our circle 36921 27:26:19,759 --> 27:26:23,911 if i can say it like that this is really 36922 27:26:22,080 --> 27:26:26,080 all i had to share in this lecture i 36923 27:26:23,911 --> 27:26:27,911 hope you found it interesting we are 36924 27:26:26,080 --> 27:26:29,680 going to stop here in this lecture in 36925 27:26:27,911 --> 27:26:32,800 the next one we're going to see what 36926 27:26:29,679 --> 27:26:35,039 happens if we try to store polymorphic 36927 27:26:35,039 --> 27:26:39,279 go ahead and finish up here and meet me 36928 27:26:37,279 --> 27:26:42,399 there in this lecture we're going to 36929 27:26:39,279 --> 27:26:46,399 explore what happens if you try to 36930 27:26:42,399 --> 27:26:48,639 directly store polymorphic objects in a 36931 27:26:48,639 --> 27:26:54,551 in the last lecture we have seen that if 36932 27:26:50,872 --> 27:26:55,760 you assign a derived object to a base 36933 27:26:55,759 --> 27:27:01,439 the data from the derived object is 36934 27:26:58,320 --> 27:27:04,720 going to be basically sliced off if you 36935 27:27:01,440 --> 27:27:07,440 assign an oval to a shape the oval part 36936 27:27:04,720 --> 27:27:10,232 of the object is going to be sliced off 36937 27:27:07,440 --> 27:27:12,960 and we're going to be left with only 36938 27:27:10,232 --> 27:27:15,440 shaped data that we assigned to our 36939 27:27:12,960 --> 27:27:18,552 shape object we saw that this was 36940 27:27:15,440 --> 27:27:20,400 referred to as slicing in c plus plus 36941 27:27:18,551 --> 27:27:23,191 and the same is going to happen if you 36942 27:27:20,399 --> 27:27:25,279 assign a circle object to a shape the 36943 27:27:23,191 --> 27:27:26,231 circle information is going to be sliced 36944 27:27:26,232 --> 27:27:31,040 then the oval information is also going 36945 27:27:29,039 --> 27:27:34,399 to be sliced off and we're going to be 36946 27:27:31,039 --> 27:27:38,079 left with the inner shape part 36947 27:27:34,399 --> 27:27:39,911 that we assign to our shape object here 36948 27:27:38,080 --> 27:27:42,639 now i want you to see that this can 36949 27:27:39,911 --> 27:27:45,512 happen indirectly in ways that you might 36950 27:27:42,639 --> 27:27:48,080 not expect and we are going to use our 36951 27:27:45,512 --> 27:27:50,551 inheritance hierarchy again the base 36952 27:27:48,080 --> 27:27:53,040 class is going to be shape we're going 36953 27:27:50,551 --> 27:27:55,512 to inherit from that and get oval and 36954 27:27:53,039 --> 27:27:57,759 we're going to inherit from oval and get 36955 27:27:57,759 --> 27:28:03,039 so let's look at a piece of code here we 36956 27:28:00,639 --> 27:28:06,080 are creating a bunch of objects we have 36957 27:28:03,039 --> 27:28:08,399 a bunch of circles and ovals and we are 36958 27:28:06,080 --> 27:28:11,360 setting up an array that is going to 36959 27:28:11,360 --> 27:28:19,360 but what we are doing is storing our 36960 27:28:14,800 --> 27:28:22,000 pre created objects in our array here 36961 27:28:19,360 --> 27:28:25,512 and from what you already know if you 36962 27:28:22,000 --> 27:28:27,759 store objects in an array like this this 36963 27:28:25,512 --> 27:28:30,320 is going to create copies even if you 36964 27:28:27,759 --> 27:28:33,360 can't see that directly so what we have 36965 27:28:30,320 --> 27:28:36,640 in our array here is not dirt so what we 36966 27:28:33,360 --> 27:28:39,039 have in our array here is not any kind 36967 27:28:36,639 --> 27:28:42,399 of direct reference to the object we 36968 27:28:39,039 --> 27:28:44,319 have on top here these are just copies 36969 27:28:42,399 --> 27:28:47,119 and notice what is happening we are 36970 27:28:47,119 --> 27:28:52,959 in spots that are designed to store 36971 27:28:49,911 --> 27:28:54,551 direct shape objects and you might guess 36972 27:28:52,960 --> 27:28:56,160 what this is going to do this is going 36973 27:28:56,160 --> 27:29:01,120 circle and oval information and only 36974 27:28:59,191 --> 27:29:03,440 shape information is going to be stored 36975 27:29:01,119 --> 27:29:05,679 in this array here so if we happen to 36976 27:29:03,440 --> 27:29:07,040 look through this array like we do here 36977 27:29:09,119 --> 27:29:14,720 this is not going to call the most 36978 27:29:11,600 --> 27:29:16,872 specific drill method for circle or oval 36979 27:29:14,720 --> 27:29:19,831 this is just going to call the shape 36980 27:29:16,872 --> 27:29:22,400 version of our draw method here this is 36981 27:29:19,831 --> 27:29:24,799 something i want you to see okay some of 36982 27:29:22,399 --> 27:29:27,279 you might say what if i take my 36983 27:29:24,800 --> 27:29:29,360 shape information in here and assign 36984 27:29:27,279 --> 27:29:31,679 that to a pointer and then go through 36985 27:29:29,360 --> 27:29:34,232 that pointer to call the draw method 36986 27:29:31,679 --> 27:29:36,639 well i hate to break it to you but the 36987 27:29:34,232 --> 27:29:39,040 moment your data is sliced off in a 36988 27:29:36,639 --> 27:29:41,279 situation like this you'll never get 36989 27:29:39,039 --> 27:29:43,511 that data back so even if you try and 36990 27:29:41,279 --> 27:29:45,831 take the object and sign that to a 36991 27:29:43,512 --> 27:29:48,872 pointer or a reference you're never 36992 27:29:45,831 --> 27:29:51,119 going to get polymorphic data again the 36993 27:29:48,872 --> 27:29:53,760 data has been lost permanently and you 36994 27:29:51,119 --> 27:29:55,679 never get that so even if you try to go 36995 27:29:53,759 --> 27:29:58,000 through a pointer or a reference to 36996 27:29:55,679 --> 27:30:00,719 manage your object that we have in this 36997 27:29:58,000 --> 27:30:03,279 array if we call the draw method we want 36998 27:30:00,720 --> 27:30:06,080 to get polymorphic behavior we will get 36999 27:30:03,279 --> 27:30:09,039 to the shape version of our draw method 37000 27:30:06,080 --> 27:30:11,911 called so the data has been sliced off 37001 27:30:09,039 --> 27:30:15,191 permanently if we store our derived 37002 27:30:11,911 --> 27:30:18,800 objects in an array like this or even if 37003 27:30:15,191 --> 27:30:21,440 we directly assign a derived object to a 37004 27:30:18,800 --> 27:30:24,400 base object this is going to slice off 37005 27:30:21,440 --> 27:30:27,040 the data and we will never get it back 37006 27:30:24,399 --> 27:30:29,679 anyway you should really know this okay 37007 27:30:27,039 --> 27:30:31,679 anytime i try to explain this 37008 27:30:29,679 --> 27:30:34,799 students mostly come up with this 37009 27:30:31,679 --> 27:30:37,511 question what if i try to store 37010 27:30:37,512 --> 27:30:42,160 and do something like this now this is 37011 27:30:39,759 --> 27:30:45,191 not going to work and this is not even 37012 27:30:42,160 --> 27:30:48,080 go past the compilation stage of your 37013 27:30:45,191 --> 27:30:50,799 program the reason is we can't store 37014 27:30:48,080 --> 27:30:53,040 references in a collection and the 37015 27:30:53,039 --> 27:30:59,599 assignability rule you can't assign to a 37016 27:30:56,800 --> 27:31:02,160 reference and i changed the reference to 37017 27:30:59,600 --> 27:31:04,480 store somewhere else and it really drive 37018 27:31:02,160 --> 27:31:07,040 this home let's look at this example 37019 27:31:04,479 --> 27:31:09,759 here we have a variable called a it is 37020 27:31:07,039 --> 27:31:12,231 of entertype and we have a 56 value 37021 27:31:09,759 --> 27:31:14,479 inside and we set up a reference which 37022 27:31:12,232 --> 27:31:17,440 is going to be referencing the data we 37023 27:31:14,479 --> 27:31:20,231 have in a and down on this line here we 37024 27:31:17,440 --> 27:31:22,400 try to change the data in our reference 37025 27:31:20,232 --> 27:31:25,760 but this is not going to be changing 37026 27:31:22,399 --> 27:31:28,479 where this reference is pointing to or 37027 27:31:25,759 --> 27:31:32,319 where this reference is referencing this 37028 27:31:28,479 --> 27:31:35,911 will merely be changing the values in 37029 27:31:32,320 --> 27:31:38,480 our reference here and once we have this 37030 27:31:35,911 --> 27:31:41,119 reference we'll never be able to change 37031 27:31:38,479 --> 27:31:42,800 where it is pointing again so from this 37032 27:31:41,119 --> 27:31:46,080 hopefully you can understand that 37033 27:31:42,800 --> 27:31:48,960 references are not left assignable you 37034 27:31:46,080 --> 27:31:51,191 can't store thanks that are not left 37035 27:31:51,191 --> 27:31:57,119 because an array is designed to modify 37036 27:31:54,080 --> 27:31:59,279 the data of what is stored inside so if 37037 27:31:57,119 --> 27:32:01,512 we set up an array like this 37038 27:31:59,279 --> 27:32:04,000 the compiler thinks that at some later 37039 27:32:01,512 --> 27:32:06,400 point you will want to assign 37040 27:32:04,000 --> 27:32:08,639 other data to what is already stored in 37041 27:32:06,399 --> 27:32:10,959 this array and there is a hard 37042 27:32:08,639 --> 27:32:13,600 requirement on the data you store in 37043 27:32:10,960 --> 27:32:16,400 your collections for the type of that 37044 27:32:13,600 --> 27:32:18,800 data at least to be left assignable 37045 27:32:16,399 --> 27:32:21,039 and references are not left assignable 37046 27:32:18,800 --> 27:32:22,872 so we can't store them in a collection 37047 27:32:21,039 --> 27:32:25,191 like this if we're trying to do this 37048 27:32:22,872 --> 27:32:26,960 we're going to get a compiler error but 37049 27:32:25,191 --> 27:32:28,799 again some of you are going to say what 37050 27:32:26,960 --> 27:32:31,600 if we store in pointers well this is 37051 27:32:28,800 --> 27:32:33,911 going to work this is the basic working 37052 27:32:33,911 --> 27:32:39,479 going through base pointers to manage 37053 27:32:36,320 --> 27:32:42,000 derived objects this is going to use 37054 27:32:39,479 --> 27:32:44,551 polymorphism and here we will get the 37055 27:32:44,551 --> 27:32:49,119 so if our object is a circle we will 37056 27:32:46,960 --> 27:32:51,280 call the draw method on the circle 37057 27:32:49,119 --> 27:32:54,959 object if it is a novel we're going to 37058 27:32:51,279 --> 27:32:57,831 call the draw method for the over part 37059 27:32:54,960 --> 27:33:00,639 of our object here this is even going to 37060 27:32:57,831 --> 27:33:03,279 work if we go through smart pointers so 37061 27:33:00,639 --> 27:33:06,160 if we do something like this and store 37062 27:33:03,279 --> 27:33:08,639 in smart pointers and create our smart 37063 27:33:06,160 --> 27:33:10,480 pointers on the fly in our initializer 37064 27:33:08,639 --> 27:33:13,279 here this is going to work because 37065 27:33:10,479 --> 27:33:16,080 really a smart pointer is a wrapper on 37066 27:33:13,279 --> 27:33:18,720 top of the regular roll pointer in c 37067 27:33:16,080 --> 27:33:21,119 plus plus so again the message here is 37068 27:33:18,720 --> 27:33:24,080 that you need to be careful on what you 37069 27:33:21,119 --> 27:33:26,319 store in your collections if it is a raw 37070 27:33:24,080 --> 27:33:29,911 piece of ghana your data is going to be 37071 27:33:26,320 --> 27:33:32,320 sliced off if you store derived objects 37072 27:33:29,911 --> 27:33:33,360 in a collection that is designed to 37073 27:33:33,360 --> 27:33:38,160 base class data if you store any 37074 27:33:36,232 --> 27:33:40,720 references that's not even going to 37075 27:33:38,160 --> 27:33:43,512 compile but if you're storing any kind 37076 27:33:40,720 --> 27:33:46,160 of pointer row or smart this is going to 37077 27:33:43,512 --> 27:33:48,400 work because polymorphism was designed 37078 27:33:46,160 --> 27:33:50,639 in the first place to work with base 37079 27:33:48,399 --> 27:33:52,871 pointers that are managing derived 37080 27:33:50,639 --> 27:33:55,279 objects this is really cool and i want 37081 27:33:52,872 --> 27:33:56,639 you to be aware of this now that you 37082 27:33:55,279 --> 27:33:58,551 know this we're going to head over to 37083 27:33:56,639 --> 27:34:01,191 visual studio code and play with this a 37084 27:33:58,551 --> 27:34:03,039 little more here we are in our working 37085 27:34:01,191 --> 27:34:05,512 folder the current project is 37086 27:34:03,039 --> 27:34:07,911 polymorphic objects stored in 37087 27:34:05,512 --> 27:34:10,160 collections we are going to reuse the 37088 27:34:07,911 --> 27:34:13,440 code from the last lecture because we 37089 27:34:10,160 --> 27:34:15,680 need our inheritance hierarchy here and 37090 27:34:13,440 --> 27:34:17,360 this is going to serve us just well 37091 27:34:17,360 --> 27:34:22,399 and we are going to head over in our 37092 27:34:20,160 --> 27:34:24,000 current project and put in our code and 37093 27:34:22,399 --> 27:34:26,000 then we're going to open this in visual 37094 27:34:24,000 --> 27:34:28,080 studio code pretty quick let's do this 37095 27:34:26,000 --> 27:34:30,080 this is going to give us our code and we 37096 27:34:28,080 --> 27:34:32,960 have our shape class which is really 37097 27:34:30,080 --> 27:34:33,911 what we already know we have our oval 37098 27:34:33,911 --> 27:34:39,279 and we have our circle class take a good 37099 27:34:36,639 --> 27:34:40,720 look at this and what we want to see is 37100 27:34:40,720 --> 27:34:47,119 if we use different kinds of setups to 37101 27:34:44,320 --> 27:34:49,912 store objects of this inheritance 37102 27:34:47,119 --> 27:34:52,871 hierarchy in a collection like an array 37103 27:34:49,911 --> 27:34:54,959 let's go back to our main cpp file and 37104 27:34:52,872 --> 27:34:56,551 we are going to take out what we have in 37105 27:34:54,960 --> 27:34:59,120 here we're going to set up what we're 37106 27:34:56,551 --> 27:35:00,872 going to be using in this lecture we are 37107 27:34:59,119 --> 27:35:02,871 going to set up a bunch of objects that 37108 27:35:00,872 --> 27:35:05,600 we're going to be using here basically a 37109 27:35:02,872 --> 27:35:08,320 bunch of circles and ovals we can close 37110 27:35:05,600 --> 27:35:10,400 the left sidebar here and have some more 37111 27:35:08,320 --> 27:35:12,872 breathing room after this we're going to 37112 27:35:10,399 --> 27:35:16,479 set up an array which is going to store 37113 27:35:12,872 --> 27:35:18,960 copies of this object in this array here 37114 27:35:16,479 --> 27:35:20,720 which is called shapes one and after 37115 27:35:18,960 --> 27:35:23,192 this we're going to try and loop through 37116 27:35:20,720 --> 27:35:26,320 this array and try to do things with it 37117 27:35:23,191 --> 27:35:30,080 for example we can say for shape and we 37118 27:35:26,320 --> 27:35:33,280 may want to call the draw method on our 37119 27:35:30,080 --> 27:35:35,512 copy here so for example we can say s 37120 27:35:33,279 --> 27:35:36,959 and say draw going to see what happens 37121 27:35:36,960 --> 27:35:41,360 let's say references because we don't 37122 27:35:41,360 --> 27:35:46,479 range based for loop here so this is 37123 27:35:43,911 --> 27:35:48,000 going to avoid copies we do in this loop 37124 27:35:46,479 --> 27:35:50,231 and we're going to be directly 37125 27:35:48,000 --> 27:35:51,440 referencing whatever we have in this 37126 27:35:51,440 --> 27:35:56,320 now what do you expect to get we have 37127 27:35:53,831 --> 27:35:59,119 derived objects in this array 37128 27:35:56,320 --> 27:36:01,832 and with the setup we have here because 37129 27:35:59,119 --> 27:36:04,159 we are using virtual functions 37130 27:36:04,160 --> 27:36:08,160 polymorphic behavior so here we would 37131 27:36:08,160 --> 27:36:13,192 joe versions in circle and oval to be 37132 27:36:11,191 --> 27:36:14,479 called but that's not what is going to 37133 27:36:13,191 --> 27:36:16,799 happen we're going to see that we're 37134 27:36:14,479 --> 27:36:19,679 going to be calling shape versions of 37135 27:36:16,800 --> 27:36:21,911 our draw methods let's try and build and 37136 27:36:19,679 --> 27:36:24,551 show you this we're going to world with 37137 27:36:21,911 --> 27:36:26,959 gcc our favorite compiler the build is 37138 27:36:24,551 --> 27:36:29,512 going to go through we can kill this and 37139 27:36:26,960 --> 27:36:31,440 bring up a powershell window to allow us 37140 27:36:29,512 --> 27:36:33,191 to run our program we're going to say 37141 27:36:31,440 --> 27:36:36,400 rooster here and you're going to see 37142 27:36:33,191 --> 27:36:38,720 that we are drawing circle one over one 37143 27:36:36,399 --> 27:36:41,679 circle two over two circle three oval 37144 27:36:38,720 --> 27:36:44,320 three but we are calling the shape 37145 27:36:44,320 --> 27:36:50,800 and the reason is we are slicing off 37146 27:36:47,440 --> 27:36:53,191 derived class information in our objects 37147 27:36:50,800 --> 27:36:56,800 the moment we try to make a copy and 37148 27:36:53,191 --> 27:37:00,080 copy in a spot that is designed to store 37149 27:36:56,800 --> 27:37:01,832 objects of the base class so this is 37150 27:37:00,080 --> 27:37:02,872 basically what we saw in the last 37151 27:37:02,872 --> 27:37:08,872 on the slicing but it is disguised 37152 27:37:05,679 --> 27:37:11,439 because we are storing data in an array 37153 27:37:08,872 --> 27:37:13,279 and when you store data in an array like 37154 27:37:11,440 --> 27:37:16,639 this you're going to be making copies 37155 27:37:13,279 --> 27:37:18,160 and storing copies in your array and in 37156 27:37:16,639 --> 27:37:20,800 any copy you make you're going to be 37157 27:37:18,160 --> 27:37:23,760 basically slicing off oval and circle 37158 27:37:20,800 --> 27:37:26,551 information and only storing in shape 37159 27:37:23,759 --> 27:37:28,399 information for our objects here this is 37160 27:37:26,551 --> 27:37:30,800 what the compiler is going to do and 37161 27:37:28,399 --> 27:37:34,000 that really try and prove this we can 37162 27:37:30,800 --> 27:37:36,800 try and print the size of these objects 37163 27:37:34,000 --> 27:37:39,191 and see the sizes of these objects in 37164 27:37:36,800 --> 27:37:43,760 this array here when we loop so for 37165 27:37:39,191 --> 27:37:47,279 example we can say stdc out size of 37166 27:37:43,759 --> 27:37:49,759 circle one do we have circle one yes we 37167 27:37:47,279 --> 27:37:50,959 do so we're going to say size of circle 37168 27:37:50,960 --> 27:37:55,040 and we're going to put its size 37169 27:37:52,399 --> 27:37:57,191 information out here to be seen by 37170 27:37:55,039 --> 27:37:59,039 everybody we're going to say size of 37171 27:37:57,191 --> 27:38:01,360 circle one and we're going to print it 37172 27:37:59,039 --> 27:38:04,479 out and we are also going to print the 37173 27:38:01,360 --> 27:38:06,399 size of whatever iteration we are at in 37174 27:38:04,479 --> 27:38:08,000 our loop here that's the first thing 37175 27:38:06,399 --> 27:38:10,639 we're going to prep and we're going to 37176 27:38:08,000 --> 27:38:13,911 say size of object and we're going to 37177 27:38:10,639 --> 27:38:15,679 say size of s this is going to do 37178 27:38:13,911 --> 27:38:17,831 and we're going to loop and see if the 37179 27:38:15,679 --> 27:38:20,479 information we have on the outside is 37180 27:38:17,831 --> 27:38:22,871 the same as we have on the outside again 37181 27:38:20,479 --> 27:38:24,800 we are printing this information on the 37182 27:38:24,800 --> 27:38:29,512 which is really not yet in our array and 37183 27:38:29,512 --> 27:38:35,760 bring this in front of our array to make 37184 27:38:32,639 --> 27:38:38,160 this super clear and in our loop we are 37185 27:38:35,759 --> 27:38:41,119 printing information on a copy that we 37186 27:38:38,160 --> 27:38:44,232 have in our array because this is the 37187 27:38:41,119 --> 27:38:45,911 real deal we have in our array because 37188 27:38:44,232 --> 27:38:48,320 this is a reference hopefully this is 37189 27:38:45,911 --> 27:38:51,191 going to make sense and prove to you 37190 27:38:48,320 --> 27:38:53,680 that we are going to be working with 37191 27:38:51,191 --> 27:38:57,119 derived objects and the data is going to 37192 27:38:53,679 --> 27:38:58,959 be sliced off if we copy the data in our 37193 27:38:57,119 --> 27:39:01,119 array here this is what i want you to 37194 27:38:58,960 --> 27:39:03,192 see so we're going to run this again 37195 27:39:01,119 --> 27:39:04,720 we're going to pass this through gcc the 37196 27:39:03,191 --> 27:39:06,080 build is going to be good we're going to 37197 27:39:07,759 --> 27:39:12,871 now let's look at what we have here size 37198 27:39:10,000 --> 27:39:16,000 of circle 1 is 56 but you're going to 37199 27:39:12,872 --> 27:39:17,440 see that the size for all our objects in 37200 27:39:17,440 --> 27:39:23,191 has shrinked to 40 because derived class 37201 27:39:20,720 --> 27:39:26,232 information has been sliced off the 37202 27:39:23,191 --> 27:39:28,551 moment we copied our data in our array 37203 27:39:26,232 --> 27:39:30,872 here and this is what i want you to see 37204 27:39:28,551 --> 27:39:33,831 and i think we can even see this if we 37205 27:39:30,872 --> 27:39:36,551 go through our debugger for example we 37206 27:39:36,551 --> 27:39:42,080 and uh open our run tab and if we try to 37207 27:39:40,080 --> 27:39:44,800 run our program through the debugger the 37208 27:39:42,080 --> 27:39:47,600 build is going to be good and the binary 37209 27:39:44,800 --> 27:39:49,279 is going to be passed into our debugger 37210 27:39:47,600 --> 27:39:52,000 and we're going to hit our break point 37211 27:39:49,279 --> 27:39:54,160 in a minute so let's wait for this and 37212 27:39:52,000 --> 27:39:56,000 we have hit our break point here but 37213 27:39:56,000 --> 27:40:01,759 is look at our local variables on the 37214 27:39:59,119 --> 27:40:03,831 left let's look at our array the array 37215 27:40:03,831 --> 27:40:09,439 and if we go up we're going to see our 37216 27:40:09,440 --> 27:40:13,279 and we're going to see that we have five 37217 27:40:11,279 --> 27:40:14,872 indexes here because we have six 37218 27:40:14,872 --> 27:40:19,680 we can expand and see what we have at 37219 27:40:17,360 --> 27:40:22,320 index zero and you're going to see that 37220 27:40:19,679 --> 27:40:24,551 we just have description data because 37221 27:40:22,320 --> 27:40:26,480 this is a shape if we go back in our 37222 27:40:24,551 --> 27:40:28,479 class we're going to see that the only 37223 27:40:26,479 --> 27:40:31,191 thing we have in our shape class is 37224 27:40:28,479 --> 27:40:34,000 really a description member variable and 37225 27:40:31,191 --> 27:40:36,319 it is what we see in our array here just 37226 27:40:34,000 --> 27:40:39,039 description information because this is 37227 27:40:36,320 --> 27:40:41,360 a shaped object and we have lost or 37228 27:40:39,039 --> 27:40:43,599 derived class information and this is 37229 27:40:41,360 --> 27:40:45,831 really cool but if we go on top and for 37230 27:40:43,600 --> 27:40:48,480 example expand circle one we're going to 37231 27:40:45,831 --> 27:40:50,639 see that we have a novel part of us and 37232 27:40:48,479 --> 27:40:53,360 we have a ship part of us but these 37233 27:40:50,639 --> 27:40:56,551 events have been stripped off in our 37234 27:40:53,360 --> 27:40:59,440 array when we slice off the data from 37235 27:40:56,551 --> 27:41:01,831 derived objects to only store shape 37236 27:40:59,440 --> 27:41:04,000 information in our array here i hope 37237 27:41:01,831 --> 27:41:06,080 this makes sense now we're going to kill 37238 27:41:04,000 --> 27:41:08,479 our debugging session because we don't 37239 27:41:06,080 --> 27:41:10,960 need this anymore and we are going to 37240 27:41:08,479 --> 27:41:13,440 comment this out and see some other 37241 27:41:13,440 --> 27:41:18,551 comment with our data here we're going 37242 27:41:15,191 --> 27:41:21,360 to say if you store row derived objects 37243 27:41:18,551 --> 27:41:23,440 in an array which was set up to store 37244 27:41:21,360 --> 27:41:25,360 base class object the data is going to 37245 27:41:23,440 --> 27:41:27,680 be sliced off this is something i want 37246 27:41:25,360 --> 27:41:28,479 you to know but some of you are going to 37247 27:41:28,479 --> 27:41:32,399 what if we store it in references okay 37248 27:41:30,872 --> 27:41:34,160 so let's try and do that we're going to 37249 27:41:32,399 --> 27:41:35,831 set up a piece of code like this and 37250 27:41:34,160 --> 27:41:38,400 we're going to set up an array called 37251 27:41:35,831 --> 27:41:40,639 shapes2 and this array is going to be 37252 27:41:38,399 --> 27:41:43,279 basically storing references to the 37253 27:41:40,639 --> 27:41:44,800 objects we have on top here and we're 37254 27:41:43,279 --> 27:41:47,831 going to see what happens you notice 37255 27:41:44,800 --> 27:41:50,479 that we have a squiggly line already and 37256 27:41:47,831 --> 27:41:52,479 this is saying this is not going to work 37257 27:41:50,479 --> 27:41:54,399 because references can't really be 37258 27:41:54,399 --> 27:41:58,079 again as we saw in the slides the reason 37259 27:41:58,080 --> 27:42:03,440 references are not left assignable and 37260 27:42:00,960 --> 27:42:06,480 they can't be stored in a collection 37261 27:42:03,440 --> 27:42:09,191 like an array here the basic requirement 37262 27:42:06,479 --> 27:42:11,599 on the types that you can store in your 37263 27:42:09,191 --> 27:42:15,119 array is that they have to be left 37264 27:42:11,600 --> 27:42:17,192 assignable and our references are not 37265 27:42:15,119 --> 27:42:20,319 rest assignable so this is not going to 37266 27:42:17,191 --> 27:42:21,599 compile if we try to build our program 37267 27:42:20,320 --> 27:42:24,080 we're going to see that we're going to 37268 27:42:24,080 --> 27:42:27,911 and the build is going to finish with 37269 27:42:25,911 --> 27:42:31,191 errors and if we go up we're going to 37270 27:42:27,911 --> 27:42:33,512 see that declaration of shapes to us 37271 27:42:31,191 --> 27:42:35,679 array of references and this is not 37272 27:42:33,512 --> 27:42:38,479 allowed you can't have an array of 37273 27:42:35,679 --> 27:42:39,911 references in c plus plus and this is 37274 27:42:38,479 --> 27:42:42,159 the reason why so we're going to get a 37275 27:42:39,911 --> 27:42:43,599 compiler error here okay so now you know 37276 27:42:42,160 --> 27:42:45,279 this but some of you are going to say 37277 27:42:43,600 --> 27:42:47,232 what if we go through pointers well 37278 27:42:47,232 --> 27:42:53,040 polymorphism through virtual functions 37279 27:42:50,720 --> 27:42:55,680 was basically designed to work with base 37280 27:42:53,039 --> 27:42:58,231 pointers that are managing derived 37281 27:42:55,679 --> 27:43:00,399 objects and this is what we have here so 37282 27:42:58,232 --> 27:43:03,440 if we look we're going to be getting 37283 27:43:00,399 --> 27:43:05,511 polymorphic behavior and if we call our 37284 27:43:03,440 --> 27:43:08,479 draw method on our pointer here we're 37285 27:43:05,512 --> 27:43:10,872 going to get the most specific 37286 27:43:08,479 --> 27:43:13,039 joe method called and this is what we 37287 27:43:10,872 --> 27:43:14,720 want so we're going to try and weld this 37288 27:43:13,039 --> 27:43:16,551 again and show you that this is going to 37289 27:43:14,720 --> 27:43:17,680 work the build is going to be good we 37290 27:43:17,679 --> 27:43:22,479 and run rooster and we're going to see 37291 27:43:20,080 --> 27:43:24,080 that we are going to be looping around 37292 27:43:22,479 --> 27:43:26,720 and if we call the draw method we're 37293 27:43:24,080 --> 27:43:29,119 going to call the overdraw method circle 37294 27:43:26,720 --> 27:43:31,360 draw method overdraw method circle draw 37295 27:43:29,119 --> 27:43:33,440 method this is basically what we want 37296 27:43:31,360 --> 27:43:36,232 and we are going through base pointers 37297 27:43:33,440 --> 27:43:37,911 to really use polymorphism here this is 37298 27:43:36,232 --> 27:43:40,232 definitely going to work if you go 37299 27:43:37,911 --> 27:43:42,399 through smart pointers so let's comment 37300 27:43:40,232 --> 27:43:44,552 this out and set up another example to 37301 27:43:42,399 --> 27:43:46,720 really drive this home you can really 37302 27:43:44,551 --> 27:43:50,159 use any kind of smart pointer you can 37303 27:43:46,720 --> 27:43:52,639 use unique pointer or shared pointers so 37304 27:43:50,160 --> 27:43:54,480 here we will just be storing in shared 37305 27:43:52,639 --> 27:43:56,639 pointers because this is easier to 37306 27:43:54,479 --> 27:43:58,800 handle and we're going to head over to 37307 27:43:56,639 --> 27:44:01,191 the top and include the memory header if 37308 27:43:58,800 --> 27:44:03,832 you remember if you want to use smart 37309 27:44:01,191 --> 27:44:05,911 pointers in c plus plus you will have to 37310 27:44:03,831 --> 27:44:08,639 include the memory header which brings 37311 27:44:05,911 --> 27:44:10,399 in the data to set up smart pointers in 37312 27:44:10,399 --> 27:44:14,959 so this is going to look good now and if 37313 27:44:13,360 --> 27:44:18,000 we build we're going to get the most 37314 27:44:14,960 --> 27:44:20,800 specific version of our draw method 37315 27:44:18,000 --> 27:44:23,119 called through polymorphism that is 37316 27:44:20,800 --> 27:44:24,872 using virtual functions here so let's 37317 27:44:23,119 --> 27:44:26,871 build and show you that this is going to 37318 27:44:24,872 --> 27:44:28,080 work the book is going to be good we can 37319 27:44:28,080 --> 27:44:33,680 and run rooster and we are going to get 37320 27:44:30,551 --> 27:44:36,159 our versions of draw called the version 37321 27:44:33,679 --> 27:44:38,639 for a circle and the version for oval 37322 27:44:36,160 --> 27:44:41,680 because we are using base pointers to 37323 27:44:38,639 --> 27:44:44,000 manage derived objects here and our 37324 27:44:41,679 --> 27:44:46,479 class inheritance hierarchy is using 37325 27:44:44,000 --> 27:44:48,399 virtual functions for the draw method 37326 27:44:46,479 --> 27:44:50,399 here so this is going to work this is 37327 27:44:48,399 --> 27:44:53,279 really all i wanted you to see in this 37328 27:44:50,399 --> 27:44:54,639 lecture you should be careful if you try 37329 27:44:54,639 --> 27:45:01,679 derived objects in an array that was 37330 27:44:58,320 --> 27:45:03,760 designed to store base class data if you 37331 27:45:01,679 --> 27:45:05,039 are using raw data you're going to get 37332 27:45:05,039 --> 27:45:09,679 and you really need to be aware of this 37333 27:45:07,512 --> 27:45:12,000 because you want to have derived class 37334 27:45:09,679 --> 27:45:13,759 information if you try to store any 37335 27:45:12,000 --> 27:45:16,232 references this is not even going to 37336 27:45:13,759 --> 27:45:18,399 compile because references are not left 37337 27:45:16,232 --> 27:45:20,960 assignable but this is going to work if 37338 27:45:18,399 --> 27:45:23,119 you use role pointers or smart pointers 37339 27:45:20,960 --> 27:45:25,760 because polymorphism was basically 37340 27:45:23,119 --> 27:45:28,720 designed to work with base pointers or 37341 27:45:25,759 --> 27:45:32,159 references that are managing a derived 37342 27:45:28,720 --> 27:45:34,000 object which uses polymorphism we are 37343 27:45:32,160 --> 27:45:35,760 going to stop here in this lecture the 37344 27:45:34,000 --> 27:45:39,191 next one we're going to learn about the 37345 27:45:35,759 --> 27:45:41,360 override mechanism in c plus plus go 37346 27:45:39,191 --> 27:45:42,871 ahead and finish up here and meet me 37347 27:45:41,360 --> 27:45:45,831 there in this lecture we're going to 37348 27:45:42,872 --> 27:45:49,440 learn about the override specification 37349 27:45:45,831 --> 27:45:52,479 a kind of setup you can do to avoid 37350 27:45:49,440 --> 27:45:54,800 errors in your inheritance hierarchies 37351 27:45:52,479 --> 27:45:58,000 if you use virtual functions to set up 37352 27:45:54,800 --> 27:46:00,639 polymorphic behavior for your derived 37353 27:45:58,000 --> 27:46:03,360 objects and this is something that was 37354 27:46:00,639 --> 27:46:04,720 set up in ziplus plus to avoid easy to 37355 27:46:04,720 --> 27:46:10,232 for example if you have an inheritance 37356 27:46:07,440 --> 27:46:12,960 hierarchy like this you might be meaning 37357 27:46:10,232 --> 27:46:15,440 to set up a draw overrides for what we 37358 27:46:12,960 --> 27:46:17,912 have in the base class and an override 37359 27:46:17,911 --> 27:46:23,512 method that you have in your derived 37360 27:46:20,551 --> 27:46:25,759 class to really hide what we had in the 37361 27:46:23,512 --> 27:46:28,000 base class for example the draw method 37362 27:46:25,759 --> 27:46:30,399 novel is going to be overriding or 37363 27:46:30,399 --> 27:46:35,119 and the draw method in circle is going 37364 27:46:32,479 --> 27:46:38,000 to be overriding or hiding what we have 37365 27:46:35,119 --> 27:46:40,720 in oval this is the idea here but as you 37366 27:46:38,000 --> 27:46:42,720 set up your overrides you might make 37367 27:46:40,720 --> 27:46:46,639 mistakes for example you may mean to say 37368 27:46:42,720 --> 27:46:49,040 draw and i use a w that is in uppercase 37369 27:46:46,639 --> 27:46:51,279 and that's going to be bad because what 37370 27:46:49,039 --> 27:46:54,159 you will be putting in oval is going to 37371 27:46:51,279 --> 27:46:56,232 be a completely separate method that is 37372 27:46:54,160 --> 27:46:58,000 different from what we have on the top 37373 27:46:56,232 --> 27:46:59,832 and this is going to be really bad 37374 27:46:58,000 --> 27:47:02,639 you're not going to get polymorphic 37375 27:46:59,831 --> 27:47:05,119 behavior for the draw method in oval 37376 27:47:02,639 --> 27:47:07,440 because we are simply not overriding the 37377 27:47:05,119 --> 27:47:10,319 jaw method from shape and this is going 37378 27:47:07,440 --> 27:47:14,232 to be bad so in ziploc plus we can avoid 37379 27:47:10,320 --> 27:47:16,960 these problems by explicitly specifying 37380 27:47:14,232 --> 27:47:19,360 that the draw method we have in alpha is 37381 27:47:16,960 --> 27:47:23,192 an override and we can add this 37382 27:47:19,360 --> 27:47:25,440 specification after our method header 37383 27:47:23,191 --> 27:47:28,231 and the c plus plus compiler is going to 37384 27:47:25,440 --> 27:47:31,040 enforce that we are actually overriding 37385 27:47:28,232 --> 27:47:34,232 a method that exists in our parents 37386 27:47:31,039 --> 27:47:36,551 class which happens to be shape here if 37387 27:47:34,232 --> 27:47:38,160 we have no method called draw in the 37388 27:47:36,551 --> 27:47:40,551 base class we're going to get a compiler 37389 27:47:38,160 --> 27:47:42,960 error because we want to be basically 37390 27:47:40,551 --> 27:47:45,679 overriding any method here this is going 37391 27:47:42,960 --> 27:47:48,552 to protect us from problems if we make 37392 27:47:45,679 --> 27:47:51,191 typos in setting up our polymorphic 37393 27:47:48,551 --> 27:47:53,512 methods here this is the main thing we 37394 27:47:51,191 --> 27:47:55,759 can achieve with the override 37395 27:47:53,512 --> 27:47:57,119 specification now that you know this 37396 27:47:55,759 --> 27:47:58,871 we're going to head over to visual 37397 27:47:57,119 --> 27:48:01,759 studio code and play with us a little 37398 27:47:58,872 --> 27:48:04,080 more here we are in our working folder 37399 27:48:01,759 --> 27:48:05,599 the current project is override here 37400 27:48:04,080 --> 27:48:07,680 we're going to grab the code from the 37401 27:48:05,600 --> 27:48:09,912 last lecture because we are basically 37402 27:48:07,679 --> 27:48:10,871 going to be reusing the same code over 37403 27:48:10,872 --> 27:48:16,160 building on top of that if we need to do 37404 27:48:13,360 --> 27:48:18,080 that so we're going to put in our code 37405 27:48:16,160 --> 27:48:19,832 and we are going to open this in visual 37406 27:48:18,080 --> 27:48:22,160 studio code by dragging and dropping 37407 27:48:19,831 --> 27:48:24,080 here this is going to give us our code 37408 27:48:22,160 --> 27:48:27,040 and we can really do whatever we want 37409 27:48:24,080 --> 27:48:29,911 with that i think we can leave in the 37410 27:48:27,039 --> 27:48:32,639 code that is using the base pointer to 37411 27:48:29,911 --> 27:48:34,319 manage our derived objects i think this 37412 27:48:34,320 --> 27:48:37,760 well enough and we can take out 37413 27:48:36,080 --> 27:48:40,232 everything else because that's just 37414 27:48:37,759 --> 27:48:42,959 going to be confusion we don't need all 37415 27:48:40,232 --> 27:48:45,192 this here this is going to be serving us 37416 27:48:42,960 --> 27:48:46,960 well now we want to learn about the 37417 27:48:46,960 --> 27:48:52,232 and to learn about that we're going to 37418 27:48:49,440 --> 27:48:55,440 introduce a title on purpose here for 37419 27:48:52,232 --> 27:48:57,440 example suppose we want to set up a draw 37420 27:48:57,440 --> 27:49:02,232 an uppercase letter here for the w key 37421 27:49:00,000 --> 27:49:04,800 world i don't think this is really easy 37422 27:49:02,232 --> 27:49:06,480 to spot once you've made this mistake 37423 27:49:04,800 --> 27:49:09,192 and you're not going to be getting any 37424 27:49:09,191 --> 27:49:14,871 this is something valid you can set up a 37425 27:49:11,191 --> 27:49:16,959 completely new method and call it joe 37426 27:49:14,872 --> 27:49:20,400 and this is going to work but this will 37427 27:49:16,960 --> 27:49:22,232 be very hard to spot in your program and 37428 27:49:20,399 --> 27:49:24,551 the compiler won't protect you as we 37429 27:49:22,232 --> 27:49:26,232 just said if we build our program you're 37430 27:49:24,551 --> 27:49:29,759 going to see that it is going to bulk 37431 27:49:26,232 --> 27:49:31,512 just fine so let's pass this through gcc 37432 27:49:29,759 --> 27:49:33,911 the voltage is going to be good but we 37433 27:49:31,512 --> 27:49:36,320 want to get to the behavior that we want 37434 27:49:33,911 --> 27:49:39,440 i think we can take out this sddc out 37435 27:49:36,320 --> 27:49:41,440 statement inside our for loop here and 37436 27:49:39,440 --> 27:49:44,400 we can weld again this is going to be 37437 27:49:41,440 --> 27:49:47,680 less confusing okay so the build is good 37438 27:49:44,399 --> 27:49:49,439 we can bring up our powershell window 37439 27:49:47,679 --> 27:49:51,439 and i'm going to run rooster and see 37440 27:49:49,440 --> 27:49:53,279 what happens you're going to see that 37441 27:49:51,440 --> 27:49:54,320 for a circle we're going to draw a 37442 27:49:56,000 --> 27:50:02,000 we're going to be drawing shape so what 37443 27:49:58,080 --> 27:50:04,639 is happening here we are overriding our 37444 27:50:02,000 --> 27:50:07,191 joe method in the overclass so we should 37445 27:50:04,639 --> 27:50:10,000 get polymorphic behavior because this is 37446 27:50:07,191 --> 27:50:12,871 a virtual function and it is called draw 37447 27:50:10,000 --> 27:50:15,119 exactly the same thing like we had in 37448 27:50:12,872 --> 27:50:17,911 the base class except we've made a 37449 27:50:15,119 --> 27:50:20,319 really simple typo here but it is really 37450 27:50:17,911 --> 27:50:24,000 hard to track and get hold of we have 37451 27:50:20,320 --> 27:50:26,080 named our method draw but the w is in 37452 27:50:24,000 --> 27:50:29,911 uppercase so this is a completely 37453 27:50:26,080 --> 27:50:33,040 different method than the draw method we 37454 27:50:29,911 --> 27:50:34,959 originally intended to override in our 37455 27:50:33,039 --> 27:50:37,679 base class and this is really hard to 37456 27:50:34,960 --> 27:50:40,000 follow and keep track of we can protect 37457 27:50:37,679 --> 27:50:43,039 ourselves by adding the override 37458 27:50:40,000 --> 27:50:44,960 specification so if we say override the 37459 27:50:43,039 --> 27:50:47,119 compiler is going to help out and make 37460 27:50:44,960 --> 27:50:49,912 sure that we are actually trying to 37461 27:50:47,119 --> 27:50:52,399 override the method that exists in the 37462 27:50:49,911 --> 27:50:54,000 shape class and if it doesn't exist 37463 27:50:52,399 --> 27:50:55,679 we're going to get a compiler error then 37464 27:50:54,000 --> 27:50:57,911 we're going to look at this and really 37465 27:50:55,679 --> 27:51:00,639 spot this mistake here this is what you 37466 27:50:57,911 --> 27:51:03,759 can achieve with the override keyword 37467 27:51:00,639 --> 27:51:06,319 and in fact i do recommend and advise 37468 27:51:03,759 --> 27:51:08,720 you to use this in your inheritance 37469 27:51:06,320 --> 27:51:10,400 hierarchy if you happen to be using 37470 27:51:10,399 --> 27:51:14,720 this is going to save you a lot of time 37471 27:51:12,551 --> 27:51:16,399 if you make mistakes like this so we're 37472 27:51:14,720 --> 27:51:18,800 going to try and run this and show you 37473 27:51:16,399 --> 27:51:20,399 the compiler error we get so let's do 37474 27:51:18,800 --> 27:51:22,800 that we're going to pass this through 37475 27:51:22,800 --> 27:51:27,680 fail as we see here and we're going to 37476 27:51:24,551 --> 27:51:30,639 get a compiler saying errol virtual void 37477 27:51:27,679 --> 27:51:33,831 overall joe cost marked override but 37478 27:51:30,639 --> 27:51:36,551 does not override this is super clear we 37479 27:51:33,831 --> 27:51:39,439 are saying we want to override here but 37480 27:51:36,551 --> 27:51:42,000 we're not overriding any method called 37481 27:51:39,440 --> 27:51:43,600 joe with an uppercase because we don't 37482 27:51:42,000 --> 27:51:45,759 have that in shape here this is the 37483 27:51:43,600 --> 27:51:47,440 message once we see the problem we can 37484 27:51:45,759 --> 27:51:50,319 correct it we're going to use in 37485 27:51:47,440 --> 27:51:52,551 lowercase letter for w and this is going 37486 27:51:50,320 --> 27:51:55,280 to work just fine now we're going to use 37487 27:51:52,551 --> 27:51:57,831 the same mechanism to protect ourselves 37488 27:51:55,279 --> 27:51:59,831 from ourselves in circle class so we're 37489 27:51:57,831 --> 27:52:01,511 going to override our draw method but 37490 27:51:59,831 --> 27:52:04,959 we're going to say that we want to 37491 27:52:01,512 --> 27:52:07,760 override explicitly and if we're both 37492 27:52:04,960 --> 27:52:09,680 now let's pass this through gcc this is 37493 27:52:07,759 --> 27:52:11,191 going to build successfully and if we 37494 27:52:09,679 --> 27:52:14,079 run our program we're going to get 37495 27:52:11,191 --> 27:52:16,231 polymorphic behavior let's do that you 37496 27:52:14,080 --> 27:52:18,479 see that circle draw overdraw circle 37497 27:52:16,232 --> 27:52:20,552 draw overdraw this is what we expect 37498 27:52:18,479 --> 27:52:22,399 this is really cool this is really all i 37499 27:52:20,551 --> 27:52:25,039 had to share in this lecture i hope you 37500 27:52:22,399 --> 27:52:27,511 found it interesting use the override 37501 27:52:25,039 --> 27:52:29,831 keyword in your inheritance hierarchy 37502 27:52:27,512 --> 27:52:32,080 because that's going to protect you from 37503 27:52:29,831 --> 27:52:35,039 easy to make typos that are really going 37504 27:52:32,080 --> 27:52:37,040 to give you a hard time otherwise we are 37505 27:52:35,039 --> 27:52:38,799 going to stop here in this lecture in 37506 27:52:37,039 --> 27:52:42,159 the next one we're going to explore the 37507 27:52:38,800 --> 27:52:43,279 concepts of overriding and hiding in 37508 27:52:43,279 --> 27:52:47,191 go ahead and finish up here and meet me 37509 27:52:45,512 --> 27:52:50,232 there in this lecture we're going to 37510 27:52:47,191 --> 27:52:53,191 zoom in on overloading overriding and 37511 27:52:50,232 --> 27:52:55,680 hiding we have seen that overriding is a 37512 27:52:53,191 --> 27:52:57,191 mechanism we have in c plus plus to be 37513 27:52:57,191 --> 27:53:03,119 most specific versions of our virtual 37514 27:53:00,639 --> 27:53:05,600 methods in derived classes so for 37515 27:53:03,119 --> 27:53:08,399 example the draw method we have in oval 37516 27:53:05,600 --> 27:53:11,512 is overriding or eclipsing the one we 37517 27:53:08,399 --> 27:53:13,759 have in shape and the draw method in 37518 27:53:13,759 --> 27:53:20,231 or hiding or eclipsing the one we have 37519 27:53:17,279 --> 27:53:23,360 in awful this is the concept here but 37520 27:53:20,232 --> 27:53:26,160 what happens if we introduce the concept 37521 27:53:23,360 --> 27:53:28,639 of overloading and mix that up with 37522 27:53:26,160 --> 27:53:30,800 overriding as we have seen so far so 37523 27:53:28,639 --> 27:53:33,831 suppose we do something like this this 37524 27:53:30,800 --> 27:53:36,080 is our shape class it is going to have 37525 27:53:33,831 --> 27:53:38,871 two versions of the draw method we have 37526 27:53:36,080 --> 27:53:41,040 one that takes one parameter for example 37527 27:53:38,872 --> 27:53:43,440 this could be the color that we want to 37528 27:53:41,039 --> 27:53:45,831 draw our shape with and another one is 37529 27:53:43,440 --> 27:53:49,040 not going to take any parameter and 37530 27:53:45,831 --> 27:53:51,360 these are two overloads of our virtual 37531 27:53:49,039 --> 27:53:54,231 method here so what is going to happen 37532 27:53:51,360 --> 27:53:56,720 in our derived classes that are going to 37533 27:53:54,232 --> 27:53:58,720 inherit this virtual method so let's 37534 27:53:56,720 --> 27:54:01,119 look at an example here we have a simple 37535 27:53:58,720 --> 27:54:03,512 example that is setting up objects of 37536 27:54:01,119 --> 27:54:05,759 our shapes we have a shape object we 37537 27:54:03,512 --> 27:54:08,479 have a novel object and we have a circle 37538 27:54:05,759 --> 27:54:10,959 object here if we call the draw method 37539 27:54:08,479 --> 27:54:12,800 on our most derived class which is 37540 27:54:10,960 --> 27:54:14,400 circle we're going to see that this is 37541 27:54:12,800 --> 27:54:16,479 going to work we can draw without a 37542 27:54:14,399 --> 27:54:19,119 parameter and we can draw with one 37543 27:54:16,479 --> 27:54:21,512 parameter which is going to be our color 37544 27:54:19,119 --> 27:54:23,279 depth this is going to work just fine we 37545 27:54:23,279 --> 27:54:28,080 our inherited method from the shape 37546 27:54:25,759 --> 27:54:31,599 class and this is really cool but what 37547 27:54:28,080 --> 27:54:34,639 happens if you override this method in 37548 27:54:31,600 --> 27:54:36,872 our overclass suppose we hijack the 37549 27:54:34,639 --> 27:54:40,639 inheritance process and set up another 37550 27:54:36,872 --> 27:54:44,160 draw method in our overclass and we only 37551 27:54:40,639 --> 27:54:45,911 override the overload that doesn't take 37552 27:54:44,160 --> 27:54:48,720 any parameter we can do something like 37553 27:54:45,911 --> 27:54:51,440 this but the moment you do this this is 37554 27:54:48,720 --> 27:54:53,911 going to only override this method and 37555 27:54:51,440 --> 27:54:57,600 it is going to hide all the other draw 37556 27:54:53,911 --> 27:54:59,119 methods that we had in our base class 37557 27:54:57,600 --> 27:55:01,600 which is shaped here and this is 37558 27:54:59,119 --> 27:55:04,479 something i want you to know so once you 37559 27:55:01,600 --> 27:55:06,232 override a single overload of your 37560 27:55:06,232 --> 27:55:11,512 all the other overloads are going to be 37561 27:55:08,479 --> 27:55:14,800 hidden and they won't be usable anymore 37562 27:55:11,512 --> 27:55:17,040 by downstream inheritance classes if you 37563 27:55:14,800 --> 27:55:19,120 want all the overloads to be available 37564 27:55:19,119 --> 27:55:24,959 classes you have no choice but to 37565 27:55:21,911 --> 27:55:27,191 explicitly override each of them and 37566 27:55:24,960 --> 27:55:28,872 that's how it's going to work so if we 37567 27:55:27,191 --> 27:55:31,599 look at the example here we're going to 37568 27:55:28,872 --> 27:55:33,600 see that the draw call without a 37569 27:55:31,600 --> 27:55:37,440 parameter is going to work because this 37570 27:55:33,600 --> 27:55:39,832 is overridden in our oval class but if 37571 27:55:37,440 --> 27:55:42,320 we call the one which takes a parameter 37572 27:55:39,831 --> 27:55:44,959 this is going to be hidden at the oval 37573 27:55:42,320 --> 27:55:46,800 level and we want to have access to that 37574 27:55:44,960 --> 27:55:48,552 in circle and if we try to call this 37575 27:55:46,800 --> 27:55:52,232 we're going to get a compiler error 37576 27:55:48,551 --> 27:55:56,159 again the message is if you override one 37577 27:55:52,232 --> 27:55:58,320 overload of your virtual functions in 37578 27:55:56,160 --> 27:56:00,320 the base class all the other ones are 37579 27:55:58,320 --> 27:56:03,120 going to be hidden and you have no 37580 27:56:00,320 --> 27:56:06,000 choice but to explicitly override them 37581 27:56:03,119 --> 27:56:08,799 for them to be available and usable in 37582 27:56:06,000 --> 27:56:10,800 downstream classes this is the message i 37583 27:56:08,800 --> 27:56:13,760 wanted to convey here okay so now we 37584 27:56:10,800 --> 27:56:15,279 know this but what if we set up another 37585 27:56:15,279 --> 27:56:20,639 downstream for example what if we go in 37586 27:56:17,911 --> 27:56:22,800 our oval class and set up another 37587 27:56:20,639 --> 27:56:24,720 overload which is going to have two 37588 27:56:22,800 --> 27:56:27,360 parameters for example we might have the 37589 27:56:24,720 --> 27:56:29,440 color depth and the color here that we 37590 27:56:27,360 --> 27:56:31,680 want to draw with okay so one thing you 37591 27:56:29,440 --> 27:56:33,680 should know is that this this method is 37592 27:56:31,679 --> 27:56:37,039 not going to participate in shape 37593 27:56:33,679 --> 27:56:38,799 polymorphism and what i mean is that if 37594 27:56:37,039 --> 27:56:41,599 we go through a base pointer or 37595 27:56:38,800 --> 27:56:43,680 reference to manage derived objects we 37596 27:56:41,600 --> 27:56:46,160 can't really call this method with shape 37597 27:56:43,679 --> 27:56:48,231 polymorphism so if we go through our 37598 27:56:46,160 --> 27:56:50,552 shape pointer or reference we want to be 37599 27:56:48,232 --> 27:56:52,552 really able to call this method if we 37600 27:56:50,551 --> 27:56:55,440 try to call it we will get a compiler 37601 27:56:52,551 --> 27:56:56,479 error the reason is this method is not 37602 27:56:56,479 --> 27:57:00,872 to the shape class that's why it's not 37603 27:56:58,720 --> 27:57:03,360 going to participate in shape 37604 27:57:00,872 --> 27:57:05,040 polymorphism the only methods that are 37605 27:57:03,360 --> 27:57:07,600 going to participate in shape 37606 27:57:05,039 --> 27:57:10,159 polymorphism are the methods that are 37607 27:57:07,600 --> 27:57:12,320 declared in the base class and they are 37608 27:57:10,160 --> 27:57:15,192 virtual this is the condition here and 37609 27:57:12,320 --> 27:57:17,280 if you violate this condition in any way 37610 27:57:15,191 --> 27:57:19,279 shape polymorphism or base class 37611 27:57:17,279 --> 27:57:21,191 polymorphism in this case is not going 37612 27:57:19,279 --> 27:57:23,440 to work and you will get a compiler 37613 27:57:21,191 --> 27:57:25,512 error so if you try to do something like 37614 27:57:23,440 --> 27:57:27,512 this and you want to call your draw 37615 27:57:25,512 --> 27:57:29,191 method with two parameters you're going 37616 27:57:27,512 --> 27:57:31,191 to get a bunch of compiler errors and 37617 27:57:29,191 --> 27:57:33,360 this is not going to work another thing 37618 27:57:31,191 --> 27:57:36,720 i want you to see is that if you assign 37619 27:57:33,360 --> 27:57:40,399 a derived object to a base object 37620 27:57:36,720 --> 27:57:43,911 your derived virtual functions are going 37621 27:57:40,399 --> 27:57:46,399 to be wiped out as part of the slicing 37622 27:57:43,911 --> 27:57:49,191 that we have learned about already so 37623 27:57:46,399 --> 27:57:51,599 the circle port is going to be wiped out 37624 27:57:49,191 --> 27:57:54,000 the oval part is going to be sliced off 37625 27:57:51,600 --> 27:57:56,872 and the draw method is going to go with 37626 27:57:54,000 --> 27:57:59,119 our oval part and we will only be left 37627 27:57:56,872 --> 27:58:01,040 with the shareport and if we try to call 37628 27:57:59,119 --> 27:58:03,360 this method here we will get a compiler 37629 27:58:01,039 --> 27:58:05,599 error because it's no longer available 37630 27:58:03,360 --> 27:58:07,759 it's been sliced off this is really what 37631 27:58:05,600 --> 27:58:09,279 i want you to see here now that we know 37632 27:58:07,759 --> 27:58:11,119 this we're going to head over to visual 37633 27:58:09,279 --> 27:58:13,831 studio code and play with this a little 37634 27:58:11,119 --> 27:58:16,231 more here we are in our working folder 37635 27:58:13,831 --> 27:58:18,720 the current project is overloading 37636 27:58:16,232 --> 27:58:21,279 overriding and hiding we want to explore 37637 27:58:18,720 --> 27:58:22,720 these concepts in a little more detail 37638 27:58:21,279 --> 27:58:24,479 we're going to grab the code from the 37639 27:58:22,720 --> 27:58:26,160 last lecture because that's going to be 37640 27:58:24,479 --> 27:58:28,231 a good starting point we're going to 37641 27:58:26,160 --> 27:58:30,080 take out the binary we don't want that 37642 27:58:28,232 --> 27:58:32,720 and we're going to copy and we're going 37643 27:58:30,080 --> 27:58:34,872 to put that in our current project here 37644 27:58:32,720 --> 27:58:36,800 this is going to do and we are going to 37645 27:58:34,872 --> 27:58:39,040 open this in visual studio code pretty 37646 27:58:36,800 --> 27:58:41,192 quick by dragging and dropping here and 37647 27:58:39,039 --> 27:58:42,319 we're going to see that we have a bunch 37648 27:58:42,320 --> 27:58:47,192 virtual functions here who just have the 37649 27:58:44,160 --> 27:58:49,512 draw version but we can set up an 37650 27:58:47,191 --> 27:58:51,599 overload of this draw method here this 37651 27:58:49,512 --> 27:58:54,000 is what we want to do so i am going to 37652 27:58:51,600 --> 27:58:55,912 go below here and set up an overload of 37653 27:58:54,000 --> 27:58:59,119 this method this is going to be taking 37654 27:58:55,911 --> 27:59:01,440 the color depth as a parameter and we 37655 27:58:59,119 --> 27:59:04,319 want to see how usable this is 37656 27:59:01,440 --> 27:59:06,872 downstream so we're going to go down in 37657 27:59:06,872 --> 27:59:12,000 and take out what we don't need here we 37658 27:59:09,440 --> 27:59:15,119 really need a bunch of objects we can 37659 27:59:12,000 --> 27:59:17,119 set up circle one and over one and we 37660 27:59:15,119 --> 27:59:19,191 will see that we can call this method 37661 27:59:17,119 --> 27:59:20,720 we're going to say circle one draw 37662 27:59:19,191 --> 27:59:24,551 without a parameter and we're going to 37663 27:59:20,720 --> 27:59:26,872 say circle one draw and pass and 44 as 37664 27:59:24,551 --> 27:59:30,000 our parameter and let's go back and make 37665 27:59:26,872 --> 27:59:32,720 sure that we have our overload and this 37666 27:59:30,000 --> 27:59:34,872 is going to be taking the color depth 37667 27:59:32,720 --> 27:59:37,191 and let's try and build our program 37668 27:59:34,872 --> 27:59:39,440 because we have a squiggly line here 37669 27:59:37,191 --> 27:59:41,360 let's see if it is just a visual studio 37670 27:59:39,440 --> 27:59:43,600 code playing games with us we're going 37671 27:59:41,360 --> 27:59:45,911 to pass this through gcc and we will 37672 27:59:43,600 --> 27:59:49,040 have a compiler error here so what is 37673 27:59:45,911 --> 27:59:51,911 going on candidate virtual draw cost 37674 27:59:49,039 --> 27:59:54,551 let's go up and see what kind of problem 37675 27:59:51,911 --> 27:59:57,119 we have here ah this is the concept we 37676 27:59:54,551 --> 28:00:00,479 just talked about in the slides we are 37677 27:59:57,119 --> 28:00:03,279 setting up our overloads here but this 37678 28:00:00,479 --> 28:00:05,360 is being eclipsed by the overloads that 37679 28:00:03,279 --> 28:00:07,759 we are setting up in oval you see we 37680 28:00:05,360 --> 28:00:10,800 have a draw override and this is also 37681 28:00:07,759 --> 28:00:14,319 being eclipsed by the override we have 37682 28:00:10,800 --> 28:00:16,551 in our circle class here if we want the 37683 28:00:14,320 --> 28:00:18,400 two overloads we have from shape to be 37684 28:00:16,551 --> 28:00:20,720 available we can comment out the 37685 28:00:18,399 --> 28:00:23,039 overrides we have both in circle and 37686 28:00:20,720 --> 28:00:25,600 oval let's do that and that's going to 37687 28:00:23,039 --> 28:00:26,959 be available and usable so let's do that 37688 28:00:25,600 --> 28:00:30,480 this is a really good learning 37689 28:00:26,960 --> 28:00:31,912 opportunity and now if we go in main cpp 37690 28:00:30,479 --> 28:00:34,479 we're going to see that this is going to 37691 28:00:31,911 --> 28:00:37,759 become available so we can upload this 37692 28:00:34,479 --> 28:00:40,231 with gcc again to bring visual studio 37693 28:00:37,759 --> 28:00:43,039 code up to date and you see the build is 37694 28:00:40,232 --> 28:00:46,080 good and our methods are usable even 37695 28:00:43,039 --> 28:00:49,279 from derived classes so for example we 37696 28:00:46,080 --> 28:00:51,911 can also set up a novel object let's say 37697 28:00:49,279 --> 28:00:54,800 oval one and say draw this is going to 37698 28:00:51,911 --> 28:00:57,831 work if we say ovo one draw and pass in 37699 28:00:54,800 --> 28:00:59,832 a parameter let's put in 54 for example 37700 28:00:57,831 --> 28:01:01,679 this is going to do you're going to see 37701 28:00:59,831 --> 28:01:05,039 that this is going to be accepted by 37702 28:01:01,679 --> 28:01:07,511 visual studio code and if we weld and 37703 28:01:05,039 --> 28:01:09,911 run this program it is going to build it 37704 28:01:07,512 --> 28:01:12,551 is going to be calling the inherited 37705 28:01:12,551 --> 28:01:17,679 circle here the boat is good we can kill 37706 28:01:15,440 --> 28:01:19,680 our terminal here and bring up a 37707 28:01:17,679 --> 28:01:21,911 powershell window we can use to run our 37708 28:01:19,679 --> 28:01:24,799 program we're going to see that we call 37709 28:01:21,911 --> 28:01:27,512 shape draw and shape draw because these 37710 28:01:27,512 --> 28:01:31,600 the shape class that's why we are saying 37711 28:01:29,679 --> 28:01:34,319 shape draw here and we're going to be 37712 28:01:31,600 --> 28:01:36,160 drawing a oval and a drawing with a 37713 28:01:34,320 --> 28:01:38,160 color depth we're going to be drawing 37714 28:01:36,160 --> 28:01:40,160 the circle and drawing with the color 37715 28:01:38,160 --> 28:01:42,872 depth here this is the message i want to 37716 28:01:40,160 --> 28:01:45,680 convey but the moment we override these 37717 28:01:42,872 --> 28:01:48,800 methods in any downstream class for 37718 28:01:45,679 --> 28:01:51,039 example if we go in our over class and 37719 28:01:48,800 --> 28:01:53,760 and comment out the override for the 37720 28:01:51,039 --> 28:01:56,959 draw method notice that we are just 37721 28:01:53,759 --> 28:01:59,119 overriding a single overload again if we 37722 28:01:56,960 --> 28:02:01,440 go back to our shape class we have an 37723 28:01:59,119 --> 28:02:03,360 overload that doesn't take any parameter 37724 28:02:01,440 --> 28:02:05,832 we also have an overload which is going 37725 28:02:03,360 --> 28:02:09,512 to take one parameter in oval we are 37726 28:02:05,831 --> 28:02:11,511 just going to override the overload that 37727 28:02:09,512 --> 28:02:13,911 doesn't take any parameter but this is 37728 28:02:11,512 --> 28:02:17,040 going to hide all the other overloads we 37729 28:02:13,911 --> 28:02:19,191 had for the draw method here and this is 37730 28:02:17,039 --> 28:02:20,399 the only overload that is going to be 37731 28:02:20,399 --> 28:02:24,720 in downstream classes so if we go back 37732 28:02:23,191 --> 28:02:26,551 in main we're going to see that the one 37733 28:02:24,720 --> 28:02:27,759 with a parameter is not going to work 37734 28:02:26,551 --> 28:02:30,231 and we're going to get a bunch of 37735 28:02:27,759 --> 28:02:32,871 compiler errors this is going to be the 37736 28:02:32,872 --> 28:02:37,832 uncomment this guy the others are going 37737 28:02:35,279 --> 28:02:40,160 to be eclipsed again the other one we 37738 28:02:37,831 --> 28:02:42,319 have is just the one with one parameter 37739 28:02:40,160 --> 28:02:44,872 but this is going to stay true even if 37740 28:02:42,320 --> 28:02:46,800 you have a hundred overloads all the 37741 28:02:44,872 --> 28:02:49,600 others are not going to be available if 37742 28:02:46,800 --> 28:02:51,832 you happen to only override one of the 37743 28:02:51,831 --> 28:02:57,191 the other overloads are going to be 37744 28:02:53,759 --> 28:02:59,279 hidden by this one overall ride that you 37745 28:02:57,191 --> 28:03:01,679 are putting in your inheritance 37746 28:02:59,279 --> 28:03:03,911 hierarchy you should really know this if 37747 28:03:01,679 --> 28:03:06,399 you want this guide to be available in 37748 28:03:03,911 --> 28:03:09,279 downstream classes for example you can 37749 28:03:06,399 --> 28:03:12,159 also override that in derived classes 37750 28:03:09,279 --> 28:03:14,800 this is an option we can go in oval and 37751 28:03:12,160 --> 28:03:17,600 put in another overload for this this is 37752 28:03:14,800 --> 28:03:21,192 going to bring this back in scope so we 37753 28:03:17,600 --> 28:03:23,360 can say overdraw here with the color 37754 28:03:21,191 --> 28:03:25,911 depth i don't think we copied the right 37755 28:03:23,360 --> 28:03:28,232 thing we can grab this and go in oval 37756 28:03:25,911 --> 28:03:31,440 and put this in place this is going to 37757 28:03:28,232 --> 28:03:33,832 do we're going to say oval drawing and 37758 28:03:31,440 --> 28:03:36,320 we can go in circle and also put this 37759 28:03:33,831 --> 28:03:39,759 end this is an option and we're going to 37760 28:03:36,320 --> 28:03:41,912 say circle drawing and now you see that 37761 28:03:39,759 --> 28:03:44,551 the squiggly lines in main go away 37762 28:03:41,911 --> 28:03:47,119 because we have overridden all the 37763 28:03:44,551 --> 28:03:48,639 overloads okay if we build and run our 37764 28:03:47,119 --> 28:03:50,871 program we're going to see that this is 37765 28:03:48,639 --> 28:03:53,440 going to work as we expect the world is 37766 28:03:50,872 --> 28:03:55,120 going to be good and we can clear 37767 28:03:53,440 --> 28:03:58,080 and run rooster we're going to see that 37768 28:03:55,119 --> 28:04:00,799 overdraw called overdraw called we are 37769 28:03:58,080 --> 28:04:03,119 calling our derived overrides and this 37770 28:04:00,800 --> 28:04:05,192 is really cool this is what we want here 37771 28:04:03,119 --> 28:04:06,479 let's take this back because we don't 37772 28:04:06,479 --> 28:04:12,231 overridden in our inherited classes so 37773 28:04:09,440 --> 28:04:14,160 we're going to take this out in oval 37774 28:04:12,232 --> 28:04:17,440 and we're going to take this out in 37775 28:04:14,160 --> 28:04:20,232 circle and i want you to see that shaped 37776 28:04:17,440 --> 28:04:23,760 polymorphism is not going to work if you 37777 28:04:20,232 --> 28:04:25,760 override your methods at a downstream 37778 28:04:23,759 --> 28:04:28,231 level so what we are going to do we are 37779 28:04:25,759 --> 28:04:31,759 going to go at the oval level and set up 37780 28:04:28,232 --> 28:04:33,911 another overload for the joe method here 37781 28:04:31,759 --> 28:04:36,399 but this draw method is not going to 37782 28:04:33,911 --> 28:04:38,639 participate in shape polymorphism 37783 28:04:36,399 --> 28:04:41,831 because it is really declared at the 37784 28:04:38,639 --> 28:04:44,232 oval level and any base pointer we have 37785 28:04:41,831 --> 28:04:46,080 or any shape pointer we have is not 37786 28:04:44,232 --> 28:04:48,160 going to have any knowledge of this 37787 28:04:46,080 --> 28:04:50,232 overload that we are about to set up 37788 28:04:48,160 --> 28:04:52,552 here this is what i want you to know 37789 28:04:50,232 --> 28:04:54,872 okay so we are going to go in our oval 37790 28:04:52,551 --> 28:04:58,159 class and set up that overload we can 37791 28:04:54,872 --> 28:05:00,479 set that up here it is going to be taken 37792 28:04:58,160 --> 28:05:02,320 two parameters one is going to be the 37793 28:05:00,479 --> 28:05:05,191 color depth the other is going to be the 37794 28:05:02,320 --> 28:05:07,360 color and we don't want to pass a string 37795 28:05:05,191 --> 28:05:10,479 by a value like this we can use std 37796 28:05:07,360 --> 28:05:12,800 string view and we can include that if 37797 28:05:10,479 --> 28:05:15,279 we need to but it looks like visual 37798 28:05:12,800 --> 28:05:17,680 studio code has got this from something 37799 28:05:15,279 --> 28:05:19,759 else we have included before but we can 37800 28:05:17,679 --> 28:05:21,679 include that explicitly to avoid 37801 28:05:19,759 --> 28:05:24,479 problems so let's do that we're going to 37802 28:05:21,679 --> 28:05:26,231 say stream view here and now we want to 37803 28:05:24,479 --> 28:05:29,360 see that this method is not going to 37804 28:05:26,232 --> 28:05:31,760 participate in shape polymorphism even 37805 28:05:29,360 --> 28:05:34,000 if it is a virtual functions the reason 37806 28:05:31,759 --> 28:05:36,000 is this is not going to be known by your 37807 28:05:34,000 --> 28:05:38,320 shape pointers because it is not 37808 28:05:36,000 --> 28:05:40,080 declared at the sherpa level here i 37809 28:05:38,320 --> 28:05:42,552 really want you to know this we're going 37810 28:05:40,080 --> 28:05:44,080 to take out everything we had here and 37811 28:05:42,551 --> 28:05:47,039 we can set up a shape pointer we're 37812 28:05:44,080 --> 28:05:49,360 going to say shape and say ship ptr and 37813 28:05:47,039 --> 28:05:52,079 we're going to set up a derived object 37814 28:05:49,360 --> 28:05:54,232 here we can say new and serious circle 37815 28:05:52,080 --> 28:05:56,720 and what do we need to build the circle 37816 28:05:54,232 --> 28:05:58,800 object we need the radius and the 37817 28:05:56,720 --> 28:06:01,040 description so we're going to put that 37818 28:05:58,800 --> 28:06:03,360 in place here the radius is going to be 37819 28:06:01,039 --> 28:06:05,119 something like 10 why not and the 37820 28:06:03,360 --> 28:06:08,080 description is going to be circle one 37821 28:06:05,119 --> 28:06:10,551 how creative and we want to see if our 37822 28:06:08,080 --> 28:06:12,479 method is available through polymorphism 37823 28:06:10,551 --> 28:06:15,360 and it is not going to be available 37824 28:06:12,479 --> 28:06:18,319 because the method was not declared at 37825 28:06:15,360 --> 28:06:20,872 the shape level so if we do shape ptr 37826 28:06:18,320 --> 28:06:23,600 and say draw and we have a method which 37827 28:06:20,872 --> 28:06:26,000 is going to take two parameters at the 37828 28:06:23,600 --> 28:06:27,279 oval level we have the color depth and 37829 28:06:27,279 --> 28:06:34,000 so for example if we go in main and say 37830 28:06:34,000 --> 28:06:37,831 not let's say this is something that 37831 28:06:37,831 --> 28:06:42,159 45 because this is an integer parameter 37832 28:06:40,551 --> 28:06:44,479 and we're going to pass in a string 37833 28:06:42,160 --> 28:06:46,552 let's say red you're going to see that 37834 28:06:44,479 --> 28:06:48,551 this is not going to compile 37835 28:06:46,551 --> 28:06:51,191 because the draw method with two 37836 28:06:48,551 --> 28:06:53,360 parameters is not known at the shape 37837 28:06:51,191 --> 28:06:56,231 level this is the message i want to 37838 28:06:53,360 --> 28:06:58,960 convey here so if you want your method 37839 28:06:56,232 --> 28:07:02,080 to be usable with polymorphism the 37840 28:06:58,960 --> 28:07:04,232 method has to show up at the top level 37841 28:07:02,080 --> 28:07:05,600 this is what i want you to see here if 37842 28:07:04,232 --> 28:07:07,911 we're trying to build this we're going 37843 28:07:05,600 --> 28:07:11,040 to get a compiler error and you need to 37844 28:07:07,911 --> 28:07:12,720 be aware of this but know that even if 37845 28:07:11,039 --> 28:07:15,639 this is not working with shape 37846 28:07:12,720 --> 28:07:18,320 polymorphism this can work with oval 37847 28:07:15,639 --> 28:07:20,800 polymorphism and this is an idea i am 37848 28:07:18,320 --> 28:07:23,512 going to explore in the next lecture 37849 28:07:20,800 --> 28:07:26,160 where we learn about the fact that polar 37850 28:07:23,512 --> 28:07:28,232 morphism can be set up at different 37851 28:07:26,160 --> 28:07:30,960 levels so we are going to stop here in 37852 28:07:28,232 --> 28:07:33,360 this lecture and explore that idea in 37853 28:07:30,960 --> 28:07:35,760 detail in the next lecture so go ahead 37854 28:07:33,360 --> 28:07:37,759 and finish up here and meet me there in 37855 28:07:35,759 --> 28:07:40,119 this lecture we're going to explore the 37856 28:07:40,119 --> 28:07:45,279 polymorphism at different levels in our 37857 28:07:45,279 --> 28:07:50,872 suppose we have a hierarchy like we see 37858 28:07:48,080 --> 28:07:53,600 here the top level class is animal and 37859 28:07:50,872 --> 28:07:56,720 we can derive from animal and create a 37860 28:07:53,600 --> 28:07:59,680 feline animal we can derive from feline 37861 28:07:56,720 --> 28:08:02,479 and create a dog animal we can derive 37862 28:07:59,679 --> 28:08:04,639 from feline create a cat animal we can 37863 28:08:02,479 --> 28:08:07,360 also do it the other way and create a 37864 28:08:04,639 --> 28:08:09,759 bird which is also an animal a bird is 37865 28:08:07,360 --> 28:08:12,479 going to have its own methods and member 37866 28:08:09,759 --> 28:08:15,039 variables that make it a bird we can 37867 28:08:12,479 --> 28:08:17,440 derive from a bird create a pigeon and 37868 28:08:15,039 --> 28:08:18,479 we can derive from a bird and create a 37869 28:08:18,479 --> 28:08:22,080 and we can do all these things that 37870 28:08:22,080 --> 28:08:26,400 so the idea i want to expose here is 37871 28:08:26,399 --> 28:08:31,831 doesn't have to always only work on the 37872 28:08:29,279 --> 28:08:34,160 top level class if you have an 37873 28:08:31,831 --> 28:08:37,360 inheritance hierarchy like this if you 37874 28:08:34,160 --> 28:08:39,911 want you can declare a virtual method at 37875 28:08:37,360 --> 28:08:42,080 the feline level and make sure that 37876 28:08:39,911 --> 28:08:44,319 method is also available in deriving 37877 28:08:42,080 --> 28:08:46,800 class and you can set up feline 37878 28:08:44,320 --> 28:08:49,360 polymorphism if that makes sense for 37879 28:08:46,800 --> 28:08:51,760 your application following the same idea 37880 28:08:49,360 --> 28:08:54,160 you can also set up a virtual method in 37881 28:08:51,759 --> 28:08:56,551 the bird class and you can have that 37882 28:08:54,160 --> 28:08:59,512 method overridden in downstream 37883 28:08:56,551 --> 28:09:02,319 inheritance classes like pigeon and crow 37884 28:08:59,512 --> 28:09:04,960 and you can set up bird polymorphism in 37885 28:09:02,320 --> 28:09:07,040 your c plus plus program if that makes 37886 28:09:04,960 --> 28:09:09,832 sense of course you can still use animal 37887 28:09:07,039 --> 28:09:12,079 polymorphism if you want to do that and 37888 28:09:09,831 --> 28:09:14,399 that's going to work as well this is the 37889 28:09:12,080 --> 28:09:17,040 idea we're going to explore here okay so 37890 28:09:14,399 --> 28:09:20,159 with that inheritance hierarchy in mind 37891 28:09:17,039 --> 28:09:22,551 we can create animal polymorphism and 37892 28:09:20,160 --> 28:09:25,192 animal polymorphism is just going 37893 28:09:22,551 --> 28:09:28,399 through the animal base pointer and 37894 28:09:25,191 --> 28:09:30,399 using that to manage derived objects for 37895 28:09:28,399 --> 28:09:32,551 example here we have a dog we have a cat 37896 28:09:30,399 --> 28:09:35,279 we have a pigeon we have a crow we can 37897 28:09:32,551 --> 28:09:37,039 do animal polymorphism and storing any 37898 28:09:35,279 --> 28:09:39,360 kind of animal we have here you see we 37899 28:09:37,039 --> 28:09:41,279 are putting in a dog we are putting in a 37900 28:09:39,360 --> 28:09:42,960 cat we are putting in a pigeon and we 37901 28:09:42,960 --> 28:09:48,320 and if we do animal polymorphism and 37902 28:09:45,679 --> 28:09:49,759 call the breathe method which exists in 37903 28:09:48,320 --> 28:09:52,320 any kind of animal we have in our 37904 28:09:49,759 --> 28:09:55,039 program this is going to call the most 37905 28:09:52,320 --> 28:09:56,552 specific breathe method on each animal 37906 28:09:55,039 --> 28:09:59,191 and this is going to work really well 37907 28:09:56,551 --> 28:10:01,039 this is going to do animal polymorphism 37908 28:09:59,191 --> 28:10:04,080 following the same idea we can also do 37909 28:10:01,039 --> 28:10:06,799 feline polymorphism and in this case 37910 28:10:04,080 --> 28:10:09,440 feline is going to be acting as our base 37911 28:10:06,800 --> 28:10:11,832 pointer so again we have our dog we have 37912 28:10:09,440 --> 28:10:14,479 our cat we have our pigeon if we do 37913 28:10:11,831 --> 28:10:17,279 feline polymorphism we will have to put 37914 28:10:14,479 --> 28:10:19,440 in all kinds of crazy felines we have in 37915 28:10:17,279 --> 28:10:21,679 our program for example we can put in 37916 28:10:19,440 --> 28:10:23,832 dog and cat and this is going to work if 37917 28:10:21,679 --> 28:10:26,959 you do fill out a polymorphism like we 37918 28:10:23,831 --> 28:10:29,439 are doing here and put in an animal this 37919 28:10:26,960 --> 28:10:32,872 is not going to work because the animal 37920 28:10:29,440 --> 28:10:34,551 top level class is probably not going to 37921 28:10:34,551 --> 28:10:39,039 virtual method we are interested in here 37922 28:10:37,279 --> 28:10:41,360 for example here we are calling the run 37923 28:10:39,039 --> 28:10:43,911 method and the run method is not set up 37924 28:10:41,360 --> 28:10:46,479 at the animal level let's go back and 37925 28:10:43,911 --> 28:10:49,039 look at our inheritance hierarchy here 37926 28:10:46,479 --> 28:10:51,191 you see this is our animal class our 37927 28:10:49,039 --> 28:10:53,119 virtual method is going to be breathed 37928 28:10:51,191 --> 28:10:55,831 because any kind of animal we have is 37929 28:10:53,119 --> 28:10:59,119 going to need to breathe but the run 37930 28:10:55,831 --> 28:11:02,799 method is set up at the feline level so 37931 28:10:59,119 --> 28:11:06,080 if we try and store an an animal in a 37932 28:11:02,800 --> 28:11:08,000 setup that uses filing polymorphism the 37933 28:11:06,080 --> 28:11:09,600 run method is not going to be available 37934 28:11:08,000 --> 28:11:11,440 and we're going to get a compiler error 37935 28:11:09,600 --> 28:11:13,512 so i want you to know this but we will 37936 28:11:11,440 --> 28:11:15,512 also see this in a minute when we hit 37937 28:11:13,512 --> 28:11:18,400 visual studio code to really play with 37938 28:11:15,512 --> 28:11:20,479 us okay we can also do bird polymorphism 37939 28:11:18,399 --> 28:11:23,039 and here you see we have two objects one 37940 28:11:20,479 --> 28:11:25,360 is a pigeon the other is a crow 37941 28:11:23,039 --> 28:11:28,399 and we're going to do bird polymorphism 37942 28:11:25,360 --> 28:11:31,440 here and what that means is that we will 37943 28:11:28,399 --> 28:11:33,439 use a bird pointer to manage all kinds 37944 28:11:31,440 --> 28:11:35,832 of crazy birds in our c plus plus 37945 28:11:33,440 --> 28:11:37,600 program for example we can use that to 37946 28:11:35,831 --> 28:11:40,231 manage a pigeon we can use that to 37947 28:11:37,600 --> 28:11:43,512 manage a crow and if we loop through our 37948 28:11:40,232 --> 28:11:45,680 birds and call the fly method the most 37949 28:11:43,512 --> 28:11:47,832 specific fly method is going to be 37950 28:11:45,679 --> 28:11:49,511 called in this case at the first 37951 28:11:47,831 --> 28:11:52,159 iteration we're going to call the fly 37952 28:11:49,512 --> 28:11:53,911 method on our pigeon object at the 37953 28:11:52,160 --> 28:11:57,360 second iteration we're going to call the 37954 28:11:53,911 --> 28:11:59,512 fly method on our crawl object here and 37955 28:11:57,360 --> 28:12:01,039 this is going to work now that you know 37956 28:11:59,512 --> 28:12:02,872 this we're going to head over to visual 37957 28:12:01,039 --> 28:12:06,000 studio code and play with this a little 37958 28:12:02,872 --> 28:12:08,800 more here we are in our working folder 37959 28:12:06,000 --> 28:12:11,360 the current project is polymorphism at 37960 28:12:08,800 --> 28:12:13,512 different levels we're going to grab our 37961 28:12:11,360 --> 28:12:16,000 template files let's grab it all we're 37962 28:12:13,512 --> 28:12:17,760 going to copy this and put this in our 37963 28:12:17,759 --> 28:12:22,080 and we're going to open this little guy 37964 28:12:19,512 --> 28:12:24,551 in visual studio code like we always do 37965 28:12:22,080 --> 28:12:27,040 and before we open this up i am going to 37966 28:12:24,551 --> 28:12:29,679 put in the classes i have lying around 37967 28:12:27,039 --> 28:12:32,479 on my drive and these are going to be 37968 28:12:29,679 --> 28:12:36,159 the classes making up our inheritance 37969 28:12:32,479 --> 28:12:38,159 hierarchy here to make things go faster 37970 28:12:36,160 --> 28:12:40,720 if you want you can grab these classes 37971 28:12:38,160 --> 28:12:42,639 from the attached resource section on 37972 28:12:40,720 --> 28:12:44,479 this lecture you're going to find all 37973 28:12:42,639 --> 28:12:46,720 these files you can download them and 37974 28:12:44,479 --> 28:12:48,551 put them in your project and use that as 37975 28:12:46,720 --> 28:12:50,320 a starting point because you don't 37976 28:12:48,551 --> 28:12:52,720 really want to be creating all these 37977 28:12:50,320 --> 28:12:55,040 classes this is going to just waste your 37978 28:12:52,720 --> 28:12:58,080 time the basic idea i wanted to expose 37979 28:12:55,039 --> 28:13:00,231 here is doing polymorphism at different 37980 28:12:58,080 --> 28:13:02,000 levels so we're going to open this up in 37981 28:13:00,232 --> 28:13:04,160 visual studio code by dragging and 37982 28:13:02,000 --> 28:13:06,720 dropping here this is going to give us 37983 28:13:04,160 --> 28:13:09,120 our good starting point you see we have 37984 28:13:06,720 --> 28:13:11,440 a bunch of classes in here but all these 37985 28:13:09,119 --> 28:13:14,000 classes are really trying to set up the 37986 28:13:11,440 --> 28:13:15,832 inheritance hierarchy we have here the 37987 28:13:14,000 --> 28:13:16,800 top class is going to be the animal 37988 28:13:16,800 --> 28:13:20,720 we can derive from animal and create a 37989 28:13:20,720 --> 28:13:26,000 and we can derive from animal and also 37990 28:13:23,039 --> 28:13:28,079 create a bird class we can derive from 37991 28:13:26,000 --> 28:13:30,080 feline and create a dark we can derive 37992 28:13:28,080 --> 28:13:32,400 from feline and create a cat 37993 28:13:30,080 --> 28:13:34,960 and we can also derive from bird create 37994 28:13:32,399 --> 28:13:38,159 a pigeon we can also derive from bird 37995 28:13:34,960 --> 28:13:39,912 and create a crowd class and at each 37996 28:13:38,160 --> 28:13:41,680 level we're going to have a virtual 37997 28:13:41,679 --> 28:13:47,439 that other deriving classes can call 37998 28:13:45,191 --> 28:13:49,679 through polymorphism for example if we 37999 28:13:47,440 --> 28:13:52,551 do animal polymorphism we will get the 38000 28:13:49,679 --> 28:13:55,279 correct breathe method called regardless 38001 28:13:52,551 --> 28:13:57,279 of the actual object we are managing 38002 28:13:57,279 --> 28:14:02,399 if we do feline polymorphism we can 38003 28:14:00,000 --> 28:14:05,191 manage our objects through feline 38004 28:14:02,399 --> 28:14:07,911 pointers but we will get the correct run 38005 28:14:05,191 --> 28:14:10,399 method called regardless of which kind 38006 28:14:07,911 --> 28:14:13,039 of object we are calling this method on 38007 28:14:10,399 --> 28:14:15,759 so if we call the run method on a base 38008 28:14:13,039 --> 28:14:18,000 pointer managing a dog object we will 38009 28:14:15,759 --> 28:14:20,231 get the run method called on the dog 38010 28:14:18,000 --> 28:14:22,320 object this is what we mean here okay so 38011 28:14:20,232 --> 28:14:25,440 we have our classes here let's take a 38012 28:14:22,320 --> 28:14:27,760 closer look at our animal class it is 38013 28:14:25,440 --> 28:14:30,080 our top level class and it is going to 38014 28:14:27,759 --> 28:14:32,639 have a virtual method which is called 38015 28:14:30,080 --> 28:14:35,600 breathe it is caused because it is not 38016 28:14:32,639 --> 28:14:37,679 modifying our class here notice that we 38017 28:14:35,600 --> 28:14:40,232 don't have a virtual destructor because 38018 28:14:37,679 --> 28:14:42,959 i wanted to keep this simple but in your 38019 28:14:40,232 --> 28:14:45,600 practical object you should make your 38020 28:14:42,960 --> 28:14:47,680 destructors virtual if you happen to 38021 28:14:45,600 --> 28:14:49,832 have a virtual function in your class 38022 28:14:47,679 --> 28:14:51,679 here so for example we can even do that 38023 28:14:49,831 --> 28:14:53,759 let's make this virtual this is not 38024 28:14:54,551 --> 28:14:59,039 this is going to be our top class we 38025 28:14:56,720 --> 28:15:01,191 cannot look at the implementation file 38026 28:14:59,039 --> 28:15:03,439 it is not going to do anything special 38027 28:15:01,191 --> 28:15:05,679 it is just going to forward whatever 38028 28:15:03,440 --> 28:15:07,911 parameter we pass and use that to 38029 28:15:05,679 --> 28:15:10,551 initialize our member variable i think 38030 28:15:07,911 --> 28:15:13,279 we can also go in and change our 38031 28:15:10,551 --> 28:15:16,551 parameter to a string view so we can do 38032 28:15:13,279 --> 28:15:18,639 that hdd string view let's go in the cpp 38033 28:15:16,551 --> 28:15:21,191 file and also change that pretty quick 38034 28:15:18,639 --> 28:15:23,440 we're going to say std string view here 38035 28:15:21,191 --> 28:15:26,720 and this is our animal class we're going 38036 28:15:23,440 --> 28:15:28,720 to go in our feline class because that's 38037 28:15:28,720 --> 28:15:34,000 feline is going to be inheriting from 38038 28:15:30,639 --> 28:15:35,600 animal publicly just as we see here 38039 28:15:34,000 --> 28:15:37,279 we're not going to really do anything 38040 28:15:35,600 --> 28:15:39,680 special we're going to set up a member 38041 28:15:39,679 --> 28:15:43,511 and we're going to forward the data from 38042 28:15:41,679 --> 28:15:46,231 our constructor and use that to 38043 28:15:43,512 --> 28:15:48,639 initialize stuff in our class here our 38044 28:15:46,232 --> 28:15:51,680 parameters are also going to be passed 38045 28:15:48,639 --> 28:15:54,551 as std string view types so let's do 38046 28:15:51,679 --> 28:15:57,191 that here and this is in place we also 38047 28:15:54,551 --> 28:16:00,080 have a virtual method which is called 38048 28:15:57,191 --> 28:16:02,319 run and inside we are just printing 38049 28:16:00,080 --> 28:16:05,191 which kind of object we have 38050 28:16:02,320 --> 28:16:06,960 and uh giving a hint on the method that 38051 28:16:05,191 --> 28:16:09,512 is being called here this is what we 38052 28:16:06,960 --> 28:16:12,720 just do here and we don't intend to 38053 28:16:09,512 --> 28:16:15,760 create feline objects directly that's 38054 28:16:12,720 --> 28:16:18,160 why we're not setting up any overrides 38055 28:16:15,759 --> 28:16:20,551 of the top that's why we are not setting 38056 28:16:20,551 --> 28:16:24,551 overrides for these methods but we will 38057 28:16:22,639 --> 28:16:26,551 do that in downstream classes for 38058 28:16:24,551 --> 28:16:28,959 example dog and cat because that's going 38059 28:16:26,551 --> 28:16:31,679 to make sense there let's go in the cpp 38060 28:16:28,960 --> 28:16:33,680 file for feline and also change things a 38061 28:16:31,679 --> 28:16:35,911 little bit we're going to use std string 38062 28:16:33,679 --> 28:16:38,399 view parameters let's do that for the 38063 28:16:35,911 --> 28:16:41,191 second parameter as well and this is our 38064 28:16:38,399 --> 28:16:43,911 filling class so we can go in and look 38065 28:16:41,191 --> 28:16:46,479 at our dog class let's see if we can 38066 28:16:43,911 --> 28:16:48,720 find it and i don't think we made our 38067 28:16:46,479 --> 28:16:51,512 destructor in feline a virtual so let's 38068 28:16:48,720 --> 28:16:53,360 do that and look at our delta class 38069 28:16:51,512 --> 28:16:56,320 pretty quick here we have our dog it's 38070 28:16:53,360 --> 28:16:59,680 going to have no member variable at all 38071 28:16:56,320 --> 28:17:02,872 and it will just forward this data to 38072 28:16:59,679 --> 28:17:05,279 upstream classes but we can override our 38073 28:17:02,872 --> 28:17:08,320 breathe method we get from the animal 38074 28:17:05,279 --> 28:17:11,119 class we can do that and we are going to 38075 28:17:08,320 --> 28:17:13,360 say we can grab this entire thing and 38076 28:17:11,119 --> 28:17:14,871 we're going to go in dark we can do that 38077 28:17:13,360 --> 28:17:16,960 if we want we're going to put this 38078 28:17:14,872 --> 28:17:18,800 method down below here and we're going 38079 28:17:16,960 --> 28:17:21,832 to say breathe we're going to say that 38080 28:17:18,800 --> 28:17:23,911 this is overriding what we have on top 38081 28:17:21,831 --> 28:17:26,399 and we're going to say dog breathe 38082 28:17:23,911 --> 28:17:29,440 called this is going to do and we also 38083 28:17:26,399 --> 28:17:32,319 need to change our parameters to hdd 38084 28:17:29,440 --> 28:17:34,400 strength view let's do that this is 38085 28:17:32,320 --> 28:17:37,440 going to make things work better because 38086 28:17:34,399 --> 28:17:40,000 we will avoid unnecessary copies and we 38087 28:17:37,440 --> 28:17:42,400 will be able to pass string literals as 38088 28:17:40,000 --> 28:17:44,800 parameters to this constructor here this 38089 28:17:42,399 --> 28:17:47,831 is what we want to achieve with us we 38090 28:17:44,800 --> 28:17:48,960 can hit the cpp file for a dog class and 38091 28:17:48,960 --> 28:17:54,080 change the parameters again std string 38092 28:17:51,679 --> 28:17:55,911 view and we're going to say std string 38093 28:17:54,080 --> 28:17:58,000 view as well here and we're going to go 38094 28:17:55,911 --> 28:18:00,160 to the header file again and make sure 38095 28:17:58,000 --> 28:18:02,479 we have a virtual destructor here 38096 28:18:00,160 --> 28:18:04,639 because this is good practice if you are 38097 28:18:02,479 --> 28:18:06,800 using virtual functions like we are 38098 28:18:04,639 --> 28:18:09,831 doing here we can grab our breathe 38099 28:18:06,800 --> 28:18:12,400 method and hop over to our cat class 38100 28:18:09,831 --> 28:18:15,191 again remember the inheritance hierarchy 38101 28:18:12,399 --> 28:18:17,511 we are going after is what we have here 38102 28:18:15,191 --> 28:18:18,959 so if you are confused please come back 38103 28:18:17,512 --> 28:18:20,960 and look at this you're going to really 38104 28:18:18,960 --> 28:18:22,720 understand what we are doing here we're 38105 28:18:20,960 --> 28:18:25,192 going to go to the dog class and make 38106 28:18:22,720 --> 28:18:28,160 sure it is working as we want so we just 38107 28:18:25,191 --> 28:18:30,319 did the dog class and we just made sure 38108 28:18:28,160 --> 28:18:32,872 it works like we want now we're going to 38109 28:18:30,320 --> 28:18:34,720 hop over to cat and make sure it is now 38110 28:18:32,872 --> 28:18:37,760 we're going to hop over to the cat class 38111 28:18:34,720 --> 28:18:40,399 and make sure it is overriding the brief 38112 28:18:37,759 --> 28:18:42,231 method to make sure the correct method 38113 28:18:40,399 --> 28:18:44,959 is called okay so let's do that we're 38114 28:18:42,232 --> 28:18:46,080 going to hop over to our cat class it is 38115 28:18:46,080 --> 28:18:52,080 have its own meow method but it can also 38116 28:18:49,360 --> 28:18:54,479 override the breathe method we get from 38117 28:18:52,080 --> 28:18:55,600 the animal class so let's do that we can 38118 28:18:55,600 --> 28:19:00,080 and we can go to our cat class let's do 38119 28:18:58,479 --> 28:19:02,319 that we're going to put in our virtual 38120 28:19:00,080 --> 28:19:05,360 method and we're going to say cat 38121 28:19:02,320 --> 28:19:08,232 breathe and this is really all we also 38122 28:19:05,360 --> 28:19:11,360 need to set up our parameters correctly 38123 28:19:08,232 --> 28:19:14,320 using std string view let's do this std 38124 28:19:11,360 --> 28:19:16,720 string view and this is really all we 38125 28:19:14,320 --> 28:19:18,800 need to do here we can also make our 38126 28:19:16,720 --> 28:19:21,600 destructor virtual because this is good 38127 28:19:18,800 --> 28:19:24,479 practice we hop over in the cpp file and 38128 28:19:21,600 --> 28:19:27,120 change our parameters to be of type std 38129 28:19:24,479 --> 28:19:28,872 string view let's do this and this is 38130 28:19:27,119 --> 28:19:31,039 done we can really work with this 38131 28:19:28,872 --> 28:19:34,160 inheritance hierarchy here we're going 38132 28:19:31,039 --> 28:19:36,959 to also go pretty fast and work on the 38133 28:19:34,160 --> 28:19:39,440 other side of our inheritance hierarchy 38134 28:19:36,960 --> 28:19:41,760 we're going to work on birds let's look 38135 28:19:39,440 --> 28:19:44,000 at the bird class it is going to inherit 38136 28:19:41,759 --> 28:19:45,911 from animal it is not going to do 38137 28:19:44,000 --> 28:19:48,160 anything special really but we just want 38138 28:19:45,911 --> 28:19:50,720 to change the type of our parameters to 38139 28:19:48,160 --> 28:19:53,279 std string view and i'm going to do that 38140 28:19:50,720 --> 28:19:55,512 on the second parameter and we are going 38141 28:19:53,279 --> 28:19:57,440 to hop over in the cpp file and change 38142 28:19:55,512 --> 28:20:00,000 these things here let's do that we're 38143 28:19:57,440 --> 28:20:04,080 going to say std string view and do the 38144 28:20:00,000 --> 28:20:06,960 same thing here std string view and bird 38145 28:20:04,080 --> 28:20:09,279 is done we can grab our breathe method 38146 28:20:06,960 --> 28:20:12,000 for example let's go back to the top and 38147 28:20:09,279 --> 28:20:14,399 grab that from our animal class and 38148 28:20:12,000 --> 28:20:16,720 we're going to hop over to one kind of 38149 28:20:14,399 --> 28:20:17,759 bird we have in our program we can look 38150 28:20:17,759 --> 28:20:21,439 pigeon for example the last class here 38151 28:20:19,911 --> 28:20:24,080 we're going to override the breathe 38152 28:20:21,440 --> 28:20:26,232 method and i'm going to say pigeon 38153 28:20:24,080 --> 28:20:28,720 breathe and we are going to change the 38154 28:20:26,232 --> 28:20:30,960 type of our parameters here to std 38155 28:20:28,720 --> 28:20:34,080 string view and i'm going to do the same 38156 28:20:30,960 --> 28:20:36,960 here and let's hop over to the cpp file 38157 28:20:34,080 --> 28:20:38,872 and change the type of our parameter 38158 28:20:36,960 --> 28:20:41,680 let's do that for the second parameter 38159 28:20:38,872 --> 28:20:44,872 here this is done let's look at the 38160 28:20:41,679 --> 28:20:48,479 second kind of bird we have i think we 38161 28:20:44,872 --> 28:20:50,479 have the crow class we also want to 38162 28:20:48,479 --> 28:20:54,639 override the breathe method we're going 38163 28:20:50,479 --> 28:20:57,759 to say crow breathe and we can go up and 38164 28:20:54,639 --> 28:20:59,759 change our parameter to be of type std 38165 28:20:57,759 --> 28:21:02,159 string view and we're going to do that 38166 28:20:59,759 --> 28:21:04,639 here again if you are not interested in 38167 28:21:02,160 --> 28:21:06,000 these changes you can just grab the code 38168 28:21:04,639 --> 28:21:08,399 and you're going to have all these 38169 28:21:06,000 --> 28:21:10,720 classes ready for use i just wanted to 38170 28:21:08,399 --> 28:21:13,119 correct this little problem i had in my 38171 28:21:10,720 --> 28:21:15,759 classes on my drive because this is not 38172 28:21:13,119 --> 28:21:17,759 good to design in modern c plus plus we 38173 28:21:15,759 --> 28:21:19,911 wanted to do things proper because we 38174 28:21:17,759 --> 28:21:22,639 are really learning about modern c plus 38175 28:21:19,911 --> 28:21:24,639 plus here let's hop over to the cpp file 38176 28:21:22,639 --> 28:21:28,080 and finish this up we're going to change 38177 28:21:24,639 --> 28:21:30,639 the type to hdd stream view and uh let's 38178 28:21:28,080 --> 28:21:32,960 use view correctly and we're going to 38179 28:21:30,639 --> 28:21:35,440 say std string view on the second 38180 28:21:32,960 --> 28:21:37,832 parameter and this is done now we have 38181 28:21:35,440 --> 28:21:40,400 our inheritance hierarchy and we're 38182 28:21:37,831 --> 28:21:42,959 going to start by looking at animal 38183 28:21:40,399 --> 28:21:45,439 polymorphism again we just set up this 38184 28:21:42,960 --> 28:21:48,872 inheritance hierarchy now we are going 38185 28:21:45,440 --> 28:21:50,720 to try and do animal polymorphism okay 38186 28:21:48,872 --> 28:21:53,512 so now we're going to hop over to the 38187 28:21:50,720 --> 28:21:55,680 main cpp file and we are going to clean 38188 28:21:53,512 --> 28:21:58,720 it up a little bit remove whatever it is 38189 28:21:55,679 --> 28:22:00,719 we don't need we are going to put in our 38190 28:21:58,720 --> 28:22:04,080 includes we're going to include all of 38191 28:22:00,720 --> 28:22:06,479 the fans in our file here animal feline 38192 28:22:04,080 --> 28:22:09,440 dog cat bird pigeon and crow we're going 38193 28:22:06,479 --> 28:22:12,479 to include all this and we just want to 38194 28:22:09,440 --> 28:22:14,479 play with animal polymorphism here we 38195 28:22:12,479 --> 28:22:17,039 are going to set up a bunch of objects 38196 28:22:14,479 --> 28:22:19,831 and we're going to put in dog cat pigeon 38197 28:22:17,039 --> 28:22:21,911 and crow all kinds of crazy animals we 38198 28:22:19,831 --> 28:22:24,479 can create in our c plus plus program 38199 28:22:21,911 --> 28:22:27,279 and we're going to put them in an array 38200 28:22:24,479 --> 28:22:29,679 that is storing animal pointer and this 38201 28:22:29,679 --> 28:22:35,911 enable polymorphism for our objects that 38202 28:22:32,800 --> 28:22:38,232 we have in here remember animal sets up 38203 28:22:35,911 --> 28:22:40,551 a virtual method called draw if we go 38204 28:22:38,232 --> 28:22:43,040 back we're going to find that and we 38205 28:22:40,551 --> 28:22:45,599 made sure to override this method in all 38206 28:22:43,039 --> 28:22:47,831 kinds of crazy animals we can create for 38207 28:22:45,600 --> 28:22:50,960 example in our cat we have a breathe 38208 28:22:47,831 --> 28:22:53,191 method overridden in our dog class we 38209 28:22:50,960 --> 28:22:56,160 have the breathe method overridden and 38210 28:22:53,191 --> 28:22:59,679 what we want to achieve in our main cpp 38211 28:22:56,160 --> 28:23:02,552 file let's go there if i can find it we 38212 28:22:59,679 --> 28:23:04,000 want to achieve a setup so that if we 38213 28:23:02,551 --> 28:23:06,872 call the breathe method we're going to 38214 28:23:04,000 --> 28:23:09,279 call the most specific breathe method on 38215 28:23:06,872 --> 28:23:11,600 our animal pointer here even if we are 38216 28:23:09,279 --> 28:23:13,759 calling that method on a base pointer 38217 28:23:11,600 --> 28:23:16,160 this is what we want to achieve this is 38218 28:23:13,759 --> 28:23:19,279 going to be animal polymorphism because 38219 28:23:16,160 --> 28:23:21,680 the base pointer is of animal type this 38220 28:23:19,279 --> 28:23:23,911 is what we mean here let's try and build 38221 28:23:21,679 --> 28:23:25,511 this program so if we look and call the 38222 28:23:23,911 --> 28:23:27,679 breathe method the correct breathe 38223 28:23:25,512 --> 28:23:29,832 method is going to be called and what we 38224 28:23:27,679 --> 28:23:31,911 expect for the first object which is a 38225 28:23:29,831 --> 28:23:34,159 dark object we're going to get the dark 38226 28:23:31,911 --> 28:23:36,000 breathe method called for the second 38227 28:23:34,160 --> 28:23:38,639 iteration the category breathe method is 38228 28:23:36,000 --> 28:23:41,440 going to be called for the third 38229 28:23:38,639 --> 28:23:43,360 for the third iteration the pigeon 38230 28:23:41,440 --> 28:23:45,512 breath method is going to be called for 38231 28:23:45,512 --> 28:23:49,680 crow breathe method is going to be 38232 28:23:47,360 --> 28:23:52,000 called this is what we expect to see 38233 28:23:49,679 --> 28:23:53,831 if we run this program here so let's do 38234 28:23:52,000 --> 28:23:57,191 this we're going to pass this through 38235 28:23:53,831 --> 28:23:59,911 gcc our favorite compiler we can use any 38236 28:23:57,191 --> 28:24:02,080 compiler we want but gcc is going to do 38237 28:23:59,911 --> 28:24:04,479 in this case okay the build is good we 38238 28:24:02,080 --> 28:24:06,872 can bring up a powershell window to play 38239 28:24:04,479 --> 28:24:09,039 with and we're going to run rooster our 38240 28:24:06,872 --> 28:24:11,760 program and if we run this you're going 38241 28:24:09,039 --> 28:24:13,911 to see that in our loop the first 38242 28:24:11,759 --> 28:24:16,080 iteration is going to call dog breathe 38243 28:24:13,911 --> 28:24:18,080 because the first object is a dog the 38244 28:24:16,080 --> 28:24:19,911 second iteration is going to call cat 38245 28:24:18,080 --> 28:24:20,720 breathe because the second object is a 38246 28:24:20,720 --> 28:24:24,639 the third iteration is going to call 38247 28:24:22,399 --> 28:24:26,799 pigeon breathe because the third object 38248 28:24:24,639 --> 28:24:29,119 is a pigeon and the last iteration is 38249 28:24:26,800 --> 28:24:31,911 going to call crow breathe because the 38250 28:24:29,119 --> 28:24:34,080 last object is a crow and notice that we 38251 28:24:31,911 --> 28:24:36,679 are calling the breathe method through a 38252 28:24:34,080 --> 28:24:39,191 base animal pointer and this is what 38253 28:24:36,679 --> 28:24:41,679 polymorphism is all about and what we 38254 28:24:39,191 --> 28:24:44,231 are doing here is animal polymorphism 38255 28:24:41,679 --> 28:24:46,551 but this is not what we are really after 38256 28:24:44,232 --> 28:24:48,080 in this lecture we want to see that we 38257 28:24:50,399 --> 28:24:55,039 bird polymorphism in our inheritance 38258 28:24:52,960 --> 28:24:58,320 hierarchy so we're going to put in a 38259 28:24:55,039 --> 28:25:00,871 separator let's do sddc out and put in a 38260 28:24:58,320 --> 28:25:02,800 bunch of dashes here or hyphens some 38261 28:25:00,872 --> 28:25:05,279 people call them like that doesn't 38262 28:25:02,800 --> 28:25:08,160 really matter and we're going to play 38263 28:25:05,279 --> 28:25:10,479 with feline polymorphism let's do this 38264 28:25:08,160 --> 28:25:12,552 we are going to go down and put in a 38265 28:25:10,479 --> 28:25:15,119 simple piece of coat that we're going to 38266 28:25:12,551 --> 28:25:16,720 be using to play with things here and 38267 28:25:15,119 --> 28:25:18,871 we're going to set up a bunch of objects 38268 28:25:16,720 --> 28:25:22,399 we have a dog we have a cat we have a 38269 28:25:18,872 --> 28:25:26,160 pigeon and we are going to use a feline 38270 28:25:22,399 --> 28:25:27,759 pointer to manage feline derived objects 38271 28:25:26,160 --> 28:25:30,000 for example we're going to use this 38272 28:25:27,759 --> 28:25:31,759 pointer to manage a dog and a cat but 38273 28:25:30,000 --> 28:25:34,000 one thing i want you to see is that if 38274 28:25:31,759 --> 28:25:36,720 you put in a pigeon for example this is 38275 28:25:34,000 --> 28:25:39,119 not going to work because a pigeon is 38276 28:25:36,720 --> 28:25:41,119 not a feline so the compiler is going to 38277 28:25:39,119 --> 28:25:43,191 give you a compiler error let's try and 38278 28:25:41,119 --> 28:25:46,159 do that we're going to try and put in 38279 28:25:43,191 --> 28:25:47,831 pigeon here pigeon 2 you can select this 38280 28:25:46,160 --> 28:25:50,232 and if we do that you're going to see 38281 28:25:47,831 --> 28:25:51,911 that we get a squeaky line this is the 38282 28:25:50,232 --> 28:25:53,120 sign that this is going to go wrong 38283 28:25:53,119 --> 28:25:58,959 we are storing in felines and the pigeon 38284 28:25:55,679 --> 28:26:01,279 is really not a feline even by our 38285 28:25:58,960 --> 28:26:02,872 inheritance hierarchy so the compiler is 38286 28:26:01,279 --> 28:26:04,959 going to give us a compiler error let's 38287 28:26:02,872 --> 28:26:07,440 try and weld to show you the compiler 38288 28:26:04,960 --> 28:26:09,192 error okay the world finished with 38289 28:26:07,440 --> 28:26:12,479 errors and if we go and we're going to 38290 28:26:09,191 --> 28:26:14,799 find that cannot convert pigeon to feed 38291 28:26:12,479 --> 28:26:16,872 line pointer so this is the error here 38292 28:26:14,800 --> 28:26:19,040 you can't do something like this even if 38293 28:26:16,872 --> 28:26:21,600 you create an animal object let's say 38294 28:26:19,039 --> 28:26:23,911 that we're going to go in and create 38295 28:26:21,600 --> 28:26:25,760 animal and we're going to say animal one 38296 28:26:23,911 --> 28:26:27,679 we can do that and we're going to pass 38297 28:26:25,759 --> 28:26:30,000 in the description i think we can do 38298 28:26:27,679 --> 28:26:32,231 that we can do something like this and 38299 28:26:30,000 --> 28:26:34,551 if we try and pass in the address of 38300 28:26:32,232 --> 28:26:36,872 animal one let's do that this is also 38301 28:26:34,551 --> 28:26:40,399 going to give us a compiler error 38302 28:26:36,872 --> 28:26:42,720 because animal is not a feline again if 38303 28:26:40,399 --> 28:26:45,279 you get confused come back and look at 38304 28:26:42,720 --> 28:26:47,512 our inheritance hierarchy here 38305 28:26:45,279 --> 28:26:50,639 what we are setting up in our code we 38306 28:26:47,512 --> 28:26:53,680 are using feline polymorphism but we 38307 28:26:50,639 --> 28:26:56,479 want to store an animal in a raid that 38308 28:26:53,679 --> 28:26:59,039 is storing felines and this is going to 38309 28:26:56,479 --> 28:27:01,279 be a problem because the compiler isn't 38310 28:26:59,039 --> 28:27:03,439 really going to be able to process this 38311 28:27:01,279 --> 28:27:06,000 our base pointer is a feline and we 38312 28:27:03,440 --> 28:27:09,040 should really be able to only store in 38313 28:27:06,000 --> 28:27:11,679 derived types from feline but we are 38314 28:27:09,039 --> 28:27:13,279 putting in a superclass or a parent 38315 28:27:13,279 --> 28:27:19,039 which will not be able to have these 38316 28:27:16,679 --> 28:27:20,799 specializations that feline is doing so 38317 28:27:19,039 --> 28:27:22,871 the compiler is really saying this is 38318 28:27:20,800 --> 28:27:24,800 crazy i can't do this i am going to 38319 28:27:22,872 --> 28:27:26,872 throw a compiler error this is the 38320 28:27:24,800 --> 28:27:28,551 behavior we get here okay so this is 38321 28:27:26,872 --> 28:27:30,320 going to give us a compiler error and 38322 28:27:28,551 --> 28:27:32,872 let's run and see that we're going to 38323 28:27:30,320 --> 28:27:34,960 use gcc the book is going to finish with 38324 28:27:32,872 --> 28:27:37,440 an error and we are going to see that we 38325 28:27:34,960 --> 28:27:39,600 have an invalid conversion from animal 38326 28:27:37,440 --> 28:27:41,600 pointer to filament pointer this is what 38327 28:27:39,600 --> 28:27:45,440 we want to do here we want to convert an 38328 28:27:41,600 --> 28:27:48,400 animal pointer and store that in a slot 38329 28:27:45,440 --> 28:27:50,639 that is only supposed to store feline 38330 28:27:48,399 --> 28:27:52,959 pointers so this is not going to work 38331 28:27:50,639 --> 28:27:54,160 but if you do everything right and only 38332 28:27:54,160 --> 28:27:59,120 derived types of feline you're going to 38333 28:27:56,320 --> 28:28:02,080 get the correct length to work and let's 38334 28:27:59,119 --> 28:28:04,399 look at our dog class let's open it 38335 28:28:02,080 --> 28:28:07,512 again we're going to look at dog and 38336 28:28:04,399 --> 28:28:10,399 we're going to have a run method but we 38337 28:28:07,512 --> 28:28:12,160 don't have that set up for our dog class 38338 28:28:10,399 --> 28:28:14,079 but we can do that pretty quick let's 38339 28:28:12,160 --> 28:28:16,872 look at cat and see if we have a run 38340 28:28:14,080 --> 28:28:19,600 method we don't but we can set that up 38341 28:28:16,872 --> 28:28:21,760 let's go to feline and copy our virtual 38342 28:28:21,759 --> 28:28:26,479 and we are going to go in dog 38343 28:28:24,320 --> 28:28:28,160 and set up another virtual method which 38344 28:28:28,160 --> 28:28:33,279 run and this is going to be an override 38345 28:28:30,800 --> 28:28:35,600 of what we have in our inheritance 38346 28:28:33,279 --> 28:28:38,080 hierarchy so this is an override method 38347 28:28:35,600 --> 28:28:40,320 we are also going to do the same but 38348 28:28:38,080 --> 28:28:42,800 before we go let's say that this is the 38349 28:28:40,320 --> 28:28:44,960 dog class we are doing thanks from 38350 28:28:42,800 --> 28:28:46,960 we are going to grab this method again 38351 28:28:44,960 --> 28:28:49,440 and go in cat because that's another 38352 28:28:46,960 --> 28:28:51,192 feline we have in our program we can do 38353 28:28:49,440 --> 28:28:53,040 something like this and we're going to 38354 28:28:53,039 --> 28:28:58,159 okay so now let's come back to maine 38355 28:28:56,080 --> 28:29:00,639 and we're going to weld again we're 38356 28:28:58,160 --> 28:29:03,279 going to pass this through gcc and we 38357 28:29:00,639 --> 28:29:05,440 still have a pigeon in here so this is 38358 28:29:03,279 --> 28:29:07,831 not good let's check this out 38359 28:29:05,440 --> 28:29:10,551 make sure that we only have a few lines 38360 28:29:07,831 --> 28:29:12,479 in this array here let's build again 38361 28:29:10,551 --> 28:29:14,231 okay you see the bolt is good 38362 28:29:14,232 --> 28:29:18,160 and run rooster we're going to have our 38363 28:29:16,080 --> 28:29:20,872 separator here and you're going to see 38364 28:29:18,160 --> 28:29:22,639 that we are calling our dog method at 38365 28:29:20,872 --> 28:29:26,000 the first iteration because the first 38366 28:29:22,639 --> 28:29:28,639 object is a dog and we are calling a cat 38367 28:29:26,000 --> 28:29:32,080 running method because the second object 38368 28:29:28,639 --> 28:29:34,319 is a cat and this is filing polymorphism 38369 28:29:32,080 --> 28:29:37,279 in action hopefully this proves to you 38370 28:29:34,320 --> 28:29:39,600 that we can do polymorphism at different 38371 28:29:37,279 --> 28:29:42,479 levels depending on what we want to 38372 28:29:39,600 --> 28:29:44,400 achieve in our c plus plus program to 38373 28:29:42,479 --> 28:29:47,119 close off this lecture we are also going 38374 28:29:44,399 --> 28:29:49,119 to set up bird polymorphism let's do 38375 28:29:47,119 --> 28:29:51,679 that we're going to put in a separator 38376 28:29:49,119 --> 28:29:53,911 and we're going to say vertical demod 38377 28:29:51,679 --> 28:29:56,551 present and we are going to put in a 38378 28:29:53,911 --> 28:29:57,599 bunch of objects and set up a base 38379 28:29:57,600 --> 28:30:01,192 array which is going to be a bird 38380 28:29:59,512 --> 28:30:03,832 pointer array in this case and we're 38381 28:30:01,191 --> 28:30:06,720 going to be storing in derived objects 38382 28:30:03,831 --> 28:30:08,799 of the base pointer type so we are going 38383 28:30:06,720 --> 28:30:11,360 to be storing any derived objects of 38384 28:30:08,800 --> 28:30:13,680 bird and we have a pigeon and a crow and 38385 28:30:11,360 --> 28:30:17,039 this is the setup we want here now let's 38386 28:30:13,679 --> 28:30:19,279 go and look at the setup we have in our 38387 28:30:17,039 --> 28:30:21,279 pigeon for example we don't have a fly 38388 28:30:19,279 --> 28:30:23,279 method we just have a cool method 38389 28:30:23,279 --> 28:30:27,911 if we go in our crow we're going to see 38390 28:30:27,911 --> 28:30:32,720 fly method we're just having the cow 38391 28:30:30,320 --> 28:30:36,400 method because the crowd calls 38392 28:30:32,720 --> 28:30:38,800 but if we call our fly method because we 38393 28:30:36,399 --> 28:30:41,191 don't have this method overridden in any 38394 28:30:43,759 --> 28:30:48,080 version of the fly method called so we 38395 28:30:48,080 --> 28:30:52,800 called i want you to see this let's hop 38396 28:30:50,551 --> 28:30:54,872 over to the main cpp file and make sure 38397 28:30:52,800 --> 28:30:56,551 we have the correct setup looks like 38398 28:30:54,872 --> 28:30:59,040 this is what we want we're going to 38399 28:30:56,551 --> 28:31:01,831 build this with gcc the book is going to 38400 28:30:59,039 --> 28:31:04,639 be good as we see here and we're going 38401 28:31:01,831 --> 28:31:07,679 to run now we're going to see bird fly 38402 28:31:04,639 --> 28:31:11,440 bird fly for pigeon one and crow one 38403 28:31:07,679 --> 28:31:14,319 but this is not using any derived method 38404 28:31:14,320 --> 28:31:18,400 and a pigeon class if you want you can 38405 28:31:18,399 --> 28:31:22,871 fly method and you're going to get that 38406 28:31:20,232 --> 28:31:25,192 called let's actually do that because 38407 28:31:22,872 --> 28:31:26,872 i'd like to show you things directly so 38408 28:31:25,191 --> 28:31:29,360 that you can really see with your own 38409 28:31:26,872 --> 28:31:31,512 eyes so we're going to go in bird and 38410 28:31:29,360 --> 28:31:34,551 grab the fly method and we're going to 38411 28:31:31,512 --> 28:31:37,760 override this in pigeon we can do that 38412 28:31:34,551 --> 28:31:40,720 and we're going to override here and we 38413 28:31:37,759 --> 28:31:43,759 are going to say pigeon here and we can 38414 28:31:40,720 --> 28:31:46,960 grab this and i put this method also in 38415 28:31:43,759 --> 28:31:49,759 the crowd class can do that and we're 38416 28:31:46,960 --> 28:31:51,192 going to say crow here now if we run 38417 28:31:51,191 --> 28:31:55,191 using what we have in the main cpp file 38418 28:31:53,679 --> 28:31:57,679 for the first iteration we're going to 38419 28:31:55,191 --> 28:31:59,512 call the fly method on the pigeon object 38420 28:31:57,679 --> 28:32:02,079 because we have the fly method 38421 28:31:59,512 --> 28:32:03,832 overwritten at the pigeon level 38422 28:32:02,080 --> 28:32:05,512 as we see here this is right here 38423 28:32:03,831 --> 28:32:08,080 beneath our eyes and we're going to see 38424 28:32:05,512 --> 28:32:10,000 this in action if we run this program 38425 28:32:08,080 --> 28:32:13,440 here the same is going to happen for the 38426 28:32:10,000 --> 28:32:15,119 crow object here let's build with gcc to 38427 28:32:13,440 --> 28:32:17,191 really prove this the builder is going 38428 28:32:15,119 --> 28:32:18,871 to go through we can clear and run 38429 28:32:17,191 --> 28:32:21,759 rooster and we're going to see pigeon 38430 28:32:18,872 --> 28:32:23,832 fly crowd fly this is proving what we 38431 28:32:21,759 --> 28:32:25,759 set up in this lecture here this is 38432 28:32:23,831 --> 28:32:27,759 really all i had to share i hope you 38433 28:32:25,759 --> 28:32:30,639 found this interesting the main message 38434 28:32:27,759 --> 28:32:32,871 is that you can set up polymorphism at 38435 28:32:30,639 --> 28:32:35,191 different levels in your inheritance 38436 28:32:32,872 --> 28:32:37,512 hierarchy depending on what you want to 38437 28:32:35,191 --> 28:32:39,679 achieve in your c plus plus program we 38438 28:32:37,512 --> 28:32:41,600 saw that we could do the usual thing and 38439 28:32:41,600 --> 28:32:47,040 as the driver for our polymorphism and 38440 28:32:44,320 --> 28:32:49,440 we did animal polymorphism here but 38441 28:32:47,039 --> 28:32:52,871 nothing prevents you from doing feline 38442 28:32:49,440 --> 28:32:55,191 polymorphism or bird polymorphism as we 38443 28:32:52,872 --> 28:32:56,720 saw in this lecture we are going to stop 38444 28:32:55,191 --> 28:32:59,279 here in this one in the next one we're 38445 28:32:56,720 --> 28:33:01,119 going to see how we can do polymorphism 38446 28:33:01,119 --> 28:33:05,119 go ahead and finish up here and make me 38447 28:33:03,279 --> 28:33:08,959 there in this lecture we're going to 38448 28:33:05,119 --> 28:33:11,679 explore how inheritance and polymorphism 38449 28:33:08,960 --> 28:33:14,639 play out with static members that we 38450 28:33:11,679 --> 28:33:17,119 have in our classes and we're going to 38451 28:33:14,639 --> 28:33:18,639 be using this inheritance hierarchy here 38452 28:33:17,119 --> 28:33:21,039 our base class is going to be shape 38453 28:33:18,639 --> 28:33:23,911 we're going to inherit from that and 38454 28:33:21,039 --> 28:33:25,759 create an ellipse class let's look at 38455 28:33:23,911 --> 28:33:28,720 some code here so we're going to set up 38456 28:33:25,759 --> 28:33:31,119 our base class as shape as we said and 38457 28:33:28,720 --> 28:33:33,040 we're going to have one member variable 38458 28:33:31,119 --> 28:33:35,191 which is going to be our description 38459 28:33:33,039 --> 28:33:36,799 here we're going to have a constructor 38460 28:33:36,800 --> 28:33:39,192 and we're going to have a default 38461 28:33:37,911 --> 28:33:41,911 constructor which is really not 38462 28:33:39,191 --> 28:33:45,191 important here but notice that we have a 38463 28:33:41,911 --> 28:33:47,191 static member which is called m count 38464 28:33:45,191 --> 28:33:48,319 and if you remember a static member 38465 28:33:48,320 --> 28:33:53,760 is a kind of variable which is 38466 28:33:50,551 --> 28:33:56,080 associated with the type itself it's not 38467 28:33:53,759 --> 28:33:58,639 associated with any object that you 38468 28:33:56,080 --> 28:34:01,119 create if you forgot this please go back 38469 28:33:58,639 --> 28:34:03,191 to our chapter on classes or user 38470 28:34:01,119 --> 28:34:05,911 defined types you're going to learn all 38471 28:34:03,191 --> 28:34:08,159 about static variables here we are just 38472 28:34:05,911 --> 28:34:11,279 exploring how they play out with 38473 28:34:08,160 --> 28:34:13,760 inheritance and polymorphism now we have 38474 28:34:11,279 --> 28:34:16,160 a shape class if we inherit from it and 38475 28:34:13,759 --> 28:34:18,639 create an ellipse class for example 38476 28:34:16,160 --> 28:34:21,440 we're going to have all the data that 38477 28:34:18,639 --> 28:34:23,679 was public in our base class to be 38478 28:34:21,440 --> 28:34:25,760 public in this class as well so by that 38479 28:34:23,679 --> 28:34:27,831 logic we are also going to get this 38480 28:34:25,759 --> 28:34:30,159 static variable inherited and we're 38481 28:34:27,831 --> 28:34:32,871 going to be able to access it from 38482 28:34:30,160 --> 28:34:34,872 ellipse objects let's try and use this 38483 28:34:32,872 --> 28:34:37,120 in code here to really drive the point 38484 28:34:34,872 --> 28:34:39,911 home here we are creating a batch of 38485 28:34:37,119 --> 28:34:42,080 shape objects here is our first shape we 38486 28:34:39,911 --> 28:34:44,080 print our static variable through this 38487 28:34:42,080 --> 28:34:46,160 syntax here because it is public we can 38488 28:34:44,080 --> 28:34:48,872 do this we're going to get a one because 38489 28:34:46,160 --> 28:34:50,639 we just have one shape here we create a 38490 28:34:48,872 --> 28:34:52,551 second shape and we print the count 38491 28:34:50,639 --> 28:34:55,191 we're going to get two because we just 38492 28:34:52,551 --> 28:34:57,360 have two shapes in here we create our 38493 28:34:55,191 --> 28:34:59,119 third shape and we try and print the 38494 28:34:57,360 --> 28:35:01,600 count we're going to get three and if 38495 28:34:59,119 --> 28:35:04,000 you wonder how we are incrementing this 38496 28:35:01,600 --> 28:35:05,600 static variable we may do this in our 38497 28:35:04,000 --> 28:35:07,759 constructor we're going to see how this 38498 28:35:05,600 --> 28:35:09,760 works exactly once we hit visual studio 38499 28:35:07,759 --> 28:35:11,360 code in a minute but note that every 38500 28:35:09,759 --> 28:35:13,679 time we create an object we're going to 38501 28:35:11,360 --> 28:35:16,232 be incrementing our static variable 38502 28:35:13,679 --> 28:35:18,639 that's why we see one two three as we 38503 28:35:16,232 --> 28:35:21,192 created these objects here this is the 38504 28:35:18,639 --> 28:35:23,831 logic now we have three shapes here we 38505 28:35:21,191 --> 28:35:26,399 go down and create a new object which is 38506 28:35:23,831 --> 28:35:28,399 of ellipse type if we print the shape 38507 28:35:26,399 --> 28:35:31,039 count we're going to get four because an 38508 28:35:28,399 --> 28:35:33,599 ellipse is a shape and ellipse is 38509 28:35:31,039 --> 28:35:35,911 inheriting the static variable from 38510 28:35:33,600 --> 28:35:39,120 shape so we're going to get a 4 printed 38511 28:35:35,911 --> 28:35:41,191 out here but if we also print ellipse 38512 28:35:39,119 --> 28:35:44,231 count we're going to get a 4 38513 28:35:41,191 --> 28:35:46,959 even if we really just have one ellipse 38514 28:35:44,232 --> 28:35:49,512 so the static variable from shape is 38515 28:35:46,960 --> 28:35:52,080 being inherited and used in our ellipse 38516 28:35:53,759 --> 28:35:57,759 the problem here is that the static 38517 28:35:55,679 --> 28:36:00,159 variable we are using in shape and 38518 28:35:57,759 --> 28:36:03,119 ellipse are the same because the one we 38519 28:36:00,160 --> 28:36:05,279 have in ellipse was inherited from shape 38520 28:36:03,119 --> 28:36:08,159 that's why we have these results here 38521 28:36:05,279 --> 28:36:11,279 but sometimes we just want to have 38522 28:36:08,160 --> 28:36:13,600 ellipse count to b1 here because in 38523 28:36:11,279 --> 28:36:16,160 reality we really have one ellipse so 38524 28:36:13,600 --> 28:36:18,160 how do we do that to get that behavior 38525 28:36:16,160 --> 28:36:20,960 you can also set up another static 38526 28:36:18,160 --> 28:36:23,279 variable named exactly the same way at 38527 28:36:20,960 --> 28:36:25,600 the ellipse level and you can do that 38528 28:36:23,279 --> 28:36:28,000 like this you can also override the gate 38529 28:36:25,600 --> 28:36:30,400 count method to return the current count 38530 28:36:28,000 --> 28:36:32,800 in ellipse this is going to work through 38531 28:36:30,399 --> 28:36:34,551 polymorphism because the getcount method 38532 28:36:34,551 --> 28:36:38,800 if you call this method through a 38533 28:36:36,551 --> 28:36:40,800 basepointer you will get the correct one 38534 28:36:38,800 --> 28:36:43,512 called which is going to be the most 38535 28:36:40,800 --> 28:36:45,600 specific one through polymorphism but 38536 28:36:43,512 --> 28:36:47,360 what we want to see is how 38537 28:36:45,600 --> 28:36:50,080 things are going to work now that we 38538 28:36:47,360 --> 28:36:53,279 have a static variable which is named 38539 28:36:50,080 --> 28:36:56,080 exactly the same as the one we have in 38540 28:36:53,279 --> 28:36:58,720 our base class here the one in shape is 38541 28:36:56,080 --> 28:37:01,680 exactly named m count it is a static 38542 28:36:58,720 --> 28:37:04,232 variable of type and the one we have in 38543 28:37:01,679 --> 28:37:07,511 ellipse is also the same it is a static 38544 28:37:04,232 --> 28:37:08,480 variable called m count of type and but 38545 28:37:08,479 --> 28:37:12,639 ellipse is going to maintain its own 38546 28:37:10,232 --> 28:37:15,360 static variable and shape is going to 38547 28:37:12,639 --> 28:37:17,679 maintain its own static variable now we 38548 28:37:15,360 --> 28:37:20,160 are going to get this kind of behavior 38549 28:37:17,679 --> 28:37:22,399 we are going to see that if we try and 38550 28:37:20,160 --> 28:37:24,960 print the count of shapes we have we're 38551 28:37:22,399 --> 28:37:27,039 going to get a four here but now if we 38552 28:37:24,960 --> 28:37:28,552 try and print the count from ellipse 38553 28:37:27,039 --> 28:37:31,511 we're going to get a one because we 38554 28:37:28,551 --> 28:37:33,440 already have one ellipse and this is 38555 28:37:31,512 --> 28:37:35,360 working because ellipse is going to 38556 28:37:35,360 --> 28:37:39,600 static variable and this is going to 38557 28:37:37,039 --> 28:37:42,319 give you this behavior if it is what we 38558 28:37:39,600 --> 28:37:44,552 are after in our c plus plus program now 38559 28:37:42,320 --> 28:37:46,720 we can also go on and create a bunch of 38560 28:37:44,551 --> 28:37:48,720 other objects here we are going to store 38561 28:37:46,720 --> 28:37:51,911 them in this array which is going to be 38562 28:37:48,720 --> 28:37:53,680 storing shared pointers to shape 38563 28:37:51,911 --> 28:37:55,759 and we're going to store in derived 38564 28:37:53,679 --> 28:37:58,079 objects this is the syntax we can use to 38565 28:37:55,759 --> 28:38:01,191 do this and at the moment we do this 38566 28:37:58,080 --> 28:38:03,911 we're going to get six shapes because 38567 28:38:01,191 --> 28:38:05,759 in total we already have six shapes but 38568 28:38:03,911 --> 28:38:07,831 three of those shapes are going to be 38569 28:38:05,759 --> 28:38:09,831 ellipses and if we try to print the 38570 28:38:07,831 --> 28:38:12,639 ellipse count we're going to see that we 38571 28:38:09,831 --> 28:38:14,551 have three ellipses in our c plus plus 38572 28:38:12,639 --> 28:38:16,639 program and this is going to do 38573 28:38:14,551 --> 28:38:20,000 what we want another thing you could do 38574 28:38:16,639 --> 28:38:22,000 is also do shape polymorphism and call 38575 28:38:20,000 --> 28:38:24,160 the gatecount method and you're going to 38576 28:38:22,000 --> 28:38:27,279 get the correct getcount method and 38577 28:38:24,160 --> 28:38:29,440 print the most specific account if we 38578 28:38:27,279 --> 28:38:31,191 are calling that method on an ellipse 38579 28:38:29,440 --> 28:38:33,680 object for example we're going to print 38580 28:38:31,191 --> 28:38:35,279 three if we call that on a shape object 38581 28:38:33,679 --> 28:38:37,319 we're going to get sex and that's the 38582 28:38:35,279 --> 28:38:39,600 behavior we could achieve with 38583 28:38:37,320 --> 28:38:41,512 polymorphism with static member 38584 28:38:39,600 --> 28:38:43,600 variables and i really want you to be 38585 28:38:41,512 --> 28:38:44,872 aware of this now that you know this 38586 28:38:43,600 --> 28:38:46,720 we're going to head over to visual 38587 28:38:44,872 --> 28:38:49,600 studio code and play with us a little 38588 28:38:46,720 --> 28:38:52,080 more here we are in our working folder 38589 28:38:49,600 --> 28:38:54,720 the current project is inheritance and 38590 28:38:52,080 --> 28:38:57,191 polymorphism with static members we're 38591 28:38:54,720 --> 28:39:00,160 going to grab our template files and put 38592 28:38:57,191 --> 28:39:03,279 them in place and i am going to grab our 38593 28:39:00,160 --> 28:39:05,512 classes from my drive and use this as 38594 28:39:03,279 --> 28:39:08,080 the starting point again if you don't 38595 28:39:05,512 --> 28:39:10,320 want to type or create these classes you 38596 28:39:08,080 --> 28:39:12,400 can use the files provided in the 38597 28:39:10,320 --> 28:39:14,320 resource section of this lecture you can 38598 28:39:12,399 --> 28:39:16,231 download them and use them directly 38599 28:39:14,320 --> 28:39:18,640 without having to create them because we 38600 28:39:16,232 --> 28:39:22,000 are really interested in how static 38601 28:39:18,639 --> 28:39:24,551 members behave with our inheritance 38602 28:39:22,000 --> 28:39:26,232 hierarchy which happens to be setting up 38603 28:39:24,551 --> 28:39:28,479 polymorphism this is what we want to 38604 28:39:26,232 --> 28:39:30,232 learn about here i am going to open this 38605 28:39:28,479 --> 28:39:32,872 up in visual studio code and we're going 38606 28:39:30,232 --> 28:39:34,800 to take a good look at our classes the 38607 28:39:32,872 --> 28:39:37,120 base class is going to be shape which is 38608 28:39:34,800 --> 28:39:39,832 what we have here it's going to have a 38609 28:39:37,119 --> 28:39:42,959 virtual method which is get count here 38610 28:39:39,831 --> 28:39:44,319 and we have a static member which is of 38611 28:39:44,320 --> 28:39:48,400 i am going to change my parameter here 38612 28:39:48,399 --> 28:39:52,871 because that's what we should do in 38613 28:39:50,000 --> 28:39:55,191 modern c plus plus let's look at the cpp 38614 28:39:52,872 --> 28:39:57,120 file for this and we're going to fix 38615 28:39:55,191 --> 28:39:59,191 this a little bit and we're going to hop 38616 28:39:57,119 --> 28:40:01,191 over back to the header file and look at 38617 28:39:59,191 --> 28:40:03,279 what we have we have a virtual method 38618 28:40:01,191 --> 28:40:05,039 which is going to return this count here 38619 28:40:03,279 --> 28:40:07,279 it is a const method because it's really 38620 28:40:05,039 --> 28:40:09,191 not going to modify it's just going to 38621 28:40:07,279 --> 28:40:10,872 return a copy of the count member 38622 28:40:10,872 --> 28:40:16,160 and this is a static member variable 38623 28:40:12,960 --> 28:40:18,872 notice that it is not in line so we will 38624 28:40:16,160 --> 28:40:21,512 need to define this in the cpp file 38625 28:40:18,872 --> 28:40:24,000 somewhere because this is a declaration 38626 28:40:21,512 --> 28:40:26,160 we need to put in a definition which is 38627 28:40:24,000 --> 28:40:28,639 what we have in our cpp file here 38628 28:40:26,160 --> 28:40:31,192 because we are doing thanks in the older 38629 28:40:28,639 --> 28:40:34,080 c plus plus way we are going to 38630 28:40:31,191 --> 28:40:36,720 increment our account member variable in 38631 28:40:34,080 --> 28:40:39,279 our constructor and this is going to 38632 28:40:36,720 --> 28:40:41,680 achieve the behavior we really want to 38633 28:40:39,279 --> 28:40:43,831 have the m count member variable which 38634 28:40:41,679 --> 28:40:46,479 is static incremented each time we'll 38635 28:40:43,831 --> 28:40:49,191 create a shape object here let's go back 38636 28:40:46,479 --> 28:40:51,759 and look at our destructor and make sure 38637 28:40:49,191 --> 28:40:53,599 that it is virtual because we have 38638 28:40:51,759 --> 28:40:55,911 virtual functions in this class and this 38639 28:40:53,600 --> 28:40:58,000 is the setup we have let's look at 38640 28:40:55,911 --> 28:41:00,479 ellipse and we're going to take out this 38641 28:40:58,000 --> 28:41:03,279 static member variable because 38642 28:41:00,479 --> 28:41:05,831 we don't want to set it up as of now we 38643 28:41:03,279 --> 28:41:07,759 will enable it in a minute let's make 38644 28:41:08,960 --> 28:41:13,760 let's remove this because i don't want 38645 28:41:10,872 --> 28:41:16,000 this and now we can try to create 38646 28:41:13,759 --> 28:41:19,039 objects of our classes and see how these 38647 28:41:16,000 --> 28:41:22,639 guys play out we can also go in ellipse 38648 28:41:19,039 --> 28:41:25,279 and make sure the constructor is taken 38649 28:41:22,639 --> 28:41:28,080 std string views where we are needing 38650 28:41:25,279 --> 28:41:31,360 strings you can do that and hop over to 38651 28:41:28,080 --> 28:41:33,191 the cpp file and do that std string view 38652 28:41:31,360 --> 28:41:35,360 i think this is going to make a visual 38653 28:41:35,360 --> 28:41:40,551 we can hop over to the main cpp file and 38654 28:41:37,831 --> 28:41:42,000 clean it up a little bit and we are 38655 28:41:42,000 --> 28:41:47,191 include ellipse and in our main function 38656 28:41:45,360 --> 28:41:49,279 we're going to create a bunch of objects 38657 28:41:47,191 --> 28:41:51,360 let's close the left sidebar here 38658 28:41:49,279 --> 28:41:53,279 because we don't really need that 38659 28:41:51,360 --> 28:41:54,800 anymore we're going to grab a shape 38660 28:41:53,279 --> 28:41:56,872 object and we're going to print the 38661 28:41:54,800 --> 28:41:59,192 shape count we expect to get a one 38662 28:41:56,872 --> 28:42:01,192 because we just have one shape at this 38663 28:41:59,191 --> 28:42:03,512 moment we're going to create a second 38664 28:42:01,191 --> 28:42:05,279 shape here and if we print the count 38665 28:42:03,512 --> 28:42:07,760 we're going to get two because we have 38666 28:42:05,279 --> 28:42:09,360 two shapes this is what we expect we 38667 28:42:07,759 --> 28:42:11,119 create another shape which is going to 38668 28:42:09,360 --> 28:42:12,800 be shape three here we print the count 38669 28:42:11,119 --> 28:42:14,720 we're going to get a three but the 38670 28:42:12,800 --> 28:42:17,040 problem is going to come when we create 38671 28:42:14,720 --> 28:42:18,720 an ellipse here if we create an ellipse 38672 28:42:17,039 --> 28:42:21,039 and print the count we're going to get a 38673 28:42:18,720 --> 28:42:23,831 four because we have four shapes in our 38674 28:42:21,039 --> 28:42:26,159 c plus plus program but if we print the 38675 28:42:23,831 --> 28:42:28,159 ellipse count here we expect to get a 38676 28:42:26,160 --> 28:42:30,800 one but that's not what we're going to 38677 28:42:28,160 --> 28:42:34,080 get we're going to get a 4 because the m 38678 28:42:30,800 --> 28:42:36,232 count variable in ellipse is inherited 38679 28:42:34,080 --> 28:42:38,400 from our shape class so it is going to 38680 28:42:36,232 --> 28:42:39,832 be the same thing we have in shape here 38681 28:42:38,399 --> 28:42:42,479 because if you go back you're going to 38682 28:42:39,831 --> 28:42:45,191 see that at this point ellipse doesn't 38683 28:42:42,479 --> 28:42:47,512 have a static variable m count of its 38684 28:42:45,191 --> 28:42:50,319 own so it's going to use the one it is 38685 28:42:47,512 --> 28:42:52,639 inheritance from the shape class this is 38686 28:42:50,320 --> 28:42:54,480 the behavior we're going to get here and 38687 28:42:52,639 --> 28:42:56,720 i want you to be aware of this if this 38688 28:42:54,479 --> 28:42:58,959 is what you want this is fine but in 38689 28:42:56,720 --> 28:43:01,512 most cases you will want this to be 38690 28:42:58,960 --> 28:43:04,000 different because you want very clear 38691 28:43:01,512 --> 28:43:05,440 information about how many objects of 38692 28:43:04,000 --> 28:43:08,160 each type you have created in your 38693 28:43:05,440 --> 28:43:10,160 z-plus plus program if it is just shaped 38694 28:43:08,160 --> 28:43:12,480 this is going to be really vague because 38695 28:43:10,160 --> 28:43:14,720 you want to know if it is an ellipse if 38696 28:43:12,479 --> 28:43:16,639 it is a rectangle if it is a triangle 38697 28:43:14,720 --> 28:43:18,160 this is not going to be super clear so 38698 28:43:16,639 --> 28:43:19,759 let's weld and really show you the 38699 28:43:18,160 --> 28:43:22,400 problem here we're going to pass this 38700 28:43:19,759 --> 28:43:24,319 through gcc our favorite compiler we can 38701 28:43:22,399 --> 28:43:26,231 also bring up the left sidebar no 38702 28:43:24,320 --> 28:43:28,720 problem here and we're going to bring up 38703 28:43:26,232 --> 28:43:30,080 a terminal window or a powershell window 38704 28:43:31,679 --> 28:43:36,319 and run rooster you're going to see that 38705 28:43:33,911 --> 28:43:37,119 the shape count is going to be one two 38706 28:43:39,512 --> 28:43:44,872 because we are also incrementing the m 38707 28:43:42,399 --> 28:43:47,279 count variable in our derived class so 38708 28:43:44,872 --> 28:43:50,000 we are incrementing twice if we create 38709 28:43:47,279 --> 28:43:52,720 an ellipse object let's show you this in 38710 28:43:50,000 --> 28:43:55,191 ellipse cpp we are incrementing the m 38711 28:43:52,720 --> 28:43:58,080 count member variable but in the base 38712 28:43:55,191 --> 28:44:00,959 class we are also implementing that in 38713 28:43:58,080 --> 28:44:03,360 the cpp file that's what we have here 38714 28:44:00,960 --> 28:44:06,400 and notice that the derived class is 38715 28:44:06,399 --> 28:44:10,639 through our initializer lust here so 38716 28:44:08,800 --> 28:44:13,360 this is the behavior we have we 38717 28:44:10,639 --> 28:44:15,759 shouldn't be incrementing this because 38718 28:44:13,360 --> 28:44:18,080 ellipse doesn't have its own static 38719 28:44:15,759 --> 28:44:20,799 variable as of now now we're going to 38720 28:44:18,080 --> 28:44:22,479 build and really solve this problem now 38721 28:44:20,800 --> 28:44:24,872 you see the kinds of problems you can 38722 28:44:22,479 --> 28:44:27,119 run into if you have a static variable 38723 28:44:24,872 --> 28:44:29,440 that is named exactly the same way and 38724 28:44:27,119 --> 28:44:31,679 you have an inheritance hierarchy 38725 28:44:29,440 --> 28:44:34,551 really weird problems can pop up and you 38726 28:44:31,679 --> 28:44:36,079 need to be aware of this let's build and 38727 28:44:34,551 --> 28:44:38,479 see that the problem actually 38728 28:44:36,080 --> 28:44:39,759 disappeared okay the bolt is good we can 38729 28:44:39,759 --> 28:44:43,911 and run rooster we're going to see that 38730 28:44:41,440 --> 28:44:46,000 now this is what we expect one two three 38731 28:44:43,911 --> 28:44:48,319 because we have three shapes created up 38732 28:44:46,000 --> 28:44:50,000 to this point here if we create an 38733 28:44:48,320 --> 28:44:51,600 ellipse that's going to be another shape 38734 28:44:50,000 --> 28:44:54,399 if we get the shape count we're going to 38735 28:44:51,600 --> 28:44:57,120 get a 4 but ellipse count is also giving 38736 28:44:54,399 --> 28:44:59,279 us a 4 because ellipse doesn't have its 38737 28:44:57,119 --> 28:45:01,279 own static variable to solve this 38738 28:44:59,279 --> 28:45:03,831 problem we can go to ellipse and set up 38739 28:45:01,279 --> 28:45:06,800 our own member variable at the ellipse 38740 28:45:03,831 --> 28:45:08,399 level so we can enable this and 38741 28:45:06,800 --> 28:45:10,232 uncomment it we're going to go to the 38742 28:45:10,232 --> 28:45:15,279 incrementing to get the correct count 38743 28:45:12,800 --> 28:45:17,192 incremented here i think this is all we 38744 28:45:15,279 --> 28:45:19,831 need to do we are also going to 38745 28:45:17,191 --> 28:45:22,080 initialize the static variable we have 38746 28:45:19,831 --> 28:45:24,959 at the ellipse level this is going to do 38747 28:45:22,080 --> 28:45:27,191 what we want and we can build again now 38748 28:45:24,960 --> 28:45:29,440 we're going to get the behavior we want 38749 28:45:27,191 --> 28:45:30,871 because at this point we are going to 38750 28:45:30,872 --> 28:45:35,360 for the ellipse count and this really 38751 28:45:32,960 --> 28:45:38,232 makes sense because we have one ellipse 38752 28:45:35,360 --> 28:45:40,720 even if we have four shapes in total so 38753 28:45:40,720 --> 28:45:46,160 makes more sense in our c plus plus 38754 28:45:43,911 --> 28:45:48,720 program here we are going to build this 38755 28:45:46,160 --> 28:45:51,040 again and pass this through gcc the 38756 28:45:48,720 --> 28:45:52,960 builder is going to be good we can clear 38757 28:45:51,039 --> 28:45:55,439 and run rooster now we're going to see 38758 28:45:52,960 --> 28:45:57,680 that we have four shapes and one ellipse 38759 28:45:55,440 --> 28:45:59,832 this is what we want now that we have 38760 28:45:57,679 --> 28:46:01,439 this we can really do all kinds of crazy 38761 28:45:59,831 --> 28:46:04,551 things hopefully you can see that the 38762 28:46:01,440 --> 28:46:06,551 static variable is working exactly as we 38763 28:46:04,551 --> 28:46:08,800 want we can print the ellipse count we 38764 28:46:06,551 --> 28:46:11,191 can print the shape count if that's what 38765 28:46:08,800 --> 28:46:12,832 we want in our c plus plus program but 38766 28:46:12,831 --> 28:46:18,319 polymorphism and get the correct get 38767 28:46:15,512 --> 28:46:20,872 count method called in our c plus first 38768 28:46:18,320 --> 28:46:23,120 program for example if we call the 38769 28:46:20,872 --> 28:46:25,440 getcount method on the basepointer that 38770 28:46:23,119 --> 28:46:27,512 is managing a shape we're going to get 38771 28:46:25,440 --> 28:46:29,279 this one called if we have a best 38772 28:46:27,512 --> 28:46:30,960 pointer that is managing an ellipse 38773 28:46:29,279 --> 28:46:33,191 we're going to get to this one called 38774 28:46:30,960 --> 28:46:35,512 this is going to be super cool let's go 38775 28:46:33,191 --> 28:46:37,759 up in our program and set this up we're 38776 28:46:35,512 --> 28:46:40,000 going to do this on the fly let's say 38777 28:46:37,759 --> 28:46:42,720 shape polymorphism and we're going to 38778 28:46:40,000 --> 28:46:45,039 say shift pointer and it is going to be 38779 28:46:42,720 --> 28:46:46,720 an array let's say shapes this is going 38780 28:46:45,039 --> 28:46:48,959 to do and it is going to be just an 38781 28:46:46,720 --> 28:46:51,360 array and we're going to initialize this 38782 28:46:48,960 --> 28:46:54,320 and store in a bunch of objects let's 38783 28:46:51,360 --> 28:46:56,232 put in a closing brace here let's go up 38784 28:46:54,320 --> 28:46:59,192 and see what we have for example we have 38785 28:46:56,232 --> 28:47:00,872 shape one and ellipse one we can use 38786 28:46:59,191 --> 28:47:02,720 these we're going to put in the address 38787 28:47:00,872 --> 28:47:04,639 of shape one and we're going to put in 38788 28:47:02,720 --> 28:47:05,680 the address of ellipse one this is going 38789 28:47:05,679 --> 28:47:09,599 and we can loop through these guys and 38790 28:47:07,512 --> 28:47:12,400 call the get count method so let's do 38791 28:47:09,600 --> 28:47:14,400 this we're going to say for auto s 38792 28:47:12,399 --> 28:47:17,360 and let's do a reference because we 38793 28:47:14,399 --> 28:47:19,511 don't want copies and we're going to say 38794 28:47:17,360 --> 28:47:21,911 in shapes and we're going to call the 38795 28:47:19,512 --> 28:47:24,639 gatecount method we're going to say s 38796 28:47:21,911 --> 28:47:26,479 and say getcount this is going to 38797 28:47:24,639 --> 28:47:29,191 give us the count and we can print this 38798 28:47:26,479 --> 28:47:31,279 out if we want we can say sddc out and 38799 28:47:31,279 --> 28:47:35,679 put a new line character here to 38800 28:47:37,279 --> 28:47:41,440 put a separator here we can copy what we 38801 28:47:39,440 --> 28:47:43,832 just used in our c plus plus program 38802 28:47:41,440 --> 28:47:46,320 here just to make sure this is super 38803 28:47:43,831 --> 28:47:47,911 clear when we print things on the 38804 28:47:46,320 --> 28:47:51,040 terminal here we're going to build our 38805 28:47:47,911 --> 28:47:52,959 program and see if it actually works the 38806 28:47:51,039 --> 28:47:55,439 body is going to be good we can clear 38807 28:47:52,960 --> 28:47:57,600 and run rooster we're going to see that 38808 28:47:55,440 --> 28:47:59,040 the count is going to be 4 for the first 38809 28:47:57,600 --> 28:48:01,760 iteration because we're going to be 38810 28:47:59,039 --> 28:48:04,959 calling the get count method from 38811 28:48:01,759 --> 28:48:06,231 shape because that's our base pointer so 38812 28:48:04,960 --> 28:48:08,400 this is what we're going to get we're 38813 28:48:06,232 --> 28:48:10,720 going to print the m count member 38814 28:48:08,399 --> 28:48:13,279 variable from the base class that's why 38815 28:48:10,720 --> 28:48:16,551 we have a four but on the second 38816 28:48:13,279 --> 28:48:18,551 iteration the object is ellipse and we 38817 28:48:16,551 --> 28:48:20,800 are using polymorphism because we are 38818 28:48:18,551 --> 28:48:23,119 going through a base pointer and this 38819 28:48:20,800 --> 28:48:25,760 base pointer happens to support the 38820 28:48:23,119 --> 28:48:28,551 virtual method get count we will get the 38821 28:48:25,759 --> 28:48:31,679 most specific one called on the current 38822 28:48:28,551 --> 28:48:33,599 object that our base pointer is managing 38823 28:48:31,679 --> 28:48:35,039 here and this happens to be an ellipse 38824 28:48:33,600 --> 28:48:37,832 so we're going to call the ellipse 38825 28:48:35,039 --> 28:48:40,000 version of our virtual function here and 38826 28:48:37,831 --> 28:48:42,551 we will get a one because we really have 38827 28:48:40,000 --> 28:48:44,800 one ellipse in our c plus plus program 38828 28:48:42,551 --> 28:48:47,039 this is what we see here this is really 38829 28:48:44,800 --> 28:48:48,400 all i wanted you to see in this lecture 38830 28:48:48,399 --> 28:48:53,191 static member variables with inheritance 38831 28:48:50,872 --> 28:48:55,279 and polymorphism and you need to be 38832 28:48:53,191 --> 28:48:57,831 aware of these little things that can 38833 28:48:55,279 --> 28:48:59,911 really be confusing especially for 38834 28:48:57,831 --> 28:49:01,679 beginners we are going to stop here in 38835 28:48:59,911 --> 28:49:04,639 this lecture the next one we're going to 38836 28:49:01,679 --> 28:49:07,831 learn about the final specification we 38837 28:49:04,639 --> 28:49:09,911 can use in our inheritance hierarchy go 38838 28:49:07,831 --> 28:49:11,119 ahead and finish up here and meet me 38839 28:49:11,119 --> 28:49:14,959 go ahead and finish up here and meet me 38840 28:49:13,279 --> 28:49:18,959 there in this lecture we're going to 38841 28:49:14,960 --> 28:49:21,512 learn about the final specifier used in 38842 28:49:18,960 --> 28:49:24,480 inheritance hierarchies this is going to 38843 28:49:21,512 --> 28:49:26,800 allow you to do one of two things you 38844 28:49:24,479 --> 28:49:30,000 will either be able to restrict how you 38845 28:49:26,800 --> 28:49:32,720 override methods in the derived classes 38846 28:49:30,000 --> 28:49:35,512 or you can restrict how you can derive 38847 28:49:32,720 --> 28:49:37,040 from base classes i know this is in the 38848 28:49:35,512 --> 28:49:39,040 air but we're going to look at a few 38849 28:49:37,039 --> 28:49:41,511 examples to really drive this home 38850 28:49:39,039 --> 28:49:43,439 suppose we have an inheritance hierarchy 38851 28:49:41,512 --> 28:49:45,760 like we see here the top class is animal 38852 28:49:43,440 --> 28:49:47,760 we can derive from this and have feline 38853 28:49:45,759 --> 28:49:50,080 you can have bird as a derived class 38854 28:49:47,759 --> 28:49:52,720 feline can be inherited to have a dog 38855 28:49:50,080 --> 28:49:55,119 and cat and you can derive from cat for 38856 28:49:52,720 --> 28:49:57,191 example to have wild cat and bulldog 38857 28:49:55,119 --> 28:49:59,512 from dog you can do all these things 38858 28:49:57,191 --> 28:50:01,512 here and this is inheritance hierarchy 38859 28:49:59,512 --> 28:50:04,080 we're going to be using to learn about 38860 28:50:01,512 --> 28:50:06,000 these things now here is a possible 38861 28:50:04,080 --> 28:50:08,320 implementation for the dog class which 38862 28:50:06,000 --> 28:50:11,039 is going to be inherited from feline 38863 28:50:08,320 --> 28:50:13,680 publicly as we see here but take a look 38864 28:50:11,039 --> 28:50:17,360 at the run method we are setting up in 38865 28:50:13,679 --> 28:50:20,319 dag we are appending a final specifier 38866 28:50:17,360 --> 28:50:23,831 and what this is going to mean is that 38867 28:50:20,320 --> 28:50:26,080 no downstream inheritance class will be 38868 28:50:23,831 --> 28:50:28,231 able to override the random method 38869 28:50:26,080 --> 28:50:30,960 further this is what we can achieve so 38870 28:50:28,232 --> 28:50:32,720 if we don't want our derived classes to 38871 28:50:32,720 --> 28:50:37,512 virtual method we can mark that method 38872 28:50:35,512 --> 28:50:40,000 as final in our class and we are 38873 28:50:37,512 --> 28:50:42,720 basically saying i am going to put in my 38874 28:50:40,000 --> 28:50:46,399 implementation but i don't want any 38875 28:50:42,720 --> 28:50:48,720 deriving downstream class to override 38876 28:50:46,399 --> 28:50:50,399 this method further if they do that they 38877 28:50:48,720 --> 28:50:52,720 are going to get a compiler error and 38878 28:50:50,399 --> 28:50:55,759 this is something we can achieve using 38879 28:50:52,720 --> 28:50:57,911 the final specifier in our inheritance 38880 28:50:55,759 --> 28:51:00,479 hierarchy again in this case we are 38881 28:51:00,479 --> 28:51:06,959 how people can override our own virtual 38882 28:51:04,000 --> 28:51:08,800 method in downstream classes or i 38883 28:51:06,960 --> 28:51:11,440 shouldn't really say our own method 38884 28:51:08,800 --> 28:51:13,832 because it may be coming from upstream 38885 28:51:11,440 --> 28:51:16,551 but you are restricting people from 38886 28:51:13,831 --> 28:51:18,871 overriding that method further in 38887 28:51:16,551 --> 28:51:21,512 downstream classes here okay so this is 38888 28:51:18,872 --> 28:51:24,232 the first use we have for the final 38889 28:51:21,512 --> 28:51:27,760 specifier but we can also restrict 38890 28:51:24,232 --> 28:51:29,600 people from inheriting altogether so 38891 28:51:27,759 --> 28:51:32,159 here we have a simple example to drive 38892 28:51:29,600 --> 28:51:34,552 this home we have our cat class it is 38893 28:51:32,160 --> 28:51:37,760 going to be inheriting from feline but 38894 28:51:34,551 --> 28:51:40,080 we are marking our cat class as final 38895 28:51:37,759 --> 28:51:43,360 and what this is going to mean if 38896 28:51:40,080 --> 28:51:45,831 anybody tries to inherit or derive from 38897 28:51:43,360 --> 28:51:47,512 cat in any way they are going to get a 38898 28:51:45,831 --> 28:51:50,720 compiler error and this is what we can 38899 28:51:47,512 --> 28:51:52,479 achieve with the final specifier again 38900 28:51:50,720 --> 28:51:55,279 there are two things you can restrict 38901 28:51:52,479 --> 28:51:58,000 how people override your virtual methods 38902 28:51:55,279 --> 28:52:00,800 if you want to restrict that from the 38903 28:51:58,000 --> 28:52:03,279 current class you can mark your virtual 38904 28:52:00,800 --> 28:52:05,600 method as final you can also restrict 38905 28:52:03,279 --> 28:52:07,831 how people inherit all together from 38906 28:52:05,600 --> 28:52:11,040 your class and if you want that behavior 38907 28:52:07,831 --> 28:52:13,119 you can mark your class as final using 38908 28:52:11,039 --> 28:52:15,439 the syntax we just saw in the slides 38909 28:52:13,119 --> 28:52:17,679 here okay now that you know these things 38910 28:52:15,440 --> 28:52:20,160 let's look at a few things that may be 38911 28:52:17,679 --> 28:52:22,159 interesting for example here is a simple 38912 28:52:20,160 --> 28:52:25,440 fact that you can create a class right 38913 28:52:22,160 --> 28:52:28,480 away in market as final and when you do 38914 28:52:25,440 --> 28:52:30,800 this nobody will be able to ever inherit 38915 28:52:28,479 --> 28:52:33,119 from this class in your program and if 38916 28:52:30,800 --> 28:52:35,600 we do this we will get a compiler error 38917 28:52:33,119 --> 28:52:37,911 for example this is one thing i find 38918 28:52:35,600 --> 28:52:40,000 interesting and i really wanted you to 38919 28:52:37,911 --> 28:52:42,399 know this another thing is that it is 38920 28:52:40,000 --> 28:52:45,360 possible to introduce useless virtual 38921 28:52:42,399 --> 28:52:48,079 methods in your inheritance hierarchy 38922 28:52:45,360 --> 28:52:50,960 for example if our cat class is marked 38923 28:52:48,080 --> 28:52:53,512 as final what this means is that nobody 38924 28:52:50,960 --> 28:52:55,600 will ever be able to inherit from this 38925 28:52:53,512 --> 28:52:57,680 class but we are going further in 38926 28:52:55,600 --> 28:53:00,400 setting up a virtual method which is 38927 28:52:57,679 --> 28:53:02,719 meant to be inherited or overwritten in 38928 28:53:00,399 --> 28:53:05,039 downstream classes but how do we get a 38929 28:53:02,720 --> 28:53:07,119 downstream class if we can't inherit 38930 28:53:05,039 --> 28:53:10,319 from the cat class which is marked final 38931 28:53:07,119 --> 28:53:12,319 so so we have conflicting ideas here on 38932 28:53:10,320 --> 28:53:15,192 one hand we are saying we can never 38933 28:53:12,320 --> 28:53:17,280 inherit from this grant class but on the 38934 28:53:15,191 --> 28:53:20,231 other hand we are setting up a method 38935 28:53:17,279 --> 28:53:22,639 that is meant to be used or specialized 38936 28:53:20,232 --> 28:53:24,720 in derived classes so this is something 38937 28:53:22,639 --> 28:53:27,759 that is interesting this code is going 38938 28:53:24,720 --> 28:53:29,680 to compile and i think maybe in future 38939 28:53:27,759 --> 28:53:31,911 standards of c plus plus they are going 38940 28:53:29,679 --> 28:53:33,511 to fix this problem and maybe give you a 38941 28:53:31,911 --> 28:53:35,440 compiler error if you try to do 38942 28:53:33,512 --> 28:53:37,279 something like this but at this point 38943 28:53:35,440 --> 28:53:39,040 you're not going to get a compiler error 38944 28:53:37,279 --> 28:53:41,679 this is just going to be valid c plus 38945 28:53:39,039 --> 28:53:44,319 plus syntax even if this is really weird 38946 28:53:41,679 --> 28:53:47,039 we have conflicting ideas here okay one 38947 28:53:44,320 --> 28:53:49,192 thing you should know though is that we 38948 28:53:47,039 --> 28:53:50,719 can override in a final class and this 38949 28:53:49,191 --> 28:53:53,440 is going to make sense because for 38950 28:53:50,720 --> 28:53:55,191 example the run method is coming from 38951 28:53:55,191 --> 28:54:00,479 and here we are just specializing that 38952 28:53:57,759 --> 28:54:03,039 so we are marking that as an override 38953 28:54:00,479 --> 28:54:05,512 here even if this is the final class so 38954 28:54:03,039 --> 28:54:07,679 be careful this is valid c plus plus 38955 28:54:05,512 --> 28:54:09,512 syntax this is going to work just fine 38956 28:54:07,679 --> 28:54:11,911 another thing you can do which is pretty 38957 28:54:09,512 --> 28:54:14,720 useless in your c plus plus program is 38958 28:54:11,911 --> 28:54:18,872 to introduce a virtual method and mark 38959 28:54:14,720 --> 28:54:21,911 it as final right away so again this is 38960 28:54:18,872 --> 28:54:24,551 really weird we have two ideas that are 38961 28:54:21,911 --> 28:54:27,039 conflicting the virtual keyword means 38962 28:54:24,551 --> 28:54:29,679 that we want this to be visualized in 38963 28:54:27,039 --> 28:54:32,399 the downstream classes but again we are 38964 28:54:29,679 --> 28:54:34,639 saying this method is final so no 38965 28:54:32,399 --> 28:54:36,079 downstream class will override this if 38966 28:54:34,639 --> 28:54:38,319 they try to do that they are going to 38967 28:54:36,080 --> 28:54:40,960 get a compiler error so this is again a 38968 28:54:38,320 --> 28:54:42,640 bunch of conflicting ideas c plus plus 38969 28:54:40,960 --> 28:54:44,552 is going to allow this syntax this is 38970 28:54:42,639 --> 28:54:46,720 not going to give you a compiler error 38971 28:54:44,551 --> 28:54:48,720 but this is weird and you need to be 38972 28:54:46,720 --> 28:54:50,479 aware of this okay now that you know 38973 28:54:48,720 --> 28:54:53,191 these ideas we're going to head over to 38974 28:54:50,479 --> 28:54:55,831 visual studio code and play with the 38975 28:54:53,191 --> 28:54:57,911 final specifier in our inheritance 38976 28:54:55,831 --> 28:55:00,319 hierarchy we will be setting up this 38977 28:54:57,911 --> 28:55:02,399 kind of inheritance hierarchy and we 38978 28:55:00,320 --> 28:55:04,960 will set up a bunch of virtual methods 38979 28:55:02,399 --> 28:55:07,511 and mark them as final i think we will 38980 28:55:04,960 --> 28:55:10,160 do that in the dog class and we will 38981 28:55:07,512 --> 28:55:12,551 mark a bunch of classes as final and see 38982 28:55:10,160 --> 28:55:14,720 that if we inherit from them we're going 38983 28:55:12,551 --> 28:55:17,759 to get a bunch of compiler errors so 38984 28:55:14,720 --> 28:55:21,119 let's do this here we are in our working 38985 28:55:17,759 --> 28:55:23,360 folder the current project is final and 38986 28:55:21,119 --> 28:55:24,871 we are going to bring in our template 38987 28:55:23,360 --> 28:55:27,440 files pretty quick we're going to use 38988 28:55:24,872 --> 28:55:29,600 this as our starting point we're going 38989 28:55:29,600 --> 28:55:34,000 and i am going to put in a bunch of 38990 28:55:31,600 --> 28:55:36,400 classes i have lying around on my system 38991 28:55:34,000 --> 28:55:38,320 because i don't want to set up these 38992 28:55:36,399 --> 28:55:40,799 classes that would be a waste of your 38993 28:55:38,320 --> 28:55:43,280 time just know that we are trying to set 38994 28:55:40,800 --> 28:55:45,760 up this kind of inheritance hierarchy 38995 28:55:43,279 --> 28:55:47,600 animal is going to be our top dog or 38996 28:55:45,759 --> 28:55:50,231 animal is going to be our top class and 38997 28:55:47,600 --> 28:55:52,480 we will be inheriting from this creating 38998 28:55:50,232 --> 28:55:55,040 an inheritance hierarchy we can play 38999 28:55:52,479 --> 28:55:57,279 with in our code here okay so we are 39000 28:55:55,039 --> 28:55:59,191 going to open this up in visual studio 39001 28:55:57,279 --> 28:56:01,600 code so let's do that by dragging and 39002 28:55:59,191 --> 28:56:04,080 dropping here this is going to give us 39003 28:56:01,600 --> 28:56:06,400 all these crazy classes we have 39004 28:56:04,080 --> 28:56:08,400 the top class is going to be animal and 39005 28:56:06,399 --> 28:56:10,159 it is going to be defining the features 39006 28:56:08,399 --> 28:56:12,231 any kind of animal we support you know c 39007 28:56:10,160 --> 28:56:14,720 plus plus program is going to have for 39008 28:56:12,232 --> 28:56:17,832 example any kind of animal will need to 39009 28:56:14,720 --> 28:56:19,191 breathe and we will set up this virtual 39010 28:56:19,191 --> 28:56:24,000 and we will be inheriting from this 39011 28:56:21,279 --> 28:56:26,232 animal class to create a feline class 39012 28:56:24,000 --> 28:56:28,399 and a bird class this is something we 39013 28:56:26,232 --> 28:56:30,160 did in a few previous lectures and we 39014 28:56:28,399 --> 28:56:32,159 are just reusing this inheritance 39015 28:56:30,160 --> 28:56:34,000 hierarchy here let's look at the feline 39016 28:56:32,160 --> 28:56:36,720 class the filament class is going to set 39017 28:56:34,000 --> 28:56:39,039 up a virtual method which is called run 39018 28:56:36,720 --> 28:56:41,119 and the method is going to be overridden 39019 28:56:41,119 --> 28:56:47,360 we can override the run method but 39020 28:56:43,759 --> 28:56:50,231 notice that we are marking that as final 39021 28:56:47,360 --> 28:56:52,479 okay so look at this the method is final 39022 28:56:50,232 --> 28:56:55,360 and what this means is that 39023 28:56:52,479 --> 28:56:58,551 no downstream classes inheriting from 39024 28:56:55,360 --> 28:57:01,279 doug will ever be able to specialize or 39025 28:56:58,551 --> 28:57:03,279 override this method further if they try 39026 28:57:01,279 --> 28:57:05,911 to do that they will get a compiler 39027 28:57:03,279 --> 28:57:08,000 error notice that this may be our method 39028 28:57:05,911 --> 28:57:10,479 or it may be a method coming from 39029 28:57:08,000 --> 28:57:13,039 upstream classes in this case the run 39030 28:57:10,479 --> 28:57:16,231 method was introduced at the feline 39031 28:57:13,039 --> 28:57:18,959 level you see it was introduced here and 39032 28:57:16,232 --> 28:57:21,040 doug happens to be overriding this 39033 28:57:18,960 --> 28:57:23,280 method here that's why we have the 39034 28:57:23,279 --> 28:57:26,639 at the same time we are going to mark it 39035 28:57:29,191 --> 28:57:35,360 downstream classes from overriding this 39036 28:57:31,911 --> 28:57:37,911 method further if we look at our bulldog 39037 28:57:35,360 --> 28:57:42,160 class we're going to find the class to 39038 28:57:37,911 --> 28:57:44,479 inherit from dark we have our run method 39039 28:57:42,160 --> 28:57:46,400 here it is commented out at this moment 39040 28:57:44,479 --> 28:57:48,872 i just want you to see that our code is 39041 28:57:46,399 --> 28:57:50,000 just going to compile if we compile it 39042 28:57:50,000 --> 28:57:54,000 let's do this we're going to pass this 39043 28:57:51,512 --> 28:57:56,232 through gcc okay you see the bolt is 39044 28:57:54,000 --> 28:57:57,759 good and we have generated our binary 39045 28:57:56,232 --> 28:58:00,232 which is really not doing anything 39046 28:57:57,759 --> 28:58:02,639 because our main function is just some 39047 28:58:00,232 --> 28:58:05,040 dummy code we have here to make sure it 39048 28:58:02,639 --> 28:58:06,399 works with c plus plus 20 but what i 39049 28:58:06,399 --> 28:58:12,639 is that if you go in bulldog let's go in 39050 28:58:09,440 --> 28:58:15,512 our bulldog class and uncomment 39051 28:58:12,639 --> 28:58:16,639 this class here or this virtual method 39052 28:58:15,512 --> 28:58:18,320 we're going to see that we're going to 39053 28:58:16,639 --> 28:58:21,119 get a compiler error we have a squiggly 39054 28:58:18,320 --> 28:58:23,760 line meaning that we did something wrong 39055 28:58:21,119 --> 28:58:26,159 and well let's look at the problems tab 39056 28:58:23,759 --> 28:58:29,119 from visual studio code here cannot 39057 28:58:26,160 --> 28:58:31,120 override find a function dog run this is 39058 28:58:31,119 --> 28:58:36,080 the dog class if we try to override it 39059 28:58:33,831 --> 28:58:38,399 further we will get a compiler error 39060 28:58:36,080 --> 28:58:41,911 this is something we can achieve by 39061 28:58:38,399 --> 28:58:44,551 marking our virtual method as final as 39062 28:58:41,911 --> 28:58:46,000 we are doing here now if we try to build 39063 28:58:44,551 --> 28:58:48,231 this program we're going to get a 39064 28:58:46,000 --> 28:58:51,600 compiler error let's pass this through 39065 28:58:48,232 --> 28:58:54,872 gcc and let you see this okay you see we 39066 28:58:51,600 --> 28:58:57,912 get a compiler error and uh the error is 39067 28:58:54,872 --> 28:59:02,479 that we are overriding let's go up and 39068 28:58:57,911 --> 28:59:05,279 see the error here error and on line 12 39069 28:59:02,479 --> 28:59:08,319 in bulldog h we see that we are 39070 28:59:05,279 --> 28:59:11,600 overriding a final method virtual 39071 28:59:08,320 --> 28:59:14,000 void bulldog is a final function and we 39072 28:59:11,600 --> 28:59:15,680 are overriding this and this is going to 39073 28:59:14,000 --> 28:59:17,679 give us a compiler error this is the 39074 28:59:15,679 --> 28:59:19,911 behavior we get so the message here is 39075 28:59:17,679 --> 28:59:22,319 that you can go in your inheritance 39076 28:59:19,911 --> 28:59:25,360 hierarchy at any level and mark a 39077 28:59:22,320 --> 28:59:27,440 virtual method as final and prevent 39078 28:59:25,360 --> 28:59:30,232 download stream classes from 39079 28:59:27,440 --> 28:59:32,400 specializing or overriding that method 39080 28:59:30,232 --> 28:59:34,480 further and if they do that they will 39081 28:59:32,399 --> 28:59:36,079 get a compiler error this is something 39082 28:59:34,479 --> 28:59:38,080 we can achieve in our c plus plus 39083 28:59:36,080 --> 28:59:40,720 program another thing you can do with 39084 28:59:38,080 --> 28:59:43,680 the final specifier is that you can 39085 28:59:40,720 --> 28:59:47,119 prevent people from inheriting from your 39086 28:59:43,679 --> 28:59:49,039 class altogether let's go to our cat 39087 28:59:47,119 --> 28:59:52,000 class i think that's the example that 39088 28:59:49,039 --> 28:59:54,551 sets that up we have a cat class it is 39089 28:59:52,000 --> 28:59:57,279 going to be inheriting from feline but 39090 28:59:54,551 --> 28:59:59,512 the class is marked as final 39091 28:59:57,279 --> 29:00:01,600 now what this is going to do 39092 28:59:59,512 --> 29:00:04,160 it's going to throw a compiler error 39093 29:00:01,600 --> 29:00:06,232 anytime somebody tries to derive or 39094 29:00:04,160 --> 29:00:09,040 inherit from this class here so if 39095 29:00:06,232 --> 29:00:11,832 somebody tries to inherit from cat as we 39096 29:00:09,039 --> 29:00:13,679 are doing here create a wildcat from a 39097 29:00:11,831 --> 29:00:16,159 cat we are going to get a compiler error 39098 29:00:13,679 --> 29:00:18,639 because we can't inherit from cat 39099 29:00:16,160 --> 29:00:22,160 altogether it is a final class at this 39100 29:00:18,639 --> 29:00:24,639 moment okay so let's look at our wildcat 39101 29:00:25,831 --> 29:00:31,439 derive from cat we can do something like 39102 29:00:29,191 --> 29:00:33,279 this we can remove our comments here and 39103 29:00:31,440 --> 29:00:35,191 if we try to do this we will get a 39104 29:00:33,279 --> 29:00:37,440 compiler arrow let's look at the cpp 39105 29:00:35,191 --> 29:00:39,911 file and see what we have it has nothing 39106 29:00:37,440 --> 29:00:42,800 special but if we try to do that and 39107 29:00:39,911 --> 29:00:45,360 inherit from cat we will get a compiler 39108 29:00:42,800 --> 29:00:47,680 error because the cat class is final and 39109 29:00:45,360 --> 29:00:49,440 you cannot derive from it if you do that 39110 29:00:47,679 --> 29:00:52,000 you will get a compiler error because 39111 29:00:49,440 --> 29:00:54,320 the cat class is final this is what i 39112 29:00:52,000 --> 29:00:55,831 want you to see here let's try and build 39113 29:00:54,320 --> 29:00:58,320 our program again we're going to pass 39114 29:00:55,831 --> 29:01:00,551 this through gcc and we will see our 39115 29:00:58,320 --> 29:01:03,120 compiler error okay the world is 39116 29:01:00,551 --> 29:01:06,399 finished with errors if we go up we will 39117 29:01:03,119 --> 29:01:08,639 see that we cannot derive from a final 39118 29:01:06,399 --> 29:01:10,551 base class if we do that we will get a 39119 29:01:08,639 --> 29:01:12,551 compiler error and this is what i want 39120 29:01:10,551 --> 29:01:14,720 you to see here okay now that we notice 39121 29:01:12,551 --> 29:01:17,279 we're going to take this back 39122 29:01:14,720 --> 29:01:19,360 and take out the compiler error here 39123 29:01:17,279 --> 29:01:21,512 because we want our code to compile 39124 29:01:19,360 --> 29:01:23,831 we're going to make sure it is compiling 39125 29:01:21,512 --> 29:01:25,832 fine okay now the code is compiling 39126 29:01:23,831 --> 29:01:29,279 because we are no longer trying to 39127 29:01:25,831 --> 29:01:32,319 inherit from cat and i want you to now 39128 29:01:29,279 --> 29:01:35,119 see a few interesting facts about the 39129 29:01:32,320 --> 29:01:37,512 final specifier when it works together 39130 29:01:35,119 --> 29:01:40,000 with virtual functions let's go to the 39131 29:01:37,512 --> 29:01:42,232 main cpp file and show you the first 39132 29:01:40,000 --> 29:01:45,360 fact and that's that you can create a 39133 29:01:42,232 --> 29:01:48,160 class right away and mark that as vinyl 39134 29:01:45,360 --> 29:01:50,320 as you create it as we do here if you do 39135 29:01:48,160 --> 29:01:52,800 that this class is going to not be 39136 29:01:50,320 --> 29:01:54,640 inheritable and if you try to inherit 39137 29:01:52,800 --> 29:01:56,872 from it you will get a compiler error 39138 29:01:54,639 --> 29:01:59,279 let's take out everything we have here 39139 29:01:56,872 --> 29:02:00,960 and just say hello in our main function 39140 29:01:59,279 --> 29:02:02,551 this is going to do just to have 39141 29:02:00,960 --> 29:02:04,720 something this is going to give us a 39142 29:02:02,551 --> 29:02:07,119 compiler error because we can't inherit 39143 29:02:04,720 --> 29:02:09,831 from a final class let's build and show 39144 29:02:07,119 --> 29:02:12,159 you the compiler error this is going to 39145 29:02:09,831 --> 29:02:14,959 go through gcc and the world is going to 39146 29:02:12,160 --> 29:02:18,160 finish with errors and that we have our 39147 29:02:14,960 --> 29:02:21,280 message here and we don't have the error 39148 29:02:18,160 --> 29:02:23,040 we expected because we forgot to put our 39149 29:02:21,279 --> 29:02:25,360 closing semicolon on our class 39150 29:02:23,039 --> 29:02:28,000 definition here but we can build again 39151 29:02:25,360 --> 29:02:29,911 and get our error okay the world is 39152 29:02:29,911 --> 29:02:34,319 and the error is going to be that we 39153 29:02:32,080 --> 29:02:36,551 cannot derive from a final class this is 39154 29:02:34,320 --> 29:02:38,800 what we expect here let's comment this 39155 29:02:36,551 --> 29:02:41,599 out because i just wanted you to see 39156 29:02:38,800 --> 29:02:44,000 this fact another interesting thing c 39157 29:02:44,000 --> 29:02:50,080 is to set up a new virtual method in a 39158 29:02:47,759 --> 29:02:52,720 final class let's first build and make 39159 29:02:50,080 --> 29:02:54,720 sure the compiler error here goes away 39160 29:02:52,720 --> 29:02:57,119 and i am going to show you that okay the 39161 29:02:54,720 --> 29:02:59,360 build is good let's go back in our final 39162 29:02:57,119 --> 29:03:02,159 class which is the cat class 39163 29:02:59,360 --> 29:03:04,800 if i remember correctly this is a final 39164 29:03:02,160 --> 29:03:06,960 class we can't derive from this class 39165 29:03:04,800 --> 29:03:07,911 but we can set up a virtual method for 39166 29:03:07,911 --> 29:03:12,000 the virtual method here called meow is 39167 29:03:12,000 --> 29:03:16,479 why is it useless it is a virtual and 39168 29:03:14,479 --> 29:03:19,039 marking it virtual means that 39169 29:03:16,479 --> 29:03:21,512 you at least want people inheriting from 39170 29:03:19,039 --> 29:03:23,360 this class to inherit it and use it or 39171 29:03:21,512 --> 29:03:25,512 even better you want them to have the 39172 29:03:25,512 --> 29:03:30,720 or override this method and make it to 39173 29:03:27,911 --> 29:03:32,720 do more specialized things but you can't 39174 29:03:30,720 --> 29:03:35,040 really do that because you can't derive 39175 29:03:32,720 --> 29:03:37,279 from this class all together so this is 39176 29:03:35,039 --> 29:03:39,599 something that c plus plus allows us to 39177 29:03:37,279 --> 29:03:42,319 do but this is a useless method which 39178 29:03:39,600 --> 29:03:43,912 will never be used in any way so this is 39179 29:03:42,320 --> 29:03:45,760 something i want you to be aware of but 39180 29:03:43,911 --> 29:03:47,831 this is going to compile again let's 39181 29:03:45,759 --> 29:03:49,439 pass this through gcc you're going to 39182 29:03:47,831 --> 29:03:51,279 see that this is going to compile and 39183 29:03:49,440 --> 29:03:53,440 work even if this method is really 39184 29:03:51,279 --> 29:03:55,360 useless i want you to be aware of this 39185 29:03:53,440 --> 29:03:58,800 okay so the build is good here okay so 39186 29:03:55,360 --> 29:04:01,831 this is our interesting fact number two 39187 29:03:58,800 --> 29:04:05,360 okay even if this method is useless 39188 29:04:01,831 --> 29:04:07,831 it is possible to specialize a method 39189 29:04:05,360 --> 29:04:10,639 coming from upstream classes from your 39190 29:04:07,831 --> 29:04:11,759 final class for example in feline we 39191 29:04:11,759 --> 29:04:17,360 a method called run which was introduced 39192 29:04:14,720 --> 29:04:19,759 there i think we can override this 39193 29:04:17,360 --> 29:04:21,831 method in our final class which is the 39194 29:04:19,759 --> 29:04:24,039 cat class here so this method is going 39195 29:04:21,831 --> 29:04:26,479 to be useless because it will be 39196 29:04:24,039 --> 29:04:28,871 specializing what we get from our 39197 29:04:26,479 --> 29:04:31,279 upstream class this is going to work 39198 29:04:28,872 --> 29:04:33,440 okay so since we are looking at a few 39199 29:04:31,279 --> 29:04:35,191 useless things you can do with final 39200 29:04:35,191 --> 29:04:40,399 virtual mechanisms in c plus plus i want 39201 29:04:38,232 --> 29:04:44,080 you to see that it is possible to set up 39202 29:04:40,399 --> 29:04:46,231 a virtual method and mark it as a final 39203 29:04:44,080 --> 29:04:48,160 in the same declaration and we're going 39204 29:04:46,232 --> 29:04:49,911 to go to the bird class and really show 39205 29:04:49,911 --> 29:04:55,831 we have a virtual method which is called 39206 29:04:52,551 --> 29:04:57,512 fly but this is marked final the moment 39207 29:04:55,831 --> 29:04:59,831 we declare it and this is going to be 39208 29:04:57,512 --> 29:05:02,400 contradicted for example the virtual 39209 29:04:59,831 --> 29:05:04,399 keyword means that people downstream 39210 29:05:02,399 --> 29:05:07,511 will be able to specialize this or 39211 29:05:04,399 --> 29:05:09,679 inherit this but at the same time we are 39212 29:05:07,512 --> 29:05:12,720 marking this guy as final so we are 39213 29:05:09,679 --> 29:05:14,871 saying nobody downstream will be able to 39214 29:05:12,720 --> 29:05:17,600 override this method if they do that 39215 29:05:14,872 --> 29:05:19,832 they will get a compiler error now which 39216 29:05:17,600 --> 29:05:21,832 one is going to win virtual or final 39217 29:05:19,831 --> 29:05:24,159 final is going to win because if you try 39218 29:05:21,831 --> 29:05:26,399 to override this method in downstream 39219 29:05:24,160 --> 29:05:29,440 classes you will get a compiler error 39220 29:05:26,399 --> 29:05:31,911 let's go down and look at one bird we 39221 29:05:29,440 --> 29:05:34,000 might have in our program for example we 39222 29:05:31,911 --> 29:05:35,679 might have a pigeon let's grab this 39223 29:05:34,000 --> 29:05:37,119 method and try to override it you're 39224 29:05:35,679 --> 29:05:39,439 going to see that we get a compiler 39225 29:05:37,119 --> 29:05:41,911 error we're going to go in pidgin and 39226 29:05:39,440 --> 29:05:44,160 put in the fly method and uh for example 39227 29:05:41,911 --> 29:05:46,080 we're going to say pigeon fly here and 39228 29:05:44,160 --> 29:05:48,080 we're going to say override we are 39229 29:05:46,080 --> 29:05:49,680 trying to override this method but we're 39230 29:05:48,080 --> 29:05:51,512 going to get a compiler error you see we 39231 29:05:49,679 --> 29:05:53,911 have a squiggly line because this is a 39232 29:05:51,512 --> 29:05:57,119 final method and we are trying to 39233 29:05:53,911 --> 29:06:00,080 override this in a downstream class 39234 29:05:57,119 --> 29:06:03,360 and this class is a downstream class 39235 29:06:00,080 --> 29:06:05,440 from bird and in bird we map this method 39236 29:06:03,360 --> 29:06:07,440 as final so this is not going to work 39237 29:06:05,440 --> 29:06:10,232 even if this is virtual you want to be 39238 29:06:07,440 --> 29:06:12,551 able to really use the virtual mechanism 39239 29:06:10,232 --> 29:06:15,192 because this is final if we try to 39240 29:06:12,551 --> 29:06:17,360 inherit from this if we try to override 39241 29:06:15,191 --> 29:06:19,512 this like we are doing in pigeon here we 39242 29:06:17,360 --> 29:06:21,600 will get the compiler error let's try 39243 29:06:19,512 --> 29:06:24,160 and build our program and we will see 39244 29:06:21,600 --> 29:06:25,760 that we get our trusty compiler error 39245 29:06:24,160 --> 29:06:28,400 the bullet is going to finish with 39246 29:06:25,759 --> 29:06:30,959 errors and if we go up we're going to 39247 29:06:28,399 --> 29:06:33,911 find our error we're going to say 39248 29:06:30,960 --> 29:06:36,800 virtual function fly overriding final 39249 29:06:33,911 --> 29:06:39,039 function we are overriding a final 39250 29:06:36,800 --> 29:06:40,960 virtual method and this is going to give 39251 29:06:39,039 --> 29:06:42,799 us a compiler error we can't do this 39252 29:06:40,960 --> 29:06:44,800 this is something you can do in your c 39253 29:06:42,800 --> 29:06:47,512 plus plus program so this is really 39254 29:06:44,800 --> 29:06:49,512 confusing because in bird the virtual 39255 29:06:47,512 --> 29:06:52,000 keyword suggests that you can really 39256 29:06:49,512 --> 29:06:54,320 specialize this in downstream class but 39257 29:06:52,000 --> 29:06:55,911 this is contradictant and c plus plus 39258 29:06:54,320 --> 29:06:58,000 allows you to do this you need to be 39259 29:06:55,911 --> 29:07:00,080 aware of this again if we don't override 39260 29:06:58,000 --> 29:07:02,960 this and leave this in the code is going 39261 29:07:00,080 --> 29:07:05,119 to compile but in my opinion this is 39262 29:07:02,960 --> 29:07:06,720 really confusing and you need to be 39263 29:07:05,119 --> 29:07:08,399 aware of this that's the whole point of 39264 29:07:06,720 --> 29:07:10,800 this lecture here let's make sure the 39265 29:07:08,399 --> 29:07:13,119 code is building and this is really all 39266 29:07:10,800 --> 29:07:15,360 i had to share in this lecture 39267 29:07:13,119 --> 29:07:17,831 introducing the final keyword to you you 39268 29:07:15,360 --> 29:07:20,872 can use it to restrict how people 39269 29:07:17,831 --> 29:07:22,959 override your virtual function or you 39270 29:07:22,960 --> 29:07:28,080 people from inheriting from your class 39271 29:07:25,440 --> 29:07:30,551 altogether and use these things if they 39272 29:07:28,080 --> 29:07:33,440 make sense for whatever application you 39273 29:07:30,551 --> 29:07:35,279 are designing with c plus plus we are 39274 29:07:33,440 --> 29:07:37,191 going to stop here in this lecture and 39275 29:07:35,279 --> 29:07:39,831 the next one i am going to show you a 39276 29:07:37,191 --> 29:07:42,000 secret you probably didn't know so go 39277 29:07:39,831 --> 29:07:43,831 ahead and finish up here and meet me 39278 29:07:42,000 --> 29:07:46,320 there in this lecture we're going to 39279 29:07:43,831 --> 29:07:48,959 explore how virtual functions or 39280 29:07:46,320 --> 29:07:51,280 polymorphism plays with default 39281 29:07:48,960 --> 29:07:53,680 arguments and when we say default 39282 29:07:51,279 --> 29:07:55,679 arguments we mean a setup like this we 39283 29:07:53,679 --> 29:07:58,399 have a virtual function here which is 39284 29:07:55,679 --> 29:07:59,759 called add it is going to return double 39285 29:07:59,759 --> 29:08:05,360 default parameters here for a and b and 39286 29:08:02,639 --> 29:08:07,759 the value is five here in our derived 39287 29:08:05,360 --> 29:08:10,320 class we also have the same function we 39288 29:08:07,759 --> 29:08:13,511 are overriding that and we have a 39289 29:08:10,320 --> 29:08:15,832 different set of values acting as our 39290 29:08:13,512 --> 29:08:19,040 default parameter here we are using 10 39291 29:08:15,831 --> 29:08:21,831 for a and b here and we want to see how 39292 29:08:19,039 --> 29:08:24,159 this plays with polymorphism now please 39293 29:08:21,831 --> 29:08:26,159 note that with polymorphism if we go 39294 29:08:24,160 --> 29:08:29,512 through a base pointer to manage a 39295 29:08:26,160 --> 29:08:32,320 derived class object what we want is to 39296 29:08:29,512 --> 29:08:34,720 call the most specific implementation of 39297 29:08:32,320 --> 29:08:37,592 the virtual function here for example if 39298 29:08:34,720 --> 29:08:40,551 we have a base pointer managing a 39299 29:08:37,592 --> 29:08:43,279 derived object and we call the add 39300 29:08:40,551 --> 29:08:46,159 method from a base pointer we will get 39301 29:08:43,279 --> 29:08:48,720 the most specific method here called 39302 29:08:46,160 --> 29:08:51,279 but what you get with these default 39303 29:08:48,720 --> 29:08:54,639 arguments is that the compiler is going 39304 29:08:51,279 --> 29:08:56,399 to use static binding to decide which 39305 29:08:54,639 --> 29:08:58,872 default parameters to pass to your 39306 29:08:56,399 --> 29:09:01,439 function so even if you will get this 39307 29:08:58,872 --> 29:09:04,320 method here called the compiler will 39308 29:09:01,440 --> 29:09:06,720 still use the default arguments we have 39309 29:09:04,320 --> 29:09:09,040 in our base class so we will call this 39310 29:09:06,720 --> 29:09:11,759 method here but we will use the default 39311 29:09:09,039 --> 29:09:13,591 arguments coming from the base class and 39312 29:09:11,759 --> 29:09:15,911 this is the behavior you need to be 39313 29:09:13,592 --> 29:09:18,800 aware of okay so another way to put this 39314 29:09:15,911 --> 29:09:21,191 is that default arguments are handled at 39315 29:09:18,800 --> 29:09:23,512 compile time the compiler is going to 39316 29:09:21,191 --> 29:09:25,759 wire these things at compile time where 39317 29:09:23,512 --> 29:09:28,232 you are building your application 39318 29:09:25,759 --> 29:09:31,279 and the virtual functions are called at 39319 29:09:28,232 --> 29:09:33,440 run time with polymorphism so if you use 39320 29:09:31,279 --> 29:09:35,831 default arguments with virtual functions 39321 29:09:33,440 --> 29:09:38,160 you might get weird results with 39322 29:09:35,831 --> 29:09:40,399 polymorphism because the default 39323 29:09:38,160 --> 29:09:43,592 arguments you will be using are going to 39324 29:09:40,399 --> 29:09:45,511 be decided by static binding but the 39325 29:09:43,592 --> 29:09:48,400 actual function that you get called is 39326 29:09:45,512 --> 29:09:50,800 going to be decided by dynamic binding 39327 29:09:48,399 --> 29:09:53,279 so we will call the virtual function in 39328 29:09:50,800 --> 29:09:55,512 our derived class but we will be using 39329 29:09:53,279 --> 29:09:57,592 the default arguments from the base 39330 29:09:55,512 --> 29:09:59,760 class this is the behavior we'll get you 39331 29:09:57,592 --> 29:10:02,232 need to be aware of this and make sure 39332 29:09:59,759 --> 29:10:04,319 this is what you desire my personal 39333 29:10:02,232 --> 29:10:06,480 preference is to not use default 39334 29:10:04,320 --> 29:10:08,552 parameters or default arguments with 39335 29:10:06,479 --> 29:10:10,551 virtual functions because this is really 39336 29:10:08,551 --> 29:10:13,119 going to make my code hard to follow and 39337 29:10:10,551 --> 29:10:15,279 hard to understand especially for other 39338 29:10:13,119 --> 29:10:17,911 developers that might have to work on my 39339 29:10:15,279 --> 29:10:20,000 code base in the future okay here is a 39340 29:10:17,911 --> 29:10:22,232 simple example we have a base pointer 39341 29:10:20,000 --> 29:10:25,119 which is going to be managing a derived 39342 29:10:22,232 --> 29:10:27,592 object and we will call the add function 39343 29:10:25,119 --> 29:10:29,759 here what do we expect to get well 39344 29:10:27,592 --> 29:10:32,400 because we have polymorphism here we 39345 29:10:29,759 --> 29:10:34,551 will call the most specific add function 39346 29:10:32,399 --> 29:10:37,759 here and that's going to be the add 39347 29:10:34,551 --> 29:10:40,159 function from our derived class 39348 29:10:37,759 --> 29:10:42,720 but the default argument we pass are not 39349 29:10:40,160 --> 29:10:44,400 going to be decided by the derived class 39350 29:10:42,720 --> 29:10:47,040 the default arguments are going to be 39351 29:10:44,399 --> 29:10:49,831 plugged in at compile time using static 39352 29:10:47,039 --> 29:10:52,231 binding so because the base pointer is a 39353 29:10:49,831 --> 29:10:54,159 base object we will be using the default 39354 29:10:52,232 --> 29:10:56,480 arguments from the base implementation 39355 29:10:54,160 --> 29:10:59,192 of the add method so we will just plug 39356 29:10:56,479 --> 29:11:02,231 in five and five so we will add five to 39357 29:10:59,191 --> 29:11:04,399 five and add a two that's why we get a 39358 29:11:04,399 --> 29:11:09,279 and if we go through these references we 39359 29:11:06,872 --> 29:11:12,720 will get the same result because this is 39360 29:11:09,279 --> 29:11:15,911 going to enable or trigger polymorphic 39361 29:11:12,720 --> 29:11:18,551 behavior in our c-plus programs if we go 39362 29:11:15,911 --> 29:11:21,039 through direct objects we will be doing 39363 29:11:18,551 --> 29:11:23,360 static binding and we will use the 39364 29:11:21,039 --> 29:11:25,679 default arguments or device parameters 39365 29:11:23,360 --> 29:11:27,680 from our derived object and remember 39366 29:11:25,679 --> 29:11:30,159 that we use the value of 10 in there so 39367 29:11:27,679 --> 29:11:32,479 we are going to add 10 to 10 and add a 2 39368 29:11:30,160 --> 29:11:35,440 and we are going to get a 22 as you see 39369 29:11:32,479 --> 29:11:38,551 here through static binding if we do 39370 29:11:35,440 --> 29:11:41,279 slicing by assigning a derived object to 39371 29:11:38,551 --> 29:11:44,159 a base object the derived information is 39372 29:11:41,279 --> 29:11:46,479 going to be sliced off and if we add we 39373 29:11:44,160 --> 29:11:48,639 will use the default parameters from the 39374 29:11:46,479 --> 29:11:51,039 base class and we will use the 39375 29:11:48,639 --> 29:11:53,759 implementation in the base class so if 39376 29:11:51,039 --> 29:11:55,759 we come back to our code here we will 39377 29:11:53,759 --> 29:11:57,759 use the default arguments from the base 39378 29:11:55,759 --> 29:11:59,679 class and that's a five and a five we're 39379 29:11:57,759 --> 29:12:02,080 going to add them up because the body is 39380 29:11:59,679 --> 29:12:04,079 going to be from the base class remember 39381 29:12:02,080 --> 29:12:07,119 the derived information has been 39382 29:12:04,080 --> 29:12:09,040 completely sliced off so we are going to 39383 29:12:07,119 --> 29:12:11,360 add a five and a five and a one and 39384 29:12:09,039 --> 29:12:14,479 we're going to get 11 printed out if we 39385 29:12:11,360 --> 29:12:16,639 do slicing and that's the result we get 39386 29:12:14,479 --> 29:12:19,119 right here as a closing note before we 39387 29:12:16,639 --> 29:12:21,191 head over to visual studio code again 39388 29:12:19,119 --> 29:12:23,440 default arguments with virtual functions 39389 29:12:21,191 --> 29:12:25,679 can be very confusing they are best 39390 29:12:23,440 --> 29:12:28,320 avoided because they really are going to 39391 29:12:25,679 --> 29:12:30,231 give a hard time to anybody that is 39392 29:12:28,320 --> 29:12:31,832 going to be working on your code in the 39393 29:12:30,232 --> 29:12:33,832 future now that you know this we're 39394 29:12:31,831 --> 29:12:35,439 going to head over to visual studio code 39395 29:12:33,831 --> 29:12:38,080 and play with this a little more here we 39396 29:12:35,440 --> 29:12:40,551 are in our working folder the current 39397 29:12:38,080 --> 29:12:42,400 project is virtual functions with 39398 29:12:40,551 --> 29:12:45,039 default arguments we're going to grab 39399 29:12:42,399 --> 29:12:47,039 the code from our template project we're 39400 29:12:45,039 --> 29:12:49,679 going to copy this and put this in the 39401 29:12:47,039 --> 29:12:52,231 current project and i am going to grab 39402 29:12:49,679 --> 29:12:54,639 my classes base and derived that we are 39403 29:12:52,232 --> 29:12:56,800 going to be using here if you don't want 39404 29:12:54,639 --> 29:12:59,039 to type along you can grab the source 39405 29:12:56,800 --> 29:13:00,000 code from the resource section of the 39406 29:13:00,000 --> 29:13:04,160 if you don't want to type all these 39407 29:13:01,679 --> 29:13:07,191 classes or create them you can grab the 39408 29:13:04,160 --> 29:13:08,552 source code from the resource section on 39409 29:13:07,191 --> 29:13:10,551 this lecture you're going to download 39410 29:13:08,551 --> 29:13:12,720 this and use this you don't have to 39411 29:13:10,551 --> 29:13:15,119 waste time typing these things because 39412 29:13:12,720 --> 29:13:17,279 many of these things are already 39413 29:13:15,119 --> 29:13:19,039 familiar to you we're going to open this 39414 29:13:17,279 --> 29:13:21,360 in visual studio code and we're going to 39415 29:13:19,039 --> 29:13:23,439 have our classes here we have a base 39416 29:13:21,360 --> 29:13:24,551 class which is going to have a virtual 39417 29:13:25,440 --> 29:13:31,191 a pair of default arguments here we are 39418 29:13:33,119 --> 29:13:38,000 and if we go and derived it is going to 39419 29:13:35,440 --> 29:13:40,232 derive publicly from base and it is 39420 29:13:40,232 --> 29:13:45,040 virtual function here but notice that it 39421 29:13:45,039 --> 29:13:49,039 default parameters so if we do 39422 29:13:49,039 --> 29:13:53,439 and use the base pointer to call the add 39423 29:13:51,191 --> 29:13:56,871 method here we will get the most 39424 29:13:53,440 --> 29:13:58,960 specific or the most specialized version 39425 29:13:56,872 --> 29:14:01,040 of the add method called and this is 39426 29:13:58,960 --> 29:14:03,592 going to call this body here 39427 29:14:01,039 --> 29:14:06,399 but the parameters are going to be 39428 29:14:03,592 --> 29:14:08,960 decided by static binding so because we 39429 29:14:06,399 --> 29:14:11,191 will be going through a base pointer we 39430 29:14:08,960 --> 29:14:13,832 will use the default arguments from the 39431 29:14:11,191 --> 29:14:15,119 base class that's going to be five five 39432 29:14:13,831 --> 29:14:17,191 and we're going to play with this and 39433 29:14:15,119 --> 29:14:19,911 see what this is going to give us in the 39434 29:14:17,191 --> 29:14:21,119 main cpp file here let's clean up a 39435 29:14:19,911 --> 29:14:23,191 little bit we're going to remove 39436 29:14:23,191 --> 29:14:28,159 and we are going to include our derived 39437 29:14:25,679 --> 29:14:30,551 class we can do that we are going to put 39438 29:14:28,160 --> 29:14:32,552 in our code to really play with us let's 39439 29:14:30,551 --> 29:14:34,720 remove these spaces because we don't 39440 29:14:32,551 --> 29:14:37,440 need them we are going to go through a 39441 29:14:34,720 --> 29:14:39,759 base pointer to manage a derived object 39442 29:14:37,440 --> 29:14:41,832 and this is a setup we can use to put 39443 29:14:39,759 --> 29:14:44,159 things in place here we are going to 39444 29:14:41,831 --> 29:14:46,720 call the add method notice that we are 39445 29:14:44,160 --> 29:14:48,960 not passing in any parameter because we 39446 29:14:46,720 --> 29:14:51,080 are going through a base pointer and the 39447 29:14:51,080 --> 29:14:55,680 polymorphism will kick in and we will 39448 29:14:55,679 --> 29:15:02,000 of our add function code so we will call 39449 29:15:02,000 --> 29:15:06,872 be careful because we want to be using 39450 29:15:04,232 --> 29:15:10,320 these default parameters here if we did 39451 29:15:06,872 --> 29:15:12,720 we would add a 10 to a 10 and get to 22 39452 29:15:10,320 --> 29:15:14,960 in our main function here but that's not 39453 29:15:12,720 --> 29:15:17,512 going to be the case the compiler is 39454 29:15:14,960 --> 29:15:20,080 going to use static binding to decide 39455 29:15:17,512 --> 29:15:22,080 which default parameters to use either 39456 29:15:20,080 --> 29:15:24,479 those in the base class or those in the 39457 29:15:22,080 --> 29:15:26,872 derived class and notice that if we 39458 29:15:24,479 --> 29:15:29,360 hover over this add function you see 39459 29:15:26,872 --> 29:15:31,279 that the compiler has plugged down those 39460 29:15:29,360 --> 29:15:32,800 from the base class and that's what the 39461 29:15:31,279 --> 29:15:35,279 compiler is going to do so it is going 39462 29:15:32,800 --> 29:15:37,279 to add a five to a five and it is going 39463 29:15:35,279 --> 29:15:40,551 to add two to that i think that's what 39464 29:15:37,279 --> 29:15:43,191 we are doing in our derived version and 39465 29:15:40,551 --> 29:15:45,512 we are going to get to 12 printed out 39466 29:15:43,191 --> 29:15:46,871 let's build and see if this is actually 39467 29:15:45,512 --> 29:15:48,551 the case we're going to pass this 39468 29:15:48,551 --> 29:15:53,191 and that's the compiler that is our 39469 29:15:51,039 --> 29:15:55,679 favorite now the book is going to be 39470 29:15:53,191 --> 29:15:58,720 good we can close this and bring up a 39471 29:15:55,679 --> 29:16:00,799 powershell window and if we run rooster 39472 29:15:58,720 --> 29:16:03,680 we're going to see that the arrived ad 39473 29:16:00,800 --> 29:16:05,760 was called and the result was 12 here 39474 29:16:05,759 --> 29:16:11,511 the compiler is going to call the most 39475 29:16:08,080 --> 29:16:14,320 specialized virtual function override 39476 29:16:11,512 --> 29:16:16,400 here but it is going to use the default 39477 29:16:14,320 --> 29:16:18,872 parameters from the base class you 39478 29:16:16,399 --> 29:16:20,959 should really note this and this can 39479 29:16:18,872 --> 29:16:24,639 really be confusing especially if you 39480 29:16:20,960 --> 29:16:27,040 are a beginner to polymorphism if we go 39481 29:16:24,639 --> 29:16:29,360 through a base reference we will also 39482 29:16:29,360 --> 29:16:34,232 so if we have a derived object and we 39483 29:16:31,759 --> 29:16:36,639 set up a reference to that which is of 39484 29:16:34,232 --> 29:16:38,720 type base and we go through that base 39485 29:16:36,639 --> 29:16:40,639 reference and called the add method we 39486 29:16:38,720 --> 29:16:42,479 will get the most specialized add 39487 29:16:40,639 --> 29:16:46,551 version called so that's going to be 39488 29:16:42,479 --> 29:16:48,872 derived and but we will use the default 39489 29:16:46,551 --> 29:16:50,872 parameters from the base class because 39490 29:16:48,872 --> 29:16:54,160 which default arguments we use is 39491 29:16:50,872 --> 29:16:56,160 decided by using static binding that's 39492 29:16:54,160 --> 29:16:58,160 what we get here and official studio 39493 29:16:56,160 --> 29:17:00,232 code is really helpful here because if 39494 29:16:58,160 --> 29:17:02,480 we hover over the add function we're 39495 29:17:00,232 --> 29:17:04,639 going to see which default parameters 39496 29:17:02,479 --> 29:17:06,959 were plugged in by the compiler and you 39497 29:17:04,639 --> 29:17:09,119 see that they are those from the base 39498 29:17:06,960 --> 29:17:11,360 class so we are again going to add five 39499 29:17:09,119 --> 29:17:14,479 to a five and we are going to add a two 39500 29:17:11,360 --> 29:17:16,320 and we will get 12 printed out here if 39501 29:17:14,479 --> 29:17:18,800 we boil and run we're going to see that 39502 29:17:16,320 --> 29:17:20,000 this is the case the build is good we 39503 29:17:20,000 --> 29:17:23,679 and run rooster we're going to see that 39504 29:17:21,911 --> 29:17:26,872 with a base reference we're also going 39505 29:17:23,679 --> 29:17:28,231 to get a 12. we will get the most 39506 29:17:28,232 --> 29:17:33,832 implementation of the add method called 39507 29:17:31,360 --> 29:17:35,911 but we will plug in the default 39508 29:17:33,831 --> 29:17:38,871 parameters from the base class i really 39509 29:17:35,911 --> 29:17:40,720 wanted this to be super clear if you set 39510 29:17:38,872 --> 29:17:43,360 up things in a way that you are going to 39511 29:17:40,720 --> 29:17:45,831 get static binding then if you are going 39512 29:17:43,360 --> 29:17:48,080 through a base object you will call the 39513 29:17:45,831 --> 29:17:50,231 base version of the add method and you 39514 29:17:48,080 --> 29:17:52,080 will use the default parameters from the 39515 29:17:50,232 --> 29:17:54,960 base class if you happen to be 39516 29:17:52,080 --> 29:17:57,279 manipulating a derived object you will 39517 29:17:54,960 --> 29:17:59,512 use the information in the derived class 39518 29:17:57,279 --> 29:18:01,279 the derived version of the add method is 39519 29:17:59,512 --> 29:18:03,680 going to be called and we are going to 39520 29:18:01,279 --> 29:18:06,959 use the default argument from the 39521 29:18:03,679 --> 29:18:09,191 derived class so if we set up another 39522 29:18:06,960 --> 29:18:11,592 example here we can set up a base object 39523 29:18:09,191 --> 29:18:13,039 for example we're going to say base and 39524 29:18:13,039 --> 29:18:19,119 3 we can do that and we try to call the 39525 29:18:16,232 --> 29:18:21,192 add method we can say base 3 and say add 39526 29:18:19,119 --> 29:18:23,360 we can try and store this in our 39527 29:18:21,191 --> 29:18:26,080 research variable and we can print this 39528 29:18:23,360 --> 29:18:28,479 out we're going to say sdbcl row 39529 29:18:26,080 --> 29:18:30,551 research we can say something like this 39530 29:18:28,479 --> 29:18:32,479 and we're going to print out the result 39531 29:18:30,551 --> 29:18:34,479 here if we build and run we're going to 39532 29:18:32,479 --> 29:18:36,800 see that this is going to call 39533 29:18:34,479 --> 29:18:38,231 the base version of the add method and 39534 29:18:36,800 --> 29:18:40,960 it is going to use the default 39535 29:18:38,232 --> 29:18:42,720 parameters from the base if we go back 39536 29:18:40,960 --> 29:18:46,000 to base we're going to see that we are 39537 29:18:42,720 --> 29:18:49,040 going to add a 5 to a 5 and we will get 39538 29:18:46,000 --> 29:18:51,512 11 because we will plug in a 1 and 39539 29:18:49,039 --> 29:18:53,591 return the result here let's build and 39540 29:18:51,512 --> 29:18:55,832 run and see that this is actually the 39541 29:18:53,592 --> 29:18:58,800 case so this is going to be completely 39542 29:18:55,831 --> 29:19:00,399 doing static binding let's clear and run 39543 29:18:58,800 --> 29:19:02,160 you see that the world is good we're 39544 29:19:00,399 --> 29:19:04,399 going to run restaurant and we will get 39545 29:19:02,160 --> 29:19:07,680 11 printed out here hopefully you can 39546 29:19:04,399 --> 29:19:08,551 see this if you assign a derived object 39547 29:19:08,551 --> 29:19:14,000 a base object you will get slicing 39548 29:19:11,679 --> 29:19:16,799 let's see that we can actually do this 39549 29:19:14,000 --> 29:19:19,039 but before we do that i also want you to 39550 29:19:19,039 --> 29:19:24,000 row direct objects of derived classes if 39551 29:19:24,000 --> 29:19:28,232 get the information from the derived 39552 29:19:26,320 --> 29:19:30,872 class we will use the body from the 39553 29:19:28,232 --> 29:19:32,639 derived class and we will use the 39554 29:19:30,872 --> 29:19:35,680 default parameters from the derived 39555 29:19:32,639 --> 29:19:37,831 class so we will take 10 and add a 10 39556 29:19:35,679 --> 29:19:40,799 and we will add the 2 to that and we 39557 29:19:37,831 --> 29:19:42,479 will get a 22 printed out i think we can 39558 29:19:43,279 --> 29:19:47,831 let's put in a separator because that's 39559 29:19:45,759 --> 29:19:50,159 going to make it easier to follow these 39560 29:19:47,831 --> 29:19:52,639 lengths and we're going to build with 39561 29:19:50,160 --> 29:19:53,832 the gcc the both is going to be good 39562 29:19:53,831 --> 29:19:58,080 the bulk is good we can clear 39563 29:19:56,160 --> 29:20:01,512 and run rooster and we're going to see 39564 29:19:58,080 --> 29:20:04,551 that we get a 22 here if we assign a 39565 29:20:01,512 --> 29:20:06,639 derived object to a base object directly 39566 29:20:04,551 --> 29:20:09,039 by doing something like this event we 39567 29:20:09,039 --> 29:20:15,039 we can remove this we have a base object 39568 29:20:12,399 --> 29:20:17,511 that we are setting up here and we are 39569 29:20:15,039 --> 29:20:18,799 assigning a derived object to this base 39570 29:20:18,800 --> 29:20:24,080 if we call the add method we will get 39571 29:20:21,440 --> 29:20:26,479 the version from the base class because 39572 29:20:24,080 --> 29:20:29,119 mind your steps this is going to slice 39573 29:20:26,479 --> 29:20:31,039 off all the derived information because 39574 29:20:29,119 --> 29:20:32,959 we are not going through any kind of 39575 29:20:31,039 --> 29:20:35,679 base or reference so this is going to 39576 29:20:32,960 --> 29:20:37,192 slice off all the information we have in 39577 29:20:37,191 --> 29:20:42,551 object here and we will be left with the 39578 29:20:39,759 --> 29:20:44,720 base information and if we call the add 39579 29:20:42,551 --> 29:20:46,479 function here you see that we are using 39580 29:20:44,720 --> 29:20:49,680 the default parameters from the base 39581 29:20:46,479 --> 29:20:51,911 class and we will use the body of the 39582 29:20:49,679 --> 29:20:54,871 base class implementation here so we 39583 29:20:51,911 --> 29:20:57,591 will add a 5 to a 5 and we will add 1 to 39584 29:20:54,872 --> 29:20:59,680 that and we will get 11 printed out as 39585 29:20:57,592 --> 29:21:01,120 we see here let's build and actually 39586 29:20:59,679 --> 29:21:02,959 prove this we're going to pass this 39587 29:21:01,119 --> 29:21:05,512 through gcc the world is going to be 39588 29:21:02,960 --> 29:21:07,760 good we can clear and run rooster and we 39589 29:21:05,512 --> 29:21:11,040 are going to see that the result is 39590 29:21:07,759 --> 29:21:12,959 irreverent if we do row object 39591 29:21:11,039 --> 29:21:15,360 assignment here this is really all i 39592 29:21:12,960 --> 29:21:17,440 wanted you to see in this lecture that 39593 29:21:15,360 --> 29:21:19,440 you should really be careful if you are 39594 29:21:19,440 --> 29:21:24,232 mixed up with default arguments the 39595 29:21:22,000 --> 29:21:26,639 compiler is not going to do dynamic 39596 29:21:24,232 --> 29:21:29,360 binding on the default parameters it is 39597 29:21:26,639 --> 29:21:32,960 going to decide which default parameters 39598 29:21:29,360 --> 29:21:34,720 to plug in at compile time using static 39599 29:21:32,960 --> 29:21:36,960 binding so the compiler is basically 39600 29:21:34,720 --> 29:21:39,592 going to look at the type of the pointer 39601 29:21:36,960 --> 29:21:41,760 or object that you have and it is going 39602 29:21:39,592 --> 29:21:44,552 to use that to decide which default 39603 29:21:41,759 --> 29:21:46,551 parameters to plug in so at this example 39604 29:21:44,551 --> 29:21:49,039 here for example at line 7 it is going 39605 29:21:46,551 --> 29:21:50,959 to see what kind of pointer do i have i 39606 29:21:49,039 --> 29:21:53,360 have a base pointer so i am going to use 39607 29:21:50,960 --> 29:21:55,440 the default parameters from the base 39608 29:21:53,360 --> 29:21:56,720 implementation and if you hover over 39609 29:21:55,440 --> 29:21:59,040 here you're going to see that it is 39610 29:21:56,720 --> 29:22:01,279 going to plug in a 5 and a 5. even if we 39611 29:21:59,039 --> 29:22:04,159 are using the most specialized add 39612 29:22:01,279 --> 29:22:06,000 version in our inheritance hierarchy the 39613 29:22:04,160 --> 29:22:08,232 compiler is going to still use the 39614 29:22:06,000 --> 29:22:11,039 default argument from the base 39615 29:22:08,232 --> 29:22:13,120 implementation down here on line 16 the 39616 29:22:11,039 --> 29:22:15,039 compiler is going to say which kind of 39617 29:22:13,119 --> 29:22:17,279 reference do i have it's going to say 39618 29:22:15,039 --> 29:22:18,719 base because that's what we have here so 39619 29:22:17,279 --> 29:22:21,279 it is going to use the default 39620 29:22:18,720 --> 29:22:23,279 parameters from base and that's what it 39621 29:22:21,279 --> 29:22:25,512 is going to plug in here 5 and 5 and 39622 29:22:23,279 --> 29:22:28,232 we're going to get this result here 39623 29:22:25,512 --> 29:22:30,160 down here on line 25 the compiler is 39624 29:22:28,232 --> 29:22:32,872 going to see which kind of object do i 39625 29:22:30,160 --> 29:22:35,120 have here this three is a base so i am 39626 29:22:32,872 --> 29:22:36,160 going to use the default parameters from 39627 29:22:36,160 --> 29:22:40,552 down below on line 31 the compiler is 39628 29:22:40,551 --> 29:22:45,759 which kind of object do i have here the 39629 29:22:42,800 --> 29:22:48,000 object is derived so i will plug in the 39630 29:22:45,759 --> 29:22:49,439 default parameters for it derived again 39631 29:22:48,000 --> 29:22:52,160 this is really what the compiler is 39632 29:22:49,440 --> 29:22:54,720 going to do with default parameters the 39633 29:22:52,160 --> 29:22:56,800 compiler is never going to do dynamic 39634 29:22:54,720 --> 29:23:00,160 binding and this is going to make your 39635 29:22:56,800 --> 29:23:03,440 code really confusing again my advice is 39636 29:23:00,160 --> 29:23:06,800 to avoid default arguments or devote 39637 29:23:03,440 --> 29:23:09,512 parameters if you are using polymorphism 39638 29:23:06,800 --> 29:23:11,592 in your inheritance hierarchy we are 39639 29:23:09,512 --> 29:23:13,440 going to stop here in this lecture and 39640 29:23:11,592 --> 29:23:15,512 the next one we're going to learn about 39641 29:23:15,512 --> 29:23:19,832 go ahead and finish up here and meet me 39642 29:23:17,831 --> 29:23:23,191 there in this lecture we are going to 39643 29:23:19,831 --> 29:23:25,831 explore virtual destructors and these 39644 29:23:23,191 --> 29:23:27,039 are destructor methods you might want to 39645 29:23:27,039 --> 29:23:31,591 using dynamic binding or polymorphic 39646 29:23:31,592 --> 29:23:34,800 we are going to be using this 39647 29:23:33,039 --> 29:23:36,399 inheritance hierarchy to play with 39648 29:23:34,800 --> 29:23:38,551 thanks so the animal class is going to 39649 29:23:36,399 --> 29:23:41,039 be our base class we are going to derive 39650 29:23:38,551 --> 29:23:43,831 from that publicly and have a feline 39651 29:23:41,039 --> 29:23:46,551 class and we are going to derive from 39652 29:23:43,831 --> 29:23:49,279 feline and have a dog class this is the 39653 29:23:46,551 --> 29:23:51,591 setup we are going to be using here 39654 29:23:49,279 --> 29:23:53,759 now let's look at some code we are going 39655 29:23:51,592 --> 29:23:56,232 to set up our animal class like there's 39656 29:23:53,759 --> 29:23:58,399 nothing really special here notice that 39657 29:23:56,232 --> 29:23:59,512 the destructor is a regular destructor 39658 29:23:59,512 --> 29:24:05,440 we will derive from animal and create a 39659 29:24:03,191 --> 29:24:07,279 feline class which is going to set up 39660 29:24:05,440 --> 29:24:10,160 its own virtual function which is going 39661 29:24:07,279 --> 29:24:12,160 to be run if you want you can override 39662 29:24:10,160 --> 29:24:14,480 the breathe method but we don't want to 39663 29:24:12,160 --> 29:24:15,512 do that here because we just want to 39664 29:24:15,512 --> 29:24:20,960 on the destructors here and the order in 39665 29:24:18,639 --> 29:24:23,831 which they are going to be called we are 39666 29:24:20,960 --> 29:24:26,552 going to derive from feline and set up a 39667 29:24:23,831 --> 29:24:29,039 dog class and this dog class is going to 39668 29:24:26,551 --> 29:24:31,119 set up its own virtual function and 39669 29:24:29,039 --> 29:24:33,679 notice that it has a destructor which is 39670 29:24:31,119 --> 29:24:36,159 really a regular destructor now once we 39671 29:24:33,679 --> 29:24:38,479 have this inheritance hierarchy set up 39672 29:24:36,160 --> 29:24:40,552 we might want to do something like this 39673 29:24:38,479 --> 29:24:43,512 we can create a dark object 39674 29:24:40,551 --> 29:24:46,639 and when it is time for this object to 39675 29:24:43,512 --> 29:24:48,551 be destroyed i want you to focus on the 39676 29:24:46,639 --> 29:24:50,800 order in which the destructors are going 39677 29:24:48,551 --> 29:24:53,191 to be called we have seen this before 39678 29:24:50,800 --> 29:24:56,639 the destructors are going to be called 39679 29:24:53,191 --> 29:24:58,871 from the specialized to the most base 39680 29:24:56,639 --> 29:25:01,440 implementation of the destructor so the 39681 29:24:58,872 --> 29:25:03,192 destructor for doug will be called first 39682 29:25:01,440 --> 29:25:05,760 then we will call the destructor for 39683 29:25:03,191 --> 29:25:08,319 feline then we will call the destructor 39684 29:25:05,759 --> 29:25:11,191 for animal now a problem is going to pop 39685 29:25:08,320 --> 29:25:14,480 up if we are using a base pointer to 39686 29:25:11,191 --> 29:25:16,720 manage a derived object with polymorphic 39687 29:25:14,479 --> 29:25:18,959 behavior that we might want for our 39688 29:25:16,720 --> 29:25:21,191 inheritance hierarchy here so we are 39689 29:25:18,960 --> 29:25:23,680 going to use an animal pointer to manage 39690 29:25:21,191 --> 29:25:25,512 a dark object like this and when it is 39691 29:25:23,679 --> 29:25:26,719 time to release the memory for this 39692 29:25:26,720 --> 29:25:33,040 we may be tempted to call the delete 39693 29:25:29,911 --> 29:25:34,720 operator on our animal pointer here but 39694 29:25:33,039 --> 29:25:36,000 this is only going to call the 39695 29:25:36,000 --> 29:25:41,039 for the base class and hopefully you can 39696 29:25:38,800 --> 29:25:44,232 see the problem here because our 39697 29:25:41,039 --> 29:25:46,719 destructors are not virtual the compiler 39698 29:25:44,232 --> 29:25:48,960 is going to use static binding to decide 39699 29:25:46,720 --> 29:25:50,872 which destructor to call so it is going 39700 29:25:48,960 --> 29:25:52,232 to look at the type of a pointer it is 39701 29:25:50,872 --> 29:25:54,000 going to see that it is an animal 39702 29:25:52,232 --> 29:25:56,872 pointer and it is going to decide to 39703 29:25:54,000 --> 29:25:59,119 call the destructor for animal here 39704 29:25:56,872 --> 29:26:01,440 that's why we get this reserve we are 39705 29:25:59,119 --> 29:26:04,159 managing an object which is not just an 39706 29:26:01,440 --> 29:26:06,800 animal it has derived information in it 39707 29:26:04,160 --> 29:26:09,592 but we are only calling the destructor 39708 29:26:06,800 --> 29:26:11,680 for the base part of this stun so any 39709 29:26:09,592 --> 29:26:14,800 piece of dynamic memory we might have 39710 29:26:11,679 --> 29:26:17,511 allocated in the constructors for feline 39711 29:26:14,800 --> 29:26:20,479 or dog is going to be leaked out because 39712 29:26:17,512 --> 29:26:22,551 remember even if this is a dark object 39713 29:26:20,479 --> 29:26:24,551 it is made up of base parts the 39714 29:26:22,551 --> 29:26:26,800 foundation for this is going to be 39715 29:26:24,551 --> 29:26:29,440 animal and on top of that we are going 39716 29:26:26,800 --> 29:26:31,911 to build a feline level and on top of 39717 29:26:29,440 --> 29:26:34,160 that we are going to build a dog level 39718 29:26:31,911 --> 29:26:37,512 in this case only the destructor for dog 39719 29:26:34,160 --> 29:26:40,232 is going to be called and any dynamic 39720 29:26:37,512 --> 29:26:43,512 memory that might have been allocated at 39721 29:26:40,232 --> 29:26:45,279 the feline and dog level is going to be 39722 29:26:43,512 --> 29:26:47,191 leaked out and this is something we 39723 29:26:45,279 --> 29:26:49,512 don't want you really need to keep your 39724 29:26:47,191 --> 29:26:51,360 eye out on this and this is something 39725 29:26:49,512 --> 29:26:53,832 really bad you really need to keep your 39726 29:26:51,360 --> 29:26:55,911 eye out for this so this is really a big 39727 29:26:53,831 --> 29:26:58,399 problem and to solve it we need the 39728 29:26:55,911 --> 29:27:01,279 destructors to be called using 39729 29:26:58,399 --> 29:27:04,639 polymorphism and you can set that up by 39730 29:27:01,279 --> 29:27:06,872 marking your destructors as a virtual if 39731 29:27:04,639 --> 29:27:08,872 you do this the compiler will know 39732 29:27:06,872 --> 29:27:11,592 that if you are going through a base 39733 29:27:08,872 --> 29:27:13,592 pointer to manage a derived object when 39734 29:27:11,592 --> 29:27:15,512 the time comes for the memory to be 39735 29:27:13,592 --> 29:27:17,912 released and you release the memory 39736 29:27:15,512 --> 29:27:19,911 through a base pointer the compiler is 39737 29:27:17,911 --> 29:27:22,319 going to call the most specific 39738 29:27:19,911 --> 29:27:24,800 destructor it can find and this is going 39739 29:27:22,320 --> 29:27:27,192 to solve our problem then we will reuse 39740 29:27:24,800 --> 29:27:29,911 the order we are familiar with the most 39741 29:27:27,191 --> 29:27:32,159 derived destructor will be called and it 39742 29:27:29,911 --> 29:27:34,000 will call its best destructor and that's 39743 29:27:32,160 --> 29:27:36,720 going to happen until we hit the most 39744 29:27:34,000 --> 29:27:38,479 base class in our inheritance hierarchy 39745 29:27:36,720 --> 29:27:40,639 and the memory is going to be released 39746 29:27:38,479 --> 29:27:42,000 exactly as we would want now that you 39747 29:27:40,639 --> 29:27:43,831 know this we're going to head over to 39748 29:27:42,000 --> 29:27:46,551 visual studio code and play with us a 39749 29:27:43,831 --> 29:27:48,231 little more here we are in our working 39750 29:27:46,551 --> 29:27:50,720 folder we are going to learn about 39751 29:27:48,232 --> 29:27:52,720 virtual destructors and before we do 39752 29:27:50,720 --> 29:27:55,279 that we're going to grab our template 39753 29:27:52,720 --> 29:27:57,279 files and put them in place let's do 39754 29:27:55,279 --> 29:27:59,679 that and i am going to put in the 39755 29:27:57,279 --> 29:28:02,000 classes that make up our inheritance 39756 29:27:59,679 --> 29:28:04,551 hierarchy our most based class is going 39757 29:28:02,000 --> 29:28:06,479 to be base our most based class is going 39758 29:28:04,551 --> 29:28:08,720 to be animal after that we're going to 39759 29:28:06,479 --> 29:28:11,191 derive we are going to derive from that 39760 29:28:08,720 --> 29:28:13,592 and create the feline class and we are 39761 29:28:11,191 --> 29:28:16,159 going to derive from feline and create a 39762 29:28:13,592 --> 29:28:18,552 dog class here this is the inheritance 39763 29:28:16,160 --> 29:28:20,400 hierarchy we talked about in our slides 39764 29:28:18,551 --> 29:28:22,639 okay we are going to open this up in 39765 29:28:20,399 --> 29:28:25,360 visual studio code and this is going to 39766 29:28:22,639 --> 29:28:27,440 give us our classes here and we are 39767 29:28:25,360 --> 29:28:29,191 going to remove these virtual keywords 39768 29:28:29,191 --> 29:28:34,399 what would happen if you don't mark your 39769 29:28:31,911 --> 29:28:36,232 destructors as a virtual we're going to 39770 29:28:34,399 --> 29:28:38,479 do that for feline we're going to do 39771 29:28:36,232 --> 29:28:40,639 that for doug let's remove the virtual 39772 29:28:38,479 --> 29:28:42,479 keyword here we're going to remove that 39773 29:28:40,639 --> 29:28:44,800 and we're going to remove that for the 39774 29:28:42,479 --> 29:28:47,191 animal class and we want to see the 39775 29:28:44,800 --> 29:28:49,512 implementations for our destructors for 39776 29:28:47,191 --> 29:28:52,159 animal we are just going to say that the 39777 29:28:49,512 --> 29:28:53,911 destructor for animal was called for 39778 29:28:52,160 --> 29:28:56,000 feline we're going to say the same thing 39779 29:28:53,911 --> 29:28:57,679 feline destructor called for doubt we're 39780 29:28:56,000 --> 29:29:00,800 going to say the same thing we're going 39781 29:28:57,679 --> 29:29:04,639 to say destructor for dog called here 39782 29:29:00,800 --> 29:29:07,040 we are using strengths as our parameters 39783 29:29:04,639 --> 29:29:09,191 here so i think it is a good idea to 39784 29:29:07,039 --> 29:29:10,399 change these guys to use string view i 39785 29:29:10,399 --> 29:29:15,279 and come back when this is done 39786 29:29:12,551 --> 29:29:18,551 okay this is done all our parameters are 39787 29:29:15,279 --> 29:29:20,872 of std string view type and now that we 39788 29:29:18,551 --> 29:29:23,679 have this inheritance hierarchy let's 39789 29:29:20,872 --> 29:29:26,232 make sure none of the destructors is 39790 29:29:23,679 --> 29:29:29,591 virtual this is the case here we can 39791 29:29:26,232 --> 29:29:31,760 head over to the main cpp file and use 39792 29:29:29,592 --> 29:29:34,160 this inheritance hierarchy here we're 39793 29:29:31,759 --> 29:29:36,639 going to crack it open and remove 39794 29:29:36,639 --> 29:29:42,872 we are going to include our dog class 39795 29:29:40,000 --> 29:29:44,800 which is going to implicitly include or 39796 29:29:42,872 --> 29:29:47,592 its parent classes so we're going to 39797 29:29:44,800 --> 29:29:50,160 include dog.h here and we are going to 39798 29:29:47,592 --> 29:29:52,400 create a row.object by default here 39799 29:29:50,160 --> 29:29:55,040 we're going to say dog and let's say dog 39800 29:29:52,399 --> 29:29:57,511 one we can do that and we might not even 39801 29:29:55,039 --> 29:29:59,511 give it any parameter this is going to 39802 29:29:57,512 --> 29:30:01,911 call the default constructor and if we 39803 29:29:59,512 --> 29:30:04,160 go to doug we're going to see that it is 39804 29:30:01,911 --> 29:30:06,551 possible to specify information here 39805 29:30:04,160 --> 29:30:09,680 first style description but we're going 39806 29:30:06,551 --> 29:30:12,639 to ignore this and we just want to see 39807 29:30:09,679 --> 29:30:15,191 which order in which the destructors are 39808 29:30:12,639 --> 29:30:17,440 called let's build and run this program 39809 29:30:15,191 --> 29:30:19,679 we're going to pass this through gcc the 39810 29:30:17,440 --> 29:30:21,832 book is going to be good if we clear 39811 29:30:19,679 --> 29:30:23,831 let's bring up a powershell window to do 39812 29:30:23,831 --> 29:30:28,551 and run rooster you're going to see that 39813 29:30:26,160 --> 29:30:30,720 dog destructor was called feline 39814 29:30:28,551 --> 29:30:32,479 destructor was called animal destructor 39815 29:30:30,720 --> 29:30:36,000 was called and this is really cool this 39816 29:30:32,479 --> 29:30:38,639 is the order we expect by default but if 39817 29:30:36,000 --> 29:30:41,831 we don't do something like this and say 39818 29:30:38,639 --> 29:30:44,872 animal pointer and say p animal to mean 39819 29:30:41,831 --> 29:30:46,959 a pointer to animal and we say new dog 39820 29:30:44,872 --> 29:30:48,960 we're going to allocate a dog on the 39821 29:30:46,960 --> 29:30:53,040 heap and we're going to manage that 39822 29:30:48,960 --> 29:30:54,872 using a base pointer and if we try to 39823 29:30:53,039 --> 29:30:57,360 release the memory here let's do that 39824 29:30:54,872 --> 29:31:00,080 we're going to say delete p animal let's 39825 29:30:57,360 --> 29:31:02,080 see which destructor gets called here 39826 29:31:00,080 --> 29:31:06,160 because notice we are using a base 39827 29:31:02,080 --> 29:31:08,639 pointer to manage a must specific object 39828 29:31:06,160 --> 29:31:11,360 here which is a derived class of dog 39829 29:31:08,639 --> 29:31:14,551 indirectly and we will see that we don't 39830 29:31:11,360 --> 29:31:16,800 get the destructors called in the order 39831 29:31:14,551 --> 29:31:18,959 that we expect let's build and run this 39832 29:31:16,800 --> 29:31:21,040 program to really see this we're going 39833 29:31:21,039 --> 29:31:25,439 the world is going to be good as you see 39834 29:31:25,440 --> 29:31:31,592 and run rooster now you see that only 39835 29:31:28,232 --> 29:31:34,480 the destructor for animal is called even 39836 29:31:31,592 --> 29:31:36,320 if we are really managing a dog object 39837 29:31:34,479 --> 29:31:38,479 for things to work really well we would 39838 29:31:36,320 --> 29:31:40,640 expect the destructor for duck to be 39839 29:31:38,479 --> 29:31:42,639 called and then the destructor for a 39840 29:31:40,639 --> 29:31:45,911 feline and then the destructor for 39841 29:31:42,639 --> 29:31:48,639 animal to release or the memory that 39842 29:31:45,911 --> 29:31:51,440 might have been allocated by these 39843 29:31:48,639 --> 29:31:53,831 parent classes for our dog class if you 39844 29:31:51,440 --> 29:31:55,592 go back in the cpp file for example for 39845 29:31:53,831 --> 29:31:57,511 each of these classes you're going to 39846 29:31:55,592 --> 29:31:58,400 see that the compiler is going to be 39847 29:31:58,399 --> 29:32:02,871 each base class of the current level 39848 29:32:00,479 --> 29:32:05,440 that we are at in doug cpp we are going 39849 29:32:02,872 --> 29:32:07,120 to call the constructor for feline the 39850 29:32:05,440 --> 29:32:09,512 constructor for feline is going to be 39851 29:32:07,119 --> 29:32:11,360 called if it is doing any dynamic memory 39852 29:32:09,512 --> 29:32:13,680 allocation we need to release this 39853 29:32:11,360 --> 29:32:15,680 memory constructor for feline is also 39854 29:32:13,679 --> 29:32:18,000 going to call the constructor for animal 39855 29:32:15,679 --> 29:32:19,831 and if animal is doing any dynamic 39856 29:32:18,000 --> 29:32:22,232 memory allocation this is going to need 39857 29:32:19,831 --> 29:32:25,191 to be called but notice that we are just 39858 29:32:22,232 --> 29:32:27,512 colon the destructor for animal and if 39859 29:32:25,191 --> 29:32:29,440 we only call this destructor all of the 39860 29:32:27,512 --> 29:32:31,592 memory that was allocated by the filing 39861 29:32:29,440 --> 29:32:33,592 constructor is going to be leaked out 39862 29:32:31,592 --> 29:32:35,120 because only the animal destructor is 39863 29:32:33,592 --> 29:32:37,512 going to be called hopefully you can see 39864 29:32:35,119 --> 29:32:39,911 the problem here but this is not the 39865 29:32:37,512 --> 29:32:42,080 only problem the c plus plus standard 39866 29:32:39,911 --> 29:32:45,512 says that if you do something like we 39867 29:32:42,080 --> 29:32:47,759 are doing here and call a destructor 39868 29:32:45,512 --> 29:32:49,832 using a pointer to the base class but 39869 29:32:47,759 --> 29:32:52,231 that pointer happens to be managing a 39870 29:32:49,831 --> 29:32:54,000 derived object this is going to give you 39871 29:32:52,232 --> 29:32:56,400 undefined behavior so you're not 39872 29:32:54,000 --> 29:32:58,160 guaranteed to get to the same behavior 39873 29:32:56,399 --> 29:33:00,159 on different compilers on different 39874 29:32:58,160 --> 29:33:02,552 operating systems so this is something 39875 29:33:00,160 --> 29:33:05,192 really bad we want all the destructors 39876 29:33:02,551 --> 29:33:08,319 in our inheritance hierarchy to be 39877 29:33:05,191 --> 29:33:10,319 called to properly release the memory 39878 29:33:08,320 --> 29:33:12,872 that would potentially be dynamically 39879 29:33:10,320 --> 29:33:14,872 allocated by each level in our 39880 29:33:12,872 --> 29:33:17,192 constructor calls the fix for this is 39881 29:33:14,872 --> 29:33:20,080 really simple all you have to do is mark 39882 29:33:17,191 --> 29:33:23,039 your destructors as virtual and the 39883 29:33:20,080 --> 29:33:24,232 compiler will call the most specific 39884 29:33:24,232 --> 29:33:29,760 for your classes so all we need to do is 39885 29:33:27,191 --> 29:33:31,911 go in and mark these guys as virtual so 39886 29:33:29,759 --> 29:33:33,759 we're going to do that the base class 39887 29:33:31,911 --> 29:33:35,831 destructor is going to be marked as 39888 29:33:33,759 --> 29:33:37,360 virtual we're going to go to feline and 39889 29:33:35,831 --> 29:33:39,591 do the same thing we're going to mark 39890 29:33:37,360 --> 29:33:40,479 that as virtual and we're going to go to 39891 29:33:40,479 --> 29:33:45,759 and mark its destructor as virtual we're 39892 29:33:43,512 --> 29:33:48,000 going to do something like this and now 39893 29:33:45,759 --> 29:33:49,679 notice we don't need to change any other 39894 29:33:48,000 --> 29:33:52,232 thing if we build our program we're 39895 29:33:49,679 --> 29:33:54,871 going to pass it through gcc it is going 39896 29:33:52,232 --> 29:33:57,760 to do exactly what we want because 39897 29:33:54,872 --> 29:34:00,000 now the compiler will resolve the calls 39898 29:33:57,759 --> 29:34:01,679 to the destructors polymorphically 39899 29:34:00,000 --> 29:34:03,831 because we are going through a base 39900 29:34:01,679 --> 29:34:05,831 pointer the compiler will call the most 39901 29:34:03,831 --> 29:34:07,911 specific implementation for this 39902 29:34:05,831 --> 29:34:10,159 structure because we are going through a 39903 29:34:07,911 --> 29:34:12,399 base pointer to call the destructor here 39904 29:34:12,399 --> 29:34:16,871 the most specialized destructor we can 39905 29:34:15,440 --> 29:34:19,512 find and it is going to find the 39906 29:34:16,872 --> 29:34:21,760 destructor for duck which is going to be 39907 29:34:19,512 --> 29:34:24,479 the most specialized destructor we can 39908 29:34:21,759 --> 29:34:26,639 find in our inheritance hierarchy it is 39909 29:34:24,479 --> 29:34:29,512 going to call the dog destructor that's 39910 29:34:26,639 --> 29:34:32,000 going to go the destructor for feline in 39911 29:34:29,512 --> 29:34:34,400 turn and then that's going to call the 39912 29:34:32,000 --> 29:34:36,479 destructor for animal and our memory is 39913 29:34:34,399 --> 29:34:38,399 going to be released properly that's 39914 29:34:36,479 --> 29:34:40,800 what we're going to see here let's make 39915 29:34:38,399 --> 29:34:43,679 sure we weld this again because i'm not 39916 29:34:40,800 --> 29:34:45,192 sure if i actually built this the body 39917 29:34:43,679 --> 29:34:48,000 is going to be good anyway so we're 39918 29:34:45,191 --> 29:34:49,911 going to clear and run rooster now you 39919 29:34:48,000 --> 29:34:51,911 see that the duct destructor is called 39920 29:34:49,911 --> 29:34:53,679 the vlan destructor is called the animal 39921 29:34:51,911 --> 29:34:56,399 destructor is gold and everything is 39922 29:34:53,679 --> 29:34:58,799 just going to fall in place so my advice 39923 29:34:56,399 --> 29:35:01,759 if you are using virtual functions in 39924 29:34:58,800 --> 29:35:04,872 your inheritance hierarchy make sure you 39925 29:35:01,759 --> 29:35:06,799 also mark your destructors as virtual 39926 29:35:04,872 --> 29:35:09,592 this is going to save you future 39927 29:35:06,800 --> 29:35:11,440 headaches you might come in contact with 39928 29:35:09,592 --> 29:35:13,760 if you are using a base pointer to 39929 29:35:11,440 --> 29:35:15,760 manage a derived object and you are 39930 29:35:13,759 --> 29:35:18,231 doing some kind of dynamic memory 39931 29:35:15,759 --> 29:35:20,871 allocations in your constructors that's 39932 29:35:18,232 --> 29:35:23,360 going to release the memory properly if 39933 29:35:20,872 --> 29:35:26,160 you are releasing the memory through a 39934 29:35:23,360 --> 29:35:28,080 base pointer like we are doing on line 8 39935 29:35:26,160 --> 29:35:29,592 here this is really all i had to share 39936 29:35:28,080 --> 29:35:31,680 in this lecture i hope you found it 39937 29:35:29,592 --> 29:35:33,360 interesting we are going to stop here in 39938 29:35:31,679 --> 29:35:36,159 this one in the next one we're going to 39939 29:35:36,160 --> 29:35:40,232 go ahead and finish up here and meet me 39940 29:35:38,399 --> 29:35:43,039 there in this lecture we're going to 39941 29:35:40,232 --> 29:35:45,680 learn about dynamic casts and the 39942 29:35:43,039 --> 29:35:47,119 dynamic casts are a facility we have in 39943 29:35:47,119 --> 29:35:54,000 to do downstream transformations 39944 29:35:54,000 --> 29:35:59,440 so if we have a base planer for example 39945 29:35:56,399 --> 29:36:01,759 we can transform it to a derived type 39946 29:35:59,440 --> 29:36:04,479 and be able to use it like we use any 39947 29:36:01,759 --> 29:36:06,479 row object for example if we do this 39948 29:36:04,479 --> 29:36:09,191 kind of transformation we will be able 39949 29:36:06,479 --> 29:36:12,800 to call non-polymorphic functions and 39950 29:36:09,191 --> 29:36:14,720 this may come in handy sometimes 39951 29:36:12,800 --> 29:36:17,440 this is the hierarchy we will be using 39952 29:36:14,720 --> 29:36:19,592 our top class will be animal here we 39953 29:36:17,440 --> 29:36:21,760 will derive from this and create our 39954 29:36:19,592 --> 29:36:25,360 feline class and we will be live from 39955 29:36:21,759 --> 29:36:27,511 feline and create our dog class here if 39956 29:36:25,360 --> 29:36:29,911 we have a base pointer pointing to a 39957 29:36:27,512 --> 29:36:32,232 dark object for example the most 39958 29:36:29,911 --> 29:36:35,360 specialized class we can have in our 39959 29:36:32,232 --> 29:36:39,120 inheritance hierarchy sometimes we might 39960 29:36:35,360 --> 29:36:42,399 want to transform from this base pointer 39961 29:36:39,119 --> 29:36:45,440 to the most derived object here and get 39962 29:36:42,399 --> 29:36:47,591 to the direct most derived object that 39963 29:36:45,440 --> 29:36:50,080 we can really call anything on if you 39964 29:36:47,592 --> 29:36:52,800 remember with a base planer like animal 39965 29:36:50,080 --> 29:36:53,680 here the only thing we can do is really 39966 29:36:53,679 --> 29:36:58,799 polymorphic or virtual functions but 39967 29:36:56,720 --> 29:37:01,040 sometimes we want to do much more than 39968 29:36:58,800 --> 29:37:03,040 this for example if we want to call 39969 29:37:01,039 --> 29:37:04,871 non-polymorphic functions this is not 39970 29:37:03,039 --> 29:37:07,279 going to work because the best pointer 39971 29:37:04,872 --> 29:37:09,911 has no knowledge of those functions so 39972 29:37:07,279 --> 29:37:12,319 if we need to do that explicitly 39973 29:37:09,911 --> 29:37:14,551 transforming from a base pointer to a 39974 29:37:12,320 --> 29:37:17,512 derived pointer is going to give us that 39975 29:37:14,551 --> 29:37:20,551 capability if the actual object we are 39976 29:37:17,512 --> 29:37:22,551 pointing at happens to have that method 39977 29:37:20,551 --> 29:37:24,231 okay so this is what we are after in 39978 29:37:22,551 --> 29:37:27,360 this lecture we want to be able to 39979 29:37:24,232 --> 29:37:30,080 transform from a base class pointer 39980 29:37:27,360 --> 29:37:32,639 or reference to a derived class pointer 39981 29:37:30,080 --> 29:37:34,872 or reference at runtime another way you 39982 29:37:32,639 --> 29:37:37,759 can text this is to think of the base 39983 29:37:34,872 --> 29:37:40,232 class pointer or reference as input and 39984 29:37:37,759 --> 29:37:43,191 we want to get a derived class pointer 39985 29:37:40,232 --> 29:37:45,592 or reference as output in our c plus 39986 29:37:43,191 --> 29:37:47,551 plus program and the main benefit of 39987 29:37:45,592 --> 29:37:50,400 this is to be able to call 39988 29:37:47,551 --> 29:37:52,551 non-polymorphic methods on derived 39989 29:37:50,399 --> 29:37:55,511 pointers or references this is going to 39990 29:37:52,551 --> 29:37:58,399 be super cool in some cases okay so 39991 29:37:55,512 --> 29:38:00,400 let's look at our inheritance hierarchy 39992 29:37:58,399 --> 29:38:02,551 here the top class is going to be animal 39993 29:38:00,399 --> 29:38:04,959 as you see here it's going to have a 39994 29:38:02,551 --> 29:38:06,959 single virtual method it's going to also 39995 29:38:04,960 --> 29:38:09,360 have a virtual disruptor because we know 39996 29:38:06,960 --> 29:38:11,680 how to do this now we can derive from 39997 29:38:09,360 --> 29:38:12,872 this and create our filling class which 39998 29:38:12,872 --> 29:38:17,192 inheriting publicly it's going to have 39999 29:38:15,039 --> 29:38:19,831 its own run method and it's going to 40000 29:38:17,191 --> 29:38:22,159 declare its own other method which is 40001 29:38:19,831 --> 29:38:25,191 not virtual we can also derive from 40002 29:38:22,160 --> 29:38:27,440 feline and create a dog class this is 40003 29:38:25,191 --> 29:38:29,759 how we do this and we're going to set up 40004 29:38:27,440 --> 29:38:31,760 our own virtual method in dog which is 40005 29:38:29,759 --> 29:38:34,080 going to be bark and we can really do 40006 29:38:31,759 --> 29:38:35,759 anything we want with this inheritance 40007 29:38:34,080 --> 29:38:38,160 hierarchy okay what we're going to be 40008 29:38:35,759 --> 29:38:40,479 using as input is a base class pointer 40009 29:38:38,160 --> 29:38:43,512 or a base class reference and we set 40010 29:38:40,479 --> 29:38:45,759 them up here this is our base pointer 40011 29:38:43,512 --> 29:38:48,720 this is our base reference and we want 40012 29:38:45,759 --> 29:38:51,279 to go from these and create a derived 40013 29:38:48,720 --> 29:38:52,960 class pointer or reference that we can 40014 29:38:52,960 --> 29:38:58,232 non-polymorphic functions and we can 40015 29:38:55,360 --> 29:38:59,911 achieve that using dynamic casts here 40016 29:38:58,232 --> 29:39:02,639 we're going to show you how you can cast 40017 29:38:59,911 --> 29:39:06,551 pointers again our animal base pointer 40018 29:39:02,639 --> 29:39:08,639 is going to act as our input and the 40019 29:39:06,551 --> 29:39:11,119 output is going to be what we want to 40020 29:39:08,639 --> 29:39:13,759 get out of this transformation 40021 29:39:11,119 --> 29:39:17,191 and all we do is say dynamic cast we 40022 29:39:13,759 --> 29:39:18,871 specify a pair of angle brackets inside 40023 29:39:17,191 --> 29:39:21,191 we're going to specify the output we 40024 29:39:18,872 --> 29:39:24,000 want to get and this is usually going to 40025 29:39:21,191 --> 29:39:25,911 be a derived class pointer so the base 40026 29:39:24,000 --> 29:39:28,000 class pointer is going to be within this 40027 29:39:25,911 --> 29:39:29,831 parenthesis and and it's going to be our 40028 29:39:28,000 --> 29:39:31,679 output and this is going to do the 40029 29:39:29,831 --> 29:39:34,231 transformation and it's going to give us 40030 29:39:31,679 --> 29:39:37,439 a result that we're going to be storing 40031 29:39:34,232 --> 29:39:41,192 in our feline pointer here and if this 40032 29:39:37,440 --> 29:39:44,040 succeeds we will have a valid feline 40033 29:39:41,191 --> 29:39:47,440 pointer here and we can use it to call 40034 29:39:44,039 --> 29:39:49,439 non-polymorphic or non-virtual functions 40035 29:39:47,440 --> 29:39:51,512 like we are doing here and this is super 40036 29:39:49,440 --> 29:39:53,911 cool another thing you can do with this 40037 29:39:51,512 --> 29:39:56,479 is use the result of this transformation 40038 29:39:53,911 --> 29:39:58,800 in a pointer check for example we can 40039 29:39:56,479 --> 29:40:01,039 check and see if this pointer is not a 40040 29:39:58,800 --> 29:40:03,592 null pointer if it's not a null pointer 40041 29:40:01,039 --> 29:40:06,399 we can call our non-polymorphic function 40042 29:40:03,592 --> 29:40:08,960 to it the result was successful but if 40043 29:40:06,399 --> 29:40:11,191 it's no pointer we're going to fail and 40044 29:40:08,960 --> 29:40:14,400 fall down here and we can say that we 40045 29:40:11,191 --> 29:40:16,231 couldn't cast to a feeling pointer and 40046 29:40:16,232 --> 29:40:22,872 how can this transformation fail well if 40047 29:40:19,440 --> 29:40:25,191 we go back to our code here we have an 40048 29:40:22,872 --> 29:40:28,551 animal base pointer which is really 40049 29:40:25,191 --> 29:40:31,360 pointing to a feline object in memory 40050 29:40:28,551 --> 29:40:32,720 if we try and cast this animal pointer 40051 29:40:32,720 --> 29:40:37,119 suppose we want to do this kind of 40052 29:40:34,831 --> 29:40:38,799 transformation try to think about this 40053 29:40:37,119 --> 29:40:41,039 this is not going to work because we 40054 29:40:38,800 --> 29:40:43,512 really have no dog information in this 40055 29:40:41,039 --> 29:40:45,911 villain object and if we try to cast 40056 29:40:43,512 --> 29:40:47,680 this to dog the compiler will really 40057 29:40:45,911 --> 29:40:50,639 have no way to do this and the 40058 29:40:47,679 --> 29:40:53,911 transformation through dynamic cast will 40059 29:40:50,639 --> 29:40:55,279 fail and we will say the message here 40060 29:40:53,911 --> 29:40:58,080 meaning that we couldn't do the 40061 29:40:55,279 --> 29:41:00,232 transformation the user asked for so 40062 29:40:58,080 --> 29:41:02,720 here we can do the transformation and 40063 29:41:00,232 --> 29:41:04,960 transform from a base pointer to a 40064 29:41:02,720 --> 29:41:07,592 derived pointer but we can also 40065 29:41:04,960 --> 29:41:09,592 transform to a derived reference and i 40066 29:41:07,592 --> 29:41:11,592 am going to show you the syntax to do 40067 29:41:09,592 --> 29:41:14,552 that and it is super simple we say 40068 29:41:11,592 --> 29:41:15,680 dynamic cast and we specify our input 40069 29:41:15,679 --> 29:41:20,639 notice that this is a reference you have 40070 29:41:17,440 --> 29:41:23,680 to put it it's not a pointer and you 40071 29:41:20,639 --> 29:41:26,000 specify the output within this angle 40072 29:41:23,679 --> 29:41:28,231 brackets here once you do this the 40073 29:41:26,000 --> 29:41:30,399 output of this is going to be a derived 40074 29:41:28,232 --> 29:41:33,760 reference and we're going to store this 40075 29:41:30,399 --> 29:41:35,191 in our feline reference here once we do 40076 29:41:35,191 --> 29:41:39,119 transformation is successful we're going 40077 29:41:36,960 --> 29:41:41,512 to be able to call our non-polymorphic 40078 29:41:39,119 --> 29:41:43,679 function this is going to work but with 40079 29:41:41,512 --> 29:41:45,512 references we don't have the luxury to 40080 29:41:43,679 --> 29:41:47,911 do something like a pointer check 40081 29:41:45,512 --> 29:41:50,400 because there is no such thing as a null 40082 29:41:47,911 --> 29:41:51,759 reference a reference is always pointing 40083 29:41:51,759 --> 29:41:56,720 and if the transformation fails we will 40084 29:41:54,080 --> 29:41:58,720 have some kind of invalid reference and 40085 29:41:56,720 --> 29:42:01,360 if this fails it is probably going to 40086 29:41:58,720 --> 29:42:03,831 blow up and give us undefined behavior 40087 29:42:01,360 --> 29:42:06,399 so you don't really want to do this i 40088 29:42:06,399 --> 29:42:10,799 doing this kind of transformation with 40089 29:42:08,551 --> 29:42:12,959 references unless you know 40090 29:42:10,800 --> 29:42:15,760 that the transformation is possible 40091 29:42:12,960 --> 29:42:16,800 beforehand what i usually recommend is 40092 29:42:16,800 --> 29:42:21,360 pass through pointers and it is even 40093 29:42:18,960 --> 29:42:24,552 possible to transform a base reference 40094 29:42:21,360 --> 29:42:27,191 into a derived pointer and the syntax to 40095 29:42:24,551 --> 29:42:30,479 do that is what you see here okay and uh 40096 29:42:27,191 --> 29:42:32,399 once we do a transformation to a pointer 40097 29:42:30,479 --> 29:42:34,080 if the transformation is successful 40098 29:42:32,399 --> 29:42:36,399 we're going to have a valid pointer in 40099 29:42:34,080 --> 29:42:38,400 here if the transformation fails we're 40100 29:42:36,399 --> 29:42:40,959 going to have a null pointer and the 40101 29:42:38,399 --> 29:42:43,279 check is going to draw us here and we're 40102 29:42:40,960 --> 29:42:45,680 going to print this message to say that 40103 29:42:43,279 --> 29:42:48,551 we can't do the transformation here okay 40104 29:42:45,679 --> 29:42:50,719 again the cast may fail if it fails 40105 29:42:48,551 --> 29:42:52,800 while we are trying to transform it to a 40106 29:42:50,720 --> 29:42:55,592 dog pointer we are going to fall down 40107 29:42:52,800 --> 29:42:56,960 here and say that the cast failed we can 40108 29:42:55,592 --> 29:42:59,120 also do the same thing through 40109 29:42:56,960 --> 29:43:01,120 references for example if we want to 40110 29:42:59,119 --> 29:43:02,871 transform this base reference to a 40111 29:43:02,872 --> 29:43:06,800 and this fails we're going to follow 40112 29:43:04,720 --> 29:43:08,720 here because of the check we are doing 40113 29:43:06,800 --> 29:43:11,512 here we can do something like this to 40114 29:43:08,720 --> 29:43:13,191 make our code much safer to work with 40115 29:43:11,512 --> 29:43:15,440 and another thing i should really point 40116 29:43:15,440 --> 29:43:20,720 casting like this is usually done in 40117 29:43:17,911 --> 29:43:22,831 functions where you are passing a base 40118 29:43:20,720 --> 29:43:25,831 pointer or a base reference as a 40119 29:43:22,831 --> 29:43:28,551 parameter and in this function you 40120 29:43:25,831 --> 29:43:30,479 somehow need to call a non-polymorphic 40121 29:43:28,551 --> 29:43:32,231 function so you might do something like 40122 29:43:30,479 --> 29:43:34,639 this and take the pointer and turn that 40123 29:43:32,232 --> 29:43:36,080 into a derived pointer and if the 40124 29:43:34,639 --> 29:43:37,911 transformation is successful you're 40125 29:43:36,080 --> 29:43:39,680 going to call your method and if the 40126 29:43:37,911 --> 29:43:42,000 check fails you're going to follow here 40127 29:43:39,679 --> 29:43:44,319 and print an error message you can do 40128 29:43:42,000 --> 29:43:46,720 something like this with pointers and we 40129 29:43:44,320 --> 29:43:48,872 can do exactly the same thing with 40130 29:43:46,720 --> 29:43:51,119 references but here because we want to 40131 29:43:48,872 --> 29:43:53,512 do this check we're going to transform 40132 29:43:51,119 --> 29:43:55,911 our base reference into a derived 40133 29:43:53,512 --> 29:43:58,080 pointer and call our method on the 40134 29:43:55,911 --> 29:44:00,232 derived pointer instead this is going to 40135 29:43:58,080 --> 29:44:03,119 give us the ability to do this kind of 40136 29:44:00,232 --> 29:44:05,360 check to make our code much safer to 40137 29:44:03,119 --> 29:44:08,399 work with a word of caution here 40138 29:44:05,360 --> 29:44:10,872 overusing down casting is a sign of bad 40139 29:44:08,399 --> 29:44:12,639 design if you find yourself doing this 40140 29:44:10,872 --> 29:44:15,192 kind of thing a lot to call 40141 29:44:12,639 --> 29:44:16,960 non-polymorphic function that's maybe a 40142 29:44:15,191 --> 29:44:18,959 sign that you should make that 40143 29:44:18,960 --> 29:44:22,400 polymorphic because that's going to save 40144 29:44:22,399 --> 29:44:27,911 transformations a lot just make sure you 40145 29:44:24,720 --> 29:44:30,160 don't overuse this and do it in a few 40146 29:44:27,911 --> 29:44:31,679 cases where you really need to do this 40147 29:44:30,160 --> 29:44:33,040 kind of thing now that you know this 40148 29:44:31,679 --> 29:44:34,871 we're going to head over to visual 40149 29:44:33,039 --> 29:44:37,679 studio code and play with us a little 40150 29:44:34,872 --> 29:44:40,080 more here we are in our working folder 40151 29:44:37,679 --> 29:44:43,191 the current project is dynamic casts 40152 29:44:40,080 --> 29:44:44,639 we're going to grab our template files 40153 29:44:43,191 --> 29:44:46,871 and we're going to put them in place 40154 29:44:44,639 --> 29:44:49,440 let's do that and we are going to grab 40155 29:44:46,872 --> 29:44:51,592 the files that make up our inheritance 40156 29:44:49,440 --> 29:44:54,720 hierarchy we're going to put those in we 40157 29:44:51,592 --> 29:44:56,872 have animal dog and feline and this is 40158 29:44:54,720 --> 29:44:59,191 going to make up our inheritance 40159 29:44:56,872 --> 29:45:00,872 hierarchy here once we have this in 40160 29:44:59,191 --> 29:45:02,720 place we can open this up in visual 40161 29:45:00,872 --> 29:45:04,960 studio code by dragging and dropping 40162 29:45:02,720 --> 29:45:07,759 here and this is going to give us our 40163 29:45:04,960 --> 29:45:10,080 files okay we have our files in here we 40164 29:45:07,759 --> 29:45:12,319 can open up the animal class which is 40165 29:45:10,080 --> 29:45:14,000 our base class we can see that it is 40166 29:45:12,320 --> 29:45:16,160 really simple it's just going to have 40167 29:45:14,000 --> 29:45:18,399 one virtual method which is brief it's 40168 29:45:16,160 --> 29:45:20,720 going to have a single member variable 40169 29:45:18,399 --> 29:45:22,720 which is description here we can derive 40170 29:45:20,720 --> 29:45:24,960 from this and create our feline class 40171 29:45:22,720 --> 29:45:28,320 which is going to inherit publicly from 40172 29:45:24,960 --> 29:45:30,400 animal feline is going to set up its own 40173 29:45:30,399 --> 29:45:34,799 and we will have a virtual method called 40174 29:45:32,399 --> 29:45:37,191 run we can override in downstream 40175 29:45:34,800 --> 29:45:39,680 classes if we want to do that and let's 40176 29:45:37,191 --> 29:45:41,679 look at the implementation for feline 40177 29:45:39,679 --> 29:45:43,359 cpp if you want to do that you can take 40178 29:45:41,679 --> 29:45:46,479 a look it's just going to forward the 40179 29:45:43,360 --> 29:45:48,551 construction to the base class to build 40180 29:45:48,551 --> 29:45:52,399 let's look at dog dog is going to 40181 29:45:50,399 --> 29:45:55,039 inherit from feline and it is going to 40182 29:45:52,399 --> 29:45:56,799 set up a simple virtual function called 40183 29:45:55,039 --> 29:45:58,479 work and it's going to have a virtual 40184 29:45:56,800 --> 29:45:59,760 destructor now we know that we should 40185 29:45:59,759 --> 29:46:04,959 virtual disruptors if we happen to even 40186 29:46:02,232 --> 29:46:06,800 have a single virtual method in our 40187 29:46:04,960 --> 29:46:09,680 inheritance hierarchy because that's 40188 29:46:06,800 --> 29:46:12,720 going to make events easier now we want 40189 29:46:09,679 --> 29:46:14,871 to go to the main cpp file and play with 40190 29:46:12,720 --> 29:46:17,040 dynamic casts we're going to remove 40191 29:46:14,872 --> 29:46:19,440 whatever it is we don't need here and 40192 29:46:17,039 --> 29:46:22,399 that we can include our most derived 40193 29:46:19,440 --> 29:46:24,720 class let's include doug here this is 40194 29:46:24,720 --> 29:46:30,000 both feline and animal because this 40195 29:46:27,592 --> 29:46:32,160 header is going to include its parent 40196 29:46:30,000 --> 29:46:34,872 and its parent is going to include its 40197 29:46:32,160 --> 29:46:37,832 own parent this is the logic here we are 40198 29:46:34,872 --> 29:46:39,680 going to start out by using a simple 40199 29:46:37,831 --> 29:46:42,959 example here and show you that you can't 40200 29:46:39,679 --> 29:46:45,439 really call non-polymorphic functions 40201 29:46:42,960 --> 29:46:48,400 from a base pointer that is managing the 40202 29:46:45,440 --> 29:46:51,119 derived object and here we have an 40203 29:46:48,399 --> 29:46:54,799 animal pointer it is a base pointer and 40204 29:46:51,119 --> 29:46:57,279 it is managing a real feline object in 40205 29:46:54,800 --> 29:46:59,512 memory but if we try and call a 40206 29:46:57,279 --> 29:47:01,911 non-virtual function through this base 40207 29:46:59,512 --> 29:47:04,320 pointer this is going to blow up this is 40208 29:47:04,320 --> 29:47:08,800 we can't really call a non-polymorphic 40209 29:47:06,479 --> 29:47:10,800 function through a base pointer because 40210 29:47:08,800 --> 29:47:12,800 that's not a virtual function the 40211 29:47:10,800 --> 29:47:15,760 requirement to be able to do this is for 40212 29:47:12,800 --> 29:47:18,639 the function to be virtual and do some 40213 29:47:15,759 --> 29:47:20,959 feeling thingy is not a virtual function 40214 29:47:18,639 --> 29:47:22,960 that we know at the base level so this 40215 29:47:20,960 --> 29:47:24,552 is not going to work by default and if 40216 29:47:22,960 --> 29:47:26,080 we're trying to build code like this 40217 29:47:24,551 --> 29:47:28,872 this is going to give us a compiler 40218 29:47:26,080 --> 29:47:31,191 error let's pass this through gcc we're 40219 29:47:28,872 --> 29:47:33,192 going to do that we are going to finish 40220 29:47:31,191 --> 29:47:35,512 the world with errors and if we go up 40221 29:47:33,191 --> 29:47:38,000 we're going to find that class animal 40222 29:47:35,512 --> 29:47:39,911 has no member name to do some feline 40223 29:47:38,000 --> 29:47:42,232 thingy so this is the compiler error 40224 29:47:39,911 --> 29:47:44,232 here this is not going to work if we 40225 29:47:42,232 --> 29:47:47,192 need to do something like this one 40226 29:47:44,232 --> 29:47:48,960 solution might be to do dynamic casts 40227 29:47:47,191 --> 29:47:51,039 that we are learning about in this 40228 29:47:48,960 --> 29:47:53,832 lecture here and the syntax is 40229 29:47:51,039 --> 29:47:56,399 ridiculously simple so we can go down 40230 29:47:53,831 --> 29:47:58,720 here and put in a simple separator to be 40231 29:47:56,399 --> 29:48:00,959 able to follow this lens on our 40232 29:47:58,720 --> 29:48:03,440 powershell window you can do this if you 40233 29:48:00,960 --> 29:48:06,552 want and we're going to do our dynamic 40234 29:48:03,440 --> 29:48:09,360 cast for example if we wanted to do a 40235 29:48:06,551 --> 29:48:12,319 transformation and transform this to a 40236 29:48:09,360 --> 29:48:14,160 dark object this would give us a problem 40237 29:48:12,320 --> 29:48:16,720 and let's try and look at this again we 40238 29:48:14,160 --> 29:48:19,192 have a feline object here and we are 40239 29:48:16,720 --> 29:48:21,279 managing this through a base pointer and 40240 29:48:19,191 --> 29:48:23,679 what we want to do is to take this space 40241 29:48:23,679 --> 29:48:28,079 a dog object if you look at this 40242 29:48:26,232 --> 29:48:30,400 remember that we don't really have any 40243 29:48:28,080 --> 29:48:32,479 dog information in our feline object 40244 29:48:30,399 --> 29:48:35,191 this is going to just go up to the level 40245 29:48:32,479 --> 29:48:37,591 of feline but we want to transform this 40246 29:48:35,191 --> 29:48:40,551 to a dog to make this super clear let's 40247 29:48:37,592 --> 29:48:43,279 look at our inheritance hierarchy again 40248 29:48:40,551 --> 29:48:45,360 what we have is a feline object and the 40249 29:48:43,279 --> 29:48:46,399 information we have really goes up to 40250 29:48:46,399 --> 29:48:50,799 and we are managing our feeling object 40251 29:48:48,551 --> 29:48:52,639 through a base pointer this is the setup 40252 29:48:50,800 --> 29:48:54,720 we have but we want to do the 40253 29:48:52,639 --> 29:48:57,831 transformation and turn this base 40254 29:48:54,720 --> 29:49:00,080 pointer into a dog object and this is 40255 29:48:57,831 --> 29:49:02,639 going to blow up especially if we have 40256 29:49:00,080 --> 29:49:04,232 any dog specific member variables this 40257 29:49:02,639 --> 29:49:06,960 is going to blow up this is not going to 40258 29:49:04,232 --> 29:49:07,832 work if we try to access dog information 40259 29:49:07,831 --> 29:49:11,911 what we get through the transformation 40260 29:49:09,831 --> 29:49:13,439 to animal to dog we're going to get a 40261 29:49:11,911 --> 29:49:15,279 crush because we have no such 40262 29:49:13,440 --> 29:49:17,592 information and we're going to look at a 40263 29:49:15,279 --> 29:49:19,592 simple example to really drive this home 40264 29:49:17,592 --> 29:49:21,592 so what we're going to do is go back to 40265 29:49:19,592 --> 29:49:23,440 our dog class and we're going to put in 40266 29:49:21,592 --> 29:49:26,000 a sample member variable we're going to 40267 29:49:23,440 --> 29:49:28,400 make it private and for example we can 40268 29:49:26,000 --> 29:49:30,399 make this a member variable to keep 40269 29:49:28,399 --> 29:49:32,551 track of the speed for the dock we can 40270 29:49:30,399 --> 29:49:34,399 do this and we're going to initialize 40271 29:49:32,551 --> 29:49:36,800 this to 0 by default and we're going to 40272 29:49:34,399 --> 29:49:39,511 set up a function which is going to 40273 29:49:36,800 --> 29:49:42,080 print some dog information let's go back 40274 29:49:39,512 --> 29:49:44,639 to feline and copy the do some feline 40275 29:49:42,080 --> 29:49:47,119 thingy method and we're going to do some 40276 29:49:44,639 --> 29:49:48,960 dog thingy that's what we're going to do 40277 29:49:47,119 --> 29:49:51,039 here i am going to put in this method 40278 29:49:48,960 --> 29:49:53,760 and adapt it a little bit we're going to 40279 29:49:51,039 --> 29:49:56,551 do some dog thingy and we're going to 40280 29:49:53,759 --> 29:49:58,551 account for that in the body but we also 40281 29:49:56,551 --> 29:50:00,639 want to print the speed for this dog 40282 29:49:58,551 --> 29:50:03,039 here so we're going to say speed and 40283 29:50:00,639 --> 29:50:05,039 we're going to print this out suppose we 40284 29:50:03,039 --> 29:50:07,831 have this kind of setup in our 40285 29:50:05,039 --> 29:50:10,079 inheritance hierarchy here a dark object 40286 29:50:07,831 --> 29:50:12,080 has some doubt specific information it 40287 29:50:10,080 --> 29:50:14,400 has an additional member variable which 40288 29:50:12,080 --> 29:50:16,320 is m speed here we won't worry about 40289 29:50:14,399 --> 29:50:17,911 initializing this from the constructor 40290 29:50:16,320 --> 29:50:21,360 you can do that if you want all we 40291 29:50:17,911 --> 29:50:23,360 really want is to add some extra data at 40292 29:50:21,360 --> 29:50:25,759 the dog level and we're going to see 40293 29:50:23,360 --> 29:50:27,911 that if we try to transform to doug this 40294 29:50:25,759 --> 29:50:30,720 is going to give us a few problems so 40295 29:50:27,911 --> 29:50:33,279 what we want is to turn this animal base 40296 29:50:30,720 --> 29:50:34,399 pointer into a dark object let's do this 40297 29:50:34,399 --> 29:50:39,679 change this to dog and the result object 40298 29:50:37,279 --> 29:50:41,759 we get is going to be a dog pointer and 40299 29:50:43,911 --> 29:50:48,551 method on the pointer we get as a result 40300 29:50:46,479 --> 29:50:50,159 of this transformation here let's see 40301 29:50:48,551 --> 29:50:51,679 what happens you're going to see that 40302 29:50:50,160 --> 29:50:53,760 this is not going to give us any 40303 29:50:51,679 --> 29:50:56,319 indication that we are doing something 40304 29:50:53,759 --> 29:50:59,191 wrong but if the transformation here 40305 29:50:56,320 --> 29:51:01,912 fails this is going to silently fail and 40306 29:50:59,191 --> 29:51:04,000 store junk data in our feline pointer 40307 29:51:01,911 --> 29:51:07,191 here and here we're going to try and 40308 29:51:04,000 --> 29:51:10,000 call a method that is going to possibly 40309 29:51:07,191 --> 29:51:12,231 use some dog stuff on the feline object 40310 29:51:10,000 --> 29:51:14,320 this is going to blow up again we don't 40311 29:51:12,232 --> 29:51:16,639 have dog information in this feline 40312 29:51:14,320 --> 29:51:18,552 object and the do some dog thinking 40313 29:51:16,639 --> 29:51:21,039 method is going to try and print the 40314 29:51:18,551 --> 29:51:23,360 speed of the dog that we don't really 40315 29:51:21,039 --> 29:51:26,159 have we don't have any dog information 40316 29:51:23,360 --> 29:51:28,232 so we have no business accessing dog 40317 29:51:26,160 --> 29:51:30,160 information from this object to start 40318 29:51:28,232 --> 29:51:32,160 with so this is going to blow up and 40319 29:51:30,160 --> 29:51:34,080 it's probably going to give us a crash 40320 29:51:32,160 --> 29:51:35,760 let's try and build and see what happens 40321 29:51:34,080 --> 29:51:37,759 we're going to pass this through gcc 40322 29:51:35,759 --> 29:51:40,000 like we always do the boat is going to 40323 29:51:37,759 --> 29:51:43,360 go through you see that this is going to 40324 29:51:40,000 --> 29:51:45,440 even blow up at run time when users are 40325 29:51:43,360 --> 29:51:48,080 actually using your application this is 40326 29:51:45,440 --> 29:51:50,080 super dangerous we're going to run this 40327 29:51:48,080 --> 29:51:51,911 and see what happens you see doing some 40328 29:51:50,080 --> 29:51:54,160 dug thingy and it's going to try and 40329 29:51:51,911 --> 29:51:56,232 print the speed the compiler is really 40330 29:51:54,160 --> 29:51:59,440 trying its best but this is really going 40331 29:51:56,232 --> 29:52:02,480 to fail and we are crashing our program 40332 29:51:59,440 --> 29:52:05,119 if you go down here and say done to make 40333 29:52:02,479 --> 29:52:07,191 sure we know when the program is done 40334 29:52:05,119 --> 29:52:09,279 and we try and run our program again 40335 29:52:07,191 --> 29:52:11,679 let's pass this through gcc to take into 40336 29:52:09,279 --> 29:52:12,959 account this thing we have on line 20. 40337 29:52:11,679 --> 29:52:14,871 the book is going to be good you're 40338 29:52:12,960 --> 29:52:16,800 going to see that the computer is going 40339 29:52:14,872 --> 29:52:18,320 to try and really run this but it's 40340 29:52:16,800 --> 29:52:20,872 going to see that it doesn't really have 40341 29:52:18,320 --> 29:52:22,552 any dog information and the computer is 40342 29:52:20,872 --> 29:52:24,800 not going to succeed because we are 40343 29:52:22,551 --> 29:52:27,831 trying to access this dub specific 40344 29:52:24,800 --> 29:52:29,592 information really on a feline object so 40345 29:52:27,831 --> 29:52:30,399 we are going to get a crash again the 40346 29:52:30,399 --> 29:52:35,911 is that this dynamic casts can fail 40347 29:52:33,592 --> 29:52:38,800 and we need a better way to do some kind 40348 29:52:35,911 --> 29:52:41,360 of checks before we call our methods 40349 29:52:38,800 --> 29:52:43,192 here and we can do this by just using 40350 29:52:41,360 --> 29:52:45,512 the pointer we get from this 40351 29:52:43,191 --> 29:52:48,080 transformation here so we can say if 40352 29:52:45,512 --> 29:52:50,639 feline pointer and check and see if we 40353 29:52:48,080 --> 29:52:52,551 have some valid data in this villain 40354 29:52:50,639 --> 29:52:54,720 pointer and if that's the case we're 40355 29:52:52,551 --> 29:52:57,191 going to do whatever we want to do with 40356 29:52:54,720 --> 29:52:58,720 this pointer here okay so this is 40357 29:52:58,720 --> 29:53:03,512 and if the transformation fails we're 40358 29:53:01,191 --> 29:53:05,759 going to fall in this else block and we 40359 29:53:03,512 --> 29:53:08,479 can put some nice message here instead 40360 29:53:05,759 --> 29:53:11,191 of letting the user crash at runtime 40361 29:53:08,479 --> 29:53:13,119 this is much better in my opinion so 40362 29:53:11,191 --> 29:53:16,319 we're going to say couldn't do the 40363 29:53:13,119 --> 29:53:18,319 transformation from animal pointer to 40364 29:53:16,320 --> 29:53:20,640 dog pointer because we really have no 40365 29:53:18,320 --> 29:53:22,552 dog information and this base pointer 40366 29:53:20,639 --> 29:53:24,479 that we have here this is something we 40367 29:53:22,551 --> 29:53:27,679 can do so we're going to say sddc out 40368 29:53:24,479 --> 29:53:30,159 i'm going to say stdendl here and we're 40369 29:53:27,679 --> 29:53:31,511 going to see what happens let's build 40370 29:53:30,160 --> 29:53:33,440 hopefully we're not going to get the 40371 29:53:31,512 --> 29:53:35,832 crash because we are doing something 40372 29:53:33,440 --> 29:53:37,832 smarter now we are checking to see 40373 29:53:35,831 --> 29:53:40,479 if we have any dog information in this 40374 29:53:37,831 --> 29:53:42,720 pointer before we call some doubt 40375 29:53:40,479 --> 29:53:44,872 specific method on this pointer here 40376 29:53:42,720 --> 29:53:47,440 this is the beauty of this we're going 40377 29:53:44,872 --> 29:53:49,120 to clear and run rooster now you see 40378 29:53:47,440 --> 29:53:52,232 couldn't do the transformation from 40379 29:53:49,119 --> 29:53:54,959 animal to dog and our program is no 40380 29:53:52,232 --> 29:53:56,872 longer crashing it is ending properly 40381 29:53:54,960 --> 29:53:59,040 and this is much better this is what you 40382 29:53:56,872 --> 29:54:01,360 should aim for so again the 40383 29:53:59,039 --> 29:54:02,159 transformations here may fail 40384 29:54:02,160 --> 29:54:07,120 if we turn this back to doing v-line 40385 29:54:04,800 --> 29:54:08,639 transformation let's do feline pointer 40386 29:54:07,119 --> 29:54:11,512 we're going to change this to fill in 40387 29:54:08,639 --> 29:54:13,759 pointer again and we're going to do some 40388 29:54:11,512 --> 29:54:15,440 feline thing now we're going to bolt 40389 29:54:15,440 --> 29:54:18,800 we're going to bolt again and this is 40390 29:54:16,960 --> 29:54:20,872 going to build successfully and it is 40391 29:54:18,800 --> 29:54:23,360 going to probably do some filament 40392 29:54:20,872 --> 29:54:25,279 thingy the build is good we can clear 40393 29:54:23,360 --> 29:54:27,191 and run rooster this is going to work 40394 29:54:25,279 --> 29:54:30,160 just fine and our program is going to 40395 29:54:27,191 --> 29:54:32,159 end properly so here we just saw that we 40396 29:54:30,160 --> 29:54:35,040 could do a transformation from a base 40397 29:54:32,160 --> 29:54:37,279 pointer to a derived pointer and this is 40398 29:54:35,039 --> 29:54:40,000 going to work just fine we can also do 40399 29:54:37,279 --> 29:54:42,479 the transformation for references let's 40400 29:54:40,000 --> 29:54:44,479 say that here and we're going to go down 40401 29:54:42,479 --> 29:54:46,231 and put in a simple example to drive 40402 29:54:44,479 --> 29:54:48,319 this home we're going to have a feline 40403 29:54:46,232 --> 29:54:51,040 object we're going to set up a reference 40404 29:54:48,320 --> 29:54:52,960 to it and if we try and call a 40405 29:54:51,039 --> 29:54:55,039 non-virtual method on this base 40406 29:54:52,960 --> 29:54:56,232 reference this is going to blow up 40407 29:54:56,232 --> 29:55:01,279 the base class has no information about 40408 29:54:58,479 --> 29:55:03,119 this non-virtual method that we declare 40409 29:55:01,279 --> 29:55:05,512 at the feline level this is going to 40410 29:55:03,119 --> 29:55:06,639 blow up if we try to build our program 40411 29:55:05,512 --> 29:55:09,832 we're going to see that we get a 40412 29:55:06,639 --> 29:55:10,720 compiler error saying no such method 40413 29:55:10,720 --> 29:55:15,119 do some feeling thing we're going to see 40414 29:55:15,119 --> 29:55:20,799 this way at the animal level 40415 29:55:18,551 --> 29:55:24,080 this is the compiler error we get if we 40416 29:55:20,800 --> 29:55:26,320 try to call a non-virtual method through 40417 29:55:24,080 --> 29:55:29,440 a base reference that we have here this 40418 29:55:26,320 --> 29:55:32,320 is not going to work but we can also do 40419 29:55:29,440 --> 29:55:33,680 a dynamic cast and transform from a base 40420 29:55:33,679 --> 29:55:38,079 a derived reference and the syntax to do 40421 29:55:35,911 --> 29:55:39,039 that is really here we're going to set 40422 29:55:39,039 --> 29:55:43,119 derived reference in this case it's 40423 29:55:41,039 --> 29:55:45,191 going to be a feedline reference we're 40424 29:55:43,119 --> 29:55:47,759 going to initialize this with a dynamic 40425 29:55:45,191 --> 29:55:49,512 cast transformation and the result of 40426 29:55:47,759 --> 29:55:51,831 the cast is going to be used to 40427 29:55:49,512 --> 29:55:53,512 initialize this reference here and we're 40428 29:55:53,512 --> 29:55:58,160 a base reference and turning that into a 40429 29:55:56,399 --> 29:56:00,720 derived reference here this is the 40430 29:55:58,160 --> 29:56:03,440 syntax the output you desire is going to 40431 29:56:00,720 --> 29:56:05,279 be within this angle brackets and the 40432 29:56:03,440 --> 29:56:07,680 input is going to be through this 40433 29:56:05,279 --> 29:56:10,000 parenthesis here if this succeeds we 40434 29:56:07,679 --> 29:56:12,000 will be able to call our method here and 40435 29:56:10,000 --> 29:56:13,512 this is going to work just fine let's 40436 29:56:13,512 --> 29:56:18,872 we are going to put in a separator to be 40437 29:56:16,479 --> 29:56:22,872 able to follow these things let's put 40438 29:56:18,872 --> 29:56:24,400 that at line 24 here this is going to do 40439 29:56:22,872 --> 29:56:26,320 and we're going to try and build our 40440 29:56:24,399 --> 29:56:29,119 program we're going to pass this through 40441 29:56:26,320 --> 29:56:31,680 gcc the boat is good we can clear and 40442 29:56:29,119 --> 29:56:34,639 run rooster now we're going to see that 40443 29:56:31,679 --> 29:56:36,639 we are doing some filling thingy through 40444 29:56:34,639 --> 29:56:38,319 a reference and this is super cool we 40445 29:56:36,639 --> 29:56:40,960 can do this you can do this kind of 40446 29:56:38,320 --> 29:56:43,192 transformation here and at the end you 40447 29:56:40,960 --> 29:56:45,760 see that we are calling our destructors 40448 29:56:43,191 --> 29:56:48,319 on our objects and this is a good 40449 29:56:45,759 --> 29:56:50,720 reminder that we didn't properly release 40450 29:56:48,320 --> 29:56:51,760 the memory on this animal pointer that 40451 29:56:51,759 --> 29:56:56,551 so we are going to go down at the end of 40452 29:56:54,320 --> 29:56:58,872 the program and release that memory this 40453 29:56:56,551 --> 29:57:02,080 is good practice and hopefully you can 40454 29:56:58,872 --> 29:57:04,551 see how easy it is to forget to release 40455 29:57:02,080 --> 29:57:06,720 some memory that you allocate for 40456 29:57:04,551 --> 29:57:08,959 with the new operator this is a good 40457 29:57:06,720 --> 29:57:10,720 learning experience here but this is a 40458 29:57:08,960 --> 29:57:12,232 good learning experience here but we're 40459 29:57:10,720 --> 29:57:13,911 going to fix this problem we're going to 40460 29:57:12,232 --> 29:57:16,320 say animal one and we're going to 40461 29:57:13,911 --> 29:57:18,080 release the memory now if we run the 40462 29:57:16,320 --> 29:57:21,680 program we're going to see that we are 40463 29:57:18,080 --> 29:57:23,911 releasing two objects when the program 40464 29:57:21,679 --> 29:57:25,759 ends we're going to clear and run 40465 29:57:23,911 --> 29:57:28,720 rooster and the destructor is going to 40466 29:57:25,759 --> 29:57:30,871 be called two times once for this guy 40467 29:57:28,720 --> 29:57:33,680 and a second for this guy this is what 40468 29:57:30,872 --> 29:57:36,400 we want and you see that because we have 40469 29:57:33,679 --> 29:57:39,191 virtual destructors in our classes we 40470 29:57:36,399 --> 29:57:41,591 are destroying at each level so here we 40471 29:57:39,191 --> 29:57:43,759 are destroying at the feline level and 40472 29:57:41,592 --> 29:57:46,080 we are destroying at the animal level 40473 29:57:43,759 --> 29:57:48,399 and this is going to properly release 40474 29:57:46,080 --> 29:57:50,479 the memory from our inheritance 40475 29:57:48,399 --> 29:57:52,959 hierarchy here so now you know that you 40476 29:57:50,479 --> 29:57:54,959 can do a transformation of references 40477 29:57:54,960 --> 29:58:00,000 but with dynamic cast we have no way to 40478 29:57:57,592 --> 29:58:03,040 check and see if the transformation was 40479 29:58:00,000 --> 29:58:05,440 successful again if we try to turn this 40480 29:58:03,039 --> 29:58:07,511 into a dark reference we can do that and 40481 29:58:05,440 --> 29:58:10,551 we're going to say dog reference here 40482 29:58:07,512 --> 29:58:13,119 and we will try and call the do some 40483 29:58:10,551 --> 29:58:14,872 duck thingy method this is not going to 40484 29:58:13,119 --> 29:58:17,119 give us an indication of a problem but 40485 29:58:14,872 --> 29:58:18,800 this is going to crash at runtime 40486 29:58:17,119 --> 29:58:20,399 because we don't really have any dog 40487 29:58:20,399 --> 29:58:25,759 original feline object here so if you 40488 29:58:23,360 --> 29:58:28,080 try and transform this reference into a 40489 29:58:25,759 --> 29:58:29,911 dog reference this is obviously not 40490 29:58:28,080 --> 29:58:32,232 going to work because we have no dog 40491 29:58:29,911 --> 29:58:34,800 information in this to start with let's 40492 29:58:32,232 --> 29:58:37,120 boil and show you the problem the book 40493 29:58:34,800 --> 29:58:39,680 is going to be good so we can clear 40494 29:58:37,119 --> 29:58:41,831 and run rooster you're going to see that 40495 29:58:39,679 --> 29:58:44,319 we are going to terminate our program 40496 29:58:41,831 --> 29:58:46,231 with the band cast and this is going to 40497 29:58:44,320 --> 29:58:48,960 crush you see that we don't get to the 40498 29:58:46,232 --> 29:58:50,639 point where we print done and we are not 40499 29:58:48,960 --> 29:58:52,480 even going to get a chance to release 40500 29:58:50,639 --> 29:58:54,720 the memory that we have allocated in 40501 29:58:52,479 --> 29:58:56,872 this main function hopefully you can see 40502 29:58:54,720 --> 29:58:59,279 that this is really bad so with 40503 29:58:59,279 --> 29:59:04,800 to check and see if the transformation 40504 29:59:01,512 --> 29:59:07,279 was successful and this is a limitation 40505 29:59:04,800 --> 29:59:10,080 that's why in most cases i usually do my 40506 29:59:07,279 --> 29:59:12,000 transformations through pointers and 40507 29:59:10,080 --> 29:59:14,872 there is a way you can even turn this 40508 29:59:12,000 --> 29:59:16,960 base reference into a base pointer and i 40509 29:59:14,872 --> 29:59:19,512 am going to show you how you can do that 40510 29:59:16,960 --> 29:59:21,280 so we are going to go down here and try 40511 29:59:19,512 --> 29:59:23,440 to change things a little bit we are 40512 29:59:21,279 --> 29:59:25,592 going to say doing proper checks with 40513 29:59:23,440 --> 29:59:28,000 references and we're going to put in a 40514 29:59:25,592 --> 29:59:29,592 piece of code to play with us and what 40515 29:59:28,000 --> 29:59:30,551 we're going to do we're going to take 40516 29:59:30,551 --> 29:59:35,759 base reference and turn that into a base 40517 29:59:33,592 --> 29:59:38,400 pointer this is the syntax here again 40518 29:59:35,759 --> 29:59:40,639 the input is the base reference but the 40519 29:59:38,399 --> 29:59:43,279 output we desire is a base pointer and 40520 29:59:40,639 --> 29:59:45,440 this is possible in c plus plus this is 40521 29:59:43,279 --> 29:59:47,279 going to work just fine now we are going 40522 29:59:45,440 --> 29:59:49,760 to go on top and comment this 40523 29:59:47,279 --> 29:59:51,512 problematic transformation we just tried 40524 29:59:49,759 --> 29:59:53,911 to do and we're going to try and build 40525 29:59:51,512 --> 29:59:56,080 and see if this is actually working 40526 29:59:53,911 --> 29:59:58,399 if this transformation succeeds we're 40527 29:59:56,080 --> 30:00:00,160 going to do some villain thingy if it 40528 29:59:58,399 --> 30:00:02,079 fails we're going to say couldn't cast 40529 30:00:00,160 --> 30:00:04,232 to feline reference and we're going to 40530 30:00:02,080 --> 30:00:06,639 say that we are sorry this is a good 40531 30:00:04,232 --> 30:00:08,639 output message if we get something wrong 40532 30:00:06,639 --> 30:00:11,191 we're going to build this with gcc and 40533 30:00:08,639 --> 30:00:12,872 see if this actually works the world is 40534 30:00:12,872 --> 30:00:17,279 and run rooster you see that this is 40535 30:00:15,512 --> 30:00:19,119 working just fine we're going to hit the 40536 30:00:17,279 --> 30:00:21,440 end of the program and we are going to 40537 30:00:19,119 --> 30:00:22,319 destroy our objects here this is super 40538 30:00:23,279 --> 30:00:27,911 if we try and do the transformation and 40539 30:00:25,279 --> 30:00:29,592 transform it to a dark object even if we 40540 30:00:27,911 --> 30:00:32,232 know that we don't have any dog 40541 30:00:29,592 --> 30:00:34,800 information in this object that we are 40542 30:00:32,232 --> 30:00:37,440 referencing to start with we hope that 40543 30:00:34,800 --> 30:00:39,120 we're going to get a good message and 40544 30:00:37,440 --> 30:00:41,440 the program is going to end properly 40545 30:00:39,119 --> 30:00:43,440 we're not going to get a crush so let's 40546 30:00:41,440 --> 30:00:45,119 make sure we are transforming to dog 40547 30:00:43,440 --> 30:00:47,680 here we're going to pass a dog pointer 40548 30:00:45,119 --> 30:00:49,759 as our output and we're going to store 40549 30:00:47,679 --> 30:00:52,399 this as a dog pointer even if the name 40550 30:00:49,759 --> 30:00:54,551 is still vlan pointer hopefully this is 40551 30:00:52,399 --> 30:00:56,720 not going to confuse you even if this is 40552 30:00:54,551 --> 30:00:59,279 named feline pointer it is a dot pointer 40553 30:00:56,720 --> 30:01:01,512 here because we just want to see 40554 30:00:59,279 --> 30:01:03,831 if we get the error message that we have 40555 30:01:01,512 --> 30:01:06,960 here so we are turning this reference 40556 30:01:03,831 --> 30:01:09,679 into a pointer and the benefit of this 40557 30:01:09,679 --> 30:01:14,399 null pointers and we can use this to our 40558 30:01:12,160 --> 30:01:16,800 advantage to check and see if the 40559 30:01:14,399 --> 30:01:19,191 transformation here was successful if it 40560 30:01:16,800 --> 30:01:22,232 was successful we will call some dog 40561 30:01:19,191 --> 30:01:23,759 thank method let's say that here and if 40562 30:01:22,232 --> 30:01:26,552 this fails we're going to say that we 40563 30:01:23,759 --> 30:01:28,959 couldn't transform to doug and this is 40564 30:01:26,551 --> 30:01:30,959 going to give us a nice message and our 40565 30:01:28,960 --> 30:01:33,440 program is not going to crash we're 40566 30:01:30,960 --> 30:01:35,360 going to build this again with gcc what 40567 30:01:33,440 --> 30:01:37,760 is going to be good as you see here 40568 30:01:35,360 --> 30:01:39,512 we're going to clear and run rooster now 40569 30:01:37,759 --> 30:01:41,679 you see that we are saying we couldn't 40570 30:01:39,512 --> 30:01:44,080 catch the dog reference and our program 40571 30:01:41,679 --> 30:01:46,231 is not crashing so if you have a 40572 30:01:44,080 --> 30:01:48,639 reference to start with and you want to 40573 30:01:46,232 --> 30:01:51,192 do some proper checks after you do the 40574 30:01:48,639 --> 30:01:53,440 transformation to a derived pointer or a 40575 30:01:51,191 --> 30:01:55,119 derived reference this is the best of 40576 30:01:53,440 --> 30:01:57,191 both worlds you can do something like 40577 30:01:55,119 --> 30:01:59,440 this and your program is going to be 40578 30:01:57,191 --> 30:02:02,399 much safer the last thing i want you to 40579 30:01:59,440 --> 30:02:05,040 see is that these kinds of dynamic casts 40580 30:02:02,399 --> 30:02:07,279 are really useful if you are passing 40581 30:02:05,039 --> 30:02:10,719 base pointer or base references into 40582 30:02:07,279 --> 30:02:12,800 functions and here i am going to put in 40583 30:02:10,720 --> 30:02:14,960 a bunch of functions the first one is 40584 30:02:12,800 --> 30:02:16,720 going to take a base pointer the second 40585 30:02:14,960 --> 30:02:18,552 one is going to take a base reference 40586 30:02:16,720 --> 30:02:20,720 and it is going to say do something with 40587 30:02:18,551 --> 30:02:22,551 animal pointer and do something with 40588 30:02:20,720 --> 30:02:24,160 animal reference and inside we're going 40589 30:02:22,551 --> 30:02:25,831 to do our thing here we're going to take 40590 30:02:24,160 --> 30:02:27,760 the animal pointer turn that into a 40591 30:02:25,831 --> 30:02:29,279 feline pointer and we're going to do our 40592 30:02:27,759 --> 30:02:31,679 proper check and we're going to do some 40593 30:02:29,279 --> 30:02:33,831 feline thingy if the transformation was 40594 30:02:31,679 --> 30:02:36,159 successful we can do something like this 40595 30:02:33,831 --> 30:02:37,911 we can also do exactly the same thing 40596 30:02:36,160 --> 30:02:39,911 with references we're going to take our 40597 30:02:37,911 --> 30:02:42,319 input reference we're going to turn that 40598 30:02:39,911 --> 30:02:44,551 into a derived object and we can call 40599 30:02:42,320 --> 30:02:46,480 our method on this pointer and we're 40600 30:02:44,551 --> 30:02:49,119 going to do proper chats because we have 40601 30:02:46,479 --> 30:02:51,119 a pointer which has a concept of node 40602 30:02:51,119 --> 30:02:56,159 transformation here fails this is the 40603 30:02:53,440 --> 30:02:58,000 setup we can use in our c plus programs 40604 30:02:56,160 --> 30:03:00,480 and we can use these methods to get 40605 30:02:58,000 --> 30:03:02,551 derived pointers that we can call even 40606 30:03:00,479 --> 30:03:05,119 non-polymorphic functions on and this is 40607 30:03:02,551 --> 30:03:08,319 going to work really well so we can go 40608 30:03:05,119 --> 30:03:11,119 down and comment out everything i guess 40609 30:03:08,320 --> 30:03:12,480 to make this super easy to follow but i 40610 30:03:11,119 --> 30:03:14,319 don't think we need to comment out 40611 30:03:12,479 --> 30:03:16,159 everything we can just comment out what 40612 30:03:14,320 --> 30:03:19,040 we just did here and we're going to be 40613 30:03:16,160 --> 30:03:21,040 reusing the reference we have here and 40614 30:03:19,039 --> 30:03:23,191 the pointer we have all the way to the 40615 30:03:21,039 --> 30:03:25,360 top so we're going to go down and put in 40616 30:03:23,191 --> 30:03:27,279 a separator here we can do that and 40617 30:03:25,360 --> 30:03:30,080 because of this separator i don't think 40618 30:03:27,279 --> 30:03:31,592 we even need to comment things out we 40619 30:03:30,080 --> 30:03:34,000 can remove our comment and leave the 40620 30:03:31,592 --> 30:03:36,552 code in because i want you to see that 40621 30:03:34,000 --> 30:03:37,831 super easily and we're going to call our 40622 30:03:36,551 --> 30:03:40,000 functions here we're going to say do 40623 30:03:37,831 --> 30:03:42,080 something with animal pointer and we're 40624 30:03:40,000 --> 30:03:43,679 going to pass an animal one which is a 40625 30:03:42,080 --> 30:03:46,080 pointer and we're going to say do 40626 30:03:46,080 --> 30:03:50,479 we can do something like this and we're 40627 30:03:50,479 --> 30:03:54,159 ref we have this reference here and we 40628 30:03:52,320 --> 30:03:56,800 can do something like this you're going 40629 30:03:54,160 --> 30:03:58,480 to see that this is not going to give us 40630 30:03:56,800 --> 30:04:00,232 any kind of problem and it is going to 40631 30:03:58,479 --> 30:04:02,319 work just fine that's world we're going 40632 30:04:00,232 --> 30:04:04,400 to pass this through gcc the photo is 40633 30:04:02,320 --> 30:04:06,552 going to be good we can clear and run 40634 30:04:04,399 --> 30:04:08,720 rooster and down here you see that in 40635 30:04:06,551 --> 30:04:11,191 function taking base pointer doing some 40636 30:04:08,720 --> 30:04:13,512 feline thingy in function taking some 40637 30:04:11,191 --> 30:04:15,591 biz reference doing some feeling thingy 40638 30:04:13,512 --> 30:04:18,479 and this is working really fine this is 40639 30:04:15,592 --> 30:04:20,400 what we want in our c plus plus programs 40640 30:04:18,479 --> 30:04:22,080 this is really all i wanted you to see 40641 30:04:20,399 --> 30:04:24,479 in this lecture hopefully you didn't 40642 30:04:22,080 --> 30:04:27,040 find it confusing again the message is 40643 30:04:24,479 --> 30:04:29,440 that you can do dynamic casts to 40644 30:04:27,039 --> 30:04:32,639 transform from a base pointer to a 40645 30:04:29,440 --> 30:04:35,440 derived pointer or to transform from a 40646 30:04:32,639 --> 30:04:36,551 base reference to a derived reference as 40647 30:04:36,551 --> 30:04:42,639 right here but transforming to a derived 40648 30:04:39,440 --> 30:04:45,040 reference has a drawback in that you 40649 30:04:42,639 --> 30:04:47,512 don't have a way to check and see 40650 30:04:45,039 --> 30:04:49,360 if the transformation was successful so 40651 30:04:47,512 --> 30:04:51,191 if you want to be able to do chats you 40652 30:04:49,360 --> 30:04:53,759 can do something like this and turn a 40653 30:04:51,191 --> 30:04:55,911 base reference into a derived pointer 40654 30:04:53,759 --> 30:04:58,000 and then we can check and see if the 40655 30:04:55,911 --> 30:04:59,440 pointer is not a node pointer if it's a 40656 30:04:58,000 --> 30:05:01,512 no pointer we will know that the 40657 30:04:59,440 --> 30:05:03,360 transformation failed and we will print 40658 30:05:01,512 --> 30:05:05,191 a good error message if the 40659 30:05:03,360 --> 30:05:07,680 transformation was successful we will 40660 30:05:05,191 --> 30:05:10,871 have a valid pointer in here and we will 40661 30:05:07,679 --> 30:05:13,119 call our derived non-polymorphic method 40662 30:05:10,872 --> 30:05:14,960 on our pointer this is going to work 40663 30:05:13,119 --> 30:05:17,679 really fine the last thing i want you to 40664 30:05:14,960 --> 30:05:20,552 see before we wrap up this lecture is 40665 30:05:17,679 --> 30:05:23,279 that dynamic casts are only going to 40666 30:05:20,551 --> 30:05:26,231 work with polymorphic inheritance 40667 30:05:23,279 --> 30:05:29,679 hierarchies so for example if we try and 40668 30:05:26,232 --> 30:05:31,040 turn a double into a string obviously 40669 30:05:29,679 --> 30:05:33,279 that's not going to work with the 40670 30:05:31,039 --> 30:05:34,959 dynamic cast but let's try and do that 40671 30:05:33,279 --> 30:05:36,800 to really show you that this is going to 40672 30:05:34,960 --> 30:05:38,720 give us a problem we're going to say 40673 30:05:36,800 --> 30:05:40,800 sddc out and we're going to put in a 40674 30:05:38,720 --> 30:05:42,960 separator here and we're going to set up 40675 30:05:40,800 --> 30:05:44,639 two variables one is going to be a net 40676 30:05:42,960 --> 30:05:47,680 for example and we're going to say data 40677 30:05:44,639 --> 30:05:50,232 we're going to put in a 45 and if we do 40678 30:05:47,679 --> 30:05:52,319 a dynamic cast to an sdd string for 40679 30:05:50,232 --> 30:05:56,160 example that's not going to work we're 40680 30:05:52,320 --> 30:05:58,400 going to say std string yana str meaning 40681 30:05:56,160 --> 30:06:00,800 that this is a string we're going to say 40682 30:05:58,399 --> 30:06:03,591 dynamic cast let's say we want to turn 40683 30:06:00,800 --> 30:06:05,440 this into an sdd string for example and 40684 30:06:03,592 --> 30:06:06,872 the input is going to be our data if 40685 30:06:05,440 --> 30:06:08,479 you're trying to do something like this 40686 30:06:06,872 --> 30:06:10,320 this is going to blow up you see that 40687 30:06:08,479 --> 30:06:12,319 even visual studio code is giving us a 40688 30:06:10,320 --> 30:06:16,160 squiggly line so this is not going to 40689 30:06:12,320 --> 30:06:19,120 work just make sure you use dynamic cast 40690 30:06:16,160 --> 30:06:22,320 in an inheritance hierarchy that 40691 30:06:19,119 --> 30:06:25,679 supports virtual functions this is the 40692 30:06:22,320 --> 30:06:28,552 design purpose for the dynamic cast if 40693 30:06:25,679 --> 30:06:30,799 you use this outside this context you're 40694 30:06:28,551 --> 30:06:33,039 going to get undefined behavior or even 40695 30:06:30,800 --> 30:06:34,479 compiler errors if you are lucky let's 40696 30:06:33,039 --> 30:06:36,719 try and weld and show you this we're 40697 30:06:34,479 --> 30:06:38,399 going to pass this through gcc and we're 40698 30:06:36,720 --> 30:06:41,360 probably going to get an error saying 40699 30:06:38,399 --> 30:06:43,511 that we can't transform from an integer 40700 30:06:41,360 --> 30:06:46,479 to a string for example let's go here 40701 30:06:43,512 --> 30:06:49,512 cannot dynamic cast so basically it is 40702 30:06:46,479 --> 30:06:52,399 saying dynamic casts are meant to work 40703 30:06:49,512 --> 30:06:55,832 only with pointers or references but 40704 30:06:52,399 --> 30:06:58,551 even then let's see if we can transform 40705 30:06:55,831 --> 30:07:01,119 from an end pointer to a string pointer 40706 30:06:58,551 --> 30:07:04,399 let's try and do that let's say we want 40707 30:07:01,119 --> 30:07:06,639 to turn the address of data or let's set 40708 30:07:04,399 --> 30:07:09,279 up a pointer to be explicit we're going 40709 30:07:06,639 --> 30:07:11,759 to say data ptr and we're going to 40710 30:07:09,279 --> 30:07:13,592 initialize this with the address of data 40711 30:07:11,759 --> 30:07:17,039 we can do something like this and we're 40712 30:07:13,592 --> 30:07:18,960 going to cast data ptr to an std 40713 30:07:17,039 --> 30:07:21,511 strength pointer let's see if we can do 40714 30:07:18,960 --> 30:07:23,592 this because it is meant to cast 40715 30:07:21,512 --> 30:07:26,160 between pointers and references but you 40716 30:07:23,592 --> 30:07:28,000 see that this is even not going to work 40717 30:07:26,160 --> 30:07:31,279 so you shouldn't really do something 40718 30:07:28,000 --> 30:07:33,679 like this if your input and output are 40719 30:07:31,279 --> 30:07:36,160 not part of the same polymorphic 40720 30:07:33,679 --> 30:07:38,231 inheritance hierarchy again dynamic 40721 30:07:38,232 --> 30:07:43,680 from base information to derived 40722 30:07:40,639 --> 30:07:46,960 information and the input and output 40723 30:07:43,679 --> 30:07:49,039 must be part of the same polymorphic 40724 30:07:46,960 --> 30:07:51,440 inheritance hierarchy this is the 40725 30:07:49,039 --> 30:07:53,360 information i am trying to convey here 40726 30:07:51,440 --> 30:07:55,040 so this is bad don't do something like 40727 30:07:53,360 --> 30:07:57,191 this if you do this you're going to get 40728 30:07:55,039 --> 30:07:59,360 undefined behavior or you're even going 40729 30:07:57,191 --> 30:08:01,679 to get a bunch of compiler errors if you 40730 30:07:59,360 --> 30:08:03,592 are lucky this is really all we set up 40731 30:08:01,679 --> 30:08:06,231 to do in this lecture showing you that 40732 30:08:03,592 --> 30:08:08,232 you can use dynamic casts we are going 40733 30:08:06,232 --> 30:08:10,639 to stop here in this lecture the next 40734 30:08:08,232 --> 30:08:12,872 one we're going to show you what happens 40735 30:08:10,639 --> 30:08:15,440 if you try to call a polymorphic 40736 30:08:15,440 --> 30:08:19,911 go ahead and finish up here and meet me 40737 30:08:17,592 --> 30:08:23,512 there in this lecture i am going to give 40738 30:08:19,911 --> 30:08:26,639 you a warning and that is to never call 40739 30:08:23,512 --> 30:08:29,191 virtual functions from constructors or 40740 30:08:26,639 --> 30:08:32,080 destructors i am going to repeat this 40741 30:08:29,191 --> 30:08:34,720 never never call virtual or polymorphic 40742 30:08:32,080 --> 30:08:36,800 functions either from constructors or 40743 30:08:34,720 --> 30:08:38,479 destructors and we're going to explore 40744 30:08:36,800 --> 30:08:39,911 why we're going to be using this 40745 30:08:38,479 --> 30:08:42,159 inheritance hierarchy we're going to 40746 30:08:39,911 --> 30:08:44,399 have a base class called base how 40747 30:08:42,160 --> 30:08:47,040 creative we're going to inherit from 40748 30:08:44,399 --> 30:08:49,439 base and create derived which is our 40749 30:08:47,039 --> 30:08:51,911 inherited class and we're going to be 40750 30:08:49,440 --> 30:08:54,320 using this to explore why we can't call 40751 30:08:51,911 --> 30:08:55,679 virtual functions from constructors and 40752 30:08:55,679 --> 30:09:00,079 this is what our code is going to look 40753 30:08:57,512 --> 30:09:02,232 like the base class is going to have a 40754 30:09:00,080 --> 30:09:04,479 protected member variable it's going to 40755 30:09:02,232 --> 30:09:07,120 have a bunch of virtual functions 40756 30:09:04,479 --> 30:09:09,191 we will have a virtual function to set 40757 30:09:07,119 --> 30:09:11,911 things up and it is just going to set 40758 30:09:09,191 --> 30:09:13,279 the value of our member variable to 10 40759 30:09:11,911 --> 30:09:15,279 and we're going to have a virtual 40760 30:09:13,279 --> 30:09:17,512 function called cleanup which is going 40761 30:09:15,279 --> 30:09:20,080 to maybe do some cleanup we're going to 40762 30:09:17,512 --> 30:09:22,000 see how this works out we also have a 40763 30:09:20,080 --> 30:09:24,160 method to get the value but i don't 40764 30:09:22,000 --> 30:09:26,800 think we will use this but you can keep 40765 30:09:24,160 --> 30:09:29,192 this and if you want notice that we are 40766 30:09:26,800 --> 30:09:30,720 trying to call the setup method from the 40767 30:09:30,720 --> 30:09:34,551 and you see that we have a comment that 40768 30:09:32,872 --> 30:09:36,320 says that this is going to do static 40769 30:09:34,551 --> 30:09:38,872 binding but we're going to see why in a 40770 30:09:36,320 --> 30:09:41,120 minute so this is our base class we can 40771 30:09:38,872 --> 30:09:42,960 derive from this class and create our 40772 30:09:41,119 --> 30:09:44,159 derived class we're going to inherit 40773 30:09:44,160 --> 30:09:49,120 and we are going to override the methods 40774 30:09:46,872 --> 30:09:51,192 we have in the base class we're going to 40775 30:09:49,119 --> 30:09:52,871 override the setup method as you see 40776 30:09:51,191 --> 30:09:54,799 here we're going to override the cleanup 40777 30:09:54,800 --> 30:09:58,872 our destructor is also going to be 40778 30:09:56,639 --> 30:10:01,440 virtual because we are good c plus plus 40779 30:09:58,872 --> 30:10:03,512 citizens and this is the setup we have 40780 30:10:01,440 --> 30:10:05,360 once you have your inheritance hierarchy 40781 30:10:03,512 --> 30:10:07,440 set up you're going to do something like 40782 30:10:05,360 --> 30:10:09,440 this because you want polymorphic 40783 30:10:07,440 --> 30:10:11,191 behavior you're going to set up the base 40784 30:10:09,440 --> 30:10:13,911 pointer and you're going to use that to 40785 30:10:11,191 --> 30:10:16,080 manage a derived object and you can do 40786 30:10:16,080 --> 30:10:20,720 considering this statement here i want 40787 30:10:18,160 --> 30:10:22,960 you to think about the order in which 40788 30:10:20,720 --> 30:10:25,119 constructors and destructors are going 40789 30:10:22,960 --> 30:10:27,832 to be called we have touched on this 40790 30:10:25,119 --> 30:10:29,759 subject before and we are going to 40791 30:10:27,831 --> 30:10:30,959 revisit this because it is really 40792 30:10:30,960 --> 30:10:35,832 in terms of calling virtual functions in 40793 30:10:33,592 --> 30:10:37,832 destructors and destructors 40794 30:10:35,831 --> 30:10:40,231 so when we do something like this the 40795 30:10:37,831 --> 30:10:42,551 base part of this object is going to be 40796 30:10:40,232 --> 30:10:44,320 wealth first so we are going to call the 40797 30:10:42,551 --> 30:10:46,800 base constructor first we're going to 40798 30:10:44,320 --> 30:10:48,872 set up the best part of us after that 40799 30:10:46,800 --> 30:10:50,400 we're going to set up the derived part 40800 30:10:48,872 --> 30:10:52,160 of us and we're going to call the 40801 30:10:52,160 --> 30:10:57,512 and after the derived part of us is set 40802 30:10:54,960 --> 30:10:59,832 up we can use our object like we do 40803 30:10:57,512 --> 30:11:02,080 always so we're going to use our object 40804 30:10:59,831 --> 30:11:04,720 to do whatever it is we want to do when 40805 30:11:02,080 --> 30:11:07,119 the time comes for this object to be 40806 30:11:04,720 --> 30:11:09,512 destroyed we're going to destroy the 40807 30:11:09,512 --> 30:11:13,440 and then we're going to destroy the base 40808 30:11:13,440 --> 30:11:17,512 now that we have considered this order 40809 30:11:15,759 --> 30:11:20,080 try to think about the setup when we 40810 30:11:20,080 --> 30:11:23,512 from a constructor that's what we're 40811 30:11:21,759 --> 30:11:25,591 going to look at first the best 40812 30:11:23,512 --> 30:11:27,760 constructor is going to be called but 40813 30:11:25,592 --> 30:11:30,639 notice that by the time we call the base 40814 30:11:27,759 --> 30:11:33,911 constructor the derived part of us won't 40815 30:11:30,639 --> 30:11:36,960 be set up yet so if we try to call a 40816 30:11:33,911 --> 30:11:39,759 virtual function in the base constructor 40817 30:11:36,960 --> 30:11:42,720 there won't be a derived part to call 40818 30:11:39,759 --> 30:11:44,639 the most specialized method for so the 40819 30:11:42,720 --> 30:11:47,040 compiler will notice that we don't have 40820 30:11:44,639 --> 30:11:49,592 a derived part yet we will be at this 40821 30:11:47,039 --> 30:11:52,000 stage building the base part and if we 40822 30:11:49,592 --> 30:11:54,000 call a virtual polymorphic function we 40823 30:11:52,000 --> 30:11:56,639 want the most specific parts to be 40824 30:11:54,000 --> 30:11:58,720 called which should live in the derived 40825 30:11:56,639 --> 30:12:01,279 part but we haven't done the derived 40826 30:11:58,720 --> 30:12:03,040 part yet we are still building the base 40827 30:12:01,279 --> 30:12:05,191 part of us so the compiler is going to 40828 30:12:03,039 --> 30:12:07,119 notice that we don't have a derived part 40829 30:12:05,191 --> 30:12:09,191 and it is going to call the base version 40830 30:12:07,119 --> 30:12:11,440 of our virtual function that's why we 40831 30:12:09,191 --> 30:12:13,759 said that if you call a virtual function 40832 30:12:11,440 --> 30:12:15,360 from a constructor or a destructor 40833 30:12:13,759 --> 30:12:17,591 you're going to get static binding 40834 30:12:15,360 --> 30:12:19,512 results if you call a virtual function 40835 30:12:17,592 --> 30:12:22,160 you want the most specific version 40836 30:12:19,512 --> 30:12:24,872 called but we don't have that yet in the 40837 30:12:22,160 --> 30:12:26,872 base part because the derived part 40838 30:12:24,872 --> 30:12:29,040 haven't been set up that's why we get 40839 30:12:26,872 --> 30:12:30,800 static binding results i hope this makes 40840 30:12:29,039 --> 30:12:32,799 sense and we're going to get to the same 40841 30:12:30,800 --> 30:12:35,680 result if you try to call a virtual 40842 30:12:32,800 --> 30:12:37,680 function from a destructor if we get to 40843 30:12:35,679 --> 30:12:39,591 the point where our object needs to be 40844 30:12:37,679 --> 30:12:41,679 destroyed the derived part is going to 40845 30:12:39,592 --> 30:12:43,440 be wiped out first we're going to be 40846 30:12:41,679 --> 30:12:46,000 left with the base destructor and when 40847 30:12:43,440 --> 30:12:47,512 we hit this base destructor and call a 40848 30:12:47,512 --> 30:12:50,639 that would mean to call the most 40849 30:12:50,639 --> 30:12:56,319 version of that method but the derived 40850 30:12:54,080 --> 30:12:59,119 part of our object has been already 40851 30:12:56,320 --> 30:13:00,800 wiped down so this is no longer existent 40852 30:12:59,119 --> 30:13:02,551 and we are trying to call the method on 40853 30:13:00,800 --> 30:13:04,639 this so the compiler is going to do the 40854 30:13:02,551 --> 30:13:06,551 next best thing it can do it is going to 40855 30:13:04,639 --> 30:13:08,720 call the best version of our virtual 40856 30:13:06,551 --> 30:13:11,831 function and again we are going to get 40857 30:13:08,720 --> 30:13:13,680 static binding results that's why it 40858 30:13:11,831 --> 30:13:16,319 isn't really wise to call a virtual 40859 30:13:13,679 --> 30:13:18,231 function either from a constructor or a 40860 30:13:16,320 --> 30:13:20,960 destructor now some of you must be 40861 30:13:18,232 --> 30:13:22,872 asking what if i really need to do setup 40862 30:13:20,960 --> 30:13:25,280 and clean up in the way that we set up 40863 30:13:22,872 --> 30:13:27,040 these things in our classes what if this 40864 30:13:25,279 --> 30:13:28,319 method is really useful maybe it is 40865 30:13:27,039 --> 30:13:30,479 setting up things we're going to be 40866 30:13:28,320 --> 30:13:33,192 using in our application we really need 40867 30:13:30,479 --> 30:13:35,440 this and the most logical point to call 40868 30:13:33,191 --> 30:13:36,871 this is the base constructor well you 40869 30:13:35,440 --> 30:13:39,592 shouldn't really put these in a 40870 30:13:36,872 --> 30:13:42,639 constructor what you could do 40871 30:13:39,592 --> 30:13:45,592 is maybe call this virtual method on 40872 30:13:42,639 --> 30:13:47,512 your object directly when the object has 40873 30:13:45,592 --> 30:13:49,832 finished being a constructor that's 40874 30:13:47,512 --> 30:13:52,872 going to be a better place to do this 40875 30:13:49,831 --> 30:13:54,871 because if you put that in a constructor 40876 30:13:52,872 --> 30:13:56,872 or a destructor you're not going to get 40877 30:13:54,872 --> 30:13:59,832 dynamic binding results and that's going 40878 30:13:56,872 --> 30:14:01,192 to be really bad so to sum up calling a 40879 30:14:01,191 --> 30:14:05,831 from a constructor or a destructor want 40880 30:14:03,440 --> 30:14:08,400 to give you polymorphic or dynamic 40881 30:14:05,831 --> 30:14:10,319 binding results the call will never go 40882 30:14:08,399 --> 30:14:12,399 to a more derived class than the 40883 30:14:10,320 --> 30:14:13,680 currently executing constructor or 40884 30:14:13,679 --> 30:14:18,719 and we will get static binding results 40885 30:14:15,911 --> 30:14:21,591 this is the idea i want to convey here 40886 30:14:18,720 --> 30:14:23,759 and as a guideline you should never call 40887 30:14:21,592 --> 30:14:26,080 virtual functions or polymorphic 40888 30:14:23,759 --> 30:14:28,319 functions from constructors or 40889 30:14:26,080 --> 30:14:31,440 disruptors if you really need this the 40890 30:14:28,320 --> 30:14:33,760 next best thing you can do is to call 40891 30:14:31,440 --> 30:14:36,400 your virtual functions directly on your 40892 30:14:33,759 --> 30:14:37,439 object after the object has been 40893 30:14:37,440 --> 30:14:41,680 set up we can do something like this and 40894 30:14:39,440 --> 30:14:43,512 call our setup method and then we can 40895 30:14:41,679 --> 30:14:46,000 call cleanup when we think that we need 40896 30:14:43,512 --> 30:14:48,160 to clean up and you call cleanup after 40897 30:14:46,000 --> 30:14:50,479 you construct the object and you call 40898 30:14:48,160 --> 30:14:52,872 delete after you clean up your object 40899 30:14:50,479 --> 30:14:54,800 this is going to do whatever it is you 40900 30:14:52,872 --> 30:14:57,120 want to do with your polymorphic 40901 30:14:54,800 --> 30:14:59,192 functions and the calls to these virtual 40902 30:14:57,119 --> 30:15:01,279 functions want to be leaving inside a 40903 30:14:59,191 --> 30:15:03,039 constructor or a disruptor and this is 40904 30:15:01,279 --> 30:15:04,399 going to work really well now that you 40905 30:15:03,039 --> 30:15:06,159 know this we're going to head over to 40906 30:15:04,399 --> 30:15:08,959 visual studio code and play with us a 40907 30:15:06,160 --> 30:15:10,720 little more here we are in our working 40908 30:15:08,960 --> 30:15:13,192 folder the current project is 40909 30:15:10,720 --> 30:15:15,360 polymorphic functions and the structures 40910 30:15:13,191 --> 30:15:17,440 we should really say with constructors 40911 30:15:15,360 --> 30:15:20,160 and instructors but this is going to do 40912 30:15:17,440 --> 30:15:22,479 we're going to grab our template files 40913 30:15:20,160 --> 30:15:24,400 and we're going to put them in place and 40914 30:15:22,479 --> 30:15:26,080 we are going to open this little guy in 40915 30:15:24,399 --> 30:15:28,231 visual studio code by dragging and 40916 30:15:26,080 --> 30:15:30,800 dropping here this is going to give us 40917 30:15:28,232 --> 30:15:32,552 our main cpp file we can clean up a 40918 30:15:30,800 --> 30:15:35,192 little bit remove whatever it is we 40919 30:15:32,551 --> 30:15:37,591 don't need and i am just going to put in 40920 30:15:35,191 --> 30:15:40,000 my classes i am going to do that in the 40921 30:15:37,592 --> 30:15:42,872 main cpp file because that's going to be 40922 30:15:40,000 --> 30:15:44,960 much easier to follow we have our base 40923 30:15:42,872 --> 30:15:47,440 class it is going to have a single 40924 30:15:44,960 --> 30:15:50,552 member variable called m value and we 40925 30:15:47,440 --> 30:15:52,639 will have a virtual destructor 40926 30:15:50,551 --> 30:15:54,639 we will have a bunch of virtual 40927 30:15:52,639 --> 30:15:56,551 functions one is going to be setup which 40928 30:15:54,639 --> 30:15:58,319 is going to set our value the other is 40929 30:15:56,551 --> 30:16:00,720 going to be cleanup which is going to be 40930 30:15:58,320 --> 30:16:03,280 doing some cleanup here we will also 40931 30:16:00,720 --> 30:16:05,440 have a getvalue method which will get 40932 30:16:03,279 --> 30:16:08,000 this value and print it out if we want 40933 30:16:05,440 --> 30:16:10,080 but it is really not that useful here we 40934 30:16:08,000 --> 30:16:12,800 will also create a derived class which 40935 30:16:10,080 --> 30:16:14,720 is going to inherit publicly from base 40936 30:16:12,800 --> 30:16:17,040 it is not going to have its own member 40937 30:16:14,720 --> 30:16:19,512 variable all it's going to do is set up 40938 30:16:19,512 --> 30:16:23,760 the virtual functions we have in the 40939 30:16:21,360 --> 30:16:25,592 base class we're going to override setup 40940 30:16:23,759 --> 30:16:28,159 and in derived we're going to set our 40941 30:16:25,592 --> 30:16:30,080 value to 100 and we're going to clean up 40942 30:16:28,160 --> 30:16:32,480 and put a message that we are cleaning 40943 30:16:30,080 --> 30:16:34,872 up here once we have this set up we can 40944 30:16:32,479 --> 30:16:36,720 head over in the main function and set 40945 30:16:34,872 --> 30:16:39,832 up a base pointer which is going to be 40946 30:16:36,720 --> 30:16:41,911 managing a derived object a classic 40947 30:16:39,831 --> 30:16:44,159 setup for polymorphic behavior so we're 40948 30:16:41,911 --> 30:16:46,080 going to say base and i'm going to say p 40949 30:16:44,160 --> 30:16:48,872 bass and we're going to say equals new 40950 30:16:46,080 --> 30:16:51,040 derived this is going to give us our 40951 30:16:48,872 --> 30:16:53,192 object which is managed by a base 40952 30:16:51,039 --> 30:16:55,591 pointer and one thing i didn't show you 40953 30:16:53,191 --> 30:16:56,720 is that we are calling the setup virtual 40954 30:16:56,720 --> 30:17:01,512 from the base constructor this is what 40955 30:17:00,000 --> 30:17:04,399 we want to see we want to see what is 40956 30:17:01,512 --> 30:17:05,512 happening here now if we call this setup 40957 30:17:05,512 --> 30:17:10,320 we would expect polymorphism to kick in 40958 30:17:08,232 --> 30:17:12,639 because this is a virtual function 40959 30:17:10,320 --> 30:17:15,280 if the derived part is available we will 40960 30:17:12,639 --> 30:17:18,319 call the most specific setup function 40961 30:17:15,279 --> 30:17:21,039 and that would set up the value to be 40962 30:17:18,320 --> 30:17:23,120 100 here if we get the value that's what 40963 30:17:21,039 --> 30:17:25,279 we would get let's see what we get here 40964 30:17:25,279 --> 30:17:30,479 value that we have in this object here 40965 30:17:27,759 --> 30:17:33,511 so we will get auto value and we're 40966 30:17:30,479 --> 30:17:35,440 going to say p base get value we have 40967 30:17:33,512 --> 30:17:37,512 this function here and we will try to 40968 30:17:35,440 --> 30:17:40,080 print this out okay this is something we 40969 30:17:37,512 --> 30:17:41,592 can do in our c plus bus program and 40970 30:17:40,080 --> 30:17:43,512 we're going to see the value we get 40971 30:17:41,592 --> 30:17:45,912 because we are calling this virtual 40972 30:17:43,512 --> 30:17:48,479 function we expect polymorphism to kick 40973 30:17:45,911 --> 30:17:50,639 in and if polymorphism kicks in we're 40974 30:17:48,479 --> 30:17:52,800 going to call the most specialized 40975 30:17:50,639 --> 30:17:54,479 version of our virtual function here 40976 30:17:52,800 --> 30:17:57,040 that would be the override we have in 40977 30:17:54,479 --> 30:17:59,679 derived and that would give us 100. so 40978 30:17:57,039 --> 30:18:01,360 one would get to get 100 printed out 40979 30:17:59,679 --> 30:18:04,079 here but let's see what we get we're 40980 30:18:01,360 --> 30:18:05,831 going to run this through gcc our 40981 30:18:04,080 --> 30:18:08,479 favorite compiler the builder is going 40982 30:18:05,831 --> 30:18:10,720 to be good we can clear or bring up a 40983 30:18:08,479 --> 30:18:13,039 powershell window we're going to clear 40984 30:18:10,720 --> 30:18:14,960 now and run rooster we're going to see 40985 30:18:13,039 --> 30:18:17,191 that the base constructor is going to be 40986 30:18:14,960 --> 30:18:19,832 called base setup was called you see we 40987 30:18:17,191 --> 30:18:22,479 called the base version of this and the 40988 30:18:19,831 --> 30:18:24,231 derived constructor will be called and 40989 30:18:22,479 --> 30:18:26,959 we're going to see that the value 40990 30:18:24,232 --> 30:18:29,360 doesn't exist oh we didn't put the value 40991 30:18:26,960 --> 30:18:31,040 out let's do that my bad here we're 40992 30:18:29,360 --> 30:18:33,512 going to build again we're going to use 40993 30:18:31,039 --> 30:18:36,231 gcc to build and the world is good we 40994 30:18:33,512 --> 30:18:38,232 can clear and run rooster now we see 40995 30:18:36,232 --> 30:18:41,440 that the base setup method was called 40996 30:18:38,232 --> 30:18:43,680 and set the value to b10 and we are 40997 30:18:41,440 --> 30:18:46,080 really getting static binding results 40998 30:18:43,679 --> 30:18:48,399 because we are calling the setup 40999 30:18:46,080 --> 30:18:50,800 method from the base class and the base 41000 30:18:48,399 --> 30:18:54,000 version is being called so this is 41001 30:18:50,800 --> 30:18:56,720 classic static binding behavior but we 41002 30:18:54,000 --> 30:19:00,000 want polymorphic behavior to be working 41003 30:18:56,720 --> 30:19:02,639 here but we just saw the reason why this 41004 30:19:00,000 --> 30:19:05,360 is in the slides by the time we call 41005 30:19:02,639 --> 30:19:07,679 this virtual function we haven't set up 41006 30:19:07,679 --> 30:19:13,911 and we have no way to call this virtual 41007 30:19:11,039 --> 30:19:16,000 function which lives in the derived part 41008 30:19:13,911 --> 30:19:18,000 of us so the compiler is going to do the 41009 30:19:16,000 --> 30:19:20,080 next best thing it can do it's going to 41010 30:19:18,000 --> 30:19:22,479 call the best version of this method and 41011 30:19:20,080 --> 30:19:24,872 we are going to get static binding 41012 30:19:22,479 --> 30:19:27,279 results and we are going to get the same 41013 30:19:24,872 --> 30:19:30,080 results if we try to call a virtual 41014 30:19:27,279 --> 30:19:33,360 function from the destructor for example 41015 30:19:30,080 --> 30:19:35,040 if we go in our destructor here 41016 30:19:33,360 --> 30:19:36,960 let's see if we can find it we're going 41017 30:19:35,039 --> 30:19:39,119 to go in our instructor here and say 41018 30:19:36,960 --> 30:19:41,120 this and say cleanup can do something 41019 30:19:39,119 --> 30:19:43,512 like this and you're going to see that 41020 30:19:41,119 --> 30:19:45,279 the cleanup version we call is the base 41021 30:19:45,279 --> 30:19:49,911 and we don't really want to do something 41022 30:19:47,279 --> 30:19:51,831 like this we want the most derived 41023 30:19:49,911 --> 30:19:54,232 cleanup version to be called but we 41024 30:19:51,831 --> 30:19:57,279 won't get that because by the moment we 41025 30:19:54,232 --> 30:19:58,320 call this virtual function from our base 41026 30:19:58,320 --> 30:20:03,512 the derived part will already have been 41027 30:20:00,872 --> 30:20:06,479 wiped out so it won't be available and 41028 30:20:03,512 --> 30:20:08,639 the compiler will do the next best thing 41029 30:20:06,479 --> 30:20:10,551 it can do it is going to call the best 41030 30:20:08,639 --> 30:20:12,872 version of our cleanup function here i 41031 30:20:12,872 --> 30:20:17,192 again to really drive this home i 41032 30:20:14,872 --> 30:20:19,760 realize this can be confusing to many 41033 30:20:17,191 --> 30:20:21,279 people especially beginners if we are 41034 30:20:19,759 --> 30:20:23,759 calling a virtual function from a 41035 30:20:21,279 --> 30:20:25,440 constructor we will call that virtual 41036 30:20:23,759 --> 30:20:27,679 function when we hit the base 41037 30:20:25,440 --> 30:20:29,832 constructor but by the time we hit the 41038 30:20:27,679 --> 30:20:32,639 base constructor the derived part of us 41039 30:20:29,831 --> 30:20:34,959 hasn't been constructed yet so if we try 41040 30:20:32,639 --> 30:20:37,191 to call a polymorphic function needing 41041 30:20:34,960 --> 30:20:39,512 to hold the derived implementation that 41042 30:20:37,191 --> 30:20:42,080 won't be available and the compiler will 41043 30:20:39,512 --> 30:20:43,911 do the next best thing it can do it is 41044 30:20:42,080 --> 30:20:45,512 going to call the base version the same 41045 30:20:43,911 --> 30:20:48,232 phenomenon is going to happen in the 41046 30:20:45,512 --> 30:20:50,720 destructor when we are done using our 41047 30:20:48,232 --> 30:20:53,512 object we will destroy the derived part 41048 30:20:50,720 --> 30:20:55,440 information first and when we hit the 41049 30:20:53,512 --> 30:20:57,440 base destructor and call a virtual 41050 30:20:55,440 --> 30:20:59,760 function there won't be a most 41051 30:20:57,440 --> 30:21:02,160 specialized version of the virtual 41052 30:20:59,759 --> 30:21:04,159 function because the derived part has 41053 30:21:02,160 --> 30:21:06,800 been already destroyed and it doesn't 41054 30:21:04,160 --> 30:21:08,960 exist anymore so if we call a virtual 41055 30:21:06,800 --> 30:21:10,872 function from the base destructor we 41056 30:21:08,960 --> 30:21:13,040 will get the best version called and 41057 30:21:10,872 --> 30:21:15,040 this will give us static binding 41058 30:21:13,039 --> 30:21:17,911 behavior this is what we are 41059 30:21:15,039 --> 30:21:20,639 experiencing here okay so let's try and 41060 30:21:17,911 --> 30:21:22,479 run this program and see what we get we 41061 30:21:20,639 --> 30:21:25,039 will print out value here which is going 41062 30:21:22,479 --> 30:21:28,231 to give us static binding results but we 41063 30:21:25,039 --> 30:21:30,231 want to see which version of cleanup is 41064 30:21:28,232 --> 30:21:32,000 called this is what we want to see here 41065 30:21:30,232 --> 30:21:34,552 we're going to run the task to build 41066 30:21:32,000 --> 30:21:36,639 with gcc the world is going to be good 41067 30:21:34,551 --> 30:21:38,479 as usual we're going to clear and run 41068 30:21:36,639 --> 30:21:40,479 rooster and i'm going to see base 41069 30:21:38,479 --> 30:21:42,399 constructor called derived constructor 41070 30:21:40,479 --> 30:21:44,399 called and we're going to have a value 41071 30:21:42,399 --> 30:21:46,720 which is a junk value because we never 41072 30:21:44,399 --> 30:21:49,360 initialize this value but we don't see 41073 30:21:46,720 --> 30:21:50,479 our destructure is called what is going 41074 30:21:50,479 --> 30:21:55,679 let's investigate this a little more the 41075 30:21:53,039 --> 30:21:57,679 reason is we are not explicitly 41076 30:21:55,679 --> 30:21:59,359 releasing this memory here through the 41077 30:21:57,679 --> 30:22:02,231 base pointer let's do that we're going 41078 30:21:59,360 --> 30:22:05,191 to delete p base here and we're going to 41079 30:22:02,232 --> 30:22:07,680 run again we're going to weld with gcc 41080 30:22:05,191 --> 30:22:09,759 now our destructors should be called 41081 30:22:07,679 --> 30:22:11,439 we're going to run rooster and we are 41082 30:22:09,759 --> 30:22:13,360 going to see that the base cleanup 41083 30:22:11,440 --> 30:22:16,080 version was called and again this is 41084 30:22:13,360 --> 30:22:18,479 giving us static binding results you 41085 30:22:16,080 --> 30:22:19,592 don't want something like this so if you 41086 30:22:19,592 --> 30:22:24,480 get to this behavior i would advise not 41087 30:22:22,000 --> 30:22:27,191 to call the cleanup or setup functions 41088 30:22:24,479 --> 30:22:28,872 inside constructors or destructors and 41089 30:22:27,191 --> 30:22:30,959 that's the guideline i gave in the 41090 30:22:28,872 --> 30:22:32,960 slides you should really never call 41091 30:22:30,960 --> 30:22:35,912 virtual functions either from 41092 30:22:32,960 --> 30:22:38,720 constructors or disruptors especially if 41093 30:22:35,911 --> 30:22:41,759 you are doing this from a parent class 41094 30:22:38,720 --> 30:22:44,800 hoping to get the most specific virtual 41095 30:22:41,759 --> 30:22:46,959 function called through polymorphism if 41096 30:22:44,800 --> 30:22:49,832 you want to do that the best thing you 41097 30:22:46,960 --> 30:22:50,800 can do is to call the cleanup functions 41098 30:22:50,800 --> 30:22:54,872 is to call the setup and cleanup 41099 30:22:52,479 --> 30:22:57,512 functions after your object has been 41100 30:22:54,872 --> 30:23:00,320 properly constructed or before your 41101 30:22:57,512 --> 30:23:03,760 object gets to be destroyed so what you 41102 30:23:00,320 --> 30:23:06,720 can do here is call the setup method on 41103 30:23:03,759 --> 30:23:08,639 the pbes pointer here let's do that and 41104 30:23:06,720 --> 30:23:10,800 we're going to call setup this is going 41105 30:23:08,639 --> 30:23:13,119 to call our virtual function and we are 41106 30:23:10,800 --> 30:23:15,760 going to get polymorphic behavior this 41107 30:23:13,119 --> 30:23:17,591 is going to print a 100 as we expect 41108 30:23:17,592 --> 30:23:24,000 the derived version of setup here and 41109 30:23:20,872 --> 30:23:26,320 before we delete our object we will need 41110 30:23:24,000 --> 30:23:28,639 to call the cleanup function if we need 41111 30:23:26,320 --> 30:23:29,912 to do some custom cleanup here so i'm 41112 30:23:29,911 --> 30:23:35,119 base and call the cleanup function and 41113 30:23:32,639 --> 30:23:37,759 this is going to give us the behavior we 41114 30:23:35,119 --> 30:23:40,231 want we're going to build our program 41115 30:23:37,759 --> 30:23:42,319 pass this through gcc the both is going 41116 30:23:42,320 --> 30:23:47,120 and run rooster and we are going to see 41117 30:23:44,960 --> 30:23:49,440 that this constructor was called derived 41118 30:23:47,119 --> 30:23:51,591 constructor was called we will call the 41119 30:23:49,440 --> 30:23:54,551 derived version of setup our value will 41120 30:23:51,592 --> 30:23:57,120 be 100 and when we get to release or 41121 30:23:54,551 --> 30:23:59,679 clean up our memory the derived version 41122 30:23:57,119 --> 30:24:01,911 of cleanup will be called and then we 41123 30:23:59,679 --> 30:24:04,319 will destroy the derived part of us and 41124 30:24:01,911 --> 30:24:07,191 then we will destroy the base part of us 41125 30:24:04,320 --> 30:24:09,440 this is going to work in line with the 41126 30:24:07,191 --> 30:24:11,591 behavior we are familiar with with 41127 30:24:09,440 --> 30:24:13,592 polymorphism and this is really cool so 41128 30:24:11,592 --> 30:24:16,160 this is what you should do you should 41129 30:24:13,592 --> 30:24:18,160 never call virtual functions from 41130 30:24:16,160 --> 30:24:20,480 distractors and constructors because 41131 30:24:18,160 --> 30:24:22,400 that's going to give you static binding 41132 30:24:20,479 --> 30:24:23,440 results and what you want in this case 41133 30:24:23,440 --> 30:24:28,080 dynamic binding behavior this is really 41134 30:24:26,320 --> 30:24:30,320 all i have to share in this lecture i 41135 30:24:28,080 --> 30:24:32,400 hope you found it interesting we are 41136 30:24:30,320 --> 30:24:34,160 going to stop here in this one the next 41137 30:24:32,399 --> 30:24:37,679 one we're going to learn about the type 41138 30:24:34,160 --> 30:24:40,000 id operator that can come in handy 41139 30:24:37,679 --> 30:24:42,319 especially if you want to debug 41140 30:24:40,000 --> 30:24:45,119 polymorphic code in your c plus plus 41141 30:24:42,320 --> 30:24:47,040 program go ahead and finish up here and 41142 30:24:45,119 --> 30:24:48,720 meet me there in this lecture we're 41143 30:24:47,039 --> 30:24:52,231 going to learn about pure virtual 41144 30:24:48,720 --> 30:24:54,800 functions and abstract classes and pure 41145 30:24:52,232 --> 30:24:56,720 virtual functions are a mechanism we 41146 30:24:56,720 --> 30:25:01,592 to mean that the method isn't meant to 41147 30:25:03,831 --> 30:25:08,720 here we have a simple inheritance 41148 30:25:05,831 --> 30:25:11,119 hierarchy to try and drive this home the 41149 30:25:08,720 --> 30:25:13,759 base class is shape and we are deriving 41150 30:25:11,119 --> 30:25:16,159 from this class to create a circle class 41151 30:25:13,759 --> 30:25:18,799 and on the other side we are deriving to 41152 30:25:16,160 --> 30:25:20,552 create a rectangle class if we look in 41153 30:25:18,800 --> 30:25:22,232 the shape class we have two virtual 41154 30:25:20,551 --> 30:25:24,479 methods one is going to be used to 41155 30:25:22,232 --> 30:25:26,160 compute the perimeter of the shape 41156 30:25:24,479 --> 30:25:28,551 the other is going to be used to compute 41157 30:25:26,160 --> 30:25:30,960 the surface of the shape but if we go 41158 30:25:28,551 --> 30:25:32,319 down in our inheritance classes we're 41159 30:25:30,960 --> 30:25:34,639 going to see that we have the same 41160 30:25:32,320 --> 30:25:37,440 methods these are going to be overrides 41161 30:25:34,639 --> 30:25:39,119 of what we have in the base class 41162 30:25:37,440 --> 30:25:41,040 but the special thing about this 41163 30:25:41,039 --> 30:25:46,479 is that we don't have implementations 41164 30:25:43,360 --> 30:25:48,800 for these methods in the base class here 41165 30:25:46,479 --> 30:25:51,119 and this is by design if you think about 41166 30:25:48,800 --> 30:25:52,800 it this is an abstract shape we don't 41167 30:25:51,119 --> 30:25:54,551 know if it's a rectangle we don't know 41168 30:25:52,800 --> 30:25:57,440 if it's a circle we don't know if it is 41169 30:25:54,551 --> 30:25:59,759 a triangle so we don't really have 41170 30:25:57,440 --> 30:26:02,000 concrete information we can use 41171 30:25:59,759 --> 30:26:04,871 to properly compute its perimeter and 41172 30:26:02,000 --> 30:26:07,512 its surface so it makes sense to really 41173 30:26:04,872 --> 30:26:10,000 leave in the declaration for these 41174 30:26:07,512 --> 30:26:13,119 methods but leave the implementation 41175 30:26:10,000 --> 30:26:15,191 details to inheriting classes because 41176 30:26:13,119 --> 30:26:17,759 these classes are going to have concrete 41177 30:26:15,191 --> 30:26:19,759 information we can use to reliably 41178 30:26:17,759 --> 30:26:22,080 compute the perimeter and the surface 41179 30:26:19,759 --> 30:26:24,799 and we can get this information here 41180 30:26:22,080 --> 30:26:27,119 so in c plus plus we mean this kind of 41181 30:26:24,800 --> 30:26:29,360 design by setting up these methods as 41182 30:26:29,360 --> 30:26:33,512 and what this is going to tell the 41183 30:26:30,872 --> 30:26:36,400 compiler is these methods are not meant 41184 30:26:33,512 --> 30:26:37,911 to be implemented in the base class they 41185 30:26:37,911 --> 30:26:42,720 overridden and implemented by 41186 30:26:39,831 --> 30:26:45,360 inheritance classes and another side 41187 30:26:42,720 --> 30:26:47,279 effect of this is that the compiler is 41188 30:26:45,360 --> 30:26:50,000 going to prevent you from creating 41189 30:26:47,279 --> 30:26:52,080 objects of this class because it really 41190 30:26:50,000 --> 30:26:54,320 doesn't make sense to create an object 41191 30:26:54,320 --> 30:26:57,760 if you want to be able to compute its 41192 30:26:56,232 --> 30:26:59,911 perimeter if you want to be able to 41193 30:26:57,759 --> 30:27:02,000 compute its surface so why bother 41194 30:26:59,911 --> 30:27:04,232 creating an object that you won't really 41195 30:27:02,000 --> 30:27:06,399 be using in your c plus plus program the 41196 30:27:04,232 --> 30:27:10,232 meaning for this base class is just to 41197 30:27:06,399 --> 30:27:12,551 be a an idea or a representation of a 41198 30:27:10,232 --> 30:27:14,872 shape without really going into the 41199 30:27:12,551 --> 30:27:16,479 concrete implementation of how that 41200 30:27:16,479 --> 30:27:21,119 hopefully this makes sense so let's see 41201 30:27:18,720 --> 30:27:23,279 how we can do this in code here we have 41202 30:27:21,119 --> 30:27:26,551 a simple class which is going to be our 41203 30:27:23,279 --> 30:27:28,232 shape class we are going to have two 41204 30:27:26,551 --> 30:27:30,231 virtual functions we're going to have 41205 30:27:28,232 --> 30:27:31,760 the perimeter function we're going to 41206 30:27:30,232 --> 30:27:33,911 have the surface function these are 41207 30:27:31,759 --> 30:27:36,871 going to be virtual functions or virtual 41208 30:27:33,911 --> 30:27:38,399 member functions i should say but we are 41209 30:27:38,399 --> 30:27:43,039 pure virtual functions by using the 41210 30:27:41,039 --> 30:27:46,231 syntax you see here so we're going to 41211 30:27:43,039 --> 30:27:48,871 prepare an echo zero and this is going 41212 30:27:46,232 --> 30:27:51,279 to tell the compiler that these are pure 41213 30:27:48,872 --> 30:27:52,872 virtual functions once you set up these 41214 30:27:51,279 --> 30:27:55,679 functions a few things are going to 41215 30:27:52,872 --> 30:27:58,639 happen in your class this class is going 41216 30:27:55,679 --> 30:28:00,719 to become an abstract class in that 41217 30:27:58,639 --> 30:28:02,720 you want to be able to create objects of 41218 30:28:00,720 --> 30:28:04,960 this class anymore if you try to do that 41219 30:28:02,720 --> 30:28:07,191 you will get a compiler error another 41220 30:28:04,960 --> 30:28:09,440 thing is that you don't need to put in 41221 30:28:07,191 --> 30:28:12,871 implementations for these methods 41222 30:28:09,440 --> 30:28:14,960 because you marked them as pure virtual 41223 30:28:12,872 --> 30:28:17,440 and what this means is that the 41224 30:28:14,960 --> 30:28:20,639 implementations will be put in by 41225 30:28:17,440 --> 30:28:22,551 inheriting downstream classes because 41226 30:28:20,639 --> 30:28:25,191 they will have concrete information to 41227 30:28:22,551 --> 30:28:27,279 compute the perimeter or the surface as 41228 30:28:25,191 --> 30:28:29,279 we have here so we don't need to bother 41229 30:28:27,279 --> 30:28:31,119 with definitions for these functions 41230 30:28:29,279 --> 30:28:32,872 here if you even try to put in a 41231 30:28:31,119 --> 30:28:35,039 definition here you're going to get a 41232 30:28:32,872 --> 30:28:37,512 compiler error because this is a virtual 41233 30:28:35,039 --> 30:28:39,831 function we have no business putting an 41234 30:28:37,512 --> 30:28:42,160 implementation in the base class here 41235 30:28:39,831 --> 30:28:44,871 these are meant to be implemented by 41236 30:28:42,160 --> 30:28:47,192 downstream inheritance classes okay once 41237 30:28:44,872 --> 30:28:49,592 we have our ship class and our 41238 30:28:47,191 --> 30:28:52,399 inheritance hierarchy we can try to run 41239 30:28:49,592 --> 30:28:54,320 this in our code for example if we try 41240 30:28:52,399 --> 30:28:56,871 and create a shape object we're going to 41241 30:28:54,320 --> 30:28:58,800 get a compiler error because shape is an 41242 30:28:56,872 --> 30:29:01,360 abstract class so we can't create 41243 30:28:58,800 --> 30:29:03,911 objects of it and i don't think i made 41244 30:29:01,360 --> 30:29:06,872 this super clear once you set up at 41245 30:29:03,911 --> 30:29:09,512 least one pure virtual function in your 41246 30:29:06,872 --> 30:29:12,232 class the class is automatically going 41247 30:29:09,512 --> 30:29:14,080 to become an abstract class and what 41248 30:29:12,232 --> 30:29:16,639 that means is that you want to be able 41249 30:29:14,080 --> 30:29:19,680 to create objects of this class so this 41250 30:29:16,639 --> 30:29:22,399 is an abstract class we have here and if 41251 30:29:19,679 --> 30:29:24,959 we try to create an object of it as we 41252 30:29:22,399 --> 30:29:27,511 are doing on this line here this is 41253 30:29:24,960 --> 30:29:30,552 going to give us a compiler error but we 41254 30:29:27,512 --> 30:29:32,160 can still use a base pointer to manage a 41255 30:29:32,160 --> 30:29:36,080 a common setup we do with polymorphism 41256 30:29:34,551 --> 30:29:38,551 so for example here we can set up a 41257 30:29:36,080 --> 30:29:41,512 shape pointer and use this to manage a 41258 30:29:38,551 --> 30:29:44,231 rectangle object if we call the surface 41259 30:29:41,512 --> 30:29:46,960 member function on our base pointer this 41260 30:29:44,232 --> 30:29:49,279 is going to do a polymorphic call and we 41261 30:29:46,960 --> 30:29:50,800 will get the surface of the rectangle 41262 30:29:49,279 --> 30:29:52,080 and we're going to print this out here 41263 30:29:50,800 --> 30:29:54,232 you're going to see that this is going 41264 30:29:52,080 --> 30:29:56,639 to work we can also use the base pointer 41265 30:29:54,232 --> 30:29:58,552 to manage a circle object as we see here 41266 30:29:56,639 --> 30:30:00,872 if we call the surface method this is 41267 30:29:58,551 --> 30:30:03,440 going to do polymorphism and it is going 41268 30:30:00,872 --> 30:30:05,680 to call the surface version of the 41269 30:30:03,440 --> 30:30:07,592 circle class and we can see it printed 41270 30:30:05,679 --> 30:30:10,000 out here this is the design we can 41271 30:30:07,592 --> 30:30:12,800 achieve even if shape is an abstract 41272 30:30:10,000 --> 30:30:15,592 class we can't create direct objects of 41273 30:30:12,800 --> 30:30:18,720 shape as we are doing here but we can 41274 30:30:15,592 --> 30:30:21,040 use a base pointer or a shape pointer to 41275 30:30:18,720 --> 30:30:23,911 manage derived object through 41276 30:30:21,039 --> 30:30:26,079 polymorphism and you need to be aware of 41277 30:30:23,911 --> 30:30:28,720 this okay now that you have an idea 41278 30:30:26,080 --> 30:30:31,191 about what a pure virtual function is 41279 30:30:28,720 --> 30:30:33,831 and what an abstract class is 41280 30:30:31,191 --> 30:30:36,399 let's see a few ideas you need to keep 41281 30:30:33,831 --> 30:30:39,279 in mind if your class has at least one 41282 30:30:36,399 --> 30:30:41,511 pure virtual function it will become an 41283 30:30:39,279 --> 30:30:43,440 abstract class you already know this you 41284 30:30:41,512 --> 30:30:45,440 can't create objects of an abstract 41285 30:30:43,440 --> 30:30:47,760 class if you try to do that you will get 41286 30:30:45,440 --> 30:30:50,400 a compiler error another thing i don't 41287 30:30:47,759 --> 30:30:53,439 think amid super clear is that derived 41288 30:30:50,399 --> 30:30:56,871 classes from an abstract class must 41289 30:30:53,440 --> 30:30:59,279 explicitly override all the pure virtual 41290 30:30:56,872 --> 30:31:01,440 functions from the base class 41291 30:31:01,440 --> 30:31:06,639 pure virtual class that you don't put in 41292 30:31:03,831 --> 30:31:08,231 an implementation in for your derived 41293 30:31:06,639 --> 30:31:10,319 class is also going to become an 41294 30:31:08,232 --> 30:31:12,000 abstract class and you want to be able 41295 30:31:10,320 --> 30:31:13,760 to create objects of that you need to 41296 30:31:12,000 --> 30:31:16,720 keep this in mind another thing you 41297 30:31:13,759 --> 30:31:17,911 can't do you can't call the pure virtual 41298 30:31:17,911 --> 30:31:22,000 from the constructor of the abstract 41299 30:31:20,479 --> 30:31:24,159 class if you do this you're going to get 41300 30:31:22,000 --> 30:31:26,551 a compiler error because we don't have 41301 30:31:24,160 --> 30:31:28,800 implementations for these things so this 41302 30:31:26,551 --> 30:31:31,759 is not going to work the constructor of 41303 30:31:28,800 --> 30:31:34,479 our abstract class is used by deriving 41304 30:31:31,759 --> 30:31:37,119 classes to build the base part of our 41305 30:31:34,479 --> 30:31:39,759 derived objects but they are not meant 41306 30:31:37,119 --> 30:31:41,440 to be used from the outside because we 41307 30:31:39,759 --> 30:31:43,911 can't really create an object of an 41308 30:31:41,440 --> 30:31:45,191 abstract class so now that you notice 41309 30:31:43,911 --> 30:31:47,039 we're going to head over to visual 41310 30:31:45,191 --> 30:31:50,159 studio code and play with this a little 41311 30:31:47,039 --> 30:31:52,479 more here we are in our working folder 41312 30:31:50,160 --> 30:31:55,680 the current project is pure virtual 41313 30:31:52,479 --> 30:31:58,159 functions and abstract classes we are 41314 30:31:55,679 --> 30:32:00,231 going to grab our template files and put 41315 30:31:58,160 --> 30:32:03,040 them in place and we're going to paste 41316 30:32:00,232 --> 30:32:05,592 them in this project here and we are 41317 30:32:03,039 --> 30:32:07,591 going to grab the files that make up our 41318 30:32:05,592 --> 30:32:09,512 inheritance hierarchy you are going to 41319 30:32:07,592 --> 30:32:11,440 get these files from the resource 41320 30:32:09,512 --> 30:32:13,592 section of the course if you want you 41321 30:32:11,440 --> 30:32:16,479 can download them and use them like i am 41322 30:32:13,592 --> 30:32:19,192 using them here or even better you can 41323 30:32:16,479 --> 30:32:21,191 type the code and see how these things 41324 30:32:19,191 --> 30:32:23,679 really work that's the best way to learn 41325 30:32:21,191 --> 30:32:25,831 i do recommend that but we want to save 41326 30:32:23,679 --> 30:32:28,479 on time here so i am just going to reuse 41327 30:32:25,831 --> 30:32:30,399 these glasses that i have lying on my 41328 30:32:28,479 --> 30:32:32,399 drive i am going to open this in visual 41329 30:32:30,399 --> 30:32:35,679 studio code by dragging and dropping 41330 30:32:32,399 --> 30:32:38,079 here we have our shape class here this 41331 30:32:35,679 --> 30:32:40,639 is going to be our base class you're 41332 30:32:38,080 --> 30:32:42,080 going to see that we have our member 41333 30:32:40,639 --> 30:32:45,039 variable which is going to be the 41334 30:32:42,080 --> 30:32:47,680 description for our shape we have a 41335 30:32:45,039 --> 30:32:50,231 bunch of constructors here but the most 41336 30:32:47,679 --> 30:32:53,191 important thing here is that we have a 41337 30:32:50,232 --> 30:32:54,872 bunch of pure virtual functions here and 41338 30:32:53,191 --> 30:32:56,080 if you go in the cpp file you're going 41339 30:32:56,080 --> 30:33:01,512 implementations then for the perimeter 41340 30:32:58,720 --> 30:33:03,360 and surface functions these are pure 41341 30:33:01,512 --> 30:33:05,911 virtual functions and we have no 41342 30:33:03,360 --> 30:33:08,320 business putting in an implementation in 41343 30:33:05,911 --> 30:33:11,119 the base class once we have these 41344 30:33:08,320 --> 30:33:14,400 virtual functions in and mark them as 41345 30:33:11,119 --> 30:33:16,799 pure virtual functions by prepending n 41346 30:33:14,399 --> 30:33:19,439 equals zero here this class here is 41347 30:33:16,800 --> 30:33:21,760 going to become an abstract class and 41348 30:33:19,440 --> 30:33:24,639 what that means is that we can't create 41349 30:33:21,759 --> 30:33:28,000 objects of this class it is meant ready 41350 30:33:24,639 --> 30:33:30,551 to be inherited from and downstream 41351 30:33:28,000 --> 30:33:32,000 inheritance classes are going to be 41352 30:33:32,000 --> 30:33:36,479 override these methods here otherwise 41353 30:33:34,399 --> 30:33:38,639 they are themselves going to become 41354 30:33:36,479 --> 30:33:40,800 abstract classes we're going to have a 41355 30:33:38,639 --> 30:33:42,639 chance to look at this here we also have 41356 30:33:40,800 --> 30:33:44,960 our rectangle class which is going to be 41357 30:33:42,639 --> 30:33:47,191 inheriting from shape it is going to 41358 30:33:44,960 --> 30:33:49,040 override our two methods we're going to 41359 30:33:47,191 --> 30:33:50,959 override the perimeter function we're 41360 30:33:49,039 --> 30:33:53,279 going to override the surface function 41361 30:33:50,960 --> 30:33:55,760 for rectangle the perimeter is computed 41362 30:33:53,279 --> 30:33:58,720 this way we're going to multiply 41363 30:33:55,759 --> 30:34:00,799 the width by two and the height by two 41364 30:33:58,720 --> 30:34:02,720 and we're going to add these two things 41365 30:34:00,800 --> 30:34:04,960 up we're going to compute the surface by 41366 30:34:02,720 --> 30:34:06,872 multiplying width and height and this is 41367 30:34:04,960 --> 30:34:08,960 going to give us the data we want we can 41368 30:34:06,872 --> 30:34:10,960 look at circle it is going to also 41369 30:34:08,960 --> 30:34:13,360 inherit from shape and it is going to 41370 30:34:10,960 --> 30:34:16,800 override our methods here if we want we 41371 30:34:13,360 --> 30:34:19,680 can even put in a radius and a variable 41372 30:34:16,800 --> 30:34:21,760 to keep track of pi we can do this you 41373 30:34:19,679 --> 30:34:23,679 can really do this however you want this 41374 30:34:21,759 --> 30:34:26,080 is just going to allow us to compute the 41375 30:34:23,679 --> 30:34:28,231 surface relatively easily in this class 41376 30:34:26,080 --> 30:34:30,400 here let's look at the implementation 41377 30:34:28,232 --> 30:34:32,720 nothing special here this is just going 41378 30:34:30,399 --> 30:34:34,479 to forward the construction of the base 41379 30:34:34,479 --> 30:34:38,959 base constructor and this is going to do 41380 30:34:36,639 --> 30:34:40,960 whatever it is we want to do here let's 41381 30:34:38,960 --> 30:34:43,040 head over to the main cpp file and 41382 30:34:43,039 --> 30:34:49,119 we are going to include circle and 41383 30:34:46,000 --> 30:34:51,759 rectangle let's do that circle dot h and 41384 30:34:49,119 --> 30:34:54,319 we're going to put in rectangle that h 41385 30:34:51,759 --> 30:34:56,720 and we're going to first try and create 41386 30:34:54,320 --> 30:34:59,912 a shape object let's do that we're going 41387 30:34:56,720 --> 30:35:02,399 to say shape and say shape pointer and 41388 30:34:59,911 --> 30:35:04,639 this is going to be new shape we can use 41389 30:35:02,399 --> 30:35:05,911 a default constructor and if we do 41390 30:35:04,639 --> 30:35:07,759 something like this you're going to see 41391 30:35:05,911 --> 30:35:10,000 that we're going to have a compiler 41392 30:35:07,759 --> 30:35:12,959 error here we have a squiggly line but 41393 30:35:10,000 --> 30:35:15,440 if we bring up a terminal window and 41394 30:35:12,960 --> 30:35:18,400 look at the problems tab we're going to 41395 30:35:15,440 --> 30:35:20,800 see that object of abstract class type 41396 30:35:18,399 --> 30:35:22,799 shape is not allowed so shape is an 41397 30:35:22,800 --> 30:35:28,160 and why is it an abstract class because 41398 30:35:25,039 --> 30:35:30,479 we have an at least one pure virtual 41399 30:35:28,160 --> 30:35:32,960 function here we actually have two and 41400 30:35:30,479 --> 30:35:35,512 this is going to make our shape class an 41401 30:35:32,960 --> 30:35:37,912 abstract class what that means is that 41402 30:35:35,512 --> 30:35:39,360 we can't create objects of this class 41403 30:35:37,911 --> 30:35:41,831 and if we do that we're going to get a 41404 30:35:39,360 --> 30:35:43,512 compiler error let's try and build this 41405 30:35:41,831 --> 30:35:46,231 program we're going to pass this through 41406 30:35:43,512 --> 30:35:48,000 gcc our favorite compiler we are going 41407 30:35:46,232 --> 30:35:50,000 to see that the bolt is going to fail 41408 30:35:48,000 --> 30:35:53,360 we're going to have a bunch of errors if 41409 30:35:50,000 --> 30:35:56,551 we go up and look at the error we have 41410 30:35:53,360 --> 30:35:58,320 invalid new expression of abstract class 41411 30:35:58,320 --> 30:36:03,040 so we can't really create an object of 41412 30:36:00,639 --> 30:36:05,759 this let's go down and see what these 41413 30:36:03,039 --> 30:36:07,759 errors are saying note because the 41414 30:36:05,759 --> 30:36:10,231 following virtual functions are pure 41415 30:36:07,759 --> 30:36:12,551 within shape and they are going to try 41416 30:36:10,232 --> 30:36:15,040 and really be helpful in the errors that 41417 30:36:12,551 --> 30:36:17,512 they give here and all they are saying 41418 30:36:15,039 --> 30:36:19,759 is that we can't create objects of this 41419 30:36:17,512 --> 30:36:22,160 class because we have pure virtual 41420 30:36:19,759 --> 30:36:24,799 functions inside okay now you know this 41421 30:36:22,160 --> 30:36:27,360 if a class is abstract you can't create 41422 30:36:24,800 --> 30:36:29,911 objects of that class but you can still 41423 30:36:27,360 --> 30:36:32,800 use a base pointer to manage derived 41424 30:36:29,911 --> 30:36:35,191 objects of that class so let's comment 41425 30:36:32,800 --> 30:36:37,592 this out because this is going to make 41426 30:36:35,191 --> 30:36:39,039 our code not compile and we're going to 41427 30:36:37,592 --> 30:36:41,440 say that this is going to give us a 41428 30:36:39,039 --> 30:36:43,591 compiler error and we're going to try 41429 30:36:41,440 --> 30:36:46,160 and use the base pointer to manage a 41430 30:36:43,592 --> 30:36:48,480 rectangle object we can do this so we're 41431 30:36:46,160 --> 30:36:50,552 going to set up a shape pointer and it 41432 30:36:48,479 --> 30:36:52,639 is going to be managing the memory of a 41433 30:36:50,551 --> 30:36:54,399 rectangle object we're going to go 41434 30:36:52,639 --> 30:36:56,800 through the base pointer and call the 41435 30:36:54,399 --> 30:36:59,119 surface method and this is going to call 41436 30:36:56,800 --> 30:37:01,040 the implementation in rectangle because 41437 30:36:59,119 --> 30:37:03,360 our base pointer is really managing the 41438 30:37:01,039 --> 30:37:06,159 rectangle object if you want you can 41439 30:37:03,360 --> 30:37:08,720 even use the type id operator to print 41440 30:37:06,160 --> 30:37:10,160 the dynamic type of this shape rect 41441 30:37:08,720 --> 30:37:12,080 pointer you're going to see that it is 41442 30:37:10,160 --> 30:37:13,592 managing a rectangle you can actually do 41443 30:37:12,080 --> 30:37:15,831 this because we learned about that in 41444 30:37:13,592 --> 30:37:18,480 the last lecture so let's practice that 41445 30:37:15,831 --> 30:37:21,511 we're going to say dynamic type of shape 41446 30:37:18,479 --> 30:37:23,279 wrap and we're going to say type id and 41447 30:37:21,512 --> 30:37:25,040 we're going to de-reference the pointer 41448 30:37:23,279 --> 30:37:27,039 we're going to say shipwrecked and we're 41449 30:37:25,039 --> 30:37:29,511 going to print the name of this and 41450 30:37:27,039 --> 30:37:31,831 we're going to see it print out on our 41451 30:37:29,512 --> 30:37:33,911 powershell window and after this we are 41452 30:37:31,831 --> 30:37:36,551 going to print the surface that we got 41453 30:37:33,911 --> 30:37:39,119 from this call here and we're going to 41454 30:37:36,551 --> 30:37:40,872 see what this gives us let's try and 41455 30:37:39,119 --> 30:37:43,679 build this program we're going to pass 41456 30:37:40,872 --> 30:37:45,192 it through gcc our favorite compiler the 41457 30:37:43,679 --> 30:37:47,359 bullet is going to be good we're going 41458 30:37:45,191 --> 30:37:50,000 to clear and run rooster we're going to 41459 30:37:47,360 --> 30:37:52,232 see that the dynamic type is rectangle 41460 30:37:50,000 --> 30:37:54,800 you can see that right here so the base 41461 30:37:52,232 --> 30:37:57,592 pointer is really managing a rectangle 41462 30:37:54,800 --> 30:37:59,911 object and this is what we have here and 41463 30:37:57,592 --> 30:38:03,279 if we call the surface method we will be 41464 30:37:59,911 --> 30:38:04,959 calling the method on the dynamic type 41465 30:38:03,279 --> 30:38:07,679 so we will be calling this surface 41466 30:38:04,960 --> 30:38:08,720 method on the rectangle class and we 41467 30:38:08,720 --> 30:38:13,512 using the implementation we have here so 41468 30:38:11,191 --> 30:38:16,479 the surface function that is going to be 41469 30:38:13,512 --> 30:38:18,720 called is what we have on line 20 in 41470 30:38:16,479 --> 30:38:21,360 rectangle here and this is going to just 41471 30:38:18,720 --> 30:38:24,080 be a multiplication of width and height 41472 30:38:21,360 --> 30:38:26,399 and we're going to get 100 here because 41473 30:38:24,080 --> 30:38:28,800 our width and height happen to be 41474 30:38:26,399 --> 30:38:31,679 10 and 10 so multiplying this is going 41475 30:38:28,800 --> 30:38:33,512 to give us 100 as we see here this is 41476 30:38:31,679 --> 30:38:36,159 really cool this is what we expect we 41477 30:38:33,512 --> 30:38:38,400 can even use our base pointer to manage 41478 30:38:36,160 --> 30:38:40,320 a circle object so let's do that we're 41479 30:38:38,399 --> 30:38:43,759 going to put in a separator to be able 41480 30:38:40,320 --> 30:38:46,232 to follow this easily and our output so 41481 30:38:43,759 --> 30:38:48,319 let's say stdndl here and we're going to 41482 30:38:46,232 --> 30:38:50,232 put in the code so we're going to set up 41483 30:38:48,320 --> 30:38:53,120 a base pointer which is really going to 41484 30:38:50,232 --> 30:38:55,680 be managing a circle object here and we 41485 30:38:53,119 --> 30:38:57,591 will call our surface method this is 41486 30:38:57,592 --> 30:39:00,960 polymorphically because surface is a 41487 30:39:00,960 --> 30:39:05,512 so we will get polymorphism here and we 41488 30:39:03,679 --> 30:39:07,831 will call the surface method on the 41489 30:39:05,512 --> 30:39:09,760 circle object which is really our 41490 30:39:07,831 --> 30:39:12,159 dynamic type if we print this out we're 41491 30:39:09,759 --> 30:39:14,959 going to see the surface printed out but 41492 30:39:12,160 --> 30:39:16,720 if we want we can even print the dynamic 41493 30:39:16,720 --> 30:39:20,720 base pointer here so we're going to do 41494 30:39:18,320 --> 30:39:23,592 the same thing we did for rectangle we 41495 30:39:20,720 --> 30:39:26,479 can use our type id operator to print 41496 30:39:23,592 --> 30:39:28,639 the dynamic type of shape circle and 41497 30:39:26,479 --> 30:39:31,119 we're going to say shape circle here and 41498 30:39:28,639 --> 30:39:32,872 this is going to do what we want let's 41499 30:39:31,119 --> 30:39:35,591 look at this we're going to be printing 41500 30:39:32,872 --> 30:39:37,592 the surface here this is exactly what we 41501 30:39:35,592 --> 30:39:40,000 want so let's build and run we're going 41502 30:39:37,592 --> 30:39:41,912 to pass this through gcc the bolt is 41503 30:39:40,000 --> 30:39:42,960 going to go through as you see here if 41504 30:39:42,960 --> 30:39:47,912 and run rooster we're going to see that 41505 30:39:45,360 --> 30:39:50,399 the dynamic type is now a circle so the 41506 30:39:47,911 --> 30:39:52,639 base pointer or the shape pointer we 41507 30:39:50,399 --> 30:39:55,511 have here is really managing 41508 30:39:52,639 --> 30:39:57,831 a circle object polymorphically so if we 41509 30:39:55,512 --> 30:39:59,911 call a virtual function on this base 41510 30:39:57,831 --> 30:40:01,119 pointer it is going to call 41511 30:40:01,119 --> 30:40:05,911 specific implementation of the surface 41512 30:40:03,592 --> 30:40:08,000 object we have and that happens to be 41513 30:40:05,911 --> 30:40:08,959 for this circle object here this is what 41514 30:40:08,960 --> 30:40:12,480 if we compute the surface we're going to 41515 30:40:10,720 --> 30:40:14,872 see that it is going to be using the 41516 30:40:12,479 --> 30:40:17,440 data we have here and we will get this 41517 30:40:14,872 --> 30:40:18,639 number as our surface if you plug this 41518 30:40:17,440 --> 30:40:21,040 in you're going to see that this is 41519 30:40:18,639 --> 30:40:23,592 really right so this is really all i had 41520 30:40:21,039 --> 30:40:26,639 to share in this lecture we can set up 41521 30:40:23,592 --> 30:40:28,400 pure virtual functions in a base class 41522 30:40:26,639 --> 30:40:31,759 and what that's going to do it is going 41523 30:40:28,399 --> 30:40:33,591 to make our class an abstract class but 41524 30:40:31,759 --> 30:40:35,679 we want also be able to put in 41525 30:40:35,679 --> 30:40:39,511 methods anymore in the base class 41526 30:40:37,592 --> 30:40:41,680 because they are meant to be 41527 30:40:39,512 --> 30:40:44,720 overwritten and implemented by 41528 30:40:41,679 --> 30:40:46,639 downstream inheritance classes so if you 41529 30:40:44,720 --> 30:40:49,191 go in here and try to put in an 41530 30:40:46,639 --> 30:40:50,960 implementation we will probably get a 41531 30:40:49,191 --> 30:40:53,119 compiler error because this is not 41532 30:40:50,960 --> 30:40:55,280 allowed you see we have a problem here 41533 30:40:53,119 --> 30:40:57,279 if we try to build we will get a weird 41534 30:40:55,279 --> 30:40:59,831 compiler error let's make sure we see 41535 30:40:57,279 --> 30:41:02,872 the compiler error so world finished 41536 30:40:59,831 --> 30:41:05,191 with errors and we are going to see 41537 30:41:02,872 --> 30:41:06,232 what's the problem here let's go up and 41538 30:41:06,232 --> 30:41:12,639 pure specifier on function definition so 41539 30:41:09,831 --> 30:41:15,591 this is not allowed so we are really 41540 30:41:12,639 --> 30:41:18,000 conflicting ourselves here we are 41541 30:41:15,592 --> 30:41:19,040 marking this function as a pure virtual 41542 30:41:19,039 --> 30:41:24,000 and what that means and a pure virtual 41543 30:41:22,320 --> 30:41:26,400 functions isn't meant to have a 41544 30:41:24,000 --> 30:41:28,000 definition but we are also putting in a 41545 30:41:26,399 --> 30:41:29,911 definition here so the compiler is going 41546 30:41:28,000 --> 30:41:32,160 to be confused this is not something we 41547 30:41:29,911 --> 30:41:34,800 should be doing another thing i want to 41548 30:41:32,160 --> 30:41:35,911 point out before we wrap up this lecture 41549 30:41:35,911 --> 30:41:42,160 downstream inheriting classes are forced 41550 30:41:39,679 --> 30:41:44,719 to override and implement all the pure 41551 30:41:42,160 --> 30:41:47,440 virtual functions from the class that 41552 30:41:44,720 --> 30:41:49,440 they inherit from if they don't do that 41553 30:41:47,440 --> 30:41:51,440 they are themselves going to become 41554 30:41:49,440 --> 30:41:54,232 abstract and we want to be able to 41555 30:41:51,440 --> 30:41:56,080 create objects of them so let's try and 41556 30:41:54,232 --> 30:41:58,232 for example comment out the surface 41557 30:41:56,080 --> 30:42:02,000 implementation here and that's going to 41558 30:41:58,232 --> 30:42:03,911 make our circle class an abstract class 41559 30:42:02,000 --> 30:42:06,000 and if we try to create an object with 41560 30:42:03,911 --> 30:42:08,639 circle we will get a compiler error 41561 30:42:06,000 --> 30:42:10,232 because now circle is an abstract class 41562 30:42:08,639 --> 30:42:12,479 let's try and build and show you the 41563 30:42:10,232 --> 30:42:15,279 compiler error and this can really be 41564 30:42:12,479 --> 30:42:17,759 confusing if you are a beginner on this 41565 30:42:15,279 --> 30:42:20,720 you will be wondering why you can't 41566 30:42:17,759 --> 30:42:23,831 create objects of your class even if you 41567 30:42:20,720 --> 30:42:26,232 didn't really explicitly put in any pure 41568 30:42:23,831 --> 30:42:29,360 virtual function the reason is we are 41569 30:42:26,232 --> 30:42:32,720 inheriting from an abstract class that 41570 30:42:29,360 --> 30:42:35,759 has two pure virtual functions but we 41571 30:42:32,720 --> 30:42:38,160 are only overriding one pure virtual 41572 30:42:35,759 --> 30:42:40,399 function we need to override all of them 41573 30:42:38,160 --> 30:42:42,720 otherwise our class is also going to be 41574 30:42:40,399 --> 30:42:44,639 an abstract class let's look at the 41575 30:42:42,720 --> 30:42:47,440 compiler error we are getting here let's 41576 30:42:44,639 --> 30:42:49,911 go up and see if we can find it we are 41577 30:42:47,440 --> 30:42:52,479 going to say in function in main 41578 30:42:49,911 --> 30:42:55,119 evaluate new expression of abstract 41579 30:42:52,479 --> 30:42:58,159 class type circle we are trying to 41580 30:42:55,119 --> 30:43:00,231 create an object from an abstract class 41581 30:42:58,160 --> 30:43:01,832 this is the error we have here and you 41582 30:43:01,831 --> 30:43:06,551 so let's go back and make sure we are 41583 30:43:04,399 --> 30:43:08,720 overriding all these stunts because we 41584 30:43:06,551 --> 30:43:11,512 need to do that to be able to create 41585 30:43:08,720 --> 30:43:14,080 objects of a circle and now this is 41586 30:43:11,512 --> 30:43:16,160 going to work exactly as it should let's 41587 30:43:14,080 --> 30:43:18,232 build and make sure of that the bullet 41588 30:43:16,160 --> 30:43:20,800 is going to be good we are fine this is 41589 30:43:18,232 --> 30:43:22,480 working exactly as we want this is 41590 30:43:20,800 --> 30:43:24,720 really all i wanted you to see in this 41591 30:43:22,479 --> 30:43:27,119 lecture i hope you found it interesting 41592 30:43:24,720 --> 30:43:28,960 we are going to stop here in this one 41593 30:43:27,119 --> 30:43:31,591 the next one we're going to learn about 41594 30:43:28,960 --> 30:43:33,440 interfaces in c plus plus and they are 41595 30:43:31,592 --> 30:43:35,360 going to be reusing the knowledge we 41596 30:43:33,440 --> 30:43:37,832 learned about in this lecture go ahead 41597 30:43:35,360 --> 30:43:39,360 and finish up here and meet me there in 41598 30:43:37,831 --> 30:43:43,119 this lecture we're going to see that we 41599 30:43:39,360 --> 30:43:45,592 can model interfaces in c plus plus 41600 30:43:45,592 --> 30:43:50,000 and now there's a lot of things that 41601 30:43:47,279 --> 30:43:52,479 might not make sense in what i just said 41602 30:43:50,000 --> 30:43:55,911 so let's break this apart an interface 41603 30:43:52,479 --> 30:43:59,440 can be thought of as an abstract class 41604 30:43:55,911 --> 30:44:02,080 with only pure virtual functions and no 41605 30:43:59,440 --> 30:44:03,832 member variable so if you have a setup 41606 30:44:02,080 --> 30:44:06,232 like that you can call that thing an 41607 30:44:03,831 --> 30:44:08,159 interface let's look at a simple example 41608 30:44:06,232 --> 30:44:10,232 here here we have a simple class called 41609 30:44:08,160 --> 30:44:12,872 stream insertable you see that it 41610 30:44:10,232 --> 30:44:16,232 doesn't have any member variable it only 41611 30:44:12,872 --> 30:44:18,232 has a pure virtual function here and a 41612 30:44:16,232 --> 30:44:20,400 helper function that we could set up to 41613 30:44:18,232 --> 30:44:23,832 help us do a few things the only 41614 30:44:20,399 --> 30:44:26,720 specification to make this an interface 41615 30:44:23,831 --> 30:44:29,511 is to have in only pure virtual 41616 30:44:26,720 --> 30:44:31,360 functions and no member variables if you 41617 30:44:29,512 --> 30:44:33,760 have in a member variable this is going 41618 30:44:31,360 --> 30:44:35,440 to stop becoming an interface in c plus 41619 30:44:33,759 --> 30:44:38,000 plus okay so you can think of an 41620 30:44:35,440 --> 30:44:40,960 interface as an abstract class 41621 30:44:38,000 --> 30:44:43,119 with at least one pure virtual function 41622 30:44:40,960 --> 30:44:45,440 and no member variable that's going to 41623 30:44:43,119 --> 30:44:48,159 make it an interface down here you see 41624 30:44:45,440 --> 30:44:51,119 that an interface can be thought of as a 41625 30:44:48,160 --> 30:44:54,080 specification of something that will be 41626 30:44:51,119 --> 30:44:56,551 fully implemented in a derived class 41627 30:44:54,080 --> 30:44:59,680 but the specification itself is going to 41628 30:44:56,551 --> 30:45:02,551 reside in the abstract class another way 41629 30:44:59,679 --> 30:45:05,039 to think about an interface is to treat 41630 30:45:05,039 --> 30:45:10,319 you can attach to your types to give 41631 30:45:07,279 --> 30:45:12,160 them superpowers for example this stream 41632 30:45:12,160 --> 30:45:17,911 has a single job and that's going to 41633 30:45:14,479 --> 30:45:20,872 allow our types to insert data into 41634 30:45:17,911 --> 30:45:22,800 output streams like sddc out this is the 41635 30:45:20,872 --> 30:45:25,440 only thing this interface is going to 41636 30:45:22,800 --> 30:45:27,360 add to our times so we can take this 41637 30:45:25,440 --> 30:45:29,360 interface and attach that to a point 41638 30:45:27,360 --> 30:45:31,759 class for example and we will 41639 30:45:29,360 --> 30:45:34,479 automatically be able to print point 41640 30:45:31,759 --> 30:45:37,039 objects to the output stream and we want 41641 30:45:34,479 --> 30:45:39,591 to need to overload the stream output 41642 30:45:37,039 --> 30:45:41,679 operator in the point class we can do 41643 30:45:39,592 --> 30:45:44,160 the same thing to our bird class we can 41644 30:45:41,679 --> 30:45:46,079 do the same thing to our dog class and 41645 30:45:44,160 --> 30:45:48,232 we will be able to print these things 41646 30:45:46,080 --> 30:45:50,400 without having to overload the stream 41647 30:45:48,232 --> 30:45:53,512 output operator hopefully you can see 41648 30:45:50,399 --> 30:45:55,679 how powerful this is if we manage to set 41649 30:45:53,512 --> 30:45:58,720 up this kind of relationship between our 41650 30:45:55,679 --> 30:46:00,799 type and our interfaces so again an 41651 30:45:58,720 --> 30:46:03,592 interface is something you can attach to 41652 30:46:00,800 --> 30:46:05,832 your types to give them powers or 41653 30:46:03,592 --> 30:46:07,912 features that they originally didn't 41654 30:46:05,831 --> 30:46:10,231 have and that's going to save you from 41655 30:46:07,911 --> 30:46:12,399 having to implement these features 41656 30:46:10,232 --> 30:46:14,400 separately in each class for real time 41657 30:46:12,399 --> 30:46:16,799 so this is a huge time saver now you 41658 30:46:14,399 --> 30:46:19,439 must be asking how do i take my 41659 30:46:16,800 --> 30:46:22,080 interface and attach it to my type well 41660 30:46:19,440 --> 30:46:25,760 the way c plus plus does this you have 41661 30:46:22,080 --> 30:46:28,160 to inherit from the interface from your 41662 30:46:25,759 --> 30:46:30,080 type to attach the interface to your 41663 30:46:28,160 --> 30:46:32,872 type so for example if we wanted to 41664 30:46:30,080 --> 30:46:34,720 attach this interface to our point class 41665 30:46:32,872 --> 30:46:37,440 what we would do is something like this 41666 30:46:34,720 --> 30:46:40,720 we would set up our point class 41667 30:46:37,440 --> 30:46:42,800 and inherit from our interface which is 41668 30:46:40,720 --> 30:46:44,720 stream insertable the moment we do this 41669 30:46:42,800 --> 30:46:47,192 our point is going to be able to print 41670 30:46:44,720 --> 30:46:49,512 thanks to the console through the stream 41671 30:46:47,191 --> 30:46:52,159 output operator but hopefully you can 41672 30:46:49,512 --> 30:46:55,040 see that something is missing how does 41673 30:46:52,160 --> 30:46:57,360 our point class know how to print stuff 41674 30:46:55,039 --> 30:46:59,591 if we go up and look at our stream 41675 30:46:57,360 --> 30:47:02,160 insertable interface we're going to see 41676 30:46:59,592 --> 30:47:05,040 that it is declaring a stream output 41677 30:47:02,160 --> 30:47:07,592 operator it is this thing that is used 41678 30:47:05,039 --> 30:47:09,679 to print data to the output stream you 41679 30:47:07,592 --> 30:47:12,960 see this is a regular output stream 41680 30:47:09,679 --> 30:47:16,000 operator we will be printing to sddo 41681 30:47:12,960 --> 30:47:18,160 stream and the object is named out here 41682 30:47:16,000 --> 30:47:20,080 and we will be printing whatever operand 41683 30:47:18,160 --> 30:47:22,400 that is passed as a second parameter 41684 30:47:22,399 --> 30:47:25,591 we will need to set up a virtual 41685 30:47:25,592 --> 30:47:31,680 that will need to be implemented by 41686 30:47:28,080 --> 30:47:34,080 whoever inherits from our interface here 41687 30:47:31,679 --> 30:47:36,159 and this is going to be super powerful 41688 30:47:34,080 --> 30:47:38,872 we are going to take over the job to 41689 30:47:36,160 --> 30:47:42,232 print to the output stream but we will 41690 30:47:38,872 --> 30:47:44,232 let inheriting download classes to 41691 30:47:42,232 --> 30:47:47,760 specify what we print and they are going 41692 30:47:44,232 --> 30:47:49,832 to specify that in this virtual method 41693 30:47:47,759 --> 30:47:52,639 let's look at our point class again and 41694 30:47:49,831 --> 30:47:55,279 really put the puzzles together here and 41695 30:47:52,639 --> 30:47:57,831 understand how this works okay this is 41696 30:47:55,279 --> 30:48:00,551 our point class and it is going to be 41697 30:47:57,831 --> 30:48:02,871 inheriting from our interface and it is 41698 30:48:00,551 --> 30:48:04,720 going to implement our virtual function 41699 30:48:02,872 --> 30:48:07,040 here we are going to set it up here and 41700 30:48:04,720 --> 30:48:09,279 we are going to mark it as an override 41701 30:48:07,039 --> 30:48:11,191 because it is overriding a method that 41702 30:48:09,279 --> 30:48:13,119 comes from our stream insertable 41703 30:48:11,191 --> 30:48:16,159 interface here and it is going to 41704 30:48:13,119 --> 30:48:18,159 specify which data we print to the 41705 30:48:16,160 --> 30:48:20,552 output stream and how the data is going 41706 30:48:18,160 --> 30:48:22,639 to be printed out you can specify 41707 30:48:20,551 --> 30:48:24,319 these things here we're going to format 41708 30:48:22,639 --> 30:48:27,039 the data in a point and we're going to 41709 30:48:24,320 --> 30:48:30,000 be using these square brackets to put mx 41710 30:48:27,039 --> 30:48:33,759 and my here and notice that this is an 41711 30:48:30,000 --> 30:48:36,000 override so the inheritance class has 41712 30:48:33,759 --> 30:48:38,551 all the information we need to put in 41713 30:48:36,000 --> 30:48:40,800 our output stream operator and we will 41714 30:48:38,551 --> 30:48:43,119 do that through our stream insert 41715 30:48:40,800 --> 30:48:46,551 override here now let's go back to our 41716 30:48:43,119 --> 30:48:48,959 stream insertable interface to see how 41717 30:48:46,551 --> 30:48:51,591 our output stream operator actually 41718 30:48:48,960 --> 30:48:53,512 works what we have here is what would be 41719 30:48:51,592 --> 30:48:55,760 a header and this is going to just put 41720 30:48:53,512 --> 30:48:58,400 in a declaration for this but we can 41721 30:48:55,759 --> 30:49:00,639 even look at its implementation the 41722 30:48:58,399 --> 30:49:02,720 implementation for our output stream 41723 30:49:00,639 --> 30:49:04,551 operator may look something like this we 41724 30:49:02,720 --> 30:49:06,872 are going to pass in the first operand 41725 30:49:04,551 --> 30:49:09,191 and the second operand here but notice 41726 30:49:06,872 --> 30:49:10,000 what we are doing here we are going to 41727 30:49:10,000 --> 30:49:16,399 the stream insert virtual function on 41728 30:49:13,191 --> 30:49:17,360 our operand here and this is going to 41729 30:49:17,360 --> 30:49:22,872 the most specific implementation we can 41730 30:49:20,320 --> 30:49:24,720 find for the stream insert if we are 41731 30:49:22,872 --> 30:49:27,592 trying to print a circle for example 41732 30:49:24,720 --> 30:49:30,320 this is going to call the stream insert 41733 30:49:27,592 --> 30:49:32,872 override on a circle object if we are 41734 30:49:30,320 --> 30:49:35,440 trying to print a point this is going to 41735 30:49:32,872 --> 30:49:37,512 call this method on our point object and 41736 30:49:35,440 --> 30:49:40,232 this is going to use polymorphism 41737 30:49:37,512 --> 30:49:42,479 because our second operand here is pass 41738 30:49:40,232 --> 30:49:44,480 by reference hopefully you can see how 41739 30:49:42,479 --> 30:49:46,551 this is super powerful i know this is 41740 30:49:44,479 --> 30:49:49,039 convoluted because we are jumping around 41741 30:49:46,551 --> 30:49:51,360 trying to explain this but i couldn't 41742 30:49:49,039 --> 30:49:54,159 really come up with an easier way to put 41743 30:49:51,360 --> 30:49:57,191 this we have to try this encode to 41744 30:49:54,160 --> 30:49:59,680 really see the power of this but this is 41745 30:49:57,191 --> 30:50:02,000 really the power of interfaces we can 41746 30:49:59,679 --> 30:50:04,479 design our features and wrap them in an 41747 30:50:02,000 --> 30:50:06,551 interface and we can really attach that 41748 30:50:04,479 --> 30:50:09,039 interface to any type we have in our c 41749 30:50:06,551 --> 30:50:11,512 plus plus program and that type is going 41750 30:50:09,039 --> 30:50:14,079 to automatically inherit the features 41751 30:50:11,512 --> 30:50:16,400 that we have set up in our interface for 41752 30:50:14,080 --> 30:50:19,279 example now that we have attached this 41753 30:50:16,399 --> 30:50:21,439 interface to our point here we can print 41754 30:50:19,279 --> 30:50:23,360 our points on the output stream and this 41755 30:50:21,440 --> 30:50:25,512 is going to work automatically you see 41756 30:50:23,360 --> 30:50:28,320 that we don't need to set up an output 41757 30:50:25,512 --> 30:50:30,800 stream operator on our class altogether 41758 30:50:28,320 --> 30:50:33,360 we can even go further and attach this 41759 30:50:30,800 --> 30:50:35,911 interface to a full inheritance 41760 30:50:33,360 --> 30:50:38,551 hierarchy for example if we have our 41761 30:50:35,911 --> 30:50:40,551 base class to be animal and we inherit 41762 30:50:38,551 --> 30:50:43,759 from this class to create all kinds of 41763 30:50:40,551 --> 30:50:46,479 crazy classes we can go on and attach 41764 30:50:43,759 --> 30:50:49,591 our interface to the base class here and 41765 30:50:46,479 --> 30:50:52,000 this is going to make all our classes in 41766 30:50:49,592 --> 30:50:54,480 our inheritance hierarchy have the 41767 30:50:52,000 --> 30:50:57,191 features that the interface has brought 41768 30:50:54,479 --> 30:50:59,759 to the base class so for example we can 41769 30:50:57,191 --> 30:51:02,479 attach our interface to the animal class 41770 30:50:59,759 --> 30:51:05,039 by inheriting from stream insertable 41771 30:51:02,479 --> 30:51:08,399 here and this is going to give our 41772 30:51:05,039 --> 30:51:10,871 animal the powers to be printed out to 41773 30:51:08,399 --> 30:51:13,119 output streams and this is going to be 41774 30:51:10,872 --> 30:51:15,592 powered by the virtual function that we 41775 30:51:15,592 --> 30:51:20,639 we are going to specify which data to 41776 30:51:18,479 --> 30:51:22,872 print in our animal class and how the 41777 30:51:20,639 --> 30:51:24,960 data is going to be printed out here and 41778 30:51:22,872 --> 30:51:27,040 we can do that in this override and this 41779 30:51:24,960 --> 30:51:28,960 is super powerful once we do this we're 41780 30:51:27,039 --> 30:51:31,679 going to go to each class in our 41781 30:51:28,960 --> 30:51:33,280 inheritance hierarchy and implement the 41782 30:51:33,279 --> 30:51:37,191 override because if we don't do that 41783 30:51:35,592 --> 30:51:40,000 this class is also going to become an 41784 30:51:37,191 --> 30:51:42,399 abstract class because it is inheriting 41785 30:51:40,000 --> 30:51:45,279 from a class that has a pure virtual 41786 30:51:42,399 --> 30:51:48,319 function remember if you inherit from a 41787 30:51:45,279 --> 30:51:51,119 class that has at least one pure virtual 41788 30:51:48,320 --> 30:51:54,000 function you will be forced to implement 41789 30:51:51,119 --> 30:51:55,831 all the pure virtual functions that come 41790 30:51:54,000 --> 30:51:58,232 from your parent class if you don't do 41791 30:51:55,831 --> 30:52:00,159 that you are going to become an abstract 41792 30:51:58,232 --> 30:52:02,639 class yourself and we don't really want 41793 30:52:00,160 --> 30:52:05,192 that that's why we are overriding the 41794 30:52:02,639 --> 30:52:07,279 stream insert override here and with 41795 30:52:05,191 --> 30:52:09,360 this we are going to specify which 41796 30:52:07,279 --> 30:52:11,592 information to print from our object 41797 30:52:09,360 --> 30:52:14,080 here and here we are going to put out 41798 30:52:11,592 --> 30:52:16,480 the description and the wing color and 41799 30:52:14,080 --> 30:52:18,800 this is the format we specify here and 41800 30:52:16,479 --> 30:52:21,191 this method is going to be called by our 41801 30:52:18,800 --> 30:52:24,232 output stream operator which is coming 41802 30:52:21,191 --> 30:52:26,231 from our stream insertable interface 41803 30:52:24,232 --> 30:52:28,000 hopefully you can see how this is super 41804 30:52:26,232 --> 30:52:29,360 powerful okay now that you know this 41805 30:52:28,000 --> 30:52:31,119 we're going to head over to visual 41806 30:52:29,360 --> 30:52:34,080 studio code and play with us a little 41807 30:52:31,119 --> 30:52:36,959 more here we are in our working folder 41808 30:52:34,080 --> 30:52:39,831 the current project is abstract classes 41809 30:52:36,960 --> 30:52:42,960 as interfaces we're going to grab our 41810 30:52:39,831 --> 30:52:45,039 template files and put the data in place 41811 30:52:42,960 --> 30:52:46,232 and i'm going to copy this and put that 41812 30:52:46,232 --> 30:52:50,480 we are going to put in the files we're 41813 30:52:48,160 --> 30:52:52,960 going to be using in our inheritance 41814 30:52:50,479 --> 30:52:55,512 hierarchy you see we have our animal 41815 30:52:52,960 --> 30:52:58,160 hierarchy here you can grab these files 41816 30:52:55,512 --> 30:53:00,080 from the resource section of this 41817 30:52:58,160 --> 30:53:02,480 lecture and you can reuse them like we 41818 30:53:00,080 --> 30:53:04,160 are using here or if you want you can 41819 30:53:02,479 --> 30:53:05,911 even type this but i don't really 41820 30:53:04,160 --> 30:53:07,911 recommend this because this is too much 41821 30:53:05,911 --> 30:53:10,080 typing we just want to learn about 41822 30:53:07,911 --> 30:53:11,831 interfaces here and that's what you 41823 30:53:10,080 --> 30:53:14,320 should focus on so we're going to open 41824 30:53:11,831 --> 30:53:16,551 this up in videos video code and this is 41825 30:53:14,320 --> 30:53:18,960 going to give us all the files we need 41826 30:53:16,551 --> 30:53:21,191 we're going to look at the top class and 41827 30:53:18,960 --> 30:53:24,400 we will see that it is inheriting from 41828 30:53:21,191 --> 30:53:27,119 our stream insertable interface so let's 41829 30:53:24,399 --> 30:53:29,039 look at our stream insertable interface 41830 30:53:27,119 --> 30:53:32,080 because this is the focus of this 41831 30:53:29,039 --> 30:53:33,831 lecture here so it is a regular class as 41832 30:53:32,080 --> 30:53:35,680 you see here it is class stream 41833 30:53:33,831 --> 30:53:38,399 insertable we're going to set up an 41834 30:53:35,679 --> 30:53:40,719 output stream operator and it is really 41835 30:53:38,399 --> 30:53:42,079 like any other output stream operator 41836 30:53:40,720 --> 30:53:44,639 you have seen we're going to pass the 41837 30:53:44,639 --> 30:53:49,512 and we are going to specify our pure 41838 30:53:47,279 --> 30:53:52,000 virtual function which is going to be 41839 30:53:49,512 --> 30:53:54,800 implemented by downstream inheriting 41840 30:53:52,000 --> 30:53:57,440 classes from this class here and this is 41841 30:53:54,800 --> 30:53:58,872 going to set up polymorphism and what 41842 30:53:58,872 --> 30:54:04,400 is if we call this method on our second 41843 30:54:02,000 --> 30:54:06,800 operand here we are going to call this 41844 30:54:04,399 --> 30:54:08,479 method polymorphically so if you are 41845 30:54:06,800 --> 30:54:10,551 trying to print a point we are going to 41846 30:54:08,479 --> 30:54:12,959 call this method on a point object and 41847 30:54:10,551 --> 30:54:15,360 we will print it out exactly how the 41848 30:54:12,960 --> 30:54:17,680 point wants to be printed if we are 41849 30:54:15,360 --> 30:54:20,080 printing a circle we will call this 41850 30:54:17,679 --> 30:54:22,719 stream insert method on the circle and 41851 30:54:20,080 --> 30:54:25,360 we will print a circle exactly how a 41852 30:54:22,720 --> 30:54:27,759 circle wants to be printed out let's 41853 30:54:25,360 --> 30:54:29,680 look at the implementation and it is 41854 30:54:27,759 --> 30:54:32,479 nothing really complicated we're going 41855 30:54:29,679 --> 30:54:34,871 to specify our output stream object as 41856 30:54:32,479 --> 30:54:37,039 the first parameter here and the second 41857 30:54:34,872 --> 30:54:39,192 parameter is what we want to print out 41858 30:54:37,039 --> 30:54:41,759 we will pass this by reference and this 41859 30:54:39,191 --> 30:54:43,591 is going to allow us to do polymorphism 41860 30:54:41,759 --> 30:54:45,759 remember that stream insertable is 41861 30:54:43,592 --> 30:54:48,872 really going to become our top base 41862 30:54:45,759 --> 30:54:51,591 class so we will basically be setting up 41863 30:54:48,872 --> 30:54:53,512 stream insertable polymorphism here you 41864 30:54:51,592 --> 30:54:56,552 need to keep this in mind we are going 41865 30:54:53,512 --> 30:54:58,160 to call our stream insert method on our 41866 30:54:56,551 --> 30:55:00,479 operand and we're going to pass the 41867 30:54:58,160 --> 30:55:02,960 stream output we want to print on and 41868 30:55:00,479 --> 30:55:05,512 we're going to return this output stream 41869 30:55:05,512 --> 30:55:11,512 like a regular output stream operator to 41870 30:55:08,551 --> 30:55:14,231 allow output chaining in our operator 41871 30:55:11,512 --> 30:55:16,400 here once we have this stream insertable 41872 30:55:14,232 --> 30:55:19,279 interface we can really use it however 41873 30:55:16,399 --> 30:55:22,079 we want let's head over to the main cpp 41874 30:55:19,279 --> 30:55:25,119 file and try to play with us so what we 41875 30:55:22,080 --> 30:55:27,279 are going to do is crack this open and 41876 30:55:25,119 --> 30:55:30,639 we are going to remove whatever it is we 41877 30:55:27,279 --> 30:55:32,639 don't need we are going to include our 41878 30:55:30,639 --> 30:55:35,232 interface so we're going to include 41879 30:55:32,639 --> 30:55:37,759 stream insertable let's do that 41880 30:55:35,232 --> 30:55:39,592 insertable.h i think this is the name 41881 30:55:37,759 --> 30:55:42,399 and we're going to set up a point class 41882 30:55:39,592 --> 30:55:44,720 which we wish to be printed out on the 41883 30:55:42,399 --> 30:55:47,360 output stream notice that we want to 41884 30:55:44,720 --> 30:55:50,000 inherit from stream insertable and what 41885 30:55:47,360 --> 30:55:52,872 this is really doing is attaching the 41886 30:55:50,000 --> 30:55:55,592 interface to the point class here and 41887 30:55:52,872 --> 30:55:58,551 because this stream insertable 41888 30:55:55,592 --> 30:56:00,000 interface has a pure virtual function 41889 30:56:00,000 --> 30:56:04,800 override this pure virtual function here 41890 30:56:02,872 --> 30:56:07,040 if we don't we want to be able to create 41891 30:56:04,800 --> 30:56:08,479 objects of our point here let's go down 41892 30:56:07,039 --> 30:56:11,039 and show you that we're going to get 41893 30:56:08,479 --> 30:56:12,800 that problem if we don't do that we're 41894 30:56:12,800 --> 30:56:16,872 and say p1 and for example put in 10 and 41895 30:56:16,872 --> 30:56:22,080 we will have a compiler error because 41896 30:56:19,191 --> 30:56:24,720 now the point class is an abstract class 41897 30:56:22,080 --> 30:56:27,279 because it is inherited from a class 41898 30:56:24,720 --> 30:56:29,680 that has a pure virtual function and it 41899 30:56:27,279 --> 30:56:30,639 is not overriding that pure virtual 41900 30:56:30,639 --> 30:56:37,191 so in other words this class still has a 41901 30:56:34,512 --> 30:56:38,800 non-implemented pure virtual function 41902 30:56:38,800 --> 30:56:43,440 an abstract class and we can't create an 41903 30:56:41,119 --> 30:56:45,191 object of it if we're trying to run this 41904 30:56:43,440 --> 30:56:48,320 code we're going to pass this through 41905 30:56:45,191 --> 30:56:50,231 gcc we will get a compiler okay we have 41906 30:56:48,320 --> 30:56:52,960 a compiler error here and we need to fix 41907 30:56:50,232 --> 30:56:55,832 this all we need to do is to implement 41908 30:56:52,960 --> 30:56:58,552 the interface that comes from our stream 41909 30:56:55,831 --> 30:57:01,511 insertable interface here so we can grab 41910 30:56:58,551 --> 30:57:05,279 our pure virtual function and implement 41911 30:57:01,512 --> 30:57:07,440 it in our main cpp file for the point 41912 30:57:05,279 --> 30:57:10,639 class we're going to put it in here and 41913 30:57:07,440 --> 30:57:12,551 we will remove the pure virtual function 41914 30:57:10,639 --> 30:57:15,039 specifier here and we will put in an 41915 30:57:12,551 --> 30:57:17,279 implementation we want to specify that 41916 30:57:15,039 --> 30:57:21,360 this is an override of a method that is 41917 30:57:17,279 --> 30:57:23,039 coming from our upstream parent class 41918 30:57:21,360 --> 30:57:25,680 and we are going to head over in the 41919 30:57:23,039 --> 30:57:27,759 body of our function and put in the 41920 30:57:25,679 --> 30:57:29,911 information we want to put out 41921 30:57:27,759 --> 30:57:33,591 this is a point object so we just want 41922 30:57:29,911 --> 30:57:36,080 to print the m x coordinate and the m y 41923 30:57:33,592 --> 30:57:39,592 coordinate here and this is the format 41924 30:57:36,080 --> 30:57:41,680 that we want and at this moment the job 41925 30:57:39,592 --> 30:57:44,720 of this function is really done 41926 30:57:41,679 --> 30:57:46,799 and it will be picked up polymorphically 41927 30:57:44,720 --> 30:57:49,360 if somebody tries to call this on a 41928 30:57:46,800 --> 30:57:50,960 point object through polymorphism and 41929 30:57:49,360 --> 30:57:53,191 this is going to work now that we have 41930 30:57:50,960 --> 30:57:55,512 this point we can try and build and make 41931 30:57:53,191 --> 30:57:57,759 sure this is working let's make sure we 41932 30:57:55,512 --> 30:57:59,911 don't have the compiler error anymore 41933 30:57:57,759 --> 30:58:02,399 the world finished successfully so we 41934 30:57:59,911 --> 30:58:04,399 are cool here but what we want to see is 41935 30:58:04,399 --> 30:58:09,591 point objects because that's the whole 41936 30:58:06,320 --> 30:58:11,680 point of attaching this interface to our 41937 30:58:09,592 --> 30:58:13,279 point class here so what we are going to 41938 30:58:11,679 --> 30:58:15,831 do is print this out we're going to say 41939 30:58:13,279 --> 30:58:18,551 sddc out we're going to say p1 and we're 41940 30:58:15,831 --> 30:58:21,039 going to say p1 here this is how we 41941 30:58:18,551 --> 30:58:22,800 would use our output stream operator you 41942 30:58:21,039 --> 30:58:25,039 see we don't have a compiler error this 41943 30:58:22,800 --> 30:58:27,279 is really magic some of you are going to 41944 30:58:25,039 --> 30:58:28,719 ask how is this even working somebody is 41945 30:58:27,279 --> 30:58:30,399 going to look at your class they are 41946 30:58:28,720 --> 30:58:32,720 going to look you don't have an output 41947 30:58:30,399 --> 30:58:35,511 stream operator but you can print your 41948 30:58:32,720 --> 30:58:37,279 objects so how is this working well this 41949 30:58:35,512 --> 30:58:40,000 is working through the magic of 41950 30:58:37,279 --> 30:58:43,119 interfaces we are attaching this 41951 30:58:40,000 --> 30:58:45,512 interface to our point class and this is 41952 30:58:43,119 --> 30:58:47,591 going to give our class the powers that 41953 30:58:45,512 --> 30:58:50,232 come from this interface and one of 41954 30:58:47,592 --> 30:58:52,552 those powers is to use the output stream 41955 30:58:50,232 --> 30:58:54,872 operator we get from this interface we 41956 30:58:52,551 --> 30:58:56,159 can use that directly and what this is 41957 30:58:54,872 --> 30:58:58,551 going to do let's look at the 41958 30:58:56,160 --> 30:59:01,512 implementation of this interface this is 41959 30:58:58,551 --> 30:59:04,479 going to just call the stream insert 41960 30:59:01,512 --> 30:59:07,040 virtual method on the object that we 41961 30:59:04,479 --> 30:59:09,512 pass as a second parameter in this 41962 30:59:07,039 --> 30:59:11,360 operator here and the second parameter 41963 30:59:09,512 --> 30:59:13,911 happens to be the point that we are 41964 30:59:11,360 --> 30:59:15,191 printing here so what this is really 41965 30:59:15,191 --> 30:59:19,679 is something like this we're going to 41966 30:59:17,360 --> 30:59:22,720 try and figure out what the compiler is 41967 30:59:19,679 --> 30:59:25,279 going to try and do we are going to say 41968 30:59:22,720 --> 30:59:27,440 p1 and we're going to say operator 41969 30:59:25,279 --> 30:59:30,232 stream output and we're going to say 41970 30:59:27,440 --> 30:59:32,080 stdc out that's the stream we want to be 41971 30:59:32,080 --> 30:59:36,232 and the point we want to print is p1 41972 30:59:34,551 --> 30:59:38,639 this is basically what the compiler is 41973 30:59:36,232 --> 30:59:41,040 going to do and it is going to print our 41974 30:59:38,639 --> 30:59:43,679 thumb and this method is available in 41975 30:59:41,039 --> 30:59:46,719 our stream insertable header and it is a 41976 30:59:43,679 --> 30:59:49,039 friend of our interface that's why we 41977 30:59:46,720 --> 30:59:51,360 are able to do something like this 41978 30:59:49,039 --> 30:59:52,871 accessing the data from our interface 41979 30:59:51,360 --> 30:59:54,232 let's build and actually show you that 41980 30:59:52,872 --> 30:59:57,120 this is going to work we're going to 41981 30:59:54,232 --> 30:59:58,800 pass this through our gcc compiler the 41982 30:59:57,119 --> 31:00:01,360 build is going to be good so we can 41983 30:59:58,800 --> 31:00:03,279 clear not clear we're going to control z 41984 31:00:01,360 --> 31:00:05,440 on this and we're going to bring up a 41985 31:00:03,279 --> 31:00:07,440 powershell window and we're going to 41986 31:00:05,440 --> 31:00:09,040 clear this time and run rooster you see 41987 31:00:07,440 --> 31:00:10,872 that we are creating our point here and 41988 31:00:09,039 --> 31:00:13,360 this is really cool we can even 41989 31:00:10,872 --> 31:00:15,760 uncomment this and we're going to use 41990 31:00:13,360 --> 31:00:18,232 sddc out because the compiler is going 41991 31:00:15,759 --> 31:00:21,360 to take this and turn that into this and 41992 31:00:18,232 --> 31:00:23,440 we have the ability to have code that is 41993 31:00:21,360 --> 31:00:25,592 much more readable by c plus plus 41994 31:00:23,440 --> 31:00:27,760 developers out there we're going to 41995 31:00:25,592 --> 31:00:30,160 build and run this again the build is 41996 31:00:27,759 --> 31:00:32,159 going to be good we can clear and run 41997 31:00:30,160 --> 31:00:34,872 rooster and this is going to print our 41998 31:00:32,160 --> 31:00:37,192 point here this is really cool but this 41999 31:00:34,872 --> 31:00:40,551 is not the end of our store we can even 42000 31:00:37,191 --> 31:00:43,759 print an entire inheritance hierarchy 42001 31:00:40,551 --> 31:00:47,039 look at what we did at our animal class 42002 31:00:43,759 --> 31:00:49,759 here we attached this interface to our 42003 31:00:47,039 --> 31:00:52,639 animal class and the moment we do that 42004 31:00:49,759 --> 31:00:53,591 we need to override the pure virtual 42005 31:00:53,592 --> 31:00:57,912 that comes with this interface and 42006 31:00:55,592 --> 31:01:00,552 that's what we did down here we have 42007 31:00:57,911 --> 31:01:02,000 overwritten the stream insert 42008 31:01:02,000 --> 31:01:06,399 and in the body we're going to specify 42009 31:01:04,232 --> 31:01:08,800 which data we want to print and how to 42010 31:01:06,399 --> 31:01:11,360 print that data that's the whole point 42011 31:01:08,800 --> 31:01:14,160 of this override we are doing here we 42012 31:01:11,360 --> 31:01:16,479 are going to do the same thing in each 42013 31:01:14,160 --> 31:01:18,400 inheritance class from animal for 42014 31:01:16,479 --> 31:01:20,720 example if we go to feline we're going 42015 31:01:18,399 --> 31:01:23,279 to do exactly the same thing we are 42016 31:01:20,720 --> 31:01:25,512 going to inherit from animal and this 42017 31:01:23,279 --> 31:01:26,872 will indirectly inherit from our 42018 31:01:28,872 --> 31:01:33,360 the pure virtual function that comes 42019 31:01:31,039 --> 31:01:35,191 with that interface and we do that here 42020 31:01:33,360 --> 31:01:36,551 we're going to specify which data we 42021 31:01:35,191 --> 31:01:39,279 want to print in this case we're going 42022 31:01:36,551 --> 31:01:42,231 to put the description out and the first 42023 31:01:39,279 --> 31:01:44,232 style and we are going to specify the 42024 31:01:42,232 --> 31:01:46,320 format in which we want the data to be 42025 31:01:44,232 --> 31:01:49,440 printed out again that's the whole point 42026 31:01:46,320 --> 31:01:51,360 of overriding this virtual function here 42027 31:01:49,440 --> 31:01:53,512 we're going to do the same thing on each 42028 31:01:51,360 --> 31:01:55,831 inheritance class in our inheritance 42029 31:01:53,512 --> 31:01:57,592 hierarchy for example we did the exact 42030 31:01:57,592 --> 31:02:02,400 we did the exact same thing for a cat 42031 31:02:00,399 --> 31:02:05,439 and we're going to do the same thing on 42032 31:02:02,399 --> 31:02:08,551 our bird classes that's exactly what we 42033 31:02:05,440 --> 31:02:10,400 do here specify which data to print out 42034 31:02:08,551 --> 31:02:13,911 and how the data is going to be printed 42035 31:02:10,399 --> 31:02:16,639 out and this stream insert override is 42036 31:02:13,911 --> 31:02:19,911 going to be picked up by our interface 42037 31:02:16,639 --> 31:02:22,160 when we get to call the output stream 42038 31:02:19,911 --> 31:02:24,639 operator here notice that we are calling 42039 31:02:22,160 --> 31:02:27,192 this thing polymorphically we are 42040 31:02:27,191 --> 31:02:32,551 method on a reference that we pass here 42041 31:02:30,399 --> 31:02:34,959 and this is going to resolve this using 42042 31:02:32,551 --> 31:02:37,279 dynamic binding this is going to work 42043 31:02:34,960 --> 31:02:39,592 really well now that we have this setup 42044 31:02:37,279 --> 31:02:42,080 we can head over to the main cpp file 42045 31:02:39,592 --> 31:02:44,800 and really do all kinds of crazy things 42046 31:02:42,080 --> 31:02:46,000 let's put in a separator here and we are 42047 31:02:46,000 --> 31:02:52,232 play with us we are going to include the 42048 31:02:49,119 --> 31:02:54,159 classes we need to set up our animals 42049 31:02:52,232 --> 31:02:56,800 and we are going to put them in here we 42050 31:02:54,160 --> 31:02:59,192 have animal feline dog cat bird pigeon 42051 31:02:56,800 --> 31:03:01,120 and crow the moment we do this we don't 42052 31:02:59,191 --> 31:03:02,871 need to put in this stream insertable 42053 31:03:01,119 --> 31:03:04,720 because it is coming in directly from 42054 31:03:02,872 --> 31:03:07,279 this but we don't need to worry about 42055 31:03:04,720 --> 31:03:09,592 this because we have include guards in 42056 31:03:07,279 --> 31:03:12,080 but we are going to be using smart 42057 31:03:09,592 --> 31:03:14,960 pointers so let's include the memory 42058 31:03:12,080 --> 31:03:16,800 header and we can set up an animal and 42059 31:03:14,960 --> 31:03:18,960 try to print it out we will be using 42060 31:03:16,800 --> 31:03:22,080 polymorphism so we will go through a 42061 31:03:18,960 --> 31:03:24,320 base pointer to animal and use that to 42062 31:03:22,080 --> 31:03:26,720 manage a dog object here we are just 42063 31:03:24,320 --> 31:03:29,680 going to pass in the data and we will 42064 31:03:26,720 --> 31:03:31,911 try to print this animal out notice that 42065 31:03:29,679 --> 31:03:34,079 we are going through a base pointer 42066 31:03:31,911 --> 31:03:35,911 calling the output stream operator so 42067 31:03:37,759 --> 31:03:42,871 polymorphically and it is going to print 42068 31:03:40,160 --> 31:03:45,512 this information at the dog level and 42069 31:03:42,872 --> 31:03:47,440 what i mean by that is that if we go in 42070 31:03:45,512 --> 31:03:49,360 dog we're going to find our stream 42071 31:03:47,440 --> 31:03:50,800 insert that's going to print this 42072 31:03:50,800 --> 31:03:55,592 like we want to print a dog and this is 42073 31:03:52,872 --> 31:03:57,512 super powerful hopefully you can see how 42074 31:03:55,592 --> 31:03:59,192 cool this is we're going to try and 42075 31:03:57,512 --> 31:04:01,440 build this program we're going to pass 42076 31:03:59,191 --> 31:04:03,911 this through gcc the boat is going to be 42077 31:04:01,440 --> 31:04:06,551 good we can clear and run rooster we're 42078 31:04:03,911 --> 31:04:08,959 going to see that we print this as a dog 42079 31:04:06,551 --> 31:04:11,512 even if we used a base pointer to try 42080 31:04:08,960 --> 31:04:14,400 and print this out how cool is this we 42081 31:04:11,512 --> 31:04:16,000 can even use this to set up a bird and 42082 31:04:14,399 --> 31:04:18,000 print it out we're going to set up a 42083 31:04:16,000 --> 31:04:20,232 base pointer that is going to be 42084 31:04:18,000 --> 31:04:21,831 managing a bird you can see that we are 42085 31:04:20,232 --> 31:04:23,832 using a unique pointer here you can 42086 31:04:21,831 --> 31:04:25,511 really use any kind of smart pointer you 42087 31:04:23,831 --> 31:04:28,159 want we're going to print this out and 42088 31:04:25,512 --> 31:04:30,160 this will be printed out like a bird 42089 31:04:28,160 --> 31:04:31,680 wants to be printed out and that's going 42090 31:04:31,679 --> 31:04:36,399 the override from our bird class and 42091 31:04:34,639 --> 31:04:38,160 that's going to say bird is going to put 42092 31:04:36,399 --> 31:04:39,591 out the description it's going to put 42093 31:04:38,160 --> 31:04:42,080 out the wing color and this is really 42094 31:04:39,592 --> 31:04:44,552 going to work really well we're going to 42095 31:04:42,080 --> 31:04:46,639 pass this through gcc again the world is 42096 31:04:44,551 --> 31:04:48,479 going to be good we can clear and run 42097 31:04:46,639 --> 31:04:51,759 rooster you see that we are printing out 42098 31:04:48,479 --> 31:04:54,959 bird here and this is using polymorphism 42099 31:04:51,759 --> 31:04:57,511 combined with interfaces and this can 42100 31:04:54,960 --> 31:04:59,760 come in handy if this helps your 42101 31:04:57,512 --> 31:05:02,000 application depending on what you want 42102 31:04:59,759 --> 31:05:05,039 to achieve with zipclose plus we can 42103 31:05:02,000 --> 31:05:07,440 even put our animal data in an array and 42104 31:05:05,039 --> 31:05:10,000 try to print that out so we're going to 42105 31:05:07,440 --> 31:05:13,760 put out a separator to be able to follow 42106 31:05:10,000 --> 31:05:15,911 this nicely in our output terminal this 42107 31:05:13,759 --> 31:05:17,831 is going to do and we're going to go 42108 31:05:15,911 --> 31:05:20,080 down and put in the code to play with us 42109 31:05:17,831 --> 31:05:22,231 we're going to share an array of shared 42110 31:05:20,080 --> 31:05:24,080 pointers this is how we do this and 42111 31:05:22,232 --> 31:05:26,232 we're going to initialize this with all 42112 31:05:24,080 --> 31:05:28,080 kinds of crazy animals we're going to 42113 31:05:26,232 --> 31:05:29,512 put in a dog we're going to put in a cat 42114 31:05:28,080 --> 31:05:32,479 we're going to put in a crow and a 42115 31:05:29,512 --> 31:05:34,479 pigeon and we will be managing this 42116 31:05:32,479 --> 31:05:37,039 through base pointers notice that this 42117 31:05:34,479 --> 31:05:39,512 is an array of base pointers 42118 31:05:37,039 --> 31:05:42,231 and we're going to be calling the output 42119 31:05:39,512 --> 31:05:44,160 stream operator polymorphically so for 42120 31:05:42,232 --> 31:05:47,040 the first element here we will print a 42121 31:05:44,160 --> 31:05:49,680 dog how a dog wants to be printed out so 42122 31:05:47,039 --> 31:05:51,831 we will be using the stream insert 42123 31:05:49,679 --> 31:05:53,039 override from the dog class if we go 42124 31:05:51,831 --> 31:05:56,000 there we're going to see that we're 42125 31:05:53,039 --> 31:05:57,591 going to prefix our data with dog if we 42126 31:05:56,000 --> 31:05:59,911 hit the second element we're going to 42127 31:05:57,592 --> 31:06:01,832 print this like a cat wants to be 42128 31:05:59,911 --> 31:06:04,319 printed down the third element is going 42129 31:06:01,831 --> 31:06:06,080 to be printed out as a crow the fourth 42130 31:06:04,320 --> 31:06:08,080 element is going to be printed out as a 42131 31:06:06,080 --> 31:06:08,872 pigeon and this is going to fall in and 42132 31:06:08,872 --> 31:06:13,360 right away down here you see that we are 42133 31:06:11,191 --> 31:06:14,799 looping calling our output stream 42134 31:06:13,360 --> 31:06:17,119 operator and this is going to work 42135 31:06:14,800 --> 31:06:18,800 really well let's try and build this 42136 31:06:17,119 --> 31:06:21,279 program we're going to pass this through 42137 31:06:18,800 --> 31:06:23,760 gcc the build is going to be good so we 42138 31:06:21,279 --> 31:06:25,759 can clear and run rooster and you're 42139 31:06:23,759 --> 31:06:27,439 going to see that we are printing our 42140 31:06:25,759 --> 31:06:30,000 information here the first thing is a 42141 31:06:27,440 --> 31:06:32,232 dog the second thing is a cat the third 42142 31:06:30,000 --> 31:06:34,720 thing is a crew the fourth fan is a 42143 31:06:32,232 --> 31:06:37,440 pigeon and this is working really well 42144 31:06:34,720 --> 31:06:40,399 hopefully you can see how interfaces can 42145 31:06:37,440 --> 31:06:42,551 be powerful in your c plus plus design 42146 31:06:40,399 --> 31:06:44,231 use this if it makes sense for whatever 42147 31:06:42,551 --> 31:06:46,800 application you are designing with z 42148 31:06:44,232 --> 31:06:50,232 plus plus my main goal here is to 42149 31:06:46,800 --> 31:06:52,639 introduce you to the idea of interfaces 42150 31:06:50,232 --> 31:06:54,480 and how powerful they can be again an 42151 31:06:52,639 --> 31:06:56,399 interface is something you can attach to 42152 31:06:56,399 --> 31:07:00,399 and at the moment you attach that 42153 31:06:58,160 --> 31:07:02,480 interface to your type your type is 42154 31:07:00,399 --> 31:07:05,360 going to have the powers that come with 42155 31:07:02,479 --> 31:07:08,159 that interface and all you need to do in 42156 31:07:05,360 --> 31:07:10,160 your deriving classes is to implement 42157 31:07:08,160 --> 31:07:12,800 the virtual functions that come with 42158 31:07:10,160 --> 31:07:14,800 that interface and the interface will 42159 31:07:12,800 --> 31:07:16,479 pick them up and use them to do whatever 42160 31:07:16,479 --> 31:07:21,119 polymorphically and that's going to be 42161 31:07:18,720 --> 31:07:23,360 really cool we are going to stop here in 42162 31:07:21,119 --> 31:07:26,319 this lecture this one we're going to try 42163 31:07:23,360 --> 31:07:28,639 and recap what we saw in this chapter 42164 31:07:26,320 --> 31:07:31,120 go ahead and finish up here and meet me 3168677

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.