Why does this programn work without cmath? The code works fine. I was playing around and thought cmath was necessary. However, the code works the same without it.
#include <iostream>
//#include <cmath>
#include <Windows.h>
usingnamespace std;
// Function Prototype
double sqrRoot(double);
int main()
{
double num, result;
cout << "Enter a number and this program\n"
<< "will return its square root: ";
cin >> num;
try
{
result = sqrRoot(num);
cout << "The square root of " << num << " is " << result << endl;
}
catch (constchar *exceptionString)
{
cout << exceptionString;
}
cout << "End of program.\n";
system("pause");
return 0;
}
double sqrRoot(double num)
{
if (num <= 0 || sqrt(num) != static_cast<int>(sqrt(num)))
throw"Error: The number is not a perfect square.\n";
return sqrt(num);
}
Look inside of windows.h, I imagine cmath is included as a child header in it or one of its #includes. I'm on Unix so I don't have a copy of it but an easy way to find out if that's the case would be to comment out #include <Window.h> and see if it still compiles.
A standard C ++ header may include other standard C++ headers (either in full or in part). In this particular implementation, the declaration of at least one of the overloads of std::sqrt was present in some header included by <iostream>http://rextester.com/HOB4139
> I was playing around and thought cmath was necessary.
It is necessary.
The entities in the C ++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate #include preprocessing directive
Even in the case where it appeared to work, all the overloads of std::sqrt() need not be visible; we may not always be calling the overload that would have been called had we included <cmath>
Okay thank you JLBorges. That makes perfect sense. edge6768 Windows.h would not, respectively, have anything to do with sqrt. Thank you both for the response.