X hits on this document

47 views

0 shares

0 downloads

0 comments

11 / 14

The pre-increment and post-increment functions look exactly the same:  same function name and same number and type of arguments.  In order to distinguish the two forms, an extra (unnamed) dummy parameter is included in the post-increment form.

//overloading7.cpp

//uses Rational class w/ overloaded ++ operators added

//Borland C++ 5.02

//Project Target Type: Application - Target Model: Console

//modified from Hubbard

#include <iostream>

class Rational {

friend Rational operator* (const Rational&, const Rational&);

  friend int operator== (const Rational&, const Rational&);

  friend ostream& operator<< (ostream&, const Rational&);

public:

  Rational (int=0, int=1);

  Rational (const Rational&);

  Rational& operator= (const Rational&);

  Rational& operator*= (const Rational&);

  Rational operator++();//pre-increment

  Rational operator++(int);//post-increment

private:

int num, den;

  int gcd (int, int);

  void reduce ();

};

int main(){

Rational x(23,7), y, z;

  cout << "x =  " << x << "  y = " << y << endl;

  y = x++;

  cout << "x =  " << x << "  y = " << y << endl;

  cout << "\nx =  " << x << "  z = " << z << endl;

  z = ++y;

  cout << "x =  " << x << "  z = " << z << endl;

  cout << "\n\n\nPress any key to close console window:  ";

  char c; cin >> c;

return 0;

}

Rational::Rational (int n, int d) : num (n), den (d){

reduce();

}

Rational::Rational (const Rational& r) : num(r.num), den(r.den){

}

Rational& Rational::operator= (const Rational& r){

num = r.num;

  den = r.den;

  return *this;

}

overloading.doc11 of 14

Document info
Document views47
Page views47
Page last viewedSat Dec 10 08:24:33 UTC 2016
Pages14
Paragraphs547
Words2961

Comments