We can modify the program in (1) as follows:
Make the following changes to main.cpp:
# include<iostream>
# include"people.h"
using namespace xiaoguai; //使用命名空间xiaoguai
int main(int argc, const char * argv[])
{
people *p = new people();
p->sayhello(); //通过指针p访问到成员方法
delete p; //释放内存空间
system("pause");
return 0;
}
Make the following modifications to people.h and people.cpp respectively:
#pragma once
namespace xiaoguai{ //将类放到命名空间xiaoguai内
class people
{
public:
void sayhello();
};
}
# include<iostream>
#include "people.h"
using namespace std;
namespace xiaoguai{ // 放到命名空间小乖内
void people::sayhello()
{
cout << "Hello World\n";
}
}
After completion, save and run O(∩_∩)O, and then succeed to get the same result as in (1).