in this video we're going to talk about the keyword this in c plus we'll talk about what it is and we'll go over some examples of where we can use it so the key word to this is used in non-static member functions so non-static member functions basically have this implicit argument they can use called this and i say implicit because we don't see it in the list of parameters but it's there and this is going to be a pointer to that instance of the object so let's go for an example we'll make a student class here and it'll be a pretty simple class we'll have two public member variables a name and an age for the student the name is a string the age is in it and here we'll make a constructor we'll say student and i'll say string set name and int set age and in the constructor we'll set the name to set name and the age to set age let's also try to output this so here we'll say c out and we'll output the keyword this we'll say this memory address colon i'll output this followed by an end line now let's try to make a student object so the main function here we'll say student student1 lucas and 20 and then here we're also going to try to output the memory address of student1 so we'll say c out student1 memory address colon and here we're going to output the memory address of student1 followed by an end line so we'll save this here now and we'll run it and we get that this memory address is this address here and the student one memory address is the exact same thing so that's what the this keyword is it's a pointer to this particular object instance for which this non-static member function is running in this case the constructor so we can use this to access the object instance for which the non-static member function is actually running so now that we have this pointer to the object instance itself how can we use it one of the ways we can use it is in a situation like this constructor where we have parameters here that are really just name and age values but i can't call them name and age right now because that's going to be an error in terms of the expected behavior we're going to have a bug because we have member variables called name and age and here i'm trying to use parameters with the exact same name this won't work what we could do though is say this arrow name and this arrow age now we're using the pointer to the object instance and we're accessing it's member variable name and now we're using the parameter name and we're assigning its value to that member variable and we do the same thing with age here so by using this we're able to use the same name for these parameters as the object's member variables so all of the non-static member functions are going to have access to this so for example we can make another non-static member function here we'll call it increase age and it's going to be a void function so it has no return value but what it is going to do is going to increase the age so we'll say this age is equal to this age plus 1. and so this function also has access to this it's kind of like an implicit argument that's given to all these non-static member functions and they've all just got it the other thing we can do with this is we could use it to call one of the objects member functions so we'll make another function here void increase and output age and this function is going to call the increase age member function using the pointer to the object instance this so we'll say this increase age and then we'll output the h we'll say age colon and output vh so let's test this out now in the main function down here we'll output lucas's age by calling that function increase in output age so if we see and run this we should get an age of 21 and we do because it was initially 20 and then we increased an output of the age and we get 21 there another thing we can do with the this pointer is have the member function pass this pointer as an argument to another function basically passing a pointer to the object instance for which it was called to that function let's make a function that's going to work with a student object pointer so we'll say void create report student star student so create report is going to accept a pointer to a student object as an argument now for this to compile we're going to have to say here class student so that way this function declaration here is aware that there's going to be a student type that we're going to define later we'll define the create report function now and this function will use its pointer to the student object to output the student data so we'll say see out and we'll output the student's name followed by a space followed by the student's age and then an end line and now we can make a member function that will pass the this pointer to the create report function so you will say void graduate and when a student graduates will output congratulations because they should get a congratulations when they graduate but after that we'll create a report for the student because that's probably a good time to create a report so we'll call create report and we'll pass this we'll pass a pointer to the object instance for which graduate was called so down in the main function now we'll try calling graduate we'll say student one dot graduate and if we save and run this we get congratulations and then we get the report on our student which includes their name and their age so we could also use the this keyword to help us implement method chaining where say for example we want to set multiple member variables of the student object by using setter methods in a chain so for example we could say student 1 dot set name john and dot set age and then we'll say 23 and the idea is that after calling student1 dot set name with john this set name member function would actually return a reference to the student1 object and by doing that by having set name return a reference to the student one object we can actually call set age right after it with dot set age here because it's going to be acting on a reference to student one it's going to be acting on student one and that's actually going to be okay so to do this we can use the this keyword to help us so we'll say student and set name string name and we'll say this name is equal to name so we'll set the member variable name of the object instance for which set name was called to the name argument that was provided and then we're going to return star this so the star operator is going to dereference the pointer when used in conjunction with and here which is going to signal that we're going to return a reference to a student object what we're going to return is a reference to this student object we can do the same thing with set age so i'll copy and paste this and we'll also make a set age setter function that will work with method chaining so we'll say int age age and age and now we're going to set the age member variable in the same way so if we save around this we're going to cause student 1's name to be set to john and their age should be set to 23 and we're using the this pointer to help us implement method chaining so we can save and run this and now when the student graduates their name is john and they're 23 years old and again what's happening here is that when student1.setname is called it's returning a reference to the student object for which it was called that's why setage can pick up here with that student object and continue to work with it so that's the this keyword in c plus and some potential use cases checkout portfolio courses.com where we'll help you build a portfolio that will impress employers
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