luqingfei@C++

为中华之崛起而崛起!
兼听则明,偏听则暗。

关于使用命名空间及声明外部变量的练习

File1
#include <string>

namespace data {
    
// The next two variables have external linkage
    int count;                // will be initialized to 9 by default
    float phi = 1.618f;        // the divine proportion or golden ratio

    
// without the extern, the following would not be accessible
    
// from another file because they would have internal linkage
    extern const double pi = 3.14159265;
    
extern const std::string days[] = { "Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday" };
}


File2
#include <iostream>
#include 
<string>
#include 
<iomanip>

namespace data {
    
//Declare external variables
    extern float phi;
    
extern const double pi;
    
extern const std::string days[];
    
extern int count;
}

void main() {
    std::cout 
<< std::setprecision(3<< std::fixed;
    std::cout 
<< std::endl
        
<< "To 3 decimal places" << std::endl;

    std::cout 
<< " a circle with a diameter of phi has an area of"
        
<< data::pi * data::phi * data::phi /4
         
<< std::endl;

    std::cout 
<< "phi squared is " << data::phi * data::phi << std::endl;
    std::cout 
<< " in fact, phi+1 is also " << data::phi+1 << std::endl;

    std::cout 
<< "value of count is " << data::count << std::endl;

    data::count 
+= 3;
    std::cout 
<< "today is " << data::days[data::count] << std::endl;
}

posted on 2009-02-16 16:38 luqingfei 阅读(501) 评论(0)  编辑 收藏 引用 所属分类: C++基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

统计

留言簿(6)

随笔分类(109)

随笔档案(105)

Blogers

Game

Life

NodeJs

Python

Useful Webs

大牛

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜