redefine the operations of operators
+ * / << >> ( ) [ ] …, 參考p. 286
eg. Complex number
Complex A, B, C;
C = A.add(B); // use member function add
C = A + B; // overload the operator +
格式: (function prototype)
Complex operator+(Complex&);
Complex::Complex(){
real =imag =0.0;
}
Complex::Complex(double re ){
real =re;
imag =0.0;
}
Complex::Complex(double re,double im ){
real =re;
imag =im;
}
friend function
a function defined outside the class but allowed to access the private and protected members of the given class
prototype is declared within the class and leading with the keyword friend
eg.
overload the input and output operators
cin >> i; // interpreted as cin.operator>>(i);
cin is a system declared object of istream class
cout is a system declared object of ostream class
>> must be overload as a top-level friend function
operator overloading
4
5
5 + 9i
overload the >> and << operators for Complex
eg.
Complex a, b, c;
cin >> b;
cin >> c;
a = b+c;
cout << a;
operator overloading
class Complex{
…
friend istream& operator>> (istream&, Complex&);
friend ostream& operator<<(ostream&, Complex&);
…
};
istream& operator>>(istream& in, Complex& c){
in >> c.real >> c.imag;
return in;
}
operator overloading
ostream& operator<<(ostream& in, Complex& c){
out << c.real << ‘+’ << c.imag << ‘i’ ;
return out;
}
overload the subscript operator [ ]
class intArray{
public:
intArrary(int s);
int& operator[ ](int);
const int& operator[ ](int) const;
int get_size( ) const {
return size; }
private:
int size;
int* a;
}; intArray::intArray( int s) {
try {
a = new int[s];
}
catch (bad_alloc) {
cerr << “Unable to allocate\
storage for intArrar\n”;
throw;
}
size = a;
}
overload the subscript operator [ ]
int& intArray::operator[ ](int i) {
if (i < 0 || i >= size)
throw string(“OutOfBounds”);
return a[ i ];
}
const int& intArray::operator[ ](int i) const {
if (i < 0 || i >= size)
throw string(“OutOfBounds”);
return a[ i ];
}
overload the subscript operator [ ]
0
2
4
6
8
OutOfBounds
i=5
int main( ) {
intArray b(5);
int i;
try {
for (i=0; i < b.get_size; i++)
b[ i ] = 2 * i;
for (i=0; i < 6; i++)
cout << b[ i ] << ‘\n’;
}
catch ( string s) {
cerr << s << ‘\n’;
cerr << “i=“ << i <<‘\n’;
}
return 0;
}
operator overloading (example)
class Clock {
public:
Clock (int =12, int=0, int=0);
Clock tick( );
friend ostream& operator<<(
ostream&, const Clock&);
Clock operator++( ); // ++a
Clock operator++(int); // a++
private:
int hour;
int min; int ap;
};
Clock::Clock(int h, int m, int ap_flag){
hour = h;
min = m;
ap = ap_flag;
}
operator overloading (example)
Clock Clock::tick( ) {
++min;
if (min == 60) {
hour++;
min = 0;
}
if (hour == 13)
hour = 1;
if (hour == 12 && min == 0)
ap = !ap;
return *this;
} Clock Clock::operator++( ) {
return tick( );
}
Clock Clock::operator++(int n) {
Clock c=*this;
tick( );
return c;
}
operator overloading (example)
Clock c: 12:00 AM
Clock d: 12:01 AM
ostream& operator<<(ostream& out, const Clock& c) {
out << setfill(‘0’) << setw(2) << c.hour << ‘:’ << setw(2) << c.min;
if (c.ap) out << “ PM”;
else out << “ AM”;
return out;
}
int main( ) {
Clock c,d;
c = d++;
cout << “Clock: “ << c << ‘\n’;
cout << “Clock: “ << d << ‘\n’;
…
}
operator overloading
Clock c: 12:01 AM
Clock d: 12:01 AM
int main( ) {
Clock c,d;
c = ++d;
cout << “Clock: “ << c << ‘\n’;
cout << “Clock: “ << d << ‘\n’;
…
}
Comments