#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;
}
协作开发,多个人运用一个命名空间