0

I am overloading a function it just prints the value which it received as an argument . Here is the code

#include<iostream>
using namespace std;
void show(int c)
{
    cout<<"Int C : "<<c<<endl;
}
void show(char c)
{
    cout<<"char C : "<<c<<endl;
}
void show(float c)
{
    cout<<"Float C : "<<c<<endl;
}
void show(string c)
{
    cout<<"String C : "<<c<<endl;
}
void show(const char* c)
{
    cout<<"char *C : "<<c<<endl;
}
main()
{
    string s("Vector");
    show(25);
    show('Z');
    show("C++");
    show("Hello");
    show(4.9f);
    show(s);
}

Here show(65) calls the integer function.

Is there any possibility that I can simply call show(65) to print the ASCII equivalent value 'A' i.e calling show(char) without calling show(int)

Anjaneyulu
  • 434
  • 5
  • 21

3 Answers3

4

Is there any possibility that I can simply call show(65) to print the ASCII equivalent value 'A' i.e calling show(char) without calling show(int)

Yes. You convert it to a char

show(static_cast<char>(65));

or convert it using a "function-style cast" fashion.

show(char(65));

Note: This will work only if your environment uses ASCII, because C++ does not mandate ASCII encoding for characters. Though systems using a different encoding are rare.

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
1

Or you can try by creating variable with char type and u can pass it to Show function.

Ex :

char test = 'Z';

Show(test);

It will works!

ban
  • 71
  • 7
0

No, it won't work because:

  1. You already have a function which takes a number as an argument.
  2. You have to manually convert it to char:show(char(65)).
  3. Even if you convert it to char, it would only work if you're in an environment which uses ASCII (it's not the only encoding system in C++).

If you already know what char you want you can simply do this:

show('A');