Programare Orientată Obiect
Template-uri
Template-urile permit scrierea de cod generic care funcționează cu orice tip de date.
templates.cpp
template<typename T>
T maxim(T a, T b) {
return (a > b) ? a : b;
}
cout << maxim(3, 7) << endl; // 7 (int)
cout << maxim(3.14, 2.71) << endl; // 3.14 (double)
cout << maxim('a', 'z') << endl; // z (char)Clasă template
template<typename T>
class Pereche {
public:
T primul, al_doilea;
Pereche(T a, T b) : primul(a), al_doilea(b) {}
T suma() { return primul + al_doilea; }
};
Pereche<int> pi(3, 4);
cout << pi.suma() << endl; // 7
Pereche<double> pd(1.5, 2.5);
cout << pd.suma() << endl; // 4.0STL (Standard Template Library) este construită pe template-uri:
vector<T>, sort<T> etc.