Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Function Overloading Practice | Diving Into Functions
C++ Introduction Video Course

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

Soluzione

solution.cpp

solution.cpp

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

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
single

single

#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

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

We use cookies to make your experience better!
some-alt