#include <iostream>
using namespace std;
template <class T>
class Arithmetic
{
private :
T a;
T b;
public :
Arithmetic(T a,T b);
T add();
T sub();
};
template<class T>
Arithmetic<T>::Arithmetic(T a,T b)
{
this->a =a;
this->b =b;
}
template <class T>
T Arithmetic<T>:: add()
{
T c;
c = a+b;
return c;
}
template <class T>
T Arithmetic<T>:: sub()
{
T c;
c = a-b;
return c;
}
int main()
{
Arithmetic<int> ar(10,5);
cout<<ar.add()<<endl;
cout<<ar.sub()<<endl;
Arithmetic<float> ar1(1.22,1.3);
cout<<ar1.add()<<endl;
cout<<ar1.sub()<<endl;
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter