Multiply Function that works for All Numeric Types in C++ Last Updated : 16 Jun, 2025 Comments Improve Suggest changes Like Article Like Report In C++, function overloading allows us to create multiple functions with the same name but different parameter types. Using this concept, we can multiply functions to perform multiplication on numeric values of different data types.This program should be able to do the following:Multiply two int values.Multiply two double values.Multiply an int and a double (or vice versa).Multiply two numeric strings after converting them to numbers.Implementation C++ #include <bits/stdc++.h> using namespace std; // Multiply two integers int multiply(int a, int b) { return a * b; } // Multiply two doubles double multiply(double a, double b) { return a * b; } // Multiply int and double double multiply(int a, double b) { return a * b; } // Multiply double and int double multiply(double a, int b) { return a * b; } // Multiply two numeric strings double multiply(const string& a, const string& b) { double num1, num2; stringstream ss1(a), ss2(b); ss1 >> num1; ss2 >> num2; return num1 * num2; } int main() { cout << "Int * Int: " << multiply(3, 4) << endl; cout << "Double * Double: " << multiply(2.5, 4.0) << endl; cout << "Int * Double: " << multiply(3, 4.5) << endl; cout << "Double * Int: " << multiply(5.5, 2) << endl; cout << "String * String: " << multiply("6.5", "2"); return 0; } OutputInt * Int: 12 Double * Double: 10 Int * Double: 13.5 Double * Int: 11 String * String: 13The compiler chooses the appropriate multiply function based on the argument types. For string inputs, we convert the numeric string values into double using stringstream and then perform multiplication. Comment A abhishekcpp Follow 0 Improve A abhishekcpp Follow 0 Improve Article Tags : C++ C++ Basic Programs Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like