Section 14i

C++ Overloaded Operators


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Easier Operations

Replacing methods with Operators (which are actually methods)

Overloaded operators are not a fundamental Computing Science artifact. Thus, they will not be used very much in this reference. However, they are a part of C++ and some other languages so it would be good to know about them and in some cases, you might find yourself writing them. For that reason, this topic will briefly support the use of overloaded operators.

First Word: Overloaded

So what are overloaded operators? The first word is overloaded. In programming, overloaded functions (or methods) are sometimes used to accomplish the same thing, but with different conditions, and specifically with different parameters.

For example, in C++ there are five different get methods that all do the same thing but with different conditions. There is a get method that captures a character from the stream, two that capture strings, and two more that capture strings under special condtions. By allowing overloading with functions/methods, a programmer can provide more adaptable tools for others and still use the same intuitive name.

Second Word: Operator

In the case of operators, they will be overloaded for a given class because they are already being used somewhere else. An example is the plus sign ("+"). The plus sign is used for adding two numerical values together. However, it can be overloaded to add two strings together, a process called concatenation. Once again, the overloaded operator does the same thing, which is adding two entities, but the entities are different from each other.

Many operators are binary, in that the operator stands in the middle of two other entities. The plus sign is one example. The original plus sign sits between two numerical values. The concatenation plus sign sits between two strings. That said, unary operators, or operators that only require one entity can also be overloaded. The "++" operator means to increment a value by one; however it could also advance an index in an array class by one if it were overloaded to do that.

Another example is that the binary operator plus-equals ("+=") sign can append one string to another as shown here.

finalStr += otherStr;

Note that the string might have to be resized to handle all the other strings but all of this is managed in the overloaded operator code. Consider the following.

const SimpleString& SimpleString::operator += ( const SimpleString &ssStr // in: SimpleString object to be appended
)
{
// initialize function/variables

// create temporary copy of this string
SimpleString leftStr( *this );

// create temporary copy of incoming string
// function: copy constructor
SimpleString rightStr( ssStr );

// get length of appended string
// function: getLength
int rightStrLen = rightStr.getLength();

// get length of destination string
// function: getStrLen
int leftStrLen = getStrLen( strData );

// resize string as needed
resizeStr( leftStrLen + rightStrLen + 1 );

// copy current string into strData
// function: copyStr
copyStr( strData, leftStr.strData );

// concatenate the data
// function: concatStr
concatStr( strData, rightStr.strData );

// return pointer to this object
return *this;
}

This actually looks like a pretty complicated process. Okay, it's not that complicated but there is still quite a bit of action in this method for one little plus sign. That's the cool part of this. That concatenation operation makes the process easy for the user/programmer and she doesn't need to deal with any of the issues such as looking to see if there is enough memory available in the resulting string. All of this is covered.

Note also that there actually is a concatenate method that does the actual concatenating after the memory issues have been resolved but as you can see, using the operator is much easier and cleaner. This class will be provided to you toward the end of this page and you will also note that there is a compareTo method in the class and this method can be used to compare two strings. However, as external methods, there are an equals test, a less than test, and a greater than test available as operators. This is strictly to make the programming easier to write and easier to read. That's what operators do.

For purposes of expanding your understanding and use of C++ classes and overloaded operators, and to support use of string management in future operations, a string management class called SimpleString is also provided in this topic. The header file is here and the implementation file is here.

There is a lot to be learned from reviewing this class. Enjoy.

Last Word: Overloaded Operators

As promised, this is a brief review of overloaded operators. They are again, not a fundamental component of fundamental programming but they can be handy. Use this reference as needed.