DSA 1.30 : template classes

PHOTO EMBED

Wed Mar 08 2023 05:50:17 GMT+0000 (Coordinated Universal Time)

Saved by @saakshi #c++

#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;
}
content_copyCOPY