Author Topic: ProgEx24 Function Overloading  (Read 2203 times)

0 Members and 1 Guest are viewing this topic.

Offline Frederick J. Harris

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 941
    • Frederick J. Harris
ProgEx24 Function Overloading
« on: November 09, 2009, 01:19:05 AM »
Code: [Select]
/*
  ProgEx24  C++ Program Showing Function Overloading

  The program below contains only two functions (in addition to main()) but they
  both have the same name -- Max.  This is allowed in C++ if the parameter list
  of each function is different.  What differs in the two versions of Max below
  is that the first parameter of each is an array of a different type.  The
  first version of Max takes an array of ints, and the second takes an array of
  doubles.  They each return the index of the largest element in the array
  passed as a parameter.  The second parameter of each function is simply the
  count of elements in the array.

  They each return an int but I could have writen them to return the actual
  value of the highest element.  I'd recommend you do that as an exercise to
  solidify your understanding of this topic.  Note that we could have created
  as many of these functions named Max as there are seperate variable types in
  the C++ language.  You can keep going if you want and create one for longs,
  unsigned ints, char, etc.

  I apologize for this sort of expression...

  iAr[Max(iAr,sizeof(iAr)/sizeof(iAr[0]))])

  It is pretty ugly.  Sometimes I get carried away (been doing C too long).  Its
  just putting the Max function call inside the array brackets where the index
  of the array goes.  The Max function returns the index of the biggest element
  so putting the Max function call in iAr's brackets just indictes the largest
  element in iAr[].

  This...

  sizeof(iAr)

  will return the size of the entire array.  For example, the iAr[] array down
  in main contains 6 ints.  Each int is 4 bytes (on 32 bit OS).  Therefore, the
  size of the array in memory is 24 bytes.

  This...

  sizeof(iAr[0])

  will return the size of element zero which is of course the size of every
  other element and that would be 4 bytes.  So this whole ugly expression...

  sizeof(iAr)/sizeof(iAr[0])

  will end up equaling 6.  That is the count of elements passed to the version
  of Max that deals with ints.

  Function overloading is an important topic in C++ and really comes into its
  own in the context of classes - which we'll turn to shortly.  It has a dark
  downside though.  The compiler creates an alias name for each version of the
  overload and this name mangling is a source of a lot of linking difficulties.
  We explored this topic a bit when we were experimenting with importing
  functions from C++ Dlls.
*/

#include <stdio.h>


int Max(int ar[], int iSize)           //ar[] is a pointer to the base address
{                                      //of an array of ints.  iSize is the
 int iLargest=0;                       //count of elements in the array.  The
                                       //variable iLargest keeps track of the
 for(unsigned int i=1;i<iSize;i++)     //largest element encountered as the
 {                                     //loop runs through the array passed
     if(ar[i]>ar[iLargest])            //in as a parameter.  After the entire
        iLargest=i;                    //array is transversed, iLargest is
 }                                     //returned.
 
 return iLargest;
}


int Max(double ar[], int iSize)
{
 int iLargest=0;
 
 for(unsigned int i=1;i<iSize;i++)
 {
     if(ar[i]>ar[iLargest])
        iLargest=i;     
 }
 
 return iLargest;
}


int main(void)
{
 int     iAr[]    = {123,24,12,2,17,88};
 double  dblAr[]  = {3.14159, 17.45, -1.234, 187.5};
 int     iBiggest;

 //First Integers
 iBiggest=Max(iAr,sizeof(iAr)/sizeof(iAr[0]));
 printf("iAr[iBiggest]                                        = %d\n",iAr[iBiggest]);
 printf("iBiggest                                             = %d\n",iBiggest);
 printf("iAr[Max(iAr[],sizeof(iAr)/sizeof(iAr[0]))]           = %d\n",iAr[Max(iAr,sizeof(iAr)/sizeof(iAr[0]))]);

 //Then Doubles
 iBiggest=Max(dblAr,sizeof(dblAr)/sizeof(dblAr[0]));
 printf("dblAr[iBiggest]                                      = %f\n",dblAr[iBiggest]);
 printf("iBiggest                                             = %d\n",iBiggest);
 printf("dblAr[Max(dblAr[],sizeof(dblAr)/sizeof(dblAr[0]))]   = %f\n",dblAr[Max(dblAr,sizeof(dblAr)/sizeof(dblAr[0]))]);   
 getchar();   
   
 return 0;   
}

/*
iAr[iBiggest]                                        = 123
iBiggest                                             = 0
iAr[Max(iAr[],sizeof(iAr)/sizeof(iAr[0]))]           = 123
dblAr[iBiggest]                                      = 187.500000
iBiggest                                             = 3
dblAr[Max(dblAr[],sizeof(dblAr)/sizeof(dblAr[0]))]   = 187.500000
*/