Can u help me guys. I have problem of calling function from class and overloading. When I'm trying to print short int type number, the program brakes down. No errors are shown which tell me whats wrong.

#include <cstdlib>
#include <iostream>

using namespace std;
int i=0, j=0, zero_fill, l=0;
class dec_to_bin
{
public:
    char bin[33], rev[33];
    char *to_bin(unsigned int dec, int size);
    char *to_bin(unsigned short int dec, int size);
};
char *dec_to_bin :: to_bin(unsigned int dec, int size)
{
    while(dec > 0) {
        bin[i] = dec%2+48;
        dec = dec/2;
        i++;
    }
    zero_fill = size-i;
    while(l<zero_fill) {
        bin[i+l] = '0';
        l++;
    }
    while(j < i+l) {
        rev[j] = bin[i+l-j-1];
        j++;
    }
    rev[j] = 0;
    return rev;
}


char *dec_to_bin :: to_bin(unsigned short int dec, int size)
{
    to_bin(dec,size); //using same function again
}
int main(int argc, char *argv[])
{

    unsigned int num = 20;
    unsigned short int num1 = 21;
    dec_to_bin b;
    printf("bin int: %s\n", b.to_bin(num, 32)); //works fine
    printf("bin short: %s\n", b.to_bin(num1, 16)); //program breakes down
    system("PAUSE");
    return EXIT_SUCCESS;
}
to_bin(dec,size); //using same function again

Yes, you're using the same function again. This is a recursive call with no base case, which means your error is a stack overflow. Try casting the arguments into an exact match for the overloaded function if you want to reuse it:

to_bin(static_cast<unsigned int>(dec), size); //using same function again

The program now doesn't breaks down, but it should print unsigned short int type, but it prints unsigned int type again. Am I making band overloading or smth? C++ is kind of new for me...

What happenes if you do

return to_bin(static_cast<unsigned int>(dec), size);

on line 36?

Same thing. When I'm printing only unsigned short number everything is ok. But printing both(unsigned int and unsigned short), it prints unsigned int type.

But printing both(unsigned int and unsigned short), it prints unsigned int type.

Duh. Of course it has the same results because you use the same algorithm (and the same types) to generate the string.

How can I use same algorithm with different types of arguments?

How can I use same algorithm with different types of arguments?

That's like a leading question for templates. But you can actually do this without making a template by tweaking the algorithm slightly (more like simplifying it, actually):

#include <algorithm> // For fill() and reverse()
#include <iostream>

using namespace std;

class dec_to_bin {
    char bin[33];
public:
    char *to_bin(unsigned int value, int size);
};

char *dec_to_bin::to_bin(unsigned value, int size)
{
    int i = 0;
    
    std::fill(bin, bin + sizeof bin, '\0');
    
    while (value != 0 && i < size) {
        bin[i++] = value % 2 + '0';
        value /= 2;
    }
    
    while (i < size)
        bin[i++] = '0';
    
    std::reverse(bin, bin + size);
    
    return bin;
}

int main()
{
    dec_to_bin b;
    
    cout << "bin int:   " << b.to_bin(20, 32) << '\n';
    cout << "bin short: " << b.to_bin((unsigned short)21, 16) << '\n';
}

No overload is needed because smaller types will be promoted to the larger type. You might see some wierdness with a signed to unsigned conversion, but that's a problem for another day. Also a problem for another day is sanity checks such as making sure that size is within the bounds of unsigned int 's bit count.

thats cool. But I want to do with overloading (I have such a task :@).

But I want to do with overloading

Then do it with overloading. It's completely redundant and unnecessary, but with the algorithm changes you'll get the same results:

#include <algorithm> // For fill() and reverse()
#include <iostream>

using namespace std;

class dec_to_bin {
    char bin[33];
public:
    char *to_bin(unsigned int value, int size);
    char *to_bin(unsigned short value, int size);
};

char *dec_to_bin::to_bin(unsigned value, int size)
{
    int i = 0;
    
    std::fill(bin, bin + sizeof bin, '\0');
    
    while (value != 0 && i < size) {
        bin[i++] = value % 2 + '0';
        value /= 2;
    }
    
    while (i < size)
        bin[i++] = '0';
    
    std::reverse(bin, bin + size);
    
    return bin;
}

char *dec_to_bin::to_bin(unsigned short value, int size)
{
    return to_bin((unsigned)value, size);
}

int main()
{
    dec_to_bin b;
    
    cout << "bin int:   " << b.to_bin(20U, 32) << '\n';
    cout << "bin short: " << b.to_bin((unsigned short)21, 16) << '\n';
}

If you had actually put some thought into this before rejecting my help, you'd have come to the same conclusion.

I have found a mistake I've been doing. I was using global variables in all my functions. I've changed them to local and everything works fine.
I must thank Narue for patience and help, Thank you. Problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.