Introduction to C++ Namespace | CPP Programming Video Tutorial

LearningLad1,815 words

Full Transcript

hi this is Anil and welcome to the video tutorial for the learning lad on C++ programming so in this tutorial we're going to learn about the name spaces in C++ so first of all what the hack is a name space and also why the hack we need to use the name spaces all right so here you know before explaining you know what is this name space and why we need to use it I'm going to show you the problem that's going to arise if you don't use this name space so just for the demonstration purpose I have created two files under my sources folder you know file 1. CPP and file 2. CPP and this is our main.cpp so here in this file 1. CPP what I'm going to do is I'm going to create a variable called X and I just going to initialize it one to a value of 10 and then I'm going to create a a function called display so it's going to be void display and no I just going to use a c out statement here and I just going to say display from file one and I just going to end this line and I just going to copy all the statements and I'm going to paste that in this file 2. CPP and I just going to change this one to file two and you know we're just going to change this value let's say 20 or any other value all right save these two files and then we're going to include these two files in our main. CPP so I'm going to use hash include and it's going to be file 1. CPP and then it's going to be hash include and it's going to be file 2. CPP all right now we have included these two files to our program and these two files are going to define the same variable or the identifier X and the function called display so if I call the display function from this main function right now it's going to produce the name conflict and if I build and run this right now it's going to produce the error specifying that you are declaring this display function two times so I just going to save this and I'm going to build and onun this and now you guys can see we get the error and if I and if I open up the logs it says redefinition of int X and you know redefinition of wide display that's because you know here you know we are not declaring this variable X and this function display inside any other functions or inside any declarative region such as the name spaces and that's why they are said to have the global scope so here you know when we run this program the C++ will come to know that okay we are having two variables you know the X and the two function display and that's why it thinks that you are redefining the variable X and you are redefining the function display so here what happened is you know we are using this function and identifier with the same name so you guys may tell like you know you know why the heck I'm going to use the same name for these identifiers and the functions you know I'm going to use different names but you know when you write very large programs let's say a program of around 2,000 to 3,000 lines you know at that time it is very difficult to you know remember you know what name you have used for a function and for a variable and for a class and also you know when we talk about the large programs at that time we need to include the files from the third parties you know the code developed by some other developers and also you know we're going to use the functions available from the you know the standard labrary you know you guys may Define a function for example let's say ABS in our standard Library we also have a function called ABS so at that time there is the possibility of the name conflicts so whenever we're going to write the large programs there is the possibility of the name conflicts you know we we may use the class or function or the identifiers with the same name so if anything like this happens then we're going to get the error and to solve these kind of problems what we're going to do is we going to use something called name spaces in C++ so this name spaces are going to allow us to define a declarative region and whatever we going to Define between that region will be available only in that region so if you want to access that identifiers or the classes or functions defined inside a name space then you guys need to use a specific method so by using these name spaces you know we can solve this kind of name conflicts so the purpose of this name space is to localize the names of identifiers to avoid name collisions so here you know you guys can see we have used a lan called using namespace STD you know the standard C++ library is defined in this STD Nam space or the standard name space and this why you know we are specifying here that we going to be using the contents defined in this standard namespace so to define a namespace we going to use the keyword namespace and then we need to give a name for our name space so here just for the demonstration purpose I'm going to say one and then the curly bra and then the closing curly brace so here whatever we going to write between these two curly braces you know between this name space definition will be local to this name space if you want to access this outside the name space at that time we just need to specify the name space where it is defined and then the scope resolution operator and then the property or you know what you want to access so here for this file two also I'm going to define a name space so it's going to be name space and let's give a name let's call it as two and you know we need to write the statements between the curly places so here these statements will be present inside this name space so now if I run this program we're going to get the error but in a not because of this name Collision so I just going to build and run this and if I open up the box it says you know display was not declared in this scope thus because you know in this file one this display function is defined inside this name space one and this file two you know this display function is defined inside this name space two so they are not directly available outside the name space so now how to access the identifiers the classes or the functions defined within a name space so first we need to use the name space where it is defined here it's going to be either one or two we're going to refer this display function from this name space one that's why I need to write the name space which is one and then the scope resolution operator then what you want to access whether this identifier X or the variable X or this display function you know we just want to access display function so it's going to be display now if I build and run this you know display from file one so similarly I can access this uh display from file two so I just need to write the name space name which is two capital T and then scope resolution operator and then the function name which is display and if I build and run you know we not going get the you know display from file one and display from file two all right the next thing that I want you guys to teach you is you know here we are using this using keyword then the name space and the name space name you know the standard C++ Library functions are defined inside this STD name space and we are specifying that here so when we specify like this using this using keyword you know all the properties are defined inside that Nam space are available so here you know we are using this name space and then the scope resolution operator and accessing a particular property but here you know when we Define like this we're going to you know we going to access all the properties so if you want to specify that you know you are using all the properties from a particular namespace here you know you just need to use the using keyword you know using the name space which is also a keyword and then the Nam space name you know let's say one and now I'm going to call the display function and if I build and run this you know we get display from file one so here you know when you use this uh using keyword word to include a nam space to your program you know make sure that you know you are specifying the Nam space so that you know there is not going to be any um name Collision for example if I include the name space two also here you know in this method at that time we're going to get the name conflict you know I just going to build and run you know you guys can see that's because you know this display function is defined in these both name spaces and you know now when we call it the C++ is going to be you know confused to know you know which display function it has to call whether from this Nam space one or from this name space too so when you use this using keyword you know make sure that U you know you are specifying a name space so that you know there will not be any name conflicts all right so this is it guys this is about the name spaces in C++ you know what is a name space and why the heck we need to use a name space in our program so this is it guys thank you for watching and don't forget to subscribe and like my Facebook page at facebook.com/ learninglad and stay updated with the latest videos that we're going to make and I'll see you in the next tutorial

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Introduction to C++ Namespace | CPP Programming Video Tut...