Day3 (作用域限定符,this关键字,static关键字,const关键字)

5. 作用域限定符 ::

5.1 名字空间掌握)

名字空间是一种代码的层级划分。

#include <iostream>

using namespace std;
// C++课程中几乎所有的类型(不包括基本数据类型)都在std中

int a = 1;

// 新建一个名字空间
namespace my_space {
    int a = 3;
    string s = "哈哈哈";
}

namespace my_space2 {
    int a = 4;
    string s = "嘿嘿嘿";
}

// 使用自己的名字空间
using namespace my_space2;


int main()
{
    int a = 2;
    std::cout << "你好" << std::endl;
    cout << a << endl; // 2
    cout << ::a << endl; // 1

    // 访问my_space的内容
    cout << my_space::a << my_space::s << endl;
    // 访问my_space2的内容
    cout << my_space2::a << s << endl;

    return 0;
}

5.2 类内声明类外定义重点)

函数可以声明定义分离成员函数也可以声明定义分离成员函数声明放置类内成员函数定义放置类外因为定义类外所以需要类名::制定定义函数哪个

#include <iostream>

using namespace std;

class Student
{
private:
    string name;

public:
    // 类内声明
    Student(string n);
    string get_name();
    void set_name(string n);
};

// 类外定义
Student::Student(string n)
{
    name = n;
}

string Student::get_name()
{
    return name;
}

void Student::set_name(string n)
{
    name = n;
}

int main()
{
    Student s("张三");
    s.set_name("张三丰");
    cout << s.get_name() << endl;

    return 0;
}

5.3 配合静态使用

后续static关键字讲解

6. this关键字

6.1 概念掌握)

this类中表示一个指针this指针一种特殊指针保存当前类对象首地址指向了当前类的对象)

#include <iostream>

using namespace std;

class Student
{
private:
    string name;

public:
    Student(string n):name(n)
    {
        cout << this << endl;
    }

    string get_name()
    {
        cout << this << endl;
        return name;
    }

    void set_name(string n)
    {
        cout << this << endl;
        name = n;
    }
};



int main()
{
    Student s("张三");
    cout << &s << endl;
    s.set_name("张三丰");
    cout << s.get_name() << endl;

    cout << endl;

    Student* s2 = new Student("李四");
    cout << s2 << endl;
    cout << s2->get_name() << endl;

    return 0;
}

6.2 调用成员掌握)

结论成员必须对象调用

#include <iostream>

using namespace std;

class Student
{
private:
    string name;

public:
    Student(string n)
    {
        this->name = n;
        cout << this->get_name() << endl;
    }

    string get_name()
    {
        return this->name;
    }

    void set_name(string n)
    {
        this->name = n;
    }
};

上面的代码所有在类调用成员位置如果程序员不写this指针(实际上没有必要手写,比较繁琐),编译器都会成员调用之前添加this指针

6.3 区分重名成员变量与局部变量掌握)

#include <iostream>

using namespace std;

class Student
{
private:
    string name;

public:
    Student(string name)
    {
        this->name = name;
    }

    string get_name()
    {
        return name;
    }

    void set_name(string name)
    {
        this->name = name;
    }
};



int main()
{
    Student s("Tom");
    cout << s.get_name() << endl;
    s.set_name("Jerry");
    cout << s.get_name() << endl;

    return 0;
}

6.4 链式调用熟悉)

结论:如果一个成员函数返回值当前类型引用说明这个函数支持链式调用return内容一定*this

#include <iostream>

using namespace std;

class Value
{
private:
    int value;

public:
    Value(int value)
    {
        this->value = value;
    }

    Value& add(int value)
    {
        this->value += value;
        return *this;
    }

    int get_value()
    {
        return value;
    }
};



int main()
{
    Value v1(0);
    // 普通的调用方式:分步操作
    v1.add(1);
    v1.add(2);
    v1.add(4);
    v1.add(7);
    cout << v1.get_value() << endl; // 14

    cout << endl;

    Value v2(0);
    // 链式调用
    cout << v2.add(1).add(2).add(4).add(7).get_value() << endl; // 14
    cout << v2.get_value() << endl; // 14

    return 0;
}

7. static关键字

7.1 静态局部变量掌握)

使用static修饰的局部变量就是静态局部变量所在函数第一次调用时创建直到程序运行结束销毁所有对象共用这个一个静态局部变量

#include <iostream>

using namespace std;

class Test
{
public:
    void test_static()
    {
        int a = 1;
        static int b = 1; // 局部静态变量
        cout << a++ << endl;
        cout << b++ << endl << endl;
    }
};

int main()
{
    Test t1;
    Test t2;
    t1.test_static();
    t2.test_static();
    t1.test_static();

    return 0;
}

7.2 静态成员变量掌握)

使用static关键字修饰成员变量就是静态成员变量

静态成员变量具有以下特点

1. const修饰的静态成员变量不能类内初始化必须类外初始化

2. 同一个所有对象共享一份静态成员变量

3. 静态成员变量可以直接通过类名调用无需关联任何对象因为静态变量程序执行创建程序结束销毁

#include <iostream>

using namespace std;

class Test
{
public:
    int a = 1; // 成员变量
    static int b; // 静态成员变量
};

int Test::b = 1; // 类外初始化

int main()
{
    // 通过类名直接调用静态成员变量
    cout << Test::b << " " << &Test::b << endl;

    Test t1;
    Test t2;
    cout << t1.a++ << " " << &t1.a << endl;
    cout << t2.a++ << " " << &t2.a << endl;
    cout << t1.b++ << " " << &t1.b << endl;
    cout << t2.b++ << " " << &t2.b << endl;

    return 0;
}

7.3 静态成员函数掌握)

使用static修饰成员函数就是静态成员函数静态成员函数具有以下特点

1. 没有this指针因此不能调用非静态成员

2. 如果静态成员函数声明定义分离static需要写在声明即可

3. 除了可以使用对象调用还可以直接通过类名::调用更加推荐后者

#include <iostream>

using namespace std;

class Test
{
public:
    int a = 1;
    static int b;

    static void func(); // 静态成员函数
};

int Test::b = 1;

void Test::func() // 类外定义
{
//        cout << a << endl; 错误
    cout << b << endl;
}

int main()
{
    Test t;
    t.func();

    Test::func();

    return 0;
}

7.4 单例模式了解)

模式设计(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。

单例 = 实例

实例 instance = 对象 object

单例模式可以全局创建保持一个

基于指针单例模式非完全版)

#include <iostream>

using namespace std;

/**
 * @brief The Singleton class 单例模式的类
 */
class Singleton
{
private: // 构造函数私有化防止外部调用
    Singleton(){}
    Singleton(const Singleton&){}

    static Singleton* instance; // 类内的对象,即单例

public:
    static Singleton* get_instance()
    {
        if(instance == NULL)
            instance = new Singleton;
        return instance;
    }
};

Singleton* Singleton::instance = NULL;


int main()
{
    Singleton* s1 = Singleton::get_instance();
    cout << s1 << endl;
    Singleton* s2 = Singleton::get_instance();
    cout << s2 << endl;

    return 0;
}

基于引用单例模式

#include <iostream>

using namespace std;

/**
 * @brief The Singleton class 单例模式的类
 */
class Singleton
{
private: // 构造函数私有化防止外部调用
    Singleton(){}
    Singleton(const Singleton&){}

public:
    static Singleton& get_instance()
    {
        static Singleton instance;
        return instance;
    }
};

int main()
{
    Singleton& s1 = Singleton::get_instance();
    Singleton& s2 = Singleton::get_instance();
    cout << &s1 << " " << &s2 << endl;

    return 0;
}

8. const关键字

8.1 修饰成员函数(掌握)

const修饰成员函数表示成员函数可以调用const成员变量但是不能修改数值不能调用const的成员函数

如果常成员函数声明定义分离const声明定义使用

#include <iostream>

using namespace std;

class Test
{
private:
    string name;
public:
    void set_name(string name)
    {
        this->name = name;
    }

    string get_name() const; // 常成员函数

    void test_const() const // 常成员函数
    {
//        set_name("哈哈哈"); 错误
        cout << get_name() << endl;
        cout << name << endl;
//        name = "你好"; 错误
    }
};

string Test::get_name() const // 类外定义
{
    return name;
}

int main()
{
    Test t;
    t.set_name("再见");
    t.test_const();

    return 0;
}

建议只要成员函数不修改属性值使用const修饰提升代码安全性例如getter

8.2 修饰对象掌握)

const修饰对象表示该对象常量对象这样对象成员变量数值不可变不能调用任何const修饰成员函数

#include <iostream>

using namespace std;

class Test
{
public:
//    string name = "张三"; // 直接初始化可以

    string name;

//    Test(string name)
//    {
//        this->name = name; // 构造函数函数体初始化可以
//    }

    Test(string name):name(name){}

    void set_name(string name)
    {
        this->name = name;
    }

    string get_name() const
    {
        return name;
    }
};



int main()
{
    // const可以放在类型前后
    Test const t1("张三");
//    t1.set_name("你好"); 错误
    const Test t2("李四");
//    t2.name = "你好"; 错误
    cout << t1.get_name() << endl;
    cout << t2.get_name() << endl;

    return 0;
}

8.3 修饰成员变量掌握)

const修饰成员变量表示成员变量这样成员变量不允许修改

成员变量初始化方式两种

  • 直接初始化
  • 构造初始化列表优先级更高)

#include <iostream>

using namespace std;

class Test
{
public:

    const string name = "张三"; // 直接初始化

    Test(string name):name(name) // 构造初始化列表
    {}

//    Test(string name)
//    {
//        this->name = name; 错误
//    }

    void set_name(string name)
    {
//        this->name = name; 错误
    }

    string get_name() const
    {
        return name;
    }
};


int main()
{
    Test t("王五");
    cout << t.get_name() << endl;

    return 0;
}

8.4 修饰局部变量

表示局部变量不可被修改常见于const修饰引用参数

代码

8.5 constexpr 常量表达式熟悉)

constexprconst严谨关键字表示编译期确定常量

#include <iostream>

using namespace std;

class Test
{
public:
    constexpr static int a = 1; // a=1在编译期确定
//    constexpr int b = 2; 错误
    const int c = 3;
};

上面例子a不需要配合任何对象使用对象程序运行期间创建因此a可以constexpr修饰表示编译确定反之b需要对象创建之后才能创建因此不能constexpr修饰

constexpr修饰的内容表示编译期间可以确定,C++部分代码需要编译期间确定

#include <iostream>
#include <array> // 后面要学习的一个头文件

using namespace std;

// 表示是否可以在编译期间计算出返回值
constexpr int calc_len(int i)
{
    return i+5; // 随便写的计算规则
}

int main()
{
    // 5表示创建的arr对象的长度,必须在编译期间确定
    array<int,5> arr;

    // 编译期间可以计算出结果为6,正确
    array<int,calc_len(1)> arr2;

    int i = 1;
    // 编译期间无法计算出最后结果,报错
//    array<int,calc_len(i)> arr3; 错误

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值