I got some confusion in mind between what my professor told us at uni and what I have read in Stroustrup's book.
We all know that a C++
headeris basically a collection of declarations (defined in a file.h) and they can contain for example a collection of classes. They are very useful because they give us a lot of features stored in a single spaceA namespace is someting invented to organize classes, functions, types (...) in a part of the program without defining a type.
I cannot see the concrete difference here when I have to create a project.
If I had (for example) to make a program that solves equations of various degrees, I'd put the classes that I need in a single file. For example I am going to place in equations.h all this stuff: class secondDeg, class thirdDeg, class fourthDeg and so on.
Why should I use a namespace then?
The answer (I guess) is: because you can give a name for a better organization (see std::cin). But in this case I should
- Create equations.h (or whatever)
- Create a namespace called
eqfor example - Put my classes in the namespace
Is this really necassary? Cannot I only use a header file and put all my classes inside?