Advertisement
vvccs

template function

Jun 6th, 2023
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T>
  5. class Sum
  6. {
  7. public:
  8.     T a, b;
  9.     T sum(T a, T b)
  10.     {
  11.         T c;
  12.         c = a + b;
  13.         return c;
  14.     }
  15. };
  16.  
  17. int main()
  18. {
  19.     Sum <int>a1;
  20.     cout << "Sum of 2 integer data-types is: " << a1.sum(4, 7) << endl;
  21.     Sum<float> a2;
  22.     cout << "Sum of 2 float data-types is: " << a2.sum(3.6, 4.2) << endl;
  23.  
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement