This content introduces the fundamental concepts of classes in programming, explaining their structure, purpose, and key components like constructors and destructors. It emphasizes how classes allow for the creation of complex custom data types by encapsulating both data (state) and behavior (methods).
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
hello welcome to preim Technologies I am
wer this is part 19 introduction to
classes in this session we will learn
what a class is purpose of a class
Constructor overloading class
Constructors understanding this keyword and
and
destructors so what is a class so far in
this video tutorial we have seen very
simple data types like integer float
double Boolean Etc now if you want to
create create a complex custom type then
we can make use of classes what do we
mean by a complex custom type let's say
for example if I want to store a number
I can do that using an integer variable
but if I want to store my customer
information then I can make use of
classes what kind of information does a
customer has he has first name last name
date of birth email Etc Now using the
built-in fields we can create a class
and also not only a customer class you
know it not only contains data it can
also do certain things like save the
customer to the database print the
customer full name so essentially a
class consists of data represented by
its fields and behavior represented by
the methods okay so a class has State
and behavior state is nothing but the
data behavior is nothing but what is the
class capable of doing for examp example
we can save a customer to a database we
can print his full name Etc let's look
at an example you know which will make things
clear okay so how do we create a class
to create a class we use the class
keyword followed by the name of the
class so class
customer and all of us know customer
will have first name and last name so
let's create them string
first name actually we will use an
underscore string first name
name
String last name so these two Fields
represent the state the data that this
class is having okay the first name and
last name okay now to initialize these
fields a class can also have a
Constructor so how does a Constructor
look like a con instructor will have the
same name as that of your class okay so
Public public is nothing but the access
modifier we will be talking about access
modifiers in a later session in a great
detail so access modifier and then the
name of the class public
customer and what is the purpose of
Constructors in life Constructors are
basically used to initialize your class
Fields okay we will we'll later
understand in a bit how we use this
Constructor okay so for now I'm passing
two parameters into this Constructor
string first name
name
String last
name okay so this is the Constructor
it's having the same name as that of
your class okay and it can take
parameters but a Constructor does not
return a value whereas a method can have
a return type and if your method doesn't
written anything you know the return
type is void otherwise you can have any
return return type like inlow double
Boolean Etc but a Constructor you know
the way you can identify this as a
Constructor is by looking at its name it
will have the same name as that of class
and it will not have a return return
type but a Constructor can take
parameters now I am actually using this
Constructor to initialize these two
class Fields first name
and last name so I can simply say
underscore so this field is equal
to the parameter that we are passing
into this Constructor similarly
underscore last name is equal to the
parameter that we are passing into this
Constructor okay now you can refer to
this this class field you know just by
its name or you can say this Dot and
look at this the moment I say this dot
the intellisense shows both the class
Fields underscore first name underscore
last name okay so now this keyword
actually refers to an instance of this
class to an object of this
class okay so you can either say you
know underscore first name or you know
for better readability you can use this
keyword this. last name okay so
basically we are using this Constructor
to initialize both of these class Fields
with these parameters that we are
passing into this Constructor and this
Constructor will get called
automatically when we create an instance
of this class we'll have a look at that
in a
bit okay so so far we have two fields
and a Constructor to initialize those
tool Fields so a class has some state
right now represented by these tool
Fields now let's say I want to add some
behavior in the sense the class should
be capable of doing something for
example I want this class to be in a
position to print the full name of our
customer okay so for that let's have a
function let's have a method and the
written type of the method is void
public void and I'm going to call this
method print full name so what is this
method going to do this method is going
to print the full name of this customer
line full
to underscore first
name have a space in between first name
and last name and _ last name again if
you want to refer to this class Fields
you can just you know type in the fields
just like this or for better readability
you can use the this keyword and as we
know this keyword refers to an instance
of this class to an in to an to an
object of this
class okay so if you look at our class
now it has got two Fields a Constructor
and a method apart from that a class can
also have destructors and destructors
will have the same name as that of your
class but they don't take parameters
they cannot have written type and they
have a tiled in front of them now
usually in C you don't require
destructors know the objects you know
basically we use destructors to clean up
the resources to clean up any resources
your class was holding on on to during
its lifetime okay so any code to clean
up those resources goes in this
distructor and these destructors we
don't have to call them these
destructors are called automatically by
the garbage collector you know when it
when it kind of tries to clean objects
from the memory we will be talking about
distractors and garbage collection in a
very great detail in a later session for
now just understand that a Destructor
will have have the same name as that of
your class it cannot take parameters and
it has a tilled symbol in front of it
and destructors are basically used to
clean up any resources that your class
was holding on to during its lifetime
and these are automatically called by
the garbage collector when it turns up
to clean your objects from the
memory okay so your cleanup code goes
here and this is your method you know
which represents the behavior
this is your Constructor you know
basically used to initialize your class
Fields now if I want to use this class I
can do that in our main method okay so
we can go ahead and create a an instance
of this class customer C1 is equal to
new customer now look at this I'm
creating C1 is nothing but an instance
of this class so I'm creating an
instance of customer class using the new
keyword the instance is also called as
object object and instance these terms
are used interchangeably so I'm creating
a new customer object now look at this
the moment I open up this parenthesis it
shows up our
Constructor this Constructor which takes
in first name and last name and let's
say for example I am passing in the
first name as Pim and the last name as Tech
Technologies so what's going to happen
now this preim will be passed into this
parameter technologies will be passed
into this parameter and these parameters
are then used to initialize these two
class Fields so understand that you know
this is the
Constructor which is getting called
automatically when we're creating an
instance of this customer class
okay so what are Constructors
Constructors are called automatically
when you create an instance of your
class and what is the purpose of this
Constructor it is basically used to
initialize your class Fields you know
this do first name this do last name
will now be initialized to first name to
regime and last name to Technologies so
that you know makes it clear the
definition of Constructor a Constructor
is called automatically when you create
an instance of this CL class and the
Constructor is basically used to
initialize those those class
members okay and then if you want to
print the customer full name you just
say C1 print full name and if you look
at this you know after we create this
object we are calling the print full
name so now in this customer object the
state which is the first name and last
name fields are initialized to piman
Technologies and your print full name is
actually concatenating them together
together and writing them out to the
console okay so your Constructor is
ensuring that this fields are populated
before we actually use those fields in this
this
method okay so that proves that a
Constructor is used to initialize your
class fields and it's automatically
called when you try to create an
instance with your class okay so if we
go ahead and run this program as you
might expect full name is equal to preim
Tech Technologies
now is it mandatory that we have to
provide a Constructor every time we
create a class not necessarily for
example let's comment this you know
Constructor and you can do that by
clicking that button so once I comment
that obviously you know since we don't
have that Constructor you know it gives
an error there is no you know if you
look at the error customer does not
contain a Constructor that takes two
arguments there is no Constructor which
takes two arguments so now we can't do
that but what I can do is look at this
when I say new
customer automatically I'm getting a
Constructor without any parameters if
you look at this customer open
parenthesis close parenthesis it doesn't
take any parameters so what does that
mean if you do not write a Constructor
if you do not provide a Constructor for
your class NET Framework will provide
one for you for free okay and it is a
default parameterless Constructor okay
there are no parameters the default
Constructor provided by NET Framework is
a parameterless default
Constructor okay and this default
Constructor what it does is basically
walks through all the fields in the
class and initialize them to their
defaults now look at this when I call
this default customer this first name
and last name they are not initialized
to anything okay they're basically empty
so now if I go ahead and run this
application look at what's going to
happen full name is equal to empty and
now somebody is able to use you know
call this method in spite of those
fields not being initialized that's why
you have that full name is equal to
empty okay so this default parameter
less Constructor is not really that
useful and if you want to make that a
little more useful what you can do is
basically you can you can provide you
know let's say I have this Constructor
but another thing to keep in mind look
at this the moment I provide a
Constructor you know I have provided a
Constructor for my own class and the
moment I have done that look at to look
at what happens to the default parameter
L Constructor now customer does not
contain a Constructor that takes zero
argument so what's happening here is
that once we provide our own Constructor
to this class you know net takes away
that default parameter less Constructor
which it has provided when your class
doesn't have any
Constructor okay so so it's taking away
that Constructor that's why it says the
AA message customer does not contain a
Constructor that takes zero
arguments okay so now let's say we want
to provide that capability you know
somebody should be able to create a
customer object without passing in any
parameters to the Constructor so how do
you do that okay you can actually write
a Constructor which does not take any
arguments something like this public
public
customer and then what you can basically
do is when you call this customer I mean
when somebody calls that Constructor I want
basically I want basically the
Constructor which takes two parameters
to be called but then I'm going to
initialize them to their defaults like
you know um
no first name
name
provided and similarly for the last name
basically okay if somebody calls this
default you know if somebody wants to
create a customer object without passing
in parameters that's fine they can do
that like this okay customer C1 is equal
to new customer now when this if you
right click on that and when you say go
to definition it comes to this
Constructor but if you look at this
Constructor it's called calling this
customer this Constructor this refers to
this instance of the class so this here
refers to this particular Constructor
and passing in no first name provided
for first name and no last name provided
for last name properties and these are
then used to initialize these class
Fields so obviously now if somebody is
creating a customer object like this and
if you go ahead and run that look at
that no first name provided is their
first name no no last name provided is
their last
name okay so if you want to make that
default parameter L Constructor a little
more useful and if you want to make sure
that your class fields are initialized
to something before you know somebody
calls methods and do something with that
class then you can provide an
implementation for that parameterless
Constructor and invoke any of the you
know parameter constru any of the
constructors with parameters to
initialize your class Fields the way you
want then okay but on the other hand you
know if somebody else wants to create a
customer another customer object they
can do customer C2 is equal to new
customer and now look at this there are
two Constructors now one Constructor
which does not if you look at this one
of two Constructors one Constructor if
you look on the right side no parameters
okay parameter less Constructor there
there is no need to pass parameters I
can do that or
there is another Constructor which takes
first name and last name so there are
two Constructors with the same name you
know differing by just the number of
parameters so this is called as
overloading Constructors so this class
has two Constructors one which does not
take parameters at all another one which
takes two parameters okay basically we
can overload a Constructor you know on
the number and type of parameters
okay say for example p is my first name
and maybe T is my last
name and if I say C2 do printful name so
for the second customer it will say PT p
slides so purpose of a class Constructor
the purpose of a CL class Constructor is
to initialize your class Fields a class
Constructor is automatically called when
an instance of a class is created
Constructors do not have written values
and always have the same name as that of
your class yeah that's that's one
important difference to keep between a
Constructor and a class methods I'm
sorry Constructor and methods methods
will have written type whereas your
Constructor doesn't have your doesn't
have any written type and they have the
same name as that of your class
Constructors are not mandatory if we do
not provide Constructor a default
parameter less Constructor is
automatically provided by NET Framework
but once we provide an implementation
you know of a Constructor the default
parameter less Constructor Constructor
provided by NET Framework is taken away
and Constructors can be overloaded by
the number and type of parameters and we
have seen an example of that as
well so what are destructors destructors
have the same name as that of your class
with a till symbol in front of them they
don't don't take any parameters and they
don't have written values as well
destructors are places where you put
code to release any resources your class
was holding on to during its lifetime we
will actually talk about destructors and
the process of garbage collection in a later
later
session that's it for today thank you
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.