This is a simple calculator using templated functions in c++. This program have the basic operation of a calulator (+,-,* and /). the main aim of this program is to demonstrate templated functions.
#include <iostream>
using namespace std;
template<class type1, class type2> double add(type1 num1, type2 num2)
{
return num1+num2;
}
template<class type1, class type2> double subtract(type1 num1, type2 num2)
{
return num1+num2;
}
template<class type1, class type2> double divide(type1 num1, type2 num2)
{
return num1/num2;
}
template<class type1, class type2> double multiply(type1 num1, type2 num2)
{
return num1*num2;
}
int main()
{
double num1, num2;
char op;
cout << "Please enter first number: ";
cin >> num1;
cout << "Enter Operator [+,-,*,/] : ";
cin >> op;
cin.ignore();
cin.clear();
cout << "Please enter second number: ";
cin >> num2;
int result;
if(op == '+'){result = add(num1, num2);}
else if(op == '-'){result = subtract(num1, num2);}
else if(op == '*'){result = multiply(num1, num2);}
else{result = divide(num1, num2);}
cout << "Result: " << result << endl;
return 0;
}