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
  • 254,901
  • 44
  • 429
  • 631
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
  • 60,864
  • 6
  • 61
  • 103
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,436
  • 2
  • 20
  • 40
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,699
  • 3
  • 31
  • 44