 Hello everyone, I'm Mrs. Meenakshi Shreya Gandhi from Walshian Institute of Technology, Sholapur. Welcome to the video lecture on copy constructor. Learning outcome, at the end of this session students will be able to explain and write program using copy constructor. What are constructors? Constructors are nothing but special member function whose task is to initialize the objects of its class. These are called as special because its name is the same as the class name. This can be best illustrated by an example where we have a class called as number and there are two data members a and b and under the public section we have a member function here you can see that this member function name is same as the class name so this is called as the constructor so the constructor has been declared here now here the constructor has been defined the role of the constructor is to initialize the data members a and b equal to zero in the main we have created the object n for the class number the constructors are invoked whenever an object of its associated class is created so in this case the constructor number is invoked when the object n has been created so when object n has been created automatically the constructor number gets invoked and the data members a and b will be initialized to zero for the object n it is called constructor because it constructs the values of data members of the class so when the constructor is invoked whatever data member values are there those values will be initialized for the objects that's why it is called as constructor. Let us see what is the need of constructors if a normal member function is defined for zero initialization we need to invoke the function for each object separately this would be inconvenient if there are large number of objects so in this example let us see this we have a class called as number and two data members a and b and the public section we have a member function called as initialize so the member function has been declared in the public section and the member function has been defined outside the class the role of the member function initialize is to initialize the data members a and b with value 10 and b equal to 20 and in main function suppose for the class number we have created five objects that is n1 to n5 and each time I want to call the function initialize now every time I'm calling the function initialize by object n1 n1 dot initialize gets called and the values of a and b will be initialized to 10 and 20 for object n1 for n2 again the the function gets called and the values of a and b will be initialized to 10 and 20 for object n2 and if suppose if I have 100 objects and every time I want to initialize the data members to some values for all the objects every time I have to call the initialize function separately by using each object separately and this is very invisible so for that we are using constructor so for a constructor there is no need to write any statement to invoke the constructor function so when the objects for the class has been created it initializes this data member so for constructor whenever a constructor has been defined there is no need to call the function separately by using object and the dot operator that is the main advantage of using constructor this can be best explained by declaring a constructor in the program so let us see that so here you can see that the constructor number has been declared because the name of the function is the same as the class name so here is the constructor which has been declared and we have a constructor which has been defined here the role of the constructor here is to initialize the data members a and b equal to zero and when the objects are created that time the constructors are invoked automatically and the values of a and b are initialized to zero for all the objects let us see the characteristics of constructor before seeing the characteristic of constructor let us consider an example where we have a class called a sample with data members a and b and under public section we have a constructor sample the constructor sample has been defined outside the class which initializes the data members a and b equal to zero in the main function we have declared the object for the class called a sample the first characteristic is there should be declared in the public section now here you can see the constructor has been declared or can be defined under the public section they are invoked automatically when the objects are created so when the object s has been declared the constructor is invoked when the objects are declared so it gets invoked and the data members for a and b are initialized to zero for object s they do not have written types not even void and therefore they cannot return values so in this the constructor do not return any values they cannot be inherited though a derived class can call the base class constructor let us see the types of constructor there are four types of constructor the default constructor a constructor that accepts no parameter is called as a default constructor parameterize constructor the constructor that can take arguments are called as parameterize constructor 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 called as a do nothing constructor and the last type of constructor 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 copy constructor try to think and answer usually when the copy constructors are used so the copy constructors are usually used when we want to declare and initialize an object from another object as the name suggests here we copy the values of one object into the another object of the same class let us consider an example where we have a class called as a and there are two data members mnn under the public section we have a parameterize constructor where it accepts two parameter and the values of mnn are initialized to x and y and we have one more type of constructor which is nothing but a copy constructor which has been defined inside the class a copy constructor usually accepts the objects as parameter and it initializes the values of mnn with mnn of another object firstly we are creating the object a1 which by passing two parameters that is 10 and 20 this will invoke the parameterize constructor and mnn will be initialized to 10 and 20 for object a1 now here we are created the object a2 by passing object a1 here the copy constructor gets invoked here a1 is now copied to object t and using t.m and t.n we are trying to access the data members of object a1 which are copied to data members mnn of object a2 so this is where we are invoking the copy constructor so here the object a3 has been created and here we are assigning object a1 to a3 this will also invoke the copy constructor so here a1 will be copied to object t and by using t.m and t.n we are accessing the mnn of object a1 which are assigned to mnn which are the data members of object a3 let us consider example using copy constructor so here we have a class called as number and we have two data members called as a and b and under public section we have a parameterize constructor where it accepts two parameters x and y which are initialized to a and b we have one more constructor this is nothing but the copy constructor which has been declared here and we have one more member function called as sum here the copy constructor has been defined here the member function sum has been defined where it finds the sum of the data members a and b and the result has been displayed in the main function when object a has been created a parameterize constructor is invoked by passing two parameters to it that is to valent 24 to valent 24 will be assigned to data members a and b of object a when object b has been created a copy constructor is invoked because the object a has been passed as a parameter now this object a will be copied to x and by using x dot a and x dot b we are trying to access the data members of object a that is 12 and 24 and those are assigned to a and b of object b now here this will also invoke the copy constructor by passing the object a as the parameter to the copy constructor and again by using x dot a and x dot b we are trying to access the members of object a which are initialized to a and b of object c now here we are trying to find the sum of the data members of object a by calling the function sum which will find the sum of a and b in this case 12 and 24 are added and the result for object a has been displayed so the result will be in this way so the sum for object a is 36 will be displayed similarly for object b the sum function has been called and the result will be displayed in this way same for object c by using object c the sum function will be called and the result will be displayed let us consider one more example using copy constructor so we have a class called as coordinate and there are two data members x and y and we have a parameterized constructor with two parameters here it is initializing x and y which are the data members with value of m and n we have one more constructor in this case it is a copy constructor which has been declared in the class and we have one more member function called as display now here the copy constructor has been defined here the display function displays the coordinates of x and y in the coordinate form in the main function object one has been created by passing two parameters that is 36 and 48 here it invokes a parameterized constructor and x and y are initialized to 36 and 48 for object one here the object two has been created so here it will invoke the copy constructor because object one has been passed as a parameter now it will initialize the data members of object one to the data members of object two after that the coordinates of object one are displayed by calling the function display by so here the coordinates of object one are displayed in this way those are 36 and 48 and similarly the coordinates of object two are also displayed by calling the display function by using object two so the the coordinates are displayed in this way that is 36 and 48. So these are my references. Thank you.