Lab 7 Templates

 

Redesign your Array class from lab6 as a class template to work with the application below. Due to the g++ compiler, your class declaration and class method definitions must both reside in the header file, Array.h. Do not create a separate Array.C file for your class template. In addition, write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below.

 

#include "Array.h"

 

main()

{

Array<char> c(3);

c.setValue(0,'c');

c.setValue(1,'s');

c.setValue(2,'c');

cout << c;

 

Array<int> i(3);

i.setValue(0,1);

i.setValue(1,2);

i.setValue(2,5);

cout << i;

 

Array<int> j(3);

j.setValue(0,10);

j.setValue(1,20);

j.setValue(2,50);

cout << j;

 

Array<int> ij;

ij = i + j;

cout << ij;

}

 

The output for this program should be as follows:

 

Array:

size: 3

contents: c s c

Array:

size: 3

contents: 1 2 5

Array:

size: 3

contents: 10 20 50

Array:

size: 3

contents: 11 22 55