C++ programming
-
Hi friends! I am new to c++ and just went through the concept of Namespace.What I have understood about namespace till now is that "it is a region where identifiers can be declared in form of group". But then I went through the following code somewhere on internet Namespace identi { int a; Class S { public: int z; float x; } } So I am bit confused after seeing the definition of class S inside namespace.isn't it only a declarative region where we can only declare identifiers together in a group or can we define identifiers as well inside it? Just like we did with class S.Kindly someone please answer, would be so nice of you.
-
Hi friends! I am new to c++ and just went through the concept of Namespace.What I have understood about namespace till now is that "it is a region where identifiers can be declared in form of group". But then I went through the following code somewhere on internet Namespace identi { int a; Class S { public: int z; float x; } } So I am bit confused after seeing the definition of class S inside namespace.isn't it only a declarative region where we can only declare identifiers together in a group or can we define identifiers as well inside it? Just like we did with class S.Kindly someone please answer, would be so nice of you.
-
Hi friends! I am new to c++ and just went through the concept of Namespace.What I have understood about namespace till now is that "it is a region where identifiers can be declared in form of group". But then I went through the following code somewhere on internet Namespace identi { int a; Class S { public: int z; float x; } } So I am bit confused after seeing the definition of class S inside namespace.isn't it only a declarative region where we can only declare identifiers together in a group or can we define identifiers as well inside it? Just like we did with class S.Kindly someone please answer, would be so nice of you.
the main reason of why we use namespace identifier is classify our code in a bigger categories and parts. its useful to manage your libraries by "using namespaces". Imagine you can have many different name spaces in your code and every time you need you can use it. its helps you to choose smaller and more abstract names for your classes and functions. for example you can have many Mathematic function and classes in a namespace like "Matrix". in that case you can have simple function names like "add" , "div", "mul" instead of "matrix_add" ,"matrix_dev" and ... to use them in an specific part of your code you can use it in to ways .. 1:
Matrix.add(m1,m2);
Matrix.div(m3,m4);2:
using Matrix {
add(m1,m2);
div(m3.m4);
}also for defining classes and functions in a namespace you can do it like this ...
namespace Matrix {
void add(matrix a, matrix b){ ... } void div(matrix a, matrix b){ ... } void mul(matrix a, matrix b){ ... } . . .
}