160 likes | 174 Views
Learn about operator overloading in C++ from basic function overloading to advanced binary operations. Practice with Rational class examples.
E N D
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.
11.3 Operator Overloading (P.488) • int a, b, c; • string x, y, z; • a = b + c; // integer addition • x = y + z; // string catenation
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?
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;
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();
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;
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().
Exercise: Operators - * / • Overload the subtraction, multiplication and division operators of class Rational. (P.530 Ex 11.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.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)
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:
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
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
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
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()