• C++:类的封装


    ●参数初始化列表

    1、带参构造

    类名(int参数1,int参数2....)

    {

    属性1 =参数1;

    属性2 =参数2;

    }

    2、初始化列表

    类名(<类型1>参数1, < 类型2>参数2...): 属性1(参数1),属性2(参数2)

    {

    代码:

    1. #include
    2. using namespace std;
    3. class Car
    4. {
    5. public:
    6. #if 1
    7. Car()
    8. {
    9. cout << "Car构造函数" << endl;
    10. }
    11. #endif
    12. Car(string brand, string color):c_brand(brand),c_color(color)
    13. {
    14. cout << "have arg construct" << endl;
    15. //c_brand = brand;
    16. //c_color = color;
    17. }
    18. Car(int num):c_num(num)
    19. {
    20. }
    21. ~Car()
    22. {
    23. cout << "Car析构函数" << endl;
    24. }
    25. string c_brand;
    26. string c_color;
    27. int c_num;
    28. };
    29. class Person
    30. {
    31. public:
    32. Person()
    33. {
    34. m_name = "zs";
    35. m_age = 23;
    36. cout << "Person构造函数" << endl;
    37. }
    38. Person(Car c1)
    39. {
    40. m_car = c1;
    41. cout << "Person构造函数" << endl;
    42. }
    43. ~Person()
    44. {
    45. cout << "Person析构" << endl;
    46. }
    47. void PersonInfo()
    48. {
    49. cout << "name:" << m_name << endl;
    50. cout << "age:" << m_age << endl;
    51. cout << "car:" << m_car.c_brand << " #color:" << m_car.c_color << endl;
    52. }
    53. string m_name;
    54. int m_age;
    55. Car m_car;
    56. };
    57. class Dog
    58. {
    59. public:
    60. ~Dog()
    61. {
    62. cout << "dog析构" << endl;
    63. }
    64. };
    65. #if 0
    66. void test01()
    67. {
    68. Dog d1;
    69. Person p1;
    70. p1.m_car.c_brand = "bmw";
    71. p1.m_car.c_color = "black";
    72. p1.PersonInfo();
    73. }
    74. #endif
    75. void test02()
    76. {
    77. Car c1("bmw", "pink");
    78. Person p1(c1);
    79. p1.PersonInfo();
    80. }
    81. void test03()
    82. {
    83. Person p2;
    84. p2.PersonInfo();
    85. }
    86. int main()
    87. {
    88. test03();
    89. return 0;
    90. }

     

    ●组合思想

    - ==一个类包含一个或多个其他类的对象.==

    构造与析构顺序:先构造类中的对象,再构造自身,析构顺序与构造相反.

    1. #include
    2. using namespace std;
    3. class Car
    4. {
    5. public:
    6. Car() //必须有无参数
    7. {
    8. cout<<"no agr construct"<
    9. }
    10. Car(string brand,string color):c_brand(brand),c_color(color)
    11. {
    12. cout<<"have agr construct"<
    13. }
    14. string c_brand;
    15. string c_color;
    16. };
    17. class Person
    18. {
    19. public:
    20. void PersonInfo()
    21. {
    22. cout<<"name:"<
    23. cout<<"age:"<
    24. cout<<"car:"<"#color:"<
    25. }
    26. string m_name;
    27. int m_age;
    28. Car m_car;
    29. };
    30. void test01()
    31. {
    32. Person p1;
    33. p1.m_car.c_brand="bmw";
    34. p1.m_car.c_color="black";
    35. p1.PersonInfo();
    36. }
    37. int main(int argc, char *argv[])
    38. {
    39. test01();
    40. return 0;
    41. }

    ●对象模型与this指针

    (1)对象的成员属性与成员方法其实是分开存储的,类中的所有成员方法都只有一份实例

    (2)只有非静态成员属性才会占用对象空间,使用sizeof运算符来验证

    *对象模型

    1. #include
    2. using namespace std;
    3. class Animal
    4. {
    5. public:
    6. int a_age;
    7. int a_num;
    8. void AnimalInfo()
    9. {
    10. cout<<"age:"<"num:"<
    11. }
    12. };
    13. void test01()
    14. {
    15. Animal a1;
    16. cout<<sizeof(a1)<
    17. }
    18. int main(int argc, char *argv[])
    19. {
    20. test01();
    21. return 0;
    22. }

    (3)this指针是每一个非静态成员函数都拥有的参数,只是该参数被编译器隐藏,但是可以在函数内使用

    *对象模型与this指针

    1. #include
    2. using namespace std;
    3. class Animal
    4. {
    5. public:
    6. Animal(){}
    7. Animal(string a_name)
    8. {
    9. this->a_name = a_name;
    10. }
    11. int a_age;
    12. int a_num;
    13. string a_name;
    14. void AnimalInfo()
    15. {
    16. cout << "name:" << this->a_name << " num:" << this->a_num << endl;
    17. }
    18. Animal AgeAdd()
    19. {
    20. this->a_age += 1;
    21. return *this; //a2.AgeAdd().AgeAdd().AgeAdd()
    22. }
    23. };
    24. void test01()
    25. {
    26. Animal a1;
    27. //cout << sizeof(a1) << endl;
    28. a1.a_num = 34;
    29. a1.a_name = "yangtuo";
    30. a1.a_age = 5;
    31. a1.AnimalInfo(); //this ---> a1
    32. Animal a2("sheep");
    33. a2.AnimalInfo(); //this ---> a2
    34. }
    35. int main()
    36. {
    37. test01();
    38. return 0;
    39. }

    (4)this指针: == 指向当前调用成员函数的对象.

    class Person

    {

    int PersonFun(int n)

    {

    this->num = n;

    }

    int num;

    }

    *this指针

    1. #include
    2. using namespace std;
    3. class Animal
    4. {
    5. public:
    6. Animal()
    7. {
    8. a_age=0;
    9. }
    10. Animal(string a_name)
    11. {
    12. this->a_name=a_name;
    13. }
    14. ~Animal()
    15. {
    16. cout<<"析构函数"<
    17. }
    18. int a_age;
    19. int a_num;
    20. string a_name;
    21. void Animallnfo()
    22. {
    23. cout<<"name"<<this->a_name<<"num:"<<this->a_num<
    24. }
    25. Animal &AgeAdd()
    26. {
    27. this->a_age+=1;
    28. return *this; //a2.AgeAdd().AgeAdd().AgeAdd()
    29. }
    30. };
    31. void test01()
    32. {
    33. Animal a1;
    34. a1.a_num=34;
    35. a1.a_name="yanao";
    36. a1.a_age=5;
    37. a1.Animallnfo(); //this------->a1
    38. Animal a2("sheep");
    39. a2.Animallnfo(); //this-------->a2
    40. }
    41. void test02()
    42. {
    43. Animal a3;
    44. a3.AgeAdd().AgeAdd().AgeAdd().AgeAdd();
    45. cout<<"age:"<
    46. }
    47. int main(int argc, char *argv[])
    48. {
    49. test02();
    50. return 0;
    51. }

    (5)this指针的作用:

    1)区分成员属性与参数(属性与参数重名)

    2)成员函数需要返回对象本身时,需要使用this(实现链式编程思想)

  • 相关阅读:
    【Android】-- Activity页面(启动和结束、生命周期、启动模式和实例)
    华为被迫开源,从认知到落地SpringBoot企业级实战手册(完整版)
    第2篇 机器学习基础 —(1)机器学习概念和方式
    奇舞周刊第 505 期:实践指南-前端性能提升 270%!
    C++:STL::String模拟实现
    十三、CANdelaStudio入门-DTC编辑
    【华为OD机试真题 python】叠积木【2022 Q4 | 200分】
    开源软件与知识产权:需要注意什么?
    【pytorch】torch.cuda.is_available() = False 可能的解决办法
    Linux多线程第一讲——线程的创建和基本使用
  • 原文地址:https://blog.csdn.net/qq_63626307/article/details/126715977