What are namespaces used for | Sololearn: Learn to code for FREE!
0

What are namespaces used for

i dont how they work but what are they used for

4th Jun 2019, 6:07 AM
Cat Sauce
Cat Sauce - avatar
2 Answers
+ 4
Namespaces are mostly used(as far as I know of) to have a variable name multiple times but with different declarations. This can also be usefull to group variables together. Let me clearify: use namespace std; namespace test1 { string var = "This variable is part of namespace test1"; } int main() { int var = 10; cout << var ; // To access the namespace var use: cout << test1::var ; return 0; }
4th Jun 2019, 6:45 AM
Jorn S
Jorn S - avatar
+ 4
Namespaces are used to prevent names clashes in large projects. You can declare a foo() function two times, namespaces help you in this case. App::foo(); Utility::foo(); Or if you declare a bar() function still present in a header you included.. namespace MyNames{ int rand(int range){ returns a int random number generated whit my algorithms } } include<iostream.. ctime cstdio... int main(){ int X = rand()%10; int Y = MyNames::rand(10);
4th Jun 2019, 9:00 PM
AZTECCO
AZTECCO - avatar