
 |
![]() |
|
|
|
|
 |
Class Templates
We can now move onto the class template. They work they same way as the function templates. The only different is the template generates classes instead of functions. Here is the syntax of the class template:
 | |  | | |
template <class T>
class dog
{
//code
}; |
|  |
|
Here is an example of using the class template with the class Employee.
 | |  | | |
//temp.h
template <class T>
class Employee
{
public:
T getAge() { return itsAge;}
void setAge(T age);
private:
T itsAge;
};
template <class T>
void Employee::setAge(T age)
{
itsAge = age;
} |
|  |
|
|
Note: this assumes that the equal operator is overloaded for the age type. If you are using a standard type such as int, long, short, float, double, char, etc.. you will not need to worry about that. |  |
|  |
When a template class is created it makes the class an abstract class, and the class cannot be instantiated unless you pass in the type. Therefore, the following can be instantiated in the following manner:
 | |  | | |
ClassName <type> varName(constructor arguments if any); |
|  |
|
Observe the following example:
 | |  | | |
// purchase.h
template <class ItemType, class TagType>
class purchase
{
private:
ItemType m_item; // the item to purchase
TagType m_price; // the price of the item
// more [...]
public:
purchase(ItemType item, TagType price); // default constructor
//~purchase(); // destructor (theres one provided)
ItemType getItem();
TagType getPrice();
// more methods here [...]
} |
|  |
|
 | |  | | |
// purchase.cpp
#include "purchase.h"
template <class ItemType, class TagType>
purchase <ItemType, TagType>::purchase (ItemType item, TagType price)
{
m_item = item;
m_price = price;
}
template <class ItemType, class TagType>
ItemType purchase<ItemType, TagType>::getItem ()
{
return m_item;
}
template <class ItemType, class TagType>
TagType purchase<ItemType, TagType>::getPrice ()
{
return m_price;
} |
|  |
|
 | |  | | |
// test.cpp
#include <iostream.h>
#include "string.h" // just a standard string class
#include "purchase.h"
int main(void)
{
string Soda = "CokaCola";
float Price = 2.45F; // the ‘F’ is short for float, this will force the type
purchase <string,float> myOrder(Soda, Price);
// Note: its not necesary to tell it the
// types beforehand <string, float> because
// the compiler (having created the above variables
// knows which type they are and can fill them
// in for you.
// but its usefull if you want to cast or use constants.
cout << "You have Purchased:" << endl;
cout << myOrder.getItem() << endl; // CokaCola
cout << "$" << myOrder.getPrice() << endl; // .45
return 0;
}
// Output:
// You have Purchased:
// CokaCola
// .45 |
|  |
|
So now you created a purchase template, so what?? I will tell you why it is much better than just building, say a purchase class. First of all, your purchase template can support ANY type of Item and ANY type of price. Second, your purchase class will not be created at all if you do not use it and will only be compiled once for each pair of types you use it with. Imagine trying to do that with normal classes. Now we're talking.Please Rate this Code:
Comments for: Class Templates
There are NO user contributed comments for Class Templates.
|
 |
|
 |
|
|
|
|
|
|
|