






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of templates and static members in object-oriented programming (oop) through a lecture series on docsity.com. The use of templates in classes a and the impact of static members on reliability. Additionally, it discusses the implementation of a generic algorithm for finding an element in a container and its application to the vector class.
Typology: Slides
1 / 11
This page cannot be seen from the preview
Don't miss anything!







template< class T > template< class T >
class A { class A {
public:
public:
static int data;
static int data;
static void doSomething( T & );
static void doSomething( T & );
►
►
template< typename T >
template< typename T >
bool isEqual( T x, T y ) {
bool isEqual( T x, T y ) {
return ( x == y ); return ( x == y );
template< typename P, typename T > template< typename P, typename T >
P find( P start, P beyond, P find( P start, P beyond,
const T& x ) {
const T& x ) {
while ( start != beyond &&
while ( start != beyond &&
*start != x )
*start != x )
++start; ++start;
return start; return start;
template< class T >
template< class T >
class Vector { class Vector {
private:
private:
T* ptr; T* ptr;
int size;
int size;
int index; int index; // initialized with zero// initialized with zero
public:
public:
Vector( int = 10 );
Vector( int = 10 );
template< class T >
template< class T >
int Vector< T >::getIndex() const { int Vector< T >::getIndex() const {
return index;
return index;
template< class T >
template< class T >
void Vector< T >::setIndex( int i ) { void Vector< T >::setIndex( int i ) {
if ( index >= 0 && index < size )
if ( index >= 0 && index < size )
index = i; index = i;
int main() {
int main() {
Vector
iv[0] = 10;
iv[0] = 10;
iv[1] = 20; iv[1] = 20;
iv[2] = 30;
iv[2] = 30;
Vector
beyond.setIndex( iv.getSize() );
beyond.setIndex( iv.getSize() );
found = find( iv, beyond, 20 ); found = find( iv, beyond, 20 );
cout<<
cout<< “
Index:
Index: ”
<<found.getIndex();
<<found.getIndex();
return 0; return 0;
►
►
►
►