Constructor Introduction, Default Constructor in C++ | C ++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to nourish 80 this is Kishore and today we are going to discuss about c plus s constructors and destructors okay we are going to discuss about constructors and destructors now what is called construct and what is called destructor if you are going to discuss constructors and districts topic now I am going to give one small example first of all why we need the constructors okay the first of all I am going to show you one example later I am going to player the constructor and destructor topic now watch this example suppose there is a class okay hash include iostream now H later some class test later int equal to 10 comma B equal to 20 now watch it here a B actually they are called data members under here a value positive with 10 and a p-value initialized with the 20 now the problem is C++ compiler never allows this kind of initializations because of data members especially private data members ok the data members of a class should be should be initialized through the member functions of same class okay actually in C++ there is a rule what is that means the data member should be initialized through the number functions of same class which is called at a hiding concept okay but actually we are sending or initializing the values that that is why this concept is not allowed in C++ next now how to initialize this then suppose here you are going to declare like this int a comma B okay later you are creating one object of this one wild main suppose here test d actually test is the word class name T is the object name and here you are sending the values like this ten twenty once again it is giving error what is there a testicle on : e not accessible text : : B naught X because of and we are the private members and private members are initially is not initialized directly there should be issuance to the number of functions and there is another alternative what it is you have to declare a comma B as public okay you how to declare public and now when they are public public numbers can be accessible from anywhere public members can be accessible from anywhere in our program that's why this concept is we allowed but it is against data hiding concept actually simplex is main concept is what data hiding means private data should be accessible with the member functions and means it is not available outside the class when it is public they are not secured that's why we are not having any data security means the hiding concept society is against data hiding next suppose you are going to use nothing int a comma B this time suppose you are going to use like this Co th co to be actually they are giving what garbage values because of there is no initialization okay whenever there is no initialization the data members are having garbage values that's why without initialization the values will become always now here the point is I want to initialize the data members without using a member function means without calling a member function for example here I am going to write a function like this suppose in public area wild get here equal to 10 B equal to 20 now C out a and B okay the flame giving this example now what happens get is the member function and actually the rule is what with through the number function we can access the private data that is why here a and B are initialized with 10 and 20 suppose here it is the main function we know that in every function we are having three bars function declaration function calling function definition actually it will become function declaration and definition now calling liquid when calling is their first you have to declare object of that class okay test it let us T dot gate that means here that member function is in what are called when this one is called program goes to here and a value becomes 10 B value becomes 20 and they are going to be print suppose this line is missing this line is missing what happens now this number function is not called when it is not called meter and we are not initialized that means and we are having garbage valves now without calling a member function in this example without calling this number function a B values are not initialized but I want to make it automatic means without calling member function I want to initially we end the values then how it is possible means only through the constructor only through the constructor because of constructor is a special member function was it constructor also function ok constructor also function but it is a special member function which is easy to initialize the object which is easy to initialize the object itself and which is called automatic initialization of object ok the says constructor is a special member which is used to initialize an object itself and this process is called automatic initialization of object now what are the and what are the benefits of constructors okay next here the first point when constructor it user data members are automatically initialized and when they are initialized when the object of that class is created okay now here the point is T is what object when the object is created automatically constructor is invoker and the data members are initialized for this what are the rules we have to follow okay the first rule for constructor is the name should be equivalent to class name the name should be equivalent to class name for example here watch it our class name is what tests and data members a and B now in previous example I have used void gate actually class name dist member function name gate that means there is no relation but here for construction the first tool is what the constructor should be similar to the class name because of constructor meaning is what it is a special member function it is a special member function whose name is similar to the class name and which is easy to initialize the object members that is why here dist okay like it's close now watch it here class name is list and member function name also test and here watch it here there is no wide or integer or float because of there is another rule for constructors what is that means constructor never returns any value our constructor never have any return data type including void okay including what it is the another important rule for constructed first tool is what constructor name should be similar to the classic and second rule constructor never having any written value and it returned data type including void including void that's why do start with void or integer or anything just start with this one okay fine it is the second okay fine next a value 10 and B value 20 no brackets close in my example it is called what constructor okay it is the constructor next here what happens suppose we are going for main function while the main okay in main function CLRS Sarah how to play next test ''tis here test is what class a and B is the object name test is a class named tiers object now the object is created when this object is created this constructor function automatically initialized means it is automatically called now what happens a witch a TDOT a becomes 10 and B becomes 20 that's why here this is it is the key object and it is having a B now a value B constant B value becomes 20 and watch it here we have called any function no just to only the object is created that's why the most important feature of constructor is constructed in work color automatically when an object of that class is created when an object of that class is created that's why this constructor automatically executed a value constant B value becomes 20 now here watch it it is the public area it is the private int and suppose you are removing public now your program use is a this problem gives error because of there is another rule for constructor what is that means constructors and destructors both should be declared in the public area remember this when they are private they are not invoker and they will give errors that is why constructors and destructors should be declared in public next another it is a constructor name and here there is no argument okay that means constructor without argument reload and constructor with arguments for soil plus y here constructor may are may not have arguments or parameters that is why it is possible to overload the constructor that means constructors are participating in overloading did another important point next constructors okay constructors never participate in inheritance they cannot parcel it in inheritance it is a important another rule the Constructors never participated in hell and we are not able to find out the address it is another important we are not able to find out the address of a constructor and they will make implicit cause they will make implicit calls to new under delete operators we know the dynamic memory allocation okay when you and delete operator for user constructors are invoked automatically this way they will make implicit calls to new only delete operators next another important thing constructors also having default argument okay next another important thing generally when it is a member function we can call any number of times we can call any number of times but constructor is linker with what object name that's why here constructor is executed only once it is the most important thing constructor is executed only once when the object is created further callings are not load okay the so we can use a constructor only once in entire class when it is when the object is created and further callings are not alone suppose it is normal function we can call any number of texts but it is not possible for constructors okay because of their lingual with objects and another important thing when it is normal function we are going to call with object name dot operator with the dot operator okay for example previous example so we'll get function T dot get we are using but here whenever it is a constructor do not use the object name followed by dot membership operator they never invoke over to the object name they never invoked with the object name because of they are automatically executed no in this example these are the rules and regulations and constructors why we need means they are used to initialize the object itself and second one it provides the resources for the object members okay and there is no need of calling the constructor member functions okay and next they are participating in overloading that means we can declare any number of constructors within a class by changing the number of arguments by changing the number of arguments data types and order okay next the side is called constructor overloading okay and another rule is what constructor should be declared within the public area and they do not have any written data type including wide are any return value okay constructor never it is and this should be declared in public here only when it is private they are not in occur because of private members are not in vocal from main function it is the problem okay and when they are going to execute it and the class object is created generally creation is also called construction that's why when they are going to and they are going to execute when the object of that class is constructed that is why they are called constructors an X in people express we are having three types of constructors one is default constructor second one parameterize reconstruct third one copy construct we are having three types of constructors default parameterised copy constructor and in default constructor before than sector means what a constructor without any argument or in zero parametrized constructor a constructor without any argument for examples it is test there is MD brackets means here there is no argument now it is called default constructor yes like a constructor without any argument is called default or a 0 parameter I easily see this Jairam is nothing no parameters the so it is also called default and we are having two types of constructors default constructors one is what it is system defined okay our system created one second one user-defined okay Owen is compiler written one is compiler written means compiler creates the constructors and second one is the user-defined ok for example what is compile are defined suppose here I am going to explain with for example series now you have declared a class like this class test AV public void get something so watch this in this class there is no construct there is no constructor now you have to compile this now okay when this program is compiled the compiler watches any constructor is defined on no constructed immediately it creates a constructor inside the class without any Co remember this okay now after compiling this program your compiler creates a new class without any code with constants means it a constructor I know it is called compiler defined or compiler written constructor that's why a class without any constructor after compilation is having a default constructors which is called compiler written or compiler define second one is what user defined now I will show you what is it user defined okay now watch this here class test two data members now I am going to declare a constructors now it is called user-defined default constructor and Here I am going to enter a value 10 B value 20 next I want to print this data now wide show here see out a equal to a and and 3 out B equal to B and and class close another point it is the default constructor it is a normal number function now what happens testa t.t is not object of this class now object created when this object created automatically this function in worker a comma B we love becomes 10 and 20 now a value 10 B value point later t dot show now get CH program close indeed of show is called what happens it is going to print the a and the B value that is why here it is going to show a and B values now it is created by the user now that is why it is called user-defined default constructor it is a user-defined default constructor that is why we are having two types of constructors one is what system written which is created by the compiler at compile time whenever the class is not having a constructor second one we have to define that is called default constructor it is the example for okay use the redefined default constructor now what is a parameterized constructor inefficient we are going to discuss what is a parametrized constructor okay thank you for watching [Music]
Info
Channel: Naresh i Technologies
Views: 160,316
Rating: undefined out of 5
Keywords: Default Constructor, Introduction to Constructor, Constructor, Constructor in C++, C ++ Tutorial, cpp, Kishore, Naresh IT, Default Constructor in C++, Constructor Introduction
Id: PVrnazUTMm4
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Tue Apr 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.