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
jimmy_carter_2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
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;
};

0
nietodCommented:
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;
};

0
nietodCommented:
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;
}
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

s_franklinCommented:
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);
}
0
jimmy_carter_2000Author Commented:
Got the same error I was getting before
about ambiguos function call.  And I do need the union inclusive in this part two.
0
Shay050799Commented:
this is homework !!!!!!!!!!!!!!!!!!!
0
nietodCommented:
shay,

Perhaps it is homework, although he says it is not.  However, in either case, I don't think I went too far.  He wrote most of it, I just showed how to do the overload part.


jimmy_carter_2000
You've rejected my answer.  You should not reject an answer if you only need additional information or help.  You should only reject if the expert is wrong or unable to help.

>> about ambiguos function

Where do you get this?  Probably for

  showNum(value_mixed.value_int);

and

  showNum(value_mixed.value_long);

this is probably because int and long int are treated as the same type by your computer.  You might try changing one of them to short int so the two are really different.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimmy_carter_2000Author Commented:
Switched to short int and it stopped the ambiguos error and the program now works

thanx Nietod

J_C_2000
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.