Link to home
Start Free TrialLog in
Avatar of jimmy_carter_2000
jimmy_carter_2000

asked on

function overload


Hi,

I am using a book to teach myself C++ and
I am having a problem with one of the questions in the book. I did the first part of the question which required me to create a union and display various amounts in hex format.

But it is the second part of the question that I am having trouble with it requires me to:  

Use function overloading  so a function called showNum() can be used to display each data type. (Same function name, different types of parameters: this requires three different functions to be written, all with the same name; showNum(int), showNum(long), showNum(float)) but with different data types being passed.)

Can you show me how to modify this program
to those specifications.


////////////////////////////////////////////



#include <iostream.h>      // to use cout
#include <stdio.h>      // to use printf

union mixed
{
      int value_int;
      long value_long;
      float value_float;
      unsigned char value_char[8];
};

main()
{
      mixed value_mixed;
      
      value_mixed.value_int=(int)13420;
      cout << "The integer " <<            value_mixed.value_int;
      cout << " is stored in memory as " << sizeof(int);
      cout << " bytes as follows: ";
        for (int i=0; i<sizeof(int); i++)
              printf("[%X]", value_mixed.value_char[i]);
      cout << endl;
                    


        value_mixed.value_long=(long)13420;
      cout << "The long " << value_mixed.value_long;
      cout << " is stored in memory as " << sizeof(long);
      cout << " bytes as follows: ";
        for (int j=0; j<sizeof(long); j++)
              printf("[%X]", value_mixed.value_char[j]);
        cout << endl;
              
        value_mixed.value_float=(float)13420;
      cout << "The float " << value_mixed.value_float;
      cout << " is stored in memory as " << sizeof(float);
      cout << " bytes as follows: ";
        for (int y=0; y<sizeof(float); y++)
              printf("[%X]", value_mixed.value_char[y]);
        cout << endl;
}

*******************************************

Any help you can give me would be appreciated
             J_C_2000
Avatar of nietod
nietod

You need to write three functions that have the same name but different types of parameters, like

showNum(int i)
{
   cout << i;
};

showNum(float f)
{
   cout << f;
};

showNum(long l)
{
   cout << l;
};

You need to write three functions that have the same name but different types of parameters, like

showNum(int i)
{
   cout << i;
};

showNum(float f)
{
   cout << f;
};

showNum(long l)
{
   cout << l;
};

You could use the functions like this

main()
{
   mixed value_mixed;

   value_mixed.value_int=(int)13420;
   cout << "The integer ";

   showNum(value_mixed.value_int);  

   cout << " is stored in memory as " << sizeof(int);
   cout << " bytes as follows: ";
   for (int i=0; i<sizeof(int); i++)
        printf("[%X]", value_mixed.value_char[i]);
   cout << endl;
                 


    value_mixed.value_long=(long)13420;
    cout << "The long ";

   showNum(value_mixed.value_long);

   cout << " is stored in memory as " << sizeof(long);
   cout << " bytes as follows: ";
    for (int j=0; j<sizeof(long); j++)
       printf("[%X]", value_mixed.value_char[j]);
   cout << endl;
                 
   value_mixed.value_float=(float)13420;
   cout << "The float ";

   showNum(value_mixed.value_float);
   cout << " is stored in memory as " << sizeof(float);
   cout << " bytes as follows: ";
   for (int y=0; y<sizeof(float); y++)
      printf("[%X]", value_mixed.value_char[y]);
   cout << endl;
}
Few notes:

* Factored out the dumping of the memory
* Removed the need for printf - cout can generally do what printf can if you look at the spec
* I've assumed that you don't need the union integrated with the second example

#include <iostream.h>

void dump(char *valueType, void *value, int size) {
  std::cout << "The " << valueType << " is " << size << " bytes as follows:" << endl;
  for(int i=0; i< size; i++) {
    std::cout << "[" << (hex) << (void *)((char *)value)[i] << "]";
  }
  std::cout << endl << endl;
}

void showNum(int value) {
  std::cout << "The integer value is: " << value << std::endl;
  dump("Integer",&value,sizeof(int));
}

void showNum(float value) {
  std::cout << "The float value is: " << value << std::endl;
  dump("Float",&value,sizeof(float));
}

void showNum(long value) {
  std::cout << "The long value is: " << value << std::endl;
  dump("Long",&value,sizeof(long));
}

int main()
{
  int myInt = 3;
  float myFloat = 3.1415;
  long myLong = 3;

  showNum(myInt);
  showNum(myFloat);
  showNum(myLong);

  return(0);
}
Avatar of jimmy_carter_2000

ASKER

Got the same error I was getting before
about ambiguos function call.  And I do need the union inclusive in this part two.
this is homework !!!!!!!!!!!!!!!!!!!
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Switched to short int and it stopped the ambiguos error and the program now works

thanx Nietod

J_C_2000