12、28

#include 

using namespace std;
class Person
{
    int *age;
    string &name;
public:
    Person(int age,string name):age(new int(age)),name(name)//构造
    {

    }
    ~Person()//析构
    {
        delete age;
    }
    Person(const Person &other):age(new int(*(other.age))),name(other.name)//拷贝构造
    {

    }
    Person &operator=(const Person &other)//拷贝赋值
    {
        age=new int(*(other.age));
        name=other.name;
        return *this;
    }
    int get_age();
    string get_name();
};
class Stu
{
    double *score;
    Person p1;
public:
    void show();
    Stu(double score,int age,string name):score(new double(score)),p1(age,name)//构造
    {

    }
    ~Stu()//析构
    {
        delete score;
        p1.~Person();
    }
    Stu(const Stu &other):score(new double(*(other.score))),p1(other.p1)//拷贝构造
    {

    }
    Stu &operator=(const Stu &other)//拷贝赋值
    {
        score=other.score;
        p1=other.p1;
        return *this;
    }

};
int Person::get_age()
{
    return *age;
}
string Person::get_name()
{
    return name;
}
void Stu::show()
{
    cout<

12、28_第1张图片

你可能感兴趣的:(算法)