Constructor Overloading Example-2 | C++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to Nara shady this is Kishore and now I am going to give the example for constructor overloading okay in gravy session we have discussed how to use constructors in different ways means your constructor overloading concept how to declare several constructors within the same program now I am going to give one more example on constructor overloading but the difference between previously and this one is how to use one constructor within another constructor in previous example what happened we have declared one default constructor one parameter is constructed okay and they are individual and we have invoker separately but this time I am going to show you how to call a constructor of one class within the constructor of same class that means how to call how to invoke a constructor within another constructor of same class okay now I am going to show you how to call the constructor first okay common header file I was streamed or h4 input and output Co don't feel like as usual Koni or h4 CLRS here and let CH later I am going to start a class class or you stood into class okay I am going to access terrain class okay here student generally is having student name general every student how to maintain student a and student how to join a course this way cool okay here student name student join course next year when suddently joining a course means he have to pay some money also that is why here float fee now I have declared three data members one is student name student joined course under student fees next here in public area because of here the rule is what constructor should be declared within the public area in public area I am going to declare a constructor like this is still you okay watch it here st UST is what class name when class mate when class name and function name both are same what it is called constructor and here it is the curve name of 20 comma cat course of 20 now what is it is a constructor okay we know that on this constructor is having how many arguments only two arguments only two arguments next now the point is name string type course string type and we know that in C and C++ we are not able to copy a string directly into another string we know that in C and C++ we are not able to copy a string directly by using assignment or equal operator into another string that's why they have given a function called STR CP why is he our CPU that's why here we should have to go for strcpy but here the thing is strcpy.c a library function and it belongs to each hydrofoil string dot H that is why here I am going to use string dot H also now hash include Stingo dot H that's why in this example totally we need three header files string with our hitch I was stream that hitch Konya dot H next h3 our CPI of okay I want to store this name in this name but both names are saying that's why just before I have discussed it means just previous session we have discussed we have to use this arrow mark name or store as this dot makestar this dot we're number nine now I am going to use this arrow mark on it actually this arrow mark name indicates what this name and it is initialized with this name now the meaning is what this name is copied into this name and actually this name is what okay local variable our argument now the argument is copied into this name this name is student name next same thing strcpy of this arrow mark course comma course now what happens this course copied into okay data number course actual discourse is what argument course and this one is copying to local where means data member course that is why here both name and course or corporate and one more data memory is it that is what see here I am going to view XI is equal to zero that's all okay now phase equal to 0 and now name is initialized with to this name course is initialized this course and phase becomes 0 okay fine now watch this here I am going to give one more constructor actually it is what parameterised constructor with how many parameters two parameters now there are meter I easy constructor ok next step here I am going to start one more concept class name is what student the SOI s tu but this time watch this what I am doing care name of 20 comma care course of 20 comma float - fee okay now watch this here class name is what student here constructor name means function name student when the function name and class name both are same it is called what constructor and it is having a arguments are parameters the so it is what parameterised constructor and this parameter is constructive carrying how many arguments only two arguments name and course but here watch it here also function name class name same that's why it is also what constructor but it is having three argument already we have discussed said that constructor overloading means what okay it is the process of declaring several constructors by changing by changing the number of argument or data types are order now it is the example for what by changing the number of arguments here it is carrying only two arguments but this one is carrying three arguments and now watch this generally we have to write like this once again test ERC be of this name name this course course but instead of this one we can use this one also s tu : : s T you offer name comma course the sole now watch it s t u st is what class name here this one is the class name next it is what constructor which you construct this constructors okay we are calling this constructor within this construct okay here we are calling why because how to distinguish means here two arguments are there here three are what actually two arguments means this one that is why actually hear what happened within one constructor we are calling another constructor okay like this also we can invoke one constructor within another constructor next now only two values are possible whatever the third one now I'm going to write like this okay this arrow mark C equal to fee here watch it sees value is not zero but here see value received from this okay now brackets close this calling constructor okay and it is what parameterised constructor okay that is why here in this example how many constructors I have used to one is with two arguments one is with a three argument whenever we are declaring several constructors means what constructor overloading but the interesting thing is in this example we are calling one constructor within another constructor okay next how to invoke this next I want to show the data then wide show student okay it is another number function see out first student name here name and L of C is going to pin to the strain it and see out student the course now it is going to bring to the course and here fee it is going to bring to the fee okay fine and it is another member function which is for output purpose actually these are for input and this one is for out now I have to invoke now main function is require void main here watch it us to see a lot as you console is clear later st us1 actual st you is what class name and s1 is what object and here in racket i am going to send like this st u s1 of brahma comma course equal to c place please okay also this st you s on s1 is what object of this one okay we know that another constructor even Vogler when the objectives clean now the object is declared but her point is we are having two constructors one is with the two argument only with the three orbit and here how many values you are sending only two that is why it is naturally with this constructor now this ROM opposite to name that name is received with this object name and here C++ is retaken by course that course is fostered to local course how first student it unfazed becomes 0 that means what happens whenever this program is executed okay first object create a No suppose it is not s1 object now it is a student name it is a student course under these student fee now what you are sending here named Amma that's why student name becomes Rama assumes today of course is what see place place next actually you are not sending anything that's a here see this what 0 the surface becomes 0 X s tu is 2 now what happened another object grade and this time you're sending leg is 1 comma suppose C plus this comma phytozine now what happened has to also object now but it is having 3 argument it is having 3 argument when 3 arguments are this here it is having only 2 so it is not much now it is having how many arguments 3 argument that means it is matched with this constructor now Warren store in need and simplest list course and 5000 see that means second object created okay and here s 2 is the second object and here name equal to 0 and here course equal to C plus B and C equal to five thousands beside first student phase is 0 second student phase is five thousand now we have to bring to the data then s1 dot for grinding which function how designed shows student now it is going to bring to the name course that is why is one dot show SPU is going to print s1 student data means name course o phase printer later s 2 dot o student it is going to bring to our second student data okay means or own say plus less 5000 now for students seconds to enter both are displayed now the process completed thus I get CH and program also finish it is how the because how the constructor is called within another constructor whenever this one is called what happened once if now they are here and fees will become this one okay it is how to call a constructor within another constructor and the syntax for constructor calling is it is okay I am giving the syntax whenever you want to call a constructor within another constructor of same class you have to follow like this foster class name because of class in here there is no written type when there is no return Drive directly start with class name colon colon means co-operators later constructor okay in brackets arguments or parameters that's all actually it is the syntax for calling a constructor within another constructor first classname scope operator sector name arguments arguments are optional okay in arguments or option + it it is constructor overloading another example okay thank you for watching in next session we are going for some more examples thank you [Music]
Info
Channel: Naresh i Technologies
Views: 42,501
Rating: undefined out of 5
Keywords: Constructor, Overloading, Constructor Overloading, Constructor Overloading in C++, cpp, Cpp Tutorial, C++ Tutorial, Kishore, Naresh IT, C++ Training, Examples in C++, c++ Examples
Id: V9sWulrIgl0
Channel Id: undefined
Length: 16min 6sec (966 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.