 Hey guys, what's up, this is Umar Khan and I welcome you to a new section here on this course on advanced C++ and in this section, we are going to be covering a very interesting topic that is namespace in C++. Now this is basically going to be the content which we are going to be covering in this section. So first we are going to start on with the introduction to namespaces, then we are going to see the relation between classes and namespaces, then we are going to see that how you can basically access a namespace, then we are going to study about nested namespaces, then we are going to see that how you can create an alias name for a namespace and then finally we are going to study about inline namespaces. So if I tell you this is a very important and a very interesting topic and it is a topic that is most often collected by many programmers. So I suggest that you stick on to this section because this is such an important topic in C++ to learn. So thank you so much for now and the next tutorial we are going to start on with the introduction to namespaces. So bye-bye till then. Hey guys, what's up, this is Umar Khan and I welcome you to the very first tutorial on namespaces in C++. So as I told you that in this tutorial we are going to be covering the introduction to namespaces and we are going to be covering some basic examples for how basically this works. So consider a situation when we have for example let's say two persons that have the very same name in the very same class. So whenever we need to differentiate between them definitely we would have to use some additional information along with their name like for example where they live, for example what is their father name, what is their mother name or we are going to add some information that is actually going to distinguish between those two individuals. For example if we have two people that have the name Jack then for example one has the name Jack Reacher and then let's say the second one has the name as let's say Jack Rowe and what we are going to do is that we are going to call one as Reacher let's say and let's say call the other one as Joe. So this is basically some distinction of the second name. So there has to be added some distinction that is to distinguish between those two individuals. So same situation can arise in your C++ applications as well where you have to declare two variables that are going to have the very same name for example one is an integer value and let's say one is a double value then you know that it is going to generate an error. So to solve that error basically C++ has a feature that is name spaces and that is going to solve your error. So if you just move on to the compiler and let me show you that how it is going to generate error. So for example in this main function you have let's say a variable X that is equal to let's say two. So now if you just see out this X it is going to work perfectly fine you know that let's just run it and you can see that it worked perfectly but for example we have a double value that is also from the name X and V let's say declare it to three and then see out X. So now you know that it is going to generate an error as you can see right here that it says conflicting declaration of double X previously declaration as in X. So this is the situation where name spaces come into help and solve such errors. Now in each scope a name can only represent a single entity so there cannot be two variables with the same name in the same scope. So using name spaces what we are going to do is that we are going to create two variables or member function that are going to have the same name. So for example let's just create a namespace right in here let's just remove this stuff from here and let's just write in namespace then you have to write in the name of the namespace you are going to use for example we use the name as W then what we are going to do is that we are going to specify the brackets and in these brackets you can basically define this variable X equal to let's say 10 this time. So now to basically use this int X equal to 10 and you can see that we have this int X equal to 2 because if you run this code you can see right here that it has actually printed out this variable that is the local variable to this main function that is int X equal to 2. So now what if you want to access this int X equal to 10. So what you are going to do is that you are going to see out and then you are going to write the name of the namespace which you are going to access that is named as W and then you are going to specify two columns and then you are going to write in the name of the variable and finally we are going to add a namespace. So if you now just run this code so you can see right here that we have this 10 as well which has been printed from this statement first you have to write the name of the namespace because this is actually the name of the namespace this is a building word and then in writing these brackets you have to actually provide the definition in which you can declare variables or you can do anything else. So basically what's going on in here is that this namespace W has basically rated a new namespace for example if you just consider the very same example of a class what it has done is that it has basically created a new class in which we have one jack here and then one jack is present in this class. So now the conflict is over if you call in this X that is if you call in this jack then this jack is going to reply to you and if you call in this jack that is the local jack basically this is going to reply to you. So if you talk in a more programming sense this int X that is equal to 10 is a part of this namespace and then this int X equal to 2 is a part of this local main function. So if we talk about the definition and creation of namespaces namespaces basically allow us to group named entities that otherwise would have global scope into narrower scopes giving them namespace scopes this allows organizing the elements of a program into different logical scopes referred to by the names. So if you talk some points about what is a namespace basically so first namespace is a feature that is added in only C++ it is not present in the C language. Secondly a namespace is a declarative region that provides a scope to the identifiers that can be names of the type functions variables and anything else inside it. Third multiple namespace blocks with the same name are allowed all declarations within those blocks are declared in the named scope. So basically a namespace definitions begin with the keyword that is namespace as you can see right in here then it is followed by the namespace I think I've just written it on slides as well yes right in here this is the syntax for using your namespaces first you have to write in the building keyword that is namespace then you have to use the name of the namespace which can be anything you can just write in anything you want in here and then you have to write in anything here because this is the scope that is all for the code declaration where x and y are basically declared in the namespace namescope that is whatever is written here. So namespace declarations appear only at global scope namespace declaration can be nested within other namespaces as well moreover namespaces declarations don't have access specifiers that are public or private moreover like right in here if you can see that we have actually now a single namespace that is named as w and then you also have this namespace as well but you can also create multiple namespaces as well for example if I just write in here using the very same syntax that is namespace then you write in the name of the namespace that is let's say y and in here I write in index equal to let's say five so now we have actually three namespaces that is first is this w namespace then this we have this y and then we have this local namespace as well so if you want to access the namespace that is named as y what you're going to do is you're going to write in the name of the namespace then you're going to write the variable you want to print and you're going to write in and that so if you just run it you can see that we have a five here on our output screens as well so this was I guess all about namespaces in C++ introduction you have seen how multiple namespaces can be created in C++ so I guess that's it with this tutorial in the next tutorial we are going to see what is the relation basically between classes and namespaces and how you can actually declare namespaces in the class so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial hey guys what's up this is marhan and I welcome you to another tutorial on namespaces in C++ in this tutorial we are going to see the relation between classes and namespace and to elaborate the word relation I actually mean that how we can actually initialize a classes in a namespace how you can only declare a class in a namespace and then give it definition outside this is basically the content we are going to be covering in this tutorial we are going to create classes inside a namespace we are also going to create classes outside the namespace and then we are going to at the very end cover a very small topic that is how you can declare methods outside namespace after actually initializing or declaring them inside so let's move on to our editor because I guess there is nothing much to speak about this stuff so the first thing is that how you can basically declare and initialize and do everything about a class in a namespace so first what you have to do is that you have to create a namespace and let's say the name of the namespace is w and then in this namespace what you have to do is that you have to create a class for example the name of the class is let's say x this is the body of our class and this also has to add a colon at the end and now in this class let's say we have a public method that is let's say white display and what it's going to do is that it's going to simply see out inside the namespace so right here in the main function what we are going to do is that we are going to create an object now how an object for a class within a namespace is going to be created first you have to write in the name of the namespace in this case the name of the namespace is w so you're going to write in w then two columns and then you are going to write in the name of the class that is x then you have to add a space and then you have to write in the object which you want to create and then for example if you want to let's say call this function that is the white display function that must be identified something like this so if you want to call this display function what you have to do is that you have to use this object because now we are into the namespace we have created an object of the class x that is in the namespace w so now what you have to do is that you have to simply write an object dot display and finally if you want to add this statement as well it is very good and now if you just run this code you can see that we have the output that says inside the namespace you so you have seen that how we have actually accessed a class that is inside a namespace now if i just talk about the previous case which we were discussing in the introduction tutorial that we can have multiple variables of the very same name so for example if you want to declare this class x again you can declare it outside the namespace that is class x and then you can do anything right in here for example if you just run this code you are going to see that it is going to run perfectly it is going to generate no error for example let's just create an object of this class as well so just write in x and let's name it as o and let's just run it and you can see right here that it ran perfectly and it generated the same output so you can create actually multiple classes one inside one namespace and one inside another namespace this is actually the global scope this in this case so now to the second part of the tutorial that says that how you can declare a class outside the namespace so for that what you have to do is that you have to first declare the class because we have already this class right in here that is the class x in the global scope so in this global scope you cannot initialize or declare sorry not use another class that is outside so what you have to do is that you have to actually i'll just remove this from here and what you have to do is that you have to write in this class x right in here and let's say we want to give its implementation outside this namespace you can give it but what you have to do is that you are going to write in class then you have to write in the name of the namespace for which this class is going to be defined and then you have to write in the name of the function because if you don't write this thing over here then it is going to generate error because we already have a class x in this global scope now by writing this what you are going to refer is that you are going to refer that this class x belong to basically this namespace and when you don't write this with this class x then you are referring to the global scope that this class belong to the global scope and this belong to the namespace that is named as w so now in here we are going to do the very same stuff that is public and let's say the function is the very same that is the display function and in the body what we are going to do is that we are going to simply see out and now we are going to write in outside the namespace because this implementation has been given outside the name of the namespace that was named as w just going to display and we already have an object to this class let's just run it so you can see it generated the very same output but this time it says outside the namespace because this implementation is done outside the namespace we all we only have declared the class here but the defining of the class is outside the namespace we still have our class x in here in the global scope where we have actually provided the implementation but it is not going to generate error because this class x is in a different scope and this class x is in a different scope now to talk about the last part of this tutorial that how you can actually define methods outside the namespace so if you consider this very same example and let's say we have a function right here that is white display and we have actually only declared that function in over here and then in this class x what we have is that we also have a public method that is white display and also we have not provided an implementation of this class right here in the namespace w as well so now right down here in this class w x or whatever stuff is written in here what we are going to do is that we are going to actually define our method so to define a method of a namespace outside the namespace what you have to do is that first you have to write in white over here then you have to write in the name of the namespace that is w in this case and then you have to just remove it from here and then what you have to do is that you have to write in the name of the class and then you have to write in the name of the function let's just remove it from here all right so I repeat it again first you have to define the return type of the function then you have to write in the name of the namespace to which this function belongs then you have to write in the name of the class to which this function belongs and then you have to write in the name of the function So in this function, you are going to do what you're going to do is that first you are going to just remove this from here, because it is a function now and a colon is not expected at the end. So what you're going to do is that you're going to simply see out, this is the class display function. And now you have another function right in here, that is the void display function also in this namespace that is not in this class. So to define that function, what you have to do is that you are going to gain right in the return type since now we are not dealing with the class function. So we are going to write in only the name of the namespace and then we have to call in the display function. And in this display function, we are going to write in namespace function outside the class. Since this is the name, this is the function of the very same namespace that is named as w, but it is actually outside the class. As you can see right in here, we have two display functions, one is a part of the class that is this function. And we have one more function in this namespace that is named by the very same name, but it is outside this class. So to provide the implementation of this display function that is inside the namespace as well as inside the class, you have to write in and refer to both the namespace and the class as well. But when you are dealing with this display function, that is not inside the class and only inside the namespace, you only have to deal with the namespace and you only have to write in w, call and call and display, and then you can just provide its implementation right in here. Now, in the main function, let's just remove this object, you have seen the purpose of it that it is not going to generate error. So let's just remove it. Now, what you're going to do is that you are going to, in the very same way, create an object of the x class. And then you are going to now call in the display function. Now to call in the display function of the class, sorry, the function that is inside the class where you have to do is that you have to actually use in this object. So you're going to write an object or display the very same way. Now to call the second function that is outside that is the namespace function that is outside the class where you have to do is that you have to write in the name of the namespace. And then you have to simply write in the function which you want to call in. So let's just run it. Yes, we forgot to provide this here at the end. And I've spotted one more error. We forgot to provide the brackets for the function and right here as well. And let's just segment it right down here. And I guess that's perfect. So let's just run it. So as you can see, the first output is that it says this is the class display function. And the second output says that namespace function outside the class. So first the class display function has been has been called because we call it using the object. And then secondly, we call in the function that was only inside the namespace not inside the class. So you have seen now in this tutorial that how you can actually define a class inside and outside the namespace but when you have to declare it outside what you have to do is you have to first declare it inside and then provide its implementation outside that namespace. And secondly, you have also seen that how by only declaring a function inside a namespace, you can access it outside. So we have covered good stuff of examples as well. So I guess that's it with this tutorial. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Amir Khan and I welcome you to another tutorial on namespaces in C++. In this tutorial, we are going to see that how you can extend a namespace and how unnamed namespaces are used in C++. So we are going to start on with namespaces having different names and this part have already been covered in the introduction tutorial. Let me just give you a very quick example for how this is implemented. Now I'm talking about the case when we just declared in two namespaces first with the name of W and then we have a namespace with let's say a name of that is of the name by then as you can see right here now we have two namespaces for example we have a variable x that is equal to 10 in this namespace and then we have a variable x that is equal to 2 in this namespace. So if you want to access any of this x for example if you want to access this x that is in this w namespace what you have to do is that you have to write in w and then you have to write in x and in the same way if you want to access the x variable of the y namespace then you have to write in y and call an x. So, this is going to just do your job and if you just write in endel in here and run this code, you can see right here that 10 and 2 are printed in the very same sequence they were called in. So, we have already covered this in very detailed introduction tutorial. So, to move on to the part which we are going to be covering in this tutorial is how you can extend a namespace and how you can basically use unnamed namespaces in C++. So, starting on with extending namespaces. Now, what do I mean by extending namespaces? Extending namespaces mean that using the very same name twice for a very same namespace. Now, let's see for example, we have this let's say namespace w and I also name this namespace to be w as well. So, as you can see right here that we have this namespace w and then this namespace w in which we have the very same variable int x equal to 10 and int x equal to 2. So, if you just run this code, so since these are going to be of the very same namespace, so this scenario is going to generate an error. As you can see right here, it says that it was previously defined here. Now, the purpose to show you this error was that as you can see right here that these have been declared separately. This namespace is declared separately and this namespace is declared separately. But when you use the very same name for a namespace, w it is basically extending a namespace and these two are basically a single namespace that have the name of w. If you use the very same variable int x equal to 10 and int x equal to 2 and write in here what it's going to do is that it's going to simply generate an error message for you. So, you can use here for example, you can use in here y and when you then write in here y and run this code it is going to run perfectly. This is the very same scenario for example, you define int x equal to 10 and then int y equals to 2. So, this is the very same case right in here since as you can see right here that these two that is int x equal to 10 and int y equal to 2 they are in the same namespace that is the local main, local main function these, they have a local scope basically these two variables. So, in this case, this variable x and this variable y also have the very same namespace so they are going to be of the same scope that is named as w. So, I hope it is clear. So, now moving on to unnamed namespaces by unnamed namespaces I means that you do not specify the name of the namespace. Let me just give you a quick example. For example, if I just remove this name from here this is basically calling a namespace that has no name. So, if you just remove this stuff from here and you also remove this from here since it now does not belong to w let's just remove this as well and remove this as well. So, you can see right here that we have a namespace that has got no name and in that namespace we have a variable x that is equal to 10 and in the main function we have c out x without specifying the namespace. So, if you just run it, you can still see that it printed out 10 because it has access to a namespace that is unnamed now. For example, you define a variable x equal to 2 here and now run this code. So, you are going to see that it is now printing 2. Now, why is this stuff going on right in here? Now, if you declare a variable in the scope where you are printing it without specifying the namespace since it does not have a name. So, the priority is going to be for the variable that is declared in that very scope since in this case the c out x statement is actually in this main function that is the that is local for this main function. So, when this int x is declared in this local main function what it's going to do is that it's going to this c out statement is going to give a priority to this int x equal to 2 and print out the value of x as 2. But if it is not present here, then this namespace that is int x equal to 10 is going to be given a priority. Now, what if for example, we have let's say int x equal to 3 right in here in the global scope. So, if you now just run it, it is going to generate an error because this namespace also declared this x equal to 10 as I told you before that in the introduction tutorial that namespaces have a global scope. So, basically, if you don't specify a name here, it is going to be a global scope of this very same namespace in which this program is going on. That is a single namespace for this entire program. But since here it was local, so this x was global and this x is local, that was int x equal to 2, this is local. So, it does not generate an error and it printed out 2 for us. But when you write in int x equal to 3 in the global scope and since this int x equal to 10 is also in the global scope and also it does not have a name, which means that it is a part of this namespace in which this x equal to 3 is present. So, that is the default actually global scope of this program. So, this x equal to 10 is also in the default global scope since it does not have a name. So, this is a contradiction that is x equal to 10 and x equal to 3. So, this statement have to be removed from here. And now if you just comment it out, this statement right in here that is the global, that is the default, I must say default global namespace of this program is going to run and it's going to print out 10 for us. So, I hope it is clear. So, that's it with this tutorial as well. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial on namespaces in C++. In this tutorial, we are going to be covering about accessing namespace. So, basically there are two ways to access a namespace. The first one is the normal way and then we have a directive way as well. So, let me just give you a quick example of how you can access a namespace by using the normal way, which we have already covered in our previous tutorials. But there, I haven't told you that this is actually the normal way. So, for example, we have a namespace named as A and in this, we have a variable x equal to 10. And now if you just write in C out and first write the name of the namespace, sorry, it is going to be A and then we have to write in this x over here and then and line. So, this is basically the normal way of accessing a namespace. So, if you just run, you know that then it's going to be printed out on your output screens. So, this is basically the normal way of doing it. Now, the second way is by the using directive way. Now, you can actually avoid pre-pending of namespace with the using namespace directive. This directive tells the compiler that the subsequent code is making use of the names in the specified namespace. The namespace is thus implied as let's say a main function that is going to actually direct your code. So how this is going to be done, let's just see an example of this. So, we will define a function in this namespace. Let's name it as white punk. And in this function, what we are going to do is that we are going to simply have a C out statement. And in this, we are going to print out namespace number one, which means that we are also going to add more namespaces. So, outside here, we are going to specify a second namespace. And you don't make you understand this concept. Initializing two namespaces is very essential. And why you're going to just learn in a moment. So, we have the second namespace in which, for example, we have the very same function. And in this, we are going to see out namespace number two. All right, now, if you just move on to this main function, now you know that if you have to call this function, then you have to write in this B and then this colon colon, and then you have to write in punk. And this function is going to be called in. And if you want to call the function that is right here at the top, and you have to write in this A over here. So, for example, we want this thing not to be written here. And if you want to call the function of, let's say, this namespace B by just doing this, then how this is going to be done. This is basically going to be done using. This stuff over here that we were discussing that is using directive way. So, let's just remove it from here. We do not have to specify this C out over here because we have already used the C of statement here. And since it is returning nothing, so C of statement would have generated an error for us. So, what we are going to do right above this main function is that we are going to write in using, which is a built in keyword. Then you have to write in namespace, which is again a built in word. And then you have to write in the name of the namespace for which you want to run this main function. So, I want to run it for this B and then have to specify this over here. And then we have this main function in which we are going to simply call this and this and I'll not hear for sure. And I guess that's it. If you now just run this code, you can see right here that it printed out namespace to because this main function basically now ran for this namespace. If I just specify this A over here, that is the first namespace right here at the top that also has a function that print out namespace number one. So, if you now just run this code, you can see right here that it printed out namespace number one. So, the using directive basically it has directed our main function. So, the using directed can also be used to refer to a particular item within a namespace. For example, if you have, let's say, a variable, let's say we have something like int x equal to 10. Then, since we are dealing with A, let's just make it to B and then C out and then we are going to specify this x right here. That is variable x. So, when we just run this code, you can see that first we have the namespace number two since now we are dealing with the namespace that is B. So, it printed out namespace number two and we have also printed out our variable as well. So, this is how basically you can use the using directive way in C++. You have to just write in, if you talk about the syntax, you have to write in the using keyword, then you have to write in namespace, which is again a building, and then you have to write in the namespace name for which you want to direct this main function. So, in this case, it is directed for this B namespace. So, when the func is called, it is going to call the function that is in this namespace. So, I guess that's it with this tutorial. You now know how to use the using directive way and the normal way to access namespaces. So, thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Omar Khan and I welcome you to another tutorial on namespaces in C++. In this tutorial, we are going to be covering nested namespaces. Now, what does the word nested basically mean? Nested means something inside something. So, nested namespaces means that a namespace that is declared inside a namespace. So, this is basically the syntax for what I am talking. We have this namespace N1 that has its first bracket right in here and this second bracket right in here. And inside this body of the namespace N1, what we have that, we have the declarations of namespace N1 but moreover, we also have a namespace inside this namespace N1 that is named as N2. And in the namespace N2 body, we can have the declarations for the namespace N2. So, now how this is done practically with the help of an example and how we can actually then call the namespace 2 that is actually nested inside the namespace N1. So, let's see how all this stuff is done. So, for example, we have the first namespace as let's say S1 or let's just call it N1. And then we have another namespace that is N2. And in the first namespace, for example, we have a function that print out a simple C out statement for us. That is the outside namespace function. And what this second namespace is going to have is it's also going to have a function that has the type void. And what this is going to do is that it's going to print out. It is the inside function. So, we're going to write an inside namespace function and now this namespace as I told you is going to be nested. So, we are going to just remove it from here. And we are going to paste it right in here. Now, as you can remember from the syntax that we have the namespace N1 and then we have the declarations for namespace S1 as you can see right here. We have the namespace N1, then we have the declarations, then we have the namespace N2 and then it's declarations. The very same which is written right here, namespace N1. Then the declarations that is the function for namespace N1 then we have the namespace N2 that is right. This one over here, this bracket as well. This is the namespace N2 in which we have a function and this is the closing bracket for the nested namespace. So, this is now a nested namespace in which we have this namespace N1 which contains a namespace N2 and both have their functions. Now, if you talk about the main function and let's say for example, you want to call this function that is the nested function, sorry, the function that is the outside namespace function which is not in N2 but is a part of N1. So, to use that using the using directive where what you're going to write and you're going to write in using namespace and as you know that you have to write in the name of the namespace and then this column and then you have to simply write in the name of the function which you want to call. So, if you just run this code now, as you can see right here that it printed out outside namespace function because in this case, we were using the using namespace N1 which was right here at the top that has the function that printed out outside namespace function. Now, if you just change it to N2 and run this code, it is generating errors. Now, why it is generating errors? Because it says that N2 is not a namespace name because since now N2 is inside the namespace of this N1. So, what we are going to do is that we are going to tell our compiler that N2 is basically inside N1. Now, how we are going to tell him, we are going to write in N1, colon colon N2, which means that N2 is in N1. So, now if you just run it, it's going to print this statement as you can see right here. Now, it says inside namespace function since we have told him that N2 is inside N1 and we have to print out the function. So, it searched for N1, it searched for something and do that is inside N1 and then it searched for this function that is inside N2. So, it printed out inside namespace function. So, this is basically the concept of nested namespaces in C++. So, I guess you have understood it well. So, thank you so much guys for watching and I will see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial on namespaces in C++. In this tutorial, we are going to be covering the topic that is namespace aliasing and it is a very easy topic to understand and basically this thing that is the aliasing of the namespace introduced in this concept because we have to provide some ease to our user. For example, we have a nested namespace in which we have let's say five to six routes. For example, we have a namespace in which we have a namespace and in that namespace we again have a namespace and so on goes until let's say five to six leads. Then what we are going to do is that we are going to provide ease to our user. For example, if you want to access the last namespace that is nested in each and every namespace then to call any function from that namespace it's going to be a very fussy task. So that is where namespace aliasing is going to help us out. So how it is going to be done. Let's see this without. So for example, we have a namespace that is s one and inside that this namespace we have another namespace. Let's say s two in which we have another namespace. Let's say s three in which we have another namespace. Let's say s four and I guess that's enough. So for example, we have a function in this namespace in which we have to simply see out a statement inside the fourth namespace. So as you can see right here that this namespace s four is nested in s three, which is nested in s two, which is nested in s one. So for the main function, for example, we are using the normal way to access the namespace. Then what you have to do basically is that you have to actually write in first s one then you have to write in s two then you have to write in s three then you have to write in s four and then you have to write in funk. So if you just run this code, you can see that it says inside the fourth namespace. So for a single case, I guess that's good. But what for example, we have a variable. Let's say int x equal to 10 in this, then we have a variable int y equals to let's say 20, then we have another variable in z equal to let's say 30 right here in this next state namespace s four. So to use each of these what you have to do every time is that you have to write in s one then s two then s three then s four and then you have to write in X. For example, you want to access the value for X and you have also to add a C out statement for this. So if you just run it, you can simply access the value of X and as well as this function right here at the top as you can see right here is inside the fourth namespace and then it says 10. So now if you want to access this why you have to again write this complete set and for that you have to write in again this complete set and for each and every entry you want to access from this namespace you have to use this. So why not just give this statement right from here to here a name. So then every time we are going to use that name to actually call this namespace. So what we are going to do is that we are going to use the namespace aliasing for this purpose. So right above here where we are going to write in we are going to write in namespace. Then we are going to write an alias name. Let's name it as alias and then what we are going to do is that we are going to write this stuff that is s one then s two then s three and then s four. Add a column here and now whenever you want to use this stuff what you can simply do is that you can just use in this alias keyword and this alias keyword here as well. Let's just run it and you can see that the very same output has been generated as it was in the previous case. So now if you want to get access to the variable why or variable that you can just copy this and paste it over here and you can just change it from here why to that. Now as you can see that the first the task of writing this stuff has been removed and now you can simply write in alias over here. So I guess that was helpful and this is what is the purpose of namespace aliasing in C++ that is to provide ease to the user. So I guess that's it with this tutorial as well. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys. What's up? This is Amir Khan. Now welcome you to another tutorial on namespaces in C++ in this tutorial we are going to see what is basically inline namespaces and inline namespace is basically a namespace that uses the optional keyword inline in its original namespace definition. By using this inline keyword what is the advantage basically is that you don't have to specify the object every time you want to access an element of a nested namespace. For example if we just move on to our compiler and let's say we have a namespace let's say s1 and inside that namespace what we have is that we have an inline namespace that is s2 and in this what we have is that we have let's say a simple variable int x equal to 10 and for example we want to now access this int x equal to 10. So what we can do in this main function that we can just see out s1 and then this x. So if you just run this code you can see that it will run perfectly and you can see right here that 10 has been printed out but if you remember from your previous concept that when you have to use the nested space then you have to write in stuff like you have to write in then s1 and then you have to write in s2 and then this x and then it's going to be printed out but now in case of inline namespaces inline namespaces s2 is going to be treated as it is actually s1 and this variable in x equal to 10 is going to be treated like it is the part of this namespace s1. So as you can see that members of an inline namespaces are treated as if they are the members of the enclosing namespace in many situations. Now this property is transitive if a namespace for example if you have a namespace n and it contains an inline namespace m which in turn contain an inline namespace o then the members of o can be used as though they were the members of m or n. For example we have s2 then we have let's say another namespace in this that is again an inline namespace that is s3 and let's say we have now here a variable int x equal to 10. So just this statement right over here is going to do our job. We just print it out. You can see that 10 has been printed out because this 10 is considered to be a part of like it is a part of this s2 or this s1. You can also use it using this s2 but this is going to now generate error now why? Because s2 is basically inside and not outside. So the compiler does not know that s2 actually exists. So what you have to do is that you have to write an s1 call an s2 and now it is going to run perfectly and it is going to again print out that 10 for us. So this 10 is treated to be like it is a part of s2 and s1 is going to think that it is a part of my. So basically you have also understood one more concept in here that is how an inline space name space can contain another inline name space. So the inline specifier basically makes the declarations from the nested name space and it appear exactly as if they had been declared in the enclosing name space as you have seen in this example. This means that it drags out the declaration from a nested name space to the containing name space. Now what are the advantages of using a name space? There are basically two main advantages of using name spaces. The first one is it avoid the verbose. For example, if you consider this code. And if these were not inline name spaces and you have to access this variable X and what you have to do is that you have to add one more that is s3 and then you have to print it out. Now this looks good only if the name space names are short as in this example we have covered but by using inline what the namespaces there is no need to type the entire namespace as given above or by using the directive method as well. So if you talk about the next part of this tutorial is that how inline namespaces are going to help you out in the using directive inside namespaces. It is going to help you in the very same behavior and it can also be achieved by using the using directive using directive that names the inline namespace is implicitly inserted in the enclosing namespaces. For example, if we consider this very same example in which we have namespace s1 and then inline namespace s2 and then inline space name space s3 and then inline namespace s3 contain a variable index equal to 10 then if you are using the using directive way and you try using namespace and you only have to mention the name of s1 and then this stuff is going to be removed from here and then if you just print out this x and just run this code you can see that it ran and printed out 10 as well. So this was the very same concept in which this s1 thinks that x is a part of it s2 thinks x is a part of it and s3 also thinks x is a part of it. So this makes it actually a single bracket in which this x equal to 10 is contained and you can also print it like this because s2 also thinks that it is a part of it so if you just run it you can see that 10 is again printed and s3 also think it is a part of it so you can write in s3 here as well and run this and again it is going to print out 10 for us. You can see right here. So this was basically the purpose of using inline name spaces in C++ and you have also learned about the advantages of using namespaces sorry inline namespaces and you have also seen how using the normal way and how by using the using directive inside namespaces you can use the inline namespaces and it will help you out. So that's it with this tutorial. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys what's up this is Umar Khan and I welcome you to a new section here on this course on advanced C++ in which we are going to be seeing the new features that are added in C++ 11. So the content covered in this tutorials are basically the additions that are made to C++ 11. These additions are lambda expressions automatic type declaration and declaration type uniform initializations deleted and defaulted functions null pointer delegating constructors or value references and new smart pointer classes. So we are going to be seeing all of these and we'll start on with the introduction tutorial in the next tutorial. So that's it with this tutorial. Thank you so much guys for watching and I will see you guys in the next tutorial. Hey guys what's up this is Umar Khan now welcome you to another tutorial on C++ new features that were added in C++ 11. So this is basically an introduction tutorial in which we will see the introduction to C++ 11 and the features that were added to it. Now from 13 years since the first iteration of the C++ language, Danny Califf that is a former member of the C++ Standards Committee explains how the programming language has been approved and how it can help you write better code. Now Bajanes drops that is the creator of C++ said recently that C++ 11 feels like a new language. The pieces just fit together better. Indeed core C++ has changed significantly. It now supports lambda expressions automatic type deduction of objects, uniform initialization syntax, delegating constructors, deleted and defaulted function declarations, null pointer and most importantly the R value references. That is a new feature that argues a paradigm shift in how one conceives and handles object and that's just a sample we will cover all of these in this section. Now the C++ standard library was also revamped with the new algorithms, new container classes, automatic operations, type traits, regular expressions, new smart pointers, async facilities and a course of multi-threading libraries. Now after the approval of C++ standard in 1998, two committee members prophesied that the next C++ standard would certainly include a built-in garbage collector that is also referred as GC and that it probably would not support multi-threading because of the technical complexities involved in defining a portable threading model. Now 13 years later the new C++ standard that is C++ 11 was completed and guess what it lacks a garbage collector but it does include a state of the art threading library. So from the next tutorial we will start on with the features that will that were included in C++ 11 one by one and after seeing them you will understand yourself that how much useful these features are. So that's it with this tutorial. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial that discuss about the new features that were added in C++ 11 and in this tutorial we are going to be discussing the very first feature that is lambda expressions. So a lambda expression basically let you define functions locally at the place of the call here by eliminating much of the TDM and security risks that function objects incur or maybe they want to search for all email addresses that also show up in another list now there are a lot of potentially interesting things to search for instead of building all of these options into the class wouldn't it be nice to provide a generate find method that takes a procedure for defining if an email address is interesting or not. Now to talk about the syntax of using lambda expressions you can see it right on your screens. First we have to define the captures then we have to define the parameters then the return types declaration and in the body we will have our lambda statements. Now to elaborate further you talk about capture the capture clause is also known as the lambda introducer and it specifies which outside variables are available for the lambda function and whether they should be captured by value or by reference. You will also be able to identify the start of a lambda expression with the presence of the capture clause and an empty capture clause means capture nothing in which case the lambda expression body does not access variable in the closing scope. So we will get into much details of this when we cover our example spot. So the next one is the parameters. Now this is the optional parameter list also known as the lambda declarator you can admit the parameter list if you want a function that takes zero arguments. Then we have the return types declaration as you can see on your screen now this is basically the return type most of the time the compilers can deduce the return type of the lambda expression when you have zero or one return statement. However if it makes it easier to understand the code you can specify the return type now there are some differences among compilers regarding the automatic detection of return types when you have more than one return type statement because the standard does not guarantee the automatic detection of return types. Now notice that the specification of the return types is basically based on the optional return value syntax that is introduced with C++11 which puts the return value after the function. And now to talk about the last thing that is lambda statements that is basically the body of the lambda function the statements within the lambda body can access the captured to variables and the parameters that are passed into that lambda function. So let's move on to the examples path now. So let's move on to our compiler for that. And we will start on with a very simple example in which we will make everything equal to let's say none because we are going to have no capture clause we are going to pass in no arguments that return type is going to be white and in the lambda statement part we are going to be printing a simple statement that it's going to be a C out statement. So we already have this texture in the main function what we are going to do is that we are going to write in auto lambda. Since this is a lambda function and first we will define these brackets that are for the capture clause and these are going to be empty which means that this function basically wants in no capture clause. Then we are going to define empty brackets which means that it is also going to have no arguments and then we are going to specify the body as you can see that I have skipped the return type declaration and now I have skipped that when I skip that the compiler will deduce that the return type is basically white if you I want to have some return type I would have specified it here but in this case the return type is going to be void and the compiler is going to deduce it by himself that the return type is white since I have written nothing over here. So in here we are going to have a simple C out statement which is going to say let's say it's going to say I am a lambda function. And finally we have to put a colon in here and now what we are going to do is that we are going to call the function as lambda and then if we just run this code you can see the output right here on your screen that say I am a lambda function. So this function has been called using the name with which it has been actually initialized. So this is the way you have to call in lambda functions and lambda functions is going to be called in automatically. Now as previously explained some components of the lambda definitions are optional so if you write in three lines these are going to be completely equivalent. So let me just write in those three lines. So first we have let's say this very same line. So we are going to just copy this from here. Paste it right here to just show you as you can see that the first one in which we have no capture clause no arguments no return type and we have done this body that have a simple see out statement. Now if you just write in here and just paste it very same thing and right here in these parenthesis if you write in white which means that it includes the parent parenthesis for the parameter list with the white keyword now this means that it is going to return in and we are going to if you write in white over here this means that it is going to receive in no arguments and this statement above and this statement below have the very same meaning you can write it in both ways whatever you like. Now the third one is that you can also specify the type as I told you that if you don't write anything over here this would the compiler is going to assume that the return type is going to be white but if you want to write in something here for your satisfaction then you can write in white like this and arrow and then this white statement added is going to have the very same output. So let me just show you how all this stuff is going to work this is basically the first way this is the second way and if I just remove it from here copy this from from here and then paste it right here and write in white over here and these are the three basically ways and these three lines show basically the very same examples of the definition of a lambda expression. So let me just use them right above here. So I will just remove it. You now know the three ways so we can as I told you use white over here. So if I just run it you're going to see that it's going to print out the very same output. And if we talk about the third way we have to put in the return type that is going to be white and now if you just run this code you are going to yes you're going to witness the very same output that says I am a lambda function. So these are the three ways in which you can initialize this very same function that is going to print out the very same output white here means that there are no arguments which this function is going to receive and this white means that this function is going to return nothing. So now let's move on to an example in which we are going to receive in some arguments. So for example we are going to receive in two arguments. Let's say the first one is an integer value x and then we have an integer value y. And let's just remove it for now. All right now in this function what we are going to do is that we are going to let's say just print out the sum of those two numbers sum of numbers is and in here what we are going to do is that we are going to write in x plus y simple enough. All right now the place from where we are calling this function we have to actually pass in some arguments to this function now. So for example the arguments are three and four. So now if you just run this function you are going to see right in that it says sum of numbers is seven. So this is how you can actually pass in arguments to a function now for example you want to call this function again with let's say some other arguments like say let's say this time we want to pass in five and six to this function. So if you now run this code you can see that it says first sum of numbers is seven that is for the first two inputs and then it says the sum of numbers is 11 that is for the second set of inputs. And to now discuss on with the final case that is when this receives in some arguments and then it also has to return some value. So in that case what we are going to do is that we are going to specify the return type as well. So for example what this function is going to do is that it's going to let's say return an integer value then in that case what we are going to write in after this is we are going to write in this arrow and then we are going to write in it which means that the return type now is going to be an integer value and what is going to return is let's just cut this out and we are going to write in return the sum of those two numbers we are going to write in return x plus y. So we'll just remove this second statement from here and we will operate it for these two values. So it printed out nothing because we haven't store it in some variable we have to store it first we have to write in a equals to lambda then we have to see out the sum of two numbers is and then we are going to write in a that is the value that is stored. So now we are going to run it and you can see right in that says the sum of two numbers is 7. So this is how basically you can just add a return type as well in this very same function. So this is how lambda functions can be used in your C++ programming this was the syntax and the use as well. In the future tutorials you are also going to have some more tutorial on lambda expressions and you will then come to know about certain building function in lambda expressions for example you are going to learn about for each method and many others as well. So for now let's move on to the conclusion part. So we have basically provided some very simple examples that allowed us to focus on the new syntax that was introduced by C++ 11 lambda expressions. You can apply all the things learned in these examples in a more complex scenario such as when you call an STL method or you work on the libraries that require functions that can be replaced with lambda as happens with threading building blocks. So I suspect you will find that once when you start using C++ 11 lambda's so when you start on using C++ 11 lambda's there is no going back. So that's it. I guess with this tutorial and in the next tutorial we will cover more features that were added in C++ 11. So thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Omar Khan and I welcome you to another tutorial on this section in which we are discussing the new features that were added in C++ 11. In this tutorial we are going to be discussing about uniform initializations. Now uniform initializations is a feature in C++ 11 that allows the usage of a consistent syntax to initialize variables and objects ranging from primitive type to aggregates. In other words, it introduces brace initialization that uses braces to enclose initializer values. Now the syntax is shown right here on your screens. You can see first you have to specify the type then you have to specify the variable name and then you have to specify the arguments. Now there are some of the different ways of initializing different types in uniform initializations. Let me just show you how this is done. So we'll move on to our compiler and we don't want to output anything. What I need to do is I need to only show it to you that how we are going to do it. Now the first one is the uninitialized built-in type that is a simple integer like this. This is basically the uninitialized built-in data type. Now the second one is the initialized built-in type that is basically something that has a value as well. For example, if you just make it equal to int i equal to 10 that it is no more an uninitialized. It is an initialized built-in type. Then in the initialized built-in type, we have one more type that is for example, if you write in something like int k and we specify some value right in here, let's say it v2. So this is also an initialized built-in type. Then we have the aggregate initialization in which we have something like int a and specify the size right in here and then make it equal to some set of elements. Let's say this. So this is basically aggregate initialization. Then we have the default constructors and parameterized constructors to talk about the default constructors. It looks something like for example, if we have the constructor a and then this is going to be v1 is going to be the object for this. So this is basically the default constructor to talk about the parameterized constructors. What we are going to do is that we are also going to pass in, let's say some arguments. Let's say one is going to be passed in to the function. So then we have the parameterized constructor with a single argument. You can, for example, if you have a single argument, which in this case is it is also a single argument, you can pass it in one more way. You can just write in the name of the object and you can make it equal to one. So this one is going to be passed in to the parameterized constructor as well. And then finally we have the copy constructors in which what we are going to do is that we are going to copy from one object to another object. Now this very same thing is initialized using brace initialization. Then this very same initialization which we have done here can be rewritten in a different way. Now, for example, the first one was the uninitialized type that looks something like this. Now, if this is initialized using the brace initialization, then it would look something like this. And for the second one that is the initialized building type, this thing right here can be written in the very same way. This is written right here. For example, if it i equal to 10, then for brace initialization, this can be done it like this. So the next one is the aggregate initialization, which will look the very same way when you are dealing with the brace initialization. Similarly, if you talk about the constructor and the structure, then it is going to look a bit different. For example, the default constructor is going to look something like, for example, this is going to be class a for example, then x one is the object. Then for brace initialization, you have to add these brackets for the default constructor. For the parameterized constructor, it is going to look the very same way and for the copy constructor that is right here in brace initialization, this thing right over here that is actually the copy constructor. It is going to look something like a and you have to write in we one. Then the object to which it is going to be copied is going to be written right in the brackets. So this is how this can be done using brace initializations. So now if you move ahead, the next thing is the application of uniform initializations. Now there are four applications of uniform initialization. The first one is the initialization of dynamically allocated arrays. The second one is the initialization of an array data member of a class. The third one is the initialization of an array data member of a class and the fourth one is the implicitly initialize function parameter. So let's start on with the first one that is the initialization of dynamically allocated arrays. So let's just see an example of how it will be done. So we will just move on here. We will remove this stuff from here. And now what we are going to do is that we are going to write a simple C plus program that is going to demonstrate the initialization of dynamic array in C++ using uniform initialization. So this library as you know from the start that this is going to be included right in here. So the next thing we are going to do is that we are going to call this main function. And what are we going to do in this main function is that first we are going to declare a dynamic array, then we are going to print the contents of that array and the dynamic array which we are going to declare is going to be initialized using braces. So for example, it is an integer array, which means that it store integer elements. So it is going to be an integer pointer since it is going to point towards the element basically and it is going to be created using new int, which means an array class is going to be called in. Five are going to be the number of elements in that array and let's say the elements are one, two, three, four and five. Now, if you want to print the contents of that array, what we are going to do is that we are going to use the for loop int i equal to zero since we are going to start on from the very first element, then it is going to iterate until i less than five and then finally i plus plus. So in this loop, what we are going to do is that we are going to write in C out and we are going to write in five plus i. So for the first time, it is going to print out the first element, then it is going to keep our trading until the last element. So let's just run this code and equal to sign over here and here is the output that says one, two, three, four, five. That is the output of this program that has printed out the elements of the dynamically allocated array that was initialized using the brace initialization. So this was a very simple example of the first case that is the initialization of dynamically allocated arrays. So the second one is the initialization of an array data member of a class. So that's also just got an example for how this is going to work. So I'll just remove this and we are going to write in a C plus plus program to initialize an array data member of a class with uniform initialization. So in this case, we are going to create a class right after this. And in this class, what we are going to do is that we are going to create an array of let's say the size of the array is four. And after this, we are going to initialize an array using uniform initialization and that array is going to be public in this case. So what we are going to do in here is that we are going to create a and then we are going to receive the elements in this constructor function that are and x and y and z and let's say next element is in W. So right in here, what we are going to do is that we are going to insert these elements in the array. So these are actually the elements of that are going to be received in this constructor function. And now what we are going to do is that we are going to actually simply insert these elements into the array. So since this is a single line, let's just do it like this. And then you're going to write in the name of the array and then you are going to write in x, y, z and w and finally these brackets and these are basically the brackets of this constructor function. So now after this, what we are going to do is that we are going to create a function that is the print function. And this print function, what we are going to do is that we are going to simply print out the elements of this array. So we are going to use a for loop for this and we are going to write in for int i equal to zero. i is less than four i plus plus. And in this follow up, what we are going to do is that we are going to simply see out the array elements as we did in the previous case. So it is going to be array plus one, not plus one, it is going to be I. And finally, this, and now right down here in the main function, what we are going to do is that we are going to create new object. And the number to initialize the array with will be passed into as arguments to the constructor. So we are going to write in a a we are then going to pass the elements, let's say the element are 123 and four, then using this object we have created, we are going to call in the print function. And finally return zero. Now one more thing we need to do in here is that you have also Yes, we already have this. So let's just run this code. So you can see that it printed out the elements of the array. So this was how you can basically initialize an array data member of a class and print the elements of the array. Now, the third case is the implicit initialization objects that is to return. So let's also see an example of how this is done. So we'll just remove this stuff from here. We will remove this stuff from here as well. And we are also going to create a class in this case. And in this class, what we are going to do is that we are going to create in two elements, that is int x and int y. And we are going to basically implicitly initialize two objects, and we are going to return these values, whatever will be these values. And these values will basically what is going to be passed in when the object is implicitly created. So we are going to create the constructor function that is going to be public. And in this, what we are going to do is that we are going to receive in two values, let's say int a and int b. And what we are going to do with these value is that we are going to write in a, not a, we are going to write in x a and a comma, then we are going to write in y b. And then finally, this is the body of the constructor function. So basically, this is called the implicit initialization of an object to return some values. So this is what is going to return the values for us. Basically, this int x and this int y is going to store these values and then these values are going to be returned. Now how these are going to be returned for that, we have to create a print function that is going to print out the elements. And in this print function, what we are going to do is that we are going to see out the value of a, and then we are going to see out the value of b. Now what we are going to do is that we are going to actually return these values. Now how these values are going to be returned, this print function is also going to be public in this case. Now, right after this, what we are going to do is that we are going to create an overloaded constructor. And the compiler is going to automatically deduce that the constructor of the class a needs to be called and the function parameter of the function are basically going to be passed here. So how this is going to be done, we are going to write in the name of the class, then you have to write in anything like f. And then you have to write in the values that are int a and int b. They are going to be x and y in this case, and x and y here as well. Because these are actually the values that are now stored in as public right above here. Since these values are only visible into a and this int b are only visible to this constructor function since these are the arguments passed into this. So we are going to use this x and y in which the values are actually passed in. Now in this function, what we are going to do is that we are going to write in return a not a x and y. Now, the compiler is automatically going to deduce that the constructor of class a needs to be called. And the function parameter of this f right here are needed to be passed here. Now, if you talk about the main function, right in here, what we are going to do is that we are going to first create an object. And we are going to write in F, then we are going to pass in some values, let's say one and two. And then using this object, you are going to call in the print function and finally return zero. Yes, one mistake we did here is that we have to make this function out of this class because if and yes, one thing we have to do in here is that we have to declare this stuff out of this class because if you don't do so, this statement that is a xf one two is going to generate error. Now why it is going to basically generate error is because this thing is outside the scope of this call, for example, by just press control Z. And this statement is right in here and you run this code. You're going to see an error generated that says F was not declared in the scope. But if you just remove this and paste it right in here, this statement over here is going to excesses. Now, how is the compiler going to know that this constructor is going to be called as I told you earlier that the compiler is going to automatically deduce that the constructor of class a needs to be called and the function parameter of F are needed to be passed in here. So these values are going to be then returned here. And what this extra print is going to do is that it's going to call in this function. And it is going to print in basically this error here is going to tell it that it is the part of the class a. So it is going to receive the values of this x and y. And these are going to be returned from here and printed using this statement. So if you now just run this code, it is not going to generate any error, I guess. And yes, it didn't. And it has also printed out the values for us. So this was the third case that was if implicitly initialize the object to return values. Now the final and the fourth case is implicitly initialize the function parameter. So let's see an example of this as well. So we will remove this stuff from here. And we will also remove this stuff from here. Now what we are going to do is that we are going to create a C plus plus program to demonstrate how to initialize a function parameter using uniform initialization. So the same we are going to have a class a and then we are going to have two variables, let's say int a and int b. And then in the public constructor, what we are going to do is that we are also going to receive in two values, int x and it wide b. And with these values, what we are going to do is that we are going to actually store them and where we are going to store them in these variables. So we are going to write in a x and a y. And then this is the body of the constructor function. And now right in here, what we are going to do is that we are going to create print function as we did in the previous example. And we are going to simply see out a, see out b. Now you don't need to get confused that in the previous example, I have written x and y here and now I'm writing a and b here. That's because I have actually initialized a and b in here. In the previous case, I have initialized x and y here and int a and in b in here. But now I've just reversed it. So that was not on purpose. That was just declared. So we have to use an a and b in here and a and b right here as well. And this needs to be white print. Now what we have to do is that the very same way we have to create. Now in the previous case, as you know that we have actually used the constructor. Now what we are going to do is that we are going to now create a function actually. And we are going to first write in the name of the function. And then we are going to write in this x that is the object. And in this function, what we are going to do is that we are going to call in the print function. And in here in the main function, what we are going to do is that we are going to call the function and initialize its argument using the brace initialization. So we are going to write in F. And then right in here, we are going to use the brace initialization and we are going to pass in the value. Let's say the value are one and two. And we are going to write in return zero. So if you just run this code, it is going to solve our case number four. Right, it says expected unqualified ID and then it says multiple initializations given for this a. So yes, this needs to be removed from here and this has to be by. So now let's just run it again. And right here, it has generated the output we required that are the elements that is one and two. So it has the very same logic that the compiler is automatically going to deduce that the function of class a needs to be called in the function parameter of void function that was declared outside that class are going to be passed into this print function that is going to print out the values for us. So first, the values are going to be received in this. These are going to be copied in the global variables right here that are initialized. And then these are going to be printed using this function. So that's it with case number four. So I guess that's clear. So that's it with this tutorial as well. So this is how basically uniform initialization is used in C++ 11. So I will see you guys in the next tutorial where we will cover more on the features added in C++ 11. Thank you so much for watching. And I will see you guys in the next tutorial. Hey guys, what's up? This is Sumer Khan and I welcome you to another tutorial on the section in which we are discussing about the new features that were added in C++ 11. So in this tutorial, we are going to be learning about the deleted and defaulted functions. So let's start on with the default. Now explicitly defaulted function declaration is a new form of function declaration that is introduced into the C++ 11 standard. You can append the equal to default that is a built-in word. This specifies to the end of a function declaration to declare that function as an explicitly defaulted function. Now the compiler is going to generate the default implementation for explicitly defaulted functions, which are more efficient than manually programmed function implementations. Now a function that is explicitly defaulted must be a special member function and has no default arguments. Explicitly defaulted function can save your effort of defining those functions manually. So if you have a class that is defined with any constructor, the compiler will not generate a default constructor. Now this is useful in many cases but it is sometimes waxing. For example, if I just show you an example and I move on to my compiler and I have for example a class A and in this class A I have a public constructor that looks something like A and that also receive an arguments let's say A and this is the body. So in the main function if I just write in A, A then the compiler will complain that we have no default constructor. For example, if I just run this code you can see that it is generating an error that says no matching function for call A to A. It says the candidates are A into A. Candidates expects one argument zero provided and many other type of errors. Now these errors are basically generated because the compiler did not make the one for us because we already have had one that we defined. Now we can force our compiler to make the one for us by using the default specifier. For example, if we have this very constructor right in here and let's say we define one more constructor that receives in no argument and we define it as default. So now if we do the very same stuff that is AA and we run this code you can see that it ran perfectly because the compiler did not complaint about this anymore because when this was called in it excess this that was the default constructor. So you can basically declare both inline and out of line explicitly defaulted functions. You can declare a function as an explicitly defaulted function only if the function is a special member function and has no default arguments. The explicitly defaulted function declaration enable more opportunities in optimization because the compiler might treat explicitly defaulted function as trivial. So this is how default works in C++11. Now there was one more feature that was added. We are also going to discuss that that was the deleted. Now the C++11 standard basically introduced deleted function declaration as a new form of function declaration. Now to declare a deleted function you can append the equal to delete as in the previous case you have to append equal to delete sorry the equal to default specifier so in this case you have to append the equal to delete specifier to the end of a function declaration. The compiler prohibits the usage of a deleted function. For example you can declare the implicitly defined copy constructor and copy assignment operator of class any class let's say class A as deleted functions to prevent object copy of that class. So for example we have a class that is going to have a constructor that is going to take an integer. So it is going to be a class A and it is going to have a public constructor int A and we don't have this stuff right in here. Then if we just move on to the main function then we are going to write in first A A10 which means that we have actually called in this constructor that is having some argument. So this statement is going to work perfectly this is going to be a correct statement. Now the second statement is let's say we initialize one more object and pass in let's say a float value to it let's say the value of pi 3.14. So this is also going to be okay because the 3.14 is going to be converted to an integer of 3 and this part 14 that is the decimal part is going to be truncated itself. Now if you write in A equals to B so this is also going to be okay because we have a compiler generated assignment operator however what if that was not we wanted we do not want the constructor allow double type parameter nor the assignment to work. C++11 allow us to disable certain feature by using delete. So what we are going to do is that we are going to actually disable the conversion. So we are going to write in here we are going to have the very same constructor then what we are going to do is that we are going to create A that is going to have double and we are going to write in delete. Now what does this mean this means that the conversion has now been disabled and we are going to write in A it is going to equal to and this is basically that the assignment operator has been now disabled. Now if we write the code that is the very same here in the main function a a e a a then this is going to be completely fine but now if you write in A B that is 3.14 this is going to generate an error because conversion from double to integer has been disabled. Write in here as you can see that we have disabled the conversion to double using this constructor and using this delete specifier and secondly we also have this line that is equal to B that is the assignment operator line so this has also been disabled right here so this line is also going to generate an error. So basically the purpose of the delete specifier is to actually disable certain features in C++ which you want to disable for example in the previous case when you passed in this 3.14 to this very same constructor that is expecting an integer value so what it's going to do with that double value that it's going to convert that double value into an integer value by truncating the decimal part for example you don't want any decimal value to be passed into this constructor function so what you are going to do is because this is the default behavior of C++ compiler to actually convert this so what you want in your code was that for example you don't want any conversion from double to integer you only want this function this constructor to receive an integer value so what you are going to do is that you are going to use the delete specifier and you are going to disable certain feature and in this case we have disabled two features first we have actually stopped the conversion and secondly we have disabled the assignment operator so these two lines are going to generate an error message for us and I guess this has to be a build-in world that is operator over here that is going to actually stop the assignment operator this has to be a build-in work so if to write in the complete operator as you can see that it also has been bolded by the compiler itself so now these two lines you are going to see that they are going to generate an error so you can see that it says line number 19 that is the use of deleted function a to double is not allowed secondly in line number 20 as you can see right in here use of defaulted function a and operate which means that the assignment operator has also been disabled so in this way we could achieve what we intended so once you have used a deleted keyword with a function then the compiler will prohibit the usage of a deleted function for example you can declare the implicitly defined copy constructor and copy assignment operator of class a as deleted functions to prevent copy object of that class for example you can declare the implicitly defined copy constructor and copy assignment operator of class a as deleted functions to prevent the object copy of that class so let me give you one more example of what i'm talking so we will have this very same class in which we have a public constructor and this public constructor is going to be empty it's going to be something like this and then we will actually disable the copy constructor and we will disable the copy assignment operator now you know that copy assignment operator is deleted in this way that it was an operator equal to constant a and and then we are going to delete this now to disable the copy constructor what you are going to write in you are going to write in a very same way this one is written right in here and then you are going to write in it is going to equal to delete so simply the name of the class the constant and then a address so this is going to be deleted which means that this assignment for the class the address pointing that address is going to be deleted so if we have in the main function we will just remove this stuff from here and for example we have the object one that is a1 and then if we use the copy constructor for example if i just write in a a2 that is an object 2 and we may i make it equal to a1 so this is going to generate error because the copy constructor has been disabled already so if i just run this code you're going to see that it is going to generate error that says use of deleted function which means that a function that was deleted has been used which is not allowed and it was deleted right up here so the second thing was the copy assignment operator and it is going to be the very same as it was in the previous case for example we have an object 3 and i just write in a3 equal to a1 so it is also going to generate error as the previous one had generated an error so i will just comment it out so that you can see this error so you can see right here that it says use of deleted function and it is the very same so in this example although only one copy constructor and one copy assignment operator are declared as deleted function explicitly the compiler can detect the declaration of the user defined copy and copy assignment operator so the compiler will no longer explicitly define any other copy constructor or copy assignment operator that have different parameters so in this case the copy and assignment operations between the class objects are totally prohibited so this is how delete function actually delete specifier actually work you can disable the features you want to disable so this is it with this tutorial you now know the two keywords that were introduced into c++11 the default and the deleted specifier so that's it with this tutorial thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is Umar Khan and i welcome you to enter the tutorial on this section in which we are discussing the new features that were added in c++11 so in this tutorial we are going to be basically discussing about null pointer so c++11 basically introduced a new keyword null ptr as a null pointer constant the null pointer constant can be distinguished from integer zero for overloaded functions the constants of zero and null are treated as of the integer type for overloaded functions whereas null pointer can be implicitly converted to only the pointer type pointer to pointer member type and boolean type so before c++ if you talk initializing null pointers with zero or null makes it impossible to distinguish between a null pointer and integer zero for overloaded function for example if you see the following two functions as mentioned on the slides you can see that the first one is receiving an integer value and then the second one is receiving a pointer actually so when you call the overloaded function with this statement that is the funk and then you pass in null to it so can you guess which function is going to be called now you might be thinking that this second function over here that is white funk character steric s this should be called because the actual parameter null looks like a pointer however surprisingly the call funk null resolve to funk int the top one here this call is going to call this now why is this going to call the above function because null is a macro and equals to the integer zero which is of the integral type instead of the pointer type now to solve such problems of null pointer constant c++11 introduced a new keyword that was null ptr the null ptr constant can be distinguished from integer zero for overloaded function for example if you see the very same example right on your screen and call the previous overloaded function with this statement now that is the funk null ptr then can you guess what function is going to be called yes now you are correct because this very same function is going to be called now a null pointer constant with the null pointer value has some characteristics the first characteristics it has that it can be converted to any pointer or pointer to member type secondly it cannot be implicitly converted to any other type except for the boolean type third it cannot be used in an arithmetic expression fourth it can be compared with the integer zero and fifth it can be used in relational expressions to compare with pointers or data of the standard null ptr type now it should be noted that in c++11 it is still acceptable to assign the value zero or null to a pointer now what are basically the difference between null and null ptr so basically null is a manifest constant as defined by c that is actually an integer that can be assigned to a pointer because of an implicit conversion whereas if you talk about null ptr it is a keyword that is representing a value of self defined type that can be converted into a pointer but not into integers so as you can see right on your screen you can see some example the first one is int i equal to null then you can see the very same i equal to null ptr and then you can see a pointer that equal to null and then you can see a pointer p that equal to null ptr now can you guess which statement is actually correct and which statement is wrong now the first statement over here is completely correct because you can initialize an integer to null but if you talk about the second one it is going to generate an error because it is not an integer convertible value if you talk about the third one it is completely okay because integer is converted into a pointer whereas if you talk about the fourth one it is also going to be perfectly all right and why it is going to be perfectly all right because of the very same reason we discussed in the previous slide so that is important to solve an ambiguity that can arise in generic programming now suppose you have again two overloaded functions as you can see on your screen the first one is void func and is receiving and is expecting to actually receive an integer value and the second one is void func that is expecting to receive a pointer value so now if you make the very same call as you did in the previous case you call in func null then you are actually calling two things first you are calling the variant being null and integer but func null pointer that is right in here it will call the second variant being null pointer not an integer now to avoid the risk to call one function instead of another always use a zero if you want an integer and null pointer if you want a pointer so i guess that's clear so this was basically the purpose of null ptr to conclude my statements null ptr was only added to remove the ambiguity about zero and now since those two were treated as the very same by the compiler so to remove that confusion c plus plus 11 basically introduced this keyword that was the null ptr so i guess that's clear so that's it with this tutorial as well thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is umar han and i welcome you to another tutorial on this section in which we are discussing the new features that were added in c plus plus 11 so in this tutorial we are going to be discussing about the feature that is a telegating constructors that were added in c plus plus 11 before c plus plus 11 common initialization in multiple constructor of the same class could not be concentrated in one placed in a robust maintainable manner so partially elevated as problem in the existing c plus plus program you could use the assignment instead of initialization or add a common initialization function now with the delegating constructor feature you can concentrate common initialization and post initializations in one constructor named as target constructor now delegating constructor can call the target constructor to do the initialization a delegating constructor can also be used as the target constructor of one or more delegating constructor so you can use this feature to make program more readable and maintainable now delegating constructors and target constructors present the same interface as other constructors target constructors do not need special handling to become the target of a delegating constructor they are selected by the overload resolution or template argument detection now after the target constructor completes its execution the delegating constructor gets its control back a delegating constructor can be a target constructor of another delegating constructor thus forming a delegating chain the first constructor invoked in the construction of an object is called the principal constructor a constructor cannot delegate to itself directly or indirectly the compiler can detect this violation if the constructor invoked in a recursive chain of delegation are all defined in one translation unit now you can use the delegating constructors feature interacting with other existing techniques that includes first when several constructors have the same name name and overload resolution can then determine which constructor is the target constructor secondly when using delegating constructor in a template class the deduction of template parameter works normally now if you talk about c++ three there are often cases where it would be more useful for one constructor to call another constructor in the same class unfortunately this is disallowed by c++03 commonly this end up resulting in either duplicate code or use of an initializer non constructor function to keep the common code accessible to both constructors that need it now how does it actually look like with c++03 and how it was then corrected by c++11 i will give you an example of that and then you will be much more clear of what i'm talking so here we are so in here for example we have a class a and in this class a what we are going to have is that we are going to have a public constructor and this is our constructor body so for example if we have one more constructor here that receives some value a and for example we have to write some code in here that is named as code one and then in here we have to write in some code two but we also want to use code a so what we are going to do is that we are going to write in code one write in here again so as you can see right in here that the code one is defined twice and as i talked about that use of initializer a non constructor function when you have to write in the common code or you have to write in the initializer a non constructor function to keep the common code accessible to both constructor so in that case this very same code is going to look something like we are going to have this very same a function and in here we are going to have i and i t that is the initializer for let's say let's call it as x and then right in here in the second function we are going to have this very same initializer that is i and i t x and as you can see right in here that the code to write in is here the code to write in this is right in here and now for example we have a white i and i t x right in here and in here for example we have to write in code number one so while using the i and i t method it is considered a better practice than duplicating code now it has a couple of downsides as well first it's not quite as readable as it adds a new function and several new function calls second because i and i t x is not a constructor it can be called during the normal program flow where member variables may already be set and dynamically allocated memory may already be allocated this means that i and i t x needs to be additionally complex in order to handle both the new initialization and reinitializes cases properly now c plus plus 11 actually solves these problems and it adds the ability to chain constructor together that is also called as delegating constructor for which i've been talking at the very start now how this is going to actually help you that we are going to have this very same thing in which we are going to have a class a then we are going to have a constructor and in here for example we have to write in code number one and then we have the second constructor that has some argument then what we are going to do is that for example in here we want code number one and code number two as well so now what delegating constructor is going to help us that we don't have to write in this code now how this constructor that is receiving an argument is going to get this code is by the property of c plus plus 11 to actually chain the constructor now how this constructor is going to be chained to this constructor is a very easy task you all may have to add this constructor after this so this constructor that is receiving no argument is going to actually be chained to this constructor that is receiving an argument and code number one and is automatically going to be accessed by this constructor so as you can see that it is much cleaner so one thing to note you should always use the initialization list syntax when delegating constructor as compilers that do not support delegating constructor will generally flag this as a compiler error so if you try to call one constructor from the body of another constructor the compiler will not complain and your program may also not behave as expected so this is how basically constructors are chained and your code looks much cleaner much easier the duplication of code is also removed as you can see that this code one and code two is used by this as a whole and this constructor only can use code number one so i guess that's it with this tutorial thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is Umar Khan and i welcome you to another tutorial on this section in which we are discussing the new features that were added in C++11 in this tutorial we are going to be discussing about the r-value references but before discussing r-value references you need to know about what is passed by value so when a function gets its parameter as by value it does copy the compiler knows how to copy it if it's a user defined type we may need to provide the copy constructor and probably the assignment operator as well to be able to do a deep copy however copying process is expensive actually there are a bunch of copying going on when we use the stl containers when an object tossed in by value since it's a temporary object we are really wasting our precious resources including the cpu as well as the memory and so on so we need to find a way to avoid such an unnecessary copy now the question arises why our values were introduced so the primary purpose of introducing an r-value is to actually implement move semantics so what is move semantics so before moving ahead you need to know about this term in c++3 there was a costly and unnecessary deep copies that can happen implicitly when objects are passed by values we can avoid the performance hit due to the deep copy by using an r-value references now we have a way to determine if a reference variable refers to a temporary object or to a permanent object so how it can be used the main usage of r-value reference is to create a move constructor and a move assignment operator if you talk about the move constructor it acts like a copy constructor it takes an instance of an object as its argument and it creates a new instance from the original object however the move constructor can avoid memory reallocation because we know it has been provided a temporary object so rather than copy of the fields of the object we will move them in other words the r-value references and move semantics allow us to avoid unnecessary copies when working with temporary objects we do not want to move the temporary which will go away so the resources needed for the temporary objects can be used for other objects as well the r-values are typically temporary and they can be modified if we know that our function parameter is an r-value we can use it as a temporary storage or take its contents without affecting the output of our program this means that we can just move the contents rather than the to copy the contents of an r-value parameter this saves a lot of memory allocation and it provides a lot of scope for optimization for large dynamic structures so now let's move on to r-value references so as i told you that the primary purpose of introducing an r-value is to basically implement move semantics r-value references actually enable us to distinguish between an l-value that is actually the locating value or you may say the locator value from an r-value the c++11 standard introduces r-value references which bind only to r-values they are declared with two amster stands rather than one ampersand so let me just give you some example of how r-values look like and how they can be used in c++11 so this is our compiler and in here if you talk about r-value references it actually looks something like you have to write in the type and then you have to add in two addresses then you have to write in the name of the variable let it be ref and then you can write in a value that is 99 so you have to note here that only an l-value can be assigned to our normal reference so if we for example write in int and ref equal to 99 then this part is going to generate error that is the invalid initialization of a non-constant reference to the type this that is int address from an r-value of type integer so if you run this code you can see that it is going to generate an error that says conflicting declaration previous declaration as int and ref or if you just change its name to something like refe it is again going to generate an error because it says that invalid initialization of a non-constant reference of type this from an r-value of type int as I told you before so if you're dealing with this part so if you just remove this from here and you want to do it using a single operator then what you have to do is that you have to first initialize the value for example I just write in int x equal to 2 and then I'm going to actually use this reference and then I'm going to write in the name of the variable actually I cannot make a direct reference so if I now just run this code it is going to run perfectly so in the previous case you have seen that if I just write in some variable here and directly make a reference to it like this it is going to generate error saying that it is an invalid initialization of a non-constant reference to the type int and from an r-value of type int so this is an example of r-value reference now we have let's say two function one that's taking an l-value reference and the other that is taking an r-value reference so how this can be done let's just remove this stuff from here and write above here let's say we have two functions first one is white f that is going to receive a normal one that as I told you that is going to take an l-value which means that it is going to be something like this then in the body of this what we are going to do is that we are going to see out since this is an l-value so we are going to write an l-value reference and we are going to print out the variable and an n-line space so we are going to now create one more function that is going to have the very same name but it's going to use an r-value reference and with this what we are going to do in this body is that we are going to see out since this is an r-value reference so we are going to write an r-value reference and we are going to print out the value so good enough now in the main function what we are going to do is that we are going to do the calling part so first we need to give this i a value let's say the value of i is 10 so now if i just call this function by passing this then what is going to be called it is going to call the l-value reference but if i just pass in some value right in here then what it's going to do is that it's going to call in this r-value reference and why this is going to actually call this as i told you before when i was doing this is an example that if you write in this and let's say you make it equal to x and you make it point towards something then it is going to work perfectly but if you make something like this it is not going to work perfectly but if you make it like some value here and then you point it towards this x then it's going to work perfectly you have to just change this to some other value so now it's going to work perfectly so it's you can see in this example that if you pass in the value so you know that in case of r-value you cannot directly reference a value so in this case since this is not directly referencing a value in this case since first we have initialized a value and then we are pointing towards the integer in which this value is actually stored in that is the variable i so this is going to call the l value and in this case since we are making a direct reference towards this 99 so what is going to do is it is going to call the second function that says r-value reference yeah we missed this one and right here as you can see that it says l value reference 10 and r-value reference 99 so i guess this is clear so if you talk about move semantics here so if you just scroll down here into the main function and what we are going to do is that we are going to call in the f function but what we are going to now do is that we are going to use the move function and then we are going to write in i which means the value has been now passed now what is going to be called actually this function that is the r-value reference function is going to be called now why it is going to be called because that the move i is calling a function taking an r value this is possible because the move takes an l value and it converts it into an r value so an expression is basically an r value if it results in a temporary object so if you just run this code you are going to see the output that says r value reference 10 so you can see that for the first one the l value reference was done but now when you use the move function and pass in the l value it results in calling the r value reference so as i told you that r value is basically an expression that results in a temporary object for example if i have a function right above here and for example i have a function that's return type is int and let's say the name of the function is a and what we have in this function is a variable let's say x is the variable and it is equal to let's say value 2 and then what i do is that i simply return this x now in the main function if you just remove this stuff from here and what you do is that you see out only this a function whatever is returned by this function since this is a returning function and it is going to return an integer value so if you just run this code right now it's going to run yep as you can see that it has printed that value so basically this function a is an r value because if you note that the value being returned is not a reference to the variable x but it's just a temporary value now in c++ 0x we could use a normal or l value reference to bind temporary object but only if it was constant for example i just moved down here and as i'm talking that we could use a normal or l value reference to bind temporary object but only if it was a constant so i mean by this that whatever this reference is going to be it is going to be a constant value as you can see right here for example we can now call in the function a and it is going to be perfectly all right so i'll just remove it from here paste it right here and if i just run this code it is going to run perfectly so no error generated but now if i do the very same and just remove this this statement from here and do not make it constant and run this code it is going to generate error sayings that it is an invalid initialization of non-constant reference of type integer reference from an r value of type in now in c++ 11 the r value reference let you bind a mutable reference to an r value but not an l value in other words r value references are perfect for detecting whether a value is a temporary object or not for example you just remove this statement from here and then you write in constant and an r value and then you call this very same function a then this is perfectly right as it was in the previous case when you use a single and operator but now in the previous case if you wrote in something like this with a single and operator and call the function a it generated an error but now in case of r value since you are using r value now it is not going to generate any kind of error all right this has to be a different variable name let's make it equal to value and this is going to be now perfectly all right so as you can see that the code ran perfectly no error was generated in here so if you just compare those references so how to compare those references we are going to make into functions for that so the first function is going to be for an l value and a constant value that is going to be receiving a constant value and the second one is going to be for an r value so for example let's say the name of the first function is a and it is receiving a constant l value all right so the next function right here is going to be a void and this is also going to be a void function since it's not going to return anything now what it's going to do is that it's going to simply see out whatever value is received in all right so right down here in the second function let's say the name of the function is b then in this function what we are going to do is that we are going to simply see out the whatever value is received in this function and how this value is going to be received this is going to be an r value receiving so it is going to be written with double and operator and then this value so basically the function a takes a constant l value reference and it will accept any argument whether it be an l value or an r value however the second overloaded b steric take only an r value reference in other words by using the r value references we can use function overloading to determine whether the function parameters are l values or r values by having one overloaded function taking an l value reference and another taking an r value reference in other words c++11 introduces a new non-constant type reference called an r value reference identified by the name of the variable and then a double and operator and yes one thing over here this has to be the very same name as it is for this because we are discussing actually the overloading here because when an l value is going to be passed in this function is going to be called in and when the r value is passed into a function this function is going to be called in so you don't have to change the name of these functions the name of these function is going to be the very same what's going to determine or going to tell the main function which functions to be called is it's going to determine by these arguments and how it is going to determine by these argument because this receive only an l value and this receive only an r value so now basically this refers to temporaries that are permitted to be modified after they are initialized which is the corner stone of move semantics so let me give you an example of this so we have very same stuff over here what we are going to do is let's add some professionalism to this and since this is the left value so we are going to write in this and for this one what we are going to write in we are going to write in simply r value and this so now what we are going to add in this is that we are going to add a function that is going to be of integer type and it is going to be the very same function which was initialized in the previous example that was the a function but since now we have already used a function so we are going to use it as b function and in this what we are going to print out we are going to actually print out we are going to first initialize something and then we are going to return it since it is a function that is going to return an integer value so what we are going to do is that we are going to initialize a temporary value and since as you can see right here that we are actually dealing with a value over here so first what we are going to do is that we are going to create a temporary value and make it equal to any number then let's say and then we are going to simply return that temporary value so now in the main function what we are going to do is that first we are going to initialize a value let's say i is the value and make it equal to 11 then what we are going to do is that we are going to call in the a function bypassing in this i and then we are going to call in the a function but now what we are going to do is that we are actually going to pass in this function that is the function b and this is actually the a function and it is actually going to pass in the value 10 so if you just run this code you are going to see yourself what is going on so as you can see that first it says l value for this one it says the l value 11 which means that it is printed out this because this was passed in to the function that was the l value function so it printed out simply this 11 now when you pass in this function what it actually did is that it went to this function it grabbed its value that was stored in this variable since this function was returning this temp value so it grabbed that value put it in these brackets and now this a function was called in by passing in this value so when this value only this value was passed in what it did that it called the r value function and this stuff over here was printed as you can see right on your screens so i hope it's clear so as you can note here that we took out the constant from the void a function that was the l value function actually so that it can take only the l value now we have two distinct functions that can be overloaded one taking an l value and the other one taking an r value so what it basically gives us a way to write more efficient program with less coding so i hope you have understood all these examples so if i just give you a brief summary for what we have been covering so far so basically r value references are added into c++11 and they are basically the declaration r value references and they are new to c++11 secondly non-constant l value reference binds to an object and thirdly r value reference binds to a temporary object which typically will not be used again so these three points actually summarize all the concepts we have been covering for the last 20 minutes so i guess that's clear to you guys so that's it with this tutorial thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is Umar Khan now welcome you to another tutorial in which we are discussing actually about new features that were added in c++11 so in this tutorial we are going to be learning about automatic type declaration and the declaration type so basically type inference refers to automatic deduction of the data type of an expression in a programming language now before if you talk before c++11 each data type needs to be explicitly declared at compile time that actually limit the values of an expression at runtime but after the new version of c++ many keywords are included which allows a programmer to leave the type deduction to the compiler itself now with type inference capabilities we can spend less time having to write our things the compiler already knows as all the types are deduced in compiler phase only the time for compilation increases slightly but it does not affect the runtime of the program now before moving on to the automatic type declaration you need to first know the use of auto keyword and the normal declaration type and at the end we are going to see an example in which we are going to see that how these two are implemented together so let's start on with the auto keyword the auto keyword basically specifies that the type of the variable that is being declared will be automatically deducted from its initializer in case of functions if their return type is auto then that will be evaluated by return type expressions at runtime so let me just show you a c++ program to demonstrate the working of auto and the type inference so i will just move on to my compiler right in here and what we are going to do in here that we are going to write in a very simple program in which we will have two auto variables first one is going to be auto x that is going to be equal to let's say one and then we have a second variable let's say auto y that is going to be equal to let's say two now we are also going to have a pointer variable we are going to make it equal to pointer and we are going to make it point towards this very first variable that is right in here x so we are going to write in x over here and what we are going to do is that we are going to see out the type id x dot name that is a built-in function and similarly we are going to do it for the other tree as well so we are going to write in type id and we are going to write in y dot name and similarly for the third variable as well that is pointer basically so we are going to write in type id we are going to write in ptr dot name and find the endel now if we just run this code we are going to add a library for this type id as well because it is going to answer which error and that library is basically the name of the library is x that is a standard c++ header file so it is going to be std c++ dot edge and it's going to have this text as well so i guess it's complete now so now let's just run this code so you can see right in here that first we have i right in here then again we have an i and then finally we have ti as an output so in here we have basically used the type id for getting the type of the variables type id is basically an simple operator which is used where a dynamic type of an object needs to be known type id x dot name that is the first one right in here it is basically going to return the shorthand name of the data type of x for example it returns i for integers d for a doubles pi for the pointer to integer at a cycle but actually name is returned that is mostly compiler dependent so i guess that is clear so let me give you a one more example in which we are going to see the use of auto to avoid long initialization when creating i creators for containers so we are going to remove this stuff from here and again we are going to be needing this library that is bit standard c++ header file and now what you're going to do basically is that we are going to create a set of strings and we are going to create an object let's say a for this now what we need to do is that we need to basically insert data into it so using this a what we are going to do is that we are going to call you insert function and after calling this insert function what you're going to do is that you are going to insert data into it so what is going to be the data we are going to insert multiple data elements into it for example the first one is my name then let's say it has my second name and then let's say it's a tutorials and i guess that's it so call them right here so now what we are going to do is that basically this pointer is it is going to evaluate i trigger two sets of string type automatically so what we are going to do basically is you're going to do the for loop and what we are going to do is that we are going to use the auto keyword which is going to help us to evaluate to i trigger to a set of string that is present right in here if we have inserted the three elements that's umar han and then tutorials and what we are going to do with this is that we are going to create one more element that is b a dot begin which means that it is going to start from here which means that it is going to take in from the very start and what it's going to do is it will iterate till the very end so it is going to be until and unless this b right in here it is not equal to a dot end so what it's going to do is that it's going to keep adding it incrementing this one and then in this for loop what we are going to do is that we are going to simply see out the steric which means the pointer of this object b all right so let's just find the right and return zero and i guess let's just run this code so you can see that it says han then it says umar and then it says tutorials so this is how basically the auto keyword is going to help us when using long initialization and it's going to actually avoid the long initialization when we are creating the iterators for containers so let's move on to our slides and the next thing we are going to see is a normal declaration type so the decl type keyword is used for that purpose it basically inspects the declared type of an entity or the type of an expression the auto lets you declare a variable with particular type whereas this type that is the decl type also known as declaration type it lets you extract the type from the variable so declaration type is basically a sort of an operator that evaluates the type of past expression so let me just explain this and it uses what the help of an example so let me just demonstrate a c++ program to use decl type in your code so what we are going to do is that we are going to first remove this stuff from here we are going to be needing this library that is bits standard c++.h and what we are going to now do is that we are going to first initialize two functions right here at the top and then using decl type what we are going to do is that we are basically going to use those functions and using the type id operator as in the previous tutorial we are going to return the type id dot name of those function variables so first we are going to initialize in two variables first function has a return type of integer and let's say the name of the function is a and talk about the body of this function what it's going to do it is going to return simply a variable let's say two since it is an integer function so let's say the second function is a corrector function and its name is let's say b and since it is a corrector function so it's also going to return a character so we are going to write in let's say it's return g so now in the main function what we are going to do is right here first we are going to create actually the variables for those using the declaration type which we did using the auto keyword in the previous previous part so we are going to write in a and then we are going to let's say name it as x as we did in the previous tutorial and then we are going to do the same for the second one that is the function b and let's name it as y and now what we are going to do is that we are going to see out it in this very same manner the way we did it in the previous tutorial type id x dot name and similarly see out type id y dot name and a space of line so writing this return zero let's just run this code so you can see that it says i and c and it has the very same logic as was for the previous case so let me just give you one more example to demonstrate the use of declaration type and for that we will just remove it and in this example we are going to remain simple and be out of the this stuff of functions so what we are going to do is that we are going to initialize a normal variable x equal to let's say two and now what we are going to do is that we are basically going to use the dc l type for this variable x that has been initialized we are going to write in the name of the variable right in here that is let's say a this time and what we are going to do is that we are going to actually increment the value of x let's say we want to increment it by two so now what we are going to do is that if we now see out the type id of a dot name and write and return zero and run this code then in output you are going to see this i variable so this is basically the reference towards the pointer that is basically printed out by this type id variable and i've already explained what this type id does so i guess that's clear about declaration type as well we've already covered an example so now let's move on to automatic type declaration that is basically let you can call it actually a combination of auto and the ecl type so to discuss the relationship between auto and declaration type auto and declaration type basically serve different purposes so they don't map one to one auto is a keyword in c plus plus eleven and later that is used for automatic type deduction the declaration type specifier yields the type of a specified expression unlike auto that deduces types based on values being assigned to the variable dcl type deduces the type from an expression pass to it the value written by declaration type can directly be used to define another variable now auto follows the rule of template parameter deduction while declaration type has rules it should follow defined in the standard for example if you consider the size of operator then similar to that size of operator the operant of the declaration type is unevaluated informally the type returned by the declaration type is reduced using some rules the first one is that if the expression e refers to a variable in local or namespace scope then a static element variable or a function parameter then the result is that variables or parameters declared type secondly if e is a function call or an overloaded operator in vocation the declaration type for that variable e denotes the declared return type of that function otherwise the third rule that is followed is that if e is an l value then the declaration type for e is a template address where template is the type of e if e is an r value the result is the template now these semantics were basically designed to fulfill the needs of generic library writer while at the same time being intuitive for novice programmers because the return type of declaration type always matches the type of the object or function exactly as declared in the source code more formally the rule one applies to unparanthoized id expressions and class members excessive expressions for function calls the deduce type is the return type of the statically chosen function as determined by the rules for overload resolution so now let me give you a very quick example of how auto and declaration type are used together so we will define a c plus plus template function that is going to return the minimum of two numbers the two numbers can be of any integral type the return type is determined using the type of minimum of sets so let's just go ahead so we'll move on to my compiler we will remove this stuff from here and as in the previous cases we are also going to be needing this library so the first thing we need to do above this main function is that we have to create a template using the template keyword you've already covered template in the previous section so you have an idea of how templates actually work so we are going to define into class a and then a class b and right down here what we are going to do using this template is that we are going to create a function that is the minimum function in which we are going to as i told you that it is going to return the minimum value for class a it is going to be a a which means a variable a of data type a then for the class b what we are going to do is that we are going to initialize a variable b now this was basically the job of auto to declare those two variables so now what declaration type over here is going to do is we are going to write in declaration type and this basically is going to check for the condition that is a is less than v then if it is then what it's going to do is that it's going to check it using the afl's conditions and then in the body of the function what we are going to do is that we are going to return a is less than b and we are also going to specify the afl's right in here so to talk about the driver code that is the main function code what we are going to do in here is that we are going to see out whatever result is returned from the above function and first what we are going to do is that we are going to pass into values to that function that is the minimum function so we are going to let's say call in the function and pass in let's say the first value is an integer value and let's say the second value is a float value now you're going to add nline space now what it's going to do basically is since we have used template so you know from your previous knowledge that this a can be of any data type this b can be of any data type so this 3v pass in was an integer and this 4.5 pass in was a float it doesn't care whatever the value was sent to it because it is to receive any kind of data value since it is a template now if we just write and return 0 over here and run this code it is this function over here is going to return the minimum value in this case so let's just run it and you can see that it printed out 3 which is our minimum value in this case so this is how basically you can use the auto keyword and the declaration type in a single function that is also called automatic type declaration so that's it with this tutorial in the next tutorial we will cover more features that were added in c++11 so up till now thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is umar khan and i welcome you to a new section here on this course on advanced c++ in which we are going to be seeing the new features that are added in c++11 so the content covered in these tutorials are basically the additions that are made to c++11 these additions are lambda expressions automatic type declaration and declaration type uniform initializations deleted and defaulted functions null pointer delegating constructors r-value references and new smart pointer classes so we are going to be seeing all of these and we'll start on with the introduction tutorial in the next tutorial so that's it with this tutorial thank you so much guys for watching and i will see you guys in the next tutorial hey guys what's up this is umar khan and i welcome you to the very first tutorial on user defined literals in this tutorial we will cover the introduction to user defined literals we will see the syntax for how it is written and then finally when we will see some examples for how it is done in c++ so what is a user defined literal and what is its use in programming language so moving on to the definition user defined literals are a unique feature in all mainstream programming languages they empower you to combine values with units let me just show you a simple example to help you understand this definition so i will move on to my editor right here and i don't want to output anything i just want to show you that what have i actually spoken in the definition here so for example you have something like a long double and then you have weight and you have initialized it to 2.3 let's say now what is it is it in pounds is it in kilograms or is it in grams this 2.3 so with the user defined literals we can attach units to the values which has some advantages number one the code becomes readable and number two that the conversion computations are done at the compile time so if weight equals to 2.3 kgs for example if we have like weight equals to 2.3 kg and for example we want to compute some kind of ratio that is 2.3 kg divided by 1.2 lb now to compute this ratio actually it is necessary to convert 2.3 and 1.2 into a same unit now this is where user defined literals come into help user defined literals help us to overcome unit translation cost we can define user defined literals for user defined types and new form of literals for built-in types they help to make constraints in code more readable the value of a UDL or user defined literal is substituted with the actual value defined in the code by the compiler at the compile time user defined literals don't save much of a coding time but more and more calculations can be shifted to compile time for a faster and a safer execution so i hope that you've now understood what is definition and what is the advantage of using the user defined literals so moving ahead so moving ahead to the syntax now you can see right here that you have the syntax right on your screens that is first a built-in literals then a plus plus means concatenation sign then we have this underscore right here and then we have the suffix right here now if i just show you that how this syntax is actually implemented you can see some of the examples right here the first one is some binary values then we have an underscore this is actually the built-in literal as i showed you right here this is the built-in literal which is this one zero one one and then we have this underscore these plus plus shows that this is concatenated actually these plus plus don't have to be added when you are writing and finally we have this b which means binary and this is actually the suffix that defined actually this for example in the previous case of weight you can write in as something like let me just show you you can write it as 2.3 underscore kg and this is this 2.3 actually is going to be your built-in literal and then this is going to be the underscore sign and this kg which is actually defining this weight that what what is it this weight in actually in kgs in grams or whatever so this is actually defining this weight so this is the suffix for that and you can see two more examples 6 to 3 and then for example we say that it stands as 10 for seconds or something like that and then we have 58 and then underscore and cent is the syntax sorry not the syntax it's a suffix actually so this is actually the syntax and these examples define how this syntax is going to be implemented in your code now what is the key benefit of a user defined literal the c++ compiler maps the user defined literates to the corresponding literal operator this literal operator has of course to be implemented by the programmer now to talk about the magic in this case let's have a look at the very first user defined literal that is right here that is 1010101 underscore b that for sure represent the binary value now the compiler maps the user defined literals to the literal operators underscore b which means a long long integer binary value now a few special rules are still missing there has to be a space between the quotation marks and the underscore with the suffix underscore b now you have the binary values in the variable bin now the compiler does not find the corresponding literal operator the compilation is going to fail now in c++ 14 an alternate syntax is used for user defined literals they differ from the c++ 11 syntax because it requires no space therefore it is possible to use reserved keyword like underscore capital C as suffix and use a user defined literal of the form 11 underscore c now the compiler will map 11 underscore c to the literal operator that is underscore c which is an unsigned long long integer now the simple rule is now that you can use suffixes starting with an appleto so finally now let's move on to def c++ and cover some examples so here is my def c++ so let me just zoom it so that it is much more visible yep seems to be good enough so now let's first solve an example that is exactly related to something like this weight on stuff so outside this main function actually paste it again as i was telling you there that this has to be defined as kg and when ratio of this and this has to be found then these two needs to be in the same unit now this is what user defined literal is going to do for me so this is actually going to be defined in the main function right up here but not like this actually we are going to do it in a pattern but first you need to actually define here that what is going to return what for example if you're talking about kilogram then what is going to return if you are talking about grams then what is going to return if you're talking about for example milligrams then what is going to return so first you have to define that values and these has to be made right above this main function so that they are global to the main function then it can be used anywhere in your code so for example the first one is long double operator and let's say we are talking about underscore kg and what it's going to return is sorry what's going to receive is a long double x and then what is going to do with that long double x is it is going to receive here it is going to actually return x steric thousand which means so whatever this value is going to be received is going to be in kgs so what it's going to do is that it's going to return and turn it into grams first so multiplying your weight in kgs by thousand it's going to return it or convert it into grams and this is what this long double operator function not lon it's going to be long so this this is what this long double operator underscore kg function is going to return it's going to return the value in grams actually but this long double x was actually in kg since as i told you that this actually defines what value is going to be received so whenever you enter a value in kg this function is going to be called this value is going to be received which means since this function is called so this value has to be in kgs and it's going to return the value after converting it into grams so moving on to now the second function that we are going to define for grams for example we say that the value that was enter was in grams so for that purpose you're also going to define a long double underscore g which means grams and it is also going to be received in long double x which means that if we specify this underscore g with some values then this function is going to be called and when this underscore g is called which means that this x is going to be equal to a value in some grams so what it's going to do is going to simply return the value of x which means it is going to do nothing with that value since what we want is that we want every value to be converted into grams before performing any computations with it so when it is in grams we don't need to do anything with it so for the final one for example we have a value long double operator and underscore mg which means that the value is now in mg grams and what is in mg grams whatever that is received here in this argument so what it's going to do is that it's going to convert it into grams and return it so how it is going to convert it you know the formula for that x divided by 1000 is going to convert that value and it is going to be returned so now after all this is done we have every kind of value converted into the unit we want we have converted every value into grams so now we can actually define our main function right down here so in the main function we can now perform any kind of computation for example we have a long double weight that is equal to 3.6 underscore kg which means that the weight is in kg second what is going to do is let me just see out this weight then what we are going to do is that we are actually going to set the precision let's just see out the precise result using the set precision function and the set precision is going to be 8 and we are going to write in weight plus 2.3 whatever the weight is it's going to add 2.3 milligrams into it and finish that function as well so next what we need to do is that we need to actually find some ratios now so for example we want first to divide something like 32.3 underscore kg and we want to divide it by 2.0 let's say gram underscore g and finally n l so now what it's going to do is that kg is going to be converted into grams this is going to be remaining the very same unit and this computation is going to be performed let's perform one more computation and let's just perform a multiplication of two values let's say the first one is 32.3 underscore milligram this time and we want to multiply it with 2.0 underscore gram and finally we are going to write in return zero so if i now just run this code you will understand what is going on in here all right the error says that set precision was not declared in scope yes we did not declare it and to declare it actually we have to include one more library that is named as io manip now this library you have to include when you want to actually call this function set precision so this set precision is actually going to limit this output right here to eight values and let me just run it and then i'll explain each and every output one by one now you can see right here my output on the screen we've got four outputs since we have four cout statements and let's just see all of them one by one now you can see that the first output is 3600 you can see right here that the weight entered was 3.6 underscore kg now when this weight was cout you know that when this underscore kg is used actually this function right above at the top is going to be called and you know that this x is now in kgs which means 3.6 kg is going to be received here what it's going to do it's going to multiply 3.6 with 1000 and return this value so when it multiplied 3.6 with 1000 and return that value that value was actually equal to 3600 that was our first output now to talk about the second output that says set precision 8 weight plus 2.3 mg now weight was actually equal to 3600 gram and this 2.3 was divided by 1000 since milligram when called return x by 1000 and x is now 2.3 it is going to be divided by 1000 is going to be added with 3600 and its precision is going to be set to 8 so if you see the output right here it is 3600.0023 so this was our second output and if you talk about this third output it was 32.3 kg which means that 32.3 was multiplied with 1000 it make it equal to i guess 3 2 3 triple 0 something like that and this 2.0 was as it is because you know that when gram is passed it is going to simply return this value as it is it was passed so this is going to be divided by 2 and the output is going to be 16150 and similarly when this last computation was performed this 32.3 was divided by 1000 as you can see at this function right here and it was multiplied by 2.0 as it is because it is in grams and we got this output that was 0.0646 so this is what is the purpose of user define literals in c++ this is how they help us to actually define our types of kg grams or something like that convert them into the values we want have one unit for every type so that operations like multiplications division and something like that can become possible so let me just give you one quick example more and then we can quit on with this tutorial so let's just remove this stuff from here and let's just remove this stuff at the top right here as well now you guys have just a basic knowledge of mathematics then you know that what is actually iota iota are actually the imaginary values now how do you have to use them in your c++ programming language to for example if you are given two values for example let's just initialize these values so then i can speak about them so for example the first complex value is a double value and it is stored in a variable name set and it is equal to 3.0 plus 4.0 underscore i which means that this is actually the iota part of that value and for example we have one more value that is also double and it is stored in a variable named y and that is equal to 2.3 plus 5.0 underscore iota so actually these are our two values in which if you talk about this z this 3 is actually the real part and this 4.0 is actually the imaginary part and if you talk about y this 2.3 is actually the real part and this 5.0 is our imaginary part now from your basic mathematics knowledge you know that the basic operations that are performed on any two such values that contain a real value and an imaginary value as well so these are addition or you can just multiply those two numbers so for example you want to multiply this z with y then how the compiler is going to interpret this 4.0 underscore iota and this 5.0 underscore iota and how it is going to add this with a real value now this is the place where user defined literals come into place and they are going to define what this iota is actually so what is this iota is actually first let us define that at the top here and then we can perform the computations so we are going to just define our imaginary literals right here literal values so let's just write in so it is going to be constant expressions the type is going to be complex double and then this following the syntax underscore i which means that this function is going to be called when ever it sees an underscore i and this is going to receive a long double value in this d part and then what is going to do with that value is it's going to return a complex double value using 0.0 comma static casting of that double value that is d so this is actually something like your lambda expressions and these lambda expressions are going to be coming in the future tutorial where i will explain what is a lambda function so for now you need to just understand that a static cast of double value d is going to be done and it is going to convert it into that static cast so let's just now perform our operations so the first operation we want to perform is let's say z plus y and then simply n del and the second operation we want to perform is let's say z static y and n l as well and finally we already have a return zero so let's just run this code yep it says complex just don't have a name we again forget to add the library for that let's just remove this library and add the complex library that deals with complex number and aota values so let's just run it now and you can see right here that it runs perfectly we have got our output the first output is actually you can see right here that we actually added two numbers that was z plus y so this is actually the output of z plus y and this is the output of z static y so in case of z plus y 3.0 was added with 2.3 you can see right here that is 5.3 and 4 and 5 imaginary values will ever added and it gives us 9 and this is actually the multiplicative inverse type multiplication so you can just sort it out using your multiplication and you can see that the answer is going to be the same because you are not much smarter than your compiler so i guess that's it so now you know the use of user defined literals what is their purpose how they are going to be implemented how these global functions are going to help us in defining the values for our literals that are user defined so i guess that's it so now these are some of the restrictions you have to follow and a user defined literals can work only with the one given in this slide but the return value can be of any time it can work with constant characters unsigned long double char constant char constant char 16 constant char 32 constant and uh these are going to be just standard libraries for that purpose this 16 this underscore t this 32 actually serving as the size so that's why it's from such standard library of size so but yes it can return any other value you have to remember that these are the values you deal work with but the return value can be of any type so i guess that's it with this tutorial in the next tutorial we will cover a second topic of some other advanced topic that is going to be not related to user defined literals so that's it with user defined literals so thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is Amir Khan and i welcome you to a new tutorial here on advanced c++ and as i told you that in this tutorial we are going to be starting on with a new topic that is placement new operator in c++ now placement new is a variation new operator in c++ normal new operator does two things it allocates memory and it constructs an object in the allocated memory now what placement new operator does that it allow us to separate the above two things that is allocating memory and constructing an object in the allocated memory in placement new we can pass a pre-allocated memory and constructs an object in the past memory so now what is the difference between the new and the placement new difference number one is that normal new allocates memory in the heap and construct objects there whereas using the placement new operator object construction can be done at a known address the second difference is that with normal new it is not known that at which address or memory location the new operator is pointing to whereas the address or memory location that placement new operator is pointing to is known because it is a memory of a known address the third difference come at the deletion the deallocation is done using delete operation one allocation is done by the new but there is no placement delete but if it is needed one can write it with the help of a destructor which we will be seeing at the end of this tutorial when we will be covering how to delete the memory or you may say the buffer so this is actually the syntax for doing or you may say using your placement new operator we have the new operator then address then the type and then the initializer as we can see that we can specify an address where we want a new object of given type to be constructed in the address part that is all done and we can just type in the address of our type now when to prefer using the placement new operator as it allow us to construct an object on memory that is already allocated it is required for optimizations as it is faster not to reallocate all the time there may be cases when it is required to reconstruct an object multiple times so placement new operator might be more efficient in those cases so let me just show you an example to illustrate the placement new operator so let's move on to our compiler and in here what we can do is that first we need to actually define a buffer because as I told you that the memory is going to be off of a choice we have to specify the address we want to where we want to store any kind of data so we have to initialize some buffer in which we want to then specify the address of that buffer in which the data is going to be stored so we are going to write an unsigned character buffer and we are going to specify the size of it as well so the size of it is going to be equal to int steric 2 which means that the size of this buffer is going to be equal to the size of int which is 4 and steric 2 means 4 into 2 8 which means that the size of the buffer is going to be equal to 8 blocks so 8 blocks in the memory are going to be just allocated right here when we write in this code so the next thing is that we need to use the placement new operator in the buffer and initialize some values so we are going to write in integer values and they are going to be equal to new then you know that we have to specify the address and the address in this case is going to be this buf that is the buffer which we created and the second thing is that we need to define in the value which we want to store so int3 is going to store in our value so now let's store one more value and let's name it as qint and that is going to equal to new buf is going to be where it is going to be stored and this size of int and int5 what it's going to do is it is going to actually add this five but first it's going to leave four spaces means for example if we have eight blocks let me just show you now for example we have actually a set of eight blocks that were initialized using this because the size of int is four one two three four and when we multiply it with two which means that one two three four blocks more has to be created so we have these eight blocks right here so when we write in in static p int equal to new buffer which means this is the buffer right here so what this buffer is going to do is in this buffer we write in new buffer int3 which means that three is going to be stored in this buffer but when we are storing the value of this five right here first what we are going to do is that we are going to specify the location and then we are going to write in where we want to actually store it so size of int means that it's going to leave four spaces one two three and these are the four spaces actually one two three four this space is going to be counted as well because we have not specified that we have filled this so we are going to leave in four spaces and then we are going to store five in right here so this is how three and five are going to be stored in the memory now the next thing is that we have to actually point in some pointers at the location we have actually stored so that later on we can just print out our this is not going to be it's going to be buffer so we can just print out our addresses in that case so it is going to be int steric and for the first one it is going to be buffer plus zero because the first is this three right here this this pbuf is going to point towards the location where this p int is stored and you know that it is stored at the very first location so this is going to be equal to buf plus zero and for the second one that is the buffer of q what we are going to do is that we are going to write an int steric and the buffer name that is buf and we are going to add in size of int which means that this second pointer is going to point for example let me just show you it here this p buffer is going to point right in here this pbuf is going to point right into this three because we have written buf plus zero which means the zero location and for the q buffer it has to leave the size of four and then point right here so q buf is going to point right here so now we can just see out our addresses so that you can just see what's going on in here so we are going to write in c out let's just see out the buffer address first buffer address for let's say first we want to the buffer address of the p buffer so it is going to be p buf and let's unline it and secondly we have to just print out the integer so we are going to write in that p integer is whatever was stored in this pint and secondly we have to specify the q buffer as well so we are going to write in c out we are going to just specify p with it as well p buffer address and then we are going to write in q buffer address and in this case the q buf is going to be printed out finally we can just print out the integer as well so q integer is q int let's just add a line right here to distinguish that what we are talking about so let's just put a dot dot dot dot dot here so that these two can be just separated from each other so now since you know that we have initialized this three and five for the pointer so what this pint is going to give us it's not going to give us the integer it's going to give us the p integer address so we are going to write in p integer address here and it is also going to be q integer addresses and finally let's just put a line again and now we can just print out both the integers so c out first integer and this is going to be printed out using the steric and if you have the concept of pointers then you know that what is going on in here because when we have to just write in the and sign so that we are pointing towards the address and when we are writing in this steric then what do we mean and when something is initialized with a pointer then removing this steric from this pint and see printing it out on the console screen what is going to print and just writing in the steric and then printing out this pbuf, qbuf or pint or qint and what is going to print so this is all from the knowledge of your pointers topics so you need to go through them first if you don't get the concept of pointers because if you don't get the concept of pointers you're not going to understand what's going on in here so we are going to print out the second integer as well so the second integer is steric, qbuf and as you know that this p buffer and q buffer are actually pointing towards this integer 3 and this integer 5 respectively so when we write in steric buf pbuf since this is pointing towards 3 it is going to print out 3 and since this qbuf is pointing towards 5 so this steric qbuf is going to print out 5 for us so let's just run this code and i hope it ran perfectly yes it does so you can see right here that it says that pbuf address is right here is p integer address is this which as you can see is the very same because the buffer address and the integer address are both pointing towards the same location then the q buffer address is this the q integer address is this because q integer address and p buffer address are going to be the same because q integer is stored in the q buffer and then the first integer is 3 and the second integer is 5 so i hope it is clear let me just explain it to you with the help of a diagram which i added so in this diagram actually the example i just covered is explained so you can see that this is actually the buffer vision initialized using the size of int which is 4 multiplied by 2 which means that you can see here that 4 plus 4 these 8 blocks have been initialized for this buffer so now the first integer was 3 the second integer was 5 now we initialized p buffer to the 0th location and then this p buffer sorry this is going to be the q buffer i just i guess misprinted it is going to be the q buffer the q buffer is going to be pointing at plus 4 location which means that the size of int location is going to be skipped which means four locations are going to be skipped and the q buffer is going to point right in here the first integer is going to be stored right in here the second integer is going to be stored right in here the p buffer along with a steric sign when c out is going to generate 3 the q buffer with a steric sign when c out is going to generate a 5 the first integer pointer is going to generate an address for this integer. The second pointer in which this integer is stored is going to generate this address for the second integer and then simply writing in p-buffer is going to generate this address and simply writing in q-buffer is going to generate this address. Moreover, the second integer address and this q-buffer address is going to be the very same since they are pointing at the same location whereas the q-buffer sorry this p-buffer address and the first integer address is also going to be the same right in here as for second integer at the cube buffer. So I guess that is clear. So let me just cover one more example. So we'll move on to defc++ and we will just remove the stop from here. So let's just remove it from here. And now in this tutorial what we are going to do is that we are going to initialize a value for a variable and then using the new operator what we are going to do is that we are going to change the value of x and then you are going to see the impact that what happens before using the placement of new operator and what happens after using the placement new operator. So just before what we are going to do is that we are going to write in a variable named as x and we are going to make it equal to 10. Now the next step is that we are going to actually see out before using placement new operator and L and then what we are going to do is that we are going to see out the variable. We are going to write in the variable is x at this time and we are going to see out the address of that as well. So we are going to write in the address is and operator and this x right here and an and L. So now we are going to use the placement new operator. So what it's going to do is that let's initialize a new variable. Let's name it since we are talking about integer because x is an integer. So we are going to initialize it to integer and then we are going to store it in a variable let's say y. So this is going to equal to new. The first thing we need to do is that we need to give this new variable allocation. So what in this tutorial I want I want that this value should be overridden because I'm going to pass in the address where this x is stored. So we know that this x is stored at this location and x. So what we are going to do is that we are going to pass in and x as the location and then we are going to write in the value which we want to store at that location. So for example 100 is the value we want to store at that location. So now we are going to see out after using placement new operator and L. Let's just place one more space here. So now what we are going to do is that we are going to see out the variable s which is now going to be 100 in this case more or we are going to see out this y actually. So we are going to write in memory address is and L and thirdly what we are going to do is that we are also going to see out x address as well. So we can write in x address is not just and L it's going to be and x and then an end out. Now what is this going to specify here is yes we need to add address here as well for y. Yes we need to add x here as well. Yeah now perfectly fine and a column right here. Now what is this these two statements actually going to specify when these two addresses are going to be equal which means that the value has been actually overridden. Let me just run it for you. You can see that before using the placement new operator the variable is 10 and the address is something like e34 and after using the placement new operator the variable is now 100 but the address is the very same because we have placed this 100 at the location of the x and you can see that the x address is also the very same. Now what happened actually here is that it is now clear that the new value of x is assigned at the address of x with the help of placement new operator. This is clear by the fact that the value of this x which is the x address and the memory address are both equal. Now let me show you this with the help of a diagram finally you can see right here. You can see that this was step number one in which we initialize the value of x equal to 10 and this was an x that was fe8 address can be anything. So then what happened that in the step number two we initialized a new variable y or you may say mem or any variable you want to initialize it and we initialize it the address of x which means that the new value that is 100 is going to be stored at the location of x at which 10 is already stored. So what it's going to do is it is going to remove 10 from there store itself right there. So when this x was printed out it printed out 100 the address of x as well as the address of this mem or y variable was also equal which shows that the value has been overridden successfully. So that's how placement new operator is actually used in C++. Now moving towards the new thing of the placement new operator is how we can delete the allocated memory in this. So the operator delete can only delete the storage located in the heap. So when the placement new is used, delete operator cannot be used to delete the storage. In the case of memory allocation using placement new operator since it is created in the stack the compiler knows when to delete it and it will handle the deallocation of the memory automatically. If required one can write it with the help of a destructor and how it is done with the help of a destructor let me show you this with the help of an example. So let me just move on to my editor and just remove this stuff from here. So here the first thing we need to do before doing anything is that we need to declare a buffer and we have to give it a size let's say 100 is the size of that buffer. Now before doing anything in this main function what we have to do is that we have to make actually classes in this case because we are going to do this with the help of the constructor and destructor function and why we are using destructor I've already told you because we cannot use the delete operator directly for the placement new operator. So we will define a class named let's say deletion. This class is going to do a lot but in this case we are going to make it as deletion because the main purpose of using of solving this example is just to show you the deletion operation. So we are going to have private members and in the private members we are going to have double RE underscore and I am at a score. So these are the two variables we are going to have and in the public area we are going to have the constructor as well as the destructor. So first thing is going to be the constructor that is going to be named as deletion and it is going to contain some arguments that are going to the values of the values which we write initialized right here. So it is going to be double RE equals to zero and double IM equals to zero. Moreover we are also going to write in IM underscore IM. So these are going to be something like these are going to be something like default values and these are going to be the past in values. So this is going to be the body of the constructor function. Now in the body of this constructor function what we are going to do is that we are going to simply print out the values. So we are going to write in RE this and similarly we are going to print out the other values as well. So now we will define our destructor function. So right after this here we are going to define our destructor function and you know that a destructor function has a negation sign at the start like this and then we have to mention the name of the class that we are going to use that is the deletion class and then this is our body of the destructor and in this destructor what we are going to do is that we are going to see out the values again. So you will understand that by in both the constructor and destructor we have just printed out these values. So down here after the destructor function what we are going to do is that we are going to return the square root of these numbers. So what we are going to write in that we are going to define a function as double. This is going to be the name of the function and this function we are going to return the square root and you know the square root is going to be something like RE steric RE added with IM steric IM and then we will have a print function and in this print function what we are going to print out that we are going to print out the value of RE and the value of IM. So these are going to be our end values that are going to be printed out at the very end. So now moving towards the main function code we have already initialized our buffer let's just remove these spaces from here. All right good enough. So now in this main function what we are going to do is that we have already initialized our buffer on stack. Now we are going to initialize objects and define the values for actually these two variables that are the RE variable and the IM variable. So what we are going to write and we are going to write in deletion and we are going to make them as pointers since in a case of this because we are pointing towards the location of the buffer. So we want the values which we print out. So let me just initialize those values first and let's say the values are 4.2 and 5.3. So now why I have used this pointer because I want these values to point at a certain location in this buffer. So that's why since they are pointing towards a specific address in this buffer so that's why this has been defined as a pointer this object has been defined as a pointer. So whatever the values are they are going to point towards a specific location in the buffer. So that's why they are objects. So let's make one more object and it is going to equal to new deletion and now it's time finally to use over object sorry the placement new operator and placement new operator is going to be used like the name of the object is going to be PE and then we are going to write in new and then we are going to specify the location at which we want to store our object and then finally we are going to call in this 2.6 comma 3.9 because these are going to be the second values that are going to be passed in. So finally we are now going to be using the objects so we are going to write in pc print then we are going to write in pd 0.print then pd 1.print then we are going to write in pe print. Now as you can see right here that the second object that was the deletion pd equals to new deletion 2 this means that these are going to be locations from 0, 1 and 2 so that's why we have written as 0 and 1 right in here we have not just used the second location which we are going to not use because this means two locations and pd 0 and pd 1 are those two locations. So now we have used these objects to call the print function so now we are going to release our objects and call the destructor and then release the memory so we are going to write in delet PC which is this object right here so after releasing the objects by calling the destructor function and then releasing the memory now we will call the destructor for object pd 0 and then we are going to release the memory and we are going to do the very same for pd 1 so we are going to write in delete not in this line let's make it in the new line so we are going to write in delete pd now we cannot make an explicit call to the destructor so we are going to write in pe deletion so if i just now run this code first let me just remove these spaces now run this code all right let's say sqrt was not declared in the scope yes we need to initialize the libraries for that right here and the library for that is going to be equal to hash include cmath and this is going to just solve all the problems so let's now run this code and you can see that it ran perfectly let me just scroll it down so that you can see that what is actually printed out so for the first call you know that when the object was initialized right at the top here the constructor function was called and 4.2 and 5.3 were passed in then for this 00 and this 00 because pd 0 and pd 1 were called and you can see that no values were passed in you can see right here the constructors are called two times for first for for deletion zero and then for deletion one but no values were passed in so as you can see right here that when no values are passed in the re and im are initialized to the values that are default values that we have defined right here as 00 so for pd 0 and pd 1 that these four zeros are present the first zero is for re then for im then again for re then for im these two zeros are for pd 0 and these are for pd 1 so then after that the deletion pe using the new buffer was called in and the constructor was also called in so this 2.6 and 3.9 were called in and then when pc print was called in these 5.2 and 5.3 4.2 and 5.3 sorry were called in and then again for pd 0 and pd 1 these four zeros were printed out and then when pe print was called in this 2.6 and 3.9 were called in and then these are the values down right here that are called in for this deletion function actually deletion PC is going to generate these four values and then deletion PD is going to generate these sorry deletion PC is going to generate these two values deletion PD is going to generate these four values because deletion PD contains pd 0 and PD 1 so PD 0 has these two values then pd1 has these two values and then pe and calling the destructor function deletes 2.6 and 3.9 from the scenes. So to give you a summary of what's going on in here you have clearly seen that here the destructor is explicitly called because here it cannot be packaged within the delete operator because delete will need to release the memory which you do not have here and it cannot be implicit as it is a dynamic process which we want to manage ourselves. So this is how deletion is done using the placement new operator in C++ so let's move ahead. So the next thing is what is segmentation fault. Now though the placement new operator should be used with care the address which is passed can be a reference or a pointer pointing to a valid memory location it may show error when the address passed is a pointer such as a null pointer a pointer that is not pointing to an indication and it cannot be a void pointer unless it points to some location. So let me give you a simple example of a C++ program to illustrate segmentation fault while using the placement new operator. So let's move on to def C++ and let's just remove all this stuff from the main function as well as this class right here. As well as we don't need cmath as well now so let's just remove it as well. So now what we are going to do is that we are going to define a pointer sorry not a pointer variable that is going to equal to 10 and then what we want is that we want a pointer let's name it as IPT and it is going to contain the address of whatever this value I stored in this is going to print out the address for us this pointer is actually pointing towards the location for example if you print out this it is going to print out the location where I store so this is the pointer that is pointing to that location where this 10 is stored at this time so we are going to define one more pointer let's name it as I1 and this is where the new operator is going to be used sorry not the new operator the placement new operator so we are going to write in new then we are going to write in IPT and we are going to write in int9 which means that we are overwriting this 10 because IPT is pointing at the location where this I store so when we write in IPT which means that the pointer that the address at which this 10 is stored at that address we now want to store 9 so if we now talk about this this is actually correct so we are going to write in correct here because this was a variable I that stores 10 and we initialize a pointer that pointed towards the location of where this I store and then we initialize one more pointer and using that pointer we used actually the placement new operator in which in the location part we specified that pointer which was pointing where this I was stored so now at this time this 9 is going to actually replace this 10 so this is a completely fine practice but now to talk about the incorrect method that is going to generate a segmentation fault for us what is that going to be it's going to be incorrect as the IP may not be a valid address for example if we write in int IP and then I write in int second pointer I2 and I'll make it as new IP and int4 so now as you can see that in this case this IPT was pointing at a location but now this is simply a pointer pointing to no location if you write in something like here and of I then it is going to be perfectly fine but in this case when this is not here and a colon is placed right here this means that this is a simple pointer not a location this is a pointing device but it's not pointing to some location as I told you before that the errors can be generated if a pointer is a null pointer a pointer is not pointing to any location and it can be avoid pointer until and unless it points to some location so in this case we have this pointer IP but it may not be a valid address because it is a pointing device but it is not pointing to some location as in this case this IPT was pointing at a certain location where this 10 is stored so using this IP as a location to store this for is going to generate a segmentation fault so if I just run this code you can see that it ran does not generate it any error but if you can just see you something right here something interesting but print it out let's say IPT in this case since it is used as a location and then I print out c out IP since it is also the location not a location in this case is a pointer it is not a location as I told you it is since this is incorrect if I just run it you can see the difference you can see right here that this IPT which was pointing at I location that address has been printed out but secondly you can see that nothing has been printed because there is no such location so this is an incorrect method in this case so now to talk about the second fault that can be here is right here so the correct method and then we are going to write in incorrect method all right to talk about the correct method let's just suppose that we have white pointer let's say VD and it is equal to end of I which means this very I in which then is stored and then we write in int steric I3 equals to new VD the pointer and we store in a value let's say 12 so this is completely okay since this is a white pointer but it is pointing to a location so this I3 is going to store this 12 sorry this I3 is going to point at the location where this 12 store and this is going to this VD is going to be the location in this case so if we just see out this VD and write in end L and run this code this was the address problem for previous part and this was not generated because of I guess this let's just remove it from here now run it you can see right here that that address has been printed out so now to talk about the incorrect method let's suppose that we have a variable let's say int a and we write in int the fifth pointer and we say that it is equal to new this a and we want to store a value let's say 6 so now it is not a valid address as well since this a is actually nothing it's simply a variable that also need a memory so we cannot write in something like this if we use it something like this when int i equals to 10 and then we write it in steric IPT we need a pointer that pointed the location of this then we can use this for example I'm talking that if we write in in steric let's say z equals to and of a and then we use not this a again this pointer actually we write in the Z here then it is going to be perfectly all right but in this case since we have this scenario this is completely wrong so if we just print out c out a since we are defining it as a location you can see that it is straight away generated and error so not allowed so let's just just remove this incorrect method from here as well so this is what segmentation fault is these are the two correct methods and if I just control that this and this is the second incorrect method so I guess that is clear so that was how segmentation fault can occur in your code so now moving towards the end slides now what are the advantages and this what disadvantages of using the placement new operator now the first is that the address of memory allocation is known beforehand which means that this is quite a big advantage of the placement new operator now the placement new operator is useful when when building a memory pool a garbage collector or simply when performance and exception safety are paramount which is a king counted as an advantage of the placement new operator moreover there is no danger of allocation failure since the memory has already been allocated and constructing an object on a pre allocated memory buffer takes very less times which means not only this is time efficient but moreover the allocation failure in the memory does not occur which is which are both the advantages of using the placement new operator and moreover this feature becomes useful by working in an environment with limited sources so these are very handy advantages I guess I discussed four advantages or five advantages and these are all the advantages of using the placement new operator over the new operator because these are all the disadvantages in new operator because the memory is not known beforehand we do not have a memory pool we have a danger of allocation memory since the allocation memory has not been yet allocated and the allocation on the memory that is not allocated takes a lot of time and this feature is not useful when we have limited resources because the new operator can not be used when we have limited resources so using the placement new operator these five advantages come into hand so I guess you know now what is the purpose of placement new operator how to create one how to use one how to define memory how to delete one what is segmentation fall and what are the advantages of placement new operator over the new operator so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial where we will be discussing about a new dimension of the topic so bye bye till then hey guys what's up this is Umar Han and I welcome you to a very new topic here in this section of one C plus plus and in this tutorial we are going to be studying the copy and swap idiom now before studying the copy and swap idiom what we are going to do is that we are going to study about the normal overloaded assignment operator and then we are going to study the drawbacks that occur in the overloaded assignment operator and then we are going to see that how these drawbacks are going to be solved by the copy and swap idiom so let me just show you an example of the use of the normal assignment overloaded assignment operator and you can see right here that we have an example already solved and in this example actually we are using the normal overloaded assignment operator so in this tutorial in this example as you can see right here that we have this two integer one is an integer value and one is an integer pointer and then we have these public constructors this is the first constructor that is actually the initializer list in which we have made s equal to zero size as then the ptr size is going to equal to the new in size and that's points corresponds towards the null pointer then we have this which is the copy constructor in this case and it is going to equal to an object of the constructor class and again the ptr size is going to equal to the new in size and it's going to correspond to a null pointer and in this constructor function that is actually the copy constructor function this over here is the construct copy constructor for constructor function so in this copy constructor what we are going to do is that we are going to call in the memory move function now what is the purpose of the memory move function the memory move function in c plus plus is actually used to copy a specified byte of data from the source to the destination now to talk about the parameter is it has three parameters actually the first one is the destination that is the pointer to the memory location where the content are copied to then we have the second one that is the source and it is from with that pointer to the memory location where the content are copied from and then we have the third argument that is the count where we see the number of bytes to copy from the source to the destination so you can see right here that this is actually the destination this is actually the source and finally this size steric size of int is the number of bytes that are going to be copied from this source where this ptr is pointing towards this destination where this object dot ptr is pointed which is right here so then we have this xn operator in which actually our main job is done so what is our main job in this case is that we are to actually call the overloaded assignment operator and that's where our overloaded assignment operator is being used so in this case this is the xn operator that is equal to the constant xn object which means that operator address operator is equal to the address of the object whereas the object is pointing where our data is actually copied so what we are going to check is that if this and what is this you already know what is this from our basic from your basic concepts that this is actually going to be the pointer that is actually the recent pointer so we say that if this is not equal to the location where this new memory is copied in so what we are going to do is that we are going to delete in the pointer the size is going to be equal to the object size the ptr is going to be size equal to new and size and it is going to correspond to another pointer the memory move function is going to be called and the ptr is going to be the source object dot ptr is going to be the destination and the size steric size of int is going to be the number of bytes that are going to be copied and what it is going to do is that it's going to simply return this and this is then going to be our destructor function if you want to use it so we just run this code you can see that it just ran perfectly now before going ahead you can see that the above assignment operator does two things the first one is that it does the self assignment check and where is this this is actually the self assignment check in which it checks that if the recent pointer is not equal to the memory in which the contents are to be copied so the second thing is going to check is if their assignment is not to self then it do three things it deallocates the memory assigned to the pointer it allocates new memory to the pointer and copying the values and finally it return the recent object that is this object that points to the very current location so now to talk about the drawbacks of the above approach the first drawback is the self assignment check as i told you the self allocation is done very rarely so the self assignment check is not relevant in most of the scenarios this just slows down our code the second drawback of the above approach is the memory deallocation and allocation as it can be seen that first the memory is deallocated leaving the pointer dangling and then the new chunk of memory is allocated now if due to some reason the memory is not allocated and an exception is thrown then the this pointer will be left a dangling pointer to a deallocated memory this scenario should be either the assignment is successful or the object should not be altered at all now this is where the job of the copy and swap idiom comes into account this approach allegedly solves the above issues and also provide a scope for the code reusability now let us see how this is done so we will just modify the code we already have here and our def c++ so let's just scroll above and this job is going to be the very same because this is the initializer function and then this is our function in which actually our constructor function actually in which our constructor's jobs are done because this is the copy constructor constructor in which our copying is done so this is going to be the same so right after this we are going to be using the swap function so the swap function looks something like right wide swap and why I've added this swap I will just explain in a moment and then the swap function takes in two objects actually and it takes in actually the pointers towards that object and it stores them here in as addresses so these addresses are then going to be used to swap the object sizes as well as the object pointers and when you swap the object sizes and the object pointers then this is going to actually swap your values so we are going to write an x and object one so this is object one and then we have x and object two now using these two objects what we are going to do is that we are going to swap object one size with object two size call in after this and what we are going to do next is that we are going to swap the pointers as well so we are going to write an object one dot ptr with object two dot ptr good enough so let's just place a call in here and now in the overloaded assignment operator the argument passed by values will call the copy constructor so right in here in this x and operator equals to const x and j we are going to just remove this set of statements and this is actually going to call our friend function so but before calling this friend function we need to just remove it and modify it as x object because we no no more need a memory we just need this obj that is actually printing out our memory part so if we just call our swap function because this is going to call our friend function and this is going to call it using the current object wherever where our pointer recent pointer is actually pointing at so this that that is going to be the memory where we are going to start from and we are going to copy this with obj at the location that is the destination location actually where we want to copy our memory and finally we are going to return this so that's i guess perfectly fine now and then we have this constructor function as usual so i guess that's cool enough so let me just run it now so it ran perfectly so now the memories have been swapped actually and that's where our copy and constructor function comes into hand now in the above example the parameter to the object are passed by values which calls the copy constructor to create the object local to the operator of the x-class and then the value of the temp object is swapped with the this object that is the left hand side of the assignment operator call now what are the advantages of using the copy and swap idiom the first advantage is that no more self assignment check is needed as the parameter is passed by value this means that no more memory memory deallocation is needed moreover a self allocation is very rare so the overhead of copying in case of self assignment should not be a problem anymore the second advantage is that now as copy constructor is used to create the temp object therefore the swapping will only be done if the temp object is at all created basically what we were doing manually there the compiler is going to do it for us in here the third one is the code reusability as i told you as we can see the operator does not have much code in its body rather we are using the copy constructor and swap function to do the job as if i'll just show you again you can see right here that we are again and again calling this swap function and we are actually using the code again and again which is actually a case of code reusability so i guess that's it with copy and swap idiom now you know that what are the advantages of using copy and swap idiom over a normal overloaded assignment operator so that's it with this tutorial in the next tutorial we are going to be starting a very new topic as in this section we are in every tutorial covering a new topic so in the next tutorial you will be seeing a new topic so that's it with copy and swap idiom thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is umar han and i welcome you to another tutorial in this section on advanced c++ so in this tutorial we are going to be covering a new topic that is lambda expressions now lambda expressions in c++ were introduced in c++ 11 to allow us to write an inline function which can be used for short snippets of code that are not going to be reused and not worth naming in its simplest form lambda expressions can be expressed as shown on to you in the syntax on your screens you can see that it has a capture clause then the parameters then you to specify the return type and finally you have to define the definition of the method generally the return type in lambda functions are evaluated by the compiler itself and we don't need to specify that explicitly and the return type part can be ignored but in some complex case as in conditional statements compiler can't make out the return type and they need to specify that now various uses of lambda expressions with standard functions can be explained with the help of an example so let's just move on to a compiler here we are so the example i'm about to solve is going to be containing several paths the first part is that we are going to initialize a vector and then we are going to simply print it out using the lambda expressions secondly we will select a specific number from that vector and we will see the first occurrence of that number when it is greater than some number then we are going to sort that vector as well using lambda expressions then we are going to select a specific number and see that from how many number that number is greater the next we are going to just remove that redundant elements from that vector again then we are going to be calculating the factorial of a number using lambda expressions and finally we will find the square of a number using the lambda expression so it is going to be a very comprehensive example in which you will see all the uses of lambda expressions and i've covered all these scenarios because lambda expression is a very very very important topic so the first thing we need to do is that we need to initialize a vector it is going to be an integer vector whose name is going to be v and it is going to be equal to a set of numbers let's say four one three five two three one seven four i guess that's enough so this is going to be a vector in and this vector we are going to first print it out so the first thing we need to do is that we need to define a function here that is going to simply print out all the elements in that vector using the lambda expression for sure so the vector is going to be print vector and it's going to be a white function and in the argument it is actually going to receive that vector that has the name v and then what we are going to do is that we are going to use now lambda expression to print out the elements of that vector so we are going to use in for each loop for that purpose and then we are going to write in v dot begin v dot end and these are the expressions that were always used and finally int i so now in the body of that what we are going to do is that we are going to simply write in c out i so it is going to actually print out the elements for us so what actually is going on in here in this lambda expression is that we have actually used for each loop and in that for each loop we have actually written v dot begin which you know that it is going to actually grab the beginning of the vector which we have passed in right here and print start from the very first element v dot end is going to iterate it to the till the end loop and then int i in int i every time it is going to contain only one element so for this for each loop when it begins when this v dot begin is called it store the very first element that is for right in here and it come into this loop print out this for for us went back check for the next element which was not we don't end since this is the ending criteria for this for each loop so the next element that was stored in this int i would be this one then three then five and two and so on till four when this v dot end which is going to be this last element is going to stop this and the loop is going to stop so let's just run this code all right i guess a vector was not declared in the scope we have to include i guess library for that purpose that is hash include this is the library for that and one more thing we need to do right here is that we have to write an std so i guess that's good enough it's going to now run perfectly all right we have one more that says expected this yep i guess yes this this bracket don't need to be put here because for the for each loop this bracket has to be put after this body and a column right here this this is actually the ending bracket for this this ending bracket right here is for this int i and this is right for this this is how lambda expressions work on so i guess that's good enough so let's just run this code now and i think it's going to work perfectly fine now and yes it ran perfectly all we need to now do is that in the main function we have to just call this print function so that it just generates some output for us so we are going to write in print vector we need to be in small and what we need to do is that we need to pass in this vector so the name of this vector is right here we we pass it to this function print vector and now this vector and we has actually this vector which we have defined right in here so if you now run this code it is going to just generate an output for us as well so here is my output and as you can see that i have got all the elements that are four one three up till this four so this is how elements of a simple vector that are initialized in the sequence and then printed out in the sequence as well so let's just remove this end i from here add space here and i guess that's going to be much more better yeah that seems to be much more better way of presenting our output and just put a one line space here for our second output to find the occurrence of a first element greater than a specific element for example if we just take in this number four then what we are going to see is that we are going to find the first element that is actually greater than this four in this case it is actually five so our output is going to be something like the number that is greater than this four in this case it is going to be five so now let's just code this so in the main function what we are going to do is that we are going to be using the vector and using that what we are going to do is that we are going to be using an iterator p in which we are going to actually store our element and what we are going to do is that we are going to use in find if and the same conditions we don't begin and we are going to keep iterating until the very end so we are going to write in v and because we want to search for each and every element we want to check our first element for which we want to find a greater number so we want this four to be checked with each and every number so we're going to start from v dot begin and then i trade till v dot and and what we are going to search for is the number so that number is going to be stored in this int i and what we are going to simply do is that we are going to write in i is greater than four is going to be returned simple enough and here is where we are going to put in colon we just put a tap space so that it looks much more like the body path so this iterator p is going to store the value because this find if is going to start from v dot begin it's going to iterate till v dot end but since we have written in the body that return i is greater than four and i is in this case is going to be the value we want to check so the value we want to check since i for the first case is four so i is going to be four so it's going to start looking for this for any element that is greater than four so at the first case this four was greater than sorry this five was greater than this four so at the very first instant when it found this it returned it so the rest of this iterator loop was not executed and the first occurrences of any limit greater than this was printed out so in here we are going to simply see out first number greater than four is steric p and now you might be thinking that why i've not called this find if function because this is how lambda expressions work iterator p actually with this variable when i call in like this what it's going to do is that it's going to execute anything with this now what is the purpose of this find if is it a building function or is it not yes it is it is a building function and what it does is that it is going to search for an element for which any argument which we passed in becomes true since in this case the third argument that this is the zero this is the first this is the second and this is the third argument the third argument returned true so this loop is going to stop so what it's going to print is is going to print that number at which the condition becomes true so in this case the condition becomes true at this five right here so this p is actually going to point at this location so when i write in steric p it's going to print me out that number so if i just run this code yes i've messed the body brackets and this needs to be right in here all right good enough now let's just place it right over here as well so it's going to now work perfectly so you can see right here this is our first output that was printing out the simple vector and then this is my second output that says the first number greater than four is five so now towards the next part and in the next part what we are going to do is that we are simply going to sort this vector which we have right up here so we are going to sort it up starting from the greatest number and then moving on towards the smallest number so it's going to be sorted something like seven five then four then three and then two then one something like that so let's just code it as well so we will just code it right behind this so what we are actually going to now do is that we are going to make a function to sort the vector using lambda expression and lambda expression is for actually sorting in the non-decreasing order compiler can make out the return type as bool but we can just make it work our own way so how it is going to be done it's going to be done using sort function that is also build-in function and what it's going to do is it is going to start from the very start that is v dot begin and it is going to iterate till v dot end and then these are the bracket for the lambda expressions and then in this bracket this time what we are going to do is that we are going to write in constant int and a and constant int and b and they are going to correspond to a bool value now what is actually going on in here let me just code the body first and then i'll tell you what is going on in here so right before behind this what we are going to write in we are going to return a is greater than b and then this column right here now right above here what i just told you that whenever this condition is true then what it's going to do is that it's going to stop right in here and it's going to return that number for me so in this case right down here this sort function is going to actually sort starting from the very first element till the very last element and now what is the purpose of these two constant integer addresses actually here now these are actually going to store the two values and then if this condition is true which is return a is greater than b since in this case we want the elements to be sorted in the form of decreasing order so if this bool is actually true so what it's going to do is that which means that this is actually going to be true so when this is true this bool is going to be equal to true and the two values are going to be stored right in here and those values are then going to be sorted out using this function when these are not for example some values are always there that don't need to be sorted out for example in this case let's just see this case right here these four and one don't need to be sorted because four is already four and all already in the form we want them to be on the output screen there need to be elements in between them but for this case for the first comparison this condition is actually false which means that a is greater than b is false because a at this time is four whereas b at this time is one so this condition is false so this bool is false so we don't need to store some values in here and perform the sorting function values are stored in there four is stored in there one is stored in there so that's why they check in using this return a is greater than b but the sort function don't work for such values which are always in sorted form so for values for example three and five or you may say one and three such values need to be sorted out so in that case one is going to be stored in int a three is going to be stored in constant int and b and these are going to be sorted out using this sort function so when this function actually complete from v dot begin till v dot and then we already have our array sorted out all we need to do is that we need to print it out so how to print it out we have to actually call simply the print vector function and pass in our vector right from here a column right in here and let's just see out as well sorted vector is this print vector v and finally and l so now if i just run this code all right it says some error and this it says no match for greater reference here yep a very silly mistake we can just see out this print vector v because we are not actually returning something from up there so let's just remove this from here or let's just cut it out and we have simply called in this function and right above here we can just write in this statement sorted vector is and then this print vector v is going to simply print out each and everything for us we don't have to specify c out with a function here that's just silly of me so let's just run it now as you can see right here that we have the sorted vector right here seven five four four three three two one one so you can see that we have this simple vector then we sorted out this using the lambda expression and finally we sorted out our vector using the lambda expressions so now moving towards the next spot and in the next spot what we're going to see is that we are going to actually count the numbers that are greater than or equal to a specific number so how it is going to be done let's just call it so right down here we are going to initialize the variable name its count because what we want is that we want a count of a number that that is greater than or equal to some specific numbers we want that how many numbers are greater than a specific number so that are going to be stored in this count so we are going to write in count underscore if and then we are going to write in v dot begin then v dot end then this and finally we are going to write an int a that is actually going to store the number for us and then this is going to be the body and in this body what we are going to do is that we are going to return a is greater than or equal to let's say five so the number that we want to check is actually the five so write this call in here and write this call in here so one more thing before i run this code that i need to see out the number of elements greater than or equal to five are actually whatever the values of this count variable now let me just you one more thing before i run this code because the coding is complete just i want to tell you one more thing here now you can be seeing that this count if function this sort function then this find if function we are actually and then finally this function also as well this print vector function all of these are lambda functions so as you can see right here that when i have to use this print vector function i have to actually call it as you can see that i have called it twice once when i was printing simple the vector and secondly right down here when i was actually printing the sorted vector so and i don't call these functions right in here that that sort function the count if and then the find if function right above here now what is the reason for this because as you can see that this is the main function these functions that at the point of function this sort function and then right at the bottom the content function are actually in the sequence of this main function so these don't need to be called they are called by themselves that's why i'm always calling them as build-in function these are not something like building the library of the c++ they are not because these are actually lambda expressions they are called in by themselves they are executed by themselves that's why they are building they are not building the sense that they are something like store in the background of our c++ libraries like we have this int stored we have this return value that is something like a build-in value that is stored in our c++ libraries and variables or something like that this is something not like that completely not like that because this find it is a normal function but since it is a lambda expression right here so this don't need to be called when the sequence of execution comes here and it sees that it is actually talking about a lambda expressions so it execute the body of it as itself it don't need to call it but why do we call this function is because this is out of the body of the main function so when we have to use is the sequence of execution don't come here at any stage but it can come here at the stages when we call it so when we call it twice the sequence of execution moves right here and this function is called but if this function was right in between these in the main function we would not have the need to call this function but if we have used it since you know that we have called this function twice so if i just copy it from here and paste it instead of this and also this right here then this is not actually a good programming technique because in c++ or any other programming language code reusability is something so we have to reuse of a code so that's why we have written this set of statements which we want to execute again and again it is a lambda expression i'm not saying that it is not a lambda expression it is a lambda expression but they are written right at the top here so that we only have the need to call this function not repeat this entire set of code because that this would not be then a good programming technique so i guess uh now i need to run it and you can see right here that the number of elements greater than five or equal to five are two as you can see in this that is the first number and that is the second number these two elements are actually greater than or equal to five this seven is greater than five and this five is equal to five no other number is greater than or equal to five so this count was equal to two so the next thing in this tutorial we are going to do is that we are going to make a function for removing duplicates from this very list available that is in the sorted form now so what we are going to do is we are going to do it right here so we are going to write in p equals to unique and we are going to then again write in v dot beggin we are going to write in v dot end and then this is the sign for the lambda expressions and then again since in this we are also talking about comparisons so we need two values that is int a and int b and then we have this body right here and but we are going to do is that we are going to return a equal equals to b and a column right in here so i guess that's good enough let's just run it now but before i run it this actually just remove over elements so all we need to do is that we need to call our print vector as well now we have to pass in the same v and let's just print out a cout statement at the top here as well that says unique elements in the list are right at inhale because since this function it's going to start from the very start it's going to move at the very end and it is going to return the values that are actually equal so when i just run it it's going to print out the unique elements for me so you can see right here that it says that the unique elements in the list are seven five four three two one and then the repeated elements as you can see are put right at the bottom you can see that it first printed out the unique elements for me that at seven five four three two one and after that as you can see if you consider this sequence it has a four four then a three three and then a two one one but in here it looks for this element it has removed this four from here it has removed this three from here and it has moved it up till here and then finally it has actually concatenated this two one one right in here so good enough so the next part is how to find the factorial of a number now how to find the factorial of a number actually we have to store all the values into an array first for example if you want to find the factorial of ten then you need an array that stores elements from one to ten because you can actually not i trade this loop again and again so for that what we need is that we need an array that contains element from one to ten good enough now what we need to do is that we need to actually store it in a value as well so it is going to be equal to let's say the name of the function is tutorial or let's just call effect and it is going to take in the array from one to ten and it is going to start from the very first value and these are the new brackets for the lambda expression and finally we are going to actually store two values and i and nj and i and nj for the very first case is going to store one and two and what it's going to do with one and two is that it's going to simply return their product so it's going to multiply them and the next time this three is taken into account it is going to multiply this product with this three and at the end our factorial is going to be found so we can just see out factorial of ten is so let's just run this code all right it says that fact was not declared in the scope all right yes the name of this function has to be accumulate because these are some of the names that we have to use for lambda expressions these names right in here we will begin we don't end then this name of the function name count if these are actually something that we have to use in again and again so then finally we are going to put i guess nothing else let's just run this code and it says the factorial of ten is something very big value yes it is so you can just find it using your calculator as well and the final thing i told you was how to find actually the square of a number so for that i'm going to write an auto square square and i guess we don't need any spaces in between this and it is going to equal to and i and this int i is actually going to be the value which we are going to pass in ourselves and for the body of this what is going to do is that it's going to return i steric i which means that it's going to multiply it with itself which is actually the square because square of the number is multiplication of the number by itself so down here we are going to write in c out square of let's say we want to find the square of six is we will write the name as square and we're going to pass in six and i guess we need a space here as well so now let's just run it all right this don't need to be here now let's just run it and you can see it right here that the square of six is 36 which is perfectly correct now this auto here is actually going to automate the square whenever it is called so this i was paused in which was six so it returned six cross six that was 36 which was printed out using this c out statement now a lambda expression can have more power than an ordinary function by having access to variables from the enclosing scope we can capture external variables from enclosing scope using three ways the first one is by using capture by reference then capture by value and then capture by both now the syntax used for the capture of a variable are some signs for example we can use the and sign that is for capturing all the external variables by reference then if you want to capture by value you have to use the equal to sign that capture all the external variables by value then if you want to capture by both value as well as by reference then you have to write in a normal variable for example let's say x and then a variable after putting a comma sign and of y for example you can write in something like and of x and then sorry not x and then and of y so this is actually going to capture both by reference as well as by value so a lambda with empty capture clauses can access only those variables which are local to it now how this this capturing is actually done so if you can see right in hill write about in this for example let's say let's talk about this function in which we have actually printed out the number which in this case we were actually talking about the first occurrence of an element which is greater than this four so whatever we want to talk about some other element for example we want to talk about this five and see that which is the occurrence when this element is greater than something so for that what we are going to do is that you have to specify for example let's say int n and we say let's in n equal to five and then in these brackets we have to write in this n and in here we are going to write in i is greater than n so this is how we have actually captured variable so if i just run it now you can see right here that the first number greater than four is seven it is not actually now four it is actually now five so in the previous case the output was this five because we were talking about four the first number greater than the four was this five but the first number now greater than this five is actually seven so now we have actually captured the value that is in between any value in these brackets of the lambda expressions you can see right in here that now we have the first number greater than five is actually this seven so now to give you one more example we talk about let's say this count of case in which we were actually counting number of values that were as greater than a specific number so if we just write in an equal to sign here which means that now we are talking about capture all the external variables by value so when we put this equal to sign in here this denotes that we can access all the variables now it is going to count numbers that are greater than or equal to n actually so let's just run it again now one thing we need to check our stop we are not talking about this unique we are talking about this counter function so we have to put in this equal to here and now we are going to write in a is greater than or equal to n so now we are going to talk about capturing all the values by reference and we are going to actually access all the variables so in this case we have actually access this n so now in the previous case it was giving us two because the occurrence of the specific value was two but now in this case it's n which is five so if we just run this code so you can see right here that the number of elements greater than or equal to five are actually two so actually we have talked about the very same element let's just change it to six not six because six doesn't exist let's just change it to one and right here we are also going to change this equal to one and now run it so as you can see right here that this value is also changed and you can see right here that the number of elements greater than or equal to one are nine because all the elements are greater than or equal to one so this generated the total number of elements for us and you can see right here the first number of grades in it now it's actually talking about one so the first element in our list was four which was greater than one so it generated as four as an output so this is how we can actually capture some values so we have already solved our examples so one more thing before I sign up from this tutorial is that you need to understand that c++11 and the versions after the c++11 only supports lambda expressions if you are working with any compilers that are before that c++11 and they're after versions then they are not going to be compatible with lambda expressions and they are not going to run that for you so you need to keep this in mind that when you are running it you have to check the compatibility of your compiler that it is c++11 or the version after it so thank you so much guys for watching I'll see you guys in the next tutorial hey guys what's up this is Sumerhan and I welcome you to another tutorial here on this section on advanced c++ so in this tutorial we are going to be starting on with a new topic that is signal handling now what are signals signals are basically the interrupts that force an operating system to stop its ongoing task and attend this task for which the interrupt has been sent these interrupts can cause a service in any program of an operating system similarly c++ also offers various signals which it can catch and process in a program now as you can see right here we have a list of various signals and its operation that c++ provides the user to work with the first one is the signal int that produces the receipt for an active signal the second one is the sigterm that send a termination request to the program then we have the sigbus that is the bus error which indicates access to an invalid address then we have sigll that detects an illegal command then we have the sigalrm this is used by the alarm function to indicate the expiration of a timer then we have some more functions right here that is the sigabrit that operation is to termination of a program abnormally then we have sig stop the signal can be blocked cannot blocked handled and ignore and can stop a process then we have sigsegv that's operation is to detect invalid access to storage then we have sigfpe whose operation is to overflow operations or mathematically incorrect operations like divide by zero then finally we have sigusr1 and sigusr2 that are actually the user defined signals now we have this signal function the signal function is provided by the signal library and it is used to trap unexpected interrupts or events this is the syntax right here in front of you that is the signal the registered signal is the first argument and that signal handler is the second argument so the first argument is actually an integer and it represents the signal number and the second argument is actually the pointer to the signal handling function we must keep in mind that the signal that we would like to catch must be registered using a signal function and it must be associated with a signal handling function now the signal handling function whenever it is defined it is going to be a white type now let me just show you an example of how this signal function is going to be implemented and how an interrupt is going to be caused and it is going to just stop the flow of a program so they just move on to my editor right here and the first thing we need to do is that we need to actually initialize one more library right here that is for signal handling and it is hash include and the name of this library is c signal not in capitals all right so this is actually the library that is the c plus plus version of the standard c signal dot h header and it declares function and macros related to the signal handling library so now in the main function we are actually going to do the calling part but before that main function we are actually going to use our functions so for example we have one function that is named as signal handler and in this signal handler function what we are going to receive is the signal number that is going to cause an interrupt and what we are going to see out is simply we are going to write in interrupt signal and then we are going to print out its number that is the signal number and then we are going to write in received so it is going to simply print out the number that is going to be received. And then this is going to end. And then what it's going to do is after receiving this signal that it is going to clean up and close up some stuff here and terminate the program. So for that, we are going to write an exit. And signal number is going to identify the signal which we want to exit actually. So now down here in this main function, we just scroll it down so that it is much visible. Right in here what we are going to do is that we are going to register the signal using the S, I, G, I and T and the signal handler. So we are going to call in the signal function. The first thing that we need to do in here is that we need to use actually some library from right in here. So we are going to use in this signal initializer that is going to produce the received for an active signal. So if we move back to our def C++ right in here, we are going to write an S, I, G, I and T. And then what we are going to do is that we are going to call in signal handler. And finally, what we are going to do is that we are going to actually run a program. For example, let's say it's while one, what it's going to do is that it's going to see out going to sleep and it's going to sleep for one second. This is actually the time. And then finally, what we're going to do is that we are going to simply write in return zero here. So now if I just run this code, you're going to see something very interesting. What's the problem with this? It says that sleep was not declared in the scope. Yes, I think I just forget to include the library for that. And it is going to be hash include unistd.h. This is actually the library to run the sleep function. What is the purpose of actually the sleep function in here? It is going to actually print out this going to sleep after one millisecond. So now if I just run this code, you can see right here that going to sleep, going to sleep, going to sleep. But if I just press in control plus C, you can just note it down that you have to press control plus C to actually initiate that function. So if I just press in control plus C, you can see that this function was actually executed and it existed whatever the signal number was. So if I just put in a sleep timer here to just show you what happens actually here, sleep one, now run this code, you can see right here that it says going to sleep, going to sleep, going to sleep. But now if I just press control plus C, it says that interrupt signal to whatever let's just increase this timer so that it is visible for some time so that I can show you. Now you can see for now press control plus C, it says interrupt signal to receive. This is actually the signal number that is received. So after six millisecond, the program ended because this condition that says exit signal number two. So it was excited actually. So I hope this is clear that how the interrupt signal was actually caught in when we press in control C. So now moving ahead. So you know now how the signal function work, how you have to use the register signal and signal handler. And now the next thing is the raise function. The raise function is actually used to generate signals and the syntax for it is very simple. You try it and raise and the signal. This argument is right as mentioned in the list. So let's just solve an example for how this raise function actually work. So let's move on to our editor. And we will have the very same program in which we will have this white signal handler in which we will just print out this interrupt signal and then the signal number. And then we will also just exit the signal number and let's just decrease the sleep timer to three. And then right here in this main function, what we are going to do is that we are going to initialize a variable that is in i equals to zero. And then what we are going to do is that we are going to in this very same way call in this function. And let's just remove it from here. Let's just align it a bit. And now after calling this function, what we are going to do is that now we are going to initialize a while loop that is sliced using plus i i not plus i it's going to be plus plus i. And what we are going to do in this function is that we are going to see out going to sleep. And now we are going to use an if condition that if i equal equals to three, what is going to do is that it's going to raise the signal. And what is going to be the signal it is going to be s i g i n t and then a sleep function right in here for one second. And then finally our return zero is already here. So let's just put away these lines and run this code. Now as you can see that when the code is executed, it produces this output and it would come out automatically that the interrupt signal two is received because now actually we have raised a signal when i was equal to three. So it printed out going to sleep three times. We don't have to press control plus see this time because what the race function did was using right in here. It raised a signal and initialized that signal. So when this function was called in this white signal handler was automatically called in the previous case, this signal race function was called in when we press in control C. But now we have actually done it using a code and we have used the race function for that purpose and right in s i g i n t. So when this function is called in when i equal to three, which means that three times going to sleep after one second is going to be printed first. And then it's going to print out interrupt signal to receive and it is going to terminate that program. So I guess that is clear. So this is how you can just use or use the signal function. And this is how you can just use the race function to actually initialize your signal. So these are some of the other functions as well which you can use. So you can see the examples of them yourself. These are quite easy. You have to use them in the very same fashion as I explained this first time right first word right here. So I guess that's it with this tutorial. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial here on advanced C++. In this tutorial we are going to be starting on with a new topic that is command line arguments. Now to tell you anything about this command line argument and what is the purpose of command line arguments in C or C++. You need to know one thing right here. Now you know that there are many kind of functions you can use in C++. You can use build in function. You can use manual function. And then we have a function named as min that actually controls the flow of your code. So the most important function of a C program or a C++ program is that main function. It is mostly defined with a return type of integer and mostly without arguments. Now we can also give command line arguments in C and C++. Command line arguments are given after the name of the program in command line shell of the operating system. To pass command line arguments we typically define main with two arguments. First argument is the number of command line arguments and second is a list of command line arguments. Now this is how the syntax actually look like. You have the main function then you have two arguments. First is the integer argc and then we have the character a double pointer and then argv. Now if you talk about the first one that is int argc this is actually the argument count. It is an integer and it stores the number of command line arguments that are passed by the user including the name of the program. So if you pass a value to a program value of the argc would be two. Now why it would be two because the first value in the argc is going to be for the argument and the second one is going to be for the program name. Then we have the second argument that is char then steric steric and argv. This is actually an argument vector which is an array of character pointers listing all arguments. If argc is greater than zero the array elements from argv zero to argv until argc minus one will contain the pointer to strings. Now argv zero in every case is going to be the name of the program after which all the arguments are going to be initialized. So let me just show you a very simple code and then you can see the difference between these two arguments and how they are actually going to be used. So here we are in defc plus plus. Now in the main function what we are going to do is that we are going to pass in two arguments actually. Right here the first one is going to be argc that is the argument count as I told you and then we have this character steric steric argv that is the argument vector. Now in this function what we are going to do is that we are going to simply see out and we are going to write in you have entered argc which is actually the argument count so it's going to generate as the argument count and then we are going to write in arguments. So it is going to be a simple statement that is going to say that you have generated for example let's say if you have two arguments then it's going to say that you have entered two arguments. So now what we are going to do in this main function is that we are going to initialize a for loop and in this for loop what we are going to do is that we are going to write in i equals to zero i is less than argc and then i plus plus is going to be a colon and it is going to be a pre-argument not a post one so it's going to be plus plus i not i plus plus all right. So now in the body of this main function what we are going to do is that we are going to simply see out argv that is our argument vector and here and i and finally what we are going to do is that we are going to write in return zero after this. Now for example we have two arguments then the value of this i is going to be equal to two because this argc is actually this argument count so if we have two argument this loop is going to be running for i equal to zero and then i equal to one and after that this condition is going to be false because now then two is not less than two because two equal to two so i equal to zero this condition is going to run perfectly and this for loop whatever it is whatever this statement is it is going to be executed and then for i equal to one this statement is again going to be executed and what this statement is actually because as I told you that this argv is actually the argument vector so for two arguments what is going to do is that argv zero and then argv one is going to be printed out now always remember that argv zero in every case is going to be the name of the program and then argv one until argc minus one since for two this argc is not going to this condition however here is not going to run because this condition is going to be false so it is going to run until argc minus one so every other value other than i equal to zero until argc minus one is going to be the elements in the argument vector so if you just run this code now write it is scanning this all right it says you have entered one argument since now we have not called this function because this function is going to be called from your command line and where is that command line actually this command line is actually going to run from your Linux machine so if you run this code from your Linux machine by entering this coordinates and let me just write you the code for that so that you can just run it so you can write in in your Linux machine main return dot cpp minus o means output and then main and then you are going to write in main and then you can write in anything like let's write your name anything else so what is going to do is that is going to run your code for you in this case what is this omer and this han is going to be the argument which means that we have passed in two arguments to this main function so in this case it is going to generate that you have entered three arguments why it is going to say that you have entered three arguments because first is the name of the program as i told you then this omer is the first first argument and then this is the second argument so zero one two makes equal to three arguments so it is going to generate you have entered three arguments so if you write in something like anything like i am cool so then it's going to say that you have entered four arguments so you can just run it from your Linux machine and then you can see that how it is going to run so you have seen this now no other platform dependent formats are also allowed by c and c plus plus standard for example unix and microsoft visual c plus plus have a third argument given as the program's environment otherwise accessible through get env in standard library dot h so for that you can refer the c programs now to talk about the properties of command line arguments first property is that they are passed to the main function they are the parameters or arguments that are supplied to the program when it is invoked thirdly they are used to control the program from outside instead of hard coding those values inside the code fourth one is that argv argc is a null pointer then argv holds the name of the program as i told you and then argv one until argc minus one points to the first command line argument and then so on until the last argument so you pass all the command line arguments separated by a space but if argument itself has a space then you can pass such arguments by putting them inside double codes or single codes now to talk about the last thing that is what are the changes in output if you pass in different arguments for example if you pass no argument and call it without argument one the above code is compiled and executed without passing an argument it is going to say that you have entered only one argument and that argument in that case is going to be actually the name of the program as i told you when i ran that code but run it again you can see right here that it says that you have entered for an argument because we have passed in nothing to this now if you pass in three arguments what it's going to say is that you have passed in four arguments in which the argv zero is going to be the name of the program and then argv one argv two and argv three are going to be the first second and third argument respectively similarly if you pass in a single argument then what is going to say is that it's going to say that you have actually passed in two arguments the first one is going to be the name of the program that is argv zero and argv one is going to be the first argument in this case then for example you want to pass an argument that already has a comma in between then you can use quotation marks for that purpose for example you can quote a larger statement in single quotation marks for example you can just write in like i am and comma then so this kind of arguments in which you have a comma in between you can put them in quotation marks and it is going to treat them as a single argument so in this case it is going to also say that you have passed in two arguments first argument is the name of the program and the second argument is right in here in this quotation marks so i hope it's clear so that's it with this tutorial and that's how you can use command line arguments you can try it in your Linux machine and it's going to run very perfectly and you're going to enjoy it so that's it with this tutorial thank you so much guys for watching and i'll see you guys in the next tutorial