Logical Operator Overloading (Programming)

Logical operator overload (Programming) . The logical operators are often represented by their English names (uppercase): AND (&&); OR (||) and NOT (!). The first two are binary, while the last one is unary, which means that AND and OR accept two arguments, while the negation NOT accepts only one. The operands of the global versions of these operators are converted to a bool type, and that the result is also a bool type of true / false value (true / false).

Summary

[ hide ]

  • 1 Permanence of formal laws
  • 2 NOT operator overload
  • 3 Overloading AND and OR operators
  • 4 You can consult
  • 5 Source

Permanence of formal laws

The C ++ language does not impose any restrictions on the form that an operator overload takes. Thus, it is possible to achieve that even basic operators such as the sum (+), the assignment (=) or the identity (==) acquire a totally different character for the abstract types from the one they adopt for the basic types. However, the normal, logical, and advisable thing is to maintain the maximum conceptual homogeneity in the polymorphism. That is to say, that the basic effect of the operators (the mental image of their meaning) is maintained, although their concrete implementation varies from one class to another. For example, the definitions of sum (+), assignment (=), and identity (==) for elements of a class C should guarantee that after the statement c = d; on instances c and d of that class, the result of (c == d) would be true.

NOT operator overload

The logical negation NOT operator(!) is related to its opposite (which has no name or representation). To explain the meaning of this statement, suppose an object c of a class C. As we have pointed out, an attempt to use it, for example, the expression: if (! C) {/ * … * /} generates an error of build: ‘operator!’ not implemented in type ‘C’ …, indicating that the logical negation NOT operator is not defined for objects of the class. However, we can see that an analogous attempt without the operator: if (c) {/ * … * /} also produces an error, although in this case the indication is more ambiguous: Illegal structure operation in function … ( Borland) o: conditional expression of type ‘class C’ is illegal (Visual C ++). In the latter case, the compiler is indicating that it does not know how to convert the expression in parentheses (c) to a bool type. Remember that the if (<condition>) … statement expects to receive a <condition> expression that resolves to a bool.

AND and OR operator overload

If you have a user conversion that guarantees the conversion of a c object from type C to type bool, then it is not really necessary to overload the AND or OR operators to be able to use them. For example, assuming the definition (§3.2) and the vectors (§3.3) above, the sentences:

if (p3 && p1) cout << “p3 and p1 Ok.”;

else cout << “p3 and p1 NOT ok.”;

if (p3 || p1) cout << “p3 or p1 Ok.”;

else cout << “p3 or p1 NOT Ok.”;

They produce the following outputs:

p3 and p1 NOT ok.

p3 or p1 Ok.

 

by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment