Table of Contents
1. Introduction
When creating our own classes, we may want to make certain operators
mean certain things when used with our class. By default, we can't
use math operators (+, -, /, *), stream operators (<<,
>>), relational operators (<, <=, >, >=, ==, !=), the
subscript operator [], and the assignment operator (=) with the
classes we create. In order to do so, we would have to use operator
overloading to define our own functions and how these operators
work on our class.
For example, we could define our own Fraction class, without overloaded arithmetic operators we would have to do math like this:
Hard-to-read code way to add two fractions, using classes:
Frac frac1( 1, 2 ); // 1/2 Frac frac2( 2, 3 ); // 2/3 Frac sum = frac1.Add( frac2 ); // Add 1/2 + 2/3
When we set up the arithmetic operators so you could simply do:
~
Adding two fractions when the operator+ has been overloaded:
Frac frac1( 1, 2 ); // 1/2 Frac frac2( 2, 3 ); // 2/3 Frac sum = frac1 + frac2; // Add 1/2 + 2/3
…Which is much more intuitive.
Review 1. When we write custom classes, C++ (○ does / ○ does not) automatically "know" how to use arithmetic operators on two objects of that class.
2. Arithmetic operators
Arithmetic operators will be friend functions of our class. This is because it will take two objects and return a third, so it doesn't "neatly" fit inside one class as a member function.
Example: Arithmetic operators - Friend arithmetic function declarations:
class MyClass { public: friend MyClass operator+( const MyClass& item1, const MyClass& item2 ); friend MyClass operator-( const MyClass& item1, const MyClass& item2 ); friend MyClass operator/( const MyClass& item1, const MyClass& item2 ); friend MyClass operator*( const MyClass& item1, const MyClass& item2 ); };
Then the function definition would go in a source file (it could go
in MyClass.cpp as well, but it's not a member function so don't
prefix MyClass:: on these functions).
Example: Defining an arithmetic operator function
MyClass operator+( const MyClass& item1, const MyClass& item2 ) { MyClass sum; sum.memberA = item1.memberA + item2.memberA; return sum; }
Review 2. Given a Fraction class already created and has arithmetic
operators overloaded. The program has three fraction objects,
fracA, fracB, and fracSum already created. How would you
subtract fracB from fracA and store the result in fracSum?
3. Relational operators
Relational operators also operate on two objects so these are also friend functions.
Example: Relational operators - Friend relational function declarations:
class MyClass { public: friend bool operator==( const MyClass& item1, const MyClass& item2 ); friend bool operator!=( const MyClass& item1, const MyClass& item2 ); friend bool operator<( const MyClass& item1, const MyClass& item2 ); friend bool operator<=( const MyClass& item1, const MyClass& item2 ); friend bool operator>( const MyClass& item1, const MyClass& item2 ); friend bool operator>=( const MyClass& item1, const MyClass& item2 ); };
Example: Defining a relational operator function
bool operator==( const MyClass& item1, const MyClass& item2 ) { return ( item1.memberA == item2.memberA ); }
Review 3. Given a Word class already defined, including relational
operators, and two Word objects wordA and wordB, how would you
write an if statement with a condition of if wordA is greater
than or equal to wordB?
4. Stream operators
Stream operators will take in a stream reference and the object to
input or output and return the stream reference of the modified
stream. These are also friend functions. We use the ostream and
istream classes as the return type and parameter since this is the
parent class of cout/cin and ofstream/ifstream, as well as other
stream types.
Terminal:
File output:
Program output:
Example: Stream operators - Friend stream function declarations:
class MyClass { public: friend ostream& operator<<( ostream& out, MyClass& item ); friend istream& operator>>( istream& in, MyClass& item ); };
Example: Defining the stream operator functions
ostream& operator<<( ostream& out, MyClass& item ); { out << item.memberA; return out; } istream& operator>>( istream& in, MyClass& item ); { in >> item.memberA; return in; }
You can output or input multiple items in these functions so that you can get input for all class members, or output all class members, in one statement.
Review 4. Given a Word class already defined as well as the stream
operators, and a Word object named myWord, how would you display
myWord to the screen?
Review 5. Given a Word class already defined as well as the stream
operators, and a Word object named myWord, how would you get
input from the keyboard and store it in myWord?
5. Subscript operator
The subscript operator is usually used when creating a type of list structure so that we can access an element at some position. This function is a member function of the class.
Example: Subscript operator - Member function declaration:
class MyClass { public: string& operator[] ( const int index ); private: string m_data[100]; };
Example: Defining subscript operator function
string& MyClass::operator[]( const int index ) { return m_data[ index ]; }
Review 6. An OrderedPair class has been defined as well as its
subscript operator. [0] corresponds to the x value and [1]
corresponds to the y value. Given an OrderedPair object named
origin, how would you display the coordinate in x, y form to the
screen?
6. Assignment operator
The assignment operator is similar to the copy constructor in that we are telling our class how it will copy the data from another object of the same class. It's up to us to decide which members to copy over as part of this process.
Example: Assignment operator - Member function declaration:
class MyClass { public: MyClass& operator=( const MyClass& other ); private: int a; float b; };
Example: Defining assignment operator function
MyClass& MyClass::operator=( const MyClass& other ) { if ( this == &other ) { return *this; } a = other.a; b = other.b; return *this; }
Note the if statement in our assignment operator. This is checking to see if the memory address of "this" class is the same as the "other" class being passed in. We don't want to make a copy if these are actually the same object, so in that case we return "this" (but it must be de-referenced).
Otherwise, we can copy whichever member variables we would like, and make sure to return "this" at the end (also de-referenced).
Review 7. A Pet class has already been defined as well as its
assignment operator. Two Pet objects have been created:
rachelsCat and raisCat. How would you assign the value of
rachelsCat to raisCat?