+ 2

just simple question where that values come :P

i write calculator for int and double on template... i was think its get any off variabels and i try add char and int ( not only add :P ) and my question its how its calculate. Code : #include <iostream> using namespace std; template <class Cal1 , class Cal2> Cal1 sum(Cal1 a, Cal2 b) { return a+b; } template <class Cal1 , class Cal2> Cal1 sub(Cal1 a, Cal2 b){ return a-b; } template <class Cal1 , class Cal2> Cal1 sub1(Cal1 a, Cal2 b){ return b-a; } template <class Cal1 , class Cal2> Cal1 mul(Cal1 a, Cal2 b){ return a*b; } template <class Cal1 , class Cal2> Cal1 divi(Cal1 a, Cal2 b){ if ( b != 0){ return a/b; } else { cout << "Error division by "; } return 0; } template <class Cal1 , class Cal2> Cal1 divi1(Cal1 a, Cal2 b){ if ( b != 0){ return b/a; } else { cout << "Error division by "; } return 0; } int main() { char x='1'; int y=2.0; if (sizeof(x)>=8){ cout << "calculating for double:" << endl; cout << sum(x,y) << endl << sub(x,y) << endl; cout << mul(x,y) << endl; cout << divi(x,y)<< endl;} else{ cout << "calculating for double:" << endl; cout << sum(y,x) << endl << sub1(y,x) << endl; cout << mul(y,x) << endl; cout << divi1(y,x)<< endl;} return 0; } and output: calculating for double: 51 47 98 24

23rd Apr 2017, 8:01 PM
MKey
MKey - avatar
3 Answers
+ 1
but i dont use the same Type. at start i add double and int.. for fun i change double for char and is Wonder where from is that output
23rd Apr 2017, 9:35 PM
MKey
MKey - avatar
+ 1
oww now i understood :) i try add double and int witchout template... i was thinking it is two diffrence Type but bouth are Digital. i just learning ;)
23rd Apr 2017, 9:47 PM
MKey
MKey - avatar
0
Why did you use Cal2 for the second parameter? Use Cal1 if you mean the same type.
23rd Apr 2017, 9:32 PM
SAMI Awad
SAMI Awad - avatar