انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Overloading operators

الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 2
أستاذ المادة مهدي عبد سلمان المسلماوي       3/29/2011 10:36:41 AM
overloading operators
c++ incorporates the option to use language standard operators between classes in addition to
between fundamental types. for example:
int a, b, c
a = b + c
is perfectly valid, since the different variables of the addition are all fundamental types.
nevertheless, is not so obvious that we can perform the following operation (in fact it is incorrect):
struct { char product [50] float price } a, b, c
a = b + c
the assignation of a class (or struct) to another one of the same type is allowed (default copy
constructor). what would produce an error would be the addition operation, that in principle is not
valid between non-fundamental types.
but thanks to the c++ ability to overload operators, we can get to do that. objects derived from
composed types such as the previous one can accept operators which would not be accepted
otherwise, and we can even modify the effect of operators that they already admit. here is a list of
all the operators that can be overloaded:
+ - * / = < > += -= *= /= < < > >
< < = > > = == != < = > = ++ % & ^ ! |
~ & = ^= |= & & || %= [] () new deleting
to overload an operator we only need to write a class member function whose name is operator
followed by the operator sign that we want to overload, following this prototype:
type operator sign (parameters)
here you have an example that includes the operator +. we are going to sum the bidimensional
vectors a(3,1) and b(1,2). the addition of two bidimensional vectors is an operation as simple
as adding the two x coordinates to obtain the resulting x coordinate and adding the two y
coordinates to obtain the resulting y. in this case the result will be (3+1,1+2) = (4,3).


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .