Hi this is Anil and welcome to the video tutorial for the learning Lad on C plus plus programming.
so in this tutorial we are gonna learn about the copy constructors in C plus plus.
so first of all what are this copy constructors.
A copy constructor is nothing but overloaded constructor.
You know a we have learnt how to overload the constructors to take different different
parameters and different types of parameters.
So this copy constructor is one of them.
And the next thing is what is the use of this copy constructor or why we have to use this
copy constructor.
so before explaining anything I want to show you the problem that is gonna arise if you
don't use the copy constructor.
so here just for the demonstration purpose, you know I have already created a class in
my program, and the class name is Person and we have two public members called name and
age and then we have a constructor called person you know which is gonna two parameters
and its gonna initialise the class members.
And then we have a method called change Name and age you know which is gonna two parameters
and its gonna change the values stored in this members.
You know its gonna change the name and also its gonna change the age and then we have
introduce method you know which is gonna print out the value stored in this members.
All Right simple enough.
The next thing that we are gonna do is for the demonstration propose, i'm gonna create
an object of this class person.
so here in my main function i'm gonna write Person and lets say Anil and we are gonna
Pass 2 parameters.
first parameter is gonna be the name Anil and then the age 24.
The next thing that we are gonna do is we are gonna call the introduce method.
So its gonna be anil dot introduce.
so here you know the value Anil will be stored to the name member and this 24 will be stored
to the age of member of this object Anil.
Now When we use this introduce method, you know its gonna print out the value.
so I'm build and run this.
hey I am Anil and I am 24 years old.
All right.
The next thing that we are gonna do is we are gonna create another object.
Let's say person duplicate Anil and this duplicate Anil, its gonna contain the same values as
this Anil. so i'm gonna use the equal to symbol and I'm
gonna write anil.
so now what we were intended to do is we want to copy all the members of the Anil, you know
the name and age and we want the exact copy in this duplicate Anil.
So what i'm gonna do is i'm gonna use duplicate Anil and i'm gonna call the introduce method.
now you know if I build and run this, Hey I am Anil and I am 24 years old.
that's from the Anil object and then hey I am Anil and I'm 24 years old.
that's from the duplicate Anil.
It means that you know when we use this equal to assignment, all the members of this Anil
object is copied to this duplicate Anil object.
The next thing that we are gonna do is we are gonna change the value of this name and
age of the Anil object by using change name and age method.
so here i'm gonna call the method.
So its gonna be Anil dot change name and age and here I just want to say Anil Shetty and
then I'm gonna pass age lets say hundred and anil is 100 years old now.
Now i'm gonna call this Anil dot introduce again and then i'm gonna call duplicate dot
introduce.
so now if i build and run this, now you guys can see, we get hey I am Anil and I am 24 years
old and the same thing in the second line that is from this anil dot introduce and then
the duplicate anil dot introduce.
you know the value is exactly copied and then we have changed the name and age of this Anil
object and that's why when we call the introduce method, it says hey I am Anil Shetty, the
new name and I am 100 years old and then from this duplicate Anil object, we get hey I am
Anil and I am 24 years old.
So everything is perfect.
so here we have the simple types.
you know the string and int and we are not using any pointers or you know we haven't
used any dynamic memory allocation here.
That's why you know when we use this type of simple members, you know the allocation
works as we expect it.
The next thing or the problem arises when we use the dynamic memory allocation.
here let's say we are gonna have a pointer of this name and we are gonna change the code,
slight a bit.
so here in this constructor we are gonna allocate dynamically.
so its gonna be new and then string and we are gonna pass the name.
so here you guys can see, this name is a pointer. which is gonna contain a string value and
this age is a simple variable.
that's it.
And since it is a pointer you know we need to use the star operator here while changing
the value.
so its gonna be star and then i'm gonna use parentheses to group this.
name equal to name and here also when we refer this, we need to use star.
otherwise you know this name is a pointer its gonna contain the address of the memory.
you know its gonna print out the address.
so to get the value stored in that pointer you know we are gonna use the value at operator.
all right.
now we haven't changed anything here.
now I'm gonna build and run this program.
and now you guys can see it says hey I am Anil and I am 24 years old.
That's from the Anil object you know when we created it and when we call this introduce
method.
And then we have created the duplicate object of that Anil using the equal to, and it says
hey I am Anil and 24 years old.
fine.
Next we have changed the name and age of the Anil object.
so you guys can see name of the Anil object is changed to Anil Shetty and age is 100 and
then when we call the introduce method it says I am Anil Shetty and I am 100 years old.
and then we just call the introduce method from the duplicate object.
what do we get?? we get Hey, i'm Anil Shetty and I am 24 years
old.
look at this.
you know this duplicate objects name property is also changed when we change the name of
this Anil object.
so and also you know the age is not changed.
age is still 24 in the duplicate object and it is 100 in the original Anil object.
so it means that when we use the pointers as the class members and we use the dynamic
memory allocation, and also when we copy the object at that time the bit by bit copy takes
place.
And that's why in this anil object, you know the name is containing a pointer, you know
a memory address and that's why that memory address is copied to this duplicate anil's
name member.
At that time you know both objects are containing the memory address of the same string.
so it is Anil Shetty you know it is changed by our change name and age method.
so that's why the name property of this Anil and his duplicate Anil's object pointing to
the same string.
So we don't want that.
what we want is when we create a new object and assign the values of one object, both
object should contain different different values and if you change the value of one
object and it should not reflect in another object.
so it means that there is a problem when we copy one object to another object.
you know the same problem occurs when we pass an object to a function by copy.
At that time you know the copy takes place and the same problem and also if a function
is returning any object at that time also same problem occurs.
so what is the solution?
The solution is to use the copy constructor.
A copy constructor is nothing but an overloaded Constructor and this copy constructor is called,
whenever we copy an object of that class.
so to define a copy constructor, you know since it is a constructor, it has the same
name as a class.
So its gonna be Person and then you know its gonna take an object of this class by Reference.
so we are gonna use the constant keyword you know just to make sure that the value of that
passed object should not be changed.
so its gonna be const and then person, the object of the same class type.
And then lets say P and here we are taking it by reference.
S here you know this copy constructor will be called whenever we copy an object of this
class.
so here what we can do is we can make sure that when we use the pointers as our class
members the different memory should be allocated to that member.
so here what we are gonna do is when we copy, we want to allocate an entirely different
memory and we want to copy the string.
To do that, here inside this copy Constructor i'm gonna write name equal to and then I'm
going to create a new string.
so a different memory.
so its gonna be new strings and in that string what I want to store.
I want to store the name of the object that we are copying.
Here in this case you know when we copy that here you know this Anil object will be passed.
so this p is gonna contains the Anil object.
what we want to store in the name member of the duplicate object.
we want to store the name of the Anil object.
we can get that by using p.name.
so this p.name is containing the address of a memory location, which contains the name.
so we want the name.
not the memory address.
That's why we are gonna use the value at operator here.
That's it.
And the next thing is you know this copy constructor will be called, whenever we copy one object
to another.
So here that's why if you don't initialise this age at that time you know the age variable
of that copied object is gonna contain garbage value.
so we are gonna initialise that one also.
so its gonna be age equal to p dot age.
So here we are not using the value at operator.
you know because this age is not a memory address.
it is containing a value.
so I'm gonna save this, i'm going to build and run this program and now you guys can
see, you know when we create a new object of this Anil and call the introduce method,
it says hey I am Anil and I am 24 years old and then we have created the duplicate object
at that time the copy constructor is called.
so just to demonstrate that I can add a cout out here.
copy constructor is called and lets end this line and also we need to add the semicolon
in the end.
now I'm gonna Build and run it again.
All right.
now you know, hey I am Anil and I'm 24 years old that's from the Anil object and then we
copy the Anil object to duplicate Anil.
At that time the copy constructor is called and the introduce method of the duplicate
anil says hey am Anil and 24 years old.
you know all the values are exactly copied. and then we have changed the value of the
Anil object using the change name and age.
At that time, the value of the Anil object has changed and we can see that Hey I am Anil
Shetty and I'm hundred years old.
you know the changed name and age and then if you call the duplicate Anil, Anils introduce
method you know it says hey i', Anil and I am 24 years old.
so now because of this copy constructor, you know these two objects are not sharing any
same memory location.
So both of them are containing different different values in different different memory locations.
And also when don't define this copy constructor, you know there will be a default copy constructor
called by the c++ but you know there will not be any statement.
that's why you know the bit by bit copy takes place.
so this is it guys this is about the copy constructor in C plus plus.
you know why we have to use this, what's the problem that can arise if you don't use it
and also how to use it.
thank you for watching and don't forget to subscribe.
like my Facebook page @ facebook.com/LearningLad and stay updated with the latest videos that
we are gonna make and I'll see you next tutorial.