day3-C++

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

代码:

#include <iostream>

using namespace std;

class Per
{
private:
    string name;
    int age;
    int *height;
    int *weight;
public:
//无参构造函数
    Per()
    {
        cout << "Per::无参构造函数" << endl;
    }
//有参构造函数
    Per(string name, int age, int height, int weight):name(name),age(age),height(new int (height)),weight(new int (weight))
    {
        cout << "Per::有参构造函数" << endl;
    }
//析构函数
    ~Per()
    {
        cout << "Per::析构函数" << endl;
        delete(height);
        delete(weight);
    }
//拷贝构造函数
    Per(const Per &other):name(other.name),age(other.age),height(new int( *(other.height))),weight(new int( *(other.weight)))
    {
        cout << "Per::拷贝构造函数" << endl;
    }
    void show()
    {
        cout << name << " " << age << " " << *height << " " << *weight << endl;
    }
};

class Stu
{
private:
    double score;
    Per p1;
public:
    Stu()
    {
        cout << "Stu::无参构造函数" << endl;
    }
    Stu(double score, string name, int age, int height, int weight):score(score),p1(name,age,height,weight)
    {
        cout << "Stu::有参构造函数" << endl;
    }
    ~Stu()
    {
        cout << "Stu::析构构造函数" << endl;
    }
    Stu(const Stu &other):score(other.score),p1(other.p1)
    {
        cout << "Stu::拷贝构造函数" << endl;
    }
    void show()
    {
        cout << score << endl;
    }
};

int main()
{
    Per p;
    Per p1("zzh",18,180,70);
    Per p2(p1);

    Stu s;
    Stu s1(99.9,"zzh",18,180,70);
    Stu s2(s1);
    s1.show();
    return 0;
}

结果:

2>思维导图

相关推荐

  1. <span style='color:red;'>c</span>++ <span style='color:red;'>day</span><span style='color:red;'>3</span>

    c++ day3

    2024-03-14 10:54:03      57 阅读
  2. c++day3

    2024-03-14 10:54:03       50 阅读
  3. <span style='color:red;'>C</span>++<span style='color:red;'>Day</span><span style='color:red;'>3</span>

    C++Day3

    2024-03-14 10:54:03      58 阅读
  4. <span style='color:red;'>C</span>++ <span style='color:red;'>day</span><span style='color:red;'>3</span>

    C++ day3

    2024-03-14 10:54:03      60 阅读
  5. <span style='color:red;'>day</span><span style='color:red;'>3</span>-<span style='color:red;'>C</span>++

    day3-C++

    2024-03-14 10:54:03      31 阅读
  6. <span style='color:red;'>c</span>++<span style='color:red;'>day</span><span style='color:red;'>3</span>

    c++day3

    2024-03-14 10:54:03      39 阅读
  7. <span style='color:red;'>C</span>++ <span style='color:red;'>day</span><span style='color:red;'>3</span>

    C++ day3

    2024-03-14 10:54:03      36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-14 10:54:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-14 10:54:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-14 10:54:03       87 阅读
  4. Python语言-面向对象

    2024-03-14 10:54:03       96 阅读

热门阅读

  1. Rockchip android7.1.2 强制第三方apk横屏显示

    2024-03-14 10:54:03       44 阅读
  2. Centos8 使用编译安装nginx

    2024-03-14 10:54:03       39 阅读
  3. ubuntu源码安装nginx

    2024-03-14 10:54:03       41 阅读
  4. ubuntu 20.04 Python pip 配置 pip.conf

    2024-03-14 10:54:03       39 阅读
  5. Python如何使用Redis

    2024-03-14 10:54:03       46 阅读
  6. 人工智能入门学习笔记2:人工智能发展史

    2024-03-14 10:54:03       36 阅读
  7. C语言自学笔记3-----C语言运算符及优先级

    2024-03-14 10:54:03       42 阅读
  8. FDU 2020 | 2.斗牛

    2024-03-14 10:54:03       49 阅读