11.30 C++类特殊成员函数

11.30 C++类特殊成员函数_第1张图片

#include 

using namespace std;
class Per
{
private:
    string name;
    int age;
    double *high;
    double weight;
public:
    //构造函数
    Per(string name,int age,double high,double weight):name(name),age(age),high(new double(high)),weight(weight)
    {
        cout << "Per::构造函数" << endl;
    }
    //拷贝构造函数
    Per(const Per &other):name(other.name),age(other.age),high(new double(*(other.high))),weight(other.weight)
    {
        cout << "Per::拷贝构造函数" << endl;
    }
    //析构函数
    ~Per()
    {
        delete high;
        cout << "Per::析构函数" << endl;
    }
};
class Stu
{
private:
    double score;
    Per p1;
public:
    Stu(double score,string name,int age,double high,double weight):score(score),p1(name,age,high,weight)
    {
        cout << "Stu::构造函数" << endl;
    }
    Stu(const Stu &other):score(other.score),p1(other.p1)
    {
        cout << "Stu::拷贝构造函数" << endl;
    }
    ~Stu()
    {
        cout << "Stu::析构函数" << endl;
    }
};
int main()
{
    Per p1("张三",18,185,80);
    Per p2(p1);
    cout << "++++++++++++++++++++++++++++++++++++" << endl;
    Stu s1(99,"李四",20,175,60);
    Stu s2(s1);
    cout << "++++++++++++++++++++++++++++++++++++" << endl;
    return 0;
}

11.30 C++类特殊成员函数_第2张图片

你可能感兴趣的:(c++,开发语言,算法)