ex6-3.cpp - Example 6-3 Operator overloading - fraction class with ! and +

// File: ex6-3.cpp - overloaded + and ! operator for the fraction class

#include <iostream>
using namespace std;

class fraction
{
  private:
	 int numer;
	 int denom;
  public:
	 fraction(int n = 0, int d = 1);
	 void operator!(void) const;
	 fraction operator+(const fraction&);
};

fraction::fraction(int n, int d)
{
  numer = n;
  denom = d;
}

void fraction::operator!(void) const
{
  cout << numer << '/' << denom << endl;
}

fraction fraction::operator+(const fraction& f2)
{
  fraction temp(0,0);
  temp.numer = numer * f2.denom + f2.numer * denom;
  temp.denom = denom * f2.denom;
  return temp;
}

int main(void)
{
  fraction f(3,4);           // initialize fraction f & g
  fraction g(2,3);
  fraction h = f + g;
  !h;
  return 0;
}



CIS27: Programming in C++    Instructor: Joe Bentley