• C++的构造函数


      1.析构函数(析构器)

    ------------------------------------------------------------------------------------------------------------

       1.作用
             当对象的地址空间被释放的时候,析构函数会被自动调用
             实际开发中的作用:析构函数往往用来做收尾工作,QT里面经常这样子做
                  例子1:
                         class YY
                         {
                          public:
                                 YY()
                                 {
                                     打开硬件设备/文件
                                 }
                                 ~YY()
                                 {
                                     关闭硬件设备/文件
                                 }

                         }
                  例子2:
                         class YY
                         {
                          public:
                                 YY()
                                 {
                                     p=new char[20]; //分配堆空间
                                 }
                                 ~YY()
                                 {
                                     delete []p;  //释放堆空间
                                 }
                          private:
                                 char *p;
                         }

       2.语法规则
             ~类名()
             {
                析构函数的源码;
             }

       3.析构函数的特点
             第一个:析构函数没有返回值类型,没有形参
             第二个:析构函数的名字必须跟类名一模一样
             第三个:如果程序员没有写析构函数,编译器会自动帮你生成默认的析构函数,该析构函数源码中什么事情都没有做
                            ~Cat()
                            {
                               
                            }
             如果程序员写了析构函数,编译器就不会帮你生成默认的析构函数
             第四个:析构函数不可以重载  


    =========================================================================  析构函数的用法:

    1. #include <iostream>
    2. using namespace std;
    3. class Cat
    4. {
    5. public:
    6. //定义一个构造函数
    7. Cat(int _age,float _weight);
    8. void show();
    9. private:
    10. int age;
    11. float weight;
    12. };
    13. void Cat::show()
    14. {
    15. cout<<"猫的年龄是: "<<age<<endl;
    16. cout<<"猫的体重是: "<<weight<<endl;
    17. }
    18. Cat::Cat(int _age,float _weight)
    19. {
    20. cout<<"猫的构造函数调用了"<<endl;
    21. age=_age;
    22. weight=_weight;
    23. }
    24. int main()
    25. {
    26. //创建猫的对象
    27. //Cat c1; //对应的无参构造函数Cat()
    28. Cat c2(5,20.5); //对应的带参数的构造函数Cat(int,float)
    29. c2.show();
    30. }

    =========================================================================

    2. 拷贝构造函数

    ----------------------------------------------------------------------------------------------------------------

    1.作用
            当你用一个已经存在的对象去初始化赋值给另外一个新的对象的时候,就会自动调用拷贝构造函数  
            例如:
                  int a=99;
                  int b=a;   //定义了新的变量b,并用a去初始化赋值给b
            同样的道理:
                  Cat c1(5);
                  Cat c2=c1;  //定义了新的对象c2,并用c1去初始化赋值给c2

       2.语法规则
            类名(类对象引用)
            {
                  源码
            }
            Cat(Cat &other)
            {
            }

    =================================================================

    拷贝构造函数的例子

    1. #include <iostream>
    2. using namespace std;
    3. class Cat
    4. {
    5. public:
    6. Cat(int _age)
    7. {
    8. age=_age;
    9. cout<<"猫的带参构造函数"<<endl;
    10. }
    11. Cat()
    12. {
    13. cout<<"猫的无参构造函数"<<endl;
    14. }
    15. Cat(Cat &othercat)
    16. {
    17. cout<<"othercat地址是: "<<&othercat<<endl;
    18. cout<<"othercat里面的age值是: "<<othercat.age<<endl;
    19. cout<<"this is"<<this<<endl;
    20. cout<<"猫的拷贝构造函数"<<endl;
    21. }
    22. ~Cat()
    23. {
    24. cout<<"猫的析构函数"<<endl;
    25. }
    26. private:
    27. int age;
    28. };
    29. /*
    30. &在不同的场合表示不同意思
    31. 类型名 &引用名;
    32. &变量名
    33. */
    34. int main()
    35. {
    36. //创造猫的对象
    37. Cat c1(5);
    38. cout<<"c1的地址是: "<<&c1<<endl;
    39. //写法一:调用拷贝构造函数
    40. Cat c2=c1; //Cat(Cat &othercat)
    41. //c2.Cat(c1); //辅助你理解,但是这种写法本身是不正确的
    42. //Cat &othercat=c1;
    43. //写法二:调用带参构造
    44. //Cat c2(7);
    45. //写法三:调用无参构造
    46. //Cat c2;
    47. //c2=c1;
    48. }

     ===================================================================

     3.拷贝构造函数的特点
            第一:名字跟类名相同,没有返回值类型,形参有且仅有一个,要求是引用
            第二:程序员没有写拷贝构造函数,编译器会自动生成一个默认的拷贝构造函数,但是这个默认的拷贝构造函数是有bug的
                  bug: 默认的拷贝构造函数,它遇到指针分配堆空间这种情况,它会共用同一个堆空间(不会重新分配新的堆空间)
                  产生bug的情况总结成公式:
                        class 类名
                        {

                         private:
                            成员变量1;
                            成员变量2;
                            。。。。。
                            类型 *指针名; //有指针,就要申请堆空间
                        }
                        类名 对象1;
                        类名 对象2=对象1; //调用默认的拷贝构造函数,bug就产生了,对象1和对象2共用同一个堆空间

        4.深拷贝和浅拷贝
             浅拷贝:默认的拷贝构造函数实现的就是浅拷贝
             深拷贝:程序员知道浅拷贝的bug以后,自己动手写拷贝构造函数,解决了浅拷贝的bug,我们就把自己写的拷贝构造函数称为深拷贝

    1. #include <iostream>
    2. #include "myhead.h"
    3. using namespace std;
    4. class Cat
    5. {
    6. public:
    7. Cat(const char *_name,int _age)
    8. {
    9. //分配堆空间
    10. name=new char[20];//分配堆空间,并让成员chare *name指针指向该空间
    11. strcpy(name,_name);
    12. age=_age;
    13. cout<<"猫的带参构造函数,此时申请的name指向的堆空间首地址是: "<<(int *)name<<endl;
    14. cout<<"此时_name指向的对空间首地址是: "<<(int *)_name<<endl;
    15. }
    16. ~Cat()
    17. {
    18. delete []name;
    19. cout<<"猫的析构函数"<<endl;
    20. }
    21. //自定义拷贝构造函数--》实现深拷贝
    22. Cat(Cat &other)
    23. {
    24. //给当前对象的指针name单独申请堆空间
    25. this->name=new char[20];
    26. strcpy(this->name,other.name);
    27. this->age=other.age;
    28. cout<<"我自己写的是深拷贝"<<endl;
    29. }
    30. //修改猫的属性信息的函数
    31. void setattr(const char *newname,int newage)
    32. {
    33. strcpy(name,newname);
    34. age=newage;
    35. }
    36. void show()
    37. {
    38. cout<<"当前对象地址是: "<<this<<" 名字是:"<<name<<" 年龄是:"<<age<<endl;
    39. cout<<"name地址是:"<<(int *)name<<" age的地址是:"<<&age<<endl;
    40. }
    41. private:
    42. int age;
    43. char *name;
    44. };
    45. int main()
    46. {
    47. //创造猫的对象
    48. Cat c1("小黄",5);
    49. Cat c2=c1; //一定会调用拷贝构造函数,我自己没有写拷贝构造,调用编译器自动生成的
    50. cout<<"c1地址是: "<<&c1<<endl;
    51. cout<<"c2地址是: "<<&c2<<endl;
    52. c1.show();
    53. c2.show();
    54. //我想修改c1的信息
    55. c1.setattr("旺财",6);
    56. cout<<"===========修改之后============="<<endl;
    57. c1.show();
    58. c2.show();
    59. }

    ===============================================================

    练习

    1.  不可以使用任何C语言的库函数,只能使用string中的方法,实现
                   只要B字符串中出现的字符(不论大小写),把它从A字符串中剔除
                            比如: A字符串是  "fhdshffFHDSHF"   
                                   B字符串是  "hfdhfd"    

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int i,j;
    6. string A;
    7. string B;
    8. cout<<"请输入两个字符串!"<<endl;
    9. cin>>A;
    10. cin>>B;
    11. cout<<"A is: "<<A<<endl;
    12. cout<<"B is: "<<B<<endl;
    13. //伪代码分析理清思路--》代码中使用你喜欢的符号,汉字去分析思路
    14. for(i=0; i<B.length(); i++)
    15. {
    16. for(j=0; j<A.length(); j++)
    17. {
    18. if(B[i]==A[j] || B[i]-A[j]==32 || B[i]-A[j]==-32)
    19. {
    20. //删除A里面的这个字符
    21. A.erase(j,1);
    22. j--; //很重要,防止漏掉一些字符,跟j++相互抵消
    23. }
    24. }
    25. }
    26. //打印删除之后的结果
    27. cout<<"=============删除之后=================="<<endl;
    28. cout<<"A is: "<<A<<endl;
    29. cout<<"B is: "<<B<<endl;
    30. }

    2.  定义按钮类 class Button
             属性:宽
                   高
                   按钮背景颜色
             要求定义不同版本的构造函数,分别初始化不同的对象

    1. #include <iostream>
    2. using namespace std;
    3. class Button
    4. {
    5. public:
    6. Button()
    7. {
    8. w=0;
    9. h=0;
    10. color=0;
    11. cout<<"无参构造函数"<<endl;
    12. }
    13. Button(int _w,int _h,int _color)
    14. {
    15. w=_w;
    16. h=_w;
    17. color=_color;
    18. cout<<"带参构造"<<endl;
    19. }
    20. /*
    21. Button(int w,int h,int color) //可读性太差
    22. {
    23. w=w;
    24. h=h;
    25. color=color;
    26. } */
    27. private:
    28. int w;
    29. int h;
    30. int color;
    31. };
    32. int main()
    33. {
    34. Button b;
    35. Button b1(100,50,0xffffff);
    36. }

  • 相关阅读:
    30C++编程提高篇-----1、函数模板原理
    Jsp-九大内置对象
    IntelliJ IDEA 2022.3正式发布,配置云同步&支持Redis好用到炸
    公共字段自动填充-@TableField的fill实现(2)
    PyTorch入门学习(十五):现有网络模型的使用及修改
    ant target的depends属性
    kafka集群配置
    PASCAL VOC2012 数据集讲解与制作自己的数据集
    LLM(大语言模型)「Agent」开发教程-LangChain(三)
    视频压缩技术—H264
  • 原文地址:https://blog.csdn.net/weixin_44651073/article/details/132702476