Hi everyone.
So in this reader we will discuss that topic template.
So I have written some code.
First I will explain you the code.
So I have created this class glass beer so I have a glass beer.
So it has two private properties X and Y and both are in danger.
So then we have public.
So we have set text function.
It will take X and I don't assign the value of x to this X..
Right.
And similarly we have ghettos and setters.
So it is returning the value fix.
Similarly this is set to y function.
It will set the value of y.
And similarly we have got Y function and it is going to return the value of a.
So we have simple getters and setters.
Now what I want to do so I want to create this beer glass.
So this pane of glass has the type in videos so x and y out of type integers.
Now what I want to do I want to create a beer a glass of double.
So how can we do that.
So I will create the glass buildable and inside that X and Y both will readable.
So I will write Double X and I will write to double y.
Basically I will copy paste all this thing here and instead of integer what I will write I will write
double.
So I will replace integer here by double.
Similarly I will replace integer by double here and so on.
So I will replace integer by double and I will replace integer by double.
So just copy paste this code and change in digit to double because now X and Y are of type double.
So now we have a big class of integer and appear a class of double.
Now later I want to create one more class B in a class of character.
So again what I will lose so Class B are character and again so what we have to do we will copy paste
this entire class.
We will copy paste this entire class and it will be straight here.
Copy and paste it here.
The only change that we have to do is basically I will replace in Deja by character.
Everything else will remain the same.
So what we are doing we are copy pasting the class and we are just changing the data type so you can
see there is so much repetition of code.
What is the difference between this class and this class and this class.
So the only difference is of data type.
Everything else is same sometimes the data type is in danger.
Sometimes the type is double and sometimes the data type is character.
So what we can do so we can avoid this.
Copy pasting thing we can use templates
we can use templates.
So how we can use templates.
So what we will do so here what we are writing in Deja X so what I will write.
I will write the x.
Similarly I will write Tevi so how we will read this line.
So the meaning of this line is X is a variable of type.
The Y is a variable of type.
So just like we used write in pediatrics so X is a variable of type in digit.
So similarly X is a variable of type Integer sorry exit a variable of type B and similarly Y is a variable
of type B and modesty.
So t is our temporary Nina type these are temporary integer type.
Now this the value of t can be in danger.
The value of t can buildable.
It can be corrected it can be flawed it can be boolean anything right.
So the value of t can be anything.
Now what we do.
So first let us do this.
So what we have to do instead of writing this Integer i will write these similarly instead of writing
this Integer i will write T.
And similarly I have to replace integer by d every year.
So what I have to do so we will replace we will replace this integer Bayati everywhere.
Right.
So let's do that.
So this this is not in danger.
This is T V entertaining T.
And similarly VI is of type T and Y is of type T right.
So now we do not have integer WS character anywhere.
We only have t so the compiler will say hey I don't know.
Okay.
So compiler will say you are using T every year but if you will compile this file the compiler will
give us error that it does not know modesty.
So what we have to do.
So I have to write a line template type name.
So we have to write this line above class.
So write this line above class.
So let's do that
template type m. and D.
So now the compiler will not give us error because it knows that modesty.
So these are template.
So now the compiler will not give us error if we will try to run this file.
Now how we can create the object of this a class.
So how to create object to a simple simpleton in Texas be happy right.
So what is appear to be so pure B.
So you have this X and Y X and Y and both out of type T.
So what will happen you will get an added Violet because compiler doesn't know what is the value of
t.
So you cannot create objects like this.
So how we will create object be it.
So we have to give the value of t.
So I want you to be integer so I will write parent and B.
So what will happen to the effect of this line.
So again you have X and Y and now what will happen.
So T is going to be replaced by integer.
So these will be replaced by integer here.
So you can see so here.
The value of p is in danger.
So we are passing integer to be so P will be replaced by integer everywhere in the code.
So this is T.
Now this will become integer.
Similarly DST it will become integer and everywhere in the code.
So T is going to be replaced by integer.
So similarly this will become integer and this will become a teacher.
And this will become integer.
So this is this index for creating the objects now and this will be the condition X and Y both are now
integers.
Now I want X and Y should be double and not integer so what I will lose so when we are creating the
object I will simply pass double instead of integer.
So if all right pair double lets it be too.
So what will happen.
The value of p is going to be replaced by a double.
So everywhere in the code.
So this is going to be replaced by the will this be replaced by the will this be replaced by level replaced
by double replaced by double and replaced mutable.
So now what will happen.
So you are x and y both are now double double X double y so you can see with the help of template with
the help of template you do not need to copy paste the class you just need to pass that data type if
possible then you will have x and y both double if you can also pass character you can pass anything
so t can be anything you can pass character B three so it will happen X and Y are now characters.
So the logic is very simple.
The value that you are passing the value that we are passing.
So T is going to be replaced by that value everywhere in the world.
So let's write the code and let's check so let's write the code be it.
Now these should be replaced by integer.
So I am passing integer B1 and now it does give the value of x and y.
So we'll call the function.
So even dot syntax let's say the value is 1.
Similarly even dot sent away the value is 2.
And now let us also print the value so see out we will not get X and we will also print the value of
life.
So we cannot get away
and now let's create a bit of a double.
So I am creating a bit of a double letter the name is B2 so B2 dot surtax.
Let's say one point seventy.
Similarly we do that certainly let's say the value is to point to 1 and again it will also bring devalue.
So you don't get X
beauty art getaway.
So what should be our output.
So it's very simple.
So you're creating a pair of integers so this is P1 and inside P1 both X and Y are of type integers
then you're calling the function statics so set takes the value of these integer.
So you are a passing integer 1 and 2.
Right.
So the value of x is when the valley of y is to the value of Y is true.
And then you're calling the Gattex function.
So get x.
This is in danger.
And similarly this is integer so it will return the value of x and then the value of y.
So at this line my output should be 1.
Then we have space.
So 1 space 2.
And similarly for this P2 what will happen.
We have discussed already.
So it will be double X and double y and everywhere in the code.
So this deal is going to be replaced by double again.
This tree is replaced by the will this be replaced by double.
And also this tree is replaced by level.
So this is double expendable right.
So the logic is very simple.
He is going to be replaced by double everywhere inside the code.
And for this these replaced by integer everywhere.
You have used the.
It will be replaced by integer inside the code.
Then you are calling the next function and get way function.
So the value of X was one point seventy and the value of why was to point to one.
So again your output should be one point seventy three then a space then two point two one.
So let's see where that our logic is correct or not
let's add in this file
so you can see the output.
I have one end to the value of x and y.
And again I have the value of x and y one point seventy three and two point two one.
So that's how it is working.
Now similarly what you can do you can create you can also create pair of characters.
So what you have to do be it off you will positively type your type character and be three.
So in this case what will happen to be three so be three as X and Y.
And this time the D replaced by characters.
So this is character X and character y.
And similarly you can set the value of x you can set the value of a you can call get X function and
get way function and you can print the values of x and y.
Now what I want.
So I have two variables right.
I have X and I m y.
Now I want my X should be in danger and my Y should be double.
So currently what do we have so I am writing if I will write B it often digits then both X and Y will
become integer if I will pass if I will create objects like this pair of double notes.
This is P1 and this is P2.
So if I will pass double then both X and Y will become double.
But what do I want.
I want my X should be integer and my Y should be double.
So how we can do that.
So again it's very simple.
So what we will do.
So here we are using only one embedded in our day.
What we will do we will use one model type.
So I will write template X and sorry D X and v y so X is of type D Y is of type V and then will create
object so will pass the value of the envy.
So I will write like this year end and double
B3.
So what will happen this V.
So this is going to be replaced by integer and V is going to be replaced by W.
So let me write the code so that you will be able to understand to properly write
instead of the X. We will use one more time Brady type little name is V so compiler doesn't know what
is V so what do you do you will add this here.
So type name is we know okay.
So we have to type names the envy we are using to compute D and V so X is of type B VI is of type v
perfect.
So what do we have here.
Yes X is of type T I will return t now VI is of type V and similarly for get Y Y is of type V so what
I'm doing here.
So X is of type T VI is of type V so x type T.
And similarly when are calling get text function we are turning x of type T.
So similarly VI is of type V and here the return type is v simple and also we have updated.
So compiler will say Ah I don't know what s v.
So I have added here type name V so now when we create the object.
So I have to create objects like this why because I have to pass the value of B as well as the value
of B.
So first the value of t should be given.
So the value of t is integer then you have to give the value of V.
So this is V.
So it is just like function.
So in function function takes some arguments and when you want to call this function you have to pass
the arguments and then the arguments will be passed in order right.
So again we have two arguments here.
The end V.
So we are passing the value of first argument which is integer and the value of second argument is double.
So let's do that.
So this is this will not work because you have to pass to a lose.
So combined out everything so you will write like this.
Beer.
What is the first value the value of x is in danger and the value of V is double.
Let's see the name is B1.
So what will happen now so B1 have two values X and Y.
Now y x is of type in digit axis of type Integer and Y is of type double
now similarly where do we look.
We will call these tactics functions that way function and get taxing get way function.
So let's do that so after creating the object so let's call the functions it takes and set way so be
rendered set x so x is in D a right.
So for example I am passing one point seventy and let's call do I.
So we went out said wait and let's say I am passing one point seventy.
Let us call the function get X and get way to devalue to get X
and be rendered get to a
So what should be on output so be when right.
So we have between X and Y X was B.
So initially we had the X and we y.
So the value of these integer.
So this is in danger and the value of V is double.
So this is double.
So we have integer X and double y now settings.
So this should be in D right.
But I am passing one point seventy so what will happen this one point seventy has to be type cluster.
So implicit type casting will take place and I will store only 1 because X is of type Integer and Vi
is of type double.
So one point.
So why the value of y you would be one point seventy three because values of type double since x is
of type in danger.
So implicit typecasting will take place and the value of x is 1 and the value of y is one point seventy
and then I am bringing the value of x so one should be my one should be printed here and one point seventy
should be printed here.
So let's see the output should be one and one point seventy
so you can see the value of x is 1 and the value of y is one point seventy.
So implicit typecasting is taking place in case of X because X is of type Integer and you are passing
the value w I think there should be warning here.
Okay so there is no warning but if you're using a good compiler.
So what you can get.
So if you are using a good compiler sometimes you will get a warning that implicit type casting is taking
place.
So now what else can we do.
So now what I want to do so I want to create a triplet.
So I want to create a triplet
and inside this triplet so it is obvious I will have three values let's say x y and z axis of Type D
Y is of type V and Z is of type W and obviously I will have here a template.
So type name type empty called my type name type name V and type name W.
So if I want to create a template.
So these these are the three values the three variable x y and z so X is of type T values of type V
and Z is of type W.
And here we have defined what our TV and W.
So how do we create the object of this triplet class.
So for creating the object you will write something like this triplet you have to pass three values
the value of B.
So for example D integer let's say vs also integer and later the Blues also integer.
And even so that is how you will create the object of triplet class.
So what will be the effect of this line.
So the effect is very simple X Y and Z all these three will be integer.
Now here also if you want X and Y both should be in danger then instead of passing and comma that what
you can do you can write in combined B1.
So what will be the effect.
So both X and Y both X and Y will be integer.
In this case.
So this is the problem statement that you want to create a tablet.
So the problem statement is create a template with three values X Y and Z.
And these are the B dare type TV and blue.
But the problem is a little bit different.
You cannot create this class.
You have to use peer class.
You have to use spin class to write this triplet class.
So what I want to say here is I do not want to create a new class.
I want to use Peri class to have three values I am a British myself.
I do not want to create this triplet class.
I want to use a class in such a way that this bitter class can act as a prepared class.
So I want this of class to act as if it is a prepared class.
So let's see how we can do that.
So what will happen if you will write fear and mind and B.
So what will happen.
So I have two values x and y.
This is X and this is why so X is of type Integer so this is in digit and Y is of type Integer.
Now what will happen if I write like this bit.
So this was your t and this was your V now what will happen if I write like this.
Bit of deja combined Deja coma and Deja B.
So what will happen.
So we will take the help of this simple example.
So the first value is deep and the second value is we.
So this is the first value.
So this is deep and this is the second value.
So this is the So let's draw the diagram so this big B has two values X and Y.
So this is X and this is why what is why.
Why is it simply in danger.
So why is it simply integer.
What is x.
So X is of type B and D appear so again it is appeared and since it does appear it will have two values
X and Y.
Now what is the value of this X.
So this is in Egypt.
This is integer and what is the value of y.
So why is also integer.
So the effect of this line is basically your peer class will look no object will look something like
this.
So yes what you can do so what is T.
These are temporary data type and you can replace T.
Yes you can replace T with objects like this.
So this is nothing it is just idea type right.
So you can replace T with something like this user defined your type.
So this is user defined their type.
So you can replace T with the user defined your type also.
Now my question is what I want to do.
So now we have three values right.
So this is the first one.
This is the second value and this is the third value so I am able to store three values.
Now what I want to do I want to write 10 here.
I want to write 10 here.
I want to write 20 here and I want to write party here.
So how can we do that.
So the object the name of the object is B.
So putting party here is very simple what I will do so I will write like this.
B DOT said we beat out Saturday and I will pass the value today.
So today will be placed here.
Now the question is how to put pen here and how to put 20 here.
So what I will do B Dot said X so this is X right.
So I am calling the function settings now set X function what it takes as input.
So if you can see where is set x rays yes.
So this is a static function.
It takes input D and what is P.
These appear these appear so you cannot give values Ten Commandments No this is wrong.
Why this is wrong because now do the type is.
So you have to pass a appear.
You have to pass up period.
So what I will do I will create one more bit.
So let's create one more beer.
So beer and combined went into combined.
Because I have to put pen into India and let's say the name is before.
Now before that surtax has been and before that Saturday is 20.
So what is before.
So P four has two values X and Y X and Y.
X is of type in Deja VI is of type Integer we are calling the functions are text to the value of 10.
So then the liberals today are calling the value you are calling the function set way and they will
use 20 so twenty will replaced here and now let's rewrite dysfunction.
So be DOT said X so this is X and what it takes.
So it takes a bit as input and we will pass this bill before.
So I will pass before here.
So I am passing before here and what is before and so before is this simple P an X and Y.
So what will happen.
These two values 10 and 20 will get copied.
So 10 will be placed here and twenty will be placed here.
So that's how we will be able to put 10 and 20 here.
Now if you want to print the value so I want to print the value.
So in order to print the value so be dot let's say you want to print 30.
So it is very simple.
You will simply call the function get Y and Gateway function will return.
We saw Gateway function earlier than we and we is in danger.
So this is v v as in digit.
So the output will be then sorry output and retard.
Now I want to print 10 and 20 also.
So if I will call the function and get X if I will call the function get x.
So this is good check function it will return to and modesty so D is a bit off and Deja common.
So this thing is going to return me a bit.
So this beer will be returned to me and I want to print the value of x.
So what I will do I will write get X so the output will return.
And similarly if I want to print divide I lose a b dart get X so get X it will return me appeared so
it is going to return a bit and I want to end of that pier I want to access the value of y so I will
call get Y.
So the output will be n here.
Okay so that's how everything is working.
So now let's command out everything and let's write the code for this one
so let's come and don't have anything
and let us it is the screen also we will write the code from fresh
okay.
So I want to use.
So the problem statement was you want to use PID so that it can behave as a template by triplet behaving
means you guys you will be able to store three values.
So with the help of a glass you against all three values like this.
So.
Beer.
Beer and.
And so this is the value of the.
And this is the value of me.
Let's say the name of the object this be and let us set the value of y first.
So certainly the value was tardy as decided.
And now it will do so we have to create a beer first.
So let's give it one more beer.
So beer in Deja in Deja let's call it temp and temp dart sceptics the value is 10 so temp.
Not certainly the value is 20.
And what do we do now.
So be not stocks this set X function it will take as input and I am passing temp so now my beard is
ready.
So what I have to do I have to print the values now.
So let's print the values so let's see out
be dart get x.
So it will return me up here and of that bet I want to access the x value.
So print x God get x.
Similarly
again call B dot get x.
So it is going to return me a bit and of that bit I want to exist.
The y values so get Y and now and again do so we can print 30.
So for printing that 30 what will I do.
So be dot get Y
Okay so let's first run of a code and let us first run this file and then we will brighten what is happening
so you can see the output is 10 20 and 30.
So this is all that we have wanted.
So this this is the correct output 10 20 and 30.
And now let's see how everything is working let's write a novel code.
So yes.
So this is B.
Okay so create an object.
So this B has two things X and Y.
So this is your X and this is your way.
Okay so X is this appeared.
So if you remember we have d x and v vi so VI is in danger.
So this is simply deja and X is B and D again appeared so again appear.
That means you will have again X and Y.
So this is your x and this is your right.
And both out of indigent integer and Deja.
So this is also in Egypt and this is also integer fine.
So be that set the value function.
So this is set to y you are passing today.
So what will happen 10 will be present here.
Simple.
You are creating at the appeared temp so let's create and then better temp so this temp will have x
and y.
So this is temp and this is B.
So this is temp right in deja A.J..
OK so this is in danger and this is all turned Deja now.
Settings you are passing ten so ten was replaced here said Dwight you are passing 20 so now 20 is presented
so you are calling B dot site X. So this is B This was b so B Dart said x.
So that means this thing and this thing requires a b it as input it requires Hep B it is input and you
are passing temp so temp is up here so you are passing this be a hard copy assignment operator will
be called here
okay so internally copy assignment operator will be called and these two values 10 and 20 will be copied.
So 10 will be copied here and 20 will be copied here.
So internally we are calling the copy assignment operator to copy these two values 10 and 20.
So now 10 is presented and 20 is present here then you are calling B dot get X so b don't get X it will
return you appear.
So this part is going to return you appear and of that page you want to X is the value x so get X you
will call you and get in.
So that's how N is getting printed here then we have space.
So again be dot and get x.
So this is X again you will get up here.
So again you will get up here.
So this is a bit and then you are calling get Y functions so this is 20.
So that's how it guarantees getting printed here and this is very simple.
Bedard get.
So be dot get Y.
So today will be printed here.
So that's how your output becomes so 10 then 20 and then 30.
So then then space 2010 spaced happy.
So that's how everything is working.
So now for example if you want to create a triplet such that the first value is in digit the second
value let's say is double and that total value is related to so you want to create a place like this.
So what will be this index
now if you want you can pass this bit as b or you can pass this bit as we so let's hear.
So the first rule losing digits I am passing and beach ahead.
Now the second value I have to pass.
So I will look club these two for example I am loving these two.
So this too will become a beer.
So beer what is the first value double and what is the second value character.
Right.
Simple.
And this is B.
So what will happen where will the effect of this line again you will get two values X and Y X and Y.
X is integer so x is in it.
So right now is a peer.
So this is appeared again.
I will have two parts x and y two the first is double.
So here you have double and the second is character.
So here you have Gary.
So this is how everything is working.
Now one more thing.
So this is a nesting right so you can do many levels of nesting.
Okay.
So so you can do as many levels of nesting as you want.
So this is one level of nesting right.
So this is first level of nesting.
So speaking from my own experience you will never acquire listing of second level or more.
So the first level nesting is more than enough.
So this is more than enough.
This is more than enough.
So first level of nesting is good enough.
You will never encountered a problem in which you need to use second level of nesting or mode.
So this is all in templates.
So if you have any doubt in templates you can ask me.
OK so this is it from this video.
Thank you.