main.C
that should be written as template function.
The C++ Standard library already has a function named
min
whose full name is
std::min
because it belongs to the standard namespace
std
.
The full name of my
min
functions is
::min
,
because they do not belong to any namespace.
I had to write
::min
to make sure I was calling my own min
functions, not the one in the standard library.
To get the expression b < a
to compile when
b
and
a
are of data type
const date&
,
I had to define the functions
operator<
or
operator int
for class
date
.
if (b < a) { //if operator<(b, a) {
if (b < a) { //if (b.operator int() < a.operator int()) {
c++ main.C date.C
10 2.71 5/1/2025 goodbye
main.C
that will work for every data type except
const char *
.
The first three calls to min
in the main
function create three
instantiations
of the function template.
In the three instantiations,
the computer deduces
that the T
should be changed to
int
,
double
,
double
,
const date&
respectively.
(An instantiation is also called an
implicit specialization.)
The a
and
b
are called
function arguments.
And the 10
and 20
are the actual function arguments.
date.h
:
exactly the same class
date
as in the previous example.date.C
main.C
:
exactly the same main
function as in the previous example, but the rest of
main.C
is different.c++ main.C date.C
10 2.71 5/1/2025 goodbye
main.C
.
date.h
:
exactly the same class
date
as in the previous example.date.C
main.C
:
exactly the same main
function as in the previous example, but the rest of
main.C
is different.c++ main.C date.C
10 2.71 5/1/2025 goodbye
.h
(header) file.
min.h
contains the declarations and definitions of the
min
template function.
See the important comment about the data type
T
.
date.h
:
exactly the same class
date
as in the previous example.date.C
main.C
:
exactly the same main
function as in the previous example, but the rest of
main.C
is different.c++ main.C date.C
10 2.71 5/1/2025 goodbyeSee this
min.h
for a better version
that passes function arguments of type
T
by reference, i.e., as a
const T&
.
T
.
min
in the C++ Standard Library does not know that the data type
const char *
probably needs to be handled differently.
print
function has two arguments,
which may be of different data types.
c++ main.C date.C
10 20 3.14 5/1/2025 A hello
print.h
contains the templatedate.h
:
exactly the same class
date
as in the previous example.date.C
main.C
c++ main.C date.C
10 20 3.14 5/1/2025 A hello
swap
functions:
c++ main.C date.C
i = 20, j = 10 d = 2.71, e = 3.14 today = 5/2/2025, tomorrow = 5/1/2025
const T
:
date.h
:
exactly the same class
date
as in the previous example.date.C
swap.h
contains the templatemain.C
c++ main.C date.C
i = 20, j = 10 d = 2.71, e = 3.14 today = 5/2/2025, tomorrow = 5/1/2025
#include <algorithm>
for the C++ Standard Library
swap
.
min
template will not compile,
because our min
template requires two actual arguments of the same data type.
c++ main.C
main.C: In function ‘int main()’: main.C:14:22: error: no matching function for call to ‘min(int&, double&)’ 14 | cout << ::min(i, d) << "\n"; //won’t compile | ~~~~~^~~~~~
point
with double
data members:
c++ main.C point.C
(3, 4) 5
point
with float
data members:
c++ main.C point.C
(3, 4) 5
point
.
There is no more point.C
file.
c++ main.C
(3, 4) 5
counted
is not a template class.
It has a static member function named
count
which receives no invisible argument.
c++ main.C counted.C
3
numeric_limits
is a template class with two static member
functions,
min
and
max
.
c++ main.C
short -32768 32767 int -2147483648 2147483647 long -9223372036854775808 9223372036854775807We will never create any object of classes
numeric_limits<int>
,
numeric_limits<long>
,
etc.
The only purpose of these classes is to give us information about the
data types
int
,
long
,
etc.
Other examples of classes that give us information about other data types
are
char_traits
and
iterator_traits
.
vector
is a template class.
Ditto for classes
list
and
set
.
c++ main.C
10 20 30 10.5 20.5 30.5
map
is a template class with two template arguments.
c++ map.C
typedef
,
but now we say using
.
c++ using.C
10 20 30
wolf
,
rabbit
,
boulder
,
landmine
)
contains only one copy of the grandparent
(wabbit
).
wabbit.h
:
grandparent classwabbit.C
immobile.h
:
three motion classes,
with the keyword virtual
randomly.h
manual.h
herbivore.h
:
three rank-in-the-food-chain classes,
with the keyword virtual
carnivore.h
inert.h
wolf.h
:
four grandchild classes.
These header files #include
d by
main.C
.rabbit.h
boulder.h
landmine.h
main.C
:
the
main
functionterm.h
term.c
terminal.h
terminal.C
cc -DUNIX= -c term.c ls -l term.o c++ main.C wabbit.C terminal.C term.o -lcurses ls -l a.out ./a.outNote that the grandchild classes
wolf
,
rabbit
,
boulder
,
landmine
are now identical,
except for the names of the two parent classes of each grandchild,
and the character that is the grandchild’s name on the screen.
We can therefore write all the grandchild classes as a single template class.
wolf
,
rabbit
,
boulder
,
landmine
from the game.
Simply do not include any of their header files
wolf.h
,
rabbit.h
,
boulder.h
,
landmine.h
in
main.C
.
Instead,
have
main.C
#include
the following header file grandchild.h
.
Can
main.C
also create boulder
and landmine
objects?
With the motion and rank classes we already have,
we could easily create 3 × 3 = 9
classes of grandchildren.
cc -DUNIX= -c term.c ls -l term.o c++ main.C wabbit.C terminal.C term.o -lcurses ls -l a.out ./a.out