11.2 Fundamentals Restrictions 限制 o To use an operator on class objects, that operator must be overloaded with three exceptions(针对对象,有三个运算符不用重 载): #include <iostream> using namespace std int main() int i, j; =18 1=(j++,j+188,999+1); cout < i; return 0; 0 2018, SEU. All rights reserved. 11
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 11 11.2 Fundamentals & Restrictions --- 限制 To use an operator on class objects, that operator must be overloaded with three exceptions(针对对象,有三个运算符不用重 载): assignment operator (=) address operators (&) comma operators (,)
11.2 Fundamentals Restrictions 限制 Operators that can be overloaded & ‰ < [] ( new delete new [] delete [ Operators that cannot be overloaded 0 2018, SEU. All rights reserved. 12
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 12 Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] 11.2 Fundamentals & Restrictions --- 限制 Operators that cannot be overloaded . .* :: ?:
11.2 Fundamentals Restrictions 限制 °重载运算符应该仿效其相应的内置对象的功能 °重载成的成员函数必须是非 static的(带着问题思考) o Hugelnt operator+(const Hugelnt& a); °不能更改 Precedence(优先级, Associativity(结合 律)以及 Number of Operands(操作数数目) ●仅能重载现有运算符,不能创造新运算符 0 2018, SEU. All rights reserved. 13
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 13 11.2 Fundamentals & Restrictions --- 限制 重载运算符应该仿效其相应的内置对象的功能 重载成的成员函数必须是非static的(带着问题思考) HugeInt operator+(const HugeInt& a); 不能更改Precedence(优先级), Associativity(结合 律) 以及 Number of Operands(操作数数目) 仅能重载现有运算符,不能创造新运算符
11.2 Fundamentals Restrictions 限制 continue °仅能重载应用于用户定义数据类型操作数的运算 符 o int int X o Hugeint hHugeint v o Hugeint int v o int+ Hugeint v °运算符必须显性重载 重载+和=不代表重载了+= 0 2018, SEU. All rights reserved. 14
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 14 11.2 Fundamentals & Restrictions --- 限制 continue 仅能重载应用于用户定义数据类型操作数的运算 符 int + int X Hugeint + Hugeint √ Hugeint + int √ int + Hugeint √ 运算符必须显性重载 重载+和=不代表重载了+=
Topics 11.1 Introduction 11.2 Fundamentals Restrictions o 11.3 Operator Functions as class Members vs Global Functions 11.4 Overloading stream Insertion and stream Extraction Operators 11.5 Overloading Unary Operators 11.6 Overloading Binary Operators 11.7 Case Study: Array Class 11.8 Converting between types 11.9 Case Study: String Class 11.10 Standard Library Class string(self study) 11.11 Overloading + and -(self study) 11.12 Case Study: A Date Class(self study) 0 2018. SEU. All rights reserved. 15
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 15 Topics 11.1 Introduction 11.2 Fundamentals & Restrictions 11.3 Operator Functions as Class Members vs. Global Functions 11.4 Overloading Stream Insertion and Stream Extraction Operators 11.5 Overloading Unary Operators 11.6 Overloading Binary Operators 11.7 Case Study: Array Class 11.8 Converting between types 11.9 Case Study: String Class 11.10 Standard Library Class string(self study) 11.11 Overloading ++ and ––(self study) 11.12 Case Study: A Date Class(self study)