• C++day4


    1.思维导图 

     

    2.设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数、拷贝赋值函数。

     

    1. #include
    2. using namespace std;
    3. class Per
    4. {
    5. friend class Stu;
    6. private:
    7. string name;
    8. int age;
    9. int *height;
    10. int *weight;
    11. public:
    12. Per()//per的无参构造函数
    13. {
    14. cout <<"Per的无参构造函数" << endl;
    15. }
    16. Per(string name,int age,int height,int weight):name(name),age(age),height(new int(height)),weight(new int(weight))//Per的有参构造函数,并使用初始化列表初始化
    17. {
    18. cout << "Per的有参构造函数" << endl;
    19. }
    20. Per(const Per &other):name(other.name),age(other.age),height(new int (*other.height)),weight(new int (*other.weight))//Per的拷贝构造函数
    21. {
    22. cout << "Per的拷贝构造函数" << endl;
    23. }
    24. Per &operator=(const Per &other)//Per的拷贝赋值函数
    25. {
    26. if(this!=&other)//避免自己给自己赋值
    27. {
    28. name=other.name;
    29. age=other.age;
    30. height=new int(*(other.height));
    31. weight=new int(*(other.weight));
    32. }
    33. cout << "Per的拷贝赋值函数" <
    34. return *this;
    35. }
    36. ~Per()
    37. {
    38. cout << "Per的析构函数" << endl;
    39. delete weight;
    40. delete height;
    41. weight=nullptr;
    42. height=nullptr;
    43. }
    44. void show()
    45. {
    46. cout << name << " " << age << " " << *height << " " << *weight << endl;
    47. }
    48. };
    49. class Stu
    50. {
    51. private:
    52. double score;
    53. Per p1;
    54. public:
    55. Stu()//Stu的无参构造函数
    56. {
    57. cout << "Stu的无参构造函数" << endl;
    58. }
    59. Stu(double score,Per &p1):score(score),p1(p1)//Stu的有参构造函数
    60. {
    61. cout << "Stu的有参构造函数" << endl;
    62. }
    63. Stu(const Stu &other):score(other.score),p1(other.p1)//Stu的拷贝构造函数
    64. {
    65. cout << "Stu的拷贝构造函数" << endl;
    66. }
    67. Stu &operator=(const Stu &other)//Stu的拷贝赋值函数
    68. {
    69. if(this!=&other)
    70. {
    71. score=other.score;
    72. p1=other.p1;
    73. }
    74. return *this;
    75. cout << "Stu的拷贝赋值函数" << endl;
    76. }
    77. ~Stu()
    78. {
    79. cout << "Stu的析构函数" << endl;
    80. }
    81. void show()
    82. {
    83. cout << score << " " <" " << p1.age << " " << *p1.height << " " << *p1.weight << endl;//这里要访问Per类里private里的成员所以要在Per类增加友元
    84. }
    85. };
    86. int main()
    87. {
    88. Per s1;//调用Per无参构造函数
    89. Per s2("张三",18,180,75);//调用Per有参构造函数
    90. Per s3(s2);//调用Per的拷贝构造函数
    91. Per s4;
    92. s4=s3;//调用Per的拷贝赋值函数
    93. s4.show();
    94. cout << "--------------------------------" << endl;
    95. Stu b1;//stu无参构造函数
    96. Stu b2(100,s2);//stu有参构造函数
    97. Stu b3(b2);//stu拷贝构造函数
    98. Stu b4;
    99. b4=b3;//stu拷贝赋值函数
    100. b4.show();
    101. return 0;
    102. }

     

  • 相关阅读:
    使用电力系统稳定器 (PSS) 和静态 VAR 补偿器 (SVC) 提高瞬态稳定性(Matlab代码实现)
    做推特群发群推“三不要怕”
    如何完美通过token获取用户信息(springboot)
    【杂记】Ubuntu20.04装系统,安装CUDA等
    【Mysql】EXPLAIN
    新型LINUX驱动开发 DTS设备树
    详解基于栈的算法
    Go 反射终极指南:从基础到高级全方位解析
    车载多媒体没法显示歌词的解决办法 —— 修改 LRC 文件的编码格式为 UTF-8
    计算机网络基础 ---- NAT----网络地址转换----详解
  • 原文地址:https://blog.csdn.net/u014137683/article/details/134274046