Book Image

Microsoft Visual C++ Windows Applications by Example

By : Stefan Bjornander, Stefan Björnander
Book Image

Microsoft Visual C++ Windows Applications by Example

By: Stefan Bjornander, Stefan Björnander

Overview of this book

Table of Contents (15 chapters)
Microsoft Visual C++ Windows Applications by Example
Credits
About the Author
About the Reviewer
Preface
Index

Operator Overloading


C++ supports operator overloading; that is, letting a method of a class be disguised to look like an operator. The priority and associativity of the operators are unaffected as well as their number of operands. As an example, let us look at a class handling rational numbers. A rational number is a number that can be expressed as a quotient between two integers, for instance 1/2 or 3/4. The integers are called numerator and denominator, respectively.

Rational.h

class Rational
{
  public:
    Rational(int iNumerator = 0, int iDenominator = 1);

    Rational(const Rational& rational);
    Rational operator=(const Rational& rational);

    bool operator==(const Rational &number) const;
    bool operator!=(const Rational &number) const;
    bool operator< (const Rational &number) const;
    bool operator<=(const Rational &number) const;
    bool operator> (const Rational &number) const;
    bool operator>=(const Rational &number)...