Tempate Operator Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam

    Tempate Operator Question

    If I pass class or struct to sum class, I have to overload "+" and <<
    operator for all types except arithmetic operator and STL. Is it correct? Is
    there any other way I can do this? Thank you very much.

    template<class C> class sum{
    private:
    C T, T1;
    public:
    sum(C,C);
    void display();
    };
    template<class C>
    sum<C>::sum(C a, C b){
    T=a;
    T1=b;
    }

    template<class C>
    void sum<C>::display (){
    C t2 = T+T1; //
    cout << t2 <<"\n";
    }

    typedef struct st{
    string Name;
    int age;
    } ST;

    int main(){
    typedef sum<int> SINT;
    sum<int> s1(100,100);
    ST st1,st2;
    st1.Name="Name1 ";
    st1.age =10;
    st2.Name="Name2 "
    st2.age =20;
    sum<string > *s2 = new sum<string>("He llo","How are you?");
    sum<ST> stsum = new sum<ST>(st1,st2 ); // I am getting compiler error
    s1.display();
    s2->display();
    }

    Sam


Working...