Programming definitions

Hey everyone!

I have two questions really, I was wondering if you can help. They're a bit funny, I don't think you can really google them:

Firstly, I've just started to learn C++ and I've always thought that anyone with a average level of intelligence can learn anything they want to. I consider myself to have an average level of intelligence. As long as you put the right amount of effort in you'll get it. You just have to figure out how to relate it to yourself. Would you agree? An Elder was saying to me "I think it's one of those things you either have a natural edge for or you'll never get it". I'm determined to learn it. What do you think? Did you all find it hard at first or did it come quite naturally?

Secondly, I'm getting extremely confused by the grammar used - I can write a basic script for "Hello World" but I want to know more about how the inner bits work. I know that at the beginning of a script I should write:

1
2
#include <iostream>
using namespace std;


That is a habit I have been told to do from the book and so I do it. Because of this I can write shorts scripts where the computer outputs a message and I can input a message but when I try and find out why I have to do this habit, I get lost.

For example, when I wanted to know what the purpose of a "Namespace" is. This is a definition I found on page 10 of a book designed for "People with no programming experience at all":

"A namespace creates a declarative region in which various program elements can be placed. Elements declared in one name space are separate from elements in another."

Although that kind of make sense to me, it's written in such a way that the wording confuses me. It seems as though the writer has forgotten I have no programming experience at all!

I consider myself lucky because I know a little bit about programming but "Declarative regions", "elements", "syntax", "Constants", there's so many words I don't understand and it just expects me to know it from the start!

In short, is there somewhere I can find a really basic, informal definition for these words so I can relate it to myself?

In advance, I'd like to thank you for your responses! :)

Daryl
closed account (S6k9GNh0)
A namespace is... that definition :D. I'll try and provide some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

namespace example
{
   int func() { return 1; }
}

namespace test
{
   int func() { return 5; }
}

int main()
{
   using example::func;
  
   std::cout << func() << std::endl;
}


This shows a basic use of namespaces. You have two functions with the exact same name. Placing them in different namespaces allows us to differentiate them. For instance, in the STL, there is a class named std::list. What if we want a class named list as well? We would just use another namespace. In older C++ and C, you couldn't do this so you would just have to keep using different names e.g. stdList instead of std::list. A good example of this is wxWidgets. It's a C++ library that does *not* use a namespace. All of its members are prefixed with wx, example is: wxFrame or wxWindow.

In C++, if something is not within a namespace, then it is within the global namespace. If the namespace is not given a name, it's considered a special namespace called the anonymous namespace. It's similar to global namespace but it has special properties that will probably confuse you if I tell you :D. There is nothing that is not within a namespace in C++.

Also, I think the book is wrong. Making that a habit isn't necessarily a good thing. As a matter of fact, I made it a factor in my programming to *not* do that. In the Boost libraries and in the standard library, there are very similar members, so close that they are almost interchangeable. Using that declaration would 1) increase compile time, 2) confuse which namespace a member is being used in, and 3) possibly make code less readable. You'll almost always see me using std::cout instead of cout. The explicit use of the namespace makes it clear where it is coming from. However, this can be a bad thing since it might lose flexibility with other namespaces the give the same functionality as another, e.g. boost::bind and std::bind are generally interchangeable thus it may not be a good thing to add the prefixed namespace if used all over the code in the case you want to switch between the namespace.
Last edited on
Namespaces help avoid name collisions. If I write a library that has a global function called add and you write one called add and we put them in different namespaces there is no problem (same applies to global variables). Of couse the statememt using namespace std; means you don't have to specify the namespace to access the stuff in it. Might not work if using other libraries with the same global names.
Cheers for the help guys but that was just an example of the problem I have when I'm looking at every C++ definition and it's driving me mad because I don't seem to understand the definition of anything because it's described with such complexity!

I kinda understand a namespace but I ideally needed some resource that just says "Yeah a namespace is like a notepad: You can have lots of them and your computer writes the variables down on the notepad you tell it to. When your computer is doing a bit of code, it has a look at the notepad you've told it to look at and this means you can have the same name for multiple variables, they just have to be written down in different notepads so it doesn't get confused!"

Basically, what I'm asking, is "Is there anyway I can get a C++ for utter retards" book? Not dummies, just literally an average bloke.
Well, the thing is that you need to learn the words/phrases (and their meaning). Without that you won't get far.

So don't evade them. The more often you read about it the more you get used to it.

Most of it you will find in wikipedia anyway
Topic archived. No new replies allowed.