Program to find out sum of two numbers using templates in C++

Written by Sharad Raj on 3rd of November, 2018

C++ LANGUAGE PROGRAMS   BASIC OOPS PROGRAMS

PROGRAM CODE

#include<iostream>
using namespace std;

template <class T>
class sum {
  T a,b,c;
public:
  T add(T m,T n) {
    return c=(a=m)+(b=n);
  }
};

int main() {
  sum <float> o;
  cout<<"sum = a + b\n=> 3.8 + 4.2 = "<<o.add(3.8,4.2);
  return 0;
}

OUTPUT

sum = a + b
=> 3.8 + 4.2 = 8