Code: Select all
/*
class templates example 1 by Kristoffe Brodeur. ©2010 All Rights Reserved.
06-13-2010
a 'template' identifier is placed before a class to allow various types to be sent to it
notice a new class inside of <*****> that holds whatever type vars that come into it
and it is then used as if referencing the ***** class and then var name *****->?
*/
#include <cstdlib>
#include <iostream>
using namespace std;
//-----
template<class typeEx>
class temperature
{
private:
typeEx hoursArray[24];
public:
typeEx sum();
typeEx avg();
//constructor
temperature(typeEx sentVals[])
{
//
for(int h=0;h<24;h++)
{
hoursArray[h]=sentVals[h];
}
}
void printArray()
{
cout << "\nTemperatures in Greenhouse hours(0-23)\n";
//
for(int h=0;h<24;h++)
{
cout << h << "\t" << hoursArray[h] << endl;
}
}
};
//-----
int main()
{
//integer example
int roundedTemp[]=
{
35,37,45,46,48,49,50,81,
99,90,92,101,121,111,105,99,
81,50,49,48,46,45,37,35
};
//in class templates, the type must be in <*****> between the class and instance names
temperature <int> tropicalRoom(roundedTemp);
tropicalRoom.printArray();
//double example
double dblFloatTemp[]=
{
35.465,37.033,45.2345,46.1,48.234,49.8,50.78,81.01,
99.3334,90.4,92.1,101.245,121.0123,111.99,105.997,99.69,
81.0,50.01,49.022,48.023,46.02345,45.2,37.66,35.901
};
//in class templates, the type must be in <*****> between the class and instance names
temperature <double> tropicalRoom2(dblFloatTemp);
tropicalRoom2.printArray();
system("PAUSE");
return EXIT_SUCCESS;
}