namespace 命名空间——多个的相互联系及自动合并;协作开发

#include <iostream>

using namespace std;

namespace Space
{
    int x;
}

namespace Space//可以看出与上方重名了
{
    int y;
}
int main()
{
    using namespace Space;//对于上方的两个重名的Space便会自动的合并
    x=10;
    y=20;
    cout<<x<<endl<<y<<endl;//直接输出10 20
    return 0;
}
</pre><pre code_snippet_id="1971569" snippet_file_name="blog_20161107_13_7729277" name="code" class="cpp">


多个cpp的制动联络


//Automan.h
#ifndef AUTOMAN_H
#define AUTOMAN_H

namespace Robot//这里是Robot类
{
    class Automan
    {
    public:
        Automan();
    };
}


#endif // AUTOMAN_H
</pre><pre code_snippet_id="1971569" snippet_file_name="blog_20161107_7_7760179" name="code" class="cpp">//cat.h
<pre name="code" class="cpp">#ifndef CAT_H
#define CAT_H

namespace Animal//这里是Animal类
{
    class Cat
    {
    public:
       Cat();
    };
}
#endif // CAT_H


 
//dog.h
<pre name="code" class="cpp">#ifndef DOG_H
#define DOG_H

namespace Animal//Animal类
{
    class Dog
    {
    public:
        Dog();
    };
}
#endif // DOG_H


 
//Robot.cpp
<pre style="margin-top: 0px; margin-bottom: 0px;"><pre name="code" class="cpp">#include "automan.h"
namespace Robot//Robot类
{
    Automan::Automan()
    {
    }
}
</pre><pre>
 
 
//cat.cpp
<pre name="code" class="cpp">#include "cat.h"

namespace Animal//Animal类
{
    Cat::Cat()
    {

    }
}


 
//dog.h
<pre name="code" class="cpp">#include "dog.h"

namespace Animal//Animal类
{
    Dog::Dog()
    {

    }
}


 
//main.cpp
<pre name="code" class="cpp">#include <iostream>
#include "cat.h"
#include "dog.h"
#include "automan.h"
using namespace std;
//引入命名空间
using namespace Animal;
using namespace Robot;

int main()
{
    Dog d;
    Automan xiaoli;//在这里便可直接使用已经自动联合的命名空间量
    return 0;
}


 协作开发,多个人运用一个命名空间 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值