I'm looking at namespaces and I don't really see a difference between these and classes. I'm teaching myself c++ I've gotten several books online, so i know I'm not learning the most effectively. Anyways can someone tell me the difference between the two and what would be the best time to use a namepace over a class. Also, I don't see much about structs in the book I'm reading.
Is this the format?
struct go
{
goNow(){ cout << "go Now"};
}
thanks in advance for your assistance.
A namespace is a way of grouping identifiers so that they don't clash.
A class is defeinition of an object that can be instantiated (usually) and which encapsulates functionallity and state.
Namespaces and classes are entirely different, and serve different purposes. They have some syntactic similarity.
Structs are classes, with a different default access specifier (public for struct, private for class) - in all other aspects they are the same.
From wikipedia
On the other hand
A class defines a type
.A namespace may contain multiple classes.
EDIT
One difference would be this:
You can have unnamed namespaces but you can't have a unnamed class
You can search on the web for the differences and i am sure you will find many; but the following are important IMHO:-
A namespace defines a new scope and members of a namespace are said to have namespace scope. They provide a way to avoid name collisions (of variables, types, classes or functions) without the inconvenience of handling nested classes.
One major difference is that namespaces can be re-opened, but classes cannot be:
is legal, but:
is not
A class is a data type. If you have a class named
Foo
, you can create objects of classFoo
and use them in many ways.A namespace is simply an abstract way of grouping items together. Normally, you cannot have two functions in your program named
bar()
. If you place them in separate namespaces, then they can coexist (for example, asA::bar()
andB::bar()
). A namespace cannot be created as an object; think of it more as a naming convention.If you are writing code that you want to be associated with an object that you can define and use as a variable, write a class. If you are writing an API or library and you want to wrap up all of the functions and constants so that their names don't clash with anything that the user might have written, use a namespace.
Classes and structs define types. You can create an object of a type. Namespaces simply declare a scope inside which other types, functions, objects, or namespaces can exist. You can't create an object of type
std
(unless of course you created a type calledstd
, which would hide thestd
namespace).When you define a function inside a struct/class (a method) you're saying "This function is a fundamental operation on the associated data". When you define a function inside a namespace you're saying "This function is logically related to other functions, types, and objects in the namespace"
Edit
It's probably worth pointing out that "everything is an object" languages like Java and C# regularly use classes as if they were namespaces because they don't allow "free" functions. This may be where the confusion comes from. If you have a class in another language that contains nothing but static members, you would want to use a namespace and free functions in the C++ version.