Presentation on theme: "Operator overloading redefine the operations of operators"— Presentation transcript:
1Operator overloading redefine the operations of operators eg. Complex numberComplex A, B, C;C = A.add(B); // use member function addC = A + B; // overload the operator +格式: (function prototype)Complex operator+(Complex&);
6friend functions friend function a function defined outside the class but allowed to access the private and protected members of the given classprototype is declared within the class and leading with the keyword friendeg.
9operator overloading( >> and <<) overload the input and output operatorscin >> i; // interpreted as cin.operator>>(i);cin is a system declared object of istream classcout is a system declared object of ostream class>> must be overload as a top-level friend function
10operator overloadingoverload the >> and << operators for Complexeg.Complex a, b, c;cin >> b;cin >> c;a = b+c;cout << a;455 + 9i