1 / 16

Chapter 11 Operator Overloading

Chapter 11 Operator Overloading. Function Overloading (P.266). int square( int x ); double square( double x ); int max( int x , int y ); int max( int x , int y , int z );. L.5 Several functions of the same name that perform similar tasks, but on different data types.

beard
Télécharger la présentation

Chapter 11 Operator Overloading

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 11Operator Overloading

  2. Function Overloading (P.266) • int square( int x ); • double square( double x ); • int max( int x , int y ); • int max( int x , int y , int z ); L.5 Several functions of the same name that perform similar tasks, but on different data types.

  3. 11.3 Operator Overloading (P.488) • int a, b, c; • string x, y, z; • a = b + c; // integer addition • x = y + z; // string catenation

  4. 11.4 Overloading Binary Operators (P.489) • Let’s consider the class Rational: class Rational {public: Rational(int n, int d): numerator(n), denominator(d) {}; int numerator; int denominator; }; • How do we perform addition on two rational numbers?

  5. Addition of Rational Numbers • Rational a, b, c; • a.denominator = b.denominator * c.denominator; • a.numerator = b.numerator * c.denominator + b.denominator * c.numerator; • cout << a.numerator << '/' << a.denominator << endl;

  6. Member Functions add() and print() Rational add(const Rational& c) const { Rational result(0, 1); result.denominator = denominator * c.denominator; result.numerator = numerator * c.denominator + denominator * c.numerator; return result; } void print() const { cout << numerator << '/' << denominator << endl; } a = b.add(c); a.print();

  7. operator+() Rational operator+(const Rational& b) const { Rational result(0, 1); result.denominator = denominator * b.denominator; result.numerator = numerator * b.denominator + denominator * b.numerator; return result; } Now C++ knows how to perform a = b + c;

  8. operator+() and add() Rational add(const Rational& b) const { Rational result(0, 1); result.denominator = denominator * b.denominator; result.numerator = numerator * b.denominator + denominator * b.numerator; return result; } Rational operator+(const Rational& b) const { return this->add(b); } You may re-use the member function add().

  9. Exercise: Operators - * / • Overload the subtraction, multiplication and division operators of class Rational. (P.530 Ex 11.10)

  10. 11.5 Overloading the Binary Stream Insertion (<<) friend ostream& operator<<( ostream& output, const Rational& b ) { output << b.numerator << '/' << b.denominator; return output; } a.print(); cout << " = "; b.print(); cout << " + "; c.print(); cout << endl; cout << a << " = " << b << " + " << c << endl;

  11. 11.12 Overloading the Conversion Operator (P.515) • Conversion operator • Also known as “cast operator” • Used to convert an object of one class to an object of another class (usually a fundamental data type) (P.516 L.10) • As other overloading operators, this is achieved by defining a member function: • CLASS::operator OtherClass() const; • CLASS::operator int() const; • CLASS::operator char*() const; • With this cast operator (char*), you can use cout without overloading the operator<<. (L.-7)

  12. Stream Insertion Operator (<<) • Consider the one we defined for Rational: friend ostream& operator<<(ostream& output, const Rational& b) { output << b.numerator; if (b.denominator > 1) output << '/' << b.denominator; return output; } • Actually, it has some alignment problem:

  13. setw() in <iomanip> • Consider the following code: #include <iostream> #include <iomanip> #include "Rational.h" using std::cout; using std::setw; int main() { Rational a(1, 3); Rational b(11, 3); Rational c(1, 33); cout << setw(5) << a << setw(5) << a << '\n' << setw(5) << b << setw(5) << b << '\n' << setw(5) << c << setw(5) << c << '\n'; return 0; } • setw() only affects the next output item (numerator)! 1/3 1/3 11/3 11/3 1/33 1/33

  14. Cast Operator for Class Rational You need not specify the return type. The return type is “char*”! If you don’t know ostringstream (Ch18, P.746), try this: operator char*() const { // Conversion operator (a.k.a. cast operator) string s = to_string(numerator) + "/" + to_string(denominator); char* p = new char[s.length() + 1]; strcpy(p, s.c_str()); // g++ (GCC) 4.8.2 does not support to_string yet. return p; } operator char*() const { ostringstreamoss; if (denominator > 1) oss << numerator << '/' << denominator; else oss << numerator; string s = oss.str(); char* p = new char[s.length() + 1]; strcpy(p, s.c_str()); return p; }; 1/3 1/3 11/3 11/3 1/33 1/33

  15. 11.9 Dynamic Memory Management • Review Section 14.1.8 Stack vs Heap Allocation in last semester. Heap Memory space for static variables dynamically allocated by the new operator Code Storing local variables parameters passed (by value)to the function Stack

  16. Heap vs. Stack Allocation • http://STU.ipv6.club.tw/~solomon/Lang/C++/heap_vs_stack.cpp heap heap heap heap f2() f1() f1() f2() main() main() main() main()

More Related