June 2025
Intermediate to advanced
1093 pages
33h 24m
English
If you have defined the function
void print(int value) { std::cout << "int value: " << value;}
but do not want print(3.75) to output a nonsensical int value: 3, you can also define the print function for a parameter of type double. If a function with the same name is defined multiple times, it is called overloading.
// https://godbolt.org/z/9YcPraT15#include <iostream>void print(int value) { std::cout << "int value: " << value << " "; }void print(double value) { std::cout << "double value: " << value << " "; }void print(int v1, double v2) { std::cout << "Values: "<<v1<<", "<<v2<<" "; }int add(int n, int m) { return n + m; }double add(double a, double b) { return a + b; }int main() { print( add(3, 4) ); // add(int, ...
Read now
Unlock full access