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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to narration this is Kishore and today we are going to discuss about construct this concept already we have discussed that how the constructor is working what is a constructor and how this one is working now I am going to continue the constructor concept and in this session I am going to cover how to overload a constructor okay first of all lower loading is what defining defining several things or objects or functions okay defining several things or functions or objects okay with the same name with the same name by changing the number of arguments order of arguments type of arguments within one class okay here I want to say like for example what is called constructor overloading okay are what is called function overloading defining several functions with the same name defining several functions with the same name in the same class by changing the number of arguments or the rough augments type of arguments is called generally function overloading is called function overloading now constructors also participating in overloading that means within a class we can define any number of constructors okay here constructor overloading means what defining several constructor defining several constructors within the same class by changing the number of arguments order of arguments and type of arguments okay next actually constructor overloading is nothing but a polymorphic feature of C++ okay actually this behavior is nothing but polymorphic feature of C plus plus actually this concept tell us to extend the functionality of the constructors other we know that one constructor is mental mentioned for one work by using overloading means what we are using the same constructor name to do different class that is called extends that is called extensibility besides constructor overloading allows the user to extend the functionality of constructor and it is called what polymorphism it is called constructor polymorphism that is why constructor overloading is nothing but the polymorphic behavior of conciseness now how to overload the constructor okay first I am going to start one example this watch it first a common header file hash include iostream next hash include corner root rich now I am going to start a class like this class or kills ok now I am going to declare class generally class is having radius circle is having radius float are now every circle contains some radius next here wild ok and here one important point we have discuss what is our concept constructor overloading actually there is a rule for constructor what it is means every constructor should be declared in the public area ok every constructor should be declared within the public area and constructor never contains any return data type and return value including void means even while that is why don't use the void ok so don't use the why and constructor should be declared where in the public that is why here public now public and this is okay our public visibility more level next he the constructor major rule is what constructor name should be matching V to the class name actually class name is what circle that's what our function name should be same now circle and now it is called what constructed and already we have discussed two types of constructors Boni default constructor second one is parametrized constructor a constructor without any argument is called default constructor and a constructor with parameters is called parameterised constructor now I want to use both in this class now such is here breakage MD brackets generally empty brackets indicates what there is no argument when there is no argument generally it is called default constructor no it is the default constructor next here I want to assign some value or equal to five not it now brackets close here what happened are is initialized with the fight that's why when the object is created okay when the object is created this object data means especially our value becomes that is why constructor is usual for what automatic initialization of the object and it is initialized when the object is created that's why when this object means whenever we are declaring an object's from circle class now that object R becomes Phi due to the default constructor next I want to use one more constructor parameter I is the constructor now I am going for parameterised constructor once again circle this time float are once again now watch this here class name is circle here function name is circle that is why once again it is a constructor but it is having a argument that side is called what parameter Iser constructed now the parameter name is what R and data member name is what R that means here one is local variable one is data number already we have discussed when local variable and data members are with the same name your compiler doesn't recognizes which one is which one and it gives priority to local variable only and I said in this situation we should have to use this pointer okay to distinguish our data members from local variables okay to distinguish the data members from local variables we have to use this point now I am going to say like star this dot R equal to R no modulus what happens here this R is the argument or means local variable and this means objected or object means what class variable that means object is the instance of the class now that is why this R is nothing but this R okay that is why here this R is the local variable an argument and this R is nothing but data member that's why this value can show to this value if this argument value Pasir to this data member now watch it it is what default constructors and it is what parametrized constructor okay here it is the default constructor it is the parameterised constructor i have used both within a single class now it is nothing but constructor overloading the such constructor overloading is nothing but declaring several constructors declaring more than one constructors okay within the same class it is called constructor overloading it is a polymorphic behavior of C++ okay next this one allows to use the constructor for different purposes okay Lex now I want to bring to the data then void show another function here I want to print or I want to find out area then area equal to generally to find out the circle area formula is what PI R square to find out the circle area the formula is what PI R square so here R is available pi value we know that 3.14 that is why area equal to 3 point 1 4 into R into R later ndele now function closed now what happens whenever this one is called when our show area is called it is going to print the circle area by using this form okay now function definitions are completed close the class ok here closing of the class later we have to call this member functions but here on important point is that constructor is never invoked because of it is invoked automatically when the object of that class is created ok normal member functions are invoked using object name followed by dot operator okay everybody knows if suppose show area show area is what it is a member function and the rule for member function is what it should be accessed with the class object using dot membership operator but it is what constructor it is also what constructor we know that we never we never access a constructor using the object name using dot membership operator because of their invoking automatically when the object circuit that is why now I am going to use in main series how to use me wide main now CLRS see our screen is clear now I have to invoke a faster default constructor now circle c1 now watch this is circulate what class name here circle is the class name and c1 is what object and here this object is having any argument no when there is no argument means automatically it is linked with the default constructor that's why object without any argument object without any argument it implicitly linker with the default constructor okay now here C 1 C 1 and C 1 is cor declared automatically C 1 R becomes 5 that is why here just cerium it is the C 1 object and it is having ARMA now it becomes automatically fire next three circle of C 2 of 10 now what happens another object created here C 1 C 2 both are what objects and it is also object and we know that whenever an object of class is created constructor in vocal implicitly are automatically that's why when C 1 object created C 1 invokes the default constructor because of there is no argument but here C 2 C 2 is having what some argument that is why here there is no argument of the so it is not linker this one these having argument now both are matching that is why now the 10 is received by R and this R becomes R that is why here C 2 objects also created and this R becomes 10 okay say this here we are automatically the object values are initialized that is why this concept is called what automatic initialization of the objects okay now C 1 value C 1 R value 5 and C 2 R value 10 finishes later we have to print is a alien that is why here I am going to write like this C 1 dot show area actually C 1 means this 1 C 1 dot show area now R value 5 5 into 5 into 3.14 something is going to print area let's see 2 dot show area now it is going to print what C 2 area based on 10 that is why it is going to put into the circle one area it is going to bring to the circle two area now the program's finish with H one program is closed now in this example what happened we have declared two constructors we have declared two constructors one is default constructor one is parameterised constructor that means we have used more than one constructor within the same class and this concept is called constructor overloading but I declaring several constructors okay declaring several constructors by changing the number of arguments our argument datatypes our order is called constructor overloading okay thank you for watching in this session I am going to give another example on constructor overloading how to call a constructor within another constructor thank you [Music] Oh
Info
Channel: Naresh i Technologies
Views: 65,228
Rating: undefined out of 5
Keywords: Constructor, Overloading, Constructor Overloading, Constructor Overloading in C++, cpp, Cpp Tutorial, C++ Tutorial, Kishore, Naresh IT, C++ Training
Id: OmqTPFCdC-w
Channel Id: undefined
Length: 14min 39sec (879 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.