#include <iostream>
using namespace std;
class Per//封装一个Per类
{
private://表示私有属性
string name;//姓名
int age;//年龄
int *height;//身高
double *weight;//体重
public:
//无参构造函数
Per()
{
cout << "Per::无参构造函数" << endl;
}
//有参构造函数
Per(string name,int age,int height,double weight):name(name),age(age),\
height(new int(height)),weight(new double(weight)) //初始化列表
{
cout << "Per::有参构造函数" << endl;
}
//析构函数
~Per()
{
cout << "Per::析构函数" << endl;
delete height;
delete weight;
}
//拷贝构造函数
Per(const Per &other):name(other.name),age(other.age)//初始化列表
{
height=new int(*other.height);//拷贝身高
weight=new double(*other.weight);//拷贝体重
cout << "Per::拷贝构造函数" << endl;
}
//把成员都输出在终端上
void show()
{
cout << "name:" << name << ",";
cout << "age:" << age << ",";
cout << "height:" << *height << ",";
cout << "weight:" << *weight << endl;
}
};
class Stu//封装一个Stu类
{
private:
double score;//成绩
Per p1;//Per类对象p1
public:
//无参构造函数
Stu()
{
cout << "Stu::无参构造函数" << endl;
}
//有参构造函数
Stu(double score,string name,int age, int height,double weight):score(score),p1(name,age,height,weight)
{
cout << "Stu::有参构造函数" << endl;
}
//析构函数
~Stu()
{
cout << "Stu::析构函数" << endl;
}
//拷贝构造函数
Stu(const Stu &other):score(other.score),p1(other.p1)
{
cout << "Stu::拷贝构造函数" << endl;
}
void show()
{
cout << "score:" << ",";
p1.show();
}
};
int main()
{
Per p;//用Per实例化一个对象
Per p2("张三",18,180,85.5);
cout << "p2:" ;
p2.show();
Per p3=p2;
cout << "p3:" ;
p3.show();
Stu s1;//用Stu实例化一个对象
Stu s2(99.99,"李四",19,186,77.7);
cout << "s2:" ;
s2.show();
Stu s3=s2;
cout << "s3:" ;
s3.show();
return 0;
}