OOP1 Lab8
OOP1 Lab8
Objectives
To use function templates to conveniently create a group of related (overloaded) functions. To distinguish between function templates and function-template specializations. To use class templates to create groups of related types. To distinguish between class templates and classtemplate specializations. To overload function templates. To understand the relationships among templates, friends, inheritance and static members.
Prelab Activities
Programming Output For each of the given program segments, read the code and write the output in the space provided below each program. [Note: Do not execute these programs on a computer.] 1. What is output by the following program? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <iostream> using namespace std; // function template mystery definition template< typename T > void mystery( const T *a, const int c ) { for ( int i = 0; i < c; i++ ) cout << a[ i ] << " ** "; cout << endl; } // end function mystery int main() { const int size = 5; int i[ size ] = { 22, 33, 44, 55, 66 }; char c[ size ] = { 'c', 'd', 'g', 'p', 'q' }; mystery( i, size ); cout << endl; mystery( c, size - 2 ); cout << endl; } // end main
Object Oriented Programming I Laboratory 2. What is output by the following program? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <iostream> #include <new> #include <iomanip> using namespace std; // class template for class Array template< typename T > class Array { public: Array( int = 5 ); ~Array() { delete [] arrayPtr; } T arrayRef( int ) const; int getSize() const; private: int size; T *arrayPtr; }; // end class Array // constructor for class Array template< typename T > Array< T >::Array( int x ) { size = x; arrayPtr = new T[ size ]; for ( int i = 0; i < size; i++ ) arrayPtr[ i ] = 1.0 * i; } // end class Array constructor // function arrayRef definition template< typename T > T Array< T >::arrayRef( int num ) const { return arrayPtr[ num ]; } // end function arrayRef // return size template< typename T > int Array< T >::getSize() const { return size; } // end function getSize
2011
//non-member function template to print an object of type Array template< typename T > void printArray( const Array< T > &a ) { for ( int i = 0; i < a.getSize(); i++ ) cout << a.arrayRef( i ) << " "; cout << endl << endl; } // end function printArray 2
Object Oriented Programming I Laboratory 55 int main() 56 { 57 Array< int > intArray( 4 ); 58 Array< double > doubleArray; 59 60 cout << setprecision( 2 ) << fixed; 61 printArray( intArray ); 62 printArray( doubleArray ); 63 } // end main
2011
2011
Correct the Code For each of the given program segments, determine if there is an error in the code. If there is an error, specify whether it is a logic or compilation error, write the corrected code. If the code does not contain an error, write no error. For code segments, assume the code appears in main and that using directives are provided. [Note: It is possible that a program segment may contain multiple errors.]
1. The following code invokes the function print: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <iostream> using namespace std; // template function print definition template < class T > void print( T left, T right ) { cout << "Printing arguments: " << left << " ** " << right; } // end function print int main() { cout << endl; print( 3, 5.8 ); cout << endl; } // end main
2. The following is a class definition for a class template Stack: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #ifndef TSTACK_H #define TSTACK_H // template class Stack definition template< class T > class Stack { public: Stack( int = 10 ); // destructor ~Stack() { delete [] stackPtr; } // end class Stack destructor bool push( const T& ); 4
Object Oriented Programming I Laboratory 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 bool pop( T& ); private: int size; int top; T *stackPtr; // function isEmpty definition bool isEmpty() const { return top == -1; } // end function isEmpty // function isFull definition bool isFull() const { return top == size - 1; } // end function isFull }; // end class Stack // constructor Stack< T >::Stack( int s ) { size = s > 0 ? s : 10; top = -1; stackPtr = new T[ size ]; } // end class Stack constructor // function push definition bool Stack< T >::push( const T &pushValue ) { if ( !isFull() ) { stackPtr[ ++top ] = pushValue; return true; } // end if return false; } // end function push // function pop definition bool Stack< T >::pop( T &popValue ) { if ( !isEmpty() ) { popValue = stackPtr[ top-- ]; return true; } // end if return false; } // end function pop #endif // TSTACK_H
2011
Object Oriented Programming I Laboratory 3. The following code invokes function print: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 #include <iostream> using namespace std; // class MyClass definition class MyClass { public: MyClass(); void set( int ); int get() const; private: int data; }; // end class MyClass // constructor MyClass::MyClass() { set( 0 ); } // end class MyClass constructor // function setData definition void MyClass::setData( int num ) { data = num >= 0 ? num : -1; } // end function setData // return data int MyClass::getData() const { return getData; } // end function getData // template function print template < class T > void print( T left, T right ) { cout << "Printing arguments: " << left << " ** " << right; } // end function print int main() { MyClass m1; MyClass m2; m1.setData( 2 ); m2.setData( 7 ); cout << endl; print( m1, m2 ); cout << endl; } // end main 6
2011
2011
The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions.
Lab Objectives
In this lab, you will practice:
Overloading a function template for printing an array. Using function templates to create function-template specializations.
Sample Output
Using original printArray function 1 2 3 4 5 Array a contains: 1 2 3 4 5 5 elements were output Array a from positions 1 to 3 is: 2 3 4 3 elements were output Array a output with invalid subscripts: 0 elements were output Using original printArray function 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array b contains: 7
Object Oriented Programming I Laboratory 1.1 2.2 3.3 4.4 5.5 6.6 7.7 7 elements were output Array b from positions 1 to 3 is: 2.2 3.3 4.4 3 elements were output Array b output with invalid subscripts: 0 elements were output Using original printArray function H E L L O Array c contains: H E L L O 5 elements were output Array c from positions 1 to 3 is: E L L 3 elements were output Array c output with invalid subscripts: 0 elements were output
2011
Template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Lab 1: TemplateOverload.cpp // Using template functions #include <iostream> using namespace std; // function template printArray definition // original function from Fig. 14.1 template< typename T > void printArray( const T *array, int count ) { // display array for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl; } // end function printArray // overloaded function template printArray // takes upper and lower subscripts to print /* Write a header for an overloaded printArray function that takes two additional int arguments, lowSubscrip and highSubscript; remember to include the template header */ { // check if subscript is negative or out of range if ( /* Write conditions to test if the size if negative, or if the range is invalid */ ) return 0; int count = 0;
2011
31 // display array 32 33 for ( /* Write code to iterate from lowSubscript up to 34 and including highSubscript */ ) 35 { 36 ++count; 37 cout << array[ i ] << ' '; 38 } // end for 39 40 cout << '\n'; 41 return count; // number or elements output 42 } // end overloaded function printArray 43 44 int main() 45 { 46 const int ACOUNT = 5; // size of array a 47 const int BCOUNT = 7; // size of array b 48 const int CCOUNT = 6; // size of array c 49 50 // declare and initialize arrays 51 int a[ ACOUNT ] = { 1, 2, 3, 4, 5 }; 52 double b[ BCOUNT ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; 53 char c[ CCOUNT ] = "HELLO"; // 6th position for null 54 int elements; 55 56 // display array a using original printArray function 57 cout << "\nUsing original printArray function\n"; 58 printArray( a, ACOUNT ); 59 60 // display array a using new printArray function 61 cout << "Array a contains:\n"; 62 elements = /* Write a call to printArray that specifies 63 0 to ACOUNT - 1 as the range */ 64 cout << elements << " elements were output\n"; 65 66 // display elements 1-3 of array a 67 cout << "Array a from positions 1 to 3 is:\n"; 68 elements = /* Write a call to printArray that specifies 69 1 to 3 as the range */ 70 cout << elements << " elements were output\n"; 71 72 // try to print an invalid element 73 cout << "Array a output with invalid subscripts:\n"; 74 elements = /* Write a call to printArray that specifies 75 -1 to 10 as the range */ 76 cout << elements << " elements were output\n\n"; 77 78 // display array b using original printArray function 79 cout << "\nUsing original printArray function\n"; 80 printArray( b, BCOUNT ); 81 82 // display array b using new printArray function 83 cout << "Array b contains:\n"; 84 elements = /* Write a call to printArray that specifies 85 0 to BCOUNT - 1 as the range */ 9
2011
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 }
cout << elements << " elements were output\n"; // display elements 1-3 of array b cout << "Array b from positions 1 to 3 is:\n"; elements = /* Write a call to printArray that specifies 1 to 3 as the range */ cout << elements << " elements were output\n"; // try to print an invalid element cout << "Array b output with invalid subscripts:\n"; elements = /* Write a call to printArray that specifies -1 to 10 as the range */ cout << elements << " elements were output\n\n"; // display array c using original printArray function cout << "\nUsing original printArray function\n"; printArray( c, CCOUNT ); // display array c using new printArray function cout << "Array c contains:\n"; elements = /* Write a call to printArray that specifies 0 to CCOUNT - 2 as the range */ cout << elements << " elements were output\n"; // display elements 1-3 of array c cout << "Array c from positions 1 to 3 is:\n"; elements = /* Write a call to printArray that specifies 1 to 3 as the range */ cout << elements << " elements were output\n"; // try to display an invalid element cout << "Array c output with invalid subscripts:\n"; elements = /* Write a call to printArray that specifies -1 to 10 as the range */ cout << elements << " elements were output" << endl; // end main
Problem-Solving Tips 1. To overload the printArray function template, declare another function template, also named print-Array, that takes two additional int parameters, lowSubscript and highSubscript. 2. When iterating over the range from lowSubscript to highSubscript, make sure to include both values within the range, to avoid an off-by-one error.
10
2011
The program template represents a complete working C++ program, with one or more key lines of code replaced with comments. Read the problem description and examine the sample output; then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the program. Compare your output with the sample output provided. Then answer the follow-up questions.
Lab Objectives
In this lab, you will practice:
Overloading a function template for testing equality. Using function templates to create function-template specializations.
Sample Output
Enter two integer values: 2 5 2 and 5 are not equal Enter two character values: a a a and a are equal Enter two double values: 2.5 7.5 2.5 and 7.5 are not equal
11
2011
Template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Lab 2: isEqualTo.cpp // A function template for equality #include <iostream> using namespace std; // function template isEqualTo /* Write a template header with one formal type parameter */ /* Write a header for function template isEqualTo */ { return arg1 == arg2; } // end function isEqualTo int main() { int a; // integers used for int b; // testing equality // test if two cout << "Enter cin >> a >> b; cout << a << " <<(/* Write a ints input by user are equal two integer values: "; and " << b << " are " call to isEqualTo*/? "equal" : "not equal")<<'\n';
char c; // chars used for char d; // testing equality // test if two chars input by user are equal cout << "\nEnter two character values: "; cin >> c >> d; cout << c << " and " << d << " are " <<(/*Write a call to isEqualTo*/?"equal":"not equal")<<'\n'; double e; // double values used for double f; // testing equality // test if two doubles input by user are equal cout << "\nEnter two double values: "; cin >> e >> f; cout << e << " and " << f << " are " <<(/* Write a call to isEqualTo*/?"equal":"not equal")<<'\n'; } // end main
Problem-Solving Tips 1. Function template isEqualTo compares two values for equality, so both parameters should be of the same type and use the same formal type parameter.
Reference: www.pearsonhighered.com/deitel/.
12