Pointer to Object | Pass & return Objects in Functions | C++ Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you what's going on guys than my offer simple snippets and I'm back with another video tutorial on C++ programming so in this video tutorial we will be looking into the concept of pointers and objects and some other more functionalities that they are going to look in terms of object-oriented programming so in the previous video tutorial we saw topic on constructor and destructor and if you have missed that video you can check the link on the top right corner of this video so let's see what all topics that we are going to cover in this video tutorial so as you can see in this screen the three key points in concept that we are going to cover in this video tutorial is going to be how to pass objects is an argument in functions for a turning of objects from functions and the last as we know is pointer to objects now if you don't know what functions are or how to pass variables or arguments to functions and how to return values from functions we have discussed that in this entire tutorial series so I'll put a card on the top right corner and you can check that out and also we have discussed pointers as a concept as well as we've seen some programs and some concepts based on pointers for example dynamic memory allocation so you can see the cards to that on the top right corner as well so if you have missed these concepts of functions and pointers you can check these videos which I have put as cards on the top right corner because those are the very basics and fundamentals and this video will be very understandable if you already know those concepts so with that being said let's get started directly with the program because there is no theoretical aspect to this video ok so quickly open your day c++ ID and I would recommend that you put along with me so that you get good programming practice and that is the best way to actually practice programming and be good at programming so as you can see I've already created a file which is complex NO dot CPP and I have already typed down the basic structure of the program so you can just type it out you can pause this screen and type it out with me you can go to file create new and create a new file and if you have any other ID you can go ahead with that as well but I would highly recommend to use de C++ ID ok so what we are going to do in this program is we are going to create a custom class known as complex number and we are going to create few objects now what is a complex number in mathematics a complex number is a number which has a real part and an imaginary part and if you want you can go google out some more details about what complex numbers are so these are the two data variables that our class would have so let's start off with creating the complex number class so just below this using namespace standard I'll start with class complex number if you don't know how to create a class you can check out few videos from this playlist itself we've discussed in the very basics of object and programming what is class what is objects and so on so your again you can see I've created a class complex number now as I mentioned inside this we have to create the private section and inside that we have to create two parts the first one is going to be int 3l and for the imaginary part I will take it as float imaginary so these are the two data members so these are the data members of the class and what we are going to do in this program is we are going to create two objects so say for example calm fun and let's say the calm fun would have the real part as five and the imaginary part is 6i so this is how complex numbers are represented the eye is stands for imaginary so calm fun is 5 plus 6i similarly we have calm 2 which would be something like 3 plus 4i so this is just a representation I am just giving you an example and what we are going to do is we are going to create a function which would take these two objects as arguments so that is when concept that we are going to cover and what Pitts will do is that function will add the individual real and imaginary parts of the two objects create a third new object form 3 which would be something like 5 plus 3 for the real part plus 6 plus 4 and this is going to be for the imaginary part and this comm 3 would be returned from the function and we'll take it and create a new object out of it so this is the whole idea and so for that let's first go ahead and create some functions so in the public part we declare all the member functions so the first thing I'll do is I'll just create a constructor now you already know what constructors are and if you don't then you can check out the previous video or create a default constructor after that I will create a parameterised constructor that would take in the two values or the two data member values so I will see int I comma int R float I so R and I are just placeholders R stands for yell and I would be for imaginary and inside the parametrized constructor I will just assign these values and say real is equal to R imaginary is equal to I so the default constructor as well as the parameter is constructed is ready we'll create a one function which would display the data so I'll say display later now this function is simply going to print out the real and imaginary part so I'll say see out real part or I would directly print out the entire number so I'll say complex number is then your I will see a real then append a space to it and inside that I will print plus so this is hard-coded again append the excretion operator or the output operator which would print to the screen and I will say imaginary and append a I to it and then just give end Andrew times so that the cursor gets onto the alternate line so this is for the display data so you can see our class is pretty much ready so let's try to create a complex variable or object is a complex number com1 and URL pass in 5 comma 4 so this parametrized constructor will be called so this is how you explicitly call the parameterized constructor you have to pass these values in down brackets and let's see if this works I will say chrome 1 dot you can see display data so I will just see you this and let's try to execute this ok I did not put all listen comments I'll just cut this and resist over here so this was all the comment part and forgot to comment it out okay I'll just save this and compile and run and I think it would run fine so there you go you can see complex number is 5 plus 4i so our basic class is ready now what do you want to do as I told you is we want to create two complex numbers so let's say calm - let's see this is again 5 comma 4 and now what I want to do is I'm going to create one more complex number from 3 but I'll say calm 3 is equal to and here I'm going to call a function which is going to take income fun and calm - as arguments and return a new complex number which will have the total of 5 plus 5 as real part and 4 plus 4 as imaginary part so let's go ahead and create that function and that function is going to be outside the class so it's not going to be inside the class in this case we were to create it outside the class because we want to call it without using any object so if if it would have been inside the class we would have to use calm fun dot and then that function name so we don't want to do that so what I'll do is I'll say complex number add two numbers ok so what just happened is this is my function name add two numbers and this is the return type now see that this is not int or float or double or all the standard datatypes but this is our own class name because we want to return an object of this type so that is why the return type is going to be complex number now open and close round brackets so this is how the syntax goes for functions you must be already knowing it if you don't then you can check the video as I mentioned earlier so inside this I am going to pass again arguments which are going to be objects of complex number so I'll say complex number n1 comma complex number n2 so these are the two objects of the same data type that I have passed and the data type here is our own user-defined class which is complex number then opening and closing brackets and inside that we will perform all define what the function is going to do so I'll create two variables into R comma int R and float I so these are just going to be temporary variables and now what I want to do is I want C R is equal to n1 dot and I want the real part of n1 to be stored in this R so you can see if I just hit control space I'm not able to get the variable directly over here and this is where the object-oriented concept of encapsulation and data hiding comes into picture now because these variables are private you cannot access them by eye on any out outsider function so since this add to number function that we've created outside this class you cannot directly access them using the object and using the dot operator so in that case we create two more functions we will say in get real part and what this is going to do is as you can see the return type is int which means that this is just going to be returning the real part as the function name itself suggests similarly we will create one more function which is float get imaginary part and this is going to return the imaginary part control space okay so these two functions will declare inside our class in the public section so now what we can do is you're using n1 I will say but get real parts and add it with n to get real part so these two objects that we've passed over here n1 and n2 we are getting the real individual real parts we are taking a sum and storing it in our temporary our variable now similarly for the float I that we've just created inside this function I will say I is equal to n1 dot get imaginary part plus n2 dot get imaginary part so the addition of real parts of n1 and n2 is in this temporary our variable and the addition of imaginary parts of n1 and n2 is is in this imaginary I variable now we will create a complex number object and we'll name it as temp and inside this will pass these two new RN I temporary variables so R and I so because our is having addition of real part of the two complex numbers n1 and n2 and I is having addition of two imaginary parts of n1 and n2 we'll pass them in this temporary object that we've created and lastly we'll just return this object so this is the complete add two numbers function that we've created since the return type is complex number we have to return an object of the same data type that is complex numbers in this case so I have just returned temp and now let's try to use this function in action what we will do is we will change these values first we will say 2 comma 3 now what we're going to do is we'll first display comp 1 and come to data so I'll say display data for both so from fun should have 5 plus 4i which would be printed over here at this line then at this line it should print comm twos data that is 2 plus 3i and then what we will do is we will call that function so I'll say comp 3 so this is come 3 that we've created and the default constructor is called which means that the imaginary and real part of comm 3 that is the data members of comm 3 have not been assigned any value so I'll say calm 3 is equal to let's say add two numbers so I'm going to call this function and because this function is going to return a comp objects or a complex number object that object is going to be assigned to this comm three object so all the data member values that is going to be returned by this function is going to be transferred to this comm 3 object so in this we are going to pass two complex numbers so this would become 1 comma 2 so let me just print out a message addition of m1 and comp - and then we will just stay calm 3 dot display data okay so let's just save this and let's see what the output is ok so I've saved this I'll go to execute I'll say compile and run ok I just missed out a comma over here so I'll just give that comma close this and again go to compile and run ok one more typing error over here I just merge this entire sentence let me just tell you it out the statement itself save this and I think we are good to go so this is the int main function we created all the complex numbers and performed all the operations go to execute compile and run and I think this time it would run fine okay there you go so as you can see the first complex number comm 1 is 5 plus 4i so going back to the program you can see this is what it is going to print the second one is 2 plus 3i so this is what we've passed and the third one wherein we displayed the message addition on com1 and come to the complex number is 7 plus 7i which means that the addition happened successfully so 5 plus 2 which is 7 again 4 plus 3 is 7 so what happened is these two numbers confident come to were passed as arguments in this function then we got the individual real parts and took us some and store it in our temporary our variable similarly for the imaginary part we stored it in a temporarily I variable and using rni we created a new object of type complex number since it is a temporary object because we just need that object to return it from this function we just returned that you can see over here and this temp is now assigned or the values of the data members of temp is now copied in this third complex number that we've created over here and then when we say display data we can see the addition happening so in this we achieved two things so we achieve the passing objects as an argument so we pass the two complex numbers in that function and we also saw returning objects from the function so after addition we returned the temp object so over here you can see we have returned the temp operate which is of type complex number itself so now the last part pointer to object is left so let's try to create a pointer first so I am going to create a pointer which is going to be of type complex number itself cells a complex number and the way we create pointer is we have to use a star and then I will say PTR 1 so here I just declared a pointer and then I will see PTR 1 is equal to and use the address operator and and I'll pass the address of comp 3 now if you've seen the pointer video in this series it was in the procedural oriented part wherein we created integer pointer or floor pointer but in this case since we want to point to an of type complex number you have to create a pointer with the same data type so essentially this is acting as a data type over here so we've created that point of that same type and only then this pointer one can now point to comm 3 since comm 3 is a type of complex number and this is a user-defined class now let's see if this pointer PTR 1 can be used to call calm 3 is display data now pointer 1 is pointing to calm three objects essentially we can use this pointer to call function of comm 3 and let's see if that is possible we have to use the arrow operator to actually call it and you can see that in the intelligence it is giving us certain options you can see it is showing display data as well so using this pointer which is pointing to com3 object I'm calling the display data function so let's see if this works what I'm going to do is I will just print out a message over here and say pointer to object just save this go to execute and I will see compile and run so there you go you can see pointer to object and I'm using the pointer to call out display data of comm 3 and you can see CompTIA 7 plus 7 I over here and it is the same over here which means that the pointer is pointing properly and the program works fine so this is how you use a pointer to point an object and does not usually use but sometimes in there may be a case wherein you need to use a single pointer to point to different objects of the same class and use that pointer to call out functions so essentially what I can do is I can say PTR of 1 is equal to and calm too so I can do this over here so after displaying data of calm 3 using pointer 1 what I can do is I can use the same pointer 1 to now point to comp 2 which is another object so it is this object so it should print 2 plus 3i over here if I say beauty r1 display data so let's see if this happens compile and run so there you go the second time the pointer is pointing to the com2 object and using the same pointer I am printing the data of calm to object so this is 2 plus 3i so this is how using the same pointer I'm calling out functions of multiple objects of the same class now if you want to see what is so now what we can do is if you want to see what is there stored inside the pointer you can just save PTR one and what this is going to do is it will just print what does the pointer have so what is stored inside the pointer now you know that pointer always stores a dress so let's see if PTR one has some address stored so you just save this and go ahead and execute this and there you go you can see that there is a address number that is being printed now usually memory addresses are in this format we're in 0x 6ff it is sort of like hexadecimal numbers so this is what is printed because pointer stores address so hence prove that the pointer has an address and this address belongs to this com2 object because it is currently pointing to the com2 object which is of type complex number okay so that's it for this video guys the covered three key points and concepts that is passing objects as arguments in functions and returning that object from functions and the last was pointer to objects hope you guys understood this tutorial and if you have any doubts you can always put them in the comment section and if you like this video give it a thumbs up share it with your friends and don't forget to subscribe to this channel peace
Info
Channel: Simple Snippets
Views: 51,865
Rating: undefined out of 5
Keywords: pass object as argument to function, return objects from functions in c++, pointer to object in c++, c++ programming practically, c++, programming, objects, c++ object oriented programming concepts, c++ programming tutorials by simple snippets, pointer to class, functions, pointer to an object, pointer to object in c++ simple snippets, pointer to an object in c++
Id: w35ObkSRq48
Channel Id: undefined
Length: 18min 39sec (1119 seconds)
Published: Wed Dec 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.