CIS 22B - Notes for Tuesday, 11/17

Announcements and Reminders

Recording

Function and Operator Overloading

Notes from CIS27

Operator Overloading

Notes

Examples

Fraction class with overloaded +   and ! operators
A friendly overloaded +
The Overloaded fraction class 

Summary


Overloading the insertion operator

Examples
Overloading the insertion operator for the card class
Overloading the insertion and extraction operators for the fraction class
Printing a deck

Videos
Operator Overloading
More on Operator Overloading

Tutorial
C++ Overloading


Lab Exercise #9

Put your name, the compiler used, and Lab Exercise #9 in a comment at the top of your program. Email your source code. This lab exercise is due at the beginning of the next lecture.

Complete the Rectangle class.  

class Rectangle
{
    int length, width;
public:
...
};

Overload the +=, !, and << operators.  The += operator should add the int argument to both the length and width members.  The ! operator should return the area of the Rectangle.  The << operator should be written as a friend of the Rectangle class.

Use this main() as a test.

int main()
{
    Rectangle R(6,4);
    cout << R << " has area " << !R << endl;
    R += 5;
    cout << R << " has area " << !R << endl;
}

It should produce the output:

Rectangle: length=6 width=4 has area 24
Rectangle: length=11 width=9 has area 99