namespace 变量作用域的作用空间,这样可以防止相同名称的变量被调用时带来的问题
#include "iostream"
#include <string>
using namespace std;
namespace coachnamespace
{
string coachname = "陈培昌";
class mycoach
{
private:
string name;
int age;
public:
void myfunc(string _name, int _age)
{
name = _name;
age = _age;
cout << "大家好,我是" << name << "今年" << age;
}
};
}
namespace studentnamespace
{
string name = "saintding";
namespace smspace
{
string fv1 = "散打";
string fv2 = "巴西柔术";
}
namespace normalspace
{
string f1 = "买书";
string f2 = "吃大餐";
string f3 = "花钱";
}
}
void main()
{
using namespace coachnamespace;
using namespace studentnamespace;
studentnamespace::name;
string _fv1= studentnamespace::smspace::fv1;
cout << studentnamespace::name << "喜欢玩荤的" << _fv1 << endl;
coachnamespace::mycoach mc;
mc.myfunc("程劲",20);
system("pause");
}
去掉.h的理由,早期引用头文件导致程序运行速度很慢