一、思维导图
二、习题
#include <iostream>
using namespace std;
class Per
{
private:
string name;
int age;
float * high;
float * weight;
public:
//有参构造函数
Per(string n,int a,float h,float w):name(n),age(a),high(new float (h)),weight(new float (w))
{
}
//析构函数
~Per()
{
delete high; //释放堆区空间
delete weight;
}
//拷贝构造函数
Per(const Per &other):name(other.name),age(other.age),high(new float (*(other.high))),weight(new float (*(other.weight)))
{
}
//输出函数
void show()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "身高:" << *high << endl;
cout << "体重:" << *weight << endl;
}
};
class Stu
{
private:
float sorce;
Per p1;
public:
Stu(float s,string name, int age,float high,float weight):sorce(s),p1(name,age,high,weight)
{
cout << "有参构造函数" << endl;
}
//拷贝构造函数
Stu(const Stu &other):sorce(other.sorce),p1(other.p1)
{}
//输出函数
void show()
{
cout << "成绩:" << sorce << endl;
}
Per p2=p1;
//析构函数会自动调用Per的析构函数
};
int main()
{
Stu s1(98,"张三",18,175,65);
s1.show();
s1.p2.show();
return 0;
}