Operator overloading is one of the most exciting features of
object-oriented programming. It can transform complex, obscure
program listings into intuitively obvious ones. For example,
statements like
d3.addobjects(d1, d2);
or the similar but equally obscure
d3 = d1.addobjects(d2);
can be changed to the much more readable
d3 = d1 + d2;
The rather forbidding term operator overloading refers to giving the normal C++ operators, such as +, *, <=, and +=, additional meanings when they are applied to user-defined data types.
Normally,
a = b + c;
works only with basic types such as int and float, and attempting
to apply it when a, b, and c are objects of a user-defined class
will cause complaints from the compiler. However, using
overloading, you can make this statement legal even when a, b, and
c are user-defined types.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.