Use an int template nontype parameter numberOfElements and a type parameter element- Type to help create a template for the Array class (Figs. 11.6–11.7) we developed in Chapter 11. This template will enable Array objects to be instantiated with a specified number of elements of a specified element type at compile time. Write a program with class template Array. The template can instantiate

an Array of any ele- ment type. Override the template with a specific definition for an Array of float elements (class Array< float >). The driver should demonstrate the instantiation of an Array of int through the template and should show that an attempt to instantiate an Array of float uses the definition pro- vided in class Array< float >.

What will be an ideal response?


```
// Class template Array definition.
#ifndef ARRAY1_H
#define ARRAY1_H

#include
using namespace std;

template < typename elementType, int numberOfElements >
class Array
{
public:
Array(); // default constructor
~Array(); // destructor
int getSize() const; // return size
bool operator==( const Array & ) const; // compare equal
bool operator!=( const Array & ) const; // compare !equal
elementType &operator[]( int ); // subscript operator
static int getArrayCount(); // return count of arrays instantiated
void inputArray(); // input the array elements
void outputArray() const; // output the array elements
private:
elementType elements[ numberOfElements ]; // array of elements
static int arrayCount; // # of Arrays instantiated
}; // end class Array

// define static data member at file scope
template < typename elementType, int numberOfElements >
int Array< elementType, numberOfElements >::arrayCount = 0; // no objects

// default constructor for class Array
template < typename elementType, int numberOfElements >
Array< elementType, numberOfElements >::Array()
{
arrayCount++; // count one more object

// initialize array
for ( int i = 0; i < numberOfElements; i++ )
elements[ i ] = elementType();
} // end Array constructor

// destructor for class Array
template < typename elementType, int numberOfElements >
Array< elementType, numberOfElements >::~Array()
{
arrayCount--;
} // end Array destructor

// get the size of the array
template < typename elementType, int numberOfElements >
int Array< elementType, numberOfElements >::getSize() const
{
return numberOfElements;
} // end function getSize

// determine if two arrays are equal and return true or false
template < class elementType, int numberOfElements >
bool Array< elementType, numberOfElements >::
operator==( const Array &right ) const
{
// return false if arrays not equal
for ( int i = 0; i < numberOfElements; i++ )
{
if ( elements[ i ] != right.elements[ i ] )
return false;
} // end for

return true; // arrays are equal
} // end overloaded == operator

// determine if two arrays are not equal and return true or false
template < typename elementType, int numberOfElements >
bool Array< elementType, numberOfElements >::
operator!=( const Array &right ) const
{
// return false if arrays not equal
for ( int i = 0; i < numberOfElements; i++ )
{
if ( elements[ i ] != right.elements[ i ] )
return true;
} // end for

return false; // arrays are equal
} // end overloaded != operator

// overloaded subscript operator
template < typename elementType, int numberOfElements >
elementType &Array< elementType, numberOfElements >::
operator[]( int subscript )
{
// check for subscript
assert( 0 <= subscript && subscript < numberOfElements );
return elements[ subscript ]; // reference return creates lvalue
} // end overloaded subscript operator

// return the number of Array objects instantiated
template < typename elementType, int numberOfElements >
int Array< elementType, numberOfElements >::getArrayCount()
{
return arrayCount;
} // end function getArrayCount

// input values for entire array.
template < typename elementType, int numberOfElements >
void Array< elementType, numberOfElements >::inputArray()
{
// get values of array from user
for ( int i = 0; i < numberOfElements; i++ )
cin >> elements[ i ];
} // end function inputArray

// Output the array values
template < typename elementType, int numberOfElements >
void Array< elementType, numberOfElements >::outputArray() const
{
int i;

// output array
for ( i = 0; i < numberOfElements; i++ )
{
cout << elements[ i ] << ' ';

// form rows for output
if ( ( i + 1 ) % 10 == 0 )
cout << '\n';
} // end for

if ( i % 10 != 0 )
cout << '\n';
} // end function outputArray

#endif
```
Enter 5 integer values:
99 98 97 96 95
The values in intArray are:
99 98 97 96 95
Enter 7 one-word string values:
one two three four five six seven
The values in the stringArray are:
one two three four five six seven

Computer Science & Information Technology

You might also like to view...

When the method readLine() tries to read beyond the end of a file, it returns the value of:

a. 1 b. -1 c. null d. none of the above

Computer Science & Information Technology

The __________ in a binary tree is similar to the head pointer in a linked list.

a. root pointer b. leaf pointer c. null pointer d. binary pointer e. None of these

Computer Science & Information Technology

2. The main responsibility of a crawler is to

a. find web pages with false or illegal information b. count the number of web pages c. make sure lots of people visit certain pages d. build a list of tokens that are associated with each page

Computer Science & Information Technology

If you ________ your computer, a password is required to log back onto your desktop

Fill in the blank(s) with correct word

Computer Science & Information Technology