• C++——类和对象(中)(1)


    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. void Init(int year, int month, int day)
    7. {
    8. _year = year;
    9. _month = month;
    10. _day = day;
    11. }
    12. void Print()
    13. {
    14. cout << _year << "-" << _month << "-" << _day << endl;
    15. }
    16. private:
    17. //只是声明
    18. int _year;
    19. int _month;
    20. int _day;
    21. };
    22. int main()
    23. {
    24. Date d1;
    25. Date d2;
    26. d1.Init(2023, 10, 28);
    27. d2.Init(2023, 10, 26);
    28. d1.Print();
    29. d2.Print();
    30. return 0;
    31. }

    对于Date类,可以通过 Init 公有方法给对象设置日期,但如果每次创建对象时都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?

    这时候就要请出构造函数了。 

    构造函数

    概念

    构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证
    每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。

    构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任务并不是开空间创建对象,而是初始化对象。

    构造函数特性

    其特征如下:
    1.函数名与类名相同
    2.无返回值。
    3.对象实例化时编译器自动调用对应的构造函数

    4.构造函数可以重载

    构造玩法

    无参构造和带参构造

    它们俩构造函数形成了重载。

    如果Date带参 对象后面跟括号。

    如果Date不带参 对象后面不用跟括号。

    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. //无参构造
    7. Date()
    8. {
    9. _year = 1;
    10. _month = 1;
    11. _day = 1;
    12. }
    13. //带参构造
    14. Date(int year, int month, int day)
    15. {
    16. _year = year;
    17. _month = month;
    18. _day = day;
    19. }
    20. void Print()
    21. {
    22. cout << _year << "-" << _month << "-" << _day << endl;
    23. }
    24. private:
    25. //只是声明
    26. int _year;
    27. int _month;
    28. int _day;
    29. };
    30. int main()
    31. {
    32. Date d1;
    33. d1.Print();
    34. Date d2(2023, 10, 28);
    35. d2.Print();
    36. return 0;
    37. }

      

     无参构造
    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. 无参构造
    7. Date()
    8. {
    9. _year=1;
    10. _month=1;
    11. _day=1;
    12. }
    13. void Print()
    14. {
    15. cout << _year << "-" << _month << "-" << _day << endl;
    16. }
    17. private:
    18. //只是声明
    19. int _year;
    20. int _month;
    21. int _day;
    22. };
    23. int main()
    24. {
    25. Date d1;
    26. d1.Print();
    27. return 0;
    28. }

    当是无参构造时定义对象时,后面不需要带括号。

    如果带了括号编译器会报错

    因为编译器会分不清Date d1是声明还是实例化.

    带参构造(缺省值)
    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. 带参构造 并且给了缺省值
    7. Date(int year=1, int month=1, int day=1)
    8. {
    9. _year = year;
    10. _month = month;
    11. _day = day;
    12. }
    13. void Print()
    14. {
    15. cout << _year << "-" << _month << "-" << _day << endl;
    16. }
    17. private:
    18. //只是声明
    19. int _year;
    20. int _month;
    21. int _day;
    22. };
    23. int main()
    24. {
    25. Date d1(2023, 10, 28);
    26. d1.Print();
    27. Date d2;
    28. d2.Print();
    29. return 0;
    30. }

    带参构造(缺省值)和无参构造存在问题

     带参构造(缺省值)和无参构造可以形成重载不能同时存在 因为调用会存在歧义

    原因:无参的构造函数全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。

    注意: 无参构造函数全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。

    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. //无参构造
    7. Date()
    8. {
    9. _year = 1;
    10. _month = 1;
    11. _day = 1;
    12. }
    13. //带参构造
    14. Date(int year=1, int month=1, int day=1)
    15. {
    16. _year = year;
    17. _month = month;
    18. _day = day;
    19. }
    20. void Print()
    21. {
    22. cout << _year << "-" << _month << "-" << _day << endl;
    23. }
    24. private:
    25. //只是声明
    26. int _year;
    27. int _month;
    28. int _day;
    29. };
    30. int main()
    31. {
    32. Date d1;
    33. d1.Print();
    34. Date d2(2023, 10, 28);
    35. d2.Print();
    36. return 0;
    37. }

    编译器默认生成构造

     如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,用户显式定义编译器将不再生成

    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. //无参构造
    7. //Date()
    8. //{
    9. // _year = 1;
    10. // _month = 1;
    11. // _day = 1;
    12. //}
    13. 带参构造
    14. //Date(int year=1, int month=1, int day=1)
    15. //{
    16. // _year = year;
    17. // _month = month;
    18. // _day = day;
    19. //}
    20. void Print()
    21. {
    22. cout << _year << "-" << _month << "-" << _day << endl;
    23. }
    24. private:
    25. //只是声明
    26. int _year;
    27. int _month;
    28. int _day;
    29. };
    30. int main()
    31. {
    32. Date d1;
    33. d1.Print();
    34. return 0;
    35. }

    随机值 

    栈的构造函数被我们屏蔽掉后,编译器会默认帮我们把Stack初始化吗?

    没屏蔽之前

    1. class Stack
    2. {
    3. public:
    4. Stack(size_t capacity = 3)
    5. {
    6. _a = (int*)malloc(sizeof(int) * capacity);
    7. if (_a == nullptr)
    8. {
    9. perror("malloc fail");
    10. return;
    11. }
    12. _capacity = capacity;
    13. _top = 0;
    14. }
    15. private:
    16. int* _a;
    17. int _top;
    18. int _capacity;
    19. };
    20. int main()
    21. {
    22. Stack st;
    23. return 0;
    24. }

    屏蔽掉之后

    1. class Stack
    2. {
    3. public:
    4. /*Stack(size_t capacity = 3)
    5. {
    6. _a = (int*)malloc(sizeof(int) * capacity);
    7. if (_a == nullptr)
    8. {
    9. perror("malloc fail");
    10. return;
    11. }
    12. _capacity = capacity;
    13. _top = 0;
    14. }*/
    15. private:
    16. int* _a;
    17. int _top;
    18. int _capacity;
    19. };
    20. int main()
    21. {
    22. Stack st;
    23. return 0;
    24. }

     可以看出,当栈把构造函数屏蔽掉以后,编译器并不会给栈初始化。

    那么队列呢?

    1. class Stack
    2. {
    3. public:
    4. Stack(size_t capacity = 3)
    5. {
    6. cout << "Stack(size_t capacity = 3)" << endl;
    7. _a = (int*)malloc(sizeof(int) * capacity);
    8. if (_a == nullptr)
    9. {
    10. perror("malloc fail");
    11. return;
    12. }
    13. _capacity = capacity;
    14. _top = 0;
    15. }
    16. private:
    17. int* _a;
    18. int _top;
    19. int _capacity;
    20. };
    21. class Myqueque
    22. {
    23. Stack _push;
    24. Stack _pop;
    25. };
    26. int main()
    27. {
    28. Stack st;
    29. Myqueque qq;
    30. return 0;
    31. }

    队列竟然被初始了?间接调用了Stack的构造函数完成了初始化。默认构造函数价值

     我们给队列加个sz 看看会不会被初始化?

     竟然完成了初始化?其实结果是错误的,这是编译器VS2019优化带来的结果。

    VS2013的调试结果 其实sz并没有被编译器初始化。

    关于编译器生成的默认成员函数,很多童鞋会有疑惑: 不实现构造函的情况下,编译器会生成默认的构造函数。但是看起来默认构造函数又没什么用? d对象调用了编译器生成的默认构造函数,但是d对象 _year/_month/_day,依旧是随机值。也就说在这里编译器生成的默认构造函数并没有什么用? ?
    解答: C++把类型分成内置类型(基本类型)和自定义类型

    内置类型就是语言提供的数据类型,如: int/char...

    自定义类型就是我们使用class/struct/union等自己定义的类型。

    默认生成的构造函数:对内置类型不做处理,自定义类型会去调用它的默认构造函数。

    C++11中打了个补丁,支持声明给缺省值;

    VS2019

    总结

    1.一般情况下,我们都要自己去写构造函数。

    2.成员都是自定义类型or声明时给了缺省值 可以考虑让编译器自己生成一个构造函数。

    3.自己写构造函数时推荐使用带参构造(缺省值)。

    析构函数

    概念

    通过前面构造函数的学习,我们知道一个对象是怎么来的,那一个对象又是怎么没呢的?

    析构函数:与构造函数功能相反析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。

    特性

    析构函数是特殊的成员函数,其特征如下
    1.析构函数名是在类名前加上字符 ~
    2. 无参数无返回值类型。
    3.一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意: 析构函数不能重载
    4.对象生命周期结束时,C++编译系统系统自动调用析构函数。

    析构玩法

    日期类

    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. Date(int year=1, int month=1, int day=1)
    7. {
    8. _year = year;
    9. _month = month;
    10. _day = day;
    11. }
    12. void Print()
    13. {
    14. cout << _year << "-" << _month << "-" << _day << endl;
    15. }
    16. ~Date()
    17. {
    18. cout << "~Date()" << endl;
    19. }
    20. private:
    21. //只是声明
    22. int _year;
    23. int _month;
    24. int _day;
    25. };
    26. int main()
    27. {
    28. Date d1(2023, 10, 28);
    29. d1.Print();
    30. return 0;
    31. }

    严格来说日期类其实并不需要析构函数 ,因为日期类变量出了作用域自己就会销毁。

    内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;

    这里是为了给大家演示下析构函数的自动调用。

    栈类

    栈需要内存、指针释放,因此需要析构函数的帮助。

    1. class Stack
    2. {
    3. public:
    4. Stack(size_t capacity = 3)
    5. {
    6. cout << "Stack(size_t capacity = 3)" << endl;
    7. _a = (int*)malloc(sizeof(int) * capacity);
    8. if (_a == nullptr)
    9. {
    10. perror("malloc fail");
    11. return;
    12. }
    13. _capacity = capacity;
    14. _top = 0;
    15. }
    16. ~Stack()
    17. {
    18. cout << "~Stack()" << endl;
    19. free(_a);
    20. _a = nullptr;
    21. _top = _capacity = 0;
    22. }
    23. private:
    24. int* _a;
    25. int _top;
    26. int _capacity;
    27. };

    析构函数我们不写也会生成默认析构,那我们屏蔽掉我们写的析构,编译器默认生成的会不会帮栈把内存空间释放掉呢?

    答案是不会的,在程序结束前的最后一刻,栈的成员变量还是构造函数初始化的值。

    那么默认的析构函数有什么价值呢?

    我们继续邀请队列进场

    1. class Stack
    2. {
    3. public:
    4. Stack(size_t capacity = 3)
    5. {
    6. cout << "Stack(size_t capacity = 3)" << endl;
    7. _a = (int*)malloc(sizeof(int) * capacity);
    8. if (_a == nullptr)
    9. {
    10. perror("malloc fail");
    11. return;
    12. }
    13. _capacity = capacity;
    14. _top = 0;
    15. }
    16. ~Stack()
    17. {
    18. cout << "~Stack()" << endl;
    19. free(_a);
    20. _a = nullptr;
    21. _top = _capacity = 0;
    22. }
    23. private:
    24. int* _a;
    25. int _top;
    26. int _capacity;
    27. };
    28. //
    29. class Myqueque
    30. {
    31. Stack _push;
    32. Stack _pop;
    33. int _sz=0;
    34. };
    35. int main()
    36. {
    37. Myqueque qq;
    38. return 0;
    39. }

    队列又赚麻了, 躺着赚钱,又是间接调用栈的析构完成自己的析构,这就是默认析构的价值。

     Ps:如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。

  • 相关阅读:
    全是模板的数据分析工具有哪些?
    规范你的Typescript注释,一步一步教你生成API文档
    夭寿啦!我的网站被攻击了了735200次还没崩
    kubebuilder的安装与基本使用
    9.21
    vue3中使用插件vite-plugin-svg-icons
    CF981G Magic multisets
    数据结构之队列
    elasticsearch 1.5 + mysql安装配置与简单使用
    Redis-数据结构-String
  • 原文地址:https://blog.csdn.net/weixin_74795859/article/details/134093102