Lecture 9- Constructors in C++ (Part 2)

IIT Roorkee July 201
61 followers
Jan 2, 2025
[Music]
[Music]
so welcome to lecture number nine
classes and objects so in the last
lecture we had seen default Constructor
and parameterized
Constructor all right so with the help
of parameters Constructor so can we
solve this particular problem all right
so the problem is create a complex
number class right the name of the class
is complex numbers and add two complex
numbers right it is nothing so C1 is A1
+
ib1 C2 is equal to A2 +
ib2 in fact we had seen the
multiplication so C1 + C2 so let us say
C3 is C1 + C2 so which will be A1 +
A2 + I into B1 +
B2 right so this we know so let us solve
this using the parameter Constructor all
right so the main idea
is so when you are adding two complex
numbers
you take one number from user so let us
say C1 I'm taking from the user and C2
should be pass using the Constructor so
you're passing so when I am passing
means I have to use as argument right so
that means it is having the
parameter so that means it is a
parameterized Constructor so another
input should be using the Constructor so
let us see how we are going to write
this in C++ so let us have the class
complex so the class complex has two
member data real and
imaginary almost similar thing we had
seen the multiplication of two complex
numbers and here you have the
parameterized Constructor so we had seen
the name of the class is complex and the
name of the special member function is
also complex so obviously this is a
Constructor and we are using two
parameters r and i the real imaginary so
therefore you call this as a
parameterized Constructor so you have
used the parameterized Constructor so R
will be initialized right whatever
you're passing that will be initialized
to real and whatever you are passing for
I that will be initialized to imaginary
all right so this is a special member
function a Constructor line number 11 to
14 now here you go you have line number
17
correct so line number 17 you have a
member function called
add all right you have a member function
called add so we have two arguments so
one is C1 and another one is
C2 so two complex numbers we have
already calculated how to add that means
C1 do real plus C2 real A1 + A2 right so
this is what we worked out A1 + A2 that
means that real numbers the real part
will be added and then imaginary part
should be added so the real part will be
added put some real and another variable
sum
imaginary so C1 do imaginary plus C2 do
imaginary put it in Su imaginary and you
are returning complex Su real and Su
imaginary all right return the object
all right the name of the object is
right the complex with some real and
some imaginary correct so we will see
how this is working when we are going to
talk about the main so now line number
24 you have wide display one of the
member functions all right so it is
printing real plus I into imaginary or
imaginary I okay so 2 + 3 I that's a
usual way all right so that will be
displayed over here so now what was the
problem one input you have to take from
the user all right so it is asking user
real and user
imaginary all right so we have a double
the main program is starting here user
real and user imaginary so we take the
input from the user for example assume
that so 2 + 3 I is my input user input
all right so now here you go I
have user complex
object right user complex object object
I am pausing user
real and user
imaginary right so whatever I have taken
the input from the user I passing this
so this is object user complex so that
means this will invoke all right
whenever you are creating an object this
will invoke The Constructor all right so
the Constructor will be invoked suppose
I'm passing two 2 + 3 I right
yes so real will be two and imaginary
will be three all right in the first
case so that means under user complex
object my real will be two and imaginary
part will be three all right and you
have another object called Constructor
complex so the Constructor complex you
have you are passing 3.5 and 2.0 so one
we set user has to give us a input
another one you are pausing so this is
an example all right so 3.5 and 2.0
assume that 3.5 + 2.0 I right so when
this object is being created again the
Constructor will be called so under this
object name what is the object name
Constructor complex so your real will be
whatever number you have passed and
imaginary will be whatever number you
have passed so I pass 3.5 and
2.0 all right so this is by user
and this is by
Constructor right I hope you
understood so now we will see
further all right so
now we are calling all right so let us
say complex result result is an
object all right so complex result
result is an object so that object is
invoking AD right so that object is
invoking ad so you are pausing user
complex and Constructor
complex all right so this is what
exactly we are doing in the case of
add complex C1 so now C1
is user right user complex C1 is nothing
but user complex and C2 is nothing but
Constructor complex so now here C1 and
C2 so the corresponding C1 real and C2
real will be added right user complex
real part and user
complex right Constructor complex real
part will be added similarly the
respective imaginary part will be added
now it is some real and some
imaginary all right so now it is
returning
complex all right so which is carrying
some real and some imaginary all right
for example if I'm adding 2 + 3 a and 3
+
5 3.5 + 2.0 I all right what I will get
5 .5
+ 5.0 I all right so this will be
returned in line number 20 all right so
when it is return if you look at the
main program that will be stored in
result all right so that means result do
real will
be 5.5 and result do imaginary will be
5.0 right so you can see over here right
so that will be returned to result so
result that carry real part and
imaginary part so whenever I have a
complex right the object so here the
object is result so the result that
has real and imaginary look at line
number six and seven all right so that
will be returned and uh right this uh
result. add when it is invoked the
addition will be returned to result so
the result as real part and imaginary
part so so now see how user complex
number you are
displaying what is user complex number
so let us say 2 + 3 I right suppose 2
and three are the input 2 + 3 I this
will be
displayed and Constructor complex will
be displayed now sum of these two
complex numbers so that is why the
objectoriented program will be elegant
all right so you have done everything in
the
class all right and then when the member
function is doing all the addition the
Constructor is doing all the
addition right Constructor initially
saving initializing and then you have a
member function that will be doing the
addition all right so now here you have
result do display some of the complex
numbers result. display so that means
the result as real number real part and
result as the imaginary part so that
will be
display all right suppose let us assume
my
input is I am giving 5.0 and 2.0 as a
input user
input right so we have already passed
3.5 and 2.5 that is a Constructor if you
look at the program we have passed this
3.5 and
2.0 right 3.5 and 2.0 5 comma 2 is my
input user input user
complex all right so when I add this 5 +
3.5 is 8.5
2 + 2 is 4 so I will get the output 8.5
+ 4 I so the addition of two complex
numbers right so the crucial part here
is so when you are declaring the object
instanciate an object
result right in the right hand side this
object result is invoking
ADD at member function so you pausing
user complex so this will be considered
as C1 inside the function and this will
be considered as C2 inside the function
right copying the usual one all right
call by value call by reference all
right so here it is C1 you're
pausing all right user complex and
you're pausing Constructor complex all
right so when you are passing this this
will be assigned to C1 and C2 it's
exactly happening over here and then the
corresponding reals will be added
corresponding imaginary will be added
and line number 20 it is returning the
complex object all right which is
carrying some real and some imaginary
all right so it is nothing but you look
it is a special member function kind of
right complex some real some imaginary
right so here you have when you look at
the Constructor complex r i r will be
assigned to real and I will be assigned
to IM imaginary this is what exactly
happening all right and then where it is
returning you can see that it will be
returned to the result so that means the
result as real part result as imaginary
part so that is what exactly we are
displaying in line number 52 and 53 okay
so this is how this particular program
is working using parameterized
Constructor so you can do the same
problem in one more way right so how to
do that the same all right so we have
imaginary we have this Constructor it's
it is nothing but a parameter
Constructor up to line number 15 it is
same and here you go so here we have
complex add all right I have complex
reference variable
other all right is exactly like few
classes back we have done all right
addition of two distances if you
remember all right feet and
inches so here you have real with the
other
real right imaginary with the other
imaginary so we'll see how this will be
working and rest of them are same we are
going to return the some real and some
imaginary whatever we had seen in the
previous program and the display
function right so only this part is
crucial we are going to see how this is
working so now the user real and user
imaginary all right so when you are
creating this particular object the
Constructor parameterized Constructor
will be invoked with the user real and
user imaginary and then another one is
Constructor complex you are pausing the
value 3.5 and 2.0 exactly the same value
we are passing only the line number 16
to 21 all right so it is a different way
in fact we had seen this I'll explain
one more time for the other example so
now you you can see this right right you
have declared complex
result now the user
complex the user complex is invoking at
member function and you're pausing
Constructor
complex right user complex is
invoking ADD member function and then
you're pausing Constructor
complex all right so if you look at the
syntax it goes like this complex and
others complex reference
other which will be equal
to right so complex and
other
correct which will be equal to
Constructor
complex all right so the meaning is
other and Constructor complex is
pointing out the same
location all right you're copying the
reference right pass by
reference all right so user complex is
invoking ADD function you're passing
Constructor complex so
complex reference operator other so both
are pointing to other which is available
in your ad function
right that is also a complex object
right complex class other object is a
complex class and Constructor complex
which is also a complex class so they
are going to point out the same location
right so content will be same so suppose
I have what is that Constructor complex
you 3.5 comma 2.0 means other will also
have now 3.5 comma
2.0 okay that is a meaning so
since here your user complex is invoking
the function right since a user complex
is invoking the function you are real
user complex real whatever you are right
writing as a input giving as a input
user complex all right so that will be
your actual real and the imaginary the
user complex since it is invoking so the
user complex real will be real here and
user complex imaginary will be imaginary
here and then rest is same you are
adding the corresponding real part and
you are adding the corresponding
imaginary part so then you are returning
now rest of the things are same whatever
we had seen in the previous program all
right when you run the code right so now
it is everything is same so only here it
is crucial line number 44 is crucial
user complex is invoking so that means
user complex. real will be the real that
you are using in the function and user
complex complex right imaginary user
complex imaginary will be imaginary
right so now you display the user
complex numbers Constructor complex
numbers and the sum of these two so when
I run the code assume that 5 comma 2 is
my
input all right assume that 5 comma 2 is
my
input and 3.5 comma 2.0 we are
pausing all right so we will get the
output 8.5 + 4 I all right so 5 + 3.5
8.5 2 + 2 is 4 all right so this is how
we can have
the usage of parameterized Constructor
right so in one
case we have done the result is invoking
the add function
all right you are pausing two two
arguments C1 and C2 user complex and the
Constructor
complex correct and then it will be
copied to C1 and
C2 and then rest is same the
corresponding the real numbers the real
part will be added real parts will be
added and the corresponding imaginary
part will be added so the complex
addition have been done all right so
this is the case
when the object result itself is
invoking add all right so in another
case here you have the user complex
object is invoking the add member
function and then you are passing
Constructor complex so when you're are
passing the Constructor complex the user
complex do real will be the usual real
in the function right that is line
number 18 and user complex imaginary
will be the imaginary in the function
right so other anyway the other will be
copied all right so that means this
Constructor complex will be copied into
other so other do real and other do
imaginary so the corresponding real part
and the corresponding imaginary parts
will be added and then that is returning
some real and some imaginary so when we
are running the code all right so we are
getting the same output so this is how
we can use the applications of the
parameter Constructor so we had seen
several applications on the default
Constructors and the parameters
Constructor so in the parameters
Constructor this particular problem it
is slightly interesting because
the two input that means the real and
imaginary you are taking from the user
another one you are pausing as a
Constructor so slowly you are getting
familiar with these object classes the
constructors concept right apart from
that we had also seen is it possible to
define the member function outside the
class so in a similar way so the special
member function Constructor you can
think of whatever program that we have
written done so far so is it possible
that can we write this Constructor
outside the class with the syntax that
we have used so the next concept we are
going to see copy
Constructor right so what do you mean by
copy Constructor so
here let us assume we have the class
example all right so let us assume we
have class example I have one member
data in a right so I have one member
data int
a right so here I have one parameterized
Constructor the usual one parameterized
Constructor now you know example all
right so which is same as the name of
the
class which is same as the name of the
class and you are pausing one parameter
Index right so you are pausing one
parameter which is
index so here you have it's a special
member function so I'm copying X into a
that means a equal to X I'm
copying X into a so AAL to
X and now
here I have another Constructor so this
is the copy
Constructor all right so here you can
see the name of the class
example and I am using object reference
all right so example is a class and here
the object reference
address
OB all right and ob.
a the object. a is equal to a right so
in fact a equal to ob. a right ob. a is
assigned to a I mean to say right so a
is equal to ob. a so this we call it as
copy Constructor so we will see in the
main program how we can use this all
right so here you go your class is over
here line number 15 your class is
getting
over so
now this is the usual one when you are
calling when you are creating the object
E1 all right so you're creating an
object E1 by passing the value
36 all right so what is exactly
happening in line number
19 all right so that means you are
calling the parameters Constructor right
you're creating an object
E1 passing the value
36 all right so the meaning is you are
invoking all right in
fact this will be the E1 right when
you're creating E1 so it is
automatically invoking Constructor so
here in this case parameters Constructor
because you are passing the value 36 all
right so that means a is taking the
value 36 so here you are pausing so inex
so X is 36 so 36 will be copied into a
all right so a equal to 36 for the
object
E1 for object E1 right so it is nothing
but e1. a which is equal
to suppose I'm accessing this from
outside right in fact it is outside e1.
a is 36
all right so the next line so slightly
different first time we are seeing you
have the class example and then you have
object
E2 so object E2 you are pausing another
object
E1 right what is the meaning of this E2
you're pausing another object all right
so that means your
example is the class name of the class
address OB
which is equal to what you are passing
E1 all right so when you're creating
this object E1 will have some memory all
right so let us say this will be the
location
4 all right when I display E1 see out E1
so it will give 4 location 42 so now
when I have this right
example reference operator OB whereas if
you look in the main I'm passing e 1 all
right I passing E1 so when I'm passing
E1 right so here you go OB and E1 which
is pointing to the same
location right OB and E1 which is
pointing to the same
location all right so we know e1. a is
what
36 e1. a is 36 so OB is pointing to 40
02 E1 is also pointing to 42 assume that
this is some exad decimal
numbers correct so e1. a
is 36 that we know
already right so now if e1. a is 36 what
is ob.
a that is also 36 because both are
pointing to the same location correct
ob. a is 36 so that you are assigning to
a that is a meaning OB a is a all right
so now when I want to print e2. a all
right so that means when I'm creating an
object ob. a you are passing right
assigning to a meaning is you are e2.
a which is also
36 that is the meaning all right ob. a
you are assigning to a ob. a is what
36 so here when it is equal to right a
equal to ob. a so a equal to 36 for
object
E2 otherwise you call it as e2. a is
36 all right e2. a is 36 so when I want
to display this line number 21 C out is
e2. a so you know the output so what
will be the output we'll get we will get
the output
36 all right I hope it is clear for
everyone and line number 20 20 if you
look so this is called the copy
Constructor right first time we are
seeing we have seen default Constructor
we had seen parameters Constructor and
this is a Syntax for the copy
Constructor so object E2 is an object
and you're pausing
object that object is E1 you're pausing
the object E1 so and then once you are
pausing this so this is the syntax so
example
address OB which is equal to
E1 right example address OB which is
equal to
E1 and then if you look over here both
OB and E1 is pointing to the same
address so that
means e1. a is 36 ob. a is also
36 all right so when ob. a is
36 when you are assigning to a so this
will be nothing but e2. a all right the
meaning is this is nothing but e2. a
what is the value of e2. a 36 so when
you are trying to display this in the
main e2. a you are getting
36 all right I hope it is clear for
everyone and the last concept that we
are going to see in C++ is the
distractors right in today's last class
so let us consider the class employee
all right so here you have the default
Constructor all right so default
Constructor you are displaying
Constructor
inv and then first time we are seeing
this is
distractor all right so this is
distructor so distructor symbol is Tilda
and then you have employee same almost
like a Constructor but only difference
is you put the Tilda symbol so that is
nothing but
distractors right when you're creating a
memory all right when you're are
creating an object you require the
Constructor all right for the memory and
then you have
to destroy the memory so for that you
require distractors so you might have
used the new operator and delete
operator right in the case of aray right
so new operator is for allocating a
memory and and delete operator is for
destroying the memory all right
deallocating the
memory so for the deallocation we have
the
distractors right assume that you have
an object E1 whose class is
employee right so you are just printing
see out address E1 I'm printing address
of E1 right I'm printing the address of
E1 and another object E2 I'm printing
address of E2 so when you are creating
an object
Constructor will be called right when
you're creating the object Constructor
invoke will be displayed right so
Constructor invoked I'll just simply
write CI stands for Constructor invoked
and address of E1 it will print
some number right exad decimal number
address of E1 and employee E2
Constructor will be
invoked and address Z2 some other
address 0x 2 3
E7 something like that I'm writing some
other address and then return zero so
before that you will have the distractor
that means you are deallocating the
memory for E1 and E2 all right so two
more output you will get because of two
objects so you will get distractor
invoked right so I'll write da two times
continuation da
da stands for distructor invoked CA
stands for Constructor invoked so this
you have to
get these four as output right so you
can see here right Constructor
invoked whenever you have employee E1
then you have address
E1 employee E2 Constructor invoked and
then you have address and finally before
going to the return zero both E1 and E2
because of this till the employee the
distructor so what you can do you can
try without line number 10 and 13 right
without line number 10 and 13 you try
the same program the last two output
will not be
displayed right so this is the usage of
the distor that means now this memory
space right so these two will
be D ocated that mean the object E1 and
E2 are deallocated from this memory
space so that is the meaning right that
is what you are getting as a
output so in today's class so we talked
about the parameterized Constructor and
then the applications that we had seen
in terms of addition of two complex
numbers we had also seen one more
Constructor called copy Constructor and
now you know the meaning of distructor
as well thank you
[Music]
