
#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;
}