1 / 23

Operator Overloading. class complex { int real, img; public: complex add() ; complex sub() ; complex&amp; assign(); void show() ; };. // + // - // = // &lt;&lt;. Operator Overloading. main() { complex c1(5,3), c2(2,2) ; c1.assign(c1.add(c2)) ; // c1 = c1 + c2 ;

Télécharger la présentation

E N D

Presentation Transcript

1. Operator Overloading class complex { int real, img; public: complex add() ; complex sub() ; complex& assign(); void show() ; }; // + // - // = // <<

2. Operator Overloading main() { complex c1(5,3), c2(2,2) ; c1.assign(c1.add(c2)) ; // c1 = c1 + c2 ; c1.show( ) ; // cout << c1 ; };

3. Operator Overloading • Binary Operator • +, -, *, /, …., >, < , >= , <= • Unary Operator • ++, --, +, - • I/O Stream • <<, >> • Type Conversion • char *, float, int

4. Overloading Binary Operator: operatorX() class complex { int real, img ; public: // constructor complex operator+(const complex&) const ; complex operator=(const complex&) ; complex& operator+=(const complex &); bool operator>(const complex&) ; };

5. Usage main() { complex a(1,1) ; // a= 1+1i ; complex b(2,2), c ; c = a+b ; a = b = c ; }

6. How to call operatorX() ? complex operator+(const complex& right) const{….} complex& operator=(const complex& right) const{….} main ( ) { complex c,a, b; …... c = a + b ; } (1) a+b <=> a.operator+(b) (2) a = b <=> a.operator=(b)

7. operator+ // left + right complex complex::operator+(const complex& right) { complex temp ; temp.real = real+right.real ; templ.img = img+right.img; return temp; }; Q: How many Objects generated here? why?

8. Insight the operator+() Q: why can’t we return a reference from operator+? complex & complex::operator+(const complex&right) { complex temp; temp.real = real+right.real; temp.img = img+right+img ; return temp ; }

9. Insight operator+() // (續上) complex operator+(const complex& right) const { return complex(real+right.real,img+right.img); } • Why not void operator+( ) ? a+b+c <=> o1 = a.operator+(b)); o2 = o1.operator+(c)

10. operator= complex& complex::operator=(const complex&right) { real = right.real; img = right.img; return *this; } main() { complex a(2,2), b=a, c; c = b ; // c.operator=(b) ; } Q1: what’s this about *this ; Q2: Why complex& can(should) be used here? Q3: Compare with copy constructor.

11. Proper Return Type class complex { int real, img ; public: // constructor complex operator+(const complex&) const ; complex& operator=(const complex&) ; complex operator+=(const complex &); bool operator>(const complex&) ; };

12. Overload Unary Operator: ++, --, +, - // Overloading ++ // a) prefix version complex& operator++() {real++; img++; return *this} // b) postfix version complex& operator++(int) {real++; img++; return *this;} …... main() { complex a(1,1), b(2,2) ; a++; ++b ; }

13. Overload Unary Operator • what’s the proper return type of ++ ? • void (no return value needed) • complex ( by value) • complex & (by reference) • how to override subtraction and negative ? • (-) • what’s the proper return type?

14. Overload index operator [] class safe_arr{ private: const int szie ; int *arr ; public: int_arr(int n):size(n){ arr = new int[n] ;} int& operator[](int index); ~int_arr() {….} }

15. operator[] // why the return type: int& int& safe_arr::operator[] (int index) { ????? } main() { safe_arr a(10) ; int b ; b = a[3]; a[5] = 7 ; }|

16. operator[] int& safe_arr::operator[] (int index) { if (n <0 || n >= size) { cout << “index out of range” ; exit(1) ; return arr[index] ; } main() { safe_arr a(10) ; int b ; b = a[3]; a[5] = 7 ; }| // b = a.operator[](3) ; // a.operator[](5) = 7;

17. Discussions • You can’t overloading anying • +, -, *, /, =, >, <, [], new, delete… OK! • not-existing operator(say **, \$) not OK! • special built-in operator . :: ?: not OK!

18. Discussions complex operator+(const complex& right) const return type (by value) parameter (constant reference) function-style (const)

19. Discussions operators return-type parameter fun-style arithmetic(+,-,*,/) value const ref const assignment(=,+=,-=) ref(*this) const ref non-const comparison(>,<…) value(bool) const ref const prefix-unary(++,--) ref(*this) - non-const post-unary(++,--) ref(*this) int(useless) non-const sign-unary(+,-) value - const index([]) ref integer non-const

20. Discussions When design a class, what kind of member-functions needed? (1) constructor (2) destructor (3) Object I/O: (4) operator = (above are necessary ) (5) others: +, - ,….

21. Type Conversion of User-defined Classes • built-in explicit conversion int total, cnt; float avg ; ….. avg = (float)total/cnt; avg = float(total)/cnt; avg =static_cast<float>(total)/cnt;

22. Type Conversion of User-defined Classes • built-in implicit conversion int a=1, b=2, c = 3; float x(a) ;// x(a), x = float(a) a = b ; x = c ; // x = float(c)

23. User-defined Class toBuilt-in data type EX1: Complex class complex { float real, img ; public: operator float() {return sqrt(real*real+img*img);} }; no return type needed main() { complex a(1,1) ; float norm ; norm = float(a) ; // explicit norm = a; // implicit }

More Related