Use Xcode to write C + + programs [6] Name visibility
This summary includes some of the usage details of the namespace
Name space
#include <iostream>using namespacestd;namespaceFoo {//function intvalue () {return 5; }}namespaceBar {//Constants Const DoublePI =3.1416; //function Doublevalue () {return 2*Pi; }}intMain () {cout<< Foo::value () <<'\ n'; cout<< Bar::value () <<'\ n'; cout<< bar::p i <<'\ n'; return 0;}
Print results
5 6.2832 3.1416 0
Using namespaces
#include <iostream>using namespacestd;namespaceFirst {intx =5; inty =Ten;}namespaceSecond {Doublex =3.1416; Doubley =2.7183;}intMain () {//declaring an element in a namespace usingfirst::x; usingsecond::y; cout<< x <<'\ n'; cout<< y <<'\ n'; //directly using an element in a namespacecout << first::y <<'\ n'; cout<< second::x <<'\ n'; return 0;}
Print results
5 2.7183 Ten 3.1416 0
#include <iostream>using namespacestd;namespaceFirst {intx =5; inty =Ten;}namespaceSecond {Doublex =3.1416; Doubley =2.7183;}intMain () {//declaring an element in the namespace first using namespaceFirst ; cout<< x <<'\ n'; cout<< y <<'\ n'; //using elements in the namespace secondcout << second::x <<'\ n'; cout<< second::y <<'\ n'; return 0;}
Print results
5 2.7183 Ten 3.1416 0
#include <iostream>using namespacestd;namespaceFirst {intx =5;}namespaceSecond {Doublex =3.1416;}intMain () {//using the namespace first { using namespaceFirst ; cout<< x <<'\ n'; } //using namespaces second { using namespacesecond; cout<< x <<'\ n'; } return 0;}
Print results
5 3.1416 0
[C + +] using Xcode to write C + + programs [6] Name visibility