C++ Programming

Status
Not open for further replies.
It saves typing...Take the hello world application for example:

With using namespace std;
Code:
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World";
    cout << endl;
    return 0;
}

Without using namespace std;
Code:
#include <iostream>
int main()
{
    std::cout << "Hello World!";
    std::cout << endl;
    return 0;
}

In a small application like that typeing std:: doesn't make too much difference, but in a large application typing all the namespaces can be a pain in the butt! So when you include a using statement, you no longer have to include the fully quallified name.
 
Without using namespace std;

#include <iostream.h>

int main()
{
cout << "Hello World";
cout << endl;
return 0;
}

would work properly know. Then why we have to use the

using namespace std;

statement separately. Its waste of time know????????/
 
Code:
#include <iostream.h>

int main()
{
cout << "Hello World";
cout << endl;
return 0;
}
will not work properly. Because cout is part of the std namespace. Which means it's fully quallified name is std::cout.
 
We are including iostream.h header file. This means standard classes for the methods for the classes will be automatically included in the statement know???
 
Not exactly. You still need to inlcude the namespace that's what the using statment does.
Try running you code sample, it will not run. Then add the using statment and it works fine.
 
I compiled the code without using the

Without using namespace std;

statement this long.I saw the code
in this forum's one of the thread. Then I got this doubt and posted a thread.

Can u clear my doubt????????
 
If the code worked then it's because cout and cin are so common that the compiler did it automatically.

cout (and cin) are both part of the std namespace. A namespace is like a collection of objects and classes. So if you have two classes named Math you could put them in different namespaces.

Say, for example, that I had two classes, bother were called Math. Yet one of them did operations with numbers and the other did operations with strings. Normally I couldn't have two classes both named Math. Because the compiler wouldn't know which one I was refering to. So to fix that, I would put them in different namespaces. String and Number (respectivly) then I could refer the Math class by it's fully qualified name:

String::Math
and
Number::Math

That way the compiler knows which Math class I'm talking about. Now then if I didn't like typing "Number::Math" each time but only wanted to type Math, I could say:
Code:
using namespace Number;
Then I would only have to type Math and not Number::Math. That little statment tells the compiler, "Hey, I'm using the Number namespace, so each time you see a class or object that looks like it comes some namespace, that's the one to use!"
 
Iron_Cross is right. Even in VB.net or ASP.net we import namespaces. Its a way of shortening the code. In case of C# its using followed by the namespaces name.
We write Console.WriteLine as namespace 'System' is automatically loaded.

If you make a new namespace 'mySpace' with some class 'square'. You can import this and directly call functions in it. Eg if there was a function Area(sidelength) which was SHARED. Then you could directly call it Area(5) wherever you imported the name space. C# namespace can be imported in VB.net and Vice versa. The same holds true for other languages too.

Even in general programming the namespace has to be imported to call the function directly/in shorthand.

Eg if you have to write Dim abc As New Thread, then you import System.Threading. Otherwise you have to write the complete thing.
You may not Import Std namespace cause it might be imported by default, just as 'System' namespace is.
 
Status
Not open for further replies.
Back
Top Bottom