/*
    overloading.cpp how to write a multiple functions with the same name.  See Nyhoff P390-392.

    C++ allows us to define many functions with the same name. Since C++ knows the type of
    all variables, it picks the specific function matching the type of the variable.

    Lets for instance say that we want to define an isZero function for integers, doubles,
    complex and string. What would be do?  The meaning of zero is different for different types.

    1) Write code to check some of the examples below.
    
    2) Remove the remarks. What error will you get?
    
    cout has type ostream, so we would have to define a isZero function for ostream e.g.:
    
    bool isZero(const ostream & cstr)
    {
       return cstr.good();
    }
*/

#include <iostream>         
#include <string>           
#include <complex>
using namespace std;

const double SMALL = 1e-10;   // Arbitrary number.

bool isZero(int n)
{
   return n==0;               // fairly obvious.
}

bool isZero(double x)
{
   return fabs(x)<SMALL;      // |x|<small perhaps less obvious.
}

bool isZero(complex z)
{
   return norm(z)<SMALL;      // |z|<small perhaps even less obvious.
}

bool isZero(string s)
{
   return s.length()==0;      // No characters.
}

int main()
{         
   double x=1e-12;   
   cout << "x=" << x << "   isZero(x)=" << isZero(x) << "\n";

/*
   cout << "isZero(cout)=" << isZero(cout) << "\n";
   
*/

   return 0;
}
