1
Chapter 8
Operator Overloading
2
User-defined Meaning for “built-in” Operators
Natural, suggestive usage Complex number addition,
subtraction, etc.Semantic integrity
Assignment for objects with pointersUniformity with built-in types
<< operator with user defined types
3
Format for Overloaded Operators
int& Array::operator+ (int i);
array1 = array2 + 10 array2.operator+(10)
Standard return typeand Class
Operatorkeyword Operator being
overloaded
ParameterParameterParameter
4
Example Overloaded Operators for Array Class
class Array {private: int array[20];public: Array(int init = 0); // overload the index operator, [] int& operator[](int i); ~Array();};
int& Array::operator[](int i) { assert(0 <= i && i < 20); return array[i];}
Interface
Implementation
Checks arraybounds
5
Using Overloaded Operator
Array array;
array[0] = 1;array[1] = 1;// compute first twenty// Fibonnaci numbersfor (int i = 2; i < 20; i++) array[i] = array[i-1] + array[i-2];
6
Mixing User-Defined and Built-In Operations
Array safe; int regular[20];
regular[10] = safe[10];safe[11] = regular[11];safe[0] = safe[0] + regular[0]
“Safe” array in terms ofindex bounds
Typical array, no indexbounds checking
7
Implementing Overloaded Operators for Arrays
int& Array::operator[](int i) { assert(0 <= i && i < 20); return array[i];}
Indexoperator
8
Additional Overloaded ArrayOperators
class Array {private: int array[20];public: int& operator[](int i);
// addition operator Array operator+(Array& other); // subtraction operator Array operator-(Array& other); ~Array();};
Adding + and- operators
9
Implementation of + and -Overloaded Operators
Array Array::operator+(Array& other) { Array result; for(int i=0; i<20; i++) result[i] = array[i] + other[i]; return result;}
Array Array::operator-(Array& other) { Array result; for(int i=0; i<20; i++) result[i] = array[i] - other[i]; return result;}
10
Multiply Overloading an Operatorclass Array { private: int array[20];public: // addition operator Array operator+(Array& other); // add increment to all Array operator+(int increment); };
Array Array::operator+(int increment){ Array result; for(int i = 0; i< 20; i++) result[i] = array[i] + increment; return result;}
Both are +operators, but
parameters differ
11
Usage of Multiply Overloaded Operators
Array one(1); // all elements are 1
// add 1 to each elementArray two = one + 1;
// pairwise addition of Arrays one and twoArray three = one + two;
Can we do this?Array four = 1 + one;
12
When to Overload Operators with Nonmember Functions
Operators on primitive data types E.g., int, float, arrays
Class source not always available E.g., ostream
Type casting
13
Operator Lookup Rules
Suppose:
Compiler looks for ‘op’ in this order Member function in X of form
Nonmember function of form
Return type is not part of lookup
Y y;X x; x op y;
operator op(Y)
operator op(X,Y)
14
Nonmember Function Operator Overload
Array operator+(int increment, Array array){ Array result;
for(int i=0; i<20; i++)result[i] = array[i] + increment;
return result;}
Now we can do this!// add 1 to each elementArray four = 1 + one;
15
Input-Output Using a Method
class Array{private: int array[20];public: // ... void Print(); ~Array();};
16
Input-Output Using a Method (cont’d)
void Array::Print() { cout << "[ "; for(int i = 0; i < 19; i++) cout << array[i] << ", "; cout << array[19] << " ]";}
Array test;test.Print();
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
17
Defining Stream Operators
ostream& operator<< (ostream& os, Array& a) { os << "[ "; for(int i = 0; i < 19; i++) os << a[i] << ", "; os << a[19] << " ]";}
ofstream outFile("array.data");
Array a;// write values of a to outfileoutFile << a;
// write to standard outputcout << a;
Cool!We can write
to any ostream!
18
Overloading Stream Operators
ofstream& operator<<(ofstream& ofs, Array& a){
ofs.write((char*)(a.array), 20*sizeof(int)); return ofs;};
ofstream binaryFile("array.bin");
Array a;
// write a to file in binary form
binaryFile << a;
binaryFile.close();
How do we know the binary implementation
will be invoked?
But this is private data!
19
Declaring a "friend" Stream I/O Operator
class Array { //... friend ofstream& operator<< (ofstream& ofs, Array& a);};
But not:class Array { //...
friend class ofstream;}
Why not?
20
How do I make an overloaded << operator for my Parts Class?
class Part {public:
Part(PartType type, int time, int identifier, float quality);int GetTime() {return time;}int GetIdentifier() {return identifier;}PartType GetType() {return type;}float GetQuality() {return quality;}
protected:PartType type;int time;int identifier;float quality;
};
Show definition anddeclaration changesand additions.
21
Stream Operator for a Part/Piece Object
/* This is not in the Parts class. */ostream& operator<< (ostream &os, Part&p) {
os << “ (“ << TranslateType(p.type) << “) ID# “;os << p.identifier << “ Quality: “;os << setw(4) << setprecision(2) << p.quality;os << “ Entered: “ << TranslateTime(p.time);os << “ #Recycles: << p.recycles << endl;
}
friend ofstream& operator<< (ofstream& ofs, Part& p);This is in the Parts class.
22
Type Conversion
Object of Class Y
Need
Object of Class X
Have
Type Conversion
23
Implicit and Explicit Type Conversion
int i; float f;
f = i; // implicit conversion
f = (float)i; // explicit conversion
f = float(i); // explicit conversion
24
Type Conversion
Have:Counter count(0);// manipulate countif ( count.Value() > 0) ...if ( count.Value() == 100) ...cout << count.Value();
Want:Counter count(0);// manipulate countif ( count > 0) ...if ( count == 100) ...cout << count;
25
Defining a Type Conversion Operator
class Counter: public DisplayableNumber{
...public:
...operator int();...
};
Simple int typeconversion operator
Used when implicit or explicit conversion done on a Counter
26
Constructors as Type Conversion Mechanisms
class Counter : public DisplayableNumber {
...public:
Counter(int i);...
};
A constructor canbe viewed as a
way to convert anint to a Counter.
27
Type Conversion andOperator Overloading
Given a class XConversion can be used to simplify
X + int int + X X + X
Provide a basic conversion from int to X
28
Type Conversion andOperator Overloading: Colors
Color is Red, Green, Blue combinations
Each primary color 0 - 63 Color something(Red(?), Green(?),
Blue(?))
29
Type Conversion andOperator Overloading: Colors
Color aqua(Red(0), Green(50), Blue(50));
Color darkRed = Red(63);Color purple = darkRed + aqua + Blue(13);Color peach = purple - Blue(13) - Green(3);
Color test1 = darkRed + peach;Color test2 = peach + darkRed;
cout << aqua << endl;cout << purple << endl;cout << peach << endl;cout << darkRed << endl;