C++--DAY3

发布于:2024-06-06 ⋅ 阅读:(198) ⋅ 点赞:(0)

思维导图

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

#include <iostream>

using namespace std;
class Person
{
    string name;
    int age;
    int *high;
    double *weight;
public:
    Person():name("路人甲"),age(18),high(new int(175)),weight(new double(45.00))
    {
        cout << "person的无参构造" << endl;
    }
    Person(string name,int age,int high,double weight):name(name),age(age),high(new int(high)),weight(new double(weight))
    {
        cout << "person的有参构造" << endl;
    }
    ~Person()
    {
        delete high;
        delete weight;
        cout << "person的析构函数" << endl;
    }
    void show()
    {
        cout << " 姓名:" << name << " 年龄:" << age << " 身高:" << *high << " 体重:" << *weight << endl;
    }
};
class Student
{
    float score;
    Person p1;
public:
    Student():score(60.00),p1()
    {
        cout << "students的无参构造" << endl;
    }
    Student(string name,int age,int high,double weight,float score):score(score),p1(name,age,high,weight)
    {
        cout << "students的有参构造" << endl;
    }
    ~Student()
    {
        p1.~Person();
        cout << "student的析构函数" << endl;
    }
    void show()
    {
        cout << "成绩:" << score
                ;
        p1.show();

    }
};

int main()
{
    Person p2;
    p2.show();
    Student s1("浮生",22,185,53.03,86.5);
    s1.show();
    return 0;
}

网站公告

今日签到

点亮在社区的每一天
去签到