Original Post
🔒 Locked
Operator Overloading?
Advertisement
The point is you can write code that makes sense by using the operators. If you create your own string class, you can concatenate strings using the + operator. If you create an arbitrary sized integer class, you can write the +, -, *, /, etc. operators for it. If you didn''t do that, you would have to do something like:
BigInt a = 2134325423652364565324543252;
BigInt b = 23423456324566;
BigInt c = add(a,b);
Instead, since you can overload operators, you can do this:
BigInt c = a + b;
That is much more clear to a C/C++ programmer. You could also overload the input and output stream operators for your class. So you could do this:
cin >> a >> b;
c = a + b;
I don''t know of any case that they are necessary, but it''s cool to be able to use them with iostream stuff instead of calling a function like, display_big_int(BigInt & b), or whatever.
BigInt a = 2134325423652364565324543252;
BigInt b = 23423456324566;
BigInt c = add(a,b);
Instead, since you can overload operators, you can do this:
BigInt c = a + b;
That is much more clear to a C/C++ programmer. You could also overload the input and output stream operators for your class. So you could do this:
cin >> a >> b;
c = a + b;
I don''t know of any case that they are necessary, but it''s cool to be able to use them with iostream stuff instead of calling a function like, display_big_int(BigInt & b), or whatever.
So you can code "a operator b" rather than "func(a,b)" or "a.func(b)". Or in more elaborate situation "a + b + c + d" rather than "a.sum(b.sum(c.sum(d)))" or "sum(a,sum(b,sum(c,d)))".
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement