Advertisement
Eternoseeker

Calculator using template

Nov 29th, 2022 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. template<class T>
  6.  
  7. class calculator{
  8.     T n1, n2;
  9.     public:
  10.     calculator(){
  11.         n1 = 0;
  12.         n2 = 0;
  13.     }
  14.     void input(){
  15.         cout << "Enter num1: " << endl;
  16.         cin >> n1;
  17.         cout << "Enter num2: " << endl;
  18.         cin >> n2;
  19.     }
  20.    
  21.     void display(){
  22.         cout << n1 << " + " << n2 << " = " << add() << endl;
  23.         cout << n1 << " - " << n2 << " = " << subtract() << endl;
  24.         cout << n1 << " * " << n2 << " = " << product() << endl;
  25.         cout << n1 << " / " << n2 << " = " << divide() << endl;
  26.         cout << n1 << " ^ 2 " << " = " << squaren1() << endl;
  27.         cout << n2 << " ^ 2 " << " = " << squaren2() << endl;
  28.         cout << n1 << " ^ " << n2 << " = " << power() << endl << endl;
  29.     }
  30.    
  31.     T add(){
  32.         return n1 + n2;
  33.     }
  34.    
  35.     T subtract(){
  36.         return n1 - n2;
  37.     }
  38.    
  39.     T product(){
  40.         return n1 * n2;
  41.     }
  42.    
  43.     T divide(){
  44.         return n1 / n2;
  45.     }
  46.    
  47.     T squaren1(){
  48.         return pow(n1, 2);
  49.     }
  50.    
  51.     T squaren2(){
  52.         return pow(n2, 2);
  53.     }
  54.    
  55.     T power(){
  56.         return pow(n1, n2);
  57.     }
  58.    
  59.    
  60. };
  61.  
  62. int main()
  63. {
  64.     calculator<float> a;
  65.     a.input();
  66.     a.display();
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement