• day4作业


    设计一个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 *hig;
    10. int *wig;
    11. public:
    12. Per(string name,int age,int* hig,int* wig):name(name),age(age),hig(new int(*hig)),wig(new int(*wig))
    13. {
    14. cout << "有参构造函数" << endl;
    15. }
    16. ~Per()
    17. {
    18. delete hig;
    19. delete wig;
    20. hig = nullptr;
    21. wig = nullptr;
    22. cout << "析构函数" << endl;
    23. }
    24. Per(const Per &other):name(other.name),age(other.age),hig(new int(*(other.hig))),wig(new int(*(other.wig)))
    25. {
    26. cout << "拷贝构造函数" << endl;
    27. }
    28. Per & operator=(const Per &p)
    29. {
    30. name = p.name;
    31. age = p.age;
    32. hig = new int(*(p.hig));
    33. wig = new int(*(p.wig));
    34. cout << "拷贝赋值函数" << endl;
    35. return *this;
    36. }
    37. void show()
    38. {
    39. cout << name << " " << age << " " << *hig << " " << *wig << endl;
    40. }
    41. };
    42. class Stu
    43. {
    44. private:
    45. int soc;
    46. Per p1;
    47. public:
    48. Stu(int soc,Per &p):soc(soc),p1(p)
    49. {
    50. cout << "stu有参构造函数" << endl;
    51. }
    52. ~Stu()
    53. {
    54. cout << "stu析构函数" << endl;
    55. }
    56. Stu(const Stu &ss):soc(ss.soc),p1(ss.p1)
    57. {
    58. cout<< "stu拷贝构造函数" << endl;
    59. }
    60. Stu & operator=(const Stu & ss)
    61. {
    62. soc = ss.soc;
    63. p1 = ss.p1;
    64. cout << "stu拷贝赋值函数" << endl;
    65. return *this;
    66. }
    67. void show()
    68. {
    69. cout << soc << " " << p1.name << " " << p1.age << " " << *(p1.hig) << " " << *(p1.wig) << endl;
    70. }
    71. };
    72. int main()
    73. {
    74. int a = 105;
    75. int b = 188;
    76. Per p("ko",18,&b,&a);//有参构造函数
    77. Per pp(p);//拷贝构造函数
    78. pp = p; //拷贝赋值函数
    79. pp.show();
    80. Stu s1(99,pp);//stu有参构造函数
    81. Stu s2(s1);//stu拷贝构造函数
    82. Stu s3(89,p);
    83. s3 = s1;//stu拷贝赋值函数
    84. s3.show();
    85. return 0;
    86. }

  • 相关阅读:
    力扣、752-打开转盘锁
    Java进阶学习
    STL——vector与迭代器
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    牛客-946B-筱玛爱阅读
    目标检测网络系列——YOLO V2
    相关分析——皮尔森相关系数、t显著性检验及Python实现
    Python 打印文本进度条
    FDM3D打印系列——Blue Mary
    Linux服务器部署java项目
  • 原文地址:https://blog.csdn.net/weixin_71469238/article/details/134274308