In the C++ program below, I include the string.h file and I successfully instantiate the C++ string class in it and call one of its member functions: size().
#include <iostream>
#include <string.h>
using namespace std;
int main( )
{
string s = "Hello";
cout << "String: " << s << endl;
cout << "Size of string: " << s.size() << endl;
cin.get();
return 0;
}
The output is:
String: Hello
Size of string: 5
I am using Dev-C++ 4.9.9.2
My question: doesn’t the string.h file just provide the functions for manipulating C strings? It doesn’t include the definition of the C++ string class right? So, how is it that I am able to access the C++ string class without using #include <string>? My understanding is that the string.h file is the C strings library file and <string> includes the C++ string library file. Is this not right?
Thanks!
This is because
std::stringis defined though one of the files included in the<iostream>header. The streams provide support for input and output of strings, so they need to include a string header in order to define the corresponding>>and<<operations.