Page 33 - DCAP404 _Object Oriented Programming
P. 33

Object-oriented Programming




                    Notes          2.3.2 Assignment  Operators

                                   Operator  Description                Example      Explanation
                                      =     Assign the value of the      a = b       Assigns the value of b to a
                                            right operand to the left

                                     +=     Adds the operands and        a +=b       Adds the of b to a
                                            assigns the result to the                The expression could also be
                                                                                     left operand written as a = a+b
                                     –=     Subtracts the right          a -=b       Subtracts b from a
                                            operand from the left                    Equivalent to a = a-b
                                            operand and stores the
                                            result in the left operand
                                     *=     Multiplies the left operand   a*=b       Multiplies the values a and b by
                                            stores the result in the left            the right operand and
                                            operand                                  and stores the result in a
                                                                                     Equivalent to a = a*b
                                     /=     Divides the left operand     a/=b        Divides a by b and stores the
                                            stores the result in the left            by the right operand and
                                            operand                                  result in a Equivalent to a = a/b

                                     *=     Divides the left operand     a%=b        Divides a by b and stores the
                                            stores the remainder in                  by the right operand and
                                            the left operand                         remainder in a
                                                                                     Equivalent to a = x%y
                                   Any of the operators used as shown below:
                                       A <operator>=y
                                   can also be represented as

                                       a=a <operator> b
                                   that is, b is evaluated before the operation takes place.
                                   You can also assign values to more than one variable at the same time. The assignment will take
                                   place from the right to the left. For example,
                                       a = b = 0;
                                   In the example given above, first b will be initialized and then a will be initialized.



                                     Did u know?  What is self assignment?
                                     Self assignment is when someone assigns an object to itself. For example,

                                       #include  “Fred.h”    //  Defines  class  Fred
                                       void  userCode(Fred&  x)
                                      {
                                        x = x;           // Self-assignment
                                      }



          26                                LOVELY PROFESSIONAL UNIVERSITY
   28   29   30   31   32   33   34   35   36   37   38