19-OperatorOverloading

19-OperatorOverloading - #include #include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <cstring> #include <iomanip> using namespace std; // The operators for c++ work for many of the different // simple types that are already available in the language // such as the + operator being able to handle integers, // doubles etc, as well as the << and >> operators being // able to handle many of the built in types. The fact // that they know how to deal with the various types // is because they have been overloaded to deal with // them. For our user defined types the operations // available to use with c++'s operators aren't likely // to give us what we want. So, what we have to do is // alter the operators such that they know what to // do with the new types. // Note, new operators are allowed by the language // Operator overloading allows us to write code without // having to use explicit function calls to do the same // thing, such as with a string class. We have already // seen functions that will manipulate strings, such // as concatenating two strings together. If the + // operator is overloaded, then we wouldn't need to // make a function call, we could instead just use // the + operator // overloadable ops: //+ - * / = < > += -= *= /= << >> //<<= >>= == != <= >= ++ -- % & ^ ! | //~ &= ^= |= && || %= [] () , ->* -> new //delete new[] delete[] class Fraction { private: int numerator; int denominator; public: Fraction(int numerator = 0, int denominator = 1) { mutate(numerator, denominator); } // An add function that will add a fraction // to the current object and return a new // fraction as a result. // The original fraction is unaltered { Fraction temp; temp.denominator = this->denominator * rhs.denominator; temp.numerator = rhs.denominator * this->numerator + this->denominator * rhs.numerator; return temp; } void mutate(int numerator = 0, int denominator = 1) { if (denominator != 0) { this->numerator = numerator;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
this->denominator = denominator; } else { this->numerator = 0; this->denominator = 1; } } void print() const { cout << numerator << "/" << denominator << endl; } { Fraction temp; temp.denominator = this->denominator * rhs.denominator; temp.numerator = rhs.denominator * this->numerator + this->denominator * rhs.numerator; return temp; } { Fraction temp; temp.denominator = denominator; temp.numerator = rhs * numerator + denominator * rhs; return temp; } // The generalization of overloading an operator is: // return_type operator op (parameter list); // The generalization of a member function is: // return_type function_name (parameter list); // so there isn't much difference, except the return // type for the overload is often the class type // += { this->numerator = rhs.denominator * this->numerator +
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

19-OperatorOverloading - #include #include...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online