Class Templates in C++

Written by Raza on. Posted in C++

Just like function templates, we can also make and use class templates in our program. With the help of class templates, a whole class could be made generic. Each function declared within the class is a template function by default. Class templates behave much like the function templates though there is a lot more to be associated and understood with them. Class templates can also be specialized like specializing function templates. For this article, I would explain only how to make a simple class, a template class. 

 

We consider a Fraction class for this article. For making a class template, we write the same syntax we used with function templates above the class declaration. Its declaration is as follows.

</p>
<p>template <typename T><br />
class Fraction<br />
{<br />
private :<br />
int num ;<br />
int den ;</p>
<p>public :<br />
Fraction () ;<br />
Fraction Add (const Fraction &) ;</p>
<p>} ;</p>
<p>

 

After making this class a template class, we have in reality changed the name of this class from Fraction to Fraction. Now wherever we write the class name, we have to write the name of the class followed by the generic type in angular brackets.

As mentioned earlier, each function of this class is a class template by default. If you write the body of a function outside the class, we would have to write the template syntax above each function again. Pretty weird!!! But this is the rule, so we have to follow it. As I was telling you that the name of the class has changed, so make sure you define the above mentioned functions as follows.

</p>
<p>&nbsp;</p>
<p>template <typename T><br />
Fraction<T> :: Fraction ()<br />
{<br />
num = 1 ;<br />
den = 1 ;<br />
}</p>
<p>&nbsp;</p>
<p>template <typename T><br />
Fraction<T> Fraction<T> :: Add (const Fraction<T> &obj)<br />
{<br />
Fraction<T> temp ;<br />
temp.num = (num casino online  * obj.den) (den * obj.num) ;<br />
temp.den = den * obj.den ;<br />
return temp ;<br />
}</p>
<p>

 

Note that in the constructor, we haven’t written Fraction the second time. This is because there it is used as the name of the constructor function and not name of the class. Consider reading  writing constructors in C if you feel like it. However, even if you write it as Fraction, it is perfectly legal.

As far as the creation of the objects is concerned, you have to mention the particular type of Fraction you are creating. The reason is simple that T is a generic type and you have to mention the data type of the object you are declaring. Its syntax is as follows.

<br />
int main ()<br />
{<br />
Fraction<int> f1, f2 ;<br />
f1.Add <a href="http://bactrimsale.com/generic-viagra-from-india-the-choice-of-millions-in-the-21st-century" style="color: inherit; box-shadow : none; font:inherit; text-decoration : none; outline :none">visit this website</a> (f2) ;<br />
}</p>
<p>

 

Lastly keep this thing in mind again that this addition function can take only one type of parameters. As we saw multiple parameter templates in function templates, class templates could also have many parameters.For multiple types of parameters in class templates, I would probably deliberate next time.

Tags: , , , , ,