Constructor in C++ | Types | OOPs in C++ | Lec-17 | Bhanu Priya

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi students coming to our next topic and the subject object-oriented programming language is construct us so what is it constructor actually it is a special function that has function name same as class name so whatever the class name is same as the constructor name so that's why we call it as a special function so constructor is a special function okay because the class name is same as a constructor name actually it is used why we are using the Constructors it is used to it is used to construct and initialize all the data members okay so that's why we call it as a constructor means we are constructing that what we are constructing we are constructing the data members so it is used to construct and initialize all the data members means the variables so whatever you are using in the program the data members that you're constructing means you are declaring and you are initializing that you call it as a constraint so the constructor name that is a function name is same as the class name so that is it's the speciality of the constructors it is automatically called when an object is created okay so automatically called the constructors will automatically call when they called when an object is created when object is created so they have no parameters and they have no return values okay so that is a constructors so let us see what is a how the stripe constructor program okay so suppose a Class C a so here the air is the class name in Texas the data members public so public a so here this is the constructor this is the constructed do they observe that here the class name is same as the constructor name so whatever the data functions that we are using that will be declared as a public so that's my public a okay with no parameters so this is a constructor so constructive can be defined either inside the class class definition or outside the class definition okay that means a constructor can be defined can be defined inside the class definition or outside the class definition so you can define inside the class definition or you can call it as outside the class definition with the help of class name how you'll call that let us see class a okay so int I is the variable public so a this is here I declare the constructor declare constructor okay so this is inside so I declare the constructor now I'm calling the constructor outside the class definition how I can call with the class name is the class name and scope resolution operator so if you want the call the constructor if any function outside the class definition you have to use the scope resolution operator okay so a is the class name scope resolution operator and the constructor so this is a here you can write the constructor definition means you can initialize the values and you can write the function what op method you have to perform in the constructor so a is the class name and this is a construction a constructor name here I'm setting the value I is equal to 1 okay so this is the way you have you can write the constructor so constructor is just Li it's just a function okay it's the main for special speciality about this is the same the class name is same as the constructor name okay so this will be automatic automatically call when an object is created so they have no any they have any number of pyramid means you can in add they have any number of parameters so now let us see the different types of constructors actually there are three different types of constructors will be there so three different types of constructors are present so let us see what other so the first one is default constructor default constructor so what we call this default constructor a default constructor is nothing but a constructor with no parameters constructor with no parameters you call it as a default constructor means the parameters will be supplied at the time of the compilation so example let us see the example for this then you will understand what is the main what is the default constructor so there here the default constructor means constructor with no parameters so here the class name okay so right now let us take the example glass cube so cube is a class name now int side side is the data member with the data type int public now I am Here I am declaring the constructor that is the cube see here with constructor with no parameters so this is the constructor with no parameters inside the constructor I am writing the value the side is equal to 10 close the constructor and close the class so this is the class now call the main function now so how will call just int main so first you enter into a main program after that it is calling the functions so here cube C so I declare an object to see okay cube is a class name and C is the object so with the help of this I'm calling this constructor with the help of the object I'm calling the constructor so just see out C dot side okay so first the program enters here it reads cube C so cube is a class which there and C is the object so with the help of the object and calling the constructor so that is a C dot side so what will be the output here the output will be 10 so C dot side what is the side value 10 so the output is 10 so this is the default constructor constructor with no parameters without no parameters here no parameters are there so that is default constructor so in this case as soon as object is created the constructor is called which initializes its data members okay so as soon as the object is created a constructor is called so shortly whenever the object is created you can call the constructor so which initializes its data members now let us see the second one so what is the second one the second type of constructor is parametrized constructor parametrized constructor so what is this parameter is the constructor actually a constructed width parameters you that are used for initializing data members you call it as parameter default constructor means with no parameters parameter is constructor means with parameters so constructed with parameters that are used refer then we can use these parameters that are used for initializing data members that you call it as a parameter S constructors let us see the example then you understand example for the parameter ISKCON constructor means the constructor with arguments with parameters so here the class again we take the cube class cube public so inside is the data members now coming to construct a cube into eggs so here I am using the arguments Here I am using the parameters so constructor with parameters you call it as a parameter is constructor so in the previous example I didn't use any arguments I didn't use any parameters so that's why it is a default constructor constructor with no parameters you call it as a default constructor and constructor with parameters is parametrized constructor so int X now side is equal to X and the class now write the main program int main so now you call the constructor by using the class name cube let us see one is the object with value cube C 2 with value cube C 3 with value okay so now print these values C out C 1 dot side C out I'm just printing the values C 2 dot sight C out C 3 dot side okay so here I'm just calling the constructor so the constructor here I declare already it is a constructor with parameters so that is the data type int so I'm calling this cube is a class name and C 1 is object with the object I'm just calling the constructor so in the previous I already said actually as soon as object is created the constructor is gone which initializes the data members so just it calling this side is equal to X so here the x value is 10 so if it is C to 20 if X is 20 side is 20 if I X is 30 side is 30 so here the output is what will be the output for this output is 10 20 30 okay so this is a second type of constructor that is a parameterised constructor now let us see the third type the third type is constructed is so this is a copy constructor so so far we discussed the default constructor parameter is constructed and the copy constructor so what is the constructed a copy constructed used when one object of class initialized initialize the when one object of class initialized the other object it takes reference to an object of the same class as an argument of the same class as an argument so here just copy concept means one object of class will be initialized to the other object just initializing one class object to the another class so you it takes reference to an object of the same class as an argument so let us see the example of the copy constructor so example for this is just class copy con copy con is a constructed means the class name i am taking as a copy con int a comma b so public now call it copy corn a constructor with arguments index comma int Y now a is equal to X B is equal to Y and here the see how twist means some initializing here is the initialization of a constructor so some statement whatever it may be so just close the copy constructor now I'm taking the void display also quite display C out here I'm entering the values that is a the new tab and the B okay now close the class so here this is a copy constructor just a class a class here I taking a copy constructor two with two arguments and display means to display this value so whatever the values that I entered here the X&Y that values will be displayed here okay so that values will be displayed here a and nm B so let us see the main program void main so this is the now call this copy corn object so use when one object of class one object of class initialize the other object so one class object copy con object I am taking as an object so that value will be 30 , 40 index int Y ok so this object now copy constructor just see the difference how I'm using the copy constructor now copy con objected to is equal to object so this object will be used in here so use when one object of class initialize the other object so one object of the class is initialize the other object this is a copy constructor here for object int X 3040 will be initialized that will be initialized to other object that is a copy con object too so that's why I have written this copy con object we is equal to object now display object dot display so first it takes this and enter into this play section and print a is 30 and B is 4 40 30 40 again object to dot display now it prints again the object to because the values are copied here for this also it brings the a value 10 and us or a value 30 and B value 40 okay so this is about the copy constructor so these are the three different types of constructors that are used in the Constructors means consider you can go for the default constructor or parametrized constructor or this copy constructor thank you
Info
Channel: Education 4u
Views: 135,402
Rating: undefined out of 5
Keywords: constructor in oops, constructor in oops with example, constructor in c++, constructor in c++ examples, copy constructor in c++, copy constructor in c++ example program, copy constructor in cpp, copy constructor c++ in english, parameterized constructor in c++ in english, parameterized constructor in c++ examples, default constructor in c++
Id: TrX3T2sdxO4
Channel Id: undefined
Length: 18min 23sec (1103 seconds)
Published: Wed Jan 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.