! Thing class methodsFor: 'Creation' !
new | r |
r _ super new
^ r init !
(super is superclass of self object)
! Thing methodsFor: 'Using things' !
init aValue _ 0 !!
int max(int a, int b)
// Postcondition: The return value is the larger of a and b
{
return (a>b ? a : b);
}
double max(double a, double b)
// Postcondition: The return value is the larger of a and b
{
return (a>b ? a : b);
}
string max(string a, string b) // Postcondition: The return value is the larger of a and b { return (a>b ? a : b); }
typedef _________ Etype;
Etype max(Etype a, Etype b)
// Postcondition: The return value is the larger of a and b
{
return (a>b ? a : b);
}
template <class Etype>
Etype max(Etype a, Etype b)
// Postcondition: The return value is the larger of a and b
{
return (a>b ? a : b);
}
String s1("beans"), s2("frijoles");
double d1=0.1, d2=0.02;
cout << max(1000,2000);
cout << max(d1,d2);
cout << max(s1,s2)
template <class Etype>
void doubleArray( Etype * & array, int & currentSize)
{
Etype * oldArray = array;
const int MINSIZE = 5;
int newSize = array ? 2*currentSize : MINSIZE;
array = new Etype [newsize];
for(int i = 0; i < currentSize; i++)
array[i] = oldArray[i];
currentSize=newSize;
delete [ ] oldArray;
}
int * array = NULL;
int itemsRead=0;
doubleArray<int> (array, itemsRead);
template <class ArrayType, class SizeType>
void doubleArray( Etype * & array, SizeType & currentSize)
{
Etype * oldArray = array;
const SizeType MINSIZE = 5;
SizeType newSize = array ? 2*currentSize : MINSIZE;
array = new Etype [newsize];
for(int i = 0; i < currentSize; i++)
array[i] = oldArray[i];
currentSize=newSize;
delete [ ] oldArray;
}
template <class Etype>
immediately before the class definition.
template <class IntType>
class Rational
{
class definition exactly as it is on pg’s 68 and 69
};
template <class IntType>
const Rational<IntType> &
Rational<IntType>::operator=(const Rational<IntType> & rhs)
{
if (this != &rhs)
{
numer = rhs.numer;
denom = rhs.denom;
}
return *this;
}
main ( )
{
Rational<long> x;
...
}