C++day3

作者 : admin 本文共695个字,预计阅读时间需要2分钟 发布时间: 2024-06-5 共4人阅读

思维导图

C++day3插图

#include 

using namespace std;

class Person{
private:
    string name;
    int age;
    int *heigh;
    int *weight;

public:
    Person(){
        cout << "person::空参构造" << endl;
    }

    Person(string name, int age, int *heigh, int *weight):name(name), age(age), heigh(heigh), weight(weight){
        cout << "person::含参构造" << endl;
    }
    ~Person(){
        delete heigh;
        delete weight;
        cout << "person::析构函数" << endl;
    }
};

class Student{
private:
    Person p;
    double score;

public:
    Student(){
        cout << "student::空参构造" << endl;
    }

    Student(string name, int age, int *heigh, int *weight, double score):p(name, age, heigh, weight), score(score){
        cout << "student::含参构造" << endl;
    }

    ~Student(){
        cout << "student::析构函数" << endl;
    }
};

int main()
{
    int heigh = 180;
    int weight = 70;
    int *h = &heigh;
    int *w = &weight;
    Student s1();
    Student s2("zhangsan", 23, h, w, 99.9);
    return 0;
}

本站无任何商业行为
个人在线分享 » C++day3
E-->