参观湖北省反腐倡廉警示教育基地有感_第1页
参观湖北省反腐倡廉警示教育基地有感_第2页
参观湖北省反腐倡廉警示教育基地有感_第3页
参观湖北省反腐倡廉警示教育基地有感_第4页
参观湖北省反腐倡廉警示教育基地有感_第5页
已阅读5页,还剩25页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Chapter 13Operator Overloading(运算符重载13.1 Introduction13.2 Operator Functions13.3 Overloading Simple Operators13.4 Overloading “13.5 Overloading “13.6 Object conversion13.7 Overloading “= 13.8 Overloading “() and Functor13.1 IntroductionOperators 运算符How about object variables?String operators2string

2、s1 = ABC;string s2 = s1 + DEF;couts1endl;couts2)endl;.int a = 1;int b = a+1;if(ab) couta;class C;C c, a, b;c = a + b;/?The Rational Class 分数31/2, 1/3, 11/12,3 4 23 84 7 32 9baRational.cppRunRational.hTestRationalClass.cpp分子分母Operation of the Rational Class4Rational Rational:add(Rational &secondRatio

3、nal) long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator();long d = denominator * secondRational.getDenominator();return Rational(n, d);Rational Rational:subtract(Rational &secondRational)long n = numerator * secondRational.getDenominator() - denominator

4、* secondRational.getNumerator();long d = denominator * secondRational.getDenominator();return Rational(n, d); coutr1.toString() + r2.toString() = r1.add(r2).toString() endl; coutr1.toString() - r2.toString() = r1.subtract(r2).toString() endl; cout r1 + r2 = r1+r2 endl; cout r1 - r2 = r1-r2 endl; Ope

5、rator Functions 运算符函数The functions to overload operators5bool Rational:operatorcompareTo(secondRational) 0) return true; else return false;if(r1 r2) cout“r1 is less than r2.endl;if(r1.operator(r2) cout“r1 is less than r2.endl;Overloadable Operators 6Unoverloadable Operators: . *? :#13.3 Overloading

6、Simple OperatorsRelational Operators7bool Rational:operatorcompareTo(secondRational) 0) return true; else return false;bool Rational:operator (Rational &secondRational)long n = numerator * secondRational.getDenominator() denominator * secondRational.getNumerator();if (n 0) return true; else return f

7、alse; A member function of Rational class.Parameter: usually pass-by-reference (can by value) one for a binary operator (The other one the object of the function)keywordReturn type: the data type of “add(secondRational);Return type: Rational, the data type of “+To be used as an operand in complex op

8、eration!NotesThe left operand is fixed to be the operating object automatically c1 = c2 + c3 ; c1 = c2.operator+(c3); The number of parameters is determined by the operator itselfYou cannot change itOverloading does not change the operator precedence 优先级 and associativity 结合性The (return) type of the

9、 operator function can be defined by youUsually the same class type to be operational in complex operation9Overloading Shorthand Operators10+=, -=, *=, /= Rational Rational:operator+=(Rational &secondRational) *this = this-add(secondRational); return (*this);Rational r1(2, 4);Rational r2 = r1 += Rat

10、ional(2, 3);cout r1 is r1.toString() endl;cout r2 is r2.toString() endl;Overloading Unary Operators 11+ and - -no parametersRational Rational:operator-() numerator *= -1; return *this;Overloading + and -Prefix vs. Postfix12+r1r1+Rational Rational:operator+() numerator += denominator; return *this; R

11、ational Rational:operator+() Rational temp(numerator, denominator); numerator += denominator; return temp;(int dummy)Same signature,How to distinguish them?哑元参数,the value is never used;It must be “int.Same signature,How to distinguish them?13.4 Overloading “The array subscript is an operator You can

12、 overload this operator to access the contents of the object using the array-like syntax For example, r0: to access numerator r1: to access denominator13Overloading “ 14long Rational:operator(const int &index) if (index = 0) return numerator; else if (index = 1) return denominator; else cout subscri

13、pt error endl; exit(0); Accessor and MutatorThe operator functions as both accessor and mutator. After adding this operator to the Rational class, the Rational class is mutable.15Rational r4(1, 2); r40 = 3; r41 = 4;long &Rational:operator(const int &index)13.5 Overloading “: stream extraction operat

14、orFor reading values from cinOverload them?16Rational r4(1, 2); coutr40endl;coutr41endl;Rational r4(1, 2); coutr4;Overloading “17Rational r4(1, 2); coutr4;Rational Rational:operator (?) Rational Rational:operator (ostream &str, Rational &rational) !The left operand is fixed to be the operating objec

15、t automatically:Rational Rational:operator+(Rational &);an instance of ostreamThen, how to do?Overloading “Making use of friendship!18 friend ostream &operator(istream &, Rational &);ostream &operator(ostream &str, Rational &rational) str rational.numerator / (istream &str, Rational &rational) cout

16、rational.numerator; cout rational1; return str;Other operators can also be overloaded as friends!13.6 Object ConversionConvert a class type to other data types?Using operator functions19double i, j;i=4 + 5.5;double i;Rational r1;i= 2+r1;Object Conversion Rational:operator double() return 1.0 * getNu

17、merator() / getDenominator();20Rational r1(1, 4);double d = r1 + 5.1; cout r1 + 5.1 is d 60; ;int main()Comparer cmp;int i;cini;if(cmp(i) coutYes, passed!n;else cout60;Object acting as a functionFunction object (functor, 仿函数)Functor as ParameterTo count an integral arrays elements that satisfy some condition, e.g. =60 =85=9026int countArray(int a, int s, int th);int countArray(int a, int s);int countArrayIf(int a, int s, Comparer cmp);Functor as Parameter27class Comparerpublic: Comparer(int val = 60):vs(val) int

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论