Topic : C++ tutorial for C users
Author : Eric Brasseur
Page : << Previous 3  Next >>
Go to page :

double min (double a, double b). That does the job for the whole program.
Would you have tried something like calculating min (i1, d1) the compiler would have reported that as an error. Indeed the template tells both parameters are of the same type.

You can use a random number of different template data types in a template definition. And not all parameter types must be templates, some of them can be of standard types or user defined (char, int, double...). Here is an example where the min function takes parameters of any, possibly different, types and outputs a value that has the type of the first parameter:

#include <iostream.h>

template <class type1, class type2>
type1 min (type1 a, type2 b)
   type1 r, b_converted;
   r = a;
   b_converted = (type1) b;
   if (b_converted < a) r = b_converted;
   return r;

void main ()
   int i;
   double d;

   i = 45;
   d = 7.41;

   cout << "Most little: " << min (i, d) << endl;
   cout << "Most little: " << min (d, i) << endl;
   cout << "Most little: " << min ('A', i) << endl;

The keywords new and delete can be used to allocate and deallocate memory. They are much more sweet than the functions malloc and free from standard C. new [] and delete [] are used for arrays:

#include <iostream.h>
#include <string.h>

void main ()
   double *d;                         // d is a variable whose purpose
                                      // is to contain the address of a
                                      // zone where a double is located

   d = new double;                    // new allocates a zone of memory
                                      // large enough to contain a double
                                      // and returns its address.
                                      // That address is stored in d.

   *d = 45.3;                         // The number 45.3 is stored
                                      // inside the memory zone
                                      // whose address is given by d.

   cout << "Type a number: ";
   cin >> *d;

   *d = *d + 5;

   cout << "Result: " << *d << endl;

   delete d;                          // delete deallocates the
                                      // zone of memory whose address
                                      // is given by pointer d.
                                      // Now we can no more use that zone.

   d = new double[15];                // allocates a zone for an array
                                      // of 15 doubles. Note each 15
                                      // double will be constructed.
                                      // This is pointless here but it
                                      // is vital when using a data type
                                      // that needs its constructor be
                                      // used for each instance.

   d[0] = 4456;
   d[1] = d[0] + 567;

   cout << "Content of d[1]: " << d[1] << endl;

   delete [] d;                       // delete [] will deallocate the
                                      // memory zone. Note each 15
                                      // double will be destructed.
                                      // This is pointless here but it
                                      // is vital when using a data type
                                      // that needs its destructor be
                                      // used for each instance (the ~
                                      // method). Using delete without

Page : << Previous 3  Next >>