Lecture 8- Constructors in C++ (Part 1)

IIT Roorkee July 201
61 followers
23 Video Views·Jan 2, 2025
Welcome to lecture 8, Classes and Objects.
So we are going to see C++ Constructor.
So constructor is nothing but a special method or a member function.
So this will be automatically invoked when you are creating an object.
So assume that, suppose you have a class and then you have an object, let us say student
S1.
So when I am creating an object, assume that we are going to see how we are going to write
this special member function, what is the meaning of this special member function.
So here when I create this object or instantiate the object, so the constructor will be automatically
called.
We will see how we are going to check this, right.
So also whenever you want to initialize, right, so when you want to initialize something on
the data members.
For example, I want to give A is equal to 10 or string is equal to IIT Roorkee, so something
like this, right.
So in that case, so when I want to initialize the data members, correct, so constructors
is being used.
So talking about the constructor, if you look at the name of the class, say for example
here student is the name of the class, right.
So the name of the constructor will also be same like class, right, or even a structure.
In the procedural oriented programming you might have studied structure.
So when you want to create or when you want to have the constructor, so it should have
a same name.
So the syntax go like this.
So you have name of the class, right, and then we will have a list of parameters.
It looks like a function, right.
So that is why it is called as a special member function, right.
That is the reason we are calling this as a special member function.
Another one is you will have, in fact, when I am talking about the function, you can have
a list of parameters, correct.
Also, so here you do not have any definition.
The first syntax does not have any constructor definition, whereas in the second syntax if
you look, you have name of the class and then list of parameters begin and then you have
constructor definition, right.
So you can have, we are going to see two types, all right.
So we will see what are those.
So in fact, so when I talk about this syntax, so you have name of the class, scope resolution
operator, again the name of the class which will be like name of the constructor.
One is name of the class, another one is a name of the constructor and then you will
have the open braces like a function.
You will have a list of parameters and then we will have constructor definition.
So this is how we are going to define the constructor.
So there are two types of constructors in C++.
One is the default constructor, right.
Another one is the parameterized constructor, right.
Default constructor and the parameterized constructor.
So we are going to see what is meant by default constructor and what is meant by the parameterized
constructor.
So let us see what is the meaning of default constructor.
So let us have class employee, right.
The name of the class is class employee.
And here if you look, the name of the class and this is called the constructor.
In fact, this is a default constructor, all right.
So, the name of the class and name of the constructor should be same and then as usual
I mean we all know functions right.
So, as usual like function you are writing cout testing default constructor some cout
statement all right and then your class is over in line number 10.
So, then you are going to main program.
So, main program if you look you are creating two objects E1 and E2 under employee class.
So, whenever you are creating the objects the constructor will be automatically called.
So, this is what we define if you look at the two slides back we define this right.
So, constructor is a special member function which is invoked automatically at the time
of object creation.
So, here I have created objects right.
So, first when a line number 13 when I create objects you have right the constructor will
be invoked when I create this the constructor will be invoked.
So, that means testing default constructor will be printed first time and when I create
the second object E2 testing default constructor all right will be displayed second time ok.
So, you can see the output.
So, when you run the code you will get the output like this.
So, this is what when I am creating an object the constructor will be automatically called
right.
So, this is how we define the constructor right.
So, this is called the default constructor the meaning of default constructor here you
do not have any parameters all right.
So, here if you look you do not have any parameter.
So, when I do not have any parameters you call this as a default constructor this is
a default constructor.
So, now with the help of default constructor so, let us try to solve this problem in object
oriented programming.
So, the problem goes like this design a class called book right the name of the class is
book and then you have the attributes right with attributes for the title, title should
be a string C++ string and author should be a C++ string and price the double data type
ok.
So, now the question is implement a default constructor that sets the title and author
to unknown right.
So, title and author initially it should be unknown you are going to set right with the
help of default constructor and the price to be 0 price is double should be 0.0 understood.
Now write a C++ program to create an instance of the book class right we have to we are
going to create the instance of the class book with the help of default constructor
and display the attributes.
So, whatever the attributes that you have defined in the constructor that has to be
displayed right.
So, this is a simple problem let us see how we are going to write the code.
So, let us define a class book all right.
So, still we have not seen the access modifier right now no need to worry about this keyword
we will define slightly later.
So, now you have three member data one is string title, second one is string author,
third one is double price right title whose data type is string right I have included
string right once you include this header file problem solved right you can use the
string data type right the string it is like IADR or India all right.
So, this is called as a string character array all right and then price whose data type is
double.
So, now under public right do not worry about the keyword still we have the constructor
right if you look at the name of the constructor which is same as the name of the class and
then I do not have any parameters.
So, that means this is called the default constructor all right.
So, this is the syntax where I can directly right.
So, either you can do this or I can do like this book that is one way all right.
So, I have book then followed by the parenthesis then I can have title equal to unknown right
that is another way author is equal to unknown right price equal to 0.0 right this is a standard
way whenever you have a function you write this all right.
So, this is same as I am writing in one line right this is also valid statement valid syntax
right.
So, the both are same.
So, 1, 2 both are equivalent right both are equivalent.
So, either you follow line number 14 or this you can try all right.
So, book which is a constructor title unknown author unknown and price is 0.0 ok.
So, now we have one more member function called display book detail we have one more member
function called display book detail.
So, here you go see out title author and price that we are displaying and the class is over
line number 22 your class is over.
So, now when I go to the main program I have created an object my book right.
So, when I create my book you know that what will happen your default constructor will
be called will be invoked called or invoked right your default constructor will be called
or invoked.
So, my book is an object line number 26 under class book.
So, this will be invoked right.
So, title will be set my book dot title will be unknown my book dot author will be unknown
and my book dot price will be 0.0.
So, this is what the initialization means right we have when we define the constructor
we talked about the attributes or the member data when you want to initialize.
So, initially I am doing this title unknown author unknown and price is initialized to
0 this is the meaning right price is initialized to 0.0 right.
So, now what will happen your details will be under my book object my book dot title
will be unknown my book dot author will be unknown and my book dot price is equal to
0.0 that is the meaning right.
So, now I want to display this book detail.
So, my book will invoke display book detail member function right.
So, when it is invoking.
So, this is a display book detail right.
So, my book is invoking.
So, title author and price you know.
So, unknown unknown and 0.0 will be printed right.
So, this will be printed.
So, you can see when you run this code you will get the output.
So, book details line number 29 and then if you look title unknown author unknown price
rupees 0.0 ok.
So, this is the application of the constructor right.
So, I have initialized right I have defined or declared the variables you call it as a
member data and then I when I want to initialize using constructor.
So, we have done this line number either you can use line number 14 or here I have
given all right the second one.
So, you can use any one and then we have seen the output.
So, now this is all about default constructor.
So, the next concept is parameterized constructor.
So, the default constructor you can see no parameters for example, we have seen two default
constructors the last example we had seen book.
So, under book you do not have any parameter correct.
So, now when you look the parameterized constructor.
So, let us consider the class employee right let us consider the class employee.
So, we know that when I am talking when I am going to talk about constructor the name
should be same right where do you find the name should be same where is where do you
find name is same on line number 8 employee.
Then immediately you can find out this is the constructor right.
So, you have int id member data or data member string name float salary these are all the
data members and then you have constructor.
So, this constructors has three parameters right this constructor has three parameters
int i string in n and float s right i whose data type is integer n is a string and s is
the float all right.
So, here I am setting right getter and setter function we have seen.
So, similarly i will be initialized to id because I am using this n will be name and
s will be salary.
Then I will have another member function called void display this is in fact, the special
member function.
So, which is called the constructor and you have parameter three parameters.
So, therefore, it is called the parametrized constructor.
So, here you have void display right here you have void display.
So, cout id name and salary right.
So, we are printing id name and salary.
So, now you have int main I am creating an object e1 right and when I am creating an
object e1 you can look at the right hand side you have employee of 18 virat and 100000 right
18 virat and 100000.
So, this is a special member function constructor right.
So, this will be invoked and id name and salary whatever I passed.
So, what are the things I passed 18 virat and 100000 we passed.
So, now for e1 the object e1 id will be 18 name will be virat and salary will be 100000
right.
So, similarly when you are creating the second object e2 right hand side if you look this
is a constructor you are passing 45 rohit and 90000 right you are passing 45 rohit and
90000 right.
So, for e2 the e2.id is 45 e2.name is rohit and e2.salary is 90000 right.
So, now here you have e1.display right so that means the object e1 is invoking display
right the object e1 which is invoking display.
So, when it is invoking display you can see that we are printing id name and salary.
So, e1.id right 18 e1.name is virat and e1.salary is 100000 right.
So, this will be printed and similarly when e2 is invoking display e2.id is 45 e2.salary
e2.name is rohit and e2.salary is 90000 ok.
So, this will be printed.
So, when I run the code I will get the output 18 virat and 100000 45 rohit and 90000.
So, the main idea here is I have the class employee the same name I have in line number
8.
So, this I call it as a special member function right.
So, that means this is nothing but a constructor and I have 3 parameters over here int i, string
n, flow test therefore, I call this as parameterized constructor ok this is a concept of parameterized
constructor.
So, the main idea is this is useful to initialize the value.
So, i so whatever I am passing so that will be initialized to id name and salary.
So, now you can manipulate inside if you are writing another function or you are doing
some other program.
So, we can manipulate that we will look at it right.
So, this is all about your parameterized constructor maybe we can take one example one problem
design a class circle with a parameterized constructor that takes the radius as an argument
ok.
So, you will have radius as argument.
So, we are going to work it out for the parameterized constructor and calculates the area of the
circle in C++ right it is not a difficult problem, but we are going to use the parameterized
constructor all right and then how we are going to pass the argument in the parameterized
constructor.
So, that we are going to check here.
So, here you go you will have class circle all right.
So, we have class circle and then I use one of the member data radius right.
So, you have radius as a member data.
So, now, I have constructor all right.
So, here you have the constructor all right.
So, in fact, I have one argument r right double r.
So, therefore, it is called parameterized constructor and the meaning of this is with
the colon the meaning of this is radius is equal to r right you can write other way round
circle double r right circle double r open the parenthesis radius equal to r.
So, whichever you are comfortable you do that both the meanings are same.
So, this is 1 and this is 2 both 1 and 2 are same all right.
So, when you are when you want to become the advanced programmer I mean this is a better
syntax the advanced syntax kind of.
So, those who are the beginner in oops I mean to say object oriented programming structure
you can use this also initially all right.
So, now, we will see another function all right.
So, now, we will see another function all right which is another member function double
calculate area all right.
So, this will return pi this is the value of pi into power of r that means, radius comma
2.
So, it is nothing, but radius into radius right the power function that I am calling
with the help of C math right the power function that I am calling with the help of C math
and this is nothing, but pi r square pi into radius square right pi r square.
So, the class is over here you go this is parameterized constructor all right and then
I have one member function called calculate area correct and here you go the main program
all right.
In the main program since I have not used namespace std using namespace std I am using
std double colon right std scope resolution operator.
So, if you are using namespace std like we had seen in the previous classes or previous
program itself you do not need to use ok.
So, enter the radius of the circle.
So, let us say we are entering the radius let us say 10 ok.
So, you are passing this circle you are creating an object, object is my circle all right and
then you are passing this radius whatever radius you have taken the input from the user
you are passing this.
So, circle my circle radius all right.
So, assume that suppose I am passing let us say radius value is 10 I am giving the input
10 all right.
So, my circle with 10 correct.
So, what will happen my circle is an object under circle.
So, this will invoke the default right in fact, this will be invoking parameterized
constructor not a default constructor because you have the argument right you are passing
the value all right.
So, this will invoke the parameterized constructor.
So, when it is invoking the parameterized constructor.
So, the radius will be equal to r.
So, r what you are passing we are passing let us say 10 right now radius will be equal
to 10 correct under my circle the radius will be equal to 10.
So, now I have function right now this object my circle right this object my circle will
invoke calculate area all right.
So, this object my circle will invoke calculate area all right.
So, here you have the area of the circle with radius that will be the print statement the
radius you have passed let us say I am passing 10 assume that it is 10.
So, which is area so that means, when it is calling this calculated area right calculate
area member function all right.
So, calculate area member function is pi into r star r right this is the value of pi all
right.
So, we will have pi r square.
So, r already right 10 all right so that means, this is nothing, but pi into the radius squared
right so this is what we are expecting.
So, here radius will be equal to r right from the mean from this meaning radius is equal
to r.
Suppose I pass 10 radius will be equal to 10 now all right.
So, pi star 10 into 10.
So, whatever be the value thus that will be returned all right.
So, when it is returned that will be stored in area all right.
So, pi star r star r whatever be the value that you are giving so that will be stored
in area.
So, the value will be displayed.
So, now, when we are giving a input let us assume that we are giving r is equal to 10
as a input all right.
So, what will happen?
So, here my circle the radius will take the value 10 all right.
So, that means, I am passing the value 10.
So, here you go right the radius here it is 10 and when you look at the parametrized constructor
the r value will be 10 all right.
So, that means, radius is equal to r.
So, radius will take the value 10 correct and then when this my circle the object is
invoking calculate area all right.
So, calculate area you have 3.14 all right into r into r right the radius into radius
correct.
So, that means, pi into radius square so, pi r square so, that will be calculated.
So, when I give 10 as a input I will get this as a output.
So, you can check all right.
So, when you are giving 10 as a input so, the area of the circle will be 314.159 all
right.
So, this is how the parametrized constructor work all right.
So, we had seen two examples.
In the previous example we had passed three parameters and here you are having one parameter
right.
So, here you go the radius right one argument and we know this is a special member function
where the name of the class and name of the constructor will be same that is why it is
called a special member function all right.
So, we had seen two examples of parametrized constructors.
So, in one case we had three arguments and in this example we have one argument.
So, the next program we are going to see is it possible to define the member function
outside the class right the answer is yes.
So, here you go I have a class name of the class is my class right I have a member function
which is print message.
So, here you can see I have only declared right here I have only declared print message
right and this is the name of the class, class is beginning over here and it is ending over
here right.
So, now, can I define this particular function member function outside the class yes you
can do that right, but you have to follow certain syntax right.
So, here you have the return type void the same return type and then you should have
name of the class all right.
So, you should have return type right return type name of the class right name of the class
followed by scope resolution operator two colons when you put it is a scope resolution
operator and then you can use the name of the member function right.
So, for example, print message right member function so, whatever be the function and
then you use as usual right.
So, whatever you want to write in this member function it goes right.
So, exactly we are writing.
So, you have a return type void and name of the class is my class and then you have scope
resolution operator and then you have name of the function and inside the function whatever
you want right you can do that.
So, for example, here we have only one cout statement cout allow from my class all right.
So, now, we will see the main program classes over here itself and I am defining outside
the class right.
So, now, if you look at the main program my obj right which is the object whose class
is my class all right my obj under my class.
So, this my obj object is invoking print message.
So, you have a dot operator invoking the print message.
So, print message will be invoked right so, that means, you are defined outside the class.
So, what it is you have one cout statement allow from my class will be printed right.
So, this we are expecting that as a output right.
So, if you run the code I will get allow from my class right.
So, the literal meaning over here is so, this is nothing, but whatever you want to write
inside the class you are as such writing outside the class all right.
So, for example, usually what will you do void print message right and then you will
write cout right allow from my class all right.
So, this is what you or we have already done right.
So, now, as such what I want to write I am not writing inside the class the question
is can I write outside the class.
Yes, you can do that, but we have to follow the syntax or the procedure properly right.
So, when I run this code I will get the output.
So, whatever you have written in this print message all right in the number function.
So, that will be printed right.
So, in today's lecture we talked about constructor.
So, whenever you are creating or instantiating an object the constructor will be automatically
called.
So, one constructor we had seen default constructor another one is parameters constructor.
The major difference is in the case of default constructor you do not have any parameters
you are not passing any value all right in fact, no argument constructor.
The second one is with arguments right one example we had seen three arguments another
example we had seen one argument so that means, you should have more than one argument right
or greater than or equal to one argument you should have.
So, then you call such type of constructor is a parameterized constructor and after this
we had seen this program where.
So, is it possible to use the member function outside the class and we had seen this particular
example and yes we can use, but we have to follow the syntax that you are seeing in line
number 12.
So, with this I am concluding this particular lecture.
Thank you.
