This Pointer in C++ | C ++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to nourish 80 this is Kishore and now I am going to discuss about this pointer in C++ okay now the topic is this pointer now what is called this pointer and why we need the this pointer actually this point that it is a new concept introduced in C++ and here this point it is obviously used for this pointer is used for retrieving the object address okay generally in C language to find out the variable address what we are using ampersand symbol okay in C language to find out a variable address generally we are using ampersand which is also called address operator and in C++ also we can retrieve in C++ also we can retrieve or to know the current object address to know the current object address they have introduced this point next not only that one this point or another advantage is it is used to distinguish our data members from local variables when both are declared with the same name that means whenever our data members and function arguments number function arguments are declared with the same name okay the compiler is getting configured what is the confusion which one is the data member which one is the argument thus what to avoid this problem to identify the data members we have to use this pointer and I will show you practically next next every non-static number generally in C language our simplest this we are having two types of members static members non static members and here every non static number of C++ is having the local variable called this okay and that means static variables or static members never contains this and okay this pointer returns the address by default in hexadecimal format okay now I will give you one practical example hunt this pointer okay how to use the dis pointer in our programming just watch it a shingle or some iostream dot h next hash include Canio dot h no basically two header files will hold it clear now we are having two head of us later classes declare suppose class test now it is the class test in class test we are having two different members int a comma B now these are the data numbers next in public area wide show okay here I am going to access the variables like this a equal to 10 B equal to 20 and I want to print the object of this class object clatters then generally we are going to use C out object addresses okay ampersand generally what ampersand object name but here the problem is this one is conducted inside the member function inside the member function there is no object creation then how it is creative means how it is working that's why here I am going to write like this what is that be not so now whenever this is easier here this is a pointer different returns what the current object address now here this is returning the current object address that means whenever this object created this class object is created objects or what memory areas means variables variables compulsorily occupies what memory areas that is why that memory address fee is going to print here okay next not only that once we access the a and B we can use at this point at how it is suppose and then next suppose C out I want to print the a value using this pointer then a equal to this arrow mark a and uncie out be equal to this arrow mark being how program rolls and now what happens is actually here only this is user well only this is user on it returns the current class object address beside here object or the sees printed next line a value a value is the number of this class that's why this arrow mark key means current object a and B because this object B means current object B means data members now this one is going to bring to the a value this one is going to put into the B value okay next how to print this suppose main function now why do main first of all we have to declare the object no then test it e now T is the object created from test on T stored somewhere in or well you know now I want to get that address then simply the out objects are diseases okay here already object rattles I have given but I directly do one thing okay T is the object now now directly call T dot okay function name is what show now get CH program finish well this function is called now the definition is executed a value B constant B value becomes 20 now object others now it gives the T object address here in the form of hexadecimal format now it is going to print the object address at here in the form of or decimal form later this year of marque now initialize with the Tanana ten printed later b-value 2020 it is how to access how to access the data members okay under object address using the disappoint or next not only this one this point is also used to distinguish the argument under our data members now what it is local variables and data member how they are distinguished should using this pointer now I will show you how to differentiate our data members from local variables okay just watch this example here class test two members already cleared one is a one is be later I am going to use boiled show function same now this time I'm going to use X comma int Y now everybody knows they are called formal kilometers later a equal to X do equal to Y now what happens this x value copied into a this Y value copied into B okay that's why no problem at all now suppose it is the main function void main now in this example what I am doing here watch it suppose first of all test the object created and later T dot show off 10 comma 20 now what happens 10 value possible to X and 20 value possible to what the Phi X becomes 10 y becomes 20 but 10 password to a 24 to be now there is no problem there is no special T in this program now the situation is are now the issue is suppose you are using once again here here on behavior okay listen careful here suppose you are declaring the formal parameters with the end B now the problem starts suppose you are using a equal to B equal to B now here equal to a B equal to B what happened is which one this a is which one okay here already we know that in every program local variables how the first brat in every programming local variables are having the first grade that's why in this situation what happened our data numbers and arguments both are with really the same name or arguments and data members both power declared it extreme now your compiler gives priority to local variables means your compiler understand the same is BC this means this week let me say value copy to same okay that means indirectly this here I will copy to 30 ay B will copy into that B means they are not positive this here and the B because of their data number Smith's class members these are the local variables which are having different blocks which are having different of means different areas memory also allocated it is giving that problem now to solve this problem okay actually after execution of this program what happens you are going to get garbage values for example here wild display function here C out a next in this space and B now the function closed class also suppose I am going to call this function t dot display no good CH program closed now what happened display function is executed display what happens a value B value printed now suppose in this situation a value not possible to DC behavior is not possibility because of local variable system is recognizing just so here data members are not recognized and without initialization their initial values are what plot is garbage that's why T dot display gives garbage X now it is a problem then how to all these forums only simple solution just here you can able to write like this this arrow mark a this arrow mark to be now your compiler understand this way means this class here and BCA meter that is argument here okay this Sierra mark M is this classy like this one and the same into the argument you came to same B also okay now this one and this one copied into this way this way values are initialized third problem is salt and one more thing you can use this technique also here star this dotty okay here star this dot a and star this not be same mini okay here we can use arrow mark in free language also pointer to member access operator pointer remember so here star this dot equal to R star this dot B equal to V now both are giving the is why in this example whenever in this example whenever our local variables and the data members local variables and data members are declared with the same name to distinguish or to identify our data members from that local variables just appreciated with this point of now it is giving the data members Netta okay thank you for watching [Music]
Info
Channel: Naresh i Technologies
Views: 113,705
Rating: undefined out of 5
Keywords: This Pointer in C++, This Pointer, C++, Kishore, this pointer in c++ example, C++ this Pointer, Pointer, pointer, Pointer in C++, C ++ Tutorial, Naresh IT
Id: wA8LriKDBAI
Channel Id: undefined
Length: 13min 6sec (786 seconds)
Published: Fri Mar 31 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.