3234242432432432432432

在这里插入图片描述

#include <iostream>

using namespace std;
class Person{
    string name;
    int age;
    int *hight,*weight;
public:
    Person(){
        name="";
        age=0;
        hight=new int(0);
        weight=new int(0);
        cout<<"person无参构造this="<<this<<endl;
    }
    Person(string name,int age,int hight,int weight):name(name),age(age),hight(new int(hight)),weight(new int(weight)){
        cout<<"Person有参构造this"<<this<<endl;
    }
    Person(const Person& other){
        hight=new int;
        weight=new int;
        name=other.name;
        age=other.age;
        *hight=*other.hight;
        *weight=*other.weight;
        cout<<"Person拷贝构造this="<<this<<endl;
    }
    Person &operator=(const Person &other){
        if(this!=&other){
            name=other.name;
            age=other.age;
            *hight=*other.hight;
            *weight=*other.weight;
        }
        cout<<"person拷贝赋值运算符(赋值运算符重载)this="<<this<<endl;
        return *this;
    }
    ~Person(){
        delete hight;
        delete weight;
        hight=nullptr;
        weight=nullptr;
        cout<<"person析构this="<<this<<endl;
    }
    void show(){
        cout<<"name="<<name<<" age="<<age<<endl;
        cout<<"weight="<<*weight<<" height="<<*hight<<endl;
    }
};
class Student{
    Person person;
    double score;
public:
    Student():person(),score(0){
        cout<<"Student无参构造this="<<this<<endl;
    }
    Student(Person person,double score):person(person),score(score){
        cout<<"Perosn有参构造this="<<this<<endl;

    }
    Student(const Student &other):person(other.person),score(0){
        cout<<"Student拷贝构造函数this="<<this<<endl;
    }
    Student &operator=(const Student &other){
        if(this!=&other){
            this->person=other.person;
            this->score=other.score;
        }
        cout<<"Student拷贝赋值运算符(运算符重载)this="<<this<<endl;
        return *this;
    }
    void show(){
        person.show();
        cout<<score<<endl;
    }
    ~Student(){
        cout<<"Student析构this="<<this<<endl;
    }
};

int main()
{
    Person personA;
    Student studentA;
    cout<<"personA="<<&personA<<endl;
    cout<<"studentA="<<&studentA<<endl;
    cout<<"================"<<endl;
    Person personB("张1",1,1,1);
    Student studentB(personB,100);
    cout<<"============="<<endl;
    cout<<"赋值前personA:"<<endl;
    personA.show();
    cout<<"赋值前personB:"<<endl;
    personB.show();
    cout<<"================="<<endl;
    cout<<"赋值后personA:"<<endl;
    personA=personB;
    personA.show();
    cout<<"赋值后personB:"<<endl;
    personB.show();
    cout<<"=================="<<endl;
    cout<<"拷贝构造personC:"<<endl;
    Person personC(personB);
    personC.show();
    cout<<"拷贝构造StuentC:"<<endl;
    Student studentC(studentB);
    studentC.show();
    cout<<"==============="<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值