
#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
int *height;
public:
int *weight;
//无参构造函数
Per()
{
cout << "Per:无参构造函数\n";
}
//有参构造函数
Per(string name,int age,int height,int weight):name(name),age(age),height(new int(height)),weight(new int(weight))
{
cout << "Per:有参构造函数\n";
}
//析构函数
~Per()
{
delete height;
delete weight;
height = nullptr;
weight = nullptr;
cout << "Per:析构函数\n";
}
//拷贝构造函数
Per(const Per &other):name(other.name),age(other.age),height(new int(*other.height)),weight(new int(*other.weight))
{
cout << "Per:拷贝构造函数\n" << endl;
}
void show()
{
cout << "name:" << name << endl;
cout << "age:" << age << endl;
cout << "height:" << *height << endl;
cout << "weight:" << *weight << endl;
}
};
class Stu
{
private:
int score;
Per p1;
public:
//无参构造函数
Stu()
{
cout << "Stu: 无参构造函数\n";
}
//有参构造函数
Stu(int score,string name,int age,int height,int weight):score(score),p1(name,age,height,weight)
{
cout << "Stu: 有参构造函数\n";
}
//析构函数
//拷贝构造函数9
Stu(const Stu &other):score(other.score),p1(other.p1)
{
cout<< "Stu:拷贝构造函数\n";
}
void show()
{
p1.show();
cout << "score:" << score << endl;
}
};
int main()
{
cout << "--------------------s1---------------------\n";
Stu s1;
cout << "--------------------s2--------------------\n";
Stu s2(100,"linlin",19,184,140);
s2.show();
cout << "--------------------s3---------------------\n";
Stu s3=s2;
s3.show();
return 0;
}
