[C + +] using Xcode to write C + + programs [6] Name visibility

Source: Internet
Author: User
Tags visibility

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.