Function Overloading Practice
Compito
Swipe to start coding
- Make function overloading for
add
function to support various types:- Create an
add
function that takes float inputs and returns a float. - Create an
add
function that takes strings and returns a string.
- Create an
Soluzione
solution.cpp
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
int add(int a, int b) { return a + b; }
float add(float a, float b) { return a + b; }
std::string add(std::string a, std::string b) { return a + b; }
int main()
{
int a = 2, b = 5;
float c = 9.2, d = 1.6;
std::cout << add(a, b) << std::endl;
std::cout << add(c, d) << std::endl;
std::cout << add("c<>de", "finity");
}
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 4
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
int add(int a, int b) { return a + b; }
// overload add for float or double
// overload add for string
int main()
{
int a = 2, b = 5;
float c = 9.2, d = 1.6;
std::cout << add(a, b) << std::endl;
std::cout << add(c, d) << std::endl;
std::cout << add("c<>de", "finity");
}
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione