250 likes | 369 Views
Operator Overloading. class complex { int real, img; public: complex add() ; complex sub() ; complex& assign(); void show() ; };. // + // - // = // <<. Operator Overloading. main() { complex c1(5,3), c2(2,2) ; c1.assign(c1.add(c2)) ; // c1 = c1 + c2 ;
E N D
Operator Overloading class complex { int real, img; public: complex add() ; complex sub() ; complex& assign(); void show() ; }; // + // - // = // <<
Operator Overloading main() { complex c1(5,3), c2(2,2) ; c1.assign(c1.add(c2)) ; // c1 = c1 + c2 ; c1.show( ) ; // cout << c1 ; };
Operator Overloading • Binary Operator • +, -, *, /, …., >, < , >= , <= • Unary Operator • ++, --, +, - • I/O Stream • <<, >> • Type Conversion • char *, float, int
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&) ; };
Usage main() { complex a(1,1) ; // a= 1+1i ; complex b(2,2), c ; c = a+b ; a = b = c ; }
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)
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?
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 ; }
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)
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.
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&) ; };
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 ; }
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?
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() {….} }
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 ; }|
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;
Discussions • You can’t overloading anying • +, -, *, /, =, >, <, [], new, delete… OK! • not-existing operator(say **, $) not OK! • special built-in operator . :: ?: not OK!
Discussions complex operator+(const complex& right) const return type (by value) parameter (constant reference) function-style (const)
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
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: +, - ,….
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;
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)
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 }