 Hello everyone, I'm Mrs. Meenakshi Shreya Gandhi from Walton Institute of Technology, Sholapur. Welcome to the video lecture on parametrized constructors. Learning outcome, at the end of this session, students will be able to explain and write program using parametrized constructors. What are constructors? Constructors are nothing but special member function whose task is to initialize the objects of its class. It is called as special because its name is the same as the class name. So, in this example, we have a class called as number and there are two data members A and B. And under the public section, we have a constructor. Here we can see that the name of the member function is same as the class name. That's why it is called as a constructor. So it is constructor is declared here and the constructor is defined here. The role of the constructor is to initialize the data members A and B to zero. Now here we can see that number, this number is indicating the class name and this number is indicating the name of the member function that is nothing but a constructor name that is number. In the main function, we are declaring the object N. So the constructor is invoked whenever an object of its associated class is created. So here in this example, when the object N has been created, the constructor gets invoked automatically and the data members A and B are initialized to zero. It is called constructor because it constructs the values of data members of the class. Need of constructors? If a normal member function is defined for zero initialization, we need to invoke the function for each object separately. This won't be inconvenient if there are large number of objects. Let us consider an example. Here we have a class called as number where there are two data members called as A and B. Under the public section, we have a member function called as get data. Here the member function has been declared and here the member function has been defined. The role of the member function is to take the values for A and B from the user. The five objects has been created, that is N1 to N5. If suppose I want to initialize data members A and B for all the objects, then I have to call the get data function separately by using each object. Like here I have done, I have called the function, the get data function by N1. It will take the values of A and B for object N1. Similarly, by using object N2, I'm calling the function get data, which will initialize the values of A and B for object N2. So if there are 100 objects, the same process has to be repeated for all the 100 objects. And this is very infeasible. So for a constructor, there is no need to write any statement to invoke the constructor function. So when the object for the class has been created, it initializes its data members. So for a constructor, when we are using constructor, there is no need to invoke the function separately by using the object and the dot operator. So a constructor is invoked when the objects are created. That's why when the objects are created, automatically the constructor gets invoked. This can be explained by using an example here. So here we have a constructor called as number, here it has been declared, and the constructor has been defined here. So it is initializing the values of A and B to zero. So the constructor is invoked when the first step, that is the object creation happens. So if there are five objects has been created, five times the constructor gets invoked, and it will initialize the data members A and B equal to zero for each object separately. Let us see the characteristics of constructor. Before seeing the characteristics, let us see an example. So we have a class called as sample, where there are two data members A and B. And under the public section, we have a constructor called as sample. Here the constructor has been defined, which initializes the data members A and B equal to zero. And in the main function, we have only created the object for the class called as sample. Now the first characteristic is that they should be declared in the public section. So this can be explained here. The constructor has been declared under the public section. They are invoked automatically when the objects are created. So as it is shown here, when the objects are created, the constructors are invoked automatically. They do not have written types, not even void, and therefore they cannot return values. So here you can see a constructor is not having any written type, not even a void. That's why a constructor cannot return any values. They cannot be inherited, though a derived class can call the base class constructor. Let us see the types of constructors. There are four types of constructors. Default constructor, a constructor that accepts no parameter is called as a default constructor. Then parameterized constructor. The constructor that can take arguments are called as parameterized constructors. Do nothing constructor. The constructor that doesn't have any arguments and contains empty body and does not do anything and which is used to satisfy the compiler is a do nothing constructor. There is a copy constructor. The constructor, which is used to declare and initialize an object from another object, that is, copy the values of one object into the another object of the same class is called as a copy constructor. Try to think and answer, usually when the parameterized constructors are used. Pause the video for some time and note down the answer in your book. Parameterized constructors. The constructors that can take arguments are called as parameterized constructors. This is used when it is necessary to initialize the various data elements of different objects with different values when they are created. So here it shows a class called as a and there are two data members m and n. There is a constructor a, which accepts two parameters x and y and here it is initializing the data members m and n with value of x and y. In the main, when the object a has been created, this will be not be sufficient because here the constructor accepts some parameter is not a default constructor. So only object creation will not work. This is an incorrect declaration because in this case, the constructor is accepting some parameter, so only object creation will not invoke the parameterized constructor. So this statement will not work. So the correct statement to invoke a parameterized constructor is this way. That is when the object has been created, we need to pass some parameters. So in this case, we have passed two parameter 10 and 20, 10 and 20 gets passed in x and y and those are assigned to m and n. So this is a correct declaration. So similarly, our object has been declared that is called as c and here again, the parameters are passed. So the parameterized constructors are invoked again with new values that is 30 and 40 and m is initialized, m and n are initialized with 30 and 40 for object c. Let us see an example using parameterized constructor. So here we have class called as number and there are two data members a and b. And under the public section, we have a parameterized constructor called as number, which accepts two parameter and it initializes the data members a and b with value of x and y. So parameterized constructor, this is a parameterized constructor and there is one more member function called as mean. The role of the mean function is to find the mean of the data members a and b. It will find the mean and it will display the result. So in main, the first step is to create the object n. So this invokes a parameterized constructor by passing two values to it that is 12 and 24. Now the parameterized constructor will initialize the data members a and b with this value that is 12 and 24 for object n. After that, the object n is invoking the function mean. So mean function gets invoked where the data members of object n that is a and b for this values of a and b, the mean has been calculated and the result has been displayed. Let us see one more example using parameterized constructor. So we have a class called as area. Under this, we have length and breadth as data members and under public section we have a constructor parameterized constructor called as area, which accepts two parameters that is x and y, which initializes the length equal to yx and breadth to y. After this, we have one more member function called as display. The role of the display function is to find the area of rectangle and display the result. In main, first the object has been created and the two parameters are also passed. This invokes a parameterized constructor, which passes 14 to x and 24 to y. This initializes length and breadth to 14 and 24 for object a. After that, object a calls the display function by using the dot operator. This invokes the display function where the area of the rectangle has been calculated and it has been displayed. These are my references. Thank you.