Expressions and Assignment Statements

2nd chuck

Aydin Bagiyev
3 min readNov 16, 2020

Outline:

  1. Overloaded operators, both predefined and user defined, are then discussed, along with their effects on the expressions in programs.

Arithmetic operators are often used for more than one purpose

The picture illustrate operator overloading in a joyful manner.

Operator overloading

Rule

Operator overloading should not violate neither readability nor reliability

  • Example: use of the ampersand (&) in C++. As a binary operator, it specifies a bitwise logical AND operation. As a unary operator, however, its meaning is totally different. As a unary operator with a variable as its operand, the expression value is the address of that variable. In this case, the ampersand is called the address-of operator.
  • Explanation: There are two problems with this multiple use of the ampersand. First, using the same symbol for two completely unrelated operations is detrimental to readability. Second, the simple keying error of leaving out the first operand for a bitwise AND operation can go undetected by the compiler, because it is interpreted as an address-of operator. Such an error may be difficult to diagnose.

Known problems

Minus operator

The compiler cannot tell if the operator is meant to be binary or unary.

  • However, the meanings of the two operations, unary and binary, are at least closely related, so readability is not adversely affected.

Benefit of Overloading

Usage on abstract data types supported languages like C++, C#, and F#.

  • Example: suppose a user wants to define the * operator between a scalar integer and an integer array to mean that each element of the array is to be multiplied by the scalar
  • Explanation: if this new definition for * is defined in a C# program, a C# compiler will use the new definition for * whenever the * operator appears with a simple integer as the left operand and an integer array as the right operand

When sensibly used, user-defined operator overloading can aid readability.

  • Example: if + and * are overloaded for a matrix abstract data type and A, B, C, and D are variables of that type, then A * B + C * D can be used instead of MatrixAdd(MatrixMult(A, B), MatrixMult(C, D))

Harm of Overloading

When not sensibly used, user-defined operator overloading can be harmful to readability.

  • Consideration: nothing prevents a user from defining + to mean multiplication
  • Consideration: seeing an * operator in a program, the reader must find both the types of the operands and the definition of the operator to determine its meaning. Any or all of these definitions could be in other files
  • Consideration: the process of building a software system from modules created by different groups. If the different groups overloaded the same operators in different ways, these differences would obviously need to be eliminated before putting the system together

In C++, class or structure member operator (.) and the scope resolution operator cannot be overloaded.

For known issues, operator overloading has not been copied to Java from C++, however, it was one of the main features.

REFERENCES

  1. Sebesta, R. W. (n.d.). Expressions and Assignment Statements. In Concepts of Programming Languages (12th ed., pp. 311–314).

--

--