1 / 32

Chapter 7 Operator Overloading and Type Conversions

Chapter 7 Operator Overloading and Type Conversions. §7.1 Defining Operator Overloading §7.2 Overloading Unary Operators §7.3 Overloading Binary Operators §7.4 Overloading Other Operators §7.5 Type Conversions. §7.1 Introduction. Operators (运算符) How about object variables?

mervin
Download Presentation

Chapter 7 Operator Overloading and Type Conversions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7Operator Overloading and Type Conversions §7.1 Defining Operator Overloading §7.2 Overloading Unary Operators §7.3 Overloading Binary Operators §7.4 Overloading Other Operators §7.5 Type Conversions

  2. §7.1 Introduction • Operators (运算符) • How about object variables? • String operators int a = 1; int b = a+1; if(a<b) cout<<a; class C{}; Cc, a, b; c = a + b;//? string s1="ABC"; string s2=s1+"DEF"; cout<<s1<<endl; cout<<(s1>s2)<<endl; ...

  3. 3 4 23 8 b 4 7 32 9… a The Rational Class (分数) 分子 分母 Rational.h 1/2, 1/3, 11/12,… Rational.cpp TestRationalClass.cpp Run

  4. Operation of the Rational Class • Rational Rational::add(Rational &secondRational) { • 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 * secondRational.getNumerator(); • long d = denominator * secondRational.getDenominator(); • return Rational(n, d); • } • … cout<<r1.toString()<< " + "<< r2.toString()<< " = "<< r1.add(r2).toString()<< endl; cout<<r1.toString()<< " - "<< r2.toString()<< " = "<< r1.subtract(r2).toString()<< endl; … cout << r1<< " + " << r2 << " = "<< r1+r2<< endl; cout << r1<< " - " << r2 << " = "<<r1-r2<< endl; …

  5. Operator Functions (运算符函数) bool Rational::operator<(Rational &secondRational) { if (this->compareTo(secondRational) < 0) returntrue; else return false; } if(r1.operator<(r2)) cout<<“r1 is less than r2.”<<endl; if(r1 < r2) cout<<“r1 is less than r2.”<<endl; The functions to overload operators

  6. Defining Operator Overloading • General form • Can also be friends: type classname :: operatorop(arglis) { Function body // task defined } vector operator+ (vector); // vector addition vector operator- (); // unary minus friend vector operator+ (vector, vector);//vector addition friend vector operator- (vector); // unary minus

  7. §7.2 Overloading Unary Operators • ‘+’ and ‘-’ --no parameters void space :: operator-(){ x=-x; y=-y; z=-z; } //declaration friendvoidoperator-(space &s); //definition voidoperator-(space &s) { s.x=-s.x; s.y=-s.y; s.z=-s.z; } Overloading Unary Minus

  8. Overloading ++ and -- • Prefix vs. Postfix Same signature, How to distinguish them? Same signature, How to distinguish them? • Rational Rational::operator++() { • numerator += denominator; • return *this; • } ++r1 r1++ • Rational Rational::operator++(){ • Rational temp(numerator, denominator); • numerator += denominator; • return temp; • } (int dummy){ 哑元参数,the value is never used; It must be “int”.

  9. Overloading “[]” • The array subscript [] is an operator • Can be overloaded to access the members of an object • long Rational::operator[](constint &index){ • if (index == 0) • return numerator; • else if (index == 1) • return denominator; • else{ • cout << "subscript error" << endl; • exit(0); • } • } • int main(){ • Rational r(2,3); • cout << r[0]/r[1]; • }

  10. [] Accessor and Mutator • The [] operator functions as both accessor and mutator. • After adding this operator to the Rational class, the Rational class is mutable. long&Rational::operator[](constint&index) Rational r4(1, 2); r4[0] = 3; r4[1] = 4;

  11. §7.3 Overloading Binary Operators //functional notation C = sum ( A, B); //arithmetic notation C = A + B; complex complex :: operator+(complex c){ complex temp; temp.x = x + c.x; temp.y = y + c.y; return(temp); } Overloading + operator

  12. Operands of Binary Operators • Features: • Only one complex argument • Returns a complex type value • A member function of complex complex complex :: operator+(complex c){ complex temp; temp.x = x + c.x; temp.y = y + c.y; return(temp); } C3 = C1 + C2; C3 = C1.operator+(C2);

  13. Operands of Binary Operators complex complex :: operator+(complex c) { complex temp; temp.x = c.x + x; temp.y = c.y + y; return(temp); } temp 4.10 6.20 C3 = C1 + C2; 4.10 x 2.50 x 1.60 x 6.20 y 3.50 y 2.70 y Fig.7.1 ⇔ Implementation of the overload + operator

  14. Notes • The 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 itself • You cannot change it • Overloading does not change the operator precedence (优先级) and associativity (结合性) • The (return) type of the operator function can be defined by you • Usually the same class type to be operational in complex operation

  15. §7.4 Overloading Other Operators +=, -=, *=, /= Rational Rational::operator +=(Rational &secondRational) { *this = this->add(secondRational); return (*this); } Rational r1(2, 4); Rational r2 = r1 += Rational(2, 3); cout << "r1 is " << r1.toString() << endl; cout << "r2 is " << r2.toString() << endl;

  16. Overloading “=” • By default, “=” performs a memberwise copy • For Rational class, it is OK. • How about others? Rational r1(1, 2); Rational r2(4, 5); r1 = r2; class Person{ private: int id; Date* birthDate; }; … p1 = p2;

  17. Overloading “=” • “The rule of three” • Copy constructor • Destructor • “=”

  18. Overloading “=” const Person Person::operator=(const Person &p){ id = p.id; Date *p=p.getBirthDate(); birthDate = new Date(*p); return *this; }

  19. Friend vs. Member Friend: the left-hand operand as an argument friendcomplex operator+(complex a, complex b); complex operator+(complex a, complex b) { return complex((a.x+b.x),(a.y+b.y)); } C3 = C1 + C2  C3 = C1.operator+(C2) C3 = C1 + 2  C3 = C1.operator+(2) C3 = 2 + C1  C3 = 2.operator+(C1) ??? C3 = 2 + C1  C3 = operator+(2, C1)

  20. Overloading >> and << • Two versions p = 2 * m;//equivalentto p = operator*(2,m) q = n * 2;//equivalentto q = operator*(n,2) • By overloading >> and << using functions: friend istream& operator>>(istream&, vector&); friend ostream& operator<<(ostream&, vector&); vector variables in input and output statements can be used like simple variables Overloading as friends.cpp

  21. Manipulation of Strings • To define operators to manipulate the strings • For example: string3 = string1 + string2; if( string1 >= string2 ) string = string1; • An example program overloads + and <= // overloading + operator String operator+(const String &s, const String &t){ String temp; temp.len = s.len + t.len; temp.p = new char[temp.len+1]; strcpy(temp.p,s.p); strcat(temp.p,t.p); return temp; } // overloading <= operator int operator<=(const String &s, const String &t){ int m = strlen(s.p); int n = strlen(t.p); if(m <= n) return(1); else return(0); } Mathematical operations on strings

  22. Overloadable Operators Unoverloadable Operators: :: . .* ? : # sizeof A friend cannot be used: = ( ) [ ] ->

  23. §7.5 Type Conversions • Automatic type conversion for built-in types • Constants and variables of different types are mixed in an expression • Right type of an assignment operator “=“ to the left • For example, int m; float x = 3.14159; m = x; • What happens when they are user-defined data types? Using Casting operator or Constructor

  24. Three types of situations • Conversion from basic type to class type • Conversion from class type to basic type • Conversion from one class type to another class type

  25. Basic to Class Type • Implemented by using constructors with single argument string::string (char * a) { length = strlen(a); p = new char[length+1]; strcpy(p, a); } string s1,s2; char* name1 = “IBM PC”; char* name2 = “Apple”; s1 = string(name1); s2 = name2;

  26. Class to Basic Type • By defining an overloaded casting operator • Usually referred to as a conversion function: operator typename(){ ...... ......(Function statements) } Rational::operator double(){ return numerator/denominator; } int main(){ Rational r(2,3); double d = r; } string:: operator char*(){ return (p); }

  27. Notes on class  basic type • It must be a class member • It must not specify a return type • It must not have any arguments

  28. One Class to Another Class Type • By either a constructor or a conversion function objx = objy // Y is a source class Class Y Casting operator function Converted value of type X Conversion here (source class) Class X Class Y Constructor function Data access functions Argument of type Y Conversion here (destination class) Fig.7.2 ⇔ Conversion between object

  29. An Example of Class to Class • A data conversion example • From invent1 to invent2 //constructor for conversion // invent2(invent1 p) { code = p.getcode(); value =p.getitems() * p.getprice(); } //operator function for conversion operator invent2() { invent2 temp; temp.code = code; temp.value = price * items; return temp; } Data conversions

  30. Summary of all the three conversions

  31. Summary • Operator functions to overload operators • Overloading as member or friend • Overloading unary, binary operators • Operands? • Overloading other operators • Type conversion

  32. Review Questons • Q7.3 • Q7.7 • Q7.8

More Related