Function overloading with Float and Integers.

So I'm trying to make a program that calculates celsius to fahrenheit with function overload.

I have 1 function that calculates and 2 others that takes the data from the input
and whether its a integer or a decimal number it will use function overload to decide witch function to use.

My question is when i declare my function that calculates what data type I'm I supposed to use, as for now I'm using INT but that feels wierd when im trying to send in a float and expect to get a float back.

I have tried to use "void" but it wont work.

Best regards!

Hi @stlund,
you can implement
something like this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//func_ov.cpp
//##

#include <iostream>

using namespace std;


int square(int a);
float square(float b);

int main(){

        int a=10;
        float b=3.3;

        cout<<"Integer: "<<square(a)<<endl; 
        cout<<"Float: "<<square(b)<<endl;
                          
return 0; //indicates success
}//@end of main           
                          

int square(int a){
        return a*a;
}//end function square int

float square(float a){
        return a*a;
}//end function square float
Integer: 100
Float: 10.89
This is what it look's like.
but it feels wierd that the function CTF is declared as an "int"
sending in a float trough a function with int.

could I use void instead? tested but I diden't get it to work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

//Function::Calculates Celsius to Fahrenheit.
int CTF(int celsius)
{
    int fahrenheit = ((celsius *9) / 5) + 32;
    return fahrenheit;
}

//Function overload, integer.
int OLCels(int celsius)
{
    cout << "INT ";
    celsius = CTF(celsius);
    return celsius;
}

//Function overload, float.
float OLCels(float celsius)
{
    cout << "FLOAT ";
    celsius = CTF(celsius);
    return celsius;
}

int main()
{
    cout << OLCels(98.8f); // <--- VALUE HERE!
    return 0;
}
You can write an overloaded
version of CTF as well,
1
2
3
4
float CTF(float c){
    //formula
    //return floating point value
}


I do not know why
you need it (an integer version of CTF) but you
can do it;
Topic archived. No new replies allowed.