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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Motivation, Prints an array, Integer elements, Array of characters, Array of doubles, Array of integers, Wraps arrays, Arrays of boolean variables, Generic Programming are the points you can learn in this object oriented programming subject.
Typology: Slides
1 / 24
void printArray(int* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }
void printArray(char* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }
void printArray(double* array, int size) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }
class Array { int* pArray; int size; public: … };
class Array { double* pArray; int size; public: … };
class Array { bool* pArray; int size; public: … };
template< class T > void funName( T x ); // OR template< typename T > void funName( T x ); // OR template< class T, class U, … > void funName( T x, U y, … );
template< typename T > void printArray( T* array, int size ) { for ( int i = 0; i < size; i++ ) cout << array[ i ] << “, ”; }
int main() { int iArray[5] = { 1, 2, 3, 4, 5 }; void printArray( iArray, 5 ); // Instantiated for int[] char cArray[3] = { ‘a’, ‘b’, ‘c’ }; void printArray( cArray, 3 ); // Instantiated for char[] return 0; }
template
int main() { int x; x = getInput(); // Error! double y; y = getInput(); // Error! }
int main() { int x; x = getInput< int >(); double y; y = getInput< double >(); }
template< typename T > bool isEqual( T x, T y ) { return ( x == y ); }
int main { isEqual( 5, 6 ); // OK isEqual( 7.5, 7.5 ); // OK isEqual( “abc”, “xyz” ); // Logical Error! return 0; }
template< > bool isEqual< const char* >( const char* x, const char* y ) { return ( strcmp( x, y ) == 0 ); }
int main { isEqual( 5, 6 ); // Target: general template isEqual( 7.5, 7.5 ); // Target: general template isEqual( “abc”, “xyz” ); // Target: user specialization return 0; }