• 10.9作业


    设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。
    1. #include
    2. using namespace std;
    3. class Per{
    4. private:
    5. string name;
    6. int age;
    7. double* height;
    8. double* weight;
    9. public:
    10. Per(string name, int age, double height, double weight);
    11. Per(Per &per);
    12. ~Per();
    13. void show();
    14. };
    15. class Stu{
    16. private:
    17. double score;
    18. Per p;
    19. public:
    20. Stu(double score,string name, int age, double height, double weight);
    21. Stu(Stu &stu);
    22. ~Stu();
    23. void show();
    24. };
    25. Per::Per(string name, int age, double height, double weight)
    26. :name(name),age(age),height(new double(height)),weight(new double(weight)){
    27. cout << "Per::有参构造函数" << endl;
    28. }
    29. Per::Per(Per &per)
    30. :name(per.name),age(per.age),height(new double(*per.height)),weight(new double(*per.weight)){
    31. cout << "Per::拷贝构造函数" << endl;
    32. }
    33. Per::~Per(){
    34. delete height;
    35. delete weight;
    36. cout << "Per::析构函数" << endl;
    37. }
    38. void Per::show(){
    39. cout << "name = " << name << "\tage = " << age << "\theight = " << *height << "\tweight = " << *weight;
    40. }
    41. Stu::Stu(double score,string name, int age, double height, double weight)
    42. :score(score),p(name,age,height,weight){
    43. cout << "Stu::有参构造函数" << endl;
    44. }
    45. Stu::Stu(Stu &stu):score(stu.score),p(stu.p){
    46. cout << "Stu::拷贝构造函数" << endl;
    47. }
    48. Stu::~Stu(){
    49. cout << "Stu::析构函数" << endl;
    50. }
    51. void Stu::show(){
    52. p.show();
    53. cout << "\tscore = " << score << endl;
    54. }
    55. int main()
    56. {
    57. Stu s(95.5,"zhang",18,175,120);
    58. s.show();
    59. Stu s1=s;
    60. s1.show();
    61. return 0;
    62. }

  • 相关阅读:
    2022 年全国职业院校技能大赛高职组云计算赛项-容器云环境搭建
    Jmeter接口测试
    C++算法竞赛常用编程模板总结
    练习-Java类和对象之包的定义
    Redis数据类型-Hash-基本使用
    农业物联网
    Shell 编程基础
    一个C++读取XML的类
    linux驱动_uart
    Numpy基础教程
  • 原文地址:https://blog.csdn.net/weixin_45904815/article/details/133712758