C++ day3

思维导图

 C++ day3_第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 << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "high:" << high << endl;
        cout << "weight:" << weight << endl;
    }
    ~Per()
    {
        cout << "Per::析构函数" << endl;
    }
    Per(const Per &other):name(other.name),age(other.age),high(other.high),weight(other.weight)
    {
        cout << "Per::拷贝构造函数" << endl;
    }
    void show()
    {
        cout << "name:" << name << endl;
        cout << "age:" << age << endl;
        cout << "high:" << *high << endl;
        cout << "weight:" << weight << endl;
    }
};
class Stu
{
private:
    double score;
    Per p;
public:
    Stu(double score,string name,int age,double high,double weight):score(score),p(name,age,high,weight)
    {
        cout << "score:" << score << endl;
    }
    ~Stu()
    {
        cout << "Stu::析构函数" << endl;
    }
    Stu(const Stu &other):score(other.score),p(other.p)
    {
        cout << "Stu::拷贝构造函数" << endl;
    }
    void show()
    {
        p.show();
        cout << "score:" << score << endl;
    }
};
int main()
{
    Stu s1(99,"王虎",25,180,140);
    Stu s2=s1;
    s2.show();

    return 0;
}

C++ day3_第2张图片

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