码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++&QT-day3


    1. #include
    2. /*设计一个Per类,类中包含私有成员:姓名、年龄、
    3. * 指针成员身高、体重,再设计一个Stu类,
    4. * 类中包含私有成员:成绩、Per类对象p1,
    5. * 设计这两个类的构造函数、析构函数和拷贝构造函数。*/
    6. using namespace std;
    7. class Per
    8. {
    9. private:
    10. string name; //姓名
    11. int age; //年龄
    12. double *height; //身高
    13. double *weight; //体重
    14. public:
    15. Per():height(new double(1)),weight(new double(1))
    16. {
    17. cout << "无参构造函数" << endl;
    18. }
    19. Per(string name, int age, double h, double w):name(name),age(age),height(new double(h)),weight(new double(w))
    20. {
    21. cout << "有参构造函数" << endl;
    22. }
    23. ~Per() //定义析构函数
    24. {
    25. delete height; //释放指针空间
    26. delete weight;
    27. height = nullptr;
    28. weight = nullptr;
    29. cout << "Per::析构函数:" << this << endl;
    30. }
    31. //Per(const Per &other):name(other.name),age(other.age),height(other.height),weight(other.weight)//浅拷贝构造函数
    32. Per(const Per &other):name(other.name),age(other.age),height(new double(*(other.height))),weight(new double(*(other.weight)))//深拷贝构造函数
    33. {
    34. cout << "拷贝构造函数" << endl;
    35. }
    36. void show()
    37. {
    38. cout << "姓名:" << name << endl;
    39. cout << "年龄:" << age << endl;
    40. cout << "身高:" << *height << endl;
    41. cout << "体重:" << *weight << endl;
    42. }
    43. };
    44. class Stu
    45. {
    46. private:
    47. int score; //成绩
    48. Per p1;
    49. public:
    50. Stu()
    51. {
    52. cout << "无参构造函数" << endl;
    53. }
    54. Stu(int score, Per &p1):score(score),p1(p1)
    55. {
    56. cout << "有参构造函数" << endl;
    57. }
    58. ~Stu()
    59. {
    60. cout << "Stu::析构函数:" << this<< endl;
    61. }
    62. Stu(const Stu &other):score(other.score),p1(other.p1)//拷贝构造函数
    63. {
    64. cout << "拷贝构造函数" << endl;
    65. }
    66. void show()
    67. {
    68. p1.show();
    69. cout << "成绩:" << score << endl;
    70. }
    71. };
    72. int main()
    73. {
    74. Per p1("张三", 18, 180, 150);
    75. Per p2 = p1; //调用拷贝构造函数
    76. //p2.show();
    77. Stu s1; //无参构造函数
    78. Stu s2(98, p1); //有参构造函数
    79. Stu s3(s2); //拷贝构造函数
    80. cout << endl;
    81. s3.show();
    82. cout << endl;
    83. return 0;
    84. }

  • 相关阅读:
    Leecode刷题日记
    consul部署
    uboot启动流程源码分析(二)
    牛血清白蛋白修饰葡萄糖 BSA-glucose,木糖/半乳糖/乳糖偶联牛血清白蛋白
    Unity在安卓Build时报错解决:CommandInvokationFailure和编译器 (1.8.0-adoptopenjdk) 中出现异常错误
    深入理解 JVM 之——字节码指令与执行引擎
    Express 7 指南 - 开发中间件
    SASS常用内置函数
    C语言中的结构体和联合体有什么区别?
    java计算机毕业设计在线学习跟踪系统后台源程序+mysql+系统+lw文档+远程调试
  • 原文地址:https://blog.csdn.net/qq_53195772/article/details/133715238
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号