CSC270 Tutorial: C++ Operators


Operators

An operator is one of: + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete In C++, these can be redefined to operate on members of a class. An operator can be written as a function call. The following two assignments are equivalent: Complex a,b,c; a = b + c; a = b.operator+(c); In this case, operator+ is a function defined in Complex, like class Complex { private: float real, imag; public: // the constructors (which one is used depend upon the arguments) Complex() { real = 0; imag = 0; } Complex( float re, float im ) { real = re; imag = im; } // addition of complex numbers Complex operator+( Complex x ) { return Complex( real+x.real, imag+x.imag ); } }; Above, the function operator+ returns a new Complex using the Complex() constructor. For BINARY operators, like +, -, *, /, the first argument (a in a+b) is always implicit. Above, the first argument is the class instance in which the function is executing, so "real" and "imag" are components of the first argument. The second argument (b in a+b) is passed as a parameter, so "x.real" and "x.imag" are components of the second argument. It's a bit confusing. For UNARY operators, like &, -, !, the only argument is implicit. Here's a Boolean class (note that C++ already has a bool class, so this class is just for instructional purposes): typedef enum {TRUE,FALSE} bool_type; class Boolean { private: bool_type b; public: Boolean( bool_type bool_val ) { b = bool_val; } Boolean() { b = FALSE; } Boolean operator!() { if (b == TRUE) return Boolean( FALSE ); else return Boolean( TRUE ); } }; This would be used as follows: Boolean a, b; a = TRUE; b = !a;

Defining the output operator "<<" for your class

The output operator << can be defined for your class. The following code does so for the Boolean class. This code should appear *inside* the class definition, in the public part. friend ostream& operator<< ( ostream& o, Boolean& boolean ) { if (boolean.b == TRUE) o << "TRUE"; else o << "FALSE"; return o; } The operator << takes an output stream and a class instance as arguments and returns the output stream (more on this below). Inside the function, stuff is sent to the output stream. In this case, the stuff is either the character string "TRUE" or the character string "FALSE". Note that there's not a newline "\n" at the end. If the programmer wants a newline, he or she will put it in explicitly where the code uses the operator <<. If operator<< is declared as a "friend" inside the Boolean class definition, it will have access to all the components of the class. This important, since operator<< is an *independent* function that shouldn't be associated with any particular class. The reason for returning the output stream is a bit esoteric: it allows us to string together many outputs in one statement: cout << a << b; This is really parsed as (cout << a) << b; so b is being sent to the stream (cout << a), which is the value returned by the call to operator<<( cout, a ) which is cout. It's a bit convoluted. You just have to use the template shown above and remember to return the stream.