Hello, welcome to the 56th tutorial on the c++ programming series. And in this program we're looking at template classes. template classes local functions allow you to make the class more abstract. This is useful if you need several classes perform essentially the same functionality, but using different data types like integers or floats. And templates are a great feature for this scenario. So let's open up our project and see how to implement sorry about that seek to implement templates within c++.
So what you want to do is to do create template class use the keyword template and then you put angle brackets you just put the keyword class and then we just put an all three value no I'm not number. I should I say a value here like text by the T name, I should say mind's going blank class, then the name of your class and I'm just gonna plug and play class, then in here is going to be public. And we're going to use t i and x. This is basically means whatever data type that this class is declared with t will be that so maybe it's an integer. So anytime you use t will be an integer, or if it's float anytime you use to be a float, but you'll see that a little off later on in this tutorial. So we're going to do template class.
This is the overloaded constructor, we're gonna do t. First, T, second, and then we're gonna simply implement it to implement a function. So you need to do template class and then t again, very similar to appear basically the same you can copy and paste it. then here you do template class. And then in here you put t, which is the value from here, then you colon colon template class. And in here we just input to third to second. And here we just do i equals first, x equals second.
Now what we can do is template class and in here we provide the data type of int called float. We do t t, that's what we're going to call it from that company TT games. 4.5 f 4.7 STD c, t t.on STD line 4.5. But if we were to change this to integer and run this, we get four because even though we passed in float value, because they were set up as integers, these values were truncated, hence why I was four. And that's it really for this tutorial on templates. Obviously, you can use this for functions as well.
And then the functions can perhaps do essentially very similar tasks to other classes, but you can combine them so you could have maybe a class that perform loads of math functions on two pieces of data. And perhaps it's just addition, subtraction, multiplication, maybe something a little more than that, but it's just generic per different class. And the only difference in differences in the classes is the data type with templates, you can get around that. Let's have one class. If you need to modify it, you just modify the one class. So that's it.
For templates. Just bear in mind any function that you implemented, I saw the classes are not in line. These two have the template line proceed in it. This is the same for regular functions, or as demonstrated constructors, basically this line here, if you're declared in line here, the function then you wouldn't need this but because it's not you need it. But yeah, that's it. In the next part of this series, we're gonna be looking at class structures.
If you have any questions feel free to message us at support at solar systems career UK, email will be in the description. You can comment on this video just directly messages via YouTube, or the recording for source code will also be in the description as usual fun fortune and hope you have a nice day.