X hits on this document

41 views

0 shares

0 downloads

0 comments

13 / 14

//overloading6.cpp

//uses Rational class w/ overloaded conversion operator 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&);

  operator double() const;//const added for use on

private:// const Rationals

int num, den;

  int gcd (int, int);

  void reduce ();

};

int main(){

Rational x(-5,8);

  const Rational PI(22,7);

  cout << "x =  " << x << " = " << double(x) << endl;

  cout << "PI =  " << PI << " = " << double(PI) << 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;

}

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

num = num * r.num;

  den = den * r.den;

  return *this;

}

Rational::operator double () const{

return double (num)/den;

}

overloading.doc13 of 14

Document info
Document views41
Page views41
Page last viewedMon Dec 05 06:59:07 UTC 2016
Pages14
Paragraphs547
Words2961

Comments