Copy Constructor 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 today we are going to discuss about copy constructor what is a copy constructor and how it is useful in our programming okay already we have discussed a constructor is a special member function whose name is similar to class name and which is easy to initialize the object itself okay which is called automatic initialization of object and we are having basically three types of constructors one is default constructor second one parameterised constructor and third one copy constructor in previous sessions we have discussed it what is a default constructor and how many types of default constructors are there and later we have discussed a parameterised constructor also now today I am going to start copy constructors okay now what is called copy constructed a copy constructor is a constructor which is easy to initialize initialize the current object values with another object values okay let's say a copy see this the name is very clear copy generally copy means what instance copy means was instance which is also called a Xerox that means copy constructor is easy to create another copy are Xerox our instance of one object okay that means right now it is used to initialize one object values with another object means current object is initialized with another object values okay next where it is used generally input we are having in C++ that is operator overloading okay in C++ we are having operator overloading concept actually there we are going to use copy constructors next here most important thing is copy constructors are having we constructors are having reference type parameters okay here the most important thing is copy constructors are having reference type parameters already we have discussed that in C++ they have introduced a new concept called a reference variables okay through the help of reference datatype now in copy constructors we are going to use this reference type parameters next another important thing in copy constructor is in copy constructors we are having objects means class type parameters okay in difference parameters we are having class type parameters that means what objects thus I here it is receiving another object it is receiving another object to initialize the current object data okay thus I copy constructor receives another object to initialize the current object now so it is called copy constructor okay and copy constructor is having what okay it is having a reference type arguments and it is having class type parameters which are called objects now I will show you how the copy constructor is working okay with example first of all common header files next cuneo th here i was streamed at which is fir see out and see in and Konya villages for CLRS here and get CH in my program next now i am going to start the class void suppose sample here i want to start the class now that's why class sample okay and here it is the class now generally class is having what both the data members earn member functions that is why here to data members a comma b next in public area okay i want to initialize the a and b through a constructor through a parameterised constructor that's one sample T int comma int B now what it is class name and hear what it is functioning now the function name and class name both are similar when both are similar what it is called constructor and this constructor is having what arguments okay that's why I know it is called parameter I is it a constructor okay now it is the parameter is constructed and here on important point is that actually data remember names are what a be an argument names also a be here both are same name then our computer is getting confused that is why here we have discussed this point that is why here star this dot a equal to a and star this dot V equal to B now close in this copy okay in this constructor what happens this see a value copied into this dot a and this B values copied into this dot means argument values are copied into here data members next now it is called parameter constructor okay finish it now we need the copy constructor already we have discussed parameterised constructor also what one of the constructor and constructor primary rule is what the name should be similar to the class name that is why here sample okay now function and started and already we have discussed that copy constructors are having what object type means class type parameters which are called objects that is why here class name is what sample and here reference type parameters always class copy constructors are receiving what objects as reference that is why here I am percent okay here old for example I have infinite old now the meaning is what old object data now the world object data I want to copy into the new object okay now I want to initialize the new objective data members with world object data members that is why here sample is the word classmate and ampersand indicates what reference variable or reference the data type and world is the object and which kind of object reference object now here I am going to write like this AE equal to okay equal to voltage and V equal to will do dot to B now what happens when this constructor is in worker we are sending world object data that object is copied into this new object that's it now one object values are copied into another object that is why it is called copy constructor now it is nothing but copy constructors okay now the copy constructor also created and we have discussed that copy constructor is having reference type parameters and copy constructor is having class type parameter next now wide print I want to show the Daytona the so I suppose viral print now see out first I want to bring a value ndele next see out be equal to be endl now class also closed it is for what printing the AVL is now here it is the parameterised constructor and it is the copy constructor and it is the print print function now I want to call these functions okay it is the main function would mean in main function what I want to do I want to first initialize one object that's why class name is what sampled the class name sample and s1 okay actually this one is what one object and Here I am sending 10 comma 20 now what happened sample is the class name and s1 is the object and object is having world parameters that's why it is going to invoke a parameterised constructor now 10 is positive yay 20 positive B but 10 and 20 are received by this object a and B means this object means es 1 that means now the memory s on suppose it is ass one object it is having a now that here becomes 10 B becomes 20 okay whenever this object is created automatically this parameter is the constructor is invoker's antennas received by a 20 is received by B and when it is created and s own object is in vocal that's why here as one is having what we and we know now here this here becomes this value means nothing but a 10 and this way becomes this B value that's what here 20 that is why s 1 and s 2 becomes 10 and 20 now I want to copy this s 1 data into S 2 means other object that's why Here I am doing like this okay here in Luke parameterised constructor okay this line invokes the parameterised constructor next watch it sample yes - okay here sample test but here I want to initialize s2 data with which one s1 data that is why here ampersand because of reference arguments are declared like this sample I am percent s2 off okay here yes one now what happened the s-1 data is possible to and here watch it actually s1 is what object an object is already having mod 10 and 20 and here what we want to send sample is to of s1 here meaning is what now yes - is another object which is having s1 object s one is already having about 10 and 20 now 10 is the a value country is the B value now what it is it is going to invoke the this copy constructor now it is going to invoke the copy constructor but so what happens s1 means this one s 1 is having about 10 and 20 now that's why old value becomes s1 data s1 is already having 10 and 20 that 10 is positive this a 20 is positive B okay here 10 is positive this here 20 is also to this P that's why here s2 data initialized with yes one data that's why here it is the s2 and he regularly becomes 10 nearly becomes 20 now what happens s1 s1 is already having about 10 and 20 now now it is going to invoke the copy constructor and the world value becomes s1 values means older dot K means distant poll dot B means 20 they are copied into this he means we are sending s 2 that is why s 2 object values are copied it s one object values now I want to print this data s 1 dot print when it is called what happens s 1 a B values are painted and no 20 and later s 2 dot print now what happens is two of the data printed means 10 and 20 now the process completed get CH that it's closed now watch it we are reading this data first for s 1 we are reading the data for s 1 later we are copying the S 1 data into S 2 using what copy constructor that's why copy constructor is used to initialize one object with another objective data copy constructor is used to initialize one object with another object data it is the pieces of a copy constructor okay it is how the copy constructor is working thank you for watching ok next session we are going for another concept thank you [Music]
Info
Channel: Naresh i Technologies
Views: 122,746
Rating: undefined out of 5
Keywords: Copy Constructor, Constructor, Constructor in C++, C++ Tutorial, c++, cpp, Kishore, Naresh IT
Id: dcWTU5uRZrc
Channel Id: undefined
Length: 14min 13sec (853 seconds)
Published: Wed Apr 12 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.