1.定义一个Person类,包含私有成员,int *age,string &name,一个Stu类,包含私有成员double *score,Person p1,写出Person类和Stu类的特殊成员函数,并写一个Stu的show函数,显示所有信息。
代码实现:
cpp
#include <iostream>
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;
}
效果: