您的位置:首页 > 其它

使用命名空间解决名字冲突

2017-03-15 22:37 441 查看
//main()函数

**#include <iostream>
#include <fstream>
#include <cstring>
#include <strstream>
#include "header1.h.h"
#include "hearder2.h.h"
using namespace std;
int main()
{
ns1::Student stud1(101,"Wang",18);
stud1.get_data();
cout<<ns1::fun(5,3)<<endl;
ns2::Student stud2(102,"Li",'f');
stud2.get_data();
cout<<ns2::fun(5,3)<<endl;
}

**


//header1.h文件

**//
// Created by andrew on 15/03/17.
//

#ifndef UNTITLED9_HEADER1_H_H
#define UNTITLED9_HEADER1_H_H
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
namespace ns1
{
class Student
{
public:
Student(int n,string nam,int a)
{num=n;
name=nam;
age=a;
}
void get_data();
private:
int num;
string name;
int age;

};
void Student::get_data()
{
cout<<num<<" "<<name<<" "<<age<<endl;
}
double fun(double a,double b)
{return sqrt(a+b);}

}

#endif //UNTITLED9_HEADER1_H_H
**


//hearder2.h头文件

**//
// Created by andrew on 15/03/17.
//

#ifndef UNTITLED9_HEARDER2_H_H
#define UNTITLED9_HEARDER2_H_H
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
namespace ns2 {
class Student {
public:
Student(int n, string nam, char s) {
num = n;
name = nam;
sex = s;
}

void get_data();

private:
int num;
string name;
char sex;

};

void Student::get_data() {

cout << num << " " << name << " " << sex << endl;
}

double fun(double a, double b)
{
return sqrt(a-b);
}
}

#endif //UNTITLED9_HEARDER2_H_H
**
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐