Advertisement
TheWhiteFang

Tutorial 11 Q3

Jan 15th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T, typename U>
  6. class MathCalc
  7. {
  8.     T val1;
  9.     U val2;
  10.  
  11. public:
  12.  
  13.     T Sum(T augend, U addend);
  14.     /*T Sum( T augend, U addend){
  15.         return T(augend + addend);
  16.     }
  17.     */
  18.     //define inside class
  19.     T Subtact(T subtrahend, U minuend){
  20.    
  21.         return T(subtrahend - minuend);
  22.     }
  23.    
  24.     T Multiply(T multiplicand, U multiplier){
  25.    
  26.         return T(multiplicand - multiplier);
  27.     }
  28.  
  29.     T Divide(T dividend, U divisor){
  30.    
  31.         return T(dividend / divisor);
  32.     }
  33.  
  34. };
  35.  
  36.  
  37. //defining outside of class
  38. template <typename T, typename U>
  39. T MathCalc::Sum(T augend, U addend)
  40. {
  41.     return T(augend + addeend);
  42. }
  43.  
  44. int main(){
  45.    
  46.     MathCalc <float,double> obj;
  47.  
  48.     float num1 = 0.0;
  49.     double num2 = 0.0;
  50.     cout << "Enter 2 variables";
  51.     cin >> num1;
  52.     cin >> num2;
  53.    
  54.     cout << endl << "Sum: " << obj.Sum(num1, num2) << endl;
  55.     cout << "Subtract: " << obj.Subtact(num1, num2) << endl;
  56.     cout << "Multiply: " << obj.Multiply(num1, num2) << endl;
  57.     cout << "Divide: " << obj.Divide(num1, num2) << endl;
  58.    
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement