码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 2024-3-13-C++day3作业


    1.思维导图

    2.

    要求:

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

    源代码:

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

    效果图:

  • 相关阅读:
    .NET Conf China 2023分享-.NET应用国际化-AIGC智能翻译+代码生成
    一文读懂:低代码和无代码的演进历程、应用范围
    【考研】操作系统——同步互斥问题(P、V操作)2
    java计算机毕业设计Vue潍坊学院宿舍管理系统设计与实现MyBatis+系统+LW文档+源码+调试部署
    web自动化基础
    Vuex 状态管理
    SM4国密4在jdk1.7版本和jdk1.8版本中的工具类使用
    MATLAB数字图像处理 大作业:人脸表情识别
    【2023,学点儿新Java-34】基本数据类型变量 运算规则:自动类型提升、强制类型转换 | 为什么标识符的声明规则里要求不能数字开头?(通俗地讲解——让你豁然开朗!)
    08-流媒体-RTMP拉流
  • 原文地址:https://blog.csdn.net/weixin_59312119/article/details/136690314
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号