1

Generally when somebody creates a console program he writes

#include <iostream>
#include <stdlib.h>

int main(){
    std::cout<<"hello world"<<std::endl;
    system("pause");
}

The std must be included to call cout and endl statements.

When I create a library using the headers and the code in the .h and .cpp, and then I include that library, I must use the name of functions/clases/structs/etc directly. How can I make it so I have to use a pre-word like the std for cout and endl?

Keith Thompson
  • 242,098
  • 41
  • 402
  • 602
newtonis
  • 47
  • 1
  • 8

3 Answers3

4

It's called a namespace.

You can declare your own stuff inside a namespace like this:

namespace mystuff
{
    int foo();
}

To define:

int mystuff::foo()
{
    return 42;
}

To use:

int bar = mystuff::foo();

Or, import a namespace, just like you can do with std if you don't want to fully-qualify everything:

using namespace mystuff;
// ...
int bar = foo();
paddy
  • 55,641
  • 6
  • 55
  • 99
0

you must define namespace like this

namespace mynamespace {
    class A{
        int func(){
        }
    }
    void func2(){}
}

you can import namespace like this

using namespace mynamespace;
Hossein Nasr
  • 1,346
  • 2
  • 17
  • 39
0

STD prefix is a namespace.

to define/declare a namespace you can follow that example:

namespace test
{ int f(); };

f belong to the namspace test. to call f you can

test::f();

or

using namespace test;
 ....
 f();
alexbuisson
  • 7,007
  • 3
  • 27
  • 42