c++ day3

1.定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。

代码实现:

#include 

using namespace std;

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

    void show()
    {
        cout << "age=" << *age << endl;
        cout << "name=" << 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=" << *score << endl;
        p1.show();
    }
};

int main()
{
    double s = 80.0;
    int a = 23;
    string n = "jsj";

    Stu res(&s, &a, n);
    res.show();

    return 0;
}

效果:

c++ day3_第1张图片

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