The code doesn't seem to display right in Yahoo Answers so I will just post the link to the site I asked this question on. The link takes you directly to the comment I made, which is by the username Wesley. (Random name I thought of)
http://www.learncpp.com/cpp-tutorial/06-...
You can respond to the comment on the site or directly here in Yahoo Answers.
Reason for asking:
The C++ book I ended up buying shows the code like that and also other codes. Instead of what I'm used to.. cout %26lt;%26lt; "Hello world"
it's some other ****.
Two different HELLO WORLD programs in c++ what's the difference?
if you pick the "namespace std" then you don't put std in it.
cout %26lt;%26lt; "Hello World!n"; //don't put std if you use namespace std (that is standard namespace.)
Howerver if you not pick a "namespace std" then put std in it. such as:
std::cout %26lt;%26lt; "Hello World!n"; // you must put std if don't pick namespace std;
Reply:The true beauty of computer programming is that there are many ways of providing a valid solution. The difference you are seeing is with the standard namespace.
Namespaces allow you to give a "possession" to written code. For instance, if you write a function and Chuck Norris writes a function, you can each put it in your own namespace. Say if Norris wants to use your function, he will include your namespace, so when he's using it, he will know to whom the code belongs. It is used often in work environments.
In C++, you can use the standard namespace by writing the statement
using namespace std;
or you can use std:: with each statement that is part of the namespace. As you will see in programming with the standard namespace, it is good practice to use the single line above your main program. Try out each way and get accustomed to different code, because as you will see, everyone writes their code differently.
Reply:Many functions and objects in C++ are standard and are contained in the standard library. Neither example is wrong. To help clarify your confusion, you should look up namespaces and the using statement, as well as the scope resolution operator ::. This is probably beyond your understanding, but cout is an ostream object. At this point, don't be too concerned about that, but think of it like a variable that is declared. Now, say by accident you made a variable called cout (eg. int cout;). How would the compiler know which cout that you are referring to? Well, through the use of namespaces, one can put all their created objects and variables in a namespace. cout happens to reside in a namespace called 'std', which is short for standard. 'endl' is also in there. The proper way to use a namespace is to put the namespace, followed by the scope resolution operator ::, then the name of the object (eg. std::cout, std::cin, std::endl). However, this can beome cumbersome in a large program. The way to save some typing is with the using statement. There are a few ways to use this statement. One can use it locally in a function or globally. Many people will use the std namespace globally, so at the top of their program after the includes, you will see " using namespace std;" This means anything in that namespace, you will not need to put std:: infront of it, though you still can.
So you can do helloworld either way:
using namespace std;
int main()
{
cout %26lt;%26lt; "Hello World!" %26lt;%26lt; endl;
// you can still even do std::cout with
// the using namespace std; statement
return 0;
}
or
int main()
{
std::cout %26lt;%26lt; "Hello World!" %26lt;%26lt; std::endl;
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment