2
votes

I am a student who is learning C++. I have gone through tutorials in the Internet. All of them do not use a .h extension after some include files, such as

#include <iostream>

But my C++ lecturer says that I have to include a .h extension after iostream, such as:

#include <iostream.h>

My book "The Waite Group's Object Oriented Programming In Turbo C++" written by Robert Lafore also tells me to put a .h extension after iostream. Both my lecturer and the book says there is no need to have the following line of code when using the cout and cin functions.

using namespace std;

When using cout and cin functions, the namespace std is needed, right? If I try to do what the book and lecturer tells, my compiler(g++) gives me errors. The book also says to use

void main()

rather than

int main()

If I follow what the lecturer or the book says, I get errors during compiling. What is happening? I am using g++ on Linux Mint 17 for compiling.

3
Your C++ lecturer is a little out of date :)Jonathan Potter
The <iostream.h> header file is from before C++ was standardized in 1998.Some programmer dude
Your lecturer, compiler and book are so out of date that you're better off dropping all of them and learning by yourself. There's a good list of books here.juanchopanza
which is 13 years ago...tumdum
Anything that has "turbo c++" in the title is largely obsolete. Unless you want a job in a turboc++ shop.juanchopanza

3 Answers

11
votes

Your lecturer and his book are incorrect/depend on things that were sort of the case 20 years ago.

Before C++ was standardized in 1998, many compilers (or rather: their library implementations) did in fact know a header <iostream.h> in which several symbols that are in the namespace std in standard C++ existed in the global namespace. Which symbols exactly varied from compiler to compiler, as many things did in those days. These headers were not included in the C++ standard of 1998, and not in any after them. Today, recent compilers will outright reject code that attempts to use them.

But that will not convince your lecturer that he was wrong these last 15 years, so here's what you can show him (if you believe that doing so will not make you an enemy for life):

This is a link to the last publicly available draft of the C++11 standard, which is the one you can reasonably expect to be able to use today. Open it, go to page 429 (or search for [headers]), see that <iostream> is listed while <iostream.h> is not.

See at the bottom on page 428 in [contents] that symbols of the standard library are in namespace std:

All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std. (...)

Additionally, about the main function, go to page 61 (or search for [basic.start.main]) to see that void main is not allowed:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is imlementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* ... */ }

and

int main(int argc, char *argv[]) { /* ... */ }

(...)

(Emphasis mine)

4
votes

This book appears to be very outdated. Get a new one.

Do not write <iostream.h>, <iostream> is correct (same for all standard headers).

Do not use using namespace std;, here is why. Use the fully qualified names like std::cout << "Hello World\n"; instead.

Your main should either be int main () or int main (int argc, char **argv), depending on if you want to handle command line arguments or not.

Also, do not use TurboC++, it is terribly outdated.

-2
votes

According to my knowledge and what my prof told us last time:

You can choose to or not to include with the .h. The .h are usually older versions. Once again the using namespace can be optional for you right now. (Does not means this is a good practice, but as a student many schools allowed it.)

Some books explain with using namespace, some don't.

So what is the difference of using namespace and not using it?

Using cout for example. If you used namespace std at the beginning of your codes. You can simply do this:

cout << "hello" ;

However, if you didn't add the line using namespace std;. You have to write it this way:

std::cout << "hello";

By the way, we always use int main() in school back then when I was in university. I don't think void main() is a good idea.