c++作业day3

本文详细介绍了C++中per和stu类的构造函数、析构函数、拷贝构造函数以及类成员的使用,涉及动态内存分配和对象复制。
摘要由CSDN通过智能技术生成

#include <iostream>

using namespace std;

class per
{
    string name;
    int age;
    int *high;
    int *weight;
 public:

    //构造函数
    per():high(new int),weight(new int)
    {
        cout << "调用per无参构造函数" << endl;
    }

    per(int high,int weight)
    {
        this->high = new int(high);
        this->weight = new int(weight);
        cout << "调用per有参构造函数" << endl;

    }

    per(string name,int age,int high,int weight):name(name),age(age)
    {

        this->high = new int(high);
        this->weight = new int(weight);
        cout << "调用per有参构造函数" << endl;

    }

    //拷贝构造函数
    per(per &other)
    {
        this->name = other.name;
        this->age = other.age;
        this->high = new int(*(other.high));
        this->weight = new int(*(other.weight));
        cout << "调用per拷贝构造函数" << endl;
    }

    //析构函数
    ~per()
    {
        delete high;
        delete weight;
        cout << "调用per析构函数" << endl;
    }

    //设置、获取各个类成员
    void set_high(int high)
    {
        *(this->high) = high;
    }

    int get_high()
    {
        return *high;
    }

    void set_weight(int weight)
    {
        *(this->weight) = weight;
    }

    int get_weight()
    {
        return *weight;
    }

    void set_name(string name)
    {
        this->name = name;
    }

    string get_name()
    {
        return name;
    }

    void set_age(int age)
    {
        this->age = age;
    }

    int get_age()
    {
        return age;
    }

    //输出地址
    void showaddr()
    {
        cout << "high = " << high << "\tweight = " << weight << endl;
    }

    //输出打印值
    void show()
    {
        cout << "名字为" << name << "年龄为" << age << "身高为" << *high << "体重为" << *weight << endl;
    }
};


class stu
{
    int score;
    per p1;
public:
    stu():p1(0,0)
    {
        cout << "调用stu无参构造函数" << endl;
    }

    stu(int score,per p1):score(score),p1(p1)
    {
        cout << "调用stu有参构造函数" << endl;
    }
    stu(int score,string name,int age,int high,int weight):score(score),p1(name,age,high,weight)
    {
        cout << "调用stu有参构造函数" << endl;
    }

    stu(stu &other)
    {
        this->score = other.score;
        this->p1.set_high(other.p1.get_high());
        this->p1.set_weight(other.p1.get_weight());
        this->p1.set_name(other.p1.get_name());
        this->p1.set_age(other.p1.get_age());

    }

    ~stu()
    {
        cout << "调用stu析构函数" << endl;
    }

    per get_per()
    {
//        cout << &p1 << endl;
        return p1;
    }

    void show()
    {

        p1.show();
        cout << "成绩为" << score << "\t";
    }

};
int main()
{
    per p1(180,80);
    p1.show();
    p1.showaddr();
    stu s1(10,"zhangsan",18,180,70);
    s1.show();
    s1.get_per().show();
    s1.get_per().showaddr();


    return 0;
}

思维导图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值