• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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
0
jimmy_carter_2000
Asked:
jimmy_carter_2000
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now