【C++的类】

#include 

using namespace std;

class Person
{
    int *age;
    string &name;

public:
    Person(int *age, string &name) : age(age), name(name){}

    void show()
    {

        cout << *age << endl;
        cout << name << endl;
    }
};

class Stu
{
    double *score;
    Person p1;

public:
    Stu(double *score, int *age, string &name) : score(score), p1(age, name){}

    void show()
    {
        cout << *score << endl;
        p1.show();
    }
};

int main()
{
    double scoreValue = 30.0;
    int ageValue = 20;
    string nameValue = "John";

    Stu stu(&scoreValue, &ageValue, nameValue);
    stu.show();

    return 0;
}

【C++的类】_第1张图片

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