C++,命名空间与string类

一、命名空间基本概念

1.1 命名空间定义

namespace 命名空间名
{
    变量名;
    函数;
    结构体;
    枚举名;
}

1.2 全局引入命名空间

using namespace 命名空间名;

1.3 部分引入命名空间

using namespace 命名空间名::变量名

注意:::表示域限定符,在哪个位置使用,在哪写域限定符

1.4 命名空间冲突问题 

 1.4.1 局部变量和命名空间冲突

        当局部变量和命名空间冲突时,即使引入命名空间,但在使用时因为就近原则,所以默认使用局部变量,如果想要使用命名空间中的变量,则需要使用域限定符。

#include <iostream>
using namespace std;

namespace zpp {
    int age;
    char name[20] = "zhangsan";
    int cout;
}
using namespace zpp;

int main()
{
    //定义局变量
    char name[20] = "hello";
    std::cout<<"name = "<<name<<endl;      //hello
    std::cout<<"name = "<<zpp::name<<endl; //zhangsan

    return 0;
}
1.4.2 多个命名空间中命名冲突问题

         当遇到多个命名空间冲突问题,通常有两种解决方法:1)只引入某一个命名空间,引入需要使用的命名空间的标识符。2)在使用变量时,使用域限定符。

#include <iostream>
using namespace std;

namespace zpp {
    int age;
    char name[20] = "zhangsan";
    int cout;
}
using namespace zpp;
int main()
{
    std::cout << "Hello World!" << endl;

    //当多个命名空间名产生冲突时,可以使用命名空间名和作用域限定符解决
    std::cout<<zpp::cout<<endl;        //0

    return 0;
}
1.4.3 全局变量和命名空间冲入问题

        当全局变量和命名空间冲突, 通常有两种解决方法:1)只引入某一个命名空间,引入需要使用的命名空间的标识符。2)在使用变量时,使用域限定符,全局变量默认和匿名空间存在一起。

#include <iostream>
using namespace std;

namespace zpp {
    int age;
    char name[20] = "zhangsan";
    int cout;
}
using namespace zpp;


//定义全局变量
int age = 30;

int main()
{
    std::cout << "Hello World!" << endl;

    //当命名空间中的名字和全局变量冲突时,也要用命名空间名和作用域限定符区分
    zpp::age = 18;
    //使用全局变量的方式,系统会将全局变量放到一个命名空间中,该命名空间为匿名空间
    //匿名空间中的名字使用方式: ::名字
    std::cout<<"zpp::age = "<<zpp::age << "   全局age = "<<::age<<endl;

    return 0;
}

1.5 命名空间添加

        如果定义多个相同的命名空间名,最后会合并成一个命名空间。

namespace A 
{
    int num;
    int a;
}
namespace A
{
    int b;
}

1.6 命名空间的嵌套 

        如果在一个命名空间A内嵌套另一个命名空间B,在引入命名空间B时,需要逐级引入。

#include <iostream>
using namespace std;

namespace A
{
    int a = 0;
    namespace B
    {
        int a = 100;
        char c = 'a';
    }
}

using namespace A;
using namespace A::B;
int main()
{
    cout << A::a << endl; //a = 0;
    cout << A::B::a << endl; //a = 100;    
    return 0;
}

1.7 给命名空间重命名

namespace A
{
    int a = 0;
}
namespace NEW = A;

1.8 总结

  1. 定义命名空间,关键字为namespace,声明命名空间使用关键字using
  2. 命名冲突问题,多个命名空间冲突、命名空间与全局变量冲突、命名空间与局部变量冲突,解决方法:加上命名空间名和作用域限定符
  3. 命名空间可以嵌套定义,需要使用作用域限定符一级一级找到最低一级进行使用
  4. 一个程序可以定义多个同名的命名空间,但是要求命名空间中的名字不能相同

二、练习

        定义一个学生的结构体,包含学生的姓名,年龄,成绩,性别,学生的成绩,姓名,定义为私有权限;定义一个学生类型的结构体变量,设置公有函数用于给学生的成绩和名字进行赋值,(结构体中的函数:结构体中声明,结构体外定义)

#include <iostream>

using namespace std;
struct stu
{
    int age;
    float score;
    string sex;
    void func();
    void set_name(string &str,float &s)
    {
        name = str;
        score = s;
    }
private:
    string name;
};

void stu::func()
{
    cout << name << endl;
    cout << age << endl;
    cout << sex << endl;
    cout << score << endl;
}
int main()
{
    stu students;
    string str = "zhangsan";
    students.age = 18;
    students.sex = "男";
    float s = 95;
    students.set_name(str,s);
    students.func();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_53478812/article/details/132433695