Tempate Operator Question

S

sam

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>("Hello","How are you?");
sum<ST> stsum = new sum<ST>(st1,st2); // I am getting compiler error
s1.display();
s2->display();
}

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top