Constructors in C++ Programming | Default Constructor | Parameterized Constructor | Copy Constructor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to discuss about constructors in C++ a constructor is a special member function which is useful to initialize object we know that a class contains a connection of member variables so constructor is mainly useful in order to initialize values to the member variables so fine it is called yes constructor why because constructor constructs an object constructor provides values to the variables of the object so that's why it is known as constructed so the major advantage of the constructor is it is useful in order to perform initialization of an object there are some important points regarding a constructor let's see all those important points the first important point is the name of the constructor must be same as the class name let's take this program here the class name is Sam be constructed name is also savvy let's take this program in this program class name is not various constructed they use on Sunnah so the first important point of constructivist constructor name must be same as the class name constructor is nothing but a method only a function only so that's why it contains some arguments left power the system analysis here the argument we see is empty here we have some arguments unknown X is the second important point of the constructed there is no need to call the constructor explicitly with the help of an object whenever an object is created then automatically the corresponding constructor will be called so whenever an object is created then the corresponding constructor will be called so there is no need to call the constructor explicitly with the help of an object so that is the second important point regarding a constructed and the third important point is constructive must be be carried or defined under public section so here we have public section so under the public section only we have defined the constructor so here also under the public section only we have defined the consented here also under the public section only we have defined the constrictor why because whenever an object is created then automatically is the corresponding can second will be called suppose if that constructor is defined under private section then we wrote that private members cannot be accessible from outside the class so it is not possible to access that constructor so we have to declare on define the constructor and at the public section and one more important point is constructive do not return any value so there is no need of any return time before the constructor here before the hell state there there is no reader of any written type like int float cat that would not even why is also not needed so here you should not specify void also so constructor do not retain any value so there is no need of any written type before that constructor so we should not specify even void also so these four are the important points about the constructive now let's see about the types of constructors in C++ in C++ constructors are mainly classified into three types first one before consented Syrian one parameter is defense effective thermal copy constructor before constructive parameter as the constructive copy constructed let us discuss these three concepts one by one let's see the first one default and sector the constructor without arguments these gordias default constructor so be so the default constructor should not contain any Oh meters let's take this example here we are using aversive knowledge why because in this program we are using co2 object co2 object is available I was REME got hitched here we have the class name of the class he shall be so primate codon in PA comedy so under credit section we have two variables a and B next under the public section we have a constructor as well as a function so what is the name of the constructor class name in this example the class name is Safi so sample is the constructed we have this is an example for default constructor default constructor does not contain any parameters so here the argument you see is empty so why we are using constructive in order to perform initialization to the variables in order to construct the object so here what are the variables of the class EAB so a value 10 B value 20 so we have performed some initialization inside that constructor next we have defined a method function a member function display so see how class that means then slash in K is equal to a less than less than / and B is equal to B so we have printed a and B values inside display method next class iteration is over we know that class declaration yet switch semicolon and X this is nothing but our main method so return type of the main method is integer so here the last statement is written to you if the program is successfully compiled and executed then it returns a value for 0 next year we have created two objects what is the name of the class sample so sample Rhodesia 1 comma go be simple we know that whenever an object is created then memory will be allocated to the variables of the class so what are the variables of the class a and B so whenever were busybodies created their memory will be allocated to and be likewise wherever an object is created so we have one more object Buddha's into so whenever an object is created then memory will be allocated to the variables a and B next here we have a constructed whenever an object is created then automatically the corresponding constructor will be called so whenever this object is created then automatically control goes to the constructor so whenever go b7 is created controller goes to the constructor saying yay can be destroyed in P xx will be stored so likewise whenever go visit to is created then automatically that responding constructed BB chord so impervious a two-year can will be stored in were busy to be front even miss stoper next both is a 1 not display so control goes to the displaymessage so in the movies a one object what is a value 10 so a is equal to 10 B be printed and after that here we have slash it so cancer we places in the new name so B is equal to 20 will contain okay next word is a 2 dot is played so control goes to the displaymessage invoke easy to also what is a man a value mistake B value is 20 it will be green yet okay here if you don't define any default constructor then automatically the compiler will defines the default constructor and provide some default initial values for the variables let us assume is you that in this example we'd only have this constructor we don't have this constructor we identify this kind second then also what we happen is the compiler implicitly defines the transected and provides initial value C as zeroes so inhale here 0 will be stored in B 0 will be stored here also in a 0 will be stored in B 0 understood now a when you become 0 B value becomes you a value becomes 0 B value will become 0 so this is about what is a default constructor so what is default constructor that we thought on humans is called as default constructor so what is the problem with the default constructor here for the first object for the second object if we create any number of objects then for all the objects the output should be the same so for object 1 the content is 1024 object to the content is 1024 object tree the content is 1024 object for the content is 1040 likewise let us assume that we didn't define any constricted then for all the objects the values of 0 and G so that is the problem here we can work on this problem with the help of the parametrized constructor so with the help of the parameter is you construct a sari for different objects we can provide different values let's see what is the parameter is the constructor a constructor with parameters this gordias parameter is the constructor so any parameter is the constructor must accept parameters let us say this example here name of the classes and now so under the previous section we have a and P and at the public section here we have we can say it but this constructor transfer contains parameters the first parameter is I the second parameter is chip next EA is equal to IB is equal to J next this is nothing but display method with the help of display method we are displaying a value and be done next this is nothing but our main method here we can call a parameter s the considered get into this the fast way is class name space object of values this is called as implicit constructed card this is coria's implicit constructor call let's see this example here the class name is num next object let object name is Ian one of how many values we have to probably two integer values 10 comma 20 so this is Correa's implicit constructor calm way as the second car is like this class name space object is equal to name of terra meters so here what is the class name now space that object name is m2 so now space m2 is equal to na all that P comma 40 so this is called as explicit constructor called so fast this statement is executed so so you know whenever an object is created that memory will be allocated to the way member variables of the class so in our class we have two member variables a and B so far a and B some memory will be allocated next whenever an object is created then automatically the responding constructor will be caught so control virtually constant care we should not call the constructor explicitly whenever an object is created then automatically the corresponding constructor will be called here can for me so can we be master - hi 20 will be possible chief so now what we have in J is equal to I what is it man it so even you will become a cat the value will become 20 next now m2 is equal to knock off that P comma 40 this is called as explicit constructor call so whenever your object is created then memory will be allocated to the member variables of the class class now contains two member variables a and B so funny and be some memory will be allocated next these two values will be positive now I will become party J will become 40 so ya is equal to IP z / nu J so now even you will become 30 B value will become 40 n1 dot display so what is n1 and Raven you didn t value 20 so Camden goes with display method so a value take B values 20 will be written next gen 2 dot display so control goes to the displaymessage so in the end to object what are the values a value 30 and B value for T we could indeed so here what we have to do object 1 contains values as 10 and 20 object 2 contains values Y hasta en 40 so for 8 object we can have its more values various coming to the default constructor all the objects should contain same values now let's see the farm but that type of constrictor is copy constructor copy constructor means creating a new object from world object so if you want to create a new object from world object then we have to use as the copy constructor so here what we happen is now the new object contains the values of the world object suppose if the world object contains two variables let those values are 10 and 20 now those two values 10 and 20 will be copied to the new object now let's see this example here net name of the class is now let me have a never visible would leave a member variable and at the private section that is he under the public section we have a parameter is a constructor so if you want to implement a copy constructor then we should have either before we constructed or parametrized constructor why because what is copy constricted we need to create your object from the existing object so before the copy constructor we need to help another object okay it may be without constructor or by creating a normal object also we need to provide some values okay here we have our parameter is constructed so what is the parameter is against it it is accepting and bigger value so now off in tight so a is equal to I so this is nothing but copy constructor so this is the syntax for the copy constraint the class name of class name am percent would be C so this is the syntax for a copy constructor here what is the class name now is the class name off what is the gas thing nun is the class name damn percent net object is what we say so we have what we are doing J is equal to this movie say da da y is equal to movie set out yet in this dilemma that simply we have displayed the value of a we know that execution starts from a main function so now what we say one of ten this is nothing but parametric inspector car so control goes to the parameter is inspector so what is a value 10 so what is the object more visible is the object name so whenever your object is created that memory will be allocated to the variable of the class so this is nothing but parameter is the constructed ha so under goes to the parameters you consented so what is the value 10 so can we be muscle to I now a value will become 10 now our target is we want to create another object called vulpes they took from Roby save one okay so for that purpose we are using the statement now now Bogey's a to space where we say two off road is a one so if the syntax is very simple class name new object off world object new object off world object so here what is a new object will be set to OFF what is the world object will be same fun so whenever the statement is executed then the control goes to the Coffee inspector so what is hope is a one here Bogey's m1 has a variable called yet that a has a value called temp so that will be copied to the movie say this is nothing but different variable we know what is a reference variable the reference variable is an alias mean for the existing variable okay so what we have enough would be there would be this word to say is also point to the whole being fun so if you perform any modifications or movies ever then it will be effect and reflected on what is it likewise if you perform any modifications on will be cheap it will be reflected on what we say from a is equal to VZ not here so what is what is a dot G the cannelloni okay now in boobies in also in a we have F so now we have an object called body shape so in what we say we have a variable called here that they contain stem then after that control go comes to the statement now this will be say content will be copied to so now in vogue easy to behave ah we have a variable called yet which contains step so what is a tune or display control goes to the displaymessage so what is a where you can be printed so if you want this statement can also be written as now what we say 2 is equal to go be same path now what we say 2 is equal to go the same fun ok so whenever this statement is executed then the content of rupees in 1 will be copied to the book visit so thus is the same statement can be written as like this also so this is about different types of constructors in C++
Info
Channel: Sudhakar Atchala
Views: 2,470
Rating: undefined out of 5
Keywords: c++, programming, procedural, object oriented, oops, general purpose, high level, compiler, execution, introduction, basics, fundamentals, lectures, interview, languages, cpp file, c file, byte code, case sensitive, constructors, default constructors, parameterized constructors, implicit call, c++ constructors, constructor overloading, sudhakar, atchala, copy Constructor
Id: y4eQXKi3u7M
Channel Id: undefined
Length: 17min 22sec (1042 seconds)
Published: Fri Feb 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.