4. Templates
•Templates allows you to :
•define classes and functions that are much more general .
•define functions and classes that have parameters for type names.
6. Function Templates
•Function swapValues swap between variables1 and variables2 :
•With Template … MORE GENERAL !!
7. Function Templates
•Function swapValues swap between variables1 and variables2 :
•With Template … MORE GENERAL !!
Template prefix
8. Function Templates
•Function swapValues swap between variables1 and variables2 :
•With Template … MORE GENERAL !!
Template < class T , Z …..>
9. Function Templates
•Function swapValues swap between variables1 and variables2 :
•With Template … MORE GENERAL !!
Template works only if the definition be in the same file where we calling
14. Class Templates
#include <iostream>
using std::cout;
using std::endl;
template<class T>
class Pair
{
public:
Pair( );
Pair(T firstValue, T secondValue);
void setFirst(T newValue);
void setSecond(T newValue);
T getFirst( ) const;
T getSecond( ) const;
private:
T first;
T second;
};
int main(){
cout<<"Object representing pair of int values :n";
cout<<" Object name: scoren";
Pair<int> score(3,0);
Cout <<" object value: {" << score.getFirst()
<<", "<< score.getSecond() << "}n";
cout<<"object representing pair of char values :n";
cout<<" Object name: seatsn";
Pair<char> seats;
seats.setFirst(65);//A
seats.setSecond(90);//Z
cout <<" object value: {" << seats.getFirst()
<< ", " << seats.getSecond() << "}n";
}
15. Class Templates
#include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; }
T in members ( functions + variables )
16. Class Templates
#include <iostream> using std::cout; using std::endl; template<class T> class Pair { public: Pair( ); Pair(T firstValue, T secondValue); void setFirst(T newValue); void setSecond(T newValue); T getFirst( ) const; T getSecond( ) const; private: T first; T second; }; int main(){ cout<<"Object representing pair of int values :n"; cout<<" Object name: scoren"; Pair<int> score(3,0); Cout <<" object value: {" << score.getFirst() <<", "<< score.getSecond() << "}n"; cout<<"object representing pair of char values :n"; cout<<" Object name: seatsn"; Pair<char> seats; seats.setFirst(65);//A seats.setSecond(90);//Z cout <<" object value: {" << seats.getFirst() << ", " << seats.getSecond() << "}n"; }
When you define an Object from a class template
put the Type between “ <“ “>”
17. Class Templates
template<class T>
Pair<T>::Pair()
{
first ;
second ;
}
template<class T>
Pair<T>::Pair(T firstValue, T secondValue)
{
first = firstValue;
second = secondValue;
}
template<class T>
void Pair<T>::setFirst(T newValue)
{
first = newValue;
}
template<class T>
T Pair<T>::getFirst( ) const
{
return first;
}
template<class T>
T Pair<T>::getSecond( ) const
{
return second;
}
template<class T>
void Pair<T>::setSecond(T newValue)
{
second = newValue;
}
18. Class Templates
template<class T> Pair<T>::Pair() { first ; second ; } template<class T> Pair<T>::Pair(T firstValue, T secondValue) { first = firstValue; second = secondValue; } template<class T> void Pair<T>::setFirst(T newValue) { first = newValue; } template<class T> T Pair<T>::getFirst( ) const { return first; } template<class T> T Pair<T>::getSecond( ) const { return second; } template<class T> void Pair<T>::setSecond(T newValue) { second = newValue; }
You have to prefix the
template<class T>
Before the function class Template members
19. Class Templates
template<class T>
int addUp(const Pair<T>& thePair);
•When you define the function member in class template :
• the function gets a classTemplate object parameter